diff --git a/Cargo.lock b/Cargo.lock index a25dc0534..7237770bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -655,6 +655,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crypto-mac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "csv" version = "1.1.5" @@ -1207,6 +1217,26 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5af1f635ef1bc545d78392b136bfe1c9809e029023c84a3638a864a10b8819c8" +[[package]] +name = "hkdf" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01706d578d5c281058480e673ae4086a9f4710d8df1ad80a5b03e39ece5f886b" +dependencies = [ + "digest", + "hmac", +] + +[[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac", + "digest", +] + [[package]] name = "humantime" version = "1.3.0" @@ -1822,6 +1852,7 @@ dependencies = [ "cipher", "digest", "gam", + "hkdf", "llio", "locales", "log", @@ -1845,6 +1876,8 @@ dependencies = [ "xous", "xous-ipc 0.9.7", "xous-names", + "zeroize", + "zeroize_derive", ] [[package]] @@ -2996,9 +3029,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.12.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "unicode-bidi" diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 867783abc..b7c7fd03f 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -33,6 +33,9 @@ tts-frontend = {path="../tts"} # passwords sha2 = {path = "../engine-sha512"} digest = "0.9.0" +hkdf = "0.11.0" +zeroize = "1.3.0" +zeroize_derive = "1.1.0" # bcrypt blowfish = { version = "0.8.0", features = ["bcrypt"] } @@ -64,4 +67,8 @@ deterministic = [] ci = [] # this feature is for text-to-speech support tts = [] -default = ["mbbb"] +# support migration type 1, from version 00.00.01.01 -> 00.00.02.01 +migration1 = [] +# hazardous debug flag decorates any debug paths that might accidentally leak key material +hazardous-debug = [] +default = ["mbbb", "migration1", "hazardous-debug"] diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 43cac7163..cd9e1c59e 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -28,8 +28,14 @@ pub(crate) const KEY_NAME_LEN: usize = 127 - 8 - 8 - 8 - 4 - 4; // u64: vaddr/le pub(crate) const PASSWORD_LEN: usize = 72; // this is actually set by bcrypt #[allow(dead_code)] pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42]; +/// migrateable version pairs +/// PDDB_MIGRATE_1: +/// 00.00.01.01 - xous 0.9.7 release (original base release) +/// 00.00.02.01 - xous 0.9.8 migration -> hkdf added on basis key derivation to make separate PT/data keys #[allow(dead_code)] -pub(crate) const PDDB_VERSION: u32 = 0x00_00_01_01; +pub(crate) const PDDB_MIGRATE_1: (u32, u32) = (0x00_00_01_01, 0x00_00_02_01); +#[allow(dead_code)] +pub(crate) const PDDB_VERSION: u32 = 0x00_00_02_01; #[allow(dead_code)] // PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing. #[allow(dead_code)] diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index ec2aec5c9..930c21f55 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -788,10 +788,10 @@ impl BasisCache { pub(crate) fn basis_unlock(&mut self, hw: &mut PddbOs, name: &str, password: &str, policy: BasisRetentionPolicy) -> Option { let basis_key = hw.basis_derive_key(name, password); - if let Some(basis_map) = hw.pt_scan_key(&basis_key, name) { + if let Some(basis_map) = hw.pt_scan_key(&basis_key.pt, name) { let aad = hw.data_aad(name); if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { - let vpage = match hw.data_decrypt_page_with_commit(&basis_key, &aad, root_page) { + let vpage = match hw.data_decrypt_page_with_commit(&basis_key.data, &aad, root_page) { Some(data) => data, None => {log::error!("Could not find basis {} root", name); return None;}, }; @@ -863,9 +863,9 @@ impl BasisCache { for (&src, dst) in slice_iter.zip(block.iter_mut()) { *dst = src; } - hw.data_encrypt_and_patch_page_with_commit(&basis_key, &aad, &mut block, &pp); + hw.data_encrypt_and_patch_page_with_commit(&basis_key.data, &aad, &mut block, &pp); - let cipher = Aes256::new(GenericArray::from_slice(&basis_key)); + let cipher = Aes256::new(GenericArray::from_slice(&basis_key.pt)); for (&virt, phys) in basis_v2p_map.iter_mut() { hw.pt_patch_mapping(virt, phys.page_number(), &cipher); // mark the entry as clean, as it has been sync'd to disk @@ -1008,13 +1008,13 @@ impl BasisCacheEntry { /// the basis. If `lazy` is true, it stops with the minimal amount of effort to respond to a query. /// If it `lazy` is false, it will populate the dictionary cache and key cache entries, as well as /// discover the location of the `large_alloc_ptr`. - pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &[u8; AES_KEYSIZE], lazy: bool, policy: BasisRetentionPolicy) -> Option { - if let Some(basis_map) = hw.pt_scan_key(key, name) { - let cipher = Aes256GcmSiv::new(Key::from_slice(key)); + pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &BasisKeys, lazy: bool, policy: BasisRetentionPolicy) -> Option { + if let Some(basis_map) = hw.pt_scan_key(&key.pt, name) { + let cipher = Aes256GcmSiv::new(Key::from_slice(&key.data)); let aad = hw.data_aad(name); // get the first page, where the basis root is guaranteed to be if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { - let vpage = match hw.data_decrypt_page_with_commit(key, &aad, root_page) { + let vpage = match hw.data_decrypt_page_with_commit(&key.data, &aad, root_page) { Some(data) => data, None => {log::error!("System basis decryption did not authenticate. Unrecoverable error."); return None;}, }; @@ -1044,8 +1044,8 @@ impl BasisCacheEntry { num_dicts: basis_root.num_dictionaries, dicts: HashMap::::new(), cipher, - cipher_ecb: Aes256::new(GenericArray::from_slice(key)), - key: GenericArray::clone_from_slice(key), + cipher_ecb: Aes256::new(GenericArray::from_slice(&key.pt)), + key: GenericArray::clone_from_slice(&key.data), aad, age: basis_root.age, free_dict_offset: None, diff --git a/services/pddb/src/backend/hosted.rs b/services/pddb/src/backend/hosted.rs index 39c048728..d785cb021 100644 --- a/services/pddb/src/backend/hosted.rs +++ b/services/pddb/src/backend/hosted.rs @@ -64,7 +64,10 @@ fn flashmem() -> &'static mut FlashSingleton { #[derive(Copy, Clone)] pub struct KeyExport { pub basis_name: [u8; 64], + /// data key pub key: [u8; 32], + /// page table key + pub pt_key: [u8; 32], } pub struct EmuStorage { } @@ -105,6 +108,7 @@ impl EmuStorage { for key in known_keys { f.write_all(&key.basis_name).unwrap(); f.write_all(&key.key).unwrap(); + f.write_all(&key.pt_key).unwrap(); } f.flush().unwrap(); } diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index ba9484919..c7fa284ec 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -25,6 +25,8 @@ type FspaceSet = HashSet::; #[cfg(feature="deterministic")] type FspaceSet = BTreeSet::; +use zeroize::Zeroize; + /// Implementation-specific PDDB structures: for Precursor/Xous OS pair pub(crate) const MBBB_PAGES: usize = 10; pub(crate) const FSCB_PAGES: usize = 16; @@ -40,22 +42,30 @@ pub const VPAGE_SIZE: usize = PAGE_SIZE - size_of::() - size_of::() pub const KCOM_CT_LEN: usize = 4004; const WRAPPED_AES_KEYSIZE: usize = AES_KEYSIZE + 8; -const SCD_VERSION: u32 = 1; +const SCD_VERSION: u32 = 2; +#[cfg(feature="migration1")] +const SCD_VERSION_MIGRATION1: u32 = 1; + +#[derive(Zeroize)] +#[zeroize(drop)] #[repr(C)] // this can map directly into Flash pub(crate) struct StaticCryptoData { /// a version number for the block pub(crate) version: u32, + /// aes-256 key of the system basis page table, encrypted with the User0 root key, and wrapped using NIST SP800-38F + pub(crate) system_key_pt: [u8; WRAPPED_AES_KEYSIZE], /// aes-256 key of the system basis, encrypted with the User0 root key, and wrapped using NIST SP800-38F pub(crate) system_key: [u8; WRAPPED_AES_KEYSIZE], - /// a pool of fixed data used as a salt - pub(crate) salt_base: [u8; 4096 - WRAPPED_AES_KEYSIZE - size_of::()], + /// a pool of fixed data used for salting. The first 32 bytes are further subdivided for use in the HKDF. + pub(crate) salt_base: [u8; 4096 - WRAPPED_AES_KEYSIZE * 2 - size_of::()], } impl StaticCryptoData { pub fn default() -> StaticCryptoData { StaticCryptoData { version: SCD_VERSION, + system_key_pt: [0u8; WRAPPED_AES_KEYSIZE], system_key: [0u8; WRAPPED_AES_KEYSIZE], - salt_base: [0u8; 4096 - WRAPPED_AES_KEYSIZE - size_of::()], + salt_base: [0u8; 4096 - WRAPPED_AES_KEYSIZE * 2 - size_of::()], } } } @@ -69,6 +79,44 @@ impl Deref for StaticCryptoData { } } +#[repr(C)] // this can map directly into Flash +#[cfg(feature="migration1")] +pub(crate) struct StaticCryptoDataV1 { + /// a version number for the block + pub(crate) version: u32, + /// aes-256 key of the system basis, encrypted with the User0 root key, and wrapped using NIST SP800-38F + pub(crate) system_key: [u8; WRAPPED_AES_KEYSIZE], + /// a pool of fixed data used as a salt + pub(crate) salt_base: [u8; 4096 - WRAPPED_AES_KEYSIZE - size_of::()], +} +#[cfg(feature="migration1")] +impl StaticCryptoDataV1 { + pub fn default() -> StaticCryptoDataV1 { + StaticCryptoDataV1 { + version: SCD_VERSION_MIGRATION1, + system_key: [0u8; WRAPPED_AES_KEYSIZE], + salt_base: [0u8; 4096 - WRAPPED_AES_KEYSIZE - size_of::()], + } + } +} +#[cfg(feature="migration1")] +impl Deref for StaticCryptoDataV1 { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + core::slice::from_raw_parts(self as *const StaticCryptoDataV1 as *const u8, size_of::()) + as &[u8] + } + } +} + +#[derive(Zeroize)] +#[zeroize(drop)] +pub(crate) struct BasisKeys { + pub(crate) pt: [u8; AES_KEYSIZE], + pub(crate) data: [u8; AES_KEYSIZE] +} + // emulated #[cfg(not(any(target_os = "none", target_os = "xous")))] type EmuMemoryRange = EmuStorage; @@ -97,7 +145,7 @@ pub(crate) struct PddbOs { fscb_phys_base: PageAlignedPa, data_phys_base: PageAlignedPa, /// We keep a copy of the raw key around because we have to combine this with the AAD of a block to derive the AES-GCM-SIV cipher. - system_basis_key: Option<[u8; AES_KEYSIZE]>, + system_basis_key: Option, /// derived cipher for handling fastspace -- cache it, so we can save the time cost of constructing the cipher key schedule cipher_ecb: Option, /// fast space cache @@ -188,8 +236,8 @@ impl PddbOs { pub fn dbg_dump(&self, name: Option, extra_keys: Option<&Vec::>) { self.pddb_mr.dump_fs(&name); let mut export = Vec::::new(); - if let Some(key) = self.system_basis_key { - log::info!("(hosted mode debug) written key: {:x?}", key); + if let Some(key) = &self.system_basis_key { + log::info!("(hosted mode debug) written key: {:x?}, {:x?}", key.pt, key.data); let mut name = [0 as u8; 64]; for (&src, dst) in PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut()) { *dst = src; @@ -197,7 +245,8 @@ impl PddbOs { export.push( KeyExport { basis_name: name, - key, + key: key.data, + pt_key: key.pt, } ); } @@ -385,6 +434,30 @@ impl PddbOs { /// scans the page tables and returns all entries for a given basis /// basis_name is needed to decrypt pages in case of a journal conflict + /// + /// Here, we may encounter page table entries that are bogus for two reasons: + /// 1. PT entries that pass the checksum, but don't map to valid data due to random collisions from the short 32-bit checksum space + /// 2. Conflicting entries due to an unclean power-down + /// + /// This routine does a "lazy" check of improper data: + /// If the bad data happens to land on the same page mapping as valid data, a conflict resolution happens where + /// the block is decrypted and validated. + /// - If there is only one valid block, the valid block is picked to populate the page table (handles half of case 1) + /// - If two valid blocks are found, the one with the older journal entry is used (handles the easy version of case 2) + /// - If two valid blocks with identical journal versions happen (hard version of case 2), this is an internal consistency error. + /// It shouldn't happen; but, if it happens, it retains the first version encountered and prints an error to the log. + /// + /// This leaves the "hard version" of case 1 - there was a checksum collision, and it wasn't detected because no other valid + /// block happened to map to it. In this case, the "imposter" entry is left to stand, under the theory that typically this represents + /// this represents a "leak" of free space where this orphaned bock may never be allocated or used. However, because these are + /// rare (perhaps around 1 in a billion chance?) this leakage is fine. Note that if the FSCB is generated before this page gets + /// allocated, the imposter page is avoided in the scan, so this leaked memory is "forever". An orphaned node search could + /// try to chase this out, if it becomes a problem. + /// + /// If the "imposter" entry also happens to be "allocated" down the road (that is, the FSCB happens to have an entry + /// that also maps to the imposter), the imposter is effectively de-allocated because the FSCB report is inserted directly + /// into the page table, overwriting the impostor entry in the paget able. On the next mount, this turns into the "trivial conflict" + /// case, where one page will validate and the other will not. pub(crate) fn pt_scan_key(&self, key: &[u8; AES_KEYSIZE], basis_name: &str) -> Option> { let cipher = Aes256::new(&GenericArray::from_slice(key)); let pt = self.pt_as_slice(); @@ -465,19 +538,18 @@ impl PddbOs { let scd: &StaticCryptoData = unsafe{scd_ptr.as_ref().unwrap()}; scd } + #[cfg(feature="migration1")] + fn static_crypto_data_get_v1(&self) -> &StaticCryptoDataV1 { + let scd_ptr = self.pddb_mr.as_slice::()[self.key_phys_base.as_usize()..self.key_phys_base.as_usize() + PAGE_SIZE].as_ptr() as *const StaticCryptoDataV1; + let scd: &StaticCryptoDataV1 = 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. #[allow(dead_code)] 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() { - unsafe { - b.add(i).write_volatile(core::mem::zeroed()); - } - } - core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); - } + // this implements zeroize, so replacing it with None should do the trick + self.system_basis_key = None; self.cipher_ecb = None; } pub(crate) fn clear_password(&self) { @@ -493,21 +565,50 @@ impl PddbOs { log::error!("Version mismatch for keystore, declaring database as uninitialized"); return PasswordState::Uninit; } - match self.rootkeys.unwrap_key(&scd.system_key, AES_KEYSIZE) { - Ok(mut syskey) => { - let cipher = Aes256::new(GenericArray::from_slice(&syskey)); - self.cipher_ecb = Some(cipher); - let mut system_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; - for (&src, dst) in syskey.iter().zip(system_key.iter_mut()) { - *dst = src; + match self.rootkeys.unwrap_key(&scd.system_key_pt, AES_KEYSIZE) { + Ok(mut syskey_pt) => { + match self.rootkeys.unwrap_key(&scd.system_key, AES_KEYSIZE) { + Ok(mut syskey) => { + let cipher = Aes256::new(GenericArray::from_slice(&syskey_pt)); + self.cipher_ecb = Some(cipher); + let mut system_key_pt: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; + // copy over the pt key only after we've unwrapped and decrypted the data key + for (&src, dst) in syskey_pt.iter().zip(system_key_pt.iter_mut()) { + *dst = src; + } + // copy over the data key + let mut system_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; + for (&src, dst) in syskey.iter().zip(system_key.iter_mut()) { + *dst = src; + } + + self.system_basis_key = Some( + BasisKeys { + data: system_key, + pt: system_key_pt, + } + ); + // erase the old vector completely + let nuke = syskey.as_mut_ptr(); + for i in 0..syskey.len() { + unsafe{nuke.add(i).write_volatile(0)}; + } + let nuke = syskey_pt.as_mut_ptr(); + for i in 0..syskey_pt.len() { + unsafe{nuke.add(i).write_volatile(0)}; + } + PasswordState::Correct + } + Err(e) => { + // the PT key is still lingering on this arm, make sure we get rid of that + let nuke = syskey_pt.as_mut_ptr(); + for i in 0..syskey_pt.len() { + unsafe{nuke.add(i).write_volatile(0)}; + } + log::error!("Couldn't unwrap our system key: {:?}", e); + PasswordState::Incorrect + } } - self.system_basis_key = Some(system_key); - // erase the old vector completely - let nuke = syskey.as_mut_ptr(); - for i in 0..syskey.len() { - unsafe{nuke.add(i).write_volatile(0)}; - } - PasswordState::Correct } Err(e) => { log::error!("Couldn't unwrap our system key: {:?}", e); @@ -575,8 +676,8 @@ impl PddbOs { /// 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); + if let Some(system_basis_key) = &self.system_basis_key { + let key = Key::from_slice(&system_basis_key.data); let cipher = Aes256GcmSiv::new(key); let nonce_array = self.entropy.borrow_mut().get_nonce(); let nonce = Nonce::from_slice(&nonce_array); @@ -732,7 +833,7 @@ impl PddbOs { /// fn fast_space_read(&mut self) { self.syskey_ensure(); - if let Some(system_key) = self.system_basis_key { + 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(); @@ -776,7 +877,7 @@ impl PddbOs { msg: &fscb_buf, aad: &aad, }; - let key = Key::from_slice(&system_key); + let key = Key::from_slice(&system_key.data); let cipher = Aes256GcmSiv::new(key); match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::()]), payload) { Ok(msg) => { @@ -803,7 +904,7 @@ impl PddbOs { } // 2. visit the update_page_addrs and modify the fspace_cache accordingly. - let cipher = Aes256::new(GenericArray::from_slice(&system_key)); + let cipher = Aes256::new(GenericArray::from_slice(&system_key.pt)); let mut block = Block::default(); log::info!("space_log_addrs len: {}", self.fspace_log_addrs.len()); for page in &self.fspace_log_addrs { @@ -1306,12 +1407,12 @@ impl PddbOs { pub(crate) fn pddb_mount(&mut self) -> Option { self.fast_space_read(); self.syskey_ensure(); - if let Some(syskey) = self.system_basis_key { - if let Some(sysbasis_map) = self.pt_scan_key(&syskey, PDDB_DEFAULT_SYSTEM_BASIS) { + if let Some(syskey) = self.system_basis_key.take() { + if let Some(sysbasis_map) = self.pt_scan_key(&syskey.pt, PDDB_DEFAULT_SYSTEM_BASIS) { let aad = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS); // get the first page, where the basis root is guaranteed to be if let Some(root_page) = sysbasis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { - let vpage = match self.data_decrypt_page_with_commit(&syskey, &aad, root_page) { + let vpage = match self.data_decrypt_page_with_commit(&syskey.data, &aad, root_page) { Some(data) => data, None => {log::error!("System basis decryption did not authenticate. Unrecoverable error."); return None;}, }; @@ -1335,13 +1436,19 @@ impl PddbOs { return None; } log::info!("System BasisRoot record found, generating cache entry"); - BasisCacheEntry::mount(self, &basis_name, &syskey, false, BasisRetentionPolicy::Persist) + let bce = BasisCacheEntry::mount(self, &basis_name, &syskey, false, BasisRetentionPolicy::Persist); + self.system_basis_key = Some(syskey); + bce } else { // i guess technically we could try a brute-force search for the page, but meh. log::error!("System basis did not contain a root page -- unrecoverable error."); + self.system_basis_key = Some(syskey); None } - } else { None } + } else { + self.system_basis_key = Some(syskey); + None + } } else { None } @@ -1460,14 +1567,22 @@ impl PddbOs { self.tt.sleep_ms(100).unwrap(); } assert!(size_of::() == PAGE_SIZE, "StaticCryptoData structure is not correctly sized"); + let mut system_basis_key_pt: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; let mut system_basis_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; + self.entropy.borrow_mut().get_slice(&mut system_basis_key_pt); self.entropy.borrow_mut().get_slice(&mut system_basis_key); // build the ECB cipher for page table entries - self.cipher_ecb = Some(Aes256::new(GenericArray::from_slice(&system_basis_key))); - let cipher_ecb = Aes256::new(GenericArray::from_slice(&system_basis_key)); // a second copy for patching the page table later in this routine interior mutability blah blah work around oops + self.cipher_ecb = Some(Aes256::new(GenericArray::from_slice(&system_basis_key_pt))); + let cipher_ecb = Aes256::new(GenericArray::from_slice(&system_basis_key_pt)); // a second copy for patching the page table later in this routine interior mutability blah blah work around oops // now wrap the key for storage let wrapped_key = self.rootkeys.wrap_key(&system_basis_key).expect("Internal error wrapping our encryption key"); - self.system_basis_key = Some(system_basis_key); // this causes system_basis_key to be owned by self and go out of scope + let wrapped_key_pt = self.rootkeys.wrap_key(&system_basis_key_pt).expect("Internal error wrapping our encryption key"); + self.system_basis_key = Some( + BasisKeys { + pt: system_basis_key_pt, + data: system_basis_key, + } + ); // this causes system_basis_key to be owned by self and go out of scope let mut crypto_keys = StaticCryptoData::default(); crypto_keys.version = SCD_VERSION; // should already be set by `default()` but let's be sure. if let Some(modals) = progress { @@ -1480,6 +1595,10 @@ impl PddbOs { for (&src, dst) in wrapped_key.iter().zip(crypto_keys.system_key.iter_mut()) { *dst = src; } + assert!(wrapped_key_pt.len() == 40, "wrapped pt key did not have the expected length"); + for (&src, dst) in wrapped_key_pt.iter().zip(crypto_keys.system_key_pt.iter_mut()) { + *dst = src; + } // initialize the salt self.entropy.borrow_mut().get_slice(&mut crypto_keys.salt_base); // commit keys @@ -1624,8 +1743,8 @@ impl PddbOs { for (&src, dst) in slice_iter.zip(block.iter_mut()) { *dst = src; } - let syskey = self.system_basis_key.unwrap(); // take the key out - self.data_encrypt_and_patch_page_with_commit(&syskey, &aad, &mut block, &pp); + let syskey = self.system_basis_key.take().unwrap(); // take they key out + self.data_encrypt_and_patch_page_with_commit(&syskey.data, &aad, &mut block, &pp); self.system_basis_key = Some(syskey); // put the key back if let Some(modals) = progress { modals.update_progress(66).expect("couldn't update progress bar"); @@ -1703,7 +1822,7 @@ impl PddbOs { /// Derives a 256-bit AES encryption key for a basis given a basis name and its password. /// You will also need to derive the AAD for the basis using the basis_name. - pub(crate) fn basis_derive_key(&self, basis_name: &str, password: &str) -> [u8; AES_KEYSIZE] { + pub(crate) fn basis_derive_key(&self, basis_name: &str, password: &str) -> BasisKeys { use sha2::{FallbackStrategy, Sha512Trunc256}; use digest::Digest; use backend::bcrypt::*; @@ -1728,6 +1847,83 @@ impl PddbOs { let scd = self.static_crypto_data_get(); let mut salt = [0u8; 16]; let mut hasher = Sha512Trunc256::new_with_strategy(FallbackStrategy::SoftwareOnly); + hasher.update(&scd.salt_base[32..]); // reserve the first 32 bytes of salt for the HKDF + hasher.update(&bname_copy); + hasher.update(&plaintext_pw); + let result = hasher.finalize(); + for (&src, dst) in result.iter().zip(salt.iter_mut()) { + *dst = src; + } + log::info!("derived salt: {:x?}", salt); + + // 3. use the salt + password and run bcrypt on it to derive a key. + let mut hashed_password: [u8; 24] = [0; 24]; + let start_time = self.timestamp_now(); + bcrypt(BCRYPT_COST, &salt, password, &mut hashed_password); // note: this internally makes a copy of the password, and destroys it + let elapsed = self.timestamp_now() - start_time; + log::info!("derived bcrypt password in {}ms", elapsed); + + // 4. take the resulting 24-byte password and expand it to 2x 32 byte keys using HKDF. + // one key is for the AES-256 ECB-encoded page tables, one key is for the AES-GCM-SIV data pages + let hkpt = hkdf::Hkdf::::new(Some(&scd.salt_base[..32]), &hashed_password); + let mut okm_pt = [0u8; 32]; + hkpt.expand(b"pddb page table key", &mut okm_pt).expect("invalid length specified for HKDF"); + + let hkdt = hkdf::Hkdf::::new(Some(&scd.salt_base[..32]), &hashed_password); + let mut okm_data = [0u8; 32]; + hkdt.expand(b"pddb data key", &mut okm_data).expect("invalid length specified for HKDF"); + + // 5. erase extra plaintext copies made of the basis name and password using a routine that + // shouldn't be optimized out or re-ordered + let bn_ptr = bname_copy.as_mut_ptr(); + for i in 0..bname_copy.len() { + unsafe{bn_ptr.add(i).write_volatile(core::mem::zeroed());} + } + let pt_ptr = plaintext_pw.as_mut_ptr(); + for i in 0..plaintext_pw.len() { + unsafe{pt_ptr.add(i).write_volatile(core::mem::zeroed());} + } + core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); + + #[cfg(feature="hazardous-debug")] + log::info!("okm_pt: {:x?}", okm_pt); + #[cfg(feature="hazardous-debug")] + log::info!("okm_data: {:x?}", okm_data); + + //6. return the keys + BasisKeys { + pt: okm_pt, + data: okm_data, + } + } + + /// legacy key derivation for migrations. This can be removed once the migration is de-supported. + #[cfg(feature="migration1")] + pub(crate) fn basis_derive_key_v00_00_01_01(&self, basis_name: &str, password: &str) -> [u8; AES_KEYSIZE] { + use sha2::{FallbackStrategy, Sha512Trunc256}; + use digest::Digest; + use backend::bcrypt::*; + + // 1. derive the salt from the "key" region. First step is to create the salt lookup + // table, which is done by hashing the name and password together with SHA-512 + // manage the allocation of the data for the basis & password explicitly so that we may wipe them later + let mut bname_copy = [0u8; BASIS_NAME_LEN]; + for (src, dst) in basis_name.bytes().zip(bname_copy.iter_mut()) { + *dst = src; + } + let mut plaintext_pw: [u8; 73] = [0; 73]; + for (src, dst) in password.bytes().zip(plaintext_pw.iter_mut()) { + *dst = src; + } + plaintext_pw[72] = 0; // always null terminate + + log::info!("creating salt"); + // uses Sha512Trunc256 on the salt array to generate a compressed version of + // the basis name and plaintext password, which forms the Salt that is fed into bcrypt + // our salt is probably way too big but what else are we going to use all that page's data for? + let scd = self.static_crypto_data_get_v1(); + let mut salt = [0u8; 16]; + let mut hasher = Sha512Trunc256::new_with_strategy(FallbackStrategy::SoftwareOnly); hasher.update(&scd.salt_base); hasher.update(&bname_copy); hasher.update(&plaintext_pw); @@ -1768,4 +1964,5 @@ impl PddbOs { //6. return the key key } + } diff --git a/services/pddb/src/tests.rs b/services/pddb/src/tests.rs index c86a3eb88..0147eb380 100644 --- a/services/pddb/src/tests.rs +++ b/services/pddb/src/tests.rs @@ -414,7 +414,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { } let extra_export = KeyExport { basis_name: name, - key: extra_basis_key, + key: extra_basis_key.data, + pt_key: extra_basis_key.pt, }; let mut export: Vec:: = Vec::new(); export.push(extra_export);