File tree 2 files changed +11
-6
lines changed
2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change 8
8
9
9
//! Implementation for ESP-IDF
10
10
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
+ }
11
16
12
17
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
- //
16
18
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
17
19
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:
18
20
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
19
21
//
20
22
// 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 ( ) ) } ;
22
27
23
28
Ok ( ( ) )
24
29
}
Original file line number Diff line number Diff line change 27
27
//! | Hermit | `x86_64-*-hermit` | [`RDRAND`][18]
28
28
//! | SGX | `x86_64‑*‑sgx` | [RDRAND][18]
29
29
//! | 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]
31
31
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
32
32
//! | WASI | `wasm32‑wasi` | [`random_get`][17]
33
33
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16]
143
143
//! [20]: https://www.unix.com/man-page/mojave/4/random/
144
144
//! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
145
145
//! [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
147
147
148
148
#![ doc(
149
149
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