expand audit and add boot0 key indelibility test

audit is now expanded to track one way counters and
summarizes security state

boot0 key indelibility test added as a feature, to be
used in the future for CI
This commit is contained in:
bunnie 2025-10-28 03:37:28 +08:00
parent 6e41294854
commit 28e5484c57
No known key found for this signature in database
2 changed files with 115 additions and 13 deletions

View file

@ -43,5 +43,7 @@ unsafe-debug = []
verbose-debug = ["bao1x-hal/debug-print-uart", "bao1x-hal/verbose-debug"]
print-ifr = []
test-boot0-keys = []
verilator-only = []
default = []
default = ["test-boot0-keys"]

View file

@ -213,14 +213,8 @@ impl Repl {
}
"audit" => {
let owc = OneWayCounter::new();
crate::println!(
"Board type reads as: {:?}",
owc.get_decoded::<bao1x_api::offsets::BoardTypeCoding>()
);
crate::println!(
"Boot partition is: {:?}",
owc.get_decoded::<bao1x_api::offsets::AltBootCoding>()
);
crate::println!("Board type reads as: {:?}", owc.get_decoded::<BoardTypeCoding>());
crate::println!("Boot partition is: {:?}", owc.get_decoded::<AltBootCoding>());
crate::println!("Semver is: {}", crate::version::SEMVER);
crate::println!("Description is: {}", crate::RELEASE_DESCRIPTION);
let slot_mgr = bao1x_hal::acram::SlotManager::new();
@ -300,10 +294,59 @@ impl Repl {
),
Err(e) => crate::println!("Next stage did not validate: {:?}", e),
}
// TODO:
// - devboot seen
// - oem keys erased
// - uuid
// detailed state checks
let mut secure = true;
// check that boot1 pubkeys match the indelible entries
let pubkey_ptr = bao1x_api::BOOT1_START as *const bao1x_api::signatures::SignatureInFlash;
let pk_src: &bao1x_api::signatures::SignatureInFlash =
unsafe { pubkey_ptr.as_ref().unwrap() };
let reference_keys = [
bao1x_api::BAO1_PUBKEY,
bao1x_api::BAO2_PUBKEY,
bao1x_api::BETA_PUBKEY,
bao1x_api::DEV_PUBKEY,
];
let slot_mgr = bao1x_hal::acram::SlotManager::new();
let mut good_compare = 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 = false;
}
}
if !good_compare {
crate::println!("== BOOT1 FAILED PUBKEY CHECK ==");
// this may "not" be a security failure if boot1 was intentionally replaced
// but in that case, the developer customizing the image should have also edited this
// check out.
secure = false;
}
// check developer mode
if owc.get(DEVELOPER_MODE).unwrap() != 0 {
crate::println!("== IN DEVELOPER MODE ==");
secure = false;
}
if owc.get(BOOT0_PUBKEY_FAIL).unwrap() != 0 {
crate::println!("== BOOT0 REPORTED PUBKEY CHECK FAILURE ==");
secure = false;
}
if owc.get(CP_BOOT_SETUP_DONE).unwrap() == 0 {
crate::println!("== CP SETUP FAILED ==");
secure = false;
}
if owc.get(IN_SYSTEM_BOOT_SETUP_DONE).unwrap() == 0 {
crate::println!("In-system keys have NOT been generated");
if owc.get_decoded::<BoardTypeCoding>().unwrap() == BoardTypeCoding::Baosec {
// this is only a security failure on baosec systems
secure = false;
}
} else {
crate::println!("In-system keys have been generated");
}
if !secure {
crate::println!("** System did not meet minimum requirements for security **");
}
}
"lockdown" => {
match bao1x_hal::sigcheck::validate_image(
@ -336,6 +379,61 @@ impl Repl {
}
}
}
#[cfg(feature = "test-boot0-keys")]
"publock" => {
let rram = CSR::new(utra::rrc::HW_RRC_BASE as *mut u32);
crate::println!("RRAM security settings: {:x}", rram.rf(utra::rrc::SFR_RRCCR_SFR_RRCCR));
use bao1x_hal::acram::AccessSettings;
let keys = [
bao1x_api::BAO1_PUBKEY,
bao1x_api::BAO2_PUBKEY,
bao1x_api::BETA_PUBKEY,
bao1x_api::DEV_PUBKEY,
];
let ifr_slot = unsafe { core::slice::from_raw_parts(0x6040_0340 as *const u8, 32) };
crate::println!("IFR permissions at 0x340: {:x?}", ifr_slot);
let slot_mgr = bao1x_hal::acram::SlotManager::new();
let mut rram = bao1x_hal::rram::Reram::new();
// some value that's not 0, so we can differentiate it from access denied state
const ERASE_VALUE: u8 = 7;
let mut pass = true;
// remember: we call these keys, but they live in data slots, because they are public keys.
for key in keys {
// first print the key
let access = key.get_access_spec();
crate::println!("Permissions (spec): {:?}", access);
let acl = slot_mgr.get_acl(&key).unwrap();
crate::println!("Permissions (actual): {:x?}", acl);
// attempt to clear the permissions, making the keys malleable
slot_mgr
.set_acl(&mut rram, &key, &AccessSettings::Key(KeySlotAccess::new_with_raw_value(0)))
.ok(); // if we can't clear, that's by design
let acl = slot_mgr.get_acl(&key).unwrap();
crate::println!("Permissions (attacked): {:x?}", acl);
crate::println!("Data: {:x?}", slot_mgr.read(&key).ok());
let eraser = [ERASE_VALUE; SLOT_ELEMENT_LEN_BYTES];
match slot_mgr.write(&mut rram, &key, &eraser) {
Ok(_) => {}
Err(e) => {
crate::println!("Couldn't erase pubkey in data slot {}: {:?}", key.get_base(), e)
}
}
let check = slot_mgr.read(&key).unwrap();
if check.iter().all(|&b| b == ERASE_VALUE) {
crate::println!("Data at {} was mutable, failure!", key.get_base());
pass = false;
}
}
use bao1x_hal::board::{BOOKEND_END, BOOKEND_START};
crate::println!(
"{}PUB_MUT,{},{}",
BOOKEND_START,
if pass { "PASS" } else { "FAIL" },
BOOKEND_END
);
}
#[cfg(feature = "unsafe-debug")]
"peek" => {
const COLUMNS: usize = 4;
@ -377,6 +475,8 @@ impl Repl {
crate::print!(
"Commands include: reset, echo, boot, bootwait, localecho, uf2, boardtype, audit, lockdown"
);
#[cfg(feature = "test-boot0-keys")]
crate::print!(", publock");
crate::println!("");
}
}