diff options
Diffstat (limited to 'security')
-rw-r--r-- | security/Makefile | 1 | ||||
-rw-r--r-- | security/digest/Makefile | 34 | ||||
-rw-r--r-- | security/digest/distinfo | 1 | ||||
-rw-r--r-- | security/digest/files/Makefile | 35 | ||||
-rw-r--r-- | security/digest/files/digest.1 | 88 | ||||
-rw-r--r-- | security/digest/files/digest.c | 189 | ||||
-rw-r--r-- | security/digest/files/md5.h | 55 | ||||
-rw-r--r-- | security/digest/files/md5c.c | 361 | ||||
-rw-r--r-- | security/digest/files/md5hl.c | 117 | ||||
-rw-r--r-- | security/digest/files/rmd160.c | 463 | ||||
-rw-r--r-- | security/digest/files/rmd160.h | 49 | ||||
-rw-r--r-- | security/digest/files/rmd160hl.c | 100 | ||||
-rw-r--r-- | security/digest/files/sha1.c | 284 | ||||
-rw-r--r-- | security/digest/files/sha1.h | 30 | ||||
-rw-r--r-- | security/digest/files/sha1hl.c | 107 | ||||
-rw-r--r-- | security/digest/pkg-comment | 1 | ||||
-rw-r--r-- | security/digest/pkg-descr | 6 |
17 files changed, 1921 insertions, 0 deletions
diff --git a/security/Makefile b/security/Makefile index e4e94c1b61fb..084d97b85d3a 100644 --- a/security/Makefile +++ b/security/Makefile @@ -23,6 +23,7 @@ SUBDIR += cryptopp SUBDIR += cyrus-sasl SUBDIR += ddos_scan + SUBDIR += digest SUBDIR += donkey SUBDIR += dsniff SUBDIR += fakebo diff --git a/security/digest/Makefile b/security/digest/Makefile new file mode 100644 index 000000000000..a00e1d21f00a --- /dev/null +++ b/security/digest/Makefile @@ -0,0 +1,34 @@ +# New ports collection makefile for: digest +# Date created: 2001-03-09 +# Whom: trevor +# +# $NetBSD: Makefile,v 1.4 2001/03/07 23:03:19 kim Exp $ +# $FreeBSD$ +# + +PORTNAME= digest +PORTVERSION= 20010302 +CATEGORIES= security sysutils +MASTER_SITES= # +DISTFILES= +EXTRACT_ONLY= + +MAINTAINER= trevor@FreeBSD.org + +CFLAGS+= "-DVERSION=${PORTVERSION}" +MAKE_ENV+= BINDIR=${LOCALBASE}/bin MANDIR=${LOCALBASE}/man +MAN1= digest.1 +PLIST= ${WRKDIR}/pkg-plist +WRKSRC= ${WRKDIR} + +do-extract: + ${MKDIR} ${WRKSRC} + cd ${FILESDIR} && pax -r -w * ${WRKSRC} + +pre-install: + ${ECHO} bin/digest > ${PLIST} + +post-install: + ${INSTALL_MAN} ${FILESDIR}/digest.1 ${PREFIX}/man/man1/ + +.include <bsd.port.mk> diff --git a/security/digest/distinfo b/security/digest/distinfo new file mode 100644 index 000000000000..94456aba2bcf --- /dev/null +++ b/security/digest/distinfo @@ -0,0 +1 @@ +# intentionally left blank diff --git a/security/digest/files/Makefile b/security/digest/files/Makefile new file mode 100644 index 000000000000..3df4b3a18794 --- /dev/null +++ b/security/digest/files/Makefile @@ -0,0 +1,35 @@ +# $NetBSD: Makefile,v 1.1.1.1 2001/03/06 11:21:04 agc Exp $ +# $FreeBSD$ + +# When adding new digest algorithms, please use rmd160 as the template, +# and bump the version definition in the package Makefile + +PROG= digest +SRCS= digest.c +CPPFLAGS+= -I${.CURDIR} +LDFLAGS= -L${LOCALBASE}/lib -lmd +WARNS= 2 + +.if !exists(/usr/include/rmd160.h) +SRCS+= rmd160.c rmd160hl.c +.endif + +.if !exists(/usr/include/sha1.h) +SRCS+= sha1.c sha1hl.c +.endif + +.if !exists(/usr/include/md5.h) +SRCS+= md5c.c md5hl.c +.endif + +# use definition for correct endian.h header file +.if exists(/usr/include/sys/endian.h) +CPPFLAGS+= -DHAVE_SYS_ENDIAN_H_ +.endif +.if exists(/usr/include/machine/endian.h) +CPPFLAGS+= -DHAVE_MACHINE_ENDIAN_H_ +.endif + +LDSTATIC?= -static + +.include <bsd.prog.mk> diff --git a/security/digest/files/digest.1 b/security/digest/files/digest.1 new file mode 100644 index 000000000000..1fcd51d658f7 --- /dev/null +++ b/security/digest/files/digest.1 @@ -0,0 +1,88 @@ +.\" $NetBSD: digest.1,v 1.1.1.1 2001/03/06 11:21:04 agc Exp $ +.\" +.\" +.\" Copyright (c) 2001 Alistair G. Crooks. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Alistair G. Crooks. +.\" 4. The name of the author may not be used to endorse or promote +.\" products derived from this software without specific prior written +.\" permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +.\" +.\" +.Dd February 28, 2001 +.Dt DIGEST 1 +.Os NetBSD +.Sh NAME +.Nm digest +.Nd calculate message digests +.Sh SYNOPSIS +.Nm "" +.Ar algorithm +.Op file... +.Sh DESCRIPTION +The +.Nm +utility calculates message digests of files or, +if no file is specified, standard input. +The list of possible algorithms is: +.Bl -tag -width Ds +.It md5 +the +.Xr md5 3 +algorithm will be used. +.It sha1 +the +.Xr sha1 3 +algorithm will be used. +.It rmd160 +the +.Xr rmd160 3 +algorithm will be used. +.El +.Pp +The +.Nm +utility is a simple wrapper for the various different +algorithm implementations, which are located in the standard +C library, and was designed to be scalable as new message digest +algorithms are developed. +.Pp +The +.Nm +utility exits 0 on success, and >0 if an error occurs. +.Sh SEE ALSO +.Xr cksum 1 , +.Xr md5 3 , +.Xr rmd160 3 , +.Xr sha1 3 +.Sh HISTORY +The +.Nm +utility first appeared in +.Nx 1.6 . +.Sh AUTHOR +The +.Nm +utility was written by Alistair G. Crooks (agc@netbsd.org). diff --git a/security/digest/files/digest.c b/security/digest/files/digest.c new file mode 100644 index 000000000000..f5538b998dc7 --- /dev/null +++ b/security/digest/files/digest.c @@ -0,0 +1,189 @@ +/* $NetBSD: digest.c,v 1.1.1.1 2001/03/06 11:21:04 agc Exp $ */ +/* $FreeBSD$ */ + +/* + * Copyright (c) 2001 Alistair G. Crooks. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Alistair G. Crooks. + * 4. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. + */ + +#include <sys/cdefs.h> + +#ifndef lint +__COPYRIGHT("@(#) Copyright (c) 2001 \ + The NetBSD Foundation, Inc. All rights reserved."); +__RCSID("$NetBSD: digest.c,v 1.1.1.1 2001/03/06 11:21:04 agc Exp $"); +#endif + +#include <sys/types.h> + +#include <err.h> +#include <locale.h> +#include "md5.h" +#include "rmd160.h" +#include "sha1.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +/* perform an md5 digest, and print the results if successful */ +static int +md5_digest_file(char *fn) +{ + MD5_CTX m; + char in[BUFSIZ * 20]; + char digest[33]; + int cc; + + if (fn == NULL) { + MD5Init(&m); + while ((cc = read(STDIN_FILENO, in, sizeof(in))) > 0) { + MD5Update(&m, (u_char *)in, (unsigned) cc); + } + (void) printf("%s\n", MD5End(&m, digest)); + } else { + if (MD5File(fn, digest) == NULL) { + return 0; + } + (void) printf("MD5 (%s) = %s\n", fn, digest); + } + return 1; +} + +/* perform an sha1 digest, and print the results if successful */ +static int +sha1_digest_file(char *fn) +{ + SHA1_CTX sha; + char in[BUFSIZ * 20]; + char digest[41]; + int cc; + + if (fn == NULL) { + SHA1Init(&sha); + while ((cc = read(STDIN_FILENO, in, sizeof(in))) > 0) { + SHA1Update(&sha, (u_char *)in, (unsigned) cc); + } + (void) printf("%s\n", SHA1End(&sha, digest)); + } else { + if (SHA1File(fn, digest) == NULL) { + return 0; + } + (void) printf("SHA1 (%s) = %s\n", fn, digest); + } + return 1; +} + +/* perform an ripemd160 digest, and print the results if successful */ +static int +rmd160_digest_file(char *fn) +{ + RMD160_CTX rmd; + char in[BUFSIZ * 20]; + char digest[41]; + int cc; + + if (fn == NULL) { + RMD160Init(&rmd); + while ((cc = read(STDIN_FILENO, in, sizeof(in))) > 0) { + RMD160Update(&rmd, (u_char *)in, (unsigned) cc); + } + (void) printf("%s\n", RMD160End(&rmd, digest)); + } else { + if (RMD160File(fn, digest) == NULL) { + return 0; + } + (void) printf("RMD160 (%s) = %s\n", fn, digest); + } + return 1; +} + +/* this struct defines a message digest algorithm */ +typedef struct alg_t { + const char *name; /* algorithm name */ + int (*func)(char *); /* function to call */ +} alg_t; + +/* list of supported message digest algorithms */ +static alg_t algorithms[] = { + { "md5", md5_digest_file }, + { "rmd160", rmd160_digest_file }, + { "sha1", sha1_digest_file }, + { NULL } +}; + +/* find an algorithm, given a name */ +static alg_t * +find_algorithm(const char *a) +{ + alg_t *alg; + + for (alg = algorithms ; alg->name && strcasecmp(alg->name, a) != 0 ; alg++) { + } + return (alg->name) ? alg : NULL; +} + +int +main(int argc, char **argv) +{ + alg_t *alg; + int rval; + int i; + + (void) setlocale(LC_ALL, ""); + while ((i = getopt(argc, argv, "V")) != -1) { + switch(i) { + case 'V': + printf("%d\n", VERSION); + return EXIT_SUCCESS; + } + } + if (argc == optind) { + (void) fprintf(stderr, "Usage: %s algorithm [file...]\n", *argv); + return EXIT_FAILURE; + } + if ((alg = find_algorithm(argv[optind])) == NULL) { + errx(EXIT_FAILURE, "No such algorithm `%s'", argv[optind]); + } + rval = EXIT_SUCCESS; + if (argc == optind + 1) { + if (!(*alg->func)(NULL)) { + warn("stdin"); + rval = EXIT_FAILURE; + } + } else { + for (i = optind + 1 ; i < argc ; i++) { + if (!(*alg->func)(argv[i])) { + warn("%s", argv[i]); + rval = EXIT_FAILURE; + } + } + } + return rval; +} diff --git a/security/digest/files/md5.h b/security/digest/files/md5.h new file mode 100644 index 000000000000..50e13e2a6015 --- /dev/null +++ b/security/digest/files/md5.h @@ -0,0 +1,55 @@ +/* $NetBSD: md5.h,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ + +/* + * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest + * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.ORG> + * for portability and formatting. + */ + +/* + * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + * rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD5 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD5 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * 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. + */ + +#ifndef _SYS_MD5_H_ +#define _SYS_MD5_H_ + +#include <sys/types.h> + +/* MD5 context. */ +typedef struct MD5Context { + u_int32_t state[4]; /* state (ABCD) */ + u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} MD5_CTX; + +__BEGIN_DECLS +void MD5Init __P((MD5_CTX *)); +void MD5Update __P((MD5_CTX *, const unsigned char *, unsigned int)); +void MD5Final __P((unsigned char[16], MD5_CTX *)); +#ifndef _KERNEL +char *MD5End __P((MD5_CTX *, char *)); +char *MD5File __P((const char *, char *)); +char *MD5Data __P((const unsigned char *, unsigned int, char *)); +#endif /* _KERNEL */ +__END_DECLS + +#endif /* _SYS_MD5_H_ */ diff --git a/security/digest/files/md5c.c b/security/digest/files/md5c.c new file mode 100644 index 000000000000..78ed92c5b5cd --- /dev/null +++ b/security/digest/files/md5c.c @@ -0,0 +1,361 @@ +/* $NetBSD: md5c.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $FreeBSD$ */ + +/* + * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest + * Algorithm and has been modifed by Jason R. Thorpe <thorpej@NetBSD.ORG> + * for portability and formatting. + */ + +/* + * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + * rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD5 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD5 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * 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. + */ + +#if defined(_KERNEL) || defined(_STANDALONE) +#include <lib/libkern/libkern.h> +#include <sys/param.h> +#include <sys/md5.h> +#define _DIAGASSERT(x) (void)0 +#else +/* #include "namespace.h" */ +#include <sys/types.h> +#include <assert.h> +#include <string.h> +#include "md5.h" +#endif /* _KERNEL || _STANDALONE */ + +#define ZEROIZE(d, l) memset((d), 0, (l)) + +typedef unsigned char *POINTER; +typedef u_int16_t UINT2; +typedef u_int32_t UINT4; + +/* + * Constants for MD5Transform routine. + */ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +#if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias) +__weak_alias(MD5Init,_MD5Init) +__weak_alias(MD5Update,_MD5Update) +__weak_alias(MD5Final,_MD5Final) +#endif + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +static void MD5Transform __P((UINT4 [4], const unsigned char [64])); + +static void Encode __P((unsigned char *, UINT4 *, unsigned int)); +static void Decode __P((UINT4 *, const unsigned char *, unsigned int)); + +/* + * Encodes input (UINT4) into output (unsigned char). Assumes len is + * a multiple of 4. + */ +static void +Encode (output, input, len) + unsigned char *output; + UINT4 *input; + unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* + * Decodes input (unsigned char) into output (UINT4). Assumes len is + * a multiple of 4. + */ +static void +Decode (output, input, len) + UINT4 *output; + const unsigned char *input; + unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +static const unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* + * F, G, H and I are basic MD5 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* + * ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* + * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. + * Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ +} + +/* + * MD5 initialization. Begins an MD5 operation, writing a new context. + */ +void +MD5Init(context) + MD5_CTX *context; /* context */ +{ + + _DIAGASSERT(context != 0); + + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* + * MD5 block update operation. Continues an MD5 message-digest + * operation, processing another message block, and updating the + * context. + */ +void +MD5Update(context, input, inputLen) + MD5_CTX *context; /* context */ + const unsigned char *input; /* input block */ + unsigned int inputLen; /* length of input block */ +{ + unsigned int i, idx, partLen; + + _DIAGASSERT(context != 0); + _DIAGASSERT(input != 0); + + /* Compute number of bytes mod 64 */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - idx; + + /* Transform as many times as possible. */ + if (inputLen >= partLen) { + /* LINTED const castaway ok */ + memcpy((POINTER)&context->buffer[idx], + input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); + + idx = 0; + } else + i = 0; + + /* Buffer remaining input */ + /* LINTED const castaway ok */ + memcpy((POINTER)&context->buffer[idx], &input[i], + inputLen - i); +} + +/* + * MD5 finalization. Ends an MD5 message-digest operation, writing the + * message digest and zeroing the context. + */ +void +MD5Final(digest, context) + unsigned char digest[16]; /* message digest */ + MD5_CTX *context; /* context */ +{ + unsigned char bits[8]; + unsigned int idx, padLen; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + /* Save number of bits */ + Encode(bits, context->count, 8); + + /* Pad out to 56 mod 64. */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); + MD5Update (context, PADDING, padLen); + + /* Append length (before padding) */ + MD5Update(context, bits, 8); + + /* Store state in digest */ + Encode(digest, context->state, 16); + + /* Zeroize sensitive information. */ + ZEROIZE((POINTER)(void *)context, sizeof(*context)); +} + +/* + * MD5 basic transformation. Transforms state based on block. + */ +static void +MD5Transform(state, block) + UINT4 state[4]; + const unsigned char block[64]; +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode(x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. */ + ZEROIZE((POINTER)(void *)x, sizeof (x)); +} diff --git a/security/digest/files/md5hl.c b/security/digest/files/md5hl.c new file mode 100644 index 000000000000..8f7d21f28537 --- /dev/null +++ b/security/digest/files/md5hl.c @@ -0,0 +1,117 @@ +/* $NetBSD: md5hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $FreeBSD$ */ + +/* + * Written by Jason R. Thorpe <thorpej@netbsd.org>, April 29, 1997. + * Public domain. + */ + +#define MDALGORITHM MD5 + +/* #include "namespace.h" */ +#include "md5.h" + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +/* $NetBSD: md5hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * + * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp + */ + +/* + * Modifed April 29, 1997 by Jason R. Thorpe <thorpej@netbsd.org> + */ + +#include <sys/types.h> + +#include <assert.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define CONCAT(x,y) __CONCAT(x,y) +#define MDNAME(x) CONCAT(MDALGORITHM,x) + +char * +MDNAME(End)(ctx, buf) + MDNAME(_CTX) *ctx; + char *buf; +{ + int i; + unsigned char digest[16]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != 0); + + if (buf == NULL) + buf = malloc(33); + if (buf == NULL) + return (NULL); + + MDNAME(Final)(digest, ctx); + + for (i = 0; i < 16; i++) { + buf[i+i] = hex[(u_int32_t)digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; + } + + buf[i+i] = '\0'; + return (buf); +} + +char * +MDNAME(File)(filename, buf) + const char *filename; + char *buf; +{ + unsigned char buffer[BUFSIZ]; + MDNAME(_CTX) ctx; + int f, i, j; + + _DIAGASSERT(filename != 0); + /* buf may be NULL */ + + MDNAME(Init)(&ctx); + f = open(filename, O_RDONLY, 0666); + if (f < 0) + return NULL; + + while ((i = read(f, buffer, sizeof(buffer))) > 0) + MDNAME(Update)(&ctx, buffer, (unsigned int)i); + + j = errno; + close(f); + errno = j; + + if (i < 0) + return NULL; + + return (MDNAME(End)(&ctx, buf)); +} + +char * +MDNAME(Data)(data, len, buf) + const unsigned char *data; + unsigned int len; + char *buf; +{ + MDNAME(_CTX) ctx; + + _DIAGASSERT(data != 0); + + MDNAME(Init)(&ctx); + MDNAME(Update)(&ctx, data, len); + return (MDNAME(End)(&ctx, buf)); +} diff --git a/security/digest/files/rmd160.c b/security/digest/files/rmd160.c new file mode 100644 index 000000000000..6ff032670a60 --- /dev/null +++ b/security/digest/files/rmd160.c @@ -0,0 +1,463 @@ +/* $NetBSD: rmd160.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $FreeBSD$ */ + +/********************************************************************\ + * + * FILE: rmd160.c + * + * CONTENTS: A sample C-implementation of the RIPEMD-160 + * hash-function. + * TARGET: any computer with an ANSI C compiler + * + * AUTHOR: Antoon Bosselaers, ESAT-COSIC + * (Arranged for libc by Todd C. Miller) + * DATE: 1 March 1996 + * VERSION: 1.0 + * + * Copyright (c) Katholieke Universiteit Leuven + * 1996, All Rights Reserved + * +\********************************************************************/ + +#include <sys/cdefs.h> +#ifndef lint +__RCSID("$NetBSD: rmd160.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); +#endif /* not lint */ + +/* header files */ +#include <sys/types.h> + +#ifdef HAVE_SYS_ENDIAN_H_ +#include <sys/endian.h> +#endif + +#ifdef HAVE_MACHINE_ENDIAN_H_ +#include <machine/endian.h> +#endif + +/* #include "namespace.h" */ + +#include <assert.h> +#include "rmd160.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +#if !defined(_KERNEL) && defined(__weak_alias) +__weak_alias(RMD160Transform,_RMD160Transform) +__weak_alias(RMD160Init,_RMD160Init) +__weak_alias(RMD160Update,_RMD160Update) +__weak_alias(RMD160Final,_RMD160Final) +#endif + +/********************************************************************/ + +/* macro definitions */ + +/* collect four bytes into one word: */ +#define BYTES_TO_DWORD(strptr) \ + (((u_int32_t) *((strptr)+3) << 24) | \ + ((u_int32_t) *((strptr)+2) << 16) | \ + ((u_int32_t) *((strptr)+1) << 8) | \ + ((u_int32_t) *(strptr))) + +/* ROL(x, n) cyclically rotates x over n bits to the left */ +/* x must be of an unsigned 32 bits type and 0 <= n < 32. */ +#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* the three basic functions F(), G() and H() */ +#define F(x, y, z) ((x) ^ (y) ^ (z)) +#define G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define H(x, y, z) (((x) | ~(y)) ^ (z)) +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define J(x, y, z) ((x) ^ ((y) | ~(z))) + +/* the eight basic operations FF() through III() */ +#define FF(a, b, c, d, e, x, s) { \ + (a) += F((b), (c), (d)) + (x); \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define GG(a, b, c, d, e, x, s) { \ + (a) += G((b), (c), (d)) + (x) + 0x5a827999U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define HH(a, b, c, d, e, x, s) { \ + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define II(a, b, c, d, e, x, s) { \ + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define JJ(a, b, c, d, e, x, s) { \ + (a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define FFF(a, b, c, d, e, x, s) { \ + (a) += F((b), (c), (d)) + (x); \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define GGG(a, b, c, d, e, x, s) { \ + (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define HHH(a, b, c, d, e, x, s) { \ + (a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define III(a, b, c, d, e, x, s) { \ + (a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} +#define JJJ(a, b, c, d, e, x, s) { \ + (a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \ + (a) = ROL((a), (s)) + (e); \ + (c) = ROL((c), 10); \ +} + +/********************************************************************/ + +void +RMD160Init(RMD160_CTX *context) +{ + + _DIAGASSERT(context != NULL); + + /* ripemd-160 initialization constants */ + context->state[0] = 0x67452301U; + context->state[1] = 0xefcdab89U; + context->state[2] = 0x98badcfeU; + context->state[3] = 0x10325476U; + context->state[4] = 0xc3d2e1f0U; + context->length[0] = context->length[1] = 0; + context->buflen = 0; +} + +/********************************************************************/ + +void +RMD160Transform(u_int32_t state[5], const u_int32_t block[16]) +{ + u_int32_t aa, bb, cc, dd, ee; + u_int32_t aaa, bbb, ccc, ddd, eee; + + _DIAGASSERT(state != NULL); + _DIAGASSERT(block != NULL); + + aa = aaa = state[0]; + bb = bbb = state[1]; + cc = ccc = state[2]; + dd = ddd = state[3]; + ee = eee = state[4]; + + /* round 1 */ + FF(aa, bb, cc, dd, ee, block[ 0], 11); + FF(ee, aa, bb, cc, dd, block[ 1], 14); + FF(dd, ee, aa, bb, cc, block[ 2], 15); + FF(cc, dd, ee, aa, bb, block[ 3], 12); + FF(bb, cc, dd, ee, aa, block[ 4], 5); + FF(aa, bb, cc, dd, ee, block[ 5], 8); + FF(ee, aa, bb, cc, dd, block[ 6], 7); + FF(dd, ee, aa, bb, cc, block[ 7], 9); + FF(cc, dd, ee, aa, bb, block[ 8], 11); + FF(bb, cc, dd, ee, aa, block[ 9], 13); + FF(aa, bb, cc, dd, ee, block[10], 14); + FF(ee, aa, bb, cc, dd, block[11], 15); + FF(dd, ee, aa, bb, cc, block[12], 6); + FF(cc, dd, ee, aa, bb, block[13], 7); + FF(bb, cc, dd, ee, aa, block[14], 9); + FF(aa, bb, cc, dd, ee, block[15], 8); + + /* round 2 */ + GG(ee, aa, bb, cc, dd, block[ 7], 7); + GG(dd, ee, aa, bb, cc, block[ 4], 6); + GG(cc, dd, ee, aa, bb, block[13], 8); + GG(bb, cc, dd, ee, aa, block[ 1], 13); + GG(aa, bb, cc, dd, ee, block[10], 11); + GG(ee, aa, bb, cc, dd, block[ 6], 9); + GG(dd, ee, aa, bb, cc, block[15], 7); + GG(cc, dd, ee, aa, bb, block[ 3], 15); + GG(bb, cc, dd, ee, aa, block[12], 7); + GG(aa, bb, cc, dd, ee, block[ 0], 12); + GG(ee, aa, bb, cc, dd, block[ 9], 15); + GG(dd, ee, aa, bb, cc, block[ 5], 9); + GG(cc, dd, ee, aa, bb, block[ 2], 11); + GG(bb, cc, dd, ee, aa, block[14], 7); + GG(aa, bb, cc, dd, ee, block[11], 13); + GG(ee, aa, bb, cc, dd, block[ 8], 12); + + /* round 3 */ + HH(dd, ee, aa, bb, cc, block[ 3], 11); + HH(cc, dd, ee, aa, bb, block[10], 13); + HH(bb, cc, dd, ee, aa, block[14], 6); + HH(aa, bb, cc, dd, ee, block[ 4], 7); + HH(ee, aa, bb, cc, dd, block[ 9], 14); + HH(dd, ee, aa, bb, cc, block[15], 9); + HH(cc, dd, ee, aa, bb, block[ 8], 13); + HH(bb, cc, dd, ee, aa, block[ 1], 15); + HH(aa, bb, cc, dd, ee, block[ 2], 14); + HH(ee, aa, bb, cc, dd, block[ 7], 8); + HH(dd, ee, aa, bb, cc, block[ 0], 13); + HH(cc, dd, ee, aa, bb, block[ 6], 6); + HH(bb, cc, dd, ee, aa, block[13], 5); + HH(aa, bb, cc, dd, ee, block[11], 12); + HH(ee, aa, bb, cc, dd, block[ 5], 7); + HH(dd, ee, aa, bb, cc, block[12], 5); + + /* round 4 */ + II(cc, dd, ee, aa, bb, block[ 1], 11); + II(bb, cc, dd, ee, aa, block[ 9], 12); + II(aa, bb, cc, dd, ee, block[11], 14); + II(ee, aa, bb, cc, dd, block[10], 15); + II(dd, ee, aa, bb, cc, block[ 0], 14); + II(cc, dd, ee, aa, bb, block[ 8], 15); + II(bb, cc, dd, ee, aa, block[12], 9); + II(aa, bb, cc, dd, ee, block[ 4], 8); + II(ee, aa, bb, cc, dd, block[13], 9); + II(dd, ee, aa, bb, cc, block[ 3], 14); + II(cc, dd, ee, aa, bb, block[ 7], 5); + II(bb, cc, dd, ee, aa, block[15], 6); + II(aa, bb, cc, dd, ee, block[14], 8); + II(ee, aa, bb, cc, dd, block[ 5], 6); + II(dd, ee, aa, bb, cc, block[ 6], 5); + II(cc, dd, ee, aa, bb, block[ 2], 12); + + /* round 5 */ + JJ(bb, cc, dd, ee, aa, block[ 4], 9); + JJ(aa, bb, cc, dd, ee, block[ 0], 15); + JJ(ee, aa, bb, cc, dd, block[ 5], 5); + JJ(dd, ee, aa, bb, cc, block[ 9], 11); + JJ(cc, dd, ee, aa, bb, block[ 7], 6); + JJ(bb, cc, dd, ee, aa, block[12], 8); + JJ(aa, bb, cc, dd, ee, block[ 2], 13); + JJ(ee, aa, bb, cc, dd, block[10], 12); + JJ(dd, ee, aa, bb, cc, block[14], 5); + JJ(cc, dd, ee, aa, bb, block[ 1], 12); + JJ(bb, cc, dd, ee, aa, block[ 3], 13); + JJ(aa, bb, cc, dd, ee, block[ 8], 14); + JJ(ee, aa, bb, cc, dd, block[11], 11); + JJ(dd, ee, aa, bb, cc, block[ 6], 8); + JJ(cc, dd, ee, aa, bb, block[15], 5); + JJ(bb, cc, dd, ee, aa, block[13], 6); + + /* parallel round 1 */ + JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8); + JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9); + JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9); + JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11); + JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13); + JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15); + JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15); + JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5); + JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7); + JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7); + JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8); + JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11); + JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14); + JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14); + JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12); + JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6); + + /* parallel round 2 */ + III(eee, aaa, bbb, ccc, ddd, block[ 6], 9); + III(ddd, eee, aaa, bbb, ccc, block[11], 13); + III(ccc, ddd, eee, aaa, bbb, block[ 3], 15); + III(bbb, ccc, ddd, eee, aaa, block[ 7], 7); + III(aaa, bbb, ccc, ddd, eee, block[ 0], 12); + III(eee, aaa, bbb, ccc, ddd, block[13], 8); + III(ddd, eee, aaa, bbb, ccc, block[ 5], 9); + III(ccc, ddd, eee, aaa, bbb, block[10], 11); + III(bbb, ccc, ddd, eee, aaa, block[14], 7); + III(aaa, bbb, ccc, ddd, eee, block[15], 7); + III(eee, aaa, bbb, ccc, ddd, block[ 8], 12); + III(ddd, eee, aaa, bbb, ccc, block[12], 7); + III(ccc, ddd, eee, aaa, bbb, block[ 4], 6); + III(bbb, ccc, ddd, eee, aaa, block[ 9], 15); + III(aaa, bbb, ccc, ddd, eee, block[ 1], 13); + III(eee, aaa, bbb, ccc, ddd, block[ 2], 11); + + /* parallel round 3 */ + HHH(ddd, eee, aaa, bbb, ccc, block[15], 9); + HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7); + HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15); + HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11); + HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8); + HHH(ddd, eee, aaa, bbb, ccc, block[14], 6); + HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6); + HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14); + HHH(aaa, bbb, ccc, ddd, eee, block[11], 12); + HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13); + HHH(ddd, eee, aaa, bbb, ccc, block[12], 5); + HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14); + HHH(bbb, ccc, ddd, eee, aaa, block[10], 13); + HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13); + HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7); + HHH(ddd, eee, aaa, bbb, ccc, block[13], 5); + + /* parallel round 4 */ + GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15); + GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5); + GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8); + GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11); + GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14); + GGG(ccc, ddd, eee, aaa, bbb, block[11], 14); + GGG(bbb, ccc, ddd, eee, aaa, block[15], 6); + GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14); + GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6); + GGG(ddd, eee, aaa, bbb, ccc, block[12], 9); + GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12); + GGG(bbb, ccc, ddd, eee, aaa, block[13], 9); + GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12); + GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5); + GGG(ddd, eee, aaa, bbb, ccc, block[10], 15); + GGG(ccc, ddd, eee, aaa, bbb, block[14], 8); + + /* parallel round 5 */ + FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8); + FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5); + FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12); + FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9); + FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12); + FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5); + FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14); + FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6); + FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8); + FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13); + FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6); + FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5); + FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15); + FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13); + FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11); + FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11); + + /* combine results */ + ddd += cc + state[1]; /* final result for state[0] */ + state[1] = state[2] + dd + eee; + state[2] = state[3] + ee + aaa; + state[3] = state[4] + aa + bbb; + state[4] = state[0] + bb + ccc; + state[0] = ddd; +} + +/********************************************************************/ + +void +RMD160Update(RMD160_CTX *context, const u_char *data, u_int32_t nbytes) +{ + u_int32_t X[16]; + u_int32_t ofs = 0; + u_int32_t i; +#if BYTE_ORDER != LITTLE_ENDIAN + u_int32_t j; +#endif + + _DIAGASSERT(context != NULL); + _DIAGASSERT(data != NULL); + + /* update length[] */ + if (context->length[0] + nbytes < context->length[0]) + context->length[1]++; /* overflow to msb of length */ + context->length[0] += nbytes; + + (void)memset(X, 0, sizeof(X)); + + if ( context->buflen + nbytes < 64 ) + { + (void)memcpy(context->bbuffer + context->buflen, data, nbytes); + context->buflen += nbytes; + } + else + { + /* process first block */ + ofs = 64 - context->buflen; + (void)memcpy(context->bbuffer + context->buflen, data, ofs); +#if BYTE_ORDER == LITTLE_ENDIAN + (void)memcpy(X, context->bbuffer, sizeof(X)); +#else + for (j=0; j < 16; j++) + X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j)); +#endif + RMD160Transform(context->state, X); + nbytes -= ofs; + + /* process remaining complete blocks */ + for (i = 0; i < (nbytes >> 6); i++) { +#if BYTE_ORDER == LITTLE_ENDIAN + (void)memcpy(X, data + (64 * i) + ofs, sizeof(X)); +#else + for (j=0; j < 16; j++) + X[j] = BYTES_TO_DWORD(data + (64 * i) + (4 * j) + ofs); +#endif + RMD160Transform(context->state, X); + } + + /* + * Put last bytes from data into context's buffer + */ + context->buflen = nbytes & 63; + memcpy(context->bbuffer, data + (64 * i) + ofs, context->buflen); + } +} + +/********************************************************************/ + +void +RMD160Final(u_char digest[20], RMD160_CTX *context) +{ + u_int32_t i; + u_int32_t X[16]; +#if BYTE_ORDER != LITTLE_ENDIAN + u_int32_t j; +#endif + + _DIAGASSERT(digest != NULL); + _DIAGASSERT(context != NULL); + + /* append the bit m_n == 1 */ + context->bbuffer[context->buflen] = (u_char)'\200'; + + (void)memset(context->bbuffer + context->buflen + 1, 0, + 63 - context->buflen); +#if BYTE_ORDER == LITTLE_ENDIAN + (void)memcpy(X, context->bbuffer, sizeof(X)); +#else + for (j=0; j < 16; j++) + X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j)); +#endif + if ((context->buflen) > 55) { + /* length goes to next block */ + RMD160Transform(context->state, X); + (void)memset(X, 0, sizeof(X)); + } + + /* append length in bits */ + X[14] = context->length[0] << 3; + X[15] = (context->length[0] >> 29) | + (context->length[1] << 3); + RMD160Transform(context->state, X); + + if (digest != NULL) { + for (i = 0; i < 20; i += 4) { + /* extracts the 8 least significant bits. */ + digest[i] = context->state[i>>2]; + digest[i + 1] = (context->state[i>>2] >> 8); + digest[i + 2] = (context->state[i>>2] >> 16); + digest[i + 3] = (context->state[i>>2] >> 24); + } + } +} + +/************************ end of file rmd160.c **********************/ diff --git a/security/digest/files/rmd160.h b/security/digest/files/rmd160.h new file mode 100644 index 000000000000..b183b31ed7c9 --- /dev/null +++ b/security/digest/files/rmd160.h @@ -0,0 +1,49 @@ +/* $NetBSD: rmd160.h,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ + +/********************************************************************\ + * + * FILE: rmd160.h + * + * CONTENTS: Header file for a sample C-implementation of the + * RIPEMD-160 hash-function. + * TARGET: any computer with an ANSI C compiler + * + * AUTHOR: Antoon Bosselaers, ESAT-COSIC + * DATE: 1 March 1996 + * VERSION: 1.0 + * + * Copyright (c) Katholieke Universiteit Leuven + * 1996, All Rights Reserved + * +\********************************************************************/ + +/* + * from OpenBSD: rmd160.h,v 1.4 1999/08/16 09:59:04 millert Exp + */ + +#ifndef _RMD160_H_ +#define _RMD160_H_ + +#include <sys/cdefs.h> +#include <sys/types.h> + +typedef struct { + u_int32_t state[5]; /* state (ABCDE) */ + u_int32_t length[2]; /* number of bits */ + u_char bbuffer[64]; /* overflow buffer */ + u_int32_t buflen; /* number of chars in bbuffer */ +} RMD160_CTX; + +__BEGIN_DECLS +void RMD160Init(RMD160_CTX *); +void RMD160Transform(u_int32_t[5], const u_int32_t[16]); +void RMD160Update(RMD160_CTX *, const u_char *, u_int32_t); +void RMD160Final(u_char[20], RMD160_CTX *); +#ifndef _KERNEL +char *RMD160End(RMD160_CTX *, char *); +char *RMD160File(char *, char *); +char *RMD160Data(const u_char *, size_t, char *); +#endif /* _KERNEL */ +__END_DECLS + +#endif /* !_RMD160_H_ */ diff --git a/security/digest/files/rmd160hl.c b/security/digest/files/rmd160hl.c new file mode 100644 index 000000000000..29e4517ea9a9 --- /dev/null +++ b/security/digest/files/rmd160hl.c @@ -0,0 +1,100 @@ +/* $NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $FreeBSD$ */ + +/* rmd160hl.c + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * + * from OpenBSD: rmd160hl.c,v 1.2 1999/08/17 09:13:12 millert Exp $ + */ + +#include <sys/cdefs.h> +#ifndef lint +__RCSID("$NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); +#endif /* not lint */ + +#include <sys/types.h> + +/* #include "namespace.h" */ + +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include "rmd160.h" +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +#if defined(__weak_alias) +__weak_alias(RMD160End,_RMD160End) +__weak_alias(RMD160File,_RMD160File) +__weak_alias(RMD160Data,_RMD160Data) +#endif + +char * +RMD160End(RMD160_CTX *ctx, char *buf) +{ + int i; + char *p = buf; + u_char digest[20]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != NULL); + /* buf may be NULL */ + + if (p == NULL && (p = malloc(41)) == NULL) + return 0; + + RMD160Final(digest,ctx); + for (i = 0; i < 20; i++) { + p[i + i] = hex[(u_int32_t)digest[i] >> 4]; + p[i + i + 1] = hex[digest[i] & 0x0f]; + } + p[i + i] = '\0'; + return(p); +} + +char * +RMD160File(char *filename, char *buf) +{ + u_char buffer[BUFSIZ]; + RMD160_CTX ctx; + int fd, num, oerrno; + + _DIAGASSERT(filename != NULL); + /* XXX: buf may be NULL ? */ + + RMD160Init(&ctx); + + if ((fd = open(filename, O_RDONLY)) < 0) + return(0); + + while ((num = read(fd, buffer, sizeof(buffer))) > 0) + RMD160Update(&ctx, buffer, (size_t)num); + + oerrno = errno; + close(fd); + errno = oerrno; + return(num < 0 ? 0 : RMD160End(&ctx, buf)); +} + +char * +RMD160Data(const u_char *data, size_t len, char *buf) +{ + RMD160_CTX ctx; + + _DIAGASSERT(data != NULL); + /* XXX: buf may be NULL ? */ + + RMD160Init(&ctx); + RMD160Update(&ctx, data, len); + return(RMD160End(&ctx, buf)); +} diff --git a/security/digest/files/sha1.c b/security/digest/files/sha1.c new file mode 100644 index 000000000000..5e610d3c446d --- /dev/null +++ b/security/digest/files/sha1.c @@ -0,0 +1,284 @@ +/* $NetBSD: sha1.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ +/* $FreeBSD$ */ + +/* + * SHA-1 in C + * By Steve Reid <steve@edmweb.com> + * 100% Public Domain + * + * Test Vectors (from FIPS PUB 180-1) + * "abc" + * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D + * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 + * A million repetitions of "a" + * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + */ + +#define SHA1HANDSOFF /* Copies data before messing with it. */ + +#if defined(_KERNEL) || defined(_STANDALONE) +#include <sys/param.h> +#include <sys/systm.h> +#define _DIAGASSERT(x) (void)0 +#else +/* #include "namespace.h" */ +#include <sys/types.h> +#include <assert.h> +#include <string.h> +#endif + +#include "sha1.h" + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +/* + * XXX Kludge until there is resolution regarding mem*() functions + * XXX in the kernel. + */ +#if defined(_KERNEL) || defined(_STANDALONE) +#define memcpy(s, d, l) bcopy((d), (s), (l)) +#endif + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* + * blk0() and blk() perform the initial expand. + * I got the idea of expanding during the round function from SSLeay + */ +#if BYTE_ORDER == LITTLE_ENDIAN +# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ + |(rol(block->l[i],8)&0x00FF00FF)) +#else +# define blk0(i) block->l[i] +#endif +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ + ^block->l[(i+2)&15]^block->l[i&15],1)) + +/* + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 + */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + + +#if !defined(_KERNEL) && defined(__weak_alias) +__weak_alias(SHA1Transform,_SHA1Transform) +__weak_alias(SHA1Init,_SHA1Init) +__weak_alias(SHA1Update,_SHA1Update) +__weak_alias(SHA1Final,_SHA1Final) +#endif + +typedef union { + u_char c[64]; + u_int l[16]; +} CHAR64LONG16; + +#ifdef __sparc_v9__ +void do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); +void do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); +void do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); +void do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); + +#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) +#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) +#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) +#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) +#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) + +void +do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) +{ + nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3); + nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); + nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); + nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15); + nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); +} + +void +do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) +{ + nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23); + nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); + nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); + nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35); + nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); +} + +void +do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) +{ + nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43); + nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); + nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); + nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55); + nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); +} + +void +do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) +{ + nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); + nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); + nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); + nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); + nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); +} +#endif + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ +void SHA1Transform(state, buffer) + u_int32_t state[5]; + const u_char buffer[64]; +{ + u_int32_t a, b, c, d, e; + CHAR64LONG16 *block; + +#ifdef SHA1HANDSOFF + static u_char workspace[64]; +#endif + + _DIAGASSERT(buffer != 0); + _DIAGASSERT(state != 0); + +#ifdef SHA1HANDSOFF + block = (CHAR64LONG16 *)(void *)workspace; + (void)memcpy(block, buffer, 64); +#else + block = (CHAR64LONG16 *)(void *)buffer; +#endif + + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + +#ifdef __sparc_v9__ + do_R01(&a, &b, &c, &d, &e, block); + do_R2(&a, &b, &c, &d, &e, block); + do_R3(&a, &b, &c, &d, &e, block); + do_R4(&a, &b, &c, &d, &e, block); +#else + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); +#endif + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + + /* Wipe variables */ + a = b = c = d = e = 0; +} + + +/* + * SHA1Init - Initialize new context + */ +void SHA1Init(context) + SHA1_CTX *context; +{ + + _DIAGASSERT(context != 0); + + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* + * Run your data through this. + */ +void SHA1Update(context, data, len) + SHA1_CTX *context; + const u_char *data; + u_int len; +{ + u_int i, j; + + _DIAGASSERT(context != 0); + _DIAGASSERT(data != 0); + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1] += (len>>29)+1; + j = (j >> 3) & 63; + if ((j + len) > 63) { + (void)memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) + SHA1Transform(context->state, &data[i]); + j = 0; + } else { + i = 0; + } + (void)memcpy(&context->buffer[j], &data[i], len - i); +} + + +/* + * Add padding and return the message digest. + */ +void SHA1Final(digest, context) + u_char digest[20]; + SHA1_CTX* context; +{ + u_int i; + u_char finalcount[8]; + + _DIAGASSERT(digest != 0); + _DIAGASSERT(context != 0); + + for (i = 0; i < 8; i++) { + finalcount[i] = (u_char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + SHA1Update(context, (const u_char *)"\200", 1); + while ((context->count[0] & 504) != 448) + SHA1Update(context, (const u_char *)"\0", 1); + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + + if (digest) { + for (i = 0; i < 20; i++) + digest[i] = (u_char) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } +} diff --git a/security/digest/files/sha1.h b/security/digest/files/sha1.h new file mode 100644 index 000000000000..b0ceb5cd83de --- /dev/null +++ b/security/digest/files/sha1.h @@ -0,0 +1,30 @@ +/* $NetBSD: sha1.h,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ + +/* + * SHA-1 in C + * By Steve Reid <steve@edmweb.com> + * 100% Public Domain + */ + +#ifndef _SYS_SHA1_H_ +#define _SYS_SHA1_H_ + +#include <sys/types.h> + +typedef struct { + u_int32_t state[5]; + u_int32_t count[2]; + u_char buffer[64]; +} SHA1_CTX; + +void SHA1Transform __P((u_int32_t state[5], const u_char buffer[64])); +void SHA1Init __P((SHA1_CTX *context)); +void SHA1Update __P((SHA1_CTX *context, const u_char *data, u_int len)); +void SHA1Final __P((u_char digest[20], SHA1_CTX *context)); +#ifndef _KERNEL +char *SHA1End __P((SHA1_CTX *, char *)); +char *SHA1File __P((char *, char *)); +char *SHA1Data __P((const u_char *, size_t, char *)); +#endif /* _KERNEL */ + +#endif /* _SYS_SHA1_H_ */ diff --git a/security/digest/files/sha1hl.c b/security/digest/files/sha1hl.c new file mode 100644 index 000000000000..fc373141ac5b --- /dev/null +++ b/security/digest/files/sha1hl.c @@ -0,0 +1,107 @@ +/* $NetBSD: sha1hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */ +/* $FreeBSD$ */ + +/* sha1hl.c + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + */ + +/* #include "namespace.h" */ + +#include <sys/cdefs.h> +#include <sys/file.h> +#include <sys/types.h> +#include <sys/uio.h> + +#include <assert.h> +#include <errno.h> +#include "sha1.h" +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha1hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef _DIAGASSERT +#define _DIAGASSERT(cond) assert(cond) +#endif + +#if defined(__weak_alias) +__weak_alias(SHA1End,_SHA1End) +__weak_alias(SHA1File,_SHA1File) +__weak_alias(SHA1Data,_SHA1Data) +#endif + +/* ARGSUSED */ +char * +SHA1End(ctx, buf) + SHA1_CTX *ctx; + char *buf; +{ + int i; + char *p = buf; + u_char digest[20]; + static const char hex[]="0123456789abcdef"; + + _DIAGASSERT(ctx != NULL); + /* buf may be NULL */ + + if (p == NULL && (p = malloc(41)) == NULL) + return 0; + + SHA1Final(digest,ctx); + for (i = 0; i < 20; i++) { + p[i + i] = hex[((u_int32_t)digest[i]) >> 4]; + p[i + i + 1] = hex[digest[i] & 0x0f]; + } + p[i + i] = '\0'; + return(p); +} + +char * +SHA1File (filename, buf) + char *filename; + char *buf; +{ + u_char buffer[BUFSIZ]; + SHA1_CTX ctx; + int fd, num, oerrno; + + _DIAGASSERT(filename != NULL); + /* XXX: buf may be NULL ? */ + + SHA1Init(&ctx); + + if ((fd = open(filename,O_RDONLY)) < 0) + return(0); + + while ((num = read(fd, buffer, sizeof(buffer))) > 0) + SHA1Update(&ctx, buffer, (size_t)num); + + oerrno = errno; + close(fd); + errno = oerrno; + return(num < 0 ? 0 : SHA1End(&ctx, buf)); +} + +char * +SHA1Data (data, len, buf) + const u_char *data; + size_t len; + char *buf; +{ + SHA1_CTX ctx; + + _DIAGASSERT(data != NULL); + /* XXX: buf may be NULL ? */ + + SHA1Init(&ctx); + SHA1Update(&ctx, data, len); + return(SHA1End(&ctx, buf)); +} diff --git a/security/digest/pkg-comment b/security/digest/pkg-comment new file mode 100644 index 000000000000..60156bec524c --- /dev/null +++ b/security/digest/pkg-comment @@ -0,0 +1 @@ +MD5/SHA-1/RIPEMD-160 checksumming utility diff --git a/security/digest/pkg-descr b/security/digest/pkg-descr new file mode 100644 index 000000000000..7f61012813bd --- /dev/null +++ b/security/digest/pkg-descr @@ -0,0 +1,6 @@ +This is a utility which calculates MD5 (RFC 1321), SHA-1, and +RIPEMD-160 (RMD160) hashes (also known as message digests, checksums, +or "fingerprints"). + +Trevor Johnson +trevor@FreeBSD.org |