Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RngCore trait is implemented #1122

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ESP32-P4: Add efuse reading support (#1114)

- Allow for splitting of the USB Serial JTAG peripheral into tx/rx components (#1024)
- `RngCore` trait is implemented (#1122)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ procmacros = { version = "0.8.0", features = ["enum-dispatch", "ram"],
strum = { version = "0.25.0", default-features = false, features = ["derive"] }
void = { version = "1.0.2", default-features = false }
usb-device = { version = "0.3.1", optional = true }
rand_core = { version = "0.6.4" }

# async
embedded-hal-async = { version = "1.0.0", optional = true }
Expand Down
34 changes: 34 additions & 0 deletions esp-hal-common/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

use core::{convert::Infallible, marker::PhantomData};

use rand_core::RngCore;

use crate::{peripheral::Peripheral, peripherals::RNG};

/// Random number generator driver
Expand Down Expand Up @@ -104,3 +106,35 @@ impl embedded_hal::blocking::rng::Read for Rng {
Ok(())
}
}

// TODO: CryptoRng trait will be implemented when true randomness of RNG is
// ensured

impl RngCore for Rng {
fn next_u32(&mut self) -> u32 {
// Directly use the existing random method to get a u32 random number
self.random()
}

fn next_u64(&mut self) -> u64 {
// Call random() twice to generate a u64 random number (сombine two u32)
let upper = self.random() as u64;
let lower = self.random() as u64;
(upper << 32) | lower
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
// Fill the destination buffer with random bytes
for chunk in dest.chunks_mut(4) {
let rand_bytes = self.random().to_le_bytes();
for (dest_byte, rand_byte) in chunk.iter_mut().zip(&rand_bytes) {
*dest_byte = *rand_byte;
}
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
// Similar implementation as fill_bytes, but encapsulated in a Result
Ok(self.fill_bytes(dest))
}
}
1 change: 1 addition & 0 deletions esp32h2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ sha2 = { version = "0.10.8", default-features = false}
smart-leds = "0.4.0"
ssd1306 = "0.8.4"
static_cell = { version = "2.0.0", features = ["nightly"] }
rand_core = "0.6.4"

[features]
default = ["embassy-integrated-timers", "rt", "vectored", "zero-rtc-bss"]
Expand Down
Loading