mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add rekey operation for PDDB migrations to new devices
This commit is contained in:
parent
83eb05273f
commit
e8188309cf
8 changed files with 431 additions and 10 deletions
|
|
@ -75,4 +75,5 @@ tts = []
|
|||
migration1 = []
|
||||
# hazardous debug flag decorates any debug paths that might accidentally leak key material
|
||||
hazardous-debug = []
|
||||
test-rekey = []
|
||||
default = ["mbbb"]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,34 @@
|
|||
{
|
||||
"pddb.rekey.keys": {
|
||||
"en": "Generating encryption mappings...",
|
||||
"ja": "暗号化マッピングの生成...",
|
||||
"zh": "生成加密映射...",
|
||||
"en-tts": "Generating encryption mappings..."
|
||||
},
|
||||
"pddb.rekey.running": {
|
||||
"en": "Rekeying the database...",
|
||||
"ja": "データベースのキーの再生成...",
|
||||
"zh": "重新加密数据库...",
|
||||
"en-tts": "Rekeying the database..."
|
||||
},
|
||||
"pddb.rekey.fastspace": {
|
||||
"en": "Regenerating free space cache...",
|
||||
"ja": "空き領域キャッシュの再生成...",
|
||||
"zh": "重新生成可用空间缓存...",
|
||||
"en-tts": "Regenerating free space cache..."
|
||||
},
|
||||
"pddb.freespace.request": {
|
||||
"en": "⚠ PDDB needs to allocate more free space. ⚠\nIn order to do this safely, you must enumerate all secret Bases now. Any Basis not disclosed could be overwritten.",
|
||||
"ja": "⚠ PDDBは、より多くの空き領域を割り当てる必要があります。⚠\nこれを安全に行うには、今すぐすべてのシークレットベースのロックを解除する必要があります。 ロックされたままのベーシスは上書きされる可能性があります。",
|
||||
"zh": "⚠ PDDB 需要分配更多可用空间。⚠\n为了安全地执行此操作,您现在必须解锁所有秘密基地。 任何被锁定的基础都可以被覆盖。",
|
||||
"en-tts": "⚠ PDDB needs to allocate more free space. ⚠\nIn order to do this safely, you must enumerate all secret Bases now. Any Basis not disclosed could be overwritten."
|
||||
},
|
||||
"pddb.rekey.request": {
|
||||
"en": "⚠ PDDB needs to be keyed to this device. ⚠\nIn order to do this safely, you must enumerate all secret Bases now. Any Basis not disclosed could be overwritten.",
|
||||
"ja": "⚠ PDDBのキーを再入力する必要があります. ⚠\nこれを安全に行うには、今すぐすべてのシークレットベースのロックを解除する必要があります。 ロックされたままのベーシスは上書きされる可能性があります。",
|
||||
"zh": "⚠ PDDB 需要重新加密。⚠\n为了安全地执行此操作,您现在必须解锁所有秘密基地。 任何被锁定的基础都可以被覆盖。",
|
||||
"en-tts": "⚠ PDDB needs to be keyed to this device. ⚠\nIn order to do this safely, you must enumerate all secret Bases now. Any Basis not disclosed could be overwritten."
|
||||
},
|
||||
"pddb.freespace.enumerate": {
|
||||
"en": "Enumerate a Basis?",
|
||||
"ja": "根拠を開示しますか?",
|
||||
|
|
@ -41,6 +65,12 @@
|
|||
"zh": "我们要继续生成可用空间缓存吗?",
|
||||
"en-tts": "Enumeration terminated.\n\nShall we proceed to generate the free space cache?"
|
||||
},
|
||||
"pddb.rekey.finished": {
|
||||
"en": "Enumeration terminated.\n\nShall we proceed to rekeying?",
|
||||
"ja": "キーの再生成に進みましょう?",
|
||||
"zh": "继续重新加密?",
|
||||
"en-tts": "Enumeration terminated.\n\nShall we proceed to rekeying?"
|
||||
},
|
||||
"pddb.okay": {
|
||||
"en": "Okay",
|
||||
"ja": "OK",
|
||||
|
|
|
|||
|
|
@ -76,6 +76,26 @@ pub(crate) const FSCB_FILL_COEFFICIENT: f32 = 0.5;
|
|||
#[allow(dead_code)]
|
||||
pub(crate) const FSCB_FILL_UNCERTAINTY: f32 = 0.1;
|
||||
|
||||
/// This is a number that represents a fraction out of 255 that is the chance of a given
|
||||
/// block of unused data being recycled with noise. (So 26 would be roughly a 10% chance).
|
||||
/// This is an optimization that makes PDDB migrations/restores faster, while maintaining
|
||||
/// some amount of deniability. The higher the chance of rekey, the better the deniability.
|
||||
///
|
||||
/// If you set the REKEY_CHANCE to 0, then after a migration or rekey operation,
|
||||
/// exactly the set of data that is used will have its ciphertext changed, which is a precise
|
||||
/// leak of the amount of information in the PDDB.
|
||||
///
|
||||
/// If you set REKEY_CHANCE to 256 or greater, then, every block is mutated, regardless of
|
||||
/// the usage state of the PDDB, and full deniability is preserved. However, this will cause
|
||||
/// the rekey operation to always take about a half hour, even if the PDDB is basically empty.
|
||||
///
|
||||
/// The initial 10% threshold gives us sufficient deniability for e.g. small secrets like
|
||||
/// passwords, TOTP tokens, U2F keys while incurring just a couple extra minutes overhead.
|
||||
/// However it would probably not be sufficient if you routinely use the PDDB to store
|
||||
/// large objects like images, audio, or large blocks of text.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const FAST_REKEY_CHANCE: u32 = 26;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub const PDDB_DEFAULT_SYSTEM_BASIS: &'static str = ".System";
|
||||
// this isn't an "official" basis, but it is used for the AAD for encrypting the FastSpace structure
|
||||
|
|
@ -178,6 +198,9 @@ pub(crate) enum Opcode {
|
|||
/// Remove an empty dict
|
||||
DeleteDictStd = 41,
|
||||
|
||||
/// Rekey the PDDB
|
||||
RekeyPddb = 42,
|
||||
|
||||
/// This key type could not be decoded
|
||||
InvalidOpcode = u32::MAX as _,
|
||||
}
|
||||
|
|
@ -388,6 +411,46 @@ impl PddbKeyAttrIpc {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Eq, PartialEq)]
|
||||
pub enum PddbRekeyOp {
|
||||
/// rekeys the a restored PDDB to the current device DNA using the "fast" method.
|
||||
/// The "fast" method is significantly faster on PDDBs with a small amount of data, but
|
||||
/// it will leak information on the amount of data in the PDDB, in a manner that can be
|
||||
/// trivially recovered by doing comparative ciphertext analysis between the backup and
|
||||
/// the current database image. *Some* amount of chaffe data is written, but only a
|
||||
/// small amount.
|
||||
FromDnaFast(u64),
|
||||
/// same as the above, but blank space is also turned over, guaranteeing the deniability
|
||||
/// of stored data even if an attacker has the previous backup copy of the PDDB.
|
||||
FromDnaSafe(u64),
|
||||
/// Basically the same as FromDnaSafe, but doing a self-to-self "safe" rekey
|
||||
Churn,
|
||||
/*
|
||||
// skip this implementation for now. This opcode fits generally into this code flow,
|
||||
// but requires some rework of the UX to actually acquire the old and new passwords.
|
||||
// this UX work is off-topic from the mission of getting backup restoration done,
|
||||
// but the potential to integrate the password rotation scheme into this function is
|
||||
// noted here for future efforts.
|
||||
//
|
||||
/// Requests a single secret basis to have its password changed. This will reveal the size
|
||||
/// of the Basis if the attacker has a before-and-after image of the PDDB.
|
||||
/// Recommended to call `Churn` after this operation is done for optimal safety.
|
||||
///
|
||||
/// Note: changing the password on the .System basis is a different flow. The
|
||||
/// system basis keys are encrypted directly by the rootkeys enclave, so changing
|
||||
/// its password requires calling a routine in root_keys (that does not exist
|
||||
/// at this current time).
|
||||
ChangePass(xous_ipc::String<BASIS_NAME_LEN>),
|
||||
*/
|
||||
|
||||
/// Return codes
|
||||
Success,
|
||||
AuthFail,
|
||||
UserAbort,
|
||||
VerifyFail,
|
||||
InternalError,
|
||||
}
|
||||
|
||||
/// Debugging commands, available only in hosted mode
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
|
|
|
|||
|
|
@ -268,6 +268,9 @@ impl BasisCache {
|
|||
}
|
||||
al
|
||||
}
|
||||
pub(crate) fn rekey(&self, hw: &mut PddbOs, op: PddbRekeyOp) -> PddbRekeyOp {
|
||||
hw.pddb_rekey(op, &self.cache)
|
||||
}
|
||||
fn select_basis(&self, basis_name: Option<&str>) -> Option<usize> {
|
||||
if self.cache.len() == 0 {
|
||||
log::error!("Can't select basis: PDDB is not mounted");
|
||||
|
|
|
|||
|
|
@ -82,7 +82,11 @@ impl Deref for StaticCryptoData {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
enum DnaMode {
|
||||
Normal,
|
||||
Migration,
|
||||
}
|
||||
#[derive(Zeroize)]
|
||||
#[zeroize(drop)]
|
||||
pub(crate) struct BasisKeys {
|
||||
|
|
@ -90,6 +94,14 @@ pub(crate) struct BasisKeys {
|
|||
pub(crate) data: [u8; AES_KEYSIZE]
|
||||
}
|
||||
|
||||
struct MigrationCiphers {
|
||||
pt_ecb: Aes256,
|
||||
data_gcm_siv: AesGcmSiv<Aes256>,
|
||||
data_key: [u8; 32],
|
||||
aad_incoming: Vec::<u8>,
|
||||
aad_local: Vec::<u8>,
|
||||
}
|
||||
|
||||
// emulated
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
type EmuMemoryRange = EmuStorage;
|
||||
|
|
@ -131,6 +143,10 @@ pub(crate) struct PddbOs {
|
|||
fspace_log_len: usize,
|
||||
/// a cached copy of the FPGA's DNA ID, used in the AAA records.
|
||||
dna: u64,
|
||||
/// DNA for migrations from restored backups coming from different devices
|
||||
migration_dna: u64,
|
||||
/// set which DNA to use
|
||||
dna_mode: DnaMode,
|
||||
/// reference to a TrngPool object that's shared among all the hardware functions
|
||||
entropy: Rc<RefCell<TrngPool>>,
|
||||
/// connection to the password request manager
|
||||
|
|
@ -162,6 +178,7 @@ impl PddbOs {
|
|||
log::debug!("fscb_phys_base: {:x?}", fscb_phys_base);
|
||||
|
||||
let llio = llio::Llio::new(&xns);
|
||||
let dna = llio.soc_dna().unwrap();
|
||||
// native hardware
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
let ret = PddbOs {
|
||||
|
|
@ -180,7 +197,10 @@ impl PddbOs {
|
|||
fspace_log_addrs: Vec::<PageAlignedPa>::new(),
|
||||
fspace_log_next_addr: None,
|
||||
fspace_log_len: 0,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
dna,
|
||||
// default to our own DNA in this case
|
||||
migration_dna: dna,
|
||||
dna_mode: DnaMode::Normal,
|
||||
entropy: trngpool,
|
||||
pw_cid,
|
||||
#[cfg(all(feature="pddbtest", feature="autobasis"))]
|
||||
|
|
@ -205,7 +225,10 @@ impl PddbOs {
|
|||
fspace_log_addrs: Vec::<PageAlignedPa>::new(),
|
||||
fspace_log_next_addr: None,
|
||||
fspace_log_len: 0,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
dna,
|
||||
// default to our own DNA in this case
|
||||
migration_dna: dna,
|
||||
dna_mode: DnaMode::Normal,
|
||||
entropy: trngpool,
|
||||
pw_cid,
|
||||
#[cfg(all(feature="pddbtest", feature="autobasis"))]
|
||||
|
|
@ -661,7 +684,10 @@ impl PddbOs {
|
|||
fn fast_space_aad(&self, aad: &mut Vec::<u8>) {
|
||||
aad.extend_from_slice(PDDB_FAST_SPACE_SYSTEM_BASIS.as_bytes());
|
||||
aad.extend_from_slice(&PDDB_VERSION.to_le_bytes());
|
||||
aad.extend_from_slice(&self.dna.to_le_bytes());
|
||||
match self.dna_mode {
|
||||
DnaMode::Normal => aad.extend_from_slice(&self.dna.to_le_bytes()),
|
||||
DnaMode::Migration => aad.extend_from_slice(&self.migration_dna.to_le_bytes()),
|
||||
}
|
||||
}
|
||||
/// Assumes you are writing a "most recent" version of FastSpace. Thus
|
||||
/// Anytime the fscb is updated, all the partial records are nuked, as well as any existing record.
|
||||
|
|
@ -1210,7 +1236,10 @@ impl PddbOs {
|
|||
let mut aad = Vec::<u8>::new();
|
||||
aad.extend_from_slice(&name.as_bytes());
|
||||
aad.extend_from_slice(&PDDB_VERSION.to_le_bytes());
|
||||
aad.extend_from_slice(&self.dna.to_le_bytes());
|
||||
match self.dna_mode {
|
||||
DnaMode::Normal => aad.extend_from_slice(&self.dna.to_le_bytes()),
|
||||
DnaMode::Migration => aad.extend_from_slice(&self.migration_dna.to_le_bytes()),
|
||||
}
|
||||
aad
|
||||
}
|
||||
|
||||
|
|
@ -1806,6 +1835,243 @@ impl PddbOs {
|
|||
}
|
||||
}
|
||||
|
||||
/// pddb_rekey() must be called from a state where all the data has been synchronized to disk.
|
||||
///
|
||||
/// If there are items allocated in the FSCB that have not had their corresponding physical
|
||||
/// page table entries on disk written, the operation will fail.
|
||||
pub(crate) fn pddb_rekey(&mut self, op: PddbRekeyOp, cache: &Vec::<BasisCacheEntry>) -> PddbRekeyOp {
|
||||
match op {
|
||||
PddbRekeyOp::FromDnaFast(dna) |
|
||||
PddbRekeyOp::FromDnaSafe(dna) => {
|
||||
if cache.len() != 0 {
|
||||
log::error!("We can't have any previously mounted Bases if the PDDB has the wrong key.");
|
||||
return PddbRekeyOp::InternalError;
|
||||
}
|
||||
self.migration_dna = dna;
|
||||
// acquire the .System basis key.
|
||||
self.dna_mode = DnaMode::Migration;
|
||||
self.syskey_ensure(); // this routine will continue to bang the user for a password. There is no way out otherwise.
|
||||
log::info!("migrating from dna 0x{:x} -> 0x{:x}", self.migration_dna, self.dna);
|
||||
self.fast_space_read(); // we hawe to re-read the FSCB, because it would have failed previously with lots of warnings
|
||||
},
|
||||
_ => (),
|
||||
};
|
||||
|
||||
// clean up any MBBB, as it will mess up our algorithm
|
||||
if let Some(mbbb) = self.mbbb_retrieve() {
|
||||
if let Some(erased_offset) = self.pt_find_erased_slot() {
|
||||
self.spinor.patch(
|
||||
self.pddb_mr.as_slice(),
|
||||
xous::PDDB_LOC,
|
||||
&mbbb,
|
||||
self.pt_phys_base.as_u32() + erased_offset,
|
||||
).expect("couldn't write to page table");
|
||||
}
|
||||
self.mbbb_erase();
|
||||
}
|
||||
|
||||
// 0. acquire all the keys
|
||||
if let Some(all_keys) = self.pddb_get_all_keys(&cache) {
|
||||
// build a modals for rekey progress
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
let modals = modals::Modals::new(&xns).unwrap();
|
||||
|
||||
modals.start_progress(t!("pddb.rekey.keys", xous::LANG), 0, all_keys.len() as u32, 0).ok();
|
||||
// we need a map of page numbers to encryption keys. The keys are referenced by their basis name.
|
||||
let mut pagemap = HashMap::<PhysAddr, &str>::new();
|
||||
// transform the returned Vec into a HashMap that maps basis names into pre-keyed ciphers.
|
||||
let mut keymap = HashMap::<&str, MigrationCiphers>::new();
|
||||
for (index, (basis_keys, name)) in all_keys.iter().enumerate() {
|
||||
if let Some(map) = self.pt_scan_key(&basis_keys.pt, &basis_keys.data, &name) {
|
||||
for pp in map.values() {
|
||||
log::info!("page {} -> basis {}", pp.page_number(), name);
|
||||
if pagemap.insert(pp.page_number(), &name).is_some() {
|
||||
log::warn!("double-insert detected of page number {}", pp.page_number());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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::<Aes256>::new(Key::from_slice(&basis_keys.data));
|
||||
self.dna_mode = DnaMode::Normal;
|
||||
let aad_local = self.data_aad(&name);
|
||||
self.dna_mode = DnaMode::Migration;
|
||||
let aad_incoming = self.data_aad(&name);
|
||||
keymap.insert(&name, MigrationCiphers { pt_ecb, data_gcm_siv, aad_incoming, aad_local, data_key: basis_keys.data.into() });
|
||||
modals.update_progress(index as u32 + 1).ok();
|
||||
}
|
||||
|
||||
// check that we have no pages in the FSCB that aren't already mapped in the
|
||||
// page table.
|
||||
let mut clean = true;
|
||||
for pp in self.fspace_cache.iter() {
|
||||
if pp.space_state() == SpaceState::Used || pp.space_state() == SpaceState::MaybeUsed {
|
||||
if !pagemap.contains_key(&pp.page_number()) {
|
||||
log::warn!(
|
||||
"FSCB records page {} ({:x?}) as used, but it's not defined in the page table. Please sync the PDDB before calling rekey.",
|
||||
pp.page_number(),
|
||||
pp
|
||||
);
|
||||
clean = false; // allow full enumeration of all errors, to help with debugging
|
||||
}
|
||||
}
|
||||
}
|
||||
modals.finish_progress().ok();
|
||||
if !clean {
|
||||
return PddbRekeyOp::VerifyFail
|
||||
}
|
||||
|
||||
// future note: if changing the password on just one basis, we'd want to ask for the new
|
||||
// password now, so we can use its new key to re-encrypt things. But for now, let's just
|
||||
// focus on rotating the DNA out.
|
||||
|
||||
// iterate through every possible page in the system.
|
||||
// First, map the PageTableInFlash structure directly onto the region. It's located
|
||||
// at the base of the `pddb_mr`.
|
||||
let pddb_data_len = PDDB_A_LEN - self.data_phys_base.as_usize();
|
||||
let pddb_data_pages = pddb_data_len / PAGE_SIZE;
|
||||
let pagetable: &[u8] = &self.pddb_mr.as_slice()[..pddb_data_pages * size_of::<Pte>()];
|
||||
log::info!("page table of len 0x{:x}", pagetable.len());
|
||||
let entries_per_page = PAGE_SIZE / size_of::<Pte>();
|
||||
modals.start_progress(t!("pddb.rekey.running", xous::LANG), 0, (pddb_data_pages * size_of::<Pte>()) as u32, 0).ok();
|
||||
for (chunk_enum, page) in pagetable.chunks(PAGE_SIZE).enumerate() {
|
||||
// this is the actual offset into pagetable[] that the page[] slice comes from
|
||||
let chunk_start_address = chunk_enum * PAGE_SIZE;
|
||||
let chunk_start_ppn = chunk_enum * entries_per_page;
|
||||
let mut chunk_len = 0; // we have to track this because the .chunks() will return a less-than-page sized chunk on the last iteration
|
||||
log::info!("processing chunk at 0x{:x} (page number {}", chunk_start_address, chunk_start_ppn);
|
||||
|
||||
let mut new_pt_page = [0u8; PAGE_SIZE];
|
||||
assert!(page.len() % size_of::<Pte>() == 0);
|
||||
for (index, (enc_pte, new_pte)) in
|
||||
page.chunks_exact(size_of::<Pte>())
|
||||
.zip(new_pt_page.chunks_exact_mut(size_of::<Pte>()))
|
||||
.enumerate() {
|
||||
let ppn = (chunk_start_ppn + index) as u32;
|
||||
chunk_len += size_of::<Pte>();
|
||||
if let Some(&keyname) = pagemap.get(&ppn) {
|
||||
log::info!("re-encrypt page {} of {}", ppn, keyname);
|
||||
let ciphers = keymap.get(keyname).expect("How do we have a mapping to a key that we don't have?");
|
||||
// ok, we know the physical page table number, and thus the page location, and the keys. we can
|
||||
// now do the following:
|
||||
// 1. re-encrypt the page table. It is relatively "free" to do a thorough re-encryption
|
||||
// so we will always either redo the nonce on every entry and/or fill the unused entries
|
||||
// with fresh noise.
|
||||
let mut block = Block::clone_from_slice(enc_pte);
|
||||
ciphers.pt_ecb.decrypt_block(&mut block);
|
||||
if let Some(mut pte) = Pte::try_from_slice(block.as_slice()) {
|
||||
pte.re_nonce(Rc::clone(&self.entropy));
|
||||
let mut enc_entry = Block::from_mut_slice(pte.deref_mut());
|
||||
ciphers.pt_ecb.encrypt_block(&mut enc_entry);
|
||||
new_pte.copy_from_slice(enc_entry.as_slice());
|
||||
}
|
||||
|
||||
// 2. re-encrypt the disclosed basis data from the previous DNA to the current DNA. Redo all the nonces
|
||||
// while we are at it.
|
||||
if let Some(mut data) = self.data_decrypt_page(
|
||||
&ciphers.data_gcm_siv,
|
||||
&ciphers.aad_incoming,
|
||||
&PhysPage(ppn)) {
|
||||
// this routine also redoes the nonce correctly.
|
||||
self.data_encrypt_and_patch_page(
|
||||
&ciphers.data_gcm_siv,
|
||||
&ciphers.aad_local,
|
||||
&mut data,
|
||||
&PhysPage(ppn)
|
||||
);
|
||||
} else {
|
||||
// see if it's a root page, which is encrypted differently due to AES-GCM-SIV salamanders and key commits.
|
||||
if let Some(mut data) = self.data_decrypt_page_with_commit(
|
||||
&ciphers.data_key,
|
||||
&ciphers.aad_incoming,
|
||||
&PhysPage(ppn)) {
|
||||
log::info!("...as basis root page...");
|
||||
self.data_encrypt_and_patch_page_with_commit(
|
||||
&ciphers.data_key,
|
||||
&ciphers.aad_local,
|
||||
&mut data,
|
||||
&PhysPage(ppn)
|
||||
);
|
||||
} else {
|
||||
log::warn!("Page number {} failed to decrypt. This data is now lost.", ppn);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// it's an unused page. fill in the PTE with garbage, and *maybe* fill in the mapped page
|
||||
// with new garbage, too.
|
||||
self.trng_slice(new_pte);
|
||||
|
||||
// the chance is expressed as a number from 0-255. It is compared against an 8-bit
|
||||
// random number, and if the random number is less than the fraction, a rekey op will happen
|
||||
let blank_rekey_chance = match op {
|
||||
PddbRekeyOp::FromDnaFast(_) => FAST_REKEY_CHANCE,
|
||||
PddbRekeyOp::FromDnaSafe(_) => 256, // 100% chance of rekey
|
||||
PddbRekeyOp::Churn => 256, // 100% chance of rekey
|
||||
_ => continue, // don't rekey blanks for all other ops
|
||||
};
|
||||
if (self.trng_u8() as u32) < blank_rekey_chance {
|
||||
log::info!("re-noising unused page {}", ppn);
|
||||
let mut noise = [0u8; PAGE_SIZE];
|
||||
self.trng_slice(&mut noise);
|
||||
self.patch_data(&noise, ppn * PAGE_SIZE as u32);
|
||||
} else {
|
||||
log::trace!("skipping unused page {}", ppn);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.patch_pagetable_raw(&new_pt_page[..chunk_len], chunk_start_address as u32);
|
||||
// this only updates once per page of PTEs, so, every 256 pages that get re-encrypted in the worst case.
|
||||
modals.update_progress(chunk_start_address as u32).ok();
|
||||
}
|
||||
modals.finish_progress().ok();
|
||||
|
||||
self.dna_mode = DnaMode::Normal; // we're done with the legacy DNA!
|
||||
|
||||
// 3. if we are not just changing the password on one Basis, regenerate the FSCB, since we
|
||||
// have all the Bases open
|
||||
let do_fscb = match op {
|
||||
PddbRekeyOp::FromDnaFast(_dna) |
|
||||
PddbRekeyOp::FromDnaSafe(_dna) => true,
|
||||
PddbRekeyOp::Churn => true,
|
||||
_ => false,
|
||||
};
|
||||
if do_fscb {
|
||||
log::info!("regenerating fast space...");
|
||||
modals.dynamic_notification(Some(t!("pddb.rekey.fastspace", xous::LANG)), None).ok();
|
||||
// convert our used page map into the structure needed by fast_space_generate()
|
||||
let mut page_heap = BinaryHeap::new();
|
||||
// drain doesn't actually de-allocate memory, but it gives us an opportunity
|
||||
// to recode this to use shrink_to_fit() every so many iterations to save on RAM
|
||||
// if we're bumping our head into that problem.
|
||||
for (ppn, _name) in pagemap.drain() {
|
||||
page_heap.push(Reverse(ppn));
|
||||
}
|
||||
// generate and nuke old free space records
|
||||
let free_pool = self.fast_space_generate(page_heap);
|
||||
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;
|
||||
}
|
||||
// write just commits a new record to disk, but doesn't update our internal data cache
|
||||
// this also clears the fast space log.
|
||||
self.fast_space_write(&fast_space);
|
||||
// this will ensure the data cache is fully in sync
|
||||
self.fast_space_read();
|
||||
modals.dynamic_notification_close().ok();
|
||||
log::info!("fast space generation done.");
|
||||
}
|
||||
PddbRekeyOp::Success
|
||||
} else {
|
||||
PddbRekeyOp::UserAbort
|
||||
}
|
||||
}
|
||||
|
||||
/// UX function that informs the user of the currently open Basis, and prompts the user to enter passwords
|
||||
/// for other basis that may not currently be open. This function is also responsible for validating
|
||||
/// that the password is correct by doing a quick scan for "any" PTEs that happen to decrypt to something
|
||||
|
|
@ -1824,12 +2090,31 @@ impl PddbOs {
|
|||
for entry in cache {
|
||||
log::info!(" - {}", entry.name);
|
||||
}
|
||||
// In the case of a migration, the basis cache would be empty, but the system basis key is already set up
|
||||
if self.dna_mode == DnaMode::Migration {
|
||||
let syskeys = self.system_basis_key.as_ref()
|
||||
.expect("Internal error: syskey_ensure() must be called prior to get_all_keys()");
|
||||
ret.push((
|
||||
BasisKeys {
|
||||
pt: syskeys.pt.into(),
|
||||
data: syskeys.data.into(),
|
||||
},
|
||||
PDDB_DEFAULT_SYSTEM_BASIS.to_string()
|
||||
));
|
||||
// note: because self.dna_mode is in 'Migration', the AAD checks in this section will be
|
||||
// using the original DNA, so they should pass.
|
||||
}
|
||||
|
||||
// 0. allow user to cancel out of the operation -- this will abort everything and cause the current
|
||||
// alloc operation to fail
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
let modals = modals::Modals::new(&xns).unwrap();
|
||||
modals.show_notification(t!("pddb.freespace.request", xous::LANG), None).ok();
|
||||
modals.show_notification(
|
||||
match self.dna_mode {
|
||||
DnaMode::Normal => t!("pddb.freespace.request", xous::LANG),
|
||||
DnaMode::Migration => t!("pddb.rekey.request", xous::LANG),
|
||||
},
|
||||
None).ok();
|
||||
self.tt.sleep_ms(SWAP_DELAY_MS).unwrap();
|
||||
|
||||
// 0.5 display the Bases that we know
|
||||
|
|
@ -1904,7 +2189,12 @@ impl PddbOs {
|
|||
self.tt.sleep_ms(SWAP_DELAY_MS).unwrap();
|
||||
}
|
||||
// done!
|
||||
if self.yes_no_approval(&modals, t!("pddb.freespace.finished", xous::LANG)) {
|
||||
if self.yes_no_approval(
|
||||
&modals,
|
||||
match self.dna_mode {
|
||||
DnaMode::Normal => t!("pddb.freespace.finished", xous::LANG),
|
||||
DnaMode::Migration => t!("pddb.rekey.finished", xous::LANG),
|
||||
}) {
|
||||
Some(ret)
|
||||
} else {
|
||||
None
|
||||
|
|
@ -2061,7 +2351,7 @@ impl PddbOs {
|
|||
}
|
||||
/// In testing, we want a way to automatically unlock all known test Bases. This stand-in feature automates that.
|
||||
#[cfg(all(feature="pddbtest", feature="autobasis"))]
|
||||
pub(crate) fn pddb_get_all_keys<'a>(&'a self, cache: &'a Vec::<BasisCacheEntry>) -> Option<Vec<(BasisKeys, String)>> {
|
||||
pub(crate) fn pddb_get_all_keys<'a>(&'a self, cache: &'a Vec::<BasisCacheEntry>, _op: GetKeyOp) -> Option<Vec<(BasisKeys, String)>> {
|
||||
// populate the "known" entries
|
||||
let mut ret = Vec::<(BasisKeys, String)>::new();
|
||||
for entry in cache {
|
||||
|
|
|
|||
|
|
@ -112,6 +112,16 @@ impl Pte {
|
|||
None
|
||||
}
|
||||
}
|
||||
/// Normally you should be using pt_patch_mapping(), which generates a new nonce every
|
||||
/// time the entry is patched. However, this function is provided for "bulk" operations
|
||||
/// such as migrations where we violate the abstractions to improve performance.
|
||||
pub fn re_nonce(&mut self, entropy: Rc<RefCell<TrngPool>>) {
|
||||
let nonce_u32 = entropy.borrow_mut().get_u32();
|
||||
self.nonce = nonce_u32.to_le_bytes();
|
||||
let pte_data = self.deref();
|
||||
let checksum = murmur3_32(&pte_data[..12], nonce_u32);
|
||||
self.checksum = checksum.to_le_bytes();
|
||||
}
|
||||
}
|
||||
impl Deref for Pte {
|
||||
type Target = [u8];
|
||||
|
|
|
|||
|
|
@ -628,6 +628,24 @@ impl Pddb {
|
|||
panic!("Internal error: wrong return code for is_efuse_secured()");
|
||||
}
|
||||
}
|
||||
/// Rekey the PDDB. This can be a very long-running blocking operation that will definitely.
|
||||
/// interrupt normal user flow.
|
||||
pub fn rekey_pddb(&self, op: PddbRekeyOp) -> Result<()> {
|
||||
let mut buf = Buffer::into_buf(op)
|
||||
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||
buf.lend_mut(self.conn, Opcode::RekeyPddb.to_u32().unwrap())
|
||||
.or(Err(Error::new(ErrorKind::Other, "Xous internal error"))).map(|_| ())?;
|
||||
let result = buf.to_original::<PddbRekeyOp, _>()
|
||||
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||
match result {
|
||||
PddbRekeyOp::Success => Ok(()),
|
||||
PddbRekeyOp::AuthFail => Err(Error::new(ErrorKind::PermissionDenied, "Permission error")),
|
||||
PddbRekeyOp::UserAbort => Err(Error::new(ErrorKind::Interrupted, "User aborted operation")),
|
||||
PddbRekeyOp::InternalError => Err(Error::new(ErrorKind::Other, "Xous internal error")),
|
||||
PddbRekeyOp::VerifyFail => Err(Error::new(ErrorKind::InvalidData, "Data verification error while rekeying")),
|
||||
_ => Err(Error::new(ErrorKind::Unsupported, "Return code was never set")),
|
||||
}
|
||||
}
|
||||
/// Triggers a dump of the PDDB to host disk
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
pub fn dbg_dump(&self, name: &str) -> Result<()> {
|
||||
|
|
|
|||
|
|
@ -1390,6 +1390,12 @@ fn wrapped_main() -> ! {
|
|||
}
|
||||
modals.show_notification(¬e, None).expect("couldn't show basis list");
|
||||
},
|
||||
Opcode::RekeyPddb => {
|
||||
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||
let rekey_op = buffer.to_original::<PddbRekeyOp, _>().unwrap();
|
||||
let result = basis_cache.rekey(&mut pddb_os, rekey_op);
|
||||
buffer.replace(result).unwrap();
|
||||
}
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
Opcode::DangerousDebug => {
|
||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
|
|
@ -1507,7 +1513,7 @@ fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cach
|
|||
}
|
||||
// correct password but no mount -> offer to format; uninit -> offer to format
|
||||
if pw_state == PasswordState::Correct || pw_state == PasswordState::Uninit {
|
||||
#[cfg(any(target_os = "none", target_os = "xous"))]
|
||||
#[cfg(any(target_os = "none", target_os = "xous", feature="test-rekey"))]
|
||||
{
|
||||
log::debug!("PDDB did not mount; requesting format");
|
||||
modals.add_list_item(t!("pddb.okay", xous::LANG)).expect("couldn't build radio item list");
|
||||
|
|
@ -1576,7 +1582,7 @@ fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cach
|
|||
false
|
||||
}
|
||||
}
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous", feature="test-rekey")))]
|
||||
{
|
||||
pddb_os.pddb_format(false, Some(&modals)).expect("couldn't format PDDB");
|
||||
let _ = xous::send_message(time_resetter,
|
||||
|
|
|
|||
Loading…
Reference in a new issue