Support for new SHA variants added.
[u/mdw/catacomb] / hashsum.c
1 /* -*-c-*-
2 *
3 * $Id: hashsum.c,v 1.6 2001/01/25 21:40:14 mdw Exp $
4 *
5 * Hash files using some secure hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: hashsum.c,v $
33 * Revision 1.6 2001/01/25 21:40:14 mdw
34 * Support for new SHA variants added.
35 *
36 * Revision 1.5 2000/12/06 20:33:27 mdw
37 * Make flags be macros rather than enumerations, to ensure that they're
38 * unsigned.
39 *
40 * Revision 1.4 2000/08/04 23:23:44 mdw
41 * Various <ctype.h> fixes.
42 *
43 * Revision 1.3 2000/07/29 17:02:43 mdw
44 * (checkhash): Be pettier about spaces between the hash and filename, for
45 * compatiblity with `md5sum'.
46 *
47 * Revision 1.2 2000/07/15 21:14:05 mdw
48 * Missed `-e' out of the usage string.
49 *
50 * Revision 1.1 2000/07/15 20:52:34 mdw
51 * Useful replacement for `md5sum' with support for many different hash
52 * functions and for reading filename lists from `find'.
53 *
54 */
55
56 /*----- Header files ------------------------------------------------------*/
57
58 #include "config.h"
59
60 #include <ctype.h>
61 #include <errno.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65
66 #include <mLib/alloc.h>
67 #include <mLib/dstr.h>
68 #include <mLib/mdwopt.h>
69 #include <mLib/quis.h>
70 #include <mLib/report.h>
71 #include <mLib/sub.h>
72 #include <mLib/str.h>
73
74 #include "ghash.h"
75
76 #include "md4.h"
77 #include "md5.h"
78 #include "rmd128.h"
79 #include "rmd160.h"
80 #include "rmd256.h"
81 #include "rmd320.h"
82 #include "sha.h"
83 #include "sha256.h"
84 #include "sha384.h"
85 #include "sha512.h"
86 #include "tiger.h"
87
88 /*----- Static variables --------------------------------------------------*/
89
90 static const gchash *hashtab[] = {
91 &md5, &md4,
92 &sha, &sha256, &sha384, &sha512,
93 &rmd128, &rmd160, &rmd256, &rmd320,
94 &tiger,
95 0
96 };
97
98 #define f_binary 1u
99 #define f_bogus 2u
100 #define f_verbose 4u
101 #define f_check 8u
102 #define f_files 16u
103 #define f_raw 32u
104 #define f_oddhash 64u
105 #define f_escape 128u
106
107 /*----- Support functions -------------------------------------------------*/
108
109 /* --- @fhash@ --- *
110 *
111 * Arguments: @const char *file@ = file name to be hashed (null for stdin)
112 * @unsigned f@ = flags to set
113 * @const gchash *gch@ = pointer to hash function to use
114 * @void *buf@ = pointer to hash output buffer
115 *
116 * Returns: Zero if it worked, nonzero on error.
117 *
118 * Use: Hashes a file.
119 */
120
121 static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
122 {
123 FILE *fp;
124 char fbuf[BUFSIZ];
125 size_t sz;
126 ghash *h;
127 int e;
128
129 if (!file)
130 fp = stdin;
131 else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
132 return (-1);
133
134 h = gch->init();
135 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
136 h->ops->hash(h, fbuf, sz);
137 h->ops->done(h, buf);
138 h->ops->destroy(h);
139 e = ferror(fp);
140 if (file)
141 fclose(fp);
142 return (e ? -1 : 0);
143 }
144
145 /* --- @puthex@ --- *
146 *
147 * Arguments: @const octet *buf@ = pointer to a binary buffer
148 * @size_t sz@ = size of the buffer
149 * @FILE *fp@ = pointer to output file handle
150 *
151 * Returns: ---
152 *
153 * Use: Writes a hex dump of a block of memory.
154 */
155
156 static void puthex(const octet *buf, size_t sz, FILE *fp)
157 {
158 while (sz) {
159 fprintf(fp, "%02x", *buf++);
160 sz--;
161 }
162 }
163
164 /* --- @gethex@ --- *
165 *
166 * Arguments: @const char *p@ = pointer to input string
167 * @octet *q@ = pointer to output buffer
168 * @size_t sz@ = size of the output buffer
169 * @char **pp@ = where to put the end pointer
170 *
171 * Returns: The number of bytes written to the buffer.
172 *
173 * Use: Reads hex dumps from the input string.
174 */
175
176 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
177 {
178 size_t i = 0;
179 while (sz > 0 &&
180 isxdigit((unsigned char)p[0]) &&
181 isxdigit((unsigned char)p[1])) {
182 char buf[3];
183 buf[0] = p[0];
184 buf[1] = p[1];
185 buf[2] = 0;
186 *q++ = strtoul(buf, 0, 16);
187 sz--;
188 p += 2;
189 i++;
190 }
191 if (pp)
192 *pp = (char *)p;
193 return (i);
194 }
195
196 /* --- @gethash@ --- *
197 *
198 * Arguments: @const char *name@ = pointer to name string
199 *
200 * Returns: Pointer to appropriate hash class.
201 *
202 * Use: Chooses a hash function by name.
203 */
204
205 static const gchash *gethash(const char *name)
206 {
207 const gchash **g, *gg = 0;
208 size_t sz = strlen(name);
209 for (g = hashtab; *g; g++) {
210 if (strncmp(name, (*g)->name, sz) == 0) {
211 if ((*g)->name[sz] == 0) {
212 gg = *g;
213 break;
214 } else if (gg)
215 return (0);
216 else
217 gg = *g;
218 }
219 }
220 return (gg);
221 }
222
223 /* --- @getstring@ --- *
224 *
225 * Arguments: @FILE *fp@ = stream from which to read
226 * @const char *p@ = string to read from instead
227 * @dstr *d@ = destination string
228 * @unsigned raw@ = raw or cooked read
229 *
230 * Returns: Zero if OK, nonzero on end-of-file.
231 *
232 * Use: Reads a filename (or something similar) from a stream.
233 */
234
235 static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
236 {
237 int ch;
238 int q = 0;
239
240 /* --- Raw: just read exactly what's written up to a null byte --- */
241
242 #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
243 #define EOFCH (fp ? EOF : 0)
244
245 if (raw) {
246 if ((ch = NEXTCH) == EOFCH)
247 return (EOF);
248 for (;;) {
249 if (!ch)
250 break;
251 DPUTC(d, ch);
252 if ((ch = NEXTCH) == EOFCH)
253 break;
254 }
255 DPUTZ(d);
256 return (0);
257 }
258
259 /* --- Skip as far as whitespace --- *
260 *
261 * Also skip past comments.
262 */
263
264 again:
265 ch = NEXTCH;
266 while (isspace(ch))
267 ch = NEXTCH;
268 if (ch == '#') {
269 do ch = NEXTCH; while (ch != '\n' && ch != EOFCH);
270 goto again;
271 }
272 if (ch == EOFCH)
273 return (EOF);
274
275 /* --- If the character is a quote then read a quoted string --- */
276
277 switch (ch) {
278 case '`':
279 ch = '\'';
280 case '\'':
281 case '\"':
282 q = ch;
283 ch = NEXTCH;
284 break;
285 }
286
287 /* --- Now read all sorts of interesting things --- */
288
289 for (;;) {
290
291 /* --- Handle an escaped thing --- */
292
293 if (ch == '\\') {
294 ch = NEXTCH;
295 if (ch == EOFCH)
296 break;
297 switch (ch) {
298 case 'a': ch = '\a'; break;
299 case 'b': ch = '\b'; break;
300 case 'f': ch = '\f'; break;
301 case 'n': ch = '\n'; break;
302 case 'r': ch = '\r'; break;
303 case 't': ch = '\t'; break;
304 case 'v': ch = '\v'; break;
305 }
306 DPUTC(d, ch);
307 ch = NEXTCH;
308 continue;
309 }
310
311 /* --- If it's a quote or some other end marker then stop --- */
312
313 if (ch == q)
314 break;
315 if (!q && isspace(ch))
316 break;
317
318 /* --- Otherwise contribute and continue --- */
319
320 DPUTC(d, ch);
321 if ((ch = NEXTCH) == EOFCH)
322 break;
323 }
324
325 /* --- Done --- */
326
327 DPUTZ(d);
328 return (0);
329
330 #undef NEXTCH
331 #undef EOFCH
332 }
333
334 /* --- @putstring@ --- *
335 *
336 * Arguments: @FILE *fp@ = stream to write on
337 * @const char *p@ = pointer to text
338 * @unsigned raw@ = whether the string is to be written raw
339 *
340 * Returns: ---
341 *
342 * Use: Emits a string to a stream.
343 */
344
345 static void putstring(FILE *fp, const char *p, unsigned raw)
346 {
347 size_t sz = strlen(p);
348 unsigned qq;
349 const char *q;
350
351 /* --- Just write the string null terminated if raw --- */
352
353 if (raw) {
354 fwrite(p, 1, sz + 1, fp);
355 return;
356 }
357
358 /* --- Check for any dodgy characters --- */
359
360 qq = 0;
361 for (q = p; *q; q++) {
362 if (isspace((unsigned char)*q)) {
363 qq = '\"';
364 break;
365 }
366 }
367
368 if (qq)
369 putc(qq, fp);
370
371 /* --- Emit the string --- */
372
373 for (q = p; *q; q++) {
374 switch (*q) {
375 case '\a': fputc('\\', fp); fputc('a', fp); break;
376 case '\b': fputc('\\', fp); fputc('b', fp); break;
377 case '\f': fputc('\\', fp); fputc('f', fp); break;
378 case '\n': fputc('\\', fp); fputc('n', fp); break;
379 case '\r': fputc('\\', fp); fputc('r', fp); break;
380 case '\t': fputc('\\', fp); fputc('t', fp); break;
381 case '\v': fputc('\\', fp); fputc('v', fp); break;
382 case '`': fputc('\\', fp); fputc('`', fp); break;
383 case '\'': fputc('\\', fp); fputc('\'', fp); break;
384 case '\"': fputc('\\', fp); fputc('\"', fp); break;
385 case '#': fputc('\\', fp); fputc('#', fp); break;
386 default:
387 putc(*q, fp);
388 break;
389 }
390 }
391
392 /* --- Done --- */
393
394 if (qq)
395 putc(qq, fp);
396 }
397
398 /*----- Guts --------------------------------------------------------------*/
399
400 static int checkhash(const char *file, unsigned f, const gchash *gch)
401 {
402 int rc;
403 FILE *fp;
404 dstr d = DSTR_INIT;
405 dstr dd = DSTR_INIT;
406 unsigned long n = 0, nfail = 0;
407 octet *buf = xmalloc(2 * gch->hashsz);
408
409 if (!file)
410 fp = stdin;
411 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
412 moan("couldn't open `%s': %s", file, strerror(errno));
413 return (EXIT_FAILURE);
414 }
415
416 while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
417 char *p = d.buf;
418 char *q;
419 unsigned ff = f;
420
421 /* --- Handle a directive --- */
422
423 if (*p == '#') {
424 p++;
425 if ((q = str_getword(&p)) == 0)
426 continue;
427 if (strcmp(q, "hash") == 0) {
428 const gchash *g;
429 if ((q = str_getword(&p)) == 0)
430 continue;
431 if ((g = gethash(q)) == 0)
432 continue;
433 gch = g;
434 xfree(buf);
435 buf = xmalloc(2 * gch->hashsz);
436 } else if (strcmp(q, "escape") == 0)
437 f |= f_escape;
438 continue;
439 }
440
441 /* --- Otherwise it's a hex thing --- */
442
443 q = p;
444 while (*p && *p != ' ')
445 p++;
446 if (!*p)
447 continue;
448 *p++ = 0;
449 if (gethex(q, buf, gch->hashsz, 0) < gch->hashsz)
450 continue;
451 if (*p == '*')
452 ff |= f_binary;
453 else if (*p != ' ')
454 continue;
455 p++;
456
457 if (f & f_escape) {
458 DRESET(&dd);
459 getstring(0, p, &dd, 0);
460 p = dd.buf;
461 }
462
463 if (fhash(p, ff, gch, buf + gch->hashsz)) {
464 moan("couldn't read `%s': %s", p, strerror(errno));
465 rc = EXIT_FAILURE;
466 continue;
467 }
468 if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
469 if (ff & f_verbose)
470 fprintf(stderr, "FAIL %s\n", p);
471 else
472 moan("%s check failed for `%s'", gch->name, p);
473 nfail++;
474 rc = EXIT_FAILURE;
475 } else {
476 if (ff & f_verbose)
477 fprintf(stderr, "OK %s\n", p);
478 }
479 n++;
480 }
481
482 dstr_destroy(&d);
483 dstr_destroy(&dd);
484 xfree(buf);
485 if ((f & f_verbose) && nfail)
486 moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
487 else if (!n)
488 moan("no files checked");
489 return (0);
490 }
491
492 static int dohash(const char *file, unsigned f, const gchash *gch)
493 {
494 int rc = 0;
495 octet *p = xmalloc(gch->hashsz);
496
497 if (fhash(file, f, gch, p)) {
498 moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
499 rc = EXIT_FAILURE;
500 } else {
501 puthex(p, gch->hashsz, stdout);
502 if (file) {
503 fputc(' ', stdout);
504 fputc(f & f_binary ? '*' : ' ', stdout);
505 if (f & f_escape)
506 putstring(stdout, file, 0);
507 else
508 fputs(file, stdout);
509 }
510 fputc('\n', stdout);
511 }
512
513 xfree(p);
514 return (rc);
515 }
516
517 static int dofile(const char *file, unsigned f, const gchash *gch)
518 {
519 return (f & f_check ? checkhash : dohash)(file, f, gch);
520 }
521
522 static int hashfiles(const char *file, unsigned f, const gchash *gch)
523 {
524 FILE *fp;
525 dstr d = DSTR_INIT;
526 int rc = 0;
527 int rrc;
528
529 if (!file)
530 fp = stdin;
531 else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
532 moan("couldn't open `%s': %s", file, strerror(errno));
533 return (EXIT_FAILURE);
534 }
535
536 for (;;) {
537 DRESET(&d);
538 if (getstring(fp, 0, &d, f & f_raw))
539 break;
540 if ((rrc = dofile(d.buf, f, gch)) != 0)
541 rc = rrc;
542 }
543
544 return (rc);
545 }
546
547 static int hashsum(const char *file, unsigned f, const gchash *gch)
548 {
549 return (f & f_files ? hashfiles : dofile)(file, f, gch);
550 }
551
552 /*----- Main driver -------------------------------------------------------*/
553
554 static void version(FILE *fp)
555 {
556 pquis(fp, "$, Catacomb version " VERSION "\n");
557 }
558
559 static void usage(FILE *fp)
560 {
561 pquis(fp, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
562 }
563
564 static void help(FILE *fp, const gchash *gch)
565 {
566 version(fp);
567 fputc('\n', fp);
568 usage(fp);
569 pquis(fp, "\n\
570 Generates or checks message digests on files. Options available:\n\
571 \n\
572 -h, --help Display this help message.\n\
573 -V, --version Display program's version number.\n\
574 -u, --usage Display a terse usage message.\n\
575 \n\
576 -a, --algorithm=ALG Use the message digest algorithm ALG.\n\
577 \n\
578 -f, --files Read a list of file names from standard input.\n\
579 -0, --null File names are null terminated, not plain text.\n\
580 \n\
581 -e, --escape Escape funny characters in filenames.\n\
582 -c, --check Check message digests rather than emitting them.\n\
583 -b, --binary When reading files, treat them as binary.\n\
584 -v, --verbose Be verbose when checking digests.\n\
585 \n\
586 For a list of supported message digest algorithms, type `$ --list'.\n\
587 ");
588 if (gch)
589 fprintf(fp, "The default message digest algorithm is %s.\n", gch->name);
590 }
591
592 int main(int argc, char *argv[])
593 {
594 unsigned f = 0;
595 const gchash *gch = 0;
596 int rc;
597
598 /* --- Initialization --- */
599
600 ego(argv[0]);
601 sub_init();
602
603 /* --- Choose a hash function from the name --- */
604
605 {
606 char *q = xstrdup(QUIS);
607 size_t len = strlen(q);
608 if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
609 q[len - 3] = 0;
610 gch = gethash(q);
611 }
612 if (!gch)
613 gch = hashtab[0];
614 xfree(q);
615 }
616
617 /* --- Read options --- */
618
619 for (;;) {
620 static struct option opts[] = {
621 { "help", 0, 0, 'h' },
622 { "verbose", 0, 0, 'V' },
623 { "usage", 0, 0, 'u' },
624
625 { "algorithm", OPTF_ARGREQ, 0, 'a' },
626 { "hash", OPTF_ARGREQ, 0, 'a' },
627 { "list", 0, 0, 'l' },
628
629 { "files", 0, 0, 'f' },
630 { "find", 0, 0, 'f' },
631 { "null", 0, 0, '0' },
632
633 { "escape", 0, 0, 'e' },
634 { "check", 0, 0, 'c' },
635 { "binary", 0, 0, 'b' },
636 { "verbose", 0, 0, 'v' },
637
638 { 0, 0, 0, 0 }
639 };
640 int i = mdwopt(argc, argv, "hVu a:l f0 ecbv", opts, 0, 0, 0);
641 if (i < 0)
642 break;
643
644 switch (i) {
645 case 'h':
646 help(stdout, gch);
647 exit(0);
648 case 'V':
649 version(stdout);
650 exit(0);
651 case 'u':
652 usage(stdout);
653 exit(0);
654 case 'a':
655 if ((gch = gethash(optarg)) == 0)
656 die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
657 f |= f_oddhash;
658 break;
659 case 'l': {
660 unsigned j;
661 for (j = 0; hashtab[j]; j++) {
662 if (j)
663 fputc(' ', stdout);
664 printf("%s", hashtab[j]->name);
665 }
666 fputc('\n', stdout);
667 exit(0);
668 } break;
669 case 'f':
670 f |= f_files;
671 break;
672 case '0':
673 f |= f_raw;
674 break;
675 case 'e':
676 f |= f_escape;
677 break;
678 case 'c':
679 f |= f_check;
680 break;
681 case 'b':
682 f |= f_binary;
683 break;
684 case 'v':
685 f |= f_verbose;
686 break;
687 default:
688 f |= f_bogus;
689 break;
690 }
691 }
692
693 if (f & f_bogus) {
694 usage(stderr);
695 exit(EXIT_FAILURE);
696 }
697 argv += optind;
698 argc -= optind;
699
700 /* --- Generate output --- */
701
702 if (!(f & f_check)) {
703 if (f & f_oddhash)
704 printf("#hash %s\n", gch->name);
705 if (f & f_escape)
706 fputs("#escape\n", stdout);
707 }
708
709 if (argc) {
710 int i;
711 int rrc;
712 rc = 0;
713 for (i = 0; i < argc; i++) {
714 if ((rrc = hashsum(argv[i], f, gch)) != 0)
715 rc = rrc;
716 }
717 } else
718 rc = hashsum(0, f, gch);
719
720 return (rc);
721 }
722
723 /*----- That's all, folks -------------------------------------------------*/