Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/hash/sha2 Add helper routines (end, filechunk, file...
details: https://anonhg.NetBSD.org/src/rev/251f3a3994a8
branches: trunk
changeset: 583804:251f3a3994a8
user: elad <elad%NetBSD.org@localhost>
date: Tue Aug 23 16:20:01 2005 +0000
description:
Add helper routines (end, filechunk, file, data) for SHA2.
diffstat:
lib/libc/hash/sha2/Makefile.inc | 8 +--
lib/libc/hash/sha2/sha256hl.c | 94 +++++++++++++++++++++++++++++++++++++++++
lib/libc/hash/sha2/sha384hl.c | 94 +++++++++++++++++++++++++++++++++++++++++
lib/libc/hash/sha2/sha512hl.c | 94 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 284 insertions(+), 6 deletions(-)
diffs (truncated from 313 to 300 lines):
diff -r 478dd5f00b53 -r 251f3a3994a8 lib/libc/hash/sha2/Makefile.inc
--- a/lib/libc/hash/sha2/Makefile.inc Tue Aug 23 15:29:48 2005 +0000
+++ b/lib/libc/hash/sha2/Makefile.inc Tue Aug 23 16:20:01 2005 +0000
@@ -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 -r 478dd5f00b53 -r 251f3a3994a8 lib/libc/hash/sha2/sha256hl.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/hash/sha2/sha256hl.c Tue Aug 23 16:20:01 2005 +0000
@@ -0,0 +1,94 @@
+/* $NetBSD: sha256hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */
+
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <phk%login.dkuug.dk@localhost> 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 <sys/param.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <crypto/sha2.h>
+
+/* 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 -r 478dd5f00b53 -r 251f3a3994a8 lib/libc/hash/sha2/sha384hl.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/hash/sha2/sha384hl.c Tue Aug 23 16:20:01 2005 +0000
@@ -0,0 +1,94 @@
+/* $NetBSD: sha384hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */
+
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <phk%login.dkuug.dk@localhost> 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 <sys/param.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <crypto/sha2.h>
+
+/* 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 -r 478dd5f00b53 -r 251f3a3994a8 lib/libc/hash/sha2/sha512hl.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/hash/sha2/sha512hl.c Tue Aug 23 16:20:01 2005 +0000
@@ -0,0 +1,94 @@
+/* $NetBSD: sha512hl.c,v 1.1 2005/08/23 16:20:01 elad Exp $ */
+
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <phk%login.dkuug.dk@localhost> 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 <sys/param.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <crypto/sha2.h>
+
+/* 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)
Home |
Main Index |
Thread Index |
Old Index