3
|
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 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 /* EAX Implementation by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef EAX_MODE |
|
16 |
|
17 int eax_decrypt_verify_memory(int cipher, |
|
18 const unsigned char *key, unsigned long keylen, |
|
19 const unsigned char *nonce, unsigned long noncelen, |
|
20 const unsigned char *header, unsigned long headerlen, |
|
21 const unsigned char *ct, unsigned long ctlen, |
|
22 unsigned char *pt, |
|
23 unsigned char *tag, unsigned long taglen, |
|
24 int *res) |
|
25 { |
143
|
26 int err; |
|
27 eax_state *eax; |
|
28 unsigned char *buf; |
|
29 unsigned long buflen; |
3
|
30 |
|
31 _ARGCHK(res != NULL); |
|
32 |
|
33 /* default to zero */ |
|
34 *res = 0; |
|
35 |
143
|
36 /* allocate ram */ |
|
37 buf = XMALLOC(taglen); |
|
38 eax = XMALLOC(sizeof(eax_state)); |
|
39 if (eax == NULL || buf == NULL) { |
|
40 if (eax != NULL) { |
|
41 XFREE(eax); |
|
42 } |
|
43 if (buf != NULL) { |
|
44 XFREE(buf); |
|
45 } |
|
46 return CRYPT_MEM; |
3
|
47 } |
|
48 |
143
|
49 if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) { |
|
50 goto __ERR; |
|
51 } |
|
52 |
|
53 if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) { |
|
54 goto __ERR; |
3
|
55 } |
|
56 |
143
|
57 buflen = taglen; |
|
58 if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) { |
|
59 goto __ERR; |
3
|
60 } |
|
61 |
|
62 /* compare tags */ |
|
63 if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) { |
|
64 *res = 1; |
|
65 } |
143
|
66 |
|
67 err = CRYPT_OK; |
|
68 __ERR: |
3
|
69 #ifdef CLEAN_STACK |
143
|
70 zeromem(buf, taglen); |
|
71 zeromem(eax, sizeof(eax_state)); |
3
|
72 #endif |
143
|
73 |
|
74 XFREE(eax); |
|
75 XFREE(buf); |
|
76 |
|
77 return err; |
3
|
78 } |
|
79 |
|
80 #endif |