summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1997-07-12 21:09:03 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1997-07-12 21:09:03 +0000
commit6103cfab79bee133e65cbb88bad41566950c0e60 (patch)
treed16bc8e0e85c2e7b69e029257178c5cd5d75979f
parent24b11c3b71fe0acc70f0a53e99ff0b6d3911b8ad (diff)
md5(1) is now capable of doing md4, md5 and sha1 digests. Currently
only md5 and sha1 are used.
-rw-r--r--bin/md5/Makefile8
-rw-r--r--bin/md5/global.h32
-rw-r--r--bin/md5/md5.c156
-rw-r--r--bin/md5/sha1.149
4 files changed, 154 insertions, 91 deletions
diff --git a/bin/md5/Makefile b/bin/md5/Makefile
index 46e59bf216c..14215dc68aa 100644
--- a/bin/md5/Makefile
+++ b/bin/md5/Makefile
@@ -1,8 +1,8 @@
-# $OpenBSD: Makefile,v 1.3 1996/11/24 02:26:00 niklas Exp $
+# $OpenBSD: Makefile,v 1.4 1997/07/12 21:09:01 millert Exp $
PROG= md5
-SRCS= md5.c
-COPTS+= -Wall -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Werror \
- -DPROTOTYPES
+MAN= md5.1 sha1.1
+LINKS= ${BINDIR}/md5 ${BINDIR}/sha1
+COPTS+= -Wall -Wconversion -Wmissing-prototypes -Werror
.include <bsd.prog.mk>
diff --git a/bin/md5/global.h b/bin/md5/global.h
deleted file mode 100644
index b3d8fd6ce24..00000000000
--- a/bin/md5/global.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/* $OpenBSD: global.h,v 1.1 1996/06/11 10:37:35 deraadt Exp $ */
-
-/* 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/bin/md5/md5.c b/bin/md5/md5.c
index 4ac4bb77962..ca30fe5fe53 100644
--- a/bin/md5/md5.c
+++ b/bin/md5/md5.c
@@ -1,11 +1,8 @@
/*
- * $OpenBSD: md5.c,v 1.4 1997/06/20 20:35:29 flipk Exp $
+ * $OpenBSD: md5.c,v 1.5 1997/07/12 21:09:02 millert Exp $
*
* Derived from:
- */
-
-/*
- * MDDRIVER.C - test driver for MD2, MD4 and MD5
+ * MDDRIVER.C - test driver for MD2, MD4 and MD5
*/
/*
@@ -21,11 +18,15 @@
* documentation and/or software.
*/
+#include <err.h>
#include <stdio.h>
+#include <stdlib.h>
#include <time.h>
#include <string.h>
-#include "global.h"
+
+#include <md4.h>
#include <md5.h>
+#include <sha1.h>
/*
* Length of test block, number of test blocks.
@@ -33,21 +34,33 @@
#define TEST_BLOCK_LEN 10000
#define TEST_BLOCK_COUNT 10000
-static void MDString PROTO_LIST((char *));
-static void MDTimeTrial PROTO_LIST((void));
-static void MDTestSuite PROTO_LIST((void));
-static void MDFilter PROTO_LIST((int));
+extern char *__progname;
-int main PROTO_LIST((int, char *[]));
+static void MDString __P((char *));
+static void MDTimeTrial __P((void *));
+static void MDTestSuite __P((void));
+static void MDFilter __P((int, void *));
-/* Main driver.
+int main __P((int, char *[]));
-Arguments (may be any combination):
- -sstring - digests string
- -t - runs time trial
- -x - runs test script
- filename - digests file
- (none) - digests standard input
+/*
+ * Globals for indirection...
+ */
+void (*MDInit)();
+void (*MDUpdate)();
+char * (*MDEnd)();
+char * (*MDFile)();
+char * (*MDData)();
+char *MDType;
+
+/* 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)
@@ -56,29 +69,61 @@ main(argc, argv)
{
int i;
char *p;
- char buf[33];
+ char buf[41];
+ void *context;
+
+ /* What were we called as? Default to md5 */
+ if (strcmp(__progname, "sha1") == 0) {
+ MDType = "SHA1";
+ MDInit = SHA1Init;
+ MDUpdate = SHA1Update;
+ MDEnd = SHA1End;
+ MDFile = SHA1File;
+ MDData = SHA1Data;
+ if ((context = malloc(sizeof(SHA1_CTX))) == NULL)
+ err(1, "malloc");
+ } else if (strcmp(__progname, "md4") == 0) {
+ MDType = "MD4";
+ MDInit = MD4Init;
+ MDUpdate = MD4Update;
+ MDEnd = MD4End;
+ MDFile = MD4File;
+ MDData = MD4Data;
+ if ((context = malloc(sizeof(MD4_CTX))) == NULL)
+ err(1, "malloc");
+ } else {
+ MDType = "MD5";
+ MDInit = MD5Init;
+ MDUpdate = MD5Update;
+ MDEnd = MD5End;
+ MDFile = MD5File;
+ MDData = MD5Data;
+ if ((context = malloc(sizeof(MD5_CTX))) == NULL)
+ err(1, "malloc");
+ }
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();
+ MDTimeTrial(context);
else if (strcmp(argv[i], "-p") == 0)
- MDFilter(1);
+ MDFilter(1, context);
else if (strcmp(argv[i], "-x") == 0)
MDTestSuite();
else {
- p = MD5File(argv[i],buf);
+ p = MDFile(argv[i], buf);
if (!p)
- perror(argv[i]);
+ warnx(argv[i]);
else
- printf("MD5 (%s) = %s\n", argv[i], p);
+ (void)printf("%s (%s) = %s\n", MDType,
+ argv[i], p);
}
else
- MDFilter(0);
+ MDFilter(0, context);
- return (0);
+ exit(0);
}
/*
* Digests a string and prints the result.
@@ -88,24 +133,24 @@ MDString(string)
char *string;
{
size_t len = strlen(string);
- char buf[33];
+ char buf[41];
- printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
+ (void)printf("%s (\"%s\") = %s\n", MDType, string,
+ MDData(string, len, buf));
}
/*
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
*/
static void
-MDTimeTrial()
+MDTimeTrial(context)
+ void *context;
{
- MD5_CTX context;
time_t endTime, startTime;
unsigned char block[TEST_BLOCK_LEN];
unsigned int i;
- char *p, buf[33];
+ char *p, buf[41];
- printf
- ("MD5 time trial. Digesting %d %d-byte blocks ...",
+ (void)printf("%s time trial. Digesting %d %d-byte blocks ...", MDType,
TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
fflush(stdout);
@@ -117,22 +162,23 @@ MDTimeTrial()
time(&startTime);
/* Digest blocks */
- MD5Init(&context);
+ MDInit(context);
for (i = 0; i < TEST_BLOCK_COUNT; i++)
- MD5Update(&context, block, (size_t)TEST_BLOCK_LEN);
- p = MD5End(&context,buf);
+ MDUpdate(context, block, (size_t)TEST_BLOCK_LEN);
+ p = MDEnd(context,buf);
/* Stop timer */
time(&endTime);
- printf(" done\n");
- printf("Digest = %s", p);
- 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));
+ (void)printf(" done\nDigest = %s", p);
+ (void)printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
+ /*
+ * Be careful that endTime-startTime is not zero.
+ * (Bug fix from Ric Anderson <ric@Artisoft.COM>)
+ */
+ (void)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.
@@ -140,7 +186,7 @@ MDTimeTrial()
static void
MDTestSuite()
{
- printf("MD5 test suite:\n");
+ (void)printf("%s test suite:\n", MDType);
MDString("");
MDString("a");
@@ -152,26 +198,26 @@ MDTestSuite()
MDString
("1234567890123456789012345678901234567890\
1234567890123456789012345678901234567890");
+ MDString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
}
/*
* Digests the standard input and prints the result.
*/
static void
-MDFilter(int pipe)
+MDFilter(pipe, context)
+ int pipe;
+ void *context;
{
- MD5_CTX context;
size_t len;
unsigned char buffer[BUFSIZ];
- char buf[33];
+ char buf[41];
- MD5Init(&context);
+ MDInit(context);
while ((len = fread(buffer, (size_t)1, (size_t)BUFSIZ, stdin)) > 0) {
- if(pipe && (len != fwrite(buffer, (size_t)1, len, stdout))) {
- perror("stdout");
- exit(1);
- }
- MD5Update(&context, buffer, len);
+ if (pipe && (len != fwrite(buffer, (size_t)1, len, stdout)))
+ err(1, "stdout");
+ MDUpdate(context, buffer, len);
}
- printf("%s\n", MD5End(&context,buf));
+ (void)printf("%s\n", MDEnd(context,buf));
}
diff --git a/bin/md5/sha1.1 b/bin/md5/sha1.1
new file mode 100644
index 00000000000..6a1d09142ac
--- /dev/null
+++ b/bin/md5/sha1.1
@@ -0,0 +1,49 @@
+.\" $OpenBSD: sha1.1,v 1.1 1997/07/12 21:09:02 millert Exp $
+.\"
+.TH SHA1 1 "Jul 12, 1997"
+.SH NAME
+sha1 \- calculate a message-digest fingerprint (checksum) for a file
+.SH SYNOPSIS
+.B sha1
+[ -p | -t | -x | -sstring | filename(s) ]
+.SH DESCRIPTION
+.B sha1
+takes as input a message of arbitrary length and produces
+as output a 160-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 SHA-1 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 -p
+echos stdin to stdout and appends the SHA-1 sum to stdout.
+.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 cksum (1)
+.BR md5 (1)
+.PP
+NIST FIPS PUB 180-1 describes the SHA-1 message-digest algorithm in detail.
+.SH ACKNOWLEDGEMENTS
+This program is placed in the public domain for free general use by
+RSA Data Security.