entropy: native_posix: Add kconfig to not seed by default

Provide a new kconfig option which can be used to disable
the default seeding of the host standard library random
generator by this driver.
This allows some other component to do so without this component
default initialization interfering.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2024-09-03 17:52:04 +02:00 committed by Anas Nashif
parent 0ac91da3a4
commit e58a491900
2 changed files with 22 additions and 1 deletions

View File

@ -12,3 +12,12 @@ config FAKE_ENTROPY_NATIVE_POSIX
not generate real entropy.
It actually generates always the same sequence of random numbers if
initialized with the same seed.
config FAKE_ENTROPY_NATIVE_POSIX_SEED_BY_DEFAULT
bool "Seed the generator by default"
default y
depends on FAKE_ENTROPY_NATIVE_POSIX
help
Apply a seed by default, even if the user does not request it through the command line.
Disabling this feature allows some other component to seed the host standard library random
generator without this component's default initialization interfering.

View File

@ -27,6 +27,7 @@
static unsigned int seed = 0x5678;
static bool seed_random;
static bool seed_set;
static int entropy_native_posix_get_entropy(const struct device *dev,
uint8_t *buffer,
@ -69,7 +70,10 @@ static int entropy_native_posix_get_entropy_isr(const struct device *dev,
static int entropy_native_posix_init(const struct device *dev)
{
ARG_UNUSED(dev);
entropy_native_seed(seed, seed_random);
if (seed_set || seed_random ||
IS_ENABLED(CONFIG_FAKE_ENTROPY_NATIVE_POSIX_SEED_BY_DEFAULT)) {
entropy_native_seed(seed, seed_random);
}
posix_print_warning("WARNING: "
"Using a test - not safe - entropy source\n");
return 0;
@ -86,6 +90,13 @@ DEVICE_DT_INST_DEFINE(0,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_native_posix_api_funcs);
static void seed_was_set(char *argv, int offset)
{
ARG_UNUSED(argv);
ARG_UNUSED(offset);
seed_set = true;
}
static void add_fake_entropy_option(void)
{
static struct args_struct_t entropy_options[] = {
@ -94,6 +105,7 @@ static void add_fake_entropy_option(void)
.name = "r_seed",
.type = 'u',
.dest = (void *)&seed,
.call_when_found = seed_was_set,
.descript = "A 32-bit integer seed value for the entropy device, such as "
"97229 (decimal), 0x17BCD (hex), or 0275715 (octal)"
},