sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit ae1da536bb25e7af5e6a6b246e9895178cfe8c2e
parent 6adb9b8ccd9a557efb98837c89661ed69aacc984
Author: Mattias Andrée <maandree@kth.se>
Date:   Wed, 24 Feb 2016 10:05:31 +0100

add sha224sum and sha384sum

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
MMakefile | 6++++++
MREADME | 2++
Alibutil/sha224.c | 26++++++++++++++++++++++++++
Mlibutil/sha256.c | 10++++++++--
Alibutil/sha384.c | 26++++++++++++++++++++++++++
Mlibutil/sha512.c | 10++++++++--
Asha224.h | 16++++++++++++++++
Asha224sum.1 | 32++++++++++++++++++++++++++++++++
Asha224sum.c | 41+++++++++++++++++++++++++++++++++++++++++
Asha384.h | 16++++++++++++++++
Asha384sum.1 | 32++++++++++++++++++++++++++++++++
Asha384sum.c | 41+++++++++++++++++++++++++++++++++++++++++
12 files changed, 254 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -11,7 +11,9 @@ HDR =\ md5.h\ queue.h\ sha1.h\ + sha224.h\ sha256.h\ + sha384.h\ sha512.h\ text.h\ utf.h\ @@ -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\ @@ -133,7 +137,9 @@ BIN =\ seq\ setsid\ sha1sum\ + sha224sum\ sha256sum\ + sha384sum\ sha512sum\ sleep\ sort\ diff --git a/README b/README @@ -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 @@ -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 @@ -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; @@ -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 @@ -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 @@ -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; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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; +}