Skip to content

Commit 7e4666a

Browse files
committed
ESP-IDF: Enable by default and use getrandom
After reviewing the updated ESP-IDF random documentation: - https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/random.html - https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf#rng I think we should enable this backend by default, and use their `getrandom` bindings. Given that ESP-IDF provides such bindings, its clear they intend for cryptographic libraries to use them. Furthurmore, it seems like the only time the Hardware RNG would lack sufficient entropy is during early boot, so I added a section to our "Early boot" documentation noting this issue. Also note that Rust's standard library unconditonally supports ESP-IDF for both hash seed generation and generating cryptographic random bytes, see https://github.com/rust-lang/rust/blob/62bf38fa600f4beb878d61c537837729d4ee689e/library/std/src/sys/random/espidf.rs#L7 Using the `getrandom` binding ensures that if ESP-IDF ever improves their implementation, we will immediately be able to take advantage of it. Signed-off-by: Joe Richey <[email protected]>
1 parent 9fb4a9a commit 7e4666a

File tree

6 files changed

+16
-43
lines changed

6 files changed

+16
-43
lines changed

.github/workflows/build.yml

+1-13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ jobs:
8686
armv6k-nintendo-3ds,
8787
armv7-sony-vita-newlibeabihf,
8888
i686-unknown-hurd-gnu,
89+
riscv32imc-esp-espidf,
8990
x86_64-unknown-hermit,
9091
x86_64-wrs-vxworks,
9192
x86_64-unknown-dragonfly,
@@ -207,19 +208,6 @@ jobs:
207208
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
208209
run: cargo build --target=aarch64-unknown-linux-gnu --features std
209210

210-
esp-idf:
211-
name: ESP-IDF
212-
runs-on: ubuntu-24.04
213-
steps:
214-
- uses: actions/checkout@v4
215-
- uses: dtolnay/rust-toolchain@nightly # Required to build libcore
216-
with:
217-
components: rust-src
218-
- uses: Swatinem/rust-cache@v2
219-
- env:
220-
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="esp_idf"
221-
run: cargo build -Z build-std=core --target=riscv32imc-esp-espidf
222-
223211
no-atomics:
224212
name: No Atomics
225213
runs-on: ubuntu-24.04

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ libc = { version = "0.2.154", default-features = false }
3131
libc = { version = "0.2.154", default-features = false }
3232

3333
# getrandom
34-
[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
34+
[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "espidf", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
3535
libc = { version = "0.2.154", default-features = false }
3636

3737
# netbsd
@@ -76,7 +76,7 @@ rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]
7676
[lints.rust.unexpected_cfgs]
7777
level = "warn"
7878
check-cfg = [
79-
'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "wasm_js", "esp_idf"))',
79+
'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "wasm_js"))',
8080
'cfg(getrandom_msan)',
8181
'cfg(getrandom_test_linux_fallback)',
8282
'cfg(getrandom_test_netbsd_fallback)',

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
6565
| WASI 0.2 | `wasm32‑wasip2` | [`get-random-u64`]
6666
| SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
6767
| Nintendo 3DS | `*-nintendo-3ds` | [`getrandom`][18]
68+
| ESP-IDF | `*‑espidf` | [`getrandom`][esp-idf-getrandom], see "Early Boot" section below
6869
| PS Vita | `*-vita-*` | [`getentropy`][19]
6970
| QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`)
7071
| AIX | `*-ibm-aix` | [`/dev/urandom`][15]
@@ -81,7 +82,6 @@ of randomness based on their specific needs:
8182
| `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
8283
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
8384
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
84-
| `esp_idf` | ESP-IDF | `*‑espidf` | [`esp_fill_random`]. WARNING: can return low-quality entropy without proper hardware configuration!
8585
| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]
8686
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
8787

@@ -247,6 +247,13 @@ sourced according to the platform's best practices, but each platform has
247247
its own limits on the grade of randomness it can promise in environments
248248
with few sources of entropy.
249249

250+
On ESP-IDF, if `getrandom` is used before enabling WiFi, BT, or the voltage
251+
noise entropy source (SAR ADC), the Hardware RNG will only be seeded via
252+
RC_FAST_CLK. This can occur during early boot unless
253+
`bootloader_random_enable()` is called. For more information see the
254+
[ESP-IDF RNG Docs][esp-idf-rng] or the
255+
[RNG section of the ESP32 Technical Reference Manual][esp-trng-docs].
256+
250257
## Error handling
251258

252259
We always prioritize failure over returning known insecure "random" bytes.
@@ -335,7 +342,9 @@ dual licensed as above, without any additional terms or conditions.
335342
[`RNDR`]: https://developer.arm.com/documentation/ddi0601/2024-06/AArch64-Registers/RNDR--Random-Number
336343
[`CCRandomGenerateBytes`]: https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html
337344
[`cprng_draw`]: https://fuchsia.dev/fuchsia-src/zircon/syscalls/cprng_draw
338-
[`esp_fill_random`]: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t
345+
[esp-idf-getrandom]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/random.html#getrandom
346+
[esp-idf-rng]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/random.html
347+
[esp-trng-docs]: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf#rng
339348
[`random_get`]: https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno
340349
[`get-random-u64`]: https://github.com/WebAssembly/WASI/blob/v0.2.1/wasip2/random/random.wit#L23-L28
341350
[configuration flags]: #configuration-flags

src/backends.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ cfg_if! {
2222
} else if #[cfg(getrandom_backend = "wasm_js")] {
2323
mod wasm_js;
2424
pub use wasm_js::*;
25-
} else if #[cfg(getrandom_backend = "esp_idf")] {
26-
mod esp_idf;
27-
pub use esp_idf::*;
2825
} else if #[cfg(any(
2926
target_os = "haiku",
3027
target_os = "redox",
@@ -46,6 +43,7 @@ cfg_if! {
4643
target_os = "freebsd",
4744
target_os = "hurd",
4845
target_os = "illumos",
46+
target_os = "espidf",
4947
// Check for target_arch = "arm" to only include the 3DS. Does not
5048
// include the Nintendo Switch (which is target_arch = "aarch64").
5149
all(target_os = "horizon", target_arch = "arm"),

src/backends/esp_idf.rs

-23
This file was deleted.

src/backends/getrandom.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! - DragonFly 5.7
1010
//! - Hurd Glibc 2.31
1111
//! - shim-3ds since Feb 2022
12+
//! - ESP-IDF since v3.2 (April 2019)
1213
//!
1314
//! For these platforms, we always use the default pool and never set the
1415
//! GRND_RANDOM flag to use the /dev/random pool. On Linux/Android/Hurd, using

0 commit comments

Comments
 (0)