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
7  * ARCH_POSIX architecture
8  */
9 
10 #undef _XOPEN_SOURCE
11 #define _XOPEN_SOURCE 700
12 
13 #include <stdbool.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <sys/random.h>
18 #include "nsi_tracing.h"
19 
entropy_native_seed(unsigned int seed,bool seed_random)20 void entropy_native_seed(unsigned int seed, bool seed_random)
21 {
22 	if (seed_random == false) {
23 		srandom(seed);
24 	} else {
25 		unsigned int buf;
26 		int err = getrandom(&buf, sizeof(buf), 0);
27 
28 		if (err != sizeof(buf)) {
29 			nsi_print_error_and_exit("Could not get random number (%i, %s)\n",
30 						 err, strerror(errno));
31 		}
32 		srandom(buf);
33 
34 		/* Let's print the seed so users can still reproduce the run if they need to */
35 		nsi_print_trace("Random generator seeded with 0x%X\n", buf);
36 	}
37 }
38