Skip to content

Commit 9cd13a7

Browse files
author
Henrik Snöman
committed
Added CryptRng and modes
1 parent d057d27 commit 9cd13a7

File tree

1 file changed

+66
-30
lines changed

1 file changed

+66
-30
lines changed

src/rng.rs

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ fn setup_clocks(prec: rec::Rng, clocks: &CoreClocks) -> Hertz {
6565
/// own verification or request documentation from ST directly.
6666
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
6767
pub trait RngNist {
68-
fn rng_nist_st_an4230(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng;
68+
fn rng_nist_st_an4230(
69+
self,
70+
prec: rec::Rng,
71+
clocks: &CoreClocks,
72+
) -> Rng<NIST>;
6973
}
7074

7175
#[cfg(any(
@@ -79,7 +83,11 @@ impl RngNist for RNG {
7983
/// using this HAL has not been performed. Users can/should do their
8084
/// own verification or request documentation from ST directly.
8185
/// Requires RNG to be disabled since some register values can only be written when RNGEN = 0
82-
fn rng_nist_st_an4230(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng {
86+
fn rng_nist_st_an4230(
87+
self,
88+
prec: rec::Rng,
89+
clocks: &CoreClocks,
90+
) -> Rng<NIST> {
8391
let rng_clk = setup_clocks(prec, clocks);
8492

8593
// ST has tested this configuration only with a RNG clock of 48MHz
@@ -105,18 +113,21 @@ impl RngNist for RNG {
105113
// Enable RNG
106114
self.cr().modify(|_, w| w.rngen().set_bit());
107115

108-
Rng { rb: self }
116+
Rng {
117+
rb: self,
118+
mode: NIST,
119+
}
109120
}
110121
}
111122

112123
pub trait RngExt {
113-
fn rng(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng;
114-
fn rng_fast(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng;
124+
fn rng(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng<NORMAL>;
125+
fn rng_fast(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng<FAST>;
115126
}
116127

117128
impl RngExt for RNG {
118129
/// This uses the register values specified in RM0481 Rev 2 section 32.6.2 RNG configuration C
119-
fn rng(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng {
130+
fn rng(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng<NORMAL> {
120131
setup_clocks(prec, clocks);
121132

122133
// Set control register values, also need to write 1 to CONDRST to be able to set the other values
@@ -156,11 +167,14 @@ impl RngExt for RNG {
156167
// Enable RNG
157168
self.cr().modify(|_, w| w.rngen().set_bit());
158169

159-
Rng { rb: self }
170+
Rng {
171+
rb: self,
172+
_mode: NORMAL,
173+
}
160174
}
161175

162176
/// This uses the register values specified in RM0481 Rev 2 section 32.6.2 RNG configuration B
163-
fn rng_fast(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng {
177+
fn rng_fast(self, prec: rec::Rng, clocks: &CoreClocks) -> Rng<FAST> {
164178
setup_clocks(prec, clocks);
165179

166180
// Set control register values, also need to write 1 to CONDRST to be able to set the other values
@@ -198,7 +212,10 @@ impl RngExt for RNG {
198212
// Enable RNG
199213
self.cr().modify(|_, w| w.rngen().set_bit());
200214

201-
Rng { rb: self }
215+
Rng {
216+
rb: self,
217+
_mode: FAST,
218+
}
202219
}
203220
}
204221

@@ -207,27 +224,39 @@ pub trait RngCore<W> {
207224
fn fill(&mut self, dest: &mut [W]) -> Result<(), Error>;
208225
}
209226

210-
pub struct Rng {
227+
#[cfg(any(
228+
feature = "stm32h562",
229+
feature = "stm32h563",
230+
feature = "stm32h573"
231+
))]
232+
/// NIST mode (type state)
233+
/// Use [RngNist::rng_nist_st_an4230] to generate [Rng] in this mode
234+
pub struct NIST;
235+
/// FAST mode (type state)
236+
/// Use [RngExt::rng_fast] to generate [Rng] in this mode
237+
pub struct FAST;
238+
/// NORMAL mode (type state)
239+
/// Use [RngExt::rng] to generate [Rng] in this mode
240+
pub struct NORMAL;
241+
242+
pub struct Rng<MODE> {
211243
rb: RNG,
244+
_mode: MODE,
212245
}
213246

214-
impl Rng {
247+
impl<MODE> Rng<MODE> {
215248
/// Returns 32 bits of randomness, or error
216249
pub fn value(&mut self) -> Result<u32, Error> {
217-
nb::block!(self.nb_value())
218-
}
219-
220-
/// Returns 32 bits of randomness, or error
221-
pub fn nb_value(&mut self) -> nb::Result<u32, Error> {
222-
let status = self.rb.sr().read();
223-
if status.cecs().bit() {
224-
Err(nb::Error::Other(Error::ClockError))
225-
} else if status.secs().bit() {
226-
Err(nb::Error::Other(Error::SeedError))
227-
} else if status.drdy().bit() {
228-
Ok(self.rb.dr().read().rndata().bits())
229-
} else {
230-
Err(nb::Error::WouldBlock)
250+
loop {
251+
let status = self.rb.sr().read();
252+
253+
if status.cecs().bit() {
254+
return Err(Error::ClockError);
255+
} else if status.secs().bit() {
256+
return Err(Error::SeedError);
257+
} else if status.drdy().bit() {
258+
return Ok(self.rb.dr().read().rndata().bits());
259+
}
231260
}
232261
}
233262

@@ -246,7 +275,7 @@ impl Rng {
246275
}
247276
}
248277

249-
impl core::iter::Iterator for Rng {
278+
impl<MODE> core::iter::Iterator for Rng<MODE> {
250279
type Item = u32;
251280

252281
fn next(&mut self) -> Option<u32> {
@@ -257,7 +286,7 @@ impl core::iter::Iterator for Rng {
257286
macro_rules! rng_core {
258287
($($type:ty),+) => {
259288
$(
260-
impl RngCore<$type> for Rng {
289+
impl<MODE> RngCore<$type> for Rng<MODE> {
261290
/// Returns a single element with random value, or error
262291
fn gen(&mut self) -> Result<$type, Error> {
263292
let val = self.value()?;
@@ -290,7 +319,7 @@ macro_rules! rng_core {
290319
macro_rules! rng_core_large {
291320
($($type:ty),+) => {
292321
$(
293-
impl RngCore<$type> for Rng {
322+
impl<MODE> RngCore<$type> for Rng<MODE> {
294323
fn gen(&mut self) -> Result<$type, Error> {
295324
const WORDS: usize = mem::size_of::<$type>() / mem::size_of::<u32>();
296325
let mut res: $type = 0;
@@ -316,7 +345,7 @@ macro_rules! rng_core_large {
316345
macro_rules! rng_core_transmute {
317346
($($type:ty = $from:ty),+) => {
318347
$(
319-
impl RngCore<$type> for Rng {
348+
impl<MODE> RngCore<$type> for Rng<MODE> {
320349
fn gen(&mut self) -> Result<$type, Error> {
321350
let num = <Self as RngCore<$from>>::gen(self)?;
322351
Ok(unsafe { mem::transmute::<$from, $type>(num) })
@@ -359,7 +388,7 @@ rng_core_large!(usize);
359388
// rand_core
360389
#[cfg(feature = "rand")]
361390
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
362-
impl rand_core::RngCore for Rng {
391+
impl<MODE> rand_core::RngCore for Rng<MODE> {
363392
/// Generate a random u32
364393
/// Panics if RNG fails.
365394
fn next_u32(&mut self) -> u32 {
@@ -393,3 +422,10 @@ impl rand_core::RngCore for Rng {
393422
})
394423
}
395424
}
425+
426+
#[cfg(all(
427+
feature = "rand",
428+
any(feature = "stm32h562", feature = "stm32h563", feature = "stm32h573")
429+
))]
430+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
431+
impl rand_core::CryptoRng for Rng<NIST> {}

0 commit comments

Comments
 (0)