rs_matter_pairing/lib.rs
1/*
2 *
3 * Copyright (c) 2026 Project CHIP Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18//! The onboarding payloads of Matter: the manual pairing code, the QR code and
19//! its scanning, and the NFC payload. And what a device authenticates
20//! commissioners with: its passcode, or the verifier computed from it.
21
22#![cfg_attr(not(feature = "std"), no_std)]
23#![allow(unknown_lints)]
24#![allow(clippy::uninlined_format_args)]
25// `cargo llvm-cov` sets `coverage_nightly` when it runs on a nightly toolchain;
26// the test modules are then excluded from the coverage report
27#![cfg_attr(all(coverage_nightly, test), feature(coverage_attribute))]
28
29use crate::crypto::{canon, EC_CANON_POINT_LEN, EC_CANON_SCALAR_LEN};
30use crate::error::{Error, ErrorCode};
31use crate::utils::bitflags::bitflags;
32
33// The dependencies, under the names the code uses for them
34use rs_matter_crypto as crypto;
35use rs_matter_tlv as tlv;
36use rs_matter_utils as utils;
37use rs_matter_utils::error;
38
39#[cfg(feature = "alloc")]
40extern crate alloc;
41
42// This mod MUST go first, so that the others see its macros.
43pub(crate) mod fmt;
44
45pub mod code;
46pub mod qr;
47
48/// The length of the setup passcode, in bytes.
49pub const SPAKE2P_VERIFIER_PASSWORD_LEN: usize = 4;
50
51/// The length of a SPAKE2+ verifier: `w0`, then `L`.
52pub const SPAKE2P_VERIFIER_STR_LEN: usize = EC_CANON_SCALAR_LEN + EC_CANON_POINT_LEN;
53
54/// Maximum size of the PASE salt buffer, in bytes.
55///
56/// The Matter Core spec (Cryptographic Building Blocks) defines the
57/// PASE salt as a variable-length octet string in **the range 16..=32**.
58pub const SPAKE2P_VERIFIER_SALT_LEN: usize = 32;
59
60/// Minimum PASE salt length per Matter Core spec.
61pub const SPAKE2P_VERIFIER_SALT_MIN_LEN: usize = 16;
62
63/// The smallest PBKDF2 iteration count of a verifier.
64pub const SPAKE2P_ITERATIONS_MIN: u32 = 1000;
65
66/// The largest PBKDF2 iteration count of a verifier.
67pub const SPAKE2P_ITERATIONS_MAX: u32 = 100_000;
68
69canon!(
70 SPAKE2P_VERIFIER_PASSWORD_LEN,
71 SPAKE2P_VERIFIER_PASSWORD_ZEROED,
72 Spake2pVerifierPassword,
73 Spake2pVerifierPasswordRef
74);
75canon!(
76 SPAKE2P_VERIFIER_STR_LEN,
77 SPAKE2P_VERIFIER_STR_ZEROED,
78 Spake2pVerifierStr,
79 Spake2pVerifierStrRef
80);
81canon!(
82 SPAKE2P_VERIFIER_SALT_LEN,
83 SPAKE2P_VERIFIER_SALT_ZEROED,
84 Spake2pVerifierSalt,
85 Spake2pVerifierSaltRef
86);
87
88/// The passcode and the discriminator: what the onboarding payloads carry.
89#[derive(Debug, Clone)]
90#[cfg_attr(feature = "defmt", derive(defmt::Format))]
91pub struct BasicCommData {
92 /// The passcode, which commissioners authenticate the device with
93 pub password: Spake2pVerifierPassword,
94 /// The 12-bit discriminator used to differentiate between multiple devices
95 pub discriminator: u16,
96}
97
98/// What a device authenticates commissioners with in its basic commissioning
99/// window.
100#[derive(Debug, Clone)]
101#[cfg_attr(feature = "defmt", derive(defmt::Format))]
102pub enum DevCommData<'a> {
103 /// The passcode itself: each window gets a verifier computed from it, and
104 /// the device can show its pairing code and QR code. For development and
105 /// tests, as the Core spec wants a device with a static passcode to be
106 /// given the verifier, and the passcode kept out of its reach.
107 Passcode(BasicCommData),
108 /// The verifier computed from the passcode at manufacturing.
109 Verifier(VerifierCommData<'a>),
110}
111
112impl DevCommData<'_> {
113 /// The discriminator.
114 pub const fn discriminator(&self) -> u16 {
115 match self {
116 Self::Passcode(data) => data.discriminator,
117 Self::Verifier(data) => data.discriminator,
118 }
119 }
120
121 /// The passcode and the discriminator, if the device has its passcode.
122 pub const fn passcode(&self) -> Option<&BasicCommData> {
123 match self {
124 Self::Passcode(data) => Some(data),
125 Self::Verifier(_) => None,
126 }
127 }
128}
129
130impl From<BasicCommData> for DevCommData<'_> {
131 fn from(data: BasicCommData) -> Self {
132 Self::Passcode(data)
133 }
134}
135
136impl<'a> From<VerifierCommData<'a>> for DevCommData<'a> {
137 fn from(data: VerifierCommData<'a>) -> Self {
138 Self::Verifier(data)
139 }
140}
141
142/// A SPAKE2+ verifier, with the salt and the iteration count it was computed
143/// with, and the discriminator.
144#[derive(Debug, Clone, Copy)]
145#[cfg_attr(feature = "defmt", derive(defmt::Format))]
146pub struct VerifierCommData<'a> {
147 discriminator: u16,
148 verifier: Spake2pVerifierStrRef<'a>,
149 salt: &'a [u8],
150 iterations: u32,
151}
152
153impl<'a> VerifierCommData<'a> {
154 /// Fails unless the verifier is [`SPAKE2P_VERIFIER_STR_LEN`] bytes, the
155 /// salt 16 to 32 bytes and the iteration count 1,000 to 100,000.
156 pub fn new(
157 discriminator: u16,
158 verifier: &'a [u8],
159 salt: &'a [u8],
160 iterations: u32,
161 ) -> Result<Self, Error> {
162 if !(SPAKE2P_VERIFIER_SALT_MIN_LEN..=SPAKE2P_VERIFIER_SALT_LEN).contains(&salt.len())
163 || !(SPAKE2P_ITERATIONS_MIN..=SPAKE2P_ITERATIONS_MAX).contains(&iterations)
164 {
165 Err(ErrorCode::InvalidData)?;
166 }
167
168 Ok(Self {
169 discriminator,
170 verifier: Spake2pVerifierStrRef::try_new(verifier)?,
171 salt,
172 iterations,
173 })
174 }
175
176 /// The discriminator.
177 pub const fn discriminator(&self) -> u16 {
178 self.discriminator
179 }
180
181 /// The verifier: `w0`, then `L`.
182 pub const fn verifier(&self) -> Spake2pVerifierStrRef<'a> {
183 self.verifier
184 }
185
186 /// The salt.
187 pub const fn salt(&self) -> &'a [u8] {
188 self.salt
189 }
190
191 /// The PBKDF2 iteration count.
192 pub const fn iterations(&self) -> u32 {
193 self.iterations
194 }
195}
196
197bitflags! {
198 #[repr(transparent)]
199 #[cfg_attr(not(feature = "defmt"), derive(Debug, Copy, Clone, Eq, PartialEq, Hash))]
200 /// The Discovery Capabilities Bitmask carried in the machine-readable
201 /// onboarding payloads (QR code, NFC tag).
202 pub struct DiscoveryCapabilities: u8 {
203 /// Soft Access Point (AP) discovery.
204 ///
205 /// Note bit 0 is reserved and SHALL be zero as of Matter 1.6;
206 /// `SOFT_AP` predates that and is kept only for backwards
207 /// compatibility - do not set it on a 1.6 device.
208 const SOFT_AP = 0x01;
209 /// BLE discovery
210 const BLE = 0x02;
211 /// IP-based discovery (e.g. mDNS or DNS-SD with Thread)
212 const IP = 0x04;
213 /// Wi-Fi Public Action Frame
214 const PAF = 0x08;
215 /// NFC Transport Layer (NTL) commissioning
216 const NTL = 0x10;
217 }
218}
219
220impl Default for DiscoveryCapabilities {
221 fn default() -> Self {
222 Self::IP
223 }
224}