random: rand32_ctr_drbg: obtain entropy device using DEVICE_DT_GET

The entropy device can be obtained at compile time. Adjusted the
assertion message slightly, as it no longer uses the entropy macro
label. Also adjusted the entropy_driver variable to entropy_dev, since
it does not hold a "driver" but a "device".

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-02-25 18:43:41 +01:00 committed by Marti Bolivar
commit f44b3dc4df

View file

@ -28,7 +28,7 @@
static K_SEM_DEFINE(state_sem, 1, 1); static K_SEM_DEFINE(state_sem, 1, 1);
static const struct device *entropy_driver; 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 const unsigned char drbg_seed[] = CONFIG_CS_CTR_DRBG_PERSONALIZATION;
#if defined(CONFIG_MBEDTLS) #if defined(CONFIG_MBEDTLS)
@ -37,7 +37,7 @@ static mbedtls_ctr_drbg_context ctr_ctx;
static int ctr_drbg_entropy_func(void *ctx, unsigned char *buf, size_t len) static int ctr_drbg_entropy_func(void *ctx, unsigned char *buf, size_t len)
{ {
return entropy_get_entropy(entropy_driver, (void *)buf, len); return entropy_get_entropy(entropy_dev, (void *)buf, len);
} }
#elif defined(CONFIG_TINYCRYPT) #elif defined(CONFIG_TINYCRYPT)
@ -51,16 +51,9 @@ static int ctr_drbg_initialize(void)
{ {
int ret; int ret;
/* Only one entropy device exists, so this is safe even if (!device_is_ready(entropy_dev)) {
* if the whole operation isn't atomic. __ASSERT(0, "Entropy device %s not ready", entropy_dev->name);
*/ return -ENODEV;
entropy_driver = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
if (!entropy_driver) {
__ASSERT((entropy_driver != NULL),
"Device driver for %s (DT_CHOSEN_ZEPHYR_ENTROPY_LABEL) not found. "
"Check your build configuration!",
DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
return -EINVAL;
} }
#if defined(CONFIG_MBEDTLS) #if defined(CONFIG_MBEDTLS)
@ -82,7 +75,7 @@ static int ctr_drbg_initialize(void)
uint8_t entropy[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE]; uint8_t entropy[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
ret = entropy_get_entropy(entropy_driver, (void *)&entropy, ret = entropy_get_entropy(entropy_dev, (void *)&entropy,
sizeof(entropy)); sizeof(entropy));
if (ret != 0) { if (ret != 0) {
return -EIO; return -EIO;
@ -109,7 +102,7 @@ int z_impl_sys_csrand_get(void *dst, uint32_t outlen)
int ret; int ret;
unsigned int key = irq_lock(); unsigned int key = irq_lock();
if (unlikely(!entropy_driver)) { if (unlikely(!entropy_dev)) {
ret = ctr_drbg_initialize(); ret = ctr_drbg_initialize();
if (ret != 0) { if (ret != 0) {
ret = -EIO; ret = -EIO;
@ -131,7 +124,7 @@ int z_impl_sys_csrand_get(void *dst, uint32_t outlen)
ret = 0; ret = 0;
} else if (ret == TC_CTR_PRNG_RESEED_REQ) { } else if (ret == TC_CTR_PRNG_RESEED_REQ) {
ret = entropy_get_entropy(entropy_driver, ret = entropy_get_entropy(entropy_dev,
(void *)&entropy, sizeof(entropy)); (void *)&entropy, sizeof(entropy));
if (ret != 0) { if (ret != 0) {
ret = -EIO; ret = -EIO;