From e164509318d283c0f85408fd75013b6563015b17 Mon Sep 17 00:00:00 2001 From: Paul Traina Date: Sat, 29 Oct 1994 23:58:02 +0000 Subject: Incorproate version of md5 placed into public domain by RSA. --- sbin/md5/Makefile | 9 +++ sbin/md5/global.h | 30 +++++++ sbin/md5/md5.1 | 43 ++++++++++ sbin/md5/md5.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 313 insertions(+) create mode 100644 sbin/md5/Makefile create mode 100644 sbin/md5/global.h create mode 100644 sbin/md5/md5.1 create mode 100644 sbin/md5/md5.c (limited to 'sbin/md5') diff --git a/sbin/md5/Makefile b/sbin/md5/Makefile new file mode 100644 index 000000000000..8c53af2fa1a5 --- /dev/null +++ b/sbin/md5/Makefile @@ -0,0 +1,9 @@ +# @(#)Makefile 8.1 (Berkeley) 6/9/93 + +PROG= md5 +SRCS= md5.c + +LDADD+= -lmd +DPADD+= /usr/lib/libmd.a + +.include diff --git a/sbin/md5/global.h b/sbin/md5/global.h new file mode 100644 index 000000000000..ee255142f5dc --- /dev/null +++ b/sbin/md5/global.h @@ -0,0 +1,30 @@ +/* GLOBAL.H - RSAREF types and constants + */ + +/* PROTOTYPES should be set to one if and only if the compiler supports + function argument prototyping. +The following makes PROTOTYPES default to 0 if it has not already + been defined with C compiler flags. + */ +#ifndef PROTOTYPES +#define PROTOTYPES 0 +#endif + +/* POINTER defines a generic pointer type */ +typedef unsigned char *POINTER; + +/* UINT2 defines a two byte word */ +typedef unsigned short int UINT2; + +/* UINT4 defines a four byte word */ +typedef unsigned long int UINT4; + +/* PROTO_LIST is defined depending on how PROTOTYPES is defined above. +If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it + returns an empty list. + */ +#if PROTOTYPES +#define PROTO_LIST(list) list +#else +#define PROTO_LIST(list) () +#endif diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1 new file mode 100644 index 000000000000..ccccb01f7e78 --- /dev/null +++ b/sbin/md5/md5.1 @@ -0,0 +1,43 @@ +.TH MD5 1 "Feb 14, 1994" +.SH NAME +md5 \- calculate a message-digest fingerprint (checksum) for a file +.SH SYNOPSIS +.B md5 +[ -t | -x | -sstring | filename(s) ] +.SH DESCRIPTION +.B md5 +takes as input a message of arbitrary length and produces +as output a 128-bit "fingerprint" or "message digest" of the input. +It is conjectured that it is computationally infeasible to produce +two messages having the same message digest, or to produce any +message having a given prespecified target message digest. +The MD5 algorithm is intended for digital signature applications, where a +large file must be "compressed" in a secure manner before being +encrypted with a private (secret) key under a public-key cryptosystem +such as +.I RSA. +.SH OPTIONS +The following four options may be used in any combination, except +that +.B "filename(s)" +must be the last objects on the command line. +.in +5 +.PP +.B -sstring +prints a checksum of the given "string". +.PP +.B -t +runs a built-in time trial. +.PP +.B -x +runs a built-in test script. +.PP +.B filename(s) +prints a checksum(s) for each of the files. +.SH "SEE ALSO" +.BR sum (1) +.PP +RFC 1321 describes in detail the MD2, MD4, and MD5 message-digest algorithms. +.SH ACKNOWLEDGEMENTS +This program is placed in the public domain for free general use by +RSA Data Security. diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c new file mode 100644 index 000000000000..d79ac5888568 --- /dev/null +++ b/sbin/md5/md5.c @@ -0,0 +1,231 @@ +/* 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. + */ + +/* The following makes MD default to MD5 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 == 2 +#include +#endif +#if MD == 4 +#include +#endif +#if MD == 5 +#include +#endif + +/* Length of test block, number of test blocks. + */ +#define TEST_BLOCK_LEN 1000 +#define TEST_BLOCK_COUNT 1000 + +static void MDString PROTO_LIST ((char *)); +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])); + +#if MD == 2 +#define MD_CTX MD2_CTX +#define MDInit MD2Init +#define MDUpdate MD2Update +#define MDFinal MD2Final +#endif +#if MD == 4 +#define MD_CTX MD4_CTX +#define MDInit MD4Init +#define MDUpdate MD4Update +#define MDFinal MD4Final +#endif +#if MD == 5 +#define MD_CTX MD5_CTX +#define MDInit MD5Init +#define MDUpdate MD5Update +#define MDFinal MD5Final +#endif + +/* 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 + */ +int main (argc, argv) +int argc; +char *argv[]; +{ + int i; + + if (argc > 1) + for (i = 1; i < argc; i++) + if (argv[i][0] == '-' && argv[i][1] == 's') + MDString (argv[i] + 2); + 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) +char *string; +{ + MD_CTX context; + unsigned char digest[16]; + unsigned int len = strlen (string); + + MDInit (&context); + MDUpdate (&context, string, len); + MDFinal (digest, &context); + + printf ("MD%d (\"%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; + + printf + ("MD%d time trial. Digesting %d %d-byte blocks ...", MD, + TEST_BLOCK_LEN, TEST_BLOCK_COUNT); + + /* 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); + printf ("\nTime = %ld seconds\n", (long)(endTime-startTime)); + /* + * Be careful that endTime-startTime is not zero. + * (Bug fix from Ric Anderson, ric@Artisoft.COM.) + */ + printf + ("Speed = %ld bytes/second\n", + (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/((endTime-startTime) != 0 ? (endTime-startTime):1)); +} + +/* Digests a reference suite of strings and prints the results. + */ +static void MDTestSuite () +{ + printf ("MD%d test suite:\n", MD); + + MDString (""); + MDString ("a"); + MDString ("abc"); + MDString ("message digest"); + MDString ("abcdefghijklmnopqrstuvwxyz"); + MDString + ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + MDString + ("1234567890123456789012345678901234567890\ +1234567890123456789012345678901234567890"); +} + +/* 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]; + + 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]; + + MDInit (&context); + while (len = fread (buffer, 1, 16, stdin)) + MDUpdate (&context, buffer, len); + MDFinal (digest, &context); + + MDPrint (digest); + printf ("\n"); +} + +/* Prints a message digest in hexadecimal. + */ +static void MDPrint (digest) +unsigned char digest[16]; +{ + unsigned int i; + + for (i = 0; i < 16; i++) + printf ("%02x", digest[i]); +} -- cgit v1.2.3