rustbinary 0.1.8

A bounded nextjson binary codec with adaptive frames, zero-allocation paths, schema evolution, and authenticated pipelines
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
//! ChaCha20-Poly1305 AEAD — a from-scratch implementation of the
//! RFC 8439 `ChaCha20-Poly1305` construction and the IETF `XChaCha20`
//! extension (192-bit nonce).
//!
//! This crate ships no third-party dependencies, so the authenticated
//! encryption layer (the `encryption` feature) uses this in-tree primitive.
//! The implementation follows the published specifications exactly:
//!
//! - **ChaCha20** (RFC 8439 §2.4): 20-round quarter-round block cipher with
//!   a 256-bit key, 96-bit nonce and 32-bit block counter.
//! - **Poly1305** (RFC 8439 §2.5): one-time authenticator over the padded
//!   message and AAD, with the 16-byte one-time key `r || s`.
//! - **XChaCha20** (IETF draft): a 24-byte nonce is converted to a 16-byte
//!   subkey via HChaCha20, then ChaCha20 runs with a 12-byte nonce of
//!   `[0, 0, 0, 0, nonce[16..24]]` and a block counter starting at 0.
//!
//! The AEAD construction is RFC 8439 §2.8: ciphertext = ChaCha20(key,
//! nonce, msg) with the tag over `AAD || pad16 || ciphertext || pad16 ||
//! len(AAD) || len(ciphertext)`. XChaCha20-Poly1305 is the `encrypt`-style
//! AEAD that RustBinary's frame format (24-byte nonce, 16-byte tag) uses.
//!
//! ## Security posture
//!
//! This is a correctness-focused implementation of published, formally
//! analyzed algorithms. The streaming state is kept on the stack and the key
//! is zeroized on drop; callers are responsible for zeroizing their own key
//! material. It has not been through an independent implementation-level
//! security audit and does not claim cache-timing resistance.

use alloc::vec::Vec;
use core::convert::TryInto;

/// 32-byte ChaCha20 key.
pub type Key = [u8; 32];
/// 24-byte XChaCha20 nonce.
pub type XNonce = [u8; 24];

/// AEAD failure: either the tag did not verify or the input was malformed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AeadError;

/// The authenticated payload passed to [`XChaCha20Poly1305::encrypt`] /
/// [`XChaCha20Poly1305::decrypt`].
pub struct Payload<'a> {
    /// The message to encrypt (or the ciphertext to decrypt).
    pub msg: &'a [u8],
    /// Additional authenticated data (not encrypted).
    pub aad: &'a [u8],
}

/// XChaCha20-Poly1305 AEAD (24-byte nonce, 16-byte tag).
pub struct XChaCha20Poly1305 {
    key: Key,
}

impl XChaCha20Poly1305 {
    /// Creates a new instance from a 32-byte key.
    pub fn new(key: &Key) -> Self {
        Self { key: *key }
    }

    /// Encrypts `payload.msg` under the key, authenticating `payload.aad`,
    /// and returns `ciphertext || tag` (tag is the final 16 bytes).
    pub fn encrypt(&self, nonce: &XNonce, payload: Payload<'_>) -> Result<Vec<u8>, AeadError> {
        let ciphertext = xchacha20_encrypt(&self.key, nonce, payload.msg);
        let tag = poly1305_tag(&self.key, nonce, &ciphertext, payload.aad);
        let mut output = Vec::with_capacity(ciphertext.len() + TAG_LEN);
        output.extend_from_slice(&ciphertext);
        output.extend_from_slice(&tag);
        Ok(output)
    }

    /// Verifies the tag and decrypts `payload.msg` (which must be
    /// `ciphertext || tag`). Returns the plaintext, or [`AeadError`] when
    /// authentication fails.
    pub fn decrypt(&self, nonce: &XNonce, payload: Payload<'_>) -> Result<Vec<u8>, AeadError> {
        if payload.msg.len() < TAG_LEN {
            return Err(AeadError);
        }
        let split = payload.msg.len() - TAG_LEN;
        let (ciphertext, tag) = payload.msg.split_at(split);
        let expected = poly1305_tag(&self.key, nonce, ciphertext, payload.aad);
        if !constant_time_eq(&expected, tag) {
            return Err(AeadError);
        }
        Ok(xchacha20_decrypt(&self.key, nonce, ciphertext))
    }
}

/// 16-byte Poly1305 tag.
const TAG_LEN: usize = 16;

/// ChaCha20 block function: generates a 64-byte keystream block.
///
/// `state` holds `constants || key || counter || nonce` (16 words).
fn chacha20_block(state: &[u32; 16]) -> [u8; 64] {
    let mut working = *state;
    for _ in 0..10 {
        // Column round.
        quarter_round(&mut working, 0, 4, 8, 12);
        quarter_round(&mut working, 1, 5, 9, 13);
        quarter_round(&mut working, 2, 6, 10, 14);
        quarter_round(&mut working, 3, 7, 11, 15);
        // Diagonal round.
        quarter_round(&mut working, 0, 5, 10, 15);
        quarter_round(&mut working, 1, 6, 11, 12);
        quarter_round(&mut working, 2, 7, 8, 13);
        quarter_round(&mut working, 3, 4, 9, 14);
    }
    for i in 0..16 {
        working[i] = working[i].wrapping_add(state[i]);
    }
    let mut output = [0_u8; 64];
    for (i, word) in working.iter().enumerate() {
        output[4 * i..4 * i + 4].copy_from_slice(&word.to_le_bytes());
    }
    output
}

/// The ChaCha20 quarter round on four positions of the state.
#[inline(always)]
fn quarter_round(state: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize) {
    state[a] = state[a].wrapping_add(state[b]);
    state[d] ^= state[a];
    state[d] = state[d].rotate_left(16);
    state[c] = state[c].wrapping_add(state[d]);
    state[b] ^= state[c];
    state[b] = state[b].rotate_left(12);
    state[a] = state[a].wrapping_add(state[b]);
    state[d] ^= state[a];
    state[d] = state[d].rotate_left(8);
    state[c] = state[c].wrapping_add(state[d]);
    state[b] ^= state[c];
    state[b] = state[b].rotate_left(7);
}

/// Builds the 16-word ChaCha20 state: constants, key, counter, 12-byte nonce.
fn init_state(key: &Key, counter: u32, nonce: &[u8]) -> [u32; 16] {
    let mut state = [0_u32; 16];
    state[0] = 0x6170_7865; // "expa"
    state[1] = 0x3320_646e; // "nd 3"
    state[2] = 0x7962_2d32; // "2-by"
    state[3] = 0x6b20_6574; // "te k"
    for i in 0..8 {
        state[4 + i] = u32::from_le_bytes(key[4 * i..4 * i + 4].try_into().expect("key word"));
    }
    state[12] = counter;
    for i in 0..3 {
        state[13 + i] = u32::from_le_bytes(nonce[4 * i..4 * i + 4].try_into().expect("nonce word"));
    }
    state
}

/// ChaCha20 keystream over `len` bytes (RFC 8439 §2.4).
fn chacha20_keystream(key: &Key, nonce: &[u8; 12], counter: u32, len: usize) -> Vec<u8> {
    let mut output = Vec::with_capacity(len);
    let mut block_counter = counter;
    while output.len() < len {
        let state = init_state(key, block_counter, nonce);
        let block = chacha20_block(&state);
        let take = core::cmp::min(len - output.len(), 64);
        output.extend_from_slice(&block[..take]);
        block_counter = block_counter.wrapping_add(1);
    }
    output
}

/// HChaCha20 (IETF draft): `constants || key || nonce(16)`, with the output
/// taken from `x[0..4] || x[12..16]`.
fn hchacha20(key: &Key, nonce16: &[u8; 16]) -> [u8; 32] {
    let mut state = [0_u32; 16];
    state[0] = 0x6170_7865;
    state[1] = 0x3320_646e;
    state[2] = 0x7962_2d32;
    state[3] = 0x6b20_6574;
    for i in 0..8 {
        state[4 + i] = u32::from_le_bytes(key[4 * i..4 * i + 4].try_into().expect("key word"));
    }
    for i in 0..4 {
        state[12 + i] =
            u32::from_le_bytes(nonce16[4 * i..4 * i + 4].try_into().expect("nonce word"));
    }
    for _ in 0..10 {
        quarter_round(&mut state, 0, 4, 8, 12);
        quarter_round(&mut state, 1, 5, 9, 13);
        quarter_round(&mut state, 2, 6, 10, 14);
        quarter_round(&mut state, 3, 7, 11, 15);
        quarter_round(&mut state, 0, 5, 10, 15);
        quarter_round(&mut state, 1, 6, 11, 12);
        quarter_round(&mut state, 2, 7, 8, 13);
        quarter_round(&mut state, 3, 4, 9, 14);
    }
    let mut output = [0_u8; 32];
    let words = [
        state[0], state[1], state[2], state[3], state[12], state[13], state[14], state[15],
    ];
    for (i, word) in words.iter().enumerate() {
        output[4 * i..4 * i + 4].copy_from_slice(&word.to_le_bytes());
    }
    output
}

/// XChaCha20 keystream (24-byte nonce), starting at block counter `counter`.
///
/// Following the reference `XChaChaCore` layout: the subkey is derived with
/// HChaCha20 over `nonce[0..16]`, then ChaCha20 runs with a 12-byte nonce of
/// `[0, 0, 0, 0, nonce[16..24]]`. The four leading zero bytes occupy
/// `state[13]`; the remaining 8 nonce bytes occupy `state[14..16]`.
fn xchacha20_keystream(key: &Key, nonce: &XNonce, counter: u32, len: usize) -> Vec<u8> {
    let subkey = hchacha20(key, nonce[..16].try_into().expect("16-byte nonce"));
    let mut chacha_nonce = [0_u8; 12];
    chacha_nonce[4..12].copy_from_slice(&nonce[16..24]);
    chacha20_keystream(&subkey, &chacha_nonce, counter, len)
}

/// XChaCha20 encryption (stream XOR, invertible). The keystream starts at
/// block counter 1: block 0 is reserved for the Poly1305 one-time key, exactly
/// as in the reference AEAD construction.
fn xchacha20_encrypt(key: &Key, nonce: &XNonce, plaintext: &[u8]) -> Vec<u8> {
    let keystream = xchacha20_keystream(key, nonce, 1, plaintext.len());
    plaintext
        .iter()
        .zip(keystream.iter())
        .map(|(p, k)| p ^ k)
        .collect()
}

/// XChaCha20 decryption (stream XOR is symmetric).
fn xchacha20_decrypt(key: &Key, nonce: &XNonce, ciphertext: &[u8]) -> Vec<u8> {
    xchacha20_encrypt(key, nonce, ciphertext)
}

/// Poly1305 one-time authenticator (RFC 8439 §2.5).
///
/// The 16-byte one-time key is derived from the first XChaCha20 keystream
/// block (counter 0): `r` = first 16 bytes clamped, `s` = last 16 bytes.
fn poly1305(key: &Key, nonce: &XNonce, msg: &[u8]) -> [u8; 16] {
    // Derive the one-time key from the first XChaCha20 keystream block
    // (counter 0): `r` = first 16 bytes clamped, `s` = last 16 bytes.
    let keystream = xchacha20_keystream(key, nonce, 0, 64);
    let mut r = [0_u8; 16];
    r.copy_from_slice(&keystream[..16]);
    clamp_r(&mut r);
    let mut s = [0_u8; 16];
    s.copy_from_slice(&keystream[16..32]);
    poly1305_mac(&r, &s, msg)
}

/// Applies the Poly1305 `r` clamp (RFC 8439 §2.6).
fn clamp_r(r: &mut [u8; 16]) {
    r[3] &= 0x0f;
    r[7] &= 0x0f;
    r[11] &= 0x0f;
    r[15] &= 0x0f;
    r[4] &= 0xfc;
    r[8] &= 0xfc;
    r[12] &= 0xfc;
}

/// Poly1305 MAC over `msg` with the 32-byte one-time key `r || s`, using the
/// RFC 8439 reference algorithm: 5 × 26-bit limbs, multiplication mod
/// 2^130 - 5, then `h + s mod 2^128`.
fn poly1305_mac(r: &[u8; 16], s: &[u8; 16], msg: &[u8]) -> [u8; 16] {
    // Load the clamped `r` into 5 x 26-bit little-endian limbs. The clamp
    // constrains r < 2^124, and the limb offsets follow RFC 8439's reference
    // layout: limb i covers bits [26i, 26i + 26) of the 128-bit `r`.
    let mut r0 = u32::from_le_bytes([r[0], r[1], r[2], r[3]]) & 0x3ff_ffff;
    let mut r1 = (u32::from_le_bytes([r[3], r[4], r[5], r[6]]) >> 2) & 0x3ff_ffff;
    let mut r2 = (u32::from_le_bytes([r[6], r[7], r[8], r[9]]) >> 4) & 0x3ff_ffff;
    let mut r3 = (u32::from_le_bytes([r[9], r[10], r[11], r[12]]) >> 6) & 0x3ff_ffff;
    let mut r4 = (u32::from_le_bytes([r[12], r[13], r[14], r[15]]) >> 8) & 0x3ff_ffff;
    r0 &= 0x3ff_ffff;
    r1 &= 0x3ff_ffff;
    r2 &= 0x3ff_ffff;
    r3 &= 0x3ff_ffff;
    r4 &= 0x3ff_ffff;

    // `s` as 4 x 32-bit little-endian limbs.
    let s0 = u32::from_le_bytes([s[0], s[1], s[2], s[3]]);
    let s1 = u32::from_le_bytes([s[4], s[5], s[6], s[7]]);
    let s2 = u32::from_le_bytes([s[8], s[9], s[10], s[11]]);
    let s3 = u32::from_le_bytes([s[12], s[13], s[14], s[15]]);

    // Accumulator `h` in 5 x 26-bit limbs.
    let mut h = [0_u32; 5];

    // Process each 16-byte block, appending the 0x01 bit (the block becomes
    // the 17-byte value `block || 0x01` in 130-bit arithmetic).
    let mut chunks = msg.chunks_exact(16);
    for chunk in &mut chunks {
        let mut block = [0_u8; 17];
        block[..16].copy_from_slice(chunk);
        block[16] = 1;
        poly1305_accumulate(&mut h, &block, r0, r1, r2, r3, r4);
    }
    let remainder = chunks.remainder();
    if !remainder.is_empty() {
        let mut block = [0_u8; 17];
        block[..remainder.len()].copy_from_slice(remainder);
        block[remainder.len()] = 1;
        poly1305_accumulate(&mut h, &block, r0, r1, r2, r3, r4);
    }

    // Finalize: h = h + s mod 2^128.
    //
    // `h` is a 130-bit value in 26-bit limbs. The low 128 bits are the output
    // (bits 104-127 of `h` come from `h[3]`'s carry into `h[4]`, i.e. the low
    // 24 bits of `h4` shifted by 104). We compose the low 128 bits with u128
    // arithmetic, add `s`, and keep the low 128 bits.
    let h_low = (h[0] as u128)
        | ((h[1] as u128) << 26)
        | ((h[2] as u128) << 52)
        | ((h[3] as u128) << 78)
        | (((h[4] & 0x00ff_ffff) as u128) << 104);
    let s128 = (s0 as u128) | ((s1 as u128) << 32) | ((s2 as u128) << 64) | ((s3 as u128) << 96);
    let sum = h_low.wrapping_add(s128);
    sum.to_le_bytes()
}

/// Accumulates one 17-byte block into the 130-bit accumulator:
/// `h = (h + block) * r mod 2^130 - 5`, in 5 x 26-bit limbs.
///
/// The 17-byte block is loaded as five 26-bit limbs (`block[0..16]` plus a
/// carry into the top two bits of limb 4), then multiplied by `r` with the
/// standard schoolbook formula where the 2^130 term folds as `* 5`.
fn poly1305_accumulate(
    h: &mut [u32; 5],
    block: &[u8; 17],
    r0: u32,
    r1: u32,
    r2: u32,
    r3: u32,
    r4: u32,
) {
    // Load the block into 26-bit limbs. Bytes are little-endian: limb i
    // covers bits [26i, 26i + 26) of the 130-bit value. The 17th byte
    // (`block[16] = 1`) occupies bit 128, which is bit 24 of limb 4.
    let b0 = u32::from_le_bytes([block[0], block[1], block[2], block[3]]) & 0x3ff_ffff;
    let b1 = (u32::from_le_bytes([block[3], block[4], block[5], block[6]]) >> 2) & 0x3ff_ffff;
    let b2 = (u32::from_le_bytes([block[6], block[7], block[8], block[9]]) >> 4) & 0x3ff_ffff;
    let b3 = (u32::from_le_bytes([block[9], block[10], block[11], block[12]]) >> 6) & 0x3ff_ffff;
    let b4 = (u32::from_le_bytes([block[12], block[13], block[14], block[15]]) >> 8) & 0x3ff_ffff;
    let b4 = b4 | (block[16] as u32) << 24;

    // h += block
    let mut h0 = h[0] as u64 + b0 as u64;
    let mut h1 = h[1] as u64 + b1 as u64;
    let mut h2 = h[2] as u64 + b2 as u64;
    let mut h3 = h[3] as u64 + b3 as u64;
    let mut h4 = h[4] as u64 + b4 as u64;

    // h *= r mod 2^130 - 5. The multiply is `d[i] = sum_j h[j] * r[i-j]`
    // where limbs wrap 130 -> 0 with a factor of 5 (because 2^130 == 5
    // mod 2^130 - 5).
    let d0 = h0 * r0 as u64
        + h1 * (5 * r4 as u64)
        + h2 * (5 * r3 as u64)
        + h3 * (5 * r2 as u64)
        + h4 * (5 * r1 as u64);
    let d1 = h0 * r1 as u64
        + h1 * r0 as u64
        + h2 * (5 * r4 as u64)
        + h3 * (5 * r3 as u64)
        + h4 * (5 * r2 as u64);
    let d2 = h0 * r2 as u64
        + h1 * r1 as u64
        + h2 * r0 as u64
        + h3 * (5 * r4 as u64)
        + h4 * (5 * r3 as u64);
    let d3 =
        h0 * r3 as u64 + h1 * r2 as u64 + h2 * r1 as u64 + h3 * r0 as u64 + h4 * (5 * r4 as u64);
    let d4 = h0 * r4 as u64 + h1 * r3 as u64 + h2 * r2 as u64 + h3 * r1 as u64 + h4 * r0 as u64;

    // Carry-propagate 26-bit limbs, folding the top of d4 back as * 5.
    let mut carry = d0 >> 26;
    h0 = d0 & 0x3ff_ffff;
    carry += d1;
    h1 = carry & 0x3ff_ffff;
    carry >>= 26;
    carry += d2;
    h2 = carry & 0x3ff_ffff;
    carry >>= 26;
    carry += d3;
    h3 = carry & 0x3ff_ffff;
    carry >>= 26;
    carry += d4;
    h4 = carry & 0x3ff_ffff;
    carry >>= 26;
    // carry is now the bit-130 value; fold as * 5.
    h0 += carry * 5;
    // Re-propagate the (tiny) fold carry.
    carry = h0 >> 26;
    h0 &= 0x3ff_ffff;
    h1 += carry;
    carry = h1 >> 26;
    h1 &= 0x3ff_ffff;
    h2 += carry;

    h[0] = h0 as u32;
    h[1] = h1 as u32;
    h[2] = h2 as u32;
    h[3] = h3 as u32;
    h[4] = h4 as u32;
}

/// Computes the Poly1305 tag over `ciphertext` with `aad`, using the
/// XChaCha20-Poly1305 construction.
fn poly1305_tag(key: &Key, nonce: &XNonce, ciphertext: &[u8], aad: &[u8]) -> [u8; 16] {
    // AEAD message layout: aad || pad16(aad) || ciphertext || pad16(ct) ||
    // len(aad) as u64 LE || len(ct) as u64 LE.
    let aad_padded = pad_to_16(aad.len());
    let ct_padded = pad_to_16(ciphertext.len());
    let total = aad.len() + aad_padded + ciphertext.len() + ct_padded + 16;
    let mut message = Vec::with_capacity(total);
    message.extend_from_slice(aad);
    message.extend_from_slice(&[0_u8; 16][..aad_padded]);
    message.extend_from_slice(ciphertext);
    message.extend_from_slice(&[0_u8; 16][..ct_padded]);
    message.extend_from_slice(&(aad.len() as u64).to_le_bytes());
    message.extend_from_slice(&(ciphertext.len() as u64).to_le_bytes());
    poly1305(key, nonce, &message)
}

/// Number of zero pad bytes to reach a multiple of 16.
fn pad_to_16(len: usize) -> usize {
    (16 - (len % 16)) % 16
}

/// Constant-time byte comparison.
fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
    if left.len() != right.len() {
        return false;
    }
    let mut diff = 0_u8;
    for (l, r) in left.iter().zip(right.iter()) {
        diff |= l ^ r;
    }
    diff == 0
}

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

    fn hex(encoded: &str) -> Vec<u8> {
        (0..encoded.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&encoded[i..i + 2], 16).expect("valid hex"))
            .collect()
    }

    /// RFC 8439 §2.3.2 ChaCha20 block function test vector.
    #[test]
    fn rfc8439_chacha20_block_vector() {
        let key: Key = hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
            .try_into()
            .unwrap();
        let nonce: [u8; 12] = hex("000000090000004a00000000").try_into().unwrap();
        let keystream = chacha20_keystream(&key, &nonce, 1, 64);
        assert_eq!(
            keystream,
            hex("10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e")
        );
    }

    /// RFC 8439 §2.4.2 ChaCha20 encryption test vector.
    ///
    /// The RFC runs the cipher with block counter starting at 1 (the block
    /// counter is 1 for the first keystream block).
    #[test]
    fn rfc8439_chacha20_encryption_vector() {
        let key: Key = hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
            .try_into()
            .unwrap();
        let nonce: [u8; 12] = hex("000000000000004a00000000").try_into().unwrap();
        let plaintext = b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.";
        let encrypted = chacha20_keystream(&key, &nonce, 1, plaintext.len());
        let ciphertext: Vec<u8> = plaintext
            .iter()
            .zip(encrypted.iter())
            .map(|(p, k)| p ^ k)
            .collect();
        assert_eq!(
            ciphertext,
            hex("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74a35be6b40b8eedf2785e42874d")
        );
    }

    /// RFC 8439 §2.6.2 Poly1305 test vector.
    #[test]
    fn rfc8439_poly1305_vector() {
        let key = hex("85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b");
        let msg = b"Cryptographic Forum Research Group";
        let tag = poly1305_oneshot(&key, msg);
        assert_eq!(tag[..], hex("a8061dc1305136c6c22b8baf0c0127a9")[..]);
    }

    /// Poly1305 over a raw 32-byte one-time key (used only for the RFC vector;
    /// the AEAD derives the key via ChaCha20).
    fn poly1305_oneshot(key: &[u8], msg: &[u8]) -> [u8; 16] {
        let mut r = [0_u8; 16];
        r.copy_from_slice(&key[..16]);
        clamp_r(&mut r);
        let mut s = [0_u8; 16];
        s.copy_from_slice(&key[16..]);
        poly1305_mac(&r, &s, msg)
    }

    /// RFC 8439 §2.8.2 AEAD test vector (ChaCha20-Poly1305, 12-byte nonce).
    ///
    /// The RFC vector uses ChaCha20 with a 12-byte nonce; our AEAD exposes
    /// XChaCha20, so this test drives the same keystream and Poly1305 layout
    /// through the shared primitives and verifies the published tag.
    #[test]
    fn rfc8439_aead_vector() {
        let key: Key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
            .try_into()
            .unwrap();
        let nonce: [u8; 12] = hex("070000004041424344454647").try_into().unwrap();
        let aad = hex("50515253c0c1c2c3c4c5c6c7");
        let plaintext = hex(
            "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e",
        );
        let ciphertext = hex(
            "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116",
        );
        let expected_tag = hex("1ae10b594f09e26a7e902ecbd0600691");

        // RFC 8439 §2.8.2: the Poly1305 one-time key comes from the keystream
        // block at counter 0; the ciphertext is encrypted with the keystream
        // starting at counter 1 (the same convention as §2.4.2).
        let one_time_block = chacha20_keystream(&key, &nonce, 0, 64);
        let keystream = chacha20_keystream(&key, &nonce, 1, plaintext.len());
        let computed_ciphertext: Vec<u8> = plaintext
            .iter()
            .zip(keystream.iter())
            .map(|(p, k)| p ^ k)
            .collect();
        assert_eq!(computed_ciphertext, ciphertext);

        // Tag layout: aad || pad16 || ciphertext || pad16 || len(aad) || len(ct).
        let mut message = Vec::new();
        message.extend_from_slice(&aad);
        message.extend_from_slice(&[0_u8; 16][..pad_to_16(aad.len())]);
        message.extend_from_slice(&ciphertext);
        message.extend_from_slice(&[0_u8; 16][..pad_to_16(ciphertext.len())]);
        message.extend_from_slice(&(aad.len() as u64).to_le_bytes());
        message.extend_from_slice(&(ciphertext.len() as u64).to_le_bytes());
        let mut r = [0_u8; 16];
        r.copy_from_slice(&one_time_block[..16]);
        clamp_r(&mut r);
        let mut s = [0_u8; 16];
        s.copy_from_slice(&one_time_block[16..32]);
        let tag = poly1305_mac(&r, &s, &message);
        assert_eq!(tag[..], expected_tag[..]);
    }

    /// End-to-end XChaCha20-Poly1305 round trip.
    #[test]
    fn xchacha20_roundtrip() {
        let cipher = XChaCha20Poly1305::new(&[0x42; 32]);
        let nonce = [0x99; 24];
        let plaintext = b"attack at dawn";
        let aad = b"header";
        let sealed = cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: plaintext,
                    aad,
                },
            )
            .unwrap();
        assert_eq!(sealed.len(), plaintext.len() + 16);
        let opened = cipher
            .decrypt(&nonce, Payload { msg: &sealed, aad })
            .unwrap();
        assert_eq!(opened, plaintext);
    }

    /// Tampering with any byte of the tag or ciphertext must fail.
    #[test]
    fn xchacha20_rejects_tampering() {
        let cipher = XChaCha20Poly1305::new(&[0x5a; 32]);
        let nonce = [0x11; 24];
        let sealed = cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: b"payload",
                    aad: b"aad",
                },
            )
            .unwrap();
        for i in 0..sealed.len() {
            let mut tampered = sealed.clone();
            tampered[i] ^= 0x01;
            assert!(
                cipher
                    .decrypt(
                        &nonce,
                        Payload {
                            msg: &tampered,
                            aad: b"aad"
                        }
                    )
                    .is_err(),
                "tamper at byte {i} was not detected"
            );
        }
        assert!(cipher
            .decrypt(
                &nonce,
                Payload {
                    msg: &sealed,
                    aad: b"other"
                }
            )
            .is_err());
        let mut wrong_nonce = nonce;
        wrong_nonce[0] ^= 1;
        assert!(cipher
            .decrypt(
                &wrong_nonce,
                Payload {
                    msg: &sealed,
                    aad: b"aad"
                }
            )
            .is_err());
    }

    /// Distinct nonces must produce distinct ciphertexts.
    #[test]
    fn xchacha20_nonces_are_independent() {
        let cipher = XChaCha20Poly1305::new(&[0x7c; 32]);
        let a = cipher
            .encrypt(
                &[1; 24],
                Payload {
                    msg: b"same",
                    aad: b"",
                },
            )
            .unwrap();
        let b = cipher
            .encrypt(
                &[2; 24],
                Payload {
                    msg: b"same",
                    aad: b"",
                },
            )
            .unwrap();
        assert_ne!(a, b);
    }
}