-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_utils.c
executable file
·48 lines (39 loc) · 1.33 KB
/
random_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*!\file random_utils.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief (A little less pseudo) random numbers generation utilities
** \note Randomness is enhanced between MCUs (using UID), through calls and time
** \warning Unfortunately, after reset, assuming call will happen at same clock tick,
** random_Get will give same result (unless start saved/restore from some storage
** is given as parameter at first call)
**/
/****************************************************************/
#include <stdlib.h>
#include <string.h>
#include "random_utils.h"
#include "exceptions.h"
/****************************************************************/
static uint32_t random32;
uint32_t random_Get(const uint32_t start)
{
static bool first_call = true;
const uint32_t * pUID = (uint32_t *) UID_BASE;
const uint32_t time = HALTicks();
uint32_t UID[3];
if (first_call)
{
random32 = start;
first_call = false;
}
memcpy(&UID, pUID, sizeof(UID));
/*** Some kind of tricky random algorithm using STM32 UID & HAL ticks ***/
// Enhancing randomness through time and calls
random32 += UID[0];
random32 ^= time;
random32 ^= UID[1] >> (time & 0x3);
random32 ^= UID[2] << (time & 0x1);
// Enhancing randomness by changing seed & xor random result with previous one
srand(random32);
random32 ^= rand();
return random32;
}