summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-04-26 19:38:13 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-04-26 19:38:13 +0000
commit4e8c04242120ede5e295c7085708ad50c0a31c15 (patch)
tree226f3edb6d843a7f5aa4958308692319743b1b27
parent6fd27862b97e1c98093ff633b0dfd1d9ceb7fef5 (diff)
Use a common source file for all the hash helper functions that
previously lived in foohl.c. The foohl.c files are now generated via sed, though perhaps cpp could be used in the future. Use u_int8_t instead of unsigned char for the buffers struct fooContext. Add constants for buffer lengths and use them in function prototypes and the man pages. This is basically cosmetic surgery; there should be no functional changes. OK deraadt@
-rw-r--r--include/rmd160.h34
-rw-r--r--include/sha1.h32
-rw-r--r--lib/libc/hash/Makefile.inc23
-rw-r--r--lib/libc/hash/helper.c (renamed from lib/libc/hash/rmd160hl.c)41
-rw-r--r--lib/libc/hash/rmd160.322
-rw-r--r--lib/libc/hash/rmd160.c26
-rw-r--r--lib/libc/hash/sha1.322
-rw-r--r--lib/libc/hash/sha1.c14
-rw-r--r--lib/libc/hash/sha1hl.c74
-rw-r--r--lib/libc/hash/sha2hl.c177
10 files changed, 121 insertions, 344 deletions
diff --git a/include/rmd160.h b/include/rmd160.h
index cfb009df150..f80327355e8 100644
--- a/include/rmd160.h
+++ b/include/rmd160.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: rmd160.h,v 1.12 2004/01/22 21:48:02 espie Exp $ */
+/* $OpenBSD: rmd160.h,v 1.13 2004/04/26 19:38:12 millert Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
*
@@ -25,31 +25,35 @@
#ifndef _RMD160_H
#define _RMD160_H
+#define RMD160_BLOCK_LENGTH 64
+#define RMD160_DIGEST_LENGTH 20
+#define RMD160_DIGEST_STRING_LENGTH (RMD160_DIGEST_LENGTH * 2 + 1)
+
/* RMD160 context. */
typedef struct RMD160Context {
- u_int32_t state[5]; /* state */
- u_int64_t count; /* number of bits, modulo 2^64 */
- unsigned char buffer[64]; /* input buffer */
+ u_int32_t state[5]; /* state */
+ u_int64_t count; /* number of bits, mod 2^64 */
+ u_int8_t buffer[RMD160_BLOCK_LENGTH]; /* input buffer */
} RMD160_CTX;
#include <sys/cdefs.h>
__BEGIN_DECLS
void RMD160Init(RMD160_CTX *);
-void RMD160Transform(u_int32_t [5], const unsigned char [64])
+void RMD160Transform(u_int32_t [5], const u_int8_t [RMD160_BLOCK_LENGTH])
__attribute__((__bounded__(__minbytes__,1,5)))
- __attribute__((__bounded__(__minbytes__,2,64)));
-void RMD160Update(RMD160_CTX *, const unsigned char *, u_int32_t)
+ __attribute__((__bounded__(__minbytes__,2,RMD160_BLOCK_LENGTH)));
+void RMD160Update(RMD160_CTX *, const u_int8_t *, u_int32_t)
__attribute__((__bounded__(__string__,2,3)));
-void RMD160Final(unsigned char [20], RMD160_CTX *)
- __attribute__((__bounded__(__minbytes__,1,20)));
-char *RMD160End(RMD160_CTX *, char *)
- __attribute__((__bounded__(__minbytes__,2,41)));
-char *RMD160File(char *, char *)
- __attribute__((__bounded__(__minbytes__,2,41)));
-char *RMD160Data(const unsigned char *, size_t, char *)
+void RMD160Final(u_int8_t [RMD160_DIGEST_LENGTH], RMD160_CTX *)
+ __attribute__((__bounded__(__minbytes__,1,RMD160_DIGEST_LENGTH)));
+char *RMD160End(RMD160_CTX *, char [RMD160_DIGEST_STRING_LENGTH])
+ __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
+char *RMD160File(char *, char [RMD160_DIGEST_STRING_LENGTH])
+ __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
+char *RMD160Data(const u_int8_t *, size_t, char [RMD160_DIGEST_STRING_LENGTH])
__attribute__((__bounded__(__string__,1,2)))
- __attribute__((__bounded__(__minbytes__,3,41)));
+ __attribute__((__bounded__(__minbytes__,3,RMD160_DIGEST_STRING_LENGTH)));
__END_DECLS
#endif /* _RMD160_H */
diff --git a/include/sha1.h b/include/sha1.h
index 857108e0026..2c2b0d2541e 100644
--- a/include/sha1.h
+++ b/include/sha1.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sha1.h,v 1.16 2004/01/22 21:48:02 espie Exp $ */
+/* $OpenBSD: sha1.h,v 1.17 2004/04/26 19:38:12 millert Exp $ */
/*
* SHA-1 in C
@@ -9,34 +9,36 @@
#ifndef _SHA1_H
#define _SHA1_H
+#define SHA1_BLOCK_LENGTH 64
+#define SHA1_DIGEST_LENGTH 20
+#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
+
typedef struct {
u_int32_t state[5];
u_int32_t count[2];
- unsigned char buffer[64];
+ u_int8_t buffer[SHA1_BLOCK_LENGTH];
} SHA1_CTX;
#include <sys/cdefs.h>
__BEGIN_DECLS
-void SHA1Transform(u_int32_t [5], const unsigned char [64])
+void SHA1Transform(u_int32_t [5], const u_int8_t [SHA1_BLOCK_LENGTH])
__attribute__((__bounded__(__minbytes__,1,5)))
- __attribute__((__bounded__(__minbytes__,2,64)));
+ __attribute__((__bounded__(__minbytes__,2,SHA1_BLOCK_LENGTH)));
void SHA1Init(SHA1_CTX *);
-void SHA1Update(SHA1_CTX *, const unsigned char *, unsigned int)
+void SHA1Update(SHA1_CTX *, const u_int8_t *, unsigned int)
__attribute__((__bounded__(__string__,2,3)));
-void SHA1Final(unsigned char [20], SHA1_CTX *)
- __attribute__((__bounded__(__minbytes__,1,20)));
-char *SHA1End(SHA1_CTX *, char *)
- __attribute__((__bounded__(__minbytes__,2,41)));
-char *SHA1File(char *, char *)
- __attribute__((__bounded__(__minbytes__,2,41)));
-char *SHA1Data(const unsigned char *, size_t, char *)
+void SHA1Final(u_int8_t [SHA1_DIGEST_LENGTH], SHA1_CTX *)
+ __attribute__((__bounded__(__minbytes__,1,SHA1_DIGEST_LENGTH)));
+char *SHA1End(SHA1_CTX *, char [SHA1_DIGEST_STRING_LENGTH])
+ __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH)));
+char *SHA1File(char *, char [SHA1_DIGEST_STRING_LENGTH])
+ __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH)));
+char *SHA1Data(const u_int8_t *, size_t, char [SHA1_DIGEST_STRING_LENGTH])
__attribute__((__bounded__(__string__,1,2)))
- __attribute__((__bounded__(__minbytes__,3,41)));
+ __attribute__((__bounded__(__minbytes__,3,SHA1_DIGEST_STRING_LENGTH)));
__END_DECLS
-#define SHA1_DIGESTSIZE 20
-#define SHA1_BLOCKSIZE 64
#define HTONDIGEST(x) do { \
x[0] = htonl(x[0]); \
x[1] = htonl(x[1]); \
diff --git a/lib/libc/hash/Makefile.inc b/lib/libc/hash/Makefile.inc
index 21500da4ca4..6d9df011903 100644
--- a/lib/libc/hash/Makefile.inc
+++ b/lib/libc/hash/Makefile.inc
@@ -1,9 +1,10 @@
-# $OpenBSD: Makefile.inc,v 1.14 2003/05/08 23:34:55 millert Exp $
+# $OpenBSD: Makefile.inc,v 1.15 2004/04/26 19:38:12 millert Exp $
# hash functions
.PATH: ${LIBCSRCDIR}/hash
-SRCS+= rmd160.c rmd160hl.c sha1.c sha1hl.c sha2.c sha2hl.c
+HELPER= rmd160hl.c sha1hl.c sha256hl.c sha384hl.c sha512hl.c
+SRCS+= rmd160.c sha1.c sha2.c ${HELPER}
MAN+= rmd160.3 sha1.3 sha2.3
MLINKS+=rmd160.3 RMD160Init.3 rmd160.3 RMD160Update.3 rmd160.3 RMD160Final.3
MLINKS+=rmd160.3 RMD160End.3 rmd160.3 RMD160File.3 rmd160.3 RMD160Data.3
@@ -17,3 +18,21 @@ MLINKS+=sha2.3 SHA384_Init.3 sha2.3 SHA384_Update.3 sha2.3 SHA384_Final.3
MLINKS+=sha2.3 SHA384_End.3 sha2.3 SHA384_File.3 sha2.3 SHA384_Data.3
MLINKS+=sha2.3 SHA512_Init.3 sha2.3 SHA512_Update.3 sha2.3 SHA512_Final.3
MLINKS+=sha2.3 SHA512_End.3 sha2.3 SHA512_File.3 sha2.3 SHA512_Data.3
+CLEANFILES+= ${HELPER}
+
+rmd160hl.c: helper.c
+ sed -e 's/hashinc/rmd160.h/g' -e 's/HASH/RMD160/g' $> > $@
+
+sha1hl.c: helper.c
+ sed -e 's/hashinc/sha1.h/g' -e 's/HASH/SHA1/g' $> > $@
+
+sha256hl.c: helper.c
+ sed -e 's/hashinc/sha2.h/g' -e 's/HASH_\{0,1\}/SHA256_/g' $> > $@
+
+sha384hl.c: helper.c
+ sed -e 's/hashinc/sha2.h/g' -e 's/HASH_\{0,1\}/SHA384_/g' $> > $@
+
+sha512hl.c: helper.c
+ sed -e 's/hashinc/sha2.h/g' -e 's/HASH_\{0,1\}/SHA512_/g' $> > $@
+
+beforedepend: rmd160hl.c sha1hl.c sha256hl.c sha384hl.c sha512hl.c
diff --git a/lib/libc/hash/rmd160hl.c b/lib/libc/hash/helper.c
index 043eff87eb1..f7dd752dcf1 100644
--- a/lib/libc/hash/rmd160hl.c
+++ b/lib/libc/hash/helper.c
@@ -1,4 +1,6 @@
-/* rmd160hl.c
+/* $OpenBSD: helper.c,v 1.1 2004/04/26 19:38:12 millert Exp $ */
+
+/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
@@ -8,32 +10,33 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: rmd160hl.c,v 1.5 2003/05/09 16:46:31 millert Exp $";
+static const char rcsid[] = "$OpenBSD: helper.c,v 1.1 2004/04/26 19:38:12 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
-#include <rmd160.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <hashinc>
+
/* ARGSUSED */
char *
-RMD160End(RMD160_CTX *ctx, char *buf)
+HASHEnd(HASH_CTX *ctx, char buf[HASH_DIGEST_STRING_LENGTH])
{
int i;
- u_char digest[20];
- static const char hex[]="0123456789abcdef";
+ u_char digest[HASH_DIGEST_LENGTH];
+ static const char hex[] = "0123456789abcdef";
- if (buf == NULL && (buf = malloc(41)) == NULL)
+ if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL)
return(NULL);
- RMD160Final(digest, ctx);
- for (i = 0; i < 20; i++) {
+ HASHFinal(digest,ctx);
+ for (i = 0; i < HASH_DIGEST_LENGTH; i++) {
buf[i + i] = hex[digest[i] >> 4];
buf[i + i + 1] = hex[digest[i] & 0x0f];
}
@@ -43,32 +46,32 @@ RMD160End(RMD160_CTX *ctx, char *buf)
}
char *
-RMD160File (char *filename, char *buf)
+HASHFile(char *filename, char buf[HASH_DIGEST_STRING_LENGTH])
{
u_char buffer[BUFSIZ];
- RMD160_CTX ctx;
+ HASH_CTX ctx;
int fd, num, oerrno;
- RMD160Init(&ctx);
+ HASHInit(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
- RMD160Update(&ctx, buffer, num);
+ HASHUpdate(&ctx, buffer, num);
oerrno = errno;
close(fd);
errno = oerrno;
- return(num < 0 ? 0 : RMD160End(&ctx, buf));
+ return(num < 0 ? 0 : HASHEnd(&ctx, buf));
}
char *
-RMD160Data (const u_char *data, size_t len, char *buf)
+HASHData(const u_char *data, size_t len, char buf[HASH_DIGEST_STRING_LENGTH])
{
- RMD160_CTX ctx;
+ HASH_CTX ctx;
- RMD160Init(&ctx);
- RMD160Update(&ctx, data, len);
- return(RMD160End(&ctx, buf));
+ HASHInit(&ctx);
+ HASHUpdate(&ctx, data, len);
+ return(HASHEnd(&ctx, buf));
}
diff --git a/lib/libc/hash/rmd160.3 b/lib/libc/hash/rmd160.3
index 65b84a1f61c..8e8eecef269 100644
--- a/lib/libc/hash/rmd160.3
+++ b/lib/libc/hash/rmd160.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: rmd160.3,v 1.20 2003/06/25 19:33:34 deraadt Exp $
+.\" $OpenBSD: rmd160.3,v 1.21 2004/04/26 19:38:12 millert Exp $
.\"
.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
@@ -35,17 +35,17 @@
.Ft void
.Fn RMD160Init "RMD160_CTX *context"
.Ft void
-.Fn RMD160Update "RMD160_CTX *context" "const u_char *data" "u_int32_t nbytes"
+.Fn RMD160Update "RMD160_CTX *context" "const u_int8_t *data" "u_int32_t nbytes"
.Ft void
-.Fn RMD160Final "u_char digest[20]" "RMD160_CTX *context"
+.Fn RMD160Final "u_int8_t digest[RMD160_DIGEST_LENGTH]" "RMD160_CTX *context"
.Ft void
-.Fn RMD160Transform "u_int32_t state[5]" "const u_char block[64]"
+.Fn RMD160Transform "u_int32_t state[5]" "const u_int8_t block[RMD160_BLOCK_LENGTH]"
.Ft "char *"
-.Fn RMD160End "RMD160_CTX *context" "char *buf"
+.Fn RMD160End "RMD160_CTX *context" "char buf[RMD160_DIGEST_STRING_LENGTH]"
.Ft "char *"
-.Fn RMD160File "char *filename" "char *buf"
+.Fn RMD160File "char *filename" "char buf[RMD160_DIGEST_STRING_LENGTH]"
.Ft "char *"
-.Fn RMD160Data "const u_char *data" "size_t len" "char *buf"
+.Fn RMD160Data "const u_int8_t *data" "size_t len" "char buf[RMD160_DIGEST_STRING_LENGTH]"
.Sh DESCRIPTION
The RMD160 functions implement the 160-bit RIPE message digest hash algorithm
(RMD-160).
@@ -147,19 +147,19 @@ 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];
+u_int8_t results[RMD160_DIGEST_LENGTH];
char *buf;
int n;
buf = "abc";
n = strlen(buf);
RMD160Init(&rmd);
-RMD160Update(&rmd, (u_char *)buf, n);
+RMD160Update(&rmd, (u_int8_t *)buf, n);
RMD160Final(results, &rmd);
/* Print the digest as one long hex value */
printf("0x");
-for (n = 0; n < 20; n++)
+for (n = 0; n < RMD160_DIGEST_LENGTH; n++)
printf("%02x", results[n]);
putchar('\en');
.Ed
@@ -167,7 +167,7 @@ putchar('\en');
Alternately, the helper functions could be used in the following way:
.Bd -literal -offset indent
RMD160_CTX rmd;
-u_char output[41];
+u_int8_t output[RMD160_DIGEST_STRING_LENGTH];
char *buf = "abc";
printf("0x%s\en", RMD160Data(buf, strlen(buf), output));
diff --git a/lib/libc/hash/rmd160.c b/lib/libc/hash/rmd160.c
index f1d6cf72fab..e38843dead5 100644
--- a/lib/libc/hash/rmd160.c
+++ b/lib/libc/hash/rmd160.c
@@ -32,7 +32,7 @@
#include <rmd160.h>
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: rmd160.c,v 1.13 2003/12/14 11:22:35 markus Exp $";
+static char rcsid[] = "$OpenBSD: rmd160.c,v 1.14 2004/04/26 19:38:12 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#define PUT_64BIT_LE(cp, value) do { \
@@ -86,7 +86,7 @@ static char rcsid[] = "$OpenBSD: rmd160.c,v 1.13 2003/12/14 11:22:35 markus Exp
#define X(i) x[i]
-static u_char PADDING[64] = {
+static u_char PADDING[RMD160_BLOCK_LENGTH] = {
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
@@ -108,8 +108,8 @@ RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
{
u_int32_t have, off, need;
- have = (ctx->count/8) % 64;
- need = 64 - have;
+ have = (ctx->count / 8) % RMD160_BLOCK_LENGTH;
+ need = RMD160_BLOCK_LENGTH - have;
ctx->count += 8 * len;
off = 0;
@@ -121,9 +121,9 @@ RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
have = 0;
}
/* now the buffer is empty */
- while (off + 64 <= len) {
+ while (off + RMD160_BLOCK_LENGTH <= len) {
RMD160Transform(ctx->state, input+off);
- off += 64;
+ off += RMD160_BLOCK_LENGTH;
}
}
if (off < len)
@@ -131,7 +131,7 @@ RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
}
void
-RMD160Final(u_char digest[20], RMD160_CTX *ctx)
+RMD160Final(u_char digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
{
int i;
u_char size[8];
@@ -140,12 +140,12 @@ RMD160Final(u_char digest[20], RMD160_CTX *ctx)
PUT_64BIT_LE(size, ctx->count);
/*
- * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes
- * for the size
+ * pad to RMD160_BLOCK_LENGTH byte blocks, at least one byte from
+ * PADDING plus 8 bytes for the size
*/
- padlen = 64 - ((ctx->count/8) % 64);
+ padlen = RMD160_BLOCK_LENGTH - ((ctx->count / 8) % RMD160_BLOCK_LENGTH);
if (padlen < 1 + 8)
- padlen += 64;
+ padlen += RMD160_BLOCK_LENGTH;
RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
RMD160Update(ctx, size, 8);
@@ -157,12 +157,12 @@ RMD160Final(u_char digest[20], RMD160_CTX *ctx)
}
void
-RMD160Transform(u_int32_t state[5], const u_char block[64])
+RMD160Transform(u_int32_t state[5], const u_char block[RMD160_BLOCK_LENGTH])
{
u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
#if BYTE_ORDER == LITTLE_ENDIAN
- memcpy(x, block, 64);
+ memcpy(x, block, RMD160_BLOCK_LENGTH);
#else
int i;
diff --git a/lib/libc/hash/sha1.3 b/lib/libc/hash/sha1.3
index 4d753ca7966..9b5b001d238 100644
--- a/lib/libc/hash/sha1.3
+++ b/lib/libc/hash/sha1.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: sha1.3,v 1.26 2003/06/25 19:33:34 deraadt Exp $
+.\" $OpenBSD: sha1.3,v 1.27 2004/04/26 19:38:12 millert Exp $
.\"
.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
@@ -35,17 +35,17 @@
.Ft void
.Fn SHA1Init "SHA1_CTX *context"
.Ft void
-.Fn SHA1Update "SHA1_CTX *context" "const u_char *data" "u_int len"
+.Fn SHA1Update "SHA1_CTX *context" "const u_int8_t *data" "u_int len"
.Ft void
-.Fn SHA1Final "u_char digest[20]" "SHA1_CTX *context"
+.Fn SHA1Final "u_int8_t digest[SHA1_DIGEST_LENGTH]" "SHA1_CTX *context"
.Ft void
-.Fn SHA1Transform "u_int32_t state[5]" "u_char buffer[64]"
+.Fn SHA1Transform "u_int32_t state[5]" "u_int8_t buffer[SHA1_BLOCK_LENGTH]"
.Ft "char *"
-.Fn SHA1End "SHA1_CTX *context" "char *buf"
+.Fn SHA1End "SHA1_CTX *context" "char buf[SHA1_DIGEST_STRING_LENGTH]"
.Ft "char *"
-.Fn SHA1File "char *filename" "char *buf"
+.Fn SHA1File "char *filename" "char buf[SHA1_DIGEST_STRING_LENGTH]"
.Ft "char *"
-.Fn SHA1Data "const u_char *data" "u_int len" "char *buf"
+.Fn SHA1Data "const u_int8_t *data" "u_int len" "char buf[SHA1_DIGEST_STRING_LENGTH]"
.Sh DESCRIPTION
The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1),
FIPS PUB 180-1.
@@ -144,19 +144,19 @@ 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];
+u_int8_t results[SHA1_DIGEST_LENGTH];
char *buf;
int n;
buf = "abc";
n = strlen(buf);
SHA1Init(&sha);
-SHA1Update(&sha, (u_char *)buf, n);
+SHA1Update(&sha, (u_int8_t *)buf, n);
SHA1Final(results, &sha);
/* Print the digest as one long hex value */
printf("0x");
-for (n = 0; n < 20; n++)
+for (n = 0; n < SHA1_DIGEST_LENGTH; n++)
printf("%02x", results[n]);
putchar('\en');
.Ed
@@ -164,7 +164,7 @@ putchar('\en');
Alternately, the helper functions could be used in the following way:
.Bd -literal -offset indent
SHA1_CTX sha;
-u_char output[41];
+u_int8_t output[SHA1_DIGEST_STRING_LENGTH];
char *buf = "abc";
printf("0x%s\en", SHA1Data(buf, strlen(buf), output));
diff --git a/lib/libc/hash/sha1.c b/lib/libc/hash/sha1.c
index 66246e8d214..cda161a05d5 100644
--- a/lib/libc/hash/sha1.c
+++ b/lib/libc/hash/sha1.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sha1.c,v 1.13 2004/03/31 23:41:53 brad Exp $ */
+/* $OpenBSD: sha1.c,v 1.14 2004/04/26 19:38:12 millert Exp $ */
/*
* SHA-1 in C
@@ -15,7 +15,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: sha1.c,v 1.13 2004/03/31 23:41:53 brad Exp $";
+static char rcsid[] = "$OpenBSD: sha1.c,v 1.14 2004/04/26 19:38:12 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#define SHA1HANDSOFF /* Copies data before messing with it. */
@@ -52,7 +52,7 @@ static char rcsid[] = "$OpenBSD: sha1.c,v 1.13 2004/03/31 23:41:53 brad Exp $";
* Hash a single 512-bit block. This is the core of the algorithm.
*/
void
-SHA1Transform(u_int32_t state[5], const u_char buffer[64])
+SHA1Transform(u_int32_t state[5], const u_char buffer[SHA1_BLOCK_LENGTH])
{
u_int32_t a, b, c, d, e;
typedef union {
@@ -62,9 +62,9 @@ SHA1Transform(u_int32_t state[5], const u_char buffer[64])
CHAR64LONG16 *block;
#ifdef SHA1HANDSOFF
- u_char workspace[64];
+ u_char workspace[SHA1_BLOCK_LENGTH];
block = (CHAR64LONG16 *)workspace;
- (void)memcpy(block, buffer, 64);
+ (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
#else
block = (CHAR64LONG16 *)buffer;
#endif
@@ -156,7 +156,7 @@ SHA1Update(SHA1_CTX *context, const u_char *data, u_int len)
* Add padding and return the message digest.
*/
void
-SHA1Final(u_char digest[20], SHA1_CTX *context)
+SHA1Final(u_char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
{
u_int i;
u_char finalcount[8];
@@ -171,7 +171,7 @@ SHA1Final(u_char digest[20], SHA1_CTX *context)
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
if (digest) {
- for (i = 0; i < 20; i++)
+ for (i = 0; i < SHA1_DIGEST_LENGTH; i++)
digest[i] = (u_char)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
diff --git a/lib/libc/hash/sha1hl.c b/lib/libc/hash/sha1hl.c
deleted file mode 100644
index a30273aec99..00000000000
--- a/lib/libc/hash/sha1hl.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/* 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 const char rcsid[] = "$OpenBSD: sha1hl.c,v 1.5 2003/05/09 16:46:31 millert Exp $";
-#endif /* LIBC_SCCS and not lint */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <sha1.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-/* ARGSUSED */
-char *
-SHA1End(SHA1_CTX *ctx, char *buf)
-{
- int i;
- u_char digest[20];
- static const char hex[]="0123456789abcdef";
-
- if (buf == NULL && (buf = malloc(41)) == NULL)
- return(NULL);
-
- SHA1Final(digest,ctx);
- for (i = 0; i < 20; i++) {
- buf[i + i] = hex[digest[i] >> 4];
- buf[i + i + 1] = hex[digest[i] & 0x0f];
- }
- buf[i + i] = '\0';
- memset(digest, 0, sizeof(digest));
- return(buf);
-}
-
-char *
-SHA1File (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 (const u_char *data, size_t len, char *buf)
-{
- SHA1_CTX ctx;
-
- SHA1Init(&ctx);
- SHA1Update(&ctx, data, len);
- return(SHA1End(&ctx, buf));
-}
diff --git a/lib/libc/hash/sha2hl.c b/lib/libc/hash/sha2hl.c
deleted file mode 100644
index dee7f3cfa23..00000000000
--- a/lib/libc/hash/sha2hl.c
+++ /dev/null
@@ -1,177 +0,0 @@
-/* sha2hl.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 const char rcsid[] = "$OpenBSD: sha2hl.c,v 1.2 2003/05/09 16:46:31 millert Exp $";
-#endif /* LIBC_SCCS and not lint */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <sha2.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-static const char hex[]="0123456789abcdef";
-
-/* ARGSUSED */
-char *
-SHA256_End(SHA256_CTX *ctx, char buf[SHA256_DIGEST_STRING_LENGTH])
-{
- int i;
- u_int8_t digest[SHA256_DIGEST_LENGTH];
-
- if (buf == NULL && (buf = malloc(SHA256_DIGEST_STRING_LENGTH)) == NULL)
- return(NULL);
-
- SHA256_Final(digest, ctx);
- for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
- buf[i + i] = hex[digest[i] >> 4];
- buf[i + i + 1] = hex[digest[i] & 0x0f];
- }
- buf[i + i] = '\0';
- memset(digest, 0, sizeof(digest));
- return(buf);
-}
-
-char *
-SHA256_File(char *filename, char buf[SHA256_DIGEST_STRING_LENGTH])
-{
- u_int8_t buffer[BUFSIZ];
- SHA256_CTX ctx;
- int fd, num, oerrno;
-
- SHA256_Init(&ctx);
-
- if ((fd = open(filename, O_RDONLY)) < 0)
- return(0);
-
- while ((num = read(fd, buffer, sizeof(buffer))) > 0)
- SHA256_Update(&ctx, buffer, num);
-
- oerrno = errno;
- close(fd);
- errno = oerrno;
- return(num < 0 ? 0 : SHA256_End(&ctx, buf));
-}
-
-char *
-SHA256_Data(const u_int8_t *data, size_t len, char buf[SHA256_DIGEST_STRING_LENGTH])
-{
- SHA256_CTX ctx;
-
- SHA256_Init(&ctx);
- SHA256_Update(&ctx, data, len);
- return(SHA256_End(&ctx, buf));
-}
-
-/* ARGSUSED */
-char *
-SHA384_End(SHA384_CTX *ctx, char buf[SHA384_DIGEST_STRING_LENGTH])
-{
- int i;
- u_int8_t digest[SHA384_DIGEST_LENGTH];
-
- if (buf == NULL && (buf = malloc(SHA384_DIGEST_STRING_LENGTH)) == NULL)
- return(NULL);
-
- SHA384_Final(digest, ctx);
- for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
- buf[i + i] = hex[digest[i] >> 4];
- buf[i + i + 1] = hex[digest[i] & 0x0f];
- }
- buf[i + i] = '\0';
- memset(digest, 0, sizeof(digest));
- return(buf);
-}
-
-char *
-SHA384_File(char *filename, char buf[SHA384_DIGEST_STRING_LENGTH])
-{
- u_int8_t buffer[BUFSIZ];
- SHA384_CTX ctx;
- int fd, num, oerrno;
-
- SHA384_Init(&ctx);
-
- if ((fd = open(filename, O_RDONLY)) < 0)
- return(0);
-
- while ((num = read(fd, buffer, sizeof(buffer))) > 0)
- SHA384_Update(&ctx, buffer, num);
-
- oerrno = errno;
- close(fd);
- errno = oerrno;
- return(num < 0 ? 0 : SHA384_End(&ctx, buf));
-}
-
-char *
-SHA384_Data(const u_int8_t *data, size_t len, char buf[SHA384_DIGEST_STRING_LENGTH])
-{
- SHA384_CTX ctx;
-
- SHA384_Init(&ctx);
- SHA384_Update(&ctx, data, len);
- return(SHA384_End(&ctx, buf));
-}
-
-/* ARGSUSED */
-char *
-SHA512_End(SHA512_CTX *ctx, char buf[SHA512_DIGEST_STRING_LENGTH])
-{
- int i;
- u_int8_t digest[SHA512_DIGEST_LENGTH];
-
- if (buf == NULL && (buf = malloc(SHA512_DIGEST_STRING_LENGTH)) == NULL)
- return(NULL);
-
- SHA512_Final(digest, ctx);
- for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
- buf[i + i] = hex[digest[i] >> 4];
- buf[i + i + 1] = hex[digest[i] & 0x0f];
- }
- buf[i + i] = '\0';
- memset(digest, 0, sizeof(digest));
- return(buf);
-}
-
-char *
-SHA512_File(char *filename, char buf[SHA512_DIGEST_STRING_LENGTH])
-{
- u_int8_t buffer[BUFSIZ];
- SHA512_CTX ctx;
- int fd, num, oerrno;
-
- SHA512_Init(&ctx);
-
- if ((fd = open(filename, O_RDONLY)) < 0)
- return(0);
-
- while ((num = read(fd, buffer, sizeof(buffer))) > 0)
- SHA512_Update(&ctx, buffer, num);
-
- oerrno = errno;
- close(fd);
- errno = oerrno;
- return(num < 0 ? 0 : SHA512_End(&ctx, buf));
-}
-
-char *
-SHA512_Data(const u_int8_t *data, size_t len, char buf[SHA512_DIGEST_STRING_LENGTH])
-{
- SHA512_CTX ctx;
-
- SHA512_Init(&ctx);
- SHA512_Update(&ctx, data, len);
- return(SHA512_End(&ctx, buf));
-}