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
//! Encrypting and decrypting a Matter message (Core §4.8, §4.9).
//!
//! Everything above the message header travels encrypted, and the header itself travels
//! *authenticated but in the clear* — a receiver has to read the Session ID before it can
//! find the key. That is why the header is the AEAD's associated data rather than part of
//! its payload: changing a single bit of it breaks the tag, but anyone can still read it.
//!
//! ```text
//!  ┌──────────── associated data (authenticated, clear) ────────────┐┌ encrypted ┐┌ tag ┐
//!  │ flags │ session │ sec │ counter │ [source] │ [dest] │ [ext]    ││  payload  ││ MIC │
//!  └───────┴─────────┴─────┴─────────┴──────────┴────────┴──────────┘└───────────┘└─────┘
//!           ╰──── never obfuscated ────╯╰─── obfuscated when P is set ───╯
//! ```
//!
//! # Privacy
//!
//! With the **P** flag set, the counter and node ids are additionally obfuscated with
//! AES-CTR under a key derived from the encryption key (§4.9). This does not add
//! confidentiality — they are already authenticated, and an attacker who has the key can
//! read them either way — it removes a *correlation handle*: without it, a passive observer
//! can follow one node across a network by its monotonically increasing counter.
//!
//! The first four octets are deliberately outside the obfuscation, because a receiver must
//! read the Session ID to know which key to try.
//!
//! # The nonce, and why it is an enum
//!
//! §4.8.1.1 gives three different answers for the nonce's Source Node ID depending on the
//! session: a CASE session uses the peer's operational id from the session context, a PASE
//! session uses the Unspecified Node ID, and a group message uses the id in the message.
//! Passing a bare `NodeId` would make all three look the same at the call site and let a
//! caller silently pick the wrong one — so [`NonceSource`] names them.

use super::header::{Destination, MessageHeader, SessionType};
use super::ids::NodeId;
use crate::crypto::{
    AEAD_MIC_LENGTH_BYTES, AEAD_NONCE_LENGTH_BYTES, SymmetricKey, aead_decrypt_in_place,
    aead_encrypt_in_place, privacy_key, privacy_xor,
};
use crate::error::{Error, ErrorCode, Result, bail};

/// The keys one session protects its messages with.
///
/// The privacy key is derived from the encryption key once, at session establishment,
/// rather than per message: it is a KDF call, and doing it on every send would be visible
/// on a microcontroller.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionKeys {
    /// The AEAD key for this direction (§4.8).
    pub encryption: SymmetricKey,
    /// The AES-CTR key for header obfuscation (§4.9.1).
    pub privacy: SymmetricKey,
}

impl SessionKeys {
    /// Derives the privacy key from an encryption key (§4.9.1).
    pub fn from_encryption_key(encryption: SymmetricKey) -> Result<Self> {
        let privacy = privacy_key(&encryption)?;
        Ok(Self {
            encryption,
            privacy,
        })
    }
}

/// Where the Source Node ID in the AEAD nonce comes from (§4.8.1.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NonceSource {
    /// A PASE session: "the Nonce Source Node ID SHALL be Unspecified Node ID".
    ///
    /// Safe despite being a constant, and the specification says why: "Because PASE
    /// negotiates strong one-time keys per session and the I2RKey and R2IKey are distinct
    /// for each direction of communication, the use of the Unspecified Node ID as the
    /// Nonce Source Node ID remains semantically secure."
    Pase,
    /// A CASE session: "determined via the Secure Session Context associated with the
    /// Session Identifier" — the operational Node ID of whoever protected the message.
    Case(NodeId),
    /// A group message: the Source Node ID carried in the message itself.
    ///
    /// "The S Flag of the message SHALL be 1 … If the S Flag of the message is 0 the
    /// message SHALL be dropped."
    Group,
}

impl NonceSource {
    fn resolve(self, header: &MessageHeader) -> Result<NodeId> {
        match self {
            Self::Pase => Ok(NodeId::UNSPECIFIED),
            Self::Case(id) => Ok(id),
            Self::Group => header.source.ok_or(Error::new(ErrorCode::MessageReserved)),
        }
    }
}

/// Builds the AEAD nonce of Table 17: `Security Flags || Message Counter || Source Node ID`.
///
/// "The scalar fields in the nonce … SHALL be encoded in little-endian byte order … that
/// is, in the same byte ordering as the segment of the message from which its data
/// originates."
fn nonce(
    security_flags: u8,
    message_counter: u32,
    source: NodeId,
) -> [u8; AEAD_NONCE_LENGTH_BYTES] {
    let mut out = [0u8; AEAD_NONCE_LENGTH_BYTES];
    out[0] = security_flags;
    // Indices are constant and within a 13-octet array.
    if let Some(slot) = out.get_mut(1..5) {
        slot.copy_from_slice(&message_counter.to_le_bytes());
    }
    if let Some(slot) = out.get_mut(5..13) {
        slot.copy_from_slice(&source.0.to_le_bytes());
    }
    out
}

/// Builds the privacy nonce of §4.9.2: `Session ID (big-endian) || MIC[5..16]`.
fn privacy_nonce(
    session_id: u16,
    mic: &[u8; AEAD_MIC_LENGTH_BYTES],
) -> Result<[u8; AEAD_NONCE_LENGTH_BYTES]> {
    let mut out = [0u8; AEAD_NONCE_LENGTH_BYTES];
    // "the 16-bit Session ID (in big-endian format)" — the one place in the message layer
    // where a scalar is *not* little-endian.
    let Some(slot) = out.get_mut(..2) else {
        bail!(BufferTooSmall)
    };
    slot.copy_from_slice(&session_id.to_be_bytes());
    let (Some(dst), Some(src)) = (out.get_mut(2..13), mic.get(5..16)) else {
        bail!(BufferTooSmall)
    };
    dst.copy_from_slice(src);
    Ok(out)
}

/// How many header octets the privacy step obfuscates.
///
/// "M = Message Counter || [Source ID] || [Destination ID]" — the counter and the node ids,
/// and nothing before or after them. The four octets before (flags, session id, security
/// flags) stay readable because a receiver needs them to find the key; the message
/// extensions after are excluded by §4.4.1.7.
const PRIVACY_OFFSET: usize = 4;

fn privacy_len(header: &MessageHeader) -> usize {
    let counter = 4usize;
    let source = if header.source.is_some() { 8 } else { 0 };
    let destination = match header.destination {
        Destination::None => 0,
        Destination::Node(_) => 8,
        Destination::Group(_) => 2,
    };
    counter.saturating_add(source).saturating_add(destination)
}

/// Encrypts one message into `out`, returning how many octets it occupies.
///
/// `payload` is the protocol header and application payload — everything that travels
/// encrypted. The header's `privacy` flag decides whether §4.9's obfuscation is applied.
///
/// The result is `header || ciphertext || MIC`.
pub fn protect(
    header: &MessageHeader,
    nonce_source: NonceSource,
    payload: &[u8],
    keys: &SessionKeys,
    out: &mut [u8],
) -> Result<usize> {
    if header.is_unsecured() {
        // §4.4.2.1: "The Message Integrity Check field SHALL be present for all messages
        // except those of Unsecured Session Type." Protecting one would be a
        // contradiction, so it is refused rather than silently producing a message with a
        // tag nobody will check.
        bail!(InvalidState)
    }
    if matches!(header.session_type, SessionType::Group) && header.source.is_none() {
        // §4.8.1.1: a group message without a Source Node ID has no nonce.
        bail!(MessageReserved)
    }

    let header_len = header.encode(out)?;
    let source = nonce_source.resolve(header)?;
    let n = nonce(security_flags_of(header), header.message_counter, source);

    // The associated data is the header exactly as encoded — no re-serialisation, so the
    // bytes that are authenticated are provably the bytes that are sent.
    let Some(aad) = out.get(..header_len) else {
        bail!(BufferTooSmall)
    };
    let mut aad_copy = [0u8; MAX_HEADER];
    let Some(aad_slot) = aad_copy.get_mut(..header_len) else {
        bail!(BufferTooSmall)
    };
    aad_slot.copy_from_slice(aad);

    let cipher_end = header_len
        .checked_add(payload.len())
        .ok_or(Error::new(ErrorCode::BufferTooSmall))?;
    let total = cipher_end
        .checked_add(AEAD_MIC_LENGTH_BYTES)
        .ok_or(Error::new(ErrorCode::BufferTooSmall))?;
    let Some(body) = out.get_mut(header_len..cipher_end) else {
        bail!(BufferTooSmall)
    };
    body.copy_from_slice(payload);

    let mut tag = [0u8; AEAD_MIC_LENGTH_BYTES];
    aead_encrypt_in_place(&keys.encryption, &n, aad_slot, body, &mut tag)?;

    let Some(mic) = out.get_mut(cipher_end..total) else {
        bail!(BufferTooSmall)
    };
    mic.copy_from_slice(&tag);

    if header.privacy {
        // §4.9.3, and note the order: obfuscation happens *after* encryption, because its
        // nonce is built from the MIC that encryption produced.
        let nonce = privacy_nonce(header.session_id.0, &tag)?;
        let end = PRIVACY_OFFSET
            .checked_add(privacy_len(header))
            .ok_or(Error::new(ErrorCode::BufferTooSmall))?;
        let Some(region) = out.get_mut(PRIVACY_OFFSET..end) else {
            bail!(BufferTooSmall)
        };
        privacy_xor(&keys.privacy, &nonce, region)?;
    }

    Ok(total)
}

/// The largest message header: flags, session, security, counter, source and destination.
const MAX_HEADER: usize = 1 + 2 + 1 + 4 + 8 + 8;

/// Reconstructs the Security Flags octet a header encodes to.
fn security_flags_of(header: &MessageHeader) -> u8 {
    let mut bits = match header.session_type {
        SessionType::Unicast => 0u8,
        SessionType::Group => 1,
    };
    if header.privacy {
        bits |= 0b1000_0000;
    }
    if header.control {
        bits |= 0b0100_0000;
    }
    bits
}

/// What a receiver can read before it has found a key.
///
/// The first four octets are never obfuscated and never encrypted, which is what makes a
/// session lookup possible at all.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Preview {
    /// Which session's keys to try.
    pub session_id: super::ids::SessionId,
    /// Unicast or group — it decides which counter rule and which key set apply.
    pub session_type: SessionType,
    /// Whether the header is obfuscated.
    pub privacy: bool,
    /// Whether this is a control message, which counts on its own counter.
    pub control: bool,
}

/// Reads the four octets of a message that are always in the clear.
///
/// This is the first thing a receiver does: it cannot parse the rest of the header until
/// it has deobfuscated it, and it cannot deobfuscate it until it has found the session.
pub fn preview(buf: &[u8]) -> Result<Preview> {
    let (Some(&flags), Some(session), Some(&security)) = (buf.first(), buf.get(1..3), buf.get(3))
    else {
        bail!(MessageTruncated)
    };
    if (flags >> 4) != super::header::MESSAGE_FORMAT_VERSION {
        bail!(UnsupportedVersion)
    }
    let Ok(id) = <[u8; 2]>::try_from(session) else {
        bail!(MessageTruncated)
    };
    let session_type = match security & 0b11 {
        0 => SessionType::Unicast,
        1 => SessionType::Group,
        _ => bail!(MessageReserved),
    };
    Ok(Preview {
        session_id: super::ids::SessionId(u16::from_le_bytes(id)),
        session_type,
        privacy: security & 0b1000_0000 != 0,
        control: security & 0b0100_0000 != 0,
    })
}

/// Decrypts one message in place, returning its header and the range of `buf` holding the
/// plaintext payload.
///
/// On success `buf[range]` is the protocol header and application payload. On failure the
/// contents of `buf` are unspecified — §3.6.2 is explicit that a failed tag check leaves
/// the payload undefined — so a caller must not look at it.
pub fn unprotect(
    buf: &mut [u8],
    keys: &SessionKeys,
    nonce_source: NonceSource,
) -> Result<(MessageHeader, core::ops::Range<usize>)> {
    let head = preview(buf)?;

    if head.privacy {
        // Deobfuscate before parsing: the counter and node ids are unreadable until now,
        // and the header cannot be parsed without them.
        let total = buf.len();
        let mic_start = total
            .checked_sub(AEAD_MIC_LENGTH_BYTES)
            .ok_or(Error::new(ErrorCode::MessageTruncated))?;
        let Some(mic) = buf.get(mic_start..total) else {
            bail!(MessageTruncated)
        };
        let Ok(mic) = <[u8; AEAD_MIC_LENGTH_BYTES]>::try_from(mic) else {
            bail!(MessageTruncated)
        };
        let nonce = privacy_nonce(head.session_id.0, &mic)?;

        // The obfuscated region's length depends on the Message Flags, which are in the
        // clear — so it can be computed without decrypting anything.
        let Some(&flags) = buf.first() else {
            bail!(MessageTruncated)
        };
        let obfuscated = privacy_len_from_flags(flags)?;
        let end = PRIVACY_OFFSET
            .checked_add(obfuscated)
            .ok_or(Error::new(ErrorCode::MessageTruncated))?;
        let Some(region) = buf.get_mut(PRIVACY_OFFSET..end) else {
            bail!(MessageTruncated)
        };
        privacy_xor(&keys.privacy, &nonce, region)?;
    }

    let (header, rest) = MessageHeader::decode(buf)?;
    if header.is_unsecured() {
        // Nothing to unprotect; a caller that reached here has a session key for a session
        // that does not use one.
        bail!(InvalidState)
    }
    // §4.7.2's validity checks that depend on the session type.
    match header.session_type {
        SessionType::Unicast => {
            if matches!(header.destination, Destination::Group(_)) {
                // "If the message is of Secure Unicast Session Type: The DSIZ field SHALL
                // NOT indicate a Group ID is present."
                bail!(MessageReserved)
            }
        }
        SessionType::Group => {
            if matches!(header.destination, Destination::None) || header.source.is_none() {
                // "The DSIZ field SHALL NOT be 0. The S Flag field SHALL NOT be 0."
                bail!(MessageReserved)
            }
        }
    }

    let header_len = buf.len().saturating_sub(rest.len());
    let mut aad = [0u8; MAX_HEADER];
    let (Some(aad_slot), Some(header_bytes)) = (aad.get_mut(..header_len), buf.get(..header_len))
    else {
        bail!(MessageTruncated)
    };
    aad_slot.copy_from_slice(header_bytes);

    let total = buf.len();
    let mic_start = total
        .checked_sub(AEAD_MIC_LENGTH_BYTES)
        .ok_or(Error::new(ErrorCode::MessageTruncated))?;
    if mic_start < header_len {
        bail!(MessageTruncated)
    }
    let Some(mic) = buf.get(mic_start..total) else {
        bail!(MessageTruncated)
    };
    let Ok(tag) = <[u8; AEAD_MIC_LENGTH_BYTES]>::try_from(mic) else {
        bail!(MessageTruncated)
    };

    let source = nonce_source.resolve(&header)?;
    let n = nonce(security_flags_of(&header), header.message_counter, source);

    let Some(body) = buf.get_mut(header_len..mic_start) else {
        bail!(MessageTruncated)
    };
    aead_decrypt_in_place(&keys.encryption, &n, aad_slot, body, &tag)?;

    Ok((header, header_len..mic_start))
}

/// The obfuscated region's length, from the Message Flags octet alone.
fn privacy_len_from_flags(flags: u8) -> Result<usize> {
    let source = if flags & 0b0000_0100 != 0 { 8 } else { 0 };
    let destination = match flags & 0b11 {
        0 => 0usize,
        1 => 8,
        2 => 2,
        _ => bail!(MessageReserved),
    };
    Ok(4usize.saturating_add(source).saturating_add(destination))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::msg::{ExchangeId, GroupId, ProtocolHeader, ProtocolId, SessionId};

    fn keys() -> SessionKeys {
        SessionKeys::from_encryption_key(SymmetricKey::new([0x5A; 16])).expect("derive")
    }

    fn case_header() -> MessageHeader {
        MessageHeader {
            session_id: SessionId(0x1234),
            message_counter: 0x0102_0304,
            ..MessageHeader::default()
        }
    }

    #[test]
    fn the_nonce_is_table_17() {
        // Security Flags(1) || Message Counter(4, LE) || Source Node ID(8, LE).
        let n = nonce(0x80, 0x0A0B_0C0D, NodeId(0x1122_3344_5566_7788));
        assert_eq!(
            n,
            [
                0x80, // security flags
                0x0D, 0x0C, 0x0B, 0x0A, // counter, little-endian
                0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // source, little-endian
            ]
        );
    }

    #[test]
    fn the_privacy_nonce_matches_the_specs_worked_example() {
        // §4.9.2 prints this one in full: session 42, and a given MIC.
        let mic = [
            0xc5, 0xa0, 0x06, 0x3a, 0xd5, 0xd2, 0x51, 0x81, 0x91, 0x40, 0x0d, 0xd6, 0x8c, 0x5c,
            0x16, 0x3b,
        ];
        let n = privacy_nonce(42, &mic).expect("nonce");
        assert_eq!(
            n,
            [
                0x00, 0x2a, // session id, big-endian
                0xd2, 0x51, 0x81, 0x91, 0x40, 0x0d, 0xd6, 0x8c, 0x5c, 0x16,
                0x3b, // MIC[5..16]
            ]
        );
    }

    #[test]
    fn a_case_message_round_trips() {
        let keys = keys();
        let header = case_header();
        let payload = b"protocol header and payload";

        let mut buf = [0u8; 256];
        let n = protect(
            &header,
            NonceSource::Case(NodeId(7)),
            payload,
            &keys,
            &mut buf,
        )
        .expect("protect");

        let (decoded, range) =
            unprotect(&mut buf[..n], &keys, NonceSource::Case(NodeId(7))).expect("unprotect");
        assert_eq!(decoded, header);
        assert_eq!(&buf[range], payload);
    }

    #[test]
    fn a_pase_message_round_trips() {
        let keys = keys();
        let header = case_header();
        let payload = b"pake1";
        let mut buf = [0u8; 128];
        let n = protect(&header, NonceSource::Pase, payload, &keys, &mut buf).expect("protect");
        let (_, range) = unprotect(&mut buf[..n], &keys, NonceSource::Pase).expect("unprotect");
        assert_eq!(&buf[range], payload);
    }

    #[test]
    fn the_wrong_nonce_source_does_not_decrypt() {
        // The whole reason `NonceSource` is an enum: picking the wrong one is a silent
        // interoperability failure, and this is what it looks like.
        let keys = keys();
        let header = case_header();
        let mut buf = [0u8; 128];
        let n =
            protect(&header, NonceSource::Case(NodeId(7)), b"x", &keys, &mut buf).expect("protect");
        assert_eq!(
            unprotect(&mut buf[..n], &keys, NonceSource::Pase)
                .unwrap_err()
                .code(),
            ErrorCode::IntegrityCheckFailed
        );
    }

    #[test]
    fn a_private_message_round_trips_and_hides_the_counter() {
        let keys = keys();
        let header = MessageHeader {
            privacy: true,
            source: Some(NodeId(0x1122_3344_5566_7788)),
            destination: Destination::Node(NodeId(9)),
            ..case_header()
        };
        let payload = b"obfuscated header, encrypted body";

        let mut buf = [0u8; 256];
        let n = protect(
            &header,
            NonceSource::Case(NodeId(0x1122_3344_5566_7788)),
            payload,
            &keys,
            &mut buf,
        )
        .expect("protect");

        // The counter is not readable on the wire…
        assert_ne!(
            &buf[4..8],
            &header.message_counter.to_le_bytes(),
            "the counter should be obfuscated"
        );
        // …but the first four octets are, because a receiver needs them.
        let head = preview(&buf[..n]).expect("preview");
        assert_eq!(head.session_id, header.session_id);
        assert!(head.privacy);

        let (decoded, range) = unprotect(
            &mut buf[..n],
            &keys,
            NonceSource::Case(NodeId(0x1122_3344_5566_7788)),
        )
        .expect("unprotect");
        assert_eq!(decoded, header);
        assert_eq!(&buf[range], payload);
    }

    #[test]
    fn a_group_message_round_trips() {
        let keys = keys();
        let header = MessageHeader {
            session_type: SessionType::Group,
            session_id: SessionId(5),
            source: Some(NodeId(42)),
            destination: Destination::Group(GroupId(7)),
            ..MessageHeader::default()
        };
        let mut buf = [0u8; 128];
        let n = protect(&header, NonceSource::Group, b"grouped", &keys, &mut buf).expect("protect");
        let (decoded, range) =
            unprotect(&mut buf[..n], &keys, NonceSource::Group).expect("unprotect");
        assert_eq!(decoded.source, Some(NodeId(42)));
        assert_eq!(&buf[range], b"grouped");
    }

    #[test]
    fn a_group_message_without_a_source_is_refused() {
        // §4.8.1.1: "If the S Flag of the message is 0 the message SHALL be dropped" —
        // there is no nonce without it.
        let keys = keys();
        let header = MessageHeader {
            session_type: SessionType::Group,
            session_id: SessionId(5),
            destination: Destination::Group(GroupId(7)),
            ..MessageHeader::default()
        };
        let mut buf = [0u8; 128];
        assert_eq!(
            protect(&header, NonceSource::Group, b"x", &keys, &mut buf)
                .unwrap_err()
                .code(),
            ErrorCode::MessageReserved
        );
    }

    #[test]
    fn an_unsecured_message_cannot_be_protected() {
        let keys = keys();
        let mut buf = [0u8; 64];
        assert_eq!(
            protect(
                &MessageHeader::default(),
                NonceSource::Pase,
                b"x",
                &keys,
                &mut buf
            )
            .unwrap_err()
            .code(),
            ErrorCode::InvalidState
        );
    }

    #[test]
    fn every_single_bit_flip_is_caught() {
        // The header is authenticated even though it is readable; the payload and MIC
        // obviously are. Nothing in the message may be changed without detection.
        let keys = keys();
        let header = case_header();
        let mut buf = [0u8; 128];
        let n = protect(
            &header,
            NonceSource::Case(NodeId(7)),
            b"payload",
            &keys,
            &mut buf,
        )
        .expect("protect");
        let original = buf;

        for byte in 0..n {
            for bit in 0..8u32 {
                let mut broken = original;
                broken[byte] ^= 1u8 << bit;
                let result = unprotect(&mut broken[..n], &keys, NonceSource::Case(NodeId(7)));
                assert!(
                    result.is_err(),
                    "flipping bit {bit} of octet {byte} went undetected"
                );
            }
        }
    }

    #[test]
    fn the_wrong_key_does_not_decrypt() {
        let keys = keys();
        let other = SessionKeys::from_encryption_key(SymmetricKey::new([0xA5; 16])).expect("k");
        let mut buf = [0u8; 128];
        let n = protect(&case_header(), NonceSource::Pase, b"x", &keys, &mut buf).expect("protect");
        assert!(unprotect(&mut buf[..n], &other, NonceSource::Pase).is_err());
    }

    #[test]
    fn a_group_id_on_a_unicast_session_is_refused() {
        // §4.7.2: "If the message is of Secure Unicast Session Type: The DSIZ field SHALL
        // NOT indicate a Group ID is present."
        let keys = keys();
        let header = MessageHeader {
            destination: Destination::Group(GroupId(1)),
            ..case_header()
        };
        let mut buf = [0u8; 128];
        let n =
            protect(&header, NonceSource::Case(NodeId(1)), b"x", &keys, &mut buf).expect("protect");
        assert_eq!(
            unprotect(&mut buf[..n], &keys, NonceSource::Case(NodeId(1)))
                .unwrap_err()
                .code(),
            ErrorCode::MessageReserved
        );
    }

    #[test]
    fn truncation_at_every_length_is_an_error_not_a_panic() {
        let keys = keys();
        let mut buf = [0u8; 128];
        let n = protect(
            &case_header(),
            NonceSource::Pase,
            b"payload",
            &keys,
            &mut buf,
        )
        .expect("protect");
        for cut in 0..n {
            let mut copy = buf;
            let _ = unprotect(&mut copy[..cut], &keys, NonceSource::Pase);
        }
    }

    #[test]
    fn a_whole_message_round_trips_with_its_protocol_header() {
        // What a real send path does: encode the protocol header, protect the result.
        let keys = keys();
        let protocol = ProtocolHeader {
            initiator: true,
            reliability: true,
            exchange_id: ExchangeId(3),
            protocol: ProtocolId::SECURE_CHANNEL,
            opcode: 0x20,
            ..ProtocolHeader::default()
        };
        let mut inner = [0u8; 64];
        let inner_len = protocol.encode(&mut inner).expect("encode");

        let mut buf = [0u8; 256];
        let n = protect(
            &case_header(),
            NonceSource::Pase,
            &inner[..inner_len],
            &keys,
            &mut buf,
        )
        .expect("protect");

        let (_, range) = unprotect(&mut buf[..n], &keys, NonceSource::Pase).expect("unprotect");
        let (decoded, app) = ProtocolHeader::decode(&buf[range]).expect("decode");
        assert_eq!(decoded, protocol);
        assert!(app.is_empty());
    }

    #[test]
    fn the_preview_refuses_what_the_header_would() {
        assert_eq!(
            preview(&[]).unwrap_err().code(),
            ErrorCode::MessageTruncated
        );
        assert_eq!(
            preview(&[0x10, 0, 0, 0]).unwrap_err().code(),
            ErrorCode::UnsupportedVersion
        );
        assert_eq!(
            preview(&[0x00, 0, 0, 0x02]).unwrap_err().code(),
            ErrorCode::MessageReserved
        );
    }
}