diff 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
line wrap: on
line diff
--- a/eax_decrypt_verify_memory.c	Tue Jun 15 14:07:21 2004 +0000
+++ b/eax_decrypt_verify_memory.c	Sun Dec 19 11:34:45 2004 +0000
@@ -23,38 +23,58 @@
           unsigned char *tag,    unsigned long taglen,
           int           *res)
 {
-   int err;
-   eax_state eax;
-   unsigned char buf[MAXBLOCKSIZE];
-   unsigned long buflen;
+   int            err;
+   eax_state     *eax;
+   unsigned char *buf;
+   unsigned long  buflen;
 
    _ARGCHK(res != NULL);
 
    /* default to zero */
    *res = 0;
 
-   if ((err = eax_init(&eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
-      return err;
+   /* allocate ram */
+   buf = XMALLOC(taglen);
+   eax = XMALLOC(sizeof(eax_state));
+   if (eax == NULL || buf == NULL) {
+      if (eax != NULL) {
+         XFREE(eax);
+      }
+      if (buf != NULL) {
+         XFREE(buf);
+      }
+      return CRYPT_MEM;
    }
 
-   if ((err = eax_decrypt(&eax, ct, pt, ctlen)) != CRYPT_OK) {
-      return err;
+   if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
+      goto __ERR;
+   }
+
+   if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
+      goto __ERR;
    }
  
-   buflen = MIN(sizeof(buf), taglen);
-   if ((err = eax_done(&eax, buf, &buflen)) != CRYPT_OK) {
-      return err;
+   buflen = taglen;
+   if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
+      goto __ERR;
    }
 
    /* compare tags */
    if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) {
       *res = 1;
    }
-
+   
+   err = CRYPT_OK;
+__ERR:
 #ifdef CLEAN_STACK
-   zeromem(buf, sizeof(buf));
+   zeromem(buf, taglen);
+   zeromem(eax, sizeof(eax_state));
 #endif
-   return CRYPT_OK;
+
+   XFREE(eax);
+   XFREE(buf);
+
+   return err;
 }
 
 #endif