[git] GCRYPT - branch, wk-sha1-playground, created. post-nuke-of-trailing-ws-14-gd61553b

by Werner Koch cvs at cvs.gnupg.org
Fri Sep 16 10:55:20 CEST 2011


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, wk-sha1-playground has been created
        at  d61553b54a71f4b16b9cdea25700142b917a3e73 (commit)

- Log -----------------------------------------------------------------
commit d61553b54a71f4b16b9cdea25700142b917a3e73
Author: Werner Koch <wk at gnupg.org>
Date:   Mon Feb 21 10:07:46 2011 +0100

    Try out 16 byte alignment for SHA-1

diff --git a/cipher/md.c b/cipher/md.c
index 053eab1..5321458 100644
--- a/cipher/md.c
+++ b/cipher/md.c
@@ -1,6 +1,6 @@
 /* md.c  -  message digest dispatcher
  * Copyright (C) 1998, 1999, 2002, 2003, 2006,
- *               2008 Free Software Foundation, Inc.
+ *               2008, 2011 Free Software Foundation, Inc.
  *
  * This file is part of Libgcrypt.
  *
@@ -30,6 +30,7 @@
 
 #include "rmd.h"
 
+
 /* A dummy extraspec so that we do not need to tests the extraspec
    field from the module specification against NULL and instead
    directly test the respective fields of extraspecs.  */
@@ -130,6 +131,7 @@ struct gcry_md_context
   GcryDigestEntry *list;
   byte *macpads;
   int macpads_Bsize;             /* Blocksize as used for the HMAC pads. */
+  int bufalignoff;
 };
 
 
@@ -435,6 +437,7 @@ md_open (gcry_md_hd_t *h, int algo, int secure, int hmac)
   struct gcry_md_context *ctx;
   gcry_md_hd_t hd;
   size_t n;
+  int alignment;
 
   /* Allocate a memory area to hold the caller visible buffer with it's
    * control information and the data required by this module. Set the
@@ -450,9 +453,11 @@ md_open (gcry_md_hd_t *h, int algo, int secure, int hmac)
    *
    * We have to make sure that private is well aligned.
    */
-  n = sizeof (struct gcry_md_handle) + bufsize;
-  n = ((n + sizeof (PROPERLY_ALIGNED_TYPE) - 1)
-       / sizeof (PROPERLY_ALIGNED_TYPE)) * sizeof (PROPERLY_ALIGNED_TYPE);
+  alignment = sizeof (PROPERLY_ALIGNED_TYPE);
+  if (alignment < 16)
+    alignment = 16;
+  n = sizeof (struct gcry_md_handle) + bufsize + 15;
+  n = (((n + alignment - 1) / alignment) * alignment);
 
   /* Allocate and set the Context pointer to the private data */
   if (secure)
@@ -466,16 +471,19 @@ md_open (gcry_md_hd_t *h, int algo, int secure, int hmac)
   if (! err)
     {
       hd->ctx = ctx = (struct gcry_md_context *) ((char *) hd + n);
-      /* Setup the globally visible data (bctl in the diagram).*/
-      hd->bufsize = n - sizeof (struct gcry_md_handle) + 1;
-      hd->bufpos = 0;
 
       /* Initialize the private data. */
       memset (hd->ctx, 0, sizeof *hd->ctx);
       ctx->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
       ctx->actual_handle_size = n + sizeof (struct gcry_md_context);
       ctx->secure = secure;
+      ctx->bufalignoff = ((16 - ((size_t)&hd->buf & 0x0f)) % 16);
+
+      /* Setup the globally visible data (bctl in the diagram).*/
+      hd->bufsize = n - sizeof (struct gcry_md_handle) + 1 - ctx->bufalignoff;
+      hd->bufpos = ctx->bufalignoff;
 
+      /* Setup the rest of the private data.  */
       if (hmac)
 	{
 	  switch (algo)
@@ -652,10 +660,11 @@ md_copy (gcry_md_hd_t ahd, gcry_md_hd_t *b_hd)
     {
       bhd->ctx = b = (struct gcry_md_context *) ((char *) bhd + n);
       /* No need to copy the buffer due to the write above. */
-      gcry_assert (ahd->bufsize == (n - sizeof (struct gcry_md_handle) + 1));
+      gcry_assert (ahd->bufsize == (n - sizeof (struct gcry_md_handle) + 1
+                                    - ahd->ctx->bufalignoff));
       bhd->bufsize = ahd->bufsize;
-      bhd->bufpos = 0;
-      gcry_assert (! ahd->bufpos);
+      bhd->bufpos = ahd->ctx->bufalignoff;
+      gcry_assert (ahd->bufpos == ahd->ctx->bufalignoff);
       memcpy (b, a, sizeof *a);
       b->list = NULL;
       b->debug = NULL;
@@ -736,7 +745,8 @@ gcry_md_reset (gcry_md_hd_t a)
 
   /* Note: We allow this even in fips non operational mode.  */
 
-  a->bufpos = a->ctx->finalized = 0;
+  a->ctx->finalized = 0;
+  a->bufpos = a->ctx->bufalignoff;
 
   for (r = a->ctx->list; r; r = r->next)
     {
@@ -790,7 +800,8 @@ md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen)
 
   if (a->ctx->debug)
     {
-      if (a->bufpos && fwrite (a->buf, a->bufpos, 1, a->ctx->debug) != 1)
+      if (a->bufpos > a->ctx->bufalignoff
+          && fwrite (a->buf, a->bufpos, 1, a->ctx->debug) != 1)
 	BUG();
       if (inlen && fwrite (inbuf, inlen, 1, a->ctx->debug) != 1)
 	BUG();
@@ -798,11 +809,11 @@ md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen)
 
   for (r = a->ctx->list; r; r = r->next)
     {
-      if (a->bufpos)
+      if (a->bufpos > a->ctx->bufalignoff)
 	(*r->digest->write) (&r->context.c, a->buf, a->bufpos);
       (*r->digest->write) (&r->context.c, inbuf, inlen);
     }
-  a->bufpos = 0;
+  a->bufpos = a->ctx->bufalignoff;
 }
 
 void
@@ -819,7 +830,7 @@ md_final (gcry_md_hd_t a)
   if (a->ctx->finalized)
     return;
 
-  if (a->bufpos)
+  if (a->bufpos > a->ctx->bufalignoff)
     md_write (a, NULL, 0);
 
   for (r = a->ctx->list; r; r = r->next)
@@ -1220,7 +1231,7 @@ md_stop_debug( gcry_md_hd_t md )
 {
   if ( md->ctx->debug )
     {
-      if ( md->bufpos )
+      if ( md->bufpos > md->ctx->bufalignoff)
         md_write ( md, NULL, 0 );
       fclose (md->ctx->debug);
       md->ctx->debug = NULL;
diff --git a/cipher/sha1.c b/cipher/sha1.c
index 4b784ac..afc7c82 100644
--- a/cipher/sha1.c
+++ b/cipher/sha1.c
@@ -58,8 +58,8 @@ typedef struct
 {
   u32           h0,h1,h2,h3,h4;
   u32           nblocks;
-  unsigned char buf[64];
   int           count;
+  unsigned char buf[64] __attribute__ ((__aligned__ (16)));
 } SHA1_CONTEXT;
 
 
@@ -111,6 +111,8 @@ transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
   register u32 tm;            /* Helper.  */
   u32 x[16];                  /* The array we work on. */
 
+  log_debug ("sha1-transform, n=%u, data=%p\n", (unsigned int)nblocks, data);
+
   /* Loop over all blocks.  */
   for ( ;nblocks; nblocks--)
     {

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


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




More information about the Gnupg-commits mailing list