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 Decrypt bytes of ciphertext with ChaCha20Poly1305
16 @param st The ChaCha20Poly1305 state
17 @param in The ciphertext
18 @param inlen The length of the input (octets)
19 @param out [out] The plaintext (length inlen)
20 @return CRYPT_OK if successful
22 int chacha20poly1305_decrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out)
24 unsigned char padzero[16] = { 0 };
28 if (inlen == 0) return CRYPT_OK; /* nothing to do */
29 LTC_ARGCHK(st != NULL);
32 padlen = 16 - (unsigned long)(st->aadlen % 16);
34 if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err;
36 st->aadflg = 0; /* no more AAD */
38 if (st->aadflg) st->aadflg = 0; /* no more AAD */
39 if ((err = poly1305_process(&st->poly, in, inlen)) != CRYPT_OK) return err;
40 if ((err = chacha_crypt(&st->chacha, in, inlen, out)) != CRYPT_OK) return err;
41 st->ctlen += (ulong64)inlen;
47 /* ref: $Format:%D$ */
48 /* git commit: $Format:%H$ */
49 /* commit time: $Format:%ai$ */