[git] GCRYPT - branch, master, updated. libgcrypt-1.5.0-278-g68cefd0

by Jussi Kivilinna cvs at cvs.gnupg.org
Mon Sep 30 20:11:16 CEST 2013


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU crypto library".

The branch, master has been updated
       via  68cefd0f1d60ac33b58031df9b1d165cb1bf0f14 (commit)
       via  a96d622e1a36d40d1504b7ada567e90ec9957443 (commit)
       via  10d7351411f19bb2c03d2e24ca5a38dabe45023b (commit)
      from  d2076f27bb7c5d505abf25fc622d21794c4a5df3 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 68cefd0f1d60ac33b58031df9b1d165cb1bf0f14
Author: Jussi Kivilinna <jussi.kivilinna at iki.fi>
Date:   Sat Sep 21 13:54:38 2013 +0300

    Make Whirlpool use the _gcry_md_block_write helper
    
    * cipher/whirlpool.c (whirlpool_context_t): Add 'bctx', remove
    'buffer', 'count' and 'nblocks'.
    (whirlpool_init): Initialize 'bctx'.
    (whirlpool_transform): Adjust context argument type and burn stack
    depth.
    (whirlpool_add): Remove.
    (whirlpool_write): Use _gcry_md_block_write.
    (whirlpool_final, whirlpool_read): Adjust for 'bctx' usage.
    --
    
    Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>

diff --git a/cipher/hash-common.h b/cipher/hash-common.h
index 3caf0a7..ce91da5 100644
--- a/cipher/hash-common.h
+++ b/cipher/hash-common.h
@@ -32,8 +32,8 @@ const char * _gcry_hash_selftest_check_one
 typedef unsigned int (*_gcry_md_block_write_t) (void *c,
 						const unsigned char *buf);
 
-#if defined(HAVE_U64_TYPEDEF) && defined(USE_SHA512)
-/* SHA-512 needs u64 and larger buffer. */
+#if defined(HAVE_U64_TYPEDEF) && (defined(USE_SHA512) || defined(USE_WHIRLPOOL))
+/* SHA-512 needs u64 and larger buffer. Whirlpool needs u64. */
 # define MD_BLOCK_MAX_BLOCKSIZE 128
 # define MD_NBLOCKS_TYPE u64
 #else
diff --git a/cipher/whirlpool.c b/cipher/whirlpool.c
index 6b5f1a9..fa632f9 100644
--- a/cipher/whirlpool.c
+++ b/cipher/whirlpool.c
@@ -38,6 +38,7 @@
 #include "cipher.h"
 
 #include "bufhelp.h"
+#include "hash-common.h"
 
 /* Size of a whirlpool block (in bytes).  */
 #define BLOCK_SIZE 64
@@ -51,10 +52,8 @@
 typedef u64 whirlpool_block_t[BLOCK_SIZE / 8];
 
 typedef struct {
+  gcry_md_block_ctx_t bctx;
   whirlpool_block_t hash_state;
-  unsigned char buffer[BLOCK_SIZE];
-  size_t count;
-  u64 nblocks;
 } whirlpool_context_t;
 
 
@@ -1161,12 +1160,20 @@ static const u64 C7[256] =
 
 
 
+static unsigned int
+whirlpool_transform (void *ctx, const unsigned char *data);
+
+
+
 static void
 whirlpool_init (void *ctx)
 {
   whirlpool_context_t *context = ctx;
 
   memset (context, 0, sizeof (*context));
+
+  context->bctx.blocksize = BLOCK_SIZE;
+  context->bctx.bwrite = whirlpool_transform;
 }
 
 
@@ -1174,8 +1181,9 @@ whirlpool_init (void *ctx)
  * Transform block.
  */
 static unsigned int
-whirlpool_transform (whirlpool_context_t *context, const unsigned char *data)
+whirlpool_transform (void *ctx, const unsigned char *data)
 {
+  whirlpool_context_t *context = ctx;
   whirlpool_block_t data_block;
   whirlpool_block_t key;
   whirlpool_block_t state;
@@ -1269,67 +1277,18 @@ whirlpool_transform (whirlpool_context_t *context, const unsigned char *data)
   block_xor (context->hash_state, state, i);
 
   return /*burn_stack*/ 4 * sizeof(whirlpool_block_t) + 2 * sizeof(int) +
-                        3 * sizeof(void*);
-}
-
-static void
-whirlpool_add (whirlpool_context_t *context,
-	       const void *buffer_arg, size_t buffer_n)
-{
-  const unsigned char *buffer = buffer_arg;
-  unsigned int burn = 0;
-
-  if (context->count == BLOCK_SIZE)
-    {
-      /* Flush the buffer.  */
-      burn = whirlpool_transform (context, context->buffer);
-      _gcry_burn_stack (burn);
-      burn = 0;
-      context->count = 0;
-      context->nblocks++;
-    }
-  if (! buffer)
-    return; /* Nothing to add.  */
-
-  if (context->count)
-    {
-      while (buffer_n && (context->count < BLOCK_SIZE))
-	{
-	  context->buffer[context->count++] = *buffer++;
-	  buffer_n--;
-	}
-      whirlpool_add (context, NULL, 0);
-      /* Can return early now that bit counter calculation is done in final.  */
-      if (!buffer_n)
-        return;
-    }
-
-  while (buffer_n >= BLOCK_SIZE)
-    {
-      burn = whirlpool_transform (context, buffer);
-      context->count = 0;
-      context->nblocks++;
-      buffer_n -= BLOCK_SIZE;
-      buffer += BLOCK_SIZE;
-    }
-  while (buffer_n && (context->count < BLOCK_SIZE))
-    {
-      context->buffer[context->count++] = *buffer++;
-      buffer_n--;
-    }
-
-  _gcry_burn_stack (burn);
+                        4 * sizeof(void*);
 }
 
 static void
 whirlpool_write (void *ctx, const void *buffer, size_t buffer_n)
 {
   whirlpool_context_t *context = ctx;
-  u64 old_nblocks = context->nblocks;
+  u64 old_nblocks = context->bctx.nblocks;
 
-  whirlpool_add (context, buffer, buffer_n);
+  _gcry_md_block_write (context, buffer, buffer_n);
 
-  gcry_assert (old_nblocks <= context->nblocks);
+  gcry_assert (old_nblocks <= context->bctx.nblocks);
 }
 
 static void
@@ -1340,13 +1299,13 @@ whirlpool_final (void *ctx)
   u64 t, lsb, msb;
   unsigned char *length;
 
-  t = context->nblocks;
+  t = context->bctx.nblocks;
   /* multiply by 64 to make a byte count */
   lsb = t << 6;
   msb = t >> 58;
   /* add the count */
   t = lsb;
-  if ((lsb += context->count) < t)
+  if ((lsb += context->bctx.count) < t)
     msb++;
   /* multiply by 8 to make a bit count */
   t = lsb;
@@ -1358,28 +1317,28 @@ whirlpool_final (void *ctx)
   whirlpool_write (context, NULL, 0);
 
   /* Pad.  */
-  context->buffer[context->count++] = 0x80;
+  context->bctx.buf[context->bctx.count++] = 0x80;
 
-  if (context->count > 32)
+  if (context->bctx.count > 32)
     {
       /* An extra block is necessary.  */
-      while (context->count < 64)
-	context->buffer[context->count++] = 0;
+      while (context->bctx.count < 64)
+	context->bctx.buf[context->bctx.count++] = 0;
       whirlpool_write (context, NULL, 0);
     }
-  while (context->count < 32)
-    context->buffer[context->count++] = 0;
+  while (context->bctx.count < 32)
+    context->bctx.buf[context->bctx.count++] = 0;
 
   /* Add length of message.  */
-  length = context->buffer + context->count;
+  length = context->bctx.buf + context->bctx.count;
   buf_put_be64(&length[0 * 8], 0);
   buf_put_be64(&length[1 * 8], 0);
   buf_put_be64(&length[2 * 8], msb);
   buf_put_be64(&length[3 * 8], lsb);
-  context->count += 32;
+  context->bctx.count += 32;
   whirlpool_write (context, NULL, 0);
 
-  block_to_buffer (context->buffer, context->hash_state, i);
+  block_to_buffer (context->bctx.buf, context->hash_state, i);
 }
 
 static byte *
@@ -1387,7 +1346,7 @@ whirlpool_read (void *ctx)
 {
   whirlpool_context_t *context = ctx;
 
-  return context->buffer;
+  return context->bctx.buf;
 }
 
 gcry_md_spec_t _gcry_digest_spec_whirlpool =

commit a96d622e1a36d40d1504b7ada567e90ec9957443
Author: Jussi Kivilinna <jussi.kivilinna at iki.fi>
Date:   Sat Sep 21 13:54:38 2013 +0300

    whirlpool: add stack burning after transform
    
    * cipher/whirlpool.c (whirlpool_transform): Return burn stack depth.
    (whirlpool_add): Do burn_stack.
    --
    
    Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>

diff --git a/cipher/whirlpool.c b/cipher/whirlpool.c
index 1ee8916..6b5f1a9 100644
--- a/cipher/whirlpool.c
+++ b/cipher/whirlpool.c
@@ -1173,7 +1173,7 @@ whirlpool_init (void *ctx)
 /*
  * Transform block.
  */
-static void
+static unsigned int
 whirlpool_transform (whirlpool_context_t *context, const unsigned char *data)
 {
   whirlpool_block_t data_block;
@@ -1267,6 +1267,9 @@ whirlpool_transform (whirlpool_context_t *context, const unsigned char *data)
 
   block_xor (context->hash_state, data_block, i);
   block_xor (context->hash_state, state, i);
+
+  return /*burn_stack*/ 4 * sizeof(whirlpool_block_t) + 2 * sizeof(int) +
+                        3 * sizeof(void*);
 }
 
 static void
@@ -1274,12 +1277,14 @@ whirlpool_add (whirlpool_context_t *context,
 	       const void *buffer_arg, size_t buffer_n)
 {
   const unsigned char *buffer = buffer_arg;
+  unsigned int burn = 0;
 
   if (context->count == BLOCK_SIZE)
     {
       /* Flush the buffer.  */
-      whirlpool_transform (context, context->buffer);
-      /*_gcry_burn_stack (80+6*sizeof(void*));*/ /* FIXME */
+      burn = whirlpool_transform (context, context->buffer);
+      _gcry_burn_stack (burn);
+      burn = 0;
       context->count = 0;
       context->nblocks++;
     }
@@ -1298,11 +1303,10 @@ whirlpool_add (whirlpool_context_t *context,
       if (!buffer_n)
         return;
     }
-  /*_gcry_burn_stack (80+6*sizeof(void*));*/ /* FIXME */
 
   while (buffer_n >= BLOCK_SIZE)
     {
-      whirlpool_transform (context, buffer);
+      burn = whirlpool_transform (context, buffer);
       context->count = 0;
       context->nblocks++;
       buffer_n -= BLOCK_SIZE;
@@ -1313,6 +1317,8 @@ whirlpool_add (whirlpool_context_t *context,
       context->buffer[context->count++] = *buffer++;
       buffer_n--;
     }
+
+  _gcry_burn_stack (burn);
 }
 
 static void

commit 10d7351411f19bb2c03d2e24ca5a38dabe45023b
Author: Jussi Kivilinna <jussi.kivilinna at iki.fi>
Date:   Sat Sep 21 13:54:38 2013 +0300

    whirlpool: do bitcount calculation in finalization part
    
    * cipher/whirlpool.c (whirlpool_context_t): Remove 'length', add
    'nblocks'.
    (whirlpool_add): Update 'nblocks' instead of 'length', and add early
    return at one spot.
    (whirlpool_write): Check for 'nblocks' overflow.
    (whirlpool_final): Convert 'nblocks' to bit-counter, and use
    whirlpool_write instead of whirlpool_add.
    --
    
    Currently Whirlpool uses large 256 bit counter that is increased in the
    'write' function. However, we could to bit counter calculation as is
    done in all the rest hash algorithms; use 64-bit block counter that is
    converted to bit counter in finalization function. This change does
    limit amount of bytes Whirlpool can process before overflowing bit counter.
    With 256-bit counter, overflow happens after ~1.3e67 gigabytes. With 64-bit
    block counter, overflow happens just after ~1.1e12 gigabytes. Patch keeps
    the old behaviour of halting if counter overflows.
    
    Main benefit for this patch is that after this change, we can use the
    _gcry_md_block_write helper for Whirlpool too.
    
    Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>

diff --git a/cipher/whirlpool.c b/cipher/whirlpool.c
index 954640a..1ee8916 100644
--- a/cipher/whirlpool.c
+++ b/cipher/whirlpool.c
@@ -54,7 +54,7 @@ typedef struct {
   whirlpool_block_t hash_state;
   unsigned char buffer[BLOCK_SIZE];
   size_t count;
-  unsigned char length[32];
+  u64 nblocks;
 } whirlpool_context_t;
 
 
@@ -1274,11 +1274,6 @@ whirlpool_add (whirlpool_context_t *context,
 	       const void *buffer_arg, size_t buffer_n)
 {
   const unsigned char *buffer = buffer_arg;
-  u64 buffer_size;
-  unsigned int carry;
-  unsigned int i;
-
-  buffer_size = buffer_n;
 
   if (context->count == BLOCK_SIZE)
     {
@@ -1286,6 +1281,7 @@ whirlpool_add (whirlpool_context_t *context,
       whirlpool_transform (context, context->buffer);
       /*_gcry_burn_stack (80+6*sizeof(void*));*/ /* FIXME */
       context->count = 0;
+      context->nblocks++;
     }
   if (! buffer)
     return; /* Nothing to add.  */
@@ -1298,6 +1294,9 @@ whirlpool_add (whirlpool_context_t *context,
 	  buffer_n--;
 	}
       whirlpool_add (context, NULL, 0);
+      /* Can return early now that bit counter calculation is done in final.  */
+      if (!buffer_n)
+        return;
     }
   /*_gcry_burn_stack (80+6*sizeof(void*));*/ /* FIXME */
 
@@ -1305,6 +1304,7 @@ whirlpool_add (whirlpool_context_t *context,
     {
       whirlpool_transform (context, buffer);
       context->count = 0;
+      context->nblocks++;
       buffer_n -= BLOCK_SIZE;
       buffer += BLOCK_SIZE;
     }
@@ -1313,29 +1313,17 @@ whirlpool_add (whirlpool_context_t *context,
       context->buffer[context->count++] = *buffer++;
       buffer_n--;
     }
-
-  /* Update bit counter.  */
-  carry = 0;
-  buffer_size <<= 3;
-  for (i = 1; i <= 32; i++)
-    {
-      if (! (buffer_size || carry))
-	break;
-
-      carry += context->length[32 - i] + (buffer_size & 0xFF);
-      context->length[32 - i] = carry;
-      buffer_size >>= 8;
-      carry >>= 8;
-    }
-  gcry_assert (! (buffer_size || carry));
 }
 
 static void
 whirlpool_write (void *ctx, const void *buffer, size_t buffer_n)
 {
   whirlpool_context_t *context = ctx;
+  u64 old_nblocks = context->nblocks;
 
   whirlpool_add (context, buffer, buffer_n);
+
+  gcry_assert (old_nblocks <= context->nblocks);
 }
 
 static void
@@ -1343,9 +1331,25 @@ whirlpool_final (void *ctx)
 {
   whirlpool_context_t *context = ctx;
   unsigned int i;
+  u64 t, lsb, msb;
+  unsigned char *length;
+
+  t = context->nblocks;
+  /* multiply by 64 to make a byte count */
+  lsb = t << 6;
+  msb = t >> 58;
+  /* add the count */
+  t = lsb;
+  if ((lsb += context->count) < t)
+    msb++;
+  /* multiply by 8 to make a bit count */
+  t = lsb;
+  lsb <<= 3;
+  msb <<= 3;
+  msb |= t >> 61;
 
   /* Flush.  */
-  whirlpool_add (context, NULL, 0);
+  whirlpool_write (context, NULL, 0);
 
   /* Pad.  */
   context->buffer[context->count++] = 0x80;
@@ -1355,15 +1359,19 @@ whirlpool_final (void *ctx)
       /* An extra block is necessary.  */
       while (context->count < 64)
 	context->buffer[context->count++] = 0;
-      whirlpool_add (context, NULL, 0);
+      whirlpool_write (context, NULL, 0);
     }
   while (context->count < 32)
     context->buffer[context->count++] = 0;
 
   /* Add length of message.  */
-  memcpy (context->buffer + context->count, context->length, 32);
+  length = context->buffer + context->count;
+  buf_put_be64(&length[0 * 8], 0);
+  buf_put_be64(&length[1 * 8], 0);
+  buf_put_be64(&length[2 * 8], msb);
+  buf_put_be64(&length[3 * 8], lsb);
   context->count += 32;
-  whirlpool_add (context, NULL, 0);
+  whirlpool_write (context, NULL, 0);
 
   block_to_buffer (context->buffer, context->hash_state, i);
 }

-----------------------------------------------------------------------

Summary of changes:
 cipher/hash-common.h |    4 +-
 cipher/whirlpool.c   |  131 ++++++++++++++++++++------------------------------
 2 files changed, 54 insertions(+), 81 deletions(-)


hooks/post-receive
-- 
The GNU crypto library
http://git.gnupg.org


_______________________________________________
Gnupg-commits mailing list
Gnupg-commits at gnupg.org
http://lists.gnupg.org/mailman/listinfo/gnupg-commits




More information about the Gcrypt-devel mailing list