diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 1a3a1dfcc..bf54a0cdc 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -18,15 +18,14 @@ pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024; /// range for the starting point of a journal number, picked from a random seed /// the goal is to reduce info leakage about the age of structures relative to each other /// in various basis in case of partial disclosure of passwords (especially the system password) +/// The idea is to pick a number that is larger than the wear-out lifetime of the FLASH memory. +/// This memory should wear out after about 100k R/W cycles, so, 100MM is probably a big enough +/// range, while avoiding exhausting a 32-bit count. #[allow(dead_code)] -pub(crate) const JOURNAL_RAND_RANGE: u32 = 1_000_000; -/// fixed offset for system journal entries. If we get "unlucky" and a new system record has -/// a low journal number, and then a basis entry has a lower journal number, we could conclude -/// that this basis data is younger (because journal numbers start at a value and count up). -/// By introducing a fixed offset on the system basis records, a lower offset on a basis entry -/// is no longer such a surprising outcome (I hope...). +pub(crate) const JOURNAL_RAND_RANGE: u32 = 100_000_000; +/// The FSCB has a much smaller journal number (256), so we can't afford to make the starting point as big. #[allow(dead_code)] -pub(crate) const JOURNAL_SYS_OFFSET: u32 = 100_000; +pub(crate) const FSCB_JOURNAL_RAND_RANGE: u8 = 24; /// A number between (0, 1] that defines how many of the "truly free" pages we /// should put into the FSCB. A value of 0.0 is not allowed as that leaves no free pages. diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 3f057d837..355ef9136 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -866,7 +866,7 @@ impl BasisCache { let aad = basis_root.aad(hw.dna()); let pp = basis_v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap()).unwrap(); assert!(pp.valid(), "v2p returned an invalid page"); - let journal_bytes = (0 as u32).to_le_bytes(); + let journal_bytes = (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes(); let slice_iter = journal_bytes.iter() // journal rev .chain(basis_root.as_ref().iter()); @@ -1405,7 +1405,11 @@ impl BasisCacheEntry { } else { log::trace!("existing data invalid, creating a new page"); // the existing data was invalid (this happens e.g. on the first time a dict is created). Just overwrite the whole page. - vec![0u8; VPAGE_SIZE + size_of::()] + let mut d = vec![0u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(d[..size_of::()].iter_mut()) { + *dst = src; + } + d }; for (index, stride) in page[size_of::()..].chunks_mut(DK_STRIDE).enumerate() { if let Some(elem) = dk_vpage.elements[index] { @@ -1469,6 +1473,9 @@ impl BasisCacheEntry { journal_bytes.iter() // journal rev .chain(basis_root.as_ref().iter()); let mut block = [0 as u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(block[..size_of::()].iter_mut()) { + *dst = src; + } for (&src, dst) in slice_iter.zip(block.iter_mut()) { *dst = src; } @@ -1567,160 +1574,3 @@ impl Default for BasisRootName { BasisRootName([0; BASIS_NAME_LEN]) } } - -/* this is retired, but kept around because it's a cool idea that could be useful later. - the code below is how the encryptor iterator would be called, followed by the iterator itself. - The idea is to create a thing that can chain iterators and generate encrypted blocks. Could - be useful for e.g. encrypting long keys and stuff...? - - The code was retired only because we decided to simplify the BasisRoot and it always fits in - one page, so all this iterator is just baroque and frankly kind of pointless in that context. - - if let Some(syskey) = self.system_basis_key { - let key = Key::from_slice(&syskey); - let cipher = Aes256GcmSiv::new(key); - let basis_encryptor = BasisEncryptor::new( - &basis_root, - self.dna, - cipher, - 0, - Rc::clone(&self.entropy), - ); - 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) { - Some(pp) => { - self.patch_data(&ppage_data, pp.page_number() * PAGE_SIZE as u32); - } - None => { - log::error!("Previously allocated page was not found in our map!"); - panic!("Inconsistent internal state"); - } - } - } - } else { - log::error!("System key was not found, but it should be present!"); - panic!("Inconsistent internal state"); - } -/// 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. -/// -/// This routine is a bit heavyweight because we were originally going to -/// attach the dictionary data to the Basis Root but have since decided against that. -#[repr(C)] -pub(crate) struct BasisEncryptor<'a> { - root: &'a BasisRoot, - cipher: Aes256GcmSiv, - cur_vpage: usize, - aad: Vec::, - journal_rev: JournalType, - entropy: Rc>, -} -impl<'a> BasisEncryptor<'a> { - pub(crate) fn new(root: &'a BasisRoot, dna: u64, cipher: Aes256GcmSiv, rev: JournalType, entropy: Rc>) -> Self { - let mut aad = Vec::::new(); - aad.extend_from_slice(&root.name.0); - aad.extend_from_slice(&PDDB_VERSION.to_le_bytes()); - aad.extend_from_slice(&dna.to_le_bytes()); - - log::info!("aad: {:?}", aad); - - BasisEncryptor { - root, - cur_vpage: 0, - aad, - cipher, - journal_rev: rev, - entropy, - } - } -} - -pub(crate) struct BasisEncryptorIter<'a> { - basis_data: BasisEncryptor<'a>, - // the virtual address of the currently requested iteration - vaddr: usize, -} -impl<'a> IntoIterator for BasisEncryptor<'a> { - type Item=[u8; PAGE_SIZE]; - type IntoIter=BasisEncryptorIter<'a>; - fn into_iter(self) -> BasisEncryptorIter<'a> { - BasisEncryptorIter { - basis_data: self, - vaddr: 0, - } - } -} -impl<'a> Iterator for BasisEncryptorIter<'a> { - type Item = [u8; PAGE_SIZE]; - - fn next<'s>(&'s mut self) -> Option { - if self.vaddr < VPAGE_SIZE { // legacy from when we tried to have a multi-page basis area - let mut block = [0 as u8; VPAGE_SIZE + size_of::()]; - let block_iter = block.iter_mut(); - - let journal_bytes = self.basis_data.journal_rev.to_le_bytes(); - let slice_iter = - journal_bytes.iter() // journal rev - .chain(self.basis_data.root.as_ref().iter() - ).skip(self.vaddr); - - // note that in the case that we've already serialized the journal, basis, and dictionary, this will produce nothing - let mut written = 0; - for(&src, dst) in slice_iter.zip(block_iter) { - *dst = src; - written += 1; - } - // which allows this to correctly pad out the rest of the prealloc region with 0's. - while written < block.len() { - block[written] = 0; - written += 1; - } - - let nonce_array = self.basis_data.entropy.borrow_mut().get_nonce(); - let nonce = Nonce::from_slice(&nonce_array); - let ciphertext = self.basis_data.cipher.encrypt( - &nonce, - Payload { - aad: &self.basis_data.aad, - msg: &block, - } - ).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 - } - } -} -*/ -/* -#[cfg(test)] -mod tests { - use super::*; - #[test] - // turns out these test are invalid because the structures always end up being aligned - fn test_dict_size() { - print!("dict size: {}", core::mem::size_of::()); - assert!(core::mem::size_of::() == 127, "Dictionary is not an even multiple of the VPAGE size"); - } - #[test] - fn test_key_size() { - print!("key size: {}", core::mem::size_of::()); - assert!(core::mem::size_of::() == 127, "Key descriptor is not an even multiple of the VPAGE size"); - } -} -*/ \ No newline at end of file diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index e9dba38b6..1839c4ffe 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -459,7 +459,11 @@ impl DictCacheEntry { // in response to this, we allocate a fresh page of 0's. log::debug!("Reserved and uninitialized page encountered updating large block: {} {:x}..{}->{}; update @{}..{}", name, kcache.start, kcache.len, kcache.reserved, offset, data.len()); - vec![0u8; VPAGE_SIZE + size_of::()] + let mut d = vec![0u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(d[..size_of::()].iter_mut()) { + *dst = src; + } + d } }; if offset > 0 { @@ -482,6 +486,9 @@ impl DictCacheEntry { if data.len() - written >= VPAGE_SIZE { // overwrite whole pages without decryption let mut block = [0u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(block[..size_of::()].iter_mut()) { + *dst = src; + } for (&src, dst) in data[written..].iter().zip(block[size_of::()..].iter_mut()) { *dst = src; written += 1; @@ -498,6 +505,9 @@ impl DictCacheEntry { } else { // page didn't exist, initialize it with 0's and merge the tail end. let mut pt_data = [0u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(pt_data[..size_of::()].iter_mut()) { + *dst = src; + } for (&src, dst) in data[written..].iter().zip(pt_data[size_of::()..].iter_mut()) { *dst = src; written += 1; @@ -799,6 +809,9 @@ impl DictCacheEntry { // 5. sync the page tables // This implementation just skips to step 3. let mut page = [0u8; VPAGE_SIZE + size_of::()]; + for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(page[..size_of::()].iter_mut()) { + *dst = src; + } let mut pool_offset = 0; // visit the entries in arbitrary order, but back them in optimally tightly packed for key_name in &entry.contents { diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 34bac631b..30585085f 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -208,6 +208,12 @@ impl PddbOs { pub(crate) fn trng_slice(&mut self, slice: &mut [u8]) { self.entropy.borrow_mut().get_slice(slice); } + pub(crate) fn trng_u32(&mut self) -> u32 { + self.entropy.borrow_mut().get_u32() + } + pub(crate) fn trng_u8(&mut self) -> u8 { + self.entropy.borrow_mut().get_u8() + } pub(crate) fn timestamp_now(&self) -> u64 {self.tt.elapsed_ms()} /// patches data at an offset starting from the data physical base address, which corresponds @@ -625,6 +631,7 @@ impl PddbOs { let mut page_pool = Vec::::new(); for page in free_pool { let mut pp = PhysPage(0); + pp.set_journal(self.trng_u8() % FSCB_JOURNAL_RAND_RANGE); pp.set_page_number(page as PhysAddr); pp.set_space_state(SpaceState::Free); pp.set_valid(true); @@ -971,6 +978,9 @@ impl PddbOs { let mut fast_space = FastSpace { free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN], }; + for pp in fast_space.free_pool.iter_mut() { + pp.set_journal(self.trng_u8() % FSCB_JOURNAL_RAND_RANGE) + } for (&src, dst) in free_pool.iter().zip(fast_space.free_pool.iter_mut()) { *dst = src; } @@ -997,6 +1007,9 @@ impl PddbOs { let mut fast_space = FastSpace { free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN], }; + for pp in fast_space.free_pool.iter_mut() { + pp.set_journal(self.trng_u8() % FSCB_JOURNAL_RAND_RANGE) + } // regenerate from the existing fast space cache for (&src, dst) in self.fspace_cache.iter().zip(fast_space.free_pool.iter_mut()) { *dst = src; @@ -1264,7 +1277,7 @@ impl PddbOs { let pp = basis_v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap()) .expect("Internal consistency error: Basis exists, but its root map was not allocated!"); assert!(pp.valid(), "v2p returned an invalid page"); - let journal_bytes = (0 as u32).to_le_bytes(); + let journal_bytes = (self.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes(); let slice_iter = journal_bytes.iter() // journal rev .chain(basis_root.as_ref().iter());