]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/eax/eax_done.c
b00bfe08441c450c198b03e03ef2ede9c149f9bd
[zpackage] / libtomcrypt / src / encauth / eax / eax_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
10 /**
11    @file eax_done.c
12    EAX implementation, terminate session, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_EAX_MODE
17
18 /**
19    Terminate an EAX session and get the tag.
20    @param eax       The EAX state
21    @param tag       [out] The destination of the authentication tag
22    @param taglen    [in/out] The max length and resulting length of the authentication tag
23    @return CRYPT_OK if successful
24 */
25 int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)
26 {
27    int           err;
28    unsigned char *headermac, *ctmac;
29    unsigned long x, len;
30
31    LTC_ARGCHK(eax    != NULL);
32    LTC_ARGCHK(tag    != NULL);
33    LTC_ARGCHK(taglen != NULL);
34
35    /* allocate ram */
36    headermac = XMALLOC(MAXBLOCKSIZE);
37    ctmac     = XMALLOC(MAXBLOCKSIZE);
38
39    if (headermac == NULL || ctmac == NULL) {
40       if (headermac != NULL) {
41          XFREE(headermac);
42       }
43       if (ctmac != NULL) {
44          XFREE(ctmac);
45       }
46       return CRYPT_MEM;
47    }
48
49    /* finish ctomac */
50    len = MAXBLOCKSIZE;
51    if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) {
52       goto LBL_ERR;
53    }
54
55    /* finish headeromac */
56
57    /* note we specifically don't reset len so the two lens are minimal */
58
59    if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) {
60       goto LBL_ERR;
61    }
62
63    /* terminate the CTR chain */
64    if ((err = ctr_done(&eax->ctr)) != CRYPT_OK) {
65       goto LBL_ERR;
66    }
67
68    /* compute N xor H xor C */
69    for (x = 0; x < len && x < *taglen; x++) {
70        tag[x] = eax->N[x] ^ headermac[x] ^ ctmac[x];
71    }
72    *taglen = x;
73
74    err = CRYPT_OK;
75 LBL_ERR:
76 #ifdef LTC_CLEAN_STACK
77    zeromem(ctmac,     MAXBLOCKSIZE);
78    zeromem(headermac, MAXBLOCKSIZE);
79    zeromem(eax,       sizeof(*eax));
80 #endif
81
82    XFREE(ctmac);
83    XFREE(headermac);
84
85    return err;
86 }
87
88 #endif
89
90 /* ref:         $Format:%D$ */
91 /* git commit:  $Format:%H$ */
92 /* commit time: $Format:%ai$ */