1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Bottom/Linux side of the pseudo-random entropy generator for the native simulator
7  */
8 
9 #undef _XOPEN_SOURCE
10 #define _XOPEN_SOURCE 700
11 
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <sys/random.h>
17 #include "nsi_tracing.h"
18 
entropy_native_seed(unsigned int seed,bool seed_random)19 void entropy_native_seed(unsigned int seed, bool seed_random)
20 {
21 	if (seed_random == false) {
22 		srandom(seed);
23 	} else {
24 		unsigned int buf;
25 		int err = getrandom(&buf, sizeof(buf), 0);
26 
27 		if (err != sizeof(buf)) {
28 			nsi_print_error_and_exit("Could not get random number (%i, %s)\n",
29 						 err, strerror(errno));
30 		}
31 		srandom(buf);
32 
33 		/* Let's print the seed so users can still reproduce the run if they need to */
34 		nsi_print_trace("Random generator seeded with 0x%X\n", buf);
35 	}
36 }
37