summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2004-05-02 17:53:29 +0000
committermillert <millert@openbsd.org>2004-05-02 17:53:29 +0000
commitcc6ea15b25ea5ade01cf429fe5d619d4e18a3c8b (patch)
tree426c3f7d3d87d3b132afe1b6db52bbf494fc8614
parentUse MD5_DIGEST_STRING_LENGTH (diff)
downloadwireguard-openbsd-cc6ea15b25ea5ade01cf429fe5d619d4e18a3c8b.tar.xz
wireguard-openbsd-cc6ea15b25ea5ade01cf429fe5d619d4e18a3c8b.zip
Add support for cksum (3 varieties), md4, sha256, sha384, and sha512.
Consequently, cksum moves becomes a link to md5 and moves to /bin. The cksum man page could use some polishing.
-rw-r--r--bin/md5/Makefile11
-rw-r--r--bin/md5/cksum.1276
-rw-r--r--bin/md5/crc.c223
-rw-r--r--bin/md5/crc.h59
-rw-r--r--bin/md5/md5.122
-rw-r--r--bin/md5/md5.c206
-rw-r--r--bin/md5/rmd160.122
-rw-r--r--bin/md5/sha1.122
8 files changed, 756 insertions, 85 deletions
diff --git a/bin/md5/Makefile b/bin/md5/Makefile
index 15eae48f19e..adb46529673 100644
--- a/bin/md5/Makefile
+++ b/bin/md5/Makefile
@@ -1,9 +1,14 @@
-# $OpenBSD: Makefile,v 1.7 2003/04/26 23:32:23 deraadt Exp $
+# $OpenBSD: Makefile,v 1.8 2004/05/02 17:53:29 millert Exp $
PROG= md5
-MAN= md5.1 sha1.1 rmd160.1
+SRCS= crc.c md5.c
+MAN= cksum.1 md5.1 sha1.1 rmd160.1
LINKS= ${BINDIR}/md5 ${BINDIR}/sha1 \
- ${BINDIR}/md5 ${BINDIR}/rmd160
+ ${BINDIR}/md5 ${BINDIR}/rmd160 \
+ ${BINDIR}/md5 ${BINDIR}/cksum \
+ ${BINDIR}/md5 ${BINDIR}/sum
+
+CPPFLAGS+= -I${.CURDIR}
COPTS+= -ansi -Wall -Wconversion -Wmissing-prototypes
.include <bsd.prog.mk>
diff --git a/bin/md5/cksum.1 b/bin/md5/cksum.1
new file mode 100644
index 00000000000..ba56407811d
--- /dev/null
+++ b/bin/md5/cksum.1
@@ -0,0 +1,276 @@
+.\" $OpenBSD: cksum.1,v 1.1 2004/05/02 17:53:29 millert Exp $
+.\"
+.\" Copyright (c) 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" the Institute of Electrical and Electronics Engineers, Inc.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" @(#)cksum.1 8.2 (Berkeley) 4/28/95
+.\"
+.Dd April 30, 2004
+.Dt CKSUM 1
+.Os
+.Sh NAME
+.Nm cksum ,
+.Nm sum
+.Nd display file checksums and block counts
+.Sh SYNOPSIS
+.Nm cksum
+.Oo Xo
+.Fl a Ar cksum | Ar md4 |
+.Ar md5 | Ar rmd160 | Ar sha1 |
+.Ar sha256 | Ar sha384 | Ar sha512 |
+.Ar sum | Ar sysvsum
+.Xc
+.Oc
+.Op Fl o Ar \&1 | Ar \&2
+.Oo
+.Fl p | Fl t | Fl x |
+.Fl s Ar string |
+.Fl c Ar [ checklist ... ] |
+.Ar file ...
+.Oc
+.Nm sum
+.Op Ar file ...
+.Sh DESCRIPTION
+The
+.Nm cksum
+utility writes to the standard output a single line each input file.
+The format of this line varies with the algorithm being used as follows:
+.Bl -tag -width Ds
+.It cksum
+The output line consists of three whitespace separated fields: a
+.Tn CRC
+checksum, the number of octets in the input
+and name of the file or string.
+If no file name is specified, the standard input is used and no file name
+is written.
+.It sum
+The output line consists of three whitespace separated fields: a
+.Tn CRC
+checksum, the number of kilobytes in the input
+and name of the file or string.
+If no file name is specified, the standard input is used and no file name
+is written.
+.It sysvsum
+The output line consists of three whitespace separated fields: a
+.Tn CRC
+checksum, the number of 512-byte blocks in the input
+and name of the file or string.
+If no file name is specified, the standard input is used and no file name
+is written.
+.It all others
+The output line consists of four whitespace separated fields:
+the name of the algorithm used, the name of the file or string in
+parenthesis, an equal sign and the cryptographic hash of the input.
+If no file name is specified, the standard input is used and only
+the cryptographic hash is output.
+.El
+.Pp
+The
+.Nm sum
+utility is identical to the
+.Nm cksum
+utility, except that it defaults to using historic algorithm 1, as
+described below.
+It is provided for compatibility only.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl a Ar algorithm
+Use the specified algorithm instead of the default (cksum).
+Supported algorithms include
+.Em cksum ,
+.Em md4 ,
+.Em md5 ,
+.Em rmd160 ,
+.Em sha1 ,
+.Em sha256 ,
+.Em sha384 ,
+.Em sha512 ,
+.Em sum
+and
+.Em sysvsum .
+Algorithms specified by the
+.Fl a
+option are case-insensitive.
+.It Fl o Ar \&1 No \&| Ar \&2
+Use historic algorithms instead of the (superior) default one
+(see below).
+.It Fl s Ar string
+Prints a checksum of the given
+.Ar string .
+.It Fl c Ar [ checklist ... ]
+Compares all checksums contained in the file
+.Ar checklist
+with newly computed checksums for the corresponding files.
+Output consists of the digest used, the file name,
+and an OK or FAILED for the result of the comparison.
+This will validate any of the supported checksums (see
+.Xr cksum 1 ).
+If no file is given, stdin is used.
+.It Fl p
+Echoes stdin to stdout and appends the
+checksum to stdout.
+.It Fl t
+Runs a built-in time trial.
+.It Fl x
+Runs a built-in test script.
+.El
+.Pp
+Algorithm 1 (aka
+.Em sum )
+is the algorithm used by historic
+.Bx
+systems as the
+.Nm sum
+algorithm and by historic
+.At V
+systems as the
+.Nm sum
+algorithm when using the
+.Fl r
+option.
+This is a 16-bit checksum, with a right rotation before each addition;
+overflow is discarded.
+.Pp
+Algorithm 2 (aka
+.Em sysvsum )
+is the algorithm used by historic
+.At V
+systems as the
+default
+.Nm sum
+algorithm.
+This is a 32-bit checksum, and is defined as follows:
+.Bd -unfilled -offset indent
+s = sum of all bytes;
+r = s % 2^16 + (s % 2^32) / 2^16;
+cksum = (r % 2^16) + r / 2^16;
+.Ed
+.Pp
+Both algorithm 1 and 2 write to the standard output the same fields as
+the default algorithm except that the size of the file in bytes is
+replaced with the size of the file in blocks.
+For historic reasons, the block size is 1024 for algorithm 1 and 512
+for algorithm 2.
+Partial blocks are rounded up.
+.Pp
+The default
+.Tn CRC
+used is based on the polynomial used for
+.Tn CRC
+error checking
+in the networking standard
+.St -iso8802-3
+The
+.Tn CRC
+checksum encoding is defined by the generating polynomial:
+.Pp
+.Bd -unfilled -offset indent
+G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
+ x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
+.Ed
+.Pp
+Mathematically, the
+.Tn CRC
+value corresponding to a given file is defined by
+the following procedure:
+.Bd -filled -offset indent
+The
+.Ar n
+bits to be evaluated are considered to be the coefficients of a mod 2
+polynomial M(x) of degree
+.Ar n Ns \-1 .
+These
+.Ar n
+bits are the bits from the file, with the most significant bit being the most
+significant bit of the first octet of the file and the last bit being the least
+significant bit of the last octet, padded with zero bits (if necessary) to
+achieve an integral number of octets, followed by one or more octets
+representing the length of the file as a binary value, least significant octet
+first.
+The smallest number of octets capable of representing this integer are used.
+.Pp
+M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
+G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
+.Pp
+The coefficients of R(x) are considered to be a 32-bit sequence.
+.Pp
+The bit sequence is complemented and the result is the CRC.
+.Ed
+.Pp
+The other available algorithms are described in their respective
+man pages in section 3 of the manual.
+.Sh DIAGNOSTICS
+The
+.Nm cksum
+and
+.Nm sum
+utilities exit 0 on success or >0 if an error occurred.
+.Sh SEE ALSO
+.Xr md5 1 ,
+.Xr rmd160 1 ,
+.Xr sha1 1
+.Pp
+The default calculation is identical to that given in pseudo-code
+in the following
+.Tn ACM
+article.
+.Rs
+.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
+.%A Dilip V. Sarwate
+.%J "Communications of the \\*(tNACM\\*(sP"
+.%D "August 1988"
+.Re
+.Sh STANDARDS
+The
+.Nm cksum
+utility is compliant with the
+.St -p1003.2-92
+specification.
+.Sh HISTORY
+A
+.Nm sum
+command appeared in
+.At v2 .
+The
+.Nm cksum
+utility appeared in
+.Bx 4.4 .
+.Sh WARNING
+Do not use the
+.Em cksum ,
+.Em md4 ,
+.Em sum ,
+or
+.Em sysvsum
+algorithms to detect hostile binary modifications.
+An attacker can trivially produce backdoored daemons which have the same
+checksum as the standard versions.
+Use a cryptographic-strength checksum (such as MD5) instead.
diff --git a/bin/md5/crc.c b/bin/md5/crc.c
new file mode 100644
index 00000000000..77a393a1056
--- /dev/null
+++ b/bin/md5/crc.c
@@ -0,0 +1,223 @@
+/* $OpenBSD: crc.c,v 1.1 2004/05/02 17:53:29 millert Exp $ */
+
+/*
+ * Copyright (c) 2004 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef lint
+static const char rcsid[] = "$OpenBSD: crc.c,v 1.1 2004/05/02 17:53:29 millert Exp $";
+#endif /* not lint */
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "crc.h"
+
+/*
+ * Table-driven version of the following polynomial from POSIX 1003.2:
+ * G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 +
+ * x^7 + x^5 + x^4 + x^2 + x + 1
+ */
+static const u_int32_t crc32tab[] = {
+ 0x00000000U,
+ 0x04c11db7U, 0x09823b6eU, 0x0d4326d9U, 0x130476dcU, 0x17c56b6bU,
+ 0x1a864db2U, 0x1e475005U, 0x2608edb8U, 0x22c9f00fU, 0x2f8ad6d6U,
+ 0x2b4bcb61U, 0x350c9b64U, 0x31cd86d3U, 0x3c8ea00aU, 0x384fbdbdU,
+ 0x4c11db70U, 0x48d0c6c7U, 0x4593e01eU, 0x4152fda9U, 0x5f15adacU,
+ 0x5bd4b01bU, 0x569796c2U, 0x52568b75U, 0x6a1936c8U, 0x6ed82b7fU,
+ 0x639b0da6U, 0x675a1011U, 0x791d4014U, 0x7ddc5da3U, 0x709f7b7aU,
+ 0x745e66cdU, 0x9823b6e0U, 0x9ce2ab57U, 0x91a18d8eU, 0x95609039U,
+ 0x8b27c03cU, 0x8fe6dd8bU, 0x82a5fb52U, 0x8664e6e5U, 0xbe2b5b58U,
+ 0xbaea46efU, 0xb7a96036U, 0xb3687d81U, 0xad2f2d84U, 0xa9ee3033U,
+ 0xa4ad16eaU, 0xa06c0b5dU, 0xd4326d90U, 0xd0f37027U, 0xddb056feU,
+ 0xd9714b49U, 0xc7361b4cU, 0xc3f706fbU, 0xceb42022U, 0xca753d95U,
+ 0xf23a8028U, 0xf6fb9d9fU, 0xfbb8bb46U, 0xff79a6f1U, 0xe13ef6f4U,
+ 0xe5ffeb43U, 0xe8bccd9aU, 0xec7dd02dU, 0x34867077U, 0x30476dc0U,
+ 0x3d044b19U, 0x39c556aeU, 0x278206abU, 0x23431b1cU, 0x2e003dc5U,
+ 0x2ac12072U, 0x128e9dcfU, 0x164f8078U, 0x1b0ca6a1U, 0x1fcdbb16U,
+ 0x018aeb13U, 0x054bf6a4U, 0x0808d07dU, 0x0cc9cdcaU, 0x7897ab07U,
+ 0x7c56b6b0U, 0x71159069U, 0x75d48ddeU, 0x6b93dddbU, 0x6f52c06cU,
+ 0x6211e6b5U, 0x66d0fb02U, 0x5e9f46bfU, 0x5a5e5b08U, 0x571d7dd1U,
+ 0x53dc6066U, 0x4d9b3063U, 0x495a2dd4U, 0x44190b0dU, 0x40d816baU,
+ 0xaca5c697U, 0xa864db20U, 0xa527fdf9U, 0xa1e6e04eU, 0xbfa1b04bU,
+ 0xbb60adfcU, 0xb6238b25U, 0xb2e29692U, 0x8aad2b2fU, 0x8e6c3698U,
+ 0x832f1041U, 0x87ee0df6U, 0x99a95df3U, 0x9d684044U, 0x902b669dU,
+ 0x94ea7b2aU, 0xe0b41de7U, 0xe4750050U, 0xe9362689U, 0xedf73b3eU,
+ 0xf3b06b3bU, 0xf771768cU, 0xfa325055U, 0xfef34de2U, 0xc6bcf05fU,
+ 0xc27dede8U, 0xcf3ecb31U, 0xcbffd686U, 0xd5b88683U, 0xd1799b34U,
+ 0xdc3abdedU, 0xd8fba05aU, 0x690ce0eeU, 0x6dcdfd59U, 0x608edb80U,
+ 0x644fc637U, 0x7a089632U, 0x7ec98b85U, 0x738aad5cU, 0x774bb0ebU,
+ 0x4f040d56U, 0x4bc510e1U, 0x46863638U, 0x42472b8fU, 0x5c007b8aU,
+ 0x58c1663dU, 0x558240e4U, 0x51435d53U, 0x251d3b9eU, 0x21dc2629U,
+ 0x2c9f00f0U, 0x285e1d47U, 0x36194d42U, 0x32d850f5U, 0x3f9b762cU,
+ 0x3b5a6b9bU, 0x0315d626U, 0x07d4cb91U, 0x0a97ed48U, 0x0e56f0ffU,
+ 0x1011a0faU, 0x14d0bd4dU, 0x19939b94U, 0x1d528623U, 0xf12f560eU,
+ 0xf5ee4bb9U, 0xf8ad6d60U, 0xfc6c70d7U, 0xe22b20d2U, 0xe6ea3d65U,
+ 0xeba91bbcU, 0xef68060bU, 0xd727bbb6U, 0xd3e6a601U, 0xdea580d8U,
+ 0xda649d6fU, 0xc423cd6aU, 0xc0e2d0ddU, 0xcda1f604U, 0xc960ebb3U,
+ 0xbd3e8d7eU, 0xb9ff90c9U, 0xb4bcb610U, 0xb07daba7U, 0xae3afba2U,
+ 0xaafbe615U, 0xa7b8c0ccU, 0xa379dd7bU, 0x9b3660c6U, 0x9ff77d71U,
+ 0x92b45ba8U, 0x9675461fU, 0x8832161aU, 0x8cf30badU, 0x81b02d74U,
+ 0x857130c3U, 0x5d8a9099U, 0x594b8d2eU, 0x5408abf7U, 0x50c9b640U,
+ 0x4e8ee645U, 0x4a4ffbf2U, 0x470cdd2bU, 0x43cdc09cU, 0x7b827d21U,
+ 0x7f436096U, 0x7200464fU, 0x76c15bf8U, 0x68860bfdU, 0x6c47164aU,
+ 0x61043093U, 0x65c52d24U, 0x119b4be9U, 0x155a565eU, 0x18197087U,
+ 0x1cd86d30U, 0x029f3d35U, 0x065e2082U, 0x0b1d065bU, 0x0fdc1becU,
+ 0x3793a651U, 0x3352bbe6U, 0x3e119d3fU, 0x3ad08088U, 0x2497d08dU,
+ 0x2056cd3aU, 0x2d15ebe3U, 0x29d4f654U, 0xc5a92679U, 0xc1683bceU,
+ 0xcc2b1d17U, 0xc8ea00a0U, 0xd6ad50a5U, 0xd26c4d12U, 0xdf2f6bcbU,
+ 0xdbee767cU, 0xe3a1cbc1U, 0xe760d676U, 0xea23f0afU, 0xeee2ed18U,
+ 0xf0a5bd1dU, 0xf464a0aaU, 0xf9278673U, 0xfde69bc4U, 0x89b8fd09U,
+ 0x8d79e0beU, 0x803ac667U, 0x84fbdbd0U, 0x9abc8bd5U, 0x9e7d9662U,
+ 0x933eb0bbU, 0x97ffad0cU, 0xafb010b1U, 0xab710d06U, 0xa6322bdfU,
+ 0xa2f33668U, 0xbcb4666dU, 0xb8757bdaU, 0xb5365d03U, 0xb1f740b4U
+};
+
+void
+CKSUM_Init(CKSUM_CTX *ctx)
+{
+ ctx->crc = 0;
+ ctx->len = 0;
+}
+
+#define UPDATE(crc, byte) do \
+ (crc) = ((crc) << 8) ^ crc32tab[((crc) >> 24) ^ (byte)]; \
+while(0)
+
+void
+CKSUM_Update(CKSUM_CTX *ctx, const unsigned char *buf, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ UPDATE(ctx->crc, buf[i]);
+ ctx->len += len;
+}
+
+void
+CKSUM_Final(CKSUM_CTX *ctx)
+{
+ off_t len = ctx->len;
+
+ /* add in number of bytes read and finish */
+ while (len != 0) {
+ UPDATE(ctx->crc, len & 0xff);
+ len >>= 8;
+ }
+ ctx->crc = ~ctx->crc;
+}
+
+char *
+CKSUM_End(CKSUM_CTX *ctx, char *outstr)
+{
+ CKSUM_Final(ctx);
+
+ if (outstr == NULL) {
+ if (asprintf(&outstr, "%u %lld", ctx->crc, ctx->len) == -1)
+ return (NULL);
+ } else {
+ (void)snprintf(outstr, CKSUM_DIGEST_STRING_LENGTH, "%u %lld",
+ ctx->crc, ctx->len);
+ }
+
+ return (outstr);
+}
+
+void
+SUM_Init(SUM_CTX *ctx)
+{
+ ctx->crc = 0;
+ ctx->len = 0;
+}
+
+void
+SUM_Update(SUM_CTX *ctx, const unsigned char *buf, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ ctx->crc = ((ctx->crc >> 1) + ((ctx->crc & 1) << 15) + buf[i]);
+ ctx->crc &= 0xffff;
+ }
+ ctx->len += len;
+}
+
+void
+SUM_Final(SUM_CTX *ctx)
+{
+ ctx->len = (ctx->len + 1023) / 1024; /* convert to 1KB blocks */
+}
+
+char *
+SUM_End(SUM_CTX *ctx, char *outstr)
+{
+ SUM_Final(ctx);
+
+ if (outstr == NULL) {
+ if (asprintf(&outstr, "%u %lld", ctx->crc, ctx->len) == -1)
+ return (NULL);
+ } else {
+ (void)snprintf(outstr, SUM_DIGEST_STRING_LENGTH, "%u %lld",
+ ctx->crc, ctx->len);
+ }
+
+ return (outstr);
+}
+
+void
+SYSVSUM_Init(SYSVSUM_CTX *ctx)
+{
+ ctx->crc = 0;
+ ctx->len = 0;
+}
+
+void
+SYSVSUM_Update(SYSVSUM_CTX *ctx, const unsigned char *buf, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ ctx->crc += buf[i];
+ ctx->len += len;
+}
+
+void
+SYSVSUM_Final(SYSVSUM_CTX *ctx)
+{
+ ctx->crc = (ctx->crc & 0xffff) + (ctx->crc >> 16);
+ ctx->crc = (ctx->crc & 0xffff) + (ctx->crc >> 16);
+ ctx->len = (ctx->len + 511) / 512; /* convert to 512 byte blocks */
+}
+
+char *
+SYSVSUM_End(SYSVSUM_CTX *ctx, char *outstr)
+{
+ SYSVSUM_Final(ctx);
+
+ if (outstr == NULL) {
+ if (asprintf(&outstr, "%u %lld", ctx->crc, ctx->len) == -1)
+ return (NULL);
+ } else {
+ (void)snprintf(outstr, SYSVSUM_DIGEST_STRING_LENGTH, "%u %lld",
+ ctx->crc, ctx->len);
+ }
+
+ return (outstr);
+}
diff --git a/bin/md5/crc.h b/bin/md5/crc.h
new file mode 100644
index 00000000000..1570d911639
--- /dev/null
+++ b/bin/md5/crc.h
@@ -0,0 +1,59 @@
+/* $OpenBSD: crc.h,v 1.1 2004/05/02 17:53:29 millert Exp $ */
+
+/*
+ * Copyright (c) 2004 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define CKSUM_DIGEST_LENGTH 4
+#define CKSUM_DIGEST_STRING_LENGTH (10 + 1 + 19)
+
+typedef struct CKSUMContext {
+ u_int32_t crc;
+ off_t len;
+} CKSUM_CTX;
+
+#define SUM_DIGEST_LENGTH 4
+#define SUM_DIGEST_STRING_LENGTH (10 + 1 + 16)
+
+typedef struct SUMContext {
+ u_int32_t crc;
+ off_t len;
+} SUM_CTX;
+
+#define SYSVSUM_DIGEST_LENGTH 4
+#define SYSVSUM_DIGEST_STRING_LENGTH (10 + 1 + 16)
+
+typedef struct SYSVSUMContext {
+ u_int32_t crc;
+ off_t len;
+} SYSVSUM_CTX;
+
+void CKSUM_Init(CKSUM_CTX *);
+void CKSUM_Update(CKSUM_CTX *, const u_int8_t *, size_t);
+void CKSUM_Final(CKSUM_CTX *);
+char *CKSUM_End(CKSUM_CTX *, char *);
+char *CKSUM_Data(const u_int8_t *, size_t, char *);
+
+void SUM_Init(SUM_CTX *);
+void SUM_Update(SUM_CTX *, const u_int8_t *, size_t);
+void SUM_Final(SUM_CTX *);
+char *SUM_End(SUM_CTX *, char *);
+char *SUM_Data(const u_int8_t *, size_t, char *);
+
+void SYSVSUM_Init(SYSVSUM_CTX *);
+void SYSVSUM_Update(SYSVSUM_CTX *, const u_int8_t *, size_t);
+void SYSVSUM_Final(SYSVSUM_CTX *);
+char *SYSVSUM_End(SYSVSUM_CTX *, char *);
+char *SYSVSUM_Data(const u_int8_t *, size_t, char *);
diff --git a/bin/md5/md5.1 b/bin/md5/md5.1
index 59f3212cee9..9cb9140524f 100644
--- a/bin/md5/md5.1
+++ b/bin/md5/md5.1
@@ -1,6 +1,6 @@
-.\" $OpenBSD: md5.1,v 1.17 2003/06/17 21:56:23 millert Exp $
+.\" $OpenBSD: md5.1,v 1.18 2004/05/02 17:53:29 millert Exp $
.\"
-.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
+.\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +18,7 @@
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
-.Dd June 4, 2001
+.Dd April 30, 2004
.Dt MD5 1
.Os
.Sh NAME
@@ -29,7 +29,7 @@
.Oo
.Fl p | Fl t | Fl x |
.Fl s Ar string |
-.Fl c Ar [ file ... ] |
+.Fl c Ar [ checklist ... ] |
.Ar file ...
.Oc
.Sh DESCRIPTION
@@ -53,16 +53,14 @@ The options are as follows:
.It Fl s Ar string
Prints a checksum of the given
.Ar string .
-.It Fl c Ar [ file ... ]
-Compares all checksums in
-.Ar file
-with newly computed checksums for the files mentioned in
-.Ar file .
+.It Fl c Ar [ checklist ... ]
+Compares all checksums contained in the file
+.Ar checklist
+with newly computed checksums for the corresponding files.
Output consists of the digest used, the file name,
and an OK or FAILED for the result of the comparison.
-This will validate any of the supported checksums in
-.Ar file ,
-including RMD-160 and SHA-1.
+This will validate any of the supported checksums (see
+.Xr cksum 1 ).
If no file is given, stdin is used.
.It Fl p
Echoes stdin to stdout and appends the
diff --git a/bin/md5/md5.c b/bin/md5/md5.c
index d4351d3bb77..55418210c1b 100644
--- a/bin/md5/md5.c
+++ b/bin/md5/md5.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: md5.c,v 1.28 2004/04/28 23:58:41 millert Exp $ */
+/* $OpenBSD: md5.c,v 1.29 2004/05/02 17:53:29 millert Exp $ */
/*
* Copyright (c) 2001, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -30,89 +30,165 @@
#include <time.h>
#include <unistd.h>
+#include <md4.h>
#include <md5.h>
-#include <sha1.h>
#include <rmd160.h>
+#include <sha1.h>
+#include <sha2.h>
+#include <crc.h>
-#define MAX_DIGEST_LEN 40
+#define MAX_DIGEST_LEN 128
union ANY_CTX {
+ CKSUM_CTX cksum;
+ MD4_CTX md4;
MD5_CTX md5;
RMD160_CTX rmd160;
SHA1_CTX sha1;
+ SHA256_CTX sha256;
+ SHA384_CTX sha384;
+ SHA512_CTX sha512;
+ SUM_CTX sum;
+ SYSVSUM_CTX sysvsum;
};
+void digest_print(const char *, const char *, const char *);
+void digest_print_short(const char *, const char *, const char *);
+void digest_print_string(const char *, const char *, const char *);
+
struct hash_functions {
char *name;
int digestlen;
void (*init)(void *);
void (*update)(void *, const unsigned char *, unsigned int);
char * (*end)(void *, char *);
- char * (*file)(char *, char *);
- char * (*data)(const unsigned char *, unsigned int, char *);
+ void (*print)(const char *, const char *, const char *);
+ void (*printstr)(const char *, const char *, const char *);
};
struct hash_functions functions[] = {
{
+ "CKSUM",
+ CKSUM_DIGEST_LENGTH * 2,
+ (void (*)(void *))CKSUM_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))CKSUM_Update,
+ (char *(*)(void *, char *))CKSUM_End,
+ digest_print_short,
+ digest_print_short
+ }, {
+ "MD4",
+ MD5_DIGEST_LENGTH * 2,
+ (void (*)(void *))MD4Init,
+ (void (*)(void *, const unsigned char *, unsigned int))MD4Update,
+ (char *(*)(void *, char *))MD4End,
+ digest_print,
+ digest_print_string
+ }, {
"MD5",
MD5_DIGEST_LENGTH * 2,
(void (*)(void *))MD5Init,
(void (*)(void *, const unsigned char *, unsigned int))MD5Update,
(char *(*)(void *, char *))MD5End,
- (char *(*)(char *, char *))MD5File,
- (char *(*)(const unsigned char *, unsigned int, char *))MD5Data
+ digest_print,
+ digest_print_string
}, {
"RMD160",
RMD160_DIGEST_LENGTH * 2,
(void (*)(void *))RMD160Init,
(void (*)(void *, const unsigned char *, unsigned int))RMD160Update,
(char *(*)(void *, char *))RMD160End,
- (char *(*)(char *, char *))RMD160File,
- (char *(*)(const unsigned char *, unsigned int, char *))RMD160Data
+ digest_print,
+ digest_print_string
}, {
"SHA1",
SHA1_DIGEST_LENGTH * 2,
(void (*)(void *))SHA1Init,
(void (*)(void *, const unsigned char *, unsigned int))SHA1Update,
(char *(*)(void *, char *))SHA1End,
- (char *(*)(char *, char *))SHA1File,
- (char *(*)(const unsigned char *, unsigned int, char *))SHA1Data
+ digest_print,
+ digest_print_string
+ }, {
+ "SHA256",
+ SHA256_DIGEST_LENGTH * 2,
+ (void (*)(void *))SHA256_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))SHA256_Update,
+ (char *(*)(void *, char *))SHA256_End,
+ digest_print,
+ digest_print_string
+ }, {
+ "SHA384",
+ SHA384_DIGEST_LENGTH * 2,
+ (void (*)(void *))SHA384_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))SHA384_Update,
+ (char *(*)(void *, char *))SHA384_End,
+ digest_print,
+ digest_print_string
+ }, {
+ "SHA512",
+ SHA512_DIGEST_LENGTH * 2,
+ (void (*)(void *))SHA512_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))SHA512_Update,
+ (char *(*)(void *, char *))SHA512_End,
+ digest_print,
+ digest_print_string
+ }, {
+ "SUM",
+ SUM_DIGEST_LENGTH * 2,
+ (void (*)(void *))SUM_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))SUM_Update,
+ (char *(*)(void *, char *))SUM_End,
+ digest_print_short,
+ digest_print_short
+ }, {
+ "SYSVSUM",
+ SYSVSUM_DIGEST_LENGTH * 2,
+ (void (*)(void *))SYSVSUM_Init,
+ (void (*)(void *, const unsigned char *, unsigned int))SYSVSUM_Update,
+ (char *(*)(void *, char *))SYSVSUM_End,
+ digest_print_short,
+ digest_print_short
}, {
NULL,
},
};
+__dead void usage(void);
+void digest_file(char *, struct hash_functions *, int);
+int digest_filelist(int, char *);
+void digest_string(char *, struct hash_functions *);
+void digest_test(struct hash_functions *);
+void digest_time(struct hash_functions *);
+
extern char *__progname;
-static void usage(void);
-static void digest_file(char *, struct hash_functions *, int);
-static int digest_filelist(int, char *);
-static void digest_string(char *, struct hash_functions *);
-static void digest_test(struct hash_functions *);
-static void digest_time(struct hash_functions *);
int
main(int argc, char **argv)
{
int fl, digest_type, error;
int cflag, pflag, tflag, xflag;
- char *input_string;
-
- /* Set digest type based on program name, defaults to MD5. */
- for (digest_type = 0; functions[digest_type].name != NULL;
- digest_type++) {
- if (strcasecmp(functions[digest_type].name, __progname) == 0)
- break;
- }
- if (functions[digest_type].name == NULL)
- digest_type = 0;
+ char *digest_name, *input_string;
input_string = NULL;
+ digest_name = __progname;
error = cflag = pflag = tflag = xflag = 0;
- while ((fl = getopt(argc, argv, "pctxs:")) != -1) {
+ while ((fl = getopt(argc, argv, "a:co:ps:tx")) != -1) {
switch (fl) {
+ case 'a':
+ digest_name = optarg;
+ break;
case 'c':
cflag = 1;
break;
+ case 'o':
+ if (strcmp(optarg, "1") == 0)
+ digest_name = "sum";
+ else if (strcmp(optarg, "2") == 0)
+ digest_name = "sysvsum";
+ else {
+ warnx("illegal argument to -o option");
+ usage();
+ }
+ break;
case 'p':
pflag = 1;
break;
@@ -132,7 +208,21 @@ main(int argc, char **argv)
argc -= optind;
argv += optind;
- /* All arguments are mutually exclusive */
+ for (digest_type = 0; functions[digest_type].name != NULL;
+ digest_type++) {
+ if (strcasecmp(functions[digest_type].name, digest_name) == 0)
+ break;
+ }
+ if (functions[digest_type].name == NULL) {
+ if (digest_name != __progname) {
+ /* Unsupported algorithm specified, exit */
+ warnx("unknown algorithm \"%s\"", digest_name);
+ usage();
+ }
+ digest_type = 0; /* default to cksum */
+ }
+
+ /* Most arguments are mutually exclusive */
fl = pflag + tflag + xflag + cflag + (input_string != NULL);
if (fl > 1 || (fl && argc && cflag == 0))
usage();
@@ -158,17 +248,37 @@ main(int argc, char **argv)
return(error ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static void
+void
digest_string(char *string, struct hash_functions *hf)
{
char digest[MAX_DIGEST_LEN + 1];
+ union ANY_CTX context;
- (void)hf->data((unsigned char *)string, (unsigned int)strlen(string),
- digest);
- (void)printf("%s (\"%s\") = %s\n", hf->name, string, digest);
+ hf->init(&context);
+ hf->update(&context, string, (unsigned int)strlen(string));
+ (void)hf->end(&context, digest);
+ hf->printstr(hf->name, string, digest);
}
-static void
+void
+digest_print(const char *name, const char *what, const char *digest)
+{
+ (void)printf("%s (%s) = %s\n", name, what, digest);
+}
+
+void
+digest_print_string(const char *name, const char *what, const char *digest)
+{
+ (void)printf("%s (\"%s\") = %s\n", name, what, digest);
+}
+
+void
+digest_print_short(const char *name, const char *what, const char *digest)
+{
+ (void)printf("%s %s\n", digest, what);
+}
+
+void
digest_file(char *file, struct hash_functions *hf, int echo)
{
int fd;
@@ -205,7 +315,7 @@ digest_file(char *file, struct hash_functions *hf, int echo)
(void)puts(digest);
} else {
close(fd);
- (void)printf("%s (%s) = %s\n", hf->name, file, digest);
+ hf->print(hf->name, file, digest);
}
}
@@ -215,7 +325,7 @@ digest_file(char *file, struct hash_functions *hf, int echo)
* generate a new checksum against the file on the filesystem.
* Print out the result of each comparison.
*/
-static int
+int
digest_filelist(int algorithm_def, char *file)
{
int fd, found, error;
@@ -352,7 +462,7 @@ digest_filelist(int algorithm_def, char *file)
#define TEST_BLOCK_LEN 10000
#define TEST_BLOCK_COUNT 10000
-static void
+void
digest_time(struct hash_functions *hf)
{
struct timeval start, stop, res;
@@ -385,7 +495,7 @@ digest_time(struct hash_functions *hf)
TEST_BLOCK_LEN * TEST_BLOCK_COUNT / elapsed);
}
-static void
+void
digest_test(struct hash_functions *hf)
{
union ANY_CTX context;
@@ -412,8 +522,7 @@ digest_test(struct hash_functions *hf)
hf->update((void *)&context, (unsigned char *)test_strings[i],
(unsigned int)strlen(test_strings[i]));
(void)hf->end(&context, digest);
- (void)printf("%s (\"%s\") = %s\n", hf->name, test_strings[i],
- digest);
+ hf->printstr(hf->name, test_strings[i], digest);
}
/* Now simulate a string of a million 'a' characters. */
@@ -423,15 +532,20 @@ digest_test(struct hash_functions *hf)
hf->update(&context, (unsigned char *)buf,
(unsigned int)sizeof(buf));
(void)hf->end(&context, digest);
- (void)printf("%s (one million 'a' characters) = %s\n",
- hf->name, digest);
+ hf->print(hf->name, "one million 'a' characters", digest);
}
-static void
+__dead void
usage(void)
{
- fprintf(stderr, "usage: %s [-p | -t | -x | -c [ checksum_file ... ]",
- __progname);
- fprintf(stderr, " | -s string | file ...]\n");
+ /* We only list the algorithms when invoked as cksum. */
+ if (strcmp(__progname, "cksum") == 0)
+ fprintf(stderr,
+ "usage: cksum [-a cksum|md4|md5|rmd160|sha1|sha256|sha384|"
+ "sha512|sum|sysvsum]\n [-o 1|2] ");
+ else
+ fprintf(stderr, "usage: %s ", __progname);
+ fprintf(stderr, "[-p | -t | -x | -c [checklist ...] | "
+ "-s string | file ...]\n");
exit(EXIT_FAILURE);
}
diff --git a/bin/md5/rmd160.1 b/bin/md5/rmd160.1
index 26ac502630d..014973a45a9 100644
--- a/bin/md5/rmd160.1
+++ b/bin/md5/rmd160.1
@@ -1,6 +1,6 @@
-.\" $OpenBSD: rmd160.1,v 1.15 2003/06/17 21:56:23 millert Exp $
+.\" $OpenBSD: rmd160.1,v 1.16 2004/05/02 17:53:29 millert Exp $
.\"
-.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
+.\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +18,7 @@
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
-.Dd June 4, 2001
+.Dd April 30, 2004
.Dt RMD160 1
.Os
.Sh NAME
@@ -29,7 +29,7 @@
.Oo
.Fl p | Fl t | Fl x |
.Fl s Ar string |
-.Fl c Ar [ file ... ] |
+.Fl c Ar [ checklist ... ] |
.Ar file ...
.Oc
.Sh DESCRIPTION
@@ -53,16 +53,14 @@ The options are as follows:
.It Fl s Ar string
Prints a checksum of the given
.Ar string .
-.It Fl c Ar [ file ... ]
-Compares all checksums in
-.Ar file
-with newly computed checksums for the files mentioned in
-.Ar file .
+.It Fl c Ar [ checklist ... ]
+Compares all checksums contained in the file
+.Ar checklist
+with newly computed checksums for the corresponding files.
Output consists of the digest used, the file name,
and an OK or FAILED for the result of the comparison.
-This will validate any of the supported checksums in
-.Ar file ,
-including MD5 and SHA-1.
+This will validate any of the supported checksums (see
+.Xr cksum 1 ).
If no file is given, stdin is used.
.It Fl p
Echoes stdin to stdout and appends the
diff --git a/bin/md5/sha1.1 b/bin/md5/sha1.1
index e9caf1ba0cd..ce6f89cba08 100644
--- a/bin/md5/sha1.1
+++ b/bin/md5/sha1.1
@@ -1,6 +1,6 @@
-.\" $OpenBSD: sha1.1,v 1.16 2003/06/17 21:56:23 millert Exp $
+.\" $OpenBSD: sha1.1,v 1.17 2004/05/02 17:53:29 millert Exp $
.\"
-.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
+.\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +18,7 @@
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
-.Dd June 4, 2001
+.Dd Aprile 30, 2004
.Dt SHA1 1
.Os
.Sh NAME
@@ -29,7 +29,7 @@
.Oo
.Fl p | Fl t | Fl x |
.Fl s Ar string |
-.Fl c Ar [ file ... ] |
+.Fl c Ar [ checklist ... ] |
.Ar file ...
.Oc
.Sh DESCRIPTION
@@ -53,16 +53,14 @@ The options are as follows:
.It Fl s Ar string
Prints a checksum of the given
.Ar string .
-.It Fl c Ar [ file ... ]
-Compares all checksums in
-.Ar file
-with newly computed checksums for the files mentioned in
-.Ar file .
+.It Fl c Ar [ checklist ... ]
+Compares all checksums contained in the file
+.Ar checklist
+with newly computed checksums for the corresponding files.
Output consists of the digest used, the file name,
and an OK or FAILED for the result of the comparison.
-This will validate any of the supported checksums in
-.Ar file ,
-including MD5 and RMD-160.
+This will validate any of the supported checksums (see
+.Xr cksum 1 ).
If no file is given, stdin is used.
.It Fl p
Echoes stdin to stdout and appends the