Update TRNG settings

new settings are much *much* better; no more need for entropy
compression.
This commit is contained in:
bunnie 2024-06-18 00:08:03 +08:00
parent 7a821a836f
commit 75cc40d866

View file

@ -91,13 +91,22 @@ pub struct Trng {
_count: u16, // vestigial, to be removed? _count: u16, // vestigial, to be removed?
mode: Mode, mode: Mode,
raw: [Option<u32>; RAW_ENTRIES], raw: [Option<u32>; RAW_ENTRIES],
#[cfg(feature = "compress-entropy")]
rng_var: u8,
} }
impl Trng { impl Trng {
pub fn new(base_addr: usize) -> Self { pub fn new(base_addr: usize) -> Self {
let csr = CSR::new(base_addr as *mut u32); let csr = CSR::new(base_addr as *mut u32);
#[cfg(feature = "compress-entropy")]
{
Trng { csr, _count: 0, mode: Mode::Uninit, raw: [None; RAW_ENTRIES], rng_var: 0 }
}
#[cfg(not(feature = "compress-entropy"))]
{
Trng { csr, _count: 0, mode: Mode::Uninit, raw: [None; RAW_ENTRIES] } Trng { csr, _count: 0, mode: Mode::Uninit, raw: [None; RAW_ENTRIES] }
} }
}
pub fn setup_raw_generation(&mut self, count: u16) { pub fn setup_raw_generation(&mut self, count: u16) {
self._count = count; self._count = count;
@ -113,9 +122,10 @@ impl Trng {
); );
// turn on all the analog generators, and declare their outputs valid // turn on all the analog generators, and declare their outputs valid
self.csr.wo(utra::trng::SFR_CRANA, (Analog::ENABLE_MASK | Analog::VALID_MASK).bits()); self.csr.wo(utra::trng::SFR_CRANA, (Analog::ENABLE_MASK | Analog::VALID_MASK).bits());
// enable the rng chains // Enable the rng chains. This must be set correctly: get this wrong, and entropy drops from something
self.csr.wo(utra::trng::SFR_CHAIN_RNGCHAINEN0, 0xffff_ffff); // like 0.5-0.8 bits/bit to ~0.01 bits/bit.
self.csr.wo(utra::trng::SFR_CHAIN_RNGCHAINEN1, 0xffff_ffff); self.csr.wo(utra::trng::SFR_CHAIN_RNGCHAINEN0, 0xfffe);
self.csr.wo(utra::trng::SFR_CHAIN_RNGCHAINEN1, 0x1ffe);
self.csr.wo( self.csr.wo(
utra::trng::SFR_PP, utra::trng::SFR_PP,
@ -132,6 +142,12 @@ impl Trng {
} }
} }
while self.csr.r(utra::trng::SFR_SR) & Status::BUFREADY.bits() == 0 {}
#[cfg(feature = "compress-entropy")]
let mut sample = self.csr.r(utra::trng::SFR_BUF);
#[cfg(feature = "compress-entropy")]
let mut i: u32 = 0;
for d in self.raw.iter_mut() {
// Perform entropy compression. The TRNG itself is a sparse-1 oracle, which will // Perform entropy compression. The TRNG itself is a sparse-1 oracle, which will
// occasionally emit a 1 in a stream of 0's. The 1 itself has some periodicity // occasionally emit a 1 in a stream of 0's. The 1 itself has some periodicity
// to it, but at a period unrelated to the system clock. The algorithm is basically // to it, but at a period unrelated to the system clock. The algorithm is basically
@ -145,11 +161,8 @@ impl Trng {
// stream does have some long-term periodic behaviors in it, but it is also not entirely // stream does have some long-term periodic behaviors in it, but it is also not entirely
// predictable. Basically, the randomness seems to fluctuate in and out based on how // predictable. Basically, the randomness seems to fluctuate in and out based on how
// much noise is actually being coupled into the TRNG circuit. // much noise is actually being coupled into the TRNG circuit.
let mut rng_var: u8 = 0; #[cfg(feature = "compress-entropy")]
while self.csr.r(utra::trng::SFR_SR) & Status::BUFREADY.bits() == 0 {} {
let mut sample = self.csr.r(utra::trng::SFR_BUF);
let mut i: u32 = 0;
for d in self.raw.iter_mut() {
let mut output_buf = [0u8; 4]; let mut output_buf = [0u8; 4];
for b in output_buf.iter_mut() { for b in output_buf.iter_mut() {
loop { loop {
@ -159,22 +172,35 @@ impl Trng {
sample = self.csr.r(utra::trng::SFR_BUF); sample = self.csr.r(utra::trng::SFR_BUF);
} }
if ((sample >> i) & 1) != 0 { if ((sample >> i) & 1) != 0 {
*b = rng_var; *b = self.rng_var;
// update loop vars *after* test and assignment // update loop vars *after* test and assignment
i += 1; i += 1;
rng_var = rng_var.wrapping_add(1); self.rng_var = self.rng_var.wrapping_add(1);
// break so we're assigning to the next byte // break so we're assigning to the next byte
break; break;
} else { } else {
// update all loop vars // update all loop vars
i += 1; i += 1;
rng_var = rng_var.wrapping_add(1); self.rng_var = self.rng_var.wrapping_add(1);
} }
} }
} }
*d = Some(u32::from_le_bytes(output_buf)); *d = Some(u32::from_le_bytes(output_buf));
} }
// With the adjusted settings for the TRNG, the output is not perfect, but
// substantially better than before. Previously it was maybe one bit per
// 64 bits in entropy, now it looks like better than 0.5. Still more analysis
// needs to be done, there are some subtle biases in the generator but they
// are small enough we can pass the numbers directly into the CSPRNG for
// mixing without compression.
#[cfg(not(feature = "compress-entropy"))]
{
while self.csr.r(utra::trng::SFR_SR) & Status::BUFREADY.bits() == 0 {}
*d = Some(self.csr.r(utra::trng::SFR_BUF));
}
}
// Run the TRNG state forward for some number of cycles to make it harder to draw // Run the TRNG state forward for some number of cycles to make it harder to draw
// any conclusions about the TRNG's state based on the reported raw samples. // any conclusions about the TRNG's state based on the reported raw samples.
for _ in 0..RAW_GUARDBAND { for _ in 0..RAW_GUARDBAND {