diff options
| author | Jeff Epler <jepler@gmail.com> | 2020-01-08 10:06:55 -0600 |
|---|---|---|
| committer | Jeff Epler <jepler@gmail.com> | 2020-01-08 10:06:55 -0600 |
| commit | b3fb0243011a2797ef41c3014c7fcdaca4331765 (patch) | |
| tree | 30779d5f1c3a577b6b4bb5ac2b9d12ec0f9ca5c4 | |
| parent | 6735283d8f6e352626d6ad7c4ee48b472ea65476 (diff) | |
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.
| -rw-r--r-- | ports/nrf/common-hal/os/__init__.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/ports/nrf/common-hal/os/__init__.c b/ports/nrf/common-hal/os/__init__.c index e38226f94..1d89c73b6 100644 --- a/ports/nrf/common-hal/os/__init__.c +++ b/ports/nrf/common-hal/os/__init__.c @@ -31,6 +31,7 @@ #ifdef BLUETOOTH_SD #include "nrf_sdm.h" +#include "tick.h" #endif #include "nrf_rng.h" @@ -66,8 +67,25 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) { uint8_t sd_en = 0; (void) sd_softdevice_is_enabled(&sd_en); - if (sd_en) - return NRF_SUCCESS == sd_rand_application_vector_get(buffer, length); + if (sd_en) { + while (length != 0) { + uint8_t available = 0; + sd_rand_application_bytes_available_get(&available); + if (available) { + uint32_t request = MIN(length, available); + uint32_t result = sd_rand_application_vector_get(buffer, request); + if (result != NRF_SUCCESS) { + return false; + } + buffer += request; + length -= request; + } else { + RUN_BACKGROUND_TASKS; + tick_delay(500); + } + } + return true; + } #endif nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY); |
