diff --git a/precursors/soc.svd b/precursors/soc.svd index 6d66c762d..077e307f5 100755 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1210,10 +1210,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x54f46891 + 0xa97001d2 32 @@ -1230,7 +1230,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 - 0x0f + 0x10 32 @@ -3439,7 +3439,7 @@ inject too fast.]]> SEED1 0x0000 - 0x5243fcb + 0x67ec0023 32 @@ -3454,7 +3454,7 @@ inject too fast.]]> SEED0 0x0004 - 0x34203b8b + 0x64b8f094 32 @@ -4235,7 +4235,7 @@ avalanche generator]]> RO_NIST 0x0018 - 0x1a433 + 0x1a443 32 @@ -4306,14 +4306,21 @@ generators]]> 7 [7:4] 4 - + ro_adaptive 11 [11:8] 8 - + + + + ro_miniruns + 15 + [15:12] + 12 + @@ -4342,7 +4349,7 @@ generators]]> 17 [17:11] 11 - + @@ -4371,7 +4378,7 @@ generators]]> 17 [17:11] 11 - + @@ -4400,7 +4407,7 @@ generators]]> 17 [17:11] 11 - + @@ -4429,7 +4436,7 @@ generators]]> 17 [17:11] 11 - + @@ -4458,7 +4465,7 @@ generators]]> 17 [17:11] 11 - + @@ -4487,13 +4494,123 @@ generators]]> 17 [17:11] 11 - + + + + + + RO_RUNSLIMIT1 + 0x003c + 0x1269b3 + 32 + + + min + 10 + [10:0] + 0 + + + + max + 21 + [21:11] + 11 + + + + + + RO_RUNSLIMIT2 + 0x0040 + 0xa10bd + 32 + + + min + 10 + [10:0] + 0 + + + + max + 21 + [21:11] + 11 + + + + + + RO_RUNSLIMIT3 + 0x0044 + 0x5984d + 32 + + + min + 10 + [10:0] + 0 + + + + max + 21 + [21:11] + 11 + + + + + + RO_RUNSLIMIT4 + 0x0048 + 0x3281a + 32 + + + min + 10 + [10:0] + 0 + + + + max + 21 + [21:11] + 11 + + + + + + RO_RUNSLIMIT5 + 0x004c + 0x1d805 + 32 + + + min + 10 + [10:0] + 0 + + + + max + 21 + [21:11] + 11 + RO_RUN0_CTRL - 0x003c + 0x0050 0x400 32 @@ -4511,7 +4628,7 @@ undefined behavior occurs]]> RO_RUN0_FRESH - 0x0040 + 0x0054 0x00 32 @@ -4526,7 +4643,7 @@ updated at least once since the last readout of any register]]> RO_RUN0_COUNT1 - 0x0044 + 0x0058 0x00 32 @@ -4541,7 +4658,7 @@ updated at least once since the last readout of any register]]> RO_RUN0_COUNT2 - 0x0048 + 0x005c 0x00 32 @@ -4556,7 +4673,7 @@ updated at least once since the last readout of any register]]> RO_RUN0_COUNT3 - 0x004c + 0x0060 0x00 32 @@ -4571,7 +4688,7 @@ updated at least once since the last readout of any register]]> RO_RUN0_COUNT4 - 0x0050 + 0x0064 0x00 32 @@ -4586,7 +4703,7 @@ updated at least once since the last readout of any register]]> RO_RUN0_COUNT5 - 0x0054 + 0x0068 0x00 32 @@ -4600,7 +4717,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_CTRL - 0x0058 + 0x006c 0x400 32 @@ -4618,7 +4735,7 @@ undefined behavior occurs]]> RO_RUN1_FRESH - 0x005c + 0x0070 0x00 32 @@ -4633,7 +4750,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_COUNT1 - 0x0060 + 0x0074 0x00 32 @@ -4648,7 +4765,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_COUNT2 - 0x0064 + 0x0078 0x00 32 @@ -4663,7 +4780,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_COUNT3 - 0x0068 + 0x007c 0x00 32 @@ -4678,7 +4795,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_COUNT4 - 0x006c + 0x0080 0x00 32 @@ -4693,7 +4810,7 @@ updated at least once since the last readout of any register]]> RO_RUN1_COUNT5 - 0x0070 + 0x0084 0x00 32 @@ -4707,7 +4824,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_CTRL - 0x0074 + 0x0088 0x400 32 @@ -4725,7 +4842,7 @@ undefined behavior occurs]]> RO_RUN2_FRESH - 0x0078 + 0x008c 0x00 32 @@ -4740,7 +4857,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_COUNT1 - 0x007c + 0x0090 0x00 32 @@ -4755,7 +4872,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_COUNT2 - 0x0080 + 0x0094 0x00 32 @@ -4770,7 +4887,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_COUNT3 - 0x0084 + 0x0098 0x00 32 @@ -4785,7 +4902,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_COUNT4 - 0x0088 + 0x009c 0x00 32 @@ -4800,7 +4917,7 @@ updated at least once since the last readout of any register]]> RO_RUN2_COUNT5 - 0x008c + 0x00a0 0x00 32 @@ -4814,7 +4931,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_CTRL - 0x0090 + 0x00a4 0x400 32 @@ -4832,7 +4949,7 @@ undefined behavior occurs]]> RO_RUN3_FRESH - 0x0094 + 0x00a8 0x00 32 @@ -4847,7 +4964,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_COUNT1 - 0x0098 + 0x00ac 0x00 32 @@ -4862,7 +4979,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_COUNT2 - 0x009c + 0x00b0 0x00 32 @@ -4877,7 +4994,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_COUNT3 - 0x00a0 + 0x00b4 0x00 32 @@ -4892,7 +5009,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_COUNT4 - 0x00a4 + 0x00b8 0x00 32 @@ -4907,7 +5024,7 @@ updated at least once since the last readout of any register]]> RO_RUN3_COUNT5 - 0x00a8 + 0x00bc 0x00 32 @@ -4921,7 +5038,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION0_CTRL - 0x00ac + 0x00c0 0x190140 32 @@ -4950,7 +5067,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION0_STAT - 0x00b0 + 0x00c4 0x00 32 @@ -4972,7 +5089,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION0_LAST_ERR - 0x00b4 + 0x00c8 0x00 32 @@ -4994,7 +5111,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION1_CTRL - 0x00b8 + 0x00cc 0x190140 32 @@ -5023,7 +5140,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION1_STAT - 0x00bc + 0x00d0 0x00 32 @@ -5045,7 +5162,7 @@ updated at least once since the last readout of any register]]> AV_EXCURSION1_LAST_ERR - 0x00c0 + 0x00d4 0x00 32 @@ -5065,10 +5182,39 @@ updated at least once since the last readout of any register]]> + + READY + 0x00d8 + 0x00 + 32 + + + av_excursion + 1 + [1:0] + 0 + + + + av_adaprop + 3 + [3:2] + 2 + + + + ro_adaprop + 7 + [7:4] + 4 + + + + EV_STATUS - 0x00c4 + 0x00dc 0x00 32 @@ -5112,7 +5258,7 @@ updated at least once since the last readout of any register]]> EV_PENDING - 0x00c8 + 0x00e0 0x00 32 @@ -5156,7 +5302,7 @@ updated at least once since the last readout of any register]]> EV_ENABLE - 0x00cc + 0x00e4 0x00 32 @@ -5200,7 +5346,7 @@ updated at least once since the last readout of any register]]> 0 - 0xd0 + 0xe8 registers diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index 19ac32c85..d3d12b523 100755 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index b748fca88..ccc0e818d 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -92,6 +92,7 @@ mod accel; use accel::*; mod sha; use sha::*; mod ecup; use ecup::*; mod aes; use aes::*; +mod trng_cmd; use trng_cmd::*; mod fcc; use fcc::*; mod pds; // dependency of the FCC file @@ -112,6 +113,7 @@ pub struct CmdEnv { sha_cmd: Sha, ecup_cmd: EcUpdate, aes_cmd: Aes, + trng_cmd: TrngCmd, fcc_cmd: Fcc, } @@ -146,6 +148,7 @@ impl CmdEnv { sha_cmd: sha, ecup_cmd: ecup, aes_cmd: aes, + trng_cmd: TrngCmd::new(), fcc_cmd: fcc, } @@ -175,6 +178,7 @@ impl CmdEnv { &mut self.sha_cmd, &mut self.ecup_cmd, &mut self.aes_cmd, + &mut self.trng_cmd, &mut self.fcc_cmd, ]; diff --git a/services/shellchat/src/cmds/trng_cmd.rs b/services/shellchat/src/cmds/trng_cmd.rs new file mode 100644 index 000000000..9f9cd7882 --- /dev/null +++ b/services/shellchat/src/cmds/trng_cmd.rs @@ -0,0 +1,82 @@ +use crate::{ShellCmdApi, CommonEnv}; +use xous_ipc::String; + +#[derive(Debug)] +pub struct TrngCmd { +} +impl TrngCmd { + pub fn new() -> Self { + TrngCmd {} + } +} + +impl<'a> ShellCmdApi<'a> for TrngCmd { + cmd_api!(trng); + + fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { + use core::fmt::Write; + let mut ret = String::<1024>::new(); + let helpstring = "trng [avnist] [ronist] [runs] [excur] [errs] [pump]"; + + let mut tokens = args.as_str().unwrap().split(' '); + + if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "avnist" => { + let ht = env.trng.get_health_tests().unwrap(); + write!(ret, "AV NIST stats: {:?}", ht.av_nist).unwrap(); + } + "ronist" => { + let ht = env.trng.get_health_tests().unwrap(); + write!(ret, "RO NIST stats: {:?}", ht.ro_nist).unwrap(); + } + "runs" => { + let ht = env.trng.get_health_tests().unwrap(); + for core in 0..4 { + write!(ret, "RO {}: ", core).unwrap(); + for bin in 0..5 { + write!(ret, "{} ", ht.ro_miniruns[core].run_count[bin]).unwrap(); + } + write!(ret, "\n").unwrap(); + } + } + "excur" => { + let ht = env.trng.get_health_tests().unwrap(); + write!(ret, "AV0: {:.0}/{:.0} mV\n" + ,(ht.av_excursion[0].min as f64 / 4096.0) * 1000.0 + ,(ht.av_excursion[0].max as f64 / 4096.0) * 1000.0 + ).unwrap(); + write!(ret, "AV0 delta: {:.0} mV\n" + ,((ht.av_excursion[0].max as f64 - ht.av_excursion[0].min as f64) / 4096.0) * 1000.0 + ).unwrap(); + write!(ret, "AV1: {:.0}/{:.0} mV\n" + ,(ht.av_excursion[1].min as f64 / 4096.0) * 1000.0 + ,(ht.av_excursion[1].max as f64 / 4096.0) * 1000.0 + ).unwrap(); + write!(ret, "AV1 delta: {:.0} mV\n" + ,((ht.av_excursion[1].max as f64 - ht.av_excursion[1].min as f64) / 4096.0) * 1000.0 + ).unwrap(); + } + "pump" => { + for i in 0..4 { + 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(); + } + "errs" => { + write!(ret, "TRNG error stats: {:?}", env.trng.get_error_stats().unwrap()).unwrap(); + } + _ => { + write!(ret, "{}", helpstring).unwrap(); + } + } + + } else { + write!(ret, "{}", helpstring).unwrap(); + } + Ok(Some(ret)) + } +} diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 80059c2eb..896b3f7e1 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -258,7 +258,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index d5d7e8e26..b6e6e26f3 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -36,11 +36,20 @@ pub struct TrngErrors { pub server_underruns: Option, } +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct TrngBuf { + pub data: [u32; 1024], + pub len: u16, +} + #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { /// Get one or two 32-bit words of TRNG data GetTrng, + /// Fill a buffer with random data + FillTrng, + /// Suspend/resume callback SuspendResume, diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index c7320a8cf..3ca32267a 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -41,6 +41,23 @@ impl Trng { panic!("unexpected return value: {:#?}", response); } } + pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> { + let mut tb = api::TrngBuf { data: [0; 1024], len: 0 }; + if data.len() > tb.data.len() { + return Err(xous::Error::OutOfMemory); + } + tb.len = data.len() as u16; + let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, api::Opcode::FillTrng.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + let rtb: api::TrngBuf = buf.to_original().unwrap(); + if rtb.len as usize != data.len() { + return Err(xous::Error::InternalError) + } + for (&src, dst) in rtb.data.iter().zip(data.iter_mut()) { + *dst = src; + } + Ok(()) + } pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> { if self.error_sid.is_none() { let sid = xous::create_server().unwrap(); diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index e5a89caa4..2c0ad6173 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -20,7 +20,7 @@ struct ScalarCallback { #[cfg(target_os = "none")] mod implementation { use utralib::generated::*; - use crate::api::{ExcursionTest, MiniRunsTest, NistTests, HealthTests, TrngErrors}; + use crate::api::{ExcursionTest, MiniRunsTest, NistTests, HealthTests, TrngErrors, TrngBuf}; use susres::{RegManager, RegOrField, SuspendResume}; use num_traits::*; @@ -29,10 +29,13 @@ mod implementation { susres_manager: RegManager::<{utra::trng_server::TRNG_SERVER_NUMREGS}>, // probably can be reduced to save space? conn: xous::CID, errors: TrngErrors, + err_stat: HealthTests, } fn trng_handler(_irq_no: usize, arg: *mut usize) { let trng = unsafe { &mut *(arg as *mut Trng) }; + // cache a copy of the stats in the interrupt handler, so we can diagnose later + trng.err_stat = trng.get_tests(); let pending = trng.csr.r(utra::trng_server::EV_PENDING); if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION0, 1)) != 0 { @@ -75,14 +78,15 @@ mod implementation { trng.errors.kernel_underruns = Some(trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) as u16); } } - // reset any error flags + // reset any error flags. try to do this a bit away from the pending clear, so it has time to take effect trng.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1); - // clear the pending interrupt(s) - trng.csr.wo(utra::trng_server::EV_PENDING, pending); // notify the main loop of the error condition xous::try_send_message(trng.conn, xous::Message::new_scalar(crate::api::Opcode::ErrorNotification.to_usize().unwrap(), 0, 0, 0, 0)).map(|_|()).unwrap(); + + // clear the pending interrupt(s) + trng.csr.wo(utra::trng_server::EV_PENDING, pending); } impl Trng { @@ -107,7 +111,8 @@ mod implementation { ro_adaptive_errs: None, kernel_underruns: None, server_underruns: None, - } + }, + err_stat: HealthTests::default(), }; ///// configure power settings and which generator to use @@ -203,8 +208,23 @@ mod implementation { pub fn get_errors(&self) -> TrngErrors { self.errors } + pub fn get_err_stats(&self) -> HealthTests { + self.err_stat + } pub fn get_tests(&self) -> HealthTests { + // the fresh bit gets reset on the first read of the register. Ensure these reads happen first before the structure is initialized. + let av_nist_fresh0 = if self.csr.rf(utra::trng_server::NIST_AV_STAT0_FRESH) == 0 {false} else {true}; + let av_nist_fresh1 = if self.csr.rf(utra::trng_server::NIST_AV_STAT1_FRESH) == 0 {false} else {true}; + let ro_mr_fresh0 = if self.csr.rf(utra::trng_server::RO_RUN0_FRESH_RO_RUN0_FRESH) == 0 {false} else {true}; + let ro_mr_fresh1 = if self.csr.rf(utra::trng_server::RO_RUN1_FRESH_RO_RUN1_FRESH) == 0 {false} else {true}; + let ro_mr_fresh2 = if self.csr.rf(utra::trng_server::RO_RUN2_FRESH_RO_RUN2_FRESH) == 0 {false} else {true}; + let ro_mr_fresh3 = if self.csr.rf(utra::trng_server::RO_RUN3_FRESH_RO_RUN3_FRESH) == 0 {false} else {true}; + let ro_nist_fresh0 = if self.csr.rf(utra::trng_server::NIST_RO_STAT0_FRESH) == 0 {false} else {true}; + let ro_nist_fresh1 = if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}; + let ro_nist_fresh2 = if self.csr.rf(utra::trng_server::NIST_RO_STAT2_FRESH) == 0 {false} else {true}; + let ro_nist_fresh3 = if self.csr.rf(utra::trng_server::NIST_RO_STAT3_FRESH) == 0 {false} else {true}; + // now initialize the return structure HealthTests { av_excursion: [ ExcursionTest { @@ -218,18 +238,19 @@ mod implementation { ], av_nist: [ NistTests { + fresh: av_nist_fresh0, adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT0_FRESH) == 0 {false} else {true}, }, NistTests { + fresh: av_nist_fresh1, adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT1_FRESH) == 0 {false} else {true}, }, ], ro_miniruns: [ MiniRunsTest { + fresh: ro_mr_fresh0, run_count: [ self.csr.r(utra::trng_server::RO_RUN0_COUNT1) as u16, self.csr.r(utra::trng_server::RO_RUN0_COUNT2) as u16, @@ -237,9 +258,9 @@ mod implementation { self.csr.r(utra::trng_server::RO_RUN0_COUNT4) as u16, self.csr.r(utra::trng_server::RO_RUN0_COUNT5) as u16, ], - fresh: if self.csr.rf(utra::trng_server::RO_RUN0_FRESH_RO_RUN0_FRESH) == 0 {false} else {true}, }, MiniRunsTest { + fresh: ro_mr_fresh1, run_count: [ self.csr.r(utra::trng_server::RO_RUN1_COUNT1) as u16, self.csr.r(utra::trng_server::RO_RUN1_COUNT2) as u16, @@ -247,9 +268,9 @@ mod implementation { self.csr.r(utra::trng_server::RO_RUN1_COUNT4) as u16, self.csr.r(utra::trng_server::RO_RUN1_COUNT5) as u16, ], - fresh: if self.csr.rf(utra::trng_server::RO_RUN1_FRESH_RO_RUN1_FRESH) == 0 {false} else {true}, }, MiniRunsTest { + fresh: ro_mr_fresh2, run_count: [ self.csr.r(utra::trng_server::RO_RUN2_COUNT1) as u16, self.csr.r(utra::trng_server::RO_RUN2_COUNT2) as u16, @@ -257,9 +278,9 @@ mod implementation { self.csr.r(utra::trng_server::RO_RUN2_COUNT4) as u16, self.csr.r(utra::trng_server::RO_RUN2_COUNT5) as u16, ], - fresh: if self.csr.rf(utra::trng_server::RO_RUN2_FRESH_RO_RUN2_FRESH) == 0 {false} else {true}, }, MiniRunsTest { + fresh: ro_mr_fresh3, run_count: [ self.csr.r(utra::trng_server::RO_RUN3_COUNT1) as u16, self.csr.r(utra::trng_server::RO_RUN3_COUNT2) as u16, @@ -267,37 +288,46 @@ mod implementation { self.csr.r(utra::trng_server::RO_RUN3_COUNT4) as u16, self.csr.r(utra::trng_server::RO_RUN3_COUNT5) as u16, ], - fresh: if self.csr.rf(utra::trng_server::RO_RUN3_FRESH_RO_RUN3_FRESH) == 0 {false} else {true}, }, ], ro_nist: [ NistTests { + fresh: ro_nist_fresh0, adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT0_FRESH) == 0 {false} else {true}, }, NistTests { + fresh: ro_nist_fresh1, adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, }, NistTests { + fresh: ro_nist_fresh2, adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, }, NistTests { + fresh: ro_nist_fresh3, adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, - fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, }, ] } } - pub fn get_data_eager(&self) -> u32 { + 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; + } xous::yield_slice(); + timeout += 1; } self.csr.rf(utra::trng_server::DATA_DATA) } @@ -309,20 +339,18 @@ mod implementation { } } - pub fn get_trng(&self, count: usize) -> [u32; 2] { - // TODO: use SHA hardware unit to robustify the TRNG output against potential hardware failures - // TODO: health monitoring of raw TRNG output + pub fn get_buf(&mut self, len: u16) -> TrngBuf { + let mut tb = TrngBuf {data: [0; 1024], len}; + for i in 0..len as usize { + tb.data[i] = self.get_data_eager(); + } + tb + } + + pub fn get_trng(&mut self, count: usize) -> [u32; 2] { let mut ret: [u32; 2] = [0, 0]; - /* - in the final implementation the algorithm should be: - 1) check fullness of software-whitened pool - 2) if software pool is full enough, return values from there - 3) if pool is low, activate hardware TRNG and refill the pool (uses SHA unit) - 4) during pool-filling, perform statistics on the hardware TRNG output to check health - 5) confirm health is OK - */ - + // eventually this will come from a hardware 'urandom' style interface // for now, we just take data directly from the hardware-managed raw TRNG pool ret[0] = self.get_data_eager(); // we don't just draw down TRNGs if not requested, because they are a finite resource @@ -347,16 +375,16 @@ mod implementation { // a stub to try to avoid breaking hosted mode for as long as possible. #[cfg(not(target_os = "none"))] mod implementation { - use log::info; - pub struct Trng { seed: u32, + msgcount: u16, // re-print the message every time we rollover } impl Trng { pub fn new() -> Trng { Trng { seed: 0x1afe_cafe, + msgcount: 0, } } @@ -370,8 +398,24 @@ mod implementation { #[allow(dead_code)] pub fn wait_full(&self) { } + pub fn get_buf(&self, len: u16) -> TrngBuf { + if msgcount < 10 { + log::info!("hosted mode TRNG is *not* random, it is an LFSR"); + } + self.msgcount += 1; + let mut tb = TrngBuf {data: [0; 1024], len}; + for i in 0..len as usize { + self.seed = self.move_lfsr(self.seed); + tb.data[i] = self.seed; + } + tb + } + pub fn get_trng(&mut self, _count: usize) -> [u32; 2] { - info!("hosted mode TRNG is *not* random, it is an LFSR"); + if msgcount < 10 { + log::info!("hosted mode TRNG is *not* random, it is an LFSR"); + } + self.msgcount += 1; let mut ret: [u32; 2] = [0; 2]; self.seed = self.move_lfsr(self.seed); ret[0] = self.seed; @@ -566,7 +610,7 @@ fn xmain() -> ! { use crate::implementation::Trng; log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -613,6 +657,7 @@ fn xmain() -> ! { }, Some(api::Opcode::ErrorNotification) => { log::error!("Got an error condition in the TRNG. Syndrome: {:?}", trng.get_errors()); + log::error!("Stats: {:?}", trng.get_err_stats()); send_event(&error_cb_conns); }, Some(api::Opcode::HealthStats) => { @@ -623,6 +668,11 @@ fn xmain() -> ! { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; buffer.replace(trng.get_errors()).unwrap(); }, + Some(api::Opcode::FillTrng) => { + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let len = buffer.as_flat::().unwrap().len; + buffer.replace(trng.get_buf(len)).unwrap(); + }, Some(api::Opcode::Quit) => { break },