Skip to content

Commit

Permalink
std: implement DefaultRandomSource on ESP-IDF
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Aug 22, 2024
1 parent 2fcf651 commit 5469400
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/std/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::sys::random as sys;
/// Windows | [`ProcessPrng`](https://learn.microsoft.com/en-us/windows/win32/seccng/processprng)
/// Apple | `CCRandomGenerateBytes`
/// DragonFly | [`arc4random_buf`](https://man.dragonflybsd.org/?command=arc4random&section=ANY)
/// ESP-IDF | [`esp_fill_random`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t)
/// FreeBSD | [`arc4random_buf`](https://man.freebsd.org/cgi/man.cgi?query=arc4random&apropos=0&sektion=0&manpath=FreeBSD+15.0-CURRENT&arch=default&format=html)
/// Fuchsia | [`cprng_draw`](https://fuchsia.dev/reference/syscalls/cprng_draw)
/// Haiku | `arc4random_buf`
Expand Down
9 changes: 9 additions & 0 deletions library/std/src/sys/random/espidf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::ffi::c_void;

extern "C" {
fn esp_fill_random(buf: *mut c_void, len: usize);
}

pub fn fill_bytes(bytes: &mut [u8], _best_effort: bool) {
unsafe { esp_fill_random(bytes.as_mut_ptr().cast(), bytes.len()) }
}
3 changes: 3 additions & 0 deletions library/std/src/sys/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ cfg_if::cfg_if! {
} else if #[cfg(target_os = "emscripten")] {
mod getentropy;
pub use getentropy::fill_bytes;
} else if #[cfg(target_os = "espidf")] {
mod espidf;
pub use espidf::fill_bytes;
} else if #[cfg(target_os = "fuchsia")] {
mod fuchsia;
pub use fuchsia::fill_bytes;
Expand Down

0 comments on commit 5469400

Please sign in to comment.