Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 29 Apr 2001 14:37:14 -0700
From:      Dima Dorfman <dima@unixfreak.org>
To:        audit@freebsd.org
Subject:   sha1 support in md5(1)
Message-ID:  <20010429213715.06D613E0B@bazooka.unixfreak.org>

next in thread | raw e-mail | index | archive | help
Attached is a patch that adds SHA1 support to the md5(1) program.
Please review.  This patch makes sha1(1) a link to md5(1); the same
program was chosen because much of the support code would be
identical.  This is also the way OpenBSD implemented it, and it makes
sense, as well as making it easier to add other digest algorithms in
the future (OpenBSD has rmd160, for example).

Thanks,

					Dima Dorfman
					dima@unixfreak.org

Index: Makefile
===================================================================
RCS file: /st/src/FreeBSD/src/sbin/md5/Makefile,v
retrieving revision 1.4
diff -u -r1.4 Makefile
--- Makefile	2001/03/26 14:33:10	1.4
+++ Makefile	2001/04/29 21:27:14
@@ -5,5 +5,6 @@
 
 LDADD+=	-lmd
 DPADD+= ${LIBMD}
+LINKS=	${BINDIR}/md5 ${BINDIR}/sha1
 
 .include <bsd.prog.mk>
Index: md5.c
===================================================================
RCS file: /st/src/FreeBSD/src/sbin/md5/md5.c,v
retrieving revision 1.21
diff -u -r1.21 md5.c
--- md5.c	2000/11/08 20:41:35	1.21
+++ md5.c	2001/04/29 21:27:14
@@ -28,25 +28,45 @@
 #include <stdio.h>
 #include <time.h>
 #include <unistd.h>
+#include <sha.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "global.h"
 
+extern const char *__progname;		/*XXX*/
+
 /*
  * Length of test block, number of test blocks.
  */
 #define TEST_BLOCK_LEN 10000
 #define TEST_BLOCK_COUNT 100000
 
+/* Maximum length of digest. */
+#define	MAX_DIGEST_LEN	41
+
 int qflag;
 int rflag;
 
+static void MDSetAlgo PROTO_LIST((char *));
 static void MDString PROTO_LIST((char *));
 static void MDTimeTrial PROTO_LIST((void));
 static void MDTestSuite PROTO_LIST((void));
 static void MDFilter PROTO_LIST((int));
 static void usage PROTO_LIST((void));
 
+/*
+ * Global pointers to procedures/info to/of the "real" message digest
+ * algorithm.
+ */
+void (*MDInit)();
+void (*MDUpdate)();
+char *(*MDEnd)();
+char *(*MDFile)();
+char *(*MDData)();
+char *MDName;
+void *MDContext;
+
 /* Main driver.
 
 Arguments (may be any combination):
@@ -63,7 +83,9 @@
 {
 	int     ch;
 	char   *p;
-	char	buf[33];
+	char	buf[MAX_DIGEST_LEN];
+
+	MDSetAlgo(__progname);
 
 	if (argc > 1) {
 		while ((ch = getopt(argc, argv, "ps:qrtx")) != -1) {
@@ -91,7 +113,7 @@
 			}
 		}
 		while (optind < argc) {
-			p = MD5File(argv[optind], buf);
+			p = MDFile(argv[optind], buf);
 			if (!p)
 				warn("%s", argv[optind]);
 			else
@@ -100,8 +122,8 @@
 				else if (rflag)
 					printf("%s %s\n", p, argv[optind]);
 				else
-					printf("MD5 (%s) = %s\n", argv[optind],
-					    p);
+					printf("%s (%s) = %s\n", MDName,
+					    argv[optind], p);
 			optind++;
 		}
 	} else
@@ -109,7 +131,36 @@
 
 	return (0);
 }
+
 /*
+ * Set the algorithm based on the argument.
+ */
+static void
+MDSetAlgo(name)
+	char   *name;
+{
+	if (strcmp(name, "sha1") == 0) {
+		MDName = "SHA1";
+		MDInit = SHA1_Init;
+		MDUpdate = SHA1_Update;
+		MDEnd = SHA1_End;
+		MDFile = SHA1_File;
+		MDData = SHA1_Data;
+		MDContext = malloc(sizeof(SHA1_CTX));
+	} else {
+		MDName = "MD5";
+		MDInit = MD5Init;
+		MDUpdate = MD5Update;
+		MDEnd = MD5End;
+		MDFile = MD5File;
+		MDData = MD5Data;
+		MDContext = malloc(sizeof(MD5_CTX));
+	}
+	if (MDContext == NULL)
+		err(1, "malloc");
+}
+
+/*
  * Digests a string and prints the result.
  */
 static void
@@ -117,14 +168,15 @@
 	char   *string;
 {
 	size_t len = strlen(string);
-	char buf[33];
+	char buf[MAX_DIGEST_LEN];
 
 	if (qflag)
-		printf("%s\n", MD5Data(string, len, buf));
+		printf("%s\n", MDData(string, len, buf));
 	else if (rflag)
-		printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
+		printf("%s \"%s\"\n", MDData(string, len, buf), string);
 	else
-		printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
+		printf("%s (\"%s\") = %s\n", MDName, string,
+		    MDData(string, len, buf));
 }
 /*
  * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
@@ -132,14 +184,13 @@
 static void
 MDTimeTrial()
 {
-	MD5_CTX context;
 	time_t  endTime, startTime;
 	unsigned char block[TEST_BLOCK_LEN];
 	unsigned int i;
-	char   *p, buf[33];
+	char   *p, buf[MAX_DIGEST_LEN];
 
 	printf
-	    ("MD5 time trial. Digesting %d %d-byte blocks ...",
+	    ("%s time trial. Digesting %d %d-byte blocks ...", MDName,
 	    TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
 	fflush(stdout);
 
@@ -151,10 +202,10 @@
 	time(&startTime);
 
 	/* Digest blocks */
-	MD5Init(&context);
+	MDInit(MDContext);
 	for (i = 0; i < TEST_BLOCK_COUNT; i++)
-		MD5Update(&context, block, TEST_BLOCK_LEN);
-	p = MD5End(&context,buf);
+		MDUpdate(MDContext, block, TEST_BLOCK_LEN);
+	p = MD5End(MDContext ,buf);
 
 	/* Stop timer */
 	time(&endTime);
@@ -175,7 +226,7 @@
 MDTestSuite()
 {
 
-	printf("MD5 test suite:\n");
+	printf("%s test suite:\n", MDName);
 
 	MDString("");
 	MDString("a");
@@ -196,24 +247,23 @@
 MDFilter(pipe)
 	int pipe;
 {
-	MD5_CTX context;
 	int     len;
 	unsigned char buffer[BUFSIZ];
-	char buf[33];
+	char buf[MAX_DIGEST_LEN];
 
-	MD5Init(&context);
+	MDInit(MDContext);
 	while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
 		if(pipe && (len != fwrite(buffer, 1, len, stdout)))
 			err(1, "stdout");
-		MD5Update(&context, buffer, len);
+		MDUpdate(MDContext, buffer, len);
 	}
-	printf("%s\n", MD5End(&context,buf));
+	printf("%s\n", MDEnd(MDContext,buf));
 }
 
 static void
 usage()
 {
-
-	fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");
+	fprintf(stderr, "usage: %s [-pqrtx] [-s string] [files ...]\n",
+	    __progname);
 	exit(1);
 }

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010429213715.06D613E0B>