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 Encrypt bytes of ciphertext with ChaCha20Poly1305
16 @param st The ChaCha20Poly1305 state
17 @param in The plaintext
18 @param inlen The length of the input (octets)
19 @param out [out] The ciphertext (length inlen)
20 @return CRYPT_OK if successful
22 int chacha20poly1305_encrypt(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);
31 if ((err = chacha_crypt(&st->chacha, in, inlen, out)) != CRYPT_OK) return err;
33 padlen = 16 - (unsigned long)(st->aadlen % 16);
35 if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err;
37 st->aadflg = 0; /* no more AAD */
39 if ((err = poly1305_process(&st->poly, out, inlen)) != CRYPT_OK) return err;
40 st->ctlen += (ulong64)inlen;
46 /* ref: $Format:%D$ */
47 /* git commit: $Format:%H$ */
48 /* commit time: $Format:%ai$ */