From 767c8fdd864db5101eb5baf093f0504387bafd36 Mon Sep 17 00:00:00 2001 From: elad Date: Sat, 24 Sep 2005 19:04:52 +0000 Subject: [PATCH] Organize hashing in libc. --- lib/libc/hash/Makefile.inc | 21 +-- lib/libc/hash/md5/Makefile.inc | 6 + lib/libc/hash/{ => md5}/hmac_md5.c | 8 +- lib/libc/hash/rmd160.3 | 225 --------------------------- lib/libc/hash/rmd160/Makefile.inc | 13 ++ lib/libc/hash/{ => rmd160}/rmd160.c | 2 +- lib/libc/hash/rmd160/rmd160hl.c | 23 +++ lib/libc/hash/rmd160hl.c | 103 ------------ lib/libc/hash/sha1.3 | 216 ------------------------- lib/libc/hash/sha1/Makefile.inc | 12 ++ lib/libc/hash/{ => sha1}/hmac_sha1.c | 8 +- lib/libc/hash/{ => sha1}/sha1.c | 4 +- lib/libc/hash/sha1/sha1hl.c | 23 +++ lib/libc/hash/sha1hl.c | 110 ------------- 14 files changed, 93 insertions(+), 681 deletions(-) create mode 100644 lib/libc/hash/md5/Makefile.inc rename lib/libc/hash/{ => md5}/hmac_md5.c (65%) delete mode 100644 lib/libc/hash/rmd160.3 create mode 100644 lib/libc/hash/rmd160/Makefile.inc rename lib/libc/hash/{ => rmd160}/rmd160.c (99%) create mode 100644 lib/libc/hash/rmd160/rmd160hl.c delete mode 100644 lib/libc/hash/rmd160hl.c delete mode 100644 lib/libc/hash/sha1.3 create mode 100644 lib/libc/hash/sha1/Makefile.inc rename lib/libc/hash/{ => sha1}/hmac_sha1.c (64%) rename lib/libc/hash/{ => sha1}/sha1.c (98%) create mode 100644 lib/libc/hash/sha1/sha1hl.c delete mode 100644 lib/libc/hash/sha1hl.c diff --git a/lib/libc/hash/Makefile.inc b/lib/libc/hash/Makefile.inc index 89fd390e8de7..f81f97c758a4 100644 --- a/lib/libc/hash/Makefile.inc +++ b/lib/libc/hash/Makefile.inc @@ -1,22 +1,11 @@ -# $NetBSD: Makefile.inc,v 1.8 2005/08/23 17:49:50 elad Exp $ +# $NetBSD: Makefile.inc,v 1.9 2005/09/24 19:04:52 elad Exp $ # $OpenBSD: Makefile.inc,v 1.5 1997/07/17 06:02:42 millert Exp $ # hash functions .PATH: ${ARCHDIR}/hash ${.CURDIR}/hash -SRCS+= rmd160.c rmd160hl.c sha1.c sha1hl.c -SRCS+= hmac_sha1.c hmac_md5.c - -MAN+= sha1.3 rmd160.3 - -MLINKS+=sha1.3 SHA1Init.3 sha1.3 SHA1Update.3 -MLINKS+=sha1.3 SHA1Final.3 sha1.3 SHA1Transform.3 -MLINKS+=sha1.3 SHA1End.3 sha1.3 SHA1File.3 -MLINKS+=sha1.3 SHA1Data.3 - -MLINKS+=rmd160.3 RMD160Init.3 rmd160.3 RMD160Update.3 -MLINKS+=rmd160.3 RMD160Final.3 rmd160.3 RMD160Transform.3 -MLINKS+=rmd160.3 RMD160End.3 rmd160.3 RMD160File.3 -MLINKS+=rmd160.3 RMD160Data.3 - +.include "${.CURDIR}/hash/md5/Makefile.inc" +.include "${.CURDIR}/hash/rmd160/Makefile.inc" +.include "${.CURDIR}/hash/sha1/Makefile.inc" .include "${.CURDIR}/hash/sha2/Makefile.inc" + diff --git a/lib/libc/hash/md5/Makefile.inc b/lib/libc/hash/md5/Makefile.inc new file mode 100644 index 000000000000..0d3959bdbffd --- /dev/null +++ b/lib/libc/hash/md5/Makefile.inc @@ -0,0 +1,6 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/24 19:04:52 elad Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/md5 + +SRCS+= hmac_md5.c diff --git a/lib/libc/hash/hmac_md5.c b/lib/libc/hash/md5/hmac_md5.c similarity index 65% rename from lib/libc/hash/hmac_md5.c rename to lib/libc/hash/md5/hmac_md5.c index 8f6b5feb96e8..d7588d76540f 100644 --- a/lib/libc/hash/hmac_md5.c +++ b/lib/libc/hash/md5/hmac_md5.c @@ -1,4 +1,4 @@ -/* $NetBSD: hmac_md5.c,v 1.2 2005/02/09 21:35:46 kleink Exp $ */ +/* $NetBSD: hmac_md5.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ /* * hmac_md5 - using HMAC from RFC 2104 @@ -6,16 +6,16 @@ #include "namespace.h" #include -#include +#include /* XXX */ #define HMAC_HASH MD5 #define HMAC_FUNC hmac_md5 #define HMAC_KAT hmac_kat_md5 -#define HASH_LENGTH 16 +#define HASH_LENGTH MD5_DIGEST_LENGTH #define HASH_CTX MD5_CTX #define HASH_Init MD5Init #define HASH_Update MD5Update #define HASH_Final MD5Final -#include "hmac.c" +#include "../hmac.c" diff --git a/lib/libc/hash/rmd160.3 b/lib/libc/hash/rmd160.3 deleted file mode 100644 index 47528543420c..000000000000 --- a/lib/libc/hash/rmd160.3 +++ /dev/null @@ -1,225 +0,0 @@ -.\" $NetBSD: rmd160.3,v 1.7 2003/04/16 13:34:40 wiz Exp $ -.\" $OpenBSD: rmd160.3,v 1.12 2000/04/18 03:01:29 aaron Exp $ -.\" -.\" Copyright (c) 1997 Todd C. Miller -.\" 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. 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 ``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. -.\" -.\" See http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html -.\" for detailed information about RIPEMD-160. -.\" -.Dd July 16, 1997 -.Dt RMD160 3 -.Os -.Sh NAME -.Nm RMD160Init , -.Nm RMD160Update , -.Nm RMD160Final , -.Nm RMD160Transform , -.Nm RMD160End , -.Nm RMD160File , -.Nm RMD160Data -.Nd calculate the ``RIPEMD-160'' message digest -.Sh SYNOPSIS -.In sys/types.h -.In rmd160.h -.Ft void -.Fn RMD160Init "RMD160_CTX *context" -.Ft void -.Fn RMD160Update "RMD160_CTX *context" "const u_char *data" "u_int nbytes" -.Ft void -.Fn RMD160Final "u_char digest[20]" "RMD160_CTX *context" -.Ft void -.Fn RMD160Transform "u_int32_t state[5]" "const u_int32_t block[16]" -.Ft "char *" -.Fn RMD160End "RMD160_CTX *context" "char *buf" -.Ft "char *" -.Fn RMD160File "char *filename" "char *buf" -.Ft "char *" -.Fn RMD160Data "u_char *data" "size_t len" "char *buf" -.Sh DESCRIPTION -The RMD160 functions implement the 160-bit RIPE message digest hash algorithm -(RMD-160). -RMD-160 is used to generate a condensed representation -of a message called a message digest. -The algorithm takes a -message less than 2^64 bits as input and produces a 160-bit digest -suitable for use as a digital signature. -.Pp -The RMD160 functions are considered to be more secure than the -.Xr md4 3 -and -.Xr md5 3 -functions and at least as secure as the -.Xr sha1 3 -function. -All share a similar interface. -.Pp -The -.Fn RMD160Init -function initializes a RMD160_CTX -.Ar context -for use with -.Fn RMD160Update , -and -.Fn RMD160Final . -The -.Fn RMD160Update -function adds -.Ar data -of length -.Ar nbytes -to the RMD160_CTX specified by -.Ar context . -.Fn RMD160Final -is called when all data has been added via -.Fn RMD160Update -and stores a message digest in the -.Ar digest -parameter. -When a null pointer is passed to -.Fn RMD160Final -as first argument only the final padding will be applied and the -current context can still be used with -.Fn RMD160Update . -.Pp -The -.Fn RMD160Transform -function is used by -.Fn RMD160Update -to hash 512-bit blocks and forms the core of the algorithm. -Most programs should use the interface provided by -.Fn RMD160Init , -.Fn RMD160Update -and -.Fn RMD160Final -instead of calling -.Fn RMD160Transform -directly. -.Pp -The -.Fn RMD160End -function is a front end for -.Fn RMD160Final -which converts the digest into an -.Tn ASCII -representation of the 160 bit digest in hexadecimal. -.Pp -The -.Fn RMD160File -function calculates the digest for a file and returns the result via -.Fn RMD160End . -If -.Fn RMD160File -is unable to open the file a NULL pointer is returned. -.Pp -The -.Fn RMD160Data -function -calculates the digest of an arbitrary string and returns the result via -.Fn RMD160End . -.Pp -For each of the -.Fn RMD160End , -.Fn RMD160File , -and -.Fn RMD160Data -functions the -.Ar buf -parameter should either be a string of at least 41 characters in -size or a NULL pointer. -In the latter case, space will be dynamically allocated via -.Xr malloc 3 -and should be freed using -.Xr free 3 -when it is no longer needed. -.Sh EXAMPLES -The follow code fragment will calculate the digest for -the string "abc" which is ``0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc''. -.Bd -literal -offset indent -RMD160_CTX rmd; -u_char results[20]; -char *buf; -int n; - -buf = "abc"; -n = strlen(buf); -RMD160Init(\*[Am]rmd); -RMD160Update(\*[Am]rmd, (u_char *)buf, n); -RMD160Final(results, \*[Am]rmd); - -/* Print the digest as one long hex value */ -printf("0x"); -for (n = 0; n \*[Lt] 20; n++) - printf("%02x", results[n]); -putchar('\\n'); -.Ed -.Pp -Alternately, the helper functions could be used in the following way: -.Bd -literal -offset indent -RMD160_CTX rmd; -u_char output[41]; -char *buf = "abc"; - -printf("0x%s\en", RMD160Data(buf, strlen(buf), output)); -.Ed -.Sh SEE ALSO -.Xr rmd160 1 , -.Xr md4 3 , -.Xr md5 3 , -.Xr sha1 3 -.Pp -.Rs -.%A H. Dobbertin, A. Bosselaers, B. Preneel -.%T RIPEMD-160, a strengthened version of RIPEMD -.Re -.Rs -.%T Information technology - Security techniques - Hash-functions - Part 3: Dedicated hash-functions -.%O ISO/IEC 10118-3 -.Re -.Rs -.%A H. Dobbertin, A. Bosselaers, B. Preneel -.%T The RIPEMD-160 cryptographic hash function -.%J Dr. Dobb's Journal -.%V Vol. 22, No. 1 -.%D January 1997 -.%P pp. 24-28 -.Re -.Sh HISTORY -The RMD-160 functions appeared in -.Ox 2.1 . -.Sh AUTHORS -This implementation of RMD-160 was written by Antoon Bosselaers. -.Pp -The -.Fn RMD160End , -.Fn RMD160File , -and -.Fn RMD160Data -helper functions are derived from code written by Poul-Henning Kamp. -.Sh BUGS -If a message digest is to be copied to a multi-byte type (ie: -an array of five 32-bit integers) it will be necessary to -perform byte swapping on little endian machines such as the i386, alpha, -and VAX. diff --git a/lib/libc/hash/rmd160/Makefile.inc b/lib/libc/hash/rmd160/Makefile.inc new file mode 100644 index 000000000000..0caf265e0b8d --- /dev/null +++ b/lib/libc/hash/rmd160/Makefile.inc @@ -0,0 +1,13 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/24 19:04:52 elad Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/rmd160 + +SRCS+= rmd160.c rmd160hl.c + +MAN+= rmd160.3 + +MLINKS+=rmd160.3 RMD160Init.3 rmd160.3 RMD160Update.3 +MLINKS+=rmd160.3 RMD160Final.3 rmd160.3 RMD160Transform.3 +MLINKS+=rmd160.3 RMD160End.3 rmd160.3 RMD160File.3 +MLINKS+=rmd160.3 RMD160Data.3 diff --git a/lib/libc/hash/rmd160.c b/lib/libc/hash/rmd160/rmd160.c similarity index 99% rename from lib/libc/hash/rmd160.c rename to lib/libc/hash/rmd160/rmd160.c index 3b07bcd522c2..193ba107472e 100644 --- a/lib/libc/hash/rmd160.c +++ b/lib/libc/hash/rmd160/rmd160.c @@ -1,4 +1,4 @@ -/* $NetBSD: rmd160.c,v 1.9 2005/09/24 18:43:17 elad Exp $ */ +/* $NetBSD: rmd160.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ /* $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ /* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */ /* diff --git a/lib/libc/hash/rmd160/rmd160hl.c b/lib/libc/hash/rmd160/rmd160hl.c new file mode 100644 index 000000000000..a9fc67184176 --- /dev/null +++ b/lib/libc/hash/rmd160/rmd160hl.c @@ -0,0 +1,23 @@ +/* $NetBSD: rmd160hl.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ + +/* + * Derived from code ritten by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: rmd160hl.c,v 1.1 2005/09/24 19:04:52 elad Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM RMD160 + +#include "namespace.h" +#include /* XXX */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include "../hash.c" diff --git a/lib/libc/hash/rmd160hl.c b/lib/libc/hash/rmd160hl.c deleted file mode 100644 index f34234c2b0cd..000000000000 --- a/lib/libc/hash/rmd160hl.c +++ /dev/null @@ -1,103 +0,0 @@ -/* $NetBSD: rmd160hl.c,v 1.8 2003/10/27 00:12:42 lukem Exp $ */ - -/* rmd160hl.c - * ---------------------------------------------------------------------------- - * "THE BEER-WARE LICENSE" (Revision 42): - * 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 $ - */ - -#if HAVE_NBTOOL_CONFIG_H -#include "nbtool_config.h" -#endif - -#include -#ifndef lint -__RCSID("$NetBSD: rmd160hl.c,v 1.8 2003/10/27 00:12:42 lukem Exp $"); -#endif /* not lint */ - -#include - -#include "namespace.h" - -#include -#include -#include -#include -#include -#include -#include - -#if !HAVE_RMD160_H - -#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)); -} - -#endif /* HAVE_RMD160_H */ diff --git a/lib/libc/hash/sha1.3 b/lib/libc/hash/sha1.3 deleted file mode 100644 index e823f5f8bcee..000000000000 --- a/lib/libc/hash/sha1.3 +++ /dev/null @@ -1,216 +0,0 @@ -.\" $NetBSD: sha1.3,v 1.13 2003/04/16 13:34:40 wiz Exp $ -.\" $OpenBSD: sha1.3,v 1.9 1998/03/07 22:18:12 millert Exp $ -.\" -.\" Copyright (c) 1997 Todd C. Miller -.\" 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 Todd C. Miller. -.\" 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 ``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. -.\" -.\" See http://csrc.nist.gov/fips/fip180-1.txt for the detailed standard -.\" -.Dd July 10, 1997 -.Dt SHA1 3 -.Sh NAME -.Nm SHA1Init , -.Nm SHA1Update , -.Nm SHA1Final , -.Nm SHA1Transform , -.Nm SHA1End , -.Nm SHA1File , -.Nm SHA1Data -.Nd calculate the NIST Secure Hash Algorithm -.Sh SYNOPSIS -.In sys/types.h -.In sha1.h -.Ft void -.Fn SHA1Init "SHA1_CTX *context" -.Ft void -.Fn SHA1Update "SHA1_CTX *context" "const u_char *data" "u_int len" -.Ft void -.Fn SHA1Final "u_char digest[20]" "SHA1_CTX *context" -.Ft void -.Fn SHA1Transform "u_int32_t state[5]" "u_char buffer[64]" -.Ft "char *" -.Fn SHA1End "SHA1_CTX *context" "char *buf" -.Ft "char *" -.Fn SHA1File "char *filename" "char *buf" -.Ft "char *" -.Fn SHA1Data "u_char *data" "size_t len" "char *buf" -.Sh DESCRIPTION -The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1), -FIPS PUB 180-1. -SHA-1 is used to generate a condensed representation -of a message called a message digest. -The algorithm takes a -message less than 2^64 bits as input and produces a 160-bit digest -suitable for use as a digital signature. -.Pp -The SHA1 functions are considered to be more secure than the -.Xr md4 3 -and -.Xr md5 3 -functions with which they share a similar interface. -.Pp -The -.Fn SHA1Init -function initializes a SHA1_CTX -.Ar context -for use with -.Fn SHA1Update , -and -.Fn SHA1Final . -The -.Fn SHA1Update -function adds -.Ar data -of length -.Ar len -to the SHA1_CTX specified by -.Ar context . -.Fn SHA1Final -is called when all data has been added via -.Fn SHA1Update -and stores a message digest in the -.Ar digest -parameter. -When a null pointer is passed to -.Fn SHA1Final -as first argument only the final padding will be applied and the -current context can still be used with -.Fn SHA1Update . -.Pp -The -.Fn SHA1Transform -function is used by -.Fn SHA1Update -to hash 512-bit blocks and forms the core of the algorithm. -Most programs should use the interface provided by -.Fn SHA1Init , -.Fn SHA1Update -and -.Fn SHA1Final -instead of calling -.Fn SHA1Transform -directly. -.Pp -The -.Fn SHA1End -function is a front end for -.Fn SHA1Final -which converts the digest into an -.Tn ASCII -representation of the 160 bit digest in hexadecimal. -.Pp -The -.Fn SHA1File -function calculates the digest for a file and returns the result via -.Fn SHA1End . -If -.Fn SHA1File -is unable to open the file a NULL pointer is returned. -.Pp -The -.Fn SHA1Data -function -calculates the digest of an arbitrary string and returns the result via -.Fn SHA1End . -.Pp -For each of the -.Fn SHA1End , -.Fn SHA1File , -and -.Fn SHA1Data -functions the -.Ar buf -parameter should either be a string of at least 41 characters in -size or a NULL pointer. -In the latter case, space will be dynamically -allocated via -.Xr malloc 3 -and should be freed using -.Xr free 3 -when it is no longer needed. -.Sh EXAMPLES -The follow code fragment will calculate the digest for -the string "abc" which is ``0xa9993e36476816aba3e25717850c26c9cd0d89d''. -.Bd -literal -offset indent -SHA1_CTX sha; -u_char results[20]; -char *buf; -int n; - -buf = "abc"; -n = strlen(buf); -SHA1Init(\*[Am]sha); -SHA1Update(\*[Am]sha, (u_char *)buf, n); -SHA1Final(results, \*[Am]sha); - -/* Print the digest as one long hex value */ -printf("0x"); -for (n = 0; n \*[Lt] 20; n++) - printf("%02x", results[n]); -putchar('\\n'); -.Ed -.Pp -Alternately, the helper functions could be used in the following way: -.Bd -literal -offset indent -SHA1_CTX sha; -u_char output[41]; -char *buf = "abc"; - -printf("0x%s", SHA1Data(buf, strlen(buf), output)); -.Ed -.Sh SEE ALSO -.\" .Xr sha1 1 , -.Xr md5 1 , -.Xr md4 3 , -.Xr md5 3 -.Pp -.Rs -.%A J. Burrows -.%T The Secure Hash Standard -.%O FIPS PUB 180-1 -.Re -.Sh HISTORY -The SHA-1 functions appeared in -.Nx 1.4 . -.Sh AUTHORS -This implementation of SHA-1 was written by Steve Reid. -.Pp -The -.Fn SHA1End , -.Fn SHA1File , -and -.Fn SHA1Data -helper functions are derived from code written by Poul-Henning Kamp. -.Sh BUGS -This implementation of SHA-1 has not been validated by NIST -and as such is not in official compliance with the standard. -.Pp -If a message digest is to be copied to a multi-byte type (ie: -an array of five 32-bit integers) it will be necessary to -perform byte swapping on little endian machines such as the i386, alpha, -and VAX. diff --git a/lib/libc/hash/sha1/Makefile.inc b/lib/libc/hash/sha1/Makefile.inc new file mode 100644 index 000000000000..a89d254f24d5 --- /dev/null +++ b/lib/libc/hash/sha1/Makefile.inc @@ -0,0 +1,12 @@ +# $NetBSD: Makefile.inc,v 1.1 2005/09/24 19:04:52 elad Exp $ + +# hash functions +.PATH: ${.CURDIR}/hash/sha1 + +SRCS+= sha1.c sha1hl.c hmac_sha1.c + +MAN+= sha1.3 + +MLINKS+=sha1.3 SHA1Init.3 sha1.3 SHA1Update.3 sha1.3 SHA1Final.3 +MLINKS+=sha1.3 SHA1End.3 sha2.3 SHA1File.3 sha1.3 SHA1Data.3 +MLINKS+=sha1.3 SHA1Transform.3 sha1.3 SHA1FileChunk.3 diff --git a/lib/libc/hash/hmac_sha1.c b/lib/libc/hash/sha1/hmac_sha1.c similarity index 64% rename from lib/libc/hash/hmac_sha1.c rename to lib/libc/hash/sha1/hmac_sha1.c index 51ac446eba4b..f118c99124b5 100644 --- a/lib/libc/hash/hmac_sha1.c +++ b/lib/libc/hash/sha1/hmac_sha1.c @@ -1,20 +1,20 @@ -/* $NetBSD: hmac_sha1.c,v 1.2 2005/02/09 21:35:46 kleink Exp $ */ +/* $NetBSD: hmac_sha1.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ /* * hmac_sha1 - using HMAC from RFC 2104 */ #include "namespace.h" -#include +#include /* XXX */ #define HMAC_HASH SHA1 #define HMAC_FUNC hmac_sha1 #define HMAC_KAT hmac_kat_sha1 -#define HASH_LENGTH 20 +#define HASH_LENGTH SHA1_DIGEST_LENGTH #define HASH_CTX SHA1_CTX #define HASH_Init SHA1Init #define HASH_Update SHA1Update #define HASH_Final SHA1Final -#include "hmac.c" +#include "../hmac.c" diff --git a/lib/libc/hash/sha1.c b/lib/libc/hash/sha1/sha1.c similarity index 98% rename from lib/libc/hash/sha1.c rename to lib/libc/hash/sha1/sha1.c index 62a84ce8efe3..65b2c2317959 100644 --- a/lib/libc/hash/sha1.c +++ b/lib/libc/hash/sha1/sha1.c @@ -1,4 +1,4 @@ -/* $NetBSD: sha1.c,v 1.13 2005/06/12 05:21:27 lukem Exp $ */ +/* $NetBSD: sha1.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ /* @@ -25,7 +25,7 @@ #else #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: sha1.c,v 1.13 2005/06/12 05:21:27 lukem Exp $"); +__RCSID("$NetBSD: sha1.c,v 1.1 2005/09/24 19:04:52 elad Exp $"); #endif /* LIBC_SCCS and not lint */ #include "namespace.h" #include diff --git a/lib/libc/hash/sha1/sha1hl.c b/lib/libc/hash/sha1/sha1hl.c new file mode 100644 index 000000000000..1cb998bf194f --- /dev/null +++ b/lib/libc/hash/sha1/sha1hl.c @@ -0,0 +1,23 @@ +/* $NetBSD: sha1hl.c,v 1.1 2005/09/24 19:04:52 elad Exp $ */ + +/* + * Derived from code ritten by Jason R. Thorpe , + * April 29, 1997. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sha1hl.c,v 1.1 2005/09/24 19:04:52 elad Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#define HASH_ALGORITHM SHA1 + +#include "namespace.h" +#include /* XXX */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include "../hash.c" diff --git a/lib/libc/hash/sha1hl.c b/lib/libc/hash/sha1hl.c deleted file mode 100644 index 23f4a9549baa..000000000000 --- a/lib/libc/hash/sha1hl.c +++ /dev/null @@ -1,110 +0,0 @@ -/* $NetBSD: sha1hl.c,v 1.9 2003/10/27 00:12:42 lukem Exp $ */ - -/* sha1hl.c - * ---------------------------------------------------------------------------- - * "THE BEER-WARE LICENSE" (Revision 42): - * 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 -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#if HAVE_NBTOOL_CONFIG_H -#include "nbtool_config.h" -#endif - -#if !HAVE_SHA1_H - -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: sha1hl.c,v 1.9 2003/10/27 00:12:42 lukem Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#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)); -} - -#endif /* HAVE_SHA1_H */