1
1
/* md5.c - Functions to compute MD5 message digest of files or memory blocks
2
2
according to the definition of MD5 in RFC 1321 from April 1992.
3
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3
Copyright (C) 1995, 1996, 2007, 2008 Free Software Foundation, Inc.
4
4
This file is part of the GNU C library.
6
6
The GNU C Library is free software; you can redistribute it and/or
7
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
8
published by the Free Software Foundation; either version 3 of the
9
9
License, or (at your option) any later version.
11
11
The GNU C Library is distributed in the hope that it will be useful,
14
14
Library General Public License for more details.
16
16
You should have received a copy of the GNU Library General Public
17
License along with the GNU C Library; see the file COPYING.LIB. If not,
18
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA. */
17
License along with the GNU C Library. If not, see
18
<http://www.gnu.org/licenses/>. */
21
20
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
24
23
# include <config.h>
28
/*#if STDC_HEADERS || defined _LIBC*/
36
/*# ifndef HAVE_MEMCPY*/
37
/*# define memcpy(d, s, n) bcopy ((s), (d), (n))*/
26
/* modified for Wget: depend on C89 */
42
31
#include "gnu-md5.h"
114
103
/* Put the 64-bit file length in *bits* at the end of the buffer. */
115
104
*(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
116
105
*(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
117
(ctx->total[0] >> 29));
106
(ctx->total[0] >> 29));
119
108
/* Process last bytes. */
120
109
md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
145
134
/* We read the file in blocks of BLOCKSIZE bytes. One call of the
146
computation function processes the whole buffer so that with the
147
next round of the loop another block can be read. */
135
computation function processes the whole buffer so that with the
136
next round of the loop another block can be read. */
151
140
/* Read block. Take care for partial reads. */
154
n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
143
n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
158
147
while (sum < BLOCKSIZE && n != 0);
159
148
if (n == 0 && ferror (stream))
162
151
/* If end of file is reached, end the loop. */
166
155
/* Process buffer with BLOCKSIZE bytes. Note that
169
158
md5_process_block (buffer, BLOCKSIZE, &ctx);
213
202
ctx->buflen += add;
215
204
if (left_over + add > 64)
217
md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
218
/* The regions in the following copy operation cannot overlap. */
219
memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
220
(left_over + add) & 63);
221
ctx->buflen = (left_over + add) & 63;
206
md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
207
/* The regions in the following copy operation cannot overlap. */
208
memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
209
(left_over + add) & 63);
210
ctx->buflen = (left_over + add) & 63;
224
213
buffer = (const char *) buffer + add;
284
273
md5_uint32 D_save = D;
286
275
/* First round: using the given function, the context and a constant
287
the next context is computed. Because the algorithms processing
288
unit is a 32-bit word and it is determined to work on words in
289
little endian byte order we perhaps have to change the byte order
290
before the computation. To reduce the work for the next steps
291
we store the swapped words in the array CORRECT_WORDS. */
276
the next context is computed. Because the algorithms processing
277
unit is a 32-bit word and it is determined to work on words in
278
little endian byte order we perhaps have to change the byte order
279
before the computation. To reduce the work for the next steps
280
we store the swapped words in the array CORRECT_WORDS. */
293
#define OP(a, b, c, d, s, T) \
296
a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
282
#define OP(a, b, c, d, s, T) \
285
a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
303
292
/* It is unfortunate that C does not provide an operator for
304
cyclic rotation. Hope the C compiler is smart enough. */
293
cyclic rotation. Hope the C compiler is smart enough. */
305
294
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
307
296
/* Before we start, one word to the strange constants.
308
They are defined in RFC 1321 as
297
They are defined in RFC 1321 as
310
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
299
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
329
318
OP (B, C, D, A, 22, 0x49b40821);
331
320
/* For the second to fourth round we have the possibly swapped words
332
in CORRECT_WORDS. Redefine the macro to take an additional first
333
argument specifying the function to use. */
321
in CORRECT_WORDS. Redefine the macro to take an additional first
322
argument specifying the function to use. */
335
#define OP(f, a, b, c, d, k, s, T) \
338
a += f (b, c, d) + correct_words[k] + T; \
324
#define OP(f, a, b, c, d, k, s, T) \
327
a += f (b, c, d) + correct_words[k] + T; \