diff --git a/libs/bao1x-api/src/offsets/baosec.rs b/libs/bao1x-api/src/offsets/baosec.rs index a8e04ea7e..892d35dc9 100644 --- a/libs/bao1x-api/src/offsets/baosec.rs +++ b/libs/bao1x-api/src/offsets/baosec.rs @@ -110,5 +110,8 @@ pub const CHAFF_KEYS: SlotIndex = SlotIndex::KeyRange(128..256, PartitionAccess: /// condensed here and used to check for access integrity using the array below. pub const DATA_SLOTS: [SlotIndex; 4] = [crate::offsets::SERIAL_NUMBER, crate::offsets::UUID, crate::offsets::IFR_HASH, crate::offsets::CP_ID]; + +/// In addition to these KEY_SLOTS, the DEVELOPER_MODE one way counter is a security-important parameter +/// that should be included as domain separation in any KDF. pub const KEY_SLOTS: [SlotIndex; 6] = [ROOT_SEED, RMA_KEY, CP_COOKIE, NUISANCE_KEYS_0, NUISANCE_KEYS_1, CHAFF_KEYS]; diff --git a/libs/bao1x-api/src/offsets/common.rs b/libs/bao1x-api/src/offsets/common.rs index 7261e768a..3b4f15dde 100644 --- a/libs/bao1x-api/src/offsets/common.rs +++ b/libs/bao1x-api/src/offsets/common.rs @@ -98,6 +98,21 @@ pub const CP_BOOT_SETUP_DONE: usize = 83; /// derived from a blend of entropy sources. It's only useful on platforms like baosec. pub const IN_SYSTEM_BOOT_SETUP_DONE: usize = 84; +/// When non-zero, the system had, at least one point in time, been challenged to boot +/// from a developer image. Thus, the state of the system cannot be attested to based on +/// the original signing keys burned from the factory. The value of this is also +/// included as AAD in key derivations. +pub const DEVELOPER_MODE: usize = 85; + +/// This is flipped when a trust transfer happens to a third party. i.e. any OEMs that +/// come to Baochip to sign an image (that may then have their public keys in it) are +/// required to set this bit as part of their signed code. It's a half-baked work-around +/// for folks that are paranoid about DEVELOPER_MODE and for whatever reason think it's +/// more trustworthy if someone they've never met used some cryptography to bless a bag +/// of bits, but at the least they can say that a person they don't know or trust most likely +/// did bless the bag of bits. +pub const OEM_MODE: usize = 86; + /// Total number of public key slots in the system. Pubkey revocations are at the "top of range" pub const PUBKEY_SLOTS: usize = 4; /// Offset in the one-way counter array for loader key revocations. Provisions for up to four diff --git a/libs/bao1x-hal/src/coreuser.rs b/libs/bao1x-hal/src/coreuser.rs index ebaa005c5..2547dc01a 100644 --- a/libs/bao1x-hal/src/coreuser.rs +++ b/libs/bao1x-hal/src/coreuser.rs @@ -149,17 +149,6 @@ impl Coreuser { self.csr.rmwf(utra::coreuser::CONTROL_ENABLE, 1); } - /// This allows boot1 to read/write keys. This setting is necessary to be able to *erase* keys - /// before going into developer mode. Eliminating this function doesn't mean that cheating - /// isn't possible - an adversary could always write this code. Cheating is always possible - /// until protect() is called! - pub unsafe fn bootloader_cheat(&mut self) { - // this is a manual patch of the table inside `set()`: entry 7 is for the boot value, - // and we're patching this up to the trusted user setting. - self.csr.rmwf(utra::coreuser::MAP_HI_LUT7, 0); - self.csr.rmwf(utra::coreuser::USERVALUE_USER7, TRUSTED_USER.as_dense()); - } - /// Sets a "one way door" that disallows any further updating to these fields. pub fn protect(&mut self) { // invert sense for Xous mode - User process 3 is trusted, the kernel is not! diff --git a/libs/bao1x-hal/src/rram.rs b/libs/bao1x-hal/src/rram.rs index 5d77483c0..9a3118a4c 100644 --- a/libs/bao1x-hal/src/rram.rs +++ b/libs/bao1x-hal/src/rram.rs @@ -98,14 +98,17 @@ impl<'a> Reram { /// that artifact alone. pub fn add_range(&'a mut self, base: usize, range: xous::MemoryRange) -> &'a Self { let top = base + range.len(); - self.range_map.insert(base..top, base); + self.range_map.insert(base..top, range); + self } + #[cfg(not(feature = "std"))] pub fn read_slice(&self) -> &[u32] { self.array } /// This is a crappy "unsafe" initial version that requires the write /// destination address to be aligned to a 256-bit boundary, and the data /// to be exactly 256 bits long. + #[cfg(not(feature = "std"))] pub unsafe fn write_u32_aligned(&mut self, addr: usize, data: &[u32]) { assert!(addr % 0x20 == 0, "unaligned destination address!"); assert!(data.len() % 8 == 0, "unaligned source data!"); @@ -144,6 +147,7 @@ impl<'a> Reram { /// ASSUME: offset has been bounds checked by a wrapper function. /// SAFETY: this is only safe in non-concurrent contexts. This means that in Xous this must be /// called from an interrupt context. + #[cfg(not(feature = "std"))] unsafe fn write_slice_inner(&mut self, offset: usize, data: &[u8]) -> Result { let mut buffer = AlignedBuffer([0u8; ALIGNMENT]); diff --git a/libs/bao1x-hal/src/sigcheck.rs b/libs/bao1x-hal/src/sigcheck.rs index 5dd3e8cdf..786e9b97a 100644 --- a/libs/bao1x-hal/src/sigcheck.rs +++ b/libs/bao1x-hal/src/sigcheck.rs @@ -3,11 +3,15 @@ use alloc::string::String; use alloc::vec::Vec; use bao1x_api::signatures::*; +#[cfg(not(feature = "std"))] +use bao1x_api::{DEVELOPER_MODE, KeySlotAccess, RwPerms, SLOT_ELEMENT_LEN_BYTES, SlotType}; use digest::Digest; use sha2_bao1x::{Sha256, Sha512}; use xous::arch::PAGE_SIZE; use crate::acram::OneWayCounter; +#[cfg(not(feature = "std"))] +use crate::acram::{AccessSettings, SlotManager}; use crate::udma::Spim; /// Current draw @ 200MHz CPU ACLK (400MHz FCLK), VDD85 = 0.80V nom (measured @0.797V): ~71mA peak @ 27C, @@ -180,11 +184,11 @@ pub fn validate_image( } } - if !secure || developer { - erase_secrets(); - } if let Some(valid_key) = passing_key { if auto_jump { + if !secure || developer { + erase_secrets(); + } jump_to(img_offset as usize); } Ok((valid_key, pk_src.sealed_data.pubkeys[valid_key].tag)) @@ -193,10 +197,79 @@ pub fn validate_image( } } -fn erase_secrets() { - // actually, this should check first if the data is 0, then 0 it. Can't afford - // to wear-out the memory every boot by repeatedly overwriting it... - crate::println!("TODO: erase secret data stores on boot to devmode!"); +#[cfg(feature = "std")] +pub fn erase_secrets() { + unimplemented!( + "erase_secrets() is not available in the run-time environment; access permissions are insufficient." + ); +} + +#[cfg(not(feature = "std"))] +pub fn erase_secrets() { + // An erase value of 0 is conflateable with access permissions being incorrect. Choose a non-0 value + // for the erase value, but also, don't pick a 0-1-0-1 dense pattern because that can assist with + // calibrating microscopy techniques. + const ERASE_VALUE: u8 = 0x03; + + // ensure coreuser settings, as we could enter from a variety of loader stages + let mut cu = crate::coreuser::Coreuser::new(); + cu.set(); + + let slot_mgr = SlotManager::new(); + let mut rram = crate::rram::Reram::new(); + + let mut zero_key_count = 0; + // statistically speaking, I suppose, maybe we could have "a" set of keys that are 0 out of a randomly + // generated set. But if we see more than the threshold below of zero keys, conclude that we don't + // have access permissions, and panic instead of allowing a boot. + const ZERO_ERR_THRESH: usize = 2; + for slot in crate::board::KEY_SLOTS.iter() { + if slot.get_type() == SlotType::Key { + let (_pa, rw_perms) = slot.get_access_spec(); + for data_index in slot.try_into_data_iter().unwrap() { + match rw_perms { + RwPerms::ReadWrite | RwPerms::WriteOnly => { + // clear the ACL so we can operate on the data + slot_mgr + .set_acl( + &mut rram, + slot, + &AccessSettings::Key(KeySlotAccess::new_with_raw_value(0)), + ) + .expect("couldn't reset ACL"); + let bytes = unsafe { slot_mgr.read_key_slot(data_index) }; + if bytes.iter().all(|&b| b == 0) { + zero_key_count += 1; + } + 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).expect("couldn't erase key"); + } + let check = unsafe { slot_mgr.read_key_slot(data_index) }; + if !check.iter().all(|&b| b == ERASE_VALUE) { + panic!("Key erasure did not succeed, refusing to boot!"); + } else { + crate::println!("Key range at {} confirmed erased", slot.get_base()); + } + } + _ => {} + } + if zero_key_count > ZERO_ERR_THRESH { + panic!( + "Saw too many zero-keys. Insufficient privilege to erase keys, panicing instead of allowing a boot!" + ); + } + } + } + } + let owc = OneWayCounter::new(); + // once all secrets are erased, advance the DEVELOPER_MODE state + // safety: the offset is correct because we're pulling it from our pre-defined constants and + // those are manually checked. + unsafe { owc.inc(DEVELOPER_MODE).unwrap() }; } pub fn jump_to(target: usize) -> ! {