1 : /* Functions to compute MD5 message digest of files or memory blocks.
2 : according to the definition of MD5 in RFC 1321 from April 1992.
3 : Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
4 : This file is part of the GNU C Library.
5 :
6 : The GNU C Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Lesser General Public
8 : License as published by the Free Software Foundation; either
9 : version 2.1 of the License, or (at your option) any later version.
10 :
11 : The GNU C Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with the GNU C Library; if not, write to the Free
18 : Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 : 02111-1307 USA. */
20 :
21 : /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
22 :
23 : #ifdef HAVE_CONFIG_H
24 : # include <config.h>
25 : #endif
26 :
27 : #include <sys/types.h>
28 :
29 : #include <stdlib.h>
30 : #include <string.h>
31 :
32 : #include "md5.h"
33 :
34 :
35 : static md5_uint32 SWAP(md5_uint32 n)
36 19428 : {
37 19428 : static int checked = 0;
38 19428 : static int bigendian = 0;
39 19428 : static md5_uint32 test;
40 :
41 19428 : if (!checked) {
42 20 : test = 1;
43 20 : if (* (char *) &test == 0)
44 0 : bigendian = 1;
45 20 : checked = 1;
46 : }
47 :
48 19428 : if (bigendian)
49 0 : return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
50 : else
51 19428 : return n;
52 : }
53 :
54 :
55 : /* This array contains the bytes used to pad the buffer to the next
56 : 64-byte boundary. (RFC 1321, 3.1: Step 1) */
57 : static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
58 :
59 :
60 : /* Initialize structure containing state of computation.
61 : (RFC 1321, 3.3: Step 3) */
62 : void
63 : md5_init_ctx (ctx)
64 : struct md5_ctx *ctx;
65 158 : {
66 158 : ctx->A = 0x67452301;
67 158 : ctx->B = 0xefcdab89;
68 158 : ctx->C = 0x98badcfe;
69 158 : ctx->D = 0x10325476;
70 :
71 158 : ctx->total[0] = ctx->total[1] = 0;
72 158 : ctx->buflen = 0;
73 : }
74 :
75 : /* Put result from CTX in first 16 bytes following RESBUF. The result
76 : must be in little endian byte order.
77 :
78 : IMPORTANT: On some systems it is required that RESBUF is correctly
79 : aligned for a 32 bits value. */
80 : void *
81 : md5_read_ctx (ctx, resbuf)
82 : const struct md5_ctx *ctx;
83 : void *resbuf;
84 158 : {
85 158 : ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
86 158 : ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
87 158 : ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
88 158 : ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
89 :
90 158 : return resbuf;
91 : }
92 :
93 : /* Process the remaining bytes in the internal buffer and the usual
94 : prolog according to the standard and write the result to RESBUF.
95 :
96 : IMPORTANT: On some systems it is required that RESBUF is correctly
97 : aligned for a 32 bits value. */
98 : void *
99 : md5_finish_ctx (ctx, resbuf)
100 : struct md5_ctx *ctx;
101 : void *resbuf;
102 158 : {
103 : /* Take yet unprocessed bytes into account. */
104 158 : md5_uint32 bytes = ctx->buflen;
105 158 : size_t pad;
106 :
107 : /* Now count remaining bytes. */
108 158 : ctx->total[0] += bytes;
109 158 : if (ctx->total[0] < bytes)
110 0 : ++ctx->total[1];
111 :
112 158 : pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
113 158 : memcpy (&ctx->buffer[bytes], fillbuf, pad);
114 :
115 : /* Put the 64-bit file length in *bits* at the end of the buffer. */
116 158 : *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
117 158 : *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
118 : (ctx->total[0] >> 29));
119 :
120 : /* Process last bytes. */
121 158 : md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
122 :
123 158 : return md5_read_ctx (ctx, resbuf);
124 : }
125 :
126 : /* Compute MD5 message digest for bytes read from STREAM. The
127 : resulting message digest number will be written into the 16 bytes
128 : beginning at RESBLOCK. */
129 : int
130 : md5_stream (stream, resblock)
131 : FILE *stream;
132 : void *resblock;
133 0 : {
134 : /* Important: BLOCKSIZE must be a multiple of 64. */
135 : #define BLOCKSIZE 4096
136 0 : struct md5_ctx ctx;
137 0 : char buffer[BLOCKSIZE + 72];
138 0 : size_t sum;
139 :
140 : /* Initialize the computation context. */
141 0 : md5_init_ctx (&ctx);
142 :
143 : /* Iterate over full file contents. */
144 0 : while (1)
145 : {
146 : /* We read the file in blocks of BLOCKSIZE bytes. One call of the
147 : computation function processes the whole buffer so that with the
148 : next round of the loop another block can be read. */
149 0 : size_t n;
150 0 : sum = 0;
151 :
152 : /* Read block. Take care for partial reads. */
153 0 : do
154 : {
155 0 : n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
156 :
157 0 : sum += n;
158 0 : }
159 : while (sum < BLOCKSIZE && n != 0);
160 0 : if (n == 0 && ferror (stream))
161 0 : return 1;
162 :
163 : /* If end of file is reached, end the loop. */
164 0 : if (n == 0)
165 0 : break;
166 :
167 : /* Process buffer with BLOCKSIZE bytes. Note that
168 : BLOCKSIZE % 64 == 0
169 : */
170 0 : md5_process_block (buffer, BLOCKSIZE, &ctx);
171 : }
172 :
173 : /* Add the last bytes if necessary. */
174 0 : if (sum > 0)
175 0 : md5_process_bytes (buffer, sum, &ctx);
176 :
177 : /* Construct result in desired memory. */
178 0 : md5_finish_ctx (&ctx, resblock);
179 0 : return 0;
180 : }
181 :
182 : /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
183 : result is always in little endian byte order, so that a byte-wise
184 : output yields to the wanted ASCII representation of the message
185 : digest. */
186 : void *
187 : md5_buffer (buffer, len, resblock)
188 : const char *buffer;
189 : size_t len;
190 : void *resblock;
191 135 : {
192 135 : struct md5_ctx ctx;
193 :
194 : /* Initialize the computation context. */
195 135 : md5_init_ctx (&ctx);
196 :
197 : /* Process whole buffer but last len % 64 bytes. */
198 135 : md5_process_bytes (buffer, len, &ctx);
199 :
200 : /* Put result in desired memory area. */
201 135 : return md5_finish_ctx (&ctx, resblock);
202 : }
203 :
204 :
205 : void
206 : md5_process_bytes (buffer, len, ctx)
207 : const void *buffer;
208 : size_t len;
209 : struct md5_ctx *ctx;
210 591 : {
211 : /* When we already have some bits in our internal buffer concatenate
212 : both inputs first. */
213 591 : if (ctx->buflen != 0)
214 : {
215 433 : size_t left_over = ctx->buflen;
216 433 : size_t add = 128 - left_over > len ? len : 128 - left_over;
217 :
218 433 : memcpy (&ctx->buffer[left_over], buffer, add);
219 433 : ctx->buflen += add;
220 :
221 433 : if (ctx->buflen > 64)
222 : {
223 46 : md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
224 :
225 46 : ctx->buflen &= 63;
226 : /* The regions in the following copy operation cannot overlap. */
227 46 : memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
228 : ctx->buflen);
229 : }
230 :
231 433 : buffer = (const char *) buffer + add;
232 433 : len -= add;
233 : }
234 :
235 : /* Process available complete blocks. */
236 591 : if (len >= 64)
237 : {
238 : #if !_STRING_ARCH_unaligned
239 : /* To check alignment gcc has an appropriate operator. Other
240 : compilers don't. */
241 : # if __GNUC__ >= 2
242 : # define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
243 : # else
244 : # define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
245 : # endif
246 152 : if (UNALIGNED_P (buffer))
247 0 : while (len > 64)
248 : {
249 0 : md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
250 0 : buffer = (const char *) buffer + 64;
251 0 : len -= 64;
252 : }
253 : else
254 : #endif
255 : {
256 152 : md5_process_block (buffer, len & ~63, ctx);
257 152 : buffer = (const char *) buffer + (len & ~63);
258 152 : len &= 63;
259 : }
260 : }
261 :
262 : /* Move remaining bytes in internal buffer. */
263 591 : if (len > 0)
264 : {
265 176 : size_t left_over = ctx->buflen;
266 :
267 176 : memcpy (&ctx->buffer[left_over], buffer, len);
268 176 : left_over += len;
269 176 : if (left_over >= 64)
270 : {
271 0 : md5_process_block (ctx->buffer, 64, ctx);
272 0 : left_over -= 64;
273 0 : memcpy (ctx->buffer, &ctx->buffer[64], left_over);
274 : }
275 176 : ctx->buflen = left_over;
276 : }
277 : }
278 :
279 :
280 : /* These are the four functions used in the four steps of the MD5 algorithm
281 : and defined in the RFC 1321. The first function is a little bit optimized
282 : (as found in Colin Plumbs public domain implementation). */
283 : /* #define FF(b, c, d) ((b & c) | (~b & d)) */
284 : #define FF(b, c, d) (d ^ (b & (c ^ d)))
285 : #define FG(b, c, d) FF (d, b, c)
286 : #define FH(b, c, d) (b ^ c ^ d)
287 : #define FI(b, c, d) (c ^ (b | ~d))
288 :
289 : /* Process LEN bytes of BUFFER, accumulating context into CTX.
290 : It is assumed that LEN % 64 == 0. */
291 :
292 : void
293 : md5_process_block (buffer, len, ctx)
294 : const void *buffer;
295 : size_t len;
296 : struct md5_ctx *ctx;
297 356 : {
298 356 : md5_uint32 correct_words[16];
299 356 : const md5_uint32 *words = buffer;
300 356 : size_t nwords = len / sizeof (md5_uint32);
301 356 : const md5_uint32 *endp = words + nwords;
302 356 : md5_uint32 A = ctx->A;
303 356 : md5_uint32 B = ctx->B;
304 356 : md5_uint32 C = ctx->C;
305 356 : md5_uint32 D = ctx->D;
306 :
307 : /* First increment the byte count. RFC 1321 specifies the possible
308 : length of the file up to 2^64 bits. Here we only compute the
309 : number of bytes. Do a double word increment. */
310 356 : ctx->total[0] += len;
311 356 : if (ctx->total[0] < len)
312 0 : ++ctx->total[1];
313 :
314 : /* Process all bytes in the buffer with 64 bytes in each round of
315 : the loop. */
316 1511 : while (words < endp)
317 : {
318 1155 : md5_uint32 *cwp = correct_words;
319 1155 : md5_uint32 A_save = A;
320 1155 : md5_uint32 B_save = B;
321 1155 : md5_uint32 C_save = C;
322 1155 : md5_uint32 D_save = D;
323 :
324 : /* First round: using the given function, the context and a constant
325 : the next context is computed. Because the algorithms processing
326 : unit is a 32-bit word and it is determined to work on words in
327 : little endian byte order we perhaps have to change the byte order
328 : before the computation. To reduce the work for the next steps
329 : we store the swapped words in the array CORRECT_WORDS. */
330 :
331 : #define OP(a, b, c, d, s, T) \
332 : do \
333 : { \
334 : a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
335 : ++words; \
336 : CYCLIC (a, s); \
337 : a += b; \
338 : } \
339 : while (0)
340 :
341 : /* It is unfortunate that C does not provide an operator for
342 : cyclic rotation. Hope the C compiler is smart enough. */
343 : #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
344 :
345 : /* Before we start, one word to the strange constants.
346 : They are defined in RFC 1321 as
347 :
348 : T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
349 : */
350 :
351 : /* Round 1. */
352 1155 : OP (A, B, C, D, 7, 0xd76aa478);
353 1155 : OP (D, A, B, C, 12, 0xe8c7b756);
354 1155 : OP (C, D, A, B, 17, 0x242070db);
355 1155 : OP (B, C, D, A, 22, 0xc1bdceee);
356 1155 : OP (A, B, C, D, 7, 0xf57c0faf);
357 1155 : OP (D, A, B, C, 12, 0x4787c62a);
358 1155 : OP (C, D, A, B, 17, 0xa8304613);
359 1155 : OP (B, C, D, A, 22, 0xfd469501);
360 1155 : OP (A, B, C, D, 7, 0x698098d8);
361 1155 : OP (D, A, B, C, 12, 0x8b44f7af);
362 1155 : OP (C, D, A, B, 17, 0xffff5bb1);
363 1155 : OP (B, C, D, A, 22, 0x895cd7be);
364 1155 : OP (A, B, C, D, 7, 0x6b901122);
365 1155 : OP (D, A, B, C, 12, 0xfd987193);
366 1155 : OP (C, D, A, B, 17, 0xa679438e);
367 1155 : OP (B, C, D, A, 22, 0x49b40821);
368 :
369 : /* For the second to fourth round we have the possibly swapped words
370 : in CORRECT_WORDS. Redefine the macro to take an additional first
371 : argument specifying the function to use. */
372 : #undef OP
373 : #define OP(f, a, b, c, d, k, s, T) \
374 : do \
375 : { \
376 : a += f (b, c, d) + correct_words[k] + T; \
377 : CYCLIC (a, s); \
378 : a += b; \
379 : } \
380 : while (0)
381 :
382 : /* Round 2. */
383 1155 : OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
384 1155 : OP (FG, D, A, B, C, 6, 9, 0xc040b340);
385 1155 : OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
386 1155 : OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
387 1155 : OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
388 1155 : OP (FG, D, A, B, C, 10, 9, 0x02441453);
389 1155 : OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
390 1155 : OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
391 1155 : OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
392 1155 : OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
393 1155 : OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
394 1155 : OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
395 1155 : OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
396 1155 : OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
397 1155 : OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
398 1155 : OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
399 :
400 : /* Round 3. */
401 1155 : OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
402 1155 : OP (FH, D, A, B, C, 8, 11, 0x8771f681);
403 1155 : OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
404 1155 : OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
405 1155 : OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
406 1155 : OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
407 1155 : OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
408 1155 : OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
409 1155 : OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
410 1155 : OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
411 1155 : OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
412 1155 : OP (FH, B, C, D, A, 6, 23, 0x04881d05);
413 1155 : OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
414 1155 : OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
415 1155 : OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
416 1155 : OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
417 :
418 : /* Round 4. */
419 1155 : OP (FI, A, B, C, D, 0, 6, 0xf4292244);
420 1155 : OP (FI, D, A, B, C, 7, 10, 0x432aff97);
421 1155 : OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
422 1155 : OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
423 1155 : OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
424 1155 : OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
425 1155 : OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
426 1155 : OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
427 1155 : OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
428 1155 : OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
429 1155 : OP (FI, C, D, A, B, 6, 15, 0xa3014314);
430 1155 : OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
431 1155 : OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
432 1155 : OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
433 1155 : OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
434 1155 : OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
435 :
436 : /* Add the starting values of the context. */
437 1155 : A += A_save;
438 1155 : B += B_save;
439 1155 : C += C_save;
440 1155 : D += D_save;
441 : }
442 :
443 : /* Put checksum in context given as argument. */
444 356 : ctx->A = A;
445 356 : ctx->B = B;
446 356 : ctx->C = C;
447 356 : ctx->D = D;
448 : }
|