1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 #ifdef LTC_CHACHA20POLY1305_MODE
15 Process an entire GCM packet in one call.
16 @param key The secret key
17 @param keylen The length of the secret key
18 @param iv The initialization vector
19 @param ivlen The length of the initialization vector
20 @param aad The additional authentication data (header)
21 @param aadlen The length of the aad
22 @param in The plaintext
23 @param inlen The length of the plaintext (ciphertext length is the same)
24 @param out The ciphertext
25 @param tag [out] The MAC tag
26 @param taglen [in/out] The MAC tag length
27 @param direction Encrypt or Decrypt mode (CHACHA20POLY1305_ENCRYPT or CHACHA20POLY1305_DECRYPT)
28 @return CRYPT_OK on success
30 int chacha20poly1305_memory(const unsigned char *key, unsigned long keylen,
31 const unsigned char *iv, unsigned long ivlen,
32 const unsigned char *aad, unsigned long aadlen,
33 const unsigned char *in, unsigned long inlen,
35 unsigned char *tag, unsigned long *taglen,
38 chacha20poly1305_state st;
41 LTC_ARGCHK(key != NULL);
42 LTC_ARGCHK(iv != NULL);
43 LTC_ARGCHK(in != NULL);
44 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(tag != NULL);
47 if ((err = chacha20poly1305_init(&st, key, keylen)) != CRYPT_OK) { goto LBL_ERR; }
48 if ((err = chacha20poly1305_setiv(&st, iv, ivlen)) != CRYPT_OK) { goto LBL_ERR; }
49 if (aad && aadlen > 0) {
50 if ((err = chacha20poly1305_add_aad(&st, aad, aadlen)) != CRYPT_OK) { goto LBL_ERR; }
52 if (direction == CHACHA20POLY1305_ENCRYPT) {
53 if ((err = chacha20poly1305_encrypt(&st, in, inlen, out)) != CRYPT_OK) { goto LBL_ERR; }
55 else if (direction == CHACHA20POLY1305_DECRYPT) {
56 if ((err = chacha20poly1305_decrypt(&st, in, inlen, out)) != CRYPT_OK) { goto LBL_ERR; }
59 err = CRYPT_INVALID_ARG;
62 err = chacha20poly1305_done(&st, tag, taglen);
64 #ifdef LTC_CLEAN_STACK
65 zeromem(&st, sizeof(chacha20poly1305_state));
72 /* ref: $Format:%D$ */
73 /* git commit: $Format:%H$ */
74 /* commit time: $Format:%ai$ */