27 #include <sys/types.h>
29 #if STDC_HEADERS || defined _LIBC
35 # define memcpy(d, s, n) bcopy ((s), (d), (n))
43 # if __BYTE_ORDER == __BIG_ENDIAN
44 # define WORDS_BIGENDIAN 1
48 #ifdef WORDS_BIGENDIAN
50 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
58 static const unsigned char fillbuf[64] = { 0x80, 0 };
72 ctx->total[0] = ctx->total[1] = 0;
82 md5_read_ctx (ctx, resbuf)
86 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
87 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
88 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
89 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
100 md5_finish_ctx (ctx, resbuf)
105 md5_uint32 bytes = ctx->buflen;
109 ctx->total[0] += bytes;
110 if (ctx->total[0] < bytes)
113 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
114 memcpy (&ctx->buffer[bytes], fillbuf, pad);
117 *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
118 *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
119 SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
122 md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
124 return md5_read_ctx (ctx, resbuf);
131 md5_stream (stream, resblock)
136 #define BLOCKSIZE 4096
138 char buffer[BLOCKSIZE + 72];
156 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
160 while (sum < BLOCKSIZE && n != 0);
161 if (n == 0 && ferror (stream))
171 md5_process_block (buffer, BLOCKSIZE, &ctx);
176 md5_process_bytes (buffer, sum, &ctx);
179 md5_finish_ctx (&ctx, resblock);
188 md5_buffer (buffer, len, resblock)
199 md5_process_bytes (buffer, len, &ctx);
202 return md5_finish_ctx (&ctx, resblock);
207 md5_process_bytes (buffer, len, ctx)
212 #define NUM_MD5_WORDS 1024
217 if (ctx->buflen != 0)
219 size_t left_over = ctx->buflen;
221 add = 128 - left_over > len ? len : 128 - left_over;
223 memcpy (&ctx->buffer[left_over], buffer, add);
226 if (left_over + add > 64)
228 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
230 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
231 (left_over + add) & 63);
232 ctx->buflen = (left_over + add) & 63;
235 buffer = (
const char *) buffer + add;
244 md5_process_block (buffer, len & ~63, ctx);
245 buffer = (
const char *) buffer + (len & ~63);
249 md5_uint32 md5_buffer[NUM_MD5_WORDS];
253 num_bytes = len & ~63;
254 while (num_bytes > 0)
256 buf_bytes = (num_bytes <
sizeof (md5_buffer)) ?
257 num_bytes :
sizeof (md5_buffer);
258 memcpy (md5_buffer, buffer, buf_bytes);
259 md5_process_block (md5_buffer, buf_bytes, ctx);
260 num_bytes -= buf_bytes;
261 buffer = (
const char *) buffer + buf_bytes;
271 memcpy (ctx->buffer, buffer, len);
281 #define FF(b, c, d) (d ^ (b & (c ^ d)))
282 #define FG(b, c, d) FF (d, b, c)
283 #define FH(b, c, d) (b ^ c ^ d)
284 #define FI(b, c, d) (c ^ (b | ~d))
290 md5_process_block (buffer, len, ctx)
295 md5_uint32 correct_words[16];
296 const md5_uint32 *words = buffer;
297 size_t nwords = len /
sizeof (md5_uint32);
298 const md5_uint32 *endp = words + nwords;
299 md5_uint32 A = ctx->A;
300 md5_uint32 B = ctx->B;
301 md5_uint32 C = ctx->C;
302 md5_uint32 D = ctx->D;
307 ctx->total[0] += len;
308 if (ctx->total[0] < len)
315 md5_uint32 *cwp = correct_words;
316 md5_uint32 A_save = A;
317 md5_uint32 B_save = B;
318 md5_uint32 C_save = C;
319 md5_uint32 D_save = D;
328 #define OP(a, b, c, d, s, T) \
331 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
340 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
349 OP (A, B, C, D, 7, 0xd76aa478);
350 OP (D, A, B, C, 12, 0xe8c7b756);
351 OP (C, D, A, B, 17, 0x242070db);
352 OP (B, C, D, A, 22, 0xc1bdceee);
353 OP (A, B, C, D, 7, 0xf57c0faf);
354 OP (D, A, B, C, 12, 0x4787c62a);
355 OP (C, D, A, B, 17, 0xa8304613);
356 OP (B, C, D, A, 22, 0xfd469501);
357 OP (A, B, C, D, 7, 0x698098d8);
358 OP (D, A, B, C, 12, 0x8b44f7af);
359 OP (C, D, A, B, 17, 0xffff5bb1);
360 OP (B, C, D, A, 22, 0x895cd7be);
361 OP (A, B, C, D, 7, 0x6b901122);
362 OP (D, A, B, C, 12, 0xfd987193);
363 OP (C, D, A, B, 17, 0xa679438e);
364 OP (B, C, D, A, 22, 0x49b40821);
370 #define OP(f, a, b, c, d, k, s, T) \
373 a += f (b, c, d) + correct_words[k] + T; \
380 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
381 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
382 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
383 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
384 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
385 OP (FG, D, A, B, C, 10, 9, 0x02441453);
386 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
387 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
388 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
389 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
390 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
391 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
392 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
393 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
394 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
395 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
398 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
399 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
400 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
401 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
402 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
403 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
404 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
405 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
406 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
407 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
408 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
409 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
410 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
411 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
412 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
413 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
416 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
417 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
418 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
419 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
420 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
421 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
422 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
423 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
424 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
425 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
426 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
427 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
428 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
429 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
430 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
431 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);