From 3f949d8e662ea209c19fe75ae689a906e59ab5b4 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 4 Sep 2023 21:40:02 +0000 Subject: [PATCH] crypto: Add helper functions for hashing --- include/crypto.h | 10 ++++++++++ src/crypto-nettle.c | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/crypto.h b/include/crypto.h index d93cbad..f2a1f14 100644 --- a/include/crypto.h +++ b/include/crypto.h @@ -23,6 +23,11 @@ enum crypto_hash_type { CRYPTO_HASH_SHA1, }; +struct crypto_data_entry { + uint8_t* data; + size_t len; +}; + void crypto_dump_base16(const char* msg, const uint8_t* bytes, size_t len); void crypto_dump_base64(const char* msg, const uint8_t* bytes, size_t len); @@ -73,6 +78,11 @@ void crypto_hash_append(struct crypto_hash* self, const uint8_t* src, void crypto_hash_digest(struct crypto_hash* self, uint8_t* dst, size_t len); +void crypto_hash_one(uint8_t* dst, size_t dst_len, enum crypto_hash_type type, + const uint8_t* src, size_t src_len); +void crypto_hash_many(uint8_t* dst, size_t dst_len, enum crypto_hash_type type, + const struct crypto_data_entry *src); + // RSA struct crypto_rsa_pub_key* crypto_rsa_pub_key_new(void); void crypto_rsa_pub_key_del(struct crypto_rsa_pub_key*); diff --git a/src/crypto-nettle.c b/src/crypto-nettle.c index db04241..ea34637 100644 --- a/src/crypto-nettle.c +++ b/src/crypto-nettle.c @@ -471,6 +471,25 @@ void crypto_hash_digest(struct crypto_hash* self, uint8_t* dst, size_t len) self->digest(&self->ctx, len, dst); } +void crypto_hash_one(uint8_t* dst, size_t dst_len, enum crypto_hash_type type, + const uint8_t* src, size_t src_len) +{ + struct crypto_hash *hash = crypto_hash_new(type); + crypto_hash_append(hash, src, src_len); + crypto_hash_digest(hash, dst, dst_len); + crypto_hash_del(hash); +} + +void crypto_hash_many(uint8_t* dst, size_t dst_len, enum crypto_hash_type type, + const struct crypto_data_entry *src) +{ + struct crypto_hash *hash = crypto_hash_new(type); + for (int i = 0; src[i].data && src[i].len; ++i) + crypto_hash_append(hash, src[i].data, src[i].len); + crypto_hash_digest(hash, dst, dst_len); + crypto_hash_del(hash); +} + struct crypto_rsa_pub_key *crypto_rsa_pub_key_new(void) { struct crypto_rsa_pub_key* self = calloc(1, sizeof(*self));