cksum.c revision 1.27 1 1.27 elad /* $NetBSD: cksum.c,v 1.27 2005/08/24 19:59:08 elad Exp $ */
2 1.6 glass
3 1.1 cgd /*-
4 1.3 cgd * Copyright (c) 1991, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.3 cgd * James W. Williams of NASA Goddard Space Flight Center.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.18 agc * 3. Neither the name of the University nor the names of its contributors
19 1.18 agc * may be used to endorse or promote products derived from this software
20 1.18 agc * without specific prior written permission.
21 1.18 agc *
22 1.18 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.18 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.18 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.18 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.18 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.18 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.18 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.18 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.18 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.18 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.18 agc * SUCH DAMAGE.
33 1.18 agc */
34 1.18 agc
35 1.18 agc /*-
36 1.18 agc * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
37 1.18 agc *
38 1.18 agc * This code is derived from software contributed to Berkeley by
39 1.18 agc * James W. Williams of NASA Goddard Space Flight Center.
40 1.18 agc *
41 1.18 agc * Redistribution and use in source and binary forms, with or without
42 1.18 agc * modification, are permitted provided that the following conditions
43 1.18 agc * are met:
44 1.18 agc * 1. Redistributions of source code must retain the above copyright
45 1.18 agc * notice, this list of conditions and the following disclaimer.
46 1.18 agc * 2. Redistributions in binary form must reproduce the above copyright
47 1.18 agc * notice, this list of conditions and the following disclaimer in the
48 1.18 agc * documentation and/or other materials provided with the distribution.
49 1.1 cgd * 3. All advertising materials mentioning features or use of this software
50 1.1 cgd * must display the following acknowledgement:
51 1.1 cgd * This product includes software developed by the University of
52 1.1 cgd * California, Berkeley and its contributors.
53 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
54 1.1 cgd * may be used to endorse or promote products derived from this software
55 1.1 cgd * without specific prior written permission.
56 1.1 cgd *
57 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 1.1 cgd * SUCH DAMAGE.
68 1.1 cgd */
69 1.1 cgd
70 1.20 jmc #if HAVE_NBTOOL_CONFIG_H
71 1.20 jmc #include "nbtool_config.h"
72 1.20 jmc #endif
73 1.20 jmc
74 1.10 lukem #include <sys/cdefs.h>
75 1.16 bjh21 #if defined(__COPYRIGHT) && !defined(lint)
76 1.10 lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
77 1.10 lukem The Regents of the University of California. All rights reserved.\n");
78 1.1 cgd #endif /* not lint */
79 1.1 cgd
80 1.16 bjh21 #if defined(__RCSID) && !defined(lint)
81 1.6 glass #if 0
82 1.7 jtc static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95";
83 1.6 glass #endif
84 1.27 elad __RCSID("$NetBSD: cksum.c,v 1.27 2005/08/24 19:59:08 elad Exp $");
85 1.1 cgd #endif /* not lint */
86 1.1 cgd
87 1.1 cgd #include <sys/cdefs.h>
88 1.1 cgd #include <sys/types.h>
89 1.5 mycroft
90 1.5 mycroft #include <err.h>
91 1.5 mycroft #include <errno.h>
92 1.1 cgd #include <fcntl.h>
93 1.9 kleink #include <locale.h>
94 1.8 thorpej #include <md5.h>
95 1.13 atatat #include <md4.h>
96 1.13 atatat #include <md2.h>
97 1.13 atatat #include <sha1.h>
98 1.27 elad #include <crypto/sha2.h>
99 1.13 atatat #include <rmd160.h>
100 1.1 cgd #include <stdio.h>
101 1.1 cgd #include <stdlib.h>
102 1.1 cgd #include <string.h>
103 1.5 mycroft #include <unistd.h>
104 1.5 mycroft
105 1.1 cgd #include "extern.h"
106 1.1 cgd
107 1.13 atatat #define HASH_MD2 0
108 1.13 atatat #define HASH_MD4 1
109 1.13 atatat #define HASH_MD5 2
110 1.13 atatat #define HASH_SHA1 3
111 1.13 atatat #define HASH_RMD160 4
112 1.13 atatat
113 1.13 atatat typedef char *(*_filefunc)(const char *, char *);
114 1.13 atatat
115 1.13 atatat struct hash {
116 1.13 atatat const char *progname;
117 1.13 atatat const char *hashname;
118 1.13 atatat void (*stringfunc)(const char *);
119 1.13 atatat void (*timetrialfunc)(void);
120 1.13 atatat void (*testsuitefunc)(void);
121 1.13 atatat void (*filterfunc)(int);
122 1.13 atatat char *(*filefunc)(const char *, char *);
123 1.13 atatat } hashes[] = {
124 1.13 atatat { "md2", "MD2",
125 1.13 atatat MD2String, MD2TimeTrial, MD2TestSuite,
126 1.13 atatat MD2Filter, MD2File },
127 1.13 atatat { "md4", "MD4",
128 1.13 atatat MD4String, MD4TimeTrial, MD4TestSuite,
129 1.13 atatat MD4Filter, MD4File },
130 1.13 atatat { "md5", "MD5",
131 1.13 atatat MD5String, MD5TimeTrial, MD5TestSuite,
132 1.13 atatat MD5Filter, MD5File },
133 1.13 atatat { "sha1", "SHA1",
134 1.13 atatat SHA1String, SHA1TimeTrial, SHA1TestSuite,
135 1.13 atatat SHA1Filter, (_filefunc) SHA1File },
136 1.13 atatat { "rmd160", "RMD160",
137 1.13 atatat RMD160String, RMD160TimeTrial, RMD160TestSuite,
138 1.13 atatat RMD160Filter, (_filefunc) RMD160File },
139 1.27 elad { "sha256", "SHA256",
140 1.27 elad SHA256_String, SHA256_TimeTrial, SHA256_TestSuite,
141 1.27 elad SHA256_Filter, (_filefunc) SHA256_File },
142 1.27 elad { "sha384", "SHA384",
143 1.27 elad SHA384_String, SHA384_TimeTrial, SHA384_TestSuite,
144 1.27 elad SHA384_Filter, (_filefunc) SHA384_File },
145 1.27 elad { "sha512", "SHA512",
146 1.27 elad SHA512_String, SHA512_TimeTrial, SHA512_TestSuite,
147 1.27 elad SHA512_Filter, (_filefunc) SHA512_File },
148 1.13 atatat { NULL }
149 1.13 atatat };
150 1.13 atatat
151 1.23 xtraeme int hash_digest_file(char *, struct hash *, int);
152 1.23 xtraeme void requirehash(const char *);
153 1.23 xtraeme void usage(void);
154 1.3 cgd
155 1.3 cgd int
156 1.22 xtraeme main(int argc, char **argv)
157 1.1 cgd {
158 1.24 simonb int ch, fd, rval, dosum, pflag, nohashstdin;
159 1.19 kleink u_int32_t val;
160 1.19 kleink off_t len;
161 1.1 cgd char *fn;
162 1.13 atatat const char *progname;
163 1.22 xtraeme int (*cfncn) (int, u_int32_t *, off_t *);
164 1.22 xtraeme void (*pfncn) (char *, u_int32_t, off_t);
165 1.13 atatat struct hash *hash;
166 1.25 elad int normal, i;
167 1.1 cgd
168 1.10 lukem cfncn = NULL;
169 1.10 lukem pfncn = NULL;
170 1.13 atatat dosum = pflag = nohashstdin = 0;
171 1.17 atatat normal = 0;
172 1.9 kleink
173 1.9 kleink setlocale(LC_ALL, "");
174 1.8 thorpej
175 1.13 atatat progname = getprogname();
176 1.13 atatat
177 1.13 atatat for (hash = hashes; hash->hashname != NULL; hash++)
178 1.13 atatat if (strcmp(progname, hash->progname) == 0)
179 1.13 atatat break;
180 1.13 atatat
181 1.13 atatat if (hash->hashname == NULL) {
182 1.13 atatat hash = NULL;
183 1.13 atatat
184 1.13 atatat if (!strcmp(progname, "sum")) {
185 1.13 atatat dosum = 1;
186 1.13 atatat cfncn = csum1;
187 1.13 atatat pfncn = psum1;
188 1.13 atatat } else {
189 1.15 atatat cfncn = crc;
190 1.13 atatat pfncn = pcrc;
191 1.13 atatat }
192 1.5 mycroft }
193 1.5 mycroft
194 1.26 elad /*
195 1.26 elad * The -1, -2, -4, -5, -6, and -m flags should be deprecated, but
196 1.26 elad * are still supported in code to not break anything that might
197 1.26 elad * be using them.
198 1.26 elad */
199 1.25 elad while ((ch = getopt(argc, argv, "a:mno:ps:tx12456")) != -1)
200 1.1 cgd switch(ch) {
201 1.25 elad case 'a':
202 1.25 elad if (hash != NULL || dosum) {
203 1.25 elad warnx("illegal use of -a option\n");
204 1.25 elad usage();
205 1.25 elad }
206 1.25 elad i = 0;
207 1.25 elad while (hashes[i].hashname != NULL) {
208 1.25 elad if (!strcasecmp(hashes[i].hashname, optarg)) {
209 1.25 elad hash = &hashes[i];
210 1.25 elad break;
211 1.25 elad }
212 1.25 elad i++;
213 1.25 elad }
214 1.25 elad if (hash == NULL) {
215 1.25 elad if (!strcasecmp(optarg, "old1")) {
216 1.25 elad cfncn = csum1;
217 1.25 elad pfncn = psum1;
218 1.25 elad } else if (!strcasecmp(optarg, "old2")) {
219 1.25 elad cfncn = csum2;
220 1.25 elad pfncn = psum2;
221 1.25 elad } else if (!strcasecmp(optarg, "crc")) {
222 1.25 elad cfncn = crc;
223 1.25 elad pfncn = pcrc;
224 1.25 elad } else {
225 1.25 elad warnx("illegal argument to -a option");
226 1.25 elad usage();
227 1.25 elad }
228 1.25 elad }
229 1.25 elad break;
230 1.13 atatat case '2':
231 1.13 atatat if (dosum) {
232 1.13 atatat warnx("sum mutually exclusive with md2");
233 1.13 atatat usage();
234 1.13 atatat }
235 1.13 atatat hash = &hashes[HASH_MD2];
236 1.13 atatat break;
237 1.13 atatat case '4':
238 1.13 atatat if (dosum) {
239 1.13 atatat warnx("sum mutually exclusive with md4");
240 1.13 atatat usage();
241 1.13 atatat }
242 1.13 atatat hash = &hashes[HASH_MD4];
243 1.13 atatat break;
244 1.8 thorpej case 'm':
245 1.13 atatat case '5':
246 1.8 thorpej if (dosum) {
247 1.8 thorpej warnx("sum mutually exclusive with md5");
248 1.8 thorpej usage();
249 1.8 thorpej }
250 1.13 atatat hash = &hashes[HASH_MD5];
251 1.13 atatat break;
252 1.13 atatat case '1':
253 1.13 atatat if (dosum) {
254 1.13 atatat warnx("sum mutually exclusive with sha1");
255 1.13 atatat usage();
256 1.13 atatat }
257 1.13 atatat hash = &hashes[HASH_SHA1];
258 1.13 atatat break;
259 1.13 atatat case '6':
260 1.13 atatat if (dosum) {
261 1.13 atatat warnx("sum mutually exclusive with rmd160");
262 1.13 atatat usage();
263 1.13 atatat }
264 1.13 atatat hash = &hashes[HASH_RMD160];
265 1.8 thorpej break;
266 1.17 atatat case 'n':
267 1.17 atatat normal = 1;
268 1.17 atatat break;
269 1.1 cgd case 'o':
270 1.13 atatat if (hash) {
271 1.13 atatat warnx("%s mutually exclusive with sum",
272 1.13 atatat hash->hashname);
273 1.8 thorpej usage();
274 1.8 thorpej }
275 1.5 mycroft if (!strcmp(optarg, "1")) {
276 1.1 cgd cfncn = csum1;
277 1.1 cgd pfncn = psum1;
278 1.5 mycroft } else if (!strcmp(optarg, "2")) {
279 1.1 cgd cfncn = csum2;
280 1.1 cgd pfncn = psum2;
281 1.1 cgd } else {
282 1.5 mycroft warnx("illegal argument to -o option");
283 1.1 cgd usage();
284 1.1 cgd }
285 1.1 cgd break;
286 1.8 thorpej case 'p':
287 1.13 atatat if (hash == NULL)
288 1.13 atatat requirehash("-p");
289 1.8 thorpej pflag = 1;
290 1.8 thorpej break;
291 1.8 thorpej case 's':
292 1.13 atatat if (hash == NULL)
293 1.13 atatat requirehash("-s");
294 1.13 atatat nohashstdin = 1;
295 1.13 atatat hash->stringfunc(optarg);
296 1.8 thorpej break;
297 1.8 thorpej case 't':
298 1.13 atatat if (hash == NULL)
299 1.13 atatat requirehash("-t");
300 1.13 atatat nohashstdin = 1;
301 1.13 atatat hash->timetrialfunc();
302 1.8 thorpej break;
303 1.8 thorpej case 'x':
304 1.13 atatat if (hash == NULL)
305 1.13 atatat requirehash("-x");
306 1.13 atatat nohashstdin = 1;
307 1.13 atatat hash->testsuitefunc();
308 1.8 thorpej break;
309 1.1 cgd case '?':
310 1.1 cgd default:
311 1.1 cgd usage();
312 1.1 cgd }
313 1.1 cgd argc -= optind;
314 1.1 cgd argv += optind;
315 1.1 cgd
316 1.1 cgd fd = STDIN_FILENO;
317 1.3 cgd fn = NULL;
318 1.1 cgd rval = 0;
319 1.1 cgd do {
320 1.1 cgd if (*argv) {
321 1.1 cgd fn = *argv++;
322 1.13 atatat if (hash != NULL) {
323 1.17 atatat if (hash_digest_file(fn, hash, normal)) {
324 1.8 thorpej warn("%s", fn);
325 1.8 thorpej rval = 1;
326 1.8 thorpej }
327 1.8 thorpej continue;
328 1.8 thorpej }
329 1.1 cgd if ((fd = open(fn, O_RDONLY, 0)) < 0) {
330 1.5 mycroft warn("%s", fn);
331 1.1 cgd rval = 1;
332 1.1 cgd continue;
333 1.1 cgd }
334 1.13 atatat } else if (hash && !nohashstdin) {
335 1.13 atatat hash->filterfunc(pflag);
336 1.13 atatat }
337 1.8 thorpej
338 1.13 atatat if (hash == NULL) {
339 1.8 thorpej if (cfncn(fd, &val, &len)) {
340 1.8 thorpej warn("%s", fn ? fn : "stdin");
341 1.8 thorpej rval = 1;
342 1.8 thorpej } else
343 1.8 thorpej pfncn(fn, val, len);
344 1.8 thorpej (void)close(fd);
345 1.1 cgd }
346 1.1 cgd } while (*argv);
347 1.1 cgd exit(rval);
348 1.1 cgd }
349 1.1 cgd
350 1.8 thorpej int
351 1.22 xtraeme hash_digest_file(char *fn, struct hash *hash, int normal)
352 1.8 thorpej {
353 1.13 atatat char buf[41], *cp;
354 1.8 thorpej
355 1.13 atatat cp = hash->filefunc(fn, buf);
356 1.8 thorpej if (cp == NULL)
357 1.22 xtraeme return 1;
358 1.8 thorpej
359 1.17 atatat if (normal)
360 1.17 atatat printf("%s %s\n", cp, fn);
361 1.17 atatat else
362 1.17 atatat printf("%s (%s) = %s\n", hash->hashname, fn, cp);
363 1.22 xtraeme return 0;
364 1.8 thorpej }
365 1.8 thorpej
366 1.8 thorpej void
367 1.22 xtraeme requirehash(const char *flg)
368 1.8 thorpej {
369 1.13 atatat warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
370 1.13 atatat flg);
371 1.8 thorpej usage();
372 1.8 thorpej }
373 1.8 thorpej
374 1.3 cgd void
375 1.22 xtraeme usage(void)
376 1.1 cgd {
377 1.5 mycroft
378 1.21 wiz (void)fprintf(stderr, "usage: cksum [-n] [-m | -1 | -2 | -4 | -5 | -6 | [-o 1 | 2]] [file ...]\n");
379 1.8 thorpej (void)fprintf(stderr, " sum [file ...]\n");
380 1.8 thorpej (void)fprintf(stderr,
381 1.17 atatat " md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
382 1.13 atatat (void)fprintf(stderr,
383 1.17 atatat " md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
384 1.13 atatat (void)fprintf(stderr,
385 1.17 atatat " md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
386 1.13 atatat (void)fprintf(stderr,
387 1.17 atatat " sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
388 1.13 atatat (void)fprintf(stderr,
389 1.17 atatat " rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
390 1.1 cgd exit(1);
391 1.1 cgd }
392