1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
90
91
92
93
94
95
96
97
100
101
102
103
104
105
106
107
108
109
110
111
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
207
208
209
210
211
212
213
214
215
216
217
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
240
241
242
243
244
245
246
247
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
281
282
283
284
288
289
290
292
293
294
295
296
297
298
312
313
314
315
316
317
318
319
320
321
327
328
329
330
331
332
333
334
335
337
338
339
341
342
343
346
347
348
355
356
357
358
359
364
365
366
367
368
369
370
371
372
373
374
378
379
/* ... */
/* ... */
#include "pico/rand.h"
#include "pico/unique_id.h"
#include "pico/time.h"
#include "hardware/clocks.h"
#include "hardware/structs/rosc.h"
#include "hardware/structs/busctrl.h"
#include "hardware/sync.h"
7 includes
static bool rng_initialised = false;
static rng_128_t __uninitialized_ram(rng_state);
#if PICO_RAND_SEED_ENTROPY_SRC_RAM_HASH
static uint64_t __uninitialized_ram(ram_hash);
#endif
#if PICO_RAND_ENTROPY_SRC_ROSC | PICO_RAND_SEED_ENTROPY_SRC_ROSC
static uint64_t __uninitialized_ram(rosc_samples);
#endif
#if PICO_RAND_ENTROPY_SRC_BUS_PERF_COUNTER
static uint8_t bus_counter_idx;
#endif
/* ... */
static __noinline uint64_t splitmix64(uint64_t x) {
uint64_t z = x + 0x9E3779B97F4A7C15ull;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
return z ^ (z >> 31);
}{ ... }
/* ... */
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}{ ... }
static __noinline uint64_t xoroshiro128ss(rng_128_t *local_rng_state) {
const uint64_t s0 = local_rng_state->r[0];
uint64_t s1 = local_rng_state->r[1];
while (s0 == 0 && s1 == 0) {
s1 = time_us_64();
}while (s0 == 0 && s1 == 0) { ... }
const uint64_t result = rotl(s0 * 5, 7) * 9;
s1 ^= s0;
local_rng_state->r[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16);
local_rng_state->r[1] = rotl(s1, 37);
return result;
}{ ... }
#if PICO_RAND_SEED_ENTROPY_SRC_RAM_HASH
static uint64_t sdbm_hash64_sram(uint64_t hash) {
for (uint i = (PICO_RAND_RAM_HASH_START + 3) & ~3; i < PICO_RAND_RAM_HASH_END; i+=4) {
uint32_t c = *(uint32_t *) i;
hash = (uint64_t) c + (hash << 6) + (hash << 16) - hash;
}for (uint i = (PICO_RAND_RAM_HASH_START + 3) & ~3; i < PICO_RAND_RAM_HASH_END; i+=4) { ... }
return hash;
}sdbm_hash64_sram (uint64_t hash) { ... }
/* ... */#endif
#if PICO_RAND_SEED_ENTROPY_SRC_TRNG | PICO_RAND_ENTROPY_SRC_TRNG
#if !HAS_RP2350_TRNG
#error PICO_RAND_SEED_ENTROPY_SRC_TRNG and PICO_RAND_ENTROPY_SRC_TRNG are only valid on RP2350
#endif
#include "hardware/structs/trng.h"
uint32_t trng_sample_words[count_of(trng_hw->ehr_data)];
static_assert(count_of(trng_hw->ehr_data) >= 2 && count_of(trng_hw->ehr_data) < 255, "");
uint8_t trng_sample_word_count;
static uint64_t capture_additional_trng_samples(void) {
spin_lock_t *lock = spin_lock_instance(PICO_SPINLOCK_ID_RAND);
uint32_t save = spin_lock_blocking(lock);
if (trng_sample_word_count < 2) {
trng_hw->sample_cnt1 = 0;
trng_hw->trng_debug_control = -1u;
trng_hw->rnd_source_enable = -1u;
trng_hw->rng_icr = -1u;
while (trng_hw->trng_busy);
for (uint i = 0; i < count_of(trng_sample_words); i++) {
trng_sample_words[i] = trng_hw->ehr_data[i];
}for (uint i = 0; i < count_of(trng_sample_words); i++) { ... }
trng_sample_word_count = count_of(trng_sample_words);
trng_hw->trng_config = rng_state.r[0];
}if (trng_sample_word_count < 2) { ... }
trng_sample_word_count -= 2;
uint64_t rc = trng_sample_words[trng_sample_word_count] |
(((uint64_t)trng_sample_words[trng_sample_word_count + 1]) << 32);
spin_unlock(lock, save);
return rc;
}{ ... }
/* ... */#endif
#if PICO_RAND_SEED_ENTROPY_SRC_ROSC | PICO_RAND_ENTROPY_SRC_ROSC
static uint64_t capture_additional_rosc_samples(uint n) {
static absolute_time_t next_sample_time;
#if !PICO_RAND_DISABLE_ROSC_CHECK
hard_assert((rosc_hw->status & ROSC_STATUS_ENABLED_BITS) &&
((clocks_hw->clk[clk_sys].ctrl & CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS) != (CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_ROSC_CLKSRC << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB)));/* ... */
#endif
bool in_exception = __get_current_exception();
assert(n);
uint64_t samples = 0;
for(uint i=0; i<n; i++) {
bool bit_done = false;
do {
absolute_time_t cached_next_sample_time = next_sample_time;
if (in_exception) {
busy_wait_until(next_sample_time);
}if (in_exception) { ... } else {
sleep_until(next_sample_time);
}else { ... }
spin_lock_t *lock = spin_lock_instance(PICO_SPINLOCK_ID_RAND);
uint32_t save = spin_lock_blocking(lock);
if (!absolute_time_diff_us(cached_next_sample_time, next_sample_time)) {
samples <<= 1;
samples |= rosc_hw->randombit & 1u;
next_sample_time = make_timeout_time_us(PICO_RAND_MIN_ROSC_BIT_SAMPLE_TIME_US);
bit_done = true;
if (i == n - 1) {
samples = rosc_samples = (rosc_samples << n) | samples;
}if (i == n - 1) { ... }
}if (!absolute_time_diff_us(cached_next_sample_time, next_sample_time)) { ... }
spin_unlock(lock, save);
...} while (!bit_done);
}for (uint i=0; i
return samples;
}capture_additional_rosc_samples (uint n) { ... }
/* ... */#endif
#if PICO_RAND_SEED_ENTROPY_SRC_BOOT_RANDOM
#include "pico/bootrom.h"
#endif
static void initialise_rand(void) {
rng_128_t local_rng_state = local_rng_state;
uint which = 0;
#if PICO_RAND_SEED_ENTROPY_SRC_RAM_HASH
ram_hash = sdbm_hash64_sram(ram_hash);
local_rng_state.r[which] ^= splitmix64(ram_hash);
which ^= 1;/* ... */
#endif
#if PICO_RAND_SEED_ENTROPY_SRC_BOARD_ID
static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES == sizeof(uint64_t),
"Code below requires that 'board_id' is 64-bits in size");
union unique_id_u {
pico_unique_board_id_t board_id_native;
uint64_t board_id_u64;
...} unique_id;
pico_get_unique_board_id(&unique_id.board_id_native);
local_rng_state.r[which] ^= splitmix64(unique_id.board_id_u64);
which ^= 1;/* ... */
#endif
#if PICO_RAND_SEED_ENTROPY_SRC_ROSC
local_rng_state.r[which] ^= splitmix64(capture_additional_rosc_samples(8 * sizeof(rosc_samples)));
which ^= 1;/* ... */
#endif
#if PICO_RAND_SEED_ENTROPY_SRC_BOOT_RANDOM
union {
uint64_t u64[2];
uint32_t u32[4];
...} br;
rom_get_boot_random(br.u32);
local_rng_state.r[which] ^= splitmix64(br.u64[0]);
local_rng_state.r[which ^ 1] ^= splitmix64(br.u64[1]);/* ... */
#endif
#if PICO_RAND_SEED_ENTROPY_SRC_TIME
local_rng_state.r[which] ^= splitmix64(time_us_64());
which ^= 1;/* ... */
#endif
#if PICO_RAND_SEED_ENTROPY_SRC_TRNG
local_rng_state.r[which] ^= splitmix64(capture_additional_trng_samples());
which ^= 1;/* ... */
#endif
spin_lock_t *lock = spin_lock_instance(PICO_SPINLOCK_ID_RAND);
uint32_t save = spin_lock_blocking(lock);
if (!rng_initialised) {
#if PICO_RAND_SEED_ENTROPY_SRC_BUS_PERF_COUNTER
#if !PICO_RAND_BUS_PERF_COUNTER_INDEX
int idx = -1;
for(uint i = 0; i < count_of(bus_ctrl_hw->counter); i++) {
if (bus_ctrl_hw->counter[i].sel == BUSCTRL_PERFSEL0_RESET) {
idx = (int)i;
break;
}if (bus_ctrl_hw->counter[i].sel == BUSCTRL_PERFSEL0_RESET) { ... }
}for (uint i = 0; i < count_of(bus_ctrl_hw->counter); i++) { ... }
hard_assert(idx != -1);
bus_counter_idx = (uint8_t)idx;/* ... */
#else
bus_counter_idx = (uint8_t)PICO_RAND_BUS_PERF_COUNTER_INDEX;
#endif
bus_ctrl_hw->counter[bus_counter_idx].sel = PICO_RAND_BUS_PERF_COUNTER_EVENT;/* ... */
#endif
(void) xoroshiro128ss(&local_rng_state);
rng_state = local_rng_state;
rng_initialised = true;
}if (!rng_initialised) { ... }
spin_unlock(lock, save);
}{ ... }
uint64_t get_rand_64(void) {
if (!rng_initialised) {
initialise_rand();
}if (!rng_initialised) { ... }
static volatile uint8_t check_byte;
rng_128_t local_rng_state = rng_state;
uint8_t local_check_byte = check_byte;
uint which = 0;
#if PICO_RAND_ENTROPY_SRC_TIME
local_rng_state.r[which] ^= splitmix64(time_us_64());
which ^= 1;/* ... */
#endif
#if PICO_RAND_ENTROPY_SRC_ROSC
local_rng_state.r[which] ^= splitmix64(capture_additional_rosc_samples(PICO_RAND_ROSC_BIT_SAMPLE_COUNT));
which ^= 1;/* ... */
#endif
#if PICO_RAND_ENTROPY_SRC_TRNG
uint64_t foo = capture_additional_trng_samples();
local_rng_state.r[which] ^= splitmix64(foo);
which ^= 1;/* ... */
#endif
#if PICO_RAND_ENTROPY_SRC_BUS_PERF_COUNTER
uint32_t bus_counter_value = busctrl_hw->counter[bus_counter_idx].value;
if (bus_counter_value == BUSCTRL_PERFCTR0_BITS) {
busctrl_hw->counter[bus_counter_idx].value = 0;
}if (bus_counter_value == BUSCTRL_PERFCTR0_BITS) { ... }
local_rng_state.r[which] ^= splitmix64(bus_counter_value);
which ^= 1;/* ... */
#endif
spin_lock_t *lock = spin_lock_instance(PICO_SPINLOCK_ID_RAND);
uint32_t save = spin_lock_blocking(lock);
if (local_check_byte != check_byte) {
local_rng_state.r[0] ^= rng_state.r[0];
local_rng_state.r[1] ^= rng_state.r[1];
}if (local_check_byte != check_byte) { ... }
uint64_t rand64 = xoroshiro128ss(&local_rng_state);
rng_state = local_rng_state;
check_byte++;
spin_unlock(lock, save);
return rand64;
}{ ... }
void get_rand_128(rng_128_t *ptr128) {
ptr128->r[0] = get_rand_64();
ptr128->r[1] = get_rand_64();
}{ ... }
uint32_t get_rand_32(void) {
return (uint32_t) get_rand_64();
}{ ... }