]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/chachapoly/chacha20poly1305_encrypt.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / encauth / chachapoly / chacha20poly1305_encrypt.c
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    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
21 */
22 int chacha20poly1305_encrypt(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 ((err = chacha_crypt(&st->chacha, in, inlen, out)) != CRYPT_OK)         return err;
32    if (st->aadflg) {
33       padlen = 16 - (unsigned long)(st->aadlen % 16);
34       if (padlen < 16) {
35         if ((err = poly1305_process(&st->poly, padzero, padlen)) != CRYPT_OK) return err;
36       }
37       st->aadflg = 0; /* no more AAD */
38    }
39    if ((err = poly1305_process(&st->poly, out, inlen)) != CRYPT_OK)           return err;
40    st->ctlen += (ulong64)inlen;
41    return CRYPT_OK;
42 }
43
44 #endif
45
46 /* ref:         $Format:%D$ */
47 /* git commit:  $Format:%H$ */
48 /* commit time: $Format:%ai$ */