From: Ian Jackson Date: Sun, 29 Sep 2019 12:47:39 +0000 (+0100) Subject: hash: Provide and use hash_hash connvenience function X-Git-Tag: v0.5.0~138 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=secnet.git;a=commitdiff_plain;h=ee43fb13660683cdbcebd2c58faf7a067cb26745;ds=sidebyside hash: Provide and use hash_hash connvenience function No functional change. Signed-off-by: Ian Jackson --- diff --git a/md5.c b/md5.c index caaa5a6..ca9ddb1 100644 --- a/md5.c +++ b/md5.c @@ -22,6 +22,7 @@ */ #include "secnet.h" +#include "util.h" #include /* for memcpy() */ #include "md5.h" @@ -287,10 +288,7 @@ void md5_module(dict_t *dict) dict_add(dict,"md5",new_closure(&st->cl)); - uint8_t ctx[st->ops.slen]; - md5_init(ctx); - md5_update(ctx,testinput,strlen(testinput)); - md5_final(ctx,digest); + hash_hash(&st->ops,testinput,strlen(testinput),digest); for (i=0; i<16; i++) { if (digest[i]!=expected[i]) { fatal("md5 module failed self-test"); diff --git a/rsa.c b/rsa.c index c87fcb7..ccfc556 100644 --- a/rsa.c +++ b/rsa.c @@ -92,10 +92,7 @@ static void rsa_priv_sethash(void *sst, struct hash_if *hash) } static void rsa_hash(struct rsacommon *c, const uint8_t *buf, int32_t len) { - uint8_t hst[c->hashi->slen]; - c->hashi->init(hst); - c->hashi->update(hst,buf,len); - c->hashi->final(hst,c->hashbuf); + hash_hash(c->hashi,buf,len,c->hashbuf); } static void emsa_pkcs1(MP_INT *n, MP_INT *m, diff --git a/sha1.c b/sha1.c index b11c25a..f442e08 100644 --- a/sha1.c +++ b/sha1.c @@ -78,6 +78,7 @@ A million repetitions of "a" /* #define SHA1HANDSOFF */ #include "secnet.h" +#include "util.h" #include #include @@ -340,10 +341,7 @@ void sha1_module(dict_t *dict) dict_add(dict,"sha1",new_closure(&st->cl)); - uint8_t ctx[st->ops.slen]; - sha1_init(ctx); - sha1_update(ctx,testinput,strlen(testinput)); - sha1_final(ctx,digest); + hash_hash(&st->ops,testinput,strlen(testinput),digest); for (i=0; i<20; i++) { if (digest[i]!=expected[i]) { fatal("sha1 module failed self-test"); diff --git a/util.c b/util.c index 5200e18..4982c93 100644 --- a/util.c +++ b/util.c @@ -484,6 +484,14 @@ int consttime_memeq(const void *s1in, const void *s2in, size_t n) return accumulator; } +void hash_hash(const struct hash_if *hashi, const void *msg, int32_t len, + uint8_t *digest) { + uint8_t hst[hashi->slen]; + hashi->init(hst); + hashi->update(hst,msg,len); + hashi->final(hst,digest); +} + void util_module(dict_t *dict) { add_closure(dict,"sysbuffer",buffer_apply); diff --git a/util.h b/util.h index eb9e51f..598e2b6 100644 --- a/util.h +++ b/util.h @@ -124,6 +124,9 @@ extern void send_nak(const struct comm_addr *dest, uint32_t our_index, extern int consttime_memeq(const void *s1, const void *s2, size_t n); +void hash_hash(const struct hash_if *hashi, const void *msg, int32_t len, + uint8_t *digest /* hi->hlen bytes */); + const char *iaddr_to_string(const union iaddr *ia); int iaddr_socklen(const union iaddr *ia);