random: ctr_drbg: initialize on first run

With f44b3dc4df 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 <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-12-28 09:54:02 +10:00 committed by Carles Cufí
parent fd618351bf
commit 38c2f892ed

View File

@ -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;