From f2371f49eb3a46d3671ec0300d58ffa0d6e03bea Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 3 Apr 2026 23:43:57 +0800 Subject: [PATCH] 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]>, }