From c9a18125df287ea7bb10bdafc345a5ce61d2f4a9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 14 Dec 2020 17:57:29 +0800 Subject: [PATCH] add hardware TRNG support to kernel (issue #16) --- kernel/src/arch/riscv.rs | 1 + kernel/src/arch/riscv/rand.rs | 56 ++++++++++++++++++++++++++++------- kernel/src/main.rs | 5 ++++ kernel/src/server.rs | 2 +- services/shell/src/main.rs | 2 +- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/kernel/src/arch/riscv.rs b/kernel/src/arch/riscv.rs index ebbcdc08e..caca4029a 100644 --- a/kernel/src/arch/riscv.rs +++ b/kernel/src/arch/riscv.rs @@ -20,6 +20,7 @@ pub fn init() { sie::set_ssoft(); sie::set_sext(); } + rand::init(); } /// Put the core to sleep until an interrupt hits. Returns `true` diff --git a/kernel/src/arch/riscv/rand.rs b/kernel/src/arch/riscv/rand.rs index 17be78d44..cc64363bb 100644 --- a/kernel/src/arch/riscv/rand.rs +++ b/kernel/src/arch/riscv/rand.rs @@ -1,16 +1,50 @@ -static mut LFSR: u32 = 0xace1u32; +use utralib::generated::*; +use crate::mem::MemoryManager; +use xous_kernel::{MemoryFlags, MemoryType, PID}; -fn move_lfsr(mut lfsr: u32) -> u32 { - lfsr ^= lfsr >> 7; - lfsr ^= lfsr << 9; - lfsr ^= lfsr >> 13; - lfsr +pub const TRNG_KERNEL: Trng = Trng { + // the HW device mapping is done in xous-rs/src/lib.rs/init() + // the manually chosen virtuall address has to be in the top 4MiB as it is the only page shared among all processes + base: 0xffce_0000 as *mut usize, +}; + +pub struct Trng { + pub base: *mut usize, +} + +pub fn init() { + // Map the TRNG so that we can allocate names + // hardware guarantees that: + // - TRNG will automatically power on + // - Both TRNGs are enabled, with conservative defaults + // - Kernel FIFO will fill with TRNGs such that at least the next 512 calls to get_u32() will succeed without delay + // - The kernel will start a TRNG server + // - All further security decisions and policies are 100% delegated to this new server. + MemoryManager::with_mut(|memory_manager| { + memory_manager + .map_range( + utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u8, + ((TRNG_KERNEL.base as u32) & !4095) as *mut u8, + 4096, + PID::new(1).unwrap(), + MemoryFlags::R | MemoryFlags::W, + MemoryType::Default, + ) + .expect("unable to map TRNG") + }); + + 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); } pub fn get_u32() -> u32 { - // The kernel is currently single-threaded, so this is a valid operation. - unsafe { - LFSR = move_lfsr(LFSR); - LFSR - } + 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) } diff --git a/kernel/src/main.rs b/kernel/src/main.rs index de0fd4c07..e811db7c1 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -87,6 +87,11 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs println!(" {}", arg); } } + + // pump the random number generator to test it + for _ in 0..100 { + arch::rand::get_u32(); + } } /// Loop through the SystemServices list to determine the next PID to be run. diff --git a/kernel/src/server.rs b/kernel/src/server.rs index 3d64924ce..adf60cd3c 100644 --- a/kernel/src/server.rs +++ b/kernel/src/server.rs @@ -360,7 +360,7 @@ impl Server { .queue .get_mut(idx) .ok_or(xous_kernel::Error::BadAddress)?; - klog!("memory in queue[{}]: {:?}", idx, val); + // klog!("memory in queue[{}]: {:?}", idx, val); let (pid, tid, server_addr, client_addr, len, forget, is_memory) = match *current_val { QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => { (pid, tid, server_addr, client_addr, len, false, true) diff --git a/services/shell/src/main.rs b/services/shell/src/main.rs index d3a2b303b..8b3edd167 100644 --- a/services/shell/src/main.rs +++ b/services/shell/src/main.rs @@ -185,7 +185,7 @@ fn shell_main() -> ! { ) .expect("couldn't map GPIO CSR range"); let mut gpio = CSR::new(gpio_base.as_mut_ptr() as *mut u32); - gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // 0 = kernel, 1 = log, 2-3 are various servers + gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 0); // 0 = kernel, 1 = log } let style_dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);