From 0f98c3bea1091d4b0323193e060fcfe52e9b1885 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 4 Oct 2025 18:55:36 +0800 Subject: [PATCH] implement signature checking on the off-chip swap image Swap can be "secured" in two ways: 1) it can be re-encrypted to a secure, locally generated key 2) it can be signature-checked with a public key. (2) is provided as a path to receive updates. Updates are distributed as "encrypted" (with the "0" key), but signed with a public key. Once the update is validated, it's meant to be re-encrypted with a per-device unique key. A big reason for the re-encryption is because the swap is already encrypted, we can rely on the AEAD for validation, and skip the signature check (saves time on boot). Recall that the reason we don't just do a sig-check and run is that is a TOCTOU: it's pretty trivial to execute a mux that swaps out the FLASH memory after the signature check with another version that has malicious stuff in it. So we go with a sector-by-sector AEAD that's ideally encrypted to a locally generated key. It also offers the possibility to prevent malleability of the swap data between devices, as many public-key signed images could be floating around, including old versions that include insecurities (e.g. prevents downgrade attack by replacing an up to date image with an older signed image with an insecurity). However, this latter possibility requires the user interface to be correctly setup to prevent malleability by asking users to opt-in to partition re-encryption. Need to consider this user experience flow - kicking the can down the line, but that time is coming soon. --- loader/Cargo.toml | 1 - loader/src/main.rs | 3 ++- loader/src/phase1.rs | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 89019652a..a38fc291e 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -94,7 +94,6 @@ bao1x = [ "debug-print", "bao1x-hal", "rand_chacha", - # "boot-delay", ] board-baosec = [ "bao1x-hal/loader-baosec", diff --git a/loader/src/main.rs b/loader/src/main.rs index ba81fc677..7ba80258c 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -264,6 +264,7 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) FunctionCode::Developer as u32, ], false, + None, ) { Ok(k) => println!("*** Kernel signature check by key @ {} OK ***", k), Err(e) => { @@ -610,7 +611,7 @@ pub fn read_initial_config(cfg: &mut BootConfig) { #[cfg(feature = "swap")] pub fn read_swap_config(cfg: &mut BootConfig) { // Read in the swap arguments: should be located at beginning of the encrypted image in swap. - let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_src_page_at(0x0); + let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_src_page_at(0x0).unwrap(); let swap_args = KernelArguments::new(page0.as_ptr() as *const usize); for tag in swap_args.iter() { if tag.name == u32::from_le_bytes(*b"IniS") { diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 3e8f241cc..d6931dbc4 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -679,7 +679,8 @@ fn copy_processes(cfg: &mut BootConfig) { cfg.swap_hal .as_mut() .expect("swap HAL uninit") - .decrypt_src_page_at(src_swap_img_page); + .decrypt_src_page_at(src_swap_img_page) + .unwrap(); } let decrypt_avail = remaining_in_page(src_swap_img_addr); let dst_page_avail = remaining_in_page(dst_page_vaddr);