Skip to content

Commit

Permalink
RNG: small fixes
Browse files Browse the repository at this point in the history
* remove unsafe blocks
* make error type empty
* update comments
  • Loading branch information
Joel Aschmann committed Mar 20, 2023
1 parent 059c9f5 commit 2b99ee5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/hwrng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ use embedded_hal::blocking::rng::Read;

#[derive(Debug)]
#[non_exhaustive]
pub enum HWRNGError {
Other,
}
pub enum HWRNGError {}

/// Represents RIOTs hwrng module. It can be used via
/// `embedded_hal`s [`embedded_hal::blocking::rng::Read`] trait.
///
/// The main purpose of this module is to generate seeds for PRNGs like
/// [`rand::rngs::StdRng`] or [`crate::random::Random`] (see `prng` module).
/// [`rand::rngs::StdRng`] or [`crate::random::Random`] (see [`crate::prng`] module).
///
/// # Security
/// As stated in RIOTs hwrng module-description the quality of the generated
Expand Down
6 changes: 3 additions & 3 deletions src/prng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn riot_prng() -> Random<32> {
/// See this modules description regarding quality of the used seeds.
pub fn rand_prng() -> StdRng {
let mut buffer = [0u8; 32];
unsafe {
HWRNG.read(&mut buffer).unwrap_unchecked();
}

HWRNG.read(&mut buffer).unwrap();

StdRng::from_seed(buffer)
}
12 changes: 4 additions & 8 deletions src/random.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{marker::PhantomData, mem::size_of};
use core::mem::size_of;

use embedded_hal::blocking::rng::Read;

Expand Down Expand Up @@ -27,7 +27,7 @@ use crate::hwrng::HWRNG;
#[derive(Debug)]
pub struct Random<const SEED_LENGTH: usize> {
// Make sure this gets not manually constructed
private: PhantomData<()>,
_private: (),
}

impl<const SEED_LENGTH: usize> RngCore for Random<SEED_LENGTH> {
Expand Down Expand Up @@ -92,9 +92,7 @@ impl<const SEED_LENGTH: usize> RandomSeed<SEED_LENGTH> {
pub fn new_from_hwrng() -> Self {
let mut seed = RandomSeed::<SEED_LENGTH>::default();

unsafe {
HWRNG.read(&mut seed.buffer()).unwrap_unchecked();
}
HWRNG.read(seed.buffer()).unwrap();

seed
}
Expand Down Expand Up @@ -129,8 +127,6 @@ impl<const SEED_LENGTH: usize> SeedableRng for Random<SEED_LENGTH> {
(seed.seed.len() / size_of::<i32>()) as i32,
);
}
Random {
private: PhantomData,
}
Random { _private: () }
}
}

0 comments on commit 2b99ee5

Please sign in to comment.