matter-crypto 0.2.0

Matter protocol session establishment: PASE (SPAKE2+) and CASE (SIGMA).
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
//! TLV codec for the 5 PASE messages (Matter Core Spec §4.14.1.2).
//!
//! Each message is an anonymous TLV structure with context-tagged fields.
//! Decoders return strongly-typed structs; encoders accept the struct
//! and emit `Vec<u8>` bytes for the wire.
//!
//! # Tag numbers (pinned from matter.js `PaseMessages.ts`, 2026-05-19)
//!
//! ```text
//! Message               Field                     Context tag
//! ──────────────────────────────────────────────────────────
//! PbkdfParamRequest     initiator_random          1
//! PbkdfParamRequest     initiator_session_id      2
//! PbkdfParamRequest     passcode_id               3
//! PbkdfParamRequest     has_pbkdf_parameters      4
//! PbkdfParamRequest     initiator_session_params  5 (opt)
//! PbkdfParamResponse    initiator_random          1
//! PbkdfParamResponse    responder_random          2
//! PbkdfParamResponse    responder_session_id      3
//! PbkdfParamResponse    pbkdf_parameters          4 (opt)
//! PbkdfParamResponse    responder_session_params  5 (opt)
//! PbkdfParamsInner      iterations                1
//! PbkdfParamsInner      salt                      2
//! Pake1                 x                         1
//! Pake2                 y                         1
//! Pake2                 verifier                  2
//! Pake3                 verifier                  1
//! ```
//!
//! # `SessionParams` (M3 pass-through)
//!
//! `SessionParams.raw_tlv` holds the raw TLV bytes of the optional
//! `SessionParameters` substructure, including the container-start control
//! byte through the end-of-container byte (`0x18`) inclusive. M3 does not
//! interpret the fields inside; M6 commissioning will decode them.
//!
//! For reference, matter.js (from `TlvSessionParameters` in `PaseMessages.ts`)
//! populates: `idleInterval`(1), `activeInterval`(2), `activeThreshold`(3),
//! `dataModelRevision`(4), `interactionModelRevision`(5), `specificationVersion`(6),
//! `maxPathsPerInvoke`(7), `supportedTransports`(8), `maxTcpMessageSize`(9).
//!
//! # Dead-code allowance
//!
//! All items here are `pub(crate)` and consumed by M3.2 Task 2 (state
//! machines) which lands in the same PR series. The allow below suppresses
//! the dead-code lint that fires while only this file is committed.
#![allow(dead_code)]

use matter_codec::{ContainerKind, Element, Tag, TlvReader, TlvWriter, Value};

use crate::error::{Error, Result};

// ---------------------------------------------------------------------------
// Byte-length constants, pinned from matter.js CryptoConstants.ts:
//   CRYPTO_GROUP_SIZE_BYTES = 32
//   CRYPTO_PUBLIC_KEY_SIZE_BYTES = 2 * 32 + 1 = 65  (uncompressed P-256 point)
//   CRYPTO_HASH_LEN_BYTES = 32
// ---------------------------------------------------------------------------

/// Byte length of an uncompressed P-256 public key (0x04 prefix + 32 + 32).
const POINT_LEN: usize = 65;

/// Byte length of a SPAKE2+ confirmation tag (SHA-256 output).
const HASH_LEN: usize = 32;

/// Byte length of the per-session random nonce fields.
const RANDOM_LEN: usize = 32;

/// PBKDF salt minimum length per Matter spec §3.10.3.
const SALT_MIN_LEN: usize = 16;

/// PBKDF salt maximum length per Matter spec §3.10.3.
const SALT_MAX_LEN: usize = 32;

// ---------------------------------------------------------------------------
// TLV end-of-container byte (Matter Core Spec §A.2, element type 0x18).
// This is the raw byte pushed to close a container when the `TlvWriter`
// borrow cannot be held (e.g., because we need to push raw optional bytes
// into the same buffer).
// ---------------------------------------------------------------------------
const END_CONTAINER_BYTE: u8 = 0x18;

// ---------------------------------------------------------------------------
// Helper: advance past the outer anonymous structure start.
// ---------------------------------------------------------------------------

/// Advance `reader` past the anonymous-structure opening element.
///
/// Returns `Error::InvalidParameter` if the first element is not an
/// anonymous TLV structure start.
fn expect_anon_struct_start(reader: &mut TlvReader<'_>) -> Result<()> {
    match reader.next()? {
        Some(Element::ContainerStart {
            tag: Tag::Anonymous,
            kind: ContainerKind::Structure,
        }) => Ok(()),
        _ => Err(Error::InvalidParameter),
    }
}

// ---------------------------------------------------------------------------
// Helper: skip a sub-structure body and return its raw TLV bytes.
// ---------------------------------------------------------------------------

/// After a `ContainerStart` for a sub-structure has been consumed from
/// `full_message_reader`, skip the sub-structure body to the matching
/// `ContainerEnd`, then re-parse `full_message` to recover the raw bytes of
/// that sub-structure (container-start byte through end-of-container byte).
///
/// The re-parse uses [`TlvReader::read_value`] on a fresh reader positioned
/// just before the last context-tagged sub-structure in `full_message`, then
/// re-encodes the value tree via [`TlvWriter::write_value`]. This preserves
/// the TLV encoding faithfully for the values that matter.js actually puts in
/// `SessionParams` (all scalars — uints and bitmaps), which have deterministic
/// minimal encoding.
///
/// # Errors
///
/// - [`Error::InvalidParameter`] if the sub-structure is not properly closed,
///   or no context-tagged sub-structure is found when re-parsing.
/// - [`Error::Codec`] on malformed TLV.
fn skip_and_capture_substructure(
    full_message: &[u8],
    main_reader: &mut TlvReader<'_>,
    context_tag: u8,
) -> Result<Vec<u8>> {
    // Skip the body of the already-opened sub-structure in `main_reader`.
    // Track depth so nested structures (possible inside SessionParams) are
    // handled without prematurely stopping.
    let mut depth: usize = 1;
    loop {
        match main_reader.next()? {
            None => return Err(Error::InvalidParameter),
            Some(Element::ContainerStart { .. }) => depth += 1,
            Some(Element::ContainerEnd) => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            // Scalars: ignore while skipping. The `_` arm is required because
            // `Element` is `#[non_exhaustive]`.
            Some(_) => {}
        }
    }

    // Re-parse `full_message` to capture the sub-element's raw bytes. We do a
    // fresh reader walk of the outer structure, use `read_value` for each
    // context-tagged container element with the matching tag, and re-encode it.
    let mut reader = TlvReader::new(full_message);
    // Consume the outer anonymous structure start.
    match reader.next()? {
        Some(Element::ContainerStart {
            tag: Tag::Anonymous,
            kind: ContainerKind::Structure,
        }) => {}
        _ => return Err(Error::InvalidParameter),
    }

    loop {
        match reader.next()? {
            None | Some(Element::ContainerEnd) => {
                // Reached the end without finding the target sub-element.
                return Err(Error::InvalidParameter);
            }
            Some(Element::ContainerStart {
                tag: Tag::Context(t),
                kind: ContainerKind::Structure,
            }) if t == context_tag => {
                // Found it. Collect children into a Value tree, then re-encode.
                let child_value = collect_structure_body(&mut reader)?;
                let mut raw = Vec::new();
                let mut w = TlvWriter::new(&mut raw);
                w.write_value(Tag::Context(t), &child_value)?;
                return Ok(raw);
            }
            Some(Element::ContainerStart { .. }) => {
                // A different sub-structure; skip over it.
                skip_container_body(&mut reader)?;
            }
            // Scalars or unknown elements at the outer level: skip.
            // `_` arm required because `Element` is `#[non_exhaustive]`.
            Some(_) => {}
        }
    }
}

/// Collect the children of a structure that has already had its
/// `ContainerStart` consumed, returning a `Value::Structure(…)`.
///
/// # Errors
///
/// - [`Error::InvalidParameter`] if the body is not properly terminated.
/// - [`Error::Codec`] on malformed TLV.
fn collect_structure_body(reader: &mut TlvReader<'_>) -> Result<Value> {
    let mut members: Vec<(Tag, Value)> = Vec::new();
    loop {
        match reader.next()? {
            Some(Element::ContainerEnd) => break,
            Some(Element::Scalar { tag, value }) => {
                members.push((tag, value));
            }
            Some(Element::ContainerStart { tag, kind }) => {
                let inner = match kind {
                    ContainerKind::Structure => collect_structure_body(reader)?,
                    ContainerKind::Array => {
                        let elems = collect_array_body(reader)?;
                        Value::Array(elems)
                    }
                    ContainerKind::List => {
                        let inner_list = collect_list_body(reader)?;
                        Value::List(inner_list)
                    }
                    // `ContainerKind` is `#[non_exhaustive]`; reject unknown kinds.
                    _ => return Err(Error::InvalidParameter),
                };
                members.push((tag, inner));
            }
            // None (unexpected EOF) or unknown element type — both are invalid.
            // `Element` is `#[non_exhaustive]` so the `Some(_)` arm is needed.
            None | Some(_) => return Err(Error::InvalidParameter),
        }
    }
    Ok(Value::Structure(members))
}

/// Collect the elements of an array body (`ContainerStart` already consumed).
fn collect_array_body(reader: &mut TlvReader<'_>) -> Result<Vec<Value>> {
    let mut elems: Vec<Value> = Vec::new();
    loop {
        match reader.next()? {
            Some(Element::ContainerEnd) => break,
            Some(Element::Scalar { value, .. }) => elems.push(value),
            Some(Element::ContainerStart { kind, .. }) => {
                let inner = match kind {
                    ContainerKind::Structure => collect_structure_body(reader)?,
                    ContainerKind::Array => Value::Array(collect_array_body(reader)?),
                    ContainerKind::List => Value::List(collect_list_body(reader)?),
                    _ => return Err(Error::InvalidParameter),
                };
                elems.push(inner);
            }
            None | Some(_) => return Err(Error::InvalidParameter),
        }
    }
    Ok(elems)
}

/// Collect the members of a list body (`ContainerStart` already consumed).
fn collect_list_body(reader: &mut TlvReader<'_>) -> Result<Vec<(Tag, Value)>> {
    let mut members: Vec<(Tag, Value)> = Vec::new();
    loop {
        match reader.next()? {
            Some(Element::ContainerEnd) => break,
            Some(Element::Scalar { tag, value }) => members.push((tag, value)),
            Some(Element::ContainerStart { tag, kind }) => {
                let inner = match kind {
                    ContainerKind::Structure => collect_structure_body(reader)?,
                    ContainerKind::Array => Value::Array(collect_array_body(reader)?),
                    ContainerKind::List => Value::List(collect_list_body(reader)?),
                    _ => return Err(Error::InvalidParameter),
                };
                members.push((tag, inner));
            }
            None | Some(_) => return Err(Error::InvalidParameter),
        }
    }
    Ok(members)
}

/// Skip the body of an already-opened container (any kind) until its
/// matching `ContainerEnd` is consumed.
fn skip_container_body(reader: &mut TlvReader<'_>) -> Result<()> {
    let mut depth: usize = 1;
    loop {
        match reader.next()? {
            None => return Err(Error::InvalidParameter),
            Some(Element::ContainerStart { .. }) => depth += 1,
            Some(Element::ContainerEnd) => {
                depth -= 1;
                if depth == 0 {
                    return Ok(());
                }
            }
            Some(_) => {}
        }
    }
}

// ---------------------------------------------------------------------------
// SessionParams — opaque TLV bytes passthrough.
// ---------------------------------------------------------------------------

/// Opaque bytes representing one of the optional `SessionParameters`
/// substructures in [`PbkdfParamRequest`] or [`PbkdfParamResponse`].
///
/// M3 preserves the bytes byte-for-byte for round-trip fidelity without
/// interpreting the fields (idle/active intervals, data-model revision, …).
/// M6 commissioning will decode the contents when it needs them.
///
/// The bytes cover the entire sub-element: the container-start control byte
/// through the matching end-of-container byte (`0x18`) inclusive, including
/// the context tag byte that identifies this field within the parent structure.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct SessionParams {
    /// Raw TLV bytes of the `SessionParameters` sub-element, context-tag
    /// through end-container, as captured from the wire or built by the encoder.
    pub raw_tlv: Vec<u8>,
}

// ---------------------------------------------------------------------------
// PbkdfParamsInner — the nested struct inside PbkdfParamResponse tag 4.
// ---------------------------------------------------------------------------

/// The inner structure encoded at context tag 4 of [`PbkdfParamResponse`].
///
/// Contains the PBKDF2 parameters the responder commits to use when deriving
/// the SPAKE2+ w0/w1 scalars from the passcode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PbkdfParamsInner {
    /// PBKDF2 iteration count. Matter spec §3.10.3 requires ≥ 1000.
    pub iterations: u32,
    /// PBKDF2 salt. Matter spec §3.10.3 requires 16–32 bytes.
    pub salt: Vec<u8>,
}

impl PbkdfParamsInner {
    /// Encode as a context-tagged TLV structure using `outer_tag`.
    ///
    /// `outer_tag` is the context tag used in the *parent* structure (tag 4
    /// in [`PbkdfParamResponse`]). The resulting bytes are self-contained:
    /// container-start through end-of-container.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if the salt length is outside
    ///   \[`SALT_MIN_LEN`, `SALT_MAX_LEN`\].
    /// - [`Error::Codec`] on any codec error.
    pub(crate) fn encode(&self, outer_tag: Tag) -> Result<Vec<u8>> {
        if self.salt.len() < SALT_MIN_LEN || self.salt.len() > SALT_MAX_LEN {
            return Err(Error::InvalidParameter);
        }
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(outer_tag)?;
        // context tag 1: iterations (u32, widened to u64 for the API).
        w.put_uint(Tag::Context(1), u64::from(self.iterations))?;
        // context tag 2: salt bytes.
        w.put_bytes(Tag::Context(2), &self.salt)?;
        w.end_container()?;
        Ok(buf)
    }

    /// Decode the body of the inner structure from `reader`.
    ///
    /// The caller must have already consumed the `ContainerStart` for this
    /// structure. This function reads until the matching `ContainerEnd`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] on unrecognised context tag, missing
    ///   required field, wrong value type, or salt outside \[16, 32\] bytes.
    /// - [`Error::Codec`] on malformed TLV.
    fn decode_body(reader: &mut TlvReader<'_>) -> Result<Self> {
        let mut iterations: Option<u32> = None;
        let mut salt: Option<Vec<u8>> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Uint(v),
                }) => {
                    iterations = Some(u32::try_from(v).map_err(|_| Error::InvalidParameter)?);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(2),
                    value: Value::Bytes(b),
                }) => {
                    if b.len() < SALT_MIN_LEN || b.len() > SALT_MAX_LEN {
                        return Err(Error::InvalidParameter);
                    }
                    salt = Some(b);
                }

                // None (unexpected EOF) or any unrecognised element.
                // `Element` is `#[non_exhaustive]` so the `Some(_)` arm is needed.
                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            iterations: iterations.ok_or(Error::InvalidParameter)?,
            salt: salt.ok_or(Error::InvalidParameter)?,
        })
    }
}

// ---------------------------------------------------------------------------
// PbkdfParamRequest
// ---------------------------------------------------------------------------

/// The first PASE message: commissioner → device.
///
/// Sent by the commissioner (initiator) to open PASE negotiation and propose
/// session parameters.
///
/// Wire format: anonymous TLV structure, context tags 1–4, and optional tag 5.
///
/// # Matter spec reference
/// Matter Core Spec §4.14.1.2 (was §3.10.5 in pre-1.3 editions).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PbkdfParamRequest {
    /// 32-byte random nonce generated by the initiator.
    pub initiator_random: [u8; RANDOM_LEN],
    /// Session ID proposed by the initiator for this session.
    pub initiator_session_id: u16,
    /// Passcode ID. Controllers typically send 0 (the default passcode).
    pub passcode_id: u16,
    /// `true` if the initiator cached the responder's PBKDF parameters from a
    /// previous interaction (responder may omit them from its response).
    pub has_pbkdf_parameters: bool,
    /// Optional session-capability advertisement from the initiator.
    pub initiator_session_params: Option<SessionParams>,
}

impl PbkdfParamRequest {
    /// Encode this message as wire TLV bytes.
    ///
    /// # Errors
    ///
    /// Propagates any [`matter_codec::Error`] via [`Error::Codec`].
    pub(crate) fn encode(&self) -> Result<Vec<u8>> {
        // Build the fixed fields inside the writer's borrow, then drop the
        // writer so we can push optional raw bytes and the end-container byte.
        let mut buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous)?;
            w.put_bytes(Tag::Context(1), &self.initiator_random)?;
            w.put_uint(Tag::Context(2), u64::from(self.initiator_session_id))?;
            w.put_uint(Tag::Context(3), u64::from(self.passcode_id))?;
            w.put_bool(Tag::Context(4), self.has_pbkdf_parameters)?;
            // Writer is dropped here; buf is exclusively owned again.
        }
        // Append optional session params sub-element (already fully encoded
        // as a context-tagged TLV structure).
        if let Some(sp) = &self.initiator_session_params {
            buf.extend_from_slice(&sp.raw_tlv);
        }
        // Close the outer anonymous structure.
        buf.push(END_CONTAINER_BYTE);
        Ok(buf)
    }

    /// Decode wire TLV bytes into a `PbkdfParamRequest`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if a required field is absent, an
    ///   unexpected context tag is present, or a byte-string has the wrong
    ///   length.
    /// - [`Error::Codec`] if the TLV is malformed.
    pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
        let mut reader = TlvReader::new(bytes);
        expect_anon_struct_start(&mut reader)?;

        let mut initiator_random: Option<[u8; RANDOM_LEN]> = None;
        let mut initiator_session_id: Option<u16> = None;
        let mut passcode_id: Option<u16> = None;
        let mut has_pbkdf_parameters: Option<bool> = None;
        let mut initiator_session_params: Option<SessionParams> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; RANDOM_LEN] =
                        b.try_into().map_err(|_| Error::InvalidParameter)?;
                    initiator_random = Some(arr);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(2),
                    value: Value::Uint(v),
                }) => {
                    initiator_session_id =
                        Some(u16::try_from(v).map_err(|_| Error::InvalidParameter)?);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(3),
                    value: Value::Uint(v),
                }) => {
                    passcode_id = Some(u16::try_from(v).map_err(|_| Error::InvalidParameter)?);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(4),
                    value: Value::Bool(b),
                }) => {
                    has_pbkdf_parameters = Some(b);
                }

                Some(Element::ContainerStart {
                    tag: Tag::Context(5),
                    kind: ContainerKind::Structure,
                }) => {
                    // Recover raw bytes for byte-exact round-trip (tag 5).
                    let raw = skip_and_capture_substructure(bytes, &mut reader, 5)?;
                    initiator_session_params = Some(SessionParams { raw_tlv: raw });
                }

                // None (unexpected EOF) or any unrecognised element.
                // `Element` is `#[non_exhaustive]` so the `Some(_)` arm is needed.
                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            initiator_random: initiator_random.ok_or(Error::InvalidParameter)?,
            initiator_session_id: initiator_session_id.ok_or(Error::InvalidParameter)?,
            passcode_id: passcode_id.ok_or(Error::InvalidParameter)?,
            has_pbkdf_parameters: has_pbkdf_parameters.ok_or(Error::InvalidParameter)?,
            initiator_session_params,
        })
    }
}

// ---------------------------------------------------------------------------
// PbkdfParamResponse
// ---------------------------------------------------------------------------

/// The second PASE message: device → commissioner.
///
/// Carries the responder's random nonce, its chosen session ID, and optionally
/// the PBKDF parameters (omitted when the initiator indicated it had them
/// cached in [`PbkdfParamRequest::has_pbkdf_parameters`]).
///
/// Wire format: anonymous TLV structure, context tags 1–3, and optional tags
/// 4 and 5.
///
/// # Matter spec reference
/// Matter Core Spec §4.14.1.2.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PbkdfParamResponse {
    /// Echo of the initiator's random nonce (from [`PbkdfParamRequest`]).
    pub initiator_random: [u8; RANDOM_LEN],
    /// 32-byte random nonce generated by the responder.
    pub responder_random: [u8; RANDOM_LEN],
    /// Session ID chosen by the responder.
    pub responder_session_id: u16,
    /// PBKDF2 parameters. Omitted when the initiator indicated it had them cached.
    pub pbkdf_parameters: Option<PbkdfParamsInner>,
    /// Optional session-capability advertisement from the responder.
    pub responder_session_params: Option<SessionParams>,
}

impl PbkdfParamResponse {
    /// Encode this message as wire TLV bytes.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if `pbkdf_parameters` is present and
    ///   the salt length is outside \[16, 32\].
    /// - [`Error::Codec`] on any codec error.
    pub(crate) fn encode(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous)?;
            w.put_bytes(Tag::Context(1), &self.initiator_random)?;
            w.put_bytes(Tag::Context(2), &self.responder_random)?;
            w.put_uint(Tag::Context(3), u64::from(self.responder_session_id))?;
            // Writer dropped here.
        }
        // Append optional PBKDF params sub-element (context tag 4).
        if let Some(pbkdf) = &self.pbkdf_parameters {
            let inner_bytes = pbkdf.encode(Tag::Context(4))?;
            buf.extend_from_slice(&inner_bytes);
        }
        // Append optional session params sub-element (context tag 5).
        if let Some(sp) = &self.responder_session_params {
            buf.extend_from_slice(&sp.raw_tlv);
        }
        buf.push(END_CONTAINER_BYTE);
        Ok(buf)
    }

    /// Decode wire TLV bytes into a `PbkdfParamResponse`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if required fields are absent,
    ///   unexpected tags appear, or byte-string lengths are wrong.
    /// - [`Error::Codec`] on malformed TLV.
    pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
        let mut reader = TlvReader::new(bytes);
        expect_anon_struct_start(&mut reader)?;

        let mut initiator_random: Option<[u8; RANDOM_LEN]> = None;
        let mut responder_random: Option<[u8; RANDOM_LEN]> = None;
        let mut responder_session_id: Option<u16> = None;
        let mut pbkdf_parameters: Option<PbkdfParamsInner> = None;
        let mut responder_session_params: Option<SessionParams> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; RANDOM_LEN] =
                        b.try_into().map_err(|_| Error::InvalidParameter)?;
                    initiator_random = Some(arr);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(2),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; RANDOM_LEN] =
                        b.try_into().map_err(|_| Error::InvalidParameter)?;
                    responder_random = Some(arr);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(3),
                    value: Value::Uint(v),
                }) => {
                    responder_session_id =
                        Some(u16::try_from(v).map_err(|_| Error::InvalidParameter)?);
                }

                Some(Element::ContainerStart {
                    tag: Tag::Context(4),
                    kind: ContainerKind::Structure,
                }) => {
                    // ContainerStart consumed; decode the body directly.
                    let inner = PbkdfParamsInner::decode_body(&mut reader)?;
                    pbkdf_parameters = Some(inner);
                }

                Some(Element::ContainerStart {
                    tag: Tag::Context(5),
                    kind: ContainerKind::Structure,
                }) => {
                    let raw = skip_and_capture_substructure(bytes, &mut reader, 5)?;
                    responder_session_params = Some(SessionParams { raw_tlv: raw });
                }

                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            initiator_random: initiator_random.ok_or(Error::InvalidParameter)?,
            responder_random: responder_random.ok_or(Error::InvalidParameter)?,
            responder_session_id: responder_session_id.ok_or(Error::InvalidParameter)?,
            pbkdf_parameters,
            responder_session_params,
        })
    }
}

// ---------------------------------------------------------------------------
// Pake1
// ---------------------------------------------------------------------------

/// The third PASE message: commissioner → device.
///
/// Carries the commissioner's SPAKE2+ share `pA` (the X point).
///
/// Wire format: anonymous TLV structure, context tag 1.
///
/// # Matter spec reference
/// Matter Core Spec §4.14.1.2.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Pake1 {
    /// The commissioner's SPAKE2+ share `pA`. 65 bytes (uncompressed P-256).
    pub x: [u8; POINT_LEN],
}

impl Pake1 {
    /// Encode this message as wire TLV bytes.
    ///
    /// # Errors
    ///
    /// Propagates any [`matter_codec::Error`] via [`Error::Codec`].
    pub(crate) fn encode(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous)?;
        w.put_bytes(Tag::Context(1), &self.x)?;
        w.end_container()?;
        Ok(buf)
    }

    /// Decode wire TLV bytes into a `Pake1`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if `x` is absent, has the wrong length,
    ///   or an unexpected tag is present.
    /// - [`Error::Codec`] on malformed TLV.
    pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
        let mut reader = TlvReader::new(bytes);
        expect_anon_struct_start(&mut reader)?;

        let mut x: Option<[u8; POINT_LEN]> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; POINT_LEN] = b.try_into().map_err(|_| Error::InvalidParameter)?;
                    x = Some(arr);
                }

                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            x: x.ok_or(Error::InvalidParameter)?,
        })
    }
}

// ---------------------------------------------------------------------------
// Pake2
// ---------------------------------------------------------------------------

/// The fourth PASE message: device → commissioner.
///
/// Carries the device's SPAKE2+ share `pB` (the Y point) and its
/// confirmation tag `cB`.
///
/// Wire format: anonymous TLV structure, context tags 1–2.
///
/// # Matter spec reference
/// Matter Core Spec §4.14.1.2.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Pake2 {
    /// The device's SPAKE2+ share `pB`. 65 bytes (uncompressed P-256).
    pub y: [u8; POINT_LEN],
    /// The device's SPAKE2+ confirmation tag `cB`. 32 bytes (SHA-256).
    pub verifier: [u8; HASH_LEN],
}

impl Pake2 {
    /// Encode this message as wire TLV bytes.
    ///
    /// # Errors
    ///
    /// Propagates any [`matter_codec::Error`] via [`Error::Codec`].
    pub(crate) fn encode(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous)?;
        w.put_bytes(Tag::Context(1), &self.y)?;
        w.put_bytes(Tag::Context(2), &self.verifier)?;
        w.end_container()?;
        Ok(buf)
    }

    /// Decode wire TLV bytes into a `Pake2`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if a required field is absent, has the
    ///   wrong length, or an unexpected tag is present.
    /// - [`Error::Codec`] on malformed TLV.
    pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
        let mut reader = TlvReader::new(bytes);
        expect_anon_struct_start(&mut reader)?;

        let mut y: Option<[u8; POINT_LEN]> = None;
        let mut verifier: Option<[u8; HASH_LEN]> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; POINT_LEN] = b.try_into().map_err(|_| Error::InvalidParameter)?;
                    y = Some(arr);
                }

                Some(Element::Scalar {
                    tag: Tag::Context(2),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; HASH_LEN] = b.try_into().map_err(|_| Error::InvalidParameter)?;
                    verifier = Some(arr);
                }

                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            y: y.ok_or(Error::InvalidParameter)?,
            verifier: verifier.ok_or(Error::InvalidParameter)?,
        })
    }
}

// ---------------------------------------------------------------------------
// Pake3
// ---------------------------------------------------------------------------

/// The fifth PASE message: commissioner → device.
///
/// Carries the commissioner's confirmation tag `cA`. The device checks this
/// to complete mutual authentication.
///
/// Wire format: anonymous TLV structure, context tag 1.
///
/// # Matter spec reference
/// Matter Core Spec §4.14.1.2.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Pake3 {
    /// The commissioner's SPAKE2+ confirmation tag `cA`. 32 bytes (SHA-256).
    pub verifier: [u8; HASH_LEN],
}

impl Pake3 {
    /// Encode this message as wire TLV bytes.
    ///
    /// # Errors
    ///
    /// Propagates any [`matter_codec::Error`] via [`Error::Codec`].
    pub(crate) fn encode(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous)?;
        w.put_bytes(Tag::Context(1), &self.verifier)?;
        w.end_container()?;
        Ok(buf)
    }

    /// Decode wire TLV bytes into a `Pake3`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidParameter`] if the `verifier` field is absent, has
    ///   the wrong length, or an unexpected tag is present.
    /// - [`Error::Codec`] on malformed TLV.
    pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
        let mut reader = TlvReader::new(bytes);
        expect_anon_struct_start(&mut reader)?;

        let mut verifier: Option<[u8; HASH_LEN]> = None;

        loop {
            match reader.next()? {
                Some(Element::ContainerEnd) => break,

                Some(Element::Scalar {
                    tag: Tag::Context(1),
                    value: Value::Bytes(b),
                }) => {
                    let arr: [u8; HASH_LEN] = b.try_into().map_err(|_| Error::InvalidParameter)?;
                    verifier = Some(arr);
                }

                None | Some(_) => return Err(Error::InvalidParameter),
            }
        }

        Ok(Self {
            verifier: verifier.ok_or(Error::InvalidParameter)?,
        })
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test-code carve-out: see CLAUDE.md.
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // Helper: build a minimal SessionParams raw_tlv at a given context tag.
    // -----------------------------------------------------------------------

    /// Build a minimal `SessionParams` at the given context `tag_num`.
    /// Contains one field: idleInterval (tag 1) = 500.
    fn minimal_session_params(tag_num: u8) -> SessionParams {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Context(tag_num)).unwrap();
        w.put_uint(Tag::Context(1), 500).unwrap();
        w.end_container().unwrap();
        SessionParams { raw_tlv: buf }
    }

    // -----------------------------------------------------------------------
    // PbkdfParamRequest
    // -----------------------------------------------------------------------

    #[test]
    fn pbkdf_param_request_roundtrip_no_session_params() {
        let req = PbkdfParamRequest {
            initiator_random: [0x42; 32],
            initiator_session_id: 0x1234,
            passcode_id: 0,
            has_pbkdf_parameters: false,
            initiator_session_params: None,
        };
        let bytes = req.encode().unwrap();
        let decoded = PbkdfParamRequest::decode(&bytes).unwrap();
        assert_eq!(decoded, req);
    }

    #[test]
    fn pbkdf_param_request_has_pbkdf_parameters_true_roundtrips() {
        let req = PbkdfParamRequest {
            initiator_random: [0xAB; 32],
            initiator_session_id: 0xFFFF,
            passcode_id: 0x8000,
            has_pbkdf_parameters: true,
            initiator_session_params: None,
        };
        let bytes = req.encode().unwrap();
        let decoded = PbkdfParamRequest::decode(&bytes).unwrap();
        assert_eq!(decoded, req);
    }

    #[test]
    fn pbkdf_param_request_with_session_params_roundtrips() {
        let req = PbkdfParamRequest {
            initiator_random: [0x11; 32],
            initiator_session_id: 0x0001,
            passcode_id: 0,
            has_pbkdf_parameters: false,
            initiator_session_params: Some(minimal_session_params(5)),
        };
        let bytes = req.encode().unwrap();
        let decoded = PbkdfParamRequest::decode(&bytes).unwrap();
        assert_eq!(decoded, req);
    }

    #[test]
    fn pbkdf_param_request_rejects_missing_required_field() {
        // Encode with only tags 1, 2, 3 — missing tag 4 (has_pbkdf_parameters).
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0u8; 32]).unwrap();
        w.put_uint(Tag::Context(2), 1).unwrap();
        w.put_uint(Tag::Context(3), 0).unwrap();
        // tag 4 intentionally omitted.
        w.end_container().unwrap();
        assert!(matches!(
            PbkdfParamRequest::decode(&buf),
            Err(Error::InvalidParameter)
        ));
    }

    #[test]
    fn pbkdf_param_request_rejects_extra_field() {
        // Tag 6 is outside the spec; decoder must reject it.
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0u8; 32]).unwrap();
        w.put_uint(Tag::Context(2), 1).unwrap();
        w.put_uint(Tag::Context(3), 0).unwrap();
        w.put_bool(Tag::Context(4), false).unwrap();
        w.put_uint(Tag::Context(6), 99).unwrap(); // unknown tag
        w.end_container().unwrap();
        assert!(matches!(
            PbkdfParamRequest::decode(&buf),
            Err(Error::InvalidParameter)
        ));
    }

    #[test]
    fn pbkdf_param_request_rejects_wrong_random_length() {
        // Tag 1 with 31 bytes instead of 32.
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0u8; 31]).unwrap(); // wrong length
        w.put_uint(Tag::Context(2), 1).unwrap();
        w.put_uint(Tag::Context(3), 0).unwrap();
        w.put_bool(Tag::Context(4), false).unwrap();
        w.end_container().unwrap();
        assert!(matches!(
            PbkdfParamRequest::decode(&buf),
            Err(Error::InvalidParameter)
        ));
    }

    // -----------------------------------------------------------------------
    // PbkdfParamResponse
    // -----------------------------------------------------------------------

    #[test]
    fn pbkdf_param_response_with_pbkdf_params_roundtrips() {
        let resp = PbkdfParamResponse {
            initiator_random: [0x11; 32],
            responder_random: [0x22; 32],
            responder_session_id: 0xABCD,
            pbkdf_parameters: Some(PbkdfParamsInner {
                iterations: 1000,
                salt: vec![0x55; 16],
            }),
            responder_session_params: None,
        };
        let bytes = resp.encode().unwrap();
        let decoded = PbkdfParamResponse::decode(&bytes).unwrap();
        assert_eq!(decoded, resp);
    }

    #[test]
    fn pbkdf_param_response_without_pbkdf_params_roundtrips() {
        // When hasPbkdfParameters was true in the request, response omits tag 4.
        let resp = PbkdfParamResponse {
            initiator_random: [0xAA; 32],
            responder_random: [0xBB; 32],
            responder_session_id: 0x0001,
            pbkdf_parameters: None,
            responder_session_params: None,
        };
        let bytes = resp.encode().unwrap();
        let decoded = PbkdfParamResponse::decode(&bytes).unwrap();
        assert_eq!(decoded, resp);
    }

    #[test]
    fn pbkdf_param_response_with_session_params_roundtrips() {
        let resp = PbkdfParamResponse {
            initiator_random: [0x33; 32],
            responder_random: [0x44; 32],
            responder_session_id: 7,
            pbkdf_parameters: Some(PbkdfParamsInner {
                iterations: 2000,
                salt: vec![0xFF; 32],
            }),
            responder_session_params: Some(minimal_session_params(5)),
        };
        let bytes = resp.encode().unwrap();
        let decoded = PbkdfParamResponse::decode(&bytes).unwrap();
        assert_eq!(decoded, resp);
    }

    #[test]
    fn pbkdf_params_inner_rejects_short_salt_on_encode() {
        let inner = PbkdfParamsInner {
            iterations: 1000,
            salt: vec![0u8; 15], // too short (< 16)
        };
        assert!(matches!(
            inner.encode(Tag::Context(4)),
            Err(Error::InvalidParameter)
        ));
    }

    #[test]
    fn pbkdf_params_inner_rejects_long_salt_on_encode() {
        let inner = PbkdfParamsInner {
            iterations: 1000,
            salt: vec![0u8; 33], // too long (> 32)
        };
        assert!(matches!(
            inner.encode(Tag::Context(4)),
            Err(Error::InvalidParameter)
        ));
    }

    // -----------------------------------------------------------------------
    // Pake1
    // -----------------------------------------------------------------------

    #[test]
    fn pake1_roundtrip() {
        let p = Pake1 { x: [0x04; 65] };
        let bytes = p.encode().unwrap();
        let decoded = Pake1::decode(&bytes).unwrap();
        assert_eq!(decoded, p);
    }

    #[test]
    fn pake1_rejects_wrong_x_length() {
        // 64 bytes instead of 65.
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0x04u8; 64]).unwrap();
        w.end_container().unwrap();
        assert!(matches!(Pake1::decode(&buf), Err(Error::InvalidParameter)));
    }

    #[test]
    fn pake1_rejects_extra_field() {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0x04u8; 65]).unwrap();
        w.put_uint(Tag::Context(2), 0).unwrap(); // extra unknown tag
        w.end_container().unwrap();
        assert!(matches!(Pake1::decode(&buf), Err(Error::InvalidParameter)));
    }

    // -----------------------------------------------------------------------
    // Pake2
    // -----------------------------------------------------------------------

    #[test]
    fn pake2_roundtrip() {
        let p = Pake2 {
            y: [0x04; 65],
            verifier: [0xCC; 32],
        };
        let bytes = p.encode().unwrap();
        let decoded = Pake2::decode(&bytes).unwrap();
        assert_eq!(decoded, p);
    }

    #[test]
    fn pake2_rejects_wrong_verifier_length() {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0x04u8; 65]).unwrap();
        w.put_bytes(Tag::Context(2), &[0xCCu8; 31]).unwrap(); // 31 instead of 32
        w.end_container().unwrap();
        assert!(matches!(Pake2::decode(&buf), Err(Error::InvalidParameter)));
    }

    // -----------------------------------------------------------------------
    // Pake3
    // -----------------------------------------------------------------------

    #[test]
    fn pake3_roundtrip() {
        let p = Pake3 {
            verifier: [0xDE; 32],
        };
        let bytes = p.encode().unwrap();
        let decoded = Pake3::decode(&bytes).unwrap();
        assert_eq!(decoded, p);
    }

    #[test]
    fn pake3_rejects_missing_verifier() {
        // Empty structure — missing tag 1.
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.end_container().unwrap();
        assert!(matches!(Pake3::decode(&buf), Err(Error::InvalidParameter)));
    }

    #[test]
    fn pake3_rejects_extra_field() {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), &[0xDEu8; 32]).unwrap();
        w.put_uint(Tag::Context(2), 42).unwrap(); // extra tag
        w.end_container().unwrap();
        assert!(matches!(Pake3::decode(&buf), Err(Error::InvalidParameter)));
    }
}