add a quick health check to the RO trng

takes 8 samples and makes sure those values aren't repeated in the
next 8 samples.

has a ~1 in 500 million chance of giving a false positive. But
generally should catch: stuck-at fault, and also, a highly trained
oscillator fault.
This commit is contained in:
bunnie 2025-12-04 15:34:39 +08:00
parent c126f7b4f1
commit 9217b9aff5
No known key found for this signature in database

View file

@ -98,6 +98,21 @@ impl ManagedTrng {
None
};
// do a quick health check on the TRNG before using it. Looks for repeated values over 8 samples.
// this will rule out e.g. TRNG output tied to 0 or 1, or an external feed just feeding it data.
// About 1 in 500 million chance of triggering falsely.
let mut values = [0u32; 8];
for value in values.iter_mut() {
*value = ro_trng.get_raw();
}
for _ in 0..values.len() {
if values.contains(&ro_trng.get_raw()) {
// don't proceed if we see a stuck value
crate::println!("TRNG had stuck value, dying!");
bao1x_hal::sigcheck::die_no_std();
}
}
// The first seed is always 0.
let mut seed = [0u8; 32];
// println!("seed: {:x?}", seed); // used to eyeball that things are working correctly