From 0aeb9daa3749d473c58394944c41197c59761f50 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 4 Feb 2026 15:55:34 +0800 Subject: [PATCH] add a method to force-rewrite keys This is useful in CI to test various stages of dev-key signed image vs secret key erase without having to go through a complete suicide cycle on the chip. This command is gated behind a feature flag that would only be invoked for beta testing. I think it's still safe to have even as a beta-signed feature because the worst you can do with this is replace existing keys - so it could be used to DoS/brick, but not leak secrets. --- libs/keystore-api/Cargo.toml | 1 + libs/keystore-api/src/gen2_api.rs | 3 + services/keystore/Cargo.toml | 13 ++- .../keystore/src/platform/baosec/server.rs | 8 ++ .../keystore/src/platform/baosec/store.rs | 83 +++++++++++-------- 5 files changed, 68 insertions(+), 40 deletions(-) diff --git a/libs/keystore-api/Cargo.toml b/libs/keystore-api/Cargo.toml index 914035a6b..51b2f0bad 100644 --- a/libs/keystore-api/Cargo.toml +++ b/libs/keystore-api/Cargo.toml @@ -29,6 +29,7 @@ efuse = [] policy-menu = [] gen2 = [] +key-testing = [] std = ["derive-rkyv", "xous-names", "xous-ipc", "locales"] derive-rkyv = ["rkyv"] diff --git a/libs/keystore-api/src/gen2_api.rs b/libs/keystore-api/src/gen2_api.rs index 290c60e63..14ef34422 100644 --- a/libs/keystore-api/src/gen2_api.rs +++ b/libs/keystore-api/src/gen2_api.rs @@ -21,6 +21,9 @@ pub enum Opcode { // ----- below are non-cryptographic opcodes but used to manipulate sensitive state ----- /// Set bootwait parameters Bootwait = 4096, + /// For testing only: overwrite secret keys + #[cfg(feature = "key-testing")] + OverwriteKeys = 4097, /// Used to map unknown opcodes InvalidCall = 65535, diff --git a/services/keystore/Cargo.toml b/services/keystore/Cargo.toml index 86a63ab2b..e71c3995b 100644 --- a/services/keystore/Cargo.toml +++ b/services/keystore/Cargo.toml @@ -108,8 +108,13 @@ board-dabao = [ ] # Dev board form factor loader-dabao = [] -hazardous-debug = [ -] # this feature enables the printing of secret materials for debug purposes -tts = [] -efuse = [] +# this feature enables the printing of secret materials for debug purposes +hazardous-debug = [] + +# this feature indicates the system is being booted to do keying testing. +# it enables some features that allows the keystore to be overwritten +# so that we can rapidly iterate through various combinations of boot +# signatures +key-testing = ["keystore-api/key-testing"] + default = [] diff --git a/services/keystore/src/platform/baosec/server.rs b/services/keystore/src/platform/baosec/server.rs index aacc65af8..1559ea8dd 100644 --- a/services/keystore/src/platform/baosec/server.rs +++ b/services/keystore/src/platform/baosec/server.rs @@ -38,6 +38,8 @@ pub fn keystore(sid: SID) -> ! { unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; // as_flat saves a copy step, but we have to deserialize some enums manually let mut aes_op = buffer.to_original::().unwrap(); + // TODO: add hardening checks to the deserialization of AesOp. Can't assume the + // values are correct coming into this server. store.aes_op(&mut aes_op).expect("couldn't perform AES op"); buffer.replace(aes_op).unwrap(); } @@ -45,6 +47,8 @@ pub fn keystore(sid: SID) -> ! { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut kwp = buffer.to_original::().unwrap(); + // TODO: add hardening checks to the deserialization of AesOp. Can't assume the + // values are correct coming into this server. store.aes_kwp(&mut kwp).expect("couldn't wrap key"); buffer.replace(kwp).unwrap(); } @@ -71,6 +75,10 @@ pub fn keystore(sid: SID) -> ! { Opcode::InvalidCall => { log::error!("Invalid call in keystore: {:?}", opcode); } + #[cfg(feature = "key-testing")] + Opcode::OverwriteKeys => { + store.force_system_init(&mut rram); + } } } } diff --git a/services/keystore/src/platform/baosec/store.rs b/services/keystore/src/platform/baosec/store.rs index 08c47377e..dea2e222d 100644 --- a/services/keystore/src/platform/baosec/store.rs +++ b/services/keystore/src/platform/baosec/store.rs @@ -35,7 +35,47 @@ impl KeyStore { Self { slot_mgr, owc, master_key: None } } + fn system_init_inner(&mut self, rram: &mut Reram) { + // one routine works for both dabao and baosec + let board_type = self.owc.get_decoded::().unwrap(); + + let xns = xous_names::XousNames::new().unwrap(); + let mut trng = bao1x_hal_service::trng::Trng::new(&xns).unwrap(); + // generate all the keys + let key_set = if board_type == bao1x_api::BoardTypeCoding::Baosec { + &bao1x_api::baosec::KEY_SLOTS[..] + } else { + &bao1x_api::dabao::KEY_SLOTS[..] + }; + let mut success = true; + for key_range in key_set.iter() { + if *key_range == THE_FLAG_1 { + // don't overwrite the flag, it's pre-loaded from static data + continue; + } + let mut storage = Vec::::with_capacity(key_range.len() * SLOT_ELEMENT_LEN_BYTES); + storage.resize(key_range.len() * SLOT_ELEMENT_LEN_BYTES, 0); + trng.fill_bytes(&mut storage); + match self.slot_mgr.write(rram, key_range, &storage) { + Ok(_) => {} + Err(e) => { + success = false; + log::error!("Couldn't initialize slot {:?}: {:?}", key_range, e); + } + } + } + // once all values are written, advance the IN_SYSTEM_BOOT_SETUP_DONE state + // safety: the offset is correct because we're pulling it from our pre-defined constants and + // those are manually checked. + if success { + unsafe { self.owc.inc(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() }; + } + log::info!("Secret ID init done."); + log::info!("{}KEYSTORE.INITDONE,{}", BOOKEND_START, BOOKEND_END); + } + pub fn ensure_system_init(&mut self, rram: &mut Reram) { + log::info!("System setup not yet done. Initializing secret identifiers..."); // debug coreuser status /* let coreuser_range = xous::map_memory( @@ -49,46 +89,17 @@ impl KeyStore { log::info!("coreuser status: {:x}", coreuser.r(utralib::utra::coreuser::STATUS)); */ - // one routine works for both dabao and baosec - let board_type = self.owc.get_decoded::().unwrap(); if self.owc.get(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() == 0 { - log::info!("System setup not yet done. Initializing secret identifiers..."); - let xns = xous_names::XousNames::new().unwrap(); - let mut trng = bao1x_hal_service::trng::Trng::new(&xns).unwrap(); - // generate all the keys - let key_set = if board_type == bao1x_api::BoardTypeCoding::Baosec { - &bao1x_api::baosec::KEY_SLOTS[..] - } else { - &bao1x_api::dabao::KEY_SLOTS[..] - }; - let mut success = true; - for key_range in key_set.iter() { - if *key_range == THE_FLAG_1 { - // don't overwrite the flag, it's pre-loaded from static data - continue; - } - let mut storage = Vec::::with_capacity(key_range.len() * SLOT_ELEMENT_LEN_BYTES); - storage.resize(key_range.len() * SLOT_ELEMENT_LEN_BYTES, 0); - trng.fill_bytes(&mut storage); - match self.slot_mgr.write(rram, key_range, &storage) { - Ok(_) => {} - Err(e) => { - success = false; - log::error!("Couldn't initialize slot {:?}: {:?}", key_range, e); - } - } - } - // once all values are written, advance the IN_SYSTEM_BOOT_SETUP_DONE state - // safety: the offset is correct because we're pulling it from our pre-defined constants and - // those are manually checked. - if success { - unsafe { self.owc.inc(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() }; - } - log::info!("Secret ID init done."); - log::info!("{}KEYSTORE.INITDONE,{}", BOOKEND_START, BOOKEND_END); + self.system_init_inner(rram); } } + #[cfg(feature = "key-testing")] + pub fn force_system_init(&mut self, rram: &mut Reram) { + self.system_init_inner(rram); + log::info!("{}KEYSTORE.FORCEINIT,{}", BOOKEND_START, BOOKEND_END); + } + /// returns `true` if collateral is erased pub fn is_collateral_erased(&mut self) -> bool { let collateral = self.slot_mgr.read(&COLLATERAL).unwrap();