Parallelize in_ifaddrhead operation
[dragonfly.git] / sbin / md5 / md5.c
blobcaf46edbc0965b6329167bfea2302509c6eb616b
1 /*
2 * Derived from:
4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
6 * $FreeBSD: src/sbin/md5/md5.c,v 1.35 2006/01/17 15:35:57 phk Exp $
7 * $DragonFly: src/sbin/md5/md5.c,v 1.4 2008/01/16 14:18:57 matthias Exp $
8 */
11 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
12 * rights reserved.
14 * RSA Data Security, Inc. makes no representations concerning either
15 * the merchantability of this software or the suitability of this
16 * software for any particular purpose. It is provided "as is"
17 * without express or implied warranty of any kind.
19 * These notices must be retained in any copies of any part of this
20 * documentation and/or software.
23 #include <sys/cdefs.h>
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #include <err.h>
29 #include <md5.h>
30 #include <ripemd.h>
31 #include <sha.h>
32 #include <sha256.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37 #include <unistd.h>
40 * Length of test block, number of test blocks.
42 #define TEST_BLOCK_LEN 10000
43 #define TEST_BLOCK_COUNT 100000
44 #define MDTESTCOUNT 8
46 int qflag;
47 int rflag;
48 int sflag;
50 typedef void (DIGEST_Init)(void *);
51 typedef void (DIGEST_Update)(void *, const unsigned char *, size_t);
52 typedef char *(DIGEST_End)(void *, char *);
54 extern const char *MD5TestOutput[MDTESTCOUNT];
55 extern const char *SHA1_TestOutput[MDTESTCOUNT];
56 extern const char *SHA256_TestOutput[MDTESTCOUNT];
57 extern const char *RIPEMD160_TestOutput[MDTESTCOUNT];
59 typedef struct Algorithm_t {
60 const char *progname;
61 const char *name;
62 const char *(*TestOutput)[MDTESTCOUNT];
63 DIGEST_Init *Init;
64 DIGEST_Update *Update;
65 DIGEST_End *End;
66 char *(*Data)(const void *, unsigned int, char *);
67 char *(*File)(const char *, char *);
68 } Algorithm_t;
70 static void MD5_Update(MD5_CTX *, const unsigned char *, size_t);
71 static void MDString(Algorithm_t *, const char *);
72 static void MDTimeTrial(Algorithm_t *);
73 static void MDTestSuite(Algorithm_t *);
74 static void MDFilter(Algorithm_t *, int);
75 static void usage(Algorithm_t *);
77 typedef union {
78 MD5_CTX md5;
79 SHA1_CTX sha1;
80 SHA256_CTX sha256;
81 RIPEMD160_CTX ripemd160;
82 } DIGEST_CTX;
84 /* max(MD5_DIGEST_LENGTH, SHA_DIGEST_LENGTH,
85 SHA256_DIGEST_LENGTH, RIPEMD160_DIGEST_LENGTH)*2+1 */
86 #define HEX_DIGEST_LENGTH 65
88 /* algorithm function table */
90 struct Algorithm_t Algorithm[] = {
91 { "md5", "MD5", &MD5TestOutput, (DIGEST_Init*)&MD5Init,
92 (DIGEST_Update*)&MD5_Update, (DIGEST_End*)&MD5End,
93 &MD5Data, &MD5File },
94 { "sha1", "SHA1", &SHA1_TestOutput, (DIGEST_Init*)&SHA1_Init,
95 (DIGEST_Update*)&SHA1_Update, (DIGEST_End*)&SHA1_End,
96 &SHA1_Data, &SHA1_File },
97 { "sha256", "SHA256", &SHA256_TestOutput, (DIGEST_Init*)&SHA256_Init,
98 (DIGEST_Update*)&SHA256_Update, (DIGEST_End*)&SHA256_End,
99 &SHA256_Data, &SHA256_File },
100 { "rmd160", "RMD160", &RIPEMD160_TestOutput,
101 (DIGEST_Init*)&RIPEMD160_Init, (DIGEST_Update*)&RIPEMD160_Update,
102 (DIGEST_End*)&RIPEMD160_End, &RIPEMD160_Data, &RIPEMD160_File }
105 static void
106 MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len)
108 MD5Update(c, data, len);
111 /* Main driver.
113 Arguments (may be any combination):
114 -sstring - digests string
115 -t - runs time trial
116 -x - runs test script
117 filename - digests file
118 (none) - digests standard input
121 main(int argc, char *argv[])
123 int ch;
124 char *p;
125 char buf[HEX_DIGEST_LENGTH];
126 int failed;
127 unsigned digest;
128 const char* progname;
130 if ((progname = strrchr(argv[0], '/')) == NULL)
131 progname = argv[0];
132 else
133 progname++;
135 for (digest = 0; digest < sizeof(Algorithm)/sizeof(*Algorithm); digest++)
136 if (strcasecmp(Algorithm[digest].progname, progname) == 0)
137 break;
139 if (digest == sizeof(Algorithm)/sizeof(*Algorithm))
140 digest = 0;
142 failed = 0;
143 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
144 switch (ch) {
145 case 'p':
146 MDFilter(&Algorithm[digest], 1);
147 break;
148 case 'q':
149 qflag = 1;
150 break;
151 case 'r':
152 rflag = 1;
153 break;
154 case 's':
155 sflag = 1;
156 MDString(&Algorithm[digest], optarg);
157 break;
158 case 't':
159 MDTimeTrial(&Algorithm[digest]);
160 break;
161 case 'x':
162 MDTestSuite(&Algorithm[digest]);
163 break;
164 default:
165 usage(&Algorithm[digest]);
167 argc -= optind;
168 argv += optind;
170 if (*argv) {
171 do {
172 p = Algorithm[digest].File(*argv, buf);
173 if (!p) {
174 warn("%s", *argv);
175 failed++;
176 } else {
177 if (qflag)
178 printf("%s\n", p);
179 else if (rflag)
180 printf("%s %s\n", p, *argv);
181 else
182 printf("%s (%s) = %s\n", Algorithm[digest].name, *argv, p);
184 } while (*++argv);
185 } else if (!sflag && (optind == 1 || qflag || rflag))
186 MDFilter(&Algorithm[digest], 0);
188 if (failed != 0)
189 return (1);
191 return (0);
194 * Digests a string and prints the result.
196 static void
197 MDString(Algorithm_t *alg, const char *string)
199 size_t len = strlen(string);
200 char buf[HEX_DIGEST_LENGTH];
202 if (qflag)
203 printf("%s\n", alg->Data(string, len, buf));
204 else if (rflag)
205 printf("%s \"%s\"\n", alg->Data(string, len, buf), string);
206 else
207 printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf));
210 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
212 static void
213 MDTimeTrial(Algorithm_t *alg)
215 DIGEST_CTX context;
216 struct rusage before, after;
217 struct timeval total;
218 float seconds;
219 unsigned char block[TEST_BLOCK_LEN];
220 unsigned int i;
221 char *p, buf[HEX_DIGEST_LENGTH];
223 printf
224 ("%s time trial. Digesting %d %d-byte blocks ...",
225 alg->name, TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
226 fflush(stdout);
228 /* Initialize block */
229 for (i = 0; i < TEST_BLOCK_LEN; i++)
230 block[i] = (unsigned char) (i & 0xff);
232 /* Start timer */
233 getrusage(0, &before);
235 /* Digest blocks */
236 alg->Init(&context);
237 for (i = 0; i < TEST_BLOCK_COUNT; i++)
238 alg->Update(&context, block, TEST_BLOCK_LEN);
239 p = alg->End(&context, buf);
241 /* Stop timer */
242 getrusage(0, &after);
243 timersub(&after.ru_utime, &before.ru_utime, &total);
244 seconds = total.tv_sec + (float) total.tv_usec / 1000000;
246 printf(" done\n");
247 printf("Digest = %s", p);
248 printf("\nTime = %f seconds\n", seconds);
249 printf
250 ("Speed = %f bytes/second\n",
251 (float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
254 * Digests a reference suite of strings and prints the results.
257 const char *MDTestInput[MDTESTCOUNT] = {
258 "",
259 "a",
260 "abc",
261 "message digest",
262 "abcdefghijklmnopqrstuvwxyz",
263 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
264 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
265 "MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made \
266 that its security is in some doubt"
269 const char *MD5TestOutput[MDTESTCOUNT] = {
270 "d41d8cd98f00b204e9800998ecf8427e",
271 "0cc175b9c0f1b6a831c399e269772661",
272 "900150983cd24fb0d6963f7d28e17f72",
273 "f96b697d7cb7938d525a2f31aaf161d0",
274 "c3fcd3d76192e4007dfb496cca67e13b",
275 "d174ab98d277d9f5a5611c2c9f419d9f",
276 "57edf4a22be3c955ac49da2e2107b67a",
277 "b50663f41d44d92171cb9976bc118538"
280 const char *SHA1_TestOutput[MDTESTCOUNT] = {
281 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
282 "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
283 "a9993e364706816aba3e25717850c26c9cd0d89d",
284 "c12252ceda8be8994d5fa0290a47231c1d16aae3",
285 "32d10c7b8cf96570ca04ce37f2a19d84240d3a89",
286 "761c457bf73b14d27e9e9265c46f4b4dda11f940",
287 "50abf5706a150990a08b2c5ea40fa0e585554732",
288 "18eca4333979c4181199b7b4fab8786d16cf2846"
291 const char *SHA256_TestOutput[MDTESTCOUNT] = {
292 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
293 "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
294 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
295 "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650",
296 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
297 "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0",
298 "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e",
299 "e6eae09f10ad4122a0e2a4075761d185a272ebd9f5aa489e998ff2f09cbfdd9f"
302 const char *RIPEMD160_TestOutput[MDTESTCOUNT] = {
303 "9c1185a5c5e9fc54612808977ee8f548b2258d31",
304 "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe",
305 "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
306 "5d0689ef49d2fae572b881b123a85ffa21595f36",
307 "f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
308 "b0e20b6e3116640286ed3a87a5713079b21f5189",
309 "9b752e45573d4b39f4dbd3323cab82bf63326bfb",
310 "5feb69c6bf7c29d95715ad55f57d8ac5b2b7dd32"
313 static void
314 MDTestSuite(Algorithm_t *alg)
316 int i;
317 char buffer[HEX_DIGEST_LENGTH];
319 printf("%s test suite:\n", alg->name);
320 for (i = 0; i < MDTESTCOUNT; i++) {
321 (*alg->Data)(MDTestInput[i], strlen(MDTestInput[i]), buffer);
322 printf("%s (\"%s\") = %s", alg->name, MDTestInput[i], buffer);
323 if (strcmp(buffer, (*alg->TestOutput)[i]) == 0)
324 printf(" - verified correct\n");
325 else
326 printf(" - INCORRECT RESULT!\n");
331 * Digests the standard input and prints the result.
333 static void
334 MDFilter(Algorithm_t *alg, int tee)
336 DIGEST_CTX context;
337 unsigned int len;
338 unsigned char buffer[BUFSIZ];
339 char buf[HEX_DIGEST_LENGTH];
341 alg->Init(&context);
342 while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
343 if (tee && len != fwrite(buffer, 1, len, stdout))
344 err(1, "stdout");
345 alg->Update(&context, buffer, len);
347 printf("%s\n", alg->End(&context, buf));
350 static void
351 usage(Algorithm_t *alg)
354 fprintf(stderr, "usage: %s [-pqrtx] [-s string] [files ...]\n", alg->progname);
355 exit(1);