diff --git a/lib/libc/hash/sha2/Makefile.inc b/lib/libc/hash/sha2/Makefile.inc index cab0a5c30ebf..061889cc0319 100644 --- a/lib/libc/hash/sha2/Makefile.inc +++ b/lib/libc/hash/sha2/Makefile.inc @@ -1,13 +1,9 @@ -# $NetBSD: Makefile.inc,v 1.1 2005/08/20 16:14:34 elad Exp $ +# $NetBSD: Makefile.inc,v 1.2 2005/08/23 16:20:01 elad Exp $ # hash functions .PATH: ${.CURDIR}/hash/sha2 -#INCSDIR=/usr/include/crypto - -#INCS= sha2.h - -SRCS+= sha2.c +SRCS+= sha2.c sha256hl.c sha384hl.c sha512hl.c MAN+= sha2.3 diff --git a/lib/libc/hash/sha2/sha256hl.c b/lib/libc/hash/sha2/sha256hl.c new file mode 100644 index 000000000000..f6ac7a0967d8 --- /dev/null +++ b/lib/libc/hash/sha2/sha256hl.c @@ -0,0 +1,94 @@ +/* $NetBSD: sha256hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +/* ARGSUSED */ +char * +SHA256_End(SHA256_CTX *ctx, char *buf) +{ + int i; + u_int8_t digest[SHA256_DIGEST_LENGTH]; + static const char hex[] = "0123456789abcdef"; + + 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_FileChunk(const char *filename, char *buf, off_t off, off_t len) +{ + struct stat sb; + u_char buffer[BUFSIZ]; + SHA256_CTX ctx; + int fd, save_errno; + ssize_t nr; + + SHA256_Init(&ctx); + + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); + if (len == 0) { + if (fstat(fd, &sb) == -1) { + close(fd); + return (NULL); + } + len = sb.st_size; + } + if (off > 0 && lseek(fd, off, SEEK_SET) < 0) + return (NULL); + + while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) { + SHA256_Update(&ctx, buffer, (size_t)nr); + if (len > 0 && (len -= nr) == 0) + break; + } + + save_errno = errno; + close(fd); + errno = save_errno; + return (nr < 0 ? NULL : SHA256_End(&ctx, buf)); +} + +char * +SHA256_File(const char *filename, char *buf) +{ + return (SHA256_FileChunk(filename, buf, (off_t)0, (off_t)0)); +} + +char * +SHA256_Data(const u_char *data, size_t len, char *buf) +{ + SHA256_CTX ctx; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, data, len); + return (SHA256_End(&ctx, buf)); +} diff --git a/lib/libc/hash/sha2/sha384hl.c b/lib/libc/hash/sha2/sha384hl.c new file mode 100644 index 000000000000..b517df6da529 --- /dev/null +++ b/lib/libc/hash/sha2/sha384hl.c @@ -0,0 +1,94 @@ +/* $NetBSD: sha384hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +/* ARGSUSED */ +char * +SHA384_End(SHA384_CTX *ctx, char *buf) +{ + int i; + u_int8_t digest[SHA384_DIGEST_LENGTH]; + static const char hex[] = "0123456789abcdef"; + + 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_FileChunk(const char *filename, char *buf, off_t off, off_t len) +{ + struct stat sb; + u_char buffer[BUFSIZ]; + SHA384_CTX ctx; + int fd, save_errno; + ssize_t nr; + + SHA384_Init(&ctx); + + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); + if (len == 0) { + if (fstat(fd, &sb) == -1) { + close(fd); + return (NULL); + } + len = sb.st_size; + } + if (off > 0 && lseek(fd, off, SEEK_SET) < 0) + return (NULL); + + while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) { + SHA384_Update(&ctx, buffer, (size_t)nr); + if (len > 0 && (len -= nr) == 0) + break; + } + + save_errno = errno; + close(fd); + errno = save_errno; + return (nr < 0 ? NULL : SHA384_End(&ctx, buf)); +} + +char * +SHA384_File(const char *filename, char *buf) +{ + return (SHA384_FileChunk(filename, buf, (off_t)0, (off_t)0)); +} + +char * +SHA384_Data(const u_char *data, size_t len, char *buf) +{ + SHA384_CTX ctx; + + SHA384_Init(&ctx); + SHA384_Update(&ctx, data, len); + return (SHA384_End(&ctx, buf)); +} diff --git a/lib/libc/hash/sha2/sha512hl.c b/lib/libc/hash/sha2/sha512hl.c new file mode 100644 index 000000000000..deba982ef60d --- /dev/null +++ b/lib/libc/hash/sha2/sha512hl.c @@ -0,0 +1,94 @@ +/* $NetBSD: sha512hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +/* ARGSUSED */ +char * +SHA512_End(SHA512_CTX *ctx, char *buf) +{ + int i; + u_int8_t digest[SHA512_DIGEST_LENGTH]; + static const char hex[] = "0123456789abcdef"; + + 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_FileChunk(const char *filename, char *buf, off_t off, off_t len) +{ + struct stat sb; + u_char buffer[BUFSIZ]; + SHA512_CTX ctx; + int fd, save_errno; + ssize_t nr; + + SHA512_Init(&ctx); + + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); + if (len == 0) { + if (fstat(fd, &sb) == -1) { + close(fd); + return (NULL); + } + len = sb.st_size; + } + if (off > 0 && lseek(fd, off, SEEK_SET) < 0) + return (NULL); + + while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) { + SHA512_Update(&ctx, buffer, (size_t)nr); + if (len > 0 && (len -= nr) == 0) + break; + } + + save_errno = errno; + close(fd); + errno = save_errno; + return (nr < 0 ? NULL : SHA512_End(&ctx, buf)); +} + +char * +SHA512_File(const char *filename, char *buf) +{ + return (SHA512_FileChunk(filename, buf, (off_t)0, (off_t)0)); +} + +char * +SHA512_Data(const u_char *data, size_t len, char *buf) +{ + SHA512_CTX ctx; + + SHA512_Init(&ctx); + SHA512_Update(&ctx, data, len); + return (SHA512_End(&ctx, buf)); +}