]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/ccm/ccm_add_aad.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / encauth / ccm / ccm_add_aad.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 #include "tomcrypt.h"
10
11 #ifdef LTC_CCM_MODE
12
13 /**
14   Add AAD to the CCM state
15   @param ccm       The CCM state
16   @param adata     The additional authentication data to add to the CCM state
17   @param adatalen  The length of the AAD data.
18   @return CRYPT_OK on success
19  */
20 int ccm_add_aad(ccm_state *ccm,
21                 const unsigned char *adata,  unsigned long adatalen)
22 {
23    unsigned long y;
24    int            err;
25
26    LTC_ARGCHK(ccm   != NULL);
27    LTC_ARGCHK(adata != NULL);
28
29    if (ccm->aadlen < ccm->current_aadlen + adatalen) {
30       return CRYPT_INVALID_ARG;
31    }
32    ccm->current_aadlen += adatalen;
33
34    /* now add the data */
35    for (y = 0; y < adatalen; y++) {
36       if (ccm->x == 16) {
37          /* full block so let's encrypt it */
38          if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
39             return err;
40          }
41          ccm->x = 0;
42       }
43       ccm->PAD[ccm->x++] ^= adata[y];
44    }
45
46    /* remainder? */
47    if (ccm->aadlen == ccm->current_aadlen) {
48       if (ccm->x != 0) {
49          if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
50             return err;
51          }
52       }
53       ccm->x = 0;
54    }
55
56    return CRYPT_OK;
57 }
58
59 #endif
60
61 /* ref:         $Format:%D$ */
62 /* git commit:  $Format:%H$ */
63 /* commit time: $Format:%ai$ */