]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/chachapoly/chacha20poly1305_memory.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / encauth / chachapoly / chacha20poly1305_memory.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   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
29  */
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,
34                                   unsigned char *out,
35                                   unsigned char *tag, unsigned long *taglen,
36                             int direction)
37 {
38    chacha20poly1305_state st;
39    int err;
40
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);
46
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; }
51    }
52    if (direction == CHACHA20POLY1305_ENCRYPT) {
53       if ((err = chacha20poly1305_encrypt(&st, in, inlen, out)) != CRYPT_OK) { goto LBL_ERR; }
54    }
55    else if (direction == CHACHA20POLY1305_DECRYPT) {
56       if ((err = chacha20poly1305_decrypt(&st, in, inlen, out)) != CRYPT_OK) { goto LBL_ERR; }
57    }
58    else {
59       err = CRYPT_INVALID_ARG;
60       goto LBL_ERR;
61    }
62    err = chacha20poly1305_done(&st, tag, taglen);
63 LBL_ERR:
64 #ifdef LTC_CLEAN_STACK
65    zeromem(&st, sizeof(chacha20poly1305_state));
66 #endif
67    return err;
68 }
69
70 #endif
71
72 /* ref:         $Format:%D$ */
73 /* git commit:  $Format:%H$ */
74 /* commit time: $Format:%ai$ */