From 9217b9aff51adb78f9138cb74d84b99c0903c6c5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Dec 2025 15:34:39 +0800 Subject: [PATCH] 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. --- bao1x-boot/boot1/src/platform/bao1x/trng.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bao1x-boot/boot1/src/platform/bao1x/trng.rs b/bao1x-boot/boot1/src/platform/bao1x/trng.rs index 5a6c26f81..60f21d6e6 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/trng.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/trng.rs @@ -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