File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change 88
99//! Implementation for ESP-IDF
1010use 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
1217pub 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}
Original file line number Diff line number Diff line change 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]
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" ,
You can’t perform that action at this time.
0 commit comments