From 95b0639c514030ec44bd408e08be601a1cf54949 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 13 Sep 2025 23:30:01 +0800 Subject: [PATCH] add trng dump code this code, when run, will continuously dump base64-encoded TRNG traces to the serial port. Combined with the script in tools/trng_b64.py which runs on the host, one can recover extensive TRNG raw data for further analysis by TRNG characterization tools. --- baremetal/Cargo.toml | 8 +- baremetal/src/platform/cramium/cramium.rs | 2 +- .../src/platform/cramium/usb/handlers.rs | 14 ++++ baremetal/src/repl.rs | 77 ++++++++++++++++++- 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/baremetal/Cargo.toml b/baremetal/Cargo.toml index 82d84b031..50c6ebe6f 100644 --- a/baremetal/Cargo.toml +++ b/baremetal/Cargo.toml @@ -23,6 +23,9 @@ ed25519-dalek = { version = "=2.1.0", optional = true, default-features = false, "rand_core", "digest", ] } +base64 = { version = "0.22.1", optional = true, default-features = false, features = [ + "alloc", +] } [features] artybio = [ @@ -58,11 +61,12 @@ nto-usb = [ "sha2", "digest", "ed25519-dalek", - "cramium-hal/debug-print-usb", + # "cramium-hal/debug-print-usb", # "cramium-hal/verbose-debug", ] nto-buttons = [] +nto-trng = ["nto-usb", "base64"] verbose-debug = [] verilator-only = [] -default = ["nto-usb"] +default = ["nto-usb", "nto-trng"] diff --git a/baremetal/src/platform/cramium/cramium.rs b/baremetal/src/platform/cramium/cramium.rs index 1da9207e0..a83e89039 100644 --- a/baremetal/src/platform/cramium/cramium.rs +++ b/baremetal/src/platform/cramium/cramium.rs @@ -36,7 +36,7 @@ pub fn early_init() { // Define the .data region - bootstrap baremetal using these hard-coded parameters. // Define the .data region - bootstrap baremetal using these hard-coded parameters. const DATA_ORIGIN: usize = 0x61000000; - const DATA_INIT: [(usize, u32); 4] = [(0x0, 0x2), (0x53c, 0x1), (0x541, 0x1), (0x546, 0x1)]; + const DATA_INIT: [(usize, u32); 5] = [(0x0, 0x2), (0x558, 0x1), (0x55b, 0x1), (0x55e, 0x1), (0x563, 0x1)]; // Clear .data, .bss, .stack, .heap regions & setup .data values unsafe { diff --git a/baremetal/src/platform/cramium/usb/handlers.rs b/baremetal/src/platform/cramium/usb/handlers.rs index 63d2fe68c..01b4eb2b0 100644 --- a/baremetal/src/platform/cramium/usb/handlers.rs +++ b/baremetal/src/platform/cramium/usb/handlers.rs @@ -5,6 +5,8 @@ use cramium_hal::usb::driver::*; use super::*; +pub static TX_IDLE: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(true); + // Locate the "disk" pub(crate) const RAMDISK_ADDRESS: usize = crate::platform::HEAP_START + crate::platform::HEAP_LEN; pub(crate) const STACK_SIZE: usize = 128 * 1024; // 128k for stack is enough? maybe? if the ramdisk overflows, it smashes stack - dangerous! @@ -335,6 +337,10 @@ pub fn usb_ep3_bulk_out_complete( pub fn flush_tx(this: &mut CorigineUsb) { let mut written = 0; + while !crate::platform::usb::TX_IDLE.swap(false, core::sync::atomic::Ordering::SeqCst) { + // wait for tx to go idle + } + let tx_buf = this.cdc_acm_tx_slice(); critical_section::with(|cs| { let mut queue = crate::USB_TX.borrow(cs).borrow_mut(); @@ -357,6 +363,9 @@ pub fn flush_tx(this: &mut CorigineUsb) { if written > 0 { this.bulk_xfer(3, USB_SEND, tx_buf.as_ptr() as usize, written, 0, 0); + } else { + // release the lock + TX_IDLE.store(true, Ordering::SeqCst); } } @@ -371,6 +380,11 @@ pub fn usb_ep3_bulk_in_complete( // crate::println!("EP3 IN"); // let length = CRG_UDC_APP_BUF_LEN - residual as usize; // crate::println!("CDC IN transfer complete, {} bytes sent", length); + + // signal that more stuff can be put into the pipe + TX_IDLE.store(true, Ordering::SeqCst); + + // this may or may not initiate a new connection, depending on how full the Tx buffer is flush_tx(this); } diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 773e38059..09c30818c 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -11,6 +11,9 @@ use utralib::*; #[cfg(any(feature = "artybio", feature = "nto-bio"))] use xous_bio_bdma::*; +#[cfg(feature = "nto-trng")] +static TRNG_INIT: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); + pub struct Error { pub message: Option<&'static str>, } @@ -781,6 +784,74 @@ impl Repl { crate::platform::clockset_wrapper(800_000_000); crate::println!("exiting wfi"); } + #[cfg(feature = "nto-trng")] + "trngro" => { + use base64::{Engine as _, engine::general_purpose}; + fn encode_base64(input: &[u8]) -> String { general_purpose::STANDARD.encode(input) } + fn as_u8_slice(slice: &[u32]) -> &[u8] { + let len = slice.len() * size_of::(); + unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, len) } + } + + let mut trng = cramium_hal::sce::trng::Trng::new(utralib::utra::trng::HW_TRNG_BASE); + // local TRNG generator test + if TRNG_INIT.swap(true, core::sync::atomic::Ordering::SeqCst) == false { + crate::println!("setting up TRNG"); + trng.setup_raw_generation(256); + trng.start(); + } else { + // safety: the handle is already initialized + unsafe { + trng.force_mode(cramium_hal::sce::trng::Mode::Raw); + } + } + crate::println!("====ROSTART===="); + const BUFLEN: usize = 256; + // test code for checking performance & alignment of decode data - eliminates + // encoding & trng generation overhead + // let b64_as_slice = + // b"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/ + // wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="; + + loop { + let mut buf = [0u32; BUFLEN / size_of::()]; + for d in buf.iter_mut() { + loop { + if let Some(word) = trng.get_u32() { + *d = word; + break; + } + } + } + let buf_u8 = as_u8_slice(&buf); + let b64 = encode_base64(buf_u8); + unsafe { + if let Some(ref mut usb_ref) = crate::platform::usb::USB { + let usb = &mut *core::ptr::addr_of_mut!(*usb_ref); + let tx_buf = usb.cdc_acm_tx_slice(); + let b64_as_slice = b64.as_bytes(); + assert!(b64_as_slice.len() + 1 < tx_buf.len()); // this is ensured by adjusting BUFLEN + tx_buf[..b64_as_slice.len()].copy_from_slice(b64_as_slice); + tx_buf[b64_as_slice.len()] = '\n' as char as u32 as u8; + while !crate::platform::usb::TX_IDLE + .swap(false, core::sync::atomic::Ordering::SeqCst) + { + // wait for tx to go idle + } + usb.bulk_xfer( + 3, + cramium_hal::usb::driver::USB_SEND, + tx_buf.as_ptr() as usize, + b64_as_slice.len() + 1, + 0, + 0, + ); + } else { + panic!("USB core not allocated, can't proceed!"); + } + } + } + } "echo" => { for word in args { crate::print!("{} ", word); @@ -791,13 +862,17 @@ impl Repl { crate::println!("Command not recognized: {}", cmd); crate::print!("Commands include: echo, poke, peek, bogomips"); #[cfg(feature = "cramium-soc")] - crate::print!(", rram, clocks, usb"); + crate::print!(", rram, clocks"); #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); #[cfg(feature = "nto-bio")] crate::print!(", bio, bdma, pin"); #[cfg(all(feature = "cramium-soc", not(feature = "nto-evb")))] crate::print!(", ldo, wfi"); + #[cfg(feature = "nto-usb")] + crate::print!(", usb"); + #[cfg(feature = "nto-trng")] + crate::print!(", trngro"); crate::println!(""); } }