#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod case;
#[cfg(feature = "rustcrypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustcrypto")))]
pub mod channel;
#[cfg(feature = "rustcrypto")]
mod pase;
pub mod status;
#[cfg(feature = "rustcrypto")]
pub use case::{
CaseInitiator, CaseOutcome, CaseResponder, MAX_ENCRYPTED2, MAX_ENCRYPTED3, MAX_SESSION_PARAMS,
MAX_SIGMA1, MAX_SIGMA2, MAX_SIGMA3, MAX_TBSDATA, ResumptionState, Sigma1, Sigma2, Sigma2Resume,
Sigma3, TbeData, Transcript,
};
#[cfg(feature = "rustcrypto")]
pub use channel::{Channel, ChannelBuffers, ChannelContext, ChannelReply, Established};
#[cfg(feature = "rustcrypto")]
pub(crate) use pase::Fields;
#[cfg(feature = "rustcrypto")]
pub use pase::{
PASSCODE_ID_COMMISSIONING, Pake1, Pake2, Pake3, PaseInitiator, PaseResponder,
PbkdfParamRequest, PbkdfParamResponse, PbkdfParameters, RANDOM_LEN, ResponderConfig,
};
pub use status::{GeneralCode, SecureChannelCode, StatusReport};
#[cfg(feature = "rustcrypto")]
use crate::error::{Error, ErrorCode, Result};
use crate::exchange::MrpParams;
use crate::platform::Duration;
#[cfg(feature = "rustcrypto")]
use crate::tlv::{Tag, TlvWriter};
use crate::transport::TransportModes;
pub mod opcode {
pub const MSG_COUNTER_SYNC_REQ: u8 = 0x00;
pub const MSG_COUNTER_SYNC_RSP: u8 = 0x01;
pub const MRP_STANDALONE_ACK: u8 = 0x10;
pub const PBKDF_PARAM_REQUEST: u8 = 0x20;
pub const PBKDF_PARAM_RESPONSE: u8 = 0x21;
pub const PAKE1: u8 = 0x22;
pub const PAKE2: u8 = 0x23;
pub const PAKE3: u8 = 0x24;
pub const SIGMA1: u8 = 0x30;
pub const SIGMA2: u8 = 0x31;
pub const SIGMA3: u8 = 0x32;
pub const SIGMA2_RESUME: u8 = 0x33;
pub const STATUS_REPORT: u8 = 0x40;
pub const ICD_CHECK_IN: u8 = 0x50;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SessionParams {
pub idle_interval_ms: u32,
pub active_interval_ms: u32,
pub active_threshold_ms: u16,
pub data_model_revision: u16,
pub interaction_model_revision: u16,
pub specification_version: u32,
pub max_paths_per_invoke: u16,
pub supported_transports: TransportModes,
pub max_tcp_message_size: Option<u32>,
}
impl SessionParams {
pub const DEFAULT_MAX_TCP_MESSAGE_SIZE: u32 = 64_000;
#[must_use]
pub fn legacy_peer() -> Self {
Self::announce_with_revisions(&MrpParams::default(), 16, 10, 0x0100_0000)
}
fn announce_with_revisions(
mrp: &MrpParams,
data_model_revision: u16,
interaction_model_revision: u16,
specification_version: u32,
) -> Self {
Self {
idle_interval_ms: clamp_ms_u32(mrp.idle_interval),
active_interval_ms: clamp_ms_u32(mrp.active_interval),
active_threshold_ms: clamp_ms_u16(mrp.active_threshold),
data_model_revision,
interaction_model_revision,
specification_version,
max_paths_per_invoke: 1,
supported_transports: TransportModes::empty(),
max_tcp_message_size: None,
}
}
#[must_use]
pub fn announce(mrp: &MrpParams) -> Self {
Self::announce_with_revisions(
mrp,
crate::DATA_MODEL_REVISION,
crate::im::INTERACTION_MODEL_REVISION,
crate::SPECIFICATION_VERSION,
)
}
#[must_use]
pub const fn with_max_paths_per_invoke(mut self, max: u16) -> Self {
self.max_paths_per_invoke = if max == 0 { 1 } else { max };
self
}
#[must_use]
pub const fn with_transports(mut self, modes: TransportModes, max_tcp_message: u32) -> Self {
self.supported_transports = modes;
self.max_tcp_message_size = if modes.is_empty() {
None
} else {
Some(max_tcp_message)
};
self
}
#[must_use]
pub fn to_mrp(self) -> MrpParams {
MrpParams {
idle_interval: Duration::from_millis(u64::from(self.idle_interval_ms)),
active_interval: Duration::from_millis(u64::from(self.active_interval_ms)),
active_threshold: Duration::from_millis(u64::from(self.active_threshold_ms)),
}
.clamped()
}
#[must_use]
pub const fn accepts_tcp(&self) -> bool {
self.supported_transports
.contains(TransportModes::TCP_SERVER)
}
#[must_use]
pub const fn max_tcp_message_size_or_default(&self) -> u32 {
match self.max_tcp_message_size {
Some(n) => n,
None => Self::DEFAULT_MAX_TCP_MESSAGE_SIZE,
}
}
#[cfg(feature = "rustcrypto")]
pub(crate) fn encode(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
w.unsigned(Tag::Context(1), u64::from(self.idle_interval_ms))?;
w.unsigned(Tag::Context(2), u64::from(self.active_interval_ms))?;
w.unsigned(Tag::Context(3), u64::from(self.active_threshold_ms))?;
w.unsigned(Tag::Context(4), u64::from(self.data_model_revision))?;
w.unsigned(Tag::Context(5), u64::from(self.interaction_model_revision))?;
w.unsigned(Tag::Context(6), u64::from(self.specification_version))?;
w.unsigned(Tag::Context(7), u64::from(self.max_paths_per_invoke))?;
w.unsigned(Tag::Context(8), u64::from(self.supported_transports.bits()))?;
if let Some(size) = self.max_tcp_message_size {
w.unsigned(Tag::Context(9), u64::from(size))?;
}
w.end_container()
}
#[cfg(feature = "rustcrypto")]
pub(crate) fn decode(fields: &mut pase::Fields<'_>) -> Result<Self> {
fields.nested(|fields| {
let mut out = Self::legacy_peer();
let mut seen = 0u32;
while let Some((tag, element)) = fields.next()? {
if tag < 32 {
let bit = 1u32 << tag;
if seen & bit != 0 {
return Err(Error::new(ErrorCode::TlvDuplicateTag));
}
seen |= bit;
}
match tag {
1 => out.idle_interval_ms = saturating_u32(element.unsigned()?),
2 => out.active_interval_ms = saturating_u32(element.unsigned()?),
3 => out.active_threshold_ms = saturating_u16(element.unsigned()?),
4 => out.data_model_revision = saturating_u16(element.unsigned()?),
5 => out.interaction_model_revision = saturating_u16(element.unsigned()?),
6 => out.specification_version = saturating_u32(element.unsigned()?),
7 => out.max_paths_per_invoke = saturating_u16(element.unsigned()?).max(1),
8 => {
out.supported_transports =
TransportModes::from_bits_truncate(saturating_u32(element.unsigned()?));
}
9 => out.max_tcp_message_size = Some(saturating_u32(element.unsigned()?)),
_ => fields.skip(&element)?,
}
}
Ok(out)
})
}
}
impl Default for SessionParams {
fn default() -> Self {
Self::announce(&MrpParams::default())
}
}
fn clamp_ms_u32(d: Duration) -> u32 {
u32::try_from(d.as_millis()).unwrap_or(u32::MAX)
}
fn clamp_ms_u16(d: Duration) -> u16 {
u16::try_from(d.as_millis()).unwrap_or(u16::MAX)
}
#[cfg(feature = "rustcrypto")]
#[expect(
clippy::cast_possible_truncation,
reason = "the branch above is the saturation"
)]
const fn saturating_u32(v: u64) -> u32 {
if v > u32::MAX as u64 {
u32::MAX
} else {
v as u32
}
}
#[cfg(feature = "rustcrypto")]
#[expect(
clippy::cast_possible_truncation,
reason = "the branch above is the saturation"
)]
const fn saturating_u16(v: u64) -> u16 {
if v > u16::MAX as u64 {
u16::MAX
} else {
v as u16
}
}