random: Add syscalls for random subsystem
Create syscalls to make possible using random APIs from user mode threads. These APIs can have different implementations, like using entropy driver or Xoroshiro128. Some of these implementations also have some globals to preserve state between calls. Make it run entire in user space would require user adding these globals to their memeory domains and/or grant access to entropy device. Syscalls simplify its usage. Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
This commit is contained in:
parent
177f2dac8b
commit
6216c6cf5b
7 changed files with 48 additions and 11 deletions
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <kernel.h>
|
||||
|
||||
/**
|
||||
* @brief Random Function APIs
|
||||
|
@ -43,7 +44,8 @@ extern "C" {
|
|||
*
|
||||
* @return 32-bit random value.
|
||||
*/
|
||||
extern uint32_t sys_rand32_get(void);
|
||||
__syscall uint32_t sys_rand32_get(void);
|
||||
|
||||
/**
|
||||
* @brief Fill the destination buffer with random data values that should
|
||||
* pass general randomness tests.
|
||||
|
@ -55,7 +57,7 @@ extern uint32_t sys_rand32_get(void);
|
|||
* @param len size of the destination buffer.
|
||||
*
|
||||
*/
|
||||
extern void sys_rand_get(void *dst, size_t len);
|
||||
__syscall void sys_rand_get(void *dst, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Fill the destination buffer with cryptographically secure
|
||||
|
@ -70,7 +72,7 @@ extern void sys_rand_get(void *dst, size_t len);
|
|||
* @return 0 if success, -EIO if entropy reseed error
|
||||
*
|
||||
*/
|
||||
extern int sys_csrand_get(void *dst, size_t len);
|
||||
__syscall int sys_csrand_get(void *dst, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -80,4 +82,5 @@ extern int sys_csrand_get(void *dst, size_t len);
|
|||
* @}
|
||||
*/
|
||||
|
||||
#include <syscalls/rand32.h>
|
||||
#endif /* ZEPHYR_INCLUDE_RANDOM_RAND32_H_ */
|
||||
|
|
|
@ -4,6 +4,7 @@ if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR
|
|||
CONFIG_TIMER_RANDOM_GENERATOR OR
|
||||
CONFIG_XOROSHIRO_RANDOM_GENERATOR)
|
||||
zephyr_library()
|
||||
zephyr_library_sources_ifdef(CONFIG_USERSPACE rand32_handlers.c)
|
||||
endif()
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_TIMER_RANDOM_GENERATOR rand32_timer.c)
|
||||
|
|
|
@ -104,7 +104,7 @@ static int ctr_drbg_initialize(void)
|
|||
}
|
||||
|
||||
|
||||
int sys_csrand_get(void *dst, uint32_t outlen)
|
||||
int z_impl_sys_csrand_get(void *dst, uint32_t outlen)
|
||||
{
|
||||
int ret;
|
||||
unsigned int key = irq_lock();
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
static struct device *entropy_driver;
|
||||
|
||||
#if defined(CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR)
|
||||
uint32_t sys_rand32_get(void)
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
{
|
||||
struct device *dev = entropy_driver;
|
||||
uint32_t random_num;
|
||||
|
@ -101,7 +101,7 @@ static int rand_get(uint8_t *dst, size_t outlen, bool csrand)
|
|||
}
|
||||
|
||||
#if defined(CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR)
|
||||
void sys_rand_get(void *dst, size_t outlen)
|
||||
void z_impl_sys_rand_get(void *dst, size_t outlen)
|
||||
{
|
||||
rand_get(dst, outlen, false);
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ void sys_rand_get(void *dst, size_t outlen)
|
|||
|
||||
#if defined(CONFIG_HARDWARE_DEVICE_CS_GENERATOR)
|
||||
|
||||
int sys_csrand_get(void *dst, size_t outlen)
|
||||
int z_impl_sys_csrand_get(void *dst, size_t outlen)
|
||||
{
|
||||
if (rand_get(dst, outlen, true) != 0) {
|
||||
/* Is it the only error it should return ? entropy_sam
|
||||
|
|
33
subsys/random/rand32_handlers.c
Normal file
33
subsys/random/rand32_handlers.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <random/rand32.h>
|
||||
#include <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)
|
||||
{
|
||||
Z_OOPS(Z_SYSCALL_MEMORY_WRITE(dst, len));
|
||||
|
||||
z_impl_sys_rand_get(dst, len);
|
||||
}
|
||||
#include <syscalls/sys_rand_get_mrsh.c>
|
||||
|
||||
#if defined(CONFIG_CTR_DRBG_CSPRNG_GENERATOR)
|
||||
static inline int z_vrfy_sys_csrand_get(void *dst, size_t len)
|
||||
{
|
||||
Z_OOPS(Z_SYSCALL_MEMORY_WRITE(dst, len));
|
||||
|
||||
return z_impl_sys_csrand_get(dst, len);
|
||||
}
|
||||
#include <syscalls/sys_csrand_get_mrsh.c>
|
||||
#endif
|
|
@ -41,7 +41,7 @@ static atomic_val_t _rand32_counter;
|
|||
* @return a 32-bit number
|
||||
*/
|
||||
|
||||
uint32_t sys_rand32_get(void)
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
{
|
||||
return k_cycle_get_32() + atomic_add(&_rand32_counter, _RAND32_INC);
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ uint32_t sys_rand32_get(void)
|
|||
* @return N/A
|
||||
*/
|
||||
|
||||
void sys_rand_get(void *dst, size_t outlen)
|
||||
void z_impl_sys_rand_get(void *dst, size_t outlen)
|
||||
{
|
||||
uint32_t len = 0;
|
||||
uint32_t blocksize = 4;
|
||||
|
|
|
@ -87,7 +87,7 @@ static uint32_t xoroshiro128_next(void)
|
|||
return (uint32_t)result;
|
||||
}
|
||||
|
||||
uint32_t sys_rand32_get(void)
|
||||
uint32_t z_impl_sys_rand32_get(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
|
@ -96,7 +96,7 @@ uint32_t sys_rand32_get(void)
|
|||
return ret;
|
||||
}
|
||||
|
||||
void sys_rand_get(void *dst, size_t outlen)
|
||||
void z_impl_sys_rand_get(void *dst, size_t outlen)
|
||||
{
|
||||
uint32_t ret;
|
||||
uint32_t blocksize = 4;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue