From 6216c6cf5baa4c4997a5053d12779f51c37b6a1b Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Thu, 4 Jun 2020 11:00:45 -0700 Subject: [PATCH] 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 --- include/random/rand32.h | 9 +++++--- subsys/random/CMakeLists.txt | 1 + subsys/random/rand32_ctr_drbg.c | 2 +- subsys/random/rand32_entropy_device.c | 6 ++--- subsys/random/rand32_handlers.c | 33 +++++++++++++++++++++++++++ subsys/random/rand32_timer.c | 4 ++-- subsys/random/rand32_xoroshiro128.c | 4 ++-- 7 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 subsys/random/rand32_handlers.c diff --git a/include/random/rand32.h b/include/random/rand32.h index f1b18023b11..5daa68cb7e2 100644 --- a/include/random/rand32.h +++ b/include/random/rand32.h @@ -22,6 +22,7 @@ #include #include +#include /** * @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 #endif /* ZEPHYR_INCLUDE_RANDOM_RAND32_H_ */ diff --git a/subsys/random/CMakeLists.txt b/subsys/random/CMakeLists.txt index 026ba2dbf3e..1eb9688eecd 100644 --- a/subsys/random/CMakeLists.txt +++ b/subsys/random/CMakeLists.txt @@ -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) diff --git a/subsys/random/rand32_ctr_drbg.c b/subsys/random/rand32_ctr_drbg.c index 9c85b6d829e..7cfa20ea5b2 100644 --- a/subsys/random/rand32_ctr_drbg.c +++ b/subsys/random/rand32_ctr_drbg.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(); diff --git a/subsys/random/rand32_entropy_device.c b/subsys/random/rand32_entropy_device.c index 873d8c9e90d..4c57efe152e 100644 --- a/subsys/random/rand32_entropy_device.c +++ b/subsys/random/rand32_entropy_device.c @@ -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 diff --git a/subsys/random/rand32_handlers.c b/subsys/random/rand32_handlers.c new file mode 100644 index 00000000000..fe16a7a4e7d --- /dev/null +++ b/subsys/random/rand32_handlers.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + + +static inline uint32_t z_vrfy_sys_rand32_get(void) +{ + return z_impl_sys_rand32_get(); +} +#include + +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 + +#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 +#endif diff --git a/subsys/random/rand32_timer.c b/subsys/random/rand32_timer.c index e2439ab63ee..64995716d81 100644 --- a/subsys/random/rand32_timer.c +++ b/subsys/random/rand32_timer.c @@ -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; diff --git a/subsys/random/rand32_xoroshiro128.c b/subsys/random/rand32_xoroshiro128.c index 2fdded203cf..1f5dd45bcb7 100644 --- a/subsys/random/rand32_xoroshiro128.c +++ b/subsys/random/rand32_xoroshiro128.c @@ -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;