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