From 5be62e372df892437f44ab462b3123e3b547ac46 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 15:44:16 +0800 Subject: [PATCH] chore: move AES-GCM-SIV off of branch to 0.11.1 official this patch moves us off the branch we had to hop on for cipher API compatibility to the one that's officially published by RustCrypto. This closes #168. --- Cargo.lock | 24 +++++++++-------- services/pddb/Cargo.toml | 4 +-- services/pddb/src/backend/basis.rs | 12 +++++---- services/pddb/src/backend/dictionary.rs | 19 +++++++------ services/pddb/src/backend/hw.rs | 36 ++++++++++++------------- services/root-keys/Cargo.toml | 4 +-- services/root-keys/src/backups.rs | 11 ++++---- 7 files changed, 56 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6127a681..9716f9fb0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,10 +16,11 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" [[package]] name = "aead" -version = "0.4.3" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877" +checksum = "ae06cea71059b6b79d879afcdd237a33ac61afc052fdd605815e6f3916254abf" dependencies = [ + "crypto-common", "generic-array", ] @@ -36,8 +37,9 @@ dependencies = [ [[package]] name = "aes-gcm-siv" -version = "0.11.0-pre" -source = "git+https://github.com/RustCrypto/AEADs.git?branch=master#479ae5f8f37d265847da80c4e713597c2c4813ac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae0784134ba9375416d469ec31e7c5f9fa94405049cf08c5ce5b4698be673e0d" dependencies = [ "aead", "aes", @@ -714,9 +716,9 @@ dependencies = [ [[package]] name = "crypto-common" -version = "0.1.3" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", "typenum", @@ -2479,9 +2481,9 @@ dependencies = [ [[package]] name = "polyval" -version = "0.5.3" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" +checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6" dependencies = [ "cfg-if 1.0.0", "cpufeatures", @@ -3780,11 +3782,11 @@ checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" [[package]] name = "universal-hash" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" +checksum = "7d3160b73c9a19f7e2939a2fdad446c57c1bbbbf4d919d3213ff1267a580d8b5" dependencies = [ - "generic-array", + "crypto-common", "subtle", ] diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 7c3034e5f..846d27c3d 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -24,8 +24,8 @@ aes = {path="../aes"} root-keys = {path="../root-keys"} cipher = "0.4.2" bitfield = "0.13.2" -# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} -aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} +aes-gcm-siv = {version = "0.11.1", default-features = false, features = ["alloc", "aes"]} +# aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} llio = {path="../llio"} subtle = {version = "2.4.1", default-features = false} tts-frontend = {path="../tts"} diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 0a82e976a..747296bc6 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -136,10 +136,12 @@ use super::*; use core::ops::{Deref, DerefMut}; use core::mem::size_of; use std::convert::TryInto; -use aes_gcm_siv::{AesGcmSiv, Key}; -use aes_gcm_siv::aead::NewAead; +use aes_gcm_siv::{ + aead::KeyInit, + Aes256GcmSiv, +}; use aes::Aes256; -use aes::cipher::{KeyInit, generic_array::GenericArray}; +use aes::cipher::generic_array::GenericArray; use std::iter::IntoIterator; use std::collections::{BinaryHeap, HashMap, HashSet}; use std::io::{Result, Error, ErrorKind}; @@ -1090,7 +1092,7 @@ pub(crate) struct BasisCacheEntry { /// expressed as a number that needs to be multiplied by DICT_VSIZE to arrive at a virtual address pub free_dict_offset: Option, /// the cipher for the basis - pub cipher: AesGcmSiv::, + pub cipher: Aes256GcmSiv, /// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule pub cipher_ecb: Aes256, /// raw AES page table key -- needed because we have to do a low-level PT scan to generate FSCB, and sometimes the key comes from @@ -1121,7 +1123,7 @@ impl BasisCacheEntry { /// discover the location of the `large_alloc_ptr`. 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, &key.data, name) { - let cipher = AesGcmSiv::::new(Key::from_slice(&key.data)); + let cipher = Aes256GcmSiv::new(&key.data.into()); 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()) { diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 434bb9b04..0858d2c46 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -4,8 +4,7 @@ use super::*; use std::num::NonZeroU32; use core::ops::{Deref, DerefMut}; use core::mem::size_of; -use aes_gcm_siv::AesGcmSiv; -use aes::Aes256; +use aes_gcm_siv::Aes256GcmSiv; use std::collections::{HashMap, BinaryHeap, HashSet}; use std::io::{Result, Error, ErrorKind}; use bitfield::bitfield; @@ -89,7 +88,7 @@ impl DictCacheEntry { /// Requires a descriptor for the hardware, and our virtual to physical page mapping. /// Does not overwrite existing cache entries, if they already exist -- only loads in ones that are missing. /// Todo: condense routines in common with ensure_key_entry() to make it easier to maintain. - pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::) -> VirtAddr { + pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv) -> VirtAddr { let mut try_entry = 1; let mut key_count = 0; let mut alloc_top = VirtAddr::new(LARGE_POOL_START).unwrap(); @@ -197,7 +196,7 @@ impl DictCacheEntry { } /// merges the list of keys in this dict cache entry into a merge_list. /// The `merge_list` is used because keys are presented as a union across all open basis. - pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, merge_list: &mut HashSet) { + pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, merge_list: &mut HashSet) { // ensure that the key cache is filled if self.keys.len() < self.key_count as usize { self.fill(hw, v2p_map, cipher); @@ -213,7 +212,7 @@ impl DictCacheEntry { /// This shares a lot of code with the fill() routine -- we should condense the common routines /// to make this easier to maintain. Returns false if the disk was searched and no key was found; true /// if cache is hot or key was found on search. - pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str) -> bool { // only fill if the key isn't in the cache. if !self.keys.contains_key(name_str) { @@ -285,7 +284,7 @@ impl DictCacheEntry { } } } - fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, + fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, data_cache: &mut PlaintextCache, key_name: &str) { if let Some(kcache) = self.keys.get_mut(key_name) { if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { @@ -368,7 +367,7 @@ impl DictCacheEntry { /// specific dictionary in a named basis. In all of these cases, the Basis resolver will have had to find the /// correct DictCacheEntry and issue the `key_update` to it; for multiple updates, then multiple calls to /// multiple DictCacheEntry are required. - pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name: &str, data: &[u8], offset: usize, alloc_hint:Option, truncate: bool, large_alloc_ptr: PageAlignedVa) -> Result { self.age = self.age.saturating_add(1); self.clean = false; @@ -702,7 +701,7 @@ impl DictCacheEntry { /// Used to remove a key from the dictionary. If you call it with a non-existent key, /// the routine has no effect, and does not report an error. Small keys are not immediately /// overwritten in paranoid mode, but large keys are. - pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str, paranoid: bool) { if paranoid { // large records are paranoid-erased, by default, because of the pool-reuse problem. @@ -815,7 +814,7 @@ impl DictCacheEntry { /// /// Observation: given the dictionary index and the small key pool index, we know exactly /// the virtual address of the data pool. - pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::) -> bool { + pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv) -> bool { for (index, entry) in self.small_pool.iter_mut().enumerate() { if !entry.clean { let pool_vaddr = VirtAddr::new(small_storage_base_vaddr_from_indices(self.index, index)).unwrap(); @@ -1219,7 +1218,7 @@ pub(crate) struct PlaintextCache { pub(crate) tag: Option, } impl PlaintextCache { - pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, aad: &[u8], + pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, aad: &[u8], req_vaddr: VirtAddr ) { if let Some(pp) = v2p_map.get(&req_vaddr) { diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 3381dc1cd..2d0e2d6e1 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -1,8 +1,8 @@ use std::convert::TryInto; use crate::*; -use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; -use aes_gcm_siv::aead::{Aead, NewAead, Payload}; +use aes_gcm_siv::{Nonce, Tag, Aes256GcmSiv}; +use aes_gcm_siv::aead::{Aead, Payload}; use aes::{Aes256, Block, BLOCK_SIZE}; use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray}; use root_keys::api::AesRootkeyType; @@ -97,7 +97,7 @@ pub(crate) struct BasisKeys { struct MigrationCiphers { pt_ecb: Aes256, - data_gcm_siv: AesGcmSiv, + data_gcm_siv: Aes256GcmSiv, data_key: [u8; 32], aad_incoming: Vec::, aad_local: Vec::, @@ -497,7 +497,7 @@ impl PddbOs { pp.set_space_state(SpaceState::Used); // handle conflicting journal versions here if let Some(prev_page) = map.get(&pte.vaddr()) { - let cipher = AesGcmSiv::::new(Key::from_slice(data_key)); + let cipher = Aes256GcmSiv::new(data_key.into()); let aad = self.data_aad(basis_name); if pte.vaddr() != VirtAddr::new(VPAGE_SIZE as u64).unwrap() { // handle case that there is a conflict in data areas @@ -761,8 +761,7 @@ impl PddbOs { 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.data); - let cipher = AesGcmSiv::::new(key); + let cipher = Aes256GcmSiv::new(&system_basis_key.data.into()); let nonce_array = self.entropy.borrow_mut().get_nonce(); let nonce = Nonce::from_slice(&nonce_array); let fs_ser: &[u8] = fs.deref(); @@ -974,8 +973,7 @@ impl PddbOs { msg: &fscb_buf, aad: &aad, }; - let key = Key::from_slice(&system_key.data); - let cipher = AesGcmSiv::::new(key); + let cipher = Aes256GcmSiv::new(&system_key.data.into()); match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::()]), payload) { Ok(msg) => { log::info!("decrypted: {}, FastSpace size: {}", msg.len(), size_of::()); @@ -1333,7 +1331,7 @@ impl PddbOs { /// returns a decrypted page that still includes the journal number at the very beginning /// We don't clip it off because it would require re-allocating a vector, and it's cheaper (although less elegant) to later /// just index past it. - pub(crate) fn data_decrypt_page(&self, cipher: &AesGcmSiv::, aad: &[u8], page: &PhysPage) -> Option> { + pub(crate) fn data_decrypt_page(&self, cipher: &Aes256GcmSiv, aad: &[u8], page: &PhysPage) -> Option> { let ct_slice = &self.pddb_mr.as_slice()[ self.data_phys_base.as_usize() + page.page_number() as usize * PAGE_SIZE .. self.data_phys_base.as_usize() + (page.page_number() as usize + 1) * PAGE_SIZE]; @@ -1408,7 +1406,7 @@ impl PddbOs { log::debug!("found kcom_nonce of {:x?}", nonce_comm); let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &nonce_comm); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc)); + let cipher = Aes256GcmSiv::new(&kenc.into()); // Attempt decryption. This is None on failure let plaintext = cipher.decrypt( @@ -1428,7 +1426,7 @@ impl PddbOs { } /// `data` includes the journal entry on top. The data passed in must be exactly one vpage plus the journal entry - pub(crate) fn data_encrypt_and_patch_page(&self, cipher: &AesGcmSiv::, aad: &[u8], data: &mut [u8], pp: &PhysPage) { + pub(crate) fn data_encrypt_and_patch_page(&self, cipher: &Aes256GcmSiv, aad: &[u8], data: &mut [u8], pp: &PhysPage) { assert!(data.len() == VPAGE_SIZE + size_of::(), "did not get a page-sized region to patch"); let j = JournalType::from_le_bytes(data[..size_of::()].try_into().unwrap()).saturating_add(1); for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::()].iter_mut()) { *dst = src; } @@ -1459,7 +1457,7 @@ impl PddbOs { self.trng_slice(&mut kcom_nonce); // generates the encryption and commit keys let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &kcom_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc)); + let cipher = Aes256GcmSiv::new(&kenc.into()); let ciphertext = cipher.encrypt( &nonce, Payload { @@ -1982,7 +1980,7 @@ impl PddbOs { log::warn!("pt_scan for basis {} failed, data may be lost", name); } let pt_ecb = Aes256::new(&GenericArray::from_slice(&basis_keys.pt)); - let data_gcm_siv = AesGcmSiv::::new(Key::from_slice(&basis_keys.data)); + let data_gcm_siv = Aes256GcmSiv::new(&basis_keys.data.into()); self.dna_mode = DnaMode::Normal; let aad_local = self.data_aad(&name); self.dna_mode = DnaMode::Migration; @@ -2579,10 +2577,10 @@ impl PddbOs { aad_v2: &Vec::, key_v1: &[u8; AES_KEYSIZE], // needed to decrypt page with commit cipher_pt_v1: &Aes256, - cipher_data_v1: &AesGcmSiv::, + cipher_data_v1: &Aes256GcmSiv, key_data_v2: &[u8; AES_KEYSIZE], // this is needed because we have to do a key commitment cipher_pt_v2: &Aes256, - cipher_data_v2: &AesGcmSiv::, + cipher_data_v2: &Aes256GcmSiv, used_pages: &mut BinaryHeap::>, ) -> bool { let blank = [0xffu8; aes::BLOCK_SIZE]; @@ -2742,7 +2740,7 @@ impl PddbOs { // build the ECB cipher for page table entries, and data cipher for data pages self.cipher_ecb = Some(Aes256::new(GenericArray::from_slice(&system_basis_key_pt))); let cipher_v2 = 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 - let data_cipher_v2 = AesGcmSiv::::new(Key::from_slice(&system_basis_key)); + let data_cipher_v2 = Aes256GcmSiv::new(Key::from_slice(&system_basis_key)); let aad_v2 = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS); // now wrap the key for storage let wrapped_key = self.rootkeys.wrap_key(&system_basis_key).expect("Internal error wrapping our encryption key"); @@ -2772,7 +2770,7 @@ impl PddbOs { // now we have a copy of the AES key necessary to re-encrypt all the basis // build the data cipher for v1 pages - let data_cipher_v1 = AesGcmSiv::::new(Key::from_slice(&system_key_v1)); + let data_cipher_v1 = Aes256GcmSiv::new(Key::from_slice(&system_key_v1)); let aad_v1 = data_aad_v1(&self, PDDB_DEFAULT_SYSTEM_BASIS); // track used pages so we can create the FSCB at the end @@ -2829,10 +2827,10 @@ impl PddbOs { let basis_aad_v1 = data_aad_v1(&self, bname.first().as_str()); let basis_aad_v2 = self.data_aad(bname.first().as_str()); let basis_pt_cipher_v1 = Aes256::new(GenericArray::from_slice(&basis_key_v1)); - let basis_data_cipher_v1 = AesGcmSiv::::new(Key::from_slice(&basis_key_v1)); + let basis_data_cipher_v1 = Aes256GcmSiv::new(Key::from_slice(&basis_key_v1)); let basis_keys = self.basis_derive_key(bname.first().as_str(), pw.as_str().unwrap_or("UTF8 error")); let basis_pt_cipher_2 = Aes256::new(GenericArray::from_slice(&basis_keys.pt)); - let basis_data_cipher_2 = AesGcmSiv::::new(Key::from_slice(&basis_keys.data)); + let basis_data_cipher_2 = Aes256GcmSiv::new(Key::from_slice(&basis_keys.data)); // perform the migration if self.migration_v1_to_v2_inner( &basis_aad_v1, &basis_aad_v2, diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 7324d6894..182c89909 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -47,8 +47,8 @@ jtag = {path = "../jtag"} tts-frontend = {path="../tts"} # backups -# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} -aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} +aes-gcm-siv = {version = "0.11.1", default-features = false, features = ["alloc", "aes"]} +#aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} subtle = {version = "2.4.1", default-features = false} usb-device-xous = {path="../usb-device-xous"} keyboard = {path="../keyboard"} diff --git a/services/root-keys/src/backups.rs b/services/root-keys/src/backups.rs index 1dee30633..5786e22aa 100644 --- a/services/root-keys/src/backups.rs +++ b/services/root-keys/src/backups.rs @@ -1,9 +1,10 @@ use zeroize::Zeroize; use core::mem::size_of; use core::ops::{Deref, DerefMut}; -use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; -use aes_gcm_siv::aead::{Aead, NewAead, Payload}; -use aes::Aes256; +use aes_gcm_siv::{ + aead::{KeyInit, Aead, Payload}, + Aes256GcmSiv, Tag, Nonce +}; use subtle::ConstantTimeEq; use crate::{BackupHeader, BackupOp}; @@ -144,7 +145,7 @@ pub(crate) fn create_backup( trng.fill_bytes(&mut kcom_nonce); let (kenc, kcom) = kcom_func(&key.0, &kcom_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc.0)); + let cipher = Aes256GcmSiv::new(&kenc.0.into()); // the backup data let mut backup_data_pt = BackupDataPt::default(); @@ -186,7 +187,7 @@ pub(crate) fn restore_backup( backup: &BackupDataCt ) -> Option { let (kenc, kcom) = kcom_func(&key.0, &backup.commit_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc.0)); + let cipher = Aes256GcmSiv::new(&kenc.0.into()); // Attempt decryption. This is None on failure let plaintext = cipher.decrypt(