Skip to content

Commit

Permalink
tests: crypto: Remove unused random configurations
Browse files Browse the repository at this point in the history
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 <georgios.vasilakis@nordicsemi.no>
  • Loading branch information
Vge0rge authored and nordicjm committed Jul 13, 2023
1 parent cddc15d commit 341e79e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 102 deletions.
88 changes: 2 additions & 86 deletions tests/crypto/src/common_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,104 +39,20 @@ 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;
}

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;
Expand Down
18 changes: 2 additions & 16 deletions tests/crypto/src/common_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mbedtls/ctr_drbg.h>

/* 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 <mbedtls/hmac_drbg.h>
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

Expand Down

0 comments on commit 341e79e

Please sign in to comment.