Skip to content

Commit b8cb133

Browse files
ivmarkovimarkov
authored and
imarkov
committed
Support for the ESP-IDF framework
1 parent 30308ae commit b8cb133

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/espidf.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for ESP-IDF
10+
use crate::Error;
11+
12+
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+
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
17+
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:
18+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
19+
//
20+
// 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) };
22+
23+
Ok(())
24+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//! | Haiku | `*‑haiku` | `/dev/random` (identical to `/dev/urandom`)
2727
//! | SGX | `x86_64‑*‑sgx` | [RDRAND][18]
2828
//! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
29+
//! | ESP-IDF | `*‑espidf` | [`getrandom()`][23]
2930
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
3031
//! | WASI | `wasm32‑wasi` | [`random_get`][17]
3132
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16]
@@ -140,6 +141,7 @@
140141
//! [20]: https://www.unix.com/man-page/mojave/4/random/
141142
//! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
142143
//! [22]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
144+
//! [23]: https://cygwin.com/git/?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/include/sys/random.h;hb=HEAD#l22
143145
144146
#![doc(
145147
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
@@ -203,6 +205,8 @@ cfg_if! {
203205
} else if #[cfg(target_os = "vxworks")] {
204206
mod util_libc;
205207
#[path = "vxworks.rs"] mod imp;
208+
} else if #[cfg(target_os = "espidf")] {
209+
#[path = "espidf.rs"] mod imp;
206210
} else if #[cfg(windows)] {
207211
#[path = "windows.rs"] mod imp;
208212
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {

0 commit comments

Comments
 (0)