diff --git a/services/pddb/src/backend.rs b/services/pddb/src/backend.rs index 448f533ea..4a754b899 100644 --- a/services/pddb/src/backend.rs +++ b/services/pddb/src/backend.rs @@ -11,6 +11,8 @@ pub use types::*; // local to the backend mod murmur3; pub(crate) use murmur3::*; +mod trngpool; +pub(crate) use trngpool::*; #[cfg(any(target_os = "none", target_os = "xous"))] mod hw; diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index a1516b854..f2836f53d 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -1,17 +1,90 @@ use crate::api::*; use super::*; +use core::cell::RefCell; +use std::rc::Rc; use core::num::NonZeroU64; use core::ops::{Deref, DerefMut}; use core::{mem, slice}; use core::mem::size_of; -use aes_gcm_siv::Nonce; +use std::convert::TryInto; +use aes_gcm_siv::{Aes256GcmSiv, Nonce}; +use aes_gcm_siv::aead::{Aead, Payload}; + -pub(crate) const FREE_CACHE_SIZE: usize = 16; pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN]; -/// In basis space, the BasisRoot is located at 0 -/// The first 4GiB is reserved for the Basis Root. -/// Keys begin at the next 4GiB. + +/// Takes in the constituents of the Basis area, and encrypts them into +/// PAGE_SIZE blocks. Can be called as an iterator, or as a single-shot +/// for a given offset. Requires a cipher that is pre-keyed with the encryption +/// key, and the DNA code from the FPGA as a `u64`. This function generates +/// the AAD based off of the DNA code + version of PDDB + Basis Name. +/// +/// The iteration step is in VPAGE units within the virtual space, but +/// it always returns a full PAGE_SIZE block. This object will handle +/// padding of the very last block so the encrypted data fills up a full +/// PAGE_SIZE; request for blocks beyond the length of the Basis pre-alloc +/// region will return None. +#[repr(C)] +pub(crate) struct BasisEncrypter<'a> { + root: &'a BasisRoot, + dicts: &'a [DictPointer], + cipher: Aes256GcmSiv, + cur_vpage: usize, + aad: Vec::, + journal_rev: JournalType, + entropy: Rc>, +} +impl<'a> BasisEncrypter<'a> { + pub fn new(root: &'a BasisRoot, dicts: &'a [DictPointer], dna: u64, cipher: Aes256GcmSiv, rev: JournalType, entropy: Rc>) -> Self { + let mut aad = Vec::::new(); + aad.extend_from_slice(&root.name); + aad.extend_from_slice(&PDDB_VERSION.to_le_bytes()); + aad.extend_from_slice(&dna.to_le_bytes()); + + BasisEncrypter { + root, + dicts, + cur_vpage: 0, + aad, + cipher, + journal_rev: rev, + entropy, + } + } +} +impl<'a> Iterator for BasisEncrypter<'a> { + type Item = [u8; PAGE_SIZE]; + + fn next<'s>(&'s mut self) -> Option { + if self.cur_vpage == 0 { + let mut block = [0 as u8; VPAGE_SIZE]; + for (&src, dst) in self.journal_rev.to_le_bytes().iter().zip(block.iter_mut()) { + *dst = src; + } + for (&src, dst) in self.root.deref().iter().zip(block[size_of::()..].iter_mut()) { + *dst = src; + } + let nonce_array = self.entropy.borrow_mut().get_nonce(); + let nonce = Nonce::from_slice(&nonce_array); + let ciphertext = self.cipher.encrypt( + &nonce, + Payload { + aad: &self.aad, + msg: &block, + } + ).unwrap(); + self.cur_vpage += 1; + Some([&nonce_array, ciphertext.deref()].concat().try_into().unwrap()) + } else { + None + } + } +} + +/// In basis space, the BasisRoot is located at VPAGE #1 (VPAGE #0 is always invalid). +/// The first 4GiB is reserved for the Basis Root + Dictionary slice. +/// Key storage begin at 4GiB. /// AAD associated with the BasisRoot consist of a bytewise concatenation of: /// - Basis name /// - version number (should match version inside; complicates downgrade attacks) @@ -23,26 +96,18 @@ pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN]; /// much longer. #[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; size_of::()], // everything below here is encrypted using AES-GCM-SIV pub(crate) magic: [u8; 4], pub(crate) version: u16, - pub(crate) journal_rev: u32, pub(crate) name: BasisRootName, /// increments every time the BasisRoot is modified. This field must saturate, not roll over. pub(crate) age: u32, - /* - /// a cache of up FREE_CACHE_SIZE indicating the location of free space for use by basis - /// functions, such as adding growing the size of this structure, adding more dictionaries, - /// adding keys to dictionaries, or extending existing keys. This saves from having to do - /// frequent free space search/compaction operations on memory during writes and updates. - pub(crate) free_cache: [Option; FREE_CACHE_SIZE], - */ /// "open end" of the pre-allocated space for the Basis. All Basis data must exist in an extent that is /// less than this value. This can be grown and shrunk with allocation and compaction processes. pub(crate) prealloc_open_end: PageAlignedVa, pub(crate) num_dictionaries: u32, + /// virtual address pointer to the start of the dictionary record + pub(crate) dict_ptr: Option, // dict_slice: [DictPointer; num_dictionaries], // DictPointers + num_dictionaries above can be turned into a dict_slice ////// the following records are appended by the Serialization routine // pad: [u8], // padding out to the next 4096-byte block less 16 bytes @@ -56,21 +121,19 @@ impl BasisRoot { /// if you get thousands of Dictionaries. Note that the intent is to have typcially no /// more than a couple dozen dictionaries; if you want to store a lot of different records, /// you can create thousands of Keys more efficiently, than you can dictionaries. - pub(crate) fn len_pages(&self) -> usize { + pub(crate) fn len_vpages(&self) -> usize { let min_len = core::mem::size_of::() - + ((self.num_dictionaries as usize) - * core::mem::size_of::()) - + core::mem::size_of::(); - if (min_len & (1 - PAGE_SIZE)) == 0 { - min_len / PAGE_SIZE + + ((self.num_dictionaries as usize) * core::mem::size_of::()); + if min_len % VPAGE_SIZE == 0 { + min_len / VPAGE_SIZE } else { - min_len / PAGE_SIZE + 1 + min_len / VPAGE_SIZE + 1 } } /// Number of bytes needed to pad between the length of the BasisRoot structure and the plaintext /// tag that will get appended to the end pub(crate) fn padding_count(&self) -> usize { - self.len_pages() * PAGE_SIZE - + self.len_vpages() * VPAGE_SIZE - (core::mem::size_of::() + ((self.num_dictionaries as usize) * core::mem::size_of::()) ) @@ -99,7 +162,7 @@ impl DerefMut for BasisRoot { pub(crate) struct DictPointer { name: [u8; PDDB_MAX_DICT_NAME_LEN], - age: u32, // increment every time the dictionary pointer is modified. + age: u32, // increment every time the dictionary pointer is modified. Used to guide memory compaction. addr: u64, // the virtual address of the dictionary } diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 1305cd017..94add7e8d 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -4,7 +4,6 @@ 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 aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray}; use root_keys::api::{AesRootkeyType, Block}; use core::ops::{Deref, DerefMut}; @@ -23,7 +22,10 @@ pub(crate) const MBBB_PAGES: usize = 10; pub(crate) const FSCB_PAGES: usize = 16; pub(crate) const INITIAL_BASIS_ALLOC: usize = 16; +/// size of a physical page pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize; +/// size of a virtual page -- after the AES encryption and journaling overhead is subtracted +pub const VPAGE_SIZE: usize = PAGE_SIZE - size_of::() - size_of::() - size_of::(); #[repr(C, packed)] // this can map directly into Flash pub(crate) struct StaticCryptoData { @@ -48,7 +50,6 @@ pub(crate) struct PddbOs { spinor: spinor::Spinor, rootkeys: root_keys::RootKeys, pddb_mr: xous::MemoryRange, - trng: trng::Trng, /// 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. @@ -68,15 +69,14 @@ pub(crate) struct PddbOs { 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::, /// a cached copy of the FPGA's DNA ID, used in the AAA records. dna: u64, + /// reference to a TrngPool object that's shared among all the hardware functions + entropy: Rc>, } impl PddbOs { - pub fn new() -> PddbOs { + pub fn new(trngpool: Rc>) -> PddbOs { let xns = xous_names::XousNames::new().unwrap(); let pddb = xous::syscall::map_memory( xous::MemoryAddress::new(xous::PDDB_LOC as usize), @@ -91,16 +91,11 @@ impl PddbOs { 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); - let mut trng = trng::Trng::new(&xns).unwrap(); - let mut cache: [u8; 8192] = [0; 8192]; - trng.fill_bytes(&mut cache); - let llio = llio::Llio::new(&xns).unwrap(); 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, - trng, pt_phys_base: PageAlignedPa::from(0 as u32), key_phys_base, mbbb_phys_base, @@ -111,8 +106,8 @@ impl PddbOs { fspace_cache: HashSet::::new(), fspace_log_addrs: Vec::::new(), fspace_log_next_addr: None, - e_cache: cache.to_vec(), dna: llio.soc_dna().unwrap(), + entropy: trngpool, } } @@ -196,43 +191,6 @@ impl PddbOs { self.system_basis_key = Some(system_key); } } - fn ensure_entropy(&mut self, amount: usize) { - if self.e_cache.len() < amount { - let mut cache: [u8; 8192] = [0; 8192]; - self.trng.fill_bytes(&mut cache); - self.e_cache.extend_from_slice(&cache); - } - } - fn trng_cache_u8(&mut self) -> u8 { - self.ensure_entropy(1); - self.e_cache.pop().unwrap() - } - fn trng_cache_u32(&mut self) -> u32 { - self.ensure_entropy(4); - let ret = u32::from_le_bytes(self.e_cache[self.e_cache.len() - 4..].try_into().unwrap()); - self.e_cache.truncate(self.e_cache.len() - 4); - ret - } - fn trng_cache_u64(&mut self) -> u64 { - self.ensure_entropy(8); - let ret = u64::from_le_bytes(self.e_cache[self.e_cache.len() - 8..].try_into().unwrap()); - self.e_cache.truncate(self.e_cache.len() - 8); - ret - } - fn trng_cache_slice(&mut self, bucket: &mut [u8]) { - self.ensure_entropy(bucket.len()); - for (src, dst) in self.e_cache.drain( - (self.e_cache.len() - bucket.len()).. - ).zip(bucket.iter_mut()) { - *dst = src; - } - } - /// generates a 96-bit nonce using the CPRNG - 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 @@ -257,7 +215,7 @@ 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.nonce_gen(); + let nonce_array = self.entropy.borrow_mut().get_nonce(); 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, @@ -274,7 +232,7 @@ impl PddbOs { // 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; + let dest_page = self.entropy.borrow_mut().get_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 @@ -350,7 +308,7 @@ impl PddbOs { } else { // page is free, but we have no space in the pool. // pick a random page from the pool, and replace it with the current page - let index = self.trng_cache_u32() as usize % free_pool.len(); + let index = self.entropy.borrow_mut().get_u32() as usize % free_pool.len(); free_pool[index] = page_candidate; } } @@ -362,7 +320,7 @@ impl PddbOs { // https://github.com/rust-random/rand/blob/0f4fc6b4c303696bd5f8765a375162ac7142b1df/src/seq/mod.rs#L586-L592 for i in (1..free_pool.len()).rev() { // invariant: elements with index > i have been locked in place. - free_pool.swap(i, self.trng_cache_u32() as usize % (i+1)); + free_pool.swap(i, self.entropy.borrow_mut().get_u32() as usize % (i+1)); } log::info!("free_pool initial count: {}", free_pool.len()); @@ -520,7 +478,7 @@ impl PddbOs { 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(); + let random_index = self.entropy.borrow_mut().get_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 { @@ -575,7 +533,7 @@ impl PddbOs { // 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(); + let random_index = self.entropy.borrow_mut().get_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 @@ -607,7 +565,7 @@ impl PddbOs { 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 update = SpaceUpdate::new(self.entropy.borrow_mut().get_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; @@ -672,7 +630,7 @@ impl PddbOs { /// 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 { + pub(crate) 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. @@ -709,7 +667,7 @@ impl PddbOs { // 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.entropy.borrow_mut().get_slice(&mut temp); self.patch_pagetable(&temp, page as u32); } if size_of::() & (PAGE_SIZE - 1) != 0 { @@ -717,7 +675,7 @@ impl PddbOs { 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()); + temp.push(self.entropy.borrow_mut().get_u8()); } self.patch_pagetable(&temp, remainder_start as u32); } @@ -729,7 +687,7 @@ impl PddbOs { //} 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); + self.entropy.borrow_mut().get_slice(&mut system_basis_key); 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); @@ -744,8 +702,8 @@ impl PddbOs { for (&src, dst) in basis_key_enc.iter().zip(crypto_keys.system_key.iter_mut()) { *dst = src; } - self.trng.fill_bytes(&mut crypto_keys.salt_base); - self.trng.fill_bytes(&mut crypto_keys.reserved); + self.entropy.borrow_mut().get_slice(&mut crypto_keys.salt_base); + self.entropy.borrow_mut().get_slice(&mut crypto_keys.reserved); 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. @@ -767,7 +725,7 @@ impl PddbOs { // 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); + self.entropy.borrow_mut().get_slice(&mut temp); if (offset / PAGE_SIZE) % 64 == 0 { log::info!("Crytpographic 'erase': {}/{}", offset, PDDB_A_LEN); } @@ -785,62 +743,83 @@ impl PddbOs { *dst = src; } let mut basis_root = BasisRoot { - p_nonce: self.nonce_gen(), magic: api::PDDB_MAGIC, version: api::PDDB_VERSION, - journal_rev: 0, name, age: 0, num_dictionaries: 0, - prealloc_open_end: PageAlignedVa::from(INITIAL_BASIS_ALLOC * PAGE_SIZE), + prealloc_open_end: PageAlignedVa::from(INITIAL_BASIS_ALLOC * VPAGE_SIZE), + dict_ptr: None, }; - // 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 - 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() { - 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); + { // this bit of code could be the start of an "allocate space for basis" routine, but we need to think about how overwritten pages are handled... + let mut basis_v2p_map = HashMap::::new(); + for virt_page in 1..=basis_root.prealloc_open_end.as_vpage_num() { + 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(VirtAddr::new((virt_page * VPAGE_SIZE) as u64).unwrap(), rpte); + } } + self.v2p_map.insert(basis_root.name, basis_v2p_map); } - self.v2p_map.insert(basis_root.name, basis_v2p_map); // step 8. write the System basis to Flash, at the physical locations noted above + // extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page + if let Some(syskey) = self.system_basis_key { + let key = Key::from_slice(&system_basis_key); + let cipher = Aes256GcmSiv::new(key); + let basis_encrypter = BasisEncrypter::new( + &basis_root, + &[], + self.dna, + cipher, + 0, + Rc::clone(&self.entropy), + ); + for ppage in basis_encrypter.into_iter() { + self.patch_data(&ppage, 0); + } + } else { + panic!("Inconsistent internal state"); + } - - //// LEFT OFF HERE -- rethink how this is done. ideally we are using the PTE mechanism to spread the Basis around. - /// - /// + /* + let br_slice: &[u8] = basis_root.deref(); + // copy the basis contents let mut basis_ser = vec![]; for &b in br_slice { basis_ser.push(b) } + // fill out any padding based on the pre-alloc space for _ in 0..basis_root.padding_count() { basis_ser.push(0) } + + let basis = Basis::new(basis_root.deref(), &[]); + for vpage in basis { + log::info!("vpage: {:?}", vpage); + } + // basis_ser can now be passed to an encryption function 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_array = self.entropy.borrow_mut().get_nonce(); let nonce = Nonce::from_slice(&nonce_array); 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!"); + // FIX THIS 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); } else { panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine. - } + }*/ // step 8. generate & write initial page table entries // page table organization: diff --git a/services/pddb/src/backend/murmur3.rs b/services/pddb/src/backend/murmur3.rs index 9019f0079..91c69560b 100644 --- a/services/pddb/src/backend/murmur3.rs +++ b/services/pddb/src/backend/murmur3.rs @@ -68,11 +68,11 @@ mod tests { fn test_static_slices() { let tests = [ Result { - slice: &[0x34333231], //"1234", + slice: &[0x31, 0x32, 0x33, 0x34], //"1234", hash_32: 0x721c5dc3, }, Result { - slice: &[0x34333231, 0x38373635], // "12345678", + slice: &[0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38], // "12345678", hash_32: 0x91b313ce, }, Result { diff --git a/services/pddb/src/backend/pagetable.rs b/services/pddb/src/backend/pagetable.rs index d9d7b131f..0f495ba26 100644 --- a/services/pddb/src/backend/pagetable.rs +++ b/services/pddb/src/backend/pagetable.rs @@ -19,7 +19,7 @@ impl Default for PtFlags { fn default() -> PtFlags {PtFlags::INVALID} } -/// A Page Table Entry. Must be equual in length to one AES block size (128 bits). +/// 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, diff --git a/services/pddb/src/backend/trngpool.rs b/services/pddb/src/backend/trngpool.rs new file mode 100644 index 000000000..ce0116094 --- /dev/null +++ b/services/pddb/src/backend/trngpool.rs @@ -0,0 +1,58 @@ +use core::mem::size_of; +use aes_gcm_siv::Nonce; +use rand_core::{CryptoRng, RngCore}; +use std::convert::TryInto; + +/// Crate-shared resource for TRNGs. +pub(crate) struct TrngPool { + trng: trng::Trng, + /// 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 hardware server. + e_cache: Vec::, +} +impl TrngPool { + pub fn new() -> Self { + let xns = xous_names::XousNames::new().unwrap(); + let mut trng = trng::Trng::new(&xns).unwrap(); + let mut cache: [u8; 8192] = [0; 8192]; + trng.fill_bytes(&mut cache); + TrngPool { trng, e_cache: cache.to_vec() } + } + pub(crate) fn ensure_entropy(&mut self, amount: usize) { + if self.e_cache.len() < amount { + let mut cache: [u8; 8192] = [0; 8192]; + self.trng.fill_bytes(&mut cache); + self.e_cache.extend_from_slice(&cache); + } + } + pub(crate) fn get_u8(&mut self) -> u8 { + self.ensure_entropy(1); + self.e_cache.pop().unwrap() + } + pub(crate) fn get_u32(&mut self) -> u32 { + self.ensure_entropy(4); + let ret = u32::from_le_bytes(self.e_cache[self.e_cache.len() - 4..].try_into().unwrap()); + self.e_cache.truncate(self.e_cache.len() - 4); + ret + } + pub(crate) fn get_u64(&mut self) -> u64 { + self.ensure_entropy(8); + let ret = u64::from_le_bytes(self.e_cache[self.e_cache.len() - 8..].try_into().unwrap()); + self.e_cache.truncate(self.e_cache.len() - 8); + ret + } + pub(crate) fn get_slice(&mut self, bucket: &mut [u8]) { + self.ensure_entropy(bucket.len()); + for (src, dst) in self.e_cache.drain( + (self.e_cache.len() - bucket.len()).. + ).zip(bucket.iter_mut()) { + *dst = src; + } + } + /// generates a 96-bit nonce using the TRNG + pub(crate) fn get_nonce(&mut self) -> [u8; size_of::()] { + let mut nonce: [u8; size_of::()] = [0; size_of::()]; + self.get_slice(&mut nonce); + nonce + } +} \ No newline at end of file diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index f44daf568..21bc2acbc 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -1,7 +1,7 @@ use core::num::{NonZeroU32, NonZeroU64}; use core::convert::TryFrom; use core::ops::Add; -use super::PAGE_SIZE; +use super::{PAGE_SIZE, VPAGE_SIZE}; use crate::SpaceState; use bitfield::bitfield; use std::hash::{Hash, Hasher}; @@ -54,56 +54,72 @@ impl PartialEq for PhysPage { } } +/// Storage for journal revisions. +pub type JournalType = u32; -pub type VirtAddr = u64; +/// A Virtual Address is 48 bits long. The top 16 bits are required to be blank +/// so that they may be used as flags in the on-disk storage format. +/// Virtual pages are shorter than physical pages, due to the overhead of +/// the nonce + tag + journal entry used to store data on disk. +/// We make the VirtAddr a NonZeroU64 so that we can apply a None option to it "for free" +/// in packed disk representations. +pub type VirtAddr = NonZeroU64; #[derive(Copy, Clone)] pub(crate) struct PageAlignedVa(VirtAddr); impl PageAlignedVa { pub(crate) fn as_u32(&self) -> u32 { - if self.0 <= u32::MAX as VirtAddr { - self.0 as u32 + if self.0 <= VirtAddr::new(u32::MAX as u64).unwrap() { + self.0.get() as u32 } else { - panic!("This PageAlignedVa would not fit into a PageAlignedPa"); + panic!("PageAlignedVa would not fit into a 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_u64(&self) -> u64 {self.0.get()} + pub(crate) fn as_usize(&self) -> usize {self.0.get() as usize} + /// This will turn a PageAlignedVa into a page number + pub(crate) fn as_vpage_num(&self) -> usize { + // we're page-aligned, so we don't have to deal with remainders. This should divide cleanly. + self.0.get() as usize / VPAGE_SIZE + } } impl From for PageAlignedVa { fn from(arg: u64) -> Self { - if arg & (PAGE_SIZE as u64 - 1) == 0 { - PageAlignedVa(arg & !(PAGE_SIZE as VirtAddr - 1)) + if arg % VPAGE_SIZE as u64 == 0 { + PageAlignedVa(VirtAddr::new(arg / VPAGE_SIZE as u64).unwrap()) } else { - PageAlignedVa((arg & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr) + PageAlignedVa(VirtAddr::new(arg / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap()) } } } +/* This is a bad idea -- because really, it needs to be a page table walk. It's not this simple. impl From for PageAlignedVa { fn from(arg: PageAlignedPa) -> Self { PageAlignedVa(arg.0 as VirtAddr) } // already aligned, and it fits. -} +}*/ impl From for PageAlignedVa { fn from(arg: u32) -> Self { - if arg & (PAGE_SIZE as u32 - 1) == 0 { - PageAlignedVa(arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + if arg as usize % VPAGE_SIZE == 0 { + PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64).unwrap()) } else { - PageAlignedVa((arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr) + PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap()) } } } impl From for PageAlignedVa { fn from(arg: usize) -> Self { - if arg & (PAGE_SIZE - 1) == 0 { - PageAlignedVa(arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + if arg % VPAGE_SIZE == 0 { + PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64).unwrap()) } else { - PageAlignedVa((arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr) + PageAlignedVa(VirtAddr::new(arg as u64 / VPAGE_SIZE as u64 + VPAGE_SIZE as u64).unwrap()) } } } impl Add for PageAlignedVa { type Output = PageAlignedVa; fn add(self, other: PageAlignedVa) -> PageAlignedVa { - PageAlignedVa(self.0 + other.0) + let a = self.0.get(); + let b = other.0.get(); + PageAlignedVa(VirtAddr::new(a.saturating_add(b)).unwrap()) } } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 99b1958cf..38f477a1a 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -11,6 +11,8 @@ use backend::*; use num_traits::*; use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; +use core::cell::RefCell; +use std::rc::Rc; /// PDDB - Plausibly Deniable DataBase /// @@ -55,7 +57,6 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; /// /// /// Threat model: -/// /// The user is forced to divulge "all the Basis passwords" on the device, through /// coercion, blackmail, subpoena, customs checkpoint etc. The adversary has physical /// access to the device, and is able to take a static disk image; they may even have @@ -80,9 +81,35 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; /// a problem handled by the OS, and it is up to the user to not type secret passwords /// in areas that may be under camera surveillance. /// +/// Auditor Notices: +/// There's probably a lot of things the PDDB does wrong. Here's a list of some things +/// to worry about: +/// - The device RootKeys shares one salt across all of its keys, instead of a per-key salt. +/// Currently there are only two keys in the RootKey store sharing the one salt. The concern +/// is that a migration to an ASIC base design would make eFuse bits scarce, so, the initial +/// draft of the RootKeys shares one salt to maximize key capacity. The per-key salt is slightly +/// modified by adding the key index to the salt. This isn't meant to be a robust mitigation: +/// it just prevents a naive rainbow attack from re-using its table. +/// - The bcrypt() implementation is vendored in from a Rust bcrypt crate. It hasn't been audited. +/// - The COST of 7 for bcrypt is relatively low by today's standards (should be 10). However, +/// we can't raise the cost to 10 because our CPU is slower than most modern x86 devices. There is +/// an open issue to try to improve this with hardware acceleration. The mitigation is to use +/// a longer passphrase instead of a 12 or 14-character password. +/// - The RootKey is used to decrypt a locally stored System Basis key. The key is encrypted using +/// straight AES-256 with no authentication. +/// - Secret basis keys are not stored anywhere on the device. They are all derived from a password +/// using bcrypt. The salt for the password is drawn from a "salt pool", whose index is derived from +/// a weak hash of the password itself. This means there is a chance that a salt gets re-used. However, +/// we do not store per-password salts because the existence of the salt would betray the existence of +/// a password. +/// - Disk blocks are encrypted & authenticated on a page-by-page basis using AES-GCM-SIV, with a 256-bit key. +/// - Page table entries and space update entries are salted & hinted using a 64-bit nonce + weak checksum. +/// There is a fairly high chance of a checksum collision, thus PTE decrypts are regarded as advisory and +/// not final; the PTE is not accepted as authentic until the AES-GCM-SIV behind it checks out. Free space +/// entries have no such protection, which means there is a slight chance of data loss due to a checksum +/// collision on the free space entries. /// /// General Operation: -/// /// The initial Basis is known as the "System" Basis. It's a low-security framework basis /// onto which all other Basis overlay. The initial set of Dictionaries, along with their /// Key/Value pairs, are available to any and all processes. @@ -312,7 +339,11 @@ fn xmain() -> ! { log::trace!("ready to accept requests"); - let pddb_os = PddbOs::new(); + // 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())); + + // OS-specific PDDB driver + let pddb_os = PddbOs::new(Rc::clone(&entropy)); // register a suspend/resume listener let sr_cid = xous::connect(pddb_sid).expect("couldn't create suspend callback connection");