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