rs-matter 0.2.0

Native Rust implementation of the Matter (Smart-Home) ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 *
 *    Copyright (c) 2026 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

//! PASE Initiator (Commissioner/Controller side) implementation.
//!
//! This module implements the initiator side of the PASE (Passcode-Authenticated Session Establishment)
//! protocol, used by commissioners to establish secure sessions with Matter devices.

use rand_core::RngCore;

use crate::crypto::{
    CanonEcPointRef, Crypto, HmacHash, HmacHashRef, Kdf, AEAD_CANON_KEY_LEN, EC_POINT_ZEROED,
    HMAC_HASH_ZEROED,
};
use crate::error::{Error, ErrorCode};
use crate::sc::pase::spake2p::{
    ProverContext, Spake2P, Spake2pRandom, Spake2pSessionKeys, Spake2pVerifierPasswordRef,
    SPAKE2P_VERIFIER_SALT_LEN, SPAKE2P_VERIFIER_SALT_MIN_LEN,
};
use crate::sc::{complete_with_status, GeneralCode, OpCode, SCStatusCodes, StatusReport};
use crate::tlv::{FromTLV, OctetStr, TLVElement, TagType, ToTLV};
use crate::transport::exchange::Exchange;
use crate::transport::session::{ReservedSession, SessionMode};
use crate::utils::storage::ReadBuf;

use super::{PBKDFParamReq, PBKDFParamResp, Pake1, Pake2, Pake3, SPAKE2_SESSION_KEYS_INFO};

/// PASE Initiator for establishing secure sessions with Matter devices.
///
/// This implements the commissioner/controller side of the PASE protocol.
/// The typical flow is:
///
/// 1. Create an unsecured exchange to the target device
/// 2. Call `PaseInitiator::initiate()` with the setup passcode
/// 3. On success, the exchange's session is upgraded to a secure PASE session
pub struct PaseInitiator<C: Crypto> {
    crypto: C,
    spake2p: Spake2P,
    initiator_random: Spake2pRandom,
    local_sessid: u16,
    peer_sessid: u16,
    prover_context: Option<ProverContext>,
    ca: HmacHash,
}

impl<C: Crypto> PaseInitiator<C> {
    /// Create a new PASE initiator
    const fn new(crypto: C) -> Self {
        Self {
            crypto,
            spake2p: Spake2P::new(),
            initiator_random: Spake2pRandom::new(),
            local_sessid: 0,
            peer_sessid: 0,
            prover_context: None,
            ca: HMAC_HASH_ZEROED,
        }
    }

    /// Initiate a PASE handshake with a Matter device.
    ///
    /// This performs the complete PASE handshake:
    /// 1. Send PBKDFParamRequest
    /// 2. Receive PBKDFParamResponse
    /// 3. Send Pake1 (with pA)
    /// 4. Receive Pake2 (with pB, cB)
    /// 5. Send Pake3 (with cA)
    /// 6. Receive StatusReport
    ///
    /// On success, the session is upgraded to a secure PASE session.
    ///
    /// # Arguments
    /// - `exchange` - An unsecured exchange to the target device
    /// - `crypto` - The crypto implementation
    /// - `password` - The setup passcode (typically 8 digits, e.g., 20202021)
    ///
    /// # Returns
    /// - `Ok(())` on successful session establishment
    /// - `Err(Error)` on failure
    pub async fn initiate(
        exchange: &mut Exchange<'_>,
        crypto: C,
        password: u32,
    ) -> Result<(), Error> {
        let session = ReservedSession::reserve(exchange.matter(), &crypto).await?;

        let mut initiator = Self::new(crypto);

        // Step 1: Send PBKDFParamRequest, receive PBKDFParamResponse
        let (salt, salt_len, iterations) = match initiator.exchange_pbkdf_params(exchange).await {
            Ok(result) => result,
            Err(e) => {
                // Send status report to notify responder of failure
                let _ = complete_with_status(exchange, SCStatusCodes::InvalidParameter, &[]).await;
                return Err(e);
            }
        };

        // Step 2: Send Pake1, receive Pake2
        if let Err(e) = initiator
            .exchange_pake1_pake2(exchange, password, &salt[..salt_len], iterations)
            .await
        {
            // Send status report to notify responder of failure (e.g., wrong password)
            let _ = complete_with_status(exchange, SCStatusCodes::InvalidParameter, &[]).await;
            return Err(e);
        }

        // Step 3: Send Pake3, receive StatusReport
        // Note: No need to send status report here since we're just receiving one
        initiator.exchange_pake3_status(exchange).await?;

        // Step 4: Complete session establishment
        initiator.complete_session(exchange, session).await
    }

    /// Exchange PBKDFParamRequest/Response
    ///
    /// Returns (salt, salt_len, iterations) on success
    async fn exchange_pbkdf_params(
        &mut self,
        exchange: &mut Exchange<'_>,
    ) -> Result<([u8; SPAKE2P_VERIFIER_SALT_LEN], usize, u32), Error> {
        // Generate random and session ID
        let mut rand = self.crypto.rand()?;
        rand.fill_bytes(self.initiator_random.access_mut());

        self.local_sessid = exchange.with_state(|state| Ok(state.sessions.get_next_sess_id()))?;

        // Build and send request
        let req = PBKDFParamReq {
            initiator_random: OctetStr::new(self.initiator_random.access()),
            initiator_ssid: self.local_sessid,
            passcode_id: 0,
            has_params: false,
            session_parameters: None,
        };

        // Start context hash with request payload
        let context = {
            let mut context = None;
            exchange
                .send_with(|_, wb| {
                    req.to_tlv(&TagType::Anonymous, &mut *wb)?;

                    context = Some(self.spake2p.start_context(
                        &self.crypto,
                        self.local_sessid,
                        0, // peer_sessid not known yet
                        wb.as_slice(),
                    )?);

                    Ok(Some(OpCode::PBKDFParamRequest.into()))
                })
                .await?;
            context.ok_or(ErrorCode::Invalid)?
        };

        // Receive response
        exchange.recv_fetch().await?;

        let rx = exchange.rx()?;
        let meta = rx.meta();

        // Check for StatusReport (error case)
        if meta.proto_opcode == OpCode::StatusReport as u8 {
            let mut rb = ReadBuf::new(rx.payload());
            let status = StatusReport::read(&mut rb)?;
            error!(
                "PASE failed: general={:?}, proto_code={}",
                status.general_code, status.proto_code
            );
            return Err(ErrorCode::Invalid.into());
        }

        // Verify opcode
        if meta.proto_opcode != OpCode::PBKDFParamResponse as u8 {
            error!(
                "Unexpected opcode: expected PBKDFParamResponse, got {}",
                meta.proto_opcode
            );
            return Err(ErrorCode::InvalidOpcode.into());
        }

        // Parse response
        let resp = PBKDFParamResp::from_tlv(&TLVElement::new(rx.payload()))?;

        // Verify echoed random matches
        if resp.initiator_random.0 != self.initiator_random.access() {
            error!("PBKDFParamResponse: initiator_random mismatch");
            return Err(ErrorCode::Invalid.into());
        }

        // Extract peer session ID
        self.peer_sessid = resp.responder_ssid;

        // Extract PBKDF parameters
        let params = resp.params.ok_or_else(|| {
            error!("PBKDFParamResponse: missing PBKDF params");
            ErrorCode::Invalid
        })?;

        let mut salt = [0u8; SPAKE2P_VERIFIER_SALT_LEN];
        let salt_len = params.salt.0.len();
        if !(SPAKE2P_VERIFIER_SALT_MIN_LEN..=SPAKE2P_VERIFIER_SALT_LEN).contains(&salt_len) {
            error!("PBKDFParamResponse: invalid salt length {}", salt_len);
            return Err(ErrorCode::Invalid.into());
        }
        salt[..salt_len].copy_from_slice(params.salt.0);

        // Finish context hash with response payload
        self.spake2p.finish_context::<&C>(context, rx.payload())?;

        Ok((salt, salt_len, params.iterations))
    }

    /// Exchange Pake1/Pake2
    async fn exchange_pake1_pake2(
        &mut self,
        exchange: &mut Exchange<'_>,
        password: u32,
        salt: &[u8],
        iterations: u32,
    ) -> Result<(), Error> {
        // Setup prover and generate pA
        let mut pa = EC_POINT_ZEROED;
        let password_bytes = password.to_le_bytes();
        let password_ref = Spake2pVerifierPasswordRef::new(&password_bytes);

        let prover_ctx =
            self.spake2p
                .setup_prover(&self.crypto, password_ref, salt, iterations, &mut pa)?;

        // Send Pake1
        let pake1 = Pake1 {
            pa: OctetStr::new(pa.access()),
        };

        exchange
            .send_with(|_, wb| {
                pake1.to_tlv(&TagType::Anonymous, wb)?;
                Ok(Some(OpCode::PASEPake1.into()))
            })
            .await?;

        // Receive Pake2
        exchange.recv_fetch().await?;

        let rx = exchange.rx()?;
        let meta = rx.meta();

        // Check for StatusReport (error case)
        if meta.proto_opcode == OpCode::StatusReport as u8 {
            let mut rb = ReadBuf::new(rx.payload());
            let status = StatusReport::read(&mut rb)?;
            error!(
                "PASE Pake1 failed: general={:?}, proto_code={}",
                status.general_code, status.proto_code
            );
            return Err(ErrorCode::Invalid.into());
        }

        // Verify opcode
        if meta.proto_opcode != OpCode::PASEPake2 as u8 {
            error!(
                "Unexpected opcode: expected PASEPake2, got {}",
                meta.proto_opcode
            );
            return Err(ErrorCode::InvalidOpcode.into());
        }

        // Parse Pake2
        let pake2 = Pake2::from_tlv(&TLVElement::new(rx.payload()))?;

        // Extract pB and cB
        let pb: CanonEcPointRef<'_> = pake2.pb.0.try_into()?;
        let cb: HmacHashRef<'_> = pake2.cb.0.try_into()?;

        // Complete prover - this verifies cB and computes cA
        let mut ca = HMAC_HASH_ZEROED;
        self.spake2p
            .complete_prover(&self.crypto, &prover_ctx, pa.reference(), pb, cb, &mut ca)
            .map_err(|_| {
                error!("PASE: cB verification failed (wrong password?)");
                ErrorCode::Invalid
            })?;

        // Store cA for Pake3
        self.ca = ca;
        self.prover_context = Some(prover_ctx);

        Ok(())
    }

    /// Exchange Pake3/StatusReport
    async fn exchange_pake3_status(&mut self, exchange: &mut Exchange<'_>) -> Result<(), Error> {
        // Send Pake3
        let pake3 = Pake3 {
            ca: OctetStr::new(self.ca.access()),
        };

        exchange
            .send_with(|_, wb| {
                pake3.to_tlv(&TagType::Anonymous, wb)?;
                Ok(Some(OpCode::PASEPake3.into()))
            })
            .await?;

        // Receive StatusReport
        exchange.recv_fetch().await?;

        let rx = exchange.rx()?;
        let meta = rx.meta();

        // Verify opcode
        if meta.proto_opcode != OpCode::StatusReport as u8 {
            error!(
                "Unexpected opcode: expected StatusReport, got {}",
                meta.proto_opcode
            );
            return Err(ErrorCode::InvalidOpcode.into());
        }

        // Parse StatusReport
        let mut rb = ReadBuf::new(rx.payload());
        let status = StatusReport::read(&mut rb)?;

        // Check for success
        if status.general_code != GeneralCode::Success
            || status.proto_code != SCStatusCodes::SessionEstablishmentSuccess as u16
        {
            error!(
                "PASE failed: general={:?}, proto_code={}",
                status.general_code, status.proto_code
            );
            return Err(ErrorCode::Invalid.into());
        }

        Ok(())
    }

    /// Complete session establishment
    async fn complete_session(
        &mut self,
        exchange: &mut Exchange<'_>,
        mut session: ReservedSession<'_>,
    ) -> Result<(), Error> {
        // Derive session keys from Ke
        let ke = self.spake2p.ke();

        let mut session_keys = Spake2pSessionKeys::new();
        self.crypto
            .kdf()?
            .expand(&[], ke, SPAKE2_SESSION_KEYS_INFO, &mut session_keys)
            .map_err(|_| ErrorCode::InvalidData)?;

        // Get peer address
        let peer_addr = exchange.with_state(|state| {
            let sess = exchange.id().session(&mut state.sessions);
            Ok(sess.get_peer_addr())
        })?;

        // Split session keys into dec_key, enc_key, att_challenge
        // Note: For initiator, the key order is swapped compared to responder
        // because what we encrypt, they decrypt and vice versa
        let (enc_key, remaining) = session_keys
            .reference()
            .split::<AEAD_CANON_KEY_LEN, { AEAD_CANON_KEY_LEN * 2 }>();
        let (dec_key, att_challenge) = remaining.split::<AEAD_CANON_KEY_LEN, AEAD_CANON_KEY_LEN>();

        // Update session
        session.update(
            0,
            0,
            self.peer_sessid,
            self.local_sessid,
            peer_addr,
            SessionMode::Pase { fab_idx: 0 },
            Some(dec_key),
            Some(enc_key),
            Some(att_challenge),
        )?;

        // Complete the reserved session
        session.complete();

        // Acknowledge the final message
        exchange.acknowledge().await?;

        info!(
            "PASE session established: local_sessid={}, peer_sessid={}",
            self.local_sessid, self.peer_sessid
        );

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pbkdf_param_req_encoding() {
        // Basic test that the TLV encoding compiles and works
        let random = [0u8; 32];
        let req = PBKDFParamReq {
            initiator_random: OctetStr::new(&random),
            initiator_ssid: 1234,
            passcode_id: 0,
            has_params: false,
            session_parameters: None,
        };

        let mut buf = [0u8; 128];
        let mut wb = crate::utils::storage::WriteBuf::new(&mut buf);
        req.to_tlv(&TagType::Anonymous, &mut wb).unwrap();

        assert!(!wb.as_slice().is_empty());
    }

    #[test]
    fn test_pake1_encoding() {
        let pa = [0u8; 65];
        let pake1 = Pake1 {
            pa: OctetStr::new(&pa),
        };

        let mut buf = [0u8; 128];
        let mut wb = crate::utils::storage::WriteBuf::new(&mut buf);
        pake1.to_tlv(&TagType::Anonymous, &mut wb).unwrap();

        assert!(!wb.as_slice().is_empty());
    }

    #[test]
    fn test_pake3_encoding() {
        let ca = [0u8; 32];
        let pake3 = Pake3 {
            ca: OctetStr::new(&ca),
        };

        let mut buf = [0u8; 128];
        let mut wb = crate::utils::storage::WriteBuf::new(&mut buf);
        pake3.to_tlv(&TagType::Anonymous, &mut wb).unwrap();

        assert!(!wb.as_slice().is_empty());
    }
}