diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 898a75479..a5f4ec55c 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -13,7 +13,7 @@ pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42]; pub(crate) const PDDB_VERSION: u16 = 0x00_00; /// PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing #[allow(dead_code)] -pub(crate) const PDDB_A_LEN: usize = 16 * 1024 * 1024; +pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024; /// 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/hosted.rs b/services/pddb/src/backend/hosted.rs index 486b62bc2..9fabbd804 100644 --- a/services/pddb/src/backend/hosted.rs +++ b/services/pddb/src/backend/hosted.rs @@ -37,6 +37,10 @@ fn flashmem() -> &'static mut FlashSingleton { } } +pub struct KeyExport { + pub basis_name: [u8; 64], + pub key: [u8; 32], +} pub struct EmuStorage { } impl EmuStorage { @@ -55,12 +59,13 @@ impl EmuStorage { f.write_all(flashmem().memory.as_slice()).unwrap(); f.flush().unwrap(); } - pub fn dump_keys(&self, known_keys: &[[u8; 32]]) { + pub fn dump_keys(&self, known_keys: &[KeyExport]) { //new().write(true).truncate(true).open let mut f = File::create("../tools/pddb.key").unwrap(); f.write_all(&(known_keys.len() as u32).to_le_bytes()).unwrap(); for key in known_keys { - f.write_all(key).unwrap(); + f.write_all(&key.basis_name).unwrap(); + f.write_all(&key.key).unwrap(); } f.flush().unwrap(); } diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 1769ac086..f7543749c 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -150,9 +150,21 @@ impl PddbOs { #[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn dbg_dump(&self) { self.pddb_mr.dump_fs(); + let mut export = Vec::::new(); if let Some(key) = self.system_basis_key { - self.pddb_mr.dump_keys(&[key]); + log::info!("written key: {:x?}", key); + let mut name = [0 as u8; 64]; + for (&src, dst) in PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut()) { + *dst = src; + } + export.push( + KeyExport { + basis_name: name, + key, + } + ); } + self.pddb_mr.dump_keys(&export); } #[cfg(any(target_os = "none", target_os = "xous"))] pub fn dbg_dump(&self) { @@ -279,7 +291,13 @@ impl PddbOs { msg: fs_ser, aad: &aad, }; + log::info!("key: {:x?}", key); + log::info!("nonce: {:x?}", nonce); + log::info!("aad: {:x?}", aad); + log::info!("payload: {:x?}", fs_ser); let ciphertext = cipher.encrypt(nonce, payload).expect("failed to encrypt FastSpace record"); + log::info!("ct_len: {}", ciphertext.len()); + log::info!("mac: {:x?}", &ciphertext[ciphertext.len()-16..]); 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); @@ -867,13 +885,17 @@ impl PddbOs { // step 9. generate & write initial page table entries if let Some(system_key) = self.system_basis_key { + log::info!("encryption key: {:x?}", system_key); let cipher = Aes256::new(GenericArray::from_slice(&system_key)); if let Some(basis_v2p_map) = self.v2p_map.get(&basis_root.name) { for (&virt, &phys) in basis_v2p_map.into_iter() { let mut pte = Pte::new(virt, PtFlags::CLEAN, Rc::clone(&self.entropy)); let mut block = Block::from_mut_slice(pte.deref_mut()); + log::info!("pte pt: {:x?}", block); cipher.encrypt_block(&mut block); + log::info!("pte ct: {:x?}", block); self.patch_pagetable(&block, phys.page_number() * aes::BLOCK_SIZE as u32); + log::info!("pte ct loc: {:x?}", phys.page_number() * aes::BLOCK_SIZE as u32); } } } diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index bda435d53..34c6ecbfc 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -24,7 +24,7 @@ pub(crate) const PHYS_PAGE_JOURNAL_MAX: u8 = 15; pub type PhysAddr = u32; #[cfg(feature = "u64_pa")] pub type PhysAddr = u64; -const BITFIELD_PAGE_WIDTH: usize = core::mem::size_of::() * 8 - 12; +const BITFIELD_PAGE_WIDTH: usize = core::mem::size_of::() * 8 - 12; // "12" should be log2(PAGE_SIZE) but https://github.com/rust-lang/rust/issues/70887 // Physical page information, coded as a bitfield, because space is a premium! bitfield! { #[derive(Copy, Clone, Eq)]