[dev] [sbase][PATCH 1/2] add sha224sum and sha384sum

From: Mattias Andrée <maandree_AT_kth.se>
Date: Wed, 24 Feb 2016 10:05:31 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 Makefile         |  6 ++++++
 README           |  2 ++
 libutil/sha224.c | 26 ++++++++++++++++++++++++++
 libutil/sha256.c | 10 ++++++++--
 libutil/sha384.c | 26 ++++++++++++++++++++++++++
 libutil/sha512.c | 10 ++++++++--
 sha224.h         | 16 ++++++++++++++++
 sha224sum.1      | 32 ++++++++++++++++++++++++++++++++
 sha224sum.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 sha384.h         | 16 ++++++++++++++++
 sha384sum.1      | 32 ++++++++++++++++++++++++++++++++
 sha384sum.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 12 files changed, 254 insertions(+), 4 deletions(-)
 create mode 100644 libutil/sha224.c
 create mode 100644 libutil/sha384.c
 create mode 100644 sha224.h
 create mode 100644 sha224sum.1
 create mode 100644 sha224sum.c
 create mode 100644 sha384.h
 create mode 100644 sha384sum.1
 create mode 100644 sha384sum.c
diff --git a/Makefile b/Makefile
index 754f4d2..87d5708 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -11,7 +11,9 @@ HDR =\
 	md5.h\
 	queue.h\
 	sha1.h\
+	sha224.h\
 	sha256.h\
+	sha384.h\
 	sha512.h\
 	text.h\
 	utf.h\
_AT_@ -62,7 +64,9 @@ LIBUTILSRC =\
 	libutil/recurse.c\
 	libutil/rm.c\
 	libutil/sha1.c\
+	libutil/sha224.c\
 	libutil/sha256.c\
+	libutil/sha384.c\
 	libutil/sha512.c\
 	libutil/strcasestr.c\
 	libutil/strlcat.c\
_AT_@ -133,7 +137,9 @@ BIN =\
 	seq\
 	setsid\
 	sha1sum\
+	sha224sum\
 	sha256sum\
+	sha384sum\
 	sha512sum\
 	sleep\
 	sort\
diff --git a/README b/README
index cddf485..ade737e 100644
--- a/README
+++ b/README
_AT_@ -72,7 +72,9 @@ The following tools are implemented:
 =*|x seq         .
 =*|x setsid      .
 =*|x sha1sum     .
+=* x sha224sum   .
 =*|x sha256sum   .
+=* x sha238sum   .
 =*|x sha512sum   .
 =*|o sleep       .
 #*|o sort        .
diff --git a/libutil/sha224.c b/libutil/sha224.c
new file mode 100644
index 0000000..fce520f
--- /dev/null
+++ b/libutil/sha224.c
_AT_@ -0,0 +1,26 @@
+/* public domain sha224 implementation based on fips180-3 */
+#include <stdint.h>
+#include "../sha224.h"
+
+extern void sha256_sum_n(void *, uint8_t *, int n);
+
+void
+sha224_init(void *ctx)
+{
+	struct sha224 *s = ctx;
+	s->len = 0;
+	s->h[0] = 0xc1059ed8;
+	s->h[1] = 0x367cd507;
+	s->h[2] = 0x3070dd17;
+	s->h[3] = 0xf70e5939;
+	s->h[4] = 0xffc00b31;
+	s->h[5] = 0x68581511;
+	s->h[6] = 0x64f98fa7;
+	s->h[7] = 0xbefa4fa4;
+}
+
+void
+sha224_sum(void *ctx, uint8_t md[SHA224_DIGEST_LENGTH])
+{
+	sha256_sum_n(ctx, md, 8);
+}
diff --git a/libutil/sha256.c b/libutil/sha256.c
index e30169b..266cfec 100644
--- a/libutil/sha256.c
+++ b/libutil/sha256.c
_AT_@ -110,13 +110,13 @@ sha256_init(void *ctx)
 }
 
 void
-sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH])
+sha256_sum_n(void *ctx, uint8_t *md, int n)
 {
 	struct sha256 *s = ctx;
 	int i;
 
 	pad(s);
-	for (i = 0; i < 8; i++) {
+	for (i = 0; i < n; i++) {
 		md[4*i] = s->h[i] >> 24;
 		md[4*i+1] = s->h[i] >> 16;
 		md[4*i+2] = s->h[i] >> 8;
_AT_@ -125,6 +125,12 @@ sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH])
 }
 
 void
+sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH])
+{
+	sha256_sum_n(ctx, md, 8);
+}
+
+void
 sha256_update(void *ctx, const void *m, unsigned long len)
 {
 	struct sha256 *s = ctx;
diff --git a/libutil/sha384.c b/libutil/sha384.c
new file mode 100644
index 0000000..0a0e777
--- /dev/null
+++ b/libutil/sha384.c
_AT_@ -0,0 +1,26 @@
+/* public domain sha384 implementation based on fips180-3 */
+#include <stdint.h>
+#include "../sha384.h"
+
+extern void sha512_sum_n(void *, uint8_t *, int n);
+
+void
+sha384_init(void *ctx)
+{
+	struct sha384 *s = ctx;
+	s->len = 0;
+	s->h[0] = 0xcbbb9d5dc1059ed8ULL;
+	s->h[1] = 0x629a292a367cd507ULL;
+	s->h[2] = 0x9159015a3070dd17ULL;
+	s->h[3] = 0x152fecd8f70e5939ULL;
+	s->h[4] = 0x67332667ffc00b31ULL;
+	s->h[5] = 0x8eb44a8768581511ULL;
+	s->h[6] = 0xdb0c2e0d64f98fa7ULL;
+	s->h[7] = 0x47b5481dbefa4fa4ULL;
+}
+
+void
+sha384_sum(void *ctx, uint8_t md[SHA384_DIGEST_LENGTH])
+{
+	sha512_sum_n(ctx, md, 6);
+}
diff --git a/libutil/sha512.c b/libutil/sha512.c
index efe4f91..25264c7 100644
--- a/libutil/sha512.c
+++ b/libutil/sha512.c
_AT_@ -127,13 +127,13 @@ sha512_init(void *ctx)
 }
 
 void
-sha512_sum(void *ctx, uint8_t md[SHA512_DIGEST_LENGTH])
+sha512_sum_n(void *ctx, uint8_t *md, int n)
 {
 	struct sha512 *s = ctx;
 	int i;
 
 	pad(s);
-	for (i = 0; i < 8; i++) {
+	for (i = 0; i < n; i++) {
 		md[8*i] = s->h[i] >> 56;
 		md[8*i+1] = s->h[i] >> 48;
 		md[8*i+2] = s->h[i] >> 40;
_AT_@ -146,6 +146,12 @@ sha512_sum(void *ctx, uint8_t md[SHA512_DIGEST_LENGTH])
 }
 
 void
+sha512_sum(void *ctx, uint8_t md[SHA512_DIGEST_LENGTH])
+{
+	sha512_sum_n(ctx, md, 8);
+}
+
+void
 sha512_update(void *ctx, const void *m, unsigned long len)
 {
 	struct sha512 *s = ctx;
diff --git a/sha224.h b/sha224.h
new file mode 100644
index 0000000..d7f4053
--- /dev/null
+++ b/sha224.h
_AT_@ -0,0 +1,16 @@
+/* public domain sha224 implementation based on fips180-3 */
+
+#include "sha256.h"
+
+#define sha224  sha256  /*struct*/
+
+enum { SHA224_DIGEST_LENGTH = 28 };
+
+/* reset state */
+void sha224_init(void *ctx);
+/* process message */
+#define sha224_update  sha256_update
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha224_sum(void *ctx, uint8_t md[SHA224_DIGEST_LENGTH]);
diff --git a/sha224sum.1 b/sha224sum.1
new file mode 100644
index 0000000..ff7ec97
--- /dev/null
+++ b/sha224sum.1
_AT_@ -0,0 +1,32 @@
+.Dd 2016-02-24
+.Dt SHA224SUM 1
+.Os sbase
+.Sh NAME
+.Nm sha224sum
+.Nd compute or check SHA-224 message digests
+.Sh SYNOPSIS
+.Nm
+.Op Fl c
+.Op Ar file ...
+.Sh DESCRIPTION
+.Nm
+writes SHA-224 (224-bit) checksums of each
+.Ar file
+to stdout.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.Sh OPTIONS
+.Bl -tag -width Ds
+.It Fl c
+Read list of SHA-224 checksums from each
+.Ar file
+and check them.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.El
diff --git a/sha224sum.c b/sha224sum.c
new file mode 100644
index 0000000..5c4a6cb
--- /dev/null
+++ b/sha224sum.c
_AT_@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <stdio.h>
+
+#include "crypt.h"
+#include "sha224.h"
+#include "util.h"
+
+static struct sha224 s;
+struct crypt_ops sha224_ops = {
+	sha224_init,
+	sha224_update,
+	sha224_sum,
+	&s,
+};
+
+static void
+usage(void)
+{
+	eprintf("usage: %s [-c] [file ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
+	uint8_t md[SHA224_DIGEST_LENGTH];
+
+	ARGBEGIN {
+	case 'c':
+		cryptfunc = cryptcheck;
+		break;
+	default:
+		usage();
+	} ARGEND
+
+	ret |= cryptfunc(argc, argv, &sha224_ops, md, sizeof(md));
+	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+	return ret;
+}
diff --git a/sha384.h b/sha384.h
new file mode 100644
index 0000000..2ab9bc4
--- /dev/null
+++ b/sha384.h
_AT_@ -0,0 +1,16 @@
+/* public domain sha512 implementation based on fips180-3 */
+
+#include "sha512.h"
+
+#define sha384  sha512  /*struct*/
+
+enum { SHA384_DIGEST_LENGTH = 48 };
+
+/* reset state */
+void sha384_init(void *ctx);
+/* process message */
+#define sha384_update  sha512_update
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha384_sum(void *ctx, uint8_t md[SHA384_DIGEST_LENGTH]);
diff --git a/sha384sum.1 b/sha384sum.1
new file mode 100644
index 0000000..c0aa5b6
--- /dev/null
+++ b/sha384sum.1
_AT_@ -0,0 +1,32 @@
+.Dd 2016-02-24
+.Dt SHA384SUM 1
+.Os sbase
+.Sh NAME
+.Nm sha384sum
+.Nd compute or check SHA-384 message digests
+.Sh SYNOPSIS
+.Nm
+.Op Fl c
+.Op Ar file ...
+.Sh DESCRIPTION
+.Nm
+writes SHA-384 (384-bit) checksums of each
+.Ar file
+to stdout.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.Sh OPTIONS
+.Bl -tag -width Ds
+.It Fl c
+Read list of SHA-384 checksums from each
+.Ar file
+and check them.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.El
diff --git a/sha384sum.c b/sha384sum.c
new file mode 100644
index 0000000..f975b61
--- /dev/null
+++ b/sha384sum.c
_AT_@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <stdio.h>
+
+#include "crypt.h"
+#include "sha384.h"
+#include "util.h"
+
+static struct sha384 s;
+struct crypt_ops sha384_ops = {
+	sha384_init,
+	sha384_update,
+	sha384_sum,
+	&s,
+};
+
+static void
+usage(void)
+{
+	eprintf("usage: %s [-c] [file ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int ret = 0, (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
+	uint8_t md[SHA384_DIGEST_LENGTH];
+
+	ARGBEGIN {
+	case 'c':
+		cryptfunc = cryptcheck;
+		break;
+	default:
+		usage();
+	} ARGEND
+
+	ret |= cryptfunc(argc, argv, &sha384_ops, md, sizeof(md));
+	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+	return ret;
+}
-- 
2.7.1
Received on Wed Feb 24 2016 - 10:05:31 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 24 2016 - 10:12:10 CET