diff options
author | joseph <joseph@7b3dc134-2b1b-0410-93df-9e9f96275f8d> | 2011-07-22 13:43:39 +0000 |
---|---|---|
committer | joseph <joseph@7b3dc134-2b1b-0410-93df-9e9f96275f8d> | 2011-07-22 13:43:39 +0000 |
commit | c837cf0bbdd09079d64188bf0028bb21df2faec7 (patch) | |
tree | d3ead9b4b8d60f25f78b12417b2ba02f14971704 /libc/crypt | |
parent | a63bdad03a9963d53373008e78fc29ee48f161af (diff) | |
download | eglibc2-c837cf0bbdd09079d64188bf0028bb21df2faec7.tar.gz |
Merge changes between 14282 and r14661 from /fsf/trunk.
git-svn-id: svn://svn.eglibc.org/trunk@14662 7b3dc134-2b1b-0410-93df-9e9f96275f8d
Diffstat (limited to 'libc/crypt')
-rw-r--r-- | libc/crypt/md5.c | 14 | ||||
-rw-r--r-- | libc/crypt/md5.h | 10 | ||||
-rw-r--r-- | libc/crypt/sha256.c | 35 | ||||
-rw-r--r-- | libc/crypt/sha256.h | 18 | ||||
-rw-r--r-- | libc/crypt/sha512.c | 28 | ||||
-rw-r--r-- | libc/crypt/sha512.h | 21 |
6 files changed, 88 insertions, 38 deletions
diff --git a/libc/crypt/md5.c b/libc/crypt/md5.c index 922e7cc7e..9bdb8e6aa 100644 --- a/libc/crypt/md5.c +++ b/libc/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/libc/crypt/md5.h b/libc/crypt/md5.h index b474a84b8..64a73bdc8 100644 --- a/libc/crypt/md5.h +++ b/libc/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/libc/crypt/sha256.c b/libc/crypt/sha256.c index 941612e17..c00cfe215 100644 --- a/libc/crypt/sha256.c +++ b/libc/crypt/sha256.c @@ -1,6 +1,6 @@ /* Functions to compute SHA256 message digest of files or memory blocks. according to the definition of SHA256 in FIPS 180-2. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2011 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -35,12 +35,23 @@ # ifdef _LIBC # include <byteswap.h> # define SWAP(n) bswap_32 (n) +# define SWAP64(n) bswap_64 (n) # else # define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +# define SWAP64(n) \ + (((n) << 56) \ + | (((n) & 0xff00) << 40) \ + | (((n) & 0xff0000) << 24) \ + | (((n) & 0xff000000) << 8) \ + | (((n) >> 8) & 0xff000000) \ + | (((n) >> 24) & 0xff0000) \ + | (((n) >> 40) & 0xff00) \ + | ((n) >> 56)) # endif #else # define SWAP(n) (n) +# define SWAP64(n) (n) #endif @@ -89,10 +100,8 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) /* First increment the byte count. FIPS 180-2 specifies the possible length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; + number of bytes. */ + ctx->total64 += len; /* Process all bytes in the buffer with 64 bytes in each round of the loop. */ @@ -186,7 +195,7 @@ __sha256_init_ctx (ctx) ctx->H[6] = 0x1f83d9ab; ctx->H[7] = 0x5be0cd19; - ctx->total[0] = ctx->total[1] = 0; + ctx->total64 = 0; ctx->buflen = 0; } @@ -206,17 +215,19 @@ __sha256_finish_ctx (ctx, resbuf) size_t pad; /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; + ctx->total64 += bytes; pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; memcpy (&ctx->buffer[bytes], fillbuf, pad); /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3); - *(uint32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); +#ifdef _STRING_ARCH_unaligned + ctx->buffer64[(bytes + pad) / 8] = SWAP64 (ctx->total64 << 3); +#else + 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. */ sha256_process_block (ctx->buffer, bytes + pad + 8, ctx); diff --git a/libc/crypt/sha256.h b/libc/crypt/sha256.h index be8b0772c..05f2db57e 100644 --- a/libc/crypt/sha256.h +++ b/libc/crypt/sha256.h @@ -1,6 +1,6 @@ /* Declaration of functions and data types used for SHA256 sum computing library functions. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2011 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -24,6 +24,7 @@ #include <limits.h> #include <stdint.h> #include <stdio.h> +#include <endian.h> /* Structure to save state of computation between the single steps. */ @@ -31,9 +32,20 @@ struct sha256_ctx { uint32_t H[8]; - uint32_t total[2]; + union + { + uint64_t total64; +#define TOTAL64_low (1 - (BYTE_ORDER == LITTLE_ENDIAN)) +#define TOTAL64_high (BYTE_ORDER == LITTLE_ENDIAN) + 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/libc/crypt/sha512.c b/libc/crypt/sha512.c index 02127476f..0720b0903 100644 --- a/libc/crypt/sha512.c +++ b/libc/crypt/sha512.c @@ -1,6 +1,6 @@ /* Functions to compute SHA512 message digest of files or memory blocks. according to the definition of SHA512 in FIPS 180-2. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2011 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -121,9 +121,13 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) /* First increment the byte count. FIPS 180-2 specifies the possible length of the file up to 2^128 bits. Here we only compute the number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; +#ifdef USE_TOTAL128 + ctx->total128 += len; +#else + ctx->total[TOTAL128_low] += len; + if (ctx->total[TOTAL128_low] < len) + ++ctx->total[TOTAL128_high]; +#endif /* Process all bytes in the buffer with 128 bytes in each round of the loop. */ @@ -237,17 +241,21 @@ __sha512_finish_ctx (ctx, resbuf) size_t pad; /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; +#ifdef USE_TOTAL128 + ctx->total128 += bytes; +#else + ctx->total[TOTAL128_low] += bytes; + if (ctx->total[TOTAL128_low] < bytes) + ++ctx->total[TOTAL128_high]; +#endif pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes; 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[0] << 3); - *(uint64_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 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/libc/crypt/sha512.h b/libc/crypt/sha512.h index 9952c5f86..28d63fcfd 100644 --- a/libc/crypt/sha512.h +++ b/libc/crypt/sha512.h @@ -1,6 +1,6 @@ /* Declaration of functions and data types used for SHA512 sum computing library functions. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2011 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -24,6 +24,8 @@ #include <limits.h> #include <stdint.h> #include <stdio.h> +#include <endian.h> +#include <bits/wordsize.h> /* Structure to save state of computation between the single steps. */ @@ -31,9 +33,22 @@ struct sha512_ctx { uint64_t H[8]; - uint64_t total[2]; + union + { +#if defined __GNUC__ && __WORDSIZE == 64 +# define USE_TOTAL128 + unsigned int total128 __attribute__ ((__mode__ (TI))); +#endif +#define TOTAL128_low (1 - (BYTE_ORDER == LITTLE_ENDIAN)) +#define TOTAL128_high (BYTE_ORDER == LITTLE_ENDIAN) + 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. |