cachekit-core 0.2.0

LZ4 compression, xxHash3 integrity, AES-256-GCM encryption for byte payloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
//! Core encryption functionality using AES-256-GCM
//!
//! This module provides the fundamental encryption/decryption operations
//! using the ring cryptography library for hardware-accelerated AES-256-GCM.
//!
//! # Nonce Uniqueness Guarantee (CWE-323 Mitigation)
//!
//! Each encryptor instance receives a **deterministic, globally unique instance ID**
//! from an atomic counter. This replaces the previous random IV approach and provides:
//!
//! - **Deterministic uniqueness**: No birthday paradox (random IV had ~2^32 collision bound)
//! - **Cross-instance safety**: Multiple FFI handles with the same key are safe
//! - **Zero additional overhead**: Single atomic increment at construction
//!
//! Nonce format: `[instance_id(8)][counter(4)]` = 12 bytes
//! - `instance_id`: Globally unique 64-bit ID assigned at encryptor creation
//! - `counter`: Per-instance 32-bit counter (0 to 2^32-1)
//!
//! This provides 2^64 unique encryptor instances, each with 2^32 encryptions,
//! for a total of 2^96 unique nonces - far exceeding any practical usage.

use crate::metrics::OperationMetrics;

// Native: ring for AES-256-GCM (hardware-accelerated, requires clang)
#[cfg(not(target_arch = "wasm32"))]
use ring::{
    aead::{Aad, LessSafeKey, Nonce, UnboundKey, AES_256_GCM},
    rand::{SecureRandom, SystemRandom},
};

// wasm32: RustCrypto aes-gcm (pure Rust, compiles on wasm32-unknown-unknown)
#[cfg(target_arch = "wasm32")]
use aes_gcm::{
    aead::{Aead, KeyInit, Payload},
    Aes256Gcm, Nonce as AesGcmNonce,
};
#[cfg(not(target_arch = "wasm32"))]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::sync::{Arc, LazyLock, Mutex};
#[cfg(target_arch = "wasm32")]
use std::sync::{Arc, Mutex};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use thiserror::Error;

// ── Native: LazyLock<AtomicU64> seeded from ring's SystemRandom ─────────────

/// Global encryptor instance counter for deterministic nonce uniqueness (native only).
///
/// Each encryptor gets a unique 64-bit instance ID from this counter,
/// which is used as the first 8 bytes of every nonce. This provides
/// deterministic cross-instance uniqueness (no birthday paradox).
///
/// # Security Properties
/// - Monotonically increasing: guarantees uniqueness across process lifetime
/// - Atomic: thread-safe for concurrent encryptor creation
/// - Starts non-zero: uses randomized initial value for cross-process uniqueness
///
/// # Why randomized start?
/// If a process restarts, the counter would start at 0 again, potentially
/// reusing instance IDs from the previous run. By starting with a random
/// 32-bit offset, we get ~2^32 cross-process collision resistance while
/// maintaining deterministic uniqueness within a single process.
#[cfg(not(target_arch = "wasm32"))]
static GLOBAL_INSTANCE_COUNTER: LazyLock<AtomicU64> = LazyLock::new(|| {
    // Initialize with random 32-bit value in upper bits for cross-process uniqueness
    // Lower 32 bits start at 0 for deterministic ordering
    let rng = SystemRandom::new();
    let mut random_seed = [0u8; 4];
    // RNG failure is a hard error — silently falling back to 0 is a security risk
    // because multiple restarts would produce the same instance IDs
    rng.fill(&mut random_seed)
        .expect("SystemRandom::fill failed during GLOBAL_INSTANCE_COUNTER initialization");
    let seed = u32::from_be_bytes(random_seed) as u64;
    AtomicU64::new(seed << 32)
});

// ── wasm32: thread_local Cell<u64> seeded from getrandom ────────────────────

// Per-thread encryptor instance counter for wasm32 (no atomics available).
//
// wasm32-unknown-unknown lacks threads, so a thread-local Cell<u64> is safe.
// Seeded from `getrandom` (which uses the JS crypto API via WASM).
#[cfg(target_arch = "wasm32")]
thread_local! {
    static WASM_INSTANCE_COUNTER: std::cell::Cell<u64> = {
        let mut seed_bytes = [0u8; 4];
        getrandom::getrandom(&mut seed_bytes)
            .expect("getrandom failed during WASM_INSTANCE_COUNTER initialization");
        let seed = u32::from_be_bytes(seed_bytes) as u64;
        std::cell::Cell::new(seed << 32)
    };
}

/// Get the next globally unique instance ID, incrementing the counter.
///
/// On native: uses `GLOBAL_INSTANCE_COUNTER` (thread-safe `AtomicU64`).
/// On wasm32: uses `WASM_INSTANCE_COUNTER` (thread-local `Cell<u64>`).
fn next_instance_id() -> u64 {
    #[cfg(not(target_arch = "wasm32"))]
    {
        GLOBAL_INSTANCE_COUNTER.fetch_add(1, Ordering::SeqCst)
    }
    #[cfg(target_arch = "wasm32")]
    {
        WASM_INSTANCE_COUNTER.with(|c| {
            let id = c.get();
            c.set(id.wrapping_add(1));
            id
        })
    }
}

// CPU feature detection
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use std::arch::is_x86_feature_detected;

/// Errors that can occur during encryption operations
#[derive(Error, Debug)]
pub enum EncryptionError {
    #[error("Invalid key length: expected 32 bytes, got {0}")]
    InvalidKeyLength(usize),

    #[error("Invalid nonce length: expected 12 bytes, got {0}")]
    InvalidNonceLength(usize),

    #[error("Encryption failed: {0}")]
    EncryptionFailed(String),

    #[error("Decryption failed: {0}")]
    DecryptionFailed(String),

    #[error("Random number generation failed")]
    RngFailure,

    #[error("Invalid ciphertext format: {0}")]
    InvalidCiphertext(String),

    #[error("Invalid encryption header: {0}")]
    InvalidHeader(String),

    #[error("Unsupported encryption version: {0}")]
    UnsupportedVersion(u8),

    #[error("Unsupported encryption algorithm: {0}")]
    UnsupportedAlgorithm(u8),

    #[error("Authentication verification failed")]
    AuthenticationFailed,

    #[error("Nonce counter exhausted - key rotation required")]
    NonceCounterExhausted,

    #[error("Key rotation not yet implemented")]
    NotImplemented(String),
}

/// Zero-knowledge encryptor using AES-256-GCM with hardware acceleration detection
pub struct ZeroKnowledgeEncryptor {
    hardware_acceleration_detected: bool,
    /// Nonce counter for provably unique nonces.
    ///
    /// Native: `AtomicU64` for lock-free thread safety.
    ///
    /// # Why AtomicU64 instead of AtomicU32?
    ///
    /// The nonce counter portion is only 4 bytes (u32), but we use AtomicU64 intentionally:
    ///
    /// **With AtomicU32 (WRONG)**:
    /// - At u32::MAX: `fetch_add(1)` returns u32::MAX, counter wraps to 0
    /// - Exhaustion check fails, but counter has wrapped
    /// - Next call: counter is 0, check passes, NONCE REUSE occurs!
    ///
    /// **With AtomicU64 (CORRECT)**:
    /// - At u32::MAX: `fetch_add(1)` returns u32::MAX, counter becomes u32::MAX + 1
    /// - Exhaustion check fails (counter >= u32::MAX)
    /// - Next call: counter is u32::MAX + 1, check fails again
    /// - Counter STAYS exhausted, no nonce reuse possible
    ///
    /// wasm32: `std::cell::Cell<u64>` — no threads on wasm32-unknown-unknown, Cell is safe.
    #[cfg(not(target_arch = "wasm32"))]
    nonce_counter: AtomicU64,
    #[cfg(target_arch = "wasm32")]
    nonce_counter: std::cell::Cell<u64>,
    /// Globally unique 64-bit instance ID (deterministic, no birthday paradox).
    ///
    /// Assigned from the platform counter at construction. Used as the first
    /// 8 bytes of every nonce to guarantee cross-instance uniqueness.
    ///
    /// # Security Properties
    /// - Deterministic: no collision possible within single process
    /// - Monotonic: each instance gets strictly larger ID
    /// - Randomized seed: cross-process collision resistance
    instance_id: u64,
    /// Last operation metrics (interior mutability for observability)
    last_metrics: Arc<Mutex<OperationMetrics>>,
}

impl ZeroKnowledgeEncryptor {
    /// Create a new encryptor instance with hardware acceleration detection
    ///
    /// Each instance receives a globally unique 64-bit instance ID that guarantees
    /// nonce uniqueness across all encryptor instances. This is a deterministic
    /// guarantee (no birthday paradox) unlike the previous random IV approach.
    ///
    /// # Errors
    /// This function is now infallible in practice. The previous RngFailure error
    /// is no longer possible since we use a deterministic instance counter instead
    /// of random IV generation. However, we keep the Result return type for API
    /// stability and future-proofing.
    pub fn new() -> Result<Self, EncryptionError> {
        let hardware_acceleration_detected = Self::detect_hardware_acceleration();

        // Get a globally unique instance ID via platform-specific counter
        let instance_id = next_instance_id();

        Ok(Self {
            hardware_acceleration_detected,
            #[cfg(not(target_arch = "wasm32"))]
            nonce_counter: AtomicU64::new(0),
            #[cfg(target_arch = "wasm32")]
            nonce_counter: std::cell::Cell::new(0),
            instance_id,
            last_metrics: Arc::new(Mutex::new(OperationMetrics::new())),
        })
    }

    /// Get the unique instance ID for this encryptor
    ///
    /// Exposed for monitoring and debugging. Each encryptor has a globally unique
    /// instance ID that guarantees nonce uniqueness across all instances.
    pub fn get_instance_id(&self) -> u64 {
        self.instance_id
    }

    /// Detect hardware acceleration capabilities
    ///
    /// This detection is informational only - ring library automatically uses
    /// the fastest available implementation regardless of this flag.
    fn detect_hardware_acceleration() -> bool {
        // On x86/x86_64, check for AES-NI instruction support
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        {
            // Use std::arch to detect CPU features
            #[cfg(target_feature = "aes")]
            return true;

            // Runtime detection fallback
            #[cfg(not(target_feature = "aes"))]
            {
                if is_x86_feature_detected!("aes") {
                    return true;
                }
                false
            }
        }

        // On ARM64, check for crypto extensions
        #[cfg(target_arch = "aarch64")]
        {
            #[cfg(target_feature = "aes")]
            {
                true
            }

            // Runtime detection for AArch64 crypto extensions
            #[cfg(not(target_feature = "aes"))]
            {
                // ARM crypto extensions are usually available on modern ARM64
                // ring library will use them automatically if available
                return cfg!(target_feature = "neon");
            }
        }

        // For other architectures, assume software implementation
        #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
        false
    }

    /// Get hardware acceleration status
    pub fn hardware_acceleration_enabled(&self) -> bool {
        self.hardware_acceleration_detected
    }

    /// Generate a provably unique nonce using counter-based approach
    ///
    /// Format: [instance_id(8)][counter(4)] = 12 bytes total
    ///
    /// Security properties:
    /// - Instance ID is globally unique (from platform counter, no birthday paradox)
    /// - Counter ensures per-instance uniqueness (up to 2^32 encryptions)
    /// - Combined: 2^96 total unique nonces possible
    /// - Overflow detection prevents wraparound and nonce reuse
    fn generate_nonce(&self) -> Result<[u8; 12], EncryptionError> {
        // Increment counter and retrieve previous value
        #[cfg(not(target_arch = "wasm32"))]
        let counter = self.nonce_counter.fetch_add(1, Ordering::SeqCst);
        #[cfg(target_arch = "wasm32")]
        let counter = {
            let c = self.nonce_counter.get();
            self.nonce_counter.set(c.wrapping_add(1));
            c
        };

        // Check for exhaustion (after 2^32 operations, require new instance)
        if counter >= u32::MAX as u64 {
            return Err(EncryptionError::NonceCounterExhausted);
        }

        // Construct nonce: [instance_id(8)][counter(4)]
        // instance_id is deterministically unique (no birthday paradox)
        let mut nonce_bytes = [0u8; 12];
        nonce_bytes[0..8].copy_from_slice(&self.instance_id.to_be_bytes());
        nonce_bytes[8..12].copy_from_slice(&(counter as u32).to_be_bytes());

        Ok(nonce_bytes)
    }

    /// Get current nonce counter value for monitoring
    ///
    /// Exposed for operational monitoring and alerting on counter exhaustion.
    pub fn get_nonce_counter(&self) -> u64 {
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.nonce_counter.load(Ordering::SeqCst)
        }
        #[cfg(target_arch = "wasm32")]
        {
            self.nonce_counter.get()
        }
    }

    /// Encrypt data using AES-256-GCM with authenticated additional data
    ///
    /// # Arguments
    /// * `plaintext` - Data to encrypt
    /// * `key` - 256-bit encryption key
    /// * `aad` - Additional authenticated data (domain separation context)
    ///
    /// # Returns
    /// Encrypted data in format: `[nonce(12)][ciphertext+auth_tag]`
    #[cfg(not(target_arch = "wasm32"))]
    pub fn encrypt_aes_gcm(
        &self,
        plaintext: &[u8],
        key: &[u8],
        aad: &[u8],
    ) -> Result<Vec<u8>, EncryptionError> {
        // Time encryption operation
        let encryption_start = Instant::now();

        // Validate key length
        if key.len() != 32 {
            return Err(EncryptionError::InvalidKeyLength(key.len()));
        }

        // Create AES-256-GCM key
        let unbound_key = UnboundKey::new(&AES_256_GCM, key)
            .map_err(|_| EncryptionError::EncryptionFailed("Invalid key".into()))?;
        let aead_key = LessSafeKey::new(unbound_key);

        // Generate provably unique nonce using counter-based approach
        let nonce_bytes = self.generate_nonce()?;
        let nonce = Nonce::assume_unique_for_key(nonce_bytes);

        // Prepare plaintext for in-place encryption
        let mut ciphertext = Vec::from(plaintext);

        // Encrypt in-place with authentication
        aead_key
            .seal_in_place_append_tag(nonce, Aad::from(aad), &mut ciphertext)
            .map_err(|e| {
                EncryptionError::EncryptionFailed(format!("AES-GCM encryption failed: {:?}", e))
            })?;

        // Format result: nonce + encrypted_data_with_tag
        let mut result = Vec::with_capacity(12 + ciphertext.len());
        result.extend_from_slice(&nonce_bytes);
        result.extend_from_slice(&ciphertext);

        // Update metrics for observability
        let encryption_micros = encryption_start.elapsed().as_micros() as u64;
        if let Ok(mut metrics) = self.last_metrics.lock() {
            *metrics = OperationMetrics::new()
                .with_encryption(encryption_micros, self.hardware_acceleration_detected);
        }

        Ok(result)
    }

    /// Decrypt data using AES-256-GCM with authenticated additional data
    ///
    /// # Arguments
    /// * `ciphertext` - Encrypted data in format `[nonce(12)][ciphertext+auth_tag]`
    /// * `key` - 256-bit decryption key
    /// * `aad` - Additional authenticated data (must match encryption)
    ///
    /// # Returns
    /// Decrypted plaintext data
    #[cfg(not(target_arch = "wasm32"))]
    pub fn decrypt_aes_gcm(
        &self,
        ciphertext: &[u8],
        key: &[u8],
        aad: &[u8],
    ) -> Result<Vec<u8>, EncryptionError> {
        // Time decryption operation
        let decryption_start = Instant::now();

        // Validate key length
        if key.len() != 32 {
            return Err(EncryptionError::InvalidKeyLength(key.len()));
        }

        // Validate minimum ciphertext length (nonce + tag minimum)
        if ciphertext.len() < 12 + 16 {
            return Err(EncryptionError::InvalidCiphertext(
                "Ciphertext too short".into(),
            ));
        }

        // Extract nonce and encrypted data
        let nonce_bytes: [u8; 12] = ciphertext[..12]
            .try_into()
            .map_err(|_| EncryptionError::InvalidNonceLength(ciphertext.len().min(12)))?;
        let nonce = Nonce::assume_unique_for_key(nonce_bytes);

        let encrypted_data = &ciphertext[12..];

        // Create AES-256-GCM key
        let unbound_key = UnboundKey::new(&AES_256_GCM, key)
            .map_err(|_| EncryptionError::DecryptionFailed("Invalid key".into()))?;
        let aead_key = LessSafeKey::new(unbound_key);

        // Prepare data for in-place decryption
        let mut plaintext = Vec::from(encrypted_data);

        // Decrypt in-place with authentication verification
        let decrypted_len = aead_key
            .open_in_place(nonce, Aad::from(aad), &mut plaintext)
            .map_err(|_e| {
                // Authentication tag verification failed
                EncryptionError::AuthenticationFailed
            })?
            .len();

        // Truncate to actual plaintext length (removes auth tag)
        plaintext.truncate(decrypted_len);

        // Update metrics for observability
        let decryption_micros = decryption_start.elapsed().as_micros() as u64;
        if let Ok(mut metrics) = self.last_metrics.lock() {
            *metrics = OperationMetrics::new()
                .with_encryption(decryption_micros, self.hardware_acceleration_detected);
        }

        Ok(plaintext)
    }

    /// Generate a secure random key for testing purposes
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub fn generate_key(&self) -> Result<[u8; 32], EncryptionError> {
        let rng = SystemRandom::new();
        let mut key = [0u8; 32];
        rng.fill(&mut key)
            .map_err(|_| EncryptionError::RngFailure)?;
        Ok(key)
    }

    /// Get metrics from last operation
    ///
    /// Returns a snapshot of metrics from the most recent encrypt_aes_gcm() or decrypt_aes_gcm() call
    pub fn get_last_metrics(&self) -> OperationMetrics {
        self.last_metrics
            .lock()
            .map(|metrics| metrics.clone())
            .unwrap_or_else(|_| OperationMetrics::new())
    }

    /// Key rotation API (stub for future implementation)
    ///
    /// This method will support gradual key migration to allow rotating encryption keys
    /// without downtime. Future implementation will:
    /// - Support dual-key mode (read from both old and new key, write with new key only)
    /// - Add version byte to ciphertext header indicating which key was used
    /// - Implement gradual migration strategy
    ///
    /// Currently returns NotImplemented error.
    pub fn rotate_key(&mut self, _new_master_key: &[u8]) -> Result<(), EncryptionError> {
        Err(EncryptionError::NotImplemented(
            "Key rotation will be implemented in a future release with gradual migration support"
                .into(),
        ))
    }

    /// Encrypt data using AES-256-GCM with authenticated additional data (wasm32)
    ///
    /// Uses RustCrypto's `aes-gcm` crate (pure Rust, compiles on wasm32-unknown-unknown).
    /// Produces identical wire format to the native `ring` path:
    /// `[nonce(12)][ciphertext][auth_tag(16)]`
    #[cfg(target_arch = "wasm32")]
    pub fn encrypt_aes_gcm(
        &self,
        plaintext: &[u8],
        key: &[u8],
        aad: &[u8],
    ) -> Result<Vec<u8>, EncryptionError> {
        if key.len() != 32 {
            return Err(EncryptionError::InvalidKeyLength(key.len()));
        }

        let nonce_bytes = self.generate_nonce()?;

        let cipher = Aes256Gcm::new_from_slice(key)
            .map_err(|e| EncryptionError::EncryptionFailed(format!("key error: {e}")))?;

        let nonce = AesGcmNonce::from_slice(&nonce_bytes);

        // encrypt() returns ciphertext || tag (no nonce) — same layout as ring's seal_in_place_append_tag
        let ciphertext_with_tag = cipher
            .encrypt(
                nonce,
                Payload {
                    msg: plaintext,
                    aad,
                },
            )
            .map_err(|e| {
                EncryptionError::EncryptionFailed(format!("AES-GCM encrypt failed: {e}"))
            })?;

        // Wire format: nonce(12) || ciphertext || tag(16) — identical to native ring output
        let mut result = Vec::with_capacity(12 + ciphertext_with_tag.len());
        result.extend_from_slice(&nonce_bytes);
        result.extend_from_slice(&ciphertext_with_tag);

        // Metrics: Instant unavailable on wasm32
        if let Ok(mut metrics) = self.last_metrics.lock() {
            *metrics = OperationMetrics::new().with_encryption(0u64, false);
        }

        Ok(result)
    }

    /// Decrypt data using AES-256-GCM with authenticated additional data (wasm32)
    ///
    /// Uses RustCrypto's `aes-gcm` crate (pure Rust, compiles on wasm32-unknown-unknown).
    /// Expects identical wire format to the native `ring` path:
    /// `[nonce(12)][ciphertext][auth_tag(16)]`
    #[cfg(target_arch = "wasm32")]
    pub fn decrypt_aes_gcm(
        &self,
        ciphertext: &[u8],
        key: &[u8],
        aad: &[u8],
    ) -> Result<Vec<u8>, EncryptionError> {
        if key.len() != 32 {
            return Err(EncryptionError::InvalidKeyLength(key.len()));
        }

        // Minimum: nonce(12) + tag(16)
        if ciphertext.len() < 12 + 16 {
            return Err(EncryptionError::InvalidCiphertext(
                "Ciphertext too short".into(),
            ));
        }

        let nonce_bytes = &ciphertext[..12];
        let encrypted_data = &ciphertext[12..]; // ciphertext + tag

        let cipher = Aes256Gcm::new_from_slice(key)
            .map_err(|e| EncryptionError::DecryptionFailed(format!("key error: {e}")))?;

        let nonce = AesGcmNonce::from_slice(nonce_bytes);

        // decrypt() verifies auth tag and returns plaintext
        let plaintext = cipher
            .decrypt(
                nonce,
                Payload {
                    msg: encrypted_data,
                    aad,
                },
            )
            .map_err(|_| EncryptionError::AuthenticationFailed)?;

        // Metrics: Instant unavailable on wasm32
        if let Ok(mut metrics) = self.last_metrics.lock() {
            *metrics = OperationMetrics::new().with_encryption(0u64, false);
        }

        Ok(plaintext)
    }
}

// Note: Default is intentionally NOT implemented.
// ZeroKnowledgeEncryptor::new() returns Result for API stability, even though
// the current implementation is infallible.

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    #[test]
    fn test_encrypt_decrypt_roundtrip() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"Hello, zero-knowledge world!";
        let aad = b"domain_separation_context";

        // Encrypt
        let ciphertext = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();

        // Decrypt
        let decrypted = encryptor.decrypt_aes_gcm(&ciphertext, &key, aad).unwrap();

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

    #[test]
    fn test_different_keys_fail() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key1 = encryptor.generate_key().unwrap();
        let key2 = encryptor.generate_key().unwrap();
        let plaintext = b"secret data";
        let aad = b"context";

        let ciphertext = encryptor.encrypt_aes_gcm(plaintext, &key1, aad).unwrap();

        // Decryption with wrong key should fail
        let result = encryptor.decrypt_aes_gcm(&ciphertext, &key2, aad);
        assert!(matches!(result, Err(EncryptionError::AuthenticationFailed)));
    }

    #[test]
    fn test_different_aad_fails() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"secret data";
        let aad1 = b"context1";
        let aad2 = b"context2";

        let ciphertext = encryptor.encrypt_aes_gcm(plaintext, &key, aad1).unwrap();

        // Decryption with wrong AAD should fail
        let result = encryptor.decrypt_aes_gcm(&ciphertext, &key, aad2);
        assert!(matches!(result, Err(EncryptionError::AuthenticationFailed)));
    }

    #[test]
    fn test_invalid_key_length() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let short_key = [0u8; 16]; // Should be 32 bytes
        let plaintext = b"test";
        let aad = b"context";

        let result = encryptor.encrypt_aes_gcm(plaintext, &short_key, aad);
        assert!(matches!(result, Err(EncryptionError::InvalidKeyLength(16))));
    }

    #[test]
    fn test_corrupted_ciphertext() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"secret data";
        let aad = b"context";

        let mut ciphertext = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();

        // Corrupt the ciphertext
        ciphertext[20] ^= 1;

        let result = encryptor.decrypt_aes_gcm(&ciphertext, &key, aad);
        assert!(matches!(result, Err(EncryptionError::AuthenticationFailed)));
    }

    #[test]
    fn test_nonce_uniqueness() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"test data";
        let aad = b"context";

        // Encrypt same data multiple times
        let ciphertext1 = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let ciphertext2 = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();

        // Ciphertexts should be different due to different nonces
        assert_ne!(ciphertext1, ciphertext2);

        // But both should decrypt to same plaintext
        let decrypted1 = encryptor.decrypt_aes_gcm(&ciphertext1, &key, aad).unwrap();
        let decrypted2 = encryptor.decrypt_aes_gcm(&ciphertext2, &key, aad).unwrap();

        assert_eq!(decrypted1, plaintext);
        assert_eq!(decrypted2, plaintext);
    }

    #[test]
    fn test_metrics_collection_on_encrypt() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"test data for encryption metrics";
        let aad = b"context";

        // Encrypt data
        encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let metrics = encryptor.get_last_metrics();

        // Verify metrics were collected
        assert!(metrics.encryption_time_micros.is_some());
    }

    #[test]
    fn test_metrics_collection_on_decrypt() {
        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = encryptor.generate_key().unwrap();
        let plaintext = b"test data for decryption metrics";
        let aad = b"context";

        // Encrypt then decrypt
        let ciphertext = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        encryptor.decrypt_aes_gcm(&ciphertext, &key, aad).unwrap();
        let metrics = encryptor.get_last_metrics();

        // Verify metrics were collected
        assert!(metrics.encryption_time_micros.is_some());
    }

    // ============================================================================
    // Nonce Exhaustion Tests (Tasks 8.1, 8.2)
    // ============================================================================

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn test_nonce_exhaustion_at_boundary() {
        // WHY: Verify nonce counter exhaustion is detected at u32::MAX
        // This is critical for AES-GCM security - nonce reuse is catastrophic
        //
        // SECURITY NOTE: The check MUST be at u32::MAX (not u64::MAX) because
        // the nonce counter portion is only 4 bytes: (counter as u32).to_be_bytes()
        // Using u64::MAX would allow ~4 billion nonce reuses per key!

        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = [0x42u8; 32];
        let plaintext = b"test";
        let aad = b"test_domain";

        // Set counter to u32::MAX - 2 to test boundary
        encryptor
            .nonce_counter
            .store(u32::MAX as u64 - 2, Ordering::SeqCst);

        // First encryption should succeed (counter = u32::MAX - 2)
        let result1 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(result1.is_ok(), "Encryption at u32::MAX-2 should succeed");

        // Second encryption should succeed (counter = u32::MAX - 1)
        let result2 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(result2.is_ok(), "Encryption at u32::MAX-1 should succeed");

        // Third encryption should fail (counter = u32::MAX)
        let result3 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(
            matches!(result3, Err(EncryptionError::NonceCounterExhausted)),
            "Encryption at u32::MAX should fail with NonceCounterExhausted: {:?}",
            result3
        );

        // Verify counter value
        let final_counter = encryptor.get_nonce_counter();
        assert_eq!(
            final_counter,
            u32::MAX as u64 + 1,
            "Counter should be at u32::MAX + 1 after 3 attempts"
        );
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn test_counter_no_wraparound_after_exhaustion() {
        // WHY: Verify that after counter exhaustion, subsequent operations
        // continue to fail (counter doesn't wrap back to 0)
        //
        // This test validates WHY AtomicU64 is used instead of AtomicU32:
        // - With AtomicU32, fetch_add at u32::MAX would wrap to 0
        // - That would allow nonce reuse after exhaustion
        // - AtomicU64 allows counter to exceed u32::MAX while still failing the check
        //
        // CRITICAL: If this test fails, nonce reuse is possible after exhaustion!

        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = [0x42u8; 32];
        let plaintext = b"test";
        let aad = b"test_domain";

        // Set counter to exactly u32::MAX (exhaustion point)
        encryptor
            .nonce_counter
            .store(u32::MAX as u64, Ordering::SeqCst);

        // First attempt at exhaustion - should fail
        let result1 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(
            matches!(result1, Err(EncryptionError::NonceCounterExhausted)),
            "First encryption at u32::MAX should fail"
        );

        // Counter should now be u32::MAX + 1 (not wrapped to 0!)
        let counter_after_first = encryptor.get_nonce_counter();
        assert!(
            counter_after_first > u32::MAX as u64,
            "Counter must NOT wrap after exhaustion (got {}, expected > {})",
            counter_after_first,
            u32::MAX
        );

        // Second attempt should ALSO fail (counter is past u32::MAX)
        let result2 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(
            matches!(result2, Err(EncryptionError::NonceCounterExhausted)),
            "Second encryption after exhaustion should also fail"
        );

        // Third attempt should ALSO fail
        let result3 = encryptor.encrypt_aes_gcm(plaintext, &key, aad);
        assert!(
            matches!(result3, Err(EncryptionError::NonceCounterExhausted)),
            "Third encryption after exhaustion should also fail"
        );

        // Verify counter keeps incrementing (never wraps)
        let final_counter = encryptor.get_nonce_counter();
        assert!(
            final_counter > counter_after_first,
            "Counter should continue incrementing past exhaustion"
        );

        println!(
            "Counter after exhaustion: {} (properly stays above u32::MAX = {})",
            final_counter,
            u32::MAX
        );
    }

    // ============================================================================
    // Multi-Handle Nonce Uniqueness Tests (CWE-323 Mitigation)
    // ============================================================================

    #[test]
    fn test_multi_handle_nonce_collision_detection() {
        // WHY: Verify that multiple encryptor handles with the same key are SAFE.
        // CWE-323 (nonce reuse) vulnerability is now FIXED via deterministic instance IDs.
        //
        // ARCHITECTURE (FIXED):
        // - Each ZeroKnowledgeEncryptor has a globally unique instance_id (8 bytes)
        // - instance_id comes from GLOBAL_INSTANCE_COUNTER (atomic, deterministic)
        // - Combined with per-instance counter (4 bytes) = guaranteed unique nonces
        //
        // SECURITY PROPERTY:
        // - No birthday paradox (deterministic uniqueness, not probabilistic)
        // - Safe to create unlimited encryptor handles with same key
        // - Each handle gets monotonically increasing instance_id

        use std::collections::HashSet;

        // Create two encryptors - simulating FFI multi-handle usage
        let enc1 = ZeroKnowledgeEncryptor::new().unwrap();
        let enc2 = ZeroKnowledgeEncryptor::new().unwrap();

        // Verify instance IDs are unique and monotonically increasing
        assert_ne!(
            enc1.get_instance_id(),
            enc2.get_instance_id(),
            "Instance IDs must be unique"
        );

        // Same key used with both encryptors - NOW SAFE due to unique instance_ids
        let shared_key = [0xABu8; 32];
        let aad = b"test_domain";
        let plaintext = b"secret data";

        // Collect nonces from both encryptors
        let mut all_nonces: HashSet<[u8; 12]> = HashSet::new();

        // Encrypt with first encryptor
        for _ in 0..100 {
            let ciphertext = enc1.encrypt_aes_gcm(plaintext, &shared_key, aad).unwrap();
            let nonce: [u8; 12] = ciphertext[..12].try_into().unwrap();

            // Verify nonce is unique (guaranteed by instance_id + counter)
            assert!(
                all_nonces.insert(nonce),
                "Nonce collision detected within same encryptor (impossible!)"
            );
        }

        // Encrypt with second encryptor using SAME KEY
        // SAFE: Different instance_id guarantees non-overlapping nonce space
        for _ in 0..100 {
            let ciphertext = enc2.encrypt_aes_gcm(plaintext, &shared_key, aad).unwrap();
            let nonce: [u8; 12] = ciphertext[..12].try_into().unwrap();

            // DETERMINISTIC GUARANTEE: Nonce MUST be unique across ALL encryptors
            // instance_id ensures non-overlapping nonce spaces
            assert!(
                all_nonces.insert(nonce),
                "SECURITY VIOLATION: Nonce collision detected! \
                 This should be impossible with deterministic instance_id. \
                 Nonce: {:02x?}",
                nonce
            );
        }

        println!(
            "Collected {} unique nonces across 2 encryptors (deterministically unique)",
            all_nonces.len()
        );
    }

    #[test]
    fn test_nonce_structure_verification() {
        // WHY: Verify the nonce format is [instance_id(8)][counter(4)] = 12 bytes
        // and that instance_id is deterministic (from global counter).
        //
        // NONCE FORMAT:
        // - Bytes 0-7: instance_id (globally unique, from GLOBAL_INSTANCE_COUNTER)
        // - Bytes 8-11: counter (per-instance, starts at 0)

        let encryptor = ZeroKnowledgeEncryptor::new().unwrap();
        let key = [0xCDu8; 32];
        let aad = b"structure_test";
        let plaintext = b"test";

        // First encryption
        let ct1 = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let nonce1: [u8; 12] = ct1[..12].try_into().unwrap();

        // Extract the instance_id portion (first 8 bytes) and counter (last 4 bytes)
        let instance_id_bytes = &nonce1[..8];
        let instance_id = u64::from_be_bytes(instance_id_bytes.try_into().unwrap());
        let counter = u32::from_be_bytes(nonce1[8..12].try_into().unwrap());

        // Verify instance_id matches the encryptor's instance_id
        assert_eq!(
            instance_id,
            encryptor.get_instance_id(),
            "Nonce instance_id must match encryptor's instance_id"
        );

        // Verify counter starts at 0
        assert_eq!(counter, 0, "First counter should be 0");

        println!(
            "Nonce structure: instance_id={}, counter={}",
            instance_id, counter
        );

        // Second encryption - counter should increment
        let ct2 = encryptor.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let nonce2: [u8; 12] = ct2[..12].try_into().unwrap();
        let counter2 = u32::from_be_bytes(nonce2[8..12].try_into().unwrap());

        assert_eq!(counter2, 1, "Second counter should be 1");

        // Verify nonces are different
        assert_ne!(nonce1, nonce2, "Nonces must be unique");

        // Verify instance_id is constant for same encryptor
        let instance_id2 = u64::from_be_bytes(nonce2[..8].try_into().unwrap());
        assert_eq!(
            instance_id, instance_id2,
            "Instance ID must be constant within an encryptor"
        );
    }

    #[test]
    fn test_deterministic_instance_id_uniqueness() {
        // WHY: Verify that each encryptor gets a unique instance_id from the
        // global counter, providing DETERMINISTIC nonce uniqueness.
        //
        // SECURITY PROPERTY: Unlike random IVs which have birthday paradox issues,
        // the global atomic counter guarantees:
        // - Each instance gets a strictly different instance_id
        // - Instance IDs are monotonically increasing within a process
        // - No collision possible within single process lifetime

        use crate::encryption::key_derivation::key_fingerprint;

        let key = [0xEFu8; 32];
        let key_fp = key_fingerprint(&key);

        // Create multiple encryptors
        let enc1 = ZeroKnowledgeEncryptor::new().unwrap();
        let enc2 = ZeroKnowledgeEncryptor::new().unwrap();
        let enc3 = ZeroKnowledgeEncryptor::new().unwrap();

        // Verify instance IDs are strictly increasing
        let id1 = enc1.get_instance_id();
        let id2 = enc2.get_instance_id();
        let id3 = enc3.get_instance_id();

        assert!(id1 < id2, "Instance IDs must be monotonically increasing");
        assert!(id2 < id3, "Instance IDs must be monotonically increasing");

        // Verify all are unique
        assert_ne!(id1, id2, "Instance IDs must be unique");
        assert_ne!(id2, id3, "Instance IDs must be unique");
        assert_ne!(id1, id3, "Instance IDs must be unique");

        println!("Key fingerprint: {:02x?}", key_fp);
        println!(
            "Instance IDs: {} < {} < {} (monotonic, deterministic)",
            id1, id2, id3
        );

        // Since instance_ids are unique, nonce spaces are completely non-overlapping
        // This eliminates the birthday paradox vulnerability entirely
    }

    #[test]
    fn test_encryptor_nonce_space_isolation() {
        // WHY: Verify each encryptor has a completely isolated nonce space.
        // This is guaranteed by the deterministic instance_id from GLOBAL_INSTANCE_COUNTER.
        //
        // SECURITY GUARANTEE:
        // - Each encryptor gets monotonically increasing instance_id
        // - Nonce = [instance_id(8)][counter(4)]
        // - Different instance_ids = completely non-overlapping nonce spaces
        // - No birthday paradox, no collision possible

        let enc1 = ZeroKnowledgeEncryptor::new().unwrap();
        let enc2 = ZeroKnowledgeEncryptor::new().unwrap();
        let enc3 = ZeroKnowledgeEncryptor::new().unwrap();

        let key = [0x11u8; 32];
        let aad = b"instance_test";
        let plaintext = b"test";

        // Get nonces from each encryptor
        let ct1 = enc1.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let ct2 = enc2.encrypt_aes_gcm(plaintext, &key, aad).unwrap();
        let ct3 = enc3.encrypt_aes_gcm(plaintext, &key, aad).unwrap();

        let nonce1: [u8; 12] = ct1[..12].try_into().unwrap();
        let nonce2: [u8; 12] = ct2[..12].try_into().unwrap();
        let nonce3: [u8; 12] = ct3[..12].try_into().unwrap();

        // Extract instance_id portions (first 8 bytes) - now deterministic, not random
        let id1_bytes = &nonce1[..8];
        let id2_bytes = &nonce2[..8];
        let id3_bytes = &nonce3[..8];

        // All counter portions should be 0 (first encryption from each)
        assert_eq!(&nonce1[8..12], &[0, 0, 0, 0], "Counter should be 0");
        assert_eq!(&nonce2[8..12], &[0, 0, 0, 0], "Counter should be 0");
        assert_eq!(&nonce3[8..12], &[0, 0, 0, 0], "Counter should be 0");

        // Instance IDs MUST be different - DETERMINISTIC guarantee, not probabilistic
        assert_ne!(
            id1_bytes, id2_bytes,
            "Instance IDs must be unique (enc1 vs enc2)"
        );
        assert_ne!(
            id1_bytes, id3_bytes,
            "Instance IDs must be unique (enc1 vs enc3)"
        );
        assert_ne!(
            id2_bytes, id3_bytes,
            "Instance IDs must be unique (enc2 vs enc3)"
        );

        // Verify instance_ids match the encryptor's get_instance_id()
        assert_eq!(
            u64::from_be_bytes(id1_bytes.try_into().unwrap()),
            enc1.get_instance_id()
        );
        assert_eq!(
            u64::from_be_bytes(id2_bytes.try_into().unwrap()),
            enc2.get_instance_id()
        );
        assert_eq!(
            u64::from_be_bytes(id3_bytes.try_into().unwrap()),
            enc3.get_instance_id()
        );

        println!("Instance IDs are deterministically unique:");
        println!(
            "  enc1: {:02x?} (instance_id={})",
            id1_bytes,
            enc1.get_instance_id()
        );
        println!(
            "  enc2: {:02x?} (instance_id={})",
            id2_bytes,
            enc2.get_instance_id()
        );
        println!(
            "  enc3: {:02x?} (instance_id={})",
            id3_bytes,
            enc3.get_instance_id()
        );
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn test_concurrent_nonce_exhaustion() {
        // WHY: Verify atomic counter behavior under concurrent access at exhaustion boundary
        // This ensures no race conditions allow nonce reuse

        use std::sync::Arc;
        use std::thread;

        let encryptor = Arc::new(ZeroKnowledgeEncryptor::new().unwrap());
        let key = [0x5au8; 32];
        let plaintext = b"concurrent test";
        let aad = b"concurrent_domain";

        // Set counter near exhaustion (50 operations from limit)
        let start_counter = u32::MAX as u64 - 50;
        encryptor
            .nonce_counter
            .store(start_counter, Ordering::SeqCst);

        // Spawn 10 threads, each trying 10 encryptions (100 total, but only 50 can succeed)
        let mut handles = vec![];
        let success_count = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let exhaustion_count = Arc::new(std::sync::atomic::AtomicU64::new(0));

        for _ in 0..10 {
            let encryptor = Arc::clone(&encryptor);
            let success = Arc::clone(&success_count);
            let exhausted = Arc::clone(&exhaustion_count);

            let handle = thread::spawn(move || {
                for _ in 0..10 {
                    match encryptor.encrypt_aes_gcm(plaintext, &key, aad) {
                        Ok(_) => {
                            success.fetch_add(1, Ordering::SeqCst);
                        }
                        Err(EncryptionError::NonceCounterExhausted) => {
                            exhausted.fetch_add(1, Ordering::SeqCst);
                        }
                        Err(e) => panic!("Unexpected error: {:?}", e),
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads
        for handle in handles {
            handle.join().expect("Thread should complete");
        }

        let total_success = success_count.load(Ordering::SeqCst);
        let total_exhausted = exhaustion_count.load(Ordering::SeqCst);

        // CRITICAL CHECK: Total operations must equal 100
        assert_eq!(
            total_success + total_exhausted,
            100,
            "All 100 operations must complete (success + exhaustion)"
        );

        // Successes should be exactly 50 (we started 50 from limit)
        assert_eq!(
            total_success, 50,
            "Exactly 50 operations should succeed (started 50 from limit)"
        );

        // Exhaustions should be exactly 50
        assert_eq!(
            total_exhausted, 50,
            "Exactly 50 operations should fail with exhaustion"
        );

        println!(
            "✓ Concurrent exhaustion: {} successes, {} exhaustions (total 100)",
            total_success, total_exhausted
        );
    }
}

// Kani Formal Verification Proofs
// These proofs verify critical cryptographic properties
// Bounded to complete within 15 minutes as per specification requirements
#[cfg(kani)]
mod kani_proofs {
    use super::*;

    /// Verify key length validation
    /// Property: Invalid key lengths are always rejected
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_key_length_validation() {
        // Symbolic key length
        let key_len: usize = kani::any();
        kani::assume(key_len < 100); // Reasonable bound for test

        // Property: Only 32-byte keys are valid for AES-256
        let is_valid_length = key_len == 32;
        let would_accept = key_len == 32;

        assert_eq!(is_valid_length, would_accept);
    }

    /// Verify ciphertext minimum size enforcement
    /// Property: Ciphertexts smaller than nonce+tag are always rejected
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_ciphertext_minimum_size() {
        let ciphertext_len: usize = kani::any();
        kani::assume(ciphertext_len < 100);

        // Minimum size is nonce(12) + tag(16) = 28 bytes
        const MIN_CIPHERTEXT_SIZE: usize = 12 + 16;

        // Property: Ciphertexts below minimum size must be rejected
        let is_too_small = ciphertext_len < MIN_CIPHERTEXT_SIZE;
        let would_reject = ciphertext_len < MIN_CIPHERTEXT_SIZE;

        assert_eq!(is_too_small, would_reject);
    }

    /// Verify nonce extraction bounds
    /// Property: Nonce extraction from ciphertext never panics
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_nonce_extraction_safety() {
        let ciphertext_len: usize = kani::any();
        kani::assume(ciphertext_len >= 12 + 16 && ciphertext_len < 100);

        // Property: Nonce is always in bounds [0..12]
        let nonce_end = 12;
        assert!(nonce_end <= ciphertext_len);

        // Property: Encrypted data starts after nonce
        let data_start = 12;
        assert!(data_start <= ciphertext_len);
    }

    /// Verify AAD (Additional Authenticated Data) binding
    /// Property: Different AADs produce different authentication tags
    /// This verifies domain separation - ciphertext from one context
    /// cannot be used in another context
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_aad_domain_separation() {
        // Symbolic AAD values (simplified to lengths for verification)
        let aad1_len: usize = kani::any();
        let aad2_len: usize = kani::any();

        kani::assume(aad1_len < 100);
        kani::assume(aad2_len < 100);

        // Property: Different AAD lengths imply different AADs
        // (Real crypto ensures different AADs produce different auth tags)
        if aad1_len != aad2_len {
            let are_different = aad1_len != aad2_len;
            assert!(are_different);
        }
    }

    /// Verify ciphertext format structure
    /// Property: Ciphertext format is always [nonce(12)][ciphertext+tag]
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_ciphertext_format_structure() {
        let plaintext_len: usize = kani::any();
        kani::assume(plaintext_len < 200);

        // Nonce size is fixed at 12 bytes for AES-GCM
        const NONCE_SIZE: usize = 12;
        // Auth tag size is 16 bytes for AES-256-GCM
        const TAG_SIZE: usize = 16;

        // Property: Ciphertext size is nonce + plaintext + tag
        let expected_ciphertext_len = NONCE_SIZE + plaintext_len + TAG_SIZE;

        // Verify calculation doesn't overflow
        assert!(expected_ciphertext_len >= NONCE_SIZE);
        assert!(expected_ciphertext_len >= plaintext_len);
        assert!(expected_ciphertext_len >= TAG_SIZE);
    }

    /// Verify authenticated encryption property
    /// Property: Tampering detection logic is sound
    #[kani::proof]
    #[kani::unwind(3)]
    fn verify_tamper_detection_logic() {
        // Symbolic ciphertext modification
        let was_modified: bool = kani::any();

        // Property: If ciphertext was modified, authentication must fail
        // (This verifies the logic - actual crypto enforcement is in ring library)
        if was_modified {
            let should_fail_auth = true;
            assert!(should_fail_auth);
        }
    }
}