[SECNET RFC PATCH 2/5] hash: Provide and use hash_hash connvenience function

Ian Jackson ijackson at chiark.greenend.org.uk
Sun Sep 29 14:47:18 BST 2019


No functional change.

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
 md5.c  | 6 ++----
 rsa.c  | 5 +----
 sha1.c | 6 ++----
 util.c | 8 ++++++++
 util.h | 3 +++
 5 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/md5.c b/md5.c
index caaa5a68..ca9ddb1a 100644
--- a/md5.c
+++ b/md5.c
@@ -22,6 +22,7 @@
  */
 
 #include "secnet.h"
+#include "util.h"
 #include <string.h>		/* 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 c87fcb76..ccfc5569 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 b11c25a6..f442e08c 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 <stdio.h>
 #include <string.h>
 
@@ -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 5200e18e..4982c93c 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 eb9e51f1..598e2b65 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);
 
-- 
2.11.0




More information about the sgo-software-discuss mailing list