2021-05-06 03:59:16 +02:00
|
|
|
#![macro_use]
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2021-05-10 01:19:07 +02:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::{into_ref, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2021-05-10 01:19:07 +02:00
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{pac, peripherals, Peripheral};
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-05-13 20:28:53 +02:00
|
|
|
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new();
|
2021-04-26 20:11:46 +02:00
|
|
|
|
2021-08-26 20:03:54 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-14 16:11:43 +02:00
|
|
|
pub enum Error {
|
|
|
|
SeedError,
|
|
|
|
ClockError,
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
pub struct Rng<'d, T: Instance> {
|
2022-07-23 14:00:19 +02:00
|
|
|
_inner: PeripheralRef<'d, T>,
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
impl<'d, T: Instance> Rng<'d, T> {
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self {
|
2021-06-09 13:54:53 +02:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(inner);
|
2022-07-23 01:29:35 +02:00
|
|
|
let mut random = Self { _inner: inner };
|
2021-05-06 22:38:53 +02:00
|
|
|
random.reset();
|
|
|
|
random
|
|
|
|
}
|
|
|
|
|
2023-07-01 03:15:39 +02:00
|
|
|
#[cfg(rng_v1)]
|
2021-05-06 22:38:53 +02:00
|
|
|
pub fn reset(&mut self) {
|
2023-02-09 12:01:44 +01:00
|
|
|
// rng_v2 locks up on seed error, needs reset
|
2023-02-09 11:44:20 +01:00
|
|
|
#[cfg(rng_v2)]
|
2023-06-19 03:07:26 +02:00
|
|
|
if T::regs().sr().read().seis() {
|
2023-02-08 16:57:37 +01:00
|
|
|
T::reset();
|
|
|
|
}
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
reg.set_ie(true);
|
|
|
|
});
|
|
|
|
T::regs().sr().modify(|reg| {
|
|
|
|
reg.set_seis(false);
|
|
|
|
reg.set_ceis(false);
|
|
|
|
});
|
2021-05-06 22:38:53 +02:00
|
|
|
// Reference manual says to discard the first.
|
|
|
|
let _ = self.next_u32();
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2023-07-01 03:15:39 +02:00
|
|
|
#[cfg(not(rng_v1))]
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(false);
|
|
|
|
reg.set_condrst(true);
|
|
|
|
// set RNG config "A" according to reference manual
|
|
|
|
// this has to be written within the same write access as setting the CONDRST bit
|
|
|
|
reg.set_nistc(pac::rng::vals::Nistc::DEFAULT);
|
|
|
|
reg.set_rng_config1(pac::rng::vals::RngConfig1::CONFIGA);
|
|
|
|
reg.set_rng_config2(pac::rng::vals::RngConfig2::CONFIGA_B);
|
|
|
|
reg.set_rng_config3(pac::rng::vals::RngConfig3::CONFIGA);
|
|
|
|
reg.set_clkdiv(pac::rng::vals::Clkdiv::NODIV);
|
|
|
|
});
|
|
|
|
// wait for CONDRST to be set
|
|
|
|
while !T::regs().cr().read().condrst() {}
|
|
|
|
// magic number must be written immediately before every read or write access to HTCR
|
|
|
|
T::regs().htcr().write(|w| w.set_htcfg(pac::rng::vals::Htcfg::MAGIC));
|
|
|
|
// write recommended value according to reference manual
|
|
|
|
// note: HTCR can only be written during conditioning
|
|
|
|
T::regs()
|
|
|
|
.htcr()
|
|
|
|
.write(|w| w.set_htcfg(pac::rng::vals::Htcfg::RECOMMENDED));
|
|
|
|
// finish conditioning
|
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
reg.set_condrst(false);
|
|
|
|
reg.set_ie(true);
|
|
|
|
});
|
|
|
|
// wait for CONDRST to be reset
|
|
|
|
while T::regs().cr().read().condrst() {}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:08:02 +01:00
|
|
|
pub async fn async_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_rngen(true);
|
|
|
|
});
|
2022-01-27 00:08:02 +01:00
|
|
|
|
|
|
|
for chunk in dest.chunks_mut(4) {
|
|
|
|
poll_fn(|cx| {
|
|
|
|
RNG_WAKER.register(cx.waker());
|
2023-06-19 03:07:26 +02:00
|
|
|
T::regs().cr().modify(|reg| {
|
|
|
|
reg.set_ie(true);
|
|
|
|
});
|
2022-01-27 00:08:02 +01:00
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
let bits = T::regs().sr().read();
|
2022-01-27 00:08:02 +01:00
|
|
|
|
|
|
|
if bits.drdy() {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else if bits.seis() {
|
|
|
|
self.reset();
|
|
|
|
Poll::Ready(Err(Error::SeedError))
|
|
|
|
} else if bits.ceis() {
|
|
|
|
self.reset();
|
|
|
|
Poll::Ready(Err(Error::ClockError))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
2023-06-19 03:07:26 +02:00
|
|
|
let random_bytes = T::regs().dr().read().to_be_bytes();
|
2022-01-27 00:08:02 +01:00
|
|
|
for (dest, src) in chunk.iter_mut().zip(random_bytes.iter()) {
|
|
|
|
*dest = *src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
impl<'d, T: Instance> RngCore for Rng<'d, T> {
|
2021-05-06 20:33:29 +02:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
loop {
|
2023-06-19 03:07:26 +02:00
|
|
|
let sr = T::regs().sr().read();
|
2023-02-08 16:52:49 +01:00
|
|
|
if sr.seis() | sr.ceis() {
|
|
|
|
self.reset();
|
|
|
|
} else if sr.drdy() {
|
2023-06-19 03:07:26 +02:00
|
|
|
return T::regs().dr().read();
|
2021-05-06 20:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
let mut rand = self.next_u32() as u64;
|
|
|
|
rand |= (self.next_u32() as u64) << 32;
|
|
|
|
rand
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
for chunk in dest.chunks_mut(4) {
|
|
|
|
let rand = self.next_u32();
|
|
|
|
for (slot, num) in chunk.iter_mut().zip(rand.to_be_bytes().iter()) {
|
|
|
|
*slot = *num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
2021-05-10 01:19:07 +02:00
|
|
|
self.fill_bytes(dest);
|
2021-05-06 20:33:29 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
impl<'d, T: Instance> CryptoRng for Rng<'d, T> {}
|
2021-05-06 20:33:29 +02:00
|
|
|
|
2021-04-26 20:11:46 +02:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
2021-05-06 20:33:29 +02:00
|
|
|
fn regs() -> pac::rng::Rng;
|
2021-04-26 20:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:54:53 +02:00
|
|
|
pub trait Instance: sealed::Instance + crate::rcc::RccPeripheral {}
|
2021-04-26 20:11:46 +02:00
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_peripheral!(
|
2021-06-03 17:09:29 +02:00
|
|
|
(rng, $inst:ident) => {
|
|
|
|
impl Instance for peripherals::$inst {}
|
|
|
|
|
|
|
|
impl sealed::Instance for peripherals::$inst {
|
|
|
|
fn regs() -> crate::pac::rng::Rng {
|
|
|
|
crate::pac::RNG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2021-06-03 17:09:29 +02:00
|
|
|
macro_rules! irq {
|
|
|
|
($irq:ident) => {
|
|
|
|
mod rng_irq {
|
|
|
|
use crate::interrupt;
|
|
|
|
|
|
|
|
#[interrupt]
|
|
|
|
unsafe fn $irq() {
|
|
|
|
let bits = $crate::pac::RNG.sr().read();
|
|
|
|
if bits.drdy() || bits.seis() || bits.ceis() {
|
|
|
|
$crate::pac::RNG.cr().write(|reg| reg.set_ie(false));
|
|
|
|
$crate::rng::RNG_WAKER.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_interrupt!(
|
2021-06-03 17:09:29 +02:00
|
|
|
(RNG) => {
|
|
|
|
irq!(RNG);
|
|
|
|
};
|
|
|
|
|
|
|
|
(RNG_LPUART1) => {
|
|
|
|
irq!(RNG_LPUART1);
|
|
|
|
};
|
|
|
|
|
|
|
|
(AES_RNG_LPUART1) => {
|
|
|
|
irq!(AES_RNG_LPUART1);
|
|
|
|
};
|
|
|
|
|
|
|
|
(AES_RNG) => {
|
|
|
|
irq!(AES_RNG);
|
|
|
|
};
|
|
|
|
|
|
|
|
(HASH_RNG) => {
|
|
|
|
irq!(HASH_RNG);
|
|
|
|
};
|
|
|
|
);
|