add base routines for deriving & controlling serial numbers

This commit is contained in:
bunnie 2025-11-04 13:13:52 +08:00
parent ef0f7848e7
commit aaf1cbd2f9
2 changed files with 62 additions and 8 deletions

View file

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

View file

@ -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::<ExternalIdentifiers>().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
}
}
}