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.
This commit is contained in:
bunnie 2026-04-03 23:43:57 +08:00
parent 9452422751
commit f2371f49eb
4 changed files with 120 additions and 2 deletions

View file

@ -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",

View file

@ -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<Block, U16>;
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<T>(&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<u32, OneWayErr> {
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<T>(&self) -> Result<T, OneWayErr>
where
T: OneWayEncoding,
T: TryFrom<u32>,
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;

View file

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

View file

@ -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]>,
}