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
//! C FFI wrappers for encryption operations.
//!
//! Provides C-compatible functions for AES-256-GCM encryption with:
//! - Panic safety via catch_unwind
//! - Comprehensive null pointer checks
//! - Counter exhaustion detection
//! - Clear error reporting via CachekitError enum
//!
//! All functions are feature-gated with #[cfg(feature = "encryption")]

// AES-256-GCM ciphertext format constants
// Output format: [nonce(12)][ciphertext][auth_tag(16)]
#[cfg(feature = "encryption")]
const NONCE_SIZE: usize = 12;
#[cfg(feature = "encryption")]
const TAG_SIZE: usize = 16;
/// Total overhead added to plaintext: 12-byte nonce + 16-byte authentication tag
#[cfg(feature = "encryption")]
const CIPHERTEXT_OVERHEAD: usize = NONCE_SIZE + TAG_SIZE; // 28 bytes

#[cfg(feature = "encryption")]
use crate::encryption::{derive_domain_key, ZeroKnowledgeEncryptor};
#[cfg(feature = "encryption")]
use crate::ffi::error::CachekitError;
#[cfg(feature = "encryption")]
use crate::ffi::handles::CachekitEncryptor;
#[cfg(feature = "encryption")]
use std::panic::catch_unwind;
#[cfg(feature = "encryption")]
use std::slice;

/// Create a new ZeroKnowledgeEncryptor instance.
///
/// # Returns
/// Opaque pointer to ZeroKnowledgeEncryptor instance, or null on failure.
/// Must be freed with `cachekit_encryptor_free`.
///
/// # Parameters
/// - `error_out`: Optional pointer to receive error code on failure (may be null)
///
/// # Error Codes
/// - `CachekitError::Ok` (0) on success
/// - `CachekitError::RngFailure` (13) if random number generation failed
///
/// # Safety
/// - Returned pointer must be freed with `cachekit_encryptor_free`
/// - Do not use pointer after calling `cachekit_encryptor_free`
/// - Function is panic-safe and will never unwind across FFI boundary
///
/// # ⚠️ CRITICAL: NONCE REUSE WARNING ⚠️
///
/// **SEVERITY: CATASTROPHIC** - Nonce reuse in AES-GCM enables complete key recovery
/// from as few as 2 ciphertexts encrypted with the same nonce.
///
/// **ARCHITECTURE**: Each `CachekitEncryptor` handle has an INDEPENDENT nonce counter
/// starting at 0. This means:
///
/// ## ✅ CORRECT USAGE (Singleton Pattern)
/// ```c
/// // ONE handle per key for the application lifetime
/// CachekitEncryptor* encryptor = cachekit_encryptor_new(NULL);
/// // Use this single handle for all encryptions with this key
/// cachekit_encrypt(encryptor, key, ...);  // nonce=0
/// cachekit_encrypt(encryptor, key, ...);  // nonce=1
/// cachekit_encrypt(encryptor, key, ...);  // nonce=2
/// ```
///
/// ## ❌ INCORRECT USAGE (Immediate Nonce Reuse!)
/// ```c
/// // DANGER: Creating multiple handles with the same key
/// CachekitEncryptor* enc1 = cachekit_encryptor_new(NULL);
/// CachekitEncryptor* enc2 = cachekit_encryptor_new(NULL);
/// cachekit_encrypt(enc1, key, ...);  // nonce=0
/// cachekit_encrypt(enc2, key, ...);  // nonce=0 AGAIN! KEY COMPROMISED!
/// ```
///
/// **RECOMMENDATION**: Store the encryptor handle in a global/singleton and reuse it.
/// Each handle supports 2^32 (~4 billion) encryptions before requiring a new key.
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_encryptor_new(
    error_out: *mut CachekitError,
) -> *mut CachekitEncryptor {
    let result = catch_unwind(|| match ZeroKnowledgeEncryptor::new() {
        Ok(encryptor) => {
            if !error_out.is_null() {
                unsafe { *error_out = CachekitError::Ok };
            }
            CachekitEncryptor::into_opaque_ptr(encryptor)
        }
        Err(e) => {
            if !error_out.is_null() {
                unsafe { *error_out = CachekitError::from(e) };
            }
            std::ptr::null_mut()
        }
    });

    result.unwrap_or_else(|_| {
        if !error_out.is_null() {
            unsafe { *error_out = CachekitError::InvalidInput };
        }
        std::ptr::null_mut()
    })
}

/// Free a ZeroKnowledgeEncryptor instance.
///
/// # Parameters
/// - `handle`: Pointer returned from `cachekit_encryptor_new`
///
/// # Safety
/// - `handle` must have been created by `cachekit_encryptor_new`
/// - `handle` must not be null
/// - `handle` must not be used after this call
/// - Function is panic-safe and will never unwind across FFI boundary
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_encryptor_free(handle: *mut CachekitEncryptor) {
    let _ = catch_unwind(|| {
        // from_opaque_ptr handles null check and validity tracking
        // Returns None for null, already-freed, or never-created handles
        // SAFETY: If Some is returned, we own the encryptor and it will be dropped
        unsafe {
            let _encryptor = CachekitEncryptor::from_opaque_ptr(handle);
            // If Some: _encryptor dropped here, memory reclaimed
            // If None: handle was invalid (null, double-free, or never created)
        }
    });
}

/// Get current nonce counter value from encryptor.
///
/// Used for monitoring counter exhaustion. Returns 0 if handle is null.
///
/// # Parameters
/// - `handle`: Pointer to encryptor instance (must not be null)
///
/// # Returns
/// Current nonce counter value (0 to 2^32-1), or 0 if handle is null.
///
/// # Safety
/// - `handle` must have been created by `cachekit_encryptor_new`
/// - `handle` must remain valid for duration of call
/// - Function is panic-safe and will never unwind across FFI boundary
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_encryptor_get_counter(handle: *const CachekitEncryptor) -> u64 {
    let result = catch_unwind(|| {
        // as_ref handles null check and validity tracking
        // SAFETY: We don't consume the handle, just borrow it
        let encryptor = match unsafe { CachekitEncryptor::as_ref(handle) } {
            Some(enc) => enc,
            None => return 0, // Invalid handle
        };
        encryptor.get_nonce_counter()
    });

    result.unwrap_or(0)
}

/// Encrypt data using AES-256-GCM with authenticated additional data.
///
/// # Parameters
/// - `handle`: Pointer to encryptor instance (must not be null)
/// - `key`: Pointer to 256-bit (32-byte) encryption key (must not be null)
/// - `key_len`: Length of key in bytes (must be 32)
/// - `aad`: Pointer to additional authenticated data (must not be null, can point to empty data)
/// - `aad_len`: Length of AAD in bytes (can be 0)
/// - `plaintext`: Pointer to plaintext data (must not be null)
/// - `plaintext_len`: Length of plaintext in bytes
/// - `output`: Pointer to output buffer (must not be null)
/// - `output_len`: Pointer to output buffer size (must not be null)
///   On input: size of output buffer (must be at least `plaintext_len + CIPHERTEXT_OVERHEAD` (28 bytes))
///   On output: actual size of encrypted data
///
/// # Output Format
/// `[nonce(12)][ciphertext][auth_tag(16)]`
///
/// # Returns
/// - `CachekitError::Ok` (0) on success
/// - `CachekitError::NullPointer` (9) if any pointer is null
/// - `CachekitError::InvalidKeyLength` (7) if key is not 32 bytes
/// - `CachekitError::RotationNeeded` (5) if counter >= 2^31 (recommended rotation threshold)
/// - `CachekitError::CounterExhausted` (6) if counter >= 2^32 (critical, requires new encryptor)
/// - `CachekitError::BufferTooSmall` (8) if output buffer is too small
/// - `CachekitError::InvalidInput` (1) for other encryption failures
///
/// # Safety
/// Caller must ensure:
/// - All pointers point to valid memory regions of specified lengths
/// - `output` buffer has at least `plaintext_len + CIPHERTEXT_OVERHEAD` (28) bytes available
/// - Pointers remain valid for duration of call
/// - Key material is never logged or exposed
///
/// Function is panic-safe and will never unwind across FFI boundary.
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_encrypt(
    handle: *mut CachekitEncryptor,
    key: *const u8,
    key_len: usize,
    aad: *const u8,
    aad_len: usize,
    plaintext: *const u8,
    plaintext_len: usize,
    output: *mut u8,
    output_len: *mut usize,
) -> CachekitError {
    let result = catch_unwind(|| {
        // Null pointer checks for non-handle parameters
        if key.is_null() {
            return CachekitError::NullPointer;
        }
        if aad.is_null() {
            return CachekitError::NullPointer;
        }
        if plaintext.is_null() {
            return CachekitError::NullPointer;
        }
        if output.is_null() {
            return CachekitError::NullPointer;
        }
        if output_len.is_null() {
            return CachekitError::NullPointer;
        }

        // Validate handle (checks null, registered, and not freed)
        // SAFETY: We don't consume the handle, just borrow it
        let encryptor = match unsafe { CachekitEncryptor::as_ref(handle) } {
            Some(enc) => enc,
            None => return CachekitError::InvalidHandle,
        };

        // SAFETY: We've verified output_len is not null
        let available_size = unsafe { *output_len };

        // Check counter before operation (2^31 threshold for rotation recommendation)
        let counter = encryptor.get_nonce_counter();

        // At 2^31, warn that rotation is needed (before reaching 2^32 hard limit)
        if counter >= (1u64 << 31) {
            return CachekitError::RotationNeeded;
        }

        // Validate key length
        if key_len != 32 {
            return CachekitError::InvalidKeyLength;
        }

        // SAFETY: We've verified pointers are not null and lengths define valid ranges
        let key_slice = unsafe { slice::from_raw_parts(key, key_len) };
        let aad_slice = unsafe { slice::from_raw_parts(aad, aad_len) };
        let plaintext_slice = unsafe { slice::from_raw_parts(plaintext, plaintext_len) };

        // Perform encryption
        let ciphertext = match encryptor.encrypt_aes_gcm(plaintext_slice, key_slice, aad_slice) {
            Ok(data) => data,
            Err(e) => {
                // Convert encryption errors to FFI error codes
                return CachekitError::from(e);
            }
        };

        // Check if output buffer is large enough
        // Ciphertext format: [nonce(12)][encrypted_data][tag(16)] = plaintext_len + CIPHERTEXT_OVERHEAD
        if ciphertext.len() > available_size {
            // SAFETY: We've verified output_len is not null
            unsafe {
                *output_len = ciphertext.len();
            }
            return CachekitError::BufferTooSmall;
        }

        // Copy to output buffer
        // SAFETY: We've verified:
        // - output is not null
        // - available_size is the valid size of output buffer
        // - ciphertext.len() <= available_size (checked above)
        unsafe {
            std::ptr::copy_nonoverlapping(ciphertext.as_ptr(), output, ciphertext.len());
            *output_len = ciphertext.len();
        }

        CachekitError::Ok
    });

    result.unwrap_or(CachekitError::InvalidInput)
}

/// Decrypt data using AES-256-GCM with authenticated additional data.
///
/// # Parameters
/// - `handle`: Pointer to encryptor instance (must not be null). Use the same handle
///   that was used for encryption to maintain API consistency. While decryption is
///   stateless, using a handle avoids the overhead of creating a new encryptor per call.
/// - `key`: Pointer to 256-bit (32-byte) decryption key (must not be null)
/// - `key_len`: Length of key in bytes (must be 32)
/// - `aad`: Pointer to additional authenticated data (must not be null, can point to empty data)
/// - `aad_len`: Length of AAD in bytes (must match encryption AAD)
/// - `ciphertext`: Pointer to encrypted data in format `[nonce(12)][ciphertext][tag(16)]`
/// - `ciphertext_len`: Length of ciphertext in bytes (must be at least `CIPHERTEXT_OVERHEAD` (28))
/// - `output`: Pointer to output buffer (must not be null)
/// - `output_len`: Pointer to output buffer size (must not be null)
///   On input: size of output buffer
///   On output: actual size of plaintext data
///
/// # Returns
/// - `CachekitError::Ok` (0) on success
/// - `CachekitError::InvalidHandle` (10) if handle is null or invalid
/// - `CachekitError::NullPointer` (9) if any other pointer is null
/// - `CachekitError::InvalidKeyLength` (7) if key is not 32 bytes
/// - `CachekitError::InvalidInput` (1) if ciphertext is too short or malformed
/// - `CachekitError::DecryptionFailed` (4) if authentication tag verification fails
/// - `CachekitError::BufferTooSmall` (8) if output buffer is too small
///
/// # Safety
/// Caller must ensure:
/// - `handle` was created by `cachekit_encryptor_new` and not freed
/// - All pointers point to valid memory regions of specified lengths
/// - `output` buffer has enough space for decrypted data
/// - Pointers remain valid for duration of call
/// - AAD matches the AAD used during encryption
///
/// Function is panic-safe and will never unwind across FFI boundary.
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_decrypt(
    handle: *const CachekitEncryptor,
    key: *const u8,
    key_len: usize,
    aad: *const u8,
    aad_len: usize,
    ciphertext: *const u8,
    ciphertext_len: usize,
    output: *mut u8,
    output_len: *mut usize,
) -> CachekitError {
    let result = catch_unwind(|| {
        // Null pointer checks for non-handle parameters
        if key.is_null() {
            return CachekitError::NullPointer;
        }
        if aad.is_null() {
            return CachekitError::NullPointer;
        }
        if ciphertext.is_null() {
            return CachekitError::NullPointer;
        }
        if output.is_null() {
            return CachekitError::NullPointer;
        }
        if output_len.is_null() {
            return CachekitError::NullPointer;
        }

        // Validate handle (checks null, registered, and not freed)
        // SAFETY: We don't consume the handle, just borrow it
        let encryptor = match unsafe { CachekitEncryptor::as_ref(handle) } {
            Some(enc) => enc,
            None => return CachekitError::InvalidHandle,
        };

        // SAFETY: We've verified output_len is not null
        let available_size = unsafe { *output_len };

        // Validate key length
        if key_len != 32 {
            return CachekitError::InvalidKeyLength;
        }

        // Validate minimum ciphertext length (nonce + tag = CIPHERTEXT_OVERHEAD bytes minimum)
        if ciphertext_len < CIPHERTEXT_OVERHEAD {
            return CachekitError::InvalidInput;
        }

        // SAFETY: We've verified pointers are not null and lengths define valid ranges
        let key_slice = unsafe { slice::from_raw_parts(key, key_len) };
        let aad_slice = unsafe { slice::from_raw_parts(aad, aad_len) };
        let ciphertext_slice = unsafe { slice::from_raw_parts(ciphertext, ciphertext_len) };

        // Perform decryption using the provided handle (avoids creating new encryptor)
        let plaintext = match encryptor.decrypt_aes_gcm(ciphertext_slice, key_slice, aad_slice) {
            Ok(data) => data,
            Err(e) => {
                // Convert decryption errors to FFI error codes
                return CachekitError::from(e);
            }
        };

        // Check if output buffer is large enough
        if plaintext.len() > available_size {
            // SAFETY: We've verified output_len is not null
            unsafe {
                *output_len = plaintext.len();
            }
            return CachekitError::BufferTooSmall;
        }

        // Copy to output buffer
        // SAFETY: We've verified:
        // - output is not null
        // - available_size is the valid size of output buffer
        // - plaintext.len() <= available_size (checked above)
        unsafe {
            std::ptr::copy_nonoverlapping(plaintext.as_ptr(), output, plaintext.len());
            *output_len = plaintext.len();
        }

        CachekitError::Ok
    });

    result.unwrap_or(CachekitError::DecryptionFailed)
}

/// Derive a domain-specific key using HKDF-SHA256.
///
/// # Parameters
/// - `master`: Pointer to master key (must not be null, minimum 16 bytes, recommended 32 bytes)
/// - `master_len`: Length of master key in bytes (minimum 16)
/// - `salt`: Pointer to salt/tenant ID (must not be null, minimum 1 byte)
/// - `salt_len`: Length of salt in bytes (minimum 1)
/// - `domain`: Pointer to domain context string (must not be null)
/// - `domain_len`: Length of domain string in bytes
/// - `out_key`: Pointer to output buffer for derived key (must not be null, must be at least 32 bytes)
///
/// # Returns
/// - `CachekitError::Ok` (0) on success, out_key contains 32-byte derived key
/// - `CachekitError::NullPointer` (9) if any pointer is null
/// - `CachekitError::InvalidKeyLength` (7) if master key is less than 16 bytes
/// - `CachekitError::InvalidInput` (1) if salt is empty or domain is empty
///
/// # Safety
/// Caller must ensure:
/// - All pointers point to valid memory regions of specified lengths
/// - `out_key` buffer has at least 32 bytes available
/// - Pointers remain valid for duration of call
///
/// Function is panic-safe and will never unwind across FFI boundary.
#[cfg(feature = "encryption")]
#[no_mangle]
pub unsafe extern "C" fn cachekit_derive_key(
    master: *const u8,
    master_len: usize,
    salt: *const u8,
    salt_len: usize,
    domain: *const u8,
    domain_len: usize,
    out_key: *mut u8,
) -> CachekitError {
    let result = catch_unwind(|| {
        // Null pointer checks
        if master.is_null() {
            return CachekitError::NullPointer;
        }
        if salt.is_null() {
            return CachekitError::NullPointer;
        }
        if domain.is_null() {
            return CachekitError::NullPointer;
        }
        if out_key.is_null() {
            return CachekitError::NullPointer;
        }

        // SAFETY: We've verified pointers are not null and lengths define valid ranges
        let master_slice = unsafe { slice::from_raw_parts(master, master_len) };
        let salt_slice = unsafe { slice::from_raw_parts(salt, salt_len) };
        let domain_slice = unsafe { slice::from_raw_parts(domain, domain_len) };

        // Convert domain bytes to UTF-8 string
        let domain_str = match std::str::from_utf8(domain_slice) {
            Ok(s) => s,
            Err(_) => return CachekitError::InvalidInput,
        };

        // Derive key using HKDF
        let derived_key = match derive_domain_key(master_slice, domain_str, salt_slice) {
            Ok(key) => key,
            Err(e) => {
                // Convert key derivation errors to FFI error codes
                return CachekitError::from(e);
            }
        };

        // Copy 32 bytes to output buffer
        // SAFETY: We've verified out_key is not null, and derived_key is always 32 bytes
        unsafe {
            std::ptr::copy_nonoverlapping(derived_key.as_ptr(), out_key, 32);
        }

        CachekitError::Ok
    });

    result.unwrap_or(CachekitError::InvalidInput)
}

#[cfg(all(test, feature = "encryption"))]
mod tests {
    use super::*;

    #[test]
    fn test_encryptor_lifecycle() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());
            assert!(!handle.is_null());

            let counter = cachekit_encryptor_get_counter(handle);
            assert_eq!(counter, 0);

            cachekit_encryptor_free(handle);
        }
    }

    #[test]
    fn test_encrypt_decrypt_roundtrip() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());
            assert!(!handle.is_null());

            let key = [0u8; 32];
            let aad = b"test_context";
            let plaintext = b"Hello, FFI encryption!";

            // Encrypt
            let mut ciphertext = vec![0u8; plaintext.len() + 100];
            let mut ciphertext_len = ciphertext.len();
            let result = cachekit_encrypt(
                handle,
                key.as_ptr(),
                32,
                aad.as_ptr(),
                aad.len(),
                plaintext.as_ptr(),
                plaintext.len(),
                ciphertext.as_mut_ptr(),
                &mut ciphertext_len,
            );
            assert_eq!(result, CachekitError::Ok);
            assert_eq!(ciphertext_len, plaintext.len() + CIPHERTEXT_OVERHEAD); // nonce(12) + tag(16)

            // Decrypt (using the same handle for API consistency)
            let mut decrypted = vec![0u8; plaintext.len() + 100];
            let mut decrypted_len = decrypted.len();
            let result = cachekit_decrypt(
                handle,
                key.as_ptr(),
                32,
                aad.as_ptr(),
                aad.len(),
                ciphertext.as_ptr(),
                ciphertext_len,
                decrypted.as_mut_ptr(),
                &mut decrypted_len,
            );
            assert_eq!(result, CachekitError::Ok);
            assert_eq!(decrypted_len, plaintext.len());
            assert_eq!(&decrypted[..decrypted_len], plaintext);

            cachekit_encryptor_free(handle);
        }
    }

    #[test]
    fn test_encrypt_null_checks() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());
            let key = [0u8; 32];
            let aad = b"test";
            let plaintext = b"test";
            let mut output = vec![0u8; 100];
            let mut output_len = output.len();

            // Null handle (returns InvalidHandle, not NullPointer - handles are validated separately)
            assert_eq!(
                cachekit_encrypt(
                    std::ptr::null_mut(),
                    key.as_ptr(),
                    32,
                    aad.as_ptr(),
                    aad.len(),
                    plaintext.as_ptr(),
                    plaintext.len(),
                    output.as_mut_ptr(),
                    &mut output_len,
                ),
                CachekitError::InvalidHandle
            );

            // Null key
            assert_eq!(
                cachekit_encrypt(
                    handle,
                    std::ptr::null(),
                    32,
                    aad.as_ptr(),
                    aad.len(),
                    plaintext.as_ptr(),
                    plaintext.len(),
                    output.as_mut_ptr(),
                    &mut output_len,
                ),
                CachekitError::NullPointer
            );

            cachekit_encryptor_free(handle);
        }
    }

    #[test]
    fn test_decrypt_wrong_key() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());

            let key1 = [0u8; 32];
            let key2 = [1u8; 32];
            let aad = b"test";
            let plaintext = b"secret";

            // Encrypt with key1
            let mut ciphertext = vec![0u8; plaintext.len() + 100];
            let mut ciphertext_len = ciphertext.len();
            let result = cachekit_encrypt(
                handle,
                key1.as_ptr(),
                32,
                aad.as_ptr(),
                aad.len(),
                plaintext.as_ptr(),
                plaintext.len(),
                ciphertext.as_mut_ptr(),
                &mut ciphertext_len,
            );
            assert_eq!(result, CachekitError::Ok);

            // Try to decrypt with key2 (using same handle, different key should fail auth)
            let mut decrypted = vec![0u8; plaintext.len() + 100];
            let mut decrypted_len = decrypted.len();
            let result = cachekit_decrypt(
                handle,
                key2.as_ptr(),
                32,
                aad.as_ptr(),
                aad.len(),
                ciphertext.as_ptr(),
                ciphertext_len,
                decrypted.as_mut_ptr(),
                &mut decrypted_len,
            );
            assert_eq!(result, CachekitError::DecryptionFailed);

            cachekit_encryptor_free(handle);
        }
    }

    #[test]
    fn test_derive_key_basic() {
        unsafe {
            let master = b"test_master_key_32_bytes_long!!!";
            let salt = b"tenant123";
            let domain = b"encryption";
            let mut out_key = [0u8; 32];

            let result = cachekit_derive_key(
                master.as_ptr(),
                master.len(),
                salt.as_ptr(),
                salt.len(),
                domain.as_ptr(),
                domain.len(),
                out_key.as_mut_ptr(),
            );
            assert_eq!(result, CachekitError::Ok);

            // Verify key is not all zeros
            assert_ne!(out_key, [0u8; 32]);
        }
    }

    #[test]
    fn test_derive_key_deterministic() {
        unsafe {
            let master = b"test_master_key_32_bytes_long!!!";
            let salt = b"tenant123";
            let domain = b"encryption";
            let mut key1 = [0u8; 32];
            let mut key2 = [0u8; 32];

            cachekit_derive_key(
                master.as_ptr(),
                master.len(),
                salt.as_ptr(),
                salt.len(),
                domain.as_ptr(),
                domain.len(),
                key1.as_mut_ptr(),
            );

            cachekit_derive_key(
                master.as_ptr(),
                master.len(),
                salt.as_ptr(),
                salt.len(),
                domain.as_ptr(),
                domain.len(),
                key2.as_mut_ptr(),
            );

            assert_eq!(key1, key2);
        }
    }

    #[test]
    fn test_invalid_key_length() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());
            let short_key = [0u8; 16]; // Should be 32
            let aad = b"test";
            let plaintext = b"test";
            let mut output = vec![0u8; 100];
            let mut output_len = output.len();

            let result = cachekit_encrypt(
                handle,
                short_key.as_ptr(),
                16,
                aad.as_ptr(),
                aad.len(),
                plaintext.as_ptr(),
                plaintext.len(),
                output.as_mut_ptr(),
                &mut output_len,
            );
            assert_eq!(result, CachekitError::InvalidKeyLength);

            cachekit_encryptor_free(handle);
        }
    }

    #[test]
    fn test_buffer_too_small() {
        unsafe {
            let handle = cachekit_encryptor_new(std::ptr::null_mut());
            let key = [0u8; 32];
            let aad = b"test";
            let plaintext = b"Hello, World!";
            let mut output = vec![0u8; 10]; // Way too small
            let mut output_len = output.len();

            let result = cachekit_encrypt(
                handle,
                key.as_ptr(),
                32,
                aad.as_ptr(),
                aad.len(),
                plaintext.as_ptr(),
                plaintext.len(),
                output.as_mut_ptr(),
                &mut output_len,
            );
            assert_eq!(result, CachekitError::BufferTooSmall);
            assert_eq!(output_len, plaintext.len() + CIPHERTEXT_OVERHEAD);

            cachekit_encryptor_free(handle);
        }
    }
}