mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
refactor TRNG reseed code
refcell leads to some panics in multithreaded code. Use an atomic usize to track reseed interval, which is a bit safer.
This commit is contained in:
parent
01a5d0aba9
commit
622652393c
1 changed files with 9 additions and 5 deletions
|
|
@ -1,14 +1,16 @@
|
|||
use std::cell::RefCell;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use rand_chacha::ChaCha8Rng;
|
||||
use rand_core::{CryptoRng, RngCore, SeedableRng};
|
||||
|
||||
const RESEED_INTERVAL: u32 = 32;
|
||||
|
||||
static RESEED: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Trng {
|
||||
csprng: RefCell<rand_chacha::ChaCha8Rng>,
|
||||
reseed_ctr: RefCell<u32>,
|
||||
}
|
||||
impl Trng {
|
||||
pub fn new(_xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||
|
|
@ -17,14 +19,16 @@ impl Trng {
|
|||
(xous::create_server_id().unwrap().to_u32().0 as u64)
|
||||
| ((xous::create_server_id().unwrap().to_u32().0 as u64) << 32),
|
||||
)),
|
||||
reseed_ctr: RefCell::new(0),
|
||||
})
|
||||
}
|
||||
|
||||
fn reseed(&self) {
|
||||
*self.reseed_ctr.borrow_mut() = *self.reseed_ctr.borrow() + 1;
|
||||
if *self.reseed_ctr.borrow() > RESEED_INTERVAL {
|
||||
*self.reseed_ctr.borrow_mut() = 0;
|
||||
let reseed_ctr = match RESEED.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)) {
|
||||
Ok(x) => x,
|
||||
Err(x) => x,
|
||||
};
|
||||
if reseed_ctr > RESEED_INTERVAL {
|
||||
RESEED.store(0, Ordering::SeqCst);
|
||||
// incorporate randomness from the TRNG
|
||||
let half = self.csprng.borrow_mut().next_u32();
|
||||
self.csprng.replace(rand_chacha::ChaCha8Rng::seed_from_u64(
|
||||
|
|
|
|||
Loading…
Reference in a new issue