From 1834ba06ac55a1c627ed601e5cf7f649dbde12f0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 12 Jan 2006 07:17:25 +0000 Subject: Fix commentary to match md5 better. Adjust to .h file changes. --- lib/sha1.c | 37 ++++++++++++++++--------------------- lib/sha256.c | 45 +++++++++++++++++++-------------------------- lib/sha512.c | 49 +++++++++++++++++++++---------------------------- 3 files changed, 56 insertions(+), 75 deletions(-) diff --git a/lib/sha1.c b/lib/sha1.c index 09198f334..0bc29b4e2 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -1,7 +1,8 @@ /* sha1.c - Functions to compute SHA1 message digest of files or memory blocks according to the NIST specification FIPS-180-1. - Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 Free Software + Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the @@ -35,9 +36,6 @@ # include "unlocked-io.h" #endif -/* SWAP does an endian swap on architectures that are little-endian, - as SHA1 needs some data in a big-endian form. */ - #ifdef WORDS_BIGENDIAN # define SWAP(n) (n) #else @@ -55,11 +53,9 @@ static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; -/* - Takes a pointer to a 160 bit block of data (five 32 bit ints) and - intializes it to the start constants of the SHA1 algorithm. This - must be called before using hash in the call to sha1_hash. -*/ +/* Take a pointer to a 160 bit block of data (five 32 bit ints) and + initialize it to the start constants of the SHA1 algorithm. This + must be called before using hash in the call to sha1_hash. */ void sha1_init_ctx (struct sha1_ctx *ctx) { @@ -100,23 +96,21 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf) { /* Take yet unprocessed bytes into account. */ uint32_t bytes = ctx->buflen; - size_t pad; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; /* Now count remaining bytes. */ ctx->total[0] += bytes; if (ctx->total[0] < bytes) ++ctx->total[1]; - 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)); + ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3); + + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); /* Process last bytes. */ - sha1_process_block (ctx->buffer, bytes + pad + 8, ctx); + sha1_process_block (ctx->buffer, size * 4, ctx); return sha1_read_ctx (ctx, resbuf); } @@ -216,7 +210,7 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) size_t left_over = ctx->buflen; size_t add = 128 - left_over > len ? len : 128 - left_over; - memcpy (&ctx->buffer[left_over], buffer, add); + memcpy (&((char *) ctx->buffer)[left_over], buffer, add); ctx->buflen += add; if (ctx->buflen > 64) @@ -225,7 +219,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) ctx->buflen &= 63; /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + memcpy (ctx->buffer, + &((char *) ctx->buffer)[(left_over + add) & ~63], ctx->buflen); } @@ -260,13 +255,13 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) { size_t left_over = ctx->buflen; - memcpy (&ctx->buffer[left_over], buffer, len); + memcpy (&((char *) ctx->buffer)[left_over], buffer, len); left_over += len; if (left_over >= 64) { sha1_process_block (ctx->buffer, 64, ctx); left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); + memcpy (ctx->buffer, &ctx->buffer[16], left_over); } ctx->buflen = left_over; } diff --git a/lib/sha256.c b/lib/sha256.c index 38d867bdd..cea855f28 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -34,26 +34,20 @@ # include "unlocked-io.h" #endif -/* - Not-swap is a macro that does an endian swap on architectures that are - big-endian, as SHA256 needs some data in a little-endian format -*/ - #ifdef WORDS_BIGENDIAN -# define NOTSWAP(n) (n) +# define SWAP(n) (n) #else -# define NOTSWAP(n) \ +# define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) #endif #define BLOCKSIZE 4096 -/* Ensure that BLOCKSIZE is a multiple of 64. */ #if BLOCKSIZE % 64 != 0 # error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. */ + 64-byte boundary. */ static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; @@ -104,8 +98,8 @@ sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf) { int i; - for ( i=0 ; i<8 ; i++ ) - ((uint32_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); + for (i = 0; i < 8; i++) + ((uint32_t *) resbuf)[i] = SWAP (ctx->state[i]); return resbuf; } @@ -115,8 +109,8 @@ sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf) { int i; - for ( i=0 ; i<7 ; i++ ) - ((uint32_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); + for (i = 0; i < 7; i++) + ((uint32_t *) resbuf)[i] = SWAP (ctx->state[i]); return resbuf; } @@ -131,23 +125,21 @@ sha256_conclude_ctx (struct sha256_ctx *ctx) { /* Take yet unprocessed bytes into account. */ uint32_t bytes = ctx->buflen; - size_t pad; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; /* Now count remaining bytes. */ ctx->total[0] += bytes; if (ctx->total[0] < bytes) ++ctx->total[1]; - 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] = NOTSWAP (ctx->total[0] << 3); - *(uint32_t *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); + ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3); + + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); /* Process last bytes. */ - sha256_process_block (ctx->buffer, bytes + pad + 8, ctx); + sha256_process_block (ctx->buffer, size * 4, ctx); } void * @@ -338,7 +330,7 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) size_t left_over = ctx->buflen; size_t add = 128 - left_over > len ? len : 128 - left_over; - memcpy (&ctx->buffer[left_over], buffer, add); + memcpy (&((char *) ctx->buffer)[left_over], buffer, add); ctx->buflen += add; if (ctx->buflen > 64) @@ -347,7 +339,8 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) ctx->buflen &= 63; /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + memcpy (ctx->buffer, + &((char *) ctx->buffer)[(left_over + add) & ~63], ctx->buflen); } @@ -382,13 +375,13 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) { size_t left_over = ctx->buflen; - memcpy (&ctx->buffer[left_over], buffer, len); + memcpy (&((char *) ctx->buffer)[left_over], buffer, len); left_over += len; if (left_over >= 64) { sha256_process_block (ctx->buffer, 64, ctx); left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); + memcpy (ctx->buffer, &ctx->buffer[16], left_over); } ctx->buflen = left_over; } @@ -474,7 +467,7 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) /* FIXME: see sha1.c for a better implementation. */ for (t = 0; t < 16; t++) { - x[t] = NOTSWAP (*words); + x[t] = SWAP (*words); words++; } diff --git a/lib/sha512.c b/lib/sha512.c index 11c3b1d16..79c03e216 100644 --- a/lib/sha512.c +++ b/lib/sha512.c @@ -34,28 +34,22 @@ # include "unlocked-io.h" #endif -/* - Not-swap is a macro that does an endian swap on architectures that are - big-endian, as SHA512 needs some data in a little-endian format -*/ - #ifdef WORDS_BIGENDIAN -# define NOTSWAP(n) (n) +# define SWAP(n) (n) #else -# define NOTSWAP(n) \ +# define SWAP(n) \ (((n) << 56) | (((n) & 0xff00) << 40) | (((n) & 0xff0000UL) << 24) \ - | (((n) & 0xff000000UL) << 8) | (((n) >> 8) & 0xff000000UL) \ - | (((n) >> 24) & 0xff0000UL) | (((n) >> 40) & 0xff00UL) | ((n) >> 56)) + | (((n) & 0xff000000UL) << 8) | (((n) >> 8) & 0xff000000UL) \ + | (((n) >> 24) & 0xff0000UL) | (((n) >> 40) & 0xff00UL) | ((n) >> 56)) #endif #define BLOCKSIZE 4096 -/* Ensure that BLOCKSIZE is a multiple of 128. */ #if BLOCKSIZE % 128 != 0 # error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. */ + 128-byte boundary. */ static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ }; @@ -106,8 +100,8 @@ sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf) { int i; - for ( i=0 ; i<8 ; i++ ) - ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); + for (i = 0; i < 8; i++) + ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]); return resbuf; } @@ -117,8 +111,8 @@ sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf) { int i; - for ( i=0 ; i<6 ; i++ ) - ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); + for (i = 0; i < 6; i++) + ((uint64_t *) resbuf)[i] = SWAP (ctx->state[i]); return resbuf; } @@ -133,23 +127,21 @@ sha512_conclude_ctx (struct sha512_ctx *ctx) { /* Take yet unprocessed bytes into account. */ uint64_t bytes = ctx->buflen; - size_t pad; + size_t size = (bytes < 112) ? 128 / 8 : 128 * 2 / 8; /* Now count remaining bytes. */ ctx->total[0] += bytes; if (ctx->total[0] < bytes) ++ctx->total[1]; - pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes; - memcpy (&ctx->buffer[bytes], fillbuf, pad); - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(uint64_t *) &ctx->buffer[bytes + pad + 8] = NOTSWAP (ctx->total[0] << 3); - *(uint64_t *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 61)); + ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 61)); + ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3); + + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes); /* Process last bytes. */ - sha512_process_block (ctx->buffer, bytes + pad + 16, ctx); + sha512_process_block (ctx->buffer, size * 8, ctx); } void * @@ -340,7 +332,7 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) size_t left_over = ctx->buflen; size_t add = 256 - left_over > len ? len : 256 - left_over; - memcpy (&ctx->buffer[left_over], buffer, add); + memcpy (&((char *) ctx->buffer)[left_over], buffer, add); ctx->buflen += add; if (ctx->buflen > 128) @@ -349,7 +341,8 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) ctx->buflen &= 127; /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~127], + memcpy (ctx->buffer, + &((char *) ctx->buffer)[(left_over + add) & ~127], ctx->buflen); } @@ -384,13 +377,13 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) { size_t left_over = ctx->buflen; - memcpy (&ctx->buffer[left_over], buffer, len); + memcpy (&((char *) ctx->buffer)[left_over], buffer, len); left_over += len; if (left_over >= 128) { sha512_process_block (ctx->buffer, 128, ctx); left_over -= 128; - memcpy (ctx->buffer, &ctx->buffer[128], left_over); + memcpy (ctx->buffer, &ctx->buffer[16], left_over); } ctx->buflen = left_over; } @@ -469,7 +462,7 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) /* FIXME: see sha1.c for a better implementation. */ for (t = 0; t < 16; t++) { - x[t] = NOTSWAP (*words); + x[t] = SWAP (*words); words++; } -- cgit v1.2.3-70-g09d2