diff options
author | Ulrich Drepper <drepper@gmail.com> | 2011-07-02 12:30:03 -0400 |
---|---|---|
committer | Ulrich Drepper <drepper@gmail.com> | 2011-07-02 12:30:03 -0400 |
commit | fcfc776bc6242fdefde0efd7b0c315fbeca08555 (patch) | |
tree | c46c6b25047a2ed2f6baea7b76985f1d7bec3c4c /crypt/sha256.c | |
parent | 99231d9abe0fd74c7957d25b08c1d1ede4cae5a0 (diff) | |
download | glibc-fcfc776bc6242fdefde0efd7b0c315fbeca08555.tar.gz glibc-fcfc776bc6242fdefde0efd7b0c315fbeca08555.tar.xz glibc-fcfc776bc6242fdefde0efd7b0c315fbeca08555.zip |
Optimize long-word additions in SHA implementation
Diffstat (limited to 'crypt/sha256.c')
-rw-r--r-- | crypt/sha256.c | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/crypt/sha256.c b/crypt/sha256.c index 941612e17b..0ca3355a05 100644 --- a/crypt/sha256.c +++ b/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. */ +#ifdef _STRING_ARCH_unaligned + *(uint64_t *) &ctx->buffer[bytes + pad] = SWAP64 (ctx->total64 << 3); +#else *(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)); +#endif /* Process last bytes. */ sha256_process_block (ctx->buffer, bytes + pad + 8, ctx); |