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