]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Use union to avoid casts in code to store results of hashsum computations
authorUlrich Drepper <drepper@gmail.com>
Tue, 19 Jul 2011 20:53:43 +0000 (16:53 -0400)
committerUlrich Drepper <drepper@gmail.com>
Tue, 19 Jul 2011 20:53:43 +0000 (16:53 -0400)
ChangeLog
crypt/md5.c
crypt/md5.h
crypt/sha256.c
crypt/sha256.h
crypt/sha512.c
crypt/sha512.h

index 2a57fc317bd509d1681a1fa55792f4c10084820c..0932ae59e409e72f9e0a7c3594af30936ce89692 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2011-07-19  Ulrich Drepper  <drepper@gmail.com>
+
+       * crypt/sha512.h (struct sha512_ctx): Move buffer into union and add
+       buffer64.
+       * crypt/sha512.c (__sha512_finish_ctx): Use buffer64 for writes instead
+       of casting of buffer.
+       * crypt/sha256.h (struct sha256_ctx): Move buffer into union and add
+       buffer32 and buffer64.
+       * crypt/sha256.c (__sha256_finish_ctx): Use buffer32 or buffer64 for
+       writes instead of casting of buffer.
+       * crypt/md5.h (struct md5_ctx): Move buffer into union and add
+       buffer32.
+       * crypt/md5.c (md5_finish_ctx): Use buffer32 for writes instead of
+       casting of buffer.
+
 2011-07-19  Andreas Schwab  <schwab@redhat.com>
 
        * string/strxfrm_l.c (STRXFRM): Fix alloca accounting.
index 922e7cc7e97352f81fde0a076fdb11886bbec7ce..9bdb8e6aa6f7f2786f1de2955eb4480b3485f563 100644 (file)
@@ -1,6 +1,6 @@
 /* Functions to compute MD5 message digest of files or memory blocks.
    according to the definition of MD5 in RFC 1321 from April 1992.
-   Copyright (C) 1995,1996,1997,1999,2000,2001,2005
+   Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2011
        Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -123,9 +123,9 @@ md5_finish_ctx (ctx, resbuf)
   memcpy (&ctx->buffer[bytes], fillbuf, pad);
 
   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
-  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
-  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
-                                                       (ctx->total[0] >> 29));
+  ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
+  ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
+                                              (ctx->total[0] >> 29));
 
   /* Process last bytes.  */
   md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
@@ -168,7 +168,7 @@ md5_stream (stream, resblock)
        }
       while (sum < BLOCKSIZE && n != 0);
       if (n == 0 && ferror (stream))
-        return 1;
+       return 1;
 
       /* If end of file is reached, end the loop.  */
       if (n == 0)
@@ -340,12 +340,12 @@ md5_process_block (buffer, len, ctx)
 
 #define OP(a, b, c, d, s, T)                                           \
       do                                                               \
-        {                                                              \
+                                                                     \
          a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
          ++words;                                                      \
          CYCLIC (a, s);                                                \
          a += b;                                                       \
-        }                                                              \
+                                                                     \
       while (0)
 
       /* It is unfortunate that C does not provide an operator for
index b474a84b8bc6c2642e53f74f557208dd79ead2fa..64a73bdc8370e6769fafde05559f32fe0f9b0bf0 100644 (file)
@@ -1,6 +1,6 @@
 /* Declaration of functions and data types used for MD5 sum computing
    library functions.
-   Copyright (C) 1995-1997,1999,2000,2001,2004,2005
+   Copyright (C) 1995-1997,1999,2000,2001,2004,2005,2011
       Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -68,7 +68,7 @@ typedef uintptr_t md5_uintptr;
      typedef unsigned long md5_uint32;
 #   else
      /* The following line is intended to evoke an error.
-        Using #error is not portable enough.  */
+       Using #error is not portable enough.  */
      "Cannot determine unsigned 32-bit data type."
 #   endif
 #  endif
@@ -88,7 +88,11 @@ struct md5_ctx
 
   md5_uint32 total[2];
   md5_uint32 buflen;
-  char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
+  union
+  {
+    char buffer[128];
+    md5_uint32 buffer32[32];
+  };
 };
 
 /*
index 1a3aca6e92649442ca7913fa1dbcd56f1faf76e1..c00cfe2151e5c9b0ebb5676ee268c2506fd736e4 100644 (file)
@@ -222,13 +222,11 @@ __sha256_finish_ctx (ctx, resbuf)
 
   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
 #ifdef _STRING_ARCH_unaligned
-  *(uint64_t *)  &ctx->buffer[bytes + pad] = SWAP64 (ctx->total64 << 3);
+  ctx->buffer64[(bytes + pad) / 8] = SWAP64 (ctx->total64 << 3);
 #else
-  *(uint32_t *) &ctx->buffer[bytes + pad + 4]
-    = SWAP (ctx->total[TOTAL64_low] << 3);
-  *(uint32_t *) &ctx->buffer[bytes + pad]
-    = SWAP ((ctx->total[TOTAL64_high] << 3) |
-           (ctx->total[TOTAL64_low] >> 29));
+  ctx->buffer32[(bytes + pad + 4) / 4] = SWAP (ctx->total[TOTAL64_low] << 3);
+  ctx->buffer32[(bytes + pad) / 4] = SWAP ((ctx->total[TOTAL64_high] << 3) |
+                                          (ctx->total[TOTAL64_low] >> 29));
 #endif
 
   /* Process last bytes.  */
index 0457bfae405a669b8ddd2ffe92ba988fbb7760fe..05f2db57e79233e50a6f3689807d78331df657f2 100644 (file)
@@ -40,7 +40,12 @@ struct sha256_ctx
     uint32_t total[2];
   };
   uint32_t buflen;
-  char buffer[128] __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
+  union
+  {
+    char buffer[128];
+    uint32_t buffer32[32];
+    uint64_t buffer64[16];
+  };
 };
 
 /* Initialize structure containing state of computation.
index 60a7ca53b99a3bd878beb3e6ec7aad421b40d203..0720b0903f6e29b4ee5bbfc81f1b0581b222f52f 100644 (file)
@@ -253,11 +253,9 @@ __sha512_finish_ctx (ctx, resbuf)
   memcpy (&ctx->buffer[bytes], fillbuf, pad);
 
   /* Put the 128-bit file length in *bits* at the end of the buffer.  */
-  *(uint64_t *) &ctx->buffer[bytes + pad + 8]
-    = SWAP (ctx->total[TOTAL128_low] << 3);
-  *(uint64_t *) &ctx->buffer[bytes + pad]
-    = SWAP ((ctx->total[TOTAL128_high] << 3) |
-           (ctx->total[TOTAL128_low] >> 61));
+  ctx->buffer64[(bytes + pad + 8) / 8] = SWAP (ctx->total[TOTAL128_low] << 3);
+  ctx->buffer64[(bytes + pad) / 8] = SWAP ((ctx->total[TOTAL128_high] << 3) |
+                                          (ctx->total[TOTAL128_low] >> 61));
 
   /* Process last bytes.  */
   sha512_process_block (ctx->buffer, bytes + pad + 16, ctx);
index 27dd71796729a6751e559872eb3d175db44f6fca..28d63fcfd3d34030d8bbc9e4ee5a0d4aa159305f 100644 (file)
@@ -44,7 +44,11 @@ struct sha512_ctx
     uint64_t total[2];
   };
   uint64_t buflen;
-  char buffer[256] __attribute__ ((__aligned__ (__alignof__ (uint64_t))));
+  union
+  {
+    char buffer[256];
+    uint64_t buffer64[32];
+  };
 };
 
 /* Initialize structure containing state of computation.