Skip to content

Commit b3fb024

Browse files
committed
nrf: Call into sd as many times as necessary to fill urandom request
Generating 51200 bytes in one go takes 4.966s, so that's a rate of about 10KiB/s.
1 parent 6735283 commit b3fb024

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

ports/nrf/common-hal/os/__init__.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#ifdef BLUETOOTH_SD
3333
#include "nrf_sdm.h"
34+
#include "tick.h"
3435
#endif
3536

3637
#include "nrf_rng.h"
@@ -66,8 +67,25 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
6667
uint8_t sd_en = 0;
6768
(void) sd_softdevice_is_enabled(&sd_en);
6869

69-
if (sd_en)
70-
return NRF_SUCCESS == sd_rand_application_vector_get(buffer, length);
70+
if (sd_en) {
71+
while (length != 0) {
72+
uint8_t available = 0;
73+
sd_rand_application_bytes_available_get(&available);
74+
if (available) {
75+
uint32_t request = MIN(length, available);
76+
uint32_t result = sd_rand_application_vector_get(buffer, request);
77+
if (result != NRF_SUCCESS) {
78+
return false;
79+
}
80+
buffer += request;
81+
length -= request;
82+
} else {
83+
RUN_BACKGROUND_TASKS;
84+
tick_delay(500);
85+
}
86+
}
87+
return true;
88+
}
7189
#endif
7290

7391
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);

0 commit comments

Comments
 (0)