diff ocb_encrypt_authenticate_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/ocb_encrypt_authenticate_memory.c	Tue Jun 15 14:07:21 2004 +0000
+++ b/ocb_encrypt_authenticate_memory.c	Sun Dec 19 11:34:45 2004 +0000
@@ -22,7 +22,7 @@
           unsigned char *tag,    unsigned long *taglen)
 {
    int err;
-   ocb_state ocb;
+   ocb_state *ocb;
 
    _ARGCHK(key    != NULL);
    _ARGCHK(nonce  != NULL);
@@ -31,20 +31,34 @@
    _ARGCHK(tag    != NULL);
    _ARGCHK(taglen != NULL);
 
-   if ((err = ocb_init(&ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
-      return err;
+   /* allocate ram */
+   ocb = XMALLOC(sizeof(ocb_state));
+   if (ocb == NULL) {
+      return CRYPT_MEM;
+   }
+
+   if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
+      goto __ERR;
    }
 
-   while (ptlen > (unsigned long)ocb.block_len) {
-        if ((err = ocb_encrypt(&ocb, pt, ct)) != CRYPT_OK) {
-           return err;
+   while (ptlen > (unsigned long)ocb->block_len) {
+        if ((err = ocb_encrypt(ocb, pt, ct)) != CRYPT_OK) {
+           goto __ERR;
         }
-        ptlen   -= ocb.block_len;
-        pt      += ocb.block_len;
-        ct      += ocb.block_len;
+        ptlen   -= ocb->block_len;
+        pt      += ocb->block_len;
+        ct      += ocb->block_len;
    }
 
-   return ocb_done_encrypt(&ocb, pt, ptlen, ct, tag, taglen);
+   err = ocb_done_encrypt(ocb, pt, ptlen, ct, tag, taglen);
+__ERR:
+#ifdef CLEAN_STACK
+   zeromem(ocb, sizeof(ocb_state));
+#endif
+
+   XFREE(ocb);
+
+   return err;
 }
 
 #endif