From 006eab20841db138ea05f5f60576ceeb7921b425 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 5 Dec 2025 23:12:15 +0800 Subject: [PATCH] add multi-pass validation in paranoid mode to boot0 this patch uses the previously modified APIs and implements a multi-pass check on signatures in the case that paranoid mode is turned on. This may effectively double the boot time but it also reduces the chance of a successful glitch attack on signature validation by a multiple of the glitch success probability. this also updates other API calls and implements the XOR-on-target-address code. --- bao1x-boot/boot0/Cargo.toml | 3 + bao1x-boot/boot0/src/main.rs | 149 ++++++++++++++++------------------- bao1x-boot/boot0/src/repl.rs | 25 ++---- 3 files changed, 76 insertions(+), 101 deletions(-) diff --git a/bao1x-boot/boot0/Cargo.toml b/bao1x-boot/boot0/Cargo.toml index 9b63d9860..e786c3ecf 100644 --- a/bao1x-boot/boot0/Cargo.toml +++ b/bao1x-boot/boot0/Cargo.toml @@ -14,6 +14,9 @@ vexriscv = { version = "0.5.6", package = "xous-riscv" } riscv = { version = "0.14.0", features = ["critical-section-single-hart"] } linked_list_allocator = { version = "0.10.5", optional = true } critical-section = "1.2.0" +hex-literal = "0.4" +sha2-bao1x = { path = "../sha2-bao1x", default-features = false } +digest = "0.10.7" [features] # Baochip feature tree below diff --git a/bao1x-boot/boot0/src/main.rs b/bao1x-boot/boot0/src/main.rs index 7f3f4d4f8..50f08d93e 100644 --- a/bao1x-boot/boot0/src/main.rs +++ b/bao1x-boot/boot0/src/main.rs @@ -14,10 +14,12 @@ use alloc::collections::VecDeque; #[cfg(feature = "unsafe-dev")] use core::cell::RefCell; -use bao1x_api::HardenedBool; -use bao1x_api::{BOOT0_PUBKEY_FAIL, bollard, signatures::FunctionCode}; +use bao1x_api::pubkeys::{BOOT0_SELF_CHECK, BOOT0_TO_ALTBOOT1, BOOT0_TO_BOOT1}; +use bao1x_api::{BOOT0_PUBKEY_FAIL, bollard}; +use bao1x_api::{HardenedBool, PARANOID_MODE, PARANOID_MODE_DUPE}; use bao1x_hal::acram::OneWayCounter; use bao1x_hal::hardening::{check_pll, die, mesh_setup}; +use bao1x_hal::sigcheck::{hardened_erase_policy, jump_to}; #[cfg(feature = "unsafe-dev")] use critical_section::Mutex; use platform::*; @@ -138,95 +140,80 @@ pub unsafe extern "C" fn rust_entry() -> ! { } // erase secrets if the boot0 pubkey doesn't check out. bollard!(die, 4); - bao1x_hal::sigcheck::erase_secrets(&mut Some(&mut csprng)); + bao1x_hal::sigcheck::erase_secrets(&mut Some(&mut csprng)) + .inspect_err(|e| crate::println!("{}", e)) + .ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks } Some(true) => (), None => die(), } bollard!(die, 4); - // self-validate the image with the keys we put in, just to make sure our code wasn't tampered with - if bao1x_hal::sigcheck::validate_image( - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &[FunctionCode::Boot0 as u32], // only boot0 is allowed for boot0 - false, - None, - Some(&mut csprng), - ) - .is_ok() - { - seal_boot0_keys(); - bollard!(die, 4); + let (paranoid1, paranoid2) = owc.hardened_get2(PARANOID_MODE, PARANOID_MODE_DUPE).unwrap(); - let allowed_functions = - [FunctionCode::Boot1 as u32, FunctionCode::UpdatedBoot1 as u32, FunctionCode::Developer as u32]; - let one_way_access = bao1x_hal::acram::OneWayCounter::new(); - match one_way_access.get_decoded::() { - Ok(bao1x_api::AltBootCoding::PrimaryPartition) => { - // Primary boot selected. Check Boot1 first, then fall back to LOADER/BAREMETAL. - bao1x_hal::sigcheck::validate_image( - bao1x_api::BOOT1_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &allowed_functions, - true, - None, - Some(&mut csprng), - ) - .ok(); - bao1x_hal::sigcheck::validate_image( - bao1x_api::LOADER_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &allowed_functions, - true, - None, - Some(&mut csprng), - ) - .ok(); - } - Ok(bao1x_api::AltBootCoding::AlternatePartition) => { - // Alternate boot selected. Check LOADER/BAREMETAL, then fall back to Boot1. - bao1x_hal::sigcheck::validate_image( - bao1x_api::LOADER_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &allowed_functions, - true, - None, - Some(&mut csprng), - ) - .ok(); - bao1x_hal::sigcheck::validate_image( - bao1x_api::BOOT1_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &allowed_functions, - true, - None, - Some(&mut csprng), - ) - .ok(); - } - Err(_) => { - crate::println!("Internal error: alt boot encoding is invalid!"); - bao1x_hal::sigcheck::die_no_std(); - } + // == self-validate the image with the keys we put in, just to make sure our code wasn't tampered with == + let boot0_check1 = bao1x_hal::sigcheck::validate_image(BOOT0_SELF_CHECK, None, Some(&mut csprng)) + .unwrap_or_else(|_| die()); + csprng.random_delay(); + if paranoid1 != paranoid2 || boot0_check1.0 != !boot0_check1.1 { + die(); + } + bollard!(die, 4); + csprng.random_delay(); + if paranoid1 != 0 || paranoid2 != 0 { + // do a redundant boot0 check if paranoid mode. + let boot0_check2 = bao1x_hal::sigcheck::validate_image(BOOT0_SELF_CHECK, None, Some(&mut csprng)) + .unwrap_or_else(|_| die()); + csprng.random_delay(); + if boot0_check2.0 != !boot0_check2.1 { + die(); } } - bao1x_hal::sigcheck::die_no_std(); -} + bollard!(die, 4); + // == end self validation == -fn seal_boot0_keys() { - // - set an ASID that locks out any boot0 secrets (currently none, as it's PK based) - // - this does not offer strong security, but prevents someone with an arbitrary read primitive from - // accessing any boot0 secrets. An arbitrary-exec primitive at this point can, of course, undo the ASID - // mapping. - // - // Only necessary if we have secrets to seal. The current implementation only contains a public key, - // so there's no secrets to seal. + bollard!(die, 4); + let boot_order = match owc.get_decoded::() { + // Primary boot selected. Check Boot1 first, then fall back to LOADER/BAREMETAL. + Ok(bao1x_api::AltBootCoding::PrimaryPartition) => [BOOT0_TO_BOOT1, BOOT0_TO_ALTBOOT1], + // Alternate boot selected. Check LOADER/BAREMETAL, then fall back to Boot1. + Ok(bao1x_api::AltBootCoding::AlternatePartition) => [BOOT0_TO_ALTBOOT1, BOOT0_TO_BOOT1], + Err(_) => { + crate::println!("Internal error: alt boot encoding is invalid!"); + bao1x_hal::sigcheck::die_no_std(); + } + }; + for configuration in boot_order { + bollard!(die, 4); + csprng.random_delay(); + match bao1x_hal::sigcheck::validate_image(configuration, None, Some(&mut csprng)) { + Ok((key, key_inv, tag, target)) => { + if key != !key_inv { + die(); + } + // implement the hardened erase policy. This is marked #[inline(always)]. + hardened_erase_policy(paranoid1, paranoid2, key, key_inv, tag, &mut csprng) + .inspect_err(|e| crate::println!("{}", e)) + .ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks + // second check if paranoid is not 0. While this branch can be glitched over, to get here, + // you had to glitch into this routine. + bollard!(die, 4); + csprng.random_delay(); + if paranoid1 != 0 || paranoid2 != 0 { + // re-check the image: it *should* pass. If not, die. + bao1x_hal::sigcheck::validate_image(configuration, None, Some(&mut csprng)) + .unwrap_or_else(|_| die()); + } + csprng.random_delay(); + bollard!(die, 4); + jump_to((target ^ u32::from_le_bytes(tag)) as usize); + } + _ => {} + } + } + + bollard!(die, 4); + bao1x_hal::sigcheck::die_no_std(); } #[cfg(feature = "print-ifr")] diff --git a/bao1x-boot/boot0/src/repl.rs b/bao1x-boot/boot0/src/repl.rs index 3471b3c3d..0532372e2 100644 --- a/bao1x-boot/boot0/src/repl.rs +++ b/bao1x-boot/boot0/src/repl.rs @@ -133,16 +133,10 @@ impl Repl { } crate::println!(""); } - "check" => match bao1x_hal::sigcheck::validate_image( - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &[FunctionCode::Boot0 as u32], // only boot0 is allowed for boot0 - false, - None, - None, - ) { - Ok(key_number, _key_number2, _id) => crate::println!("sigcheck passed on key {}", key_number), + "check" => match bao1x_hal::sigcheck::validate_image(BOOT0_SELF_CHECK, None, None) { + Ok(key_number, _key_number2, _id, _target) => { + crate::println!("sigcheck passed on key {}", key_number) + } Err(e) => crate::println!("sigcheck failed: {}", e), }, "reps" => { @@ -163,16 +157,7 @@ impl Repl { if new_time >= start_time + 5 { break; } - bao1x_hal::sigcheck::validate_image( - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_START as *const u32, - bao1x_api::BOOT0_REVOCATION_OFFSET, - &[FunctionCode::Boot0 as u32], // only boot0 is allowed for boot0 - false, - None, - None, - ) - .ok(); + bao1x_hal::sigcheck::validate_image(BOOT0_SELF_CHECK, None, None).ok(); count += 1; } crate::println!("{} reps/sec", count / 5);