From 08aa40e557f9a9a6d9e0f4bffb0e565b08a59aa3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Sep 2025 23:11:30 +0800 Subject: [PATCH] add ctap protocol testing routines --- services/bao-console/Cargo.toml | 4 +++ services/bao-console/src/ctap.rs | 53 ++++++++++++++++++++++++++++++++ services/bao-console/src/main.rs | 7 ++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 services/bao-console/src/ctap.rs diff --git a/services/bao-console/Cargo.toml b/services/bao-console/Cargo.toml index 3bb14c2e7..d17f4cf46 100644 --- a/services/bao-console/Cargo.toml +++ b/services/bao-console/Cargo.toml @@ -30,6 +30,9 @@ rand_chacha = { version = "0.3.1", optional = true } aes = { path = "../aes", default-features = false, optional = true } +# hid bringup +xous-usb-hid = { git = "https://github.com/betrusted-io/xous-usb-hid.git", branch = "main", optional = true } + # aes testing [features] cramium-soc = [ @@ -64,4 +67,5 @@ autobasis = ["rand_chacha", "rand"] modal-testing = ["ux-api", "modals/modal-testing"] test-scrollbars = [] test-flash = [] +ctap-bringup = ["xous-usb-hid"] default = [] diff --git a/services/bao-console/src/ctap.rs b/services/bao-console/src/ctap.rs new file mode 100644 index 000000000..af65ba0ed --- /dev/null +++ b/services/bao-console/src/ctap.rs @@ -0,0 +1,53 @@ +use usb_bao1x::RawFidoReport; + +pub struct XousHidConnection { + pub endpoint: usb_bao1x::UsbHid, +} +impl XousHidConnection { + pub fn u2f_wait_incoming(&self) -> Result { + self.endpoint.u2f_wait_incoming() + } + + pub fn u2f_send(&self, msg: RawFidoReport) -> Result<(), xous::Error> { self.endpoint.u2f_send(msg) } +} + +/// Barest-minimum test to do INIT and PING response as a HID responder. +pub fn ctap_test() { + let _ = std::thread::spawn({ + move || { + let endpoint = usb_bao1x::UsbHid::new(); + let hid_connection = XousHidConnection { endpoint }; + let mut errcnt = 0; + loop { + log::debug!("wait for incoming u2f"); + match hid_connection.u2f_wait_incoming() { + Ok(msg) => { + log::info!("got incoming {:x?}", msg); + let payload = msg.packet; + let mut resp = RawFidoReport::default(); + if payload[4] == 0x86 { + resp.packet[0..7].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x0, 0x11]); + resp.packet[7..7 + 8].copy_from_slice(&payload[7..15]); + resp.packet[15..15 + 9] + .copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x02, 0x05, 0x01, 0x2a, 0x01]); + log::info!("responding {:x?}", resp); + hid_connection.u2f_send(resp).ok(); + } + if payload[4] == 0x81 { + resp.packet[0..7].copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x8]); + resp.packet[7..7 + 8].copy_from_slice(&payload[7..15]); + log::info!("responding {:x?}", resp); + hid_connection.u2f_send(resp).ok(); + } + } + Err(e) => { + if errcnt < 8 { + log::info!("error waiting {:?}", e); + } + errcnt += 1; + } + } + } + } + }); +} diff --git a/services/bao-console/src/main.rs b/services/bao-console/src/main.rs index cfc282e35..4b077fe74 100644 --- a/services/bao-console/src/main.rs +++ b/services/bao-console/src/main.rs @@ -1,7 +1,8 @@ mod cmds; +#[cfg(feature = "ctap-bringup")] +mod ctap; mod repl; mod shell; - use cmds::*; fn main() { @@ -126,6 +127,10 @@ fn main() { } } } + + #[cfg(feature = "ctap-bringup")] + crate::ctap::ctap_test(); + #[cfg(any(feature = "hosted-baosec", not(feature = "battery-readout")))] loop { tt.sleep_ms(2_000).ok();