diff --git a/libs/bao1x-hal/src/acram.rs b/libs/bao1x-hal/src/acram.rs index 68106a8a0..e65ff42ec 100644 --- a/libs/bao1x-hal/src/acram.rs +++ b/libs/bao1x-hal/src/acram.rs @@ -360,6 +360,27 @@ impl SlotManager { } } + /// Safety: may return garbage data if the ACLs aren't correct. No security is compromised + /// in the case that the ACL isn't correct, but the caller would not be notified of the + /// mismatch. This call is mostly useful for verifying that data is in fact being disregarded + /// or erased. + /// + /// SlotIndex is also assumed to be correct; if it is invalid, the function will panic. + pub unsafe fn read_unchecked(&self, slot: &SlotIndex) -> &[u8] { + let offset = slot.try_into_data_offset().unwrap(); + + // safety: the unsafes below remind us to check that all values are valid during + // the pointer type cast. In this case, there are no invalid values for a `u8`. + match slot { + SlotIndex::Data(_, _, _) => unsafe { + &self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES] + }, + SlotIndex::DataRange(range, _, _) => unsafe { + &self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES * range.len()] + }, + } + } + /// Safety: the caller must resolve the index into either the data or the key array correctly. /// There is access or type checking done. This is a "raw read" primitive mostly used for debugging. pub unsafe fn read_data_slot(&self, absolute_offset: usize) -> &[u8] { diff --git a/libs/bao1x-hal/src/sigcheck.rs b/libs/bao1x-hal/src/sigcheck.rs index 59469b25d..5f8599aa5 100644 --- a/libs/bao1x-hal/src/sigcheck.rs +++ b/libs/bao1x-hal/src/sigcheck.rs @@ -9,7 +9,7 @@ use bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS; use bao1x_api::pubkeys::SecurityConfiguration; use bao1x_api::signatures::*; #[cfg(not(feature = "std"))] -use bao1x_api::{DEVELOPER_MODE, DataSlotAccess, RwPerms, SLOT_ELEMENT_LEN_BYTES, SlotType}; +use bao1x_api::{DEVELOPER_MODE, DataSlotAccess, RwPerms, SLOT_ELEMENT_LEN_BYTES}; use digest::Digest; use sha2_bao1x::{Sha256, Sha512}; use xous::arch::PAGE_SIZE; @@ -318,37 +318,35 @@ pub fn erase_collateral(csprng: &mut Option<&mut Csprng>) -> Result<(), String> let mut rram = crate::rram::Reram::new(); let slot = &bao1x_api::offsets::COLLATERAL; - for data_index in slot.try_into_data_iter().unwrap() { - bollard!(die_no_std, 4); - csprng.as_deref_mut().map(|rng| rng.random_delay()); - // only clear ACL if it isn't already cleared - if slot_mgr - .get_acl(slot) - .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) - .raw_u32() - != 0 - { - // clear the ACL so we can operate on the data - slot_mgr - .set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0))) - .expect("couldn't reset ACL"); - } - let bytes = unsafe { slot_mgr.read_data_slot(data_index) }; - // only erase if the key hasn't already been erased, to avoid stressing the RRAM array - // erase_secrets() may be called on every boot in some modes. - bollard!(die_no_std, 4); - if !bytes.iter().all(|&b| b == ERASE_VALUE) { - let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); - eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); - - slot_mgr.write(&mut rram, slot, &eraser).ok(); - } - let check = unsafe { slot_mgr.read_data_slot(data_index) }; - if !check.iter().all(|&b| b == ERASE_VALUE) { - crate::println!("Failed to erase key at {}: {:x?}", data_index, check); - } - bollard!(die_no_std, 4); + bollard!(die_no_std, 4); + csprng.as_deref_mut().map(|rng| rng.random_delay()); + // only clear ACL if it isn't already cleared + if slot_mgr + .get_acl(slot) + .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) + .raw_u32() + != 0 + { + // clear the ACL so we can operate on the data + slot_mgr + .set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0))) + .expect("couldn't reset ACL"); } + let bytes = unsafe { slot_mgr.read_unchecked(slot) }; + // only erase if the key hasn't already been erased, to avoid stressing the RRAM array + // erase_secrets() may be called on every boot in some modes. + bollard!(die_no_std, 4); + if !bytes.iter().all(|&b| b == ERASE_VALUE) { + let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); + eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); + + slot_mgr.write(&mut rram, slot, &eraser).ok(); + } + let check = unsafe { slot_mgr.read_unchecked(slot) }; + if !check.iter().all(|&b| b == ERASE_VALUE) { + crate::println!("Failed to erase key at {:?}: {:x?}", slot, check); + } + bollard!(die_no_std, 4); Ok(()) } @@ -379,72 +377,64 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> { for slot in crate::board::KEY_SLOTS.iter() { bollard!(die_no_std, 4); csprng.as_deref_mut().map(|rng| rng.random_delay()); - if slot.get_type() == SlotType::Data { - let (_pa, rw_perms) = slot.get_access_spec(); - let mut erased_keys = 0; - for data_index in slot.try_into_data_iter().unwrap() { - bollard!(die_no_std, 4); - csprng.as_deref_mut().map(|rng| rng.random_delay()); - match rw_perms { - RwPerms::ReadWrite | RwPerms::WriteOnly => { - // only clear ACL if it isn't already cleared - if slot_mgr - .get_acl(slot) - .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) - .raw_u32() - != 0 - { - // clear the ACL so we can operate on the data - match slot_mgr.set_acl( - &mut rram, - slot, - &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)), - ) { - Ok(_) => (), - Err(e) => { - crate::println!("Couldn't erase ACL: {:?}", e); - } - } - } - let bytes = unsafe { slot_mgr.read_data_slot(data_index) }; - if bytes.iter().all(|&b| b == 0) { - zero_key_count += 1; - } - // only erase if the key hasn't already been erased, to avoid stressing the RRAM array - // erase_secrets() may be called on every boot in some modes. - bollard!(die_no_std, 4); - if !bytes.iter().all(|&b| b == ERASE_VALUE) { - let mut eraser = - alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); - eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); - - slot_mgr.write(&mut rram, slot, &eraser).ok(); - } - let check = unsafe { slot_mgr.read_data_slot(data_index) }; - if !check.iter().all(|&b| b == ERASE_VALUE) { - crate::println!("Failed to erase key at {}: {:x?}", data_index, check); - /* // commented out - can lead to boot loops - // reboot on failure to erase - let mut rcurst = - utralib::CSR::new(utralib::utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); - rcurst.wo(utralib::utra::sysctrl::SFR_RCURST0, 0x55AA); - */ - } else { - erased_keys += 1; + let (_pa, rw_perms) = slot.get_access_spec(); + let mut erased_keys = 0; + csprng.as_deref_mut().map(|rng| rng.random_delay()); + match rw_perms { + RwPerms::ReadWrite | RwPerms::WriteOnly => { + // only clear ACL if it isn't already cleared + if slot_mgr + .get_acl(slot) + .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) + .raw_u32() + != 0 + { + // clear the ACL so we can operate on the data + match slot_mgr.set_acl( + &mut rram, + slot, + &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)), + ) { + Ok(_) => (), + Err(e) => { + crate::println!("Couldn't erase ACL: {:?}", e); } } - _ => {} } + // safety: this function knows to check or expect invalid ACL situations + let bytes = unsafe { slot_mgr.read_unchecked(slot) }; + if bytes.iter().all(|&b| b == 0) { + zero_key_count += 1; + } + // only erase if the key hasn't already been erased, to avoid stressing the RRAM array + // erase_secrets() may be called on every boot in some modes. bollard!(die_no_std, 4); + if !bytes.iter().all(|&b| b == ERASE_VALUE) { + let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); + eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); + slot_mgr.write(&mut rram, slot, &eraser).ok(); + } + let check = unsafe { slot_mgr.read_unchecked(slot) }; + if !check.iter().all(|&b| b == ERASE_VALUE) { + crate::println!("Failed to erase key at {:?}: {:x?}", slot, check); + /* // commented out - can lead to boot loops + // reboot on failure to erase + let mut rcurst = + utralib::CSR::new(utralib::utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); + rcurst.wo(utralib::utra::sysctrl::SFR_RCURST0, 0x55AA); + */ + } else { + erased_keys += 1; + } } - crate::println!( - "Key range at {}: {}/{} keys confirmed erased", - slot.get_base(), - erased_keys, - slot.len() - ); - bollard!(die_no_std, 4); + _ => {} } + crate::println!( + "Key range at {}: {}/{} keys confirmed erased", + slot.get_base(), + erased_keys, + slot.len() + ); bollard!(die_no_std, 4); } bollard!(die_no_std, 4);