Skip to content

Commit

Permalink
RngCore trait is implemented (esp-rs#1122)
Browse files Browse the repository at this point in the history
* WIP

made randomness ensuring function
implemented RngCore trait (final version i guess)

* Revert part of changes and leave TODO comment

Changelog entry

Get rid of warning
  • Loading branch information
playfulFence committed Jan 30, 2024
1 parent 18d185b commit f50ace2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
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

0 comments on commit f50ace2

Please sign in to comment.