rust: move U2F C workflows to bitbox02_rust::workflow
What changed, and why it matters
This commit is a straightforward internal code move: it relocates a set of U2F/FIDO2 workflow helpers from one Rust crate (bitbox02-rust-c) to another (bitbox02-rust) to avoid a future circular dependency when the main event loop is rewritten in Rust. The actual functions and behavior are copied almost unchanged; only import paths and crate references are adjusted. There is no indication this fixes or introduces a security vulnerability.
No security action required. Treat as a normal refactoring commit. Code reviewers may separately note that the moved module continues to use unsafe static mut state and unchecked C string conversions, but those pre-existing patterns are not changed by this commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change deletes src/rust/bitbox02-rust-c/src/workflow.rs and creates src/rust/bitbox02-rust/src/workflow/u2f_c_api.rs with the same C-ABI exported functions (rust_workflow_spawn_unlock, rust_workflow_spawn_confirm, rust_workflow_spawn_orientation_screen, rust_workflow_spin, rust_workflow_*_poll, rust_workflow_abort_current). The module is added under the firmware feature flag in bitbox02-rust/src/workflow.rs. Callers in the graphical simulators are updated from bitbox02_rust_c::workflow::… to bitbox02_rust::workflow::u2f_c_api::…, and an explicit extern crate bitbox02_rust_c is added to keep the C-export library linked. The code still uses unsafe static mut state and CStr::from_ptr(…).to_str().unwrap(), identical to the original.
Changed components
src/rust/bitbox02-rust-c/src/lib.rssrc/rust/bitbox02-rust/src/workflow.rssrc/rust/bitbox02-rust/src/workflow/u2f_c_api.rstest/simulator-graphical-bb03/src/main.rstest/simulator-graphical/src/main.rsInspect captured patch +178 / −173
diff --git a/src/rust/bitbox02-rust-c/src/lib.rs b/src/rust/bitbox02-rust-c/src/lib.rs
index b629d77..a136ce1 100644
--- a/src/rust/bitbox02-rust-c/src/lib.rs
+++ b/src/rust/bitbox02-rust-c/src/lib.rs
@@ -15,8 +15,6 @@ pub mod async_usb;
mod der;
#[cfg(feature = "factory-setup")]
mod secp256k1;
-#[cfg(feature = "firmware")]
-pub mod workflow;
// Expose C interface defined in bitbox_aes
#[cfg(feature = "firmware")]
diff --git a/src/rust/bitbox02-rust-c/src/workflow.rs b/src/rust/bitbox02-rust-c/src/workflow.rs
deleted file mode 100644
index 929c392..0000000
--- a/src/rust/bitbox02-rust-c/src/workflow.rs
+++ /dev/null
@@ -1,169 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-
-//! This module is a quick workaround to use async workflows from U2F/FIDO2, where the root of the
-//! usb message proessing is not ported to Rust. If that happens, the `async_usb` module can be
-//! used and this can be deleted.
-
-// TODO: figure out how to deal with the static muts below.
-// https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
-#![allow(static_mut_refs)]
-#![allow(clippy::missing_safety_doc)]
-
-extern crate alloc;
-
-use alloc::boxed::Box;
-use alloc::string::String;
-use bitbox02_rust::workflow::{confirm, orientation_screen};
-use core::task::Poll;
-use util::bb02_async::{Task, spin};
-
-enum TaskState<'a, O> {
- Nothing,
- Running(Task<'a, O>),
- ResultAvailable(O),
-}
-
-static mut UNLOCK_STATE: TaskState<'static, Result<(), ()>> = TaskState::Nothing;
-
-static mut CONFIRM_TITLE: Option<String> = None;
-static mut CONFIRM_BODY: Option<String> = None;
-static mut CONFIRM_PARAMS: Option<confirm::Params> = None;
-static mut CONFIRM_STATE: TaskState<'static, Result<(), confirm::UserAbort>> = TaskState::Nothing;
-static mut BITBOX02_HAL: bitbox02_rust::hal::BitBox02Hal = bitbox02_rust::hal::BitBox02Hal::new();
-
-static mut ORIENTATION_SCREEN_STATE: TaskState<'static, bool> = TaskState::Nothing;
-
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_spawn_unlock() {
- unsafe {
- UNLOCK_STATE = TaskState::Running(Box::pin(bitbox02_rust::workflow::unlock::unlock(
- &mut BITBOX02_HAL,
- )));
- }
-}
-
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_spawn_confirm(
- title: *const core::ffi::c_char,
- body: *const core::ffi::c_char,
-) {
- unsafe {
- CONFIRM_TITLE = Some(core::ffi::CStr::from_ptr(title).to_str().unwrap().into());
- CONFIRM_BODY = Some(core::ffi::CStr::from_ptr(body).to_str().unwrap().into());
- CONFIRM_PARAMS = Some(confirm::Params {
- title: CONFIRM_TITLE.as_ref().unwrap(),
- body: CONFIRM_BODY.as_ref().unwrap(),
- accept_only: true,
- ..Default::default()
- });
-
- CONFIRM_STATE =
- TaskState::Running(Box::pin(confirm::confirm(CONFIRM_PARAMS.as_ref().unwrap())));
- }
-}
-
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_spawn_orientation_screen() {
- unsafe {
- ORIENTATION_SCREEN_STATE =
- TaskState::Running(Box::pin(orientation_screen::orientation_screen()));
- }
-}
-
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_spin() {
- unsafe {
- match UNLOCK_STATE {
- TaskState::Running(ref mut task) => {
- let result = spin(task);
- if let Poll::Ready(result) = result {
- UNLOCK_STATE = TaskState::ResultAvailable(result);
- }
- }
- _ => (),
- }
- match CONFIRM_STATE {
- TaskState::Running(ref mut task) => {
- let result = spin(task);
- if let Poll::Ready(result) = result {
- CONFIRM_STATE = TaskState::ResultAvailable(result);
- }
- }
- _ => (),
- }
- match ORIENTATION_SCREEN_STATE {
- TaskState::Running(ref mut task) => {
- let result = spin(task);
- if let Poll::Ready(result) = result {
- ORIENTATION_SCREEN_STATE = TaskState::ResultAvailable(result);
- }
- }
- _ => (),
- }
- }
-}
-
-/// Returns true if there was a result.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_unlock_poll(result_out: &mut bool) -> bool {
- unsafe {
- match UNLOCK_STATE {
- TaskState::ResultAvailable(result) => {
- UNLOCK_STATE = TaskState::Nothing;
- match result {
- Ok(()) => *result_out = true,
- Err(()) => *result_out = false,
- }
- true
- }
- _ => false,
- }
- }
-}
-
-/// Returns true if there was a result.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_confirm_poll(result_out: &mut bool) -> bool {
- unsafe {
- match CONFIRM_STATE {
- TaskState::ResultAvailable(ref result) => {
- CONFIRM_TITLE = None;
- CONFIRM_BODY = None;
- CONFIRM_PARAMS = None;
- CONFIRM_STATE = TaskState::Nothing;
- *result_out = result.is_ok();
- true
- }
- _ => false,
- }
- }
-}
-
-/// Returns true if there was a result.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_orientation_screen_poll(result_out: &mut bool) -> bool {
- unsafe {
- match ORIENTATION_SCREEN_STATE {
- TaskState::ResultAvailable(result) => {
- ORIENTATION_SCREEN_STATE = TaskState::Nothing;
- *result_out = result;
- true
- }
- _ => false,
- }
- }
-}
-
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_workflow_abort_current() {
- unsafe {
- UNLOCK_STATE = TaskState::Nothing;
-
- CONFIRM_TITLE = None;
- CONFIRM_BODY = None;
- CONFIRM_PARAMS = None;
- CONFIRM_STATE = TaskState::Nothing;
-
- ORIENTATION_SCREEN_STATE = TaskState::Nothing;
- }
-}
diff --git a/src/rust/bitbox02-rust/src/workflow.rs b/src/rust/bitbox02-rust/src/workflow.rs
index 3867f72..4bd9446 100644
--- a/src/rust/bitbox02-rust/src/workflow.rs
+++ b/src/rust/bitbox02-rust/src/workflow.rs
@@ -18,6 +18,8 @@ pub mod testing;
pub mod transaction;
pub mod trinary_choice;
pub mod trinary_input_string;
+#[cfg(feature = "firmware")]
+pub mod u2f_c_api;
pub mod unlock;
pub mod unlock_animation;
pub mod verify_message;
diff --git a/src/rust/bitbox02-rust/src/workflow/u2f_c_api.rs b/src/rust/bitbox02-rust/src/workflow/u2f_c_api.rs
new file mode 100644
index 0000000..286fbf9
--- /dev/null
+++ b/src/rust/bitbox02-rust/src/workflow/u2f_c_api.rs
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: Apache-2.0
+
+//! This module is a quick workaround to use async workflows from U2F/FIDO2, where the root of the
+//! usb message proessing is not ported to Rust. If that happens, the `async_usb` module can be
+//! used and this can be deleted.
+
+// TODO: figure out how to deal with the static muts below.
+// https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
+#![allow(static_mut_refs)]
+#![allow(clippy::missing_safety_doc)]
+
+extern crate alloc;
+
+use crate::workflow::{confirm, orientation_screen};
+use alloc::boxed::Box;
+use alloc::string::String;
+use core::task::Poll;
+use util::bb02_async::{Task, spin};
+
+enum TaskState<'a, O> {
+ Nothing,
+ Running(Task<'a, O>),
+ ResultAvailable(O),
+}
+
+static mut UNLOCK_STATE: TaskState<'static, Result<(), ()>> = TaskState::Nothing;
+
+static mut CONFIRM_TITLE: Option<String> = None;
+static mut CONFIRM_BODY: Option<String> = None;
+static mut CONFIRM_PARAMS: Option<confirm::Params> = None;
+static mut CONFIRM_STATE: TaskState<'static, Result<(), confirm::UserAbort>> = TaskState::Nothing;
+static mut BITBOX02_HAL: crate::hal::BitBox02Hal = crate::hal::BitBox02Hal::new();
+
+static mut ORIENTATION_SCREEN_STATE: TaskState<'static, bool> = TaskState::Nothing;
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_spawn_unlock() {
+ unsafe {
+ UNLOCK_STATE =
+ TaskState::Running(Box::pin(crate::workflow::unlock::unlock(&mut BITBOX02_HAL)));
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_spawn_confirm(
+ title: *const core::ffi::c_char,
+ body: *const core::ffi::c_char,
+) {
+ unsafe {
+ CONFIRM_TITLE = Some(core::ffi::CStr::from_ptr(title).to_str().unwrap().into());
+ CONFIRM_BODY = Some(core::ffi::CStr::from_ptr(body).to_str().unwrap().into());
+ CONFIRM_PARAMS = Some(confirm::Params {
+ title: CONFIRM_TITLE.as_ref().unwrap(),
+ body: CONFIRM_BODY.as_ref().unwrap(),
+ accept_only: true,
+ ..Default::default()
+ });
+
+ CONFIRM_STATE =
+ TaskState::Running(Box::pin(confirm::confirm(CONFIRM_PARAMS.as_ref().unwrap())));
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_spawn_orientation_screen() {
+ unsafe {
+ ORIENTATION_SCREEN_STATE =
+ TaskState::Running(Box::pin(orientation_screen::orientation_screen()));
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_spin() {
+ unsafe {
+ match UNLOCK_STATE {
+ TaskState::Running(ref mut task) => {
+ let result = spin(task);
+ if let Poll::Ready(result) = result {
+ UNLOCK_STATE = TaskState::ResultAvailable(result);
+ }
+ }
+ _ => (),
+ }
+ match CONFIRM_STATE {
+ TaskState::Running(ref mut task) => {
+ let result = spin(task);
+ if let Poll::Ready(result) = result {
+ CONFIRM_STATE = TaskState::ResultAvailable(result);
+ }
+ }
+ _ => (),
+ }
+ match ORIENTATION_SCREEN_STATE {
+ TaskState::Running(ref mut task) => {
+ let result = spin(task);
+ if let Poll::Ready(result) = result {
+ ORIENTATION_SCREEN_STATE = TaskState::ResultAvailable(result);
+ }
+ }
+ _ => (),
+ }
+ }
+}
+
+/// Returns true if there was a result.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_unlock_poll(result_out: &mut bool) -> bool {
+ unsafe {
+ match UNLOCK_STATE {
+ TaskState::ResultAvailable(result) => {
+ UNLOCK_STATE = TaskState::Nothing;
+ match result {
+ Ok(()) => *result_out = true,
+ Err(()) => *result_out = false,
+ }
+ true
+ }
+ _ => false,
+ }
+ }
+}
+
+/// Returns true if there was a result.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_confirm_poll(result_out: &mut bool) -> bool {
+ unsafe {
+ match CONFIRM_STATE {
+ TaskState::ResultAvailable(ref result) => {
+ CONFIRM_TITLE = None;
+ CONFIRM_BODY = None;
+ CONFIRM_PARAMS = None;
+ CONFIRM_STATE = TaskState::Nothing;
+ *result_out = result.is_ok();
+ true
+ }
+ _ => false,
+ }
+ }
+}
+
+/// Returns true if there was a result.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_orientation_screen_poll(result_out: &mut bool) -> bool {
+ unsafe {
+ match ORIENTATION_SCREEN_STATE {
+ TaskState::ResultAvailable(result) => {
+ ORIENTATION_SCREEN_STATE = TaskState::Nothing;
+ *result_out = result;
+ true
+ }
+ _ => false,
+ }
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_workflow_abort_current() {
+ unsafe {
+ UNLOCK_STATE = TaskState::Nothing;
+
+ CONFIRM_TITLE = None;
+ CONFIRM_BODY = None;
+ CONFIRM_PARAMS = None;
+ CONFIRM_STATE = TaskState::Nothing;
+
+ ORIENTATION_SCREEN_STATE = TaskState::Nothing;
+ }
+}
diff --git a/test/simulator-graphical-bb03/src/main.rs b/test/simulator-graphical-bb03/src/main.rs
index 78d160d..dba5285 100644
--- a/test/simulator-graphical-bb03/src/main.rs
+++ b/test/simulator-graphical-bb03/src/main.rs
@@ -40,6 +40,9 @@ use tracing_subscriber::{EnvFilter, filter::LevelFilter, fmt, prelude::*};
use bitbox02::ui::ugui::UG_COLOR;
use bitbox02_rust::hal::{Hal, Memory};
+// Explicitly link library for its C exports
+extern crate bitbox02_rust_c;
+
static BG: &[u8] = include_bytes!("../bg.png");
const MARGIN: usize = 20;
@@ -521,7 +524,7 @@ impl ApplicationHandler<UserEvent> for App {
}
}
// Business logic
- unsafe { bitbox02_rust_c::workflow::rust_workflow_spin() }
+ unsafe { bitbox02_rust::workflow::u2f_c_api::rust_workflow_spin() }
bitbox02_rust::async_usb::spin();
bitbox02::usb_processing::process_hww();
bitbox02::screen::process();
diff --git a/test/simulator-graphical/src/main.rs b/test/simulator-graphical/src/main.rs
index 99bc65e..b5ddb19 100644
--- a/test/simulator-graphical/src/main.rs
+++ b/test/simulator-graphical/src/main.rs
@@ -41,6 +41,9 @@ use tracing_subscriber::{EnvFilter, filter::LevelFilter, fmt, prelude::*};
use bitbox02::ui::ugui::UG_COLOR;
use bitbox02_rust::hal::{Hal, Memory};
+// Explicitly link library for its C exports
+extern crate bitbox02_rust_c;
+
static BG: &[u8; 325362] = include_bytes!("../bg.png");
const MARGIN: usize = 20;
@@ -652,7 +655,7 @@ impl ApplicationHandler<UserEvent> for App {
}
}
// Business logic
- unsafe { bitbox02_rust_c::workflow::rust_workflow_spin() }
+ unsafe { bitbox02_rust::workflow::u2f_c_api::rust_workflow_spin() }
bitbox02_rust::async_usb::spin();
bitbox02::usb_processing::process_hww();
bitbox02::screen::process();
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.