Merge pull request #844 from betrusted-io/owc-api

OWC api
This commit is contained in:
bunnie 2026-04-05 23:21:33 +08:00 committed by GitHub
commit 3230946c73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 139 additions and 10 deletions

View file

@ -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<u32> {
const OFFSET: usize;

View file

@ -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,12 +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)]
pub enum OneWayErr {
OutOfBounds,
IncFail,
InvalidCoding,
}
pub struct OneWayCounter {
#[cfg(feature = "std")]
mapping: MemoryRange,

View file

@ -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];

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, OneWayErr};
pub use cipher::{
BlockBackend, BlockCipher, BlockClosure, BlockDecrypt, BlockEncrypt, BlockSizeUser, ParBlocksSizeUser,
consts::U16, generic_array::GenericArray, inout::InOut,
@ -234,6 +234,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_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::*;
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]>,
}