|
1 |
| -//! Implementation for ESP-IDF |
| 1 | +//! Implementation for ESP-IDF. |
| 2 | +//! |
| 3 | +//! Not that NOT enabling WiFi, BT, or the voltage noise entropy source |
| 4 | +//! (via `bootloader_random_enable`) will cause ESP-IDF to return pseudo-random numbers based on |
| 5 | +//! the voltage noise entropy, after the initial boot process: |
| 6 | +//! https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html |
| 7 | +//! |
| 8 | +//! However tracking if some of these entropy sources is enabled is way too difficult |
| 9 | +//! to implement here. |
2 | 10 | use crate::Error;
|
3 | 11 | use core::{ffi::c_void, mem::MaybeUninit};
|
4 | 12 |
|
5 |
| -pub use crate::util::{inner_u32, inner_u64}; |
| 13 | +pub use crate::default_impls::{insecure_fill_uninit, insecure_u32, insecure_u64}; |
6 | 14 |
|
7 | 15 | #[cfg(not(target_os = "espidf"))]
|
8 | 16 | compile_error!("`esp_idf` backend can be enabled only for ESP-IDF targets!");
|
9 | 17 |
|
10 | 18 | extern "C" {
|
11 |
| - fn esp_fill_random(buf: *mut c_void, len: usize) -> u32; |
| 19 | + fn esp_random() -> u32; |
| 20 | + fn esp_fill_random(buf: *mut c_void, len: usize); |
12 | 21 | }
|
13 | 22 |
|
14 |
| -pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
15 |
| - // Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`) |
16 |
| - // will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process: |
17 |
| - // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html |
18 |
| - // |
19 |
| - // However tracking if some of these entropy sources is enabled is way too difficult to implement here |
20 |
| - unsafe { esp_fill_random(dest.as_mut_ptr().cast(), dest.len()) }; |
| 23 | +pub fn u32() -> Result<u32, Error> { |
| 24 | + Ok(unsafe { esp_random() }) |
| 25 | +} |
21 | 26 |
|
| 27 | +pub fn u64() -> Result<u64, Error> { |
| 28 | + let (a, b) = unsafe { (esp_random(), esp_random()) }; |
| 29 | + let res = (u64::from(a) << 32) | u64::from(b); |
| 30 | + Ok(res) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn fill_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 34 | + unsafe { esp_fill_random(dest.as_mut_ptr().cast(), dest.len()) }; |
22 | 35 | Ok(())
|
23 | 36 | }
|
0 commit comments