Add rand-core v0.9 support.

Co-Authored-By: Aurélien Jacobs <aurel@gnuage.org>
This commit is contained in:
Dario Nieuwenhuis
2025-05-18 20:32:48 +02:00
parent e8b1ea14c7
commit e4fc487644
63 changed files with 227 additions and 131 deletions

View File

@@ -157,7 +157,6 @@ embedded-io = { version = "0.6.1" }
embedded-io-async = { version = "0.6.1" }
embedded-storage = { version = "0.3" }
embedded-storage-async = { version = "0.4.1" }
rand_core = "0.6.4"
fixed = "1.28.0"
rp-pac = { version = "7.0.0" }
@@ -167,6 +166,9 @@ embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-async = { version = "1.0" }
embedded-hal-nb = { version = "1.0" }
rand-core-06 = { package = "rand_core", version = "0.6" }
rand-core-09 = { package = "rand_core", version = "0.9" }
pio = { version = "0.3" }
rp2040-boot2 = "0.3"
document-features = "0.2.10"

View File

@@ -1776,7 +1776,8 @@ impl<'d, T: GpoutPin> Drop for Gpout<'d, T> {
pub struct RoscRng;
impl RoscRng {
fn next_u8() -> u8 {
/// Get a random u8
pub fn next_u8() -> u8 {
let random_reg = pac::ROSC.randombit();
let mut acc = 0;
for _ in 0..u8::BITS {
@@ -1785,26 +1786,60 @@ impl RoscRng {
}
acc
}
}
impl rand_core::RngCore for RoscRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
Ok(self.fill_bytes(dest))
/// Get a random u32
pub fn next_u32(&mut self) -> u32 {
rand_core_09::impls::next_u32_via_fill(self)
}
fn next_u32(&mut self) -> u32 {
rand_core::impls::next_u32_via_fill(self)
/// Get a random u64
pub fn next_u64(&mut self) -> u64 {
rand_core_09::impls::next_u64_via_fill(self)
}
fn next_u64(&mut self) -> u64 {
rand_core::impls::next_u64_via_fill(self)
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
/// Fill a slice with random bytes
pub fn fill_bytes(&mut self, dest: &mut [u8]) {
dest.fill_with(Self::next_u8)
}
}
impl rand_core_06::RngCore for RoscRng {
fn next_u32(&mut self) -> u32 {
self.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.fill_bytes(dest);
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl rand_core_06::CryptoRng for RoscRng {}
impl rand_core_09::RngCore for RoscRng {
fn next_u32(&mut self) -> u32 {
self.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.fill_bytes(dest);
}
}
impl rand_core_09::CryptoRng for RoscRng {}
/// Enter the `DORMANT` sleep state. This will stop *all* internal clocks
/// and can only be exited through resets, dormant-wake GPIO interrupts,
/// and RTC interrupts. If RTC is clocked from an internal clock source

View File

@@ -7,7 +7,6 @@ use core::task::Poll;
use embassy_hal_internal::{Peri, PeripheralType};
use embassy_sync::waitqueue::AtomicWaker;
use rand_core::Error;
use crate::interrupt::typelevel::{Binding, Interrupt};
use crate::peripherals::TRNG;
@@ -369,7 +368,7 @@ impl<'d, T: Instance> Trng<'d, T> {
}
}
impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> {
impl<'d, T: Instance> rand_core_06::RngCore for Trng<'d, T> {
fn next_u32(&mut self) -> u32 {
self.blocking_next_u32()
}
@@ -379,16 +378,32 @@ impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> {
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.blocking_fill_bytes(dest)
self.blocking_fill_bytes(dest);
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
self.blocking_fill_bytes(dest);
Ok(())
}
}
impl<'d, T: Instance> rand_core::CryptoRng for Trng<'d, T> {}
impl<'d, T: Instance> rand_core_06::CryptoRng for Trng<'d, T> {}
impl<'d, T: Instance> rand_core_09::RngCore for Trng<'d, T> {
fn next_u32(&mut self) -> u32 {
self.blocking_next_u32()
}
fn next_u64(&mut self) -> u64 {
self.blocking_next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.blocking_fill_bytes(dest);
}
}
impl<'d, T: Instance> rand_core_09::CryptoRng for Trng<'d, T> {}
/// TRNG interrupt handler.
pub struct InterruptHandler<T: Instance> {