From 7dc6bd90c569c49807462b0740b18e32fab4d8b7 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 19 Jul 2011 16:53:43 -0400 Subject: Use union to avoid casts in code to store results of hashsum computations --- ChangeLog | 15 +++++++++++++++ crypt/md5.c | 14 +++++++------- crypt/md5.h | 10 +++++++--- crypt/sha256.c | 10 ++++------ crypt/sha256.h | 7 ++++++- crypt/sha512.c | 8 +++----- crypt/sha512.h | 6 +++++- 7 files changed, 47 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2a57fc317b..0932ae59e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2011-07-19 Ulrich Drepper + + * 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 * string/strxfrm_l.c (STRXFRM): Fix alloca accounting. diff --git a/crypt/md5.c b/crypt/md5.c index 922e7cc7e9..9bdb8e6aa6 100644 --- a/crypt/md5.c +++ b/crypt/md5.c @@ -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 diff --git a/crypt/md5.h b/crypt/md5.h index b474a84b8b..64a73bdc83 100644 --- a/crypt/md5.h +++ b/crypt/md5.h @@ -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]; + }; }; /* diff --git a/crypt/sha256.c b/crypt/sha256.c index 1a3aca6e92..c00cfe2151 100644 --- a/crypt/sha256.c +++ b/crypt/sha256.c @@ -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. */ diff --git a/crypt/sha256.h b/crypt/sha256.h index 0457bfae40..05f2db57e7 100644 --- a/crypt/sha256.h +++ b/crypt/sha256.h @@ -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. diff --git a/crypt/sha512.c b/crypt/sha512.c index 60a7ca53b9..0720b0903f 100644 --- a/crypt/sha512.c +++ b/crypt/sha512.c @@ -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); diff --git a/crypt/sha512.h b/crypt/sha512.h index 27dd717967..28d63fcfd3 100644 --- a/crypt/sha512.h +++ b/crypt/sha512.h @@ -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. -- cgit 1.4.1