1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_mddriver.c - test driver for MD2, MD4 and MD5 */
3 /*
4 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
5 * rights reserved.
6 *
7 * RSA Data Security, Inc. makes no representations concerning either
8 * the merchantability of this software or the suitability of this
9 * software for any particular purpose. It is provided "as is"
10 * without express or implied warranty of any kind.
11 *
12 * These notices must be retained in any copies of any part of this
13 * documentation and/or software.
14 */
15
16 /* The following makes MD default to MD5 if it has not already been
17 defined with C compiler flags.
18 */
19 #ifndef MD
20 #define MD 5
21 #endif
22
23 #include "crypto_int.h"
24
25 /* Length of test block, number of test blocks.
26 */
27 #define TEST_BLOCK_LEN 1000
28 #define TEST_BLOCK_COUNT 1000
29
30 static void MDHash (char *, size_t, size_t, unsigned char *);
31 static void MDString (char *);
32 static void MDTimeTrial (void);
33 static void MDTestSuite (void);
34 static void MDPrint (unsigned char [16]);
35
36 struct md_test_entry {
37 char *string;
38 unsigned char digest[16];
39 };
40
41 #if MD == 4
42 #define MDProvider krb5int_hash_md4
43
44 #define HAVE_TEST_SUITE
45 /* Test suite from RFC 1320 */
46
47 struct md_test_entry md_test_suite[] = {
48 { "",
49 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
50 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0 }},
51 { "a",
52 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
53 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24 }},
54 { "abc",
55 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
56 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d }},
57 { "message digest",
58 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
59 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b }},
60 { "abcdefghijklmnopqrstuvwxyz",
61 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
62 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9 }},
63 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
64 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
65 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4 }},
66 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
67 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
68 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36 }},
69 {0, {0}}
70 };
71
72 #endif
73
74 #if MD == 5
75 #define MDProvider krb5int_hash_md5
76
77 #define HAVE_TEST_SUITE
78 /* Test suite from RFC 1321 */
79
80 struct md_test_entry md_test_suite[] = {
81 { "",
82 {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
83 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }},
84 { "a",
85 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
86 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 }},
87 { "abc",
88 {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
89 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 }},
90 { "message digest",
91 {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
92 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 }},
93 { "abcdefghijklmnopqrstuvwxyz",
94 {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
95 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b }},
96 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
97 {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
98 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f }},
99 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
100 {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
101 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a }},
102 { 0, {0} }
103 };
104
105 #endif
106
107 /* Main driver.
108
109 Arguments (may be any combination):
110 -sstring - digests string
111 -t - runs time trial
112 -x - runs test script
113 */
114 int
main(int argc,char * argv[])115 main(int argc, char *argv[])
116 {
117 int i;
118
119 for (i = 1; i < argc; i++) {
120 if (argv[i][0] == '-' && argv[i][1] == 's')
121 MDString (argv[i] + 2);
122 else if (strcmp (argv[i], "-t") == 0)
123 MDTimeTrial ();
124 else if (strcmp (argv[i], "-x") == 0)
125 MDTestSuite ();
126 }
127 return (0);
128 }
129
130 static void
MDHash(char * bytes,size_t len,size_t count,unsigned char * out)131 MDHash(char *bytes, size_t len, size_t count, unsigned char *out)
132 {
133 krb5_crypto_iov *iov;
134 krb5_data outdata = make_data (out, MDProvider.hashsize);
135 size_t i;
136
137 iov = malloc (count * sizeof(*iov));
138 if (iov == NULL)
139 abort ();
140 for (i = 0; i < count; i++) {
141 iov[i].flags = KRB5_CRYPTO_TYPE_DATA;
142 iov[i].data = make_data (bytes, len);
143 }
144 MDProvider.hash(iov, count, &outdata);
145 free(iov);
146 }
147
148 /* Digests a string and prints the result.
149 */
150 static void
MDString(char * string)151 MDString(char *string)
152 {
153 unsigned char digest[16];
154
155 MDHash (string, strlen(string), 1, digest);
156 printf ("MD%d (\"%s\") = ", MD, string);
157 MDPrint (digest);
158 printf ("\n");
159 }
160
161 /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
162 blocks.
163 */
164 static void
MDTimeTrial(void)165 MDTimeTrial(void)
166 {
167 time_t endTime, startTime;
168 unsigned char block[TEST_BLOCK_LEN], digest[16];
169 unsigned int i;
170
171 printf("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
172 TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
173
174 /* Initialize block */
175 for (i = 0; i < TEST_BLOCK_LEN; i++)
176 block[i] = (unsigned char)(i & 0xff);
177
178 /* Start timer */
179 time (&startTime);
180
181 /* Digest blocks */
182 MDHash ((char *)block, TEST_BLOCK_LEN, TEST_BLOCK_COUNT, digest);
183
184 /* Stop timer */
185 time (&endTime);
186
187 printf (" done\n");
188 printf ("Digest = ");
189 MDPrint (digest);
190 printf ("\nTime = %ld seconds\n", (long)(endTime-startTime));
191 printf
192 ("Speed = %ld bytes/second\n",
193 (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime));
194 }
195
196 /* Digests a reference suite of strings and prints the results.
197 */
198 static void
MDTestSuite(void)199 MDTestSuite(void)
200 {
201 #ifdef HAVE_TEST_SUITE
202 struct md_test_entry *entry;
203 int num_tests = 0, num_failed = 0;
204 unsigned char digest[16];
205
206 printf ("MD%d test suite:\n\n", MD);
207 for (entry = md_test_suite; entry->string; entry++) {
208 unsigned int len = strlen (entry->string);
209
210 MDHash (entry->string, len, 1, digest);
211
212 printf ("MD%d (\"%s\") = ", MD, entry->string);
213 MDPrint (digest);
214 printf ("\n");
215 if (memcmp(digest, entry->digest, 16) != 0) {
216 printf("\tIncorrect MD%d digest! Should have been:\n\t\t ", MD);
217 MDPrint(entry->digest);
218 printf("\n");
219 num_failed++;
220 }
221 num_tests++;
222 }
223 if (num_failed) {
224 printf("%d out of %d tests failed for MD%d!!!\n", num_failed,
225 num_tests, MD);
226 exit(1);
227 } else {
228 printf ("%d tests passed successfully for MD%d.\n", num_tests, MD);
229 exit(0);
230 }
231 #else
232
233 printf ("MD%d test suite:\n", MD);
234 MDString ("");
235 MDString ("a");
236 MDString ("abc");
237 MDString ("message digest");
238 MDString ("abcdefghijklmnopqrstuvwxyz");
239 MDString
240 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
241 MDString
242 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
243 #endif
244 }
245
246 /* Prints a message digest in hexadecimal.
247 */
248 static void
MDPrint(unsigned char digest[16])249 MDPrint(unsigned char digest[16])
250 {
251 unsigned int i;
252
253 for (i = 0; i < 16; i++)
254 printf ("%02x", digest[i]);
255 }
256