Skip to content

Commit c3b02b8

Browse files
committed
Use esp_fill_random instead of libc::getrandom
Import esp_fill_random by using extern "C" and skip depending of esp-idf-sys. Opted for esp_fill_random instead of esp_random since esp_fill_random implements logic for filling the u8 array efficiently from a u32 source. Figured it was more efficient than what I would implement.
1 parent ed6438c commit c3b02b8

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/espidf.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@
88

99
//! Implementation for ESP-IDF
1010
use crate::Error;
11+
use core::ffi::c_void;
12+
13+
extern "C" {
14+
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
15+
}
1116

1217
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
13-
// ESP-IDF fails and returns -1 only when the passed buffer is NULL, which cannot happen in our case:
14-
// https://github.com/espressif/esp-idf/blob/master/components/newlib/random.c#L33
15-
//
1618
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
1719
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:
1820
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
1921
//
2022
// However tracking if some of these entropy sources is enabled is way too difficult to implement here
21-
unsafe { libc::getrandom(dest.as_mut_ptr().cast(), dest.len(), 0) };
23+
//
24+
// Using esp_fill_random since it has some optimizations regarding filling a byte array from an
25+
// u32 source. See https://github.com/espressif/esp-idf/blob/master/components/esp_hw_support/hw_random.c
26+
unsafe { esp_fill_random(dest.as_mut_ptr().cast(), dest.len()) };
2227

2328
Ok(())
2429
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! | Hermit | `x86_64-*-hermit` | [`RDRAND`][18]
2828
//! | SGX | `x86_64‑*‑sgx` | [RDRAND][18]
2929
//! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
30-
//! | ESP-IDF | `*‑espidf` | [`getrandom()`][23]
30+
//! | ESP-IDF | `*‑espidf` | [`esp_fill_random()`][23]
3131
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
3232
//! | WASI | `wasm32‑wasi` | [`random_get`][17]
3333
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16]
@@ -143,7 +143,7 @@
143143
//! [20]: https://www.unix.com/man-page/mojave/4/random/
144144
//! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
145145
//! [22]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
146-
//! [23]: https://cygwin.com/git/?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/include/sys/random.h;hb=HEAD#l22
146+
//! [23]: https://github.com/espressif/esp-idf/blob/master/components/esp_hw_support/hw_random.c
147147
148148
#![doc(
149149
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",

0 commit comments

Comments
 (0)