bluetooth-mesh-stack 0.2.0

Bluetooth Mesh on the bluetooth-host stack: the security toolbox, network and transport layers, provisioning, proxy, and the foundation and generic models
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
//! The upper and lower transport layers (Mesh Protocol 3.5, 3.6).
//!
//! Upper: an access payload encrypted with an application key (AKF 1, its
//! AID in the header) or the device key (AKF 0), TransMIC 32 or 64 bits.
//! Lower: one unsegmented PDU, or up to 32 segments of 12 octets each,
//! acknowledged by Segment Acknowledgment messages; [`Reassembly`] collects
//! segments on the receiving side and says what to acknowledge.

use std::collections::HashMap;

use crate::crypto::{self, Key};

/// Which key secures an access message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessKey<'a> {
    /// An application key and its AID (k4).
    App(&'a Key, u8),
    /// The device key of the source or destination.
    Device(&'a Key),
}

/// An application or device nonce.
fn nonce(device: bool, szmic: bool, seq: u32, src: u16, dst: u16, iv_index: u32) -> [u8; 13] {
    let mut n = [0u8; 13];
    n[0] = if device { 0x02 } else { 0x01 };
    n[1] = u8::from(szmic) << 7;
    n[2..5].copy_from_slice(&seq.to_be_bytes()[1..]);
    n[5..7].copy_from_slice(&src.to_be_bytes());
    n[7..9].copy_from_slice(&dst.to_be_bytes());
    n[9..13].copy_from_slice(&iv_index.to_be_bytes());
    n
}

/// Addressing and sequence of an access message, for its nonce.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Meta {
    /// Sequence number of the first segment (SeqAuth's low 24 bits).
    pub seq: u32,
    /// Source.
    pub src: u16,
    /// Destination.
    pub dst: u16,
    /// IV index.
    pub iv_index: u32,
    /// A virtual destination's Label UUID (the additional data).
    pub label: Option<[u8; 16]>,
}

/// Encrypts an access payload; `big_mic` chooses the 64-bit TransMIC
/// (segmented messages only).
pub fn encrypt_access(key: AccessKey<'_>, m: &Meta, payload: &[u8], big_mic: bool) -> Vec<u8> {
    let (k, device) = match key {
        AccessKey::App(k, _) => (k, false),
        AccessKey::Device(k) => (k, true),
    };
    let aad = m.label.as_ref().map(|l| &l[..]).unwrap_or(&[]);
    crypto::ccm_encrypt(
        k,
        &nonce(device, big_mic, m.seq, m.src, m.dst, m.iv_index),
        aad,
        payload,
        if big_mic { 8 } else { 4 },
    )
}

/// Decrypts an upper transport access PDU.
pub fn decrypt_access(
    key: AccessKey<'_>,
    m: &Meta,
    sealed: &[u8],
    big_mic: bool,
) -> Option<Vec<u8>> {
    let (k, device) = match key {
        AccessKey::App(k, _) => (k, false),
        AccessKey::Device(k) => (k, true),
    };
    let aad = m.label.as_ref().map(|l| &l[..]).unwrap_or(&[]);
    crypto::ccm_decrypt(
        k,
        &nonce(device, big_mic, m.seq, m.src, m.dst, m.iv_index),
        aad,
        sealed,
        if big_mic { 8 } else { 4 },
    )
}

/// The largest upper transport PDU unsegmented: 15 octets (access).
pub const MAX_UNSEGMENTED_ACCESS: usize = 15;
/// Octets of upper transport PDU per access segment.
pub const SEGMENT_SIZE: usize = 12;
/// The most segments a message may have.
pub const MAX_SEGMENTS: usize = 32;

/// Lower transport PDUs for an encrypted access message. `akf_aid` is
/// `None` for the device key, `Some(aid)` for an application key. Each
/// returned PDU goes in its own Network PDU with sequence numbers
/// `seq_zero`'s message's first and onward.
pub fn segment_access(
    akf_aid: Option<u8>,
    big_mic: bool,
    seq_zero: u32,
    upper: &[u8],
    force_segmented: bool,
) -> Option<Vec<Vec<u8>>> {
    let header = match akf_aid {
        Some(aid) => 0x40 | (aid & 0x3f),
        None => 0x00,
    };
    if !force_segmented && !big_mic && upper.len() <= MAX_UNSEGMENTED_ACCESS {
        let mut p = vec![header];
        p.extend_from_slice(upper);
        return Some(vec![p]);
    }
    let n = upper.len().div_ceil(SEGMENT_SIZE).max(1);
    if n > MAX_SEGMENTS {
        return None;
    }
    let seq_zero = (seq_zero & 0x1fff) as u16;
    Some(
        upper
            .chunks(SEGMENT_SIZE)
            .enumerate()
            .map(|(o, c)| {
                let (o, last) = (o as u32, (n - 1) as u32);
                let bits: u32 =
                    (u32::from(big_mic) << 23) | (u32::from(seq_zero) << 10) | (o << 5) | last;
                let mut p = vec![0x80 | header];
                p.extend_from_slice(&bits.to_be_bytes()[1..]);
                p.extend_from_slice(c);
                p
            })
            .collect(),
    )
}

/// A parsed lower transport PDU.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Lower {
    /// An unsegmented access message: AKF/AID and the upper transport PDU.
    Access {
        /// `Some(aid)` for an application key, `None` for the device key.
        aid: Option<u8>,
        /// The upper transport PDU.
        upper: Vec<u8>,
    },
    /// One segment of an access message.
    Segment {
        /// `Some(aid)` or `None` (device key).
        aid: Option<u8>,
        /// 64-bit TransMIC.
        big_mic: bool,
        /// SeqZero (13 bits).
        seq_zero: u16,
        /// This segment's number.
        seg_o: u8,
        /// The last segment's number.
        seg_n: u8,
        /// The segment's octets.
        data: Vec<u8>,
    },
    /// An unsegmented control message: opcode and parameters.
    Control {
        /// The opcode (7 bits).
        opcode: u8,
        /// Its parameters.
        params: Vec<u8>,
    },
    /// A Segment Acknowledgment.
    Ack {
        /// Sent by a Friend on behalf of a Low Power node.
        obo: bool,
        /// SeqZero of the acknowledged message.
        seq_zero: u16,
        /// Bit `n` set: segment `n` received.
        block: u32,
    },
}

/// Parses a lower transport PDU (`ctl` from the network header).
pub fn parse(ctl: bool, p: &[u8]) -> Option<Lower> {
    let (&h, rest) = p.split_first()?;
    let seg = h & 0x80 != 0;
    match (ctl, seg) {
        (false, false) => Some(Lower::Access {
            aid: (h & 0x40 != 0).then_some(h & 0x3f),
            upper: rest.to_vec(),
        }),
        (false, true) => {
            if rest.len() < 4 {
                return None;
            }
            let bits = u32::from_be_bytes([0, rest[0], rest[1], rest[2]]);
            let (seg_o, seg_n) = (((bits >> 5) & 0x1f) as u8, (bits & 0x1f) as u8);
            if seg_o > seg_n {
                return None;
            }
            Some(Lower::Segment {
                aid: (h & 0x40 != 0).then_some(h & 0x3f),
                big_mic: bits & 0x80_0000 != 0,
                seq_zero: ((bits >> 10) & 0x1fff) as u16,
                seg_o,
                seg_n,
                data: rest[3..].to_vec(),
            })
        }
        (true, false) if h & 0x7f == 0 => {
            if rest.len() != 6 {
                return None;
            }
            let v = u16::from_be_bytes([rest[0], rest[1]]);
            Some(Lower::Ack {
                obo: v & 0x8000 != 0,
                seq_zero: (v >> 2) & 0x1fff,
                block: u32::from_be_bytes([rest[2], rest[3], rest[4], rest[5]]),
            })
        }
        (true, false) => Some(Lower::Control {
            opcode: h & 0x7f,
            params: rest.to_vec(),
        }),
        // Segmented control messages are reassembled like access ones; not needed yet.
        (true, true) => None,
    }
}

/// A Segment Acknowledgment PDU.
pub fn ack(seq_zero: u16, block: u32) -> Vec<u8> {
    let v = (seq_zero & 0x1fff) << 2;
    let mut p = vec![0x00];
    p.extend_from_slice(&v.to_be_bytes());
    p.extend_from_slice(&block.to_be_bytes());
    p
}

/// What reassembly produced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reassembled {
    /// More to come; acknowledge `block` so far.
    Partial {
        /// Segments received.
        block: u32,
    },
    /// Complete: the upper transport PDU, and the block to acknowledge.
    Complete {
        /// `Some(aid)` or `None` (device key).
        aid: Option<u8>,
        /// 64-bit TransMIC.
        big_mic: bool,
        /// The upper transport PDU.
        upper: Vec<u8>,
        /// All segments, for the final acknowledgment.
        block: u32,
    },
}

struct Incoming {
    seq_zero: u16,
    aid: Option<u8>,
    big_mic: bool,
    seg_n: u8,
    parts: Vec<Option<Vec<u8>>>,
}

/// Segments being reassembled, per source. One message per source at a
/// time, as the specification requires; a newer SeqZero replaces an older.
#[derive(Default)]
pub struct Reassembly {
    incoming: HashMap<u16, Incoming>,
}

impl Reassembly {
    /// Adds a segment from `src`.
    pub fn add(&mut self, src: u16, s: &Lower) -> Option<Reassembled> {
        let Lower::Segment {
            aid,
            big_mic,
            seq_zero,
            seg_o,
            seg_n,
            data,
        } = s
        else {
            return None;
        };
        let e = self.incoming.entry(src).or_insert_with(|| Incoming {
            seq_zero: *seq_zero,
            aid: *aid,
            big_mic: *big_mic,
            seg_n: *seg_n,
            parts: vec![None; usize::from(*seg_n) + 1],
        });
        if e.seq_zero != *seq_zero || e.seg_n != *seg_n {
            *e = Incoming {
                seq_zero: *seq_zero,
                aid: *aid,
                big_mic: *big_mic,
                seg_n: *seg_n,
                parts: vec![None; usize::from(*seg_n) + 1],
            };
        }
        // Every segment but the last is exactly 12 octets.
        if (*seg_o < *seg_n && data.len() != SEGMENT_SIZE)
            || data.is_empty()
            || data.len() > SEGMENT_SIZE
        {
            return None;
        }
        e.parts[usize::from(*seg_o)] = Some(data.clone());
        let block = e
            .parts
            .iter()
            .enumerate()
            .filter(|(_, p)| p.is_some())
            .fold(0u32, |b, (i, _)| b | (1 << i));
        if e.parts.iter().all(Option::is_some) {
            let e = self.incoming.remove(&src)?;
            let upper = e.parts.into_iter().flatten().flatten().collect();
            return Some(Reassembled::Complete {
                aid: e.aid,
                big_mic: e.big_mic,
                upper,
                block,
            });
        }
        Some(Reassembled::Partial { block })
    }
}

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

    #[test]
    fn access_encryption_round_trips_with_either_key_and_mic() {
        let app = [3u8; 16];
        let dev = [4u8; 16];
        let m = Meta {
            seq: 0x3129ab,
            src: 0x0003,
            dst: 0x1201,
            iv_index: 0x12345678,
            label: None,
        };
        for key in [
            AccessKey::App(&app, crypto::k4(&app)),
            AccessKey::Device(&dev),
        ] {
            for big in [false, true] {
                let c = encrypt_access(key, &m, b"\x82\x02\x01", big);
                assert_eq!(decrypt_access(key, &m, &c, big).unwrap(), b"\x82\x02\x01");
                let other = Meta {
                    seq: m.seq + 1,
                    ..m
                };
                assert!(
                    decrypt_access(key, &other, &c, big).is_none(),
                    "the nonce binds the sequence"
                );
            }
        }
    }

    #[test]
    fn segments_and_reassembles_out_of_order() {
        let upper: Vec<u8> = (0..30u8).collect();
        let segs = segment_access(Some(0x26), false, 0x1234, &upper, false).unwrap();
        assert_eq!(segs.len(), 3);
        let parsed: Vec<Lower> = segs.iter().map(|p| parse(false, p).unwrap()).collect();
        let mut r = Reassembly::default();
        assert_eq!(
            r.add(7, &parsed[2]),
            Some(Reassembled::Partial { block: 0b100 })
        );
        assert_eq!(
            r.add(7, &parsed[0]),
            Some(Reassembled::Partial { block: 0b101 })
        );
        assert_eq!(
            r.add(7, &parsed[1]),
            Some(Reassembled::Complete {
                aid: Some(0x26),
                big_mic: false,
                upper,
                block: 0b111
            })
        );
        // A short PDU fits unsegmented.
        let one = segment_access(None, false, 1, &[1, 2, 3], false).unwrap();
        assert_eq!(
            parse(false, &one[0]),
            Some(Lower::Access {
                aid: None,
                upper: vec![1, 2, 3]
            })
        );
        assert!(segment_access(None, false, 1, &[0; 12 * 33], false).is_none());
    }

    #[test]
    fn acks_round_trip() {
        let p = ack(0x1234, 0b1011);
        assert_eq!(
            parse(true, &p),
            Some(Lower::Ack {
                obo: false,
                seq_zero: 0x1234,
                block: 0b1011
            })
        );
        assert!(parse(true, &p[..4]).is_none());
        assert!(parse(false, &[]).is_none());
    }
}