[dev] [sbase][PATCH v2] add sha512-224sum (SHA512/224) and sha512-256sum (SHA512/256)

From: Mattias Andrée <maandree_AT_kth.se>
Date: Wed, 24 Feb 2016 11:36:59 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 Makefile             |  36 ++++++----
 README               | 190 ++++++++++++++++++++++++++-------------------------
 libutil/sha512-224.c |  26 +++++++
 libutil/sha512-256.c |  26 +++++++
 sha512-224.h         |  16 +++++
 sha512-224sum.1      |  32 +++++++++
 sha512-224sum.c      |  41 +++++++++++
 sha512-256.h         |  16 +++++
 sha512-256sum.1      |  32 +++++++++
 sha512-256sum.c      |  41 +++++++++++
 10 files changed, 347 insertions(+), 109 deletions(-)
 create mode 100644 libutil/sha512-224.c
 create mode 100644 libutil/sha512-256.c
 create mode 100644 sha512-224.h
 create mode 100644 sha512-224sum.1
 create mode 100644 sha512-224sum.c
 create mode 100644 sha512-256.h
 create mode 100644 sha512-256sum.1
 create mode 100644 sha512-256sum.c
diff --git a/Makefile b/Makefile
index 87d5708..5fd0b50 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -15,6 +15,8 @@ HDR =\
 	sha256.h\
 	sha384.h\
 	sha512.h\
+	sha512-224.h\
+	sha512-256.h\
 	text.h\
 	utf.h\
 	util.h
_AT_@ -68,6 +70,8 @@ LIBUTILSRC =\
 	libutil/sha256.c\
 	libutil/sha384.c\
 	libutil/sha512.c\
+	libutil/sha512-224.c\
+	libutil/sha512-256.c\
 	libutil/strcasestr.c\
 	libutil/strlcat.c\
 	libutil/strlcpy.c\
_AT_@ -141,6 +145,8 @@ BIN =\
 	sha256sum\
 	sha384sum\
 	sha512sum\
+	sha512-224sum\
+	sha512-256sum\
 	sleep\
 	sort\
 	split\
_AT_@ -227,21 +233,21 @@ sbase-box: $(LIB) $(SRC)
 	mkdir -p build
 	cp $(HDR) build
 	cp confstr_l.h limits_l.h sysconf_l.h pathconf_l.h build
-	for f in $(SRC); do sed "s/^main(/$${f%.c}_&/" < $$f > build/$$f; done
-	echo '#include <libgen.h>'                                                                            > build/$_AT_.c
-	echo '#include <stdio.h>'                                                                            >> build/$_AT_.c
-	echo '#include <stdlib.h>'                                                                           >> build/$_AT_.c
-	echo '#include <string.h>'                                                                           >> build/$_AT_.c
-	echo '#include "util.h"'                                                                             >> build/$_AT_.c
-	for f in $(SRC); do echo "int $${f%.c}_main(int, char **);"; done                                    >> build/$_AT_.c
-	echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);'                               >> build/$_AT_.c
-	echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;'                 >> build/$_AT_.c
-	echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);"                           >> build/$_AT_.c
-	echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);"                                     >> build/$_AT_.c
-	for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $${f%.c}_main(argc, argv);"; done >> build/$_AT_.c
-	echo 'else { fputs("[ ", stdout);'                                                                   >> build/$_AT_.c
-	for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done                                       >> build/$_AT_.c
-	echo 'putchar(0xa); }; return 0; }'                                                                  >> build/$_AT_.c
+	for f in $(SRC); do sed "s/^main(/$$(echo "$${f%.c}" | sed s/-/_/g)_&/" < $$f > build/$$f; done
+	echo '#include <libgen.h>'                                                                                                     > build/$_AT_.c
+	echo '#include <stdio.h>'                                                                                                     >> build/$_AT_.c
+	echo '#include <stdlib.h>'                                                                                                    >> build/$_AT_.c
+	echo '#include <string.h>'                                                                                                    >> build/$_AT_.c
+	echo '#include "util.h"'                                                                                                      >> build/$_AT_.c
+	for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed s/-/_/g)_main(int, char **);"; done                                    >> build/$_AT_.c
+	echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);'                                                        >> build/$_AT_.c
+	echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;'                                          >> build/$_AT_.c
+	echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);"                                                    >> build/$_AT_.c
+	echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);"                                                              >> build/$_AT_.c
+	for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $$(echo "$${f%.c}" | sed s/-/_/g)_main(argc, argv);"; done >> build/$_AT_.c
+	echo 'else { fputs("[ ", stdout);'                                                                                            >> build/$_AT_.c
+	for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done                                                                >> build/$_AT_.c
+	echo 'putchar(0xa); }; return 0; }'                                                                                           >> build/$_AT_.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $_AT_ build/*.c $(LIB)
 	rm -r build
 
diff --git a/README b/README
index ade737e..aa18ede 100644
--- a/README
+++ b/README
_AT_@ -10,100 +10,102 @@ The following tools are implemented:
 '|'  -> Audited,       'o' -> POSIX 2013 compliant,   'x' -> Non-POSIX,
 '()' -> Petty flag
 
-     UTILITY     MISSING
-     -------     -------
-=*|o basename    .
-=*|o cal         .
-=*|o cat         .
-=*|o chgrp       .
-=*|o chmod       .
-=*|o chown       .
-=*|x chroot      .
-=*|o cksum       .
-=*|o cmp         .
-#*|x cols        .
-=*|o comm        .
-=*|o cp          (-i)
-=*|x cron        .
-#*|o cut         .
-=*|o date        .
-=*|o dirname     .
-=*|o du          .
-=*|o echo        .
-   o ed          .
-=*|o env         .
-#*|o expand      .
-#*|o expr        .
-=*|o false       .
-=    find        .
-=* x flock       .
-#*|o fold        .
-=*|o getconf     (-v)
-=*|o grep        .
-=*|o head        .
-=*|x hostname    .
-=*|x install     .
-=* o join        .
-=*|o kill        .
-=*|o link        .
-=*|o ln          .
-=*|o logger      .
-=*|o logname     .
-#* o ls          (-C, -k, -m, -p, -s, -x)
-=*|x md5sum      .
-=*|o mkdir       .
-=*|o mkfifo      .
-=*|x mktemp      .
-=*|o mv          (-i)
-=*|o nice        .
-#*|o nl          .
-=*|o nohup       .
-=*|o od          .
-#* o pathchk     .
-#*|o paste       .
-=*|x printenv    .
-#*|o printf      .
-=*|o pwd         .
-=*|x readlink    .
-=*|o renice      .
-=*|o rm          (-i)
-=*|o rmdir       .
-#    sed         .
-=*|x seq         .
-=*|x setsid      .
-=*|x sha1sum     .
-=* x sha224sum   .
-=*|x sha256sum   .
-=* x sha238sum   .
-=*|x sha512sum   .
-=*|o sleep       .
-#*|o sort        .
-=*|o split       .
-=*|x sponge      .
-#*|o strings     .
-=*|x sync        .
-=*|o tail        .
-=*|x tar         .
-=*|o tee         .
-=*|o test        .
-=*|x tftp        .
-=*|o time        .
-=*|o touch       .
-#*|o tr          .
-=*|o true        .
-=* o tsort       .
-=*|o tty         .
-=*|o uname       .
-#*|o unexpand    .
-=*|o uniq        .
-=*|o unlink      .
-=*|o uudecode    .
-=*|o uuencode    .
-#*|o wc          .
-=*|x which       .
-=*|x whoami      .
-=*|o xargs       (-p)
-=*|x yes         .
+     UTILITY         MISSING
+     -------         -------
+=*|o basename        .
+=*|o cal             .
+=*|o cat             .
+=*|o chgrp           .
+=*|o chmod           .
+=*|o chown           .
+=*|x chroot          .
+=*|o cksum           .
+=*|o cmp             .
+#*|x cols            .
+=*|o comm            .
+=*|o cp              (-i)
+=*|x cron            .
+#*|o cut             .
+=*|o date            .
+=*|o dirname         .
+=*|o du              .
+=*|o echo            .
+   o ed              .
+=*|o env             .
+#*|o expand          .
+#*|o expr            .
+=*|o false           .
+=    find            .
+=* x flock           .
+#*|o fold            .
+=*|o getconf         (-v)
+=*|o grep            .
+=*|o head            .
+=*|x hostname        .
+=*|x install         .
+=* o join            .
+=*|o kill            .
+=*|o link            .
+=*|o ln              .
+=*|o logger          .
+=*|o logname         .
+#* o ls              (-C, -k, -m, -p, -s, -x)
+=*|x md5sum          .
+=*|o mkdir           .
+=*|o mkfifo          .
+=*|x mktemp          .
+=*|o mv              (-i)
+=*|o nice            .
+#*|o nl              .
+=*|o nohup           .
+=*|o od              .
+#* o pathchk         .
+#*|o paste           .
+=*|x printenv        .
+#*|o printf          .
+=*|o pwd             .
+=*|x readlink        .
+=*|o renice          .
+=*|o rm              (-i)
+=*|o rmdir           .
+#    sed             .
+=*|x seq             .
+=*|x setsid          .
+=*|x sha1sum         .
+=* x sha224sum       .
+=*|x sha256sum       .
+=* x sha238sum       .
+=*|x sha512sum       .
+=* x sha512-224sum   .
+=* x sha512-256sum   .
+=*|o sleep           .
+#*|o sort            .
+=*|o split           .
+=*|x sponge          .
+#*|o strings         .
+=*|x sync            .
+=*|o tail            .
+=*|x tar             .
+=*|o tee             .
+=*|o test            .
+=*|x tftp            .
+=*|o time            .
+=*|o touch           .
+#*|o tr              .
+=*|o true            .
+=* o tsort           .
+=*|o tty             .
+=*|o uname           .
+#*|o unexpand        .
+=*|o uniq            .
+=*|o unlink          .
+=*|o uudecode        .
+=*|o uuencode        .
+#*|o wc              .
+=*|x which           .
+=*|x whoami          .
+=*|o xargs           (-p)
+=*|x yes             .
 
 The  complement of  sbase  is  ubase[1] which  is  Linux-specific  and
 provides all  the non-portable tools.   Together they are  intended to
diff --git a/libutil/sha512-224.c b/libutil/sha512-224.c
new file mode 100644
index 0000000..a5636c1
--- /dev/null
+++ b/libutil/sha512-224.c
_AT_@ -0,0 +1,26 @@
+/* public domain sha512/224 implementation based on fips180-3 */
+#include <stdint.h>
+#include "../sha512-224.h"
+
+extern void sha512_sum_n(void *, uint8_t *, int n);
+
+void
+sha512_224_init(void *ctx)
+{
+	struct sha512_224 *s = ctx;
+	s->len = 0;
+	s->h[0] = 0x8c3d37c819544da2ULL;
+	s->h[1] = 0x73e1996689dcd4d6ULL;
+	s->h[2] = 0x1dfab7ae32ff9c82ULL;
+	s->h[3] = 0x679dd514582f9fcfULL;
+	s->h[4] = 0x0f6d2b697bd44da8ULL;
+	s->h[5] = 0x77e36f7304c48942ULL;
+	s->h[6] = 0x3f9d85a86a1d36c8ULL;
+	s->h[7] = 0x1112e6ad91d692a1ULL;
+}
+
+void
+sha512_224_sum(void *ctx, uint8_t md[SHA512_224_DIGEST_LENGTH])
+{
+	sha512_sum_n(ctx, md, 4);
+}
diff --git a/libutil/sha512-256.c b/libutil/sha512-256.c
new file mode 100644
index 0000000..d4b8449
--- /dev/null
+++ b/libutil/sha512-256.c
_AT_@ -0,0 +1,26 @@
+/* public domain sha512/256 implementation based on fips180-3 */
+#include <stdint.h>
+#include "../sha512-256.h"
+
+extern void sha512_sum_n(void *, uint8_t *, int n);
+
+void
+sha512_256_init(void *ctx)
+{
+	struct sha512_256 *s = ctx;
+	s->len = 0;
+	s->h[0] = 0x22312194fc2bf72cULL;
+	s->h[1] = 0x9f555fa3c84c64c2ULL;
+	s->h[2] = 0x2393b86b6f53b151ULL;
+	s->h[3] = 0x963877195940eabdULL;
+	s->h[4] = 0x96283ee2a88effe3ULL;
+	s->h[5] = 0xbe5e1e2553863992ULL;
+	s->h[6] = 0x2b0199fc2c85b8aaULL;
+	s->h[7] = 0x0eb72ddc81c52ca2ULL;
+}
+
+void
+sha512_256_sum(void *ctx, uint8_t md[SHA512_256_DIGEST_LENGTH])
+{
+	sha512_sum_n(ctx, md, 4);
+}
diff --git a/sha512-224.h b/sha512-224.h
new file mode 100644
index 0000000..8364fc5
--- /dev/null
+++ b/sha512-224.h
_AT_@ -0,0 +1,16 @@
+/* public domain sha512/224 implementation based on fips180-3 */
+
+#include "sha512.h"
+
+#define sha512_224  sha512  /*struct*/
+
+enum { SHA512_224_DIGEST_LENGTH = 28 };
+
+/* reset state */
+void sha512_224_init(void *ctx);
+/* process message */
+#define sha512_224_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 sha512_224_sum(void *ctx, uint8_t md[SHA512_224_DIGEST_LENGTH]);
diff --git a/sha512-224sum.1 b/sha512-224sum.1
new file mode 100644
index 0000000..5092582
--- /dev/null
+++ b/sha512-224sum.1
_AT_@ -0,0 +1,32 @@
+.Dd 2016-02-24
+.Dt SHA512-224SUM 1
+.Os sbase
+.Sh NAME
+.Nm sha512-224sum
+.Nd compute or check SHA-512/224 message digests
+.Sh SYNOPSIS
+.Nm
+.Op Fl c
+.Op Ar file ...
+.Sh DESCRIPTION
+.Nm
+writes SHA-512/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-512/224 checksums from each
+.Ar file
+and check them.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.El
diff --git a/sha512-224sum.c b/sha512-224sum.c
new file mode 100644
index 0000000..6e4a9d6
--- /dev/null
+++ b/sha512-224sum.c
_AT_@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <stdio.h>
+
+#include "crypt.h"
+#include "sha512-224.h"
+#include "util.h"
+
+static struct sha512_224 s;
+struct crypt_ops sha512_224_ops = {
+	sha512_224_init,
+	sha512_224_update,
+	sha512_224_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[SHA512_224_DIGEST_LENGTH];
+
+	ARGBEGIN {
+	case 'c':
+		cryptfunc = cryptcheck;
+		break;
+	default:
+		usage();
+	} ARGEND
+
+	ret |= cryptfunc(argc, argv, &sha512_224_ops, md, sizeof(md));
+	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+	return ret;
+}
diff --git a/sha512-256.h b/sha512-256.h
new file mode 100644
index 0000000..eb0b731
--- /dev/null
+++ b/sha512-256.h
_AT_@ -0,0 +1,16 @@
+/* public domain sha512/256 implementation based on fips180-3 */
+
+#include "sha512.h"
+
+#define sha512_256  sha512  /*struct*/
+
+enum { SHA512_256_DIGEST_LENGTH = 32 };
+
+/* reset state */
+void sha512_256_init(void *ctx);
+/* process message */
+#define sha512_256_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 sha512_256_sum(void *ctx, uint8_t md[SHA512_256_DIGEST_LENGTH]);
diff --git a/sha512-256sum.1 b/sha512-256sum.1
new file mode 100644
index 0000000..db0406d
--- /dev/null
+++ b/sha512-256sum.1
_AT_@ -0,0 +1,32 @@
+.Dd 2016-02-24
+.Dt SHA512-256SUM 1
+.Os sbase
+.Sh NAME
+.Nm sha512-256sum
+.Nd compute or check SHA-512/256 message digests
+.Sh SYNOPSIS
+.Nm
+.Op Fl c
+.Op Ar file ...
+.Sh DESCRIPTION
+.Nm
+writes SHA-512/256 (256-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-512/256 checksums from each
+.Ar file
+and check them.
+If no
+.Ar file
+is given
+.Nm
+reads from stdin.
+.El
diff --git a/sha512-256sum.c b/sha512-256sum.c
new file mode 100644
index 0000000..c2d582e
--- /dev/null
+++ b/sha512-256sum.c
_AT_@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <stdio.h>
+
+#include "crypt.h"
+#include "sha512-256.h"
+#include "util.h"
+
+static struct sha512_256 s;
+struct crypt_ops sha512_256_ops = {
+	sha512_256_init,
+	sha512_256_update,
+	sha512_256_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[SHA512_256_DIGEST_LENGTH];
+
+	ARGBEGIN {
+	case 'c':
+		cryptfunc = cryptcheck;
+		break;
+	default:
+		usage();
+	} ARGEND
+
+	ret |= cryptfunc(argc, argv, &sha512_256_ops, md, sizeof(md));
+	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+	return ret;
+}
-- 
2.7.1
Received on Wed Feb 24 2016 - 11:36:59 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 24 2016 - 11:48:09 CET