What changed, and why it matters
This commit rewrites a user-interface helper for entering text on the BitBox02 hardware wallet so it uses Rust's modern async/await style instead of older callback-based code. It is a structural cleanup: the same on-screen prompts, cancel behavior, and result handling are preserved, just expressed differently. There is no indication it fixes a security bug or introduces a vulnerability.
No security action required. Treat as a normal refactoring review; verify async cancellation and waker behavior in unit tests if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch ports trinary_input_string from a callback-driven creation API (trinary_input_string_create + closure-based confirm/cancel callbacks + RefCell<Option<...>> polling) to a native async fn that returns Result<Zeroizing<String>, ()>. It introduces a SharedState struct holding an optional waker and result, registers C callbacks that wake the async task, and uses core::future::poll_fn to suspend until confirm/cancel fires. Corresponding stub implementations are updated. No cryptographic, memory-safety, or trust-boundary semantics are changed.
Changed components
src/rust/bitbox02-rust/src/workflow/trinary_input_string.rssrc/rust/bitbox02/src/ui/ui.rssrc/rust/bitbox02/src/ui/ui_stub.rssrc/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rsInspect captured patch +96 / −102
diff --git a/src/rust/bitbox02-rust/src/workflow/trinary_input_string.rs b/src/rust/bitbox02-rust/src/workflow/trinary_input_string.rs
index 583e65b..80b1331 100644
--- a/src/rust/bitbox02-rust/src/workflow/trinary_input_string.rs
+++ b/src/rust/bitbox02-rust/src/workflow/trinary_input_string.rs
@@ -1,12 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
-pub use super::cancel::{Error, cancel, set_result};
-pub use bitbox02::ui::TrinaryInputStringParams as Params;
+pub use super::cancel::Error;
+pub use bitbox02::ui::{TrinaryInputStringParams as Params, trinary_input_string};
-use core::cell::RefCell;
-use util::bb02_async::option;
-
-use alloc::boxed::Box;
use alloc::string::String;
#[derive(Copy, Clone)]
@@ -24,20 +20,11 @@ pub async fn enter(
can_cancel: CanCancel,
preset: &str,
) -> Result<zeroize::Zeroizing<String>, Error> {
- let result = RefCell::new(None);
- let mut component = bitbox02::ui::trinary_input_string_create(
- params,
- |string| set_result(&result, string),
- match can_cancel {
- CanCancel::Yes => Some(Box::new(|| cancel(&result))),
- CanCancel::No => None,
- },
- );
- if !preset.is_empty() {
- bitbox02::ui::trinary_input_string_set_input(&mut component, preset);
- }
- component.screen_stack_push();
- option(&result)
+ let can_cancel = match can_cancel {
+ CanCancel::Yes => true,
+ CanCancel::No => false,
+ };
+ trinary_input_string(params, can_cancel, preset)
.await
- .or(Err(super::cancel::Error::Cancelled))
+ .or(Err(Error::Cancelled))
}
diff --git a/src/rust/bitbox02/src/ui/ui.rs b/src/rust/bitbox02/src/ui/ui.rs
index 02e9203..87f087f 100644
--- a/src/rust/bitbox02/src/ui/ui.rs
+++ b/src/rust/bitbox02/src/ui/ui.rs
@@ -51,66 +51,100 @@ impl Drop for Component<'_> {
}
}
-/// Creates a trinary input component.
-/// `result` - will be asynchronously set to `Some(<password>)` once the user confirms.
-pub fn trinary_input_string_create<'a, F>(
- params: &TrinaryInputStringParams,
- confirm_callback: F,
- cancel_callback: Option<ContinueCancelCb<'a>>,
-) -> Component<'a>
-where
- // Callback must outlive component.
- F: FnMut(zeroize::Zeroizing<String>) + 'a,
-{
- unsafe extern "C" fn c_confirm_callback<F2>(password: *const c_char, user_data: *mut c_void)
- where
- F2: FnMut(zeroize::Zeroizing<String>),
- {
+pub async fn trinary_input_string(
+ params: &TrinaryInputStringParams<'_>,
+ can_cancel: bool,
+ preset: &str,
+) -> Result<zeroize::Zeroizing<String>, ()> {
+ // Shared between the async context and the c callback
+ struct SharedState {
+ waker: Option<Waker>,
+ result: Option<Result<zeroize::Zeroizing<String>, ()>>,
+ }
+ let shared_state = Box::new(RefCell::new(SharedState {
+ waker: None,
+ result: None,
+ }));
+ let shared_state_ptr = shared_state.as_ref() as *const RefCell<SharedState> as *mut c_void;
+
+ unsafe extern "C" fn cancel_cb(user_data: *mut c_void) {
+ let shared_state = unsafe { &*(user_data as *mut RefCell<SharedState>) };
+ let mut shared_state = shared_state.borrow_mut();
+ if shared_state.result.is_none() {
+ shared_state.result = Some(Err(()));
+ if let Some(waker) = shared_state.waker.as_ref() {
+ waker.wake_by_ref();
+ }
+ }
+ }
+
+ unsafe extern "C" fn confirm_cb(password: *const c_char, user_data: *mut c_void) {
+ let shared_state = unsafe { &*(user_data as *mut RefCell<SharedState>) };
+ let mut shared_state = shared_state.borrow_mut();
let pw: zeroize::Zeroizing<String> = zeroize::Zeroizing::new(
unsafe { util::strings::str_from_null_terminated_ptr(password) }
.unwrap()
.into(),
);
- // The callback is dropped afterwards. This is safe because
- // this C callback is guaranteed to be called only once.
- let mut callback = unsafe { Box::from_raw(user_data as *mut F2) };
- callback(pw);
- }
-
- unsafe extern "C" fn c_cancel_callback(user_data: *mut c_void) {
- let callback = user_data as *mut ContinueCancelCb;
- unsafe { (*callback)() };
+ if shared_state.result.is_none() {
+ shared_state.result = Some(Ok(pw));
+ if let Some(waker) = shared_state.waker.as_ref() {
+ waker.wake_by_ref();
+ }
+ }
}
- let (cancel_cb, cancel_user_data) = match cancel_callback {
- None => (None, core::ptr::null_mut()),
- Some(cb) => (
- Some(c_cancel_callback as _),
- Box::into_raw(Box::new(cb)) as *mut c_void,
- ),
+ let (actual_cancel_cb, cancel_shared_state) = if can_cancel {
+ (
+ Some(cancel_cb as unsafe extern "C" fn(*mut c_void)),
+ shared_state_ptr,
+ )
+ } else {
+ (None, core::ptr::null_mut())
};
+
let mut title_scratch = Vec::new();
let component = unsafe {
bitbox02_sys::trinary_input_string_create(
¶ms.to_c_params(&mut title_scratch).data, // title copied in C
- Some(c_confirm_callback::<F>),
- // passed to c_confirm_callback as `user_data`.
- Box::into_raw(Box::new(confirm_callback)) as *mut _,
- cancel_cb,
- cancel_user_data,
+ Some(confirm_cb),
+ shared_state_ptr, // passed to confirm_cb as `user_data`.
+ actual_cancel_cb,
+ cancel_shared_state, // passed to cancel_cb as `user_data`.
)
};
- Component {
+ if !preset.is_empty() {
+ unsafe {
+ bitbox02_sys::trinary_input_string_set_input(
+ component,
+ util::strings::str_to_cstr_vec(preset).unwrap().as_ptr(),
+ )
+ }
+ }
+
+ let mut component = Component {
component,
is_pushed: false,
- on_drop: Some(Box::new(move || unsafe {
- // Drop all callbacks.
- if !cancel_user_data.is_null() {
- drop(Box::from_raw(cancel_user_data as *mut ContinueCancelCb));
- }
- })),
+ on_drop: None,
_p: PhantomData,
- }
+ };
+ component.screen_stack_push();
+
+ core::future::poll_fn({
+ let shared_state = &shared_state;
+ move |cx| {
+ let mut shared_state = shared_state.borrow_mut();
+
+ if let Some(result) = shared_state.result.take() {
+ Poll::Ready(result)
+ } else {
+ // Store the waker so the callback can wake up this task
+ shared_state.waker = Some(cx.waker().clone());
+ Poll::Pending
+ }
+ }
+ })
+ .await
}
/// Returns true if the user accepts, false if the user rejects.
@@ -443,15 +477,6 @@ pub fn confirm_transaction_fee_create<'a, 'b>(
}
}
-pub fn trinary_input_string_set_input(component: &mut Component, word: &str) {
- unsafe {
- bitbox02_sys::trinary_input_string_set_input(
- component.component,
- util::strings::str_to_cstr_vec(word).unwrap().as_ptr(),
- )
- }
-}
-
pub fn screen_stack_pop_all() {
unsafe {
bitbox02_sys::ui_screen_stack_pop_all();
diff --git a/src/rust/bitbox02/src/ui/ui_stub.rs b/src/rust/bitbox02/src/ui/ui_stub.rs
index 6bfa539..aeedd6b 100644
--- a/src/rust/bitbox02/src/ui/ui_stub.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub.rs
@@ -38,14 +38,11 @@ impl Drop for Component<'_> {
}
}
-pub fn trinary_input_string_create<'a, F>(
- _params: &TrinaryInputStringParams,
- _confirm_callback: F,
- _cancel_callback: Option<ContinueCancelCb<'a>>,
-) -> Component<'a>
-where
- F: FnMut(zeroize::Zeroizing<String>) + 'a,
-{
+pub async fn trinary_input_string(
+ _params: &TrinaryInputStringParams<'_>,
+ _can_cancel: bool,
+ _preset: &str,
+) -> Result<zeroize::Zeroizing<String>, ()> {
panic!("not used");
}
@@ -97,10 +94,6 @@ pub fn confirm_transaction_fee_create<'a, 'b>(
panic!("not used");
}
-pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
- panic!("not used")
-}
-
pub fn screen_stack_pop_all() {}
pub fn progress_create<'a>(_title: &str) -> Component<'a> {
diff --git a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
index ec15044..5e53147 100644
--- a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
@@ -35,24 +35,17 @@ impl<'a> Drop for Component<'a> {
}
}
-pub fn trinary_input_string_create<'a, F>(
- params: &TrinaryInputStringParams,
- mut confirm_callback: F,
- _cancel_callback: Option<ContinueCancelCb<'a>>,
-) -> Component<'a>
-where
- F: FnMut(zeroize::Zeroizing<String>) + 'a,
-{
+pub async fn trinary_input_string(
+ params: &TrinaryInputStringParams<'_>,
+ _can_cancel: bool,
+ _preset: &str,
+) -> Result<zeroize::Zeroizing<String>, ()> {
crate::print_stdout(&format!(
"ENTER SCREEN START\nTITLE: {}\nENTER SCREEN END\n",
params.title
));
- confirm_callback(zeroize::Zeroizing::new("".into()));
- Component {
- is_pushed: false,
- _p: PhantomData,
- }
+ Ok(zeroize::Zeroizing::new("".into()))
}
pub async fn confirm(params: &ConfirmParams<'_>) -> bool {
@@ -135,10 +128,6 @@ pub fn confirm_transaction_fee_create<'a, 'b>(
}
}
-pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
- panic!("not implemented")
-}
-
pub fn screen_stack_pop_all() {}
pub fn progress_create<'a>(_title: &str) -> Component<'a> {
Why this scored 12/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.