From b24cf4fd8efa26ef8f4709aff3d982bbe98d7b0b Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 13 Jun 2024 15:42:58 +0800 Subject: [PATCH] add guardband code to driver --- libs/cramium-hal/src/sce/trng.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libs/cramium-hal/src/sce/trng.rs b/libs/cramium-hal/src/sce/trng.rs index cf655d88e..16fdf95f4 100644 --- a/libs/cramium-hal/src/sce/trng.rs +++ b/libs/cramium-hal/src/sce/trng.rs @@ -79,6 +79,13 @@ enum Mode { } const RAW_ENTRIES: usize = 16; +/// The guardband is a number of entries of the TRNG to dispose of after +/// sampling for QC. The idea is to allow the TRNG internal state to evolve +/// for at least this many cycles before the next sample is taken, thus +/// making it more difficult for any adversary to reason about the current +/// state of the TRNG given the QC samples. +const RAW_GUARDBAND: usize = 16; + pub struct Trng { pub csr: CSR, _count: u16, // vestigial, to be removed? @@ -150,6 +157,12 @@ impl Trng { *d = Some(self.csr.r(utra::trng::SFR_BUF)); } let r = self.csr.r(utra::trng::SFR_BUF); + while self.csr.r(utra::trng::SFR_SR) & Status::BUFREADY.bits() == 0 {} + // 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. + for _ in 0..RAW_GUARDBAND { + let _ = Some(self.csr.r(utra::trng::SFR_SR)); + } r }