mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-24 09:40:52 +00:00
- 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.
114 lines
No EOL
4.4 KiB
Rust
114 lines
No EOL
4.4 KiB
Rust
use super::{PAGE_SIZE, TrngPool, VirtAddr, murmur3_32};
|
|
use core::mem::size_of;
|
|
use aes_gcm_siv::{Nonce, Tag};
|
|
use std::rc::Rc;
|
|
use core::cell::RefCell;
|
|
use core::ops::{Deref, DerefMut};
|
|
use core::convert::TryInto;
|
|
|
|
use bitflags::bitflags;
|
|
|
|
bitflags! {
|
|
/// flags used by the page table
|
|
pub struct PtFlags: u8 {
|
|
/// Pages that don't decrypt properly are marked as INVALID in the cache.
|
|
const INVALID = 0b0000_0000;
|
|
/// set for records that are synced to the copy in Flash. Every valid record
|
|
/// from Flash should have this set; it should only be cleared for blocks in Cache.
|
|
const CLEAN = 0b0000_0001;
|
|
|
|
}
|
|
}
|
|
impl Default for PtFlags {
|
|
fn default() -> PtFlags {PtFlags::INVALID}
|
|
}
|
|
|
|
/// A Page Table Entry. Must be equal in length to one AES block size (128 bits).
|
|
/// This is stored in the FLASH itself, so size is not as much of a constraint.
|
|
///
|
|
/// Contains the address map of the corresponding entry,
|
|
/// plus a nonce, and a checksum. Due to the Page Table being deliberately
|
|
/// srtuctured to have invalid entries that don't decrypt correctly, you
|
|
/// can't use a chaining approach. Thus these entries are encrypted closer to
|
|
/// an ECB-style, thus an embedded nonce is necessary to keep identical entries
|
|
/// from appearing the same in the ciphertext domain.
|
|
///
|
|
/// It's not clear at all if the nonce is large enough to prevent random collisions;
|
|
/// however, the sheer bulk of the page table demands a compact representation. Thus,
|
|
/// any routines downstream of the Pte shall be coded to handle potentially a much larger
|
|
/// nonce and checksum structure.
|
|
#[repr(C, packed)]
|
|
#[derive(Default)]
|
|
pub(crate) struct Pte {
|
|
/// the virtual address is 48 bits long
|
|
pddb_addr: [u8; 6],
|
|
/// this maps to a u8
|
|
flags: PtFlags,
|
|
reserved: u8,
|
|
/// 32-bit strength of a nonce, but can be varied
|
|
nonce: [u8; 4],
|
|
/// 32-bit "weak" checksum, used only for quick scans of the PTE to determine a coarse "in" or "out" classifier
|
|
/// checksum is computed on all of the bits prior, so checksum(pddb_addr, flags, nonce)
|
|
checksum: [u8; 4],
|
|
}
|
|
impl Pte {
|
|
pub fn new(va: VirtAddr, flags: PtFlags, entropy: Rc<RefCell<TrngPool>>) -> Self {
|
|
let nonce_u32 = entropy.borrow_mut().get_u32();
|
|
let mut pte = Pte {
|
|
pddb_addr: va.get().to_le_bytes()[..6].try_into().unwrap(),
|
|
flags,
|
|
reserved: 0,
|
|
nonce: nonce_u32.to_le_bytes(),
|
|
checksum: [0; 4],
|
|
};
|
|
let pte_data = pte.deref();
|
|
let checksum = murmur3_32(&pte_data[..12], nonce_u32);
|
|
pte.checksum = checksum.to_le_bytes();
|
|
|
|
pte
|
|
}
|
|
}
|
|
impl Deref for Pte {
|
|
type Target = [u8];
|
|
fn deref(&self) -> &[u8] {
|
|
unsafe {
|
|
core::slice::from_raw_parts(self as *const Pte as *const u8, core::mem::size_of::<Pte>())
|
|
as &[u8]
|
|
}
|
|
}
|
|
}
|
|
|
|
impl DerefMut for Pte {
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
|
unsafe {
|
|
core::slice::from_raw_parts_mut(self as *mut Pte as *mut u8, core::mem::size_of::<Pte>())
|
|
as &mut [u8]
|
|
}
|
|
}
|
|
}
|
|
|
|
pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE;
|
|
/// This structure is mapped into the top of FLASH memory, starting at
|
|
/// xous::PDDB_LOC. This actually slightly over-sizes the page table,
|
|
/// because the page table does not map the locations for the page table
|
|
/// itself, the MBBB, or the FSCB. However, the 0th entry of the page table
|
|
/// always corresponds to the base of data in FLASH, which means the excess
|
|
/// pages are going to be toward the high end of the page table range.
|
|
#[repr(C, packed)]
|
|
pub(crate) struct PageTableInFlash {
|
|
table: [Pte; PDDB_SIZE_PAGES],
|
|
}
|
|
|
|
/// This is the representation of a page of data on disk. Keys that span multiple
|
|
/// pages have to decrypt individual pages, subtracting the nonce, journalrev, and tag, to find
|
|
/// the actual data being retrieved.
|
|
pub(crate) struct EncryptedPage {
|
|
/// the nonce is not encrypted
|
|
p_nonce: [u8; size_of::<Nonce>()],
|
|
/// journal_rev is encrypted and indicates the current journal revision for the block (u32 le)
|
|
journal_rev: [u8; 4],
|
|
/// data is encrypted and holds the good stuff
|
|
data: [u8; (PAGE_SIZE - size_of::<Nonce>() - size_of::<Tag>() - size_of::<u32>())],
|
|
/// tag is the authentication tag. If the page decrypts & authenticates, we know it's a valid data block for us.
|
|
p_tag: [u8; size_of::<Tag>()],
|
|
} |