-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.cpp
96 lines (78 loc) · 1.55 KB
/
random.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#if defined(__MINGW32__) || defined(__APPLE__)
#include <cstdint>
#include <cstdlib>
#include <ctime>
void init_my_getrandom()
{
srand(time(nullptr));
}
bool my_getrandom(void *const tgt, const size_t n)
{
uint8_t *workp = reinterpret_cast<uint8_t *>(tgt);
for(size_t i=0; i<n; i++)
workp[i] = rand(); // hope for the best!
return true;
}
#elif defined(linux) || defined(__FreeBSD__)
#include <cstdint>
#include <sys/random.h>
void init_my_getrandom()
{
}
bool my_getrandom(void *const tgt, const size_t n)
{
uint8_t *workp = reinterpret_cast<uint8_t *>(tgt);
size_t todo = n;
while(todo > 0) {
ssize_t rc = getrandom(workp, todo, 0);
if (rc == -1)
return false;
workp += rc;
todo -= rc;
}
return true;
}
#elif defined(RP2040W)
#include <cstdint>
#include <cstdlib>
#include <pico/rand.h>
void init_my_getrandom()
{
}
bool my_getrandom(void *const tgt, const size_t n)
{
for(size_t i=0; i<n; i++)
reinterpret_cast<uint8_t *>(tgt)[i] = get_rand_32();
return true;
}
#elif defined(ESP32)
#include <esp_random.h>
void init_my_getrandom()
{
}
bool my_getrandom(void *const tgt, const size_t n)
{
esp_fill_random(tgt, n);
return true;
}
#elif defined(TEENSY4_1)
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <Entropy.h>
EntropyClass ec;
void init_my_getrandom()
{
ec.Initialize();
}
bool my_getrandom(void *const tgt, const size_t n)
{
for(size_t i=0; i<n; i++)
reinterpret_cast<uint8_t *>(tgt)[i] = ec.random();
return true;
}
#endif
bool my_getrandom(uint32_t *const v)
{
return my_getrandom(v, sizeof *v);
}