]> git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/md5/md5.c
diskdev_cmds: use libiosexec
[apple_cmds.git] / text_cmds / md5 / md5.c
1 /*
2 * Derived from:
3 *
4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
5 */
6
7 /*
8 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
9 * rights reserved.
10 *
11 * RSA Data Security, Inc. makes no representations concerning either
12 * the merchantability of this software or the suitability of this
13 * software for any particular purpose. It is provided "as is"
14 * without express or implied warranty of any kind.
15 *
16 * These notices must be retained in any copies of any part of this
17 * documentation and/or software.
18 */
19
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD: src/sbin/md5/md5.c,v 1.34 2005/03/09 19:23:04 cperciva Exp $");
22
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <err.h>
27 #ifndef __APPLE__
28 #include <md5.h>
29 #include <ripemd.h>
30 #include <sha.h>
31 #include <sha256.h>
32 #endif /* !__APPLE__ */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sysexits.h>
39
40 #ifdef __APPLE__
41 #include "commoncrypto.h"
42 #endif /* __APPLE__ */
43
44 /*
45 * Length of test block, number of test blocks.
46 */
47 #define TEST_BLOCK_LEN 10000
48 #define TEST_BLOCK_COUNT 100000
49 #define MDTESTCOUNT 8
50
51 int qflag;
52 int rflag;
53 int sflag;
54
55 typedef void (DIGEST_Init)(void *);
56 typedef void (DIGEST_Update)(void *, const unsigned char *, size_t);
57 typedef char *(DIGEST_End)(void *, char *);
58
59 extern const char *MD5TestOutput[MDTESTCOUNT];
60 extern const char *SHA1_TestOutput[MDTESTCOUNT];
61 extern const char *SHA256_TestOutput[MDTESTCOUNT];
62 extern const char *RIPEMD160_TestOutput[MDTESTCOUNT];
63
64 typedef struct Algorithm_t {
65 const char *progname;
66 const char *name;
67 const char *(*TestOutput)[MDTESTCOUNT];
68 #ifdef __APPLE__
69 CCDigestAlg algorithm;
70 #else /* !__APPLE__ */
71 DIGEST_Init *Init;
72 DIGEST_Update *Update;
73 DIGEST_End *End;
74 char *(*Data)(const unsigned char *, unsigned int, char *);
75 char *(*File)(const char *, char *);
76 #endif /* __APPLE__ */
77 } Algorithm_t;
78
79 #ifndef __APPLE__
80 static void MD5_Update(MD5_CTX *, const unsigned char *, size_t);
81 #endif /* !__APPLE__ */
82 static void MDString(Algorithm_t *, const char *);
83 static void MDTimeTrial(Algorithm_t *);
84 static void MDTestSuite(Algorithm_t *);
85 static void MDFilter(Algorithm_t *, int);
86 static void usage(Algorithm_t *);
87
88 #ifdef __APPLE__
89 typedef CCDigestCtx DIGEST_CTX;
90 #else /* !__APPLE__ */
91 typedef union {
92 MD5_CTX md5;
93 SHA1_CTX sha1;
94 SHA256_CTX sha256;
95 RIPEMD160_CTX ripemd160;
96 } DIGEST_CTX;
97 #endif /* __APPLE__ */
98
99 /* max(MD5_DIGEST_LENGTH, SHA_DIGEST_LENGTH,
100 SHA256_DIGEST_LENGTH, RIPEMD160_DIGEST_LENGTH)*2+1 */
101 #define HEX_DIGEST_LENGTH 65
102
103 /* algorithm function table */
104
105 struct Algorithm_t Algorithm[] = {
106 #ifdef __APPLE__
107 { "md5", "MD5", &MD5TestOutput, kCCDigestMD5, },
108 { "sha1", "SHA1", &SHA1_TestOutput, kCCDigestSHA1 },
109 { "sha256", "SHA256", &SHA256_TestOutput, kCCDigestSHA256 },
110 { "rmd160", "RMD160", &RIPEMD160_TestOutput, kCCDigestRMD160 },
111 #else
112 { "md5", "MD5", &MD5TestOutput, (DIGEST_Init*)&MD5Init,
113 (DIGEST_Update*)&MD5_Update, (DIGEST_End*)&MD5End,
114 &MD5Data, &MD5File },
115 { "sha1", "SHA1", &SHA1_TestOutput, (DIGEST_Init*)&SHA1_Init,
116 (DIGEST_Update*)&SHA1_Update, (DIGEST_End*)&SHA1_End,
117 &SHA1_Data, &SHA1_File },
118 { "sha256", "SHA256", &SHA256_TestOutput, (DIGEST_Init*)&SHA256_Init,
119 (DIGEST_Update*)&SHA256_Update, (DIGEST_End*)&SHA256_End,
120 &SHA256_Data, &SHA256_File },
121 { "rmd160", "RMD160", &RIPEMD160_TestOutput,
122 (DIGEST_Init*)&RIPEMD160_Init, (DIGEST_Update*)&RIPEMD160_Update,
123 (DIGEST_End*)&RIPEMD160_End, &RIPEMD160_Data, &RIPEMD160_File }
124 #endif
125 };
126
127 #ifndef __APPLE__
128 static void
129 MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len)
130 {
131 MD5Update(c, data, len);
132 }
133 #endif /* !__APPLE__ */
134
135 /* Main driver.
136
137 Arguments (may be any combination):
138 -sstring - digests string
139 -t - runs time trial
140 -x - runs test script
141 filename - digests file
142 (none) - digests standard input
143 */
144 int
145 main(int argc, char *argv[])
146 {
147 int ch;
148 char *p;
149 char buf[HEX_DIGEST_LENGTH];
150 int failed=0;
151 unsigned digest=0;
152 const char* progname;
153
154 if(*argv) {
155 if ((progname = strrchr(argv[0], '/')) == NULL)
156 progname = argv[0];
157 else
158 progname++;
159
160 for (digest = 0; digest < sizeof(Algorithm)/sizeof(*Algorithm); digest++)
161 if (strcasecmp(Algorithm[digest].progname, progname) == 0)
162 break;
163
164 if (digest == sizeof(Algorithm)/sizeof(*Algorithm))
165 digest = 0;
166 }
167
168 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
169 switch (ch) {
170 case 'p':
171 MDFilter(&Algorithm[digest], 1);
172 break;
173 case 'q':
174 qflag = 1;
175 break;
176 case 'r':
177 rflag = 1;
178 break;
179 case 's':
180 sflag = 1;
181 MDString(&Algorithm[digest], optarg);
182 break;
183 case 't':
184 MDTimeTrial(&Algorithm[digest]);
185 break;
186 case 'x':
187 MDTestSuite(&Algorithm[digest]);
188 break;
189 default:
190 usage(&Algorithm[digest]);
191 }
192 argc -= optind;
193 argv += optind;
194
195 if (*argv) {
196 do {
197 #ifdef __APPLE__
198 p = Digest_File(Algorithm[digest].algorithm, *argv, buf);
199 #else
200 p = Algorithm[digest].File(*argv, buf);
201 #endif
202 if (!p) {
203 warn("%s", *argv);
204 failed++;
205 } else {
206 if (qflag)
207 printf("%s\n", p);
208 else if (rflag)
209 printf("%s %s\n", p, *argv);
210 else
211 printf("%s (%s) = %s\n", Algorithm[digest].name, *argv, p);
212 }
213 } while (*++argv);
214 } else if (!sflag && (optind == 1 || qflag || rflag))
215 MDFilter(&Algorithm[digest], 0);
216
217 if (failed != 0)
218 return (1);
219
220 return (0);
221 }
222 /*
223 * Digests a string and prints the result.
224 */
225 static void
226 MDString(Algorithm_t *alg, const char *string)
227 {
228 size_t len = strlen(string);
229 char buf[HEX_DIGEST_LENGTH];
230
231 if (qflag)
232 #ifdef __APPLE__
233 printf("%s\n", Digest_Data(alg->algorithm, string, len, buf));
234 else if (rflag)
235 printf("%s \"%s\"\n", Digest_Data(alg->algorithm, string, len, buf), string);
236 else
237 printf("%s (\"%s\") = %s\n", alg->name, string, Digest_Data(alg->algorithm, string, len, buf));
238 #else /* !__APPLE__ */
239 printf("%s\n", alg->Data(string, len, buf));
240 else if (rflag)
241 printf("%s \"%s\"\n", alg->Data(string, len, buf), string);
242 else
243 printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf));
244 #endif /* __APPLE__ */
245 }
246 /*
247 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
248 */
249 static void
250 MDTimeTrial(Algorithm_t *alg)
251 {
252 DIGEST_CTX context;
253 struct rusage before, after;
254 struct timeval total;
255 float seconds;
256 unsigned char block[TEST_BLOCK_LEN];
257 unsigned int i;
258 char *p, buf[HEX_DIGEST_LENGTH];
259
260 printf
261 ("%s time trial. Digesting %d %d-byte blocks ...",
262 alg->name, TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
263 fflush(stdout);
264
265 /* Initialize block */
266 for (i = 0; i < TEST_BLOCK_LEN; i++)
267 block[i] = (unsigned char) (i & 0xff);
268
269 /* Start timer */
270 getrusage(0, &before);
271
272 /* Digest blocks */
273 #ifdef __APPLE__
274 CCDigestInit(alg->algorithm, &context);
275 for (i = 0; i < TEST_BLOCK_COUNT; i++)
276 CCDigestUpdate(&context, block, TEST_BLOCK_LEN);
277 p = Digest_End(&context, buf);
278 #else
279 alg->Init(&context);
280 for (i = 0; i < TEST_BLOCK_COUNT; i++)
281 alg->Update(&context, block, TEST_BLOCK_LEN);
282 p = alg->End(&context, buf);
283 #endif
284
285 /* Stop timer */
286 getrusage(0, &after);
287 timersub(&after.ru_utime, &before.ru_utime, &total);
288 seconds = total.tv_sec + (float) total.tv_usec / 1000000;
289
290 printf(" done\n");
291 printf("Digest = %s", p);
292 printf("\nTime = %f seconds\n", seconds);
293 printf
294 ("Speed = %f bytes/second\n",
295 (float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
296 }
297 /*
298 * Digests a reference suite of strings and prints the results.
299 */
300
301 const char *MDTestInput[MDTESTCOUNT] = {
302 "",
303 "a",
304 "abc",
305 "message digest",
306 "abcdefghijklmnopqrstuvwxyz",
307 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
308 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
309 "MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made \
310 that its security is in some doubt"
311 };
312
313 const char *MD5TestOutput[MDTESTCOUNT] = {
314 "d41d8cd98f00b204e9800998ecf8427e",
315 "0cc175b9c0f1b6a831c399e269772661",
316 "900150983cd24fb0d6963f7d28e17f72",
317 "f96b697d7cb7938d525a2f31aaf161d0",
318 "c3fcd3d76192e4007dfb496cca67e13b",
319 "d174ab98d277d9f5a5611c2c9f419d9f",
320 "57edf4a22be3c955ac49da2e2107b67a",
321 "b50663f41d44d92171cb9976bc118538"
322 };
323
324 const char *SHA1_TestOutput[MDTESTCOUNT] = {
325 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
326 "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
327 "a9993e364706816aba3e25717850c26c9cd0d89d",
328 "c12252ceda8be8994d5fa0290a47231c1d16aae3",
329 "32d10c7b8cf96570ca04ce37f2a19d84240d3a89",
330 "761c457bf73b14d27e9e9265c46f4b4dda11f940",
331 "50abf5706a150990a08b2c5ea40fa0e585554732",
332 "18eca4333979c4181199b7b4fab8786d16cf2846"
333 };
334
335 const char *SHA256_TestOutput[MDTESTCOUNT] = {
336 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
337 "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
338 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
339 "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650",
340 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
341 "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0",
342 "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e",
343 "e6eae09f10ad4122a0e2a4075761d185a272ebd9f5aa489e998ff2f09cbfdd9f"
344 };
345
346 const char *RIPEMD160_TestOutput[MDTESTCOUNT] = {
347 "9c1185a5c5e9fc54612808977ee8f548b2258d31",
348 "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe",
349 "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
350 "5d0689ef49d2fae572b881b123a85ffa21595f36",
351 "f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
352 "b0e20b6e3116640286ed3a87a5713079b21f5189",
353 "9b752e45573d4b39f4dbd3323cab82bf63326bfb",
354 "5feb69c6bf7c29d95715ad55f57d8ac5b2b7dd32"
355 };
356
357 static void
358 MDTestSuite(Algorithm_t *alg)
359 {
360 int i;
361 char buffer[HEX_DIGEST_LENGTH];
362
363 printf("%s test suite:\n", alg->name);
364 for (i = 0; i < MDTESTCOUNT; i++) {
365 #ifdef __APPLE__
366 Digest_Data(alg->algorithm, MDTestInput[i], strlen(MDTestInput[i]), buffer);
367 #else
368 (*alg->Data)(MDTestInput[i], strlen(MDTestInput[i]), buffer);
369 #endif
370 printf("%s (\"%s\") = %s", alg->name, MDTestInput[i], buffer);
371 if (strcmp(buffer, (*alg->TestOutput)[i]) == 0)
372 printf(" - verified correct\n");
373 else
374 printf(" - INCORRECT RESULT!\n");
375 }
376 }
377
378 /*
379 * Digests the standard input and prints the result.
380 */
381 static void
382 MDFilter(Algorithm_t *alg, int tee)
383 {
384 DIGEST_CTX context;
385 unsigned int len;
386 unsigned char buffer[BUFSIZ];
387 char buf[HEX_DIGEST_LENGTH];
388
389 #ifdef __APPLE__
390 CCDigestInit(alg->algorithm, &context);
391 #else
392 alg->Init(&context);
393 #endif
394 while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
395 if (tee && len != fwrite(buffer, 1, len, stdout))
396 err(1, "stdout");
397 #ifdef __APPLE__
398 CCDigestUpdate(&context, buffer, len);
399 #else
400 alg->Update(&context, buffer, len);
401 #endif
402 }
403 if (ferror(stdin)) {
404 errx(EX_IOERR, NULL);
405 }
406 #ifdef __APPLE__
407 printf("%s\n", Digest_End(&context, buf));
408 #else
409 printf("%s\n", alg->End(&context, buf));
410 #endif
411 }
412
413 static void
414 usage(Algorithm_t *alg)
415 {
416
417 fprintf(stderr, "usage: %s [-pqrtx] [-s string] [files ...]\n", alg->progname);
418 exit(1);
419 }