comparison eax_decrypt_verify_memory.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 7faae8f46238
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
21 const unsigned char *ct, unsigned long ctlen, 21 const unsigned char *ct, unsigned long ctlen,
22 unsigned char *pt, 22 unsigned char *pt,
23 unsigned char *tag, unsigned long taglen, 23 unsigned char *tag, unsigned long taglen,
24 int *res) 24 int *res)
25 { 25 {
26 int err; 26 int err;
27 eax_state eax; 27 eax_state *eax;
28 unsigned char buf[MAXBLOCKSIZE]; 28 unsigned char *buf;
29 unsigned long buflen; 29 unsigned long buflen;
30 30
31 _ARGCHK(res != NULL); 31 _ARGCHK(res != NULL);
32 32
33 /* default to zero */ 33 /* default to zero */
34 *res = 0; 34 *res = 0;
35 35
36 if ((err = eax_init(&eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) { 36 /* allocate ram */
37 return err; 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;
38 } 47 }
39 48
40 if ((err = eax_decrypt(&eax, ct, pt, ctlen)) != CRYPT_OK) { 49 if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
41 return err; 50 goto __ERR;
51 }
52
53 if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
54 goto __ERR;
42 } 55 }
43 56
44 buflen = MIN(sizeof(buf), taglen); 57 buflen = taglen;
45 if ((err = eax_done(&eax, buf, &buflen)) != CRYPT_OK) { 58 if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
46 return err; 59 goto __ERR;
47 } 60 }
48 61
49 /* compare tags */ 62 /* compare tags */
50 if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) { 63 if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) {
51 *res = 1; 64 *res = 1;
52 } 65 }
66
67 err = CRYPT_OK;
68 __ERR:
69 #ifdef CLEAN_STACK
70 zeromem(buf, taglen);
71 zeromem(eax, sizeof(eax_state));
72 #endif
53 73
54 #ifdef CLEAN_STACK 74 XFREE(eax);
55 zeromem(buf, sizeof(buf)); 75 XFREE(buf);
56 #endif 76
57 return CRYPT_OK; 77 return err;
58 } 78 }
59 79
60 #endif 80 #endif