From aaf1cbd2f9414ce3e88c668f30d507669a7be7f4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 4 Nov 2025 13:13:52 +0800 Subject: [PATCH] add base routines for deriving & controlling serial numbers --- libs/bao1x-api/src/offsets/common.rs | 22 ++++++++----- libs/bao1x-hal/src/usb/mod.rs | 48 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/libs/bao1x-api/src/offsets/common.rs b/libs/bao1x-api/src/offsets/common.rs index eddf89168..e5c82a2ac 100644 --- a/libs/bao1x-api/src/offsets/common.rs +++ b/libs/bao1x-api/src/offsets/common.rs @@ -127,14 +127,20 @@ pub const BOOT0_PUBKEY_FAIL: usize = 87; pub const INVOKE_DABAO_KEY_SETUP: usize = 88; pub const DABAO_KEY_SETUP_DONE: usize = 89; -/// When set, the system will prefer to present generic, fixed identifiers when challenged -/// by external systems. The canonical use case for this is the serial number field in the -/// USB device descriptor: normally, GENERIC_IDENTIFIERS is `0`, which means the device will -/// present a semi-unique serial number (this is useful for users who plug in multiple devices -/// and want to tell them apart). However, privacy-conscious users who don't need or want -/// to tell devices apart can increment this OWC and then the USB serial number will be -/// replaced with a fixed pattern that is common across all devices. -pub const GENERIC_IDENTIFIERS: usize = 90; +encode_oneway! { + #[offset = 90] + /// When set, the system will prefer to present generic, fixed identifiers when challenged + /// by external systems. The canonical use case for this is the serial number field in the + /// USB device descriptor: normally, ExternalIdentifiers is `0`, which means the device will + /// present a semi-unique serial number (this is useful for users who plug in multiple devices + /// and want to tell them apart). However, privacy-conscious users who don't need or want + /// to tell devices apart can increment this OWC and then the USB serial number will be + /// replaced with a fixed pattern that is common across all devices. + pub enum ExternalIdentifiers{ + SerialNumber, + Anonymous, + } +} /// Total number of public key slots in the system. Pubkey revocations are at the "top of range" pub const PUBKEY_SLOTS: usize = 4; diff --git a/libs/bao1x-hal/src/usb/mod.rs b/libs/bao1x-hal/src/usb/mod.rs index 0fee68282..b96837cc0 100644 --- a/libs/bao1x-hal/src/usb/mod.rs +++ b/libs/bao1x-hal/src/usb/mod.rs @@ -1,4 +1,52 @@ +#[cfg(all(not(feature = "std"), not(feature = "kernel")))] +extern crate alloc; +#[cfg(all(not(feature = "std"), not(feature = "kernel")))] +use alloc::string::{String, ToString}; + #[cfg(not(feature = "std"))] pub mod compat; pub mod driver; pub mod utra; + +#[cfg(all(not(feature = "std"), not(feature = "kernel")))] +pub fn derive_usb_serial_number( + owc: &crate::acram::OneWayCounter, + slot_mgr: &crate::acram::SlotManager, +) -> String { + use bao1x_api::{ExternalIdentifiers, UUID}; + + match owc.get_decoded::().unwrap() { + ExternalIdentifiers::Anonymous => "Anonymous".to_string(), + ExternalIdentifiers::SerialNumber => { + let uuid = slot_mgr.read(&UUID).unwrap(); + let mut ret = String::new(); + for (i, &src) in uuid.iter().enumerate() { + let mut masked_src = src & 0b0001_1111; + if (i == 0) && masked_src <= 10 { + masked_src += 11; // Avoid starting with a number or 'A' + } + // Translation table for charset "0123456789ABCDFGHJKLMNPQRSTVWXYZ" (32 symbols) + // 0123456789 ABCD E FGH I JKLMN O PQRST U VWXYZ + // 0000000000 1111 1 111 1 12222 2 22222 3 33333 + // 0123456789 0123 4 567 8 90123 4 56789 0 12345 + // 0000000000 1111 111 11122 22222 22233 + // 0123456789 0123 456 78901 23456 78901 + let c: char = char::from_u32(match masked_src { + x @ 0..=9 => ('0' as u8) + x, + x @ 10..=13 => ('A' as u8) + x - 10, + x @ 14..=16 => ('F' as u8) + x - 14, + x @ 17..=21 => ('J' as u8) + x - 17, + x @ 22..=26 => ('P' as u8) + x - 22, + x @ 27..=31 => ('V' as u8) + x - 27, + _ => '0' as u8, + } as u32) + .unwrap_or('.'); + ret.push(c); + if ret.len() == 8 { + break; + } + } + ret + } + } +}