From 9093bb4ed7123bcee9a6f0e14083396e13917ea3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 18 Dec 2025 02:16:50 +0800 Subject: [PATCH] simplify keystore setup The TL;DR is this now treats dabao exactly the same as baosec in terms of keystore setup from the security flow perspective. baosec & dabao were different originally because baosec would rely upon the Avalanche generator TRNG as supplemental randomness and dabao would not. This special case had to be handled in the bootloader and propagated through. However, in a previous commit, the keystore setup was moved to the OS *anyways* - thus making this difference moot. This patch backs out the one-way counters that would track the dabao initialization state. It also doesn't fail the boot on the erase key proof check failing if the keystores aren't initialized yet. This creates yet another condition that could be glitched over to bypass the erase check but it is checked twice indepedently. --- bao1x-boot/boot1/src/repl.rs | 16 ++-------------- libs/bao1x-api/src/offsets/common.rs | 10 ---------- loader/src/main.rs | 16 ++++++++++------ loader/src/platform/bao1x/bao1x.rs | 8 +------- loader/src/platform/bao1x/swap.rs | 17 ++++++++++------- 5 files changed, 23 insertions(+), 44 deletions(-) diff --git a/bao1x-boot/boot1/src/repl.rs b/bao1x-boot/boot1/src/repl.rs index 07fa94278..24c23797d 100644 --- a/bao1x-boot/boot1/src/repl.rs +++ b/bao1x-boot/boot1/src/repl.rs @@ -428,23 +428,11 @@ impl Repl { crate::println!("== CP SETUP FAILED =="); secure = false; } - if (boardtype == BoardTypeCoding::Baosec && owc.get(IN_SYSTEM_BOOT_SETUP_DONE).unwrap() == 0) - || (boardtype == BoardTypeCoding::Dabao && owc.get(DABAO_KEY_SETUP_DONE).unwrap() == 0) - { + if owc.get(IN_SYSTEM_BOOT_SETUP_DONE).unwrap() == 0 { crate::println!("In-system keys have NOT been generated"); secure = false; } else { - if boardtype == BoardTypeCoding::Baosec { - crate::println!("In-system keys have been generated"); - } else { - // assume dabao-type board - if owc.get(INVOKE_DABAO_KEY_SETUP).unwrap() != 0 { - crate::println!("In-system keys have been generated"); - } else { - crate::println!("In-system key generation is still pending"); - secure = false - } - } + crate::println!("In-system keys have been generated"); } if !secure { crate::println!("** System did not meet minimum requirements for security **"); diff --git a/libs/bao1x-api/src/offsets/common.rs b/libs/bao1x-api/src/offsets/common.rs index bfcfeaefb..e1d1a2831 100644 --- a/libs/bao1x-api/src/offsets/common.rs +++ b/libs/bao1x-api/src/offsets/common.rs @@ -152,16 +152,6 @@ pub const OEM_MODE: usize = 86; /// the data store. pub const BOOT0_PUBKEY_FAIL: usize = 87; -/// This pair of counters is used to invoke the key setup for dabao. It's not done until -/// a Xous environment is loaded onto a dabao *and* the dabao is rebooted the first time. -/// The reason it's not done all the time is that baosec boards want to set up their secure -/// keys using the TRNG avalanche generator - which is not available on dabao. Thus we have -/// to differentiate the two cases, because when a chip is "born" it thinks it's a "dabao", -/// and has to be told it's a "baosec". This is the analogous path, but for dabaos that -/// want the key store. -pub const INVOKE_DABAO_KEY_SETUP: usize = 88; -pub const DABAO_KEY_SETUP_DONE: usize = 89; - encode_oneway! { #[offset = 90] /// When set, the system will prefer to present generic, fixed identifiers when challenged diff --git a/loader/src/main.rs b/loader/src/main.rs index 1363a7980..718988f3e 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -274,6 +274,7 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) let backup = bao1x_hal::buram::BackupManager::new(); let (paranoid1, paranoid2) = owc.hardened_get2(PARANOID_MODE, PARANOID_MODE_DUPE).unwrap(); let (dev_mode1, dev_mode2) = owc.hardened_get(bao1x_api::DEVELOPER_MODE).unwrap(); + let (init1, init2) = owc.hardened_get(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap(); csprng.random_delay(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); // validate using the bao1x signature scheme @@ -298,8 +299,10 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) // developer image. csprng.random_delay(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); - if tag == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] - || key == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + // this has to gate on keys being initialized, because without key setup nothing gets erased + if (tag == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] + || key == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT) + && (init1 != 0) { csprng.random_delay(); let erase_proof: &[u8; 32] = @@ -319,7 +322,7 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) // this consumes dev_mode2 & !k2 - the alternate version - preventing the check from being // optimized out bollard!(bao1x_hal::sigcheck::die_no_std, 4); - if !key_inv == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT { + if (!key_inv == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT) && (init2 != 0) { csprng.random_delay(); let erase_proof: &[u8; 32] = backup.get_slice(ERASURE_PROOF_RANGE_BYTES).try_into().unwrap(); @@ -375,9 +378,10 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) } csprng.random_delay(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); - if tag + if (tag == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] - || key == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + || key == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT) + && (init1 != 0) { let erase_proof: &[u8; 32] = backup.get_slice(ERASURE_PROOF_RANGE_BYTES).try_into().unwrap(); @@ -398,7 +402,7 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) } csprng.random_delay(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); - if !key_inv == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT { + if (!key_inv == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT) && (init2 != 0) { let erase_proof: &[u8; 32] = backup.get_slice(ERASURE_PROOF_RANGE_BYTES).try_into().unwrap(); if dev_mode2 == 0 || erase_proof != &[ERASE_VALUE; 32] { diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index 321a36343..4b60b2b6c 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -188,12 +188,6 @@ pub fn early_init_hw() -> u32 { if one_way.get(bao1x_api::BootWaitCoding::OFFSET).unwrap() == 0 { one_way.inc_coded::().ok(); } - // set invoke_dabao_key_setup exactly once. On the next reboot, the keys - // for the dabao environment will be created using the on-chip TRNG. - if one_way.get(bao1x_api::INVOKE_DABAO_KEY_SETUP).unwrap() == 0 { - // safety: the argument is from a checked constant - unsafe { one_way.inc(bao1x_api::INVOKE_DABAO_KEY_SETUP).ok() }; - } } // Setup some global control registers that will allow the TRNG to operate once the kernel is @@ -234,7 +228,7 @@ pub fn setup_console( udma_global.map_event(uart_id, PeriphEventType::Uart(EventUartOffset::Tx), EventChannel::Channel1); let baudrate: u32 = bao1x_api::UART_BAUD; - let freq: u32 = perclk / 2; + let freq: u32 = perclk; // the address of the UART buffer is "hard-allocated" at an offset one page from the top of // IFRAM0. This is a convention that must be respected by the UDMA UART library implementation diff --git a/loader/src/platform/bao1x/swap.rs b/loader/src/platform/bao1x/swap.rs index 6d03118fa..f170478eb 100644 --- a/loader/src/platform/bao1x/swap.rs +++ b/loader/src/platform/bao1x/swap.rs @@ -285,12 +285,15 @@ impl SwapHal { } let backup = bao1x_hal::buram::BackupManager::new(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); - let erase_proof: &[u8; 32] = backup - .get_slice(bao1x_hal::buram::ERASURE_PROOF_RANGE_BYTES) - .try_into() - .unwrap(); - if erase_proof != &[ERASE_VALUE; 32] { - bao1x_hal::sigcheck::die_no_std(); + let owc = bao1x_hal::acram::OneWayCounter::new(); + if owc.get(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() != 0 { + let erase_proof: &[u8; 32] = backup + .get_slice(bao1x_hal::buram::ERASURE_PROOF_RANGE_BYTES) + .try_into() + .unwrap(); + if erase_proof != &[ERASE_VALUE; 32] { + bao1x_hal::sigcheck::die_no_std(); + } } bollard!(bao1x_hal::sigcheck::die_no_std, 4); } @@ -508,7 +511,7 @@ pub fn userspace_maps(cfg: &mut BootConfig) { let baudrate: u32 = 1_000_000; let perclk = 100_000_000; // TODO: turn this into a symbolic const, or better yet, pass in from the loader - let freq: u32 = perclk / 2; + let freq: u32 = perclk; // the address of the UART buffer is "hard-allocated" at an offset one page from the top of // IFRAM0. This is a convention that must be respected by the UDMA UART library implementation