diff --git a/subsys/random/rand32_ctr_drbg.c b/subsys/random/rand32_ctr_drbg.c index 1f292dffad5..be780c0e0ba 100644 --- a/subsys/random/rand32_ctr_drbg.c +++ b/subsys/random/rand32_ctr_drbg.c @@ -28,7 +28,7 @@ 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; #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) { - return entropy_get_entropy(entropy_driver, (void *)buf, len); + return entropy_get_entropy(entropy_dev, (void *)buf, len); } #elif defined(CONFIG_TINYCRYPT) @@ -51,16 +51,9 @@ static int ctr_drbg_initialize(void) { int ret; - /* Only one entropy device exists, so this is safe even - * if the whole operation isn't atomic. - */ - 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 (!device_is_ready(entropy_dev)) { + __ASSERT(0, "Entropy device %s not ready", entropy_dev->name); + return -ENODEV; } #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]; - ret = entropy_get_entropy(entropy_driver, (void *)&entropy, + ret = entropy_get_entropy(entropy_dev, (void *)&entropy, sizeof(entropy)); if (ret != 0) { return -EIO; @@ -109,7 +102,7 @@ int z_impl_sys_csrand_get(void *dst, uint32_t outlen) int ret; unsigned int key = irq_lock(); - if (unlikely(!entropy_driver)) { + if (unlikely(!entropy_dev)) { ret = ctr_drbg_initialize(); if (ret != 0) { ret = -EIO; @@ -131,7 +124,7 @@ int z_impl_sys_csrand_get(void *dst, uint32_t outlen) ret = 0; } else if (ret == TC_CTR_PRNG_RESEED_REQ) { - ret = entropy_get_entropy(entropy_driver, + ret = entropy_get_entropy(entropy_dev, (void *)&entropy, sizeof(entropy)); if (ret != 0) { ret = -EIO;