From 8dc1e9c83769ed61919d5ea575d4b12cb4cddbcd Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 15 Dec 2025 15:19:01 +0800 Subject: [PATCH] rework backup RAM hashing to ignore the erase proof slice a slice of key material is written to backup RAM from boot0 all the way to the loader, to prove to the loader that secret keys were erased in a higher privilege state (loader then erases the key, and this key is considered leakable without harm to security). The problem is that writing the key to backup RAM in boot0 was fixing the hash on the backup block, which is before the check happens to see if it was a cold power-on. This would defeat the RTC setting & backup setup logic. THe solution to this is to exclude that one key from the hashing area and special case it. A set of routines and API names were added to help enforce this case. --- libs/bao1x-hal/src/buram.rs | 34 ++++++++++++++++++++++++++++++++-- libs/bao1x-hal/src/sigcheck.rs | 15 +++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/libs/bao1x-hal/src/buram.rs b/libs/bao1x-hal/src/buram.rs index 636e2fd97..917aeaa74 100644 --- a/libs/bao1x-hal/src/buram.rs +++ b/libs/bao1x-hal/src/buram.rs @@ -7,6 +7,10 @@ const HASH_LOC: usize = 0; const RESERVED_LOC: usize = 1; pub const ERASURE_PROOF_RANGE_BYTES: Range = 0..32; +// special case: don't hash in the erasure_proof range, because this +// value is passed between boot0 all the way to the loader for checking, +// *but* the consistency check on backup regs is done in boot1! +pub const HASH_REGION_START: usize = ERASURE_PROOF_RANGE_BYTES.end; use crate::buram::murmur3::murmur3_32; @@ -52,8 +56,9 @@ impl BackupManager { BackupManager { bu_reg, bu_ram } } + /// Validity is computed using a hash of the backup RAM region less the "erase proof" slice. pub fn is_backup_valid(&self) -> bool { - let bu_ram_u32: &[u32] = unsafe { self.bu_ram.as_slice() }; + let bu_ram_u32: &[u32] = &unsafe { self.bu_ram.as_slice() }[HASH_REGION_START..]; let hash = murmur3_32(bu_ram_u32, 0x0); let bu_reg: &[u32] = &unsafe { self.bu_reg.as_slice() }[..utralib::utra::aobureg::AOBUREG_NUMREGS]; hash == bu_reg[HASH_LOC] @@ -65,7 +70,7 @@ impl BackupManager { } pub fn make_valid(&mut self) { - let bu_ram_u32: &[u32] = &unsafe { self.bu_ram.as_slice() }; + let bu_ram_u32: &[u32] = &unsafe { self.bu_ram.as_slice() }[HASH_REGION_START..]; let hash = murmur3_32(bu_ram_u32, 0x0); unsafe { (self.bu_reg.as_mut_ptr() as *mut u32).add(HASH_LOC).write_volatile(hash); @@ -97,19 +102,44 @@ impl BackupManager { self.make_valid(); } + /// Safety: the caller needs to follow up with `make_valid()` for the backup registers to be recognized as + /// valid + pub unsafe fn store_slice_no_hash(&mut self, input: &[T], byte_offset: usize) { + let type_offset = byte_offset / size_of::(); + // safety: safe because make_valid() is called after the slice is accessed + let dest: &mut [T] = unsafe { &mut self.bu_ram_as_mut()[type_offset..type_offset + input.len()] }; + dest.copy_from_slice(&input); + } + pub fn get_slice(&self, byte_range: core::ops::Range) -> &[T] { let type_offset = byte_range.start / size_of::(); let type_len = (byte_range.end - byte_range.start) / size_of::(); &self.bu_ram_as_slice()[type_offset..type_offset + type_len] } + /// Returns the full array of the backup RAM, including the erase validation region. Required by + /// the slice-based accessors above. pub fn bu_ram_as_slice(&self) -> &[T] { unsafe { self.bu_ram.as_slice::() } } + /// Returns the full array of the backup RAM, including the erase validation region. Required + /// by the slice-based accessors above. + /// /// Safety: modifications to the backup RAM array need a follow-up call to `make_valid` /// in order for the boot check to pass. /// /// Could try to get clever and implement a `Drop` trait which includes a make_valid() call? maybe? pub unsafe fn bu_ram_as_mut(&mut self) -> &mut [T] { self.bu_ram.as_slice_mut::() } + + /// Returns only the *hashable* region of the backup RAM. The first 32 bytes are reserved for + /// passing erase validation structures through the boot loader. + /// + /// Safety: modifications to the backup RAM array need a follow-up call to `make_valid` + /// in order for the boot check to pass. + /// + /// Could try to get clever and implement a `Drop` trait which includes a make_valid() call? maybe? + pub unsafe fn bu_hashable_ram_as_mut(&mut self) -> &mut [T] { + &mut self.bu_ram.as_slice_mut::()[HASH_REGION_START..] + } } pub mod murmur3 { diff --git a/libs/bao1x-hal/src/sigcheck.rs b/libs/bao1x-hal/src/sigcheck.rs index f7a51d1d9..3e6309b84 100644 --- a/libs/bao1x-hal/src/sigcheck.rs +++ b/libs/bao1x-hal/src/sigcheck.rs @@ -289,7 +289,11 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> { // erase the backup RAM region to 0 that is the erasure proof. bollard!(die_no_std, 4); csprng.as_deref_mut().map(|rng| rng.random_delay()); - buram.store_slice(&[0u8; 32], crate::buram::ERASURE_PROOF_RANGE_BYTES.start); + // safety: these words are excluded from the hash check because we need to pass from boot0 through + // to the loader, but the "hard reset" check happens only in boot1 + unsafe { + buram.store_slice_no_hash(&[0u8; 32], crate::buram::ERASURE_PROOF_RANGE_BYTES.start); + } let mut zero_key_count = 0; // This is set to a higher level because we need to work around an earlier issue @@ -357,7 +361,14 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> { // store the proof that the key array was erased - could lead to disclosure of one key, // but we also can't simply trust that the oneway counter below is accurate csprng.as_deref_mut().map(|rng| rng.random_delay()); - buram.store_slice(slot_mgr.read(&crate::board::ERASE_PROOF).unwrap(), ERASURE_PROOF_RANGE_BYTES.start); + // safety: these words are excluded from the hash check because we need to pass from boot0 through + // to the loader, but the "hard reset" check happens only in boot1 + unsafe { + buram.store_slice_no_hash( + slot_mgr.read(&crate::board::ERASE_PROOF).unwrap(), + ERASURE_PROOF_RANGE_BYTES.start, + ); + } let owc = OneWayCounter::new(); // once all secrets are erased, advance the DEVELOPER_MODE state