X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fmd5.c;h=511a029d1388063477fd410095bb8144f16a9be0;hb=8de557e31178699dd6e839850056f0653cdfba89;hp=1e988936a480d2bf18f0f9852701381da2169ad4;hpb=0151a413ed4a6045a9d749b03f455fc6873e6fbb;p=gnulib.git diff --git a/lib/md5.c b/lib/md5.c index 1e988936a..511a029d1 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -1,47 +1,54 @@ -/* md5.c - Functions to compute MD5 message digest of files or memory blocks -Copyright (C) 1996 Free Software Foundation, Inc. -This file is part of the GNU C Library. -Contributed by Ulrich Drepper , 1996. +/* 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 + 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 -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. + 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 + Free Software Foundation; either version 2, or (at your option) any + later version. -The GNU C Library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -/* Written by Ulrich Drepper . */ +/* Written by Ulrich Drepper , 1995. */ #ifdef HAVE_CONFIG_H # include #endif +#include "md5.h" + +#include +#include +#include #include -#if STDC_HEADERS || defined _LIBC -# include -# include -#else -# ifndef HAVE_MEMCPY -# define memcpy(d, s, n) bcopy ((s), (d), (n)) -# endif +#if USE_UNLOCKED_IO +# include "unlocked-io.h" #endif -#include "md5.h" - #ifdef _LIBC # include # if __BYTE_ORDER == __BIG_ENDIAN # define WORDS_BIGENDIAN 1 # endif +/* We need to keep the namespace clean so define the MD5 function + protected using leading __ . */ +# define md5_init_ctx __md5_init_ctx +# define md5_process_block __md5_process_block +# define md5_process_bytes __md5_process_bytes +# define md5_finish_ctx __md5_finish_ctx +# define md5_read_ctx __md5_read_ctx +# define md5_stream __md5_stream +# define md5_buffer __md5_buffer #endif #ifdef WORDS_BIGENDIAN @@ -51,6 +58,10 @@ Boston, MA 02111-1307, USA. */ # define SWAP(n) (n) #endif +#define BLOCKSIZE 4096 +#if BLOCKSIZE % 64 != 0 +# error "invalid BLOCKSIZE" +#endif /* This array contains the bytes used to pad the buffer to the next 64-byte boundary. (RFC 1321, 3.1: Step 1) */ @@ -60,8 +71,7 @@ static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; /* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */ void -md5_init_ctx (ctx) - struct md5_ctx *ctx; +md5_init_ctx (struct md5_ctx *ctx) { ctx->A = 0x67452301; ctx->B = 0xefcdab89; @@ -72,30 +82,32 @@ md5_init_ctx (ctx) ctx->buflen = 0; } -/* Put result from CTX in first 16 bytes following RESBUF. The result must - be in little endian byte order. */ +/* Put result from CTX in first 16 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ void * -md5_read_ctx (ctx, resbuf) - const struct md5_ctx *ctx; - void *resbuf; +md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) { - ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); + ((uint32_t *) resbuf)[0] = SWAP (ctx->A); + ((uint32_t *) resbuf)[1] = SWAP (ctx->B); + ((uint32_t *) resbuf)[2] = SWAP (ctx->C); + ((uint32_t *) resbuf)[3] = SWAP (ctx->D); return resbuf; } /* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. */ + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ void * -md5_finish_ctx (ctx, resbuf) - struct md5_ctx *ctx; - void *resbuf; +md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) { /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; + uint32_t bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ @@ -107,8 +119,8 @@ 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) | + *(uint32_t *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); + *(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); /* Process last bytes. */ @@ -121,12 +133,8 @@ md5_finish_ctx (ctx, resbuf) resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */ int -md5_stream (stream, resblock) - FILE *stream; - void *resblock; +md5_stream (FILE *stream, void *resblock) { - /* Important: BLOCKSIZE must be a multiple of 64. */ -#define BLOCKSIZE 4096 struct md5_ctx ctx; char buffer[BLOCKSIZE + 72]; size_t sum; @@ -144,19 +152,31 @@ md5_stream (stream, resblock) sum = 0; /* Read block. Take care for partial reads. */ - do + while (1) { n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); sum += n; - } - while (sum < BLOCKSIZE && n != 0); - if (n == 0 && ferror (stream)) - return 1; - /* If end of file is reached, end the loop. */ - if (n == 0) - break; + if (sum == BLOCKSIZE) + break; + + if (n == 0) + { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror (stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof (stream)) + goto process_partial_block; + } /* Process buffer with BLOCKSIZE bytes. Note that BLOCKSIZE % 64 == 0 @@ -164,7 +184,9 @@ md5_stream (stream, resblock) md5_process_block (buffer, BLOCKSIZE, &ctx); } - /* Add the last bytes if necessary. */ + process_partial_block:; + + /* Process any remaining bytes. */ if (sum > 0) md5_process_bytes (buffer, sum, &ctx); @@ -178,10 +200,7 @@ md5_stream (stream, resblock) output yields to the wanted ASCII representation of the message digest. */ void * -md5_buffer (buffer, len, resblock) - const char *buffer; - size_t len; - void *resblock; +md5_buffer (const char *buffer, size_t len, void *resblock) { struct md5_ctx ctx; @@ -197,10 +216,7 @@ md5_buffer (buffer, len, resblock) void -md5_process_bytes (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; +md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) { /* When we already have some bits in our internal buffer concatenate both inputs first. */ @@ -212,32 +228,62 @@ md5_process_bytes (buffer, len, ctx) memcpy (&ctx->buffer[left_over], buffer, add); ctx->buflen += add; - if (left_over + add > 64) + if (ctx->buflen > 64) { - md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx); + md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; /* The regions in the following copy operation cannot overlap. */ memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - (left_over + add) & 63); - ctx->buflen = (left_over + add) & 63; + ctx->buflen); } - buffer += add; + buffer = (const char *) buffer + add; len -= add; } /* Process available complete blocks. */ - if (len > 64) + if (len >= 64) { - md5_process_block (buffer, len & ~63, ctx); - buffer += len & ~63; - len &= 63; +#if !_STRING_ARCH_unaligned +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0) +# else +# define alignof(type) offsetof (struct { char c; type x; }, x) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (len > 64) + { + md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + md5_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } } /* Move remaining bytes in internal buffer. */ if (len > 0) { - memcpy (ctx->buffer, buffer, len); - ctx->buflen = len; + size_t left_over = ctx->buflen; + + memcpy (&ctx->buffer[left_over], buffer, len); + left_over += len; + if (left_over >= 64) + { + md5_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; } } @@ -255,19 +301,16 @@ md5_process_bytes (buffer, len, ctx) It is assumed that LEN % 64 == 0. */ void -md5_process_block (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; +md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) { - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; + uint32_t correct_words[16]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t A = ctx->A; + uint32_t B = ctx->B; + uint32_t C = ctx->C; + uint32_t D = ctx->D; /* First increment the byte count. RFC 1321 specifies the possible length of the file up to 2^64 bits. Here we only compute the @@ -280,11 +323,11 @@ md5_process_block (buffer, len, ctx) the loop. */ while (words < endp) { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; + uint32_t *cwp = correct_words; + uint32_t A_save = A; + uint32_t B_save = B; + uint32_t C_save = C; + uint32_t D_save = D; /* First round: using the given function, the context and a constant the next context is computed. Because the algorithms processing @@ -311,6 +354,10 @@ md5_process_block (buffer, len, ctx) They are defined in RFC 1321 as T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + + Here is an equivalent invocation using Perl: + + perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' */ /* Round 1. */