Skip to content

Commit ed6438c

Browse files
ivmarkovthorhs
authored andcommitted
Support for the ESP-IDF framework
1 parent e1a9c9e commit ed6438c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/espidf.rs

+24
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

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +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]
3031
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
3132
//! | WASI | `wasm32‑wasi` | [`random_get`][17]
3233
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16]
@@ -142,6 +143,7 @@
142143
//! [20]: https://www.unix.com/man-page/mojave/4/random/
143144
//! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
144145
//! [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
145147
146148
#![doc(
147149
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
@@ -209,6 +211,8 @@ cfg_if! {
209211
#[path = "vxworks.rs"] mod imp;
210212
} else if #[cfg(target_os = "solid_asp3")] {
211213
#[path = "solid.rs"] mod imp;
214+
} else if #[cfg(target_os = "espidf")] {
215+
#[path = "espidf.rs"] mod imp;
212216
} else if #[cfg(windows)] {
213217
#[path = "windows.rs"] mod imp;
214218
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {

0 commit comments

Comments
 (0)