diff pkcs_1_mgf1.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/pkcs_1_mgf1.c	Tue Jun 15 14:07:21 2004 +0000
+++ b/pkcs_1_mgf1.c	Sun Dec 19 11:34:45 2004 +0000
@@ -20,8 +20,8 @@
 {
    unsigned long hLen, counter, x;
    int           err;
-   hash_state    md;
-   unsigned char buf[MAXBLOCKSIZE];
+   hash_state    *md;
+   unsigned char *buf;
  
    _ARGCHK(seed != NULL);
    _ARGCHK(mask != NULL);
@@ -34,6 +34,19 @@
    /* get hash output size */
    hLen = hash_descriptor[hash_idx].hashsize;
 
+   /* allocate memory */
+   md  = XMALLOC(sizeof(hash_state));
+   buf = XMALLOC(hLen);
+   if (md == NULL || buf == NULL) {
+      if (md != NULL) {
+         XFREE(md);
+      }
+      if (buf != NULL) {
+         XFREE(buf);
+      }
+      return CRYPT_MEM;
+   }
+
    /* start counter */
    counter = 0;
 
@@ -43,15 +56,17 @@
        ++counter;
 
        /* get hash of seed || counter */
-       hash_descriptor[hash_idx].init(&md);
-       if ((err = hash_descriptor[hash_idx].process(&md, seed, seedlen)) != CRYPT_OK) {
-          return err;
+       if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
+          goto __ERR;
+       }
+       if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {
+          goto __ERR;
        }
-       if ((err = hash_descriptor[hash_idx].process(&md, buf, 4)) != CRYPT_OK) {
-          return err;
+       if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {
+          goto __ERR;
        }
-       if ((err = hash_descriptor[hash_idx].done(&md, buf)) != CRYPT_OK) {
-          return err;
+       if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
+          goto __ERR;
        }
 
        /* store it */
@@ -60,7 +75,17 @@
        }
    }
 
-   return CRYPT_OK;
+   err = CRYPT_OK;
+__ERR:
+#ifdef CLEAN_STACK
+   zeromem(buf, hLen);
+   zeromem(md,  sizeof(hash_state));
+#endif
+
+   XFREE(buf);
+   XFREE(md);
+
+   return err;
 }
 
 #endif /* PKCS_1 */