1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
10 #include "tomcrypt.h"
11
12 #ifdef LTC_CHACHA20POLY1305_MODE
13
14 /**
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
21 */
chacha20poly1305_decrypt(chacha20poly1305_state * st,const unsigned char * in,unsigned long inlen,unsigned char * out)22 int chacha20poly1305_decrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out)
23 {
24 unsigned char padzero[16] = { 0 };
25 unsigned long padlen;
26 int err;
27
28 if (inlen == 0) return CRYPT_OK; /* nothing to do */
29 LTC_ARGCHK(st != NULL);
30
31 if (st->aadflg) {
32 padlen = 16 - (unsigned long)(st->aadlen % 16);
33 if (padlen < 16) {
34 if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err;
35 }
36 st->aadflg = 0; /* no more AAD */
37 }
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;
42 return CRYPT_OK;
43 }
44
45 #endif
46
47 /* ref: HEAD -> master, tag: v1.18.2 */
48 /* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
49 /* commit time: 2018-07-01 22:49:01 +0200 */
50