diff pkcs_5_1.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_5_1.c	Tue Jun 15 14:07:21 2004 +0000
+++ b/pkcs_5_1.c	Sun Dec 19 11:34:45 2004 +0000
@@ -20,8 +20,8 @@
 {
    int err;
    unsigned long x;
-   hash_state md;
-   unsigned char buf[MAXBLOCKSIZE];
+   hash_state    *md;
+   unsigned char *buf;
 
    _ARGCHK(password != NULL);
    _ARGCHK(salt     != NULL);
@@ -33,17 +33,38 @@
       return err;
    }
 
+   /* allocate memory */
+   md  = XMALLOC(sizeof(hash_state));
+   buf = XMALLOC(MAXBLOCKSIZE);
+   if (md == NULL || buf == NULL) {
+      if (md != NULL) {
+         XFREE(md);
+      }
+      if (buf != NULL) { 
+         XFREE(buf);
+      }
+      return CRYPT_MEM;
+   }        
+
    /* hash initial password + salt */
-   hash_descriptor[hash_idx].init(&md);
-   hash_descriptor[hash_idx].process(&md, password, password_len);
-   hash_descriptor[hash_idx].process(&md, salt, 8);
-   hash_descriptor[hash_idx].done(&md, buf);
+   if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
+       goto __ERR;
+   }
+   if ((err = hash_descriptor[hash_idx].process(md, password, password_len)) != CRYPT_OK) {
+       goto __ERR;
+   }
+   if ((err = hash_descriptor[hash_idx].process(md, salt, 8)) != CRYPT_OK) {
+       goto __ERR;
+   }
+   if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
+       goto __ERR;
+   }
 
    while (--iteration_count) {
       // code goes here.
-      x = sizeof(buf);
+      x = MAXBLOCKSIZE;
       if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) {
-         return err;
+         goto __ERR;
       }
    }
 
@@ -52,12 +73,17 @@
        out[x] = buf[x];
    }
    *outlen = x;
-
+   err = CRYPT_OK;
+__ERR:
 #ifdef CLEAN_STACK 
-   zeromem(buf, sizeof(buf));
+   zeromem(buf, MAXBLOCKSIZE);
+   zeromem(md, sizeof(hash_state));
 #endif
 
-   return CRYPT_OK;
+   XFREE(buf);
+   XFREE(md);
+
+   return err;
 }
 
 #endif