random: Change testing random generator
The old random timer test was not random-looking enough on some platforms. Replace with new test which is psuedo-xoshiro. The generator is still deterministic and does not depend on entropy at all, but should look more random for testing. Change name of generator tree-wide also. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
This commit is contained in:
parent
e065050f1b
commit
d6881de3b3
8 changed files with 44 additions and 21 deletions
|
@ -47,9 +47,9 @@ An override of the default value can be specified in the SOC or board
|
||||||
|
|
||||||
The random number generators available include:
|
The random number generators available include:
|
||||||
|
|
||||||
:kconfig:option:`CONFIG_TIMER_RANDOM_GENERATOR`
|
:kconfig:option:`CONFIG_RANDOM_TEST_GENERATOR`
|
||||||
enables number generator based on system timer clock. This number
|
This number generator is not random and used for testing only.
|
||||||
generator is not random and used for testing only.
|
This generator does not depend on entropy.
|
||||||
|
|
||||||
:kconfig:option:`CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR`
|
:kconfig:option:`CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR`
|
||||||
enables a random number generator that uses the enabled hardware
|
enables a random number generator that uses the enabled hardware
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
CONFIG_TIMER_RANDOM_GENERATOR=y
|
CONFIG_RANDOM_TEST_GENERATOR=y
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR
|
if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR
|
||||||
CONFIG_TIMER_RANDOM_GENERATOR OR
|
CONFIG_RANDOM_TEST_GENERATOR OR
|
||||||
CONFIG_XOSHIRO_RANDOM_GENERATOR)
|
CONFIG_XOSHIRO_RANDOM_GENERATOR)
|
||||||
zephyr_library()
|
zephyr_library()
|
||||||
zephyr_library_sources_ifdef(CONFIG_USERSPACE rand32_handlers.c)
|
zephyr_library_sources_ifdef(CONFIG_USERSPACE rand32_handlers.c)
|
||||||
|
@ -17,10 +17,10 @@ endif()
|
||||||
# XOROSHIRO builds the XOSHIRO implementation because a Kconfig choice cannot
|
# XOROSHIRO builds the XOSHIRO implementation because a Kconfig choice cannot
|
||||||
# select another choice as a means of deprecating the symbol. Swapping out the
|
# select another choice as a means of deprecating the symbol. Swapping out the
|
||||||
# implementation lets out-of-tree users still build until the symbol is removed.
|
# implementation lets out-of-tree users still build until the symbol is removed.
|
||||||
zephyr_library_sources_ifdef(CONFIG_TIMER_RANDOM_GENERATOR rand32_timer.c)
|
|
||||||
zephyr_library_sources_ifdef(CONFIG_XOROSHIRO_RANDOM_GENERATOR rand32_xoshiro128.c)
|
zephyr_library_sources_ifdef(CONFIG_XOROSHIRO_RANDOM_GENERATOR rand32_xoshiro128.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_XOSHIRO_RANDOM_GENERATOR rand32_xoshiro128.c)
|
zephyr_library_sources_ifdef(CONFIG_XOSHIRO_RANDOM_GENERATOR rand32_xoshiro128.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_CTR_DRBG_CSPRNG_GENERATOR rand32_ctr_drbg.c)
|
zephyr_library_sources_ifdef(CONFIG_CTR_DRBG_CSPRNG_GENERATOR rand32_ctr_drbg.c)
|
||||||
|
zephyr_library_sources_ifdef(CONFIG_RANDOM_TEST_GENERATOR rand32_test.c)
|
||||||
|
|
||||||
if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR CONFIG_HARDWARE_DEVICE_CS_GENERATOR)
|
if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR CONFIG_HARDWARE_DEVICE_CS_GENERATOR)
|
||||||
zephyr_library_sources(rand32_entropy_device.c)
|
zephyr_library_sources(rand32_entropy_device.c)
|
||||||
|
|
|
@ -40,13 +40,12 @@ choice RNG_GENERATOR_CHOICE
|
||||||
to support random request then select that. Otherwise, select the
|
to support random request then select that. Otherwise, select the
|
||||||
XOSHIRO algorithm
|
XOSHIRO algorithm
|
||||||
|
|
||||||
config TIMER_RANDOM_GENERATOR
|
config RANDOM_TEST_GENERATOR
|
||||||
bool "System timer clock based number generator"
|
bool "System timer clock based number generator"
|
||||||
depends on TEST_RANDOM_GENERATOR
|
depends on TEST_RANDOM_GENERATOR
|
||||||
help
|
help
|
||||||
This options enables number generator based on system timer
|
This number generator is not random and used only for testing.
|
||||||
clock. This number generator is not random and used for
|
Does not rely on an entropy driver, so can be used without entropy.
|
||||||
testing only.
|
|
||||||
|
|
||||||
config ENTROPY_DEVICE_RANDOM_GENERATOR
|
config ENTROPY_DEVICE_RANDOM_GENERATOR
|
||||||
bool "Use entropy driver to generate random numbers"
|
bool "Use entropy driver to generate random numbers"
|
||||||
|
|
|
@ -14,21 +14,44 @@
|
||||||
* provide a random number generator.
|
* provide a random number generator.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/init.h>
|
||||||
#include <zephyr/random/rand32.h>
|
#include <zephyr/random/rand32.h>
|
||||||
#include <zephyr/drivers/timer/system_timer.h>
|
#include <zephyr/drivers/timer/system_timer.h>
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/sys/atomic.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
|
|
||||||
/*
|
/* seed the state with pseudo-random data */
|
||||||
* Symbols used to ensure a rapid series of calls to random number generator
|
static uint32_t state[4] = {0, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278};
|
||||||
* return different values.
|
|
||||||
*/
|
|
||||||
static atomic_val_t _rand32_counter;
|
|
||||||
|
|
||||||
#define _RAND32_INC 1000000003U
|
static struct k_spinlock rand_state_lock;
|
||||||
|
|
||||||
|
static inline uint32_t rotl(const uint32_t x, int k)
|
||||||
|
{
|
||||||
|
return (x << k) | (x >> (32 - k));
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t xoshiro128_next(void)
|
||||||
|
{
|
||||||
|
const uint32_t result = rotl(state[0] + state[3], 7) + state[0];
|
||||||
|
const uint32_t t = state[1] << 9;
|
||||||
|
|
||||||
|
k_spinlock_key_t lock_key = k_spin_lock(&rand_state_lock);
|
||||||
|
|
||||||
|
state[2] ^= state[0];
|
||||||
|
state[3] ^= state[1];
|
||||||
|
state[1] ^= state[2];
|
||||||
|
state[0] ^= state[3];
|
||||||
|
|
||||||
|
state[2] ^= t;
|
||||||
|
state[3] = rotl(state[3], 11);
|
||||||
|
|
||||||
|
k_spin_unlock(&rand_state_lock, lock_key);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a 32 bit random number
|
* @brief Get a 32 bit random number
|
||||||
|
@ -41,7 +64,7 @@ static atomic_val_t _rand32_counter;
|
||||||
*/
|
*/
|
||||||
uint32_t z_impl_sys_rand32_get(void)
|
uint32_t z_impl_sys_rand32_get(void)
|
||||||
{
|
{
|
||||||
return k_cycle_get_32() + atomic_add(&_rand32_counter, _RAND32_INC);
|
return xoshiro128_next();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,4 +91,5 @@ void z_impl_sys_rand_get(void *dst, size_t outlen)
|
||||||
outlen -= blocksize;
|
outlen -= blocksize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* __GNUC__ */
|
#endif /* __GNUC__ */
|
|
@ -3,4 +3,4 @@ CONFIG_ZTEST_NEW_API=y
|
||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_ENTROPY_GENERATOR=y
|
CONFIG_ENTROPY_GENERATOR=y
|
||||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
CONFIG_TIMER_RANDOM_GENERATOR=y
|
CONFIG_RANDOM_TEST_GENERATOR=y
|
||||||
|
|
|
@ -2,7 +2,7 @@ CONFIG_NETWORKING=y
|
||||||
CONFIG_NET_TCP=y
|
CONFIG_NET_TCP=y
|
||||||
CONFIG_ENTROPY_GENERATOR=y
|
CONFIG_ENTROPY_GENERATOR=y
|
||||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
CONFIG_TIMER_RANDOM_GENERATOR=y
|
CONFIG_RANDOM_TEST_GENERATOR=y
|
||||||
|
|
||||||
CONFIG_NET_ARP=y
|
CONFIG_NET_ARP=y
|
||||||
CONFIG_NET_L2_ETHERNET=y
|
CONFIG_NET_L2_ETHERNET=y
|
||||||
|
|
|
@ -4,7 +4,7 @@ CONFIG_ZTEST_ASSERT_VERBOSE=0
|
||||||
|
|
||||||
CONFIG_ENTROPY_GENERATOR=y
|
CONFIG_ENTROPY_GENERATOR=y
|
||||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
CONFIG_TIMER_RANDOM_GENERATOR=y
|
CONFIG_RANDOM_TEST_GENERATOR=y
|
||||||
|
|
||||||
CONFIG_ZTEST_SHUFFLE=y
|
CONFIG_ZTEST_SHUFFLE=y
|
||||||
CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT=2
|
CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT=2
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue