What changed, and why it matters
This commit is a straightforward code cleanup in the Trezor firmware's Rust implementation of the Trezor Host Protocol (THP). It replaces repeated uses of a generic type `Channel<Host, B>` or `Channel<Device, B>` with shorter type aliases `Channel<B>` inside the host and device channel modules. No behavior of the code changes, and there are no security implications.
No security action needed. Treat as normal maintenance/refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces module-local type aliases pub type Channel<B> = super::Channel<Device, B> and pub type Channel<B> = super::Channel<Host, B> in rust/trezor-thp/src/channel/device.rs and host.rs respectively. It updates internal struct fields and return types to use the alias, and removes the now-unnecessary re-export of the generic Channel from lib.rs. The example CLI is updated only to import the aliased Channel from the host module. This is a pure refactor with identical compiled semantics.
Changed components
rust/trezor-thp/src/channel/device.rsrust/trezor-thp/src/channel/host.rsrust/trezor-thp/src/lib.rsrust/trezor-thp/examples/host-cli/main.rsInspect captured patch +15 / −11
diff --git a/rust/trezor-thp/examples/host-cli/main.rs b/rust/trezor-thp/examples/host-cli/main.rs
index a15e7264..cf3c11a6 100644
--- a/rust/trezor-thp/examples/host-cli/main.rs
+++ b/rust/trezor-thp/examples/host-cli/main.rs
@@ -6,8 +6,8 @@ use std::{env, net::SocketAddr, str::FromStr};
use protobuf::Message;
use trezor_thp::{
- Backend, Channel, Host,
- channel::host::{ChannelOpen, Mux},
+ Backend,
+ channel::host::{Channel, ChannelOpen, Mux},
credential::{CredentialStore, NullCredentialStore},
};
@@ -29,7 +29,7 @@ impl Backend for RustCrypto {
}
}
-type HostChannel = Channel<Host, RustCrypto>;
+type HostChannel = Channel<RustCrypto>;
fn do_allocation(client: &mut Client<Mux<RustCrypto>>) {
client.call(0, &[]);
@@ -87,7 +87,7 @@ fn do_pairing_skip(client: &mut Client<HostChannel>) {
);
}
-fn do_ping(client: &mut Client<Channel<Host, RustCrypto>>) {
+fn do_ping(client: &mut Client<HostChannel>) {
let mut ping = Ping::new();
ping.set_message("trezor-thp/examples".into());
ping.set_button_protection(true);
diff --git a/rust/trezor-thp/src/channel/device.rs b/rust/trezor-thp/src/channel/device.rs
index e19ddf62..64072874 100644
--- a/rust/trezor-thp/src/channel/device.rs
+++ b/rust/trezor-thp/src/channel/device.rs
@@ -1,7 +1,7 @@
use heapless;
use crate::{
- Backend, Channel, ChannelIO, Device, Error,
+ Backend, ChannelIO, Device, Error,
alternating_bit::SyncBits,
channel::{
ChannelState, Nonce, PRIVKEY_LEN, PacketInResult, PairingState, noise::NoiseHandshake,
@@ -33,6 +33,8 @@ const BROADCAST_OUTGOING_QUEUE_LEN: usize = 8;
// "?##" + Failure message type + msg_size + msg_data (code = "Failure_InvalidProtocol")
const CODEC_V1_RESPONSE: &[u8] = b"?##\x00\x03\x00\x00\x00\x02\x08\x11";
+pub type Channel<B> = super::Channel<Device, B>;
+
/// Maps packets to channels. Handles broadcast channel messages, notably channel allocation.
/// Every packet interface on the device needs to have one Mux. Event loop should pass every
/// incoming packet to [`Mux::packet_in`] in order to determine what to do with it.
@@ -285,7 +287,7 @@ enum HandshakeState {
/// Please note that this object also handles sending ChannelAllocationResponse
/// which is a broadcast message, which are normally handled by [`Mux`].
pub struct ChannelOpen<C: CredentialVerifier, B: Backend> {
- channel: Channel<Device, B>,
+ channel: Channel<B>,
state: HandshakeState,
noise: NoiseHandshake<Device, B>,
internal_buffer: heapless::Vec<u8, INTERNAL_BUFFER_LEN>,
@@ -421,7 +423,7 @@ impl<C: CredentialVerifier, B: Backend> ChannelOpen<C, B> {
///
/// [Pairing phase]: https://docs.trezor.io/trezor-firmware/common/thp/specification.html#pairing-phase
/// [Credential phase]: https://docs.trezor.io/trezor-firmware/common/thp/specification.html#credential-phase
- pub fn complete(self) -> Result<Channel<Device, B>, Error> {
+ pub fn complete(self) -> Result<Channel<B>, Error> {
if self.channel.noise.is_none() {
return Err(Error::unexpected_input());
}
diff --git a/rust/trezor-thp/src/channel/host.rs b/rust/trezor-thp/src/channel/host.rs
index 4584ad16..a191c70b 100644
--- a/rust/trezor-thp/src/channel/host.rs
+++ b/rust/trezor-thp/src/channel/host.rs
@@ -1,7 +1,7 @@
use heapless;
use crate::{
- Backend, Channel, ChannelIO, Error, Host,
+ Backend, ChannelIO, Error, Host,
alternating_bit::SyncBits,
channel::{ChannelState, Nonce, PacketInResult, PairingState, noise::NoiseHandshake},
credential::CredentialStore,
@@ -22,6 +22,8 @@ use core::marker::PhantomData;
const INTERNAL_BUFFER_LEN: usize = 192;
const MAX_DEVICE_PROPERTIES_LEN: usize = 128;
+pub type Channel<B> = super::Channel<Host, B>;
+
enum AllocationState {
None,
SendingRequest {
@@ -331,7 +333,7 @@ enum HandshakeState {
/// - perform [`ChannelIO`] with empty messages until [`ChannelOpen::handshake_done`]
/// - call [`ChannelOpen::complete`] to obtain [`Channel`]
pub struct ChannelOpen<C: CredentialStore, B: Backend> {
- channel: Channel<Host, B>,
+ channel: Channel<B>,
state: HandshakeState,
noise: NoiseHandshake<Host, B>,
internal_buffer: heapless::Vec<u8, INTERNAL_BUFFER_LEN>,
@@ -463,7 +465,7 @@ impl<C: CredentialStore, B: Backend> ChannelOpen<C, B> {
///
/// [Pairing phase]: https://docs.trezor.io/trezor-firmware/common/thp/specification.html#pairing-phase
/// [Credential phase]: https://docs.trezor.io/trezor-firmware/common/thp/specification.html#credential-phase
- pub fn complete(self) -> Result<Channel<Host, B>, Error> {
+ pub fn complete(self) -> Result<Channel<B>, Error> {
if self.channel.noise.is_none() {
return Err(Error::unexpected_input());
}
diff --git a/rust/trezor-thp/src/lib.rs b/rust/trezor-thp/src/lib.rs
index 56d62752..edd42338 100644
--- a/rust/trezor-thp/src/lib.rs
+++ b/rust/trezor-thp/src/lib.rs
@@ -12,7 +12,7 @@ mod fragment;
pub mod header;
mod util;
-pub use channel::{Backend, Channel, ChannelIO};
+pub use channel::{Backend, ChannelIO};
pub use error::Error;
pub trait Role: Clone + PartialEq {
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.