zephyr/drivers/entropy/fake_entropy_native_bottom.c
Alberto Escolar Piedras 404db20877 drivers/entropy/native: Define required SOURCE macro
The srandom function is not available
unless _XOPEN_SOURCE is set > 500 or an equivalent
declaration is done.
Let's set it.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-26 07:48:55 -05:00

38 lines
854 B
C

/*
* 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
*/
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#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);
}
}