From 59703c95fb529386574a334c9cff7cd5d4f16255 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 19 Feb 2023 13:18:52 +0100 Subject: lib: Refactor hash internals The internal hash enum now matches the public one w.r.t. directory hash policies. This removes some unnecessary conversion. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- include/ouroboros/hash.h | 23 ++++++--------------- include/ouroboros/ipcp.h | 2 +- src/ipcpd/ipcp.c | 22 ++------------------ src/lib/dev.c | 3 +++ src/lib/hash.c | 54 +++++++++++++++++++++++++++--------------------- 5 files changed, 42 insertions(+), 62 deletions(-) diff --git a/include/ouroboros/hash.h b/include/ouroboros/hash.h index 248da12d..70d2eba0 100644 --- a/include/ouroboros/hash.h +++ b/include/ouroboros/hash.h @@ -26,30 +26,19 @@ #include "config.h" #include +#include -#ifdef HAVE_LIBGCRYPT -#include -#endif #include #include /* Hash algorithms */ enum hash_algo { -#ifdef HAVE_LIBGCRYPT - HASH_CRC32 = GCRY_MD_CRC32, - HASH_MD5 = GCRY_MD_MD5, - HASH_SHA3_224 = GCRY_MD_SHA3_224, - HASH_SHA3_256 = GCRY_MD_SHA3_256, - HASH_SHA3_384 = GCRY_MD_SHA3_384, - HASH_SHA3_512 = GCRY_MD_SHA3_512 -#else - HASH_CRC32 = 0, + HASH_SHA3_224 = DIR_HASH_SHA3_224, + HASH_SHA3_256 = DIR_HASH_SHA3_256, + HASH_SHA3_384 = DIR_HASH_SHA3_384, + HASH_SHA3_512 = DIR_HASH_SHA3_512, + HASH_CRC32, HASH_MD5, - HASH_SHA3_224, - HASH_SHA3_256, - HASH_SHA3_384, - HASH_SHA3_512 -#endif }; #define HASH_FMT "%02x%02x%02x%02x" diff --git a/include/ouroboros/ipcp.h b/include/ouroboros/ipcp.h index e2ae0c0e..49deeffd 100644 --- a/include/ouroboros/ipcp.h +++ b/include/ouroboros/ipcp.h @@ -57,7 +57,7 @@ enum pol_cong_avoid { }; enum pol_dir_hash { - DIR_HASH_SHA3_224 = 0, + DIR_HASH_SHA3_224, DIR_HASH_SHA3_256, DIR_HASH_SHA3_384, DIR_HASH_SHA3_512 diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 1e777637..5d9c6e7a 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -288,6 +288,8 @@ static void handle_bootstrap(ipcp_config_msg_t * conf_msg, ipcp_type = conf_msg->ipcp_type; + conf.layer_info.dir_hash_algo = conf_msg->layer_info->dir_hash_algo; + switch(ipcp_type) { case IPCP_LOCAL: break; @@ -322,26 +324,6 @@ static void handle_bootstrap(ipcp_config_msg_t * conf_msg, return; } - /* UDP and broadcast use fixed hash algorithm. */ - if (ipcp_type != IPCP_UDP && ipcp_type != IPCP_BROADCAST) { - switch(conf_msg->layer_info->dir_hash_algo) { - case DIR_HASH_SHA3_224: - conf.layer_info.dir_hash_algo = HASH_SHA3_224; - break; - case DIR_HASH_SHA3_256: - conf.layer_info.dir_hash_algo = HASH_SHA3_256; - break; - case DIR_HASH_SHA3_384: - conf.layer_info.dir_hash_algo = HASH_SHA3_384; - break; - case DIR_HASH_SHA3_512: - conf.layer_info.dir_hash_algo = HASH_SHA3_512; - break; - default: - assert(false); - } - } - ret_msg->result = ipcpi.ops->ipcp_bootstrap(&conf); if (ret_msg->result == 0) { layer_info_msg->layer_name = strdup(conf.layer_info.layer_name); diff --git a/src/lib/dev.c b/src/lib/dev.c index 360b65d5..c46bbdd7 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -50,6 +50,9 @@ #include #endif +#ifdef HAVE_LIBGCRYPT +#include +#endif #include #include #include diff --git a/src/lib/hash.c b/src/lib/hash.c index 438c43f4..12a8b09f 100644 --- a/src/lib/hash.c +++ b/src/lib/hash.c @@ -31,41 +31,47 @@ #include -#ifndef HAVE_LIBGCRYPT +#ifdef HAVE_LIBGCRYPT +#include +#else #include #include #include -#else -#include #endif #include #include #include +#ifdef HAVE_LIBGCRYPT +int gcry_algo_tbl [] = { + /* DIR_HASH policies first */ + GCRY_MD_SHA3_224, + GCRY_MD_SHA3_256, + GCRY_MD_SHA3_384, + GCRY_MD_SHA3_512, + /* Below for internal use only */ + GCRY_MD_CRC32, + GCRY_MD_MD5, +}; +#else +int hash_len_tbl [] = { + /* DIR_HASH policies first */ + SHA3_224_HASH_LEN, + SHA3_256_HASH_LEN, + SHA3_384_HASH_LEN, + SHA3_512_HASH_LEN, + /* Below for internal use only */ + CRC32_HASH_LEN, + MD5_HASH_LEN +}; +#endif + uint16_t hash_len(enum hash_algo algo) { #ifdef HAVE_LIBGCRYPT - return (uint16_t) gcry_md_get_algo_dlen(algo); + return (uint16_t) gcry_md_get_algo_dlen(gcry_algo_tbl[algo]); #else - switch (algo) { - case HASH_CRC32: - return CRC32_HASH_LEN; - case HASH_MD5: - return MD5_HASH_LEN; - case HASH_SHA3_224: - return SHA3_224_HASH_LEN; - case HASH_SHA3_256: - return SHA3_256_HASH_LEN; - case HASH_SHA3_384: - return SHA3_384_HASH_LEN; - case HASH_SHA3_512: - return SHA3_512_HASH_LEN; - default: - assert(false); - break; - } - - return 0; + return hash_len_tbl[algo]; #endif } @@ -75,7 +81,7 @@ void mem_hash(enum hash_algo algo, size_t len) { #ifdef HAVE_LIBGCRYPT - gcry_md_hash_buffer(algo, dst, buf, len); + gcry_md_hash_buffer(gcry_algo_tbl[algo], dst, buf, len); #else struct sha3_ctx sha3_ctx; struct md5_ctx md5_ctx; -- cgit v1.2.3