changeset 6371:f003a21f905d

* gc-pbkdf2-sha1.c (gc_pbkdf2_sha1): Optimize CEIL computation. Move memory allocation outside of loop.
author Simon Josefsson <simon@josefsson.org>
date Thu, 13 Oct 2005 07:49:05 +0000
parents 924984f3a8f3
children f94a5f07ce6c
files lib/ChangeLog lib/gc-pbkdf2-sha1.c
diffstat 2 files changed, 20 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2005-10-13  Simon Josefsson  <jas@extundo.com>
+
+	* gc-pbkdf2-sha1.c (gc_pbkdf2_sha1): Optimize CEIL computation.
+	Move memory allocation outside of loop.
+
 2005-10-12  Simon Josefsson  <jas@extundo.com>
 
 	* gc-pbkdf2-sha1.c: New file.
--- a/lib/gc-pbkdf2-sha1.c
+++ b/lib/gc-pbkdf2-sha1.c
@@ -68,6 +68,8 @@
   unsigned int i;
   unsigned int k;
   int rc;
+  char *tmp;
+  size_t tmplen = Slen + 4;
 
   if (c == 0)
     return GC_PKCS5_INVALID_ITERATION_COUNT;
@@ -98,9 +100,7 @@
    *        integer greater than, or equal to, x.
    */
 
-  l = dkLen / hLen;
-  if (dkLen % hLen)
-    l++;
+  l = ((dkLen - 1) / hLen) + 1;
   r = dkLen - (l - 1) * hLen;
 
   /*
@@ -145,6 +145,12 @@
    *
    */
 
+  tmp = malloc (tmplen);
+  if (tmp == NULL)
+    return GC_MALLOC_ERROR;
+
+  memcpy (tmp, S, Slen);
+
   for (i = 1; i <= l; i++)
     {
       memset (T, 0, hLen);
@@ -153,28 +159,21 @@
 	{
 	  if (u == 1)
 	    {
-	      char *tmp;
-	      size_t tmplen = Slen + 4;
-
-	      tmp = malloc (tmplen);
-	      if (tmp == NULL)
-		return GC_MALLOC_ERROR;
-
-	      memcpy (tmp, S, Slen);
 	      tmp[Slen + 0] = (i & 0xff000000) >> 24;
 	      tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
 	      tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
 	      tmp[Slen + 3] = (i & 0x000000ff) >> 0;
 
 	      rc = gc_hmac_sha1 (P, Plen, tmp, tmplen, U);
-
-	      free (tmp);
 	    }
 	  else
 	    rc = gc_hmac_sha1 (P, Plen, U, hLen, U);
 
 	  if (rc != GC_OK)
-	    return rc;
+	    {
+	      free (tmp);
+	      return rc;
+	    }
 
 	  for (k = 0; k < hLen; k++)
 	    T[k] ^= U[k];
@@ -183,5 +182,7 @@
       memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
     }
 
+  free (tmp);
+
   return GC_OK;
 }