mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +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_ssoft();
|
||||||
sie::set_sext();
|
sie::set_sext();
|
||||||
}
|
}
|
||||||
|
rand::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Put the core to sleep until an interrupt hits. Returns `true`
|
/// 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 {
|
pub const TRNG_KERNEL: Trng = Trng {
|
||||||
lfsr ^= lfsr >> 7;
|
// the HW device mapping is done in xous-rs/src/lib.rs/init()
|
||||||
lfsr ^= lfsr << 9;
|
// the manually chosen virtuall address has to be in the top 4MiB as it is the only page shared among all processes
|
||||||
lfsr ^= lfsr >> 13;
|
base: 0xffce_0000 as *mut usize,
|
||||||
lfsr
|
};
|
||||||
|
|
||||||
|
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 {
|
pub fn get_u32() -> u32 {
|
||||||
// The kernel is currently single-threaded, so this is a valid operation.
|
let trng_kernel_csr = CSR::new(TRNG_KERNEL.base as *mut u32);
|
||||||
unsafe {
|
|
||||||
LFSR = move_lfsr(LFSR);
|
while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {}
|
||||||
LFSR
|
|
||||||
}
|
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);
|
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.
|
/// Loop through the SystemServices list to determine the next PID to be run.
|
||||||
|
|
|
||||||
|
|
@ -360,7 +360,7 @@ impl Server {
|
||||||
.queue
|
.queue
|
||||||
.get_mut(idx)
|
.get_mut(idx)
|
||||||
.ok_or(xous_kernel::Error::BadAddress)?;
|
.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 {
|
let (pid, tid, server_addr, client_addr, len, forget, is_memory) = match *current_val {
|
||||||
QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => {
|
QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => {
|
||||||
(pid, tid, server_addr, client_addr, len, false, true)
|
(pid, tid, server_addr, client_addr, len, false, true)
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,7 @@ fn shell_main() -> ! {
|
||||||
)
|
)
|
||||||
.expect("couldn't map GPIO CSR range");
|
.expect("couldn't map GPIO CSR range");
|
||||||
let mut gpio = CSR::new(gpio_base.as_mut_ptr() as *mut u32);
|
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);
|
let style_dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue