diff --git a/services/pddb/src/backend.rs b/services/pddb/src/backend.rs index 99d4a9e4f..448f533ea 100644 --- a/services/pddb/src/backend.rs +++ b/services/pddb/src/backend.rs @@ -7,6 +7,11 @@ pub use fastspace::*; mod types; pub use types::*; + +// local to the backend +mod murmur3; +pub(crate) use murmur3::*; + #[cfg(any(target_os = "none", target_os = "xous"))] mod hw; pub(crate) use hw::*; diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 187b774b1..a1516b854 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -4,6 +4,8 @@ use super::*; use core::num::NonZeroU64; use core::ops::{Deref, DerefMut}; use core::{mem, slice}; +use core::mem::size_of; +use aes_gcm_siv::Nonce; pub(crate) const FREE_CACHE_SIZE: usize = 16; pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN]; @@ -19,10 +21,10 @@ pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN]; /// As a directory structure, the basis root is designed to be read into RAM in a contiguous block. /// it'll typically be less than a page in length, but a pathological number of dictionaries can make it /// much longer. -#[repr(C, packed)] +#[repr(C)] pub(crate) struct BasisRoot { /// this is stored as plaintext and generated fresh every time the block is re-encrypted - pub(crate) p_nonce: [u8; 12], + pub(crate) p_nonce: [u8; size_of::()], // everything below here is encrypted using AES-GCM-SIV pub(crate) magic: [u8; 4], pub(crate) version: u16, @@ -111,7 +113,7 @@ pub(crate) struct FreeSpace { /// 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. pub(crate) struct Dictionary { - p_nonce: [u8; 12], + p_nonce: [u8; size_of::()], journal_rev: u32, num_keys: u32, age: u32, // increment every time the dictionary definition itself is modified diff --git a/services/pddb/src/backend/fastspace.rs b/services/pddb/src/backend/fastspace.rs index d4cd9bc89..c488f8598 100644 --- a/services/pddb/src/backend/fastspace.rs +++ b/services/pddb/src/backend/fastspace.rs @@ -1,14 +1,17 @@ use core::num::NonZeroU32; -use core::ops::Deref; +use core::ops::{Deref, DerefMut}; +use core::convert::TryInto; use super::PAGE_SIZE; use crate::*; +use core::mem::size_of; +use aes_gcm_siv::{Nonce, Tag}; /// Each free_pool entry takes about 4 bytes, so give-or-take we have about 1000 free_pool /// entries per page of storage for the free_pool, or 4k * 1000 ~ 4MiB per page, when PhysAddr is a u32 pub(crate) const FASTSPACE_PAGES: usize = 2; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[repr(u8)] pub enum SpaceState { /// pages that are completely un-spoken for @@ -37,7 +40,8 @@ impl From for u8 { } } -pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) - (12 + 16)) / core::mem::size_of::(); +pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) + - (size_of::() + size_of::())) / core::mem::size_of::(); /// FastSpace tracks a limited set of physical pages /// An optimal implementation of this would fill whole pages with the free_pool array. /// This record is meant to be updated rarely, and atomically, in a make-before-break fashion: @@ -55,7 +59,7 @@ pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) /// and hopefully nothing else? /// The "rare" property is important especially as the disk size scales up; if we wanted to keep /// 100MiB of "fast space" on hand, this structure would span 25 pages. -#[repr(C, packed)] +#[repr(C)] pub (crate) struct FastSpace { /// Not sure if there is a "better" way to compute things, but we want the number of entries in the /// free_pool array to "round out" the FastSpace record to be equal to exactly one page @@ -71,13 +75,12 @@ impl Deref for FastSpace { } } -/// this structure is created just to confirm that the overall size is exactly one page, and should only -/// be used by the test included in this file. +/// this structure confirms that the overall size of the FastSpace structure as stored in Flash. #[repr(C, packed)] -struct FastSpaceInFlash { - p_nonce: [u8; 12], +pub(crate) struct FastSpaceInFlash { + p_nonce: [u8; size_of::()], ram_rep: FastSpace, - p_tag: [u8; 16], + p_tag: [u8; size_of::()], } /// a 128-bit record that stores an encrypted update to the FastSpace pool, facilitating the "rarely" update property of the structure. @@ -85,7 +88,7 @@ struct FastSpaceInFlash { #[cfg(not(feature = "u64_pa"))] pub (crate) struct SpaceUpdate { nonce: u64, - page_number: PhysAddr, + page_number: PhysPage, // this checksum is "weak" but we are protecting against two scenarios: // 1. partially written SpaceUpdate record (so the last bytes or so are FF) // 2. a malicious attacker @@ -96,15 +99,59 @@ pub (crate) struct SpaceUpdate { // being treated as free space and getting erased (data loss, not disclosure). checksum: [u8; 4], } +#[cfg(not(feature = "u64_pa"))] +impl SpaceUpdate { + pub fn try_into_phys_page(slice: &[u8]) -> Option { + let computed_sum = murmur3_32(&slice[..12], u32::from_be_bytes(slice[4..8].try_into().unwrap())); + if u32::from_le_bytes(slice[12..].try_into().unwrap()) == computed_sum { + let pp = u32::from_le_bytes(slice[8..12].try_into().unwrap()); + Some(PhysPage(pp)) + } else { + None + } + } + pub fn new(nonce: u64, page_number: PhysPage) -> Self { + let mut hashbuf: [u8; 12] = [0; 12]; + for (&src, dst) in nonce.to_le_bytes().iter().zip(hashbuf[..8].iter_mut()) { + *dst = src; + } + for (&src, dst) in page_number.0.to_le_bytes().iter().zip(hashbuf[8..12].iter_mut()) { + *dst = src; + } + let computed_sum = murmur3_32(&hashbuf[..12], u32::from_be_bytes(hashbuf[4..8].try_into().unwrap())); + SpaceUpdate { + nonce, + page_number, + checksum: computed_sum.to_le_bytes(), + } + } +} + #[cfg(feature = "u64_pa")] pub (crate) struct SpaceUpdate { nonce: u64, - page_number: PhysAddr, + page_number: PhysPage, // consider: using the top 12 bits of the PhysAddr as a checksum } -impl SpaceUpdate { - // add accessors, decryptors, and constructors so we don't shoot ourselves in the foot so much. + +impl Deref for SpaceUpdate { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + core::slice::from_raw_parts(self as *const SpaceUpdate as *const u8, core::mem::size_of::()) + as &[u8] + } + } } +impl DerefMut for SpaceUpdate { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + core::slice::from_raw_parts_mut(self as *mut SpaceUpdate as *mut u8, core::mem::size_of::()) + as &mut [u8] + } + } +} + mod tests { use super::*; diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 476504b59..1305cd017 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -3,13 +3,16 @@ 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 rand_core::{CryptoRng, RngCore}; -use cipher::{BlockCipher, BlockDecrypt, BlockEncrypt}; +use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray}; use root_keys::api::{AesRootkeyType, Block}; -use core::ops::Deref; +use core::ops::{Deref, DerefMut}; +use core::mem::size_of; use core::convert::TryFrom; use std::collections::HashMap; +use std::collections::HashSet; use std::collections::BinaryHeap; use std::cmp::Reverse; use std::io::{Result, Error, ErrorKind}; @@ -25,7 +28,7 @@ pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize; #[repr(C, packed)] // this can map directly into Flash pub(crate) struct StaticCryptoData { /// aes-256 key of the system basis, encrypted with the User0 root key - pub(crate) system_key: [u8; 32], + pub(crate) system_key: [u8; AES_KEYSIZE], /// a pool of fixed data used to pick salts, based on a hash of the basis name pub(crate) salt_base: [u8; 2048], /// also random data, but no specific purpose @@ -35,7 +38,7 @@ impl Deref for StaticCryptoData { type Target = [u8]; fn deref(&self) -> &[u8] { unsafe { - core::slice::from_raw_parts(self as *const StaticCryptoData as *const u8, core::mem::size_of::()) + core::slice::from_raw_parts(self as *const StaticCryptoData as *const u8, size_of::()) as &[u8] } } @@ -56,8 +59,15 @@ pub(crate) struct PddbOs { /// free space circular buffer base -- location in FLASH, offset from physical bottom of pddb_mr fscb_phys_base: PageAlignedPa, data_phys_base: PageAlignedPa, - system_basis_key: Option<[u8; 32]>, + system_basis_key: Option<[u8; AES_KEYSIZE]>, + /// virtual to physical page map. It's the reverse mapping of the physical page table on disk. v2p_map: HashMap>, + /// fast space cache + fspace_cache: HashSet, + /// memoize the location of the fscb log pages + fspace_log_addrs: Vec::, + /// memoize the current target offset for the next log entry + fspace_log_next_addr: Option, /// The PDDB eats a lot of entropy. Keep a local pool of entropy, so we're not wasting a lot of /// overhead passing messages to the TRNG. e_cache: Vec::, @@ -77,7 +87,7 @@ impl PddbOs { .expect("Couldn't map the PDDB memory range"); // the mbbb is located one page off from the Page Table - let key_phys_base = PageAlignedPa::from(core::mem::size_of::()); + let key_phys_base = PageAlignedPa::from(size_of::()); let mbbb_phys_base = key_phys_base + PageAlignedPa::from(PAGE_SIZE); let fscb_phys_base = PageAlignedPa::from(mbbb_phys_base.as_u32() + MBBB_PAGES as u32 * PAGE_SIZE as u32); @@ -98,6 +108,9 @@ impl PddbOs { 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::>::new(), + fspace_cache: HashSet::::new(), + fspace_log_addrs: Vec::::new(), + fspace_log_next_addr: None, e_cache: cache.to_vec(), dna: llio.soc_dna().unwrap(), } @@ -114,6 +127,15 @@ impl PddbOs { offset + self.data_phys_base.as_u32(), ).expect("couldn't write to data region in the PDDB"); } + fn patch_pagetable(&self, data: &[u8], offset: u32) { + assert!(data.len() + offset as usize <= size_of::(), "attempt to patch past page table end"); + self.spinor.patch( + self.pddb_mr.as_slice(), + xous::PDDB_LOC, + &data, + offset, + ).expect("couldn't write to page table"); + } fn patch_keys(&self, data: &[u8], offset: u32) { assert!(data.len() + offset as usize <= PAGE_SIZE, "attempt to burn key data that is outside the key region"); self.spinor.patch( @@ -133,7 +155,7 @@ impl PddbOs { ).expect("couldn't burn mbbb"); } /// raw patch is provided for 128-bit incremental updates to the FLASH. For FastSpace master record writes, - /// see write_fast_space() + /// see fast_space_write() fn patch_fscb(&self, data: &[u8], offset: u32) { assert!(data.len() + offset as usize <= PAGE_SIZE * FSCB_PAGES, "fscb patch would go out of bounds"); self.spinor.patch( @@ -143,64 +165,16 @@ impl PddbOs { self.fscb_phys_base.as_u32() + offset ).expect("couldn't burn fscb"); } - /// anytime the fscb is updated, all the partial records are nuked, as well as any existing record. - /// then, a _random_ location is picked to place the structure to help with wear levelling. - fn write_fast_space(&mut self, fs: &FastSpace) { - self.ensure_system_key(); - if let Some(system_basis_key) = self.system_basis_key { - let key = Key::from_slice(&system_basis_key); - let cipher = Aes256GcmSiv::new(key); - let nonce_array = self.gen_nonce(); - let nonce = Nonce::from_slice(&nonce_array); - let fs_ser: &[u8] = fs.deref(); - assert!( ((fs_ser.len() + core::mem::size_of::() + core::mem::size_of::()) & (PAGE_SIZE - 1)) == 0, - "FastSpace record is not page-aligned in size!"); - // create AAD: name, version number, and FPGA ID. - let mut aad = Vec::::new(); - aad.extend_from_slice(PDDB_FAST_SPACE_SYSTEM_BASIS.as_bytes()); - aad.extend_from_slice(&PDDB_VERSION.to_le_bytes()); - aad.extend_from_slice(&self.dna.to_le_bytes()); - // AAD + data => Payload - let payload = Payload { - msg: fs_ser, - aad: &aad, - }; - let ciphertext = cipher.encrypt(nonce, payload).expect("failed to encrypt FastSpace record"); - let ct_to_flash = ciphertext.deref(); - // determine which page we're going to write the ciphertext into - let page_search_limit = FSCB_PAGES - ((PageAlignedPa::from(ciphertext.len()).as_usize() / PAGE_SIZE) - 1); - log::info!("picking a random page out of {} pages for fscb", page_search_limit); - let dest_page = self.trng_cache_u32() % page_search_limit as u32; - // atomicity of the FreeSpace structure is a bit of a tough topic. It's a fairly hefty structure, - // that runs a risk of corruption as it's being written, if power is lost or the system crashes. - // However, the guiding principle of this ordering is that it's better to have no FastSpace structure - // (and force a re-computation of it by scanning all the open Basis), than it is to have a broken - // FastSpace structure + stale SpaceUpdates. In particular a stale SpaceUpdate would lead the system - // to conclude that some pages are free when they aren't. Thus, we prefer to completely erase the - // FSCB region before committing the updated version. - { // this is where we begin the "it would be bad if we lost power about now" code region - // erase the entire fscb area - let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE]; - for offset in 0..FSCB_PAGES { - self.patch_fscb(&blank_sector, (offset * PAGE_SIZE) as u32); - } - // commit the fscb data - self.patch_fscb(&[&nonce_array, ct_to_flash].concat(), dest_page * PAGE_SIZE as u32); - } // end "it would be bad if we lost power now" region - } else { - panic!("invalid state!"); - } - } /// maps a StaticCryptoData structure into the key area of the PDDB. - fn get_static_crypto_data(&self) -> &StaticCryptoData { + fn static_crypto_data_get(&self) -> &StaticCryptoData { let scd_ptr = self.key_phys_base.as_usize() as *const StaticCryptoData; let scd: &StaticCryptoData = unsafe{scd_ptr.as_ref().unwrap()}; scd } /// takes the key and writes it with zero, using hard pointer math and a compiler fence to ensure /// the wipe isn't optimized out. - fn erase_system_key(&mut self) { + fn syskey_erase(&mut self) { if let Some(mut key) = self.system_basis_key.take() { let b = key.as_mut_ptr(); for i in 0..key.len() { @@ -211,10 +185,10 @@ impl PddbOs { core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); } } - fn ensure_system_key(&mut self) { + fn syskey_ensure(&mut self) { if self.system_basis_key.is_none() { - let scd = self.get_static_crypto_data(); - let mut system_key: [u8; 32] = [0; 32]; + let scd = self.static_crypto_data_get(); + let mut system_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; for (&src, dst) in scd.system_key.iter().zip(system_key.iter_mut()) { *dst = src; } @@ -254,12 +228,73 @@ impl PddbOs { } } /// generates a 96-bit nonce using the CPRNG - pub fn gen_nonce(&mut self) -> [u8; 12] { - let mut nonce: [u8; 12] = [0; 12]; + pub fn nonce_gen(&mut self) -> [u8; size_of::()] { + let mut nonce: [u8; size_of::()] = [0; size_of::()]; self.trng_cache_slice(&mut nonce); nonce } + /// Create fast_space AAD: name, version number, and FPGA ID. + /// This data is "well known", and fixed for every device, but changes + /// from device to device. It prevents records from one device from being copied + /// and used on another, and it also makes it annoying to swap out the FPGA. + /// This makes it a bit harder to repair, but also makes it harder for an adversary + /// to change out the FPGA on your board without also having to patch the OS. + /// If you are doing a repair, patch out the LLIO function that returns the DNA with + /// the desired target DNA to effectively bypass the check (you need to, of course, + /// know what that DNA is in the first place, so hopefully you were able to extract + /// it before you destroyed the FPGA). + fn fast_space_aad(&self, aad: &mut Vec::) { + aad.extend_from_slice(PDDB_FAST_SPACE_SYSTEM_BASIS.as_bytes()); + aad.extend_from_slice(&PDDB_VERSION.to_le_bytes()); + aad.extend_from_slice(&self.dna.to_le_bytes()); + } + /// Assumes you are writing a "most recent" version of FastSpace. Thus + /// Anytime the fscb is updated, all the partial records are nuked, as well as any existing record. + /// Then, a _random_ location is picked to place the structure to help with wear levelling. + fn fast_space_write(&mut self, fs: &FastSpace) { + self.syskey_ensure(); + if let Some(system_basis_key) = self.system_basis_key { + let key = Key::from_slice(&system_basis_key); + let cipher = Aes256GcmSiv::new(key); + let nonce_array = self.nonce_gen(); + let nonce = Nonce::from_slice(&nonce_array); + let fs_ser: &[u8] = fs.deref(); + assert!( ((fs_ser.len() + size_of::() + size_of::()) & (PAGE_SIZE - 1)) == 0, + "FastSpace record is not page-aligned in size!"); + // AAD + data => Payload + let mut aad = Vec::::new(); + self.fast_space_aad(&mut aad); + let payload = Payload { + msg: fs_ser, + aad: &aad, + }; + let ciphertext = cipher.encrypt(nonce, payload).expect("failed to encrypt FastSpace record"); + let ct_to_flash = ciphertext.deref(); + // determine which page we're going to write the ciphertext into + let page_search_limit = FSCB_PAGES - ((PageAlignedPa::from(ciphertext.len()).as_usize() / PAGE_SIZE) - 1); + log::info!("picking a random page out of {} pages for fscb", page_search_limit); + let dest_page = self.trng_cache_u32() % page_search_limit as u32; + // atomicity of the FreeSpace structure is a bit of a tough topic. It's a fairly hefty structure, + // that runs a risk of corruption as it's being written, if power is lost or the system crashes. + // However, the guiding principle of this ordering is that it's better to have no FastSpace structure + // (and force a re-computation of it by scanning all the open Basis), than it is to have a broken + // FastSpace structure + stale SpaceUpdates. In particular a stale SpaceUpdate would lead the system + // to conclude that some pages are free when they aren't. Thus, we prefer to completely erase the + // FSCB region before committing the updated version. + { // this is where we begin the "it would be bad if we lost power about now" code region + // erase the entire fscb area + let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE]; + for offset in 0..FSCB_PAGES { + self.patch_fscb(&blank_sector, (offset * PAGE_SIZE) as u32); + } + // commit the fscb data + self.patch_fscb(&[&nonce_array, ct_to_flash].concat(), dest_page * PAGE_SIZE as u32); + } // end "it would be bad if we lost power now" region + } else { + panic!("invalid state!"); + } + } /// WARNING: only call this function when all knows Basis have been unlocked, otherwise locked /// Basis will be marked in the freespace sweep for deletion. @@ -273,9 +308,9 @@ impl PddbOs { /// needs to run in a fairly memory-constrained environment, keeping in mind that if the PDDB /// structures were to be extended to run on say, an external USB drive with gigabytes of space, /// we cannot afford to naively allocate vectors that count every single page. - fn collect_fastspace(&mut self) -> Vec:: { + fn fast_space_generate(&mut self) -> Vec:: { let mut free_pool = Vec::::new(); - let max_entries = FASTSPACE_PAGES * PAGE_SIZE / core::mem::size_of::(); + let max_entries = FASTSPACE_PAGES * PAGE_SIZE / size_of::(); free_pool.reserve_exact(max_entries); // 1. scan through all of the known physical pages, and add them to a binary heap. // WARNING: this could get really big for a very large filesystem. It's capped at ~100k for @@ -350,43 +385,358 @@ impl PddbOs { page_pool } + fn fscb_deref(&self) -> &[u8] { + &self.pddb_mr.as_slice()[self.fscb_phys_base.as_usize()..self.fscb_phys_base.as_usize() + FSCB_PAGES * PAGE_SIZE] + } + /// Reads the data structures in the FSCB, if any, and stores the results in the fspace_cache HashSet. + /// Note the following convention on the fscb: if the first 128 bits of a page are all 1's, then that sector + /// cannot contain the master FastSpace record. Also, if a sector is to contain *any* data, the first piece + /// of data must start at exactly 16 bytes into the page (at the 129th bit). Examples: + /// + /// FF = must be all 1's + /// xx/yy/zz = AES encrypted data. Techincally AES includes the all 1's ciphertext in its set, but it's extremely unlikely. + /// + /// Byte # + /// | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | ... # byte offset + /// --------------------------------------------------------------------------------------------------------------- + /// | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | ... # page must be blank + /// | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | xx | yy | yy | yy | yy | yy | ... # page must contain FastSpace record + /// | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | FF | zz | zz | zz | zz | zz | ... # page contains an arbitrary number of SpaceUpdate records + /// + fn fast_space_read(&mut self) { + self.syskey_ensure(); + if let Some(system_key) = self.system_basis_key { + // remove the old contents, since we're about to re-read an authorative copy from disk. + self.fspace_cache.clear(); + self.fspace_log_addrs.clear(); + self.fspace_log_next_addr = None; + + // let fscb_slice = self.fscb_deref(); // can't use this line because it causse self to be immutably borrowed, so we write out the equivalent below. + let fscb_slice = &self.pddb_mr.as_slice()[self.fscb_phys_base.as_usize()..self.fscb_phys_base.as_usize() + FSCB_PAGES * PAGE_SIZE]; + + // 1. scan through the entire space, and look for the FastSpace record. It can be identified by the + // first 16 (aes::BLOCK_SIZE) bytes not being all 1's. + let not_aes: [u8; aes::BLOCK_SIZE] = [0xff; aes::BLOCK_SIZE]; + let mut fscb_pages = 0; + let mut blank_pages = Vec::new(); + for page_start in (0..fscb_slice.len()).step_by(PAGE_SIZE) { + if (fscb_slice[page_start..page_start + aes::BLOCK_SIZE] == not_aes) + && (fscb_slice[page_start + aes::BLOCK_SIZE..page_start + aes::BLOCK_SIZE * 2] == not_aes) { + // page has met the criteria for being blank, skip to the next page + blank_pages.push(page_start); + continue + } else if fscb_slice[page_start..page_start+aes::BLOCK_SIZE] == not_aes { + // this page contains update records; stash it for scanning after we've read in the master record + self.fspace_log_addrs.push(PageAlignedPa::from(page_start)); + continue + } 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::()]; + // copy the encrypted data to the decryption buffer + for (&src, dst) in + fscb_slice[page_start + size_of::() .. page_start + size_of::() + FSCB_PAGES * PAGE_SIZE] + .iter().zip(fscb_buf.iter_mut()) { + *dst = src; + } + let mut aad = Vec::::new(); + self.fast_space_aad(&mut aad); + let mut payload = Payload { + msg: &fscb_buf, + aad: &aad, + }; + let key = Key::from_slice(&system_key); + let cipher = Aes256GcmSiv::new(key); + match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::()]), payload) { + Ok(msg) => { + // 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; + let fs_ref: &FastSpace = unsafe{fs_ptr.as_ref().unwrap()}; + // iterate through the FastSpace disk image and extract the valid and free pages, and note them in the cache + for pp in fs_ref.free_pool.iter() { + if pp.valid() && pp.space_state() == SpaceState::Free { + self.fspace_cache.insert(*pp); + } + } + }, + Err(e) => { + log::warn!("FSCB data was found, but it did not decrypt correctly. Ignoring FSCB record. Error: {:?}", e) + } + } + } + fscb_pages += 1; + } + } + assert!(fscb_pages == size_of::()); + + // 2. visit the update_page_addrs and modify the fspace_cache accordingly. + let cipher = Aes256::new(GenericArray::from_slice(&system_key)); + let mut block = Block::default(); + for page in &self.fspace_log_addrs { + for (index, ct_block) in + fscb_slice[page.as_usize() + aes::BLOCK_SIZE .. page.as_usize() + PAGE_SIZE] + .chunks_exact(aes::BLOCK_SIZE).enumerate() { + let mut is_blank = true; + for &ct in ct_block { + if ct != 0xFF { + is_blank = false; + } + } + if is_blank { + // end the scan at the first blank block. Note the location. + self.fspace_log_next_addr = Some((page.as_usize() + ((1 + index) * aes::BLOCK_SIZE)) as PhysAddr); + break; + } + // now try to decrypt the ciphertext block + for (&src, dst) in ct_block.iter().zip(block.iter_mut()) { + *dst = src; + } + cipher.decrypt_block(&mut block); + if let Some(pp) = SpaceUpdate::try_into_phys_page(block.as_slice()) { + // note: pp.valid() isn't the cryptographic check, the cryptographic check of record validity is in try_into_phys_page() + if pp.valid() { // PS: it should always be valid! + if let Some(prev_pp) = self.fspace_cache.get(&pp) { + if pp.journal() > prev_pp.journal() { + self.fspace_cache.replace(pp); + } else if pp.journal() == prev_pp.journal() { + log::error!("got two identical journal revisions -- this shouldn't happen, prev: {:?}, candidate: {:?}", prev_pp, pp); + panic!("Inconsistent FSCB state"); + } + } else { + log::info!("Strange...we have a journal entry for a free space page that isn't already in our cache. Guru meditation: {:?}", pp); + self.fspace_cache.insert(pp); + } + } + } else { + log::info!("possibly corrupted FSCB update record: {:x?}", block); + } + } + } + // at this point, fspace_cache should contain a collection of either Free or Dirty pages. Both are + // fair game for being recycled. + + // 3. Check to see if we have a target for our next cache write, if not, make one up. + if self.fspace_log_next_addr.is_none() { + if blank_pages.len() != 0 { + // pick a random page out of the blank pool (random for wear levelling) + let random_index = self.trng_cache_u32() as usize % blank_pages.len(); + // set the next log address at an offset of one AES block in from the top. + self.fspace_log_next_addr = Some((blank_pages[random_index] + aes::BLOCK_SIZE) as PhysAddr); + } else { + log::warn!("FSCB has no blank space for new update records. This will cause fast_space_alloc() to fail, which can be remedied with a call to fast_space_generate()."); + } + } + } else { + panic!("invalid state!"); + } + } + /// returns a count of the number of pages in the fspace cache + pub fn fast_space_len(&self) -> usize { + self.fspace_cache.len() + } + /// Normally, the fspace_log_next_addr is just incremented, but when it hits the end of the + /// page, it's set to None. This function will do a modestly expensive scan of the FSCB area + /// to try and either find another partially filled page, or a completely empty page. + pub fn fast_space_ensure_next_log(&mut self) -> bool { + if self.fspace_log_next_addr.is_some() { + true + } else { + let fscb_slice = self.fscb_deref(); + let blank: [u8; aes::BLOCK_SIZE] = [0xff; aes::BLOCK_SIZE]; + let mut blank_pages = Vec::new(); + for page_start in (0..fscb_slice.len()).step_by(PAGE_SIZE) { + if (fscb_slice[page_start..page_start + aes::BLOCK_SIZE] == blank) + && (fscb_slice[page_start + aes::BLOCK_SIZE..page_start + aes::BLOCK_SIZE * 2] == blank) { + // page has met the criteria for being blank, skip to the next page + blank_pages.push(page_start); + continue + } else if fscb_slice[page_start..page_start + aes::BLOCK_SIZE] == blank { + // this page contains update records; scan it for an empty slot + for (index, block) in + fscb_slice[page_start + aes::BLOCK_SIZE..page_start + PAGE_SIZE] + .chunks_exact(aes::BLOCK_SIZE).enumerate() { + // start with a size check; a failure mode of just iterating is the iterator will terminate early if the block sizes aren't the same. + let mut is_blank = block.len() == blank.len(); + // now confirm that every item is the same + for (&a, &b) in block.iter().zip(blank.iter()) { + if a != b {is_blank = false;} + } + if is_blank { + self.fspace_log_next_addr = Some( (page_start + (1 + index) * aes::BLOCK_SIZE) as PhysAddr ); + return true + } + } + } else { + // this is probably an encrypted FastSpace page, just skip it + continue + } + } + // if we got to this point, we couldn't find a partially full page. Pull a random page from the blank page pool. + if blank_pages.len() != 0 { + // pick a random page out of the blank pool (random for wear levelling) + let random_index = self.trng_cache_u32() as usize % blank_pages.len(); + // set the next log address at an offset of one AES block in from the top. + self.fspace_log_next_addr = Some((blank_pages[random_index] + aes::BLOCK_SIZE) as PhysAddr); + true + } else { + false + } + } + } + /// attempts to allocate a page out of the fspace cache (in RAM) + /// If None is returned, try calling fast_space_generate() to create a new pool of FastSpace pages. + pub fn try_fast_space_alloc(&mut self) -> Option { + // 1. Confirm that the fspace_log_next_addr is valid. If not, regenerate it, or fail. + if !self.fast_space_ensure_next_log() { + None + } else { + // 2. find the first page that is Free or Dirty. The order is already randomized, so we can do a stupid linear search. + let mut maybe_alloc = None; + for pp in self.fspace_cache.iter() { + if (pp.space_state() == SpaceState::Free || pp.space_state() == SpaceState::Dirty) && (pp.journal() < PHYS_PAGE_JOURNAL_MAX) { + let mut ppc = pp.clone(); + // take the state directly to Used, skipping MaybeUsed. If the system crashes between now and + // when the page is actually used, the consequence is a "lost" entry in the FastSpace cache. However, + // the entry will be reclaimed on the next full-space scan. This is a less-bad outcome than filling up + // the log with 2x the number of operations to record MaybeUsed and then Used. + ppc.set_space_state(SpaceState::Used); + ppc.set_journal(pp.journal() + 1); // this is guaranteed not to overflow because of a check in the "if" clause above + + // commit the usage to the journal + self.syskey_ensure(); + if let Some(system_key) = self.system_basis_key { + let cipher = Aes256::new(GenericArray::from_slice(&system_key)); + let mut update = SpaceUpdate::new(self.trng_cache_u64(), ppc); + let mut block = Block::from_mut_slice(update.deref_mut()); + cipher.encrypt_block(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; + if (next_addr & (PAGE_SIZE as PhysAddr - 1)) != 0 { + self.fspace_log_next_addr = Some(next_addr as PhysAddr); + } else { + // fspace_log_next_addr is already None because we used "take()". We'll find a free spot for the + // next journal entry the next time around. + } + + } else { + panic!("Inconsistent internal state"); + } + maybe_alloc = Some(ppc); + break; + } + } + if let Some(alloc) = maybe_alloc { + assert!(self.fspace_cache.replace(alloc).is_some(), "inconsistent state: we found a free page, but later when we tried to update it, it wasn't there!"); + } + maybe_alloc + } + } + /// This is the "try really hard" version of fast_space alloc. If the easy path doesn't work, + /// It will try to rescan the entire system and allocate a new FastSpace cache. + /// + /// If this returns None, we really are out of free space, *or* the user has cancelled out of the + /// "unlock all Basis" request and the system will act like it's out of free space. + pub fn fast_space_alloc(&mut self) -> Option { + match self.try_fast_space_alloc() { + Some(alloc) => Some(alloc), + None => { + if self.basis_request_unlock_all() { + let free_pool = self.fast_space_generate(); + if free_pool.len() == 0 { + // we're out of free space + None + } else { + let mut fast_space = FastSpace { + free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN], + }; + for (&src, dst) in free_pool.iter().zip(fast_space.free_pool.iter_mut()) { + *dst = src; + } + // write just commits a new record to disk, but doesn't update our internal data cache + self.fast_space_write(&fast_space); + // this will ensure the data cache is fully in sync + self.fast_space_read(); + + // retry the alloc -- this /should/ return a Some, but if it doesn't...we're out of space! + self.try_fast_space_alloc() + } + } else { + None + } + } + } + } + /// This function will prompt the user to unlock all the Basis. If the user asserts all + /// Basis have been unlocked, the function returns `true`. The other option is the user + /// can decline to unlock all the Basis right now, cancelling out of the process, which will + /// cause the requesting free space sweep to fail. + pub fn basis_request_unlock_all(&self) -> bool { + // this function is a morass of UX code that has to be written. Let's save it until later, + // once we've got some core functionality in the PDDB; or for testing, we could just "return true" + // and YOLO it. + unimplemented!(); + } + /// this function is dangerous in that calling it will completely erase all of the previous data /// in the PDDB an replace it with a brand-spanking new, blank PDDB. /// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference /// to the Spinor object allocated to the PDDB implementation for this operation. - pub(crate) fn format_pddb(&mut self) -> Result<()> { + pub(crate) fn pddb_format(&mut self) -> Result<()> { if !self.rootkeys.is_initialized().unwrap() { return Err(Error::new(ErrorKind::Unsupported, "Root keys are not initialized; cannot format a PDDB without root keys!")); } - // step 1. Erase the entire PDDB region. - log::info!("Erasing the PDDB region"); - let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE]; + // step 1. Erase the entire PDDB region - leaves the state in all 1's + { + log::info!("Erasing the PDDB region"); + let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE]; - // there is no convenience routine for erasing the entire disk. Maybe that's a good thing? - for offset in (0..PDDB_A_LEN).step_by(PAGE_SIZE) { - self.spinor.patch( - self.pddb_mr.as_slice(), - xous::PDDB_LOC, - &blank_sector, - offset as u32 - ).expect("couldn't erase memory"); + // there is no convenience routine for erasing the entire disk. Maybe that's a good thing? + for offset in (0..PDDB_A_LEN).step_by(PAGE_SIZE) { + if (offset / PAGE_SIZE) % 64 == 0 { + log::info!("Initial erase: {}/{}", offset, PDDB_A_LEN); + } + self.spinor.patch( + self.pddb_mr.as_slice(), + xous::PDDB_LOC, + &blank_sector, + offset as u32 + ).expect("couldn't erase memory"); + } } - // step 2. create our key material + // step 2. fill in the page table with junk, which marks it as cryptographically empty + let mut temp: [u8; PAGE_SIZE] = [0; PAGE_SIZE]; + for page in (0..size_of::()).step_by(PAGE_SIZE) { + self.trng_cache_slice(&mut temp); + self.patch_pagetable(&temp, page as u32); + } + if size_of::() & (PAGE_SIZE - 1) != 0 { + let remainder_start = size_of::() & !(PAGE_SIZE - 1); + log::info!("Page table does not end on a page boundary. Handling trailing page case of {} bytes", remainder_start); + let mut temp = Vec::::new(); + for _ in remainder_start..size_of::() { + temp.push(self.trng_cache_u8()); + } + self.patch_pagetable(&temp, remainder_start as u32); + } + + // step 3. create our key material // consider: making ensure_aes_password() a pub-scoped function? let's see how this works in practice. //if !self.rootkeys.ensure_aes_password() { // return Err(Error::new(ErrorKind::PermissionDenied, "unlock password was incorrect")); //} - assert!(core::mem::size_of::() == PAGE_SIZE, "StaticCryptoData structure is not correctly sized"); - let mut system_basis_key: [u8; 32] = [0; 32]; + assert!(size_of::() == PAGE_SIZE, "StaticCryptoData structure is not correctly sized"); + let mut system_basis_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; self.trng.fill_bytes(&mut system_basis_key); - let mut basis_key_enc: [u8; 32] = system_basis_key.clone(); + let mut basis_key_enc: [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); let mut crypto_keys = StaticCryptoData { - system_key: [0; 32], + system_key: [0; AES_KEYSIZE], salt_base: [0; 2048], reserved: [0; 2016], }; @@ -399,27 +749,43 @@ impl PddbOs { self.patch_keys(crypto_keys.deref(), 0); // now we have a copy of the AES key necessary to encrypt the default System basis that we created in step 2. - // step 3. mbbb handling + // step 4. mbbb handling // mbbb should just be blank at this point, and the flash was erased in step 1, so there's nothing to do. - // step 4. fscb handling + // step 5. fscb handling // pick a set of random pages from the free pool and assign it to the fscb - let free_pool = self.collect_fastspace(); + let free_pool = self.fast_space_generate(); let mut fast_space = FastSpace { free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN], }; for (&src, dst) in free_pool.iter().zip(fast_space.free_pool.iter_mut()) { *dst = src; } - self.write_fast_space(&fast_space); + self.fast_space_write(&fast_space); - // step 2. create the system basis root structure + // step 5. salt the free space with random numbers. this can take a while, we might need a "progress report" of some kind... + // this is coded using "direct disk" offsets...under the assumption that we only ever really want to do this here, and + // not re-use this routine elsewhere. + for offset in (self.data_phys_base.as_usize()..PDDB_A_LEN).step_by(PAGE_SIZE) { + self.trng_cache_slice(&mut temp); + if (offset / PAGE_SIZE) % 64 == 0 { + log::info!("Crytpographic 'erase': {}/{}", offset, PDDB_A_LEN); + } + self.spinor.patch( + self.pddb_mr.as_slice(), + xous::PDDB_LOC, + &temp, + offset as u32 + ).expect("couldn't fill in disk with random datax"); + } + + // step 6. create the system basis root structure let mut name: [u8; PDDB_MAX_BASIS_NAME_LEN] = [0; PDDB_MAX_BASIS_NAME_LEN]; for (&src, dst) in PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut()) { *dst = src; } let mut basis_root = BasisRoot { - p_nonce: self.gen_nonce(), + p_nonce: self.nonce_gen(), magic: api::PDDB_MAGIC, version: api::PDDB_VERSION, journal_rev: 0, @@ -431,22 +797,28 @@ impl PddbOs { // extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page let br_slice: &[u8] = basis_root.deref(); + // 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 - // step 4. Create a hashmap for our reverse PTE, and add it to the Pddb's cache - // we don't have a fscb yet, and everything is free space, so we will manually place these initial entries. let mut basis_v2p_map = HashMap::::new(); for (virt_page, phys_addr) in ( self.data_phys_base.as_u32()..self.data_phys_base.as_u32() + basis_root.prealloc_open_end.as_u32() ).step_by(PAGE_SIZE).enumerate() { - let mut rpte = PhysPage(0); - rpte.set_page_number(phys_addr / PAGE_SIZE as u32); - rpte.set_clean(true); - rpte.set_valid(true); - basis_v2p_map.insert((virt_page * PAGE_SIZE) as VirtAddr, rpte); + if let Some(alloc) = self.fast_space_alloc() { + 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((virt_page * PAGE_SIZE) as VirtAddr, rpte); + } } self.v2p_map.insert(basis_root.name, basis_v2p_map); - // step 5. write the System basis to Flash, at the physical locations noted above + // step 8. write the System basis to Flash, at the physical locations noted above + + + //// LEFT OFF HERE -- rethink how this is done. ideally we are using the PTE mechanism to spread the Basis around. + /// + /// let mut basis_ser = vec![]; for &b in br_slice { basis_ser.push(b) @@ -458,23 +830,14 @@ impl PddbOs { if let Some(system_basis_key) = self.system_basis_key { let key = Key::from_slice(&system_basis_key); let cipher = Aes256GcmSiv::new(key); - let nonce_array = self.gen_nonce(); + let nonce_array = self.nonce_gen(); let nonce = Nonce::from_slice(&nonce_array); - let ciphertext = cipher.encrypt(nonce, &basis_ser[12..]); + let ciphertext = cipher.encrypt(nonce, &basis_ser[size_of::()..]); let ct_to_flash: &[u8] = ciphertext.as_ref().unwrap(); // this now contains the encrypted basis + 16-byte tag at the very end assert!( ( ct_to_flash.len() + basis_root.p_nonce.len()) & (PAGE_SIZE - 1) == 0, "Padding failure during basis serialization!"); // we're now ready to write the encrypted basis to Flash. self.patch_data(&[&nonce_array, ct_to_flash].concat(), 0); - - // now fill in the rest of Flash with random data. This includes filling in the current Basis allocation - // with random data, as that is the "free" state (as well as, at least facially, the "used" state) - let start_offset = PageAlignedPa::from(ct_to_flash.len() + basis_root.p_nonce.len()); - let mut erase_buf: [u8; 4096] = [0; 4096]; - for page_offset in (start_offset.as_u32()..PDDB_A_LEN as u32).step_by(PAGE_SIZE) { - self.trng.fill_bytes(&mut erase_buf); - self.patch_data(&erase_buf, page_offset); - } } else { panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine. } diff --git a/services/pddb/src/backend/murmur3.rs b/services/pddb/src/backend/murmur3.rs new file mode 100644 index 000000000..9019f0079 --- /dev/null +++ b/services/pddb/src/backend/murmur3.rs @@ -0,0 +1,92 @@ +#![cfg_attr(not(target_os = "none"), allow(dead_code))] +// Copyright (c) 2020 Stu Small +// +// Licensed under the Apache License, Version 2.0 +// or the MIT +// license , at your +// option. All files in the project carrying such notice may not be copied, +// modified, or distributed except according to those terms. + + +// This murmur3 code was vendored in on April 28, 2021. We choose to vendor the code for this simple +// non-cryptographic hash in to reduce the number of crate dependencies in the build system. See +// https://github.com/betrusted-io/xous-core/issues/54 for more details on why. +// It also allowed us to adapt the code for our specific no_std needs. + +use core::convert::TryInto; + +const C1: u32 = 0x85eb_ca6b; +const C2: u32 = 0xc2b2_ae35; +const R1: u32 = 16; +const R2: u32 = 13; +const M: u32 = 5; +const N: u32 = 0xe654_6b64; + +pub fn murmur3_32(source: &[u8], seed: u32) -> u32 { + let mut processed = 0; + let mut state = seed; + + assert!(source.len() % 4 == 0, "murmur3_32 can only operate on word-aligned blocks"); + for k8 in source.chunks_exact(4) { + let k = u32::from_le_bytes(k8[..4].try_into().unwrap()); + processed += 4; + state ^= calc_k(k); + state = state.rotate_left(R2); + state = (state.wrapping_mul(M)).wrapping_add(N); + } + finish(state, processed) +} + +fn finish(state: u32, processed: u32) -> u32 { + let mut hash = state; + hash ^= processed as u32; + hash ^= hash.wrapping_shr(R1); + hash = hash.wrapping_mul(C1); + hash ^= hash.wrapping_shr(R2); + hash = hash.wrapping_mul(C2); + hash ^= hash.wrapping_shr(R1); + hash +} + +fn calc_k(k: u32) -> u32 { + const C1: u32 = 0xcc9e_2d51; + const C2: u32 = 0x1b87_3593; + const R1: u32 = 15; + k.wrapping_mul(C1).rotate_left(R1).wrapping_mul(C2) +} + + +#[cfg(test)] +mod tests { + use super::*; + struct Result { + slice: &'static [u32], + hash_32: u32, + } + + #[test] + fn test_static_slices() { + let tests = [ + Result { + slice: &[0x34333231], //"1234", + hash_32: 0x721c5dc3, + }, + Result { + slice: &[0x34333231, 0x38373635], // "12345678", + hash_32: 0x91b313ce, + }, + Result { + slice: &[], + hash_32: 0, + }, + ]; + + for test in &tests { + assert_eq!( + murmur3_32(test.slice, 0), + test.hash_32, + "Failed on slice {:x?}", test.slice + ); + } + } +} diff --git a/services/pddb/src/backend/pagetable.rs b/services/pddb/src/backend/pagetable.rs index 11cbcc3b9..d9d7b131f 100644 --- a/services/pddb/src/backend/pagetable.rs +++ b/services/pddb/src/backend/pagetable.rs @@ -1,7 +1,6 @@ use super::PAGE_SIZE; -use core::num::NonZeroU8; -use core::num::NonZeroU32; -use core::num::NonZeroU64; +use core::mem::size_of; +use aes_gcm_siv::{Nonce, Tag}; use bitflags::bitflags; @@ -66,11 +65,11 @@ pub(crate) struct PageTableInFlash { /// the actual data being retrieved. pub(crate) struct EncryptedPage { /// the nonce is not encrypted - p_nonce: [u8; 12], - /// journal_rev is encrypted and indicates the current journal revision for the block + p_nonce: [u8; size_of::()], + /// 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 - 12 - 16 - 4)], + data: [u8; (PAGE_SIZE - size_of::() - size_of::() - size_of::())], /// tag is the authentication tag. If the page decrypts & authenticates, we know it's a valid data block for us. - p_tag: [u8; 16], + p_tag: [u8; size_of::()], } \ No newline at end of file diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index c617955b0..f44daf568 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -4,7 +4,20 @@ use core::ops::Add; use super::PAGE_SIZE; use crate::SpaceState; use bitfield::bitfield; +use std::hash::{Hash, Hasher}; +/// for the life of me, I can't figure out how to query the AES crate to give me the length of a 256-bit key. +/// I mean, we know what it is, it's well-defined and never changes. But it'd just be nice to you know, +/// derive it from a const or something with symbolic meaning, but the KeySize Trait is buried in some sort +/// of a NewBlock trait and I can't figure out how to access it. Looking at the example code on the AES crate +/// on docs.rs, they just pull the number 16 out of their ass instead of referring to a trait. +/// So, maybe that's just what you're supposed to do. ¯\_(ツ)_/¯ Oddly enough, a BLOCK_SIZE constant /is/ defined, +/// but maybe that's because it's constant regardless of the key size so it's easy to do. +pub(crate) const AES_KEYSIZE: usize = 32; + +/// This has to be manually synchronized with the bit range of the `journal` field below. It doesn't look like +/// there is a good way to automatically derive this. +pub(crate) const PHYS_PAGE_JOURNAL_MAX: u8 = 15; /// We should be able to change this to a u64 and everything should "just work", but /// we'd end up using 2x the amount of data for overhead and bookkeeping. #[cfg(not(feature = "u64_pa"))] @@ -14,16 +27,31 @@ pub type PhysAddr = u64; const BITFIELD_PAGE_WIDTH: usize = (core::mem::size_of::() * 8 - 12); // Physical page information, coded as a bitfield, because space is a premium! bitfield! { - #[derive(Copy, Clone, Hash)] + #[derive(Copy, Clone, Eq)] pub struct PhysPage(PhysAddr); impl Debug; pub page_number, set_page_number: BITFIELD_PAGE_WIDTH - 1, 0; // this is only used by the page table mechanism pub clean, set_clean: BITFIELD_PAGE_WIDTH + 0; + // when set, indicates that the record contents are valid and should be used + // when cleared, the record contents are invalid and should be ignored. // valid is used by both FastSpace and the page table mechanism. Note that we rely upon the mapping of 0->not valid. pub valid, set_valid: BITFIELD_PAGE_WIDTH + 1; // these are only used by the FastSpace mechanism; they have no meaning in other contexts pub u8, from into SpaceState, space_state, set_space_state: BITFIELD_PAGE_WIDTH + 3, BITFIELD_PAGE_WIDTH + 2; + // 4 bits for a journal revision. Intended for the FastSpace mechanism + pub u8, journal, set_journal: BITFIELD_PAGE_WIDTH + 8, BITFIELD_PAGE_WIDTH + 4; +} +// hashes should only key off of the page number, not the metadata +impl Hash for PhysPage { + fn hash(&self, state: &mut H) { + self.page_number().hash(state); + } +} +impl PartialEq for PhysPage { + fn eq(&self, other: &Self) -> bool { + self.page_number() == other.page_number() + } } @@ -85,6 +113,7 @@ impl PageAlignedPa { pub(crate) fn as_u32(&self) -> u32 {self.0 as u32} pub(crate) fn as_u64(&self) -> u64 {self.0 as u64} pub(crate) fn as_usize(&self) -> usize {self.0 as usize} + pub(crate) fn as_phys_addr(&self) -> PhysAddr {self.0} } impl From for PageAlignedPa { fn from(arg: u32) -> Self { @@ -140,4 +169,8 @@ mod tests { assert!(t.0 == 0x2, "polarity of boolean bit is not as expected"); assert!(t.test() == true, "bool getter did not work as expected"); } + fn test_journal_range() { + let pp = PhysPage(u32::MAX); + assert!(pp.journal() == PHYS_PAGE_JOURNAL_MAX, "PHYS_PAGE_JOURNAL_MAX is incorrect"); + } } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 066208d2e..99b1958cf 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -25,6 +25,17 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; /// Open Basis - A Basis that is decryptable and known to the system. /// Closed Basis - A Basis that is not decryptable. It's unknown to the system and potentially treated /// identically to free disk space, e.g., it could be partially overwritten. +/// FSCB - Free Space Commit Buffer. A few pages allocated to tracking a subset of free space, +/// meant to accelerate PDDB operations. Creates a side-channel that can reveal that some activity +/// has happened, but without disclosing what and why. Contains FastSpace and SpaceUpdate records. +/// Frequently updated, so the buffer is slightly oversized, and which sector is "hot" is randomized +/// for wear-levelling. +/// MBBB - Make Before Break Buffer. A set of randomly allocated pages that are a shadow copy +/// of a page table page. If any data exists, its contents override those of a corrupted page table. +/// FastSpace - a collection of random pages that are known to be empty. The number of pages in FastSpace +/// is reduced from the absolute amount of free space available by at least a factor of FSCB_FILL_COEFFICIENT. +/// SpaceUpdate - encrypted patches to the FastSpace table. The FastSpace table is "heavyweight", and would +/// be too expensive to update on every page allocation, so SpaceUpdate is used to patch the FastSpace table. /// /// A `Path` like the following is deconstructed as follows by the PDDB: ///