xref: /freebsd/sbin/md5/md5.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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$");
22 
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <err.h>
27 #include <md5.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <unistd.h>
33 
34 /*
35  * Length of test block, number of test blocks.
36  */
37 #define TEST_BLOCK_LEN 10000
38 #define TEST_BLOCK_COUNT 100000
39 
40 int qflag;
41 int rflag;
42 int sflag;
43 
44 static void MDString(const char *);
45 static void MDTimeTrial(void);
46 static void MDTestSuite(void);
47 static void MDFilter(int);
48 static void usage(void);
49 
50 /* Main driver.
51 
52 Arguments (may be any combination):
53   -sstring - digests string
54   -t       - runs time trial
55   -x       - runs test script
56   filename - digests file
57   (none)   - digests standard input
58  */
59 int
60 main(int argc, char *argv[])
61 {
62 	int     ch;
63 	char   *p;
64 	char	buf[33];
65 
66 	while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
67 		switch (ch) {
68 		case 'p':
69 			MDFilter(1);
70 			break;
71 		case 'q':
72 			qflag = 1;
73 			break;
74 		case 'r':
75 			rflag = 1;
76 			break;
77 		case 's':
78 			sflag = 1;
79 			MDString(optarg);
80 			break;
81 		case 't':
82 			MDTimeTrial();
83 			break;
84 		case 'x':
85 			MDTestSuite();
86 			break;
87 		default:
88 			usage();
89 		}
90 	argc -= optind;
91 	argv += optind;
92 
93 	if (*argv) {
94 		do {
95 			p = MD5File(*argv, buf);
96 			if (!p)
97 				warn("%s", *argv);
98 			else
99 				if (qflag)
100 					printf("%s\n", p);
101 				else if (rflag)
102 					printf("%s %s\n", p, *argv);
103 				else
104 					printf("MD5 (%s) = %s\n", *argv, p);
105 		} while (*++argv);
106 	} else if (!sflag && (optind == 1 || qflag || rflag))
107 		MDFilter(0);
108 
109 	return (0);
110 }
111 /*
112  * Digests a string and prints the result.
113  */
114 static void
115 MDString(const char *string)
116 {
117 	size_t len = strlen(string);
118 	char buf[33];
119 
120 	if (qflag)
121 		printf("%s\n", MD5Data(string, len, buf));
122 	else if (rflag)
123 		printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
124 	else
125 		printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
126 }
127 /*
128  * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
129  */
130 static void
131 MDTimeTrial(void)
132 {
133 	MD5_CTX context;
134 	struct rusage before, after;
135 	struct timeval total;
136 	float seconds;
137 	unsigned char block[TEST_BLOCK_LEN];
138 	unsigned int i;
139 	char   *p, buf[33];
140 
141 	printf
142 	    ("MD5 time trial. Digesting %d %d-byte blocks ...",
143 	    TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
144 	fflush(stdout);
145 
146 	/* Initialize block */
147 	for (i = 0; i < TEST_BLOCK_LEN; i++)
148 		block[i] = (unsigned char) (i & 0xff);
149 
150 	/* Start timer */
151 	getrusage(0, &before);
152 
153 	/* Digest blocks */
154 	MD5Init(&context);
155 	for (i = 0; i < TEST_BLOCK_COUNT; i++)
156 		MD5Update(&context, block, TEST_BLOCK_LEN);
157 	p = MD5End(&context,buf);
158 
159 	/* Stop timer */
160 	getrusage(0, &after);
161 	timersub(&after.ru_utime, &before.ru_utime, &total);
162 	seconds = total.tv_sec + (float) total.tv_usec / 1000000;
163 
164 	printf(" done\n");
165 	printf("Digest = %s", p);
166 	printf("\nTime = %f seconds\n", seconds);
167 	printf
168 	    ("Speed = %f bytes/second\n",
169 	    (float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
170 }
171 /*
172  * Digests a reference suite of strings and prints the results.
173  */
174 
175 #define MD5TESTCOUNT 8
176 
177 char *MDTestInput[] = {
178 	"",
179 	"a",
180 	"abc",
181 	"message digest",
182 	"abcdefghijklmnopqrstuvwxyz",
183 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
184 	"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
185 	"MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made that its security is in some doubt"
186 };
187 
188 char *MDTestOutput[MD5TESTCOUNT] = {
189 	"d41d8cd98f00b204e9800998ecf8427e",
190 	"0cc175b9c0f1b6a831c399e269772661",
191 	"900150983cd24fb0d6963f7d28e17f72",
192 	"f96b697d7cb7938d525a2f31aaf161d0",
193 	"c3fcd3d76192e4007dfb496cca67e13b",
194 	"d174ab98d277d9f5a5611c2c9f419d9f",
195 	"57edf4a22be3c955ac49da2e2107b67a",
196 	"b50663f41d44d92171cb9976bc118538"
197 };
198 
199 static void
200 MDTestSuite(void)
201 {
202 	int i;
203 	char buffer[33];
204 
205 	printf("MD5 test suite:\n");
206 	for (i = 0; i < MD5TESTCOUNT; i++) {
207 		MD5Data(MDTestInput[i], strlen(MDTestInput[i]), buffer);
208 		printf("MD5 (\"%s\") = %s", MDTestInput[i], buffer);
209 		if (strcmp(buffer, MDTestOutput[i]) == 0)
210 			printf(" - verified correct\n");
211 		else
212 			printf(" - INCORRECT RESULT!\n");
213 	}
214 }
215 
216 /*
217  * Digests the standard input and prints the result.
218  */
219 static void
220 MDFilter(int tee)
221 {
222 	MD5_CTX context;
223 	unsigned int len;
224 	unsigned char buffer[BUFSIZ];
225 	char buf[33];
226 
227 	MD5Init(&context);
228 	while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
229 		if (tee && len != fwrite(buffer, 1, len, stdout))
230 			err(1, "stdout");
231 		MD5Update(&context, buffer, len);
232 	}
233 	printf("%s\n", MD5End(&context,buf));
234 }
235 
236 static void
237 usage(void)
238 {
239 
240 	fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");
241 	exit(1);
242 }
243