mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
hosted mode simulation; basic disk formatting doesn't crash.
- hosted mode uses an awful, horrific hack to simulate FLASH: it makes a global singleton that is freely mutable. Oh but wait! that's actually how the hardware behaves. It's up to the code around the hardware to create those primitives. So, maybe it makes sense that the simulation model for the hardware is terribly unsafe and unprotected. - the format routine at least doesn't crash and creates some semblance of a result that looks right-ish. Need to write a utility that helps to explore the "disk image" so we can understand what's going on.
This commit is contained in:
parent
4b33069ac6
commit
f5a5211851
11 changed files with 254 additions and 72 deletions
|
|
@ -1,12 +1,18 @@
|
|||
use rkyv::{Archive, Deserialize, Serialize};
|
||||
|
||||
pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_MAX_BASIS_NAME_LEN: usize = 64;
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_MAX_DICT_NAME_LEN: usize = 64;
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_MAX_KEY_NAME_LEN: usize = 128;
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42];
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_VERSION: u16 = 0x00_00;
|
||||
/// PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const PDDB_A_LEN: usize = 16 * 1024 * 1024;
|
||||
|
||||
/// A number between (0, 1] that defines how many of the "truly free" pages we
|
||||
|
|
@ -79,6 +85,12 @@ pub(crate) struct PddbBuf {
|
|||
pub(crate) reserved: u8,
|
||||
pub(crate) data: [u8; 4080],
|
||||
}
|
||||
impl PddbBuf {
|
||||
pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf {
|
||||
// this transforms the slice [u8] into a PddbBuf ref.
|
||||
unsafe {core::mem::transmute::<*mut u8, &mut PddbBuf>(slice.as_mut_ptr()) }
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -90,9 +102,3 @@ mod tests {
|
|||
assert!(PDDB_A_LEN <= xous::PDDB_LEN as usize, "PDDB_A_LEN is larger than the maximum extents available in the hardware");
|
||||
}
|
||||
}
|
||||
impl PddbBuf {
|
||||
pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf {
|
||||
// this transforms the slice [u8] into a PddbBuf ref.
|
||||
unsafe {core::mem::transmute::<*mut u8, &mut PddbBuf>(slice.as_mut_ptr()) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,9 +14,11 @@ pub(crate) use murmur3::*;
|
|||
mod trngpool;
|
||||
pub(crate) use trngpool::*;
|
||||
|
||||
//#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
mod hw;
|
||||
pub(crate) use hw::*;
|
||||
|
||||
|
||||
// TODO: the alternative back-end PddbOs structures for hosted mode.
|
||||
// hosted mode emulation structures
|
||||
//#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
mod hosted;
|
||||
//#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
pub(crate) use hosted::*;
|
||||
|
|
|
|||
|
|
@ -64,7 +64,6 @@ impl<'a> IntoIterator for BasisEncryptor<'a> {
|
|||
type Item=[u8; PAGE_SIZE];
|
||||
type IntoIter=BasisEncryptorIter<'a>;
|
||||
fn into_iter(self) -> BasisEncryptorIter<'a> {
|
||||
let journal_bytes = self.journal_rev.to_le_bytes();
|
||||
BasisEncryptorIter {
|
||||
basis_data: self,
|
||||
vaddr: 0,
|
||||
|
|
@ -76,11 +75,11 @@ impl<'a> Iterator for BasisEncryptorIter<'a> {
|
|||
|
||||
fn next<'s>(&'s mut self) -> Option<Self::Item> {
|
||||
if self.vaddr < self.basis_data.root.prealloc_open_end.as_usize() {
|
||||
let mut block = [0 as u8; VPAGE_SIZE];
|
||||
let mut block_iter = block.iter_mut();
|
||||
let mut block = [0 as u8; VPAGE_SIZE + size_of::<JournalType>()];
|
||||
let block_iter = block.iter_mut();
|
||||
|
||||
let journal_bytes = self.basis_data.journal_rev.to_le_bytes();
|
||||
let mut slice_iter =
|
||||
let slice_iter =
|
||||
journal_bytes.iter() // journal rev
|
||||
.chain(self.basis_data.root.deref().iter() // basis
|
||||
// .chain(self.dicts.as_slice() // dictionary
|
||||
|
|
@ -108,6 +107,7 @@ impl<'a> Iterator for BasisEncryptorIter<'a> {
|
|||
}
|
||||
).unwrap();
|
||||
self.vaddr += VPAGE_SIZE;
|
||||
//log::info!("nonce: {} ct: {} total: {}", nonce_array.len(), ciphertext.deref().len(), nonce_array.len() + ciphertext.deref().len());
|
||||
Some([&nonce_array, ciphertext.deref()].concat().try_into().unwrap())
|
||||
} else {
|
||||
None
|
||||
|
|
@ -145,6 +145,7 @@ pub(crate) struct BasisRoot {
|
|||
// pad: [u8], // padding out to the next 4096-byte block less 16 bytes
|
||||
// p_tag: [u8; 16], // auth tag output of the AES-GCM-SIV
|
||||
}
|
||||
/* // this computation got supplanted by prealloc_open_end -- basis is always laid out linearly in RAM, so we just allocate a straight section of RAM based off of that.
|
||||
impl BasisRoot {
|
||||
/// Compute the number of memory pages consumed by the BasisRoot structure itself.
|
||||
/// This is the size of BasisRoot, plus the dictionaries allocated within the Basis.
|
||||
|
|
@ -170,7 +171,7 @@ impl BasisRoot {
|
|||
+ ((self.num_dictionaries as usize) * core::mem::size_of::<DictPointer>())
|
||||
)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
impl Deref for BasisRoot {
|
||||
type Target = [u8];
|
||||
fn deref(&self) -> &[u8] {
|
||||
|
|
@ -198,12 +199,13 @@ pub(crate) struct DictPointer {
|
|||
addr: u64, // the virtual address of the dictionary
|
||||
}
|
||||
|
||||
/* this is supplanted by the FSCB
|
||||
/// FreeSpace address space is in the virtual memory space of the containing Basis
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) struct FreeSpace {
|
||||
start: u64,
|
||||
len: NonZeroU64,
|
||||
}
|
||||
}*/
|
||||
|
||||
/// Typically individual dictionaries start out life having their own 4k-page, but they
|
||||
/// can be compacted together if they seem to be static/non-changing and we need more space.
|
||||
|
|
|
|||
67
services/pddb/src/backend/hosted.rs
Normal file
67
services/pddb/src/backend/hosted.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#![allow(dead_code)]
|
||||
use crate::api::*;
|
||||
|
||||
use std::sync::Once;
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
// This is considered bad practice for Rust to use a global singleton.
|
||||
// However, this hack puts the burden of emulation on the emulator, while
|
||||
// keeping the production code clean (otherwise we'd have large sections
|
||||
// of production code with #cfg regions to deal with lifetime differences).
|
||||
// Besides, in reality, FLASH memory is a static, globally mutable pool of data.
|
||||
//
|
||||
// Note that this is a concurrently accessed, unsafe, unchecked vector.
|
||||
struct FlashSingleton {
|
||||
memory: Vec::<u8>,
|
||||
}
|
||||
|
||||
fn flashmem() -> &'static mut FlashSingleton {
|
||||
static mut SINGLETON: MaybeUninit<FlashSingleton> = MaybeUninit::uninit();
|
||||
static ONCE: Once = Once::new();
|
||||
|
||||
unsafe {
|
||||
ONCE.call_once(|| {
|
||||
let mut memory = Vec::<u8>::with_capacity(PDDB_A_LEN);
|
||||
for _ in 0..PDDB_A_LEN {
|
||||
memory.push(0xFF);
|
||||
}
|
||||
let flashmem = FlashSingleton {
|
||||
memory,
|
||||
};
|
||||
SINGLETON.write(flashmem);
|
||||
});
|
||||
SINGLETON.assume_init_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EmuStorage {
|
||||
}
|
||||
impl EmuStorage {
|
||||
pub fn new() -> Self {
|
||||
EmuStorage {
|
||||
}
|
||||
}
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
flashmem().memory.as_slice()
|
||||
}
|
||||
pub fn as_mut_slice(&mut self) -> &mut [u8] {
|
||||
flashmem().memory.as_mut_slice()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HostedSpinor {
|
||||
}
|
||||
impl HostedSpinor {
|
||||
pub fn new() -> Self {
|
||||
HostedSpinor {
|
||||
}
|
||||
}
|
||||
pub fn patch(&self, _region: &[u8], _region_base: u32, data: &[u8], offset: u32) -> Result<(), xous::Error> {
|
||||
for (&src, dst) in data.iter().zip(
|
||||
flashmem().memory.as_mut_slice()[offset as usize..offset as usize + data.len()].iter_mut()
|
||||
) {
|
||||
*dst = src;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,11 @@ use std::convert::TryInto;
|
|||
use crate::*;
|
||||
use aes_gcm_siv::{Aes256GcmSiv, Nonce, Key, Tag};
|
||||
use aes_gcm_siv::aead::{Aead, NewAead, Payload};
|
||||
use aes::Aes256;
|
||||
use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray};
|
||||
use root_keys::api::{AesRootkeyType, Block};
|
||||
use aes::{Aes256, Block};
|
||||
use aes::cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray};
|
||||
use root_keys::api::AesRootkeyType;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::mem::size_of;
|
||||
use core::convert::TryFrom;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
|
@ -46,10 +45,22 @@ impl Deref for StaticCryptoData {
|
|||
}
|
||||
}
|
||||
|
||||
// emulated
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
type EmuMemoryRange = EmuStorage;
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
type EmuSpinor = HostedSpinor;
|
||||
|
||||
// native hardware
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
type EmuMemoryRange = xous::MemoryRange;
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
type EmuSpinor = spinor::Spinor;
|
||||
|
||||
pub(crate) struct PddbOs {
|
||||
spinor: spinor::Spinor,
|
||||
spinor: EmuSpinor,
|
||||
rootkeys: root_keys::RootKeys,
|
||||
pddb_mr: xous::MemoryRange,
|
||||
pddb_mr: EmuMemoryRange,
|
||||
/// page table base -- location in FLASH, offset from physical bottom of pddb_mr
|
||||
pt_phys_base: PageAlignedPa,
|
||||
/// local key store -- one page, to store exactly one key, used for the system basis.
|
||||
|
|
@ -78,6 +89,7 @@ pub(crate) struct PddbOs {
|
|||
impl PddbOs {
|
||||
pub fn new(trngpool: Rc<RefCell<TrngPool>>) -> PddbOs {
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
let pddb = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(xous::PDDB_LOC as usize),
|
||||
None,
|
||||
|
|
@ -92,7 +104,9 @@ impl PddbOs {
|
|||
let fscb_phys_base = PageAlignedPa::from(mbbb_phys_base.as_u32() + MBBB_PAGES as u32 * PAGE_SIZE as u32);
|
||||
|
||||
let llio = llio::Llio::new(&xns).unwrap();
|
||||
PddbOs {
|
||||
// native hardware
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
let ret = PddbOs {
|
||||
spinor: spinor::Spinor::new(&xns).unwrap(),
|
||||
rootkeys: root_keys::RootKeys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
|
||||
pddb_mr: pddb,
|
||||
|
|
@ -108,12 +122,35 @@ impl PddbOs {
|
|||
fspace_log_next_addr: None,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
entropy: trngpool,
|
||||
}
|
||||
};
|
||||
// emulated
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
let ret = {
|
||||
PddbOs {
|
||||
spinor: HostedSpinor::new(),
|
||||
rootkeys: root_keys::RootKeys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
|
||||
pddb_mr: EmuStorage::new(),
|
||||
pt_phys_base: PageAlignedPa::from(0 as u32),
|
||||
key_phys_base,
|
||||
mbbb_phys_base,
|
||||
fscb_phys_base,
|
||||
data_phys_base: PageAlignedPa::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32),
|
||||
system_basis_key: None,
|
||||
v2p_map: HashMap::<BasisRootName, HashMap<VirtAddr, PhysPage>>::new(),
|
||||
fspace_cache: HashSet::<PhysPage>::new(),
|
||||
fspace_log_addrs: Vec::<PageAlignedPa>::new(),
|
||||
fspace_log_next_addr: None,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
entropy: trngpool,
|
||||
}
|
||||
};
|
||||
ret
|
||||
}
|
||||
|
||||
/// patches data at an offset starting from the data physical base address, which corresponds
|
||||
/// exactly to the first entry in the page table
|
||||
fn patch_data(&self, data: &[u8], offset: u32) {
|
||||
log::info!("patch offset: {:x} len: {:x}", offset, data.len());
|
||||
assert!(data.len() + offset as usize <= PDDB_A_LEN - self.data_phys_base.as_usize(), "attempt to store past disk boundary");
|
||||
self.spinor.patch(
|
||||
self.pddb_mr.as_slice(),
|
||||
|
|
@ -128,7 +165,7 @@ impl PddbOs {
|
|||
self.pddb_mr.as_slice(),
|
||||
xous::PDDB_LOC,
|
||||
&data,
|
||||
offset,
|
||||
self.pt_phys_base.as_u32() + offset,
|
||||
).expect("couldn't write to page table");
|
||||
}
|
||||
fn patch_keys(&self, data: &[u8], offset: u32) {
|
||||
|
|
@ -169,6 +206,7 @@ impl PddbOs {
|
|||
}
|
||||
/// takes the key and writes it with zero, using hard pointer math and a compiler fence to ensure
|
||||
/// the wipe isn't optimized out.
|
||||
#[allow(dead_code)]
|
||||
fn syskey_erase(&mut self) {
|
||||
if let Some(mut key) = self.system_basis_key.take() {
|
||||
let b = key.as_mut_ptr();
|
||||
|
|
@ -187,7 +225,9 @@ impl PddbOs {
|
|||
for (&src, dst) in scd.system_key.iter().zip(system_key.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
self.rootkeys.decrypt_block(Block::from_mut_slice(&mut system_key));
|
||||
for block in system_key.chunks_mut(aes::BLOCK_SIZE) {
|
||||
self.rootkeys.decrypt_block(block.try_into().unwrap());
|
||||
}
|
||||
self.system_basis_key = Some(system_key);
|
||||
}
|
||||
}
|
||||
|
|
@ -293,7 +333,7 @@ impl PddbOs {
|
|||
// addresses.
|
||||
let mut min_used_page = page_heap.pop();
|
||||
// consider every page
|
||||
for page_candidate in 0..PDDB_A_LEN / PAGE_SIZE {
|
||||
for page_candidate in 0..(PDDB_A_LEN - self.data_phys_base.as_usize()) / PAGE_SIZE {
|
||||
// if the page is used, skip it.
|
||||
if let Some(Reverse(mp)) = min_used_page {
|
||||
if page_candidate == mp as usize {
|
||||
|
|
@ -390,16 +430,16 @@ impl PddbOs {
|
|||
} else {
|
||||
// this page (and the ones immediately afterward) "should" contain the FastSpace encrypted record
|
||||
if fscb_pages == 0 {
|
||||
let mut fscb_buf = [0; FSCB_PAGES * PAGE_SIZE - size_of::<Nonce>()];
|
||||
let mut fscb_buf = [0; FASTSPACE_PAGES * PAGE_SIZE - size_of::<Nonce>()];
|
||||
// copy the encrypted data to the decryption buffer
|
||||
for (&src, dst) in
|
||||
fscb_slice[page_start + size_of::<Nonce>() .. page_start + size_of::<Nonce>() + FSCB_PAGES * PAGE_SIZE]
|
||||
fscb_slice[page_start + size_of::<Nonce>() .. page_start + FASTSPACE_PAGES * PAGE_SIZE]
|
||||
.iter().zip(fscb_buf.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
let mut aad = Vec::<u8>::new();
|
||||
self.fast_space_aad(&mut aad);
|
||||
let mut payload = Payload {
|
||||
let payload = Payload {
|
||||
msg: &fscb_buf,
|
||||
aad: &aad,
|
||||
};
|
||||
|
|
@ -407,6 +447,8 @@ impl PddbOs {
|
|||
let cipher = Aes256GcmSiv::new(key);
|
||||
match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::<Nonce>()]), payload) {
|
||||
Ok(msg) => {
|
||||
log::info!("decrypted: {}, FastSpace size: {}", msg.len(), size_of::<FastSpace>());
|
||||
assert!(msg.len() == size_of::<FastSpace>());
|
||||
// payload now contains the decrypted data, and the "msg" return field is truncated to the right length
|
||||
// we now map the FastSpace structure onto the payload using an unsafe operation.
|
||||
let fs_ptr = (&msg).as_ptr() as *const FastSpace;
|
||||
|
|
@ -426,7 +468,6 @@ impl PddbOs {
|
|||
fscb_pages += 1;
|
||||
}
|
||||
}
|
||||
assert!(fscb_pages == size_of::<FastSpace>());
|
||||
|
||||
// 2. visit the update_page_addrs and modify the fspace_cache accordingly.
|
||||
let cipher = Aes256::new(GenericArray::from_slice(&system_key));
|
||||
|
|
@ -567,7 +608,7 @@ impl PddbOs {
|
|||
let cipher = Aes256::new(GenericArray::from_slice(&system_key));
|
||||
let mut update = SpaceUpdate::new(self.entropy.borrow_mut().get_u64(), ppc);
|
||||
let mut block = Block::from_mut_slice(update.deref_mut());
|
||||
cipher.encrypt_block(block);
|
||||
cipher.encrypt_block(&mut block);
|
||||
let log_addr = self.fspace_log_next_addr.take().unwrap() as PhysAddr;
|
||||
self.patch_fscb(&block, log_addr);
|
||||
let next_addr = log_addr + aes::BLOCK_SIZE as PhysAddr;
|
||||
|
|
@ -688,18 +729,18 @@ impl PddbOs {
|
|||
assert!(size_of::<StaticCryptoData>() == PAGE_SIZE, "StaticCryptoData structure is not correctly sized");
|
||||
let mut system_basis_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE];
|
||||
self.entropy.borrow_mut().get_slice(&mut system_basis_key);
|
||||
let mut basis_key_enc: [u8; AES_KEYSIZE] = system_basis_key.clone();
|
||||
let mut basis_key_aes: [u8; AES_KEYSIZE] = system_basis_key.clone();
|
||||
self.system_basis_key = Some(system_basis_key); // causes system_basis_key to be owned by self
|
||||
log::info!("sanity check: plaintext system basis key: {:x?}", basis_key_enc);
|
||||
self.rootkeys.encrypt_block(Block::from_mut_slice(&mut basis_key_enc));
|
||||
log::info!("sanity check: encrypted system basis key: {:x?}", basis_key_enc);
|
||||
for block in basis_key_aes.chunks_mut(aes::BLOCK_SIZE) {
|
||||
self.rootkeys.encrypt_block(block.try_into().unwrap());
|
||||
}
|
||||
let mut crypto_keys = StaticCryptoData {
|
||||
system_key: [0; AES_KEYSIZE],
|
||||
salt_base: [0; 2048],
|
||||
reserved: [0; 2016],
|
||||
};
|
||||
// copy the encrypted key into the data structure for commit to Flash
|
||||
for (&src, dst) in basis_key_enc.iter().zip(crypto_keys.system_key.iter_mut()) {
|
||||
for (&src, dst) in basis_key_aes.iter().zip(crypto_keys.system_key.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
self.entropy.borrow_mut().get_slice(&mut crypto_keys.salt_base);
|
||||
|
|
@ -742,7 +783,7 @@ impl PddbOs {
|
|||
for (&src, dst) in PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
let mut basis_root = BasisRoot {
|
||||
let basis_root = BasisRoot {
|
||||
magic: api::PDDB_MAGIC,
|
||||
version: api::PDDB_VERSION,
|
||||
name,
|
||||
|
|
@ -751,6 +792,8 @@ impl PddbOs {
|
|||
prealloc_open_end: PageAlignedVa::from(INITIAL_BASIS_ALLOC * VPAGE_SIZE),
|
||||
dict_ptr: None,
|
||||
};
|
||||
log::info!("prealloc_open_end: {:x?}", basis_root.prealloc_open_end);
|
||||
log::info!("prealloc_open_end as vpage: {:x?}", basis_root.prealloc_open_end.as_vpage_num());
|
||||
|
||||
// step 7. Create a hashmap for our reverse PTE, allocate sectors, and add it to the Pddb's cache
|
||||
self.fast_space_read(); // we reconstitute our fspace map even though it was just generated, partially as a sanity check that everything is ok
|
||||
|
|
@ -762,15 +805,20 @@ impl PddbOs {
|
|||
let mut rpte = alloc.clone();
|
||||
rpte.set_clean(true); // it's not clean _right now_ but it will be by the time this routine is done...
|
||||
rpte.set_valid(true);
|
||||
basis_v2p_map.insert(VirtAddr::new((virt_page * VPAGE_SIZE) as u64).unwrap(), rpte);
|
||||
let va = VirtAddr::new((virt_page * VPAGE_SIZE) as u64).unwrap();
|
||||
log::info!("adding basis va {:x?} with pte {:?}", va, rpte);
|
||||
basis_v2p_map.insert(va, rpte);
|
||||
}
|
||||
}
|
||||
//for (&k, &v) in basis_v2p_map.iter() {
|
||||
// log::info!("basis_v2p_map va: {:x?} pp: {:x?}", k, v);
|
||||
//}
|
||||
self.v2p_map.insert(basis_root.name, basis_v2p_map);
|
||||
}
|
||||
|
||||
// step 8. write the System basis to Flash, at the physical locations noted above
|
||||
if let Some(syskey) = self.system_basis_key {
|
||||
let key = Key::from_slice(&system_basis_key);
|
||||
let key = Key::from_slice(&syskey);
|
||||
let cipher = Aes256GcmSiv::new(key);
|
||||
let basis_encryptor = BasisEncryptor::new(
|
||||
&basis_root,
|
||||
|
|
@ -781,6 +829,9 @@ impl PddbOs {
|
|||
Rc::clone(&self.entropy),
|
||||
);
|
||||
if let Some(basis_v2p_map) = self.v2p_map.get(&basis_root.name) {
|
||||
for (&k, &v) in basis_v2p_map.iter() {
|
||||
log::info!("basis_v2p_map retrieved va: {:x?} pp: {:x?}", k, v);
|
||||
}
|
||||
for (vpage_no, ppage_data) in basis_encryptor.into_iter().enumerate() {
|
||||
let vaddr = VirtAddr::new( ((vpage_no + 1) * VPAGE_SIZE) as u64 ).unwrap();
|
||||
match basis_v2p_map.get(&vaddr) {
|
||||
|
|
@ -809,7 +860,7 @@ impl PddbOs {
|
|||
for (&virt, &phys) in basis_v2p_map.into_iter() {
|
||||
let mut pte = Pte::new(virt, PtFlags::CLEAN, Rc::clone(&self.entropy));
|
||||
let mut block = Block::from_mut_slice(pte.deref_mut());
|
||||
cipher.encrypt_block(block);
|
||||
cipher.encrypt_block(&mut block);
|
||||
self.patch_pagetable(&block, phys.page_number() * aes::BLOCK_SIZE as u32);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ impl Pte {
|
|||
nonce: nonce_u32.to_le_bytes(),
|
||||
checksum: [0; 4],
|
||||
};
|
||||
let mut pte_data = pte.deref();
|
||||
let pte_data = pte.deref();
|
||||
let checksum = murmur3_32(&pte_data[..12], nonce_u32);
|
||||
pte.checksum = checksum.to_le_bytes();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use core::mem::size_of;
|
||||
use aes_gcm_siv::Nonce;
|
||||
use rand_core::{CryptoRng, RngCore};
|
||||
use rand_core::RngCore;
|
||||
use std::convert::TryInto;
|
||||
|
||||
/// Crate-shared resource for TRNGs.
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ pub(crate) const PHYS_PAGE_JOURNAL_MAX: u8 = 15;
|
|||
pub type PhysAddr = u32;
|
||||
#[cfg(feature = "u64_pa")]
|
||||
pub type PhysAddr = u64;
|
||||
const BITFIELD_PAGE_WIDTH: usize = (core::mem::size_of::<PhysAddr>() * 8 - 12);
|
||||
const BITFIELD_PAGE_WIDTH: usize = core::mem::size_of::<PhysAddr>() * 8 - 12;
|
||||
// Physical page information, coded as a bitfield, because space is a premium!
|
||||
bitfield! {
|
||||
#[derive(Copy, Clone, Eq)]
|
||||
|
|
@ -65,7 +65,11 @@ pub type JournalType = u32;
|
|||
/// in packed disk representations.
|
||||
pub type VirtAddr = NonZeroU64;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
/// A PageAlignedVa is guaranteed to be an address that's at least big enough to hold
|
||||
/// the constructing address. Thus it will tend to "round up" to the nearest page,
|
||||
/// unless the given address happens to be exactly one page in size.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub(crate) struct PageAlignedVa(VirtAddr);
|
||||
impl PageAlignedVa {
|
||||
pub(crate) fn as_u32(&self) -> u32 {
|
||||
|
|
@ -86,9 +90,9 @@ impl PageAlignedVa {
|
|||
impl From<u64> for PageAlignedVa {
|
||||
fn from(arg: u64) -> Self {
|
||||
if arg % VPAGE_SIZE as u64 == 0 {
|
||||
PageAlignedVa(VirtAddr::new(arg / VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new(arg as u64).unwrap())
|
||||
} else {
|
||||
PageAlignedVa(VirtAddr::new(arg / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new((arg as u64 / VPAGE_SIZE as u64 + 1) * VPAGE_SIZE as u64).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -99,18 +103,18 @@ impl From<PageAlignedPa> for PageAlignedVa {
|
|||
impl From<u32> for PageAlignedVa {
|
||||
fn from(arg: u32) -> Self {
|
||||
if arg as usize % VPAGE_SIZE == 0 {
|
||||
PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new(arg as u64).unwrap())
|
||||
} else {
|
||||
PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new((arg as u64 / VPAGE_SIZE as u64 + 1) * VPAGE_SIZE as u64).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<usize> for PageAlignedVa {
|
||||
fn from(arg: usize) -> Self {
|
||||
if arg % VPAGE_SIZE == 0 {
|
||||
PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new(arg as u64).unwrap())
|
||||
} else {
|
||||
PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap())
|
||||
PageAlignedVa(VirtAddr::new((arg as u64 / VPAGE_SIZE as u64 + 1) * VPAGE_SIZE as u64).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,6 +127,7 @@ impl Add for PageAlignedVa {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) struct PageAlignedPa(PhysAddr);
|
||||
impl PageAlignedPa {
|
||||
|
|
|
|||
|
|
@ -134,19 +134,7 @@ use std::rc::Rc;
|
|||
/// of closure callback is an additional feature to the typical Rust File interface.
|
||||
/// - If the client attempts to read or write to any keys that span a Basis modification,
|
||||
/// the now-ambiguous key operation will return a `BrokenPipe` error to the caller.
|
||||
|
||||
|
||||
/// This constant maps onto a region that's "unused" by Xous, and claimable by user
|
||||
/// processes.
|
||||
const PDDB_BACKING_BASE: usize = 0x8000_0000;
|
||||
/// This designates the largest contiguous extent that we could allocate for a file.
|
||||
/// The backing isn't allocated -- it is merely reserved. Accesses to the backing trigger
|
||||
/// page faults that are handled by the PDDB, and just the pages actively being dereferenced is
|
||||
/// swapped into physical memory on demand. This does put a pretty hard upper limit on file sizes
|
||||
/// on a 32-bit system, but PDDB is coded such that we could extend to a 64-bit system and
|
||||
/// increase this limit my changing the constants here.
|
||||
const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
||||
|
||||
///
|
||||
/// General flash->key structure
|
||||
///
|
||||
/// The basic unit of memory is a page (4k). Keys shorter than 4k will be packed into a single
|
||||
|
|
@ -348,10 +336,23 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
|||
/// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
|
||||
|
||||
|
||||
/*
|
||||
/// This constant maps onto a region that's "unused" by Xous, and claimable by user
|
||||
/// processes.
|
||||
const PDDB_BACKING_BASE: usize = 0x8000_0000;
|
||||
/// This designates the largest contiguous extent that we could allocate for a file.
|
||||
/// The backing isn't allocated -- it is merely reserved. Accesses to the backing trigger
|
||||
/// page faults that are handled by the PDDB, and just the pages actively being dereferenced is
|
||||
/// swapped into physical memory on demand. This does put a pretty hard upper limit on file sizes
|
||||
/// on a 32-bit system, but PDDB is coded such that we could extend to a 64-bit system and
|
||||
/// increase this limit my changing the constants here.
|
||||
const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
||||
*/
|
||||
|
||||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
log_server::init_wait().unwrap();
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
log::set_max_level(log::LevelFilter::Trace);
|
||||
log::info!("my PID is {}", xous::process::id());
|
||||
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
|
|
@ -361,9 +362,10 @@ fn xmain() -> ! {
|
|||
log::trace!("ready to accept requests");
|
||||
|
||||
// shared entropy cache across all process-local services (it's more efficient to request entropy in blocks from the TRNG)
|
||||
let mut entropy = Rc::new(RefCell::new(TrngPool::new()));
|
||||
let entropy = Rc::new(RefCell::new(TrngPool::new()));
|
||||
|
||||
// OS-specific PDDB driver
|
||||
log::info!("creating PddbOs structure");
|
||||
let mut pddb_os = PddbOs::new(Rc::clone(&entropy));
|
||||
log::info!("Initializing disk...");
|
||||
pddb_os.pddb_format().unwrap();
|
||||
|
|
@ -371,7 +373,7 @@ fn xmain() -> ! {
|
|||
|
||||
// register a suspend/resume listener
|
||||
let sr_cid = xous::connect(pddb_sid).expect("couldn't create suspend callback connection");
|
||||
let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
||||
let susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
||||
|
||||
loop {
|
||||
let msg = xous::receive_message(pddb_sid).unwrap();
|
||||
|
|
|
|||
|
|
@ -84,6 +84,9 @@ mod implementation {
|
|||
use gam::{ActionType, ProgressBar};
|
||||
use num_traits::*;
|
||||
use crate::{SignatureResult, GatewareRegion, MetadataInFlash};
|
||||
use aes::Aes256;
|
||||
use aes::cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray};
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -171,7 +174,7 @@ mod implementation {
|
|||
pub fn test(&mut self, _rootkeys_modal: &mut Modal, _main_cid: xous::CID) -> Result<(), RootkeyResult> {
|
||||
Ok(())
|
||||
}
|
||||
pub fn is_jtag_working(&self) -> bool {false}
|
||||
pub fn is_jtag_working(&self) -> bool {true}
|
||||
pub fn is_efuse_secured(&self) -> Option<bool> {None}
|
||||
pub fn check_gateware_signature(&mut self, _region_enum: GatewareRegion) -> SignatureResult {
|
||||
log::info!("faking gateware check...");
|
||||
|
|
@ -180,10 +183,10 @@ mod implementation {
|
|||
SignatureResult::DevKeyOk
|
||||
}
|
||||
pub fn is_pcache_update_password_valid(&self) -> bool {
|
||||
false
|
||||
true
|
||||
}
|
||||
pub fn is_pcache_boot_password_valid(&self) -> bool {
|
||||
false
|
||||
true
|
||||
}
|
||||
pub fn fpga_key_source(&self) -> FpgaKeySource {
|
||||
FpgaKeySource::Efuse
|
||||
|
|
@ -212,8 +215,38 @@ mod implementation {
|
|||
status_str: [79, 110, 32, 98, 114, 97, 110, 99, 104, 32, 109, 97, 105, 110, 10, 89, 111, 117, 114, 32, 98, 114, 97, 110, 99, 104, 32, 105, 115, 32, 117, 112, 32, 116, 111, 32, 100, 97, 116, 101, 32, 119, 105, 116, 104, 32, 39, 111, 114, 105, 103, 105, 110, 47, 109, 97, 105, 110, 39, 46, 10, 10, 67, 104, 97, 110, 103, 101, 115, 32, 110, 111, 116, 32, 115, 116, 97, 103, 101, 100, 32, 102, 111, 114, 32, 99, 111, 109, 109, 105, 116, 58, 10, 32, 32, 40, 117, 115, 101, 32, 34, 103, 105, 116, 32, 97, 100, 100, 32, 60, 102, 105, 108, 101, 62, 46, 46, 46, 34, 32, 116, 111, 32, 117, 112, 100, 97, 116, 101, 32, 119, 104, 97, 116, 32, 119, 105, 108, 108, 32, 98, 101, 32, 99, 111, 109, 109, 105, 116, 116, 101, 100, 41, 10, 32, 32, 40, 117, 115, 101, 32, 34, 103, 105, 116, 32, 114, 101, 115, 116, 111, 114, 101, 32, 60, 102, 105, 108, 101, 62, 46, 46, 46, 34, 32, 116, 111, 32, 100, 105, 115, 99, 97, 114, 100, 32, 99, 104, 97, 110, 103, 101, 115, 32, 105, 110, 32, 119, 111, 114, 107, 105, 110, 103, 32, 100, 105, 114, 101, 99, 116, 111, 114, 121, 41, 10, 32, 32, 40, 99, 111, 109, 109, 105, 116, 32, 111, 114, 32, 100, 105, 115, 99, 97, 114, 100, 32, 116, 104, 101, 32, 117, 110, 116, 114, 97, 99, 107, 101, 100, 32, 111, 114, 32, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 32, 105, 110, 32, 115, 117, 98, 109, 111, 100, 117, 108, 101, 115, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 97, 112, 112, 101, 110, 100, 95, 99, 115, 114, 46, 112, 121, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 98, 101, 116, 114, 117, 115, 116, 101, 100, 95, 115, 111, 99, 46, 112, 121, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 98, 111, 111, 116, 47, 98, 101, 116, 114, 117, 115, 116, 101, 100, 45, 98, 111, 111, 116, 47, 97, 115, 115, 101, 109, 98, 108, 101, 46, 115, 104, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 98, 111, 111, 116, 95, 116, 101, 115, 116, 46, 112, 121, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 98, 117, 105, 108, 100, 45, 100, 111, 99, 115, 46, 115, 104, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 99, 104, 101, 99, 107, 45, 116, 105, 109, 105, 110, 103, 46, 115, 104, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 99, 111, 109, 112, 105, 108, 101, 114, 95, 114, 116, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 101, 110, 99, 114, 121, 112, 116, 45, 98, 105, 116, 115, 116, 114, 101, 97, 109, 45, 112, 121, 116, 104, 111, 110, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 103, 97, 116, 101, 119, 97, 114, 101, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 108, 105, 116, 101, 100, 114, 97, 109, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 108, 105, 116, 101, 115, 99, 111, 112, 101, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 108, 105, 116, 101, 120, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 109, 105, 103, 101, 110, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 111, 112, 101, 110, 116, 105, 116, 97, 110, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 112, 121, 115, 101, 114, 105, 97, 108, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 112, 121, 116, 104, 111, 110, 100, 97, 116, 97, 45, 99, 112, 117, 45, 118, 101, 120, 114, 105, 115, 99, 118, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102, 105, 101, 100, 58, 32, 32, 32, 100, 101, 112, 115, 47, 114, 111, 109, 45, 108, 111, 99, 97, 116, 101, 32, 40, 109, 111, 100, 105, 102, 105, 101, 100, 32, 99, 111, 110, 116, 101, 110, 116, 41, 10, 9, 109, 111, 100, 105, 102],
|
||||
}
|
||||
}
|
||||
pub fn aes_op(&mut self, _key_index: u8, _op_type: AesOpType, _block: &mut [u8; 16]) {}
|
||||
pub fn aes_par_op(&mut self, _key_index: u8, _op_type: AesOpType, _block: &mut [[u8; 16]; PAR_BLOCKS]) {}
|
||||
pub fn aes_op(&mut self, key_index: u8, op_type: AesOpType, block: &mut [u8; 16]) {
|
||||
// fake a "well known" key by just expanding the index into a trivial key
|
||||
let mut key = [0 as u8; 32];
|
||||
key[0] = key_index;
|
||||
let cipher = Aes256::new(GenericArray::from_slice(&key));
|
||||
match op_type {
|
||||
AesOpType::Decrypt => {
|
||||
cipher.decrypt_block(block.try_into().unwrap())
|
||||
},
|
||||
AesOpType::Encrypt => {
|
||||
cipher.encrypt_block(block.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn aes_par_op(&mut self, key_index: u8, op_type: AesOpType, blocks: &mut [[u8; 16]; PAR_BLOCKS]) {
|
||||
// fake a "well known" key by just expanding the index into a trivial key
|
||||
let mut key = [0 as u8; 32];
|
||||
key[0] = key_index;
|
||||
let cipher = Aes256::new(GenericArray::from_slice(&key));
|
||||
match op_type {
|
||||
AesOpType::Decrypt => {
|
||||
for block in blocks.iter_mut() {
|
||||
cipher.decrypt_block(block.try_into().unwrap());
|
||||
}
|
||||
},
|
||||
AesOpType::Encrypt => {
|
||||
for block in blocks.iter_mut() {
|
||||
cipher.encrypt_block(block.try_into().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
"oqc-test",
|
||||
"net",
|
||||
"dns",
|
||||
"pddb",
|
||||
];
|
||||
let benchmark_pkgs = [
|
||||
"benchmark",
|
||||
|
|
@ -113,7 +114,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
"com",
|
||||
"susres",
|
||||
];
|
||||
let pddb_dev_pkgs = [
|
||||
let pddb_dev_pkgs = [ // just for checking compilation
|
||||
"ticktimer-server",
|
||||
"log-server",
|
||||
"susres",
|
||||
|
|
@ -121,6 +122,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
"trng",
|
||||
"pddb",
|
||||
"sha2",
|
||||
/*
|
||||
"llio",
|
||||
"root-keys",
|
||||
"jtag",
|
||||
"rtc",
|
||||
"com",
|
||||
"gam",
|
||||
"graphics-server",
|
||||
"keyboard",
|
||||
"ime-frontend",
|
||||
"ime-plugin-shell",
|
||||
"status",
|
||||
*/
|
||||
];
|
||||
|
||||
let aestest_pkgs = ["ticktimer-server", "log-server", "aes-test"];
|
||||
|
|
|
|||
Loading…
Reference in a new issue