allow gen2 to use the seeded trng fill

This commit is contained in:
bunnie 2025-11-18 19:35:59 +08:00
parent 0942858bbc
commit 69b77f3072

View file

@ -7,6 +7,7 @@ use aes_gcm_siv::Nonce;
use bao1x_emu::trng::Trng;
#[cfg(all(feature = "bao1x-hal", not(feature = "hosted-baosec")))]
use bao1x_hal_service::trng::Trng;
#[cfg(not(feature = "gen2"))]
use rand_core::RngCore;
#[cfg(all(not(feature = "bao1x-hal"), not(feature = "hosted-baosec")))]
use trng::Trng;
@ -23,14 +24,22 @@ impl TrngPool {
let xns = xous_names::XousNames::new().unwrap();
let mut trng = Trng::new(&xns).unwrap();
let mut cache: [u8; 8192] = [0; 8192];
#[cfg(not(feature = "gen2"))]
trng.fill_bytes(&mut cache);
// speed up gen2 ops with seeded fill
#[cfg(feature = "gen2")]
trng.fill_seeded_bytes(&mut cache);
TrngPool { trng: RefCell::new(trng), e_cache: RefCell::new(cache.to_vec()) }
}
pub(crate) fn ensure_entropy(&self, amount: usize) {
if self.e_cache.borrow().len() < amount {
let mut cache: [u8; 8192] = [0; 8192];
#[cfg(not(feature = "gen2"))]
self.trng.borrow_mut().fill_bytes(&mut cache);
// speed up gen2 ops with seeded fill
#[cfg(feature = "gen2")]
self.trng.borrow_mut().fill_seeded_bytes(&mut cache);
self.e_cache.borrow_mut().extend_from_slice(&cache);
}
}