subsys/random: sys_rand32_get() implementation that uses entropy API
Instead of every hardware number generator driver providing an implementation of this function, use the random device API to centralize the implementation of this function. This is a very simplistic function that can be seen as a stepping stone to refactor the random number generation in Zephyr. Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
This commit is contained in:
parent
da9b0ddf5b
commit
8b883a61a3
10 changed files with 52 additions and 43 deletions
|
@ -9,7 +9,6 @@ config ENTROPY_ESP32_RNG
|
||||||
depends on ENTROPY_GENERATOR && SOC_ESP32
|
depends on ENTROPY_GENERATOR && SOC_ESP32
|
||||||
default n
|
default n
|
||||||
select ENTROPY_HAS_DRIVER
|
select ENTROPY_HAS_DRIVER
|
||||||
select ENTROPY_DEVICE_ENTROPY_GENERATOR
|
|
||||||
help
|
help
|
||||||
This option enables the entropy number generator for ESP32 SoCs.
|
This option enables the entropy number generator for ESP32 SoCs.
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ menuconfig ENTROPY_MCUX_RNGA
|
||||||
depends on ENTROPY_GENERATOR && HAS_MCUX_RNGA
|
depends on ENTROPY_GENERATOR && HAS_MCUX_RNGA
|
||||||
default n
|
default n
|
||||||
select ENTROPY_HAS_DRIVER
|
select ENTROPY_HAS_DRIVER
|
||||||
|
select ENTROPY_DEVICE_RANDOM_GENERATOR
|
||||||
help
|
help
|
||||||
This option enables the random number generator accelerator (RNGA)
|
This option enables the random number generator accelerator (RNGA)
|
||||||
driver based on the MCUX RNGA driver.
|
driver based on the MCUX RNGA driver.
|
||||||
|
@ -18,6 +19,7 @@ menuconfig ENTROPY_MCUX_TRNG
|
||||||
depends on ENTROPY_GENERATOR && HAS_MCUX_TRNG
|
depends on ENTROPY_GENERATOR && HAS_MCUX_TRNG
|
||||||
default n
|
default n
|
||||||
select ENTROPY_HAS_DRIVER
|
select ENTROPY_HAS_DRIVER
|
||||||
|
select ENTROPY_DEVICE_RANDOM_GENERATOR
|
||||||
help
|
help
|
||||||
This option enables the true random number generator (TRNG)
|
This option enables the true random number generator (TRNG)
|
||||||
driver based on the MCUX TRNG driver.
|
driver based on the MCUX TRNG driver.
|
||||||
|
|
|
@ -9,7 +9,6 @@ menuconfig ENTROPY_STM32_RNG
|
||||||
depends on ENTROPY_GENERATOR
|
depends on ENTROPY_GENERATOR
|
||||||
default n
|
default n
|
||||||
select ENTROPY_HAS_DRIVER
|
select ENTROPY_HAS_DRIVER
|
||||||
select ENTROPY_DEVICE_ENTROPY_GENERATOR
|
|
||||||
help
|
help
|
||||||
This option enables the RNG processor, which is a entropy number
|
This option enables the RNG processor, which is a entropy number
|
||||||
generator, based on a continuous analog noise, that provides
|
generator, based on a continuous analog noise, that provides
|
||||||
|
|
|
@ -57,8 +57,3 @@ DEVICE_AND_API_INIT(entropy_esp32, CONFIG_ENTROPY_NAME,
|
||||||
entropy_esp32_init, NULL, NULL,
|
entropy_esp32_init, NULL, NULL,
|
||||||
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
||||||
&entropy_esp32_api_funcs);
|
&entropy_esp32_api_funcs);
|
||||||
|
|
||||||
u32_t sys_rand32_get(void)
|
|
||||||
{
|
|
||||||
return random_esp32_get_u32();
|
|
||||||
}
|
|
||||||
|
|
|
@ -79,15 +79,3 @@ static int entropy_mcux_rnga_init(struct device *dev)
|
||||||
RNGA_SetMode(RNG, kRNGA_ModeSleep);
|
RNGA_SetMode(RNG, kRNGA_ModeSleep);
|
||||||
return 0;
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -50,15 +50,3 @@ static int entropy_mcux_trng_init(struct device *dev)
|
||||||
|
|
||||||
return 0;
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -181,15 +181,3 @@ DEVICE_AND_API_INIT(entropy_stm32_rng, CONFIG_ENTROPY_NAME,
|
||||||
&entropy_stm32_rng_data, &entropy_stm32_rng_config,
|
&entropy_stm32_rng_data, &entropy_stm32_rng_config,
|
||||||
PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
||||||
&entropy_stm32_rng_api);
|
&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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -33,3 +33,12 @@ config TIMER_RANDOM_GENERATOR
|
||||||
This options enables number generator based on system timer
|
This options enables number generator based on system timer
|
||||||
clock. This number generator is not random and used for
|
clock. This number generator is not random and used for
|
||||||
testing only.
|
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.
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
obj-$(CONFIG_TIMER_RANDOM_GENERATOR) = rand32_timer.o
|
obj-$(CONFIG_TIMER_RANDOM_GENERATOR) = rand32_timer.o
|
||||||
obj-$(CONFIG_X86_TSC_RANDOM_GENERATOR) += rand32_timestamp.o
|
obj-$(CONFIG_X86_TSC_RANDOM_GENERATOR) += rand32_timestamp.o
|
||||||
|
obj-$(CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR) += rand32_entropy_device.o
|
||||||
|
|
40
subsys/random/rand32_entropy_device.c
Normal file
40
subsys/random/rand32_entropy_device.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2017 Intel Corporation
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <atomic.h>
|
||||||
|
#include <kernel.h>
|
||||||
|
#include <entropy.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue