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