securechip: add safe wrapper of securechip_kdf in Rust
What changed, and why it matters
This commit adds a new Rust wrapper function that lets other parts of the BitBox02 firmware ask the secure chip to perform a key-derivation operation (KDF). The wrapper is a defensive improvement: it checks error codes, returns a properly typed Rust error, and uses a zeroizing buffer so the derived 32-byte secret is cleared from memory when no longer needed. There is no direct evidence in the commit that this fixes an active security bug; it reads more like a safe API addition for future use.
Review that the SECURECHIP_ERRORS list exactly matches the C enum values and ordering, and verify that all future Rust callers of kdf() handle the Result rather than unwrap(). Consider whether the unsafe block needs additional preconditions documented (e.g., msg length limits).
Security signals we found
New safe Rust wrapper around an unsafe C FFI KDF call
Use of Zeroizing<Vec<u8>> to clear derived key material from memory after use
Error-status mapping from C enum to typed Rust Error
Bidirectional 'keep in sync' comments between C header and Rust module
No removal of unsafe code paths or change to existing callers
Evidence from the diff
The change exposes securechip_kdf() to Rust via bindgen, then wraps it in src/rust/bitbox02/src/securechip.rs as pub fn kdf(msg: &[u8]) -> Result
Changed components
src/rust/bitbox02/src/securechip.rssrc/rust/bitbox02-sys/build.rssrc/securechip/securechip.hInspect captured patch +85 / −0
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index 9b52cd7..22f1879 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -50,6 +50,7 @@ const ALLOWLIST_TYPES: &[&str] = &[
"component_t",
"confirm_params_t",
"trinary_input_string_params_t",
+ "securechip_error_t",
];
const ALLOWLIST_FNS: &[&str] = &[
@@ -142,6 +143,7 @@ const ALLOWLIST_FNS: &[&str] = &[
"sdcard_create",
"secp256k1_ecdsa_anti_exfil_host_commit",
"securechip_attestation_sign",
+ "securechip_kdf",
"securechip_model",
"securechip_monotonic_increments_remaining",
"securechip_u2f_counter_set",
@@ -167,6 +169,7 @@ const RUSTIFIED_ENUMS: &[&str] = &[
"memory_result_t",
"multisig_script_type_t",
"output_type_t",
+ "securechip_error_t",
"securechip_model_t",
"simple_type_t",
"trinary_choice_t",
diff --git a/src/rust/bitbox02/src/securechip.rs b/src/rust/bitbox02/src/securechip.rs
index 5c50ddc..793aca7 100644
--- a/src/rust/bitbox02/src/securechip.rs
+++ b/src/rust/bitbox02/src/securechip.rs
@@ -12,8 +12,62 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+extern crate alloc;
+
+use alloc::vec::Vec;
+use zeroize::Zeroizing;
+
+pub use bitbox02_sys::securechip_error_t as SecureChipError;
pub use bitbox02_sys::securechip_model_t as Model;
+#[derive(Debug, PartialEq, Eq)]
+pub enum Error {
+ SecureChip(SecureChipError),
+ Status(i32),
+}
+
+// Keep in sync with securechip.h's securechip_error_t.
+const SECURECHIP_ERRORS: [SecureChipError; 15] = [
+ // Errors common to any securechip implementation
+ SecureChipError::SC_ERR_IFS,
+ SecureChipError::SC_ERR_INVALID_ARGS,
+ SecureChipError::SC_ERR_CONFIG_MISMATCH,
+ SecureChipError::SC_ERR_SALT,
+ SecureChipError::SC_ERR_INCORRECT_PASSWORD,
+ // Errors specific to the ATECC
+ SecureChipError::SC_ATECC_ERR_ZONE_UNLOCKED_CONFIG,
+ SecureChipError::SC_ATECC_ERR_ZONE_UNLOCKED_DATA,
+ SecureChipError::SC_ATECC_ERR_SLOT_UNLOCKED_IO,
+ SecureChipError::SC_ATECC_ERR_SLOT_UNLOCKED_AUTH,
+ SecureChipError::SC_ATECC_ERR_SLOT_UNLOCKED_ENC,
+ SecureChipError::SC_ATECC_ERR_RESET_KEYS,
+ // Errors specific to the Optiga
+ SecureChipError::SC_OPTIGA_ERR_CREATE,
+ SecureChipError::SC_OPTIGA_ERR_UNEXPECTED_METADATA,
+ SecureChipError::SC_OPTIGA_ERR_PAL,
+ SecureChipError::SC_OPTIGA_ERR_UNEXPECTED_LEN,
+];
+
+fn securechip_error_from_status(status: i32) -> Option<SecureChipError> {
+ SECURECHIP_ERRORS
+ .iter()
+ .copied()
+ .find(|err| *err as i32 == status)
+}
+
+impl Error {
+ fn from_status(status: i32) -> Self {
+ if status < 0 {
+ match securechip_error_from_status(status) {
+ Some(err) => Error::SecureChip(err),
+ None => Error::Status(status),
+ }
+ } else {
+ Error::Status(status)
+ }
+ }
+}
+
pub fn attestation_sign(challenge: &[u8; 32], signature: &mut [u8; 64]) -> Result<(), ()> {
match unsafe {
bitbox02_sys::securechip_attestation_sign(challenge.as_ptr(), signature.as_mut_ptr())
@@ -31,6 +85,18 @@ pub fn monotonic_increments_remaining() -> Result<u32, ()> {
}
}
+/// Perform the secure chip KDF with the message in `msg` and return the zeroizing 32-byte result.
+pub fn kdf(msg: &[u8]) -> Result<Zeroizing<Vec<u8>>, Error> {
+ let mut result = Zeroizing::new(vec![0u8; 32]);
+ let status =
+ unsafe { bitbox02_sys::securechip_kdf(msg.as_ptr(), msg.len(), result.as_mut_ptr()) };
+ if status == 0 {
+ Ok(result)
+ } else {
+ Err(Error::from_status(status))
+ }
+}
+
#[cfg(feature = "app-u2f")]
#[cfg(not(feature = "testing"))]
pub fn u2f_counter_set(counter: u32) -> Result<(), ()> {
@@ -63,3 +129,18 @@ pub fn model() -> Result<Model, ()> {
false => Err(()),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ use hex_lit::hex;
+
+ #[test]
+ fn test_kdf() {
+ // Matches the deterministic HMAC result returned by test/hardware-fakes/src/fake_securechip.c.
+ let result = kdf(b"stub input").unwrap();
+ let expected = hex!("3d7caa0407f18f6b15a6202843c883f326d614996df67940af210d91aff5b9c8");
+ assert_eq!(result.as_slice(), expected.as_slice());
+ }
+}
diff --git a/src/securechip/securechip.h b/src/securechip/securechip.h
index ee5be60..51d84cc 100644
--- a/src/securechip/securechip.h
+++ b/src/securechip/securechip.h
@@ -22,6 +22,7 @@
#include <stddef.h>
#include <stdint.h>
+// Keep in sync with securechip.rs `SECURECHIP_ERRORS`.
typedef enum {
// Errors common to any securechip implementation
SC_ERR_IFS = -1,
Why this scored 28/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.