From 15ac4d8529b1e86475b174cbc56d1be4307f2b81 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Dec 2025 18:36:51 +0800 Subject: [PATCH] add primitives to buram for storing slices this is used by the key erase proof routine, but will be useful in general for storing data in the buram also, the ranges of slices are currently defined at the top of the file but might be moved somewhere more maintainable later. RRAM writing got bollards added to it, as RRAM write could be a glitch target: if a glitch could manipulate the address somehow to stick stuff at an arbitrary location, during write, that would be bad. However, it's probably also reasonably dangerous to try and glitch RRAM writes because there's a chance you spray over off-target items and thus trigger a signature check failure on your next boot. --- libs/bao1x-hal/src/buram.rs | 25 +++++++++++++++++++++++-- libs/bao1x-hal/src/hardening.rs | 2 +- libs/bao1x-hal/src/rram.rs | 6 ++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/libs/bao1x-hal/src/buram.rs b/libs/bao1x-hal/src/buram.rs index 43112a5d0..636e2fd97 100644 --- a/libs/bao1x-hal/src/buram.rs +++ b/libs/bao1x-hal/src/buram.rs @@ -6,6 +6,8 @@ const HASH_LOC: usize = 0; #[allow(dead_code)] const RESERVED_LOC: usize = 1; +pub const ERASURE_PROOF_RANGE_BYTES: Range = 0..32; + use crate::buram::murmur3::murmur3_32; const fn range_len(r: Range) -> usize { r.end - r.start } @@ -86,9 +88,28 @@ impl BackupManager { } } - pub fn bu_ram_as_slice(&self) -> &[u8] { unsafe { self.bu_ram.as_slice() } } + pub fn store_slice(&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 bu_ram_as_mut(&mut self) -> &mut [u8] { unsafe { self.bu_ram.as_slice_mut() } } + self.make_valid(); + } + + 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] + } + + pub fn bu_ram_as_slice(&self) -> &[T] { unsafe { self.bu_ram.as_slice::() } } + + /// 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::() } } pub mod murmur3 { diff --git a/libs/bao1x-hal/src/hardening.rs b/libs/bao1x-hal/src/hardening.rs index d92f87d19..c85bd0edd 100644 --- a/libs/bao1x-hal/src/hardening.rs +++ b/libs/bao1x-hal/src/hardening.rs @@ -97,7 +97,7 @@ impl Csprng { word.copy_from_slice(&ro_trng.get_raw().to_ne_bytes()); } - crate::println!("seed: {:x?}", seed); // used to eyeball that things are working correctly + // crate::println!("seed: {:x?}", seed); // used to eyeball that things are working correctly let mut csprng = ChaCha8Rng::from_seed(seed); let entropy_bank = csprng.next_u64(); diff --git a/libs/bao1x-hal/src/rram.rs b/libs/bao1x-hal/src/rram.rs index 11760a60c..5a1e7e29e 100644 --- a/libs/bao1x-hal/src/rram.rs +++ b/libs/bao1x-hal/src/rram.rs @@ -188,8 +188,12 @@ impl<'a> Reram { assert!(data.len() % 8 == 0, "unaligned source data!"); // crate::print!("@ {:x} > ", addr); for (outer, d) in data.chunks_exact(8).enumerate() { + #[cfg(not(feature = "kernel"))] + bao1x_api::bollard!(crate::sigcheck::die_no_std, 4); // write the data to the buffer for (inner, &datum) in d.iter().enumerate() { + #[cfg(not(feature = "kernel"))] + bao1x_api::bollard!(crate::sigcheck::die_no_std, 4); // crate::print!(" {:x}", datum); self.array .as_mut_ptr() @@ -199,6 +203,8 @@ impl<'a> Reram { } // crate::println!(""); + #[cfg(not(feature = "kernel"))] + bao1x_api::bollard!(crate::sigcheck::die_no_std, 4); self.csr.rmwf(utra::rrc::SFR_RRCCR_SFR_RRCCR, RRC_CR_WRITE_CMD | SECURITY_MODE); self.array .as_mut_ptr()