From 0818e080d8353efd3902ddb0241f7eb664cc862c Mon Sep 17 00:00:00 2001 From: millert <> Date: Sat, 12 Jul 1997 20:06:03 +0000 Subject: [PATCH] Add SHA1End, SHA1File, SHA1Data helper functions like in md5(3). --- src/include/sha1.h | 5 ++- src/lib/libc/hash/Makefile.inc | 4 +- src/lib/libc/hash/sha1.3 | 77 +++++++++++++++++++++++++++++--- src/lib/libc/hash/sha1hl.c | 80 ++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 src/lib/libc/hash/sha1hl.c diff --git a/src/include/sha1.h b/src/include/sha1.h index a3c6193b..cec4aa2e 100644 --- a/src/include/sha1.h +++ b/src/include/sha1.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sha1.h,v 1.6 1997/07/10 23:37:49 millert Exp $ */ +/* $OpenBSD: sha1.h,v 1.7 1997/07/12 20:06:01 millert Exp $ */ /* * SHA-1 in C @@ -19,5 +19,8 @@ void SHA1Transform __P((u_int32_t state[5], u_char buffer[64])); void SHA1Init __P((SHA1_CTX *context)); void SHA1Update __P((SHA1_CTX *context, u_char *data, u_int len)); void SHA1Final __P((u_char digest[20], SHA1_CTX *context)); +char *SHA1End __P((SHA1_CTX *, char *)); +char *SHA1File __P((char *, char *)); +char *SHA1Data __P((const u_char *, size_t, char *)); #endif /* _SHA1_H */ diff --git a/src/lib/libc/hash/Makefile.inc b/src/lib/libc/hash/Makefile.inc index 874d4317..b0588dc7 100644 --- a/src/lib/libc/hash/Makefile.inc +++ b/src/lib/libc/hash/Makefile.inc @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile.inc,v 1.2 1997/07/11 04:29:20 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1997/07/12 20:06:02 millert Exp $ # hash functions .PATH: ${.CURDIR}/hash -SRCS+= sha1.c +SRCS+= sha1.c sha1hl.c MAN+= sha1.3 MLINKS+=sha1.3 SHA1Init.3 MLINKS+=sha1.3 SHA1Update.3 diff --git a/src/lib/libc/hash/sha1.3 b/src/lib/libc/hash/sha1.3 index e2768e79..c016a828 100644 --- a/src/lib/libc/hash/sha1.3 +++ b/src/lib/libc/hash/sha1.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: sha1.3,v 1.3 1997/07/12 11:02:28 provos Exp $ +.\" $OpenBSD: sha1.3,v 1.4 1997/07/12 20:06:03 millert Exp $ .\" .\" Copyright (c) 1997 Todd C. Miller .\" All rights reserved. @@ -36,7 +36,10 @@ .Nm SHA1Init , .Nm SHA1Update , .Nm SHA1Final , -.Nm SHA1Transform +.Nm SHA1Transform , +.Nm SHA1End , +.Nm SHA1File , +.Nm SHA1Data , .Nd calculate the NIST Secure Hash Algorithm .Sh SYNOPSIS .Fd #include @@ -49,6 +52,12 @@ .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" "u_int len" "char *buf" .Sh DESCRIPTION The SHA1 functions implement then NIST Secure Hash Algorithm (SHA-1), FIPS PUB 180-1. SHA-1 is used to generate a condensed representation @@ -64,7 +73,7 @@ functions with which they share a similar interface. .Pp The .Fn SHA1Init -function initializes a MDX_CTX +function initializes a SHA1_CTX .Ar context for use with .Fn SHA1Update , @@ -103,7 +112,44 @@ and instead of calling .Fn SHA1Transform directly. -.Sh EXAMPLE +.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 @@ -122,7 +168,16 @@ SHA1Final(results, &sha); printf("0x"); for (n = 0; n < 20; n++) printf("%x", results[n]); -putchar('\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", MD5Data(buf, strlen(buf), output)); .Ed .Sh CAVEATS This implementation of SHA-1 has not been validated by NIST @@ -132,9 +187,17 @@ 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. -.Sh AUTHOR -This implementation of SHA-1 was written by Steve Reid . +.Sh AUTHORS +This implementation of SHA-1 was written by Steve Reid. +.br +The +.Fn SHA1End , +.Fn SHA1File , +and +.Fn SHA1Data +helper functions are derived from code written by Poul-Henning Kamp. .Sh SEE ALSO +.Xr sha1 1 , .Xr md4 3 , .Xr md5 3 .Pp diff --git a/src/lib/libc/hash/sha1hl.c b/src/lib/libc/hash/sha1hl.c new file mode 100644 index 00000000..0de559fa --- /dev/null +++ b/src/lib/libc/hash/sha1hl.c @@ -0,0 +1,80 @@ +/* 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 + * ---------------------------------------------------------------------------- + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* ARGSUSED */ +char * +SHA1End(ctx, buf) + SHA1_CTX *ctx; + char *buf; +{ + int i; + char *p = buf; + u_char digest[20]; + static const char hex[]="0123456789abcdef"; + + if (p == NULL && (p = malloc(41)) == NULL) + return 0; + + SHA1Final(digest,ctx); + for (i = 0; i < 20; i++) { + p[i + i] = hex[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; + + SHA1Init(&ctx); + + if ((fd = open(filename,O_RDONLY)) < 0) + return(0); + + while ((num = read(fd, buffer, sizeof(buffer))) > 0) + SHA1Update(&ctx, buffer, 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; + + SHA1Init(&ctx); + SHA1Update(&ctx, data, len); + return(SHA1End(&ctx, buf)); +}