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