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.
This commit is contained in:
bunnie 2025-10-04 18:55:36 +08:00
parent 7ff1e438ff
commit 0f98c3bea1
3 changed files with 4 additions and 3 deletions

View file

@ -94,7 +94,6 @@ bao1x = [
"debug-print",
"bao1x-hal",
"rand_chacha",
# "boot-delay",
]
board-baosec = [
"bao1x-hal/loader-baosec",

View file

@ -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") {

View file

@ -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);