drivers entropy fake_native: Add option to seed generator randomly
Add a command line option which will seed the random generator from /dev/urandom. This can be usefull for some particular tests in which we are interested in having different random numbers in each run, but we cannot provide a different random seed from command line. Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
parent
3fc5d971fe
commit
c6ed39e3ad
|
@ -17,7 +17,15 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c)
|
|||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SMARTBOND_TRNG entropy_smartbond.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_LITEX_RNG entropy_litex.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_FAKE_ENTROPY_NATIVE_POSIX fake_entropy_native_posix.c)
|
||||
if(CONFIG_FAKE_ENTROPY_NATIVE_POSIX)
|
||||
zephyr_library_sources(fake_entropy_native_posix.c)
|
||||
if(CONFIG_NATIVE_LIBRARY)
|
||||
target_sources(native_simulator INTERFACE fake_entropy_native_bottom.c)
|
||||
else()
|
||||
zephyr_library_sources(fake_entropy_native_bottom.c)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_TRNG entropy_gecko_trng.c)
|
||||
|
|
34
drivers/entropy/fake_entropy_native_bottom.c
Normal file
34
drivers/entropy/fake_entropy_native_bottom.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Bottom/Linux side of the pseudo-random entropy generator for
|
||||
* ARCH_POSIX architecture
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/random.h>
|
||||
#include "nsi_tracing.h"
|
||||
|
||||
void entropy_native_seed(unsigned int seed, bool seed_random)
|
||||
{
|
||||
if (seed_random == false) {
|
||||
srandom(seed);
|
||||
} else {
|
||||
unsigned int buf;
|
||||
int err = getrandom(&buf, sizeof(buf), 0);
|
||||
|
||||
if (err != sizeof(buf)) {
|
||||
nsi_print_error_and_exit("Could not get random number (%i, %s)\n",
|
||||
err, strerror(errno));
|
||||
}
|
||||
srandom(buf);
|
||||
|
||||
/* Let's print the seed so users can still reproduce the run if they need to */
|
||||
nsi_print_trace("Random generator seeded with 0x%X\n", buf);
|
||||
}
|
||||
}
|
21
drivers/entropy/fake_entropy_native_bottom.h
Normal file
21
drivers/entropy/fake_entropy_native_bottom.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef DRIVERS_ENTROPY_FAKE_ENTROPY_NATIVE_BOTTOM_H
|
||||
#define DRIVERS_ENTROPY_FAKE_ENTROPY_NATIVE_BOTTOM_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void entropy_native_seed(unsigned int seed, bool seed_random);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DRIVERS_ENTROPY_FAKE_ENTROPY_NATIVE_BOTTOM_H */
|
|
@ -23,8 +23,10 @@
|
|||
#include "soc.h"
|
||||
#include "cmdline.h" /* native_posix command line options header */
|
||||
#include "nsi_host_trampolines.h"
|
||||
#include "fake_entropy_native_bottom.h"
|
||||
|
||||
static unsigned int seed = 0x5678;
|
||||
static bool seed_random;
|
||||
|
||||
static int entropy_native_posix_get_entropy(const struct device *dev,
|
||||
uint8_t *buffer,
|
||||
|
@ -67,7 +69,7 @@ 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);
|
||||
nsi_host_srandom(seed);
|
||||
entropy_native_seed(seed, seed_random);
|
||||
posix_print_warning("WARNING: "
|
||||
"Using a test - not safe - entropy source\n");
|
||||
return 0;
|
||||
|
@ -87,18 +89,22 @@ DEVICE_DT_INST_DEFINE(0,
|
|||
static void add_fake_entropy_option(void)
|
||||
{
|
||||
static struct args_struct_t entropy_options[] = {
|
||||
/*
|
||||
* Fields:
|
||||
* manual, mandatory, switch,
|
||||
* option_name, var_name ,type,
|
||||
* destination, callback,
|
||||
* description
|
||||
*/
|
||||
{false, false, false,
|
||||
"seed", "r_seed", 'u',
|
||||
(void *)&seed, NULL,
|
||||
"A 32-bit integer seed value for the entropy device, such as "
|
||||
"97229 (decimal), 0x17BCD (hex), or 0275715 (octal)"},
|
||||
{
|
||||
.option = "seed",
|
||||
.name = "r_seed",
|
||||
.type = 'u',
|
||||
.dest = (void *)&seed,
|
||||
.descript = "A 32-bit integer seed value for the entropy device, such as "
|
||||
"97229 (decimal), 0x17BCD (hex), or 0275715 (octal)"
|
||||
},
|
||||
{
|
||||
.is_switch = true,
|
||||
.option = "seed-random",
|
||||
.type = 'b',
|
||||
.dest = (void *)&seed_random,
|
||||
.descript = "Seed the random generator from /dev/urandom. "
|
||||
"Note your test may not be reproducible if you set this option"
|
||||
},
|
||||
ARG_TABLE_ENDMARKER
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue