Skip to content

Commit bff97fb

Browse files
JordanYateshenrikbrixandersen
authored andcommitted
random: sys_csrand_get backend for TEST_RANDOM_GENERATOR
When non-random number generation is allowed via `TEST_RANDOM_GENERATOR`, enable an implementation for `sys_csrand_get` that stubs out to `sys_rand_get`. This enables libraries that request CS random numbers to be tested in CI, even if the results are not CS in that context. The documentation for `TEST_RANDOM_GENERATOR` is explicit enough about the dangers of enabling this in production. Signed-off-by: Jordan Yates <[email protected]>
1 parent fdcdc5d commit bff97fb

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

subsys/random/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ zephyr_library()
88
zephyr_library_sources_ifdef(CONFIG_USERSPACE random_handlers.c)
99
endif()
1010

11-
if (CONFIG_TIMER_RANDOM_GENERATOR)
11+
if (CONFIG_TIMER_RANDOM_GENERATOR OR CONFIG_TEST_CSPRNG_GENERATOR)
1212
message(WARNING "
13-
Warning: CONFIG_TIMER_RANDOM_GENERATOR is not a truly random generator.
14-
This capability is not secure and it is provided for testing purposes only.
15-
Use it carefully.")
13+
Warning: CONFIG_TIMER_RANDOM_GENERATOR and CONFIG_TEST_CSPRNG_GENERATOR are
14+
not truly random generators. This capability is not secure and it is
15+
provided for testing purposes only. Use it carefully.")
1616
endif()
1717

1818
zephyr_library_sources_ifdef(CONFIG_TIMER_RANDOM_GENERATOR random_timer.c)
1919
zephyr_library_sources_ifdef(CONFIG_XOSHIRO_RANDOM_GENERATOR random_xoshiro128.c)
2020
zephyr_library_sources_ifdef(CONFIG_CTR_DRBG_CSPRNG_GENERATOR random_ctr_drbg.c)
21+
zephyr_library_sources_ifdef(CONFIG_TEST_CSPRNG_GENERATOR random_test_csprng.c)
2122

2223
if (CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR OR CONFIG_HARDWARE_DEVICE_CS_GENERATOR)
2324
zephyr_library_sources(random_entropy_device.c)

subsys/random/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ config CSPRNG_ENABLED
8888
choice CSPRNG_GENERATOR_CHOICE
8989
prompt "Cryptographically secure random generator"
9090
default HARDWARE_DEVICE_CS_GENERATOR
91+
default TEST_CSPRNG_GENERATOR
9192
help
9293
Platform dependent cryptographically secure random number support.
9394

@@ -116,6 +117,13 @@ config CTR_DRBG_CSPRNG_GENERATOR
116117
is a FIPS140-2 recommended cryptographically secure random number
117118
generator.
118119

120+
config TEST_CSPRNG_GENERATOR
121+
bool "Use insecure CSPRNG for testing purposes"
122+
depends on TEST_RANDOM_GENERATOR
123+
help
124+
Route calls to `sys_csrand_get` through `sys_rand_get` to enable
125+
libraries that use the former to be tested with ZTEST.
126+
119127
endchoice # CSPRNG_GENERATOR_CHOICE
120128

121129
config CS_CTR_DRBG_PERSONALIZATION

subsys/random/random_test_csprng.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2024 Embeint Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/random/random.h>
8+
9+
int z_impl_sys_csrand_get(void *dst, size_t outlen)
10+
{
11+
sys_rand_get(dst, outlen);
12+
return 0;
13+
}

0 commit comments

Comments
 (0)