summaryrefslogtreecommitdiff
path: root/lib/libc/hash
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1997-07-12 20:06:04 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1997-07-12 20:06:04 +0000
commit12e530ac3436852e1557bb8f9bdc5564d58b52f9 (patch)
treefd26b9b8b1406de31f1ab7a3dda6519e2a4163ef /lib/libc/hash
parent748a607d6e4f5caac91dc3612866329cef546ed1 (diff)
Add SHA1End, SHA1File, SHA1Data helper functions like in md5(3).
Diffstat (limited to 'lib/libc/hash')
-rw-r--r--lib/libc/hash/Makefile.inc4
-rw-r--r--lib/libc/hash/sha1.377
-rw-r--r--lib/libc/hash/sha1hl.c80
3 files changed, 152 insertions, 9 deletions
diff --git a/lib/libc/hash/Makefile.inc b/lib/libc/hash/Makefile.inc
index 874d4317919..b0588dc775e 100644
--- a/lib/libc/hash/Makefile.inc
+++ b/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/lib/libc/hash/sha1.3 b/lib/libc/hash/sha1.3
index e2768e795b5..c016a828634 100644
--- a/lib/libc/hash/sha1.3
+++ b/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 <Todd.Miller@courtesan.com>
.\" 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 <sys/types.h>
@@ -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 <steve@edmweb.com>.
+.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/lib/libc/hash/sha1hl.c b/lib/libc/hash/sha1hl.c
new file mode 100644
index 00000000000..0de559fa4e7
--- /dev/null
+++ b/lib/libc/hash/sha1hl.c
@@ -0,0 +1,80 @@
+/* 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
+ * ----------------------------------------------------------------------------
+ */
+
+#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 <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/file.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <sha1.h>
+
+/* 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));
+}