Add MD2, MD4, SHA1, and RMD160 support to cksum. Adding new hashes to

cksum(1) should now be easy.
This commit is contained in:
atatat 2001-03-20 18:46:25 +00:00
parent df2b9ae2bd
commit 61e4caef25
9 changed files with 383 additions and 81 deletions

View File

@ -1,11 +1,19 @@
# $NetBSD: Makefile,v 1.9 1997/10/18 12:49:08 lukem Exp $
# $NetBSD: Makefile,v 1.10 2001/03/20 18:46:25 atatat Exp $
# @(#)Makefile 8.2 (Berkeley) 4/28/95
PROG= cksum
SRCS= cksum.c crc.c md5.c print.c sum1.c sum2.c
SRCS= cksum.c crc.c md2.c md4.c md5.c sha1.c rmd160.c print.c sum1.c sum2.c
LINKS= ${BINDIR}/cksum ${BINDIR}/sum
LINKS+= ${BINDIR}/cksum ${BINDIR}/md2
LINKS+= ${BINDIR}/cksum ${BINDIR}/md4
LINKS+= ${BINDIR}/cksum ${BINDIR}/md5
LINKS+= ${BINDIR}/cksum ${BINDIR}/sha1
LINKS+= ${BINDIR}/cksum ${BINDIR}/rmd160
MLINKS= cksum.1 sum.1
MLINKS+=cksum.1 md2.1
MLINKS+=cksum.1 md4.1
MLINKS+=cksum.1 md5.1
MLINKS+=cksum.1 sha1.1
MLINKS+=cksum.1 rmd160.1
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
.\" $NetBSD: cksum.1,v 1.14 2000/07/03 05:27:15 enami Exp $
.\" $NetBSD: cksum.1,v 1.15 2001/03/20 18:46:25 atatat Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -41,24 +41,57 @@
.Os
.Sh NAME
.Nm cksum ,
.Nm md2 ,
.Nm md4 ,
.Nm md5 ,
.Nm sha1 ,
.Nm rmd160 ,
.Nm sum
.Nd display file checksums and block counts
.Sh SYNOPSIS
.Nm
.Oo
.Fl m |
.Fl 1 |
.Fl 2 |
.Fl 4 |
.Fl 5 |
.Fl 6 |
.Op Fl o Ar 1 | Ar 2
.Oc
.Op Ar
.Nm sum
.Op Ar
.Nm md2
.Op Fl p
.Op Fl t
.Op Fl x
.Op Fl s Ar string
.Op Ar
.Nm md4
.Op Fl p
.Op Fl t
.Op Fl x
.Op Fl s Ar string
.Op Ar
.Nm md5
.Op Fl p
.Op Fl t
.Op Fl x
.Op Fl s Ar string
.Op Ar
.Nm sha1
.Op Fl p
.Op Fl t
.Op Fl x
.Op Fl s Ar string
.Op Ar
.Nm rmd160
.Op Fl p
.Op Fl t
.Op Fl x
.Op Fl s Ar string
.Op Ar
.Sh DESCRIPTION
The
.Nm
@ -95,10 +128,33 @@ in a secure manner before being encrypted with a private (secret)
key under a public-key encryption system such as
.Pa RSA .
.Pp
The
.Nm md2
and
.Nm md4
utilities behave in exactly the same manner as
.Nm md5
but use a different algorithms.
.Pp
The
.Nm sha1
and
.Nm rmd160
utilities also produce message digests, however the output from these
two programs is 160 bits in length, as opposed to 128.
.Pp
The options are as follows:
.Bl -tag -width indent
.It Fl m
.It Fl 1
Use the SHA1 algorithm rather than the default one.
.It Fl 2
Use the MD2 algorithm rather than the default one.
.It Fl 4
Use the MD4 algorithm rather than the default one.
.It Fl m | 5
Use the MD5 algorithm rather than the default one.
.It Fl 6
Use the RMD160 algorithm rather than the default one.
.It Fl o
Use historic algorithms instead of the (superior) default one.
.Pp
@ -137,17 +193,23 @@ for algorithm 2.
Partial blocks are rounded up.
.El
.Pp
The following options apply only when using the MD5 algorithm:
The following options apply only when using the one of the message
digest algorithms:
.Bl -tag -width indent
.It Fl s Ar string
Print the MD5 checksum of the given string
Print the hash of the given string
.Ar string .
.It Fl p
Echo input from standard input to standard output, and append an MD5 checksum.
Echo input from standard input to standard output, and append the
selected message digest.
.It Fl t
Run a built-in MD5 time trial.
Run a built-in message digest time trial.
.It Fl x
Run a built-in MD5 test script.
Run a built-in message digest test script. The tests that are run
are supposed to encompass all the various tests in the suites that
accompany the algorithms' descriptions with the exception of the the
last test for the SHA-1 algorithm and the RIPEMD-160 algorithm. The
last test for these is one million copies of the lower letter a.
.El
.Pp
The default
@ -210,6 +272,26 @@ article.
.%J "Communications of the \\*(tNACM\\*(sP"
.%D "August 1988"
.Re
.Rs
.%A R. Rivest
.%T The MD2 Message-Digest Algorithm
.%O RFC 1319
.Re
.Rs
.%A R. Rivest
.%T The MD4 Message-Digest Algorithm
.%O RFC 1186 and RFC 1320
.Re
.Rs
.%A R. Rivest
.%T The MD5 Message-Digest Algorithm
.%O RFC 1321
.Re
.Rs
.%A U.S. DOC/NIST
.%T Secure Hash Standard
.%O FIPS PUB 180-1
.Re
.Sh STANDARDS
The
.Nm
@ -220,6 +302,14 @@ The
.Nm
utility appeared in
.Bx 4.4 .
The functionality for
.Nm md2 ,
.Nm md4 ,
.Nm sha1 ,
and
.Nm rmd160
was added in
.Nx 1.6 .
.\" .Pp
.\" The
.\" .Nm sum

View File

@ -1,4 +1,4 @@
/* $NetBSD: cksum.c,v 1.12 2001/02/19 23:03:45 cgd Exp $ */
/* $NetBSD: cksum.c,v 1.13 2001/03/20 18:46:25 atatat Exp $ */
/*-
* Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
@ -47,7 +47,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
#if 0
static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95";
#endif
__RCSID("$NetBSD: cksum.c,v 1.12 2001/02/19 23:03:45 cgd Exp $");
__RCSID("$NetBSD: cksum.c,v 1.13 2001/03/20 18:46:25 atatat Exp $");
#endif /* not lint */
#include <sys/cdefs.h>
@ -58,6 +58,10 @@ __RCSID("$NetBSD: cksum.c,v 1.12 2001/02/19 23:03:45 cgd Exp $");
#include <fcntl.h>
#include <locale.h>
#include <md5.h>
#include <md4.h>
#include <md2.h>
#include <sha1.h>
#include <rmd160.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -65,9 +69,44 @@ __RCSID("$NetBSD: cksum.c,v 1.12 2001/02/19 23:03:45 cgd Exp $");
#include "extern.h"
#define HASH_MD2 0
#define HASH_MD4 1
#define HASH_MD5 2
#define HASH_SHA1 3
#define HASH_RMD160 4
typedef char *(*_filefunc)(const char *, char *);
struct hash {
const char *progname;
const char *hashname;
void (*stringfunc)(const char *);
void (*timetrialfunc)(void);
void (*testsuitefunc)(void);
void (*filterfunc)(int);
char *(*filefunc)(const char *, char *);
} hashes[] = {
{ "md2", "MD2",
MD2String, MD2TimeTrial, MD2TestSuite,
MD2Filter, MD2File },
{ "md4", "MD4",
MD4String, MD4TimeTrial, MD4TestSuite,
MD4Filter, MD4File },
{ "md5", "MD5",
MD5String, MD5TimeTrial, MD5TestSuite,
MD5Filter, MD5File },
{ "sha1", "SHA1",
SHA1String, SHA1TimeTrial, SHA1TestSuite,
SHA1Filter, (_filefunc) SHA1File },
{ "rmd160", "RMD160",
RMD160String, RMD160TimeTrial, RMD160TestSuite,
RMD160Filter, (_filefunc) RMD160File },
{ NULL }
};
int main __P((int, char **));
int md5_digest_file __P((char *));
void requiremd5 __P((const char *));
int hash_digest_file __P((char *, struct hash *));
void requirehash __P((const char *));
void usage __P((void));
int
@ -75,41 +114,81 @@ main(argc, argv)
int argc;
char **argv;
{
register int ch, fd, rval, domd5, dosum, pflag, nomd5stdin;
register int ch, fd, rval, dosum, pflag, nohashstdin;
u_int32_t len, val;
char *fn;
const char *progname;
int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
void (*pfncn) __P((char *, u_int32_t, u_int32_t));
struct hash *hash;
cfncn = NULL;
pfncn = NULL;
dosum = domd5 = pflag = nomd5stdin = 0;
dosum = pflag = nohashstdin = 0;
setlocale(LC_ALL, "");
if (!strcmp(getprogname(), "md5"))
domd5 = 1;
else if (!strcmp(getprogname(), "sum")) {
dosum = 1;
cfncn = csum1;
pfncn = psum1;
} else {
cfncn = crc;
pfncn = pcrc;
progname = getprogname();
for (hash = hashes; hash->hashname != NULL; hash++)
if (strcmp(progname, hash->progname) == 0)
break;
if (hash->hashname == NULL) {
hash = NULL;
if (!strcmp(progname, "sum")) {
dosum = 1;
cfncn = csum1;
pfncn = psum1;
} else {
cfncn = crc;
pfncn = pcrc;
}
}
while ((ch = getopt(argc, argv, "mo:ps:tx")) != -1)
while ((ch = getopt(argc, argv, "mo:ps:tx12456")) != -1)
switch(ch) {
case '2':
if (dosum) {
warnx("sum mutually exclusive with md2");
usage();
}
hash = &hashes[HASH_MD2];
break;
case '4':
if (dosum) {
warnx("sum mutually exclusive with md4");
usage();
}
hash = &hashes[HASH_MD4];
break;
case 'm':
case '5':
if (dosum) {
warnx("sum mutually exclusive with md5");
usage();
}
domd5 = 1;
hash = &hashes[HASH_MD5];
break;
case '1':
if (dosum) {
warnx("sum mutually exclusive with sha1");
usage();
}
hash = &hashes[HASH_SHA1];
break;
case '6':
if (dosum) {
warnx("sum mutually exclusive with rmd160");
usage();
}
hash = &hashes[HASH_RMD160];
break;
case 'o':
if (domd5) {
warnx("md5 mutually exclusive with sum");
if (hash) {
warnx("%s mutually exclusive with sum",
hash->hashname);
usage();
}
if (!strcmp(optarg, "1")) {
@ -124,27 +203,27 @@ main(argc, argv)
}
break;
case 'p':
if (!domd5)
requiremd5("-p");
if (hash == NULL)
requirehash("-p");
pflag = 1;
break;
case 's':
if (!domd5)
requiremd5("-s");
nomd5stdin = 1;
MDString(optarg);
if (hash == NULL)
requirehash("-s");
nohashstdin = 1;
hash->stringfunc(optarg);
break;
case 't':
if (!domd5)
requiremd5("-t");
MDTimeTrial();
nomd5stdin = 1;
if (hash == NULL)
requirehash("-t");
nohashstdin = 1;
hash->timetrialfunc();
break;
case 'x':
if (!domd5)
requiremd5("-x");
MDTestSuite();
nomd5stdin = 1;
if (hash == NULL)
requirehash("-x");
nohashstdin = 1;
hash->testsuitefunc();
break;
case '?':
default:
@ -159,8 +238,8 @@ main(argc, argv)
do {
if (*argv) {
fn = *argv++;
if (domd5) {
if (md5_digest_file(fn)) {
if (hash != NULL) {
if (hash_digest_file(fn, hash)) {
warn("%s", fn);
rval = 1;
}
@ -171,10 +250,11 @@ main(argc, argv)
rval = 1;
continue;
}
} else if (domd5 && !nomd5stdin)
MDFilter(pflag);
} else if (hash && !nohashstdin) {
hash->filterfunc(pflag);
}
if (!domd5) {
if (hash == NULL) {
if (cfncn(fd, &val, &len)) {
warn("%s", fn ? fn : "stdin");
rval = 1;
@ -187,24 +267,26 @@ main(argc, argv)
}
int
md5_digest_file(fn)
hash_digest_file(fn, hash)
char *fn;
struct hash *hash;
{
char buf[33], *cp;
char buf[41], *cp;
cp = MD5File(fn, buf);
cp = hash->filefunc(fn, buf);
if (cp == NULL)
return (1);
printf("MD5 (%s) = %s\n", fn, cp);
printf("%s (%s) = %s\n", hash->hashname, fn, cp);
return (0);
}
void
requiremd5(flg)
requirehash(flg)
const char *flg;
{
warnx("%s flag requires `md5' or -m", flg);
warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
flg);
usage();
}
@ -214,7 +296,15 @@ usage()
(void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
(void)fprintf(stderr, " sum [file ...]\n");
(void)fprintf(stderr,
" md2 [-p | -t | -x | -s string] [file ...]\n");
(void)fprintf(stderr,
" md4 [-p | -t | -x | -s string] [file ...]\n");
(void)fprintf(stderr,
" md5 [-p | -t | -x | -s string] [file ...]\n");
(void)fprintf(stderr,
" sha1 [-p | -t | -x | -s string] [file ...]\n");
(void)fprintf(stderr,
" rmd160 [-p | -t | -x | -s string] [file ...]\n");
exit(1);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.6 1997/01/30 01:10:35 thorpej Exp $ */
/* $NetBSD: extern.h,v 1.7 2001/03/20 18:46:26 atatat Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -46,8 +46,28 @@ int csum1 __P((int, u_int32_t *, u_int32_t *));
int csum2 __P((int, u_int32_t *, u_int32_t *));
int md5 __P((int, u_int32_t *, u_int32_t *));
void MDString __P((const char *));
void MDTimeTrial __P((void));
void MDTestSuite __P((void));
void MDFilter __P((int));
void MD2String __P((const char *));
void MD2TimeTrial __P((void));
void MD2TestSuite __P((void));
void MD2Filter __P((int));
void MD4String __P((const char *));
void MD4TimeTrial __P((void));
void MD4TestSuite __P((void));
void MD4Filter __P((int));
void MD5String __P((const char *));
void MD5TimeTrial __P((void));
void MD5TestSuite __P((void));
void MD5Filter __P((int));
void SHA1String __P((const char *));
void SHA1TimeTrial __P((void));
void SHA1TestSuite __P((void));
void SHA1Filter __P((int));
void RMD160String __P((const char *));
void RMD160TimeTrial __P((void));
void RMD160TestSuite __P((void));
void RMD160Filter __P((int));
__END_DECLS

21
usr.bin/cksum/md2.c Normal file
View File

@ -0,0 +1,21 @@
/* $NetBSD: md2.c,v 1.1 2001/03/20 18:46:26 atatat Exp $ */
#include <md2.h> /* this hash type */
#include <md5.h> /* the hash we're replacing */
#define HASHTYPE "MD2"
#define HASHLEN 32
#define MD5Filter MD2Filter
#define MD5String MD2String
#define MD5TestSuite MD2TestSuite
#define MD5TimeTrial MD2TimeTrial
#define MD5Data MD2Data
#define MD5Init MD2Init
#define MD5Update MD2Update
#define MD5End MD2End
#define MD5_CTX MD2_CTX
#include "md5.c"

21
usr.bin/cksum/md4.c Normal file
View File

@ -0,0 +1,21 @@
/* $NetBSD: md4.c,v 1.1 2001/03/20 18:46:26 atatat Exp $ */
#include <md4.h> /* this hash type */
#include <md5.h> /* the hash we're replacing */
#define HASHTYPE "MD4"
#define HASHLEN 32
#define MD5Filter MD4Filter
#define MD5String MD4String
#define MD5TestSuite MD4TestSuite
#define MD5TimeTrial MD4TimeTrial
#define MD5Data MD4Data
#define MD5Init MD4Init
#define MD5Update MD4Update
#define MD5End MD4End
#define MD5_CTX MD4_CTX
#include "md5.c"

View File

@ -1,4 +1,4 @@
/* $NetBSD: md5.c,v 1.2 1997/10/17 11:37:09 lukem Exp $ */
/* $NetBSD: md5.c,v 1.3 2001/03/20 18:46:26 atatat Exp $ */
/*
* MDDRIVER.C - test driver for MD2, MD4 and MD5
@ -19,7 +19,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: md5.c,v 1.2 1997/10/17 11:37:09 lukem Exp $");
__RCSID("$NetBSD: md5.c,v 1.3 2001/03/20 18:46:26 atatat Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -30,10 +30,18 @@ __RCSID("$NetBSD: md5.c,v 1.2 1997/10/17 11:37:09 lukem Exp $");
#include <string.h>
#include <time.h>
void MDFilter __P((int));
void MDString __P((const char *));
void MDTestSuite __P((void));
void MDTimeTrial __P((void));
void MD5Filter __P((int));
void MD5String __P((const char *));
void MD5TestSuite __P((void));
void MD5TimeTrial __P((void));
#ifndef HASHTYPE
#define HASHTYPE "MD5"
#endif
#ifndef HASHLEN
#define HASHLEN 32
#endif
/*
* Length of test block, number of test blocks.
@ -45,28 +53,29 @@ void MDTimeTrial __P((void));
* Digests a string and prints the result.
*/
void
MDString(string)
MD5String(string)
const char *string;
{
unsigned int len = strlen(string);
char buf[33];
char buf[HASHLEN + 1];
printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
printf("%s (\"%s\") = %s\n", HASHTYPE, string,
MD5Data(string, len, buf));
}
/*
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
*/
void
MDTimeTrial()
MD5TimeTrial()
{
MD5_CTX context;
time_t endTime, startTime;
unsigned char block[TEST_BLOCK_LEN];
unsigned int i;
char *p, buf[33];
char *p, buf[HASHLEN + 1];
printf("MD5 time trial. Digesting %d %d-byte blocks ...",
printf("%s time trial. Digesting %d %d-byte blocks ...", HASHTYPE,
TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
fflush(stdout);
@ -103,18 +112,19 @@ MDTimeTrial()
* Digests a reference suite of strings and prints the results.
*/
void
MDTestSuite()
MD5TestSuite()
{
printf("MD5 test suite:\n");
printf("%s test suite:\n", HASHTYPE);
MDString("");
MDString("a");
MDString("abc");
MDString("message digest");
MDString("abcdefghijklmnopqrstuvwxyz");
MDString
MD5String("");
MD5String("a");
MD5String("abc");
MD5String("message digest");
MD5String("abcdefghijklmnopqrstuvwxyz");
MD5String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
MD5String
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
MDString
MD5String
("1234567890123456789012345678901234567890\
1234567890123456789012345678901234567890");
}
@ -123,13 +133,13 @@ MDTestSuite()
* Digests the standard input and prints the result.
*/
void
MDFilter(pipe)
MD5Filter(pipe)
int pipe;
{
MD5_CTX context;
int len;
unsigned char buffer[BUFSIZ];
char buf[33];
char buf[HASHLEN + 1];
MD5Init(&context);
while ((len = fread(buffer, 1, BUFSIZ, stdin)) > 0) {

21
usr.bin/cksum/rmd160.c Normal file
View File

@ -0,0 +1,21 @@
/* $NetBSD: rmd160.c,v 1.1 2001/03/20 18:46:26 atatat Exp $ */
#include <rmd160.h> /* this hash type */
#include <md5.h> /* the hash we're replacing */
#define HASHTYPE "RMD160"
#define HASHLEN 40
#define MD5Filter RMD160Filter
#define MD5String RMD160String
#define MD5TestSuite RMD160TestSuite
#define MD5TimeTrial RMD160TimeTrial
#define MD5Data RMD160Data
#define MD5Init RMD160Init
#define MD5Update RMD160Update
#define MD5End RMD160End
#define MD5_CTX RMD160_CTX
#include "md5.c"

21
usr.bin/cksum/sha1.c Normal file
View File

@ -0,0 +1,21 @@
/* $NetBSD: sha1.c,v 1.1 2001/03/20 18:46:27 atatat Exp $ */
#include <sha1.h> /* this hash type */
#include <md5.h> /* the hash we're replacing */
#define HASHTYPE "SHA1"
#define HASHLEN 40
#define MD5Filter SHA1Filter
#define MD5String SHA1String
#define MD5TestSuite SHA1TestSuite
#define MD5TimeTrial SHA1TimeTrial
#define MD5Data SHA1Data
#define MD5Init SHA1Init
#define MD5Update SHA1Update
#define MD5End SHA1End
#define MD5_CTX SHA1_CTX
#include "md5.c"