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>
diff --git a/include/zephyr/random/random.h b/include/zephyr/random/random.h
index e9c2e29..bbfdd98 100644
--- a/include/zephyr/random/random.h
+++ b/include/zephyr/random/random.h
@@ -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 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
diff --git a/subsys/random/rand32_entropy_device.c b/subsys/random/rand32_entropy_device.c
index 92ce441..2ffc569 100644
--- a/subsys/random/rand32_entropy_device.c
+++ b/subsys/random/rand32_entropy_device.c
@@ -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;
diff --git a/subsys/random/rand32_handlers.c b/subsys/random/rand32_handlers.c
index 38344a0..77bf068 100644
--- a/subsys/random/rand32_handlers.c
+++ b/subsys/random/rand32_handlers.c
@@ -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));
diff --git a/subsys/random/rand32_timer.c b/subsys/random/rand32_timer.c
index 4232dcc..9d2f5ce 100644
--- a/subsys/random/rand32_timer.c
+++ b/subsys/random/rand32_timer.c
@@ -33,7 +33,7 @@
*
* @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 @@
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;
diff --git a/subsys/random/rand32_xoshiro128.c b/subsys/random/rand32_xoshiro128.c
index 2a68f55..4264c78 100644
--- a/subsys/random/rand32_xoshiro128.c
+++ b/subsys/random/rand32_xoshiro128.c
@@ -91,15 +91,6 @@
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);