/* MDDRIVE2.C - Katholieke Universiteit Leuven, test driver for MD5-MAC and other MACs from the same family */ /* Copyright (C) 1995, K.U.Leuven. All rights reserved. Derived from MDDRIVER.C Author: Bart Preneel */ /*------------------------------------------------------------------------*/ /* MDDRIVER.C - test driver for MD2, MD4 and MD5 */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All rights reserved. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind. These notices must be retained in any copies of any part of this documentation and/or software. */ /*------------------------------------------------------------------------*/ /* error was corrected in message that is printed on screen during time trial BP 09/09/92 */ /* The following makes MD default to MD5-MAC if it has not already been defined with C compiler flags. */ #ifndef MD #define MD 5 #endif #include #include #include #include "global.h" #if MD == 5 #include "md5mac.h" #endif /* Length of test block, number of test blocks. */ #define TEST_BLOCK_LEN 2048 #define TEST_BLOCK_COUNT 93750 /* test values for the key. */ static unsigned char Ktest1[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66, 0x77,0x88, 0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff }; static unsigned char Ktest2[16] = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd, 0xef,0xfe, 0xdc,0xba,0x98,0x76,0x54,0x32,0x10 }; static void MDString PROTO_LIST ((char *, unsigned char [16])); static void MDTimeTrial PROTO_LIST ((void)); static void MDTestSuite PROTO_LIST ((void)); static void MDFile PROTO_LIST ((char *)); static void MDFilter PROTO_LIST ((void)); static void MDPrint PROTO_LIST ((unsigned char [16])); static void MDKeyread PROTO_LIST ((unsigned char [16])); #if MD == 5 #define MD_CTX MD5MAC_CTX #define MDInit MD5MACInit #define MDUpdate MD5MACUpdate #define MDFinal MD5MACFinal #define MDKeysetup MD5MACKeysetup #endif /* Main driver. Arguments (may be any combination): -sstring - digests string - reads key from key.dat -t - runs time trial - 1 test key -x - runs test script - 2 test keys filename - digests file - reads key from key.dat (none) - digests standard input - reads key from key.dat */ /*------------------------------------------------------------------------*/ int main (argc, argv) int argc; char *argv[]; { int i; unsigned char key[16]; if (argc > 1) for (i = 1; i < argc; i++) if (argv[i][0] == '-' && argv[i][1] == 's') { MDKeyread (key); MDString (argv[i] + 2, key); } else if (strcmp (argv[i], "-t") == 0) MDTimeTrial (); else if (strcmp (argv[i], "-x") == 0) MDTestSuite (); else MDFile (argv[i]); else MDFilter (); return (0); } /*------------------------------------------------------------------------*/ /* Digests a string and prints the result. */ static void MDString (string, key) char *string; unsigned char key[16]; { MD_CTX context; unsigned char digest[16]; unsigned int len = strlen (string); MDKeysetup (&context, key); MDInit (&context); MDUpdate (&context, string, len); MDFinal (digest, &context); printf ("key = "); MDPrint (key); printf ("\n"); printf ("MD%1d-MAC (\"%s\") = ", MD, string); MDPrint (digest); printf ("\n"); } /*------------------------------------------------------------------------*/ /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. */ static void MDTimeTrial () { MD_CTX context; time_t endTime, startTime; unsigned char block[TEST_BLOCK_LEN], digest[16]; unsigned int i; long deltaTime; printf ("MD%1d-MAC time trial. Digesting %d %d-byte blocks ...", MD, TEST_BLOCK_COUNT, TEST_BLOCK_LEN); /* note: these variables were swapped in the original file */ /* Initialize key */ MDKeysetup (&context, Ktest1); /* Initialize block */ for (i = 0; i < TEST_BLOCK_LEN; i++) block[i] = (unsigned char)(i & 0xff); /* Start timer */ time (&startTime); /* Digest blocks */ MDInit (&context); for (i = 0; i < TEST_BLOCK_COUNT; i++) MDUpdate (&context, block, TEST_BLOCK_LEN); MDFinal (digest, &context); /* Stop timer */ time (&endTime); printf (" done\n"); printf ("Digest = "); MDPrint (digest); deltaTime = (long)(endTime-startTime); printf ("\nTime = %ld seconds\n", deltaTime); printf ("Speed = %10ld bytes/second\n", (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/deltaTime); printf (" = %10.3f Mbit/second\n", 0.000008*(double)TEST_BLOCK_LEN*(double)TEST_BLOCK_COUNT/(double)deltaTime); } /*------------------------------------------------------------------------*/ /* Digests a reference suite of strings and prints the results. */ static void MDTestSuite () { printf("MD%d-MAC test suite:\n", MD); /* test key 1 */ printf ("key 1\n"); MDString ("", Ktest1); MDString ("a", Ktest1); MDString ("abc", Ktest1); MDString ("message digest", Ktest1); MDString ("abcdefghijklmnopqrstuvwxyz", Ktest1); MDString ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", Ktest1); MDString ("12345678901234567890123456789012345678901234567890123456789012345678901234567890", Ktest1); /* test key 2 */ printf ("key 2\n"); MDString ("", Ktest2); MDString ("a", Ktest2); MDString ("abc", Ktest2); MDString ("message digest", Ktest2); MDString ("abcdefghijklmnopqrstuvwxyz", Ktest2); MDString ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", Ktest2); MDString ("12345678901234567890123456789012345678901234567890123456789012345678901234567890", Ktest2); } /*------------------------------------------------------------------------*/ /* Digests a file and prints the result. */ static void MDFile (filename) char *filename; { FILE *file; MD_CTX context; int len; unsigned char buffer[1024], digest[16], key[16]; MDKeyread (key); printf ("key = "); MDPrint (key); printf ("\n"); MDKeysetup (&context, key); if ((file = fopen (filename, "rb")) == NULL) printf ("%s can't be opened\n", filename); else { MDInit (&context); while (len = fread (buffer, 1, 1024, file)) MDUpdate (&context, buffer, len); MDFinal (digest, &context); fclose (file); printf ("MD%d (%s) = ", MD, filename); MDPrint (digest); printf ("\n"); } } /*------------------------------------------------------------------------*/ /* Digests the standard input and prints the result. */ static void MDFilter () { MD_CTX context; int len; unsigned char buffer[16], digest[16], key[16]; MDKeyread (key); MDKeysetup (&context, key); MDInit (&context); while (len = fread (buffer, 1, 16, stdin)) MDUpdate (&context, buffer, len); MDFinal (digest, &context); MDPrint (digest); printf ("\n"); } /*------------------------------------------------------------------------*/ /* Prints a message digest or a key in hexadecimal. */ static void MDPrint (digest) unsigned char digest[16]; { unsigned int i; for (i = 0; i < 16; i++) printf ("%02x", digest[i]); } /*------------------------------------------------------------------------*/ static void MDKeyread(key) unsigned char key[16]; { FILE *file; unsigned int i; int temp; if ( (file = fopen("key.dat", "r")) == NULL ) { printf("MDDRIVE2 readkey: key file key.dat can't be opened.\n"); fclose(file); exit(1); } else { if (fscanf(file,"%02x",&temp) == EOF) { printf ("MD5readkey: EOF before read was completed in key file key.dat.\n"); fclose(file); exit(1); } else { key[0] = (unsigned char)temp; for (i=1;i < 16;i++) { fscanf(file,"%02x",&temp); key[i] = (unsigned char)temp; } } } fclose(file); } /*------------------------------------------------------------------------*/