From 94524227513f8f242e016c4885039ca7f67480be Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 23:43:46 +0800 Subject: [PATCH 1/3] add some apis for owc frobbing --- libs/bao1x-hal/src/acram.rs | 7 ++++++- libs/keystore-api/src/gen2_api.rs | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libs/bao1x-hal/src/acram.rs b/libs/bao1x-hal/src/acram.rs index 95410008f..8244a6fc9 100644 --- a/libs/bao1x-hal/src/acram.rs +++ b/libs/bao1x-hal/src/acram.rs @@ -30,12 +30,17 @@ pub const ONEWAY_MAX_VALUE: u32 = 10_0000; /// a single boot attempt. pub const ONEWAY_MAX_DELTA: u32 = 512; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub enum OneWayErr { OutOfBounds, IncFail, InvalidCoding, + // used to report messaging errors in xous environments + InternalError, + // used to indicate things are OK in xous environments + None, } + pub struct OneWayCounter { #[cfg(feature = "std")] mapping: MemoryRange, diff --git a/libs/keystore-api/src/gen2_api.rs b/libs/keystore-api/src/gen2_api.rs index 24aebf613..10a125cba 100644 --- a/libs/keystore-api/src/gen2_api.rs +++ b/libs/keystore-api/src/gen2_api.rs @@ -24,6 +24,9 @@ pub enum Opcode { /// Flag operations GetFlags = 512, SetFlags = 513, + /// One way counter operations + GetOneWayCounter = 768, + IncOneWayCounter = 769, // ----- below are non-cryptographic opcodes but used to manipulate sensitive state ----- /// Set bootwait parameters @@ -41,3 +44,8 @@ pub enum EphemeralOp { GetMsb, SetMsb, } + +/// Series of magic numbers not meant for cryptographic authentication, +/// but for detecting fat-fingered API implementations. +pub const OWC_MAGIC_GET: [usize; 3] = [0x2b46_2ab3, 0xf7e3_1b59, 0x7bba_d222]; +pub const OWC_MAGIC_INC: [usize; 3] = [0xeb5d_fc81, 0x8b6d_6a90, 0xf720_491a]; From f2371f49eb3a46d3671ec0300d58ffa0d6e03bea Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 23:43:57 +0800 Subject: [PATCH 2/3] add implementation for owc frobbing by default the knob is not frobbable because frobbing the knob can lead to potential griefing more specifically the owc (one way counters) control some security state. they are all designed to "fail on increment" but just giving someone a button to increment random counters gives a DoS vector. So unless you need to open the API it's recommended to not have it activated. --- services/keystore/Cargo.toml | 4 + services/keystore/src/lib.rs | 81 ++++++++++++++++++- .../keystore/src/platform/baosec/server.rs | 35 ++++++++ .../keystore/src/platform/baosec/store.rs | 2 +- 4 files changed, 120 insertions(+), 2 deletions(-) diff --git a/services/keystore/Cargo.toml b/services/keystore/Cargo.toml index c2a6bcf7c..efb17a6cd 100644 --- a/services/keystore/Cargo.toml +++ b/services/keystore/Cargo.toml @@ -78,6 +78,10 @@ bao1x = ["utralib/bao1x", "aes/bao1x"] debug-hal = ["bao1x-hal/debug-print-duart"] +# Enables incrementing one way counters. Off by default because this API can be, at the very least, +# used to grief the security layer if not worse +owc-inc = [] + board-baosec = [ "utralib/bao1x", "keystore-api/gen2", diff --git a/services/keystore/src/lib.rs b/services/keystore/src/lib.rs index 68cc0c8fa..ae623cc8a 100644 --- a/services/keystore/src/lib.rs +++ b/services/keystore/src/lib.rs @@ -1,4 +1,4 @@ -use bao1x_api::BackupFlags; +use bao1x_api::{BackupFlags, OneWayEncoding}; pub use cipher::{ BlockBackend, BlockCipher, BlockClosure, BlockDecrypt, BlockEncrypt, BlockSizeUser, ParBlocksSizeUser, consts::U16, generic_array::GenericArray, inout::InOut, @@ -10,6 +10,8 @@ use xous_ipc::Buffer; pub(crate) type BatchBlocks = GenericArray; use std::convert::TryInto; +use bao1x_hal::acram::*; + #[derive(Debug)] // there is no confidential information in the external structure; it's safe to Debug it pub struct Keystore { conn: CID, @@ -234,6 +236,83 @@ impl Keystore { } } + /// Marked as `unsafe` because the offset needs to be correct. It's recommended to use + /// `inc_owc_coded()` where possible. This function is necessary for the cases that don't + /// fit into the `encode_oneway` mechanism, e.g. key revocations, etc. + /// + /// All you have to do to be safe is no be super-sure you got the offset right. + #[cfg(feature = "owc-inc")] + pub unsafe fn inc_owc(&self, offset: usize) -> Result<(), OneWayErr> { + let result = xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + Opcode::IncOneWayCounter.to_usize().unwrap(), + offset, + OWC_MAGIC_INC[0], + OWC_MAGIC_INC[1], + OWC_MAGIC_INC[2], + ), + ); + match result { + Ok(xous::Result::Scalar5(_, arg1, _arg2, _arg3, _arg4)) => { + match num_traits::FromPrimitive::from_usize(arg1) { + Some(OneWayErr::None) => Ok(()), + Some(e) => Err(e), + _ => Err(OneWayErr::InternalError), + } + } + _ => Err(OneWayErr::InternalError), + } + } + + /// Automatically increments the correct slot based on the OFFSET encoded in the definition + #[cfg(feature = "owc-inc")] + pub fn inc_owc_coded(&self) -> Result<(), OneWayErr> + where + T: OneWayEncoding, + T::Error: core::fmt::Debug, + { + let offset: usize = T::OFFSET; + if offset < MAX_ONEWAY_COUNTERS { + unsafe { self.inc_owc(offset) } + } else { + Err(OneWayErr::OutOfBounds) + } + } + + pub fn get_owc(&self, offset: usize) -> Result { + let result = xous::send_message( + self.conn, + xous::Message::new_blocking_scalar( + Opcode::GetOneWayCounter.to_usize().unwrap(), + offset, + OWC_MAGIC_GET[0], + OWC_MAGIC_GET[1], + OWC_MAGIC_GET[2], + ), + ); + match result { + Ok(xous::Result::Scalar5(_id, arg1, arg2, _arg3, _arg4)) => { + match num_traits::FromPrimitive::from_usize(arg1) { + Some(OneWayErr::None) => Ok(arg2 as u32), + Some(e) => Err(e), + _ => Err(OneWayErr::InternalError), + } + } + _ => Err(OneWayErr::InternalError), + } + } + + pub fn get_owc_decoded(&self) -> Result + where + T: OneWayEncoding, + T: TryFrom, + T::Error: core::fmt::Debug, + { + let raw = self.get_owc(T::OFFSET)?; + T::try_from(raw).map_err(|_| OneWayErr::InvalidCoding) + } + #[cfg(feature = "bao1x")] pub fn set_ephemeral_key(&self, key: &[u8; bao1x_hal::buram::KEY_LEN]) -> Result<(), xous::Error> { let offset = 0; diff --git a/services/keystore/src/platform/baosec/server.rs b/services/keystore/src/platform/baosec/server.rs index 4072f9ed4..6c3297539 100644 --- a/services/keystore/src/platform/baosec/server.rs +++ b/services/keystore/src/platform/baosec/server.rs @@ -1,7 +1,9 @@ use bao1x_api::BackupFlags; +use bao1x_hal::acram::{MAX_ONEWAY_COUNTERS, OneWayErr}; use bao1x_hal::board::{BOOKEND_END, BOOKEND_START}; use bao1x_hal::rram::Reram; use keystore_api::*; +use num_traits::*; use xous::SID; use xous_ipc::Buffer; @@ -119,6 +121,39 @@ pub fn keystore(sid: SID) -> ! { if store.is_developer() { scalar.arg1 = 0 } else { scalar.arg1 = 1 } } } + Opcode::IncOneWayCounter => { + if let Some(scalar) = msg.body.scalar_message_mut() { + if [scalar.arg2, scalar.arg3, scalar.arg4] == OWC_MAGIC_INC + && scalar.arg1 < MAX_ONEWAY_COUNTERS + { + match unsafe { store.owc.inc(scalar.arg1) } { + Ok(_) => { + scalar.arg1 = OneWayErr::None.to_usize().unwrap(); + } + Err(e) => scalar.arg1 = e.to_usize().unwrap(), + } + } else { + scalar.arg1 = OneWayErr::InternalError.to_usize().unwrap(); + } + } + } + Opcode::GetOneWayCounter => { + if let Some(scalar) = msg.body.scalar_message_mut() { + if [scalar.arg2, scalar.arg3, scalar.arg4] == OWC_MAGIC_GET + && scalar.arg1 < MAX_ONEWAY_COUNTERS + { + match store.owc.get(scalar.arg1) { + Ok(val) => { + scalar.arg1 = OneWayErr::None.to_usize().unwrap(); + scalar.arg2 = val as usize; + } + Err(e) => scalar.arg1 = e.to_usize().unwrap(), + } + } else { + scalar.arg1 = OneWayErr::InternalError.to_usize().unwrap(); + } + } + } Opcode::InvalidCall => { log::error!("Invalid call in keystore: {:?}", opcode); } diff --git a/services/keystore/src/platform/baosec/store.rs b/services/keystore/src/platform/baosec/store.rs index 942cbb6eb..7c83b2fbe 100644 --- a/services/keystore/src/platform/baosec/store.rs +++ b/services/keystore/src/platform/baosec/store.rs @@ -21,7 +21,7 @@ const KEY_LEN: usize = bao1x_api::SLOT_ELEMENT_LEN_BYTES; pub struct KeyStore { slot_mgr: SlotManager, - owc: OneWayCounter, + pub owc: OneWayCounter, master_key: Option<[u8; KEY_LEN]>, } From 44a53e590a9e4a1dc077b17f938660581dc65eea Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 4 Apr 2026 00:08:39 +0800 Subject: [PATCH 3/3] move error definition to api crate, fixes hosted ci --- libs/bao1x-api/src/offsets/common.rs | 11 +++++++++++ libs/bao1x-hal/src/acram.rs | 13 +------------ services/keystore/src/lib.rs | 4 +--- services/keystore/src/platform/baosec/server.rs | 4 ++-- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/libs/bao1x-api/src/offsets/common.rs b/libs/bao1x-api/src/offsets/common.rs index ba3a31408..9344e1d2d 100644 --- a/libs/bao1x-api/src/offsets/common.rs +++ b/libs/bao1x-api/src/offsets/common.rs @@ -20,6 +20,17 @@ pub const RRAM_STORAGE_LEN: usize = 0x3D_A000; pub const SWAP_START_UF2: usize = 0x7000_0000; pub const SWAP_UF2_LEN: usize = 0x0800_0000; // 128 MiB +#[derive(Clone, Copy, Debug, PartialEq, Eq, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum OneWayErr { + OutOfBounds, + IncFail, + InvalidCoding, + // used to report messaging errors in xous environments + InternalError, + // used to indicate things are OK in xous environments + None, +} + // Define a trait with just the offset pub trait OneWayEncoding: TryFrom { const OFFSET: usize; diff --git a/libs/bao1x-hal/src/acram.rs b/libs/bao1x-hal/src/acram.rs index 8244a6fc9..3c06a45b9 100644 --- a/libs/bao1x-hal/src/acram.rs +++ b/libs/bao1x-hal/src/acram.rs @@ -7,9 +7,9 @@ //! In the case of baremetal targets of course everything is mapped, so these //! restrictions are not a concern. -use bao1x_api::OneWayEncoding; use bao1x_api::bollard; use bao1x_api::offsets::*; +use bao1x_api::{OneWayEncoding, OneWayErr}; #[cfg(feature = "std")] use xous::MemoryRange; @@ -30,17 +30,6 @@ pub const ONEWAY_MAX_VALUE: u32 = 10_0000; /// a single boot attempt. pub const ONEWAY_MAX_DELTA: u32 = 512; -#[derive(Clone, Copy, Debug, PartialEq, Eq, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub enum OneWayErr { - OutOfBounds, - IncFail, - InvalidCoding, - // used to report messaging errors in xous environments - InternalError, - // used to indicate things are OK in xous environments - None, -} - pub struct OneWayCounter { #[cfg(feature = "std")] mapping: MemoryRange, diff --git a/services/keystore/src/lib.rs b/services/keystore/src/lib.rs index ae623cc8a..434315142 100644 --- a/services/keystore/src/lib.rs +++ b/services/keystore/src/lib.rs @@ -1,4 +1,4 @@ -use bao1x_api::{BackupFlags, OneWayEncoding}; +use bao1x_api::{BackupFlags, OneWayEncoding, OneWayErr}; pub use cipher::{ BlockBackend, BlockCipher, BlockClosure, BlockDecrypt, BlockEncrypt, BlockSizeUser, ParBlocksSizeUser, consts::U16, generic_array::GenericArray, inout::InOut, @@ -10,8 +10,6 @@ use xous_ipc::Buffer; pub(crate) type BatchBlocks = GenericArray; use std::convert::TryInto; -use bao1x_hal::acram::*; - #[derive(Debug)] // there is no confidential information in the external structure; it's safe to Debug it pub struct Keystore { conn: CID, diff --git a/services/keystore/src/platform/baosec/server.rs b/services/keystore/src/platform/baosec/server.rs index 6c3297539..d262421ae 100644 --- a/services/keystore/src/platform/baosec/server.rs +++ b/services/keystore/src/platform/baosec/server.rs @@ -1,5 +1,5 @@ -use bao1x_api::BackupFlags; -use bao1x_hal::acram::{MAX_ONEWAY_COUNTERS, OneWayErr}; +use bao1x_api::{BackupFlags, OneWayErr}; +use bao1x_hal::acram::MAX_ONEWAY_COUNTERS; use bao1x_hal::board::{BOOKEND_END, BOOKEND_START}; use bao1x_hal::rram::Reram; use keystore_api::*;