From 38c2f892edffdfc539c2efb21abd539e9c75a1b2 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Tue, 28 Dec 2021 09:54:02 +1000 Subject: [PATCH] random: ctr_drbg: initialize on first run With f44b3dc4dfef4ba0adde064e910f35521f09232f statically initializing the entropy_dev variable, `ctr_drbg_initialize` was not being run on the first call to `z_impl_sys_csrand_get`. Use a dedicated static bool to track whether the init needs to be run. Signed-off-by: Jordan Yates --- subsys/random/rand32_ctr_drbg.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/subsys/random/rand32_ctr_drbg.c b/subsys/random/rand32_ctr_drbg.c index be780c0e0ba..53018aeff4f 100644 --- a/subsys/random/rand32_ctr_drbg.c +++ b/subsys/random/rand32_ctr_drbg.c @@ -30,6 +30,7 @@ static K_SEM_DEFINE(state_sem, 1, 1); static const struct device *entropy_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)); static const unsigned char drbg_seed[] = CONFIG_CS_CTR_DRBG_PERSONALIZATION; +static bool ctr_initialised; #if defined(CONFIG_MBEDTLS) @@ -92,7 +93,7 @@ static int ctr_drbg_initialize(void) } #endif - + ctr_initialised = true; return 0; } @@ -102,7 +103,7 @@ int z_impl_sys_csrand_get(void *dst, uint32_t outlen) int ret; unsigned int key = irq_lock(); - if (unlikely(!entropy_dev)) { + if (unlikely(!ctr_initialised)) { ret = ctr_drbg_initialize(); if (ret != 0) { ret = -EIO;