mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +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::cell::RefCell;
|
||||||
|
use std::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
use rand_chacha::ChaCha8Rng;
|
use rand_chacha::ChaCha8Rng;
|
||||||
use rand_core::{CryptoRng, RngCore, SeedableRng};
|
use rand_core::{CryptoRng, RngCore, SeedableRng};
|
||||||
|
|
||||||
const RESEED_INTERVAL: u32 = 32;
|
const RESEED_INTERVAL: u32 = 32;
|
||||||
|
|
||||||
|
static RESEED: AtomicU32 = AtomicU32::new(0);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
csprng: RefCell<rand_chacha::ChaCha8Rng>,
|
csprng: RefCell<rand_chacha::ChaCha8Rng>,
|
||||||
reseed_ctr: RefCell<u32>,
|
|
||||||
}
|
}
|
||||||
impl Trng {
|
impl Trng {
|
||||||
pub fn new(_xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
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)
|
||||||
| ((xous::create_server_id().unwrap().to_u32().0 as u64) << 32),
|
| ((xous::create_server_id().unwrap().to_u32().0 as u64) << 32),
|
||||||
)),
|
)),
|
||||||
reseed_ctr: RefCell::new(0),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reseed(&self) {
|
fn reseed(&self) {
|
||||||
*self.reseed_ctr.borrow_mut() = *self.reseed_ctr.borrow() + 1;
|
let reseed_ctr = match RESEED.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)) {
|
||||||
if *self.reseed_ctr.borrow() > RESEED_INTERVAL {
|
Ok(x) => x,
|
||||||
*self.reseed_ctr.borrow_mut() = 0;
|
Err(x) => x,
|
||||||
|
};
|
||||||
|
if reseed_ctr > RESEED_INTERVAL {
|
||||||
|
RESEED.store(0, Ordering::SeqCst);
|
||||||
// incorporate randomness from the TRNG
|
// incorporate randomness from the TRNG
|
||||||
let half = self.csprng.borrow_mut().next_u32();
|
let half = self.csprng.borrow_mut().next_u32();
|
||||||
self.csprng.replace(rand_chacha::ChaCha8Rng::seed_from_u64(
|
self.csprng.replace(rand_chacha::ChaCha8Rng::seed_from_u64(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue