refactor(rust/trezor-thp): export more constants
What changed, and why it matters
This commit is a straightforward internal code cleanup in the Rust implementation of Trezor's transport protocol (THP). It moves several constants from inside a trait or function to the module level and makes them public so other parts of the code can use them directly. The actual numeric values and behavior do not change. There is no indication this fixes a security bug or changes how the device protects data.
No security action needed. Review as normal code-quality refactor if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors constant visibility and location in rust/trezor-thp. APP_HEADER_LEN and a new SEND_BUFFER_OVERHEAD (equal to the previous ChannelIO::BUFFER_OVERHEAD value of APP_HEADER_LEN + TAG_LEN) are exported from channel/mod.rs. buffered.rs now references SEND_BUFFER_OVERHEAD instead of C::BUFFER_OVERHEAD. MAX_RETRANSMISSION_COUNT is hoisted from inside retransmit_after_ms to a public module-level constant. No values are altered; this is a pure maintainability refactor.
Changed components
rust/trezor-thp/src/channel/buffered.rsrust/trezor-thp/src/channel/mod.rsInspect captured patch +13 / −9
diff --git a/rust/trezor-thp/src/channel/buffered.rs b/rust/trezor-thp/src/channel/buffered.rs
index e8f2b308..030cfba7 100644
--- a/rust/trezor-thp/src/channel/buffered.rs
+++ b/rust/trezor-thp/src/channel/buffered.rs
@@ -1,6 +1,6 @@
use crate::{
ChannelIO,
- channel::{APP_HEADER_LEN, PacketInResult},
+ channel::{APP_HEADER_LEN, PacketInResult, SEND_BUFFER_OVERHEAD},
error::Result,
};
@@ -66,7 +66,7 @@ impl<C: ChannelIO> Buffered<C> {
}
pub fn message_in(&mut self, session_id: u8, message_type: u16, message: &[u8]) -> Result<()> {
- let mut send_buffer = vec![0; message.len() + C::BUFFER_OVERHEAD];
+ let mut send_buffer = vec![0; message.len() + SEND_BUFFER_OVERHEAD];
let res = self.channel.message_in_from(
session_id,
message_type,
diff --git a/rust/trezor-thp/src/channel/mod.rs b/rust/trezor-thp/src/channel/mod.rs
index a316c1a4..b7348d91 100644
--- a/rust/trezor-thp/src/channel/mod.rs
+++ b/rust/trezor-thp/src/channel/mod.rs
@@ -38,7 +38,11 @@ const HANDSHAKE_BUFFER_DTH_LEN: usize = max(
2 * PUBKEY_LEN + 2 * TAG_LEN + CHECKSUM_LEN, // HandshakeInitiationResponse
);
-const APP_HEADER_LEN: usize = 3; // session id (1) + message type (2)
+pub const APP_HEADER_LEN: usize = 3; // session id (1) + message type (2)
+
+/// Required size of the send buffer in addition to serialized message length.
+/// Session ID (1B) + message type (2B) + AEAD tag (16B).
+pub const SEND_BUFFER_OVERHEAD: usize = APP_HEADER_LEN + TAG_LEN;
/// Used during channel allocation on broadcast channel.
#[derive(Copy, Clone, PartialEq, Eq)]
@@ -672,9 +676,6 @@ impl PacketInResult {
/// to be passed along every call. You can wrap the channel in [`buffered::Buffered`] to
/// handle the buffers for you.
pub trait ChannelIO {
- /// Session ID (1B) + message type (2B) + AEAD tag (16B).
- const BUFFER_OVERHEAD: usize = APP_HEADER_LEN + TAG_LEN;
-
/// Pass incoming packet into a channel.
///
/// Please note the caller should first check whether channel ID matches.
@@ -736,8 +737,8 @@ pub trait ChannelIO {
/// Submit message for channel to encrypt and fragment into packets.
///
- /// The length of `send_buffer` must be at least [`Self::BUFFER_OVERHEAD`] more
- /// than the message length.
+ /// The length of `send_buffer` must be at least [`SEND_BUFFER_OVERHEAD`] more than
+ /// the message length.
///
/// Returns [`Error::NotReady`] if the channel hasn't finished sending the previous message
/// (did not send all fragments or did not receive valid ACK), or if the channel is
@@ -901,6 +902,10 @@ impl<R: Role, B: Backend> ChannelIO for Channel<R, B> {
}
}
+/// The maximum number of transport payload retransmissions that the sender should attempt.
+/// Defined in the specification, applications are free to use lower number.
+pub const MAX_RETRANSMISSION_COUNT: u8 = 50;
+
/// Returns how many milliseconds to wait for an ACK for a given retransmission attempt.
/// First timeout (0th retry) is after 200ms till ~3.52s.
///
@@ -908,7 +913,6 @@ impl<R: Role, B: Backend> ChannelIO for Channel<R, B> {
/// you are free to use different function. It is recommended to measure the duration between
/// sending last packet and receiving an ACK ("ack_latency") and add it to this number.
pub fn retransmit_after_ms(retry: u8) -> u32 {
- const MAX_RETRANSMISSION_COUNT: u8 = 50;
let retry: u32 = retry.min(MAX_RETRANSMISSION_COUNT - 1).into();
10300 - 1010000 / retry.saturating_add(100)
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.