add optionality to turn on clock skipping manually

this is off by default, because it doesn't seem to be entirely
stable. For now, the default configuration when it is turned on,
only activates it for the PKE block. When the CPU block clock
skipping is enabled, instability is occasionally observed.
This commit is contained in:
bunnie 2026-02-10 22:56:14 +08:00
parent 272b983760
commit a2330e8911
No known key found for this signature in database
4 changed files with 84 additions and 5 deletions

View file

@ -20,7 +20,9 @@ use bao1x_api::{
};
use bao1x_api::{PARANOID_MODE, PARANOID_MODE_DUPE};
use bao1x_hal::acram::OneWayCounter;
use bao1x_hal::hardening::{check_pll, die, mesh_setup};
use bao1x_hal::hardening::{
check_pll, die, disable_clock_skipping, mesh_setup, reseed_skipping, setup_clock_skipping,
};
use bao1x_hal::sigcheck::{hardened_erase_policy, jump_to};
#[cfg(feature = "unsafe-dev")]
use critical_section::Mutex;
@ -276,6 +278,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
}
}
let use_skipping = setup_clock_skipping(csprng.get_u32());
let (paranoid1, paranoid2) = owc.hardened_get2(PARANOID_MODE, PARANOID_MODE_DUPE).unwrap();
// == self-validate the image with the keys we put in, just to make sure our code wasn't tampered with ==
@ -332,6 +335,9 @@ pub unsafe extern "C" fn rust_entry() -> ! {
for configuration in boot_order {
bollard!(die, 4);
csprng.random_delay();
if use_skipping {
reseed_skipping(csprng.get_u32());
}
match bao1x_hal::sigcheck::validate_image(configuration, None, Some(&mut csprng)) {
Ok((key, key_inv, tag, target)) => {
if key != !key_inv {
@ -352,6 +358,9 @@ pub unsafe extern "C" fn rust_entry() -> ! {
}
csprng.random_delay();
bollard!(die, 4);
if use_skipping {
disable_clock_skipping();
}
jump_to((target ^ u32::from_le_bytes(tag)) as usize);
}
Err(e) => {

View file

@ -48,6 +48,7 @@ print-ifr = []
test-boot0-keys = []
alt-boot1 = []
test-clock-skipping = []
oem = []
oem-baosec-lite = ["oem", "bao1x-hal/oem-baosec-lite"]

View file

@ -6,8 +6,8 @@ use alloc::vec::Vec;
use bao1x_api::pubkeys::BOOT0_TO_BOOT1;
#[allow(unused_imports)]
use bao1x_api::*;
use bao1x_hal::acram::OneWayCounter;
use bao1x_hal::hardening::Csprng;
use bao1x_hal::acram::{OneWayCounter, SlotManager};
use bao1x_hal::hardening::{Csprng, skipping_enabled};
use utralib::*;
pub struct Error {
@ -240,6 +240,65 @@ impl Repl {
));
}
}
"skipping" => {
let slot_mgr = SlotManager::new();
if args.len() != 1 {
return Err(Error::help("skipping [check | enable | disable]"));
}
if args[0] == "check" {
let skipping_cfg = slot_mgr.read(&bao1x_api::CLOCK_SCRAMBLE_PARAMS).unwrap();
crate::println!("Clock skipping: {:?}", skipping_enabled(skipping_cfg));
} else if args[0] == "enable" {
bao1x_hal::hardening::enable_skipping();
} else if args[0] == "disable" {
bao1x_hal::hardening::disable_skipping();
} else {
return Err(Error::help("skipping [check | enable | disable]"));
}
}
#[cfg(feature = "test-clock-skipping")]
"bogomips" => {
crate::println!("start test");
bao1x_hal::hardening::enable_skipping();
// start the RTC
let mut ao_sysctrl = CSR::new(utralib::HW_AO_SYSCTRL_BASE as *mut u32);
ao_sysctrl.wo(utra::ao_sysctrl::CR_CLK1HZFD, 0x3fff);
unsafe { (0x4006100c as *mut u32).write_volatile(1) };
let mut count: usize;
unsafe {
#[rustfmt::skip]
core::arch::asm!(
// grab the RTC value
"li t0, 0x40061000",
"lw t1, 0x0(t0)",
"li t3, 0",
// wait until the next second
"10:",
"lw t2, 0x0(t0)",
"beq t1, t2, 10b",
// start of test
"20:",
// count outer loops
"addi t3, t3, 1",
// inner loop 10,000 times
"li t4, 10000",
"30:",
"addi t4, t4, -1",
"bne x0, t4, 30b",
// after inner loop, check current time; do another outer loop if time is same
"lw t1, 0x0(t0)",
"beq t1, t2, 20b",
out("t0") _,
out("t1") _,
out("t2") _,
out("t3") count,
out("t4") _,
);
}
crate::println!("{}.{} bogomips", (count * 2 * 10_000) / 1_000_000, (count * 2) % 10_000);
bao1x_hal::hardening::disable_skipping();
ao_sysctrl.wo(utra::ao_sysctrl::CR_CLK1HZFD, 15);
}
"boardtype" => {
let one_way = OneWayCounter::new();
if args.len() == 0 {
@ -581,7 +640,7 @@ impl Repl {
_ => {
crate::println!("Command not recognized: {}", cmd);
crate::print!(
"Commands include: reset, echo, altboot, boot, bootwait, idmode, localecho, uf2, boardtype, audit, lockdown, paranoid, self_destruct, ifr"
"Commands include: reset, echo, altboot, boot, bootwait, idmode, localecho, uf2, boardtype, audit, lockdown, paranoid, self_destruct, ifr, skipping"
);
#[cfg(feature = "test-boot0-keys")]
crate::print!(", publock");

View file

@ -1,7 +1,7 @@
use bao1x_api::{
DEVELOPER_MODE, PARANOID_MODE, PARANOID_MODE_DUPE, bollard, pubkeys::BOOT1_TO_LOADER_OR_BAREMETAL,
};
use bao1x_hal::hardening::Csprng;
use bao1x_hal::hardening::{Csprng, disable_clock_skipping, reseed_skipping, setup_clock_skipping};
#[inline(always)]
fn seal_boot1_keys() {
@ -19,6 +19,7 @@ fn seal_boot1_keys() {
}
pub fn try_boot(or_die: bool, csprng: &mut Csprng) {
let use_skipping = setup_clock_skipping(csprng.get_u32());
let one_way = bao1x_hal::acram::OneWayCounter::new();
bollard!(bao1x_hal::sigcheck::die_no_std, 4);
@ -96,6 +97,9 @@ pub fn try_boot(or_die: bool, csprng: &mut Csprng) {
// double up the security check if in paranoid mode
csprng.random_delay();
if paranoid1 != 0 || paranoid2 != 0 {
if use_skipping {
reseed_skipping(csprng.get_u32());
}
bollard!(bao1x_hal::sigcheck::die_no_std, 4);
bao1x_hal::sigcheck::validate_image(BOOT1_TO_LOADER_OR_BAREMETAL, None, Some(csprng))
.unwrap_or_else(|_| bao1x_hal::hardening::die());
@ -108,6 +112,9 @@ pub fn try_boot(or_die: bool, csprng: &mut Csprng) {
// like we do in boot0.
seal_boot1_keys();
bollard!(bao1x_hal::sigcheck::die_no_std, 4);
if use_skipping {
disable_clock_skipping();
}
bao1x_hal::sigcheck::jump_to((target ^ u32::from_le_bytes(tag)) as usize);
}
Err(e) => crate::println!("Image did not validate: {:?}", e),
@ -116,4 +123,7 @@ pub fn try_boot(or_die: bool, csprng: &mut Csprng) {
crate::println!("No valid loader or baremetal image found. Halting!");
bao1x_hal::sigcheck::die_no_std();
}
if use_skipping {
disable_clock_skipping();
}
}