diff --git a/kernel/src/arch/riscv/rand.rs b/kernel/src/arch/riscv/rand.rs index 67eb501a5..c742af7f9 100644 --- a/kernel/src/arch/riscv/rand.rs +++ b/kernel/src/arch/riscv/rand.rs @@ -37,17 +37,33 @@ pub fn init() { }); let trng_kernel_csr = CSR::new(TRNG_KERNEL.base as *mut u32); - while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {} - // discard the first entry, as it is always 0x0000_0000 - // this is because the read register is a pipeline stage behind the FIFO - // once the pipeline has been filled, there is no need to prime it again. - trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA); + if false { // raw random path - left in for debugging urandom, can strip out later + while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {} + // discard the first entry, as it is always 0x0000_0000 + // this is because the read register is a pipeline stage behind the FIFO + // once the pipeline has been filled, there is no need to prime it again. + trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA); + } else { // urandom path (recommended) + // simulations show this isn't strictly necessary, but I prefer to have it + // just in case a subtle bug in the reset logic leaves something deterministic + // in the connecting logic: the simulation coverage stops at the edge of the TRNG block. + for _ in 0..4 { + // wait until the urandom port is initialized + while trng_kernel_csr.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {} + // pull a dummy piece of data + trng_kernel_csr.rf(utra::trng_kernel::URANDOM_URANDOM); + } + } + } pub fn get_u32() -> u32 { let trng_kernel_csr = CSR::new(TRNG_KERNEL.base as *mut u32); - - while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {} - - trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA) + if false { // raw random path + while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {} + trng_kernel_csr.rf(utra::trng_kernel::DATA_DATA) + } else { // urandom path (recommended) + while trng_kernel_csr.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {} + trng_kernel_csr.rf(utra::trng_kernel::URANDOM_URANDOM) + } } diff --git a/loader/src/main.rs b/loader/src/main.rs index ab21233e8..7f7b791c9 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -1091,12 +1091,13 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { } else { // resume path use utralib::generated::*; - // TRNG virtual memory mapping already set up, but the hardware needs - // to have its pipeline flushed, as the first result is always 0. + // TRNG virtual memory mapping already set up, but we pump values out just to make sure + // the pipeline is fresh. Simulations show this isn't necessary, but I feel paranoid; + // I worry a subtle bug in the reset logic could leave deterministic values in the pipeline. let trng_csr = CSR::new(utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u32); - for _ in 0..4 { // only one is strictly necessary but more is no problem - while trng_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {} - trng_csr.rf(utra::trng_kernel::DATA_DATA); + for _ in 0..4 { + while trng_csr.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {} + trng_csr.rf(utra::trng_kernel::URANDOM_URANDOM); } // setup the `susres` register for a resume diff --git a/precursors/soc.svd b/precursors/soc.svd index 077e307f5..05760c02e 100755 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -671,6 +671,7 @@ register to disable individual events.]]> TXFULL + 0x0004 0x00 32 @@ -685,6 +686,7 @@ register to disable individual events.]]> RXEMPTY + 0x0008 0x00 32 @@ -771,6 +773,7 @@ register to disable individual events.]]> TXEMPTY + 0x0018 0x00 32 @@ -785,6 +788,7 @@ register to disable individual events.]]> RXFULL + 0x001c 0x00 32 @@ -829,6 +833,7 @@ register to disable individual events.]]> TXFULL + 0x0004 0x00 32 @@ -843,6 +848,7 @@ register to disable individual events.]]> RXEMPTY + 0x0008 0x00 32 @@ -929,6 +935,7 @@ register to disable individual events.]]> TXEMPTY + 0x0018 0x00 32 @@ -943,6 +950,7 @@ register to disable individual events.]]> RXFULL + 0x001c 0x00 32 @@ -987,6 +995,7 @@ register to disable individual events.]]> TXFULL + 0x0004 0x00 32 @@ -1001,6 +1010,7 @@ register to disable individual events.]]> RXEMPTY + 0x0008 0x00 32 @@ -1087,6 +1097,7 @@ register to disable individual events.]]> TXEMPTY + 0x0018 0x00 32 @@ -1101,6 +1112,7 @@ register to disable individual events.]]> RXFULL + 0x001c 0x00 32 @@ -1210,10 +1222,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xa97001d2 + 0x92629e91 32 @@ -1230,7 +1242,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x10 + 0x16 32 @@ -3439,7 +3451,7 @@ inject too fast.]]> SEED1 0x0000 - 0x67ec0023 + 0x24a0246f 32 @@ -3454,7 +3466,7 @@ inject too fast.]]> SEED0 0x0004 - 0x64b8f094 + 0x43f81cb4 32 @@ -3938,14 +3950,49 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> 31 [31:0] 0 - + + + + + + URANDOM + + 0x0008 + 0x00 + 32 + + + urandom + 31 + [31:0] + 0 + + + + + URANDOM_VALID + + 0x000c + 0x00 + 32 + + + urandom_valid + 0 + [0:0] + 0 EV_STATUS - 0x0008 + 0x0010 0x00 32 @@ -3968,7 +4015,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> EV_PENDING - 0x000c + 0x0014 0x00 32 @@ -3991,7 +4038,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> EV_ENABLE - 0x0010 + 0x0018 0x00 32 @@ -4014,7 +4061,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> 0 - 0x14 + 0x1c registers @@ -4082,7 +4129,11 @@ when cleared, TRNGs are always on]]> 31 [31:0] 0 - + @@ -4120,6 +4171,13 @@ when cleared, TRNGs are always on]]> 21 + + chacha_ready + 22 + [22:22] + 22 + + @@ -4152,6 +4210,14 @@ decrease quality. Default is 32.]]> directly placed into the FIFO at full rate, creating a 'virtual oscilloscope' snapshot of the avalanche generator waveform.]]> + + required + 29 + [29:29] + 29 + + @@ -4501,7 +4567,7 @@ generators]]> RO_RUNSLIMIT1 0x003c - 0x1269b3 + 0x1241b8 32 @@ -4523,7 +4589,7 @@ generators]]> RO_RUNSLIMIT2 0x0040 - 0xa10bd + 0x9f0c1 32 @@ -4545,7 +4611,7 @@ generators]]> RO_RUNSLIMIT3 0x0044 - 0x5984d + 0x57850 32 @@ -4567,7 +4633,7 @@ generators]]> RO_RUNSLIMIT4 0x0048 - 0x3281a + 0x3181d 32 @@ -4589,7 +4655,7 @@ generators]]> RO_RUNSLIMIT5 0x004c - 0x1d805 + 0x1c807 32 @@ -4611,7 +4677,7 @@ generators]]> RO_RUN0_CTRL 0x0050 - 0x400 + 0x3ff 32 @@ -4619,7 +4685,7 @@ generators]]> 10 [10:0] 0 - @@ -4718,7 +4784,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_CTRL 0x006c - 0x400 + 0x3ff 32 @@ -4726,7 +4792,7 @@ updated at least once since the last readout of any register]]> 10 [10:0] 0 - @@ -4825,7 +4891,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_CTRL 0x0088 - 0x400 + 0x3ff 32 @@ -4833,7 +4899,7 @@ updated at least once since the last readout of any register]]> 10 [10:0] 0 - @@ -4932,7 +4998,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_CTRL 0x00a4 - 0x400 + 0x3ff 32 @@ -4940,7 +5006,7 @@ updated at least once since the last readout of any register]]> 10 [10:0] 0 - @@ -5343,10 +5409,104 @@ updated at least once since the last readout of any register]]> + + CHACHA + 0x00e8 + 0x100c8002 + 32 + + + reseed_interval + 11 + [11:0] + 0 + + + + selfmix_interval + 27 + [27:12] + 12 + + + + selfmix_ena + 28 + [28:28] + 28 + + + + + + SEED + + 0x00ec + 0x00 + 32 + + + seed + 31 + [31:0] + 0 + + + + + URANDOM + + 0x00f0 + 0x00 + 32 + + + urandom + 31 + [31:0] + 0 + + + + + URANDOM_VALID + + 0x00f4 + 0x00 + 32 + + + urandom_valid + 0 + [0:0] + 0 + + + + + TEST + 0x00f8 + 0x00 + 32 + + + simultaneous + 0 + [0:0] + 0 + + + + 0 - 0xe8 + 0xfc registers diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index d3d12b523..f57811887 100755 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/shellchat/src/cmds/trng_cmd.rs b/services/shellchat/src/cmds/trng_cmd.rs index 9f9cd7882..42fc7b2fa 100644 --- a/services/shellchat/src/cmds/trng_cmd.rs +++ b/services/shellchat/src/cmds/trng_cmd.rs @@ -58,13 +58,14 @@ impl<'a> ShellCmdApi<'a> for TrngCmd { ).unwrap(); } "pump" => { - for i in 0..4 { + const ROUNDS: usize = 16; + for i in 0..ROUNDS { log::debug!("pump round {}", i); let mut buf: [u32; 1024] = [0; 1024]; env.trng.fill_buf(&mut buf).unwrap(); log::debug!("pump samples: {:x}, {:x}, {:x}", buf[0], buf[512], buf[1023]); } - write!(ret, "Pumped 4x1k values out of the engine").unwrap(); + write!(ret, "Pumped {}x1k values out of the engine", ROUNDS).unwrap(); } "errs" => { write!(ret, "TRNG error stats: {:?}", env.trng.get_error_stats().unwrap()).unwrap(); diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index 61584f5c1..51b402e29 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -24,4 +24,5 @@ utralib = { path = "../../utralib"} debugprint = [] avalanchetest = [] ringosctest = [] +urandomtest = [] default = [] # "debugprint" diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index 2c0ad6173..0dbba9035 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -173,6 +173,14 @@ mod implementation { ); trng.susres_manager.push(RegOrField::Reg(utra::trng_server::RO_CONFIG), None); + // slightly reduce the frequency of these to reduce power + trng.csr.wo(utra::trng_server::CHACHA, + trng.csr.ms(utra::trng_server::CHACHA_RESEED_INTERVAL, 2) + | trng.csr.ms(utra::trng_server::CHACHA_SELFMIX_INTERVAL, 1000) + | trng.csr.ms(utra::trng_server::CHACHA_SELFMIX_ENA, 1) + ); + trng.susres_manager.push(RegOrField::Reg(utra::trng_server::CHACHA), None); + // handle error interrupts xous::claim_interrupt( utra::trng_server::TRNG_SERVER_IRQ, @@ -197,6 +205,21 @@ mod implementation { log::debug!("hardware initialized"); + if trng.csr.rf(utra::trng_server::STATUS_CHACHA_READY) == 0 { + log::trace!("chacha not ready"); + } else { + log::trace!("chacha ready"); + if trng.csr.rf(utra::trng_server::URANDOM_VALID_URANDOM_VALID) == 0 { + log::trace!("chacha not valid"); + } + } + log::trace!("chacha rands: 0x{:08x} 0x{:08x} 0x{:08x} 0x{:08x}", + trng.csr.rf(utra::trng_server::URANDOM_URANDOM), + trng.csr.rf(utra::trng_server::URANDOM_URANDOM), + trng.csr.rf(utra::trng_server::URANDOM_URANDOM), + trng.csr.rf(utra::trng_server::URANDOM_URANDOM), + ); + trng } // for the test procedure @@ -317,19 +340,27 @@ mod implementation { pub fn get_data_eager(&mut self) -> u32 { let mut timeout = 0; - while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { - if timeout > 100 { - log::debug!("TRNG ran out of data, blocked on READY: 0x{:x}", self.csr.r(utra::trng_server::READY)); - log::debug!("ROstats: 0x{:x} 0x{:x} 0x{:x} 0x{:x}", - self.csr.r(utra::trng_server::NIST_RO_STAT0), self.csr.r(utra::trng_server::NIST_RO_STAT1), - self.csr.r(utra::trng_server::NIST_RO_STAT2), self.csr.r(utra::trng_server::NIST_RO_STAT3)); - self.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1); - timeout = 0; + if false { // raw random + while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { + if timeout > 100 { + log::debug!("TRNG ran out of data, blocked on READY: 0x{:x}", self.csr.r(utra::trng_server::READY)); + log::debug!("ROstats: 0x{:x} 0x{:x} 0x{:x} 0x{:x}", + self.csr.r(utra::trng_server::NIST_RO_STAT0), self.csr.r(utra::trng_server::NIST_RO_STAT1), + self.csr.r(utra::trng_server::NIST_RO_STAT2), self.csr.r(utra::trng_server::NIST_RO_STAT3)); + self.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1); + timeout = 0; + } + xous::yield_slice(); + timeout += 1; } - xous::yield_slice(); - timeout += 1; + self.csr.rf(utra::trng_server::DATA_DATA) + } else { // urandom + // in practice, urandom generates data fast enough that we could skip this check + // you would need a fully unrolled read loop to exceed the generation rate + // but, better safe than sorry! + while self.csr.rf(utra::trng_server::URANDOM_VALID_URANDOM_VALID) == 0 {} + self.csr.rf(utra::trng_server::URANDOM_URANDOM) } - self.csr.rf(utra::trng_server::DATA_DATA) } #[allow(dead_code)] @@ -445,14 +476,14 @@ mod implementation { } } -#[cfg(any(feature = "avalanchetest", feature="ringosctest"))] +#[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] pub const TRNG_BUFF_LEN: usize = 512*1024; -#[cfg(any(feature = "avalanchetest", feature="ringosctest"))] +#[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] pub enum WhichMessible { One, Two, } -#[cfg(any(feature = "avalanchetest", feature="ringosctest"))] +#[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] struct Tester { server_csr: utralib::CSR, messible_csr: utralib::CSR, @@ -461,7 +492,7 @@ struct Tester { buffer_b: xous::MemoryRange, ticktimer: ticktimer_server::Ticktimer, } -#[cfg(any(feature = "avalanchetest", feature="ringosctest"))] +#[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] impl Tester { pub fn new(server_csr: *mut u32) -> Tester { use utralib::generated::*; @@ -551,10 +582,15 @@ impl Tester { pub fn get_data_eager(&self) -> u32 { use utralib::generated::*; - while self.server_csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { - xous::yield_slice(); + if cfg!(feature = "avalanchetest") || cfg!(feature = "ringosctest") { + while self.server_csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { + xous::yield_slice(); + } + self.server_csr.rf(utra::trng_server::DATA_DATA) + } else { + while self.csr.rf(utra::trng_server::URANDOM_VALID_URANDOM_VALID) == 0 {} + self.csr.rf(utra::trng_server::URANDOM_URANDOM) } - self.server_csr.rf(utra::trng_server::DATA_DATA) } #[allow(dead_code)] pub fn wait_full(&self) { @@ -564,7 +600,7 @@ impl Tester { } } } -#[cfg(any(feature = "avalanchetest", feature="ringosctest"))] +#[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] fn tester_thread(csr: usize) { let mut trng = Tester::new(csr as *mut u32); @@ -581,6 +617,12 @@ fn tester_thread(csr: usize) { unsafe { buff_b.add(i).write_volatile(trng.get_data_eager()) }; } log::info!("TRNG_TESTER: starting service"); + // confirm that the config flags work as embedded in get_data_eager + if cfg!(feature = "avalanchetest") || cfg!(feature = "ringosctest") { + log::info!("TRNG_TESTER: using raw data sources"); + } else { + log::info!("TRNG_TESTER: using urandom data sources"); + } let mut phase = 1; trng.messible_send(WhichMessible::One, phase); // indicate buffer A is ready to go @@ -610,7 +652,7 @@ fn xmain() -> ! { use crate::implementation::Trng; log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Debug); + log::set_max_level(log::LevelFilter::Info); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -625,7 +667,10 @@ fn xmain() -> ! { #[cfg(feature = "ringosctest")] log::info!("TRNG built with ring oscillator test enabled"); - #[cfg(any(feature = "avalanchetest", feature="ringosctest"))] + #[cfg(feature = "urandomtest")] + log::info!("TRNG built with urandom test enabled"); + + #[cfg(any(feature = "avalanchetest", feature="ringosctest", feature="urandomtest"))] xous::create_thread_1(tester_thread, trng.get_trng_csr() as usize).expect("couldn't create test thread"); // pump the TRNG hardware to clear the first number out, sometimes it is 0 due to clock-sync issues on the fifo