Merge pull request #759 from betrusted-io/cleanup-debug

Cleanup debug
This commit is contained in:
bunnie 2025-12-19 16:51:47 +08:00 committed by GitHub
commit 7ceca0a0cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 81 additions and 57 deletions

View file

@ -16,9 +16,9 @@ use core::cell::RefCell;
use bao1x_api::pubkeys::{BOOT0_SELF_CHECK, BOOT0_TO_ALTBOOT1, BOOT0_TO_BOOT1}; use bao1x_api::pubkeys::{BOOT0_SELF_CHECK, BOOT0_TO_ALTBOOT1, BOOT0_TO_BOOT1};
use bao1x_api::{BOOT0_PUBKEY_FAIL, BOOT1_PUBKEY_FAIL, DEVELOPER_MODE, bollard}; use bao1x_api::{BOOT0_PUBKEY_FAIL, BOOT1_PUBKEY_FAIL, DEVELOPER_MODE, bollard};
use bao1x_api::{HardenedBool, PARANOID_MODE, PARANOID_MODE_DUPE}; use bao1x_api::{PARANOID_MODE, PARANOID_MODE_DUPE};
use bao1x_hal::acram::OneWayCounter; use bao1x_hal::acram::OneWayCounter;
use bao1x_hal::hardening::{Csprng, check_pll, die, mesh_setup}; use bao1x_hal::hardening::{check_pll, die, mesh_setup};
use bao1x_hal::sigcheck::{hardened_erase_policy, jump_to}; use bao1x_hal::sigcheck::{hardened_erase_policy, jump_to};
#[cfg(feature = "unsafe-dev")] #[cfg(feature = "unsafe-dev")]
use critical_section::Mutex; use critical_section::Mutex;
@ -117,7 +117,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
let slot_mgr = bao1x_hal::acram::SlotManager::new(); let slot_mgr = bao1x_hal::acram::SlotManager::new();
// check that the pubkeys in boot0 matches the reference keys // check that the pubkeys in boot0 matches the reference keys
compare_refkeys( bao1x_hal::hardening::compare_refkeys(
&owc, &owc,
&slot_mgr, &slot_mgr,
&mut csprng, &mut csprng,
@ -127,7 +127,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
bollard!(die, 4); bollard!(die, 4);
// check that the pubkeys in boot1 matches the reference keys // check that the pubkeys in boot1 matches the reference keys
compare_refkeys( bao1x_hal::hardening::compare_refkeys(
&owc, &owc,
&slot_mgr, &slot_mgr,
&mut csprng, &mut csprng,
@ -238,51 +238,3 @@ fn print_ifr() {
} }
crate::println!(""); crate::println!("");
} }
/// check that pub keys in the images match those burned into the indelible key area
/// glitch_safety: I'd imagine that glitching in this routine would lead to good_compare being `false`,
/// so no additional hardening is done.
fn compare_refkeys(
owc: &OneWayCounter,
slot_mgr: &bao1x_hal::acram::SlotManager,
csprng: &mut Csprng,
pubkey_ptr: *const bao1x_api::signatures::SignatureInFlash,
fail_counter: usize,
) {
let reference_keys =
[bao1x_api::BAO1_PUBKEY, bao1x_api::BAO2_PUBKEY, bao1x_api::BETA_PUBKEY, bao1x_api::DEV_PUBKEY];
// check that the pub keys match those burned into the indelible key area
// glitch_safety: I'd imagine that glitching in this routine would lead to good_compare being `false`,
// so no additional hardening is done.
let pk_src: &bao1x_api::signatures::SignatureInFlash = unsafe { pubkey_ptr.as_ref().unwrap() };
let mut good_compare = HardenedBool::TRUE;
for (boot0_key, ref_key) in pk_src.sealed_data.pubkeys.iter().zip(reference_keys.iter()) {
let ref_data = slot_mgr.read(&ref_key).unwrap();
if ref_data != &boot0_key.pk {
good_compare = HardenedBool::FALSE;
}
}
csprng.random_delay();
match good_compare.is_true() {
Some(false) => {
bollard!(die, 4);
// don't over-increment this to avoid RRAM wear-out. However, do allow it to increment to higher
// than 1 so we have a higher hamming distance on the counter than a single bit.
if owc.get(fail_counter).unwrap() < 15 {
// safety: the offset is from a pre-validated constant, which meets the safety requirement
unsafe {
owc.inc(fail_counter).unwrap();
}
}
// erase secrets (or ensure they are erased) if the boot pubkey doesn't check out.
bollard!(die, 4);
bao1x_hal::sigcheck::erase_secrets(&mut Some(csprng))
.inspect_err(|e| crate::println!("{}", e))
.ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks
}
Some(true) => (),
None => die(),
}
bollard!(die, 4);
}

View file

@ -80,7 +80,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
let mut board_type = let mut board_type =
one_way.get_decoded::<bao1x_api::BoardTypeCoding>().expect("Board type coding error"); one_way.get_decoded::<bao1x_api::BoardTypeCoding>().expect("Board type coding error");
crate::println_d!("TX_IDLE: {:?}", crate::platform::usb::TX_IDLE.load(Ordering::SeqCst)); // crate::println_d!("TX_IDLE: {:?}", crate::platform::usb::TX_IDLE.load(Ordering::SeqCst));
let perclk: u32; let perclk: u32;
(board_type, perclk) = crate::platform::early_init(board_type); (board_type, perclk) = crate::platform::early_init(board_type);
// ensure we're running off the PLL // ensure we're running off the PLL

View file

@ -14,7 +14,6 @@ use utralib::*;
use crate::platform::usb::page_defrag::PageAssembler; use crate::platform::usb::page_defrag::PageAssembler;
use crate::uf2::Uf2Block; use crate::uf2::Uf2Block;
use crate::usb;
pub(crate) static SECTOR: Mutex<RefCell<Uf2Sector>> = Mutex::new(RefCell::new(Uf2Sector::new(0))); pub(crate) static SECTOR: Mutex<RefCell<Uf2Sector>> = Mutex::new(RefCell::new(Uf2Sector::new(0)));
pub type PageCallback = fn(usize, Box<[u8; SPINOR_ERASE_SIZE as usize]>); pub type PageCallback = fn(usize, Box<[u8; SPINOR_ERASE_SIZE as usize]>);
@ -106,12 +105,16 @@ pub fn write_spim_page(addr: usize, data: Box<[u8; SPINOR_ERASE_SIZE as usize]>)
} }
pub fn setup() -> (UsbDeviceState, u32) { pub fn setup() -> (UsbDeviceState, u32) {
/*
use crate::usb;
crate::println!( crate::println!(
"RAM disk starts at {:x} and advertises {}kiB in length, but is actually {}kiB of storage", "RAM disk starts at {:x} and advertises {}kiB in length, but is actually {}kiB of storage",
usb::RAMDISK_ADDRESS, usb::RAMDISK_ADDRESS,
usb::RAMDISK_LEN / 1024, usb::RAMDISK_LEN / 1024,
usb::RAMDISK_ACTUAL_LEN / 1024, usb::RAMDISK_ACTUAL_LEN / 1024,
); );
*/
// safety: this is safe because we're calling this before any access to `USB` static mut // safety: this is safe because we're calling this before any access to `USB` static mut
// state, and we also understand that the .data section doesn't exist in the loader and // state, and we also understand that the .data section doesn't exist in the loader and

View file

@ -458,7 +458,23 @@ impl Repl {
crate::println!("Boot1 has no valid signature, lockdown would brick the chip.") crate::println!("Boot1 has no valid signature, lockdown would brick the chip.")
} }
}, },
"self_destruct" => {
if !matches!(args.as_slice(), [s] if s == "void_my_warrantee") {
return Err(Error::help(
"Usage: 'self_destruct void_my_warrantee'. This PERMANENTLY wipes the chip and bricks it. No returns or exchanges are allowed after executing this command.",
));
}
let mut rram = bao1x_hal::rram::Reram::new();
unsafe { rram.self_destruct() }
// ... and all was null and void!
}
"baosec-init" => { "baosec-init" => {
if !matches!(args.as_slice(), [s] if s == "confirm") {
return Err(Error::help(
"Usage: 'baosec-init confirm'. WARNING: erases external storage!",
));
}
// this routine is used to initialize baosec products - sets the board type and // this routine is used to initialize baosec products - sets the board type and
// erases the off-chip FLASH // erases the off-chip FLASH
use bao1x_api::{baosec::PDDB_LEN, baosec::PDDB_ORIGIN}; use bao1x_api::{baosec::PDDB_LEN, baosec::PDDB_ORIGIN};
@ -639,7 +655,7 @@ impl Repl {
_ => { _ => {
crate::println!("Command not recognized: {}", cmd); crate::println!("Command not recognized: {}", cmd);
crate::print!( crate::print!(
"Commands include: reset, echo, altboot, boot, bootwait, idmode, localecho, uf2, boardtype, audit, lockdown, paranoid" "Commands include: reset, echo, altboot, boot, bootwait, idmode, localecho, uf2, boardtype, audit, lockdown, paranoid, self_destruct"
); );
#[cfg(feature = "test-boot0-keys")] #[cfg(feature = "test-boot0-keys")]
crate::print!(", publock"); crate::print!(", publock");

View file

@ -144,5 +144,5 @@ pub const DATA_SLOTS: [SlotIndex; 8] = [
/// In addition to these KEY_SLOTS, the DEVELOPER_MODE one way counter is a security-important parameter /// 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. /// that should be included as domain separation in any KDF.
pub const KEY_SLOTS: [SlotIndex; 7] = pub const KEY_SLOTS: [SlotIndex; 8] =
[THE_FLAG_1, CP_COOKIE, RMA_KEY, ROOT_SEED, NUISANCE_KEYS_0, NUISANCE_KEYS_1, CHAFF_KEYS]; [THE_FLAG_1, CP_COOKIE, RMA_KEY, ROOT_SEED, NUISANCE_KEYS_0, NUISANCE_KEYS_1, SWAP_KEY, CHAFF_KEYS];

View file

@ -448,3 +448,49 @@ pub fn glitch_handler(attacks_since_boot: u32) {
} }
irq13.wo(utra::irqarray13::EV_PENDING, reason); irq13.wo(utra::irqarray13::EV_PENDING, reason);
} }
/// check that pub keys in the images match those burned into the indelible key area
/// glitch_safety: I'd imagine that glitching in this routine would lead to good_compare being `false`,
/// so no additional hardening is done.
pub fn compare_refkeys(
owc: &OneWayCounter,
slot_mgr: &crate::acram::SlotManager,
csprng: &mut Csprng,
pubkey_ptr: *const bao1x_api::signatures::SignatureInFlash,
fail_counter: usize,
) {
let reference_keys =
[bao1x_api::BAO1_PUBKEY, bao1x_api::BAO2_PUBKEY, bao1x_api::BETA_PUBKEY, bao1x_api::DEV_PUBKEY];
// check that the pub keys match those burned into the indelible key area
// glitch_safety: I'd imagine that glitching in this routine would lead to good_compare being `false`,
// so no additional hardening is done.
let pk_src: &bao1x_api::signatures::SignatureInFlash = unsafe { pubkey_ptr.as_ref().unwrap() };
let mut good_compare = HardenedBool::TRUE;
for (boot0_key, ref_key) in pk_src.sealed_data.pubkeys.iter().zip(reference_keys.iter()) {
let ref_data = slot_mgr.read(&ref_key).unwrap();
if ref_data != &boot0_key.pk {
good_compare = HardenedBool::FALSE;
}
}
csprng.random_delay();
match good_compare.is_true() {
Some(false) => {
bollard!(die, 4);
// don't over-increment this to avoid RRAM wear-out. However, do allow it to increment to higher
// than 1 so we have a higher hamming distance on the counter than a single bit.
if owc.get(fail_counter).unwrap() < 15 {
// safety: the offset is from a pre-validated constant, which meets the safety requirement
unsafe {
owc.inc(fail_counter).unwrap();
}
}
// erase secrets (or ensure they are erased) if the boot pubkey doesn't check out.
bollard!(die, 4);
crate::sigcheck::erase_secrets(&mut Some(csprng)).inspect_err(|e| crate::println!("{}", e)).ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks
}
Some(true) => (),
None => die(),
}
bollard!(die, 4);
}

View file

@ -164,6 +164,13 @@ impl<'a> Reram {
rram rram
} }
/// This is unsafe because it erases the entire system. Only call this if you want to self destruct.
pub unsafe fn self_destruct(&mut self) -> ! {
self.csr.wo(utra::rrc::SFR_RRCAR, 0x2468);
// busy-wait the CPU so that the suicide flow can complete. There is no return.
loop {}
}
#[cfg(feature = "std")] #[cfg(feature = "std")]
/// `base` specifies the actual offset from the beginning of the RRAM array /// `base` specifies the actual offset from the beginning of the RRAM array
/// to where this range maps to. This is necessary because `range` is virtually /// to where this range maps to. This is necessary because `range` is virtually