random: Remove duplicated code
All implementations of random number generator where duplicating logic for sys_rand32_get. Since this subsystem already has a logic to generate random values of arbitrary size, we can generically implement sys_rand32_get on top of that. Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
This commit is contained in:
parent
d5eb328544
commit
d68db182b9
|
@ -38,16 +38,6 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Return a 32-bit random value that should pass general
|
||||
* randomness tests.
|
||||
*
|
||||
* @note The random value returned is not a cryptographically secure
|
||||
* random number value.
|
||||
*
|
||||
* @return 32-bit random value.
|
||||
*/
|
||||
__syscall uint32_t sys_rand32_get(void);
|
||||
|
||||
/**
|
||||
* @brief Fill the destination buffer with random data values that should
|
||||
|
@ -77,6 +67,25 @@ __syscall void sys_rand_get(void *dst, size_t len);
|
|||
*/
|
||||
__syscall int sys_csrand_get(void *dst, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Return a 32-bit random value that should pass general
|
||||
* randomness tests.
|
||||
*
|
||||
* @note The random value returned is not a cryptographically secure
|
||||
* random number value.
|
||||
*
|
||||
* @return 32-bit random value.
|
||||
*/
|
||||
static inline uint32_t sys_rand32_get(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
sys_rand_get(&ret, sizeof(ret));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -12,31 +12,6 @@
|
|||
static const struct device *const entropy_dev =
|
||||
DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy));
|
||||
|
||||
#if defined(CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR)
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
{
|
||||
uint32_t random_num;
|
||||
int ret;
|
||||
|
||||
__ASSERT(device_is_ready(entropy_dev), "Entropy device %s not ready",
|
||||
entropy_dev->name);
|
||||
|
||||
ret = entropy_get_entropy(entropy_dev, (uint8_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;
|
||||
}
|
||||
#endif /* CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR */
|
||||
|
||||
static int rand_get(uint8_t *dst, size_t outlen, bool csrand)
|
||||
{
|
||||
uint32_t random_num;
|
||||
|
|
|
@ -7,12 +7,6 @@
|
|||
#include <zephyr/random/random.h>
|
||||
#include <zephyr/internal/syscall_handler.h>
|
||||
|
||||
static inline uint32_t z_vrfy_sys_rand32_get(void)
|
||||
{
|
||||
return z_impl_sys_rand32_get();
|
||||
}
|
||||
#include <syscalls/sys_rand32_get_mrsh.c>
|
||||
|
||||
static inline void z_vrfy_sys_rand_get(void *dst, size_t len)
|
||||
{
|
||||
K_OOPS(K_SYSCALL_MEMORY_WRITE(dst, len));
|
||||
|
|
|
@ -33,7 +33,7 @@ static struct k_spinlock rand32_lock;
|
|||
*
|
||||
* @return a 32-bit number
|
||||
*/
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
static inline uint32_t rand32_get(void)
|
||||
{
|
||||
/* initial seed value */
|
||||
static uint64_t state = (uint64_t)CONFIG_TIMER_RANDOM_INITIAL_STATE;
|
||||
|
@ -64,7 +64,7 @@ void z_impl_sys_rand_get(void *dst, size_t outlen)
|
|||
uint32_t ret;
|
||||
|
||||
while (outlen) {
|
||||
ret = sys_rand32_get();
|
||||
ret = rand32_get();
|
||||
blocksize = MIN(outlen, sizeof(ret));
|
||||
(void)memcpy((void *)udst, &ret, blocksize);
|
||||
udst += blocksize;
|
||||
|
|
|
@ -91,15 +91,6 @@ static uint32_t xoshiro128_next(void)
|
|||
return result;
|
||||
}
|
||||
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
{
|
||||
if (unlikely(!initialized)) {
|
||||
xoshiro128_init_state();
|
||||
}
|
||||
|
||||
return xoshiro128_next();
|
||||
}
|
||||
|
||||
void z_impl_sys_rand_get(void *dst, size_t outlen)
|
||||
{
|
||||
size_t blocks = outlen / sizeof(uint32_t);
|
||||
|
|
Loading…
Reference in a new issue