From fa864ef137d2f9daa4ca949daa445c7349e16ddd Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Tue, 7 Jun 2022 23:42:01 -0700 Subject: [PATCH 01/14] Correct sha binary name Signed-off-by: Ana Hobden --- xtask/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c28fd0ab0..d8699ab85 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -62,7 +62,7 @@ fn main() -> Result<(), Box> { "llio", "susres", "codec", - "sha2:0.9.8", + "sha2", "engine-25519", "spinor", "root-keys", From 52211f078e020d5f840abc073f930155649c8582 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 8 Jun 2022 20:15:32 +0800 Subject: [PATCH 02/14] adjust autotest flow for UDP-based button pressing --- apps/vault/src/ctap/mod.rs | 1 + apps/vault/src/main.rs | 5 +++++ apps/vault/src/ux.rs | 10 +++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index 0f268dbde..928e3179d 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -952,6 +952,7 @@ where _ => return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED), } //(self.check_user_presence)(cid)?; + #[cfg(not(feature="autotest"))] if crate::ux::request_permission_blocking(t!("vault.u2f.reset_check", xous::LANG).to_string(), cid).is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) } diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 29cfb2328..9e58e24ae 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -16,6 +16,11 @@ use ctap::CtapState; mod shims; use shims::*; +// CTAP2 testing notes: +// run our branch and use this to forward the prompts on to the device: +// netcat -k -u -l 6502 > /dev/ttyS0 +// use the "autotest" feature to remove some excess prompts that interfere with the test + /* UI concept: diff --git a/apps/vault/src/ux.rs b/apps/vault/src/ux.rs index 0b64c87ed..a94545e8e 100644 --- a/apps/vault/src/ux.rs +++ b/apps/vault/src/ux.rs @@ -130,8 +130,8 @@ pub(crate) fn start_ux_thread() { let mut app_info: Option = None; let pddb = pddb::Pddb::new(); pddb.is_mounted_blocking(); - #[cfg(feature="autotest")] - modals.show_notification("WARNING: FIDO configured for autotest. Do not use for production!", None).unwrap(); + //#[cfg(feature="autotest")] + //modals.show_notification("WARNING: FIDO configured for autotest. Do not use for production!", None).unwrap(); let mut num_fido_auths = 0; // only counts FIDO2 flow auths, not U2F auths loop { let mut msg = xous::receive_message(sid).unwrap(); @@ -404,14 +404,14 @@ pub(crate) fn start_ux_thread() { } } // check if we got a hit - #[cfg(not(feature="autotest"))] + //#[cfg(not(feature="autotest"))] let key_hit = kbhit.load(Ordering::SeqCst); - #[cfg(feature="autotest")] + /*#[cfg(feature="autotest")] let key_hit = if num_fido_auths != 82 { // 82 is the test number of "don't touch it" 'y' as u32 } else { 0 - }; + };*/ if key_hit != 0 { num_fido_auths += 1; log::info!("got user presence! count: {}", num_fido_auths); From a4d0cdba80c09e5326802b2c1db71f206134787c Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 8 Jun 2022 23:47:44 +0800 Subject: [PATCH 03/14] pass unplug events down to the FIDO stack this is necessary because unplugs actually force some cryptographic state to rekey --- services/usb-device-xous/src/api.rs | 1 + services/usb-device-xous/src/lib.rs | 15 +++++++++++---- services/usb-device-xous/src/main.rs | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 3896a96e0..e132188ae 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -54,5 +54,6 @@ pub enum U2fCode { TxAck, RxWait, RxAck, + Hangup, Denied, } \ No newline at end of file diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index 495c82f59..60dbe9de3 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -209,10 +209,17 @@ impl UsbHid { let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::U2fRxDeferred.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; let ack = buf.to_original::().unwrap(); - assert_eq!(ack.code, U2fCode::RxAck, "Expected U2fCode::RxAck"); - let mut u2fmsg = FidoMsg::default(); - u2fmsg.packet.copy_from_slice(&ack.data); - Ok(u2fmsg) + match ack.code { + U2fCode::RxAck => { + let mut u2fmsg = FidoMsg::default(); + u2fmsg.packet.copy_from_slice(&ack.data); + Ok(u2fmsg) + }, + U2fCode::Hangup => { + Err(xous::Error::ProcessTerminated) + }, + _ => Err(xous::Error::InternalError) + } } pub fn u2f_send(&self, msg: FidoMsg) -> Result<(), xous::Error> { let mut req = U2fMsgIpc { diff --git a/services/usb-device-xous/src/main.rs b/services/usb-device-xous/src/main.rs index f8678312b..476dad75c 100644 --- a/services/usb-device-xous/src/main.rs +++ b/services/usb-device-xous/src/main.rs @@ -189,6 +189,7 @@ fn main() -> ! { let mut fido_rx_queue = VecDeque::<[u8; 64]>::new(); let mut lockstatus_force_update = true; // some state to track if we've been through a susupend/resume, to help out the status thread with its UX update after a restart-from-cold + let mut was_suspend = true; loop { let mut msg = xous::receive_message(usbdev_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { @@ -286,6 +287,24 @@ fn main() -> ! { Err(e) => log::trace!("U2F ERR: {:?}", e), } } + if usb_dev.state() == UsbDeviceState::Suspend { + log::info!("suspend detected"); + if was_suspend == false { + // FIDO listener needs to know when USB was unplugged, so that it can reset state per FIDO2 spec + if let Some(mut listener) = fido_listener.take() { + let mut response = unsafe { + Buffer::from_memory_message_mut(listener.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + assert_eq!(buf.code, U2fCode::RxWait, "Expected U2fcode::RxWait in wrapper"); + buf.code = U2fCode::Hangup; + response.replace(buf).unwrap(); + } + } + was_suspend = true; + } else { + was_suspend = false; + } }, Some(Opcode::SwitchCores) => msg_blocking_scalar_unpack!(msg, core, _, _, _, { if core == 1 { From 3fc038cca98f9d84df4acf0c04671453dc4dd079 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 8 Jun 2022 23:48:09 +0800 Subject: [PATCH 04/14] add some missing translations, and add a listener to the unplug state --- apps/vault/locales/i18n.json | 12 ++++++++++++ apps/vault/src/ctap/mod.rs | 15 ++++++++++++--- apps/vault/src/main.rs | 10 +++++++++- apps/vault/src/ux.rs | 12 ++---------- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/apps/vault/locales/i18n.json b/apps/vault/locales/i18n.json index d88e37d53..ae9952958 100644 --- a/apps/vault/locales/i18n.json +++ b/apps/vault/locales/i18n.json @@ -5,6 +5,18 @@ "zh": "秒 直到中止", "en-tts": "s until abort" }, + "vault.fido.pin_uv_auth": { + "en": "FIDO host is creating a PIN.\nPress any key to accept.", + "ja": "FIDOホストがPINを作成しています。\n任意のキーを押して受け入れます。", + "zh": "FIDO 主机正在创建 PIN。\n按任意键接受。", + "en-tts": "FIDO host is creating a PIN.\nPress any key to accept." + }, + "vault.fido.next_assertion": { + "en": "This site is requires multiple credentials.\nPress any key to allow the next credential.", + "ja": "このサイトには複数のクレデンシャルが必要です。\n次のクレデンシャルを許可するには、任意のキーを押してください。", + "zh": "此站点需要多个凭据。\n按任意键以允许下一个凭据。", + "en-tts": "This site is requires multiple credentials.\nPress any key to allow the next credential." + }, "vault.fido.make_credential": { "en": "FIDO Make Credential Request.\nPress any key to approve.", "ja": "FIDOは資格情報を要求します\n承認するには任意のキーを押してください", diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index 928e3179d..34928c1c9 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -397,7 +397,9 @@ where // This case was added in FIDO 2.1. if auth_param.is_empty() { //(self.check_user_presence)(cid)?; - if crate::ux::request_permission_blocking("pin_uv_auth_precheck".to_string(), cid).is_none() { + if crate::ux::request_permission_blocking( + t!("vault.fido.pin_uv_auth", xous::LANG).to_string(), + cid).is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) } if self.persistent_store.pin_hash()?.is_none() { @@ -859,7 +861,10 @@ where _now: ClockValue, ) -> Result { //self.check_command_permission(now)?; - if crate::ux::request_permission_blocking("GetAssertion".to_string(), [0xff, 0xff, 0xff, 0xff]).is_none() { + if crate::ux::request_permission_blocking( + t!("vault.fido.next_assertion", xous::LANG).to_string(), + [0xff, 0xff, 0xff, 0xff] + ).is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED); } let (assertion_input, credential) = @@ -931,6 +936,9 @@ where ) } + pub fn unplug_reset(&mut self) { + self.pin_protocol_v1.reset(self.rng); + } fn process_reset( &mut self, cid: ChannelID, @@ -939,7 +947,7 @@ where // Resets are only possible in the first 10 seconds after booting. // TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? // self.check_command_permission(now)?; - if let Some(c) = crate::ux::request_permission_blocking(locales::t!("vault.u2f.factoryreset", xous::LANG).to_string(), cid) { + if let Some(c) = crate::ux::request_permission_blocking(t!("vault.u2f.factoryreset", xous::LANG).to_string(), cid) { if c != 'y' { return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) } @@ -952,6 +960,7 @@ where _ => return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED), } //(self.check_user_presence)(cid)?; + // we skip the double-confirmation during autotesting, but leave it here for production #[cfg(not(feature="autotest"))] if crate::ux::request_permission_blocking(t!("vault.u2f.reset_check", xous::LANG).to_string(), cid).is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 9e58e24ae..5d2f22185 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -115,7 +115,15 @@ fn main() -> ! { } } Err(e) => { - log::warn!("FIDO listener got an error: {:?}", e); + match e { + xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator + log::info!("CTAP unplug_reset"); + ctap_state.unplug_reset(); + }, + _ => { + log::warn!("FIDO listener got an error: {:?}", e); + } + } } } } diff --git a/apps/vault/src/ux.rs b/apps/vault/src/ux.rs index a94545e8e..a218e97a9 100644 --- a/apps/vault/src/ux.rs +++ b/apps/vault/src/ux.rs @@ -130,8 +130,8 @@ pub(crate) fn start_ux_thread() { let mut app_info: Option = None; let pddb = pddb::Pddb::new(); pddb.is_mounted_blocking(); - //#[cfg(feature="autotest")] - //modals.show_notification("WARNING: FIDO configured for autotest. Do not use for production!", None).unwrap(); + #[cfg(feature="autotest")] + modals.show_notification("WARNING: FIDO configured for autotest. Do not use for production!", None).unwrap(); let mut num_fido_auths = 0; // only counts FIDO2 flow auths, not U2F auths loop { let mut msg = xous::receive_message(sid).unwrap(); @@ -403,15 +403,7 @@ pub(crate) fn start_ux_thread() { last_timer = new_timer; } } - // check if we got a hit - //#[cfg(not(feature="autotest"))] let key_hit = kbhit.load(Ordering::SeqCst); - /*#[cfg(feature="autotest")] - let key_hit = if num_fido_auths != 82 { // 82 is the test number of "don't touch it" - 'y' as u32 - } else { - 0 - };*/ if key_hit != 0 { num_fido_auths += 1; log::info!("got user presence! count: {}", num_fido_auths); From e68c976735f603ce4f3cecf82007ebda87bda3e8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 9 Jun 2022 00:20:36 +0800 Subject: [PATCH 05/14] configure vault for production and add maintainer's notes --- apps/vault/Cargo.toml | 2 +- apps/vault/src/main.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index 5a30e4300..66f4521e6 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -45,4 +45,4 @@ with_ctap1 = ["ctap-crypto/with_ctap1"] with_ctap2_1 = [] autotest = [] multireset = [] # allows credential reset more than once per boot -default = ["with_ctap1", "with_ctap2_1", "autotest"] +default = ["with_ctap1", "with_ctap2_1"] diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 5d2f22185..a7fc4cd79 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -21,6 +21,11 @@ use shims::*; // netcat -k -u -l 6502 > /dev/ttyS0 // use the "autotest" feature to remove some excess prompts that interfere with the test +// the OpenSK code is based off of commit f2496a8e6d71a4e838884996a1c9b62121f87df2 from the +// Google OpenSK repository. The last push was Nov 19 2021, and the initial merge into Xous +// was finished on June 9 2022. Any patches to this code base will have to be manually +// applied. Please update the information here to reflect the latest patch status. + /* UI concept: From d56360b599179e8bb4da12e270b0e37f43e278ff Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 9 Jun 2022 00:25:04 +0800 Subject: [PATCH 06/14] reflect status of "vault" moving toward a default app status --- buildpush.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildpush.ps1 b/buildpush.ps1 index 4dd03e317..608cf74be 100644 --- a/buildpush.ps1 +++ b/buildpush.ps1 @@ -3,7 +3,7 @@ # set RUSTFLAGS=--remap-path-prefix=F:\largework\rust-win\code\xous-core\=build # $env:RUSTFLAGS="--remap-path-prefix=$(Get-Location)=build" -cargo xtask app-image ball repl # vault +cargo xtask app-image vault # cargo xtask ffi-test # cargo xtask minimal precursors/soc.svd From fd9fb1cbcc1b14bf5cc47a2f335b68ad580b25f5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 9 Jun 2022 00:28:48 +0800 Subject: [PATCH 07/14] fix hosted mode --- services/usb-device-xous/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/usb-device-xous/src/main.rs b/services/usb-device-xous/src/main.rs index 476dad75c..429d525ff 100644 --- a/services/usb-device-xous/src/main.rs +++ b/services/usb-device-xous/src/main.rs @@ -189,6 +189,7 @@ fn main() -> ! { let mut fido_rx_queue = VecDeque::<[u8; 64]>::new(); let mut lockstatus_force_update = true; // some state to track if we've been through a susupend/resume, to help out the status thread with its UX update after a restart-from-cold + #[cfg(any(target_os = "none", target_os = "xous"))] let mut was_suspend = true; loop { let mut msg = xous::receive_message(usbdev_sid).unwrap(); @@ -287,6 +288,7 @@ fn main() -> ! { Err(e) => log::trace!("U2F ERR: {:?}", e), } } + #[cfg(any(target_os = "none", target_os = "xous"))] if usb_dev.state() == UsbDeviceState::Suspend { log::info!("suspend detected"); if was_suspend == false { From 42996a817737c7ca2408235110a0c34e8d718974 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 9 Jun 2022 02:57:31 +0800 Subject: [PATCH 08/14] fix U2F flow accidentally broke it while debugging FIDO2. added notes about how the flow is implemented. --- apps/vault/src/ux.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/vault/src/ux.rs b/apps/vault/src/ux.rs index a218e97a9..344ad3d23 100644 --- a/apps/vault/src/ux.rs +++ b/apps/vault/src/ux.rs @@ -10,6 +10,16 @@ use std::io::{Write, Read}; use chrono::{Utc, DateTime, NaiveDateTime}; use std::time::{SystemTime, UNIX_EPOCH}; +// conceptual note: this UX conflates both the U2F and the FIDO2 paths. +// - U2F is a polled implementation, where the state goes from Idle->Prompt->Present +// and then immediately back to Idle. It is non-blocking and always returns a value. +// - FIDO is a deferred responder, meaning it is blocking the caller. It goes from +// Idle->Prompt->Idle again. The caller is blocked until the Prompt is answered. +// You *can* take the state machine to Present after Prompt, in which case, it will +// "remember" that a user was present for another 30 seconds. This is not actually +// the mandated behavior, it was only implemented because I got confused on part +// of the spec, but the code stubs are still around. + pub(crate) const U2F_APP_DICT: &'static str = "fido.u2fapps"; #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] @@ -413,13 +423,12 @@ pub(crate) fn start_ux_thread() { fido_request.granted = char::from_u32(key_hit); log::info!("returning {:?}", fido_request.granted); buffer.replace(fido_request).unwrap(); + ux_state = UxState::Idle; // comment out this line if you want presence to persist after touch for FIDO2 + } else { + // this is the U2F flow + ux_state = UxState::Present(tt.elapsed_ms() as i64 + timeouts.presence_timeout_ms); } - // this is used only if we want presence to persist after the touch - //ux_state = UxState::Present(tt.elapsed_ms() as i64 + timeouts.presence_timeout_ms); - // this will take us straight back to Idle after a touch - ux_state = UxState::Idle; - modals.dynamic_notification_close().unwrap(); send_message(self_cid, Message::new_scalar(UxOp::Pump.to_usize().unwrap(), KEEPALIVE_MS, 0, 0, 0) From f4f54181901c38d4eebbcfd2beabb4f99fbda63e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 9 Jun 2022 17:48:20 +0800 Subject: [PATCH 09/14] xous: envelope: handle MemoryReturned in forward() Previously, we only handled messages that were forwarded. With this patch, we also handle memory messages that were returned. This fixes #165 Signed-off-by: Sean Cross --- xous-rs/src/definitions/messages/envelope.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/xous-rs/src/definitions/messages/envelope.rs b/xous-rs/src/definitions/messages/envelope.rs index 07c75283b..70a7cc503 100644 --- a/xous-rs/src/definitions/messages/envelope.rs +++ b/xous-rs/src/definitions/messages/envelope.rs @@ -124,21 +124,19 @@ impl Envelope { )); } - _ => { + Message::Borrow(_) | Message::MutableBorrow(_) | Message::Scalar(_) => { let result = crate::send_message(connection, body); let new_self = ManuallyDrop::into_inner(manual_self); - - if let Ok(crate::Result::Ok) = result { + match result { // `new_self` will have its Drop() called - return Ok(()); - } + Ok(crate::Result::Ok) | Ok(crate::Result::MemoryReturned(_, _)) => Ok(()), - // If there's an error, reconstitute ourselves and return. - if let Err(e) = result { - return Err((new_self, e)); - } + // If there's an error, reconstitute ourselves and return. + Err(e) => Err((new_self, e)), - return Err((new_self, crate::Error::MemoryInUse)); + // Any other value is a bug in the kernel + o => panic!("unrecognized return from send_message: {:?}", o), + } } } } From ab18eb0776002edef1c5a736948037fdc8914b66 Mon Sep 17 00:00:00 2001 From: kelas Date: Thu, 9 Jun 2022 23:01:43 +0200 Subject: [PATCH 10/14] Fix realloc() --- services/ffi-test/src/bindings.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/ffi-test/src/bindings.rs b/services/ffi-test/src/bindings.rs index 7bfb0183a..689f69277 100644 --- a/services/ffi-test/src/bindings.rs +++ b/services/ffi-test/src/bindings.rs @@ -148,6 +148,10 @@ pub unsafe extern "C" fn realloc( ptr: *mut c_void, size: c_uint ) -> *mut c_void { + if ptr.is_null() { + // if ptr is null, realloc() is identical to malloc() + return malloc(size); + } let mut region_index: Option = None; for (index, region) in C_HEAP.iter().enumerate() { if region.as_ptr() as usize == ptr as usize { @@ -158,7 +162,7 @@ pub unsafe extern "C" fn realloc( match region_index { Some(index) => { log::info!("realloc/free success: {:x}", ptr as usize); - let mut old = C_HEAP.remove(index); + let mut old = C_HEAP.swap_remove(index); let checked_size = if size == 0 { 1 // at least 1 element so we have a pointer we can pass back } else { @@ -170,6 +174,7 @@ pub unsafe extern "C" fn realloc( alloc.push(src); } old.clear(); + alloc.set_len(checked_size as usize); C_HEAP.push(alloc); log::info!("realloc/allocated: {:x}", ret_ptr as usize); From aa436cbcc75c07e2ba3506c0246ce764efb590b5 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 10 Jun 2022 16:52:57 +0800 Subject: [PATCH 11/14] tools: elf: don't get `raw_data` for no_copy sections The `xmas-elf` crate doesn't deal with `raw_data()` of sections that are `NO_COPY`. These sections may point to data that is outside of the file. In order to avoid hitting this assert, don't call `raw_data()` on sections that are marked `NO_COPY`. Signed-off-by: Sean Cross --- tools/src/elf.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/src/elf.rs b/tools/src/elf.rs index d14a4d36f..0deb13178 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -315,11 +315,13 @@ pub fn read_minielf>(filename: P) -> Result>(filename: P) -> Result Date: Fri, 10 Jun 2022 18:02:52 +0800 Subject: [PATCH 12/14] reduce binary burden of backup SHA implementations When the hardware engine is unavailable, the implementation falls back to a software SHA. This code path is actually rarely used, but it is the top object code consumer according to our bloat analysis tool (40kiB for each instance of the compress function alone). This patch annotates the SHA functions with `#[inline(never)]` to reduce the size of these rarely-used functions. ...it might actually be that on our small-cache system, reducing the in-lining could improve performance as well because it helps the whole routine fit inside L2 cache. Anyways, it's a rarely-used path (I've never seen the code used once tbh) so let's reduce its overhead and keep Xous trim. --- services/engine-sha512/src/sha256/soft.rs | 7 ++++--- services/engine-sha512/src/sha512/soft.rs | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/services/engine-sha512/src/sha256/soft.rs b/services/engine-sha512/src/sha256/soft.rs index 34826a7e8..0ab1f2b17 100644 --- a/services/engine-sha512/src/sha256/soft.rs +++ b/services/engine-sha512/src/sha256/soft.rs @@ -39,7 +39,7 @@ fn sha256load(v2: [u32; 4], v3: [u32; 4]) -> [u32; 4] { fn sha256swap(v0: [u32; 4]) -> [u32; 4] { [v0[2], v0[3], v0[0], v0[1]] } - +#[inline(never)] // bunnie: reduce binary size fn sha256msg1(v0: [u32; 4], v1: [u32; 4]) -> [u32; 4] { // sigma 0 on vectors #[inline] @@ -52,7 +52,7 @@ fn sha256msg1(v0: [u32; 4], v1: [u32; 4]) -> [u32; 4] { add(v0, sigma0x4(sha256load(v0, v1))) } - +#[inline(never)] // bunnie: reduce binary size fn sha256msg2(v4: [u32; 4], v3: [u32; 4]) -> [u32; 4] { macro_rules! sigma1 { ($a:expr) => { @@ -70,7 +70,7 @@ fn sha256msg2(v4: [u32; 4], v3: [u32; 4]) -> [u32; 4] { [w19, w18, w17, w16] } - +#[inline(never)] // bunnie: reduce binary size fn sha256_digest_round_x2(cdgh: [u32; 4], abef: [u32; 4], wk: [u32; 4]) -> [u32; 4] { macro_rules! big_sigma0 { ($a:expr) => { @@ -162,6 +162,7 @@ macro_rules! schedule_rounds4 { } /// Process a block with the SHA-256 algorithm. +#[inline(never)] // bunnie: reduce binary size fn sha256_digest_block_u32(state: &mut [u32; 8], block: &[u32; 16]) { let mut abef = [state[0], state[1], state[4], state[5]]; let mut cdgh = [state[2], state[3], state[6], state[7]]; diff --git a/services/engine-sha512/src/sha512/soft.rs b/services/engine-sha512/src/sha512/soft.rs index 86105e7e8..7cead514f 100644 --- a/services/engine-sha512/src/sha512/soft.rs +++ b/services/engine-sha512/src/sha512/soft.rs @@ -17,6 +17,7 @@ fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] { } /// Performs 2 rounds of the SHA-512 message schedule update. +#[inline(never)] // bunnie: reduce binary size pub fn sha512_schedule_x2(v0: [u64; 2], v1: [u64; 2], v4to5: [u64; 2], v7: [u64; 2]) -> [u64; 2] { // sigma 0 fn sigma0(x: u64) -> u64 { @@ -46,6 +47,7 @@ pub fn sha512_schedule_x2(v0: [u64; 2], v1: [u64; 2], v4to5: [u64; 2], v7: [u64; } /// Performs one round of the SHA-512 message block digest. +#[inline(never)] // bunnie: reduce binary size pub fn sha512_digest_round( ae: [u64; 2], bf: [u64; 2], @@ -100,6 +102,7 @@ pub fn sha512_digest_round( } /// Process a block with the SHA-512 algorithm. +#[inline(never)] // bunnie: reduce binary size pub fn sha512_digest_block_u64(state: &mut [u64; 8], block: &[u64; 16]) { let k = &K64X2; From 10d543154f8ab6845a489bb14dd2503c620871aa Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 10 Jun 2022 18:10:49 +0800 Subject: [PATCH 13/14] improve gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 5f6dcc89a..dc5a7b4b3 100644 --- a/.gitignore +++ b/.gitignore @@ -18,11 +18,15 @@ apps/vault/tools/crypto_data # local utility shellscripts update-precursors.sh +buildpush.ps1 # pddb debug files tools/pddb-images/ tools/__pycache__ +# renode will tickle this every time you run it, but you shouldn't commit that back to the tree +emulation/linux-server-rootfs.jffs2 + # don't record weirdnesses dealing with vscode .vscode/settings-hw.json .vscode/settings-xous.json From 2200bcc5d5b1fa71b1d15e18c45251cbfa8e241b Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 10 Jun 2022 18:11:07 +0800 Subject: [PATCH 14/14] reflect latest ci build config --- buildpush.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildpush.ps1 b/buildpush.ps1 index 608cf74be..cb542d67a 100644 --- a/buildpush.ps1 +++ b/buildpush.ps1 @@ -3,7 +3,7 @@ # set RUSTFLAGS=--remap-path-prefix=F:\largework\rust-win\code\xous-core\=build # $env:RUSTFLAGS="--remap-path-prefix=$(Get-Location)=build" -cargo xtask app-image vault +cargo xtask app-image vault ball # cargo xtask ffi-test # cargo xtask minimal precursors/soc.svd