From 341e79ebf4d72742c0f89fb66bffbce20b36b269 Mon Sep 17 00:00:00 2001 From: Georgios Vasilakis Date: Tue, 11 Jul 2023 13:47:56 +0200 Subject: [PATCH] tests: crypto: Remove unused random configurations The crypto tests only use the external_rng function and they only test for one configuration where MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled. This removes the unused code and configurations. Signed-off-by: Georgios Vasilakis --- tests/crypto/src/common_test.c | 88 +--------------------------------- tests/crypto/src/common_test.h | 18 +------ 2 files changed, 4 insertions(+), 102 deletions(-) diff --git a/tests/crypto/src/common_test.c b/tests/crypto/src/common_test.c index bab224ee2fe0..d73bf2f9ad27 100644 --- a/tests/crypto/src/common_test.c +++ b/tests/crypto/src/common_test.c @@ -39,29 +39,13 @@ const size_t test_vector_name_offset[] = { offsetof(test_vector_ecjpake_t, p_test_vector_name), }; -#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) -static int entropy_func(void *ctx, unsigned char *buf, size_t len) -{ - return entropy_get_entropy(ctx, buf, len); -} -#endif - -#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) - /* Dummy context since we don't use it in the external_rng function */ char drbg_ctx; -int init_drbg(const unsigned char *p_optional_seed, size_t len) -{ - if (p_optional_seed != NULL) { - return -EINVAL; - } - - return 0; -} - int external_rng(void *ctx, unsigned char *output, size_t len) { + /* No context is required for the nrf_cc3xx_platform library */ + (void) ctx; int out_len; int ret = nrf_cc3xx_platform_ctr_drbg_get(NULL, output, len, &out_len); return ret; @@ -69,74 +53,6 @@ int external_rng(void *ctx, unsigned char *output, size_t len) int (*drbg_random)(void *, unsigned char *, size_t) = &external_rng; -#elif defined(MBEDTLS_CTR_DRBG_C) -mbedtls_ctr_drbg_context drbg_ctx; -int (*drbg_random)(void *, unsigned char *, size_t) = &mbedtls_ctr_drbg_random; - -int init_drbg(const unsigned char *p_optional_seed, size_t len) -{ - static const unsigned char ncs_seed[] = "ncs_drbg_seed"; - - const unsigned char *p_seed; - - if (p_optional_seed == NULL) { - p_seed = ncs_seed; - len = sizeof(ncs_seed); - } else { - p_seed = p_optional_seed; - } - - const struct device *p_device = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)); - - if (!device_is_ready(p_device)) { - return -ENODEV; - } - - // Ensure previously run test is properly deallocated - // (This frees the mutex inside ctr_drbg context) - mbedtls_ctr_drbg_free(&drbg_ctx); - mbedtls_ctr_drbg_init(&drbg_ctx); - return mbedtls_ctr_drbg_seed(&drbg_ctx, entropy_func, (void *)p_device, - p_seed, len); -} -#elif defined(MBEDTLS_HMAC_DRBG_C) - -mbedtls_hmac_drbg_context drbg_ctx; -int (*drbg_random)(void *, unsigned char *, size_t) = &mbedtls_hmac_drbg_random; - -int init_drbg(const unsigned char *p_optional_seed, size_t len) -{ - static const unsigned char ncs_seed[] = "ncs_drbg_seed"; - - const unsigned char *p_seed; - - if (p_optional_seed == NULL) { - p_seed = ncs_seed; - len = sizeof(ncs_seed); - } else { - p_seed = p_optional_seed; - } - - // Ensure previously run test is properly deallocated - // (This frees the mutex inside hmac_drbg context) - mbedtls_hmac_drbg_free(&drbg_ctx); - mbedtls_hmac_drbg_init(&drbg_ctx); - - const struct device *p_device = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)); - - if (!device_is_ready(p_device)) { - return -ENODEV; - } - - const mbedtls_md_info_t *p_info = - mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); - - return mbedtls_hmac_drbg_seed(&drbg_ctx, p_info, entropy_func, - (void *)p_device, p_seed, len); -} - -#endif - const char *get_vector_name(const test_case_t *tc, uint32_t v) { uint32_t tv_offset = test_vector_sizes[tc->vector_type] * v; diff --git a/tests/crypto/src/common_test.h b/tests/crypto/src/common_test.h index 0c93c03f946a..0fe8c6e43ccf 100644 --- a/tests/crypto/src/common_test.h +++ b/tests/crypto/src/common_test.h @@ -52,26 +52,12 @@ int init_drbg(const unsigned char *p_optional_seed, size_t len); size_t hex2bin_safe(const char *hex, uint8_t *buf, size_t buflen); #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) - #include "psa/crypto.h" #include "nrf_cc3xx_platform_ctr_drbg.h" - extern char drbg_ctx; -#elif defined(MBEDTLS_CTR_DRBG_C) - -#include - -/* This context will be populated by init_drbg. - * If the initialization is successful it can be used in subsequent calls to - * mbedtls drbg APIs. - */ -extern mbedtls_ctr_drbg_context drbg_ctx; - -#elif defined(MBEDTLS_HMAC_DRBG_C) - -#include -extern mbedtls_hmac_drbg_context drbg_ctx; +#else +#error "No RNG is enabled, MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG needs to be defined in nrf-config.h" #endif