hctr2-rs 0.9.1

HCTR2 and HCTR3 length-preserving encryption with format-preserving variants
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
#![allow(deprecated)]
//! HCTR2-TwKD (HCTR2 with Tweak-Based Key Derivation) beyond-birthday-bound secure cipher.
//!
//! HCTR2-TwKD achieves 2n/3-bit multi-user security (approximately 85 bits with 128-bit blocks)
//! when the number of BC calls per tweak is bounded by 2^(n/3).
//!
//! Construction (from "Beyond-Birthday-Bound Security with HCTR2", ASIACRYPT 2025):
//! - Uses a KDF F to derive HCTR2's key from the tweak
//! - HCTR2-TwKD[F_L, E, H](T, M) = HCTR2[E_{F_L(T_0)}, H](T_*, M)
//! - Cost per block: 1 BC call + 2 field multiplications (same as HCTR2)
//! - Small overhead for key derivation per unique tweak
//!
//! Security properties:
//! - Beyond-birthday-bound: ~85-bit security when tweak repetition <= 2^(n/3)
//! - Multi-user secure
//! - Ciphertext length equals plaintext length
//! - Maintains backward compatibility with HCTR2 implementations

#[allow(deprecated)]
use aes::cipher::{Array, BlockCipherEncrypt, KeyInit};
use aes::{Aes128, Aes256};

use crate::common::{BLOCK_LENGTH, Error, xor_blocks};
use crate::hctr2::{Hctr2_128, Hctr2_256};

// Keep the old error type as an alias for backwards compatibility
#[allow(non_camel_case_types)]
#[deprecated(note = "Use common::Error instead")]
pub type Hctr2TwKDError = Error;

/// CENC-based Key Derivation Function for AES-128.
///
/// Derives an AES key from a master key and a 126-bit tweak using the CENC construction:
/// K = E_L(00||T) XOR E_L(01||T)
///
/// The 126-bit tweak T is encoded as 16 bytes with the top two bits of the first
/// byte set to zero. The 2-bit prefix (00/01/10) occupies those top bits.
pub struct CencKdf128 {
    ks: Aes128,
}

impl CencKdf128 {
    /// Master key length (same as AES-128 key length).
    pub const MASTER_KEY_LENGTH: usize = 16;

    /// Derived key length (same as AES-128 key length).
    pub const KEY_LENGTH: usize = 16;

    /// Tweak length in bytes (126 bits packed into 16 bytes).
    pub const TWEAK_LENGTH: usize = 16;

    /// Tweak bits (126 bits, top 2 bits of first byte must be zero).
    pub const TWEAK_BITS: usize = 126;

    /// Initialize the KDF with a master key.
    pub fn new(master_key: &[u8; 16]) -> Self {
        Self {
            ks: Aes128::new(Array::from_slice(master_key)),
        }
    }

    /// Derive a key from a tweak.
    ///
    /// # Panics
    /// Panics if tweak is not valid (use `validate_tweak` to check).
    pub fn derive_key(&self, tweak: &[u8]) -> [u8; 16] {
        debug_assert!(Self::validate_tweak(tweak));

        let block0 = Self::make_block(tweak, 0);
        let block1 = Self::make_block(tweak, 1);

        let mut enc0 = Array::from(block0);
        let mut enc1 = Array::from(block1);
        self.ks.encrypt_block(&mut enc0);
        self.ks.encrypt_block(&mut enc1);

        xor_blocks(&enc0.into(), &enc1.into())
    }

    /// Validate that a tweak has the correct format.
    ///
    /// Returns true if the tweak is exactly 16 bytes and the top 2 bits of
    /// the first byte are zero.
    pub fn validate_tweak(tweak: &[u8]) -> bool {
        tweak.len() == Self::TWEAK_LENGTH && (tweak[0] & 0xC0) == 0
    }

    /// Create a block from a tweak with the given prefix (0, 1, or 2).
    fn make_block(tweak: &[u8], prefix: u8) -> [u8; BLOCK_LENGTH] {
        debug_assert!(prefix <= 2);
        let mut block = [0u8; BLOCK_LENGTH];
        block.copy_from_slice(tweak);
        block[0] = (block[0] & 0x3F) | (prefix << 6);
        block
    }
}

/// CENC-based Key Derivation Function for AES-256.
///
/// Derives an AES-256 key from a master key and a 126-bit tweak using the CENC construction:
/// K = (E_L(00||T) XOR E_L(01||T)) || (E_L(00||T) XOR E_L(10||T))
///
/// The 126-bit tweak T is encoded as 16 bytes with the top two bits of the first
/// byte set to zero. The 2-bit prefix (00/01/10) occupies those top bits.
pub struct CencKdf256 {
    ks: Aes256,
}

impl CencKdf256 {
    /// Master key length (same as AES-256 key length).
    pub const MASTER_KEY_LENGTH: usize = 32;

    /// Derived key length (same as AES-256 key length).
    pub const KEY_LENGTH: usize = 32;

    /// Tweak length in bytes (126 bits packed into 16 bytes).
    pub const TWEAK_LENGTH: usize = 16;

    /// Tweak bits (126 bits, top 2 bits of first byte must be zero).
    pub const TWEAK_BITS: usize = 126;

    /// Initialize the KDF with a master key.
    pub fn new(master_key: &[u8; 32]) -> Self {
        Self {
            ks: Aes256::new(Array::from_slice(master_key)),
        }
    }

    /// Derive a key from a tweak.
    ///
    /// # Panics
    /// Panics if tweak is not valid (use `validate_tweak` to check).
    pub fn derive_key(&self, tweak: &[u8]) -> [u8; 32] {
        debug_assert!(Self::validate_tweak(tweak));

        let block0 = Self::make_block(tweak, 0);
        let block1 = Self::make_block(tweak, 1);
        let block2 = Self::make_block(tweak, 2);

        let mut enc0 = Array::from(block0);
        let mut enc1 = Array::from(block1);
        let mut enc2 = Array::from(block2);
        self.ks.encrypt_block(&mut enc0);
        self.ks.encrypt_block(&mut enc1);
        self.ks.encrypt_block(&mut enc2);

        let enc0_arr: [u8; 16] = enc0.into();
        let enc1_arr: [u8; 16] = enc1.into();
        let enc2_arr: [u8; 16] = enc2.into();

        let mut derived = [0u8; 32];
        derived[..16].copy_from_slice(&xor_blocks(&enc0_arr, &enc1_arr));
        derived[16..].copy_from_slice(&xor_blocks(&enc0_arr, &enc2_arr));
        derived
    }

    /// Validate that a tweak has the correct format.
    ///
    /// Returns true if the tweak is exactly 16 bytes and the top 2 bits of
    /// the first byte are zero.
    pub fn validate_tweak(tweak: &[u8]) -> bool {
        tweak.len() == Self::TWEAK_LENGTH && (tweak[0] & 0xC0) == 0
    }

    /// Create a block from a tweak with the given prefix (0, 1, or 2).
    fn make_block(tweak: &[u8], prefix: u8) -> [u8; BLOCK_LENGTH] {
        debug_assert!(prefix <= 2);
        let mut block = [0u8; BLOCK_LENGTH];
        block.copy_from_slice(tweak);
        block[0] = (block[0] & 0x3F) | (prefix << 6);
        block
    }
}

/// HCTR2-TwKD with AES-128 and CENC KDF.
#[allow(non_camel_case_types)]
pub struct Hctr2TwKD_128 {
    kdf: CencKdf128,
}

impl Hctr2TwKD_128 {
    /// Master key length in bytes.
    pub const KEY_LENGTH: usize = 16;

    /// AES block length in bytes (always 16).
    pub const BLOCK_LENGTH: usize = BLOCK_LENGTH;

    /// Fixed tweak length for key derivation (T0 in the paper).
    pub const KDF_TWEAK_LENGTH: usize = CencKdf128::TWEAK_LENGTH;

    /// Minimum input length in bytes.
    pub const MIN_INPUT_LENGTH: usize = BLOCK_LENGTH;

    /// Initialize HCTR2-TwKD-128 cipher state from a master key.
    pub fn new(key: &[u8; 16]) -> Self {
        Self {
            kdf: CencKdf128::new(key),
        }
    }

    /// Encrypt plaintext to ciphertext using HCTR2-TwKD.
    ///
    /// The tweak is partitioned into:
    /// - T0: first `KDF_TWEAK_LENGTH` bytes (used for key derivation)
    /// - T*: remaining bytes (passed to underlying HCTR2)
    ///
    /// Each unique T0 derives a unique HCTR2 key, providing beyond-birthday-bound
    /// security when the same T0 is not reused excessively (limit: ~2^42 encryptions
    /// per tweak for AES).
    pub fn encrypt(
        &self,
        plaintext: &[u8],
        tweak: &[u8],
        ciphertext: &mut [u8],
    ) -> Result<(), Error> {
        if tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }

        let kdf_tweak = &tweak[..Self::KDF_TWEAK_LENGTH];
        if !CencKdf128::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_128::new(&derived_key);
        hctr2.encrypt(plaintext, &tweak[Self::KDF_TWEAK_LENGTH..], ciphertext)?;
        Ok(())
    }

    /// Decrypt ciphertext to plaintext using HCTR2-TwKD.
    pub fn decrypt(
        &self,
        ciphertext: &[u8],
        tweak: &[u8],
        plaintext: &mut [u8],
    ) -> Result<(), Error> {
        if tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }

        let kdf_tweak = &tweak[..Self::KDF_TWEAK_LENGTH];
        if !CencKdf128::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_128::new(&derived_key);
        hctr2.decrypt(ciphertext, &tweak[Self::KDF_TWEAK_LENGTH..], plaintext)?;
        Ok(())
    }

    /// Encrypt with split tweak: part for key derivation, part for HCTR2.
    ///
    /// This allows finer control over the tweak split:
    /// - `kdf_tweak`: Used for key derivation (must be exactly `KDF_TWEAK_LENGTH` bytes)
    /// - `hctr2_tweak`: Passed to underlying HCTR2 (any length)
    pub fn encrypt_split(
        &self,
        plaintext: &[u8],
        kdf_tweak: &[u8],
        hctr2_tweak: &[u8],
        ciphertext: &mut [u8],
    ) -> Result<(), Error> {
        if kdf_tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }
        if kdf_tweak.len() > Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooLong);
        }
        if !CencKdf128::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_128::new(&derived_key);
        hctr2.encrypt(plaintext, hctr2_tweak, ciphertext)?;
        Ok(())
    }

    /// Decrypt with split tweak.
    pub fn decrypt_split(
        &self,
        ciphertext: &[u8],
        kdf_tweak: &[u8],
        hctr2_tweak: &[u8],
        plaintext: &mut [u8],
    ) -> Result<(), Error> {
        if kdf_tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }
        if kdf_tweak.len() > Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooLong);
        }
        if !CencKdf128::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_128::new(&derived_key);
        hctr2.decrypt(ciphertext, hctr2_tweak, plaintext)?;
        Ok(())
    }
}

/// HCTR2-TwKD with AES-256 and CENC KDF.
#[allow(non_camel_case_types)]
pub struct Hctr2TwKD_256 {
    kdf: CencKdf256,
}

impl Hctr2TwKD_256 {
    /// Master key length in bytes.
    pub const KEY_LENGTH: usize = 32;

    /// AES block length in bytes (always 16).
    pub const BLOCK_LENGTH: usize = BLOCK_LENGTH;

    /// Fixed tweak length for key derivation (T0 in the paper).
    pub const KDF_TWEAK_LENGTH: usize = CencKdf256::TWEAK_LENGTH;

    /// Minimum input length in bytes.
    pub const MIN_INPUT_LENGTH: usize = BLOCK_LENGTH;

    /// Initialize HCTR2-TwKD-256 cipher state from a master key.
    pub fn new(key: &[u8; 32]) -> Self {
        Self {
            kdf: CencKdf256::new(key),
        }
    }

    /// Encrypt plaintext to ciphertext using HCTR2-TwKD.
    ///
    /// The tweak is partitioned into:
    /// - T0: first `KDF_TWEAK_LENGTH` bytes (used for key derivation)
    /// - T*: remaining bytes (passed to underlying HCTR2)
    pub fn encrypt(
        &self,
        plaintext: &[u8],
        tweak: &[u8],
        ciphertext: &mut [u8],
    ) -> Result<(), Error> {
        if tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }

        let kdf_tweak = &tweak[..Self::KDF_TWEAK_LENGTH];
        if !CencKdf256::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_256::new(&derived_key);
        hctr2.encrypt(plaintext, &tweak[Self::KDF_TWEAK_LENGTH..], ciphertext)?;
        Ok(())
    }

    /// Decrypt ciphertext to plaintext using HCTR2-TwKD.
    pub fn decrypt(
        &self,
        ciphertext: &[u8],
        tweak: &[u8],
        plaintext: &mut [u8],
    ) -> Result<(), Error> {
        if tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }

        let kdf_tweak = &tweak[..Self::KDF_TWEAK_LENGTH];
        if !CencKdf256::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_256::new(&derived_key);
        hctr2.decrypt(ciphertext, &tweak[Self::KDF_TWEAK_LENGTH..], plaintext)?;
        Ok(())
    }

    /// Encrypt with split tweak.
    ///
    /// - `kdf_tweak`: Used for key derivation (must be exactly `KDF_TWEAK_LENGTH` bytes)
    /// - `hctr2_tweak`: Passed to underlying HCTR2 (any length)
    pub fn encrypt_split(
        &self,
        plaintext: &[u8],
        kdf_tweak: &[u8],
        hctr2_tweak: &[u8],
        ciphertext: &mut [u8],
    ) -> Result<(), Error> {
        if kdf_tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }
        if kdf_tweak.len() > Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooLong);
        }
        if !CencKdf256::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_256::new(&derived_key);
        hctr2.encrypt(plaintext, hctr2_tweak, ciphertext)?;
        Ok(())
    }

    /// Decrypt with split tweak.
    pub fn decrypt_split(
        &self,
        ciphertext: &[u8],
        kdf_tweak: &[u8],
        hctr2_tweak: &[u8],
        plaintext: &mut [u8],
    ) -> Result<(), Error> {
        if kdf_tweak.len() < Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooShort);
        }
        if kdf_tweak.len() > Self::KDF_TWEAK_LENGTH {
            return Err(Error::TweakTooLong);
        }
        if !CencKdf256::validate_tweak(kdf_tweak) {
            return Err(Error::InvalidTweak);
        }

        let derived_key = self.kdf.derive_key(kdf_tweak);
        let hctr2 = Hctr2_256::new(&derived_key);
        hctr2.decrypt(ciphertext, hctr2_tweak, plaintext)?;
        Ok(())
    }
}

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

    #[test]
    fn test_hctr2_twkd_128_roundtrip() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = b"Hello, HCTR2-TwKD!";
        let mut ciphertext = vec![0u8; plaintext.len()];
        let mut decrypted = vec![0u8; plaintext.len()];

        let tweak = [0x01u8; 20];

        cipher.encrypt(plaintext, &tweak, &mut ciphertext).unwrap();
        cipher.decrypt(&ciphertext, &tweak, &mut decrypted).unwrap();

        assert_eq!(plaintext.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_hctr2_twkd_128_roundtrip_nonzero_key() {
        let key: [u8; 16] = core::array::from_fn(|i| (i + 1) as u8);
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = b"Hello, HCTR2-TwKD!";
        let mut ciphertext = vec![0u8; plaintext.len()];
        let mut decrypted = vec![0u8; plaintext.len()];

        let tweak = [0x01u8; 20];

        cipher.encrypt(plaintext, &tweak, &mut ciphertext).unwrap();
        assert_ne!(plaintext.as_slice(), ciphertext.as_slice());

        cipher.decrypt(&ciphertext, &tweak, &mut decrypted).unwrap();
        assert_eq!(plaintext.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_hctr2_twkd_128_minimum_length() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0x42u8; 16];
        let mut ciphertext = [0u8; 16];
        let mut decrypted = [0u8; 16];
        let tweak = [0x03u8; 16];

        cipher.encrypt(&plaintext, &tweak, &mut ciphertext).unwrap();
        cipher.decrypt(&ciphertext, &tweak, &mut decrypted).unwrap();

        assert_eq!(plaintext, decrypted);
    }

    #[test]
    fn test_hctr2_twkd_128_input_too_short() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0x42u8; 15];
        let mut ciphertext = [0u8; 15];
        let tweak = [0x04u8; 16];

        assert_eq!(
            cipher.encrypt(&plaintext, &tweak, &mut ciphertext),
            Err(Error::InputTooShort)
        );
    }

    #[test]
    fn test_hctr2_twkd_128_tweak_too_short() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0x42u8; 16];
        let mut ciphertext = [0u8; 16];

        let short_tweak = [0u8; 8];
        assert_eq!(
            cipher.encrypt(&plaintext, &short_tweak, &mut ciphertext),
            Err(Error::TweakTooShort)
        );
    }

    #[test]
    fn test_hctr2_twkd_128_invalid_kdf_tweak_bits() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0x42u8; 16];
        let mut ciphertext = [0u8; 16];

        // Top two bits must be zero for CENC
        let mut bad_tweak = [0u8; 16];
        bad_tweak[0] = 0xC0;
        assert_eq!(
            cipher.encrypt(&plaintext, &bad_tweak, &mut ciphertext),
            Err(Error::InvalidTweak)
        );
    }

    #[test]
    fn test_hctr2_twkd_128_split_tweak_too_long() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = b"split tweak length";
        let mut ciphertext = vec![0u8; plaintext.len()];

        let kdf_tweak = [0x01u8; 17];
        assert_eq!(
            cipher.encrypt_split(plaintext, &kdf_tweak, b"hctr2", &mut ciphertext),
            Err(Error::TweakTooLong)
        );
    }

    #[test]
    fn test_hctr2_twkd_128_different_tweaks() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0x42u8; 32];
        let mut ciphertext1 = [0u8; 32];
        let mut ciphertext2 = [0u8; 32];

        let tweak1 = [0x05u8; 20];
        let tweak2 = [0x06u8; 20];
        cipher.encrypt(&plaintext, &tweak1, &mut ciphertext1).unwrap();
        cipher.encrypt(&plaintext, &tweak2, &mut ciphertext2).unwrap();

        assert_ne!(ciphertext1, ciphertext2);
    }

    #[test]
    fn test_hctr2_twkd_128_split_tweak() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = b"Test split tweak mode";
        let mut ciphertext = vec![0u8; plaintext.len()];
        let mut decrypted = vec![0u8; plaintext.len()];

        let kdf_tweak = [0x07u8; 16];
        let hctr2_tweak = b"hctr2 part - can be any length";

        cipher
            .encrypt_split(plaintext, &kdf_tweak, hctr2_tweak, &mut ciphertext)
            .unwrap();
        cipher
            .decrypt_split(&ciphertext, &kdf_tweak, hctr2_tweak, &mut decrypted)
            .unwrap();

        assert_eq!(plaintext.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_hctr2_twkd_128_large_message() {
        let key = [0u8; 16];
        let cipher = Hctr2TwKD_128::new(&key);

        let plaintext = [0xABu8; 1024];
        let mut ciphertext = [0u8; 1024];
        let mut decrypted = [0u8; 1024];
        let tweak = [0x08u8; 16];

        cipher.encrypt(&plaintext, &tweak, &mut ciphertext).unwrap();
        cipher.decrypt(&ciphertext, &tweak, &mut decrypted).unwrap();

        assert_eq!(plaintext.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_hctr2_twkd_256_roundtrip() {
        let key = [0u8; 32];
        let cipher = Hctr2TwKD_256::new(&key);

        let plaintext = b"Hello, HCTR2-TwKD-256!";
        let mut ciphertext = vec![0u8; plaintext.len()];
        let mut decrypted = vec![0u8; plaintext.len()];

        let tweak = [0x02u8; 20];

        cipher.encrypt(plaintext, &tweak, &mut ciphertext).unwrap();
        cipher.decrypt(&ciphertext, &tweak, &mut decrypted).unwrap();

        assert_eq!(plaintext.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_cenc_kdf_128_different_tweaks() {
        let master_key = [0u8; 16];
        let kdf = CencKdf128::new(&master_key);

        let tweak1 = [0x01u8; 16];
        let tweak2 = [0x02u8; 16];
        let key1 = kdf.derive_key(&tweak1);
        let key2 = kdf.derive_key(&tweak2);

        assert_ne!(key1, key2);
    }

    #[test]
    fn test_cenc_kdf_128_deterministic() {
        let master_key = [0u8; 16];
        let kdf = CencKdf128::new(&master_key);

        let tweak = [0x03u8; 16];
        let key1 = kdf.derive_key(&tweak);
        let key2 = kdf.derive_key(&tweak);

        assert_eq!(key1, key2);
    }

    #[test]
    fn test_cenc_kdf_256_key_derivation() {
        let master_key = [0u8; 32];
        let kdf = CencKdf256::new(&master_key);

        let tweak = [0x04u8; 16];
        let key = kdf.derive_key(&tweak);
        assert_eq!(key.len(), 32);

        let tweak2 = [0x05u8; 16];
        let key2 = kdf.derive_key(&tweak2);
        assert_ne!(key, key2);
    }
}