]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/ccm/ccm_done.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / encauth / ccm / ccm_done.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   Terminate a CCM stream
15   @param ccm     The CCM state
16   @param tag     [out] The destination for the MAC tag
17   @param taglen  [in/out]  The length of the MAC tag
18   @return CRYPT_OK on success
19  */
20 int ccm_done(ccm_state *ccm,
21              unsigned char *tag,    unsigned long *taglen)
22 {
23    unsigned long x, y;
24    int            err;
25
26    LTC_ARGCHK(ccm != NULL);
27
28    /* Check all data have been processed */
29    if (ccm->ptlen != ccm->current_ptlen) {
30       return CRYPT_ERROR;
31    }
32
33    LTC_ARGCHK(tag    != NULL);
34    LTC_ARGCHK(taglen != NULL);
35
36    if (ccm->x != 0) {
37       if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
38          return err;
39       }
40    }
41
42    /* setup CTR for the TAG (zero the count) */
43    for (y = 15; y > 15 - ccm->L; y--) {
44       ccm->ctr[y] = 0x00;
45    }
46    if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
47       return err;
48    }
49
50    cipher_descriptor[ccm->cipher].done(&ccm->K);
51
52    /* store the TAG */
53    for (x = 0; x < 16 && x < *taglen; x++) {
54       tag[x] = ccm->PAD[x] ^ ccm->CTRPAD[x];
55    }
56    *taglen = x;
57
58    return CRYPT_OK;
59 }
60
61 #endif
62
63 /* ref:         $Format:%D$ */
64 /* git commit:  $Format:%H$ */
65 /* commit time: $Format:%ai$ */