1 /* $OpenBSD: md5.c,v 1.77 2014/09/13 16:06:36 doug Exp $ */ 2 3 /* 4 * Copyright (c) 2001,2003,2005-2007,2010,2013,2014 5 * Todd C. Miller <Todd.Miller@courtesan.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * Sponsored in part by the Defense Advanced Research Projects 20 * Agency (DARPA) and Air Force Research Laboratory, Air Force 21 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 22 */ 23 24 #include <sys/types.h> 25 #include <sys/queue.h> 26 #include <netinet/in.h> 27 #include <ctype.h> 28 #include <err.h> 29 #include <fcntl.h> 30 #include <resolv.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <limits.h> 35 #include <time.h> 36 #include <unistd.h> 37 #include <errno.h> 38 39 #include <md5.h> 40 #include <rmd160.h> 41 #include <sha1.h> 42 #include <sha2.h> 43 #include <crc.h> 44 45 #define STYLE_MD5 0 46 #define STYLE_CKSUM 1 47 #define STYLE_TERSE 2 48 49 #define MAX_DIGEST_LEN 128 50 51 #define MIN(a,b) (((a)<(b))?(a):(b)) 52 #define MAX(a,b) (((a)>(b))?(a):(b)) 53 54 union ANY_CTX { 55 #if !defined(SHA2_ONLY) 56 CKSUM_CTX cksum; 57 MD5_CTX md5; 58 RMD160_CTX rmd160; 59 SHA1_CTX sha1; 60 #endif /* !defined(SHA2_ONLY) */ 61 SHA2_CTX sha2; 62 }; 63 64 struct hash_function { 65 const char *name; 66 size_t digestlen; 67 int style; 68 int base64; 69 void *ctx; /* XXX - only used by digest_file() */ 70 void (*init)(void *); 71 void (*update)(void *, const unsigned char *, unsigned int); 72 void (*final)(unsigned char *, void *); 73 char * (*end)(void *, char *); 74 TAILQ_ENTRY(hash_function) tailq; 75 } functions[] = { 76 #if !defined(SHA2_ONLY) 77 { 78 "CKSUM", 79 CKSUM_DIGEST_LENGTH, 80 STYLE_CKSUM, 81 -1, 82 NULL, 83 (void (*)(void *))CKSUM_Init, 84 (void (*)(void *, const unsigned char *, unsigned int))CKSUM_Update, 85 (void (*)(unsigned char *, void *))CKSUM_Final, 86 (char *(*)(void *, char *))CKSUM_End 87 }, 88 { 89 "MD5", 90 MD5_DIGEST_LENGTH, 91 STYLE_MD5, 92 0, 93 NULL, 94 (void (*)(void *))MD5Init, 95 (void (*)(void *, const unsigned char *, unsigned int))MD5Update, 96 (void (*)(unsigned char *, void *))MD5Final, 97 (char *(*)(void *, char *))MD5End 98 }, 99 { 100 "RMD160", 101 RMD160_DIGEST_LENGTH, 102 STYLE_MD5, 103 0, 104 NULL, 105 (void (*)(void *))RMD160Init, 106 (void (*)(void *, const unsigned char *, unsigned int))RMD160Update, 107 (void (*)(unsigned char *, void *))RMD160Final, 108 (char *(*)(void *, char *))RMD160End 109 }, 110 { 111 "SHA1", 112 SHA1_DIGEST_LENGTH, 113 STYLE_MD5, 114 0, 115 NULL, 116 (void (*)(void *))SHA1Init, 117 (void (*)(void *, const unsigned char *, unsigned int))SHA1Update, 118 (void (*)(unsigned char *, void *))SHA1Final, 119 (char *(*)(void *, char *))SHA1End 120 }, 121 { 122 "SHA224", 123 SHA224_DIGEST_LENGTH, 124 STYLE_MD5, 125 0, 126 NULL, 127 (void (*)(void *))SHA224Init, 128 (void (*)(void *, const unsigned char *, unsigned int))SHA224Update, 129 (void (*)(unsigned char *, void *))SHA224Final, 130 (char *(*)(void *, char *))SHA224End 131 }, 132 #endif /* !defined(SHA2_ONLY) */ 133 { 134 "SHA256", 135 SHA256_DIGEST_LENGTH, 136 STYLE_MD5, 137 0, 138 NULL, 139 (void (*)(void *))SHA256Init, 140 (void (*)(void *, const unsigned char *, unsigned int))SHA256Update, 141 (void (*)(unsigned char *, void *))SHA256Final, 142 (char *(*)(void *, char *))SHA256End 143 }, 144 #if !defined(SHA2_ONLY) 145 { 146 "SHA384", 147 SHA384_DIGEST_LENGTH, 148 STYLE_MD5, 149 0, 150 NULL, 151 (void (*)(void *))SHA384Init, 152 (void (*)(void *, const unsigned char *, unsigned int))SHA384Update, 153 (void (*)(unsigned char *, void *))SHA384Final, 154 (char *(*)(void *, char *))SHA384End 155 }, 156 #endif /* !defined(SHA2_ONLY) */ 157 { 158 "SHA512", 159 SHA512_DIGEST_LENGTH, 160 STYLE_MD5, 161 0, 162 NULL, 163 (void (*)(void *))SHA512Init, 164 (void (*)(void *, const unsigned char *, unsigned int))SHA512Update, 165 (void (*)(unsigned char *, void *))SHA512Final, 166 (char *(*)(void *, char *))SHA512End 167 }, 168 { 169 NULL, 170 } 171 }; 172 173 TAILQ_HEAD(hash_list, hash_function); 174 175 void digest_end(const struct hash_function *, void *, char *, size_t, int); 176 int digest_file(const char *, struct hash_list *, int); 177 int digest_filelist(const char *, struct hash_function *, int, char **); 178 void digest_print(const struct hash_function *, const char *, const char *); 179 #if !defined(SHA2_ONLY) 180 void digest_printstr(const struct hash_function *, const char *, const char *); 181 void digest_string(char *, struct hash_list *); 182 void digest_test(struct hash_list *); 183 void digest_time(struct hash_list *, int); 184 #endif /* !defined(SHA2_ONLY) */ 185 void hash_insert(struct hash_list *, struct hash_function *, int); 186 void usage(void) __attribute__((__noreturn__)); 187 188 extern char *__progname; 189 int qflag = 0; 190 FILE *ofile = NULL; 191 192 int 193 main(int argc, char **argv) 194 { 195 struct hash_function *hf, *hftmp; 196 struct hash_list hl; 197 size_t len; 198 char *cp, *input_string, *selective_checklist; 199 const char *optstr; 200 int fl, error, base64, i; 201 int bflag, cflag, pflag, rflag, tflag, xflag; 202 203 TAILQ_INIT(&hl); 204 input_string = NULL; 205 selective_checklist = NULL; 206 error = bflag = cflag = pflag = qflag = rflag = tflag = xflag = 0; 207 208 #if !defined(SHA2_ONLY) 209 if (strcmp(__progname, "cksum") == 0) 210 optstr = "a:bC:ch:pqrs:tx"; 211 else 212 #endif /* !defined(SHA2_ONLY) */ 213 optstr = "bC:ch:pqrs:tx"; 214 215 /* Check for -b option early since it changes behavior. */ 216 while ((fl = getopt(argc, argv, optstr)) != -1) { 217 switch (fl) { 218 case 'b': 219 bflag = 1; 220 break; 221 case '?': 222 usage(); 223 } 224 } 225 optind = 1; 226 optreset = 1; 227 while ((fl = getopt(argc, argv, optstr)) != -1) { 228 switch (fl) { 229 case 'a': 230 while ((cp = strsep(&optarg, " \t,")) != NULL) { 231 if (*cp == '\0') 232 continue; 233 base64 = -1; 234 for (hf = functions; hf->name != NULL; hf++) { 235 len = strlen(hf->name); 236 if (strncasecmp(cp, hf->name, len) != 0) 237 continue; 238 if (cp[len] == '\0') { 239 if (hf->base64 != -1) 240 base64 = bflag; 241 break; /* exact match */ 242 } 243 if (cp[len + 1] == '\0' && 244 (cp[len] == 'b' || cp[len] == 'x')) { 245 base64 = 246 cp[len] == 'b' ? 1 : 0; 247 break; /* match w/ suffix */ 248 } 249 } 250 if (hf->name == NULL) { 251 warnx("unknown algorithm \"%s\"", cp); 252 usage(); 253 } 254 if (hf->base64 == -1 && base64 != -1) { 255 warnx("%s doesn't support %s", 256 hf->name, 257 base64 ? "base64" : "hex"); 258 usage(); 259 } 260 /* Check for dupes. */ 261 TAILQ_FOREACH(hftmp, &hl, tailq) { 262 if (hftmp->base64 == base64 && 263 strcmp(hf->name, hftmp->name) == 0) 264 break; 265 } 266 if (hftmp == NULL) 267 hash_insert(&hl, hf, base64); 268 } 269 break; 270 case 'b': 271 /* has already been parsed */ 272 break; 273 case 'h': 274 ofile = fopen(optarg, "w"); 275 if (ofile == NULL) 276 err(1, "%s", optarg); 277 break; 278 #if !defined(SHA2_ONLY) 279 case 'C': 280 selective_checklist = optarg; 281 break; 282 case 'c': 283 cflag = 1; 284 break; 285 #endif /* !defined(SHA2_ONLY) */ 286 case 'p': 287 pflag = 1; 288 break; 289 case 'q': 290 qflag = 1; 291 break; 292 case 'r': 293 rflag = 1; 294 break; 295 case 's': 296 input_string = optarg; 297 break; 298 case 't': 299 tflag++; 300 break; 301 case 'x': 302 xflag = 1; 303 break; 304 default: 305 usage(); 306 } 307 } 308 argc -= optind; 309 argv += optind; 310 311 if (ofile == NULL) 312 ofile = stdout; 313 314 /* Most arguments are mutually exclusive */ 315 fl = pflag + (tflag ? 1 : 0) + xflag + cflag + (input_string != NULL); 316 if (fl > 1 || (fl && argc && cflag == 0) || (rflag && qflag) || 317 (selective_checklist != NULL && argc == 0)) 318 usage(); 319 if (selective_checklist || cflag) { 320 if (TAILQ_FIRST(&hl) != TAILQ_LAST(&hl, hash_list)) 321 errx(1, "only a single algorithm may be specified " 322 "in -C or -c mode"); 323 } 324 325 /* No algorithm specified, check the name we were called as. */ 326 if (TAILQ_EMPTY(&hl)) { 327 for (hf = functions; hf->name != NULL; hf++) { 328 if (strcasecmp(hf->name, __progname) == 0) 329 break; 330 } 331 if (hf->name == NULL) 332 hf = &functions[0]; /* default to cksum */ 333 hash_insert(&hl, hf, (hf->base64 == -1 ? 0 : bflag)); 334 } 335 336 if (rflag || qflag) { 337 const int new_style = rflag ? STYLE_CKSUM : STYLE_TERSE; 338 TAILQ_FOREACH(hf, &hl, tailq) { 339 hf->style = new_style; 340 } 341 } 342 343 #if !defined(SHA2_ONLY) 344 if (tflag) 345 digest_time(&hl, tflag); 346 else if (xflag) 347 digest_test(&hl); 348 else if (input_string) 349 digest_string(input_string, &hl); 350 else if (selective_checklist) { 351 error = digest_filelist(selective_checklist, TAILQ_FIRST(&hl), 352 argc, argv); 353 for (i = 0; i < argc; i++) { 354 if (argv[i] != NULL) { 355 warnx("%s does not exist in %s", argv[i], 356 selective_checklist); 357 error++; 358 } 359 } 360 } else if (cflag) { 361 if (argc == 0) 362 error = digest_filelist("-", TAILQ_FIRST(&hl), 0, NULL); 363 else 364 while (argc--) 365 error += digest_filelist(*argv++, 366 TAILQ_FIRST(&hl), 0, NULL); 367 } else 368 #endif /* !defined(SHA2_ONLY) */ 369 if (pflag || argc == 0) 370 error = digest_file("-", &hl, pflag); 371 else 372 while (argc--) 373 error += digest_file(*argv++, &hl, 0); 374 375 return(error ? EXIT_FAILURE : EXIT_SUCCESS); 376 } 377 378 void 379 hash_insert(struct hash_list *hl, struct hash_function *hf, int base64) 380 { 381 struct hash_function *hftmp; 382 383 hftmp = malloc(sizeof(*hftmp)); 384 if (hftmp == NULL) 385 err(1, NULL); 386 *hftmp = *hf; 387 hftmp->base64 = base64; 388 TAILQ_INSERT_TAIL(hl, hftmp, tailq); 389 } 390 391 void 392 digest_end(const struct hash_function *hf, void *ctx, char *buf, size_t bsize, 393 int base64) 394 { 395 u_char *digest; 396 397 if (base64 == 1) { 398 if ((digest = malloc(hf->digestlen)) == NULL) 399 err(1, NULL); 400 hf->final(digest, ctx); 401 if (b64_ntop(digest, hf->digestlen, buf, bsize) == -1) 402 errx(1, "error encoding base64"); 403 memset(digest, 0, hf->digestlen); 404 free(digest); 405 } else { 406 hf->end(ctx, buf); 407 } 408 } 409 410 #if !defined(SHA2_ONLY) 411 void 412 digest_string(char *string, struct hash_list *hl) 413 { 414 struct hash_function *hf; 415 char digest[MAX_DIGEST_LEN + 1]; 416 union ANY_CTX context; 417 418 TAILQ_FOREACH(hf, hl, tailq) { 419 hf->init(&context); 420 hf->update(&context, string, (unsigned int)strlen(string)); 421 digest_end(hf, &context, digest, sizeof(digest), 422 hf->base64); 423 digest_printstr(hf, string, digest); 424 } 425 } 426 #endif /* !defined(SHA2_ONLY) */ 427 428 void 429 digest_print(const struct hash_function *hf, const char *what, 430 const char *digest) 431 { 432 switch (hf->style) { 433 case STYLE_MD5: 434 (void)fprintf(ofile, "%s (%s) = %s\n", hf->name, what, digest); 435 break; 436 case STYLE_CKSUM: 437 (void)fprintf(ofile, "%s %s\n", digest, what); 438 break; 439 case STYLE_TERSE: 440 (void)fprintf(ofile, "%s\n", digest); 441 break; 442 } 443 } 444 445 void 446 digest_printstr(const struct hash_function *hf, const char *what, 447 const char *digest) 448 { 449 switch (hf->style) { 450 case STYLE_MD5: 451 (void)fprintf(ofile, "%s (\"%s\") = %s\n", hf->name, what, digest); 452 break; 453 case STYLE_CKSUM: 454 (void)fprintf(ofile, "%s %s\n", digest, what); 455 break; 456 case STYLE_TERSE: 457 (void)fprintf(ofile, "%s\n", digest); 458 break; 459 } 460 } 461 462 int 463 digest_file(const char *file, struct hash_list *hl, int echo) 464 { 465 struct hash_function *hf; 466 FILE *fp; 467 size_t nread; 468 u_char data[32 * 1024]; 469 char digest[MAX_DIGEST_LEN + 1]; 470 471 if (strcmp(file, "-") == 0) 472 fp = stdin; 473 else if ((fp = fopen(file, "r")) == NULL) { 474 warn("cannot open %s", file); 475 return(1); 476 } 477 478 TAILQ_FOREACH(hf, hl, tailq) { 479 if ((hf->ctx = malloc(sizeof(union ANY_CTX))) == NULL) 480 err(1, NULL); 481 hf->init(hf->ctx); 482 } 483 while ((nread = fread(data, 1UL, sizeof(data), fp)) != 0) { 484 if (echo) { 485 (void)fwrite(data, nread, 1UL, stdout); 486 if (fflush(stdout) != 0) 487 err(1, "stdout: write error"); 488 } 489 TAILQ_FOREACH(hf, hl, tailq) 490 hf->update(hf->ctx, data, (unsigned int)nread); 491 } 492 if (ferror(fp)) { 493 warn("%s: read error", file); 494 if (fp != stdin) 495 fclose(fp); 496 TAILQ_FOREACH(hf, hl, tailq) { 497 free(hf->ctx); 498 hf->ctx = NULL; 499 } 500 return(1); 501 } 502 if (fp != stdin) 503 fclose(fp); 504 TAILQ_FOREACH(hf, hl, tailq) { 505 digest_end(hf, hf->ctx, digest, sizeof(digest), hf->base64); 506 free(hf->ctx); 507 hf->ctx = NULL; 508 if (fp == stdin) 509 fprintf(ofile, "%s\n", digest); 510 else 511 digest_print(hf, file, digest); 512 } 513 return(0); 514 } 515 516 #if !defined(SHA2_ONLY) 517 /* 518 * Parse through the input file looking for valid lines. 519 * If one is found, use this checksum and file as a reference and 520 * generate a new checksum against the file on the filesystem. 521 * Print out the result of each comparison. 522 */ 523 int 524 digest_filelist(const char *file, struct hash_function *defhash, int selcount, 525 char **sel) 526 { 527 int found, base64, error, cmp, i; 528 size_t algorithm_max, algorithm_min; 529 const char *algorithm; 530 char *filename, *checksum, *buf, *p; 531 char digest[MAX_DIGEST_LEN + 1]; 532 char *lbuf = NULL; 533 FILE *listfp, *fp; 534 size_t len, nread; 535 u_char data[32 * 1024]; 536 union ANY_CTX context; 537 struct hash_function *hf; 538 539 if (strcmp(file, "-") == 0) { 540 listfp = stdin; 541 } else if ((listfp = fopen(file, "r")) == NULL) { 542 warn("cannot open %s", file); 543 return(1); 544 } 545 546 algorithm_max = algorithm_min = strlen(functions[0].name); 547 for (hf = &functions[1]; hf->name != NULL; hf++) { 548 len = strlen(hf->name); 549 algorithm_max = MAX(algorithm_max, len); 550 algorithm_min = MIN(algorithm_min, len); 551 } 552 553 error = found = 0; 554 while ((buf = fgetln(listfp, &len))) { 555 base64 = 0; 556 if (buf[len - 1] == '\n') 557 buf[len - 1] = '\0'; 558 else { 559 if ((lbuf = malloc(len + 1)) == NULL) 560 err(1, NULL); 561 562 (void)memcpy(lbuf, buf, len); 563 lbuf[len] = '\0'; 564 buf = lbuf; 565 } 566 while (isspace((unsigned char)*buf)) 567 buf++; 568 569 /* 570 * Crack the line into an algorithm, filename, and checksum. 571 * Lines are of the form: 572 * ALGORITHM (FILENAME) = CHECKSUM 573 * 574 * Fallback on GNU form: 575 * CHECKSUM FILENAME 576 */ 577 p = strchr(buf, ' '); 578 if (p != NULL && *(p + 1) == '(') { 579 /* BSD form */ 580 *p = '\0'; 581 algorithm = buf; 582 len = strlen(algorithm); 583 if (len > algorithm_max || len < algorithm_min) 584 continue; 585 586 filename = p + 2; 587 p = strrchr(filename, ')'); 588 if (p == NULL || strncmp(p + 1, " = ", (size_t)3) != 0) 589 continue; 590 *p = '\0'; 591 592 checksum = p + 4; 593 p = strpbrk(checksum, " \t\r"); 594 if (p != NULL) 595 *p = '\0'; 596 597 /* 598 * Check that the algorithm is one we recognize. 599 */ 600 for (hf = functions; hf->name != NULL; hf++) { 601 if (strcasecmp(algorithm, hf->name) == 0) 602 break; 603 } 604 if (hf->name == NULL || *checksum == '\0') 605 continue; 606 /* 607 * Check the length to see if this could be 608 * a valid checksum. If hex, it will be 2x the 609 * size of the binary data. For base64, we have 610 * to check both with and without the '=' padding. 611 */ 612 len = strlen(checksum); 613 if (len != hf->digestlen * 2) { 614 size_t len2; 615 616 if (checksum[len - 1] == '=') { 617 /* use padding */ 618 len2 = 4 * ((hf->digestlen + 2) / 3); 619 } else { 620 /* no padding */ 621 len2 = (4 * hf->digestlen + 2) / 3; 622 } 623 if (len != len2) 624 continue; 625 base64 = 1; 626 } 627 } else { 628 /* could be GNU form */ 629 if ((hf = defhash) == NULL) 630 continue; 631 algorithm = hf->name; 632 checksum = buf; 633 if ((p = strchr(checksum, ' ')) == NULL) 634 continue; 635 if (hf->style == STYLE_CKSUM) { 636 if ((p = strchr(p + 1, ' ')) == NULL) 637 continue; 638 } 639 *p++ = '\0'; 640 while (isspace((unsigned char)*p)) 641 p++; 642 if (*p == '\0') 643 continue; 644 filename = p; 645 p = strpbrk(filename, "\t\r"); 646 if (p != NULL) 647 *p = '\0'; 648 } 649 found = 1; 650 651 /* 652 * If only a selection of files is wanted, proceed only 653 * if the filename matches one of those in the selection. 654 * Mark found files by setting them to NULL so that we can 655 * detect files that are missing from the checklist later. 656 */ 657 if (sel) { 658 for (i = 0; i < selcount; i++) { 659 if (sel[i] && strcmp(sel[i], filename) == 0) { 660 sel[i] = NULL; 661 break; 662 } 663 } 664 if (i == selcount) 665 continue; 666 } 667 668 if ((fp = fopen(filename, "r")) == NULL) { 669 warn("cannot open %s", filename); 670 (void)printf("(%s) %s: %s\n", algorithm, filename, 671 (errno == ENOENT ? "MISSING" : "FAILED")); 672 error = 1; 673 continue; 674 } 675 676 hf->init(&context); 677 while ((nread = fread(data, 1UL, sizeof(data), fp)) > 0) 678 hf->update(&context, data, (unsigned int)nread); 679 if (ferror(fp)) { 680 warn("%s: read error", file); 681 error = 1; 682 fclose(fp); 683 continue; 684 } 685 fclose(fp); 686 digest_end(hf, &context, digest, sizeof(digest), base64); 687 688 if (base64) 689 cmp = strncmp(checksum, digest, len); 690 else 691 cmp = strcasecmp(checksum, digest); 692 if (cmp == 0) { 693 if (qflag == 0) 694 (void)printf("(%s) %s: OK\n", algorithm, 695 filename); 696 } else { 697 (void)printf("(%s) %s: FAILED\n", algorithm, filename); 698 error = 1; 699 } 700 } 701 if (listfp != stdin) 702 fclose(listfp); 703 if (!found) 704 warnx("%s: no properly formatted checksum lines found", file); 705 if (lbuf != NULL) 706 free(lbuf); 707 return(error || !found); 708 } 709 710 #define TEST_BLOCK_LEN 10000 711 #define TEST_BLOCK_COUNT 10000 712 713 void 714 digest_time(struct hash_list *hl, int times) 715 { 716 struct hash_function *hf; 717 struct timeval start, stop, res; 718 union ANY_CTX context; 719 u_int i; 720 u_char data[TEST_BLOCK_LEN]; 721 char digest[MAX_DIGEST_LEN + 1]; 722 double elapsed; 723 int count = TEST_BLOCK_COUNT; 724 while (--times > 0 && count < INT_MAX / 10) 725 count *= 10; 726 727 TAILQ_FOREACH(hf, hl, tailq) { 728 (void)printf("%s time trial. Processing %d %d-byte blocks...", 729 hf->name, count, TEST_BLOCK_LEN); 730 fflush(stdout); 731 732 /* Initialize data based on block number. */ 733 for (i = 0; i < TEST_BLOCK_LEN; i++) 734 data[i] = (u_char)(i & 0xff); 735 736 gettimeofday(&start, NULL); 737 hf->init(&context); 738 for (i = 0; i < count; i++) 739 hf->update(&context, data, TEST_BLOCK_LEN); 740 digest_end(hf, &context, digest, sizeof(digest), hf->base64); 741 gettimeofday(&stop, NULL); 742 timersub(&stop, &start, &res); 743 elapsed = res.tv_sec + res.tv_usec / 1000000.0; 744 745 (void)printf("\nDigest = %s\n", digest); 746 (void)printf("Time = %f seconds\n", elapsed); 747 (void)printf("Speed = %f bytes/second\n", 748 (double)TEST_BLOCK_LEN * count / elapsed); 749 } 750 } 751 752 void 753 digest_test(struct hash_list *hl) 754 { 755 struct hash_function *hf; 756 union ANY_CTX context; 757 int i; 758 char digest[MAX_DIGEST_LEN + 1]; 759 unsigned char buf[1000]; 760 unsigned const char *test_strings[] = { 761 "", 762 "a", 763 "abc", 764 "message digest", 765 "abcdefghijklmnopqrstuvwxyz", 766 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 767 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 768 "0123456789", 769 "12345678901234567890123456789012345678901234567890123456789" 770 "012345678901234567890", 771 }; 772 773 TAILQ_FOREACH(hf, hl, tailq) { 774 (void)printf("%s test suite:\n", hf->name); 775 776 for (i = 0; i < 8; i++) { 777 hf->init(&context); 778 hf->update((void *)&context, test_strings[i], 779 (unsigned int)strlen(test_strings[i])); 780 digest_end(hf, &context, digest, sizeof(digest), 781 hf->base64); 782 digest_printstr(hf, test_strings[i], digest); 783 } 784 785 /* Now simulate a string of a million 'a' characters. */ 786 memset(buf, 'a', sizeof(buf)); 787 hf->init(&context); 788 for (i = 0; i < 1000; i++) 789 hf->update(&context, buf, 790 (unsigned int)sizeof(buf)); 791 digest_end(hf, &context, digest, sizeof(digest), hf->base64); 792 digest_print(hf, "one million 'a' characters", 793 digest); 794 } 795 } 796 #endif /* !defined(SHA2_ONLY) */ 797 798 void 799 usage(void) 800 { 801 #if !defined(SHA2_ONLY) 802 if (strcmp(__progname, "cksum") == 0) 803 fprintf(stderr, "usage: %s [-bcpqrtx] [-a algorithms] [-C checklist] " 804 "[-h hashfile]\n" 805 " [-s string] [file ...]\n", 806 __progname); 807 else 808 #endif /* !defined(SHA2_ONLY) */ 809 fprintf(stderr, "usage:" 810 "\t%s [-bcpqrtx] [-C checklist] [-h hashfile] [-s string] " 811 "[file ...]\n", 812 __progname); 813 814 exit(EXIT_FAILURE); 815 } 816