diff --git a/drivers/entropy/Kconfig.esp32 b/drivers/entropy/Kconfig.esp32 index 69ae063d770..ce001d04f0e 100644 --- a/drivers/entropy/Kconfig.esp32 +++ b/drivers/entropy/Kconfig.esp32 @@ -9,7 +9,6 @@ config ENTROPY_ESP32_RNG depends on ENTROPY_GENERATOR && SOC_ESP32 default n select ENTROPY_HAS_DRIVER - select ENTROPY_DEVICE_ENTROPY_GENERATOR help This option enables the entropy number generator for ESP32 SoCs. diff --git a/drivers/entropy/Kconfig.mcux b/drivers/entropy/Kconfig.mcux index 73ce6e0d4c9..0977a843a0f 100644 --- a/drivers/entropy/Kconfig.mcux +++ b/drivers/entropy/Kconfig.mcux @@ -9,6 +9,7 @@ menuconfig ENTROPY_MCUX_RNGA depends on ENTROPY_GENERATOR && HAS_MCUX_RNGA default n select ENTROPY_HAS_DRIVER + select ENTROPY_DEVICE_RANDOM_GENERATOR help This option enables the random number generator accelerator (RNGA) driver based on the MCUX RNGA driver. @@ -18,6 +19,7 @@ menuconfig ENTROPY_MCUX_TRNG depends on ENTROPY_GENERATOR && HAS_MCUX_TRNG default n select ENTROPY_HAS_DRIVER + select ENTROPY_DEVICE_RANDOM_GENERATOR help This option enables the true random number generator (TRNG) driver based on the MCUX TRNG driver. diff --git a/drivers/entropy/Kconfig.stm32 b/drivers/entropy/Kconfig.stm32 index a342a205d31..516a094ecb5 100644 --- a/drivers/entropy/Kconfig.stm32 +++ b/drivers/entropy/Kconfig.stm32 @@ -9,7 +9,6 @@ menuconfig ENTROPY_STM32_RNG depends on ENTROPY_GENERATOR default n select ENTROPY_HAS_DRIVER - select ENTROPY_DEVICE_ENTROPY_GENERATOR help This option enables the RNG processor, which is a entropy number generator, based on a continuous analog noise, that provides diff --git a/drivers/entropy/entropy_esp32.c b/drivers/entropy/entropy_esp32.c index 10a860a6c7c..3d48601313b 100644 --- a/drivers/entropy/entropy_esp32.c +++ b/drivers/entropy/entropy_esp32.c @@ -57,8 +57,3 @@ DEVICE_AND_API_INIT(entropy_esp32, CONFIG_ENTROPY_NAME, entropy_esp32_init, NULL, NULL, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &entropy_esp32_api_funcs); - -u32_t sys_rand32_get(void) -{ - return random_esp32_get_u32(); -} diff --git a/drivers/entropy/entropy_mcux_rnga.c b/drivers/entropy/entropy_mcux_rnga.c index 6b7a0834c28..67ef463c886 100644 --- a/drivers/entropy/entropy_mcux_rnga.c +++ b/drivers/entropy/entropy_mcux_rnga.c @@ -79,15 +79,3 @@ static int entropy_mcux_rnga_init(struct device *dev) RNGA_SetMode(RNG, kRNGA_ModeSleep); return 0; } - -u32_t sys_rand32_get(void) -{ - u32_t output; - int r; - - r = random_mcux_rnga_get_entropy(DEVICE_GET(random_mcux_rnga), - (u8_t *) &output, sizeof(output)); - __ASSERT_NO_MSG(!r); - - return output; -} diff --git a/drivers/entropy/entropy_mcux_trng.c b/drivers/entropy/entropy_mcux_trng.c index 933c43f452f..c69143c0475 100644 --- a/drivers/entropy/entropy_mcux_trng.c +++ b/drivers/entropy/entropy_mcux_trng.c @@ -50,15 +50,3 @@ static int entropy_mcux_trng_init(struct device *dev) return 0; } - -u32_t sys_rand32_get(void) -{ - u32_t output; - int rc; - - rc = random_mcux_trng_get_entropy(DEVICE_GET(random_mcux_trng), - (u8_t *) &output, sizeof(output)); - __ASSERT_NO_MSG(!rc); - - return output; -} diff --git a/drivers/entropy/entropy_stm32.c b/drivers/entropy/entropy_stm32.c index 6c2efe4beca..b1edf3b476e 100644 --- a/drivers/entropy/entropy_stm32.c +++ b/drivers/entropy/entropy_stm32.c @@ -181,15 +181,3 @@ DEVICE_AND_API_INIT(entropy_stm32_rng, CONFIG_ENTROPY_NAME, &entropy_stm32_rng_data, &entropy_stm32_rng_config, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &entropy_stm32_rng_api); - -u32_t sys_rand32_get(void) -{ - u32_t output; - int rc; - - rc = random_stm32_rng_get_entropy(DEVICE_GET(random_stm32_rng), - (u8_t *) &output, sizeof(output)); - __ASSERT_NO_MSG(!rc); - - return output; -} diff --git a/subsys/random/Kconfig b/subsys/random/Kconfig index 2157066cf55..516c67957ca 100644 --- a/subsys/random/Kconfig +++ b/subsys/random/Kconfig @@ -33,3 +33,12 @@ config TIMER_RANDOM_GENERATOR This options enables number generator based on system timer clock. This number generator is not random and used for testing only. + +config ENTROPY_DEVICE_RANDOM_GENERATOR + bool + prompt "Use entropy driver to generate random numbers" + depends on ENTROPY_HAS_DRIVER + help + Enables a random number generator that uses the enabled + hardware entropy gathering driver to generate random + numbers. diff --git a/subsys/random/Makefile b/subsys/random/Makefile index cd9e16a011b..033cdd85e8d 100644 --- a/subsys/random/Makefile +++ b/subsys/random/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_TIMER_RANDOM_GENERATOR) = rand32_timer.o obj-$(CONFIG_X86_TSC_RANDOM_GENERATOR) += rand32_timestamp.o +obj-$(CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR) += rand32_entropy_device.o diff --git a/subsys/random/rand32_entropy_device.c b/subsys/random/rand32_entropy_device.c new file mode 100644 index 00000000000..8883c76d078 --- /dev/null +++ b/subsys/random/rand32_entropy_device.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +static atomic_t entropy_driver; + +u32_t sys_rand32_get(void) +{ + struct device *dev = (struct device *)atomic_get(&entropy_driver); + u32_t random_num; + int ret; + + if (unlikely(!dev)) { + /* Only one entropy device exists, so this is safe even + * if the whole operation isn't atomic. + */ + dev = device_get_binding(CONFIG_ENTROPY_NAME); + atomic_set(&entropy_driver, (atomic_t)(uintptr_t)dev); + } + + ret = entropy_get_entropy(dev, (u8_t *)&random_num, + sizeof(random_num)); + if (unlikely(ret < 0)) { + /* Use system timer in case the entropy device couldn't deliver + * 32-bit of data. There's not much that can be done in this + * situation. An __ASSERT() isn't used here as the HWRNG might + * still be gathering entropy during early boot situations. + */ + + random_num = k_cycle_get_32(); + } + + return random_num; +}