Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 1 | /* md5sum.c - Compute MD5 checksum of files or strings according to the |
| 2 | * definition of MD5 in RFC 1321 from April 1992. |
| 3 | * Copyright (C) 1995-1999 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2, or (at your option) |
| 8 | * any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> */ |
| 21 | /* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> */ |
| 22 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
| 26 | #include <ctype.h> |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 27 | #include <getopt.h> |
| 28 | |
| 29 | /* It turns out that libc5 doesn't have this in its headers |
| 30 | * even though it is actually in the lib. Force it to work */ |
| 31 | #if ! defined __GLIBC__ && ! defined __UCLIBC__ |
| 32 | #define getline __getline |
| 33 | extern _IO_ssize_t getline __P ((char **, size_t *, FILE *)); |
| 34 | #endif |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 35 | |
| 36 | //---------------------------------------------------------------------------- |
| 37 | //--------md5.c |
| 38 | //---------------------------------------------------------------------------- |
| 39 | |
| 40 | /* md5.c - Functions to compute MD5 message digest of files or memory blocks |
| 41 | * according to the definition of MD5 in RFC 1321 from April 1992. |
| 42 | * Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
| 43 | * |
| 44 | * NOTE: The canonical source of this file is maintained with the GNU C |
| 45 | * Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. |
| 46 | * |
| 47 | * This program is free software; you can redistribute it and/or modify it |
| 48 | * under the terms of the GNU General Public License as published by the |
| 49 | * Free Software Foundation; either version 2, or (at your option) any |
| 50 | * later version. |
| 51 | * |
| 52 | * This program is distributed in the hope that it will be useful, |
| 53 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 54 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 55 | * GNU General Public License for more details. |
| 56 | * |
| 57 | * You should have received a copy of the GNU General Public License |
| 58 | * along with this program; if not, write to the Free Software Foundation, |
| 59 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 60 | */ |
| 61 | |
| 62 | /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */ |
| 63 | |
| 64 | #include <sys/types.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <string.h> |
| 67 | #include <endian.h> |
| 68 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 69 | #include "busybox.h" |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 70 | //---------------------------------------------------------------------------- |
| 71 | //--------md5.h |
| 72 | //---------------------------------------------------------------------------- |
| 73 | |
| 74 | /* md5.h - Declaration of functions and data types used for MD5 sum |
| 75 | computing library functions. |
| 76 | Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
| 77 | NOTE: The canonical source of this file is maintained with the GNU C |
| 78 | Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. |
| 79 | |
| 80 | This program is free software; you can redistribute it and/or modify it |
| 81 | under the terms of the GNU General Public License as published by the |
| 82 | Free Software Foundation; either version 2, or (at your option) any |
| 83 | later version. |
| 84 | |
| 85 | This program is distributed in the hope that it will be useful, |
| 86 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 87 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 88 | GNU General Public License for more details. |
| 89 | |
| 90 | You should have received a copy of the GNU General Public License |
| 91 | along with this program; if not, write to the Free Software Foundation, |
| 92 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 93 | |
| 94 | #ifndef _MD5_H |
| 95 | #define _MD5_H 1 |
| 96 | |
| 97 | #include <stdio.h> |
| 98 | |
Pavel Roskin | 616d13b | 2000-07-28 19:38:27 +0000 | [diff] [blame] | 99 | #if defined HAVE_LIMITS_H || defined _LIBC |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 100 | # include <limits.h> |
| 101 | #endif |
| 102 | |
| 103 | /* The following contortions are an attempt to use the C preprocessor |
| 104 | to determine an unsigned integral type that is 32 bits wide. An |
| 105 | alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but |
| 106 | doing that would require that the configure script compile and *run* |
| 107 | the resulting executable. Locally running cross-compiled executables |
| 108 | is usually not possible. */ |
| 109 | |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 110 | # include <sys/types.h> |
| 111 | typedef u_int32_t md5_uint32; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 112 | |
| 113 | /* Structure to save state of computation between the single steps. */ |
| 114 | struct md5_ctx |
| 115 | { |
| 116 | md5_uint32 A; |
| 117 | md5_uint32 B; |
| 118 | md5_uint32 C; |
| 119 | md5_uint32 D; |
| 120 | |
| 121 | md5_uint32 total[2]; |
| 122 | md5_uint32 buflen; |
| 123 | char buffer[128]; |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * The following three functions are build up the low level used in |
| 128 | * the functions `md5_stream' and `md5_buffer'. |
| 129 | */ |
| 130 | |
| 131 | /* Initialize structure containing state of computation. |
| 132 | (RFC 1321, 3.3: Step 3) */ |
| 133 | extern void md5_init_ctx __P ((struct md5_ctx *ctx)); |
| 134 | |
| 135 | /* Starting with the result of former calls of this function (or the |
| 136 | initialization function update the context for the next LEN bytes |
| 137 | starting at BUFFER. |
| 138 | It is necessary that LEN is a multiple of 64!!! */ |
| 139 | extern void md5_process_block __P ((const void *buffer, size_t len, |
| 140 | struct md5_ctx *ctx)); |
| 141 | |
| 142 | /* Starting with the result of former calls of this function (or the |
| 143 | initialization function update the context for the next LEN bytes |
| 144 | starting at BUFFER. |
| 145 | It is NOT required that LEN is a multiple of 64. */ |
| 146 | extern void md5_process_bytes __P ((const void *buffer, size_t len, |
| 147 | struct md5_ctx *ctx)); |
| 148 | |
| 149 | /* Process the remaining bytes in the buffer and put result from CTX |
| 150 | in first 16 bytes following RESBUF. The result is always in little |
| 151 | endian byte order, so that a byte-wise output yields to the wanted |
| 152 | ASCII representation of the message digest. |
| 153 | |
| 154 | IMPORTANT: On some systems it is required that RESBUF is correctly |
| 155 | aligned for a 32 bits value. */ |
| 156 | extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); |
| 157 | |
| 158 | |
| 159 | /* Put result from CTX in first 16 bytes following RESBUF. The result is |
| 160 | always in little endian byte order, so that a byte-wise output yields |
| 161 | to the wanted ASCII representation of the message digest. |
| 162 | |
| 163 | IMPORTANT: On some systems it is required that RESBUF is correctly |
| 164 | aligned for a 32 bits value. */ |
| 165 | extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); |
| 166 | |
| 167 | |
| 168 | /* Compute MD5 message digest for bytes read from STREAM. The |
| 169 | resulting message digest number will be written into the 16 bytes |
| 170 | beginning at RESBLOCK. */ |
| 171 | extern int md5_stream __P ((FILE *stream, void *resblock)); |
| 172 | |
| 173 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The |
| 174 | result is always in little endian byte order, so that a byte-wise |
| 175 | output yields to the wanted ASCII representation of the message |
| 176 | digest. */ |
| 177 | extern void *md5_buffer __P ((const char *buffer, size_t len, void *resblock)); |
| 178 | |
| 179 | #endif |
| 180 | |
| 181 | //---------------------------------------------------------------------------- |
| 182 | //--------end of md5.h |
| 183 | //---------------------------------------------------------------------------- |
| 184 | |
| 185 | #define SWAP(n) (n) |
| 186 | |
| 187 | /* This array contains the bytes used to pad the buffer to the next |
| 188 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ |
| 189 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; |
| 190 | |
| 191 | /* Initialize structure containing state of computation. |
| 192 | (RFC 1321, 3.3: Step 3) */ |
| 193 | void md5_init_ctx(struct md5_ctx *ctx) |
| 194 | { |
| 195 | ctx->A = 0x67452301; |
| 196 | ctx->B = 0xefcdab89; |
| 197 | ctx->C = 0x98badcfe; |
| 198 | ctx->D = 0x10325476; |
| 199 | |
| 200 | ctx->total[0] = ctx->total[1] = 0; |
| 201 | ctx->buflen = 0; |
| 202 | } |
| 203 | |
| 204 | /* Put result from CTX in first 16 bytes following RESBUF. The result |
| 205 | must be in little endian byte order. |
| 206 | |
| 207 | IMPORTANT: On some systems it is required that RESBUF is correctly |
| 208 | aligned for a 32 bits value. */ |
| 209 | void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf) |
| 210 | { |
| 211 | ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A); |
| 212 | ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B); |
| 213 | ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C); |
| 214 | ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D); |
| 215 | |
| 216 | return resbuf; |
| 217 | } |
| 218 | |
| 219 | /* Process the remaining bytes in the internal buffer and the usual |
| 220 | prolog according to the standard and write the result to RESBUF. |
| 221 | |
| 222 | IMPORTANT: On some systems it is required that RESBUF is correctly |
| 223 | aligned for a 32 bits value. */ |
| 224 | void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf) |
| 225 | { |
| 226 | /* Take yet unprocessed bytes into account. */ |
| 227 | md5_uint32 bytes = ctx->buflen; |
| 228 | size_t pad; |
| 229 | |
| 230 | /* Now count remaining bytes. */ |
| 231 | ctx->total[0] += bytes; |
| 232 | if (ctx->total[0] < bytes) |
| 233 | ++ctx->total[1]; |
| 234 | |
| 235 | pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; |
| 236 | memcpy(&ctx->buffer[bytes], fillbuf, pad); |
| 237 | |
| 238 | /* Put the 64-bit file length in *bits* at the end of the buffer. */ |
| 239 | *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3); |
| 240 | *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] = |
| 241 | SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29)); |
| 242 | |
| 243 | /* Process last bytes. */ |
| 244 | md5_process_block(ctx->buffer, bytes + pad + 8, ctx); |
| 245 | |
| 246 | return md5_read_ctx(ctx, resbuf); |
| 247 | } |
| 248 | |
| 249 | /* Compute MD5 message digest for bytes read from STREAM. The |
| 250 | resulting message digest number will be written into the 16 bytes |
| 251 | beginning at RESBLOCK. */ |
| 252 | int md5_stream(FILE *stream, void *resblock) |
| 253 | { |
| 254 | /* Important: BLOCKSIZE must be a multiple of 64. */ |
| 255 | #define BLOCKSIZE 4096 |
| 256 | struct md5_ctx ctx; |
| 257 | char buffer[BLOCKSIZE + 72]; |
| 258 | size_t sum; |
| 259 | |
| 260 | /* Initialize the computation context. */ |
| 261 | md5_init_ctx(&ctx); |
| 262 | |
| 263 | /* Iterate over full file contents. */ |
| 264 | while (1) { |
| 265 | /* We read the file in blocks of BLOCKSIZE bytes. One call of the |
| 266 | computation function processes the whole buffer so that with the |
| 267 | next round of the loop another block can be read. */ |
| 268 | size_t n; |
| 269 | sum = 0; |
| 270 | |
| 271 | /* Read block. Take care for partial reads. */ |
| 272 | do { |
| 273 | n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); |
| 274 | |
| 275 | sum += n; |
| 276 | } |
| 277 | while (sum < BLOCKSIZE && n != 0); |
| 278 | if (n == 0 && ferror(stream)) |
| 279 | return 1; |
| 280 | |
| 281 | /* If end of file is reached, end the loop. */ |
| 282 | if (n == 0) |
| 283 | break; |
| 284 | |
| 285 | /* Process buffer with BLOCKSIZE bytes. Note that |
| 286 | BLOCKSIZE % 64 == 0 |
| 287 | */ |
| 288 | md5_process_block(buffer, BLOCKSIZE, &ctx); |
| 289 | } |
| 290 | |
| 291 | /* Add the last bytes if necessary. */ |
| 292 | if (sum > 0) |
| 293 | md5_process_bytes(buffer, sum, &ctx); |
| 294 | |
| 295 | /* Construct result in desired memory. */ |
| 296 | md5_finish_ctx(&ctx, resblock); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The |
| 301 | result is always in little endian byte order, so that a byte-wise |
| 302 | output yields to the wanted ASCII representation of the message |
| 303 | digest. */ |
| 304 | void *md5_buffer(const char *buffer, size_t len, void *resblock) |
| 305 | { |
| 306 | struct md5_ctx ctx; |
| 307 | |
| 308 | /* Initialize the computation context. */ |
| 309 | md5_init_ctx(&ctx); |
| 310 | |
| 311 | /* Process whole buffer but last len % 64 bytes. */ |
| 312 | md5_process_bytes(buffer, len, &ctx); |
| 313 | |
| 314 | /* Put result in desired memory area. */ |
| 315 | return md5_finish_ctx(&ctx, resblock); |
| 316 | } |
| 317 | |
| 318 | void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx) |
| 319 | { |
| 320 | /* When we already have some bits in our internal buffer concatenate |
| 321 | both inputs first. */ |
| 322 | if (ctx->buflen != 0) { |
| 323 | size_t left_over = ctx->buflen; |
| 324 | size_t add = 128 - left_over > len ? len : 128 - left_over; |
| 325 | |
| 326 | memcpy(&ctx->buffer[left_over], buffer, add); |
| 327 | ctx->buflen += add; |
| 328 | |
| 329 | if (left_over + add > 64) { |
| 330 | md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx); |
| 331 | /* The regions in the following copy operation cannot overlap. */ |
| 332 | memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63], |
| 333 | (left_over + add) & 63); |
| 334 | ctx->buflen = (left_over + add) & 63; |
| 335 | } |
| 336 | |
| 337 | buffer = (const char *) buffer + add; |
| 338 | len -= add; |
| 339 | } |
| 340 | |
| 341 | /* Process available complete blocks. */ |
| 342 | if (len > 64) { |
| 343 | md5_process_block(buffer, len & ~63, ctx); |
| 344 | buffer = (const char *) buffer + (len & ~63); |
| 345 | len &= 63; |
| 346 | } |
| 347 | |
| 348 | /* Move remaining bytes in internal buffer. */ |
| 349 | if (len > 0) { |
| 350 | memcpy(ctx->buffer, buffer, len); |
| 351 | ctx->buflen = len; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* These are the four functions used in the four steps of the MD5 algorithm |
| 356 | and defined in the RFC 1321. The first function is a little bit optimized |
| 357 | (as found in Colin Plumbs public domain implementation). */ |
| 358 | /* #define FF(b, c, d) ((b & c) | (~b & d)) */ |
| 359 | #define FF(b, c, d) (d ^ (b & (c ^ d))) |
| 360 | #define FG(b, c, d) FF (d, b, c) |
| 361 | #define FH(b, c, d) (b ^ c ^ d) |
| 362 | #define FI(b, c, d) (c ^ (b | ~d)) |
| 363 | |
| 364 | /* Process LEN bytes of BUFFER, accumulating context into CTX. |
| 365 | It is assumed that LEN % 64 == 0. */ |
| 366 | void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx) |
| 367 | { |
| 368 | md5_uint32 correct_words[16]; |
| 369 | const md5_uint32 *words = buffer; |
| 370 | size_t nwords = len / sizeof(md5_uint32); |
| 371 | const md5_uint32 *endp = words + nwords; |
| 372 | md5_uint32 A = ctx->A; |
| 373 | md5_uint32 B = ctx->B; |
| 374 | md5_uint32 C = ctx->C; |
| 375 | md5_uint32 D = ctx->D; |
| 376 | |
| 377 | /* First increment the byte count. RFC 1321 specifies the possible |
| 378 | length of the file up to 2^64 bits. Here we only compute the |
| 379 | number of bytes. Do a double word increment. */ |
| 380 | ctx->total[0] += len; |
| 381 | if (ctx->total[0] < len) |
| 382 | ++ctx->total[1]; |
| 383 | |
| 384 | /* Process all bytes in the buffer with 64 bytes in each round of |
| 385 | the loop. */ |
| 386 | while (words < endp) { |
| 387 | md5_uint32 *cwp = correct_words; |
| 388 | md5_uint32 A_save = A; |
| 389 | md5_uint32 B_save = B; |
| 390 | md5_uint32 C_save = C; |
| 391 | md5_uint32 D_save = D; |
| 392 | |
| 393 | /* First round: using the given function, the context and a constant |
| 394 | the next context is computed. Because the algorithms processing |
| 395 | unit is a 32-bit word and it is determined to work on words in |
| 396 | little endian byte order we perhaps have to change the byte order |
| 397 | before the computation. To reduce the work for the next steps |
| 398 | we store the swapped words in the array CORRECT_WORDS. */ |
| 399 | |
| 400 | #define OP(a, b, c, d, s, T) \ |
| 401 | do \ |
| 402 | { \ |
| 403 | a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ |
| 404 | ++words; \ |
| 405 | CYCLIC (a, s); \ |
| 406 | a += b; \ |
| 407 | } \ |
| 408 | while (0) |
| 409 | |
| 410 | /* It is unfortunate that C does not provide an operator for |
| 411 | cyclic rotation. Hope the C compiler is smart enough. */ |
| 412 | #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) |
| 413 | |
| 414 | /* Before we start, one word to the strange constants. |
| 415 | They are defined in RFC 1321 as |
| 416 | |
| 417 | T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 |
| 418 | */ |
| 419 | |
| 420 | /* Round 1. */ |
| 421 | OP(A, B, C, D, 7, 0xd76aa478); |
| 422 | OP(D, A, B, C, 12, 0xe8c7b756); |
| 423 | OP(C, D, A, B, 17, 0x242070db); |
| 424 | OP(B, C, D, A, 22, 0xc1bdceee); |
| 425 | OP(A, B, C, D, 7, 0xf57c0faf); |
| 426 | OP(D, A, B, C, 12, 0x4787c62a); |
| 427 | OP(C, D, A, B, 17, 0xa8304613); |
| 428 | OP(B, C, D, A, 22, 0xfd469501); |
| 429 | OP(A, B, C, D, 7, 0x698098d8); |
| 430 | OP(D, A, B, C, 12, 0x8b44f7af); |
| 431 | OP(C, D, A, B, 17, 0xffff5bb1); |
| 432 | OP(B, C, D, A, 22, 0x895cd7be); |
| 433 | OP(A, B, C, D, 7, 0x6b901122); |
| 434 | OP(D, A, B, C, 12, 0xfd987193); |
| 435 | OP(C, D, A, B, 17, 0xa679438e); |
| 436 | OP(B, C, D, A, 22, 0x49b40821); |
| 437 | |
| 438 | /* For the second to fourth round we have the possibly swapped words |
| 439 | in CORRECT_WORDS. Redefine the macro to take an additional first |
| 440 | argument specifying the function to use. */ |
| 441 | #undef OP |
| 442 | #define OP(f, a, b, c, d, k, s, T) \ |
| 443 | do \ |
| 444 | { \ |
| 445 | a += f (b, c, d) + correct_words[k] + T; \ |
| 446 | CYCLIC (a, s); \ |
| 447 | a += b; \ |
| 448 | } \ |
| 449 | while (0) |
| 450 | |
| 451 | /* Round 2. */ |
| 452 | OP(FG, A, B, C, D, 1, 5, 0xf61e2562); |
| 453 | OP(FG, D, A, B, C, 6, 9, 0xc040b340); |
| 454 | OP(FG, C, D, A, B, 11, 14, 0x265e5a51); |
| 455 | OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa); |
| 456 | OP(FG, A, B, C, D, 5, 5, 0xd62f105d); |
| 457 | OP(FG, D, A, B, C, 10, 9, 0x02441453); |
| 458 | OP(FG, C, D, A, B, 15, 14, 0xd8a1e681); |
| 459 | OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8); |
| 460 | OP(FG, A, B, C, D, 9, 5, 0x21e1cde6); |
| 461 | OP(FG, D, A, B, C, 14, 9, 0xc33707d6); |
| 462 | OP(FG, C, D, A, B, 3, 14, 0xf4d50d87); |
| 463 | OP(FG, B, C, D, A, 8, 20, 0x455a14ed); |
| 464 | OP(FG, A, B, C, D, 13, 5, 0xa9e3e905); |
| 465 | OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8); |
| 466 | OP(FG, C, D, A, B, 7, 14, 0x676f02d9); |
| 467 | OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a); |
| 468 | |
| 469 | /* Round 3. */ |
| 470 | OP(FH, A, B, C, D, 5, 4, 0xfffa3942); |
| 471 | OP(FH, D, A, B, C, 8, 11, 0x8771f681); |
| 472 | OP(FH, C, D, A, B, 11, 16, 0x6d9d6122); |
| 473 | OP(FH, B, C, D, A, 14, 23, 0xfde5380c); |
| 474 | OP(FH, A, B, C, D, 1, 4, 0xa4beea44); |
| 475 | OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9); |
| 476 | OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60); |
| 477 | OP(FH, B, C, D, A, 10, 23, 0xbebfbc70); |
| 478 | OP(FH, A, B, C, D, 13, 4, 0x289b7ec6); |
| 479 | OP(FH, D, A, B, C, 0, 11, 0xeaa127fa); |
| 480 | OP(FH, C, D, A, B, 3, 16, 0xd4ef3085); |
| 481 | OP(FH, B, C, D, A, 6, 23, 0x04881d05); |
| 482 | OP(FH, A, B, C, D, 9, 4, 0xd9d4d039); |
| 483 | OP(FH, D, A, B, C, 12, 11, 0xe6db99e5); |
| 484 | OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8); |
| 485 | OP(FH, B, C, D, A, 2, 23, 0xc4ac5665); |
| 486 | |
| 487 | /* Round 4. */ |
| 488 | OP(FI, A, B, C, D, 0, 6, 0xf4292244); |
| 489 | OP(FI, D, A, B, C, 7, 10, 0x432aff97); |
| 490 | OP(FI, C, D, A, B, 14, 15, 0xab9423a7); |
| 491 | OP(FI, B, C, D, A, 5, 21, 0xfc93a039); |
| 492 | OP(FI, A, B, C, D, 12, 6, 0x655b59c3); |
| 493 | OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92); |
| 494 | OP(FI, C, D, A, B, 10, 15, 0xffeff47d); |
| 495 | OP(FI, B, C, D, A, 1, 21, 0x85845dd1); |
| 496 | OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f); |
| 497 | OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0); |
| 498 | OP(FI, C, D, A, B, 6, 15, 0xa3014314); |
| 499 | OP(FI, B, C, D, A, 13, 21, 0x4e0811a1); |
| 500 | OP(FI, A, B, C, D, 4, 6, 0xf7537e82); |
| 501 | OP(FI, D, A, B, C, 11, 10, 0xbd3af235); |
| 502 | OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb); |
| 503 | OP(FI, B, C, D, A, 9, 21, 0xeb86d391); |
| 504 | |
| 505 | /* Add the starting values of the context. */ |
| 506 | A += A_save; |
| 507 | B += B_save; |
| 508 | C += C_save; |
| 509 | D += D_save; |
| 510 | } |
| 511 | |
| 512 | /* Put checksum in context given as argument. */ |
| 513 | ctx->A = A; |
| 514 | ctx->B = B; |
| 515 | ctx->C = C; |
| 516 | ctx->D = D; |
| 517 | } |
| 518 | |
| 519 | //---------------------------------------------------------------------------- |
| 520 | //--------end of md5.c |
| 521 | //---------------------------------------------------------------------------- |
| 522 | |
| 523 | #define ISWHITE(c) ((c) == ' ' || (c) == '\t') |
| 524 | #define IN_CTYPE_DOMAIN(c) 1 |
| 525 | #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c)) |
| 526 | #define STREQ(a, b) (strcmp ((a), (b)) == 0) |
| 527 | #define TOLOWER(Ch) tolower (Ch) |
| 528 | #define OPENOPTS(BINARY) "r" |
| 529 | |
| 530 | /* The minimum length of a valid digest line in a file produced |
| 531 | by `md5sum FILE' and read by `md5sum -c'. This length does |
| 532 | not include any newline character at the end of a line. */ |
| 533 | #define MIN_DIGEST_LINE_LENGTH 35 /* 32 - message digest length |
| 534 | 2 - blank and binary indicator |
| 535 | 1 - minimum filename length */ |
| 536 | |
| 537 | static int have_read_stdin; /* Nonzero if any of the files read were |
| 538 | the standard input. */ |
| 539 | |
| 540 | static int status_only = 0; /* With -c, don't generate any output. |
| 541 | The exit code indicates success or failure */ |
| 542 | static int warn = 0; /* With -w, print a message to standard error warning |
| 543 | about each improperly formatted MD5 checksum line */ |
| 544 | |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 545 | static int split_3(char *s, |
| 546 | size_t s_len, |
| 547 | unsigned char **u, |
| 548 | int *binary, |
| 549 | char **w) |
| 550 | { |
| 551 | size_t i = 0; |
| 552 | int escaped_filename = 0; |
| 553 | |
| 554 | while (ISWHITE(s[i])) |
| 555 | ++i; |
| 556 | |
| 557 | /* The line must have at least 35 (36 if the first is a backslash) |
| 558 | more characters to contain correct message digest information. |
| 559 | Ignore this line if it is too short. */ |
| 560 | if (!(s_len - i >= MIN_DIGEST_LINE_LENGTH |
| 561 | || (s[i] == '\\' && s_len - i >= 1 + MIN_DIGEST_LINE_LENGTH))) |
| 562 | return FALSE; |
| 563 | |
| 564 | if (s[i] == '\\') { |
| 565 | ++i; |
| 566 | escaped_filename = 1; |
| 567 | } |
| 568 | *u = (unsigned char *) &s[i]; |
| 569 | |
| 570 | /* The first field has to be the 32-character hexadecimal |
| 571 | representation of the message digest. If it is not followed |
| 572 | immediately by a white space it's an error. */ |
| 573 | i += 32; |
| 574 | if (!ISWHITE(s[i])) |
| 575 | return FALSE; |
| 576 | |
| 577 | s[i++] = '\0'; |
| 578 | |
| 579 | if (s[i] != ' ' && s[i] != '*') |
| 580 | return FALSE; |
| 581 | *binary = (s[i++] == '*'); |
| 582 | |
| 583 | /* All characters between the type indicator and end of line are |
| 584 | significant -- that includes leading and trailing white space. */ |
| 585 | *w = &s[i]; |
| 586 | |
| 587 | if (escaped_filename) { |
| 588 | /* Translate each `\n' string in the file name to a NEWLINE, |
| 589 | and each `\\' string to a backslash. */ |
| 590 | |
| 591 | char *dst = &s[i]; |
| 592 | |
| 593 | while (i < s_len) { |
| 594 | switch (s[i]) { |
| 595 | case '\\': |
| 596 | if (i == s_len - 1) { |
| 597 | /* A valid line does not end with a backslash. */ |
| 598 | return FALSE; |
| 599 | } |
| 600 | ++i; |
| 601 | switch (s[i++]) { |
| 602 | case 'n': |
| 603 | *dst++ = '\n'; |
| 604 | break; |
| 605 | case '\\': |
| 606 | *dst++ = '\\'; |
| 607 | break; |
| 608 | default: |
| 609 | /* Only `\' or `n' may follow a backslash. */ |
| 610 | return FALSE; |
| 611 | } |
| 612 | break; |
| 613 | |
| 614 | case '\0': |
| 615 | /* The file name may not contain a NUL. */ |
| 616 | return FALSE; |
| 617 | break; |
| 618 | |
| 619 | default: |
| 620 | *dst++ = s[i++]; |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | *dst = '\0'; |
| 625 | } |
| 626 | return TRUE; |
| 627 | } |
| 628 | |
| 629 | static int hex_digits(unsigned char const *s) |
| 630 | { |
| 631 | while (*s) { |
| 632 | if (!ISXDIGIT(*s)) |
| 633 | return TRUE; |
| 634 | ++s; |
| 635 | } |
| 636 | return FALSE; |
| 637 | } |
| 638 | |
| 639 | /* An interface to md5_stream. Operate on FILENAME (it may be "-") and |
| 640 | put the result in *MD5_RESULT. Return non-zero upon failure, zero |
| 641 | to indicate success. */ |
| 642 | static int md5_file(const char *filename, |
| 643 | int binary, |
| 644 | unsigned char *md5_result) |
| 645 | { |
| 646 | FILE *fp; |
| 647 | |
| 648 | if (STREQ(filename, "-")) { |
| 649 | have_read_stdin = 1; |
| 650 | fp = stdin; |
| 651 | } else { |
| 652 | fp = fopen(filename, OPENOPTS(binary)); |
| 653 | if (fp == NULL) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 654 | errorMsg("%s: %s\n", filename, strerror(errno)); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 655 | return FALSE; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if (md5_stream(fp, md5_result)) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 660 | errorMsg("%s: %s\n", filename, strerror(errno)); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 661 | |
| 662 | if (fp != stdin) |
| 663 | fclose(fp); |
| 664 | return FALSE; |
| 665 | } |
| 666 | |
| 667 | if (fp != stdin && fclose(fp) == EOF) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 668 | errorMsg("%s: %s\n", filename, strerror(errno)); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 669 | return FALSE; |
| 670 | } |
| 671 | |
| 672 | return TRUE; |
| 673 | } |
| 674 | |
| 675 | static int md5_check(const char *checkfile_name) |
| 676 | { |
| 677 | FILE *checkfile_stream; |
| 678 | int n_properly_formated_lines = 0; |
| 679 | int n_mismatched_checksums = 0; |
| 680 | int n_open_or_read_failures = 0; |
| 681 | unsigned char md5buffer[16]; |
| 682 | size_t line_number; |
| 683 | char *line; |
| 684 | size_t line_chars_allocated; |
| 685 | |
| 686 | if (STREQ(checkfile_name, "-")) { |
| 687 | have_read_stdin = 1; |
| 688 | checkfile_stream = stdin; |
| 689 | } else { |
| 690 | checkfile_stream = fopen(checkfile_name, "r"); |
| 691 | if (checkfile_stream == NULL) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 692 | errorMsg("%s: %s\n", checkfile_name, strerror(errno)); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 693 | return FALSE; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | line_number = 0; |
| 698 | line = 0; |
| 699 | line_chars_allocated = 0; |
| 700 | |
| 701 | do { |
| 702 | char *filename; |
| 703 | int binary; |
| 704 | unsigned char *md5num; |
| 705 | int line_length; |
| 706 | |
| 707 | ++line_number; |
| 708 | |
| 709 | line_length = getline(&line, &line_chars_allocated, checkfile_stream); |
| 710 | |
| 711 | if (line_length <= 0) |
| 712 | break; |
| 713 | |
| 714 | /* Ignore comment lines, which begin with a '#' character. */ |
| 715 | if (line[0] == '#') |
| 716 | continue; |
| 717 | |
| 718 | /* Remove any trailing newline. */ |
| 719 | if (line[line_length - 1] == '\n') |
| 720 | line[--line_length] = '\0'; |
| 721 | |
| 722 | if (split_3(line, line_length, &md5num, &binary, &filename) |
| 723 | || !hex_digits(md5num)) { |
| 724 | if (warn) { |
| 725 | errorMsg("%s: %lu: improperly formatted MD5 checksum line\n", |
| 726 | checkfile_name, (unsigned long) line_number); |
| 727 | } |
| 728 | } else { |
| 729 | static const char bin2hex[] = { |
| 730 | '0', '1', '2', '3', |
| 731 | '4', '5', '6', '7', |
| 732 | '8', '9', 'a', 'b', |
| 733 | 'c', 'd', 'e', 'f' |
| 734 | }; |
| 735 | |
| 736 | ++n_properly_formated_lines; |
| 737 | |
| 738 | if (md5_file(filename, binary, md5buffer)) { |
| 739 | ++n_open_or_read_failures; |
| 740 | if (!status_only) { |
| 741 | printf("%s: FAILED open or read\n", filename); |
| 742 | fflush(stdout); |
| 743 | } |
| 744 | } else { |
| 745 | size_t cnt; |
| 746 | /* Compare generated binary number with text representation |
| 747 | in check file. Ignore case of hex digits. */ |
| 748 | for (cnt = 0; cnt < 16; ++cnt) { |
| 749 | if (TOLOWER(md5num[2 * cnt]) |
| 750 | != bin2hex[md5buffer[cnt] >> 4] |
| 751 | || (TOLOWER(md5num[2 * cnt + 1]) |
| 752 | != (bin2hex[md5buffer[cnt] & 0xf]))) |
| 753 | break; |
| 754 | } |
| 755 | if (cnt != 16) |
| 756 | ++n_mismatched_checksums; |
| 757 | |
| 758 | if (!status_only) { |
| 759 | printf("%s: %s\n", filename, |
| 760 | (cnt != 16 ? "FAILED" : "OK")); |
| 761 | fflush(stdout); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | while (!feof(checkfile_stream) && !ferror(checkfile_stream)); |
| 768 | |
| 769 | if (line) |
| 770 | free(line); |
| 771 | |
| 772 | if (ferror(checkfile_stream)) { |
Matt Kraai | 207061a | 2000-10-23 18:03:46 +0000 | [diff] [blame] | 773 | errorMsg("%s: read error\n", checkfile_name); /* */ |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 774 | return FALSE; |
| 775 | } |
| 776 | |
| 777 | if (checkfile_stream != stdin && fclose(checkfile_stream) == EOF) { |
| 778 | errorMsg("md5sum: %s: %s\n", checkfile_name, strerror(errno)); |
| 779 | return FALSE; |
| 780 | } |
| 781 | |
| 782 | if (n_properly_formated_lines == 0) { |
| 783 | /* Warn if no tests are found. */ |
| 784 | errorMsg("%s: no properly formatted MD5 checksum lines found\n", |
| 785 | checkfile_name); |
| 786 | return FALSE; |
| 787 | } else { |
| 788 | if (!status_only) { |
| 789 | int n_computed_checkums = (n_properly_formated_lines |
| 790 | - n_open_or_read_failures); |
| 791 | |
| 792 | if (n_open_or_read_failures > 0) { |
| 793 | errorMsg("WARNING: %d of %d listed files could not be read\n", |
| 794 | n_open_or_read_failures, n_properly_formated_lines); |
| 795 | return FALSE; |
| 796 | } |
| 797 | |
| 798 | if (n_mismatched_checksums > 0) { |
| 799 | errorMsg("WARNING: %d of %d computed checksums did NOT match\n", |
| 800 | n_mismatched_checksums, n_computed_checkums); |
| 801 | return FALSE; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0 |
| 807 | && n_open_or_read_failures == 0) ? 0 : 1); |
| 808 | } |
| 809 | |
| 810 | int md5sum_main(int argc, |
| 811 | char **argv) |
| 812 | { |
| 813 | unsigned char md5buffer[16]; |
| 814 | int do_check = 0; |
| 815 | int opt; |
| 816 | char **string = NULL; |
| 817 | size_t n_strings = 0; |
| 818 | size_t err = 0; |
| 819 | int file_type_specified = 0; |
| 820 | int binary = 0; |
| 821 | |
| 822 | while ((opt = getopt(argc, argv, "g:bcstw")) != -1) { |
| 823 | switch (opt) { |
| 824 | case 'g': { /* read a string */ |
| 825 | if (string == NULL) |
| 826 | string = (char **) xmalloc ((argc - 1) * sizeof (char *)); |
| 827 | |
| 828 | if (optarg == NULL) |
| 829 | optarg = ""; |
| 830 | string[n_strings++] = optarg; |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | case 'b': /* read files in binary mode */ |
| 835 | file_type_specified = 1; |
| 836 | binary = 1; |
| 837 | break; |
| 838 | |
| 839 | case 'c': /* check MD5 sums against given list */ |
| 840 | do_check = 1; |
| 841 | break; |
| 842 | |
| 843 | case 's': /* don't output anything, status code shows success */ |
| 844 | status_only = 1; |
| 845 | warn = 0; |
| 846 | break; |
| 847 | |
| 848 | case 't': /* read files in text mode (default) */ |
| 849 | file_type_specified = 1; |
| 850 | binary = 0; |
| 851 | break; |
| 852 | |
| 853 | case 'w': /* warn about improperly formated MD5 checksum lines */ |
| 854 | status_only = 0; |
| 855 | warn = 1; |
| 856 | break; |
| 857 | |
| 858 | default: |
| 859 | usage(md5sum_usage); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | if (file_type_specified && do_check) { |
| 864 | errorMsg("the -b and -t options are meaningless when verifying checksums\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 865 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | if (n_strings > 0 && do_check) { |
| 869 | errorMsg("the -g and -c options are mutually exclusive\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 870 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | if (status_only && !do_check) { |
| 874 | errorMsg("the -s option is meaningful only when verifying checksums\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 875 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | if (warn && !do_check) { |
| 879 | errorMsg("the -w option is meaningful only when verifying checksums\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 880 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if (n_strings > 0) { |
| 884 | size_t i; |
| 885 | |
| 886 | if (optind < argc) { |
| 887 | errorMsg("no files may be specified when using -g\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 888 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 889 | } |
| 890 | for (i = 0; i < n_strings; ++i) { |
| 891 | size_t cnt; |
| 892 | md5_buffer (string[i], strlen (string[i]), md5buffer); |
| 893 | |
| 894 | for (cnt = 0; cnt < 16; ++cnt) |
| 895 | printf ("%02x", md5buffer[cnt]); |
| 896 | |
| 897 | printf (" \"%s\"\n", string[i]); |
| 898 | } |
| 899 | } else if (do_check) { |
| 900 | if (optind + 1 < argc) { |
| 901 | errorMsg("only one argument may be specified when using -c\n"); |
| 902 | } |
| 903 | |
| 904 | err = md5_check ((optind == argc) ? "-" : argv[optind]); |
| 905 | } else { |
| 906 | if (optind == argc) |
| 907 | argv[argc++] = "-"; |
| 908 | |
| 909 | for (; optind < argc; ++optind) { |
| 910 | int fail; |
| 911 | char *file = argv[optind]; |
| 912 | |
| 913 | fail = md5_file (file, binary, md5buffer); |
| 914 | err |= fail; |
| 915 | if (!fail) { |
| 916 | size_t i; |
| 917 | /* Output a leading backslash if the file name contains |
| 918 | a newline or backslash. */ |
| 919 | if (strchr (file, '\n') || strchr (file, '\\')) |
| 920 | putchar ('\\'); |
| 921 | |
| 922 | for (i = 0; i < 16; ++i) |
| 923 | printf ("%02x", md5buffer[i]); |
| 924 | |
| 925 | putchar (' '); |
| 926 | if (binary) |
| 927 | putchar ('*'); |
| 928 | else |
| 929 | putchar (' '); |
| 930 | |
| 931 | /* Translate each NEWLINE byte to the string, "\\n", |
| 932 | and each backslash to "\\\\". */ |
| 933 | for (i = 0; i < strlen (file); ++i) { |
| 934 | switch (file[i]) { |
| 935 | case '\n': |
| 936 | fputs ("\\n", stdout); |
| 937 | break; |
| 938 | |
| 939 | case '\\': |
| 940 | fputs ("\\\\", stdout); |
| 941 | break; |
| 942 | |
| 943 | default: |
| 944 | putchar (file[i]); |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | putchar ('\n'); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | if (fclose (stdout) == EOF) { |
Matt Kraai | 207061a | 2000-10-23 18:03:46 +0000 | [diff] [blame] | 954 | errorMsg("write error\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 955 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | if (have_read_stdin && fclose (stdin) == EOF) { |
Matt Kraai | 207061a | 2000-10-23 18:03:46 +0000 | [diff] [blame] | 959 | errorMsg("standard input\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 960 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 963 | if (err == 0) |
| 964 | return EXIT_SUCCESS; |
| 965 | else |
| 966 | return EXIT_FAILURE; |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 967 | } |