pub const fn initial_counter(randomness: u32) -> u32Expand description
A sender’s own counter for one key (§4.6.1).
“The message counter is generated based on the Session Type and increases monotonically for each unique message generated.” A retransmission reuses its counter — “logical retransmission is of a given message as identified by its message counter” (§4.4.1.4) — so the counter is taken once, when the message is first built, and not again.
§4.6.1.1’s Crypto_DRBG(len = 28) + 1, applied to a full-width random word.
“All message counters SHALL be initialized with a random value using the
Crypto_DRBG(len = 28) + 1 primitive” — all, which is four counters in this crate with
four different lifetimes: a secure session’s (§4.6.2, randomised at establishment), the
global unencrypted one (§4.6.1.2, randomised at startup), the two global group ones
(§4.6.1.3) and the Check-In counter (§4.6.3), both randomised at factory reset and persisted
after that. They share one rule, so they share one implementation: a counter that starts
anywhere in u32 is a rule broken four times over, or not at all.
The result is in 1..=2^28. §4.6.1.1 gives the reason for the ceiling in its own words —
the range is narrow “in order to maximize initial entropy while still reserving the vast
majority of the range to actual counter values (roughly 2³² - 2²⁸)”.
use matter_kit::msg::initial_counter;
assert_eq!(initial_counter(0), 1);
assert_eq!(initial_counter(u32::MAX), 1 << 28);
assert!((1..=(1 << 28)).contains(&initial_counter(0xDEAD_BEEF)));