Skip to content

Commit d250d7a

Browse files
thorhssunhe05
authored and
sunhe05
committed
Enable support for espidf (esp32 family of microcontrollers) (rust-random#245)
1 parent c7af854 commit d250d7a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/espidf.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
use core::ffi::c_void;
12+
13+
extern "C" {
14+
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
15+
}
16+
17+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
18+
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
19+
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:
20+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
21+
//
22+
// However tracking if some of these entropy sources is enabled is way too difficult to implement here
23+
unsafe { esp_fill_random(dest.as_mut_ptr().cast(), dest.len()) };
24+
25+
Ok(())
26+
}

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` | [`esp_fill_random()`][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://docs.espressif.com/projects/esp-idf/en/release-v4.1/api-reference/system/system.html?highlight=esp_fill_random#_CPPv415esp_fill_randomPv6size_t
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)