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.
This commit is contained in:
bunnie 2026-02-04 15:55:34 +08:00
parent 0529a10eb8
commit 0aeb9daa37
5 changed files with 68 additions and 40 deletions

View file

@ -29,6 +29,7 @@ efuse = []
policy-menu = []
gen2 = []
key-testing = []
std = ["derive-rkyv", "xous-names", "xous-ipc", "locales"]
derive-rkyv = ["rkyv"]

View file

@ -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,

View file

@ -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 = []

View file

@ -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::<AesOp, _>().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::<KeyWrapper, _>().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);
}
}
}
}

View file

@ -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::<BoardTypeCoding>().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::<u8>::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::<BoardTypeCoding>().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::<u8>::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();