add sha512-224sum (SHA512/224) and sha512-256sum (SHA512/256)

Signed-off-by: Mattias Andrée <maandree@kth.se>
This commit is contained in:
Mattias Andrée 2016-02-24 11:36:59 +01:00 committed by sin
parent ae1da536bb
commit a392cd475e
10 changed files with 347 additions and 109 deletions

View File

@ -15,6 +15,8 @@ HDR =\
sha256.h\ sha256.h\
sha384.h\ sha384.h\
sha512.h\ sha512.h\
sha512-224.h\
sha512-256.h\
text.h\ text.h\
utf.h\ utf.h\
util.h util.h
@ -68,6 +70,8 @@ LIBUTILSRC =\
libutil/sha256.c\ libutil/sha256.c\
libutil/sha384.c\ libutil/sha384.c\
libutil/sha512.c\ libutil/sha512.c\
libutil/sha512-224.c\
libutil/sha512-256.c\
libutil/strcasestr.c\ libutil/strcasestr.c\
libutil/strlcat.c\ libutil/strlcat.c\
libutil/strlcpy.c\ libutil/strlcpy.c\
@ -141,6 +145,8 @@ BIN =\
sha256sum\ sha256sum\
sha384sum\ sha384sum\
sha512sum\ sha512sum\
sha512-224sum\
sha512-256sum\
sleep\ sleep\
sort\ sort\
split\ split\
@ -227,21 +233,21 @@ sbase-box: $(LIB) $(SRC)
mkdir -p build mkdir -p build
cp $(HDR) build cp $(HDR) build
cp confstr_l.h limits_l.h sysconf_l.h pathconf_l.h 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 for f in $(SRC); do sed "s/^main(/$$(echo "$${f%.c}" | sed s/-/_/g)_&/" < $$f > build/$$f; done
echo '#include <libgen.h>' > build/$@.c echo '#include <libgen.h>' > build/$@.c
echo '#include <stdio.h>' >> build/$@.c echo '#include <stdio.h>' >> build/$@.c
echo '#include <stdlib.h>' >> build/$@.c echo '#include <stdlib.h>' >> build/$@.c
echo '#include <string.h>' >> build/$@.c echo '#include <string.h>' >> build/$@.c
echo '#include "util.h"' >> build/$@.c echo '#include "util.h"' >> build/$@.c
for f in $(SRC); do echo "int $${f%.c}_main(int, char **);"; done >> build/$@.c for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed s/-/_/g)_main(int, char **);"; done >> build/$@.c
echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);' >> build/$@.c echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);' >> build/$@.c
echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;' >> build/$@.c echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;' >> build/$@.c
echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);" >> build/$@.c echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);" >> build/$@.c
echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);" >> build/$@.c echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);" >> build/$@.c
for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $${f%.c}_main(argc, argv);"; done >> build/$@.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/$@.c
echo 'else { fputs("[ ", stdout);' >> build/$@.c echo 'else { fputs("[ ", stdout);' >> build/$@.c
for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done >> build/$@.c for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done >> build/$@.c
echo 'putchar(0xa); }; return 0; }' >> build/$@.c echo 'putchar(0xa); }; return 0; }' >> build/$@.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ build/*.c $(LIB) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ build/*.c $(LIB)
rm -r build rm -r build

190
README
View File

@ -10,100 +10,102 @@ The following tools are implemented:
'|' -> Audited, 'o' -> POSIX 2013 compliant, 'x' -> Non-POSIX, '|' -> Audited, 'o' -> POSIX 2013 compliant, 'x' -> Non-POSIX,
'()' -> Petty flag '()' -> Petty flag
UTILITY MISSING UTILITY MISSING
------- ------- ------- -------
=*|o basename . =*|o basename .
=*|o cal . =*|o cal .
=*|o cat . =*|o cat .
=*|o chgrp . =*|o chgrp .
=*|o chmod . =*|o chmod .
=*|o chown . =*|o chown .
=*|x chroot . =*|x chroot .
=*|o cksum . =*|o cksum .
=*|o cmp . =*|o cmp .
#*|x cols . #*|x cols .
=*|o comm . =*|o comm .
=*|o cp (-i) =*|o cp (-i)
=*|x cron . =*|x cron .
#*|o cut . #*|o cut .
=*|o date . =*|o date .
=*|o dirname . =*|o dirname .
=*|o du . =*|o du .
=*|o echo . =*|o echo .
o ed . o ed .
=*|o env . =*|o env .
#*|o expand . #*|o expand .
#*|o expr . #*|o expr .
=*|o false . =*|o false .
= find . = find .
=* x flock . =* x flock .
#*|o fold . #*|o fold .
=*|o getconf (-v) =*|o getconf (-v)
=*|o grep . =*|o grep .
=*|o head . =*|o head .
=*|x hostname . =*|x hostname .
=*|x install . =*|x install .
=* o join . =* o join .
=*|o kill . =*|o kill .
=*|o link . =*|o link .
=*|o ln . =*|o ln .
=*|o logger . =*|o logger .
=*|o logname . =*|o logname .
#* o ls (-C, -k, -m, -p, -s, -x) #* o ls (-C, -k, -m, -p, -s, -x)
=*|x md5sum . =*|x md5sum .
=*|o mkdir . =*|o mkdir .
=*|o mkfifo . =*|o mkfifo .
=*|x mktemp . =*|x mktemp .
=*|o mv (-i) =*|o mv (-i)
=*|o nice . =*|o nice .
#*|o nl . #*|o nl .
=*|o nohup . =*|o nohup .
=*|o od . =*|o od .
#* o pathchk . #* o pathchk .
#*|o paste . #*|o paste .
=*|x printenv . =*|x printenv .
#*|o printf . #*|o printf .
=*|o pwd . =*|o pwd .
=*|x readlink . =*|x readlink .
=*|o renice . =*|o renice .
=*|o rm (-i) =*|o rm (-i)
=*|o rmdir . =*|o rmdir .
# sed . # sed .
=*|x seq . =*|x seq .
=*|x setsid . =*|x setsid .
=*|x sha1sum . =*|x sha1sum .
=* x sha224sum . =* x sha224sum .
=*|x sha256sum . =*|x sha256sum .
=* x sha238sum . =* x sha238sum .
=*|x sha512sum . =*|x sha512sum .
=*|o sleep . =* x sha512-224sum .
#*|o sort . =* x sha512-256sum .
=*|o split . =*|o sleep .
=*|x sponge . #*|o sort .
#*|o strings . =*|o split .
=*|x sync . =*|x sponge .
=*|o tail . #*|o strings .
=*|x tar . =*|x sync .
=*|o tee . =*|o tail .
=*|o test . =*|x tar .
=*|x tftp . =*|o tee .
=*|o time . =*|o test .
=*|o touch . =*|x tftp .
#*|o tr . =*|o time .
=*|o true . =*|o touch .
=* o tsort . #*|o tr .
=*|o tty . =*|o true .
=*|o uname . =* o tsort .
#*|o unexpand . =*|o tty .
=*|o uniq . =*|o uname .
=*|o unlink . #*|o unexpand .
=*|o uudecode . =*|o uniq .
=*|o uuencode . =*|o unlink .
#*|o wc . =*|o uudecode .
=*|x which . =*|o uuencode .
=*|x whoami . #*|o wc .
=*|o xargs (-p) =*|x which .
=*|x yes . =*|x whoami .
=*|o xargs (-p)
=*|x yes .
The complement of sbase is ubase[1] which is Linux-specific and The complement of sbase is ubase[1] which is Linux-specific and
provides all the non-portable tools. Together they are intended to provides all the non-portable tools. Together they are intended to

26
libutil/sha512-224.c Normal file
View File

@ -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);
}

26
libutil/sha512-256.c Normal file
View File

@ -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);
}

16
sha512-224.h Normal file
View File

@ -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]);

32
sha512-224sum.1 Normal file
View File

@ -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

41
sha512-224sum.c Normal file
View File

@ -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;
}

16
sha512-256.h Normal file
View File

@ -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]);

32
sha512-256sum.1 Normal file
View File

@ -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

41
sha512-256sum.c Normal file
View File

@ -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;
}