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 : #include <sys/types.h>
24 :
25 : #include <stdlib.h>
26 : #include <string.h>
27 :
28 : #include "md5.h"
29 :
30 :
31 : static md5_uint32 SWAP(md5_uint32 n)
32 7074 : {
33 : static int checked = 0;
34 : static int bigendian = 0;
35 : static md5_uint32 test;
36 :
37 7074 : if (!checked) {
38 18 : test = 1;
39 18 : if (* (char *) &test == 0)
40 0 : bigendian = 1;
41 18 : checked = 1;
42 : }
43 :
44 7074 : if (bigendian)
45 0 : return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
46 : else
47 7074 : return n;
48 : }
49 :
50 :
51 : /* This array contains the bytes used to pad the buffer to the next
52 : 64-byte boundary. (RFC 1321, 3.1: Step 1) */
53 : static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
54 :
55 :
56 : /* Initialize structure containing state of computation.
57 : (RFC 1321, 3.3: Step 3) */
58 : void
59 : MD5_Init (ctx)
60 : struct MD5_CTX *ctx;
61 19 : {
62 19 : ctx->A = 0x67452301;
63 19 : ctx->B = 0xefcdab89;
64 19 : ctx->C = 0x98badcfe;
65 19 : ctx->D = 0x10325476;
66 :
67 19 : ctx->total[0] = ctx->total[1] = 0;
68 19 : ctx->buflen = 0;
69 19 : }
70 :
71 : /* Put result from CTX in first 16 bytes following RESBUF. The result
72 : must be in little endian byte order.
73 :
74 : IMPORTANT: On some systems it is required that RESBUF is correctly
75 : aligned for a 32 bits value. */
76 : void *
77 : md5_read_ctx (ctx, resbuf)
78 : const struct MD5_CTX *ctx;
79 : void *resbuf;
80 19 : {
81 19 : ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
82 19 : ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
83 19 : ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
84 19 : ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
85 :
86 19 : return resbuf;
87 : }
88 :
89 : /* Process the remaining bytes in the internal buffer and the usual
90 : prolog according to the standard and write the result to RESBUF.
91 :
92 : IMPORTANT: On some systems it is required that RESBUF is correctly
93 : aligned for a 32 bits value. */
94 : void *
95 : MD5_Final (resbuf, ctx)
96 : void *resbuf;
97 : struct MD5_CTX *ctx;
98 19 : {
99 : /* Take yet unprocessed bytes into account. */
100 19 : md5_uint32 bytes = ctx->buflen;
101 : size_t pad;
102 :
103 : /* Now count remaining bytes. */
104 19 : ctx->total[0] += bytes;
105 19 : if (ctx->total[0] < bytes)
106 0 : ++ctx->total[1];
107 :
108 19 : pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
109 19 : memcpy (&ctx->buffer[bytes], fillbuf, pad);
110 :
111 : /* Put the 64-bit file length in *bits* at the end of the buffer. */
112 19 : *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
113 19 : *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
114 : (ctx->total[0] >> 29));
115 :
116 : /* Process last bytes. */
117 19 : md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
118 :
119 19 : return md5_read_ctx (ctx, resbuf);
120 : }
121 :
122 : void
123 : MD5_Update (ctx, buffer, len)
124 : struct MD5_CTX *ctx;
125 : const void *buffer;
126 : size_t len;
127 455 : {
128 : /* When we already have some bits in our internal buffer concatenate
129 : both inputs first. */
130 455 : if (ctx->buflen != 0)
131 : {
132 437 : size_t left_over = ctx->buflen;
133 437 : size_t add = 128 - left_over > len ? len : 128 - left_over;
134 :
135 437 : memcpy (&ctx->buffer[left_over], buffer, add);
136 437 : ctx->buflen += add;
137 :
138 437 : if (ctx->buflen > 64)
139 : {
140 38 : md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
141 :
142 38 : ctx->buflen &= 63;
143 : /* The regions in the following copy operation cannot overlap. */
144 38 : memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
145 : ctx->buflen);
146 : }
147 :
148 437 : buffer = (const char *) buffer + add;
149 437 : len -= add;
150 : }
151 :
152 : /* Process available complete blocks. */
153 455 : if (len >= 64)
154 : {
155 : #if !_STRING_ARCH_unaligned
156 : /* To check alignment gcc has an appropriate operator. Other
157 : compilers don't. */
158 : # if __GNUC__ >= 2
159 : # define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
160 : # else
161 : # define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
162 : # endif
163 5 : if (UNALIGNED_P (buffer))
164 0 : while (len > 64)
165 : {
166 0 : md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
167 0 : buffer = (const char *) buffer + 64;
168 0 : len -= 64;
169 : }
170 : else
171 : #endif
172 : {
173 5 : md5_process_block (buffer, len & ~63, ctx);
174 5 : buffer = (const char *) buffer + (len & ~63);
175 5 : len &= 63;
176 : }
177 : }
178 :
179 : /* Move remaining bytes in internal buffer. */
180 455 : if (len > 0)
181 : {
182 18 : size_t left_over = ctx->buflen;
183 :
184 18 : memcpy (&ctx->buffer[left_over], buffer, len);
185 18 : left_over += len;
186 18 : if (left_over >= 64)
187 : {
188 0 : md5_process_block (ctx->buffer, 64, ctx);
189 0 : left_over -= 64;
190 0 : memcpy (ctx->buffer, &ctx->buffer[64], left_over);
191 : }
192 18 : ctx->buflen = left_over;
193 : }
194 455 : }
195 :
196 :
197 : /* These are the four functions used in the four steps of the MD5 algorithm
198 : and defined in the RFC 1321. The first function is a little bit optimized
199 : (as found in Colin Plumbs public domain implementation). */
200 : /* #define FF(b, c, d) ((b & c) | (~b & d)) */
201 : #define FF(b, c, d) (d ^ (b & (c ^ d)))
202 : #define FG(b, c, d) FF (d, b, c)
203 : #define FH(b, c, d) (b ^ c ^ d)
204 : #define FI(b, c, d) (c ^ (b | ~d))
205 :
206 : /* Process LEN bytes of BUFFER, accumulating context into CTX.
207 : It is assumed that LEN % 64 == 0. */
208 :
209 : void
210 : md5_process_block (buffer, len, ctx)
211 : const void *buffer;
212 : size_t len;
213 : struct MD5_CTX *ctx;
214 62 : {
215 : md5_uint32 correct_words[16];
216 62 : const md5_uint32 *words = buffer;
217 62 : size_t nwords = len / sizeof (md5_uint32);
218 62 : const md5_uint32 *endp = words + nwords;
219 62 : md5_uint32 A = ctx->A;
220 62 : md5_uint32 B = ctx->B;
221 62 : md5_uint32 C = ctx->C;
222 62 : md5_uint32 D = ctx->D;
223 :
224 : /* First increment the byte count. RFC 1321 specifies the possible
225 : length of the file up to 2^64 bits. Here we only compute the
226 : number of bytes. Do a double word increment. */
227 62 : ctx->total[0] += len;
228 62 : if (ctx->total[0] < len)
229 0 : ++ctx->total[1];
230 :
231 : /* Process all bytes in the buffer with 64 bytes in each round of
232 : the loop. */
233 559 : while (words < endp)
234 : {
235 435 : md5_uint32 *cwp = correct_words;
236 435 : md5_uint32 A_save = A;
237 435 : md5_uint32 B_save = B;
238 435 : md5_uint32 C_save = C;
239 435 : md5_uint32 D_save = D;
240 :
241 : /* First round: using the given function, the context and a constant
242 : the next context is computed. Because the algorithms processing
243 : unit is a 32-bit word and it is determined to work on words in
244 : little endian byte order we perhaps have to change the byte order
245 : before the computation. To reduce the work for the next steps
246 : we store the swapped words in the array CORRECT_WORDS. */
247 :
248 : #define OP(a, b, c, d, s, T) \
249 : do \
250 : { \
251 : a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
252 : ++words; \
253 : CYCLIC (a, s); \
254 : a += b; \
255 : } \
256 : while (0)
257 :
258 : /* It is unfortunate that C does not provide an operator for
259 : cyclic rotation. Hope the C compiler is smart enough. */
260 : #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
261 :
262 : /* Before we start, one word to the strange constants.
263 : They are defined in RFC 1321 as
264 :
265 : T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
266 : */
267 :
268 : /* Round 1. */
269 435 : OP (A, B, C, D, 7, 0xd76aa478);
270 435 : OP (D, A, B, C, 12, 0xe8c7b756);
271 435 : OP (C, D, A, B, 17, 0x242070db);
272 435 : OP (B, C, D, A, 22, 0xc1bdceee);
273 435 : OP (A, B, C, D, 7, 0xf57c0faf);
274 435 : OP (D, A, B, C, 12, 0x4787c62a);
275 435 : OP (C, D, A, B, 17, 0xa8304613);
276 435 : OP (B, C, D, A, 22, 0xfd469501);
277 435 : OP (A, B, C, D, 7, 0x698098d8);
278 435 : OP (D, A, B, C, 12, 0x8b44f7af);
279 435 : OP (C, D, A, B, 17, 0xffff5bb1);
280 435 : OP (B, C, D, A, 22, 0x895cd7be);
281 435 : OP (A, B, C, D, 7, 0x6b901122);
282 435 : OP (D, A, B, C, 12, 0xfd987193);
283 435 : OP (C, D, A, B, 17, 0xa679438e);
284 435 : OP (B, C, D, A, 22, 0x49b40821);
285 :
286 : /* For the second to fourth round we have the possibly swapped words
287 : in CORRECT_WORDS. Redefine the macro to take an additional first
288 : argument specifying the function to use. */
289 : #undef OP
290 : #define OP(f, a, b, c, d, k, s, T) \
291 : do \
292 : { \
293 : a += f (b, c, d) + correct_words[k] + T; \
294 : CYCLIC (a, s); \
295 : a += b; \
296 : } \
297 : while (0)
298 :
299 : /* Round 2. */
300 435 : OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
301 435 : OP (FG, D, A, B, C, 6, 9, 0xc040b340);
302 435 : OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
303 435 : OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
304 435 : OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
305 435 : OP (FG, D, A, B, C, 10, 9, 0x02441453);
306 435 : OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
307 435 : OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
308 435 : OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
309 435 : OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
310 435 : OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
311 435 : OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
312 435 : OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
313 435 : OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
314 435 : OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
315 435 : OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
316 :
317 : /* Round 3. */
318 435 : OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
319 435 : OP (FH, D, A, B, C, 8, 11, 0x8771f681);
320 435 : OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
321 435 : OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
322 435 : OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
323 435 : OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
324 435 : OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
325 435 : OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
326 435 : OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
327 435 : OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
328 435 : OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
329 435 : OP (FH, B, C, D, A, 6, 23, 0x04881d05);
330 435 : OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
331 435 : OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
332 435 : OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
333 435 : OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
334 :
335 : /* Round 4. */
336 435 : OP (FI, A, B, C, D, 0, 6, 0xf4292244);
337 435 : OP (FI, D, A, B, C, 7, 10, 0x432aff97);
338 435 : OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
339 435 : OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
340 435 : OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
341 435 : OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
342 435 : OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
343 435 : OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
344 435 : OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
345 435 : OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
346 435 : OP (FI, C, D, A, B, 6, 15, 0xa3014314);
347 435 : OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
348 435 : OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
349 435 : OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
350 435 : OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
351 435 : OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
352 :
353 : /* Add the starting values of the context. */
354 435 : A += A_save;
355 435 : B += B_save;
356 435 : C += C_save;
357 435 : D += D_save;
358 : }
359 :
360 : /* Put checksum in context given as argument. */
361 62 : ctx->A = A;
362 62 : ctx->B = B;
363 62 : ctx->C = C;
364 62 : ctx->D = D;
365 62 : }
|