X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsha1.c;h=035f898c71f1dae4fab2ed3c48ef125be0e66b5d;hb=882eb279d46373c3de620fd74504c8502f9297a0;hp=09198f3346f82e743cf2aa5c8f6fdcd6acb094f4;hpb=c43d62c6f29b412d3333808be3f1f619af19e3f4;p=gnulib.git diff --git a/lib/sha1.c b/lib/sha1.c index 09198f334..035f898c7 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 @@ -22,9 +23,7 @@ Robert Klep -- Expansion function fix */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include "sha1.h" @@ -35,9 +34,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 +51,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 +94,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); } @@ -187,7 +179,7 @@ sha1_stream (FILE *stream, void *resblock) return 0; } -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The +/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ @@ -216,7 +208,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 +217,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 +253,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; } @@ -275,10 +268,10 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) /* --- Code below is the primary difference between md5.c and sha1.c --- */ /* SHA1 round constants */ -#define K1 0x5a827999L -#define K2 0x6ed9eba1L -#define K3 0x8f1bbcdcL -#define K4 0xca62c1d6L +#define K1 0x5a827999 +#define K2 0x6ed9eba1 +#define K3 0x8f1bbcdc +#define K4 0xca62c1d6 /* Round functions. Note that F2 is the same as F4. */ #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) ) @@ -310,7 +303,7 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) if (ctx->total[0] < len) ++ctx->total[1]; -#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n)))) #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \ ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \