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 <ripemd.h> 29 #include <sha.h> 30 #include <sha256.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <time.h> 35 #include <unistd.h> 36 37 /* 38 * Length of test block, number of test blocks. 39 */ 40 #define TEST_BLOCK_LEN 10000 41 #define TEST_BLOCK_COUNT 100000 42 #define MDTESTCOUNT 8 43 44 int qflag; 45 int rflag; 46 int sflag; 47 48 typedef void (DIGEST_Init)(void *); 49 typedef void (DIGEST_Update)(void *, const unsigned char *, size_t); 50 typedef char *(DIGEST_End)(void *, char *); 51 52 extern const char *MD5TestOutput[MDTESTCOUNT]; 53 extern const char *SHA1_TestOutput[MDTESTCOUNT]; 54 extern const char *SHA256_TestOutput[MDTESTCOUNT]; 55 extern const char *RIPEMD160_TestOutput[MDTESTCOUNT]; 56 57 typedef struct Algorithm_t { 58 const char *progname; 59 const char *name; 60 const char *(*TestOutput)[MDTESTCOUNT]; 61 DIGEST_Init *Init; 62 DIGEST_Update *Update; 63 DIGEST_End *End; 64 char *(*Data)(const void *, unsigned int, char *); 65 char *(*File)(const char *, char *); 66 } Algorithm_t; 67 68 static void MD5_Update(MD5_CTX *, const unsigned char *, size_t); 69 static void MDString(Algorithm_t *, const char *); 70 static void MDTimeTrial(Algorithm_t *); 71 static void MDTestSuite(Algorithm_t *); 72 static void MDFilter(Algorithm_t *, int); 73 static void usage(Algorithm_t *); 74 75 typedef union { 76 MD5_CTX md5; 77 SHA1_CTX sha1; 78 SHA256_CTX sha256; 79 RIPEMD160_CTX ripemd160; 80 } DIGEST_CTX; 81 82 /* max(MD5_DIGEST_LENGTH, SHA_DIGEST_LENGTH, 83 SHA256_DIGEST_LENGTH, RIPEMD160_DIGEST_LENGTH)*2+1 */ 84 #define HEX_DIGEST_LENGTH 65 85 86 /* algorithm function table */ 87 88 struct Algorithm_t Algorithm[] = { 89 { "md5", "MD5", &MD5TestOutput, (DIGEST_Init*)&MD5Init, 90 (DIGEST_Update*)&MD5_Update, (DIGEST_End*)&MD5End, 91 &MD5Data, &MD5File }, 92 { "sha1", "SHA1", &SHA1_TestOutput, (DIGEST_Init*)&SHA1_Init, 93 (DIGEST_Update*)&SHA1_Update, (DIGEST_End*)&SHA1_End, 94 &SHA1_Data, &SHA1_File }, 95 { "sha256", "SHA256", &SHA256_TestOutput, (DIGEST_Init*)&SHA256_Init, 96 (DIGEST_Update*)&SHA256_Update, (DIGEST_End*)&SHA256_End, 97 &SHA256_Data, &SHA256_File }, 98 { "rmd160", "RMD160", &RIPEMD160_TestOutput, 99 (DIGEST_Init*)&RIPEMD160_Init, (DIGEST_Update*)&RIPEMD160_Update, 100 (DIGEST_End*)&RIPEMD160_End, &RIPEMD160_Data, &RIPEMD160_File } 101 }; 102 103 static void 104 MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len) 105 { 106 MD5Update(c, data, len); 107 } 108 109 /* Main driver. 110 111 Arguments (may be any combination): 112 -sstring - digests string 113 -t - runs time trial 114 -x - runs test script 115 filename - digests file 116 (none) - digests standard input 117 */ 118 int 119 main(int argc, char *argv[]) 120 { 121 int ch; 122 char *p; 123 char buf[HEX_DIGEST_LENGTH]; 124 int failed; 125 unsigned digest; 126 const char* progname; 127 128 if ((progname = strrchr(argv[0], '/')) == NULL) 129 progname = argv[0]; 130 else 131 progname++; 132 133 for (digest = 0; digest < sizeof(Algorithm)/sizeof(*Algorithm); digest++) 134 if (strcasecmp(Algorithm[digest].progname, progname) == 0) 135 break; 136 137 if (digest == sizeof(Algorithm)/sizeof(*Algorithm)) 138 digest = 0; 139 140 failed = 0; 141 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1) 142 switch (ch) { 143 case 'p': 144 MDFilter(&Algorithm[digest], 1); 145 break; 146 case 'q': 147 qflag = 1; 148 break; 149 case 'r': 150 rflag = 1; 151 break; 152 case 's': 153 sflag = 1; 154 MDString(&Algorithm[digest], optarg); 155 break; 156 case 't': 157 MDTimeTrial(&Algorithm[digest]); 158 break; 159 case 'x': 160 MDTestSuite(&Algorithm[digest]); 161 break; 162 default: 163 usage(&Algorithm[digest]); 164 } 165 argc -= optind; 166 argv += optind; 167 168 if (*argv) { 169 do { 170 p = Algorithm[digest].File(*argv, buf); 171 if (!p) { 172 warn("%s", *argv); 173 failed++; 174 } else { 175 if (qflag) 176 printf("%s\n", p); 177 else if (rflag) 178 printf("%s %s\n", p, *argv); 179 else 180 printf("%s (%s) = %s\n", Algorithm[digest].name, *argv, p); 181 } 182 } while (*++argv); 183 } else if (!sflag && (optind == 1 || qflag || rflag)) 184 MDFilter(&Algorithm[digest], 0); 185 186 if (failed != 0) 187 return (1); 188 189 return (0); 190 } 191 /* 192 * Digests a string and prints the result. 193 */ 194 static void 195 MDString(Algorithm_t *alg, const char *string) 196 { 197 size_t len = strlen(string); 198 char buf[HEX_DIGEST_LENGTH]; 199 200 if (qflag) 201 printf("%s\n", alg->Data(string, len, buf)); 202 else if (rflag) 203 printf("%s \"%s\"\n", alg->Data(string, len, buf), string); 204 else 205 printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf)); 206 } 207 /* 208 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. 209 */ 210 static void 211 MDTimeTrial(Algorithm_t *alg) 212 { 213 DIGEST_CTX context; 214 struct rusage before, after; 215 struct timeval total; 216 float seconds; 217 unsigned char block[TEST_BLOCK_LEN]; 218 unsigned int i; 219 char *p, buf[HEX_DIGEST_LENGTH]; 220 221 printf 222 ("%s time trial. Digesting %d %d-byte blocks ...", 223 alg->name, TEST_BLOCK_COUNT, TEST_BLOCK_LEN); 224 fflush(stdout); 225 226 /* Initialize block */ 227 for (i = 0; i < TEST_BLOCK_LEN; i++) 228 block[i] = (unsigned char) (i & 0xff); 229 230 /* Start timer */ 231 getrusage(0, &before); 232 233 /* Digest blocks */ 234 alg->Init(&context); 235 for (i = 0; i < TEST_BLOCK_COUNT; i++) 236 alg->Update(&context, block, TEST_BLOCK_LEN); 237 p = alg->End(&context, buf); 238 239 /* Stop timer */ 240 getrusage(0, &after); 241 timersub(&after.ru_utime, &before.ru_utime, &total); 242 seconds = total.tv_sec + (float) total.tv_usec / 1000000; 243 244 printf(" done\n"); 245 printf("Digest = %s", p); 246 printf("\nTime = %f seconds\n", seconds); 247 printf 248 ("Speed = %f bytes/second\n", 249 (float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds); 250 } 251 /* 252 * Digests a reference suite of strings and prints the results. 253 */ 254 255 const char *MDTestInput[MDTESTCOUNT] = { 256 "", 257 "a", 258 "abc", 259 "message digest", 260 "abcdefghijklmnopqrstuvwxyz", 261 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 262 "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 263 "MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made \ 264 that its security is in some doubt" 265 }; 266 267 const char *MD5TestOutput[MDTESTCOUNT] = { 268 "d41d8cd98f00b204e9800998ecf8427e", 269 "0cc175b9c0f1b6a831c399e269772661", 270 "900150983cd24fb0d6963f7d28e17f72", 271 "f96b697d7cb7938d525a2f31aaf161d0", 272 "c3fcd3d76192e4007dfb496cca67e13b", 273 "d174ab98d277d9f5a5611c2c9f419d9f", 274 "57edf4a22be3c955ac49da2e2107b67a", 275 "b50663f41d44d92171cb9976bc118538" 276 }; 277 278 const char *SHA1_TestOutput[MDTESTCOUNT] = { 279 "da39a3ee5e6b4b0d3255bfef95601890afd80709", 280 "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", 281 "a9993e364706816aba3e25717850c26c9cd0d89d", 282 "c12252ceda8be8994d5fa0290a47231c1d16aae3", 283 "32d10c7b8cf96570ca04ce37f2a19d84240d3a89", 284 "761c457bf73b14d27e9e9265c46f4b4dda11f940", 285 "50abf5706a150990a08b2c5ea40fa0e585554732", 286 "18eca4333979c4181199b7b4fab8786d16cf2846" 287 }; 288 289 const char *SHA256_TestOutput[MDTESTCOUNT] = { 290 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", 291 "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", 292 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", 293 "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650", 294 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73", 295 "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0", 296 "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e", 297 "e6eae09f10ad4122a0e2a4075761d185a272ebd9f5aa489e998ff2f09cbfdd9f" 298 }; 299 300 const char *RIPEMD160_TestOutput[MDTESTCOUNT] = { 301 "9c1185a5c5e9fc54612808977ee8f548b2258d31", 302 "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", 303 "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", 304 "5d0689ef49d2fae572b881b123a85ffa21595f36", 305 "f71c27109c692c1b56bbdceb5b9d2865b3708dbc", 306 "b0e20b6e3116640286ed3a87a5713079b21f5189", 307 "9b752e45573d4b39f4dbd3323cab82bf63326bfb", 308 "5feb69c6bf7c29d95715ad55f57d8ac5b2b7dd32" 309 }; 310 311 static void 312 MDTestSuite(Algorithm_t *alg) 313 { 314 int i; 315 char buffer[HEX_DIGEST_LENGTH]; 316 317 printf("%s test suite:\n", alg->name); 318 for (i = 0; i < MDTESTCOUNT; i++) { 319 (*alg->Data)(MDTestInput[i], strlen(MDTestInput[i]), buffer); 320 printf("%s (\"%s\") = %s", alg->name, MDTestInput[i], buffer); 321 if (strcmp(buffer, (*alg->TestOutput)[i]) == 0) 322 printf(" - verified correct\n"); 323 else 324 printf(" - INCORRECT RESULT!\n"); 325 } 326 } 327 328 /* 329 * Digests the standard input and prints the result. 330 */ 331 static void 332 MDFilter(Algorithm_t *alg, int tee) 333 { 334 DIGEST_CTX context; 335 unsigned int len; 336 unsigned char buffer[BUFSIZ]; 337 char buf[HEX_DIGEST_LENGTH]; 338 339 alg->Init(&context); 340 while ((len = fread(buffer, 1, BUFSIZ, stdin))) { 341 if (tee && len != fwrite(buffer, 1, len, stdout)) 342 err(1, "stdout"); 343 alg->Update(&context, buffer, len); 344 } 345 printf("%s\n", alg->End(&context, buf)); 346 } 347 348 static void 349 usage(Algorithm_t *alg) 350 { 351 352 fprintf(stderr, "usage: %s [-pqrtx] [-s string] [files ...]\n", alg->progname); 353 exit(1); 354 } 355