openssl: update to 1.0.2d
[tomato.git] / release / src / router / mdu / md5.c
blob581633914caa31368e6189f093d75580a17093e6
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 Free Software Foundation, Inc.
4 This file is part of the GNU C library.
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.
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.
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. */
21 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
23 #include <stdlib.h>
24 #include <string.h>
26 #include "md5.h"
28 #ifdef _LIBC
29 # include <endian.h>
30 # if __BYTE_ORDER == __BIG_ENDIAN
31 # define WORDS_BIGENDIAN 1
32 # endif
33 #endif
35 #if defined(WORDS_BIGENDIAN) || defined(_BIG_ENDIAN)
36 # define SWAP(n) \
37 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
38 #else
39 # define SWAP(n) (n)
40 #endif
42 /* This array contains the bytes used to pad the buffer to the next
43 64-byte boundary. (RFC 1321, 3.1: Step 1) */
44 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
46 static void md5_process_block PARAMS ((const void *buffer, size_t len,
47 struct md5_ctx *ctx));
48 static void md5_process_bytes PARAMS ((const void *buffer, size_t len,
49 struct md5_ctx *ctx));
51 /* Initialize structure containing state of computation.
52 (RFC 1321, 3.3: Step 3) */
53 static void
54 md5_init_ctx (struct md5_ctx *ctx)
56 ctx->A = 0x67452301;
57 ctx->B = 0xefcdab89;
58 ctx->C = 0x98badcfe;
59 ctx->D = 0x10325476;
61 ctx->total[0] = ctx->total[1] = 0;
62 ctx->buflen = 0;
65 /* Put result from CTX in first 16 bytes following RESBUF. The result
66 must be in little endian byte order.
68 IMPORTANT: On some systems it is required that RESBUF is correctly
69 aligned for a 32 bits value. */
70 static void *
71 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
73 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
74 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
75 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
76 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
78 return resbuf;
81 /* Process the remaining bytes in the internal buffer and the usual
82 prolog according to the standard and write the result to RESBUF.
84 IMPORTANT: On some systems it is required that RESBUF is correctly
85 aligned for a 32 bits value. */
86 static void *
87 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
89 /* Take yet unprocessed bytes into account. */
90 md5_uint32 bytes = ctx->buflen;
91 size_t pad;
93 /* Now count remaining bytes. */
94 ctx->total[0] += bytes;
95 if (ctx->total[0] < bytes)
96 ++ctx->total[1];
98 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
99 memcpy (&ctx->buffer[bytes], fillbuf, pad);
101 /* Put the 64-bit file length in *bits* at the end of the buffer. */
102 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
103 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
104 (ctx->total[0] >> 29));
106 /* Process last bytes. */
107 md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
109 return md5_read_ctx (ctx, resbuf);
112 #if 0
113 /* Compute MD5 message digest for bytes read from STREAM. The
114 resulting message digest number will be written into the 16 bytes
115 beginning at RESBLOCK. */
117 md5_stream (FILE *stream, void *resblock)
119 /* Important: BLOCKSIZE must be a multiple of 64. */
120 #define BLOCKSIZE 4096
121 struct md5_ctx ctx;
122 char buffer[BLOCKSIZE + 72];
123 size_t sum;
125 /* Initialize the computation context. */
126 md5_init_ctx (&ctx);
128 /* Iterate over full file contents. */
129 while (1)
131 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
132 computation function processes the whole buffer so that with the
133 next round of the loop another block can be read. */
134 size_t n;
135 sum = 0;
137 /* Read block. Take care for partial reads. */
140 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
142 sum += n;
144 while (sum < BLOCKSIZE && n != 0);
145 if (n == 0 && ferror (stream))
146 return 1;
148 /* If end of file is reached, end the loop. */
149 if (n == 0)
150 break;
152 /* Process buffer with BLOCKSIZE bytes. Note that
153 BLOCKSIZE % 64 == 0
155 md5_process_block (buffer, BLOCKSIZE, &ctx);
158 /* Add the last bytes if necessary. */
159 if (sum > 0)
160 md5_process_bytes (buffer, sum, &ctx);
162 /* Construct result in desired memory. */
163 md5_finish_ctx (&ctx, resblock);
164 return 0;
166 #endif
168 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
169 result is always in little endian byte order, so that a byte-wise
170 output yields to the wanted ASCII representation of the message
171 digest. */
172 void *
173 md5_buffer (const char *buffer, size_t len, void *resblock)
175 struct md5_ctx ctx;
177 /* Initialize the computation context. */
178 md5_init_ctx (&ctx);
180 /* Process whole buffer but last len % 64 bytes. */
181 md5_process_bytes (buffer, len, &ctx);
183 /* Put result in desired memory area. */
184 return md5_finish_ctx (&ctx, resblock);
188 static void
189 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
191 /* When we already have some bits in our internal buffer concatenate
192 both inputs first. */
193 if (ctx->buflen != 0)
195 size_t left_over = ctx->buflen;
196 size_t add = 128 - left_over > len ? len : 128 - left_over;
198 memcpy (&ctx->buffer[left_over], buffer, add);
199 ctx->buflen += add;
201 if (left_over + add > 64)
203 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
204 /* The regions in the following copy operation cannot overlap. */
205 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
206 (left_over + add) & 63);
207 ctx->buflen = (left_over + add) & 63;
210 buffer = (const char *) buffer + add;
211 len -= add;
214 /* Process available complete blocks. */
215 if (len > 64)
217 md5_process_block (buffer, len & ~63, ctx);
218 buffer = (const char *) buffer + (len & ~63);
219 len &= 63;
222 /* Move remaining bytes in internal buffer. */
223 if (len > 0)
225 memcpy (ctx->buffer, buffer, len);
226 ctx->buflen = len;
231 /* These are the four functions used in the four steps of the MD5 algorithm
232 and defined in the RFC 1321. The first function is a little bit optimized
233 (as found in Colin Plumbs public domain implementation). */
234 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
235 #define FF(b, c, d) (d ^ (b & (c ^ d)))
236 #define FG(b, c, d) FF (d, b, c)
237 #define FH(b, c, d) (b ^ c ^ d)
238 #define FI(b, c, d) (c ^ (b | ~d))
240 /* Process LEN bytes of BUFFER, accumulating context into CTX.
241 It is assumed that LEN % 64 == 0. */
243 static void
244 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
246 md5_uint32 correct_words[16];
247 const md5_uint32 *words = buffer;
248 size_t nwords = len / sizeof (md5_uint32);
249 const md5_uint32 *endp = words + nwords;
250 md5_uint32 A = ctx->A;
251 md5_uint32 B = ctx->B;
252 md5_uint32 C = ctx->C;
253 md5_uint32 D = ctx->D;
255 /* First increment the byte count. RFC 1321 specifies the possible
256 length of the file up to 2^64 bits. Here we only compute the
257 number of bytes. Do a double word increment. */
258 ctx->total[0] += len;
259 if (ctx->total[0] < len)
260 ++ctx->total[1];
262 /* Process all bytes in the buffer with 64 bytes in each round of
263 the loop. */
264 while (words < endp)
266 md5_uint32 *cwp = correct_words;
267 md5_uint32 A_save = A;
268 md5_uint32 B_save = B;
269 md5_uint32 C_save = C;
270 md5_uint32 D_save = D;
272 /* First round: using the given function, the context and a constant
273 the next context is computed. Because the algorithms processing
274 unit is a 32-bit word and it is determined to work on words in
275 little endian byte order we perhaps have to change the byte order
276 before the computation. To reduce the work for the next steps
277 we store the swapped words in the array CORRECT_WORDS. */
279 #define OP(a, b, c, d, s, T) \
280 do \
282 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
283 ++words; \
284 CYCLIC (a, s); \
285 a += b; \
287 while (0)
289 /* It is unfortunate that C does not provide an operator for
290 cyclic rotation. Hope the C compiler is smart enough. */
291 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
293 /* Before we start, one word to the strange constants.
294 They are defined in RFC 1321 as
296 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
299 /* Round 1. */
300 OP (A, B, C, D, 7, 0xd76aa478);
301 OP (D, A, B, C, 12, 0xe8c7b756);
302 OP (C, D, A, B, 17, 0x242070db);
303 OP (B, C, D, A, 22, 0xc1bdceee);
304 OP (A, B, C, D, 7, 0xf57c0faf);
305 OP (D, A, B, C, 12, 0x4787c62a);
306 OP (C, D, A, B, 17, 0xa8304613);
307 OP (B, C, D, A, 22, 0xfd469501);
308 OP (A, B, C, D, 7, 0x698098d8);
309 OP (D, A, B, C, 12, 0x8b44f7af);
310 OP (C, D, A, B, 17, 0xffff5bb1);
311 OP (B, C, D, A, 22, 0x895cd7be);
312 OP (A, B, C, D, 7, 0x6b901122);
313 OP (D, A, B, C, 12, 0xfd987193);
314 OP (C, D, A, B, 17, 0xa679438e);
315 OP (B, C, D, A, 22, 0x49b40821);
317 /* For the second to fourth round we have the possibly swapped words
318 in CORRECT_WORDS. Redefine the macro to take an additional first
319 argument specifying the function to use. */
320 #undef OP
321 #define OP(f, a, b, c, d, k, s, T) \
322 do \
324 a += f (b, c, d) + correct_words[k] + T; \
325 CYCLIC (a, s); \
326 a += b; \
328 while (0)
330 /* Round 2. */
331 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
332 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
333 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
334 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
335 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
336 OP (FG, D, A, B, C, 10, 9, 0x02441453);
337 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
338 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
339 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
340 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
341 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
342 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
343 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
344 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
345 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
346 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
348 /* Round 3. */
349 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
350 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
351 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
352 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
353 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
354 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
355 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
356 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
357 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
358 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
359 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
360 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
361 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
362 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
363 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
364 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
366 /* Round 4. */
367 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
368 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
369 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
370 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
371 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
372 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
373 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
374 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
375 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
376 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
377 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
378 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
379 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
380 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
381 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
382 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
384 /* Add the starting values of the context. */
385 A += A_save;
386 B += B_save;
387 C += C_save;
388 D += D_save;
391 /* Put checksum in context given as argument. */
392 ctx->A = A;
393 ctx->B = B;
394 ctx->C = C;
395 ctx->D = D;