oxirush-security 0.1.0

5G security algorithms — KDF, NIA1/2/3, NEA0/1/2/3, SUCI concealment per TS 33.501
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
/// NAS integrity algorithms per 3GPP TS 33.501 Annex B.4
///
/// NIA0 = null integrity (no-op)
/// NIA1 = 128-EIA1 (SNOW 3G MAC, TS 35.215/35.216)
/// NIA2 = 128-EIA2 (AES-CMAC, TS 33.501 Annex B.4.2)
/// NIA3 = 128-EIA3 (ZUC MAC, TS 35.221/35.222)
use crate::snow3g::Snow3G;
use crate::zuc::Zuc;
use aes::Aes128;
use cmac::Cmac;

/// Unified NAS integrity MAC computation.
///
/// `algo_id`: 0x00 = NIA0 (null), 0x01 = NIA1, 0x02 = NIA2, 0x03 = NIA3
///
/// Assumes message length in bits = message.len() * 8 (full bytes, no partial bits).
/// Returns 32-bit MAC-I.
pub fn nas_mac(
    key: &[u8; 16],
    count: u32,
    bearer: u8,
    direction: u8,
    message: &[u8],
    algo_id: u8,
) -> u32 {
    let bit_length = (message.len() * 8) as u64;
    match algo_id {
        0x01 => nia1_mac(key, count, bearer, direction, message, bit_length),
        0x02 => nia2_mac(key, count, bearer, direction, message),
        0x03 => nia3_mac(key, count, bearer, direction, message, bit_length),
        _ => {
            // NIA0 (null integrity) returns MAC-I = 0. Per TS 33.501 §5.5, NIA0
            // should never be selected for NAS integrity protection.
            0
        }
    }
}

// ── NIA1: 128-EIA1 (SNOW 3G MAC) ──────────────────────────────────────────────

/// Compute NAS-MAC using NIA1 / 128-EIA1 (TS 35.215 §4.3, TS 35.216)
///
/// Maps NAS inputs to UIA2 (SNOW 3G MAC), matching free5gc:
/// - FRESH = BEARER << 27
/// - IV[0] = FRESH ^ (DIRECTION << 15)
/// - IV[1] = COUNT ^ (DIRECTION << 31)
/// - IV[2] = FRESH
/// - IV[3] = COUNT
///
/// `bit_length`: number of bits in the message (may be < message.len() * 8 for
/// partial last byte; the message slice must be zero-padded in that case).
pub fn nia1_mac(
    key: &[u8; 16],
    count: u32,
    bearer: u8,
    direction: u8,
    message: &[u8],
    bit_length: u64,
) -> u32 {
    let fresh = (bearer as u32 & 0x1F) << 27;
    let dir32 = direction as u32 & 0x01;

    // Key: reverse byte order (matching free5gc)
    let k = [
        u32::from_be_bytes(
            key[12..16]
                .try_into()
                .expect("4-byte slice from 16-byte key"),
        ),
        u32::from_be_bytes(
            key[8..12]
                .try_into()
                .expect("4-byte slice from 16-byte key"),
        ),
        u32::from_be_bytes(key[4..8].try_into().expect("4-byte slice from 16-byte key")),
        u32::from_be_bytes(key[0..4].try_into().expect("4-byte slice from 16-byte key")),
    ];
    let iv = [fresh ^ (dir32 << 15), count ^ (dir32 << 31), fresh, count];

    // Only 5 keystream words needed: z[0..1]=P, z[2..3]=Q, z[4]=final mask
    let mut snow = Snow3G::new(k, iv);
    let z = snow.generate(5);

    // Polynomial evaluation MAC (UIA2, matching free5gc)
    let p = ((z[0] as u64) << 32) | (z[1] as u64);
    let q = ((z[2] as u64) << 32) | (z[3] as u64);

    // D = ceil(bit_length/64) + 1
    let d = (bit_length.div_ceil(64) + 1) as usize;

    // Process D-2 full 64-bit message blocks
    let mut eval: u64 = 0;
    for i in 0..(d.saturating_sub(2).min(message.len() / 8)) {
        let m = u64::from_be_bytes(
            message[8 * i..8 * (i + 1)]
                .try_into()
                .expect("8-byte aligned message block"),
        );
        eval ^= m;
        eval = mul64(eval, p);
    }

    // Last partial block (zero-padded to 8 bytes)
    if d >= 2 {
        let start = 8 * (d - 2);
        let mut tmp = [0u8; 8];
        let remaining = message.len().saturating_sub(start).min(8);
        if remaining > 0 {
            tmp[..remaining].copy_from_slice(&message[start..start + remaining]);
        }
        let m = u64::from_be_bytes(tmp);
        eval ^= m;
        eval = mul64(eval, p);
    }

    // Finalize: XOR with bit length, multiply by Q
    eval ^= bit_length;
    eval = mul64(eval, q);
    // MAC-I = upper 32 bits XOR z[4]
    ((eval >> 32) as u32) ^ z[4]
}

/// Multiplication in GF(2^64) with reduction polynomial x^64 + x^4 + x^3 + x + 1
fn mul64(mut a: u64, mut b: u64) -> u64 {
    let mut result: u64 = 0;
    for _ in 0..64 {
        if b & 1 != 0 {
            result ^= a;
        }
        b >>= 1;
        let carry = a >> 63;
        a <<= 1;
        if carry != 0 {
            a ^= 0x1B; // x^4 + x^3 + x + 1
        }
    }
    result
}

// ── NIA2: 128-EIA2 (AES-CMAC) ─────────────────────────────────────────────────

/// Compute NAS-MAC using NIA2 (TS 33.501 Annex B.4.2 / TS 35.217)
///
/// Input block M = COUNT[31:0] || BEARER[4:0] || DIRECTION[0] || 0*26 || message
/// Returns the 32-bit MAC-I (first 4 bytes of AES-CMAC output).
pub fn nia2_mac(key: &[u8; 16], count: u32, bearer: u8, direction: u8, message: &[u8]) -> u32 {
    let mut m = vec![0u8; 8 + message.len()];
    m[0..4].copy_from_slice(&count.to_be_bytes());
    m[4] = ((bearer & 0x1F) << 3) | ((direction & 0x01) << 2);
    // bytes 5-7: zeros
    m[8..].copy_from_slice(message);

    let mut mac: Cmac<Aes128> =
        cmac::Mac::new_from_slice(key).expect("key is &[u8; 16] — always valid for AES-128-CMAC");
    cmac::Mac::update(&mut mac, &m);
    let result = cmac::Mac::finalize(mac).into_bytes();

    u32::from_be_bytes(
        result[0..4]
            .try_into()
            .expect("CMAC output is at least 4 bytes"),
    )
}

// ── NIA3: 128-EIA3 (ZUC MAC) ──────────────────────────────────────────────────

/// Compute NAS-MAC using NIA3 / 128-EIA3 (TS 35.221 §4.3, TS 35.222)
///
/// IV construction (matching free5gc security.go):
///   Bytes [0..3]:  COUNT (big-endian)
///   Byte  [4]:     (BEARER & 0x1F) << 3
///   Bytes [5..7]:  0x000000
///   Byte  [8]:     COUNT_byte0 XOR (DIRECTION << 7)
///   Bytes [9..13]: COUNT_byte1..BEARER_byte (iv[1..5])
///   Byte  [14]:    iv[6] XOR (DIRECTION << 7)  = DIRECTION << 7 (since iv[6]=0)
///   Byte  [15]:    0x00
///
/// `bit_length`: number of message bits to authenticate (message must be
/// zero-padded if bit_length < message.len() * 8).
pub fn nia3_mac(
    key: &[u8; 16],
    count: u32,
    bearer: u8,
    direction: u8,
    message: &[u8],
    bit_length: u64,
) -> u32 {
    let mut iv = [0u8; 16];
    let count_bytes = count.to_be_bytes();
    iv[0..4].copy_from_slice(&count_bytes);
    iv[4] = (bearer & 0x1F) << 3;
    // iv[5..7] = 0

    // Upper half: iv[8..16] derived from iv[0..8] with direction bit
    iv[8] = iv[0] ^ (direction << 7);
    for i in 1..8 {
        iv[i + 8] = iv[i];
    }
    iv[14] ^= direction << 7;

    // L = ceil(bit_length / 32), generate L + 2 keystream words
    let l = bit_length.div_ceil(32) as usize;
    let n_words = l + 2;
    let mut zuc = Zuc::new(key, &iv);
    let stream = zuc.generate(n_words);

    // Compute MAC using bit-level getWord (matching free5gc genMac)
    let mut t: u32 = 0;

    // For each bit i in [0, bit_length): if bit i is 1, XOR in getWord(stream, i)
    for i in 0..bit_length as usize {
        let byte_idx = i / 8;
        let bit_idx = 7 - (i % 8); // MSB first
        if byte_idx < message.len() && (message[byte_idx] >> bit_idx) & 1 == 1 {
            t ^= get_word(&stream, i);
        }
    }

    // XOR with getWord(stream, bit_length) and stream[L+1]
    t ^= get_word(&stream, bit_length as usize);
    t ^= stream[l + 1];

    t
}

/// Extract a 32-bit word from stream starting at bit position i (MSB-first).
/// If i is 32-bit-aligned, returns stream[i/32].
/// Otherwise, spans two consecutive words.
#[inline]
fn get_word(stream: &[u32], i: usize) -> u32 {
    let word_idx = i / 32;
    let bit_off = i % 32;
    if bit_off == 0 {
        stream[word_idx]
    } else {
        (stream[word_idx] << bit_off) | (stream[word_idx + 1] >> (32 - bit_off))
    }
}

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

    // ── NIA1 test vectors from free5gc (3GPP TS 35.215/35.216) ────────────────

    #[test]
    fn test_nia1_tc1() {
        let key: [u8; 16] = [
            0x2b, 0xd6, 0x45, 0x9f, 0x82, 0xc5, 0xb3, 0x00, 0x95, 0x2c, 0x49, 0x10, 0x48, 0x81,
            0xff, 0x48,
        ];
        let msg: &[u8] = &[
            0x33, 0x32, 0x34, 0x62, 0x63, 0x39, 0x38, 0x61, 0x37, 0x34, 0x79, 0x00, 0x00, 0x00,
            0x00, 0x00,
        ];
        assert_eq!(nia1_mac(&key, 0x38a6f056, 0x1f, 0, msg, 88), 0x731f1165);
    }

    #[test]
    fn test_nia1_tc2() {
        let key: [u8; 16] = [
            0x7e, 0x5e, 0x94, 0x43, 0x1e, 0x11, 0xd7, 0x38, 0x28, 0xd7, 0x39, 0xcc, 0x6c, 0xed,
            0x45, 0x73,
        ];
        let msg: &[u8] = &[
            0xb3, 0xd3, 0xc9, 0x17, 0x0a, 0x4e, 0x16, 0x32, 0xf6, 0x0f, 0x86, 0x10, 0x13, 0xd2,
            0x2d, 0x84, 0xb7, 0x26, 0xb6, 0xa2, 0x78, 0xd8, 0x02, 0xd1, 0xee, 0xaf, 0x13, 0x21,
            0xba, 0x59, 0x29, 0xdc,
        ];
        assert_eq!(nia1_mac(&key, 0x36af6144, 0x18, 1, msg, 254), 0xe3259f6f);
    }

    #[test]
    fn test_nia1_tc3() {
        let key: [u8; 16] = [
            0xd3, 0x41, 0x9b, 0xe8, 0x21, 0x08, 0x7a, 0xcd, 0x02, 0x12, 0x3a, 0x92, 0x48, 0x03,
            0x33, 0x59,
        ];
        let msg: &[u8] = &[
            0xbb, 0xb0, 0x57, 0x03, 0x88, 0x09, 0x49, 0x6b, 0xcf, 0xf8, 0x6d, 0x6f, 0xbc, 0x8c,
            0xe5, 0xb1, 0x35, 0xa0, 0x6b, 0x16, 0x60, 0x54, 0xf2, 0xd5, 0x65, 0xbe, 0x8a, 0xce,
            0x75, 0xdc, 0x85, 0x1e, 0x0b, 0xcd, 0xd8, 0xf0, 0x71, 0x41, 0xc4, 0x95, 0x87, 0x2f,
            0xb5, 0xd8, 0xc0, 0xc6, 0x6a, 0x8b, 0x6d, 0xa5, 0x56, 0x66, 0x3e, 0x4e, 0x46, 0x12,
            0x05, 0xd8, 0x45, 0x80, 0xbe, 0xe5, 0xbc, 0x7e,
        ];
        assert_eq!(nia1_mac(&key, 0xc7590ea9, 0x17, 0, msg, 511), 0x9a16c77d);
    }

    #[test]
    fn test_nia1_tc4() {
        let key: [u8; 16] = [
            0x83, 0xfd, 0x23, 0xa2, 0x44, 0xa7, 0x4c, 0xf3, 0x58, 0xda, 0x30, 0x19, 0xf1, 0x72,
            0x26, 0x35,
        ];
        let msg: &[u8] = &[
            0x35, 0xc6, 0x87, 0x16, 0x63, 0x3c, 0x66, 0xfb, 0x75, 0x0c, 0x26, 0x68, 0x65, 0xd5,
            0x3c, 0x11, 0xea, 0x05, 0xb1, 0xe9, 0xfa, 0x49, 0xc8, 0x39, 0x8d, 0x48, 0xe1, 0xef,
            0xa5, 0x90, 0x9d, 0x39, 0x47, 0x90, 0x28, 0x37, 0xf5, 0xae, 0x96, 0xd5, 0xa0, 0x5b,
            0xc8, 0xd6, 0x1c, 0xa8, 0xdb, 0xef, 0x1b, 0x13, 0xa4, 0xb4, 0xab, 0xfe, 0x4f, 0xb1,
            0x00, 0x60, 0x45, 0xb6, 0x74, 0xbb, 0x54, 0x72, 0x93, 0x04, 0xc3, 0x82, 0xbe, 0x53,
            0xa5, 0xaf, 0x05, 0x55, 0x61, 0x76, 0xf6, 0xea, 0xa2, 0xef, 0x1d, 0x05, 0xe4, 0xb0,
            0x83, 0x18, 0x1e, 0xe6, 0x74, 0xcd, 0xa5, 0xa4, 0x85, 0xf7, 0x4d, 0x7a,
        ];
        assert_eq!(nia1_mac(&key, 0x36af6144, 0x0f, 1, msg, 768), 0xbba74492);
    }

    #[test]
    fn test_nia1_tc5() {
        let key: [u8; 16] = [
            0x68, 0x32, 0xa6, 0x5c, 0xff, 0x44, 0x73, 0x62, 0x1e, 0xbd, 0xd4, 0xba, 0x26, 0xa9,
            0x21, 0xfe,
        ];
        let msg: &[u8] = &[
            0xd3, 0xc5, 0x38, 0x39, 0x62, 0x68, 0x20, 0x71, 0x77, 0x65, 0x66, 0x76, 0x20, 0x32,
            0x38, 0x37, 0x63, 0x62, 0x40, 0x98, 0x1b, 0xa6, 0x82, 0x4c, 0x1b, 0xfb, 0x1a, 0xb4,
            0x85, 0x47, 0x20, 0x29, 0xb7, 0x1d, 0x80, 0x8c, 0xe3, 0x3e, 0x2c, 0xc3, 0xc0, 0xb5,
            0xfc, 0x1f, 0x3d, 0xe8, 0xa6, 0xdc,
        ];
        assert_eq!(nia1_mac(&key, 0x36af6144, 0x18, 0, msg, 383), 0x4145e4b0);
    }

    // ── NIA2 test vectors from free5gc (3GPP TS 33.501 Annex B.4.2) ───────────

    #[test]
    fn test_nia2_tc1() {
        let key: [u8; 16] = [
            0xd3, 0xc5, 0xd5, 0x92, 0x32, 0x7f, 0xb1, 0x1c, 0x40, 0x35, 0xc6, 0x68, 0x0a, 0xf8,
            0xc6, 0xd1,
        ];
        let msg: &[u8] = &[0x48, 0x45, 0x83, 0xd5, 0xaf, 0xe0, 0x82, 0xae];
        assert_eq!(nia2_mac(&key, 0x398a59b4, 0x1a, 1, msg), 0xb93787e6);
    }

    #[test]
    fn test_nia2_tc2() {
        let key: [u8; 16] = [
            0xb3, 0x12, 0x0f, 0xfd, 0xb2, 0xcf, 0x6a, 0xf4, 0xe7, 0x3e, 0xaf, 0x2e, 0xf4, 0xeb,
            0xec, 0x69,
        ];
        let msg: &[u8] = &[
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
            0x01, 0x01, 0xe0, 0x95, 0x80, 0x45, 0xf3, 0xa0, 0xbb, 0xa4, 0xe3, 0x96, 0x83, 0x46,
            0xf0, 0xa3, 0xb8, 0xa7, 0xc0, 0x2a, 0x01, 0x8a, 0xe6, 0x40, 0x76, 0x52, 0x26, 0xb9,
            0x87, 0xc9, 0x13, 0xe6, 0xcb, 0xf0, 0x83, 0x57, 0x00, 0x16, 0xcf, 0x83, 0xef, 0xbc,
            0x61, 0xc0, 0x82, 0x51, 0x3e, 0x21, 0x56, 0x1a, 0x42, 0x7c, 0x00, 0x9d, 0x28, 0xc2,
            0x98, 0xef, 0xac, 0xe7, 0x8e, 0xd6, 0xd5, 0x6c, 0x2d, 0x45, 0x05, 0xad, 0x03, 0x2e,
            0x9c, 0x04, 0xdc, 0x60, 0xe7, 0x3a, 0x81, 0x69, 0x6d, 0xa6, 0x65, 0xc6, 0xc4, 0x86,
            0x03, 0xa5, 0x7b, 0x45, 0xab, 0x33, 0x22, 0x15, 0x85, 0xe6, 0x8e, 0xe3, 0x16, 0x91,
            0x87, 0xfb, 0x02, 0x39, 0x52, 0x86, 0x32, 0xdd, 0x65, 0x6c, 0x80, 0x7e, 0xa3, 0x24,
            0x8b, 0x7b, 0x46, 0xd0, 0x02, 0xb2, 0xb5, 0xc7, 0x45, 0x8e, 0xb8, 0x5b, 0x9c, 0xe9,
            0x58, 0x79, 0xe0, 0x34, 0x08, 0x59, 0x05, 0x5e, 0x3b, 0x0a, 0xbb, 0xc3, 0xea, 0xce,
            0x87, 0x19, 0xca, 0xa8, 0x02, 0x65, 0xc9, 0x72, 0x05, 0xd5, 0xdc, 0x4b, 0xcc, 0x90,
            0x2f, 0xe1, 0x83, 0x96, 0x29, 0xed, 0x71, 0x32, 0x8a, 0x0f, 0x04, 0x49, 0xf5, 0x88,
            0x55, 0x7e, 0x68, 0x98, 0x86, 0x0e, 0x04, 0x2a, 0xec, 0xd8, 0x4b, 0x24, 0x04, 0xc2,
            0x12, 0xc9, 0x22, 0x2d, 0xa5, 0xbf, 0x8a, 0x89, 0xef, 0x67, 0x97, 0x87, 0x0c, 0xf5,
            0x07, 0x71, 0xa6, 0x0f, 0x66, 0xa2, 0xee, 0x62, 0x85, 0x36, 0x57, 0xad, 0xdf, 0x04,
            0xcd, 0xde, 0x07, 0xfa, 0x41, 0x4e, 0x11, 0xf1, 0x2b, 0x4d, 0x81, 0xb9, 0xb4, 0xe8,
            0xac, 0x53, 0x8e, 0xa3, 0x06, 0x66, 0x68, 0x8d, 0x88, 0x1f, 0x6c, 0x34, 0x84, 0x21,
            0x99, 0x2f, 0x31, 0xb9, 0x4f, 0x88, 0x06, 0xed, 0x8f, 0xcc, 0xff, 0x4c, 0x91, 0x23,
            0xb8, 0x96, 0x42, 0x52, 0x7a, 0xd6, 0x13, 0xb1, 0x09, 0xbf, 0x75, 0x16, 0x74, 0x85,
            0xf1, 0x26, 0x8b, 0xf8, 0x84, 0xb4, 0xcd, 0x23, 0xd2, 0x9a, 0x09, 0x34, 0x92, 0x57,
            0x03, 0xd6, 0x34, 0x09, 0x8f, 0x77, 0x67, 0xf1, 0xbe, 0x74, 0x91, 0xe7, 0x08, 0xa8,
            0xbb, 0x94, 0x9a, 0x38, 0x73, 0x70, 0x8a, 0xef, 0x4a, 0x36, 0x23, 0x9e, 0x50, 0xcc,
            0x08, 0x23, 0x5c, 0xd5, 0xed, 0x6b, 0xbe, 0x57, 0x86, 0x68, 0xa1, 0x7b, 0x58, 0xc1,
            0x17, 0x1d, 0x0b, 0x90, 0xe8, 0x13, 0xa9, 0xe4, 0xf5, 0x8a, 0x89, 0xd7, 0x19, 0xb1,
            0x10, 0x42, 0xd6, 0x36, 0x0b, 0x1b, 0x0f, 0x52, 0xde, 0xb7, 0x30, 0xa5, 0x8d, 0x58,
            0xfa, 0xf4, 0x63, 0x15, 0x95, 0x4b, 0x0a, 0x87, 0x26, 0x91, 0x47, 0x59, 0x77, 0xdc,
            0x88, 0xc0, 0xd7, 0x33, 0xfe, 0xff, 0x54, 0x60, 0x0a, 0x0c, 0xc1, 0xd0, 0x30, 0x0a,
            0xaa, 0xeb, 0x94, 0x57, 0x2c, 0x6e, 0x95, 0xb0, 0x1a, 0xe9, 0x0d, 0xe0, 0x4f, 0x1d,
            0xce, 0x47, 0xf8, 0x7e, 0x8f, 0xa7, 0xbe, 0xbf, 0x77, 0xe1, 0xdb, 0xc2, 0x0d, 0x6b,
            0xa8, 0x5c, 0xb9, 0x14, 0x3d, 0x51, 0x8b, 0x28, 0x5d, 0xfa, 0x04, 0xb6, 0x98, 0xbf,
            0x0c, 0xf7, 0x81, 0x9f, 0x20, 0xfa, 0x7a, 0x28, 0x8e, 0xb0, 0x70, 0x3d, 0x99, 0x5c,
            0x59, 0x94, 0x0c, 0x7c, 0x66, 0xde, 0x57, 0xa9, 0xb7, 0x0f, 0x82, 0x37, 0x9b, 0x70,
            0xe2, 0x03, 0x1e, 0x45, 0x0f, 0xcf, 0xd2, 0x18, 0x13, 0x26, 0xfc, 0xd2, 0x8d, 0x88,
            0x23, 0xba, 0xaa, 0x80, 0xdf, 0x6e, 0x0f, 0x44, 0x35, 0x59, 0x64, 0x75, 0x39, 0xfd,
            0x89, 0x07, 0xc0, 0xff, 0xd9, 0xd7, 0x9c, 0x13, 0x0e, 0xd8, 0x1c, 0x9a, 0xfd, 0x9b,
            0x7e, 0x84, 0x8c, 0x9f, 0xed, 0x38, 0x44, 0x3d, 0x5d, 0x38, 0x0e, 0x53, 0xfb, 0xdb,
            0x8a, 0xc8, 0xc3, 0xd3, 0xf0, 0x68, 0x76, 0x05, 0x4f, 0x12, 0x24, 0x61, 0x10, 0x7d,
            0xe9, 0x2f, 0xea, 0x09, 0xc6, 0xf6, 0x92, 0x3a, 0x18, 0x8d, 0x53, 0xaf, 0xe5, 0x4a,
            0x10, 0xf6, 0x0e, 0x6e, 0x9d, 0x5a, 0x03, 0xd9, 0x96, 0xb5, 0xfb, 0xc8, 0x20, 0xf8,
            0xa6, 0x37, 0x11, 0x6a, 0x27, 0xad, 0x04, 0xb4, 0x44, 0xa0, 0x93, 0x2d, 0xd6, 0x0f,
            0xbd, 0x12, 0x67, 0x1c, 0x11, 0xe1, 0xc0, 0xec, 0x73, 0xe7, 0x89, 0x87, 0x9f, 0xaa,
            0x3d, 0x42, 0xc6, 0x4d, 0x20, 0xcd, 0x12, 0x52, 0x74, 0x2a, 0x37, 0x68, 0xc2, 0x5a,
            0x90, 0x15, 0x85, 0x88, 0x8e, 0xce, 0xe1, 0xe6, 0x12, 0xd9, 0x93, 0x6b, 0x40, 0x3b,
            0x07, 0x75, 0x94, 0x9a, 0x66, 0xcd, 0xfd, 0x99, 0xa2, 0x9b, 0x13, 0x45, 0xba, 0xa8,
            0xd9, 0xd5, 0x40, 0x0c, 0x91, 0x02, 0x4b, 0x0a, 0x60, 0x73, 0x63, 0xb0, 0x13, 0xce,
            0x5d, 0xe9, 0xae, 0x86, 0x9d, 0x3b, 0x8d, 0x95, 0xb0, 0x57, 0x0b, 0x3c, 0x2d, 0x39,
            0x14, 0x22, 0xd3, 0x24, 0x50, 0xcb, 0xcf, 0xae, 0x96, 0x65, 0x22, 0x86, 0xe9, 0x6d,
            0xec, 0x12, 0x14, 0xa9, 0x34, 0x65, 0x27, 0x98, 0x0a, 0x81, 0x92, 0xea, 0xc1, 0xc3,
            0x9a, 0x3a, 0xaf, 0x6f, 0x15, 0x35, 0x1d, 0xa6, 0xbe, 0x76, 0x4d, 0xf8, 0x97, 0x72,
            0xec, 0x04, 0x07, 0xd0, 0x6e, 0x44, 0x15, 0xbe, 0xfa, 0xe7, 0xc9, 0x25, 0x80, 0xdf,
            0x9b, 0xf5, 0x07, 0x49, 0x7c, 0x8f, 0x29, 0x95, 0x16, 0x0d, 0x4e, 0x21, 0x8d, 0xaa,
            0xcb, 0x02, 0x94, 0x4a, 0xbf, 0x83, 0x34, 0x0c, 0xe8, 0xbe, 0x16, 0x86, 0xa9, 0x60,
            0xfa, 0xf9, 0x0e, 0x2d, 0x90, 0xc5, 0x5c, 0xc6, 0x47, 0x5b, 0xab, 0xc3, 0x17, 0x1a,
            0x80, 0xa3, 0x63, 0x17, 0x49, 0x54, 0x95, 0x5d, 0x71, 0x01, 0xda, 0xb1, 0x6a, 0xe8,
            0x17, 0x91, 0x67, 0xe2, 0x14, 0x44, 0xb4, 0x43, 0xa9, 0xea, 0xaa, 0x7c, 0x91, 0xde,
            0x36, 0xd1, 0x18, 0xc3, 0x9d, 0x38, 0x9f, 0x8d, 0xd4, 0x46, 0x9a, 0x84, 0x6c, 0x9a,
            0x26, 0x2b, 0xf7, 0xfa, 0x18, 0x48, 0x7a, 0x79, 0xe8, 0xde, 0x11, 0x69, 0x9e, 0x0b,
            0x8f, 0xdf, 0x55, 0x7c, 0xb4, 0x87, 0x19, 0xd4, 0x53, 0xba, 0x71, 0x30, 0x56, 0x10,
            0x9b, 0x93, 0xa2, 0x18, 0xc8, 0x96, 0x75, 0xac, 0x19, 0x5f, 0xb4, 0xfb, 0x06, 0x63,
            0x9b, 0x37, 0x97, 0x14, 0x49, 0x55, 0xb3, 0xc9, 0x32, 0x7d, 0x1a, 0xec, 0x00, 0x3d,
            0x42, 0xec, 0xd0, 0xea, 0x98, 0xab, 0xf1, 0x9f, 0xfb, 0x4a, 0xf3, 0x56, 0x1a, 0x67,
            0xe7, 0x7c, 0x35, 0xbf, 0x15, 0xc5, 0x9c, 0x24, 0x12, 0xda, 0x88, 0x1d, 0xb0, 0x2b,
            0x1b, 0xfb, 0xce, 0xbf, 0xac, 0x51, 0x52, 0xbc, 0x99, 0xbc, 0x3f, 0x1d, 0x15, 0xf7,
            0x71, 0x00, 0x1b, 0x70, 0x29, 0xfe, 0xdb, 0x02, 0x8f, 0x8b, 0x85, 0x2b, 0xc4, 0x40,
            0x7e, 0xb8, 0x3f, 0x89, 0x1c, 0x9c, 0xa7, 0x33, 0x25, 0x4f, 0xdd, 0x1e, 0x9e, 0xdb,
            0x56, 0x91, 0x9c, 0xe9, 0xfe, 0xa2, 0x1c, 0x17, 0x40, 0x72, 0x52, 0x1c, 0x18, 0x31,
            0x9a, 0x54, 0xb5, 0xd4, 0xef, 0xbe, 0xbd, 0xdf, 0x1d, 0x8b, 0x69, 0xb1, 0xcb, 0xf2,
            0x5f, 0x48, 0x9f, 0xcc, 0x98, 0x13, 0x72, 0x54, 0x7c, 0xf4, 0x1d, 0x00, 0x8e, 0xf0,
            0xbc, 0xa1, 0x92, 0x6f, 0x93, 0x4b, 0x73, 0x5e, 0x09, 0x0b, 0x3b, 0x25, 0x1e, 0xb3,
            0x3a, 0x36, 0xf8, 0x2e, 0xd9, 0xb2, 0x9c, 0xf4, 0xcb, 0x94, 0x41, 0x88, 0xfa, 0x0e,
            0x1e, 0x38, 0xdd, 0x77, 0x8f, 0x7d, 0x1c, 0x9d, 0x98, 0x7b, 0x28, 0xd1, 0x32, 0xdf,
            0xb9, 0x73, 0x1f, 0xa4, 0xf4, 0xb4, 0x16, 0x93, 0x5b, 0xe4, 0x9d, 0xe3, 0x05, 0x16,
            0xaf, 0x35, 0x78, 0x58, 0x1f, 0x2f, 0x13, 0xf5, 0x61, 0xc0, 0x66, 0x33, 0x61, 0x94,
            0x1e, 0xab, 0x24, 0x9a, 0x4b, 0xc1, 0x23, 0xf8, 0xd1, 0x5c, 0xd7, 0x11, 0xa9, 0x56,
            0xa1, 0xbf, 0x20, 0xfe, 0x6e, 0xb7, 0x8a, 0xea, 0x23, 0x73, 0x36, 0x1d, 0xa0, 0x42,
            0x6c, 0x79, 0xa5, 0x30, 0xc3, 0xbb, 0x1d, 0xe0, 0xc9, 0x97, 0x22, 0xef, 0x1f, 0xde,
            0x39, 0xac, 0x2b, 0x00, 0xa0, 0xa8, 0xee, 0x7c, 0x80, 0x0a, 0x08, 0xbc, 0x22, 0x64,
            0xf8, 0x9f, 0x4e, 0xff, 0xe6, 0x27, 0xac, 0x2f, 0x05, 0x31, 0xfb, 0x55, 0x4f, 0x6d,
            0x21, 0xd7, 0x4c, 0x59, 0x0a, 0x70, 0xad, 0xfa, 0xa3, 0x90, 0xbd, 0xfb, 0xb3, 0xd6,
            0x8e, 0x46, 0x21, 0x5c, 0xab, 0x18, 0x7d, 0x23, 0x68, 0xd5, 0xa7, 0x1f, 0x5e, 0xbe,
            0xc0, 0x81, 0xcd, 0x3b, 0x20, 0xc0, 0x82, 0xdb, 0xe4, 0xcd, 0x2f, 0xac, 0xa2, 0x87,
            0x73, 0x79, 0x5d, 0x6b, 0x0c, 0x10, 0x20, 0x4b, 0x65, 0x9a, 0x93, 0x9e, 0xf2, 0x9b,
            0xbe, 0x10, 0x88, 0x24, 0x36, 0x24, 0x42, 0x99, 0x27, 0xa7, 0xeb, 0x57, 0x6d, 0xd3,
            0xa0, 0x0e, 0xa5, 0xe0, 0x1a, 0xf5, 0xd4, 0x75, 0x83, 0xb2, 0x27, 0x2c, 0x0c, 0x16,
            0x1a, 0x80, 0x65, 0x21, 0xa1, 0x6f, 0xf9, 0xb0, 0xa7, 0x22, 0xc0, 0xcf, 0x26, 0xb0,
            0x25, 0xd5, 0x83, 0x6e, 0x22, 0x58, 0xa4, 0xf7, 0xd4, 0x77, 0x3a, 0xc8, 0x01, 0xe4,
            0x26, 0x3b, 0xc2, 0x94, 0xf4, 0x3d, 0xef, 0x7f, 0xa8, 0x70, 0x3f, 0x3a, 0x41, 0x97,
            0x46, 0x35, 0x25, 0x88, 0x76, 0x52, 0xb0, 0xb2, 0xa4, 0xa2, 0xa7, 0xcf, 0x87, 0xf0,
            0x09, 0x14, 0x87, 0x1e, 0x25, 0x03, 0x91, 0x13, 0xc7, 0xe1, 0x61, 0x8d, 0xa3, 0x40,
            0x64, 0xb5, 0x7a, 0x43, 0xc4, 0x63, 0x24, 0x9f, 0xb8, 0xd0, 0x5e, 0x0f, 0x26, 0xf4,
            0xa6, 0xd8, 0x49, 0x72, 0xe7, 0xa9, 0x05, 0x48, 0x24, 0x14, 0x5f, 0x91, 0x29, 0x5c,
            0xdb, 0xe3, 0x9a, 0x6f, 0x92, 0x0f, 0xac, 0xc6, 0x59, 0x71, 0x2b, 0x46, 0xa5, 0x4b,
            0xa2, 0x95, 0xbb, 0xe6, 0xa9, 0x01, 0x54, 0xe9, 0x1b, 0x33, 0x98, 0x5a, 0x2b, 0xcd,
            0x42, 0x0a, 0xd5, 0xc6, 0x7e, 0xc9, 0xad, 0x8e, 0xb7, 0xac, 0x68, 0x64, 0xdb, 0x27,
            0x2a, 0x51, 0x6b, 0xc9, 0x4c, 0x28, 0x39, 0xb0, 0xa8, 0x16, 0x9a, 0x6b, 0xf5, 0x8e,
            0x1a, 0x0c, 0x2a, 0xda, 0x8c, 0x88, 0x3b, 0x7b, 0xf4, 0x97, 0xa4, 0x91, 0x71, 0x26,
            0x8e, 0xd1, 0x5d, 0xdd, 0x29, 0x69, 0x38, 0x4e, 0x7f, 0xf4, 0xbf, 0x4a, 0xab, 0x2e,
            0xc9, 0xec, 0xc6, 0x52, 0x9c, 0xf6, 0x29, 0xe2, 0xdf, 0x0f, 0x08, 0xa7, 0x7a, 0x65,
            0xaf, 0xa1, 0x2a, 0xa9, 0xb5, 0x05, 0xdf, 0x8b, 0x28, 0x7e, 0xf6, 0xcc, 0x91, 0x49,
            0x3d, 0x1c, 0xaa, 0x39, 0x07, 0x6e, 0x28, 0xef, 0x1e, 0xa0, 0x28, 0xf5, 0x11, 0x8d,
            0xe6, 0x1a, 0xe0, 0x2b, 0xb6, 0xae, 0xfc, 0x33, 0x43, 0xa0, 0x50, 0x29, 0x2f, 0x19,
            0x9f, 0x40, 0x18, 0x57, 0xb2, 0xbe, 0xad, 0x5e, 0x6e, 0xe2, 0xa1, 0xf1, 0x91, 0x02,
            0x2f, 0x92, 0x78, 0x01, 0x6f, 0x04, 0x77, 0x91, 0xa9, 0xd1, 0x8d, 0xa7, 0xd2, 0xa6,
            0xd2, 0x7f, 0x2e, 0x0e, 0x51, 0xc2, 0xf6, 0xea, 0x30, 0xe8, 0xac, 0x49, 0xa0, 0x60,
            0x4f, 0x4c, 0x13, 0x54, 0x2e, 0x85, 0xb6, 0x83, 0x81, 0xb9, 0xfd, 0xcf, 0xa0, 0xce,
            0x4b, 0x2d, 0x34, 0x13, 0x54, 0x85, 0x2d, 0x36, 0x02, 0x45, 0xc5, 0x36, 0xb6, 0x12,
            0xaf, 0x71, 0xf3, 0xe7, 0x7c, 0x90, 0x95, 0xae, 0x2d, 0xbd, 0xe5, 0x04, 0xb2, 0x65,
            0x73, 0x3d, 0xab, 0xfe, 0x10, 0xa2, 0x0f, 0xc7, 0xd6, 0xd3, 0x2c, 0x21, 0xcc, 0xc7,
            0x2b, 0x8b, 0x34, 0x44, 0xae, 0x66, 0x3d, 0x65, 0x92, 0x2d, 0x17, 0xf8, 0x2c, 0xaa,
            0x2b, 0x86, 0x5c, 0xd8, 0x89, 0x13, 0xd2, 0x91, 0xa6, 0x58, 0x99, 0x02, 0x6e, 0xa1,
            0x32, 0x84, 0x39, 0x72, 0x3c, 0x19, 0x8c, 0x36, 0xb0, 0xc3, 0xc8, 0xd0, 0x85, 0xbf,
            0xaf, 0x8a, 0x32, 0x0f, 0xde, 0x33, 0x4b, 0x4a, 0x49, 0x19, 0xb4, 0x4c, 0x2b, 0x95,
            0xf6, 0xe8, 0xec, 0xf7, 0x33, 0x93, 0xf7, 0xf0, 0xd2, 0xa4, 0x0e, 0x60, 0xb1, 0xd4,
            0x06, 0x52, 0x6b, 0x02, 0x2d, 0xdc, 0x33, 0x18, 0x10, 0xb1, 0xa5, 0xf7, 0xc3, 0x47,
            0xbd, 0x53, 0xed, 0x1f, 0x10, 0x5d, 0x6a, 0x0d, 0x30, 0xab, 0xa4, 0x77, 0xe1, 0x78,
            0x88, 0x9a, 0xb2, 0xec, 0x55, 0xd5, 0x58, 0xde, 0xab, 0x26, 0x30, 0x20, 0x43, 0x36,
            0x96, 0x2b, 0x4d, 0xb5, 0xb6, 0x63, 0xb6, 0x90, 0x2b, 0x89, 0xe8, 0x5b, 0x31, 0xbc,
            0x6a, 0xf5, 0x0f, 0xc5, 0x0a, 0xcc, 0xb3, 0xfb, 0x9b, 0x57, 0xb6, 0x63, 0x29, 0x70,
            0x31, 0x37, 0x8d, 0xb4, 0x78, 0x96, 0xd7, 0xfb, 0xaf, 0x6c, 0x60, 0x0a, 0xdd, 0x2c,
            0x67, 0xf9, 0x36, 0xdb, 0x03, 0x79, 0x86, 0xdb, 0x85, 0x6e, 0xb4, 0x9c, 0xf2, 0xdb,
            0x3f, 0x7d, 0xa6, 0xd2, 0x36, 0x50, 0xe4, 0x38, 0xf1, 0x88, 0x40, 0x41, 0xb0, 0x13,
            0x11, 0x9e, 0x4c, 0x2a, 0xe5, 0xaf, 0x37, 0xcc, 0xcd, 0xfb, 0x68, 0x66, 0x07, 0x38,
            0xb5, 0x8b, 0x3c, 0x59, 0xd1, 0xc0, 0x24, 0x84, 0x37, 0x47, 0x2a, 0xba, 0x1f, 0x35,
            0xca, 0x1f, 0xb9, 0x0c, 0xd7, 0x14, 0xaa, 0x9f, 0x63, 0x55, 0x34, 0xf4, 0x9e, 0x7c,
            0x5b, 0xba, 0x81, 0xc2, 0xb6, 0xb3, 0x6f, 0xde, 0xe2, 0x1c, 0xa2, 0x7e, 0x34, 0x7f,
            0x79, 0x3d, 0x2c, 0xe9, 0x44, 0xed, 0xb2, 0x3c, 0x8c, 0x9b, 0x91, 0x4b, 0xe1, 0x03,
            0x35, 0xe3, 0x50, 0xfe, 0xb5, 0x07, 0x03, 0x94, 0xb7, 0xa4, 0xa1, 0x5c, 0x0c, 0xa1,
            0x20, 0x28, 0x35, 0x68, 0xb7, 0xbf, 0xc2, 0x54, 0xfe, 0x83, 0x8b, 0x13, 0x7a, 0x21,
            0x47, 0xce, 0x7c, 0x11, 0x3a, 0x3a, 0x4d, 0x65, 0x49, 0x9d, 0x9e, 0x86, 0xb8, 0x7d,
            0xbc, 0xc7, 0xf0, 0x3b, 0xbd, 0x3a, 0x3a, 0xb1, 0xaa, 0x24, 0x3e, 0xce, 0x5b, 0xa9,
            0xbc, 0xf2, 0x5f, 0x82, 0x83, 0x6c, 0xfe, 0x47, 0x3b, 0x2d, 0x83, 0xe7, 0xa7, 0x20,
            0x1c, 0xd0, 0xb9, 0x6a, 0x72, 0x45, 0x1e, 0x86, 0x3f, 0x6c, 0x3b, 0xa6, 0x64, 0xa6,
            0xd0, 0x73, 0xd1, 0xf7, 0xb5, 0xed, 0x99, 0x08, 0x65, 0xd9, 0x78, 0xbd, 0x38, 0x15,
            0xd0, 0x60, 0x94, 0xfc, 0x9a, 0x2a, 0xba, 0x52, 0x21, 0xc2, 0x2d, 0x5a, 0xb9, 0x96,
            0x38, 0x9e, 0x37, 0x21, 0xe3, 0xaf, 0x5f, 0x05, 0xbe, 0xdd, 0xc2, 0x87, 0x5e, 0x0d,
            0xfa, 0xeb, 0x39, 0x02, 0x1e, 0xe2, 0x7a, 0x41, 0x18, 0x7c, 0xbb, 0x45, 0xef, 0x40,
            0xc3, 0xe7, 0x3b, 0xc0, 0x39, 0x89, 0xf9, 0xa3, 0x0d, 0x12, 0xc5, 0x4b, 0xa7, 0xd2,
            0x14, 0x1d, 0xa8, 0xa8, 0x75, 0x49, 0x3e, 0x65, 0x77, 0x6e, 0xf3, 0x5f, 0x97, 0xde,
            0xbc, 0x22, 0x86, 0xcc, 0x4a, 0xf9, 0xb4, 0x62, 0x3e, 0xee, 0x90, 0x2f, 0x84, 0x0c,
            0x52, 0xf1, 0xb8, 0xad, 0x65, 0x89, 0x39, 0xae, 0xf7, 0x1f, 0x3f, 0x72, 0xb9, 0xec,
            0x1d, 0xe2, 0x15, 0x88, 0xbd, 0x35, 0x48, 0x4e, 0xa4, 0x44, 0x36, 0x34, 0x3f, 0xf9,
            0x5e, 0xad, 0x6a, 0xb1, 0xd8, 0xaf, 0xb1, 0xb2, 0xa3, 0x03, 0xdf, 0x1b, 0x71, 0xe5,
            0x3c, 0x4a, 0xea, 0x6b, 0x2e, 0x3e, 0x93, 0x72, 0xbe, 0x0d, 0x1b, 0xc9, 0x97, 0x98,
            0xb0, 0xce, 0x3c, 0xc1, 0x0d, 0x2a, 0x59, 0x6d, 0x56, 0x5d, 0xba, 0x82, 0xf8, 0x8c,
            0xe4, 0xcf, 0xf3, 0xb3, 0x3d, 0x5d, 0x24, 0xe9, 0xc0, 0x83, 0x11, 0x24, 0xbf, 0x1a,
            0xd5, 0x4b, 0x79, 0x25, 0x32, 0x98, 0x3d, 0xd6, 0xc3, 0xa8, 0xb7, 0xd0,
        ];
        assert_eq!(nia2_mac(&key, 0x296f393c, 0x0b, 1, msg), 0xebd5ccb0);
    }

    // ── NIA3 test vectors from free5gc (3GPP TS 35.221/35.222) ────────────────

    #[test]
    fn test_nia3_tc1() {
        // 1 bit message, all zeros
        let key = [0u8; 16];
        let msg: &[u8] = &[0x00, 0x00, 0x00, 0x00];
        assert_eq!(nia3_mac(&key, 0x0, 0x0, 0, msg, 1), 0xc8a9595e);
    }

    #[test]
    fn test_nia3_tc2() {
        let key: [u8; 16] = [
            0x47, 0x05, 0x41, 0x25, 0x56, 0x1e, 0xb2, 0xdd, 0xa9, 0x40, 0x59, 0xda, 0x05, 0x09,
            0x78, 0x50,
        ];
        let msg: &[u8] = &[
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];
        assert_eq!(nia3_mac(&key, 0x561eb2dd, 0x14, 0, msg, 90), 0x6719a088);
    }

    #[test]
    fn test_nia3_tc3() {
        let key: [u8; 16] = [
            0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb, 0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85,
            0xab, 0x0a,
        ];
        let msg: &[u8] = &[
            0x98, 0x3b, 0x41, 0xd4, 0x7d, 0x78, 0x0c, 0x9e, 0x1a, 0xd1, 0x1d, 0x7e, 0xb7, 0x03,
            0x91, 0xb1, 0xde, 0x0b, 0x35, 0xda, 0x2d, 0xc6, 0x2f, 0x83, 0xe7, 0xb7, 0x8d, 0x63,
            0x06, 0xca, 0x0e, 0xa0, 0x7e, 0x94, 0x1b, 0x7b, 0xe9, 0x13, 0x48, 0xf9, 0xfc, 0xb1,
            0x70, 0xe2, 0x21, 0x7f, 0xec, 0xd9, 0x7f, 0x9f, 0x68, 0xad, 0xb1, 0x6e, 0x5d, 0x7d,
            0x21, 0xe5, 0x69, 0xd2, 0x80, 0xed, 0x77, 0x5c, 0xeb, 0xde, 0x3f, 0x40, 0x93, 0xc5,
            0x38, 0x81, 0x00, 0x00, 0x00, 0x00,
        ];
        assert_eq!(nia3_mac(&key, 0xa94059da, 0x0a, 1, msg, 577), 0xfae8ff0b);
    }

    #[test]
    fn test_nia3_tc4() {
        let key: [u8; 16] = [
            0xc8, 0xa4, 0x82, 0x62, 0xd0, 0xc2, 0xe2, 0xba, 0xc4, 0xb9, 0x6e, 0xf7, 0x7e, 0x80,
            0xca, 0x59,
        ];
        let msg: &[u8] = &[
            0xb5, 0x46, 0x43, 0x0b, 0xf8, 0x7b, 0x4f, 0x1e, 0xe8, 0x34, 0x70, 0x4c, 0xd6, 0x95,
            0x1c, 0x36, 0xe2, 0x6f, 0x10, 0x8c, 0xf7, 0x31, 0x78, 0x8f, 0x48, 0xdc, 0x34, 0xf1,
            0x67, 0x8c, 0x05, 0x22, 0x1c, 0x8f, 0xa7, 0xff, 0x2f, 0x39, 0xf4, 0x77, 0xe7, 0xe4,
            0x9e, 0xf6, 0x0a, 0x4e, 0xc2, 0xc3, 0xde, 0x24, 0x31, 0x2a, 0x96, 0xaa, 0x26, 0xe1,
            0xcf, 0xba, 0x57, 0x56, 0x38, 0x38, 0xb2, 0x97, 0xf4, 0x7e, 0x85, 0x10, 0xc7, 0x79,
            0xfd, 0x66, 0x54, 0xb1, 0x43, 0x38, 0x6f, 0xa6, 0x39, 0xd3, 0x1e, 0xdb, 0xd6, 0xc0,
            0x6e, 0x47, 0xd1, 0x59, 0xd9, 0x43, 0x62, 0xf2, 0x6a, 0xee, 0xed, 0xee, 0x0e, 0x4f,
            0x49, 0xd9, 0xbf, 0x84, 0x12, 0x99, 0x54, 0x15, 0xbf, 0xad, 0x56, 0xee, 0x82, 0xd1,
            0xca, 0x74, 0x63, 0xab, 0xf0, 0x85, 0xb0, 0x82, 0xb0, 0x99, 0x04, 0xd6, 0xd9, 0x90,
            0xd4, 0x3c, 0xf2, 0xe0, 0x62, 0xf4, 0x08, 0x39, 0xd9, 0x32, 0x48, 0xb1, 0xeb, 0x92,
            0xcd, 0xfe, 0xd5, 0x30, 0x0b, 0xc1, 0x48, 0x28, 0x04, 0x30, 0xb6, 0xd0, 0xca, 0xa0,
            0x94, 0xb6, 0xec, 0x89, 0x11, 0xab, 0x7d, 0xc3, 0x68, 0x24, 0xb8, 0x24, 0xdc, 0x0a,
            0xf6, 0x68, 0x2b, 0x09, 0x35, 0xfd, 0xe7, 0xb4, 0x92, 0xa1, 0x4d, 0xc2, 0xf4, 0x36,
            0x48, 0x03, 0x8d, 0xa2, 0xcf, 0x79, 0x17, 0x0d, 0x2d, 0x50, 0x13, 0x3f, 0xd4, 0x94,
            0x16, 0xcb, 0x6e, 0x33, 0xbe, 0xa9, 0x0b, 0x8b, 0xf4, 0x55, 0x9b, 0x03, 0x73, 0x2a,
            0x01, 0xea, 0x29, 0x0e, 0x6d, 0x07, 0x4f, 0x79, 0xbb, 0x83, 0xc1, 0x0e, 0x58, 0x00,
            0x15, 0xcc, 0x1a, 0x85, 0xb3, 0x6b, 0x55, 0x01, 0x04, 0x6e, 0x9c, 0x4b, 0xdc, 0xae,
            0x51, 0x35, 0x69, 0x0b, 0x86, 0x66, 0xbd, 0x54, 0xb7, 0xa7, 0x03, 0xea, 0x7b, 0x6f,
            0x22, 0x0a, 0x54, 0x69, 0xa5, 0x68, 0x02, 0x7e,
        ];
        assert_eq!(nia3_mac(&key, 0x05097850, 0x10, 1, msg, 2079), 0x004ac4d6);
    }

    #[test]
    fn test_nia3_tc5() {
        let key: [u8; 16] = [
            0x6b, 0x8b, 0x08, 0xee, 0x79, 0xe0, 0xb5, 0x98, 0x2d, 0x6d, 0x12, 0x8e, 0xa9, 0xf2,
            0x20, 0xcb,
        ];
        let msg: &[u8] = &[
            0x5b, 0xad, 0x72, 0x47, 0x10, 0xba, 0x1c, 0x56, 0xd5, 0xa3, 0x15, 0xf8, 0xd4, 0x0f,
            0x6e, 0x09, 0x37, 0x80, 0xbe, 0x8e, 0x8d, 0xe0, 0x7b, 0x69, 0x92, 0x43, 0x20, 0x18,
            0xe0, 0x8e, 0xd9, 0x6a, 0x57, 0x34, 0xaf, 0x8b, 0xad, 0x8a, 0x57, 0x5d, 0x3a, 0x1f,
            0x16, 0x2f, 0x85, 0x04, 0x5c, 0xc7, 0x70, 0x92, 0x55, 0x71, 0xd9, 0xf5, 0xb9, 0x4e,
            0x45, 0x4a, 0x77, 0xc1, 0x6e, 0x72, 0x93, 0x6b, 0xf0, 0x16, 0xae, 0x15, 0x74, 0x99,
            0xf0, 0x54, 0x3b, 0x5d, 0x52, 0xca, 0xa6, 0xdb, 0xea, 0xb6, 0x97, 0xd2, 0xbb, 0x73,
            0xe4, 0x1b, 0x80, 0x75, 0xdc, 0xe7, 0x9b, 0x4b, 0x86, 0x04, 0x4f, 0x66, 0x1d, 0x44,
            0x85, 0xa5, 0x43, 0xdd, 0x78, 0x60, 0x6e, 0x04, 0x19, 0xe8, 0x05, 0x98, 0x59, 0xd3,
            0xcb, 0x2b, 0x67, 0xce, 0x09, 0x77, 0x60, 0x3f, 0x81, 0xff, 0x83, 0x9e, 0x33, 0x18,
            0x59, 0x54, 0x4c, 0xfb, 0xc8, 0xd0, 0x0f, 0xef, 0x1a, 0x4c, 0x85, 0x10, 0xfb, 0x54,
            0x7d, 0x6b, 0x06, 0xc6, 0x11, 0xef, 0x44, 0xf1, 0xbc, 0xe1, 0x07, 0xcf, 0xa4, 0x5a,
            0x06, 0xaa, 0xb3, 0x60, 0x15, 0x2b, 0x28, 0xdc, 0x1e, 0xbe, 0x6f, 0x7f, 0xe0, 0x9b,
            0x05, 0x16, 0xf9, 0xa5, 0xb0, 0x2a, 0x1b, 0xd8, 0x4b, 0xb0, 0x18, 0x1e, 0x2e, 0x89,
            0xe1, 0x9b, 0xd8, 0x12, 0x59, 0x30, 0xd1, 0x78, 0x68, 0x2f, 0x38, 0x62, 0xdc, 0x51,
            0xb6, 0x36, 0xf0, 0x4e, 0x72, 0x0c, 0x47, 0xc3, 0xce, 0x51, 0xad, 0x70, 0xd9, 0x4b,
            0x9b, 0x22, 0x55, 0xfb, 0xae, 0x90, 0x65, 0x49, 0xf4, 0x99, 0xf8, 0xc6, 0xd3, 0x99,
            0x47, 0xed, 0x5e, 0x5d, 0xf8, 0xe2, 0xde, 0xf1, 0x13, 0x25, 0x3e, 0x7b, 0x08, 0xd0,
            0xa7, 0x6b, 0x6b, 0xfc, 0x68, 0xc8, 0x12, 0xf3, 0x75, 0xc7, 0x9b, 0x8f, 0xe5, 0xfd,
            0x85, 0x97, 0x6a, 0xa6, 0xd4, 0x6b, 0x4a, 0x23, 0x39, 0xd8, 0xae, 0x51, 0x47, 0xf6,
            0x80, 0xfb, 0xe7, 0x0f, 0x97, 0x8b, 0x38, 0xef, 0xfd, 0x7b, 0x2f, 0x78, 0x66, 0xa2,
            0x25, 0x54, 0xe1, 0x93, 0xa9, 0x4e, 0x98, 0xa6, 0x8b, 0x74, 0xbd, 0x25, 0xbb, 0x2b,
            0x3f, 0x5f, 0xb0, 0xa5, 0xfd, 0x59, 0x88, 0x7f, 0x9a, 0xb6, 0x81, 0x59, 0xb7, 0x17,
            0x8d, 0x5b, 0x7b, 0x67, 0x7c, 0xb5, 0x46, 0xbf, 0x41, 0xea, 0xdc, 0xa2, 0x16, 0xfc,
            0x10, 0x85, 0x01, 0x28, 0xf8, 0xbd, 0xef, 0x5c, 0x8d, 0x89, 0xf9, 0x6a, 0xfa, 0x4f,
            0xa8, 0xb5, 0x48, 0x85, 0x56, 0x5e, 0xd8, 0x38, 0xa9, 0x50, 0xfe, 0xe5, 0xf1, 0xc3,
            0xb0, 0xa4, 0xf6, 0xfb, 0x71, 0xe5, 0x4d, 0xfd, 0x16, 0x9e, 0x82, 0xce, 0xcc, 0x72,
            0x66, 0xc8, 0x50, 0xe6, 0x7c, 0x5e, 0xf0, 0xba, 0x96, 0x0f, 0x52, 0x14, 0x06, 0x0e,
            0x71, 0xeb, 0x17, 0x2a, 0x75, 0xfc, 0x14, 0x86, 0x83, 0x5c, 0xbe, 0xa6, 0x53, 0x44,
            0x65, 0xb0, 0x55, 0xc9, 0x6a, 0x72, 0xe4, 0x10, 0x52, 0x24, 0x18, 0x23, 0x25, 0xd8,
            0x30, 0x41, 0x4b, 0x40, 0x21, 0x4d, 0xaa, 0x80, 0x91, 0xd2, 0xe0, 0xfb, 0x01, 0x0a,
            0xe1, 0x5c, 0x6d, 0xe9, 0x08, 0x50, 0x97, 0x3b, 0xdf, 0x1e, 0x42, 0x3b, 0xe1, 0x48,
            0xa2, 0x37, 0xb8, 0x7a, 0x0c, 0x9f, 0x34, 0xd4, 0xb4, 0x76, 0x05, 0xb8, 0x03, 0xd7,
            0x43, 0xa8, 0x6a, 0x90, 0x39, 0x9a, 0x4a, 0xf3, 0x96, 0xd3, 0xa1, 0x20, 0x0a, 0x62,
            0xf3, 0xd9, 0x50, 0x79, 0x62, 0xe8, 0xe5, 0xbe, 0xe6, 0xd3, 0xda, 0x2b, 0xb3, 0xf7,
            0x23, 0x76, 0x64, 0xac, 0x7a, 0x29, 0x28, 0x23, 0x90, 0x0b, 0xc6, 0x35, 0x03, 0xb2,
            0x9e, 0x80, 0xd6, 0x3f, 0x60, 0x67, 0xbf, 0x8e, 0x17, 0x16, 0xac, 0x25, 0xbe, 0xba,
            0x35, 0x0d, 0xeb, 0x62, 0xa9, 0x9f, 0xe0, 0x31, 0x85, 0xeb, 0x4f, 0x69, 0x93, 0x7e,
            0xcd, 0x38, 0x79, 0x41, 0xfd, 0xa5, 0x44, 0xba, 0x67, 0xdb, 0x09, 0x11, 0x77, 0x49,
            0x38, 0xb0, 0x18, 0x27, 0xbc, 0xc6, 0x9c, 0x92, 0xb3, 0xf7, 0x72, 0xa9, 0xd2, 0x85,
            0x9e, 0xf0, 0x03, 0x39, 0x8b, 0x1f, 0x6b, 0xba, 0xd7, 0xb5, 0x74, 0xf7, 0x98, 0x9a,
            0x1d, 0x10, 0xb2, 0xdf, 0x79, 0x8e, 0x0d, 0xbf, 0x30, 0xd6, 0x58, 0x74, 0x64, 0xd2,
            0x48, 0x78, 0xcd, 0x00, 0xc0, 0xea, 0xee, 0x8a, 0x1a, 0x0c, 0xc7, 0x53, 0xa2, 0x79,
            0x79, 0xe1, 0x1b, 0x41, 0xdb, 0x1d, 0xe3, 0xd5, 0x03, 0x8a, 0xfa, 0xf4, 0x9f, 0x5c,
            0x68, 0x2c, 0x37, 0x48, 0xd8, 0xa3, 0xa9, 0xec, 0x54, 0xe6, 0xa3, 0x71, 0x27, 0x5f,
            0x16, 0x83, 0x51, 0x0f, 0x8e, 0x4f, 0x90, 0x93, 0x8f, 0x9a, 0xb6, 0xe1, 0x34, 0xc2,
            0xcf, 0xdf, 0x48, 0x41, 0xcb, 0xa8, 0x8e, 0x0c, 0xff, 0x2b, 0x0b, 0xcc, 0x8e, 0x6a,
            0xdc, 0xb7, 0x11, 0x09, 0xb5, 0x19, 0x8f, 0xec, 0xf1, 0xbb, 0x7e, 0x5c, 0x53, 0x1a,
            0xca, 0x50, 0xa5, 0x6a, 0x8a, 0x3b, 0x6d, 0xe5, 0x98, 0x62, 0xd4, 0x1f, 0xa1, 0x13,
            0xd9, 0xcd, 0x95, 0x78, 0x08, 0xf0, 0x85, 0x71, 0xd9, 0xa4, 0xbb, 0x79, 0x2a, 0xf2,
            0x71, 0xf6, 0xcc, 0x6d, 0xbb, 0x8d, 0xc7, 0xec, 0x36, 0xe3, 0x6b, 0xe1, 0xed, 0x30,
            0x81, 0x64, 0xc3, 0x1c, 0x7c, 0x0a, 0xfc, 0x54, 0x1c, 0x00, 0x00, 0x00,
        ];
        assert_eq!(nia3_mac(&key, 0x561eb2dd, 0x1c, 0, msg, 5670), 0x0ca12792);
    }

    // ── NIA0 null integrity ───────────────────────────────────────────────────

    #[test]
    fn test_nia0_null() {
        let key = [0u8; 16];
        assert_eq!(nas_mac(&key, 0, 0, 0, &[0x7E, 0x00, 0x41], 0x00), 0);
    }

    // ── nas_mac dispatch ──────────────────────────────────────────────────────

    #[test]
    fn test_nas_mac_dispatches_nia2() {
        let key = [0u8; 16];
        let mac_direct = nia2_mac(&key, 0, 0, 0, &[0x7E, 0x00, 0x41]);
        let mac_dispatch = nas_mac(&key, 0, 0, 0, &[0x7E, 0x00, 0x41], 0x02);
        assert_eq!(mac_direct, mac_dispatch);
        assert_ne!(mac_dispatch, 0);
    }

    #[test]
    fn test_nas_mac_dispatches_nia1() {
        let key = [0x2bu8; 16];
        let msg = &[0x33u8; 8];
        let bit_len = (msg.len() * 8) as u64;
        let mac_direct = nia1_mac(&key, 42, 1, 0, msg, bit_len);
        let mac_dispatch = nas_mac(&key, 42, 1, 0, msg, 0x01);
        assert_eq!(mac_direct, mac_dispatch);
    }

    #[test]
    fn test_nas_mac_dispatches_nia3() {
        let key = [0x47u8; 16];
        let msg = &[0xABu8; 8];
        let bit_len = (msg.len() * 8) as u64;
        let mac_direct = nia3_mac(&key, 0x12345678, 3, 1, msg, bit_len);
        let mac_dispatch = nas_mac(&key, 0x12345678, 3, 1, msg, 0x03);
        assert_eq!(mac_direct, mac_dispatch);
    }
}