refactor(core): add Rust bindings for noise_xxpsk3
What changed, and why it matters
This commit adds Rust programming-language wrappers around an existing C library for a secure communication protocol called Noise XXpsk3. It does not change the underlying cryptographic logic or fix any bug; it is a code organization (refactoring) change that lets other parts of the Trezor firmware call the Noise XXpsk3 routines from Rust. There is no direct security problem visible in the change itself.
No immediate action is required. If this binding is intended for a security-sensitive feature (e.g., NFC pairing), reviewers should later verify that the underlying C implementation of `noise_xxpsk3` is correct and that the Rust wrappers uphold all preconditions (buffer sizes, key lifetimes, state zeroization).
Security signals we found
New FFI surface for Noise XXpsk3 handshake and transport functions
Bindings rely on existing C implementation; no C code changes shown
Uses project's secret-memory abstractions (SecretContext, HazardGuard, ZeroableMemory)
No changelog entry and commit is marked as refactor
Evidence from the diff
The commit introduces a new Rust module noise_xxpsk3.rs with safe bindings to the pre-existing C functions in noise_xxpsk3.c/noise_xxpsk3.h. It enables the noise_xxpsk3 Cargo feature in the crypto crate and wires it into the k1n1 and nfc firmware feature flags. The bindings use the project’s existing SecretContext/HazardGuard memory-clearing helpers and pass slices through CSlice/CSliceMut helpers. No algorithmic changes or bug fixes are present in the diff.
Changed components
core/embed/cryptocore/embed/projects/firmwarecore/embed/projects/prodtestcore/embed/rustInspect captured patch +240 / −3
### core/embed/crypto/Cargo.toml
@@ -36,6 +36,7 @@ aes_gcm = []
ed25519_no_precomp = []
mldsa = []
noise_kk1 = ["aes_gcm"]
+noise_xxpsk3 = ["aes_gcm"]
secp256k1_zkp = []
sphincsplus = []
thp = ["dep:trezor-thp"]
@@ -48,6 +49,7 @@ test = [
"models/mcu_stm32u5g",
"models/model_t3w1",
"noise_kk1",
+ "noise_xxpsk3",
"rtl/error_shims",
"secp256k1_zkp",
"sphincsplus",
### core/embed/crypto/build.rs
@@ -25,6 +25,10 @@ fn main() -> Result<()> {
add_noise_kk1(lib, &attrs)?;
}
+ if cfg!(feature = "noise_xxpsk3") {
+ add_noise_xxpsk3(lib, &attrs)?;
+ }
+
if cfg!(feature = "secp256k1_zkp") {
add_secp256k1_zkp(lib, &attrs)?;
}
@@ -255,6 +259,31 @@ fn add_noise_kk1(lib: &mut CLibrary, attrs: &CompileAttrs) -> Result<()> {
Ok(())
}
+fn add_noise_xxpsk3(lib: &mut CLibrary, attrs: &CompileAttrs) -> Result<()> {
+ lib.add_sources_in_dir_with_attrs(CRYPTO_PATH, ["noise_xxpsk3.c"], Some(attrs.clone()));
+
+ lib.add_rust_bindings(|builder| {
+ Ok(builder
+ .header(format!("{CRYPTO_PATH}/noise_xxpsk3.h"))
+ .allowlist_var("NOISE_XXPSK3_DHLEN")
+ .allowlist_var("NOISE_XXPSK3_TAG_SIZE")
+ .allowlist_type("noise_xxpsk3_initiator_t")
+ .no_copy("noise_xxpsk3_cipher_state_t")
+ .no_copy("noise_xxpsk3_symmetric_state_t")
+ .no_copy("noise_xxpsk3_transport_state_t")
+ .no_copy("noise_xxpsk3_handshake_state_t")
+ .no_copy("noise_xxpsk3_initiator_t")
+ .allowlist_function("noise_xxpsk3_initiator_init")
+ .allowlist_function("noise_xxpsk3_initiator_create_request1")
+ .allowlist_function("noise_xxpsk3_initiator_handle_response1")
+ .allowlist_function("noise_xxpsk3_initiator_create_request2")
+ .allowlist_function("noise_xxpsk3_send_message")
+ .allowlist_function("noise_xxpsk3_receive_message"))
+ })?;
+
+ Ok(())
+}
+
fn add_secp256k1_zkp(lib: &mut CLibrary, _attrs: &CompileAttrs) -> Result<()> {
// Deliberately not inheriting `attrs`: they include
// `-ftrivial-auto-var-init=zero`, which we'd like to apply to the whole
### core/embed/crypto/src/ffi.rs
@@ -1,4 +1,5 @@
#![allow(non_camel_case_types)]
+#![allow(non_upper_case_globals)]
#![allow(dead_code)]
#![allow(non_snake_case)]
### core/embed/crypto/src/lib.rs
@@ -16,6 +16,8 @@ pub mod ed25519;
mod ffi;
pub mod hmac;
pub mod merkle;
+#[cfg(feature = "noise_xxpsk3")]
+pub mod noise_xxpsk3;
pub mod secret;
pub mod sha256;
pub mod sha3;
### core/embed/crypto/src/noise_xxpsk3.rs
@@ -0,0 +1,203 @@
+use core::ops::DerefMut;
+
+use rtl::{CSlice, CSliceMut};
+
+use super::ffi;
+use super::secret::{HazardGuard, SecretContext, SecretContextLock, ZeroableMemory};
+
+pub const DHLEN: usize = ffi::NOISE_XXPSK3_DHLEN as usize;
+pub const TAG_SIZE: usize = ffi::NOISE_XXPSK3_TAG_SIZE as usize;
+
+pub type NoiseXXpsk3Ctx = SecretContext<ffi::noise_xxpsk3_initiator_t>;
+
+// SAFETY: noise_xxpsk3_initiator_t represents a valid state when zeroed
+unsafe impl ZeroableMemory for ffi::noise_xxpsk3_initiator_t {}
+
+impl HazardGuard<'_, ffi::noise_xxpsk3_initiator_t> {
+ fn init(&mut self) -> Result<(), ()> {
+ // SAFETY: ffi
+ let res = unsafe { ffi::noise_xxpsk3_initiator_init(self.hazard_mut()) };
+ if res { Ok(()) } else { Err(()) }
+ }
+
+ fn create_request1(&mut self, payload: &[u8], request: &mut [u8]) -> Result<usize, ()> {
+ let payload = CSlice::from(payload);
+ let request = CSliceMut::from(request);
+ let mut request_size = 0usize;
+ // SAFETY: ffi
+ let res = unsafe {
+ ffi::noise_xxpsk3_initiator_create_request1(
+ self.hazard_mut(),
+ payload.ptr(),
+ payload.len(),
+ request.ptr(),
+ request.len(),
+ &mut request_size as *mut _,
+ )
+ };
+ if res {
+ assert!(request_size > 0 && request_size <= request.len());
+ Ok(request_size)
+ } else {
+ Err(())
+ }
+ }
+
+ fn handle_response1(
+ &mut self,
+ response: &[u8],
+ remote_static_public_key: &mut [u8; DHLEN],
+ payload: &mut [u8],
+ ) -> Result<usize, ()> {
+ let response = CSlice::from(response);
+ let payload = CSliceMut::from(payload);
+ let mut payload_size = 0usize;
+ // SAFETY: ffi
+ let res = unsafe {
+ ffi::noise_xxpsk3_initiator_handle_response1(
+ self.hazard_mut(),
+ response.ptr(),
+ response.len(),
+ remote_static_public_key.as_mut_ptr(),
+ payload.ptr(),
+ payload.len(),
+ &mut payload_size as *mut _,
+ )
+ };
+ if res {
+ assert!(payload_size <= payload.len());
+ Ok(payload_size)
+ } else {
+ Err(())
+ }
+ }
+
+ fn create_request2(
+ &mut self,
+ static_private_key: &[u8; DHLEN],
+ static_public_key: &[u8; DHLEN],
+ psk: &[u8; DHLEN],
+ payload: &[u8],
+ request: &mut [u8],
+ ) -> Result<usize, ()> {
+ let payload = CSlice::from(payload);
+ let request = CSliceMut::from(request);
+ let mut request_size = 0usize;
+ // SAFETY: ffi
+ let res = unsafe {
+ ffi::noise_xxpsk3_initiator_create_request2(
+ self.hazard_mut(),
+ static_private_key.as_ptr(),
+ static_public_key.as_ptr(),
+ psk.as_ptr(),
+ payload.ptr(),
+ payload.len(),
+ request.ptr(),
+ request.len(),
+ &mut request_size as *mut _,
+ )
+ };
+ if res {
+ assert!(request_size > 0 && request_size <= request.len());
+ Ok(request_size)
+ } else {
+ Err(())
+ }
+ }
+
+ fn send_message(&mut self, payload: &[u8], ciphertext: &mut [u8]) -> Result<usize, ()> {
+ let payload = CSlice::from(payload);
+ let ciphertext = CSliceMut::from(ciphertext);
+ let mut ciphertext_size = 0usize;
+ // SAFETY: ffi
+ let res = unsafe {
+ ffi::noise_xxpsk3_send_message(
+ &mut self.hazard_mut().transport_state,
+ payload.ptr(),
+ payload.len(),
+ ciphertext.ptr(),
+ ciphertext.len(),
+ &mut ciphertext_size as *mut _,
+ )
+ };
+ if res {
+ assert!(ciphertext_size >= payload.len() && ciphertext_size <= ciphertext.len());
+ Ok(ciphertext_size)
+ } else {
+ Err(())
+ }
+ }
+
+ fn receive_message(&mut self, ciphertext: &[u8], payload: &mut [u8]) -> Result<usize, ()> {
+ let ciphertext = CSlice::from(ciphertext);
+ let payload = CSliceMut::from(payload);
+ let mut payload_size = 0usize;
+ // SAFETY: ffi
+ let res = unsafe {
+ ffi::noise_xxpsk3_receive_message(
+ &mut self.hazard_mut().transport_state,
+ ciphertext.ptr(),
+ ciphertext.len(),
+ payload.ptr(),
+ payload.len(),
+ &mut payload_size as *mut _,
+ )
+ };
+ if res {
+ assert!(payload_size <= ciphertext.len() && payload_size <= payload.len());
+ Ok(payload_size)
+ } else {
+ Err(())
+ }
+ }
+}
+
+pub struct NoiseXXpsk3<D: DerefMut<Target = NoiseXXpsk3Ctx>>(SecretContextLock<D>);
+
+impl<D: DerefMut<Target = NoiseXXpsk3Ctx>> NoiseXXpsk3<D> {
+ pub fn new(ctx: D) -> Result<Self, ()> {
+ let mut locked_ctx = SecretContextLock::new(ctx);
+ locked_ctx.guarded().init()?;
+ Ok(Self(locked_ctx))
+ }
+
+ pub fn create_request1(&mut self, payload: &[u8], request: &mut [u8]) -> Result<usize, ()> {
+ self.0.guarded().create_request1(payload, request)
+ }
+
+ pub fn handle_response1(
+ &mut self,
+ response: &[u8],
+ remote_static_public_key: &mut [u8; DHLEN],
+ payload: &mut [u8],
+ ) -> Result<usize, ()> {
+ self.0
+ .guarded()
+ .handle_response1(response, remote_static_public_key, payload)
+ }
+
+ pub fn create_request2(
+ &mut self,
+ static_private_key: &[u8; DHLEN],
+ static_public_key: &[u8; DHLEN],
+ psk: &[u8; DHLEN],
+ payload: &[u8],
+ request: &mut [u8],
+ ) -> Result<usize, ()> {
+ self.0.guarded().create_request2(
+ static_private_key,
+ static_public_key,
+ psk,
+ payload,
+ request,
+ )
+ }
+
+ pub fn send_message(&mut self, payload: &[u8], ciphertext: &mut [u8]) -> Result<usize, ()> {
+ self.0.guarded().send_message(payload, ciphertext)
+ }
+
+ pub fn receive_message(&mut self, ciphertext: &[u8], payload: &mut [u8]) -> Result<usize, ()> {
+ self.0.guarded().receive_message(ciphertext, payload)
+ }
+}
### core/embed/projects/firmware/Cargo.toml
@@ -41,7 +41,7 @@ log_stack_usage = ["upymod/log_stack_usage"]
memperf = ["upymod/memperf"]
micropy_enable_source_lines = ["upymod/enable_source_lines"]
miniscript = ["trezor_lib/miniscript", "upymod/miniscript", "tracked_allocator", "ward"]
-k1n1 = ["upymod/k1n1"]
+k1n1 = ["trezor_lib/k1n1", "upymod/k1n1"]
optiga_testing = ["sec/optiga_testing"]
production = ["io/production"]
pyopt = ["upymod/pyopt"]
### core/embed/projects/prodtest/Cargo.toml
@@ -79,7 +79,7 @@ hw_revision = ["sec/hw_revision"]
iwdg = ["sec/iwdg"]
lockable_bootloader = ["io/lockable_bootloader"]
mcu_attestation = ["sec/mcu_attestation"]
-nfc = ["io/nfc"]
+nfc = ["crypto/noise_xxpsk3", "io/nfc"]
nrf = ["io/nrf"]
nrf_auth = ["io/nrf_auth"]
optiga = [
### core/embed/rust/Cargo.toml
@@ -78,7 +78,7 @@ layout_delizia = []
layout_eckhart = []
micropython = ["dep:upymod"]
miniscript = []
-k1n1 = []
+k1n1 = ["crypto/noise_xxpsk3"]
nrf = []
optiga = []
pmic = []Why this scored 11/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.