]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/eax/eax_decrypt_verify_memory.c
8c6540fe36fa868421dfc5182ae74b95885864bc
[zpackage] / libtomcrypt / src / encauth / eax / eax_decrypt_verify_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 /**
11     @file eax_decrypt_verify_memory.c
12     EAX implementation, decrypt block of memory, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_EAX_MODE
17
18 /**
19    Decrypt a block of memory and verify the provided MAC tag with EAX
20    @param cipher     The index of the cipher desired
21    @param key        The secret key
22    @param keylen     The length of the key (octets)
23    @param nonce      The nonce data (use once) for the session
24    @param noncelen   The length of the nonce data.
25    @param header     The session header data
26    @param headerlen  The length of the header (octets)
27    @param ct         The ciphertext
28    @param ctlen      The length of the ciphertext (octets)
29    @param pt         [out] The plaintext
30    @param tag        The authentication tag provided by the encoder
31    @param taglen     [in/out] The length of the tag (octets)
32    @param stat       [out] The result of the decryption (1==valid tag, 0==invalid)
33    @return CRYPT_OK if successful regardless of the resulting tag comparison
34 */
35 int eax_decrypt_verify_memory(int cipher,
36     const unsigned char *key,    unsigned long keylen,
37     const unsigned char *nonce,  unsigned long noncelen,
38     const unsigned char *header, unsigned long headerlen,
39     const unsigned char *ct,     unsigned long ctlen,
40           unsigned char *pt,
41           unsigned char *tag,    unsigned long taglen,
42           int           *stat)
43 {
44    int            err;
45    eax_state     *eax;
46    unsigned char *buf;
47    unsigned long  buflen;
48
49    LTC_ARGCHK(stat != NULL);
50    LTC_ARGCHK(key  != NULL);
51    LTC_ARGCHK(pt   != NULL);
52    LTC_ARGCHK(ct   != NULL);
53    LTC_ARGCHK(tag  != NULL);
54
55    /* default to zero */
56    *stat = 0;
57
58    /* limit taglen */
59    taglen = MIN(taglen, MAXBLOCKSIZE);
60
61    /* allocate ram */
62    buf = XMALLOC(taglen);
63    eax = XMALLOC(sizeof(*eax));
64    if (eax == NULL || buf == NULL) {
65       if (eax != NULL) {
66          XFREE(eax);
67       }
68       if (buf != NULL) {
69          XFREE(buf);
70       }
71       return CRYPT_MEM;
72    }
73
74    if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
75       goto LBL_ERR;
76    }
77
78    if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
79       goto LBL_ERR;
80    }
81
82    buflen = taglen;
83    if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
84       goto LBL_ERR;
85    }
86
87    /* compare tags */
88    if (buflen >= taglen && XMEM_NEQ(buf, tag, taglen) == 0) {
89       *stat = 1;
90    }
91
92    err = CRYPT_OK;
93 LBL_ERR:
94 #ifdef LTC_CLEAN_STACK
95    zeromem(buf, taglen);
96    zeromem(eax, sizeof(*eax));
97 #endif
98
99    XFREE(eax);
100    XFREE(buf);
101
102    return err;
103 }
104
105 #endif
106
107 /* ref:         $Format:%D$ */
108 /* git commit:  $Format:%H$ */
109 /* commit time: $Format:%ai$ */