What changed, and why it matters
This is a small internal code cleanup in the Trezor hardware wallet's Rust transport-handshake-pairing (THP) library. It moves a helper method (channel_id) into a shared trait, adds a read-only packet_len accessor, turns off noisy test logging by default, derives Debug for some enums in test builds, and makes the control_byte module public. None of these changes fix a vulnerability or change security behavior.
No security action required; treat as routine refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors the trezor-thp channel layer. channel_id() is relocated from concrete Channel/ChannelOpen types into the ChannelIO trait, with implementations added for Buffered, device/host ChannelOpen wrappers, and the test WithKey wrapper. A packet_len() getter is added to Buffered. #[derive(Debug)] is gated behind test/debug_assertions for PairingState and Phase. Test logging defaults from ‘info’ to ‘off’. The control_byte module visibility changes from mod to pub mod. No cryptographic, authentication, or parsing logic is altered.
Changed components
rust/trezor-thp/src/channel/buffered.rsrust/trezor-thp/src/channel/device.rsrust/trezor-thp/src/channel/host.rsrust/trezor-thp/src/channel/mod.rsrust/trezor-thp/src/channel/test.rsrust/trezor-thp/src/lib.rsInspect captured patch +36 / −14
diff --git a/rust/trezor-thp/src/channel/buffered.rs b/rust/trezor-thp/src/channel/buffered.rs
index 030cfba7..9ed591b3 100644
--- a/rust/trezor-thp/src/channel/buffered.rs
+++ b/rust/trezor-thp/src/channel/buffered.rs
@@ -29,6 +29,10 @@ impl<C: ChannelIO> Buffered<C> {
}
}
+ pub fn packet_len(&self) -> usize {
+ self.packet_len
+ }
+
pub fn set_packet_len(&mut self, packet_len: usize) {
self.packet_len = packet_len;
}
diff --git a/rust/trezor-thp/src/channel/device.rs b/rust/trezor-thp/src/channel/device.rs
index fb2a2b58..44ab9949 100644
--- a/rust/trezor-thp/src/channel/device.rs
+++ b/rust/trezor-thp/src/channel/device.rs
@@ -270,6 +270,10 @@ where
fn message_retransmit(&mut self) -> Result<(), Error> {
Ok(())
}
+
+ fn channel_id(&self) -> u16 {
+ BROADCAST_CHANNEL_ID
+ }
}
#[derive(Copy, Clone)]
@@ -451,10 +455,6 @@ impl<C: CredentialVerifier, B: Backend> ChannelOpen<C, B> {
})
}
- pub fn channel_id(&self) -> u16 {
- self.channel.channel_id
- }
-
pub fn sending_retry(&self) -> Option<u8> {
self.channel.sending_retry()
}
@@ -555,6 +555,10 @@ where
fn message_retransmit(&mut self) -> Result<(), Error> {
self.channel.message_retransmit()
}
+
+ fn channel_id(&self) -> u16 {
+ self.channel.channel_id()
+ }
}
/// Helper for assigning consecutive channel IDs.
diff --git a/rust/trezor-thp/src/channel/host.rs b/rust/trezor-thp/src/channel/host.rs
index 4ab7312f..a1280ecb 100644
--- a/rust/trezor-thp/src/channel/host.rs
+++ b/rust/trezor-thp/src/channel/host.rs
@@ -315,6 +315,10 @@ where
fn message_retransmit(&mut self) -> Result<(), Error> {
Ok(())
}
+
+ fn channel_id(&self) -> u16 {
+ BROADCAST_CHANNEL_ID
+ }
}
#[derive(Copy, Clone)]
@@ -491,10 +495,6 @@ impl<C: CredentialStore, B: Backend> ChannelOpen<C, B> {
})
}
- pub fn channel_id(&self) -> u16 {
- self.channel.channel_id
- }
-
pub fn sending_retry(&self) -> Option<u8> {
self.channel.sending_retry()
}
@@ -581,4 +581,8 @@ where
fn message_retransmit(&mut self) -> Result<(), Error> {
self.channel.message_retransmit()
}
+
+ fn channel_id(&self) -> u16 {
+ self.channel.channel_id()
+ }
}
diff --git a/rust/trezor-thp/src/channel/mod.rs b/rust/trezor-thp/src/channel/mod.rs
index b7348d91..f0c93325 100644
--- a/rust/trezor-thp/src/channel/mod.rs
+++ b/rust/trezor-thp/src/channel/mod.rs
@@ -70,6 +70,7 @@ impl Nonce {
}
/// Sent by device after successful handshake to indicate whether pairing is required.
+#[cfg_attr(any(test, debug_assertions), derive(Debug))]
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum PairingState {
@@ -157,6 +158,7 @@ enum ReceiveState<R: Role> {
/// encrypted transport phase can also use protobuf messages, their meaning is generally
/// different than in the other phases.
/// Application can use this enum to distinguish the context.
+#[cfg_attr(any(test, debug_assertions), derive(Debug))]
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Phase {
@@ -207,10 +209,6 @@ impl<R: Role, B: Backend> Channel<R, B> {
self.noise.as_mut().ok_or_else(Error::unexpected_input)
}
- pub fn channel_id(&self) -> u16 {
- self.channel_id
- }
-
pub fn handshake_hash(&self) -> &[u8; HANDSHAKE_HASH_LEN] {
self.noise.as_ref().unwrap().handshake_hash()
}
@@ -762,6 +760,9 @@ pub trait ChannelIO {
send_buffer[3..plaintext_len].copy_from_slice(message);
self.message_in(plaintext_len, send_buffer)
}
+
+ /// Get channel identifier, or `BROADCAST_CHANNEL_ID` for muxes.
+ fn channel_id(&self) -> u16;
}
impl<R: Role, B: Backend> ChannelIO for Channel<R, B> {
@@ -900,6 +901,10 @@ impl<R: Role, B: Backend> ChannelIO for Channel<R, B> {
*retry = retry.saturating_add(1);
Ok(())
}
+
+ fn channel_id(&self) -> u16 {
+ self.channel_id
+ }
}
/// The maximum number of transport payload retransmissions that the sender should attempt.
diff --git a/rust/trezor-thp/src/channel/test.rs b/rust/trezor-thp/src/channel/test.rs
index 496fd10c..8165e367 100644
--- a/rust/trezor-thp/src/channel/test.rs
+++ b/rust/trezor-thp/src/channel/test.rs
@@ -47,9 +47,10 @@ const DEVICE_KEY: &[u8; PRIVKEY_LEN] = &[0u8; PRIVKEY_LEN];
const DEVICE_PROPERTIES: &[u8] =
b"\x0a\x04\x54\x32\x57\x31\x10\x00\x18\x02\x20\x00\x28\x02\x28\x01";
+// Set environment variable RUST_LOG=<level> to enable logging, e.g. RUST_LOG=trace.
fn setup() {
SETUP.call_once(|| {
- env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "info"));
+ env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "off"));
})
}
@@ -120,6 +121,10 @@ impl<C: CredentialVerifier, B: Backend> ChannelIO for WithKey<C, B> {
fn message_retransmit(&mut self) -> Result<()> {
self.channel.message_retransmit()
}
+
+ fn channel_id(&self) -> u16 {
+ self.channel.channel_id()
+ }
}
impl<C: CredentialVerifier, B: Backend> Deref for WithKey<C, B> {
diff --git a/rust/trezor-thp/src/lib.rs b/rust/trezor-thp/src/lib.rs
index edd42338..07b3bba4 100644
--- a/rust/trezor-thp/src/lib.rs
+++ b/rust/trezor-thp/src/lib.rs
@@ -4,7 +4,7 @@
mod alternating_bit;
pub mod channel;
-mod control_byte;
+pub mod control_byte;
mod crc32;
pub mod credential;
pub mod error;
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.