1 /* md5.c - 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 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
15
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. */
20
21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_LIMITS_H
30 # include <limits.h>
31 #endif
32
33 #ifdef HAVE_OPENSSL
34 #include <openssl/md5.h>
35 #endif
36
37 #include "ne_md5.h"
38 #include "ne_string.h" /* for NE_ASC2HEX */
39
40 #if SIZEOF_INT == 4
41 typedef unsigned int md5_uint32;
42 #elif SIZEOF_LONG == 4
43 typedef unsigned long md5_uint32;
44 #else
45 # error "Cannot determine unsigned 32-bit data type."
46 #endif
47
48 #define md5_process_block ne_md5_process_block
49 #define md5_process_bytes ne_md5_process_bytes
50 #define md5_finish_ctx ne_md5_finish_ctx
51 #define md5_read_ctx ne_md5_read_ctx
52 #define md5_stream ne_md5_stream
53 #define md5_ctx ne_md5_ctx
54
55
56 #ifdef WORDS_BIGENDIAN
57 # define SWAP(n) \
58 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
59 #else
60 # define SWAP(n) (n)
61 #endif
62
63 /* Structure to save state of computation between the single steps. */
64 struct md5_ctx
65 {
66 #ifdef HAVE_OPENSSL
67 MD5_CTX ctx;
68 #else
69 md5_uint32 A;
70 md5_uint32 B;
71 md5_uint32 C;
72 md5_uint32 D;
73
74 md5_uint32 total[2];
75 md5_uint32 buflen;
76 char buffer[128];
77 #endif
78 };
79
80 #ifndef HAVE_OPENSSL
81 /* This array contains the bytes used to pad the buffer to the next
82 64-byte boundary. (RFC 1321, 3.1: Step 1) */
83 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
84
85
86 /* Initialize structure containing state of computation.
87 (RFC 1321, 3.3: Step 3) */
88 static void
md5_init_ctx(struct md5_ctx * ctx)89 md5_init_ctx (struct md5_ctx *ctx)
90 {
91 ctx->A = 0x67452301;
92 ctx->B = 0xefcdab89;
93 ctx->C = 0x98badcfe;
94 ctx->D = 0x10325476;
95
96 ctx->total[0] = ctx->total[1] = 0;
97 ctx->buflen = 0;
98 }
99
100 struct ne_md5_ctx *
ne_md5_create_ctx(void)101 ne_md5_create_ctx(void)
102 {
103 struct md5_ctx *ctx = ne_malloc(sizeof *ctx);
104 md5_init_ctx(ctx);
105 return ctx;
106 }
107
108 extern void
ne_md5_reset_ctx(struct ne_md5_ctx * ctx)109 ne_md5_reset_ctx(struct ne_md5_ctx *ctx)
110 {
111 md5_init_ctx(ctx);
112 }
113
114 struct ne_md5_ctx *
ne_md5_dup_ctx(struct ne_md5_ctx * ctx)115 ne_md5_dup_ctx(struct ne_md5_ctx *ctx)
116 {
117 return memcpy(ne_malloc(sizeof *ctx), ctx, sizeof *ctx);
118 }
119
120 void
ne_md5_destroy_ctx(struct ne_md5_ctx * ctx)121 ne_md5_destroy_ctx(struct ne_md5_ctx *ctx)
122 {
123 ne_free(ctx);
124 }
125
126 /* Process the remaining bytes in the internal buffer and the usual
127 prolog according to the standard and write the result to RESBUF.
128
129 IMPORTANT: On some systems it is required that RESBUF is correctly
130 aligned for a 32 bits value. */
131 void *
md5_finish_ctx(struct md5_ctx * ctx,void * resbuf)132 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
133 {
134 /* Take yet unprocessed bytes into account. */
135 md5_uint32 bytes = ctx->buflen;
136 md5_uint32 swap_bytes;
137 size_t pad;
138
139 /* Now count remaining bytes. */
140 ctx->total[0] += bytes;
141 if (ctx->total[0] < bytes)
142 ++ctx->total[1];
143
144 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
145 memcpy (&ctx->buffer[bytes], fillbuf, pad);
146
147 /* Put the 64-bit file length in *bits* at the end of the buffer.
148 Use memcpy to avoid aliasing problems. On most systems, this
149 will be optimized away to the same code. */
150 swap_bytes = SWAP (ctx->total[0] << 3);
151 memcpy (&ctx->buffer[bytes + pad], &swap_bytes, sizeof (swap_bytes));
152 swap_bytes = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
153 memcpy (&ctx->buffer[bytes + pad + 4], &swap_bytes, sizeof (swap_bytes));
154
155 /* Process last bytes. */
156 md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
157
158 return md5_read_ctx (ctx, resbuf);
159 }
160
161 void
md5_process_bytes(const void * buffer,size_t len,struct md5_ctx * ctx)162 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
163 {
164 /* When we already have some bits in our internal buffer concatenate
165 both inputs first. */
166 if (ctx->buflen != 0)
167 {
168 size_t left_over = ctx->buflen;
169 size_t add = 128 - left_over > len ? len : 128 - left_over;
170
171 memcpy (&ctx->buffer[left_over], buffer, add);
172 ctx->buflen += add;
173
174 if (left_over + add > 64)
175 {
176 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
177 /* The regions in the following copy operation cannot overlap. */
178 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
179 (left_over + add) & 63);
180 ctx->buflen = (left_over + add) & 63;
181 }
182
183 buffer = (const char *) buffer + add;
184 len -= add;
185 }
186
187 /* Process available complete blocks. */
188 if (len > 64)
189 {
190 md5_process_block (buffer, len & ~63, ctx);
191 buffer = (const char *) buffer + (len & ~63);
192 len &= 63;
193 }
194
195 /* Move remaining bytes in internal buffer. */
196 if (len > 0)
197 {
198 memcpy (ctx->buffer, buffer, len);
199 ctx->buflen = len;
200 }
201 }
202
203
204 /* These are the four functions used in the four steps of the MD5 algorithm
205 and defined in the RFC 1321. The first function is a little bit optimized
206 (as found in Colin Plumbs public domain implementation). */
207 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
208 #define FF(b, c, d) (d ^ (b & (c ^ d)))
209 #define FG(b, c, d) FF (d, b, c)
210 #define FH(b, c, d) (b ^ c ^ d)
211 #define FI(b, c, d) (c ^ (b | ~d))
212
213 /* Process LEN bytes of BUFFER, accumulating context into CTX.
214 It is assumed that LEN % 64 == 0. */
215
216 void
md5_process_block(const void * buffer,size_t len,struct md5_ctx * ctx)217 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
218 {
219 md5_uint32 correct_words[16];
220 const unsigned char *words = buffer;
221 const unsigned char *endp = words + len;
222 md5_uint32 A = ctx->A;
223 md5_uint32 B = ctx->B;
224 md5_uint32 C = ctx->C;
225 md5_uint32 D = ctx->D;
226
227 /* First increment the byte count. RFC 1321 specifies the possible
228 length of the file up to 2^64 bits. Here we only compute the
229 number of bytes. Do a double word increment. */
230 ctx->total[0] += len;
231 if (ctx->total[0] < len)
232 ++ctx->total[1];
233
234 /* Process all bytes in the buffer with 64 bytes in each round of
235 the loop. */
236 while (words < endp)
237 {
238 md5_uint32 *cwp = correct_words;
239 md5_uint32 A_save = A;
240 md5_uint32 B_save = B;
241 md5_uint32 C_save = C;
242 md5_uint32 D_save = D;
243
244 /* First round: using the given function, the context and a constant
245 the next context is computed. Because the algorithms processing
246 unit is a 32-bit word and it is determined to work on words in
247 little endian byte order we perhaps have to change the byte order
248 before the computation. To reduce the work for the next steps
249 we store the swapped words in the array CORRECT_WORDS. */
250
251 #define OP(a, b, c, d, s, T) \
252 do \
253 { \
254 md5_uint32 WORD_ = (md5_uint32)words[0] | ((md5_uint32)words[1] << 8) \
255 | ((md5_uint32)words[2] << 16) | ((md5_uint32)words[3] << 24); \
256 a += FF (b, c, d) + (*cwp++ = WORD_) + T; \
257 words += 4; \
258 CYCLIC (a, s); \
259 a += b; \
260 } \
261 while (0)
262
263 /* It is unfortunate that C does not provide an operator for
264 cyclic rotation. Hope the C compiler is smart enough. */
265 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
266
267 /* Before we start, one word to the strange constants.
268 They are defined in RFC 1321 as
269
270 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
271 */
272
273 /* Round 1. */
274 OP (A, B, C, D, 7, 0xd76aa478);
275 OP (D, A, B, C, 12, 0xe8c7b756);
276 OP (C, D, A, B, 17, 0x242070db);
277 OP (B, C, D, A, 22, 0xc1bdceee);
278 OP (A, B, C, D, 7, 0xf57c0faf);
279 OP (D, A, B, C, 12, 0x4787c62a);
280 OP (C, D, A, B, 17, 0xa8304613);
281 OP (B, C, D, A, 22, 0xfd469501);
282 OP (A, B, C, D, 7, 0x698098d8);
283 OP (D, A, B, C, 12, 0x8b44f7af);
284 OP (C, D, A, B, 17, 0xffff5bb1);
285 OP (B, C, D, A, 22, 0x895cd7be);
286 OP (A, B, C, D, 7, 0x6b901122);
287 OP (D, A, B, C, 12, 0xfd987193);
288 OP (C, D, A, B, 17, 0xa679438e);
289 OP (B, C, D, A, 22, 0x49b40821);
290
291 /* For the second to fourth round we have the possibly swapped words
292 in CORRECT_WORDS. Redefine the macro to take an additional first
293 argument specifying the function to use. */
294 #undef OP
295 #define OP(f, a, b, c, d, k, s, T) \
296 do \
297 { \
298 a += f (b, c, d) + correct_words[k] + T; \
299 CYCLIC (a, s); \
300 a += b; \
301 } \
302 while (0)
303
304 /* Round 2. */
305 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
306 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
307 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
308 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
309 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
310 OP (FG, D, A, B, C, 10, 9, 0x02441453);
311 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
312 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
313 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
314 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
315 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
316 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
317 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
318 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
319 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
320 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
321
322 /* Round 3. */
323 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
324 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
325 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
326 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
327 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
328 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
329 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
330 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
331 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
332 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
333 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
334 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
335 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
336 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
337 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
338 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
339
340 /* Round 4. */
341 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
342 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
343 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
344 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
345 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
346 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
347 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
348 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
349 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
350 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
351 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
352 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
353 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
354 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
355 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
356 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
357
358 /* Add the starting values of the context. */
359 A += A_save;
360 B += B_save;
361 C += C_save;
362 D += D_save;
363 }
364
365 /* Put checksum in context given as argument. */
366 ctx->A = A;
367 ctx->B = B;
368 ctx->C = C;
369 ctx->D = D;
370 }
371 #else /* HAVE_OPENSSL */
372
ne_md5_create_ctx(void)373 struct ne_md5_ctx *ne_md5_create_ctx(void)
374 {
375 struct ne_md5_ctx *ctx = ne_malloc(sizeof *ctx);
376
377 if (MD5_Init(&ctx->ctx) != 1) {
378 ne_free(ctx);
379 return NULL;
380 }
381
382 return ctx;
383 }
384
ne_md5_process_block(const void * buffer,size_t len,struct ne_md5_ctx * ctx)385 void ne_md5_process_block(const void *buffer, size_t len,
386 struct ne_md5_ctx *ctx)
387 {
388 MD5_Update(&ctx->ctx, buffer, len);
389 }
390
ne_md5_process_bytes(const void * buffer,size_t len,struct ne_md5_ctx * ctx)391 void ne_md5_process_bytes(const void *buffer, size_t len,
392 struct ne_md5_ctx *ctx)
393 {
394 MD5_Update(&ctx->ctx, buffer, len);
395 }
396
ne_md5_finish_ctx(struct ne_md5_ctx * ctx,void * resbuf)397 void *ne_md5_finish_ctx(struct ne_md5_ctx *ctx, void *resbuf)
398 {
399 MD5_Final(resbuf, &ctx->ctx);
400
401 return resbuf;
402 }
403
ne_md5_dup_ctx(struct ne_md5_ctx * ctx)404 struct ne_md5_ctx *ne_md5_dup_ctx(struct ne_md5_ctx *ctx)
405 {
406 return memcpy(ne_malloc(sizeof *ctx), ctx, sizeof *ctx);
407 }
408
ne_md5_reset_ctx(struct ne_md5_ctx * ctx)409 void ne_md5_reset_ctx(struct ne_md5_ctx *ctx)
410 {
411 MD5_Init(&ctx->ctx);
412 }
413
ne_md5_destroy_ctx(struct ne_md5_ctx * ctx)414 void ne_md5_destroy_ctx(struct ne_md5_ctx *ctx)
415 {
416 ne_free(ctx);
417 }
418 #endif /* HAVE_OPENSSL */
419
420 /* Put result from CTX in first 16 bytes following RESBUF. The result
421 must be in little endian byte order.
422
423 IMPORTANT: On some systems it is required that RESBUF is correctly
424 aligned for a 32 bits value. */
425 void *
md5_read_ctx(const struct md5_ctx * ctx,void * resbuf)426 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
427 {
428 #ifdef HAVE_OPENSSL
429 #define SWAP_CTX(x) SWAP(ctx->ctx.x)
430 #else
431 #define SWAP_CTX(x) SWAP(ctx->x)
432 #endif
433
434 ((md5_uint32 *) resbuf)[0] = SWAP_CTX (A);
435 ((md5_uint32 *) resbuf)[1] = SWAP_CTX (B);
436 ((md5_uint32 *) resbuf)[2] = SWAP_CTX (C);
437 ((md5_uint32 *) resbuf)[3] = SWAP_CTX (D);
438
439 return resbuf;
440 }
441
442
443 /* Compute MD5 message digest for bytes read from STREAM. The
444 resulting message digest number will be written into the 16 bytes
445 beginning at RESBLOCK. */
446 int
md5_stream(FILE * stream,void * resblock)447 md5_stream (FILE *stream, void *resblock)
448 {
449 /* Important: BLOCKSIZE must be a multiple of 64. */
450 #define BLOCKSIZE 4096
451 struct ne_md5_ctx *ctx;
452 char buffer[BLOCKSIZE + 72];
453 size_t sum;
454
455 /* Initialize the computation context. */
456 ctx = ne_md5_create_ctx ();
457
458 /* Iterate over full file contents. */
459 while (1)
460 {
461 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
462 computation function processes the whole buffer so that with the
463 next round of the loop another block can be read. */
464 size_t n;
465 sum = 0;
466
467 /* Read block. Take care for partial reads. */
468 do
469 {
470 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
471
472 sum += n;
473 }
474 while (sum < BLOCKSIZE && n != 0);
475 if (n == 0 && ferror (stream))
476 return 1;
477
478 /* If end of file is reached, end the loop. */
479 if (n == 0)
480 break;
481
482 /* Process buffer with BLOCKSIZE bytes. Note that
483 BLOCKSIZE % 64 == 0
484 */
485 md5_process_block (buffer, BLOCKSIZE, ctx);
486 }
487
488 /* Add the last bytes if necessary. */
489 if (sum > 0)
490 md5_process_bytes (buffer, sum, ctx);
491
492 /* Construct result in desired memory. */
493 md5_finish_ctx (ctx, resblock);
494 ne_md5_destroy_ctx (ctx);
495
496 return 0;
497 }
498
499 /* Writes the ASCII representation of the MD5 digest into the
500 * given buffer, which must be at least 33 characters long. */
ne_md5_to_ascii(const unsigned char md5_buf[16],char * buffer)501 void ne_md5_to_ascii(const unsigned char md5_buf[16], char *buffer)
502 {
503 int count;
504 for (count = 0; count<16; count++) {
505 buffer[count*2] = NE_HEX2ASC(md5_buf[count] >> 4);
506 buffer[count*2+1] = NE_HEX2ASC(md5_buf[count] & 0x0f);
507 }
508 buffer[32] = '\0';
509 }
510
511 /* Reads the ASCII representation of an MD5 digest. The buffer must
512 * be at least 32 characters long. */
ne_ascii_to_md5(const char * buffer,unsigned char md5_buf[16])513 void ne_ascii_to_md5(const char *buffer, unsigned char md5_buf[16])
514 {
515 int count;
516 for (count = 0; count<16; count++) {
517 md5_buf[count] = ((NE_ASC2HEX(buffer[count*2])) << 4) |
518 NE_ASC2HEX(buffer[count*2+1]);
519 }
520 }
521
ne_md5_finish_ascii(struct ne_md5_ctx * ctx,char buffer[33])522 char *ne_md5_finish_ascii(struct ne_md5_ctx *ctx, char buffer[33])
523 {
524 md5_uint32 result[4];
525
526 ne_md5_finish_ctx(ctx, (void *)result);
527 ne_md5_to_ascii((void *)result, buffer);
528
529 return buffer;
530 }
531
532