From 5ffc1ec42357745c34abff14222c5603e686fb2d Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 29 Jan 2021 16:48:03 +0100 Subject: [PATCH] botan: Extract helper function to map RNG quality to Botan RNG names --- src/libstrongswan/plugins/botan/botan_rng.c | 26 +++------------- src/libstrongswan/plugins/botan/botan_util.c | 32 ++++++++++++++++++++ src/libstrongswan/plugins/botan/botan_util.h | 8 +++++ 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/libstrongswan/plugins/botan/botan_rng.c b/src/libstrongswan/plugins/botan/botan_rng.c index c49225c3c..9d88b77a9 100644 --- a/src/libstrongswan/plugins/botan/botan_rng.c +++ b/src/libstrongswan/plugins/botan/botan_rng.c @@ -22,6 +22,7 @@ */ #include "botan_rng.h" +#include "botan_util.h" #include @@ -83,29 +84,12 @@ METHOD(rng_t, destroy, void, botan_random_t *botan_rng_create(rng_quality_t quality) { private_botan_random_t *this; - const char* rng_name; + const char *rng_name; - switch (quality) + rng_name = botan_map_rng_quality(quality); + if (!rng_name) { - case RNG_WEAK: - case RNG_STRONG: - /* some rng_t instances of this class (e.g. in the ike-sa-manager) - * may be called concurrently by different threads. the Botan RNGs - * are not reentrant, by default, so use the threadsafe version. - * because we build without threading support when running tests - * with leak-detective (lots of reports of frees of unknown memory) - * there is a fallback to the default */ -#ifdef BOTAN_TARGET_OS_HAS_THREADS - rng_name = "user-threadsafe"; -#else - rng_name = "user"; -#endif - break; - case RNG_TRUE: - rng_name = "system"; - break; - default: - return NULL; + return NULL; } INIT(this, diff --git a/src/libstrongswan/plugins/botan/botan_util.c b/src/libstrongswan/plugins/botan/botan_util.c index f5728e43e..9414a2cc8 100644 --- a/src/libstrongswan/plugins/botan/botan_util.c +++ b/src/libstrongswan/plugins/botan/botan_util.c @@ -313,3 +313,35 @@ bool botan_dh_key_derivation(botan_privkey_t key, chunk_t pub, chunk_t *secret) botan_pk_op_key_agreement_destroy(ka); return TRUE; } + +/* + * Described in header + */ +const char *botan_map_rng_quality(rng_quality_t quality) +{ + const char *rng_name; + + switch (quality) + { + case RNG_WEAK: + case RNG_STRONG: + /* some rng_t instances of this class (e.g. in the ike-sa-manager) + * may be called concurrently by different threads. the Botan RNGs + * are not reentrant, by default, so use the threadsafe version. + * because we build without threading support when running tests + * with leak-detective (lots of reports of frees of unknown memory) + * there is a fallback to the default */ +#ifdef BOTAN_TARGET_OS_HAS_THREADS + rng_name = "user-threadsafe"; +#else + rng_name = "user"; +#endif + break; + case RNG_TRUE: + rng_name = "system"; + break; + default: + return NULL; + } + return rng_name; +} diff --git a/src/libstrongswan/plugins/botan/botan_util.h b/src/libstrongswan/plugins/botan/botan_util.h index 7fb74ec5d..fe8a9a8ee 100644 --- a/src/libstrongswan/plugins/botan/botan_util.h +++ b/src/libstrongswan/plugins/botan/botan_util.h @@ -125,4 +125,12 @@ bool botan_verify_signature(botan_pubkey_t key, const char* scheme, */ bool botan_dh_key_derivation(botan_privkey_t key, chunk_t pub, chunk_t *secret); +/** + * Map the given RNG quality to a name as used by Botan. + * + * @param quality RNG quality + * @return name of the Botan RNG + */ +const char *botan_map_rng_quality(rng_quality_t quality); + #endif /** BOTAN_UTIL_H_ @}*/