mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add hardware TRNG support to kernel (issue #16)
This commit is contained in:
parent
09f3390524
commit
c9a18125df
5 changed files with 53 additions and 13 deletions
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue