mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
first block retrieval from swap in userspace now decrypting
that's a good, solid step forward. now need to figure out why renode crashes after this starts working (o_O);;
This commit is contained in:
parent
57bbc4b2e5
commit
100e27db42
3 changed files with 41 additions and 80 deletions
|
|
@ -1,14 +1,12 @@
|
||||||
|
mod debug;
|
||||||
mod platform;
|
mod platform;
|
||||||
|
use core::fmt::Write;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use core::fmt::{Error, Write};
|
use debug::*;
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
|
||||||
|
|
||||||
use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR};
|
use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR};
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use platform::SwapHal;
|
use platform::{SwapHal, PAGE_SIZE};
|
||||||
use utralib::*;
|
|
||||||
|
|
||||||
static DUART_CSR_PA: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
|
|
||||||
pub struct PtPage {
|
pub struct PtPage {
|
||||||
pub entries: [u32; 1024],
|
pub entries: [u32; 1024],
|
||||||
|
|
@ -36,43 +34,6 @@ pub enum Opcode {
|
||||||
EvalTrim = 256,
|
EvalTrim = 256,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DebugUart {}
|
|
||||||
impl DebugUart {
|
|
||||||
#[cfg(feature = "debug-print")]
|
|
||||||
pub fn putc(&mut self, c: u8) {
|
|
||||||
let mut csr_ptr = DUART_CSR_PA.load(Ordering::SeqCst);
|
|
||||||
// map it if it doesn't exist
|
|
||||||
if csr_ptr == 0 {
|
|
||||||
let debug_uart_mem = xous::syscall::map_memory(
|
|
||||||
xous::MemoryAddress::new(utra::app_uart::HW_APP_UART_BASE),
|
|
||||||
None,
|
|
||||||
4096,
|
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
||||||
)
|
|
||||||
.expect("couldn't claim the debug UART");
|
|
||||||
DUART_CSR_PA.store(csr_ptr, Ordering::SeqCst);
|
|
||||||
csr_ptr = debug_uart_mem.as_ptr() as usize;
|
|
||||||
}
|
|
||||||
let mut csr = CSR::new(csr_ptr as *mut u32);
|
|
||||||
|
|
||||||
// Wait until TXFULL is `0`
|
|
||||||
while csr.r(utra::app_uart::TXFULL) != 0 {}
|
|
||||||
csr.wfo(utra::app_uart::RXTX_RXTX, c as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "debug-print"))]
|
|
||||||
pub fn putc(&self, _c: u8) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Write for DebugUart {
|
|
||||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
|
||||||
for c in s.bytes() {
|
|
||||||
self.putc(c);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This structure contains shared state accessible between the userspace code and the blocking swap call
|
/// This structure contains shared state accessible between the userspace code and the blocking swap call
|
||||||
/// handler.
|
/// handler.
|
||||||
struct SwapperSharedState {
|
struct SwapperSharedState {
|
||||||
|
|
@ -187,7 +148,9 @@ fn swap_handler(
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
// walk the PT to find the swap data
|
// walk the PT to find the swap data
|
||||||
let paddr_in_swap = ss.pt_walk(pid as u8, vaddr_in_pid);
|
let paddr_in_swap = ss
|
||||||
|
.pt_walk(pid as u8, vaddr_in_pid)
|
||||||
|
.expect("Couldn't resolve swapped data. Was the page actually swapped?");
|
||||||
writeln!(
|
writeln!(
|
||||||
DebugUart {},
|
DebugUart {},
|
||||||
"resolved address in swap for 0x{:x}:{} -> 0x{:x?}",
|
"resolved address in swap for 0x{:x}:{} -> 0x{:x?}",
|
||||||
|
|
@ -196,6 +159,14 @@ fn swap_handler(
|
||||||
paddr_in_swap
|
paddr_in_swap
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
|
// safety: this is only safe because the pointer we're passed from the kernel is guaranteed to be
|
||||||
|
// a valid u8-page in memory
|
||||||
|
let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) };
|
||||||
|
// TODO: manage swap_count
|
||||||
|
ss.hal
|
||||||
|
.decrypt_swap_from(buf, 0, paddr_in_swap, vaddr_in_pid, pid)
|
||||||
|
.expect("Decryption error: swap image corrupted, the tag does not match the data!");
|
||||||
|
// at this point, the `buf` has our desired data, we're done, modulo updating the count.
|
||||||
}
|
}
|
||||||
Some(Opcode::AllocateAdvisory) => {
|
Some(Opcode::AllocateAdvisory) => {
|
||||||
let advisories = [
|
let advisories = [
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,26 @@
|
||||||
|
use core::fmt::Write;
|
||||||
use core::mem::size_of;
|
use core::mem::size_of;
|
||||||
|
|
||||||
use aes_gcm_siv::{aead::Aead, AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag};
|
use aes_gcm_siv::{aead::Aead, AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag};
|
||||||
use cramium_hal::udma::*;
|
use cramium_hal::udma::*;
|
||||||
use loader::swap::{SwapSpec, SPIM_RAM_IFRAM_ADDR};
|
use loader::swap::{SwapSpec, SPIM_RAM_IFRAM_ADDR, SWAP_HAL_VADDR};
|
||||||
use num_traits::ToBytes;
|
use num_traits::ToBytes;
|
||||||
use xous::MemoryRange;
|
use xous::MemoryRange;
|
||||||
|
|
||||||
|
use crate::debug::*;
|
||||||
|
|
||||||
pub const PAGE_SIZE: usize = 4096;
|
pub const PAGE_SIZE: usize = 4096;
|
||||||
|
|
||||||
/// This is an implementation for SMTs that are accessible only through a SPI
|
/// This is an implementation for SMTs that are accessible only through a SPI
|
||||||
/// register interface. The base and bounds must be translated to SPI accesses
|
/// register interface. The base and bounds must be translated to SPI accesses
|
||||||
/// in a hardware-specific manner.
|
/// in a hardware-specific manner.
|
||||||
pub struct SwapHal {
|
pub struct SwapHal {
|
||||||
ifram_range: MemoryRange,
|
|
||||||
swap_mac_start: usize,
|
swap_mac_start: usize,
|
||||||
cipher: Aes256GcmSiv,
|
cipher: Aes256GcmSiv,
|
||||||
ram_spim: Spim,
|
ram_spim: Spim,
|
||||||
}
|
}
|
||||||
impl SwapHal {
|
impl SwapHal {
|
||||||
pub fn new(spec: &SwapSpec) -> Self {
|
pub fn new(spec: &SwapSpec) -> Self {
|
||||||
let ifram_range = xous::syscall::map_memory(
|
|
||||||
xous::MemoryAddress::new(SPIM_RAM_IFRAM_ADDR),
|
|
||||||
None,
|
|
||||||
PAGE_SIZE,
|
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
||||||
)
|
|
||||||
.expect("Couldn't map IFRAM area into swapper");
|
|
||||||
|
|
||||||
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
|
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
|
||||||
// because once we remove the MAC area, we need even less storage, but it's a small error.
|
// because once we remove the MAC area, we need even less storage, but it's a small error.
|
||||||
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
|
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
|
||||||
|
|
@ -38,11 +32,11 @@ impl SwapHal {
|
||||||
#[cfg(not(feature = "spi-alt-channel"))]
|
#[cfg(not(feature = "spi-alt-channel"))]
|
||||||
let channel = SpimChannel::Channel1;
|
let channel = SpimChannel::Channel1;
|
||||||
SwapMac {
|
SwapMac {
|
||||||
ifram_range,
|
|
||||||
swap_mac_start: ram_size_actual,
|
swap_mac_start: ram_size_actual,
|
||||||
cipher: Aes256GcmSiv::new((spec.key).into()),
|
cipher: Aes256GcmSiv::new((spec.key).into()),
|
||||||
// safety: this is safe because the global clocks were gated on by the bootloader
|
// safety: this is safe because the global clocks were gated on by the bootloader
|
||||||
// note that also the IFRAM0 range is pre-allocated by the bootloader
|
// note that also the IFRAM0 range is pre-allocated by the bootloader, and pre-mapped
|
||||||
|
// into the correct virtual address as well.
|
||||||
ram_spim: unsafe {
|
ram_spim: unsafe {
|
||||||
Spim::new_with_ifram(
|
Spim::new_with_ifram(
|
||||||
channel,
|
channel,
|
||||||
|
|
@ -57,7 +51,7 @@ impl SwapHal {
|
||||||
1024, // this is limited by the page length
|
1024, // this is limited by the page length
|
||||||
1024,
|
1024,
|
||||||
Some(6),
|
Some(6),
|
||||||
IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, ifram_range.as_ptr() as usize, PAGE_SIZE),
|
IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, SWAP_HAL_VADDR, PAGE_SIZE),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,35 @@
|
||||||
use core::mem::size_of;
|
use core::mem::size_of;
|
||||||
|
|
||||||
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag};
|
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, Error, KeyInit, Nonce, Tag};
|
||||||
use loader::swap::SwapSpec;
|
use loader::swap::{SwapSpec, SWAP_HAL_VADDR};
|
||||||
use xous::MemoryRange;
|
|
||||||
|
|
||||||
pub const PAGE_SIZE: usize = 4096;
|
pub const PAGE_SIZE: usize = 4096;
|
||||||
|
|
||||||
/// This defines a set of functions to get and receive MACs (message
|
/// This defines a set of functions to get and receive MACs (message
|
||||||
/// authentication codes, also referred to as the tag in AES-GCM-SIV.
|
/// authentication codes, also referred to as the tag in AES-GCM-SIV.
|
||||||
pub struct SwapHal {
|
pub struct SwapHal {
|
||||||
swap_mem: MemoryRange,
|
|
||||||
dst_data_area: &'static mut [u8],
|
dst_data_area: &'static mut [u8],
|
||||||
dst_mac_area: &'static mut [u8],
|
dst_mac_area: &'static mut [u8],
|
||||||
cipher: Aes256GcmSiv,
|
cipher: Aes256GcmSiv,
|
||||||
}
|
}
|
||||||
impl SwapHal {
|
impl SwapHal {
|
||||||
pub fn new(spec: &SwapSpec) -> Self {
|
pub fn new(spec: &SwapSpec) -> Self {
|
||||||
let swap_mem = xous::syscall::map_memory(
|
|
||||||
xous::MemoryAddress::new(spec.swap_base as usize),
|
|
||||||
None,
|
|
||||||
spec.swap_len as usize,
|
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
||||||
)
|
|
||||||
.expect("Couldn't map swap area into swapper");
|
|
||||||
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
|
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
|
||||||
// because once we remove the MAC area, we need even less storage, but it's a small error.
|
// because once we remove the MAC area, we need even less storage, but it's a small error.
|
||||||
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
|
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
|
||||||
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
|
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
|
||||||
let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
|
let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
|
||||||
|
|
||||||
// safety: this is safe because all values within the memory region can be represented in `u8`
|
|
||||||
let swap_slice = swap_mem.as_mut_ptr();
|
|
||||||
Self {
|
Self {
|
||||||
swap_mem,
|
// safety: the ram swap area is pre-mapped into our virtual address by the loader, and our
|
||||||
// safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our
|
|
||||||
// calculations on lengths ensure area alignment
|
// calculations on lengths ensure area alignment
|
||||||
dst_data_area: unsafe { core::slice::from_raw_parts_mut(swap_slice as *mut u8, ram_size_actual) },
|
dst_data_area: unsafe {
|
||||||
// safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our
|
core::slice::from_raw_parts_mut(SWAP_HAL_VADDR as *mut u8, ram_size_actual)
|
||||||
|
},
|
||||||
|
// safety: the ram swap area is pre-mapped into our virtual address by the loader, and our
|
||||||
// calculations on lengths ensure area alignment
|
// calculations on lengths ensure area alignment
|
||||||
dst_mac_area: unsafe {
|
dst_mac_area: unsafe {
|
||||||
core::slice::from_raw_parts_mut((swap_slice as *mut u8).add(ram_size_actual), mac_size)
|
core::slice::from_raw_parts_mut((SWAP_HAL_VADDR as *mut u8).add(ram_size_actual), mac_size)
|
||||||
},
|
},
|
||||||
cipher: Aes256GcmSiv::new((&spec.key).into()),
|
cipher: Aes256GcmSiv::new((&spec.key).into()),
|
||||||
}
|
}
|
||||||
|
|
@ -87,8 +77,11 @@ impl SwapHal {
|
||||||
dst_vaddr: usize,
|
dst_vaddr: usize,
|
||||||
dst_pid: u8,
|
dst_pid: u8,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// println!("Decrypt swap:");
|
// use core::fmt::Write;
|
||||||
// println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid);
|
// use crate::debug::*;
|
||||||
|
// writeln!(DebugUart {}, "Decrypt swap:").ok();
|
||||||
|
// writeln!(DebugUart {}, " offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr,
|
||||||
|
// dst_pid).ok();
|
||||||
assert!(src_offset & (PAGE_SIZE - 1) == 0);
|
assert!(src_offset & (PAGE_SIZE - 1) == 0);
|
||||||
assert!(buf.len() == PAGE_SIZE);
|
assert!(buf.len() == PAGE_SIZE);
|
||||||
|
|
||||||
|
|
@ -103,9 +96,12 @@ impl SwapHal {
|
||||||
let mut tag = [0u8; size_of::<Tag>()];
|
let mut tag = [0u8; size_of::<Tag>()];
|
||||||
let mac_offset = (src_offset / PAGE_SIZE) * size_of::<Tag>();
|
let mac_offset = (src_offset / PAGE_SIZE) * size_of::<Tag>();
|
||||||
tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::<Tag>()]);
|
tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::<Tag>()]);
|
||||||
// println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]);
|
// writeln!(DebugUart {}, "dst_mac_area: {:x?}", &self.dst_mac_area[..32]).ok();
|
||||||
buf.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]);
|
buf.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]);
|
||||||
// println!("Nonce: {:x?}, tag: {:x?}", &nonce, &tag);
|
// writeln!(DebugUart {}, "Nonce: {:x?}, tag: {:x?}", &nonce, &tag).ok();
|
||||||
self.cipher.decrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf, (&tag).into())
|
let result =
|
||||||
|
self.cipher.decrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf, (&tag).into());
|
||||||
|
// writeln!(DebugUart {}, "result: {:?}, buf: {:x?}", result, &buf[..16]).ok();
|
||||||
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue