Skip to main content

matter_kit/msg/
mod.rs

1//! The Matter message frame: headers, identifiers, and the counters that keep a message
2//! from being replayed.
3//!
4//! This is Core §4.4–4.9 — everything between "some bytes arrived on a socket" and "this
5//! is a protocol message for exchange 7". What is here is the framing and the anti-replay
6//! machinery; encryption arrives with the secure channel, and the layout below is already
7//! shaped for it (the header is outside the ciphertext because a receiver must find the
8//! key before it can decrypt).
9//!
10//! ```text
11//! ┌── message header ─────────────────────────┐┌── payload (encrypted) ──┐┌ footer ┐
12//! │ flags │ session │ sec │ counter │ src │ dst││ protocol header │ app   ││  MIC   │
13//! └───────────────────────────────────────────┘└─────────────────────────┘└────────┘
14//! ```
15//!
16//! ```
17//! use matter_kit::msg::{Destination, MessageHeader, NodeId, SessionId};
18//!
19//! let header = MessageHeader {
20//!     session_id: SessionId(0x1234),
21//!     message_counter: 42,
22//!     source: Some(NodeId(1)),
23//!     destination: Destination::Node(NodeId(2)),
24//!     ..MessageHeader::default()
25//! };
26//!
27//! let mut buf = [0u8; 64];
28//! let n = header.encode(&mut buf)?;
29//! let (decoded, payload) = MessageHeader::decode(&buf[..n])?;
30//! assert_eq!(decoded, header);
31//! assert!(payload.is_empty());
32//! # Ok::<(), matter_kit::Error>(())
33//! ```
34
35mod counter;
36mod header;
37mod ids;
38#[cfg(feature = "rustcrypto")]
39mod security;
40
41pub use counter::{
42    CounterKind, CounterWindow, MSG_COUNTER_WINDOW_SIZE, MessageCounter, Verdict, initial_counter,
43};
44pub use header::{
45    Destination, ExchangeFlags, MESSAGE_FORMAT_VERSION, MessageFlags, MessageHeader,
46    ProtocolHeader, SecurityFlags, SessionType,
47};
48pub use ids::{
49    CaseAuthenticatedTag, ExchangeId, FabricId, FabricIndex, GroupId, NodeId, NodeIdKind,
50    ProtocolId, SessionId, VendorId,
51};
52#[cfg(feature = "rustcrypto")]
53pub use security::{NonceSource, Preview, SessionKeys, preview, protect, unprotect};