matter-kit 0.3.0

Matter (CSA smart-home standard) protocol implementation in Rust: no_std, no-alloc, executor-agnostic
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
//! Passcode-Authenticated Session Establishment (Core §4.14.1).
//!
//! How a commissioner that knows a device's printed passcode gets its first encrypted
//! session with it. Five messages, all on the **unsecured** session — there is no key yet —
//! and all inside one exchange:
//!
//! ```text
//!  commissioner (initiator)                        commissionee (responder)
//!    PBKDFParamRequest   ──────────────────────▶   picks a salt and iteration count
//!                        ◀──────────────────────   PBKDFParamResponse
//!    w0,w1 = PBKDF(passcode)
//!    Pake1 (pA)          ──────────────────────▶
//!                        ◀──────────────────────   Pake2 (pB, cB)
//!    check cB
//!    Pake3 (cA)          ──────────────────────▶   check cA
//!                        ◀──────────────────────   PakeFinished (StatusReport)
//!    I2RKey ‖ R2IKey ‖ AttestationChallenge = KDF(Ke)
//! ```
//!
//! # Sans-I/O
//!
//! Neither state machine touches a socket, a clock or a random generator. Randomness is a
//! parameter — 32 octets for the SPAKE2+ ephemeral, 32 for the protocol random — so a test
//! can replay a failure exactly, and the caller's one [`Rng`](crate::platform::Rng) is the
//! only source of entropy in the process.
//!
//! # What has actually been proved when it finishes
//!
//! That the peer knew the passcode. Nothing else. A PASE session has no fabric, no
//! operational identity, and no access beyond what commissioning needs — which is why
//! [`SessionKind::Pase`](crate::session::SessionKind::Pase) is a distinct variant rather
//! than a flag on a CASE session.

use crate::crypto::{
    Confirmation, HASH_LEN_BYTES, PUBLIC_KEY_SIZE_BYTES, Spake2pProver, Spake2pVerifier,
    Spake2pVerifierData, context as spake_context, ct_eq,
};
use crate::error::{Error, ErrorCode, Result, bail};
use crate::msg::SessionId;
use crate::session::EstablishedKeys;
use crate::tlv::{ContainerKind, Tag, TlvReader, TlvWriter, Value, set_once};

use super::SessionParams;
use super::status::{SecureChannelCode, StatusReport};

/// The length of the protocol randoms (§4.14.1.2).
pub const RANDOM_LEN: usize = 32;

/// "A value of 0 for the passcodeID SHALL correspond to the PAKE passcode verifier for the
/// currently-open commissioning window … Non-zero values are reserved for future use."
pub const PASSCODE_ID_COMMISSIONING: u16 = 0;

/// The PBKDF salt and iteration count (`Crypto_PBKDFParameterSet`, §3.9).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PbkdfParameters {
    /// `CRYPTO_PBKDF_ITERATIONS_MIN <= iterations <= CRYPTO_PBKDF_ITERATIONS_MAX`.
    pub iterations: u32,
    /// "A random value per device of at least 16 bytes and at most 32 bytes".
    pub salt: heapless::Vec<u8, { crate::crypto::PBKDF_SALT_MAX_BYTES }>,
}

impl PbkdfParameters {
    /// Builds a parameter set, checking both constraints of §3.9.
    ///
    /// These arrive from a peer and go straight into a PBKDF2 that the device then spends
    /// real time on, so the bounds are enforced here rather than left to the primitive.
    pub fn new(iterations: u32, salt: &[u8]) -> Result<Self> {
        if !(crate::crypto::PBKDF_ITERATIONS_MIN..=crate::crypto::PBKDF_ITERATIONS_MAX)
            .contains(&iterations)
        {
            bail!(InvalidArgument)
        }
        let Ok(salt) = heapless::Vec::from_slice(salt) else {
            bail!(InvalidArgument)
        };
        if salt.len() < crate::crypto::PBKDF_SALT_MIN_BYTES {
            bail!(InvalidArgument)
        }
        Ok(Self { iterations, salt })
    }
}

/// `pbkdfparamreq-struct` (§4.14.1.2).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PbkdfParamRequest {
    /// `InitiatorRandom` — 32 octets from a DRBG.
    pub initiator_random: [u8; RANDOM_LEN],
    /// The session id the initiator will listen on.
    pub initiator_session_id: SessionId,
    /// Which verifier to use; always [`PASSCODE_ID_COMMISSIONING`] today.
    pub passcode_id: u16,
    /// Whether the initiator already knows the salt and iteration count — from a QR code,
    /// typically. When true, "the responder SHALL NOT return the PBKDF parameters".
    pub has_pbkdf_parameters: bool,
    /// The initiator's MRP parameters, if it sent them.
    pub session_params: Option<SessionParams>,
}

impl PbkdfParamRequest {
    /// Writes the TLV encoding, "with an anonymous tag for the outermost struct".
    pub fn encode(&self, out: &mut [u8]) -> Result<usize> {
        let mut w = TlvWriter::new(out);
        w.start_structure(Tag::Anonymous)?;
        w.octets(Tag::Context(1), &self.initiator_random)?;
        w.unsigned(Tag::Context(2), u64::from(self.initiator_session_id.0))?;
        w.unsigned(Tag::Context(3), u64::from(self.passcode_id))?;
        w.bool(Tag::Context(4), self.has_pbkdf_parameters)?;
        if let Some(params) = &self.session_params {
            params.encode(&mut w, Tag::Context(5))?;
        }
        w.end_container()?;
        Ok(w.finish()?.len())
    }

    /// Reads the TLV encoding.
    pub fn decode(buf: &[u8]) -> Result<Self> {
        let mut fields = Fields::new(buf)?;
        let mut out = Self {
            initiator_random: [0; RANDOM_LEN],
            initiator_session_id: SessionId(0),
            passcode_id: PASSCODE_ID_COMMISSIONING,
            has_pbkdf_parameters: false,
            session_params: None,
        };
        let mut seen_random = false;
        while let Some((tag, element)) = fields.next()? {
            match tag {
                1 => {
                    let bytes = element.octets()?;
                    let Ok(arr) = <[u8; RANDOM_LEN]>::try_from(bytes) else {
                        bail!(InvalidArgument)
                    };
                    out.initiator_random = arr;
                    seen_random = true;
                }
                2 => out.initiator_session_id = SessionId(u16_of(element.unsigned()?)?),
                3 => out.passcode_id = u16_of(element.unsigned()?)?,
                4 => out.has_pbkdf_parameters = element.bool()?,
                5 => out.session_params = Some(SessionParams::decode(&mut fields)?),
                // "any context-specific tags not listed in the associated TLV schemas
                // SHALL be reserved for future use, and SHALL be silently ignored".
                _ => fields.skip(&element)?,
            }
        }
        if !seen_random {
            bail!(TlvNotFound)
        }
        Ok(out)
    }
}

/// `pbkdfparamresp-struct` (§4.14.1.2).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PbkdfParamResponse {
    /// Echoed from the request, which is what binds the two messages together.
    pub initiator_random: [u8; RANDOM_LEN],
    /// `ResponderRandom` — 32 octets from a DRBG.
    pub responder_random: [u8; RANDOM_LEN],
    /// The session id the responder will listen on.
    pub responder_session_id: SessionId,
    /// Absent when the request said the initiator already had them.
    pub pbkdf_parameters: Option<PbkdfParameters>,
    /// The responder's MRP parameters, if it sent them.
    pub session_params: Option<SessionParams>,
}

impl PbkdfParamResponse {
    /// Writes the TLV encoding.
    pub fn encode(&self, out: &mut [u8]) -> Result<usize> {
        let mut w = TlvWriter::new(out);
        w.start_structure(Tag::Anonymous)?;
        w.octets(Tag::Context(1), &self.initiator_random)?;
        w.octets(Tag::Context(2), &self.responder_random)?;
        w.unsigned(Tag::Context(3), u64::from(self.responder_session_id.0))?;
        // The parameter set is always present; it is *empty* when the initiator already
        // knows the values, which is what "SHALL NOT include the PBKDF parameters" means.
        w.start_structure(Tag::Context(4))?;
        if let Some(params) = &self.pbkdf_parameters {
            w.unsigned(Tag::Context(1), u64::from(params.iterations))?;
            w.octets(Tag::Context(2), &params.salt)?;
        }
        w.end_container()?;
        if let Some(params) = &self.session_params {
            params.encode(&mut w, Tag::Context(5))?;
        }
        w.end_container()?;
        Ok(w.finish()?.len())
    }

    /// Reads the TLV encoding.
    pub fn decode(buf: &[u8]) -> Result<Self> {
        let mut fields = Fields::new(buf)?;
        let mut out = Self {
            initiator_random: [0; RANDOM_LEN],
            responder_random: [0; RANDOM_LEN],
            responder_session_id: SessionId(0),
            pbkdf_parameters: None,
            session_params: None,
        };
        while let Some((tag, element)) = fields.next()? {
            match tag {
                1 => out.initiator_random = random_of(&element)?,
                2 => out.responder_random = random_of(&element)?,
                3 => out.responder_session_id = SessionId(u16_of(element.unsigned()?)?),
                4 => out.pbkdf_parameters = decode_pbkdf_parameters(&mut fields, &element)?,
                5 => out.session_params = Some(SessionParams::decode(&mut fields)?),
                _ => fields.skip(&element)?,
            }
        }
        Ok(out)
    }
}

fn decode_pbkdf_parameters(
    fields: &mut Fields<'_>,
    element: &crate::tlv::Element<'_>,
) -> Result<Option<PbkdfParameters>> {
    if element.value.container() != Some(ContainerKind::Structure) {
        bail!(TlvWrongType)
    }
    // `Crypto_PBKDFParameterSet` is tag 4 of a `pbkdfparamresp-struct` and
    // `responderSessionParams` is tag 5, so this nested decode must stop at its own
    // end-of-container: without `nested`, it would consume the session parameters that
    // follow it and the responder's MRP announcement would vanish.
    let (iterations, salt) = fields.nested(|fields| {
        let mut iterations = None;
        let mut salt: Option<heapless::Vec<u8, { crate::crypto::PBKDF_SALT_MAX_BYTES }>> = None;
        while let Some((tag, inner)) = fields.next()? {
            match tag {
                1 => set_once(&mut iterations, u32_of(inner.unsigned()?)?)?,
                2 => {
                    let Ok(v) = heapless::Vec::from_slice(inner.octets()?) else {
                        bail!(InvalidArgument)
                    };
                    set_once(&mut salt, v)?;
                }
                _ => fields.skip(&inner)?,
            }
        }
        Ok((iterations, salt))
    })?;
    match (iterations, salt) {
        // Both present: a real parameter set, checked against §3.9's bounds.
        (Some(iterations), Some(salt)) => Ok(Some(PbkdfParameters::new(iterations, &salt)?)),
        // Neither: the empty set the responder sends when the initiator already knows them.
        (None, None) => Ok(None),
        // One without the other is malformed; guessing the missing half would mean
        // running a PBKDF with a value nobody agreed on.
        _ => Err(Error::new(ErrorCode::InvalidArgument)),
    }
}

/// `pake-1-struct` — the initiator's SPAKE2+ share.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pake1 {
    /// `pA`.
    pub pa: [u8; PUBLIC_KEY_SIZE_BYTES],
}

/// `pake-2-struct` — the responder's share and its confirmation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pake2 {
    /// `pB`.
    pub pb: [u8; PUBLIC_KEY_SIZE_BYTES],
    /// `cB`.
    pub cb: Confirmation,
}

/// `pake-3-struct` — the initiator's confirmation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pake3 {
    /// `cA`.
    pub ca: Confirmation,
}

impl Pake1 {
    /// Writes the TLV encoding.
    pub fn encode(&self, out: &mut [u8]) -> Result<usize> {
        let mut w = TlvWriter::new(out);
        w.start_structure(Tag::Anonymous)?;
        w.octets(Tag::Context(1), &self.pa)?;
        w.end_container()?;
        Ok(w.finish()?.len())
    }

    /// Reads the TLV encoding.
    pub fn decode(buf: &[u8]) -> Result<Self> {
        let mut fields = Fields::new(buf)?;
        let mut pa = None;
        while let Some((tag, element)) = fields.next()? {
            match tag {
                1 => pa = Some(point_of(&element)?),
                _ => fields.skip(&element)?,
            }
        }
        Ok(Self {
            pa: pa.ok_or(Error::new(ErrorCode::TlvNotFound))?,
        })
    }
}

impl Pake2 {
    /// Writes the TLV encoding.
    pub fn encode(&self, out: &mut [u8]) -> Result<usize> {
        let mut w = TlvWriter::new(out);
        w.start_structure(Tag::Anonymous)?;
        w.octets(Tag::Context(1), &self.pb)?;
        w.octets(Tag::Context(2), &self.cb)?;
        w.end_container()?;
        Ok(w.finish()?.len())
    }

    /// Reads the TLV encoding.
    pub fn decode(buf: &[u8]) -> Result<Self> {
        let mut fields = Fields::new(buf)?;
        let (mut pb, mut cb) = (None, None);
        while let Some((tag, element)) = fields.next()? {
            match tag {
                1 => pb = Some(point_of(&element)?),
                2 => cb = Some(confirmation_of(&element)?),
                _ => fields.skip(&element)?,
            }
        }
        Ok(Self {
            pb: pb.ok_or(Error::new(ErrorCode::TlvNotFound))?,
            cb: cb.ok_or(Error::new(ErrorCode::TlvNotFound))?,
        })
    }
}

impl Pake3 {
    /// Writes the TLV encoding.
    pub fn encode(&self, out: &mut [u8]) -> Result<usize> {
        let mut w = TlvWriter::new(out);
        w.start_structure(Tag::Anonymous)?;
        w.octets(Tag::Context(1), &self.ca)?;
        w.end_container()?;
        Ok(w.finish()?.len())
    }

    /// Reads the TLV encoding.
    pub fn decode(buf: &[u8]) -> Result<Self> {
        let mut fields = Fields::new(buf)?;
        let mut ca = None;
        while let Some((tag, element)) = fields.next()? {
            match tag {
                1 => ca = Some(confirmation_of(&element)?),
                _ => fields.skip(&element)?,
            }
        }
        Ok(Self {
            ca: ca.ok_or(Error::new(ErrorCode::TlvNotFound))?,
        })
    }
}

// --- The state machines -------------------------------------------------------------

/// What the commissionee needs before it can answer at all.
#[derive(Debug, Clone)]
pub struct ResponderConfig {
    /// The stored `(w0, L)` for the open commissioning window.
    pub verifier: Spake2pVerifierData,
    /// The salt and iteration count that verifier was computed with.
    pub parameters: PbkdfParameters,
    /// This node's MRP parameters, to advertise.
    pub session_params: Option<SessionParams>,
}

/// The commissionee's side of PASE.
#[derive(Debug)]
pub struct PaseResponder {
    config: ResponderConfig,
    local_session_id: SessionId,
    peer_session_id: SessionId,
    /// The exact octets of the two parameter messages, which the transcript hashes.
    request_bytes: heapless::Vec<u8, 256>,
    response_bytes: heapless::Vec<u8, 256>,
    verifier: Option<Spake2pVerifier>,
    expected_ca: Option<Confirmation>,
    keys: Option<EstablishedKeys>,
    state: State,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    AwaitingRequest,
    AwaitingPake1,
    AwaitingPake3,
    Established,
    Failed,
}

impl PaseResponder {
    /// A commissionee ready to receive a `PBKDFParamRequest`.
    ///
    /// `local_session_id` must not collide with any live session
    /// ([`SessionTable::allocate_id`](crate::session::SessionTable::allocate_id)).
    #[must_use]
    pub fn new(config: ResponderConfig, local_session_id: SessionId) -> Self {
        Self {
            config,
            local_session_id,
            peer_session_id: SessionId(0),
            request_bytes: heapless::Vec::new(),
            response_bytes: heapless::Vec::new(),
            verifier: None,
            expected_ca: None,
            keys: None,
            state: State::AwaitingRequest,
        }
    }

    /// The session id this node will listen on.
    #[must_use]
    pub const fn local_session_id(&self) -> SessionId {
        self.local_session_id
    }

    /// The session id the peer will listen on, once it has said.
    #[must_use]
    pub const fn peer_session_id(&self) -> SessionId {
        self.peer_session_id
    }

    /// Handles `PBKDFParamRequest`, writing `PBKDFParamResponse` into `out`.
    ///
    /// `responder_random` must be 32 octets from a cryptographically secure generator.
    pub fn on_pbkdf_param_request(
        &mut self,
        payload: &[u8],
        responder_random: &[u8; RANDOM_LEN],
        out: &mut [u8],
    ) -> Result<usize> {
        if self.state != State::AwaitingRequest {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        let request = PbkdfParamRequest::decode(payload)?;

        // §4.14.1.2: "Verify passcodeID is set to 0."
        if request.passcode_id != PASSCODE_ID_COMMISSIONING {
            self.state = State::Failed;
            bail!(InvalidArgument)
        }
        self.peer_session_id = request.initiator_session_id;

        let response = PbkdfParamResponse {
            initiator_random: request.initiator_random,
            responder_random: *responder_random,
            responder_session_id: self.local_session_id,
            // "If hasPBKDFParameters is True the responder SHALL NOT include the PBKDF
            // parameters" — the initiator read them off the QR code.
            pbkdf_parameters: if request.has_pbkdf_parameters {
                None
            } else {
                Some(self.config.parameters.clone())
            },
            session_params: self.config.session_params,
        };
        let n = response.encode(out)?;

        // The transcript hashes these two messages verbatim, so they are kept as octets
        // rather than re-encoded later — a re-encoding that differed by one byte would
        // produce a context the peer does not share, and the failure would look like a
        // wrong passcode.
        self.request_bytes = copy_of(payload)?;
        self.response_bytes = copy_of(out.get(..n).unwrap_or(&[]))?;
        self.state = State::AwaitingPake1;
        Ok(n)
    }

    /// Handles `Pake1`, writing `Pake2` into `out`.
    ///
    /// `responder_ephemeral` must be 32 octets from a cryptographically secure generator —
    /// the SPAKE2+ scalar `y`.
    pub fn on_pake1(
        &mut self,
        payload: &[u8],
        responder_ephemeral: &[u8; 32],
        out: &mut [u8],
    ) -> Result<usize> {
        if self.state != State::AwaitingPake1 {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        let pake1 = Pake1::decode(payload)?;
        let verifier = Spake2pVerifier::new(&self.config.verifier, responder_ephemeral)?;

        let context = spake_context(&self.request_bytes, &self.response_bytes);
        let (ca, cb, ke) = verifier.finish(&pake1.pa, &context)?;

        let n = Pake2 {
            pb: *verifier.pb(),
            cb,
        }
        .encode(out)?;

        // The keys exist now, but nothing may use them until cA is checked: an attacker
        // who guessed wrong would otherwise get a session.
        self.keys = Some(EstablishedKeys::derive(ke.as_bytes(), &[])?);
        self.expected_ca = Some(ca);
        self.verifier = Some(verifier);
        self.state = State::AwaitingPake3;
        Ok(n)
    }

    /// Handles `Pake3`, writing `PakeFinished` into `out` and returning the session keys.
    ///
    /// A `cA` that does not match is [`ErrorCode::IntegrityCheckFailed`] and leaves the
    /// responder permanently failed — §4.14.1.2 says to "perform no further processing",
    /// and letting the peer try again on the same exchange would turn one guess into many.
    pub fn on_pake3(&mut self, payload: &[u8], out: &mut [u8]) -> Result<(usize, EstablishedKeys)> {
        if self.state != State::AwaitingPake3 {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        let pake3 = Pake3::decode(payload)?;
        let Some(expected) = self.expected_ca else {
            self.state = State::Failed;
            bail!(InvalidState)
        };
        if !ct_eq(&pake3.ca, &expected) {
            self.state = State::Failed;
            bail!(IntegrityCheckFailed)
        }
        let Some(keys) = self.keys.clone() else {
            self.state = State::Failed;
            bail!(InvalidState)
        };
        let n = StatusReport::secure_channel(SecureChannelCode::SessionEstablishmentSuccess)
            .encode(out)?;
        self.state = State::Established;
        Ok((n, keys))
    }

    /// Whether the exchange completed successfully.
    #[must_use]
    pub const fn is_established(&self) -> bool {
        matches!(self.state, State::Established)
    }

    /// Whether the exchange failed and must be abandoned.
    #[must_use]
    pub const fn is_failed(&self) -> bool {
        matches!(self.state, State::Failed)
    }
}

/// The commissioner's side of PASE.
#[derive(Debug)]
pub struct PaseInitiator {
    passcode: u32,
    local_session_id: SessionId,
    peer_session_id: SessionId,
    /// Parameters known up front, from a QR code.
    known_parameters: Option<PbkdfParameters>,
    session_params: Option<SessionParams>,
    /// Kept so the response's echo can be checked against it. Recovering it by re-parsing
    /// the request would work, but tying the check to a byte offset in an encoding is the
    /// sort of thing that survives until the encoding changes.
    initiator_random: [u8; RANDOM_LEN],
    request_bytes: heapless::Vec<u8, 256>,
    response_bytes: heapless::Vec<u8, 256>,
    prover: Option<Spake2pProver>,
    expected_cb: Option<Confirmation>,
    ca: Option<Confirmation>,
    keys: Option<EstablishedKeys>,
    state: State,
}

impl PaseInitiator {
    /// A commissioner that knows `passcode`.
    ///
    /// `known_parameters` is `Some` when the salt and iteration count came from the
    /// onboarding payload, which saves a round trip's worth of information but not a round
    /// trip: the request is sent either way.
    #[must_use]
    pub fn new(
        passcode: u32,
        local_session_id: SessionId,
        known_parameters: Option<PbkdfParameters>,
        session_params: Option<SessionParams>,
    ) -> Self {
        Self {
            passcode,
            local_session_id,
            peer_session_id: SessionId(0),
            known_parameters,
            session_params,
            initiator_random: [0; RANDOM_LEN],
            request_bytes: heapless::Vec::new(),
            response_bytes: heapless::Vec::new(),
            prover: None,
            expected_cb: None,
            ca: None,
            keys: None,
            state: State::AwaitingRequest,
        }
    }

    /// The session id this node will listen on.
    #[must_use]
    pub const fn local_session_id(&self) -> SessionId {
        self.local_session_id
    }

    /// The session id the peer will listen on, once it has said.
    #[must_use]
    pub const fn peer_session_id(&self) -> SessionId {
        self.peer_session_id
    }

    /// Writes the opening `PBKDFParamRequest`.
    ///
    /// `initiator_random` must be 32 octets from a cryptographically secure generator.
    pub fn start(&mut self, initiator_random: &[u8; RANDOM_LEN], out: &mut [u8]) -> Result<usize> {
        if self.state != State::AwaitingRequest {
            bail!(InvalidState)
        }
        let request = PbkdfParamRequest {
            initiator_random: *initiator_random,
            initiator_session_id: self.local_session_id,
            passcode_id: PASSCODE_ID_COMMISSIONING,
            has_pbkdf_parameters: self.known_parameters.is_some(),
            session_params: self.session_params,
        };
        let n = request.encode(out)?;
        self.initiator_random = *initiator_random;
        self.request_bytes = copy_of(out.get(..n).unwrap_or(&[]))?;
        self.state = State::AwaitingPake1;
        Ok(n)
    }

    /// Handles `PBKDFParamResponse`, writing `Pake1` into `out`.
    ///
    /// `initiator_ephemeral` must be 32 octets from a cryptographically secure generator —
    /// the SPAKE2+ scalar `x`. This is the call that runs PBKDF2, which on a constrained
    /// commissioner is the most expensive thing in the exchange.
    pub fn on_pbkdf_param_response(
        &mut self,
        payload: &[u8],
        initiator_ephemeral: &[u8; 32],
        out: &mut [u8],
    ) -> Result<usize> {
        if self.state != State::AwaitingPake1 {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        let response = PbkdfParamResponse::decode(payload)?;

        // The echoed random binds this response to this request. Without the check, an
        // off-path attacker's response would be accepted into the transcript.
        if !ct_eq(&response.initiator_random, &self.initiator_random) {
            self.state = State::Failed;
            bail!(InvalidArgument)
        }
        self.peer_session_id = response.responder_session_id;

        // Whichever side supplied them, exactly one set must be available.
        let parameters = match (&self.known_parameters, &response.pbkdf_parameters) {
            (Some(known), None) => known.clone(),
            (None, Some(sent)) => sent.clone(),
            (Some(known), Some(_)) => {
                // The responder sent parameters that were not asked for. Using the known
                // ones keeps the exchange going and keeps this node's own QR code
                // authoritative.
                known.clone()
            }
            (None, None) => {
                self.state = State::Failed;
                bail!(InvalidArgument)
            }
        };

        let prover = Spake2pProver::new(
            self.passcode,
            &parameters.salt,
            parameters.iterations,
            initiator_ephemeral,
        )?;
        let n = Pake1 { pa: *prover.pa() }.encode(out)?;

        self.response_bytes = copy_of(payload)?;
        self.prover = Some(prover);
        self.state = State::AwaitingPake3;
        Ok(n)
    }

    /// Handles `Pake2`, checking `cB` and writing `Pake3` into `out`.
    ///
    /// A `cB` that does not match means the responder did not know the passcode — or is
    /// not the device the passcode belongs to. §4.14.1.2 says to stop, and this does.
    pub fn on_pake2(&mut self, payload: &[u8], out: &mut [u8]) -> Result<usize> {
        if self.state != State::AwaitingPake3 {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        let pake2 = Pake2::decode(payload)?;
        let Some(prover) = &self.prover else {
            self.state = State::Failed;
            bail!(InvalidState)
        };

        let context = spake_context(&self.request_bytes, &self.response_bytes);
        let (ca, cb, ke) = prover.finish(&pake2.pb, &context)?;

        if !ct_eq(&pake2.cb, &cb) {
            self.state = State::Failed;
            bail!(IntegrityCheckFailed)
        }

        let n = Pake3 { ca }.encode(out)?;
        self.keys = Some(EstablishedKeys::derive(ke.as_bytes(), &[])?);
        self.expected_cb = Some(cb);
        self.ca = Some(ca);
        self.state = State::Established;
        Ok(n)
    }

    /// Handles `PakeFinished`, returning the session keys.
    ///
    /// §4.14.1.2: "The initiator SHALL NOT send any encrypted application data until it
    /// receives PakeFinished from the responder" — which is why the keys are handed over
    /// here and not at [`PaseInitiator::on_pake2`].
    pub fn on_pake_finished(&mut self, payload: &[u8]) -> Result<EstablishedKeys> {
        if self.state != State::Established {
            bail!(InvalidState)
        }
        let report = StatusReport::decode(payload)?;
        if !report.is_success()
            || report.secure_channel_code() != Some(SecureChannelCode::SessionEstablishmentSuccess)
        {
            self.state = State::Failed;
            bail!(InvalidState)
        }
        self.keys.clone().ok_or(Error::new(ErrorCode::InvalidState))
    }

    /// Whether `Pake2` has been accepted.
    #[must_use]
    pub const fn is_established(&self) -> bool {
        matches!(self.state, State::Established)
    }

    /// Whether the exchange failed.
    #[must_use]
    pub const fn is_failed(&self) -> bool {
        matches!(self.state, State::Failed)
    }
}

// --- TLV helpers ----------------------------------------------------------------------

/// A cursor over the members of an anonymous outer structure.
///
/// Every PASE message is "the TLV-encoded … struct with an anonymous tag for the outermost
/// struct", so all five decoders want the same thing: iterate the context-tagged members,
/// skip the ones this revision does not know.
pub(crate) struct Fields<'a> {
    reader: TlvReader<'a>,
    depth: usize,
}

impl<'a> Fields<'a> {
    pub(crate) fn new(buf: &'a [u8]) -> Result<Self> {
        let mut reader = TlvReader::new(buf);
        let Some(first) = reader.next_element()? else {
            bail!(TlvTruncated)
        };
        if first.value.container() != Some(ContainerKind::Structure) {
            bail!(TlvWrongType)
        }
        Ok(Self { reader, depth: 1 })
    }

    /// The next member, or `None` at the end of the structure this cursor is reading.
    ///
    /// "The structure this cursor is reading" is the load-bearing part: [`Fields::nested`]
    /// moves it one level down, and without that a decoder for a nested structure would run
    /// past its own end-of-container and carry on consuming the *parent's* remaining
    /// members as though they were its own.
    pub(crate) fn next(&mut self) -> Result<Option<(u8, crate::tlv::Element<'a>)>> {
        let Some(element) = self.reader.next_element()? else {
            return Ok(None);
        };
        if matches!(element.value, Value::EndOfContainer) {
            if self.reader.depth() < self.depth {
                return Ok(None);
            }
            return self.next();
        }
        let Some(tag) = element.tag.context() else {
            // A member of a Matter structure always has a context tag; the reader has
            // already refused an anonymous one.
            bail!(TlvInvalidTag)
        };
        Ok(Some((tag, element)))
    }

    /// Reads a nested structure's members, stopping at *its* end rather than the parent's.
    ///
    /// The caller has just taken the element that opens the nested container, so the reader
    /// is one level deeper than this cursor thinks it is. `f` runs with the cursor moved to
    /// match, and the level is restored afterwards whether `f` succeeded or not — a scoped
    /// pair, because an `enter`/`leave` a caller could mismatch is the bug this exists to
    /// prevent.
    pub(crate) fn nested<T>(&mut self, f: impl FnOnce(&mut Self) -> Result<T>) -> Result<T> {
        self.depth = self.depth.saturating_add(1);
        let out = f(self);
        self.depth = self.depth.saturating_sub(1);
        out
    }

    /// Skips a member's value, including a whole container.
    pub(crate) fn skip(&mut self, element: &crate::tlv::Element<'a>) -> Result<()> {
        self.reader.skip_value(element)
    }
}

fn copy_of<const N: usize>(bytes: &[u8]) -> Result<heapless::Vec<u8, N>> {
    heapless::Vec::from_slice(bytes).map_err(|_| Error::new(ErrorCode::NoSpace))
}

fn u16_of(value: u64) -> Result<u16> {
    u16::try_from(value).map_err(|_| Error::new(ErrorCode::TlvOutOfRange))
}

fn u32_of(value: u64) -> Result<u32> {
    u32::try_from(value).map_err(|_| Error::new(ErrorCode::TlvOutOfRange))
}

fn random_of(element: &crate::tlv::Element<'_>) -> Result<[u8; RANDOM_LEN]> {
    <[u8; RANDOM_LEN]>::try_from(element.octets()?)
        .map_err(|_| Error::new(ErrorCode::InvalidArgument))
}

fn point_of(element: &crate::tlv::Element<'_>) -> Result<[u8; PUBLIC_KEY_SIZE_BYTES]> {
    <[u8; PUBLIC_KEY_SIZE_BYTES]>::try_from(element.octets()?)
        .map_err(|_| Error::new(ErrorCode::InvalidArgument))
}

fn confirmation_of(element: &crate::tlv::Element<'_>) -> Result<Confirmation> {
    <[u8; HASH_LEN_BYTES]>::try_from(element.octets()?)
        .map_err(|_| Error::new(ErrorCode::InvalidArgument))
}

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

    fn parameters() -> PbkdfParameters {
        PbkdfParameters::new(1_000, b"SPAKE2P Key Salt").expect("parameters")
    }

    fn round_trip<T, E, D>(value: &T, encode: E, decode: D)
    where
        T: core::fmt::Debug + PartialEq,
        E: Fn(&T, &mut [u8]) -> Result<usize>,
        D: Fn(&[u8]) -> Result<T>,
    {
        let mut buf = [0u8; 512];
        let n = encode(value, &mut buf).expect("encode");
        let decoded = decode(&buf[..n]).expect("decode");
        assert_eq!(&decoded, value);
        // And every truncation of it must be an error rather than a panic.
        for cut in 0..n {
            let _ = decode(&buf[..cut]);
        }
    }

    #[test]
    fn pbkdf_param_request_round_trips() {
        round_trip(
            &PbkdfParamRequest {
                initiator_random: [0xAB; RANDOM_LEN],
                initiator_session_id: SessionId(0x1234),
                passcode_id: PASSCODE_ID_COMMISSIONING,
                has_pbkdf_parameters: true,
                session_params: None,
            },
            |v, b| v.encode(b),
            PbkdfParamRequest::decode,
        );
        round_trip(
            &PbkdfParamRequest {
                initiator_random: [0x01; RANDOM_LEN],
                initiator_session_id: SessionId(1),
                passcode_id: PASSCODE_ID_COMMISSIONING,
                has_pbkdf_parameters: false,
                session_params: Some(SessionParams::announce(
                    &crate::exchange::MrpParams::default(),
                )),
            },
            |v, b| v.encode(b),
            PbkdfParamRequest::decode,
        );
    }

    #[test]
    fn pbkdf_param_response_round_trips_with_and_without_parameters() {
        // With: the manual-pairing-code path, where the device must tell the commissioner.
        round_trip(
            &PbkdfParamResponse {
                initiator_random: [0x11; RANDOM_LEN],
                responder_random: [0x22; RANDOM_LEN],
                responder_session_id: SessionId(7),
                pbkdf_parameters: Some(parameters()),
                session_params: None,
            },
            |v, b| v.encode(b),
            PbkdfParamResponse::decode,
        );
        // Without: the QR-code path, where §4.14.1.2 says the responder "SHALL NOT return
        // the PBKDF parameters" — an *empty* parameter set, not an absent field.
        round_trip(
            &PbkdfParamResponse {
                initiator_random: [0x11; RANDOM_LEN],
                responder_random: [0x22; RANDOM_LEN],
                responder_session_id: SessionId(7),
                pbkdf_parameters: None,
                session_params: None,
            },
            |v, b| v.encode(b),
            PbkdfParamResponse::decode,
        );
    }

    #[test]
    fn the_pake_messages_round_trip() {
        round_trip(
            &Pake1 {
                pa: [0x04; PUBLIC_KEY_SIZE_BYTES],
            },
            |v, b| v.encode(b),
            Pake1::decode,
        );
        round_trip(
            &Pake2 {
                pb: [0x04; PUBLIC_KEY_SIZE_BYTES],
                cb: [0x77; HASH_LEN_BYTES],
            },
            |v, b| v.encode(b),
            Pake2::decode,
        );
        round_trip(
            &Pake3 {
                ca: [0x88; HASH_LEN_BYTES],
            },
            |v, b| v.encode(b),
            Pake3::decode,
        );
    }

    #[test]
    fn unknown_context_tags_are_skipped() {
        // §4.14.1.2: "any context-specific tags not listed in the associated TLV schemas
        // SHALL be reserved for future use, and SHALL be silently ignored if seen by a
        // recipient which cannot understand them." A node that refused them could not
        // talk to a newer peer.
        let mut buf = [0u8; 256];
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).expect("open");
        w.octets(Tag::Context(1), &[0xAB; RANDOM_LEN])
            .expect("random");
        w.unsigned(Tag::Context(2), 9).expect("session");
        w.unsigned(Tag::Context(3), 0).expect("passcode id");
        w.bool(Tag::Context(4), true).expect("flag");
        // A future scalar…
        w.unsigned(Tag::Context(99), 1234).expect("unknown scalar");
        // …and a future container, which has to be skipped whole.
        w.start_structure(Tag::Context(100)).expect("open");
        w.unsigned(Tag::Context(1), 1).expect("inner");
        w.start_array(Tag::Context(2)).expect("array");
        w.unsigned(Tag::Anonymous, 2).expect("item");
        w.end_container().expect("close array");
        w.end_container().expect("close struct");
        w.end_container().expect("close");
        let n = w.finish().expect("finish").len();

        let decoded = PbkdfParamRequest::decode(&buf[..n]).expect("decode");
        assert_eq!(decoded.initiator_session_id, SessionId(9));
        assert!(decoded.has_pbkdf_parameters);
    }

    #[test]
    fn a_missing_mandatory_field_is_refused() {
        let mut buf = [0u8; 64];
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).expect("open");
        w.unsigned(Tag::Context(2), 1).expect("session");
        w.end_container().expect("close");
        let n = w.finish().expect("finish").len();
        assert_eq!(
            PbkdfParamRequest::decode(&buf[..n]).unwrap_err().code(),
            ErrorCode::TlvNotFound
        );
    }

    #[test]
    fn pbkdf_parameters_enforce_section_3_9() {
        // Below the minimum iteration count, above the maximum, and a salt outside
        // 16..=32 octets are all things a peer can send and a device must refuse.
        assert!(PbkdfParameters::new(999, b"0123456789abcdef").is_err());
        assert!(PbkdfParameters::new(100_001, b"0123456789abcdef").is_err());
        assert!(PbkdfParameters::new(1_000, b"tooshort").is_err());
        assert!(PbkdfParameters::new(1_000, &[0u8; 33]).is_err());
        assert!(PbkdfParameters::new(1_000, &[0u8; 16]).is_ok());
        assert!(PbkdfParameters::new(100_000, &[0u8; 32]).is_ok());
    }

    #[test]
    fn half_a_parameter_set_is_refused() {
        // Iterations without a salt, or the other way round: guessing the missing half
        // would mean running a PBKDF with a value nobody agreed on.
        for (iterations, salt) in [(Some(1_000u32), None), (None, Some(&[0u8; 16]))] {
            let mut buf = [0u8; 256];
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).expect("open");
            w.octets(Tag::Context(1), &[0x11; RANDOM_LEN]).expect("r1");
            w.octets(Tag::Context(2), &[0x22; RANDOM_LEN]).expect("r2");
            w.unsigned(Tag::Context(3), 1).expect("session");
            w.start_structure(Tag::Context(4)).expect("params");
            if let Some(i) = iterations {
                w.unsigned(Tag::Context(1), u64::from(i)).expect("iters");
            }
            if let Some(s) = salt {
                w.octets(Tag::Context(2), s).expect("salt");
            }
            w.end_container().expect("close params");
            w.end_container().expect("close");
            let n = w.finish().expect("finish").len();
            assert!(
                PbkdfParamResponse::decode(&buf[..n]).is_err(),
                "iterations={iterations:?} salt={:?}",
                salt.map(|s| s.len())
            );
        }
    }

    #[test]
    fn a_nonzero_passcode_id_is_refused() {
        // §4.14.1.2: "Verify passcodeID is set to 0."
        let verifier = Spake2pVerifierData::from_passcode(20_202_021, b"SPAKE2P Key Salt", 1_000)
            .expect("verifier");
        let mut device = PaseResponder::new(
            ResponderConfig {
                verifier,
                parameters: parameters(),
                session_params: None,
            },
            SessionId(1),
        );

        let mut buf = [0u8; 256];
        let n = PbkdfParamRequest {
            initiator_random: [0; RANDOM_LEN],
            initiator_session_id: SessionId(2),
            passcode_id: 1,
            has_pbkdf_parameters: true,
            session_params: None,
        }
        .encode(&mut buf)
        .expect("encode");

        let mut out = [0u8; 256];
        assert_eq!(
            device
                .on_pbkdf_param_request(&buf[..n], &[0; RANDOM_LEN], &mut out)
                .unwrap_err()
                .code(),
            ErrorCode::InvalidArgument
        );
        assert!(device.is_failed());
    }

    #[test]
    fn messages_must_arrive_in_order() {
        // A peer that sends Pake1 before the parameters have been agreed has nothing to
        // build a transcript from.
        let verifier = Spake2pVerifierData::from_passcode(20_202_021, b"SPAKE2P Key Salt", 1_000)
            .expect("verifier");
        let mut device = PaseResponder::new(
            ResponderConfig {
                verifier,
                parameters: parameters(),
                session_params: None,
            },
            SessionId(1),
        );
        let mut out = [0u8; 256];
        let mut pake1 = [0u8; 256];
        let n = Pake1 {
            pa: [0x04; PUBLIC_KEY_SIZE_BYTES],
        }
        .encode(&mut pake1)
        .expect("encode");
        assert_eq!(
            device
                .on_pake1(&pake1[..n], &[1; 32], &mut out)
                .unwrap_err()
                .code(),
            ErrorCode::InvalidState
        );
    }

    #[test]
    fn session_params_clamp_what_a_peer_sends() {
        // These decide how long this node waits before retransmitting, and they come from
        // a stranger.
        let hostile = SessionParams {
            idle_interval_ms: u32::MAX,
            active_interval_ms: u32::MAX,
            active_threshold_ms: u16::MAX,
            ..SessionParams::legacy_peer()
        };
        let mrp = hostile.to_mrp();
        assert_eq!(mrp.idle_interval.as_millis(), 3_600_000);
        assert_eq!(mrp.active_interval.as_millis(), 3_600_000);
        assert_eq!(mrp.active_threshold.as_millis(), 65_535);
    }

    #[test]
    fn absent_session_params_take_the_defaults() {
        // A peer that sends nothing is a peer at Table 23's defaults, which are exactly the
        // MRP defaults — and, for the revisions, the 1.0-era floors rather than this node's.
        let peer = SessionParams::legacy_peer();
        assert_eq!(peer.to_mrp(), crate::exchange::MrpParams::default());
        assert_eq!(peer.data_model_revision, 16);
        assert_eq!(peer.interaction_model_revision, 10);
        assert_eq!(peer.max_paths_per_invoke, 1);
        assert!(peer.supported_transports.is_empty());
    }
}

#[cfg(test)]
mod nesting_tests {
    use super::*;
    use crate::sc::SessionParams;

    #[test]
    fn a_pbkdf_param_response_keeps_the_session_params_after_its_nested_parameter_set() {
        // `pbkdf_parameters` is tag 4 and `responderSessionParams` is tag 5, so the nested
        // decode has to stop at its own end-of-container. Without that the responder's MRP
        // announcement is eaten and both ends silently fall back to the default timings —
        // a wrong retransmission schedule, with no error raised anywhere.
        let parameters = PbkdfParameters::new(1_000, b"SPAKE2P Key Salt").expect("parameters");
        let response = PbkdfParamResponse {
            initiator_random: [1; RANDOM_LEN],
            responder_random: [2; RANDOM_LEN],
            responder_session_id: SessionId(0x4321),
            pbkdf_parameters: Some(parameters.clone()),
            session_params: Some(SessionParams {
                idle_interval_ms: 1234,
                active_interval_ms: 567,
                active_threshold_ms: 8901,
                ..SessionParams::legacy_peer()
            }),
        };

        let mut buf = [0u8; 256];
        let len = response.encode(&mut buf).expect("encode");
        let mut round = [0u8; 256];
        round[..len].copy_from_slice(buf.get(..len).expect("in range"));
        let decoded =
            PbkdfParamResponse::decode(round.get(..len).expect("in range")).expect("decode");

        assert_eq!(decoded.pbkdf_parameters, response.pbkdf_parameters);
        assert_eq!(
            decoded.session_params, response.session_params,
            "the session params come after a nested structure"
        );
        assert_eq!(decoded.responder_session_id, SessionId(0x4321));
    }

    #[test]
    fn a_duplicate_tag_inside_the_parameter_set_is_refused() {
        // §A.5.1 applies to a nested structure as much as to the outer one.
        let mut buf = [0u8; 256];
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).expect("start");
        w.octets(Tag::Context(1), &[1u8; RANDOM_LEN])
            .expect("random");
        w.octets(Tag::Context(2), &[2u8; RANDOM_LEN])
            .expect("random");
        w.unsigned(Tag::Context(3), 1).expect("session id");
        w.start_structure(Tag::Context(4)).expect("parameters");
        w.unsigned(Tag::Context(1), 1_000).expect("iterations");
        w.unsigned(Tag::Context(1), 2_000)
            .expect("a second iterations");
        w.octets(Tag::Context(2), b"SPAKE2P Key Salt")
            .expect("salt");
        w.end_container().expect("end parameters");
        w.end_container().expect("end");
        let len = w.finish().expect("finish").len();

        let mut round = [0u8; 256];
        round[..len].copy_from_slice(buf.get(..len).expect("in range"));
        assert_eq!(
            PbkdfParamResponse::decode(round.get(..len).expect("in range"))
                .map(|_| ())
                .unwrap_err()
                .code(),
            ErrorCode::TlvDuplicateTag
        );
    }
}