1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Derived from:
|
1998-07-06 07:04:50 +00:00
|
|
|
*
|
1995-02-20 00:48:50 +00:00
|
|
|
* MDDRIVER.C - test driver for MD2, MD4 and MD5
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
|
1998-07-06 07:04:50 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1998-07-06 07:04:50 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <sys/types.h>
|
2003-02-14 04:48:06 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
1998-07-06 07:04:50 +00:00
|
|
|
#include <err.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <md5.h>
|
1994-10-29 23:58:02 +00:00
|
|
|
#include <stdio.h>
|
2001-06-24 23:04:23 +00:00
|
|
|
#include <stdlib.h>
|
1999-07-21 00:51:14 +00:00
|
|
|
#include <string.h>
|
2001-06-29 06:21:57 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Length of test block, number of test blocks.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1999-05-01 14:54:21 +00:00
|
|
|
#define TEST_BLOCK_LEN 10000
|
|
|
|
#define TEST_BLOCK_COUNT 100000
|
1994-10-29 23:58:02 +00:00
|
|
|
|
1999-12-04 01:29:43 +00:00
|
|
|
int qflag;
|
1999-11-07 04:14:55 +00:00
|
|
|
int rflag;
|
2001-12-19 21:19:43 +00:00
|
|
|
int sflag;
|
1999-11-07 04:14:55 +00:00
|
|
|
|
2001-05-22 10:33:43 +00:00
|
|
|
static void MDString(const char *);
|
|
|
|
static void MDTimeTrial(void);
|
|
|
|
static void MDTestSuite(void);
|
|
|
|
static void MDFilter(int);
|
|
|
|
static void usage(void);
|
1994-10-29 23:58:02 +00:00
|
|
|
|
|
|
|
/* Main driver.
|
|
|
|
|
|
|
|
Arguments (may be any combination):
|
|
|
|
-sstring - digests string
|
|
|
|
-t - runs time trial
|
|
|
|
-x - runs test script
|
|
|
|
filename - digests file
|
|
|
|
(none) - digests standard input
|
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
int
|
2001-05-22 10:33:43 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1997-12-29 14:05:49 +00:00
|
|
|
int ch;
|
1995-02-20 00:48:50 +00:00
|
|
|
char *p;
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
2001-06-25 08:52:57 +00:00
|
|
|
while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
|
2001-05-22 10:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
MDFilter(1);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
2001-12-19 21:19:43 +00:00
|
|
|
sflag = 1;
|
2001-05-22 10:33:43 +00:00
|
|
|
MDString(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
MDTimeTrial();
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
MDTestSuite();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
1997-12-29 03:40:37 +00:00
|
|
|
}
|
2001-05-22 10:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
do {
|
|
|
|
p = MD5File(*argv, buf);
|
1997-12-29 03:40:37 +00:00
|
|
|
if (!p)
|
2001-05-22 10:33:43 +00:00
|
|
|
warn("%s", *argv);
|
1997-12-29 03:40:37 +00:00
|
|
|
else
|
1999-12-04 01:29:43 +00:00
|
|
|
if (qflag)
|
|
|
|
printf("%s\n", p);
|
|
|
|
else if (rflag)
|
2001-05-22 10:33:43 +00:00
|
|
|
printf("%s %s\n", p, *argv);
|
1999-11-07 04:14:55 +00:00
|
|
|
else
|
2001-05-22 10:33:43 +00:00
|
|
|
printf("MD5 (%s) = %s\n", *argv, p);
|
|
|
|
} while (*++argv);
|
2001-12-19 21:19:43 +00:00
|
|
|
} else if (!sflag && (optind == 1 || qflag || rflag))
|
1995-02-26 01:55:31 +00:00
|
|
|
MDFilter(0);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
return (0);
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests a string and prints the result.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
MDString(const char *string)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1999-07-21 00:51:14 +00:00
|
|
|
size_t len = strlen(string);
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1994-10-29 23:58:02 +00:00
|
|
|
|
1999-12-04 01:29:43 +00:00
|
|
|
if (qflag)
|
|
|
|
printf("%s\n", MD5Data(string, len, buf));
|
|
|
|
else if (rflag)
|
1999-11-11 11:59:03 +00:00
|
|
|
printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
|
1999-11-07 04:14:55 +00:00
|
|
|
else
|
|
|
|
printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
MDTimeTrial(void)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5_CTX context;
|
2003-02-14 04:48:06 +00:00
|
|
|
struct rusage before, after;
|
|
|
|
struct timeval total;
|
|
|
|
float seconds;
|
1995-02-26 01:55:31 +00:00
|
|
|
unsigned char block[TEST_BLOCK_LEN];
|
1995-02-20 00:48:50 +00:00
|
|
|
unsigned int i;
|
1995-07-12 09:14:46 +00:00
|
|
|
char *p, buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
printf
|
|
|
|
("MD5 time trial. Digesting %d %d-byte blocks ...",
|
1997-01-16 10:12:30 +00:00
|
|
|
TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
|
1999-05-01 14:54:21 +00:00
|
|
|
fflush(stdout);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
/* Initialize block */
|
|
|
|
for (i = 0; i < TEST_BLOCK_LEN; i++)
|
|
|
|
block[i] = (unsigned char) (i & 0xff);
|
|
|
|
|
|
|
|
/* Start timer */
|
2003-02-14 04:48:06 +00:00
|
|
|
getrusage(0, &before);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
/* Digest blocks */
|
|
|
|
MD5Init(&context);
|
|
|
|
for (i = 0; i < TEST_BLOCK_COUNT; i++)
|
|
|
|
MD5Update(&context, block, TEST_BLOCK_LEN);
|
1995-07-12 09:14:46 +00:00
|
|
|
p = MD5End(&context,buf);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
/* Stop timer */
|
2003-02-14 04:48:06 +00:00
|
|
|
getrusage(0, &after);
|
|
|
|
timersub(&after.ru_utime, &before.ru_utime, &total);
|
|
|
|
seconds = total.tv_sec + (float) total.tv_usec / 1000000;
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
printf(" done\n");
|
|
|
|
printf("Digest = %s", p);
|
2003-02-14 04:48:06 +00:00
|
|
|
printf("\nTime = %f seconds\n", seconds);
|
1995-02-20 00:48:50 +00:00
|
|
|
printf
|
2003-02-14 04:48:06 +00:00
|
|
|
("Speed = %f bytes/second\n",
|
|
|
|
(float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests a reference suite of strings and prints the results.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
2003-01-26 08:23:37 +00:00
|
|
|
|
|
|
|
#define MD5TESTCOUNT 8
|
|
|
|
|
|
|
|
char *MDTestInput[] = {
|
|
|
|
"",
|
|
|
|
"a",
|
|
|
|
"abc",
|
|
|
|
"message digest",
|
|
|
|
"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
|
|
|
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
|
|
|
|
"MD5 has not yet (2001-09-03) been broken, but sufficient attacks have been made that its security is in some doubt"
|
|
|
|
};
|
|
|
|
|
|
|
|
char *MDTestOutput[MD5TESTCOUNT] = {
|
|
|
|
"d41d8cd98f00b204e9800998ecf8427e",
|
|
|
|
"0cc175b9c0f1b6a831c399e269772661",
|
|
|
|
"900150983cd24fb0d6963f7d28e17f72",
|
|
|
|
"f96b697d7cb7938d525a2f31aaf161d0",
|
|
|
|
"c3fcd3d76192e4007dfb496cca67e13b",
|
|
|
|
"d174ab98d277d9f5a5611c2c9f419d9f",
|
|
|
|
"57edf4a22be3c955ac49da2e2107b67a",
|
|
|
|
"b50663f41d44d92171cb9976bc118538"
|
|
|
|
};
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
MDTestSuite(void)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
2003-01-26 08:23:37 +00:00
|
|
|
int i;
|
|
|
|
char buffer[33];
|
1997-12-29 14:05:49 +00:00
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
printf("MD5 test suite:\n");
|
2003-01-26 08:23:37 +00:00
|
|
|
for (i = 0; i < MD5TESTCOUNT; i++) {
|
|
|
|
MD5Data(MDTestInput[i], strlen(MDTestInput[i]), buffer);
|
|
|
|
printf("MD5 (\"%s\") = %s", MDTestInput[i], buffer);
|
|
|
|
if (strcmp(buffer, MDTestOutput[i]) == 0)
|
|
|
|
printf(" - verified correct\n");
|
|
|
|
else
|
|
|
|
printf(" - INCORRECT RESULT!\n");
|
|
|
|
}
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests the standard input and prints the result.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
MDFilter(int tee)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5_CTX context;
|
2001-05-22 10:33:43 +00:00
|
|
|
unsigned int len;
|
1997-12-29 03:40:37 +00:00
|
|
|
unsigned char buffer[BUFSIZ];
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
MD5Init(&context);
|
1997-12-29 03:40:37 +00:00
|
|
|
while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
|
2001-05-22 10:33:43 +00:00
|
|
|
if (tee && len != fwrite(buffer, 1, len, stdout))
|
1998-07-06 07:04:50 +00:00
|
|
|
err(1, "stdout");
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5Update(&context, buffer, len);
|
1995-02-26 01:55:31 +00:00
|
|
|
}
|
1995-07-12 09:14:46 +00:00
|
|
|
printf("%s\n", MD5End(&context,buf));
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1997-12-29 03:40:37 +00:00
|
|
|
|
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
usage(void)
|
1997-12-29 03:40:37 +00:00
|
|
|
{
|
1997-12-29 14:05:49 +00:00
|
|
|
|
2000-11-08 20:41:35 +00:00
|
|
|
fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");
|
1997-12-29 03:40:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|