coding-agent-search 0.6.0

Unified TUI search over local coding agent histories
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
use aes_gcm::aead::{Aead, KeyInit, Payload};
use aes_gcm::{Aes256Gcm, Key, Nonce};
use ring::{
    hkdf::{self as ring_hkdf, KeyType},
    hmac,
};

pub use argon2::Params as Argon2Params;

const AES_GCM_KEY_LEN: usize = 32;
const AES_GCM_NONCE_LEN: usize = 12;
const AES_GCM_TAG_LEN: usize = 16;

struct HkdfOutputLen(usize);

impl KeyType for HkdfOutputLen {
    fn len(&self) -> usize {
        self.0
    }
}

fn validate_length(label: &str, actual: usize, expected: usize) -> Result<(), String> {
    if actual == expected {
        Ok(())
    } else {
        Err(format!(
            "{} length invalid: expected {} bytes, got {}",
            label, expected, actual
        ))
    }
}

pub fn aes_gcm_encrypt(
    key: &[u8],
    nonce: &[u8],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<(Vec<u8>, Vec<u8>), String> {
    validate_length("AES-GCM key", key.len(), AES_GCM_KEY_LEN)?;
    validate_length("AES-GCM nonce", nonce.len(), AES_GCM_NONCE_LEN)?;

    let key = Key::<Aes256Gcm>::from_slice(key);
    let cipher = Aes256Gcm::new(key);
    let nonce = Nonce::from_slice(nonce);
    let payload = Payload {
        msg: plaintext,
        aad,
    };

    // aes-gcm returns ciphertext + tag appended.
    let ciphertext_with_tag = cipher
        .encrypt(nonce, payload)
        .map_err(|e| format!("encryption failure: {}", e))?;

    if ciphertext_with_tag.len() < AES_GCM_TAG_LEN {
        return Err("encryption failure: ciphertext too short".to_string());
    }

    // Tag is 16 bytes for AES-256-GCM
    let split_idx = ciphertext_with_tag.len() - AES_GCM_TAG_LEN;
    let (cipher, tag) = ciphertext_with_tag.split_at(split_idx);

    Ok((cipher.to_vec(), tag.to_vec()))
}

pub fn aes_gcm_decrypt(
    key: &[u8],
    nonce: &[u8],
    ciphertext: &[u8],
    aad: &[u8],
    tag: &[u8],
) -> Result<Vec<u8>, String> {
    validate_length("AES-GCM key", key.len(), AES_GCM_KEY_LEN)?;
    validate_length("AES-GCM nonce", nonce.len(), AES_GCM_NONCE_LEN)?;
    validate_length("AES-GCM tag", tag.len(), AES_GCM_TAG_LEN)?;

    let key = Key::<Aes256Gcm>::from_slice(key);
    let cipher = Aes256Gcm::new(key);
    let nonce = Nonce::from_slice(nonce);

    // Combine ciphertext and tag for decryption (aes-gcm crate expects them together)
    // Use the Payload API directly to avoid manual concatenation.
    let mut payload_vec = Vec::with_capacity(ciphertext.len() + tag.len());
    payload_vec.extend_from_slice(ciphertext);
    payload_vec.extend_from_slice(tag);

    let payload = Payload {
        msg: &payload_vec,
        aad,
    };

    cipher
        .decrypt(nonce, payload)
        .map_err(|e| format!("decryption failed: {}", e))
}

pub fn argon2id_hash(
    password: &[u8],
    salt: &[u8],
    params: &Argon2Params,
) -> Result<Vec<u8>, String> {
    let argon2 = argon2::Argon2::new(
        argon2::Algorithm::Argon2id,
        argon2::Version::V0x13,
        params.clone(),
    );

    let mut output = vec![0u8; params.output_len().unwrap_or(32)];
    argon2
        .hash_password_into(password, salt, &mut output)
        .map_err(|e| format!("argon2 hashing failed: {}", e))?;
    Ok(output)
}

/// HKDF-SHA256 extract+expand. Per
/// `coding_agent_session_search-vz9t8.4`, this function is instrumented with
/// safe-to-log tracing: only operation name + lengths are recorded; the IKM,
/// salt, info, and output bytes are NEVER logged. The `info` argument is
/// treated as a domain-separation LABEL; if and only if it contains valid
/// UTF-8 AND is short (≤64 bytes), it is logged for forensics. Otherwise it
/// is replaced with a length-only summary.
#[tracing::instrument(
    name = "hkdf_extract_expand",
    skip_all,
    fields(
        operation = "hkdf_extract_expand",
        ikm_len = ikm.len(),
        salt_len = salt.len(),
        info_len = info.len(),
        info_label,
        output_len = len,
    )
)]
pub fn hkdf_extract_expand(
    ikm: &[u8],
    salt: &[u8],
    info: &[u8],
    len: usize,
) -> Result<Vec<u8>, String> {
    // Populate the `info_label` field via tracing::Span::current().record so
    // we don't unconditionally include the info bytes — only when they form a
    // short ASCII-safe domain-separation label.
    let span = tracing::Span::current();
    let label_safe = info.len() <= 64
        && std::str::from_utf8(info)
            .map(|s| {
                s.chars()
                    .all(|c| c.is_ascii_graphic() || c == ' ' || c == '-' || c == '_' || c == '.')
            })
            .unwrap_or(false);
    if label_safe {
        // SAFETY for security: only ASCII-graphic short strings reach here.
        // Actual key material (high-entropy bytes) would fail the ASCII gate.
        if let Ok(s) = std::str::from_utf8(info) {
            span.record("info_label", s);
        }
    } else {
        span.record("info_label", "<binary or oversized; redacted>");
    }
    tracing::debug!(
        target: "cass::encryption",
        operation = "hkdf_extract_expand",
        ikm_len = ikm.len(),
        salt_len = salt.len(),
        info_len = info.len(),
        output_len = len,
        "hkdf_extract_expand: entering"
    );
    let start = std::time::Instant::now();

    let salt_obj = ring_hkdf::Salt::new(ring_hkdf::HKDF_SHA256, salt);
    let prk = salt_obj.extract(ikm);
    let info_components = [info];
    let okm = prk
        .expand(&info_components, HkdfOutputLen(len))
        .map_err(|_| "hkdf expand failed: invalid output length".to_string())?;
    let mut output = vec![0u8; len];
    okm.fill(&mut output)
        .map_err(|_| "hkdf expand failed: unable to fill output buffer".to_string())?;

    let elapsed_us = start.elapsed().as_micros() as u64;
    tracing::debug!(
        target: "cass::encryption",
        operation = "hkdf_extract_expand",
        elapsed_us = elapsed_us,
        "hkdf_extract_expand: ok"
    );
    Ok(output)
}

/// HKDF extract step. Per `coding_agent_session_search-vz9t8.4`, instrumented
/// with safe tracing — only lengths are recorded.
#[tracing::instrument(
    name = "hkdf_extract",
    skip_all,
    fields(operation = "hkdf_extract", salt_len = salt.len(), ikm_len = ikm.len()),
)]
pub fn hkdf_extract(salt: &[u8], ikm: &[u8]) -> Vec<u8> {
    let key = hmac::Key::new(hmac::HMAC_SHA256, salt);
    let result = hmac::sign(&key, ikm).as_ref().to_vec();
    tracing::debug!(
        target: "cass::encryption",
        operation = "hkdf_extract",
        output_len = result.len(),
        "hkdf_extract: ok"
    );
    result
}

// =============================================================================
// Unit Tests
// =============================================================================

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

    fn assert_err_contains<T>(result: Result<T, String>, expected: &str) {
        let err = result.err().expect("operation should fail");
        assert!(
            err.contains(expected),
            "expected error containing {expected:?}, got {err:?}"
        );
    }

    // =========================================================================
    // AES-GCM Encrypt/Decrypt Tests
    // =========================================================================

    #[test]
    fn aes_gcm_encrypt_decrypt_round_trip() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"Hello, world!";
        let aad = b"additional data";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();

        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn aes_gcm_round_trip_empty_plaintext() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"";
        let aad = b"";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        assert!(ciphertext.is_empty());
        assert_eq!(tag.len(), 16);

        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();
        assert!(decrypted.is_empty());
    }

    #[test]
    fn aes_gcm_round_trip_large_data() {
        let key = [0xab; 32];
        let nonce = [0xcd; 12];
        let plaintext: Vec<u8> = (0..10000).map(|i| (i % 256) as u8).collect();
        let aad = b"large data test";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, &plaintext, aad).unwrap();

        assert_eq!(ciphertext.len(), plaintext.len());

        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn aes_gcm_round_trip_unicode_data() {
        let key = [0x42; 32];
        let nonce = [0x13; 12];
        let plaintext = "日本語テスト 🦀 Rust".as_bytes();
        let aad = "unicode AAD: émojis 🎉".as_bytes();

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();
        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();

        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn aes_gcm_encrypt_invalid_key_length() {
        let key = [0u8; 16]; // Should be 32 bytes
        let nonce = [0u8; 12];
        let plaintext = b"test";
        let aad = b"";

        let result = aes_gcm_encrypt(&key, &nonce, plaintext, aad);
        assert_err_contains(result, "key length invalid");
    }

    #[test]
    fn aes_gcm_encrypt_invalid_nonce_length() {
        let key = [0u8; 32];
        let nonce = [0u8; 16]; // Should be 12 bytes
        let plaintext = b"test";
        let aad = b"";

        let result = aes_gcm_encrypt(&key, &nonce, plaintext, aad);
        assert_err_contains(result, "nonce length invalid");
    }

    #[test]
    fn aes_gcm_decrypt_invalid_key_length() {
        let key = [0u8; 31]; // Should be 32 bytes
        let nonce = [0u8; 12];
        let ciphertext = b"ciphertext";
        let aad = b"";
        let tag = [0u8; 16];

        let result = aes_gcm_decrypt(&key, &nonce, ciphertext, aad, &tag);
        assert_err_contains(result, "key length invalid");
    }

    #[test]
    fn aes_gcm_decrypt_invalid_nonce_length() {
        let key = [0u8; 32];
        let nonce = [0u8; 8]; // Should be 12 bytes
        let ciphertext = b"ciphertext";
        let aad = b"";
        let tag = [0u8; 16];

        let result = aes_gcm_decrypt(&key, &nonce, ciphertext, aad, &tag);
        assert_err_contains(result, "nonce length invalid");
    }

    #[test]
    fn aes_gcm_decrypt_invalid_tag_length() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let ciphertext = b"ciphertext";
        let aad = b"";
        let tag = [0u8; 8]; // Should be 16 bytes

        let result = aes_gcm_decrypt(&key, &nonce, ciphertext, aad, &tag);
        assert_err_contains(result, "tag length invalid");
    }

    #[test]
    fn aes_gcm_decrypt_wrong_key_fails() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"secret message";
        let aad = b"aad";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        // Use different key for decryption
        let wrong_key = [1u8; 32];
        let result = aes_gcm_decrypt(&wrong_key, &nonce, &ciphertext, aad, &tag);
        assert_err_contains(result, "decryption failed");
    }

    #[test]
    fn aes_gcm_decrypt_wrong_aad_fails() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"secret message";
        let aad = b"correct aad";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        // Use different AAD for decryption
        let wrong_aad = b"wrong aad";
        let result = aes_gcm_decrypt(&key, &nonce, &ciphertext, wrong_aad, &tag);
        assert_err_contains(result, "decryption failed");
    }

    #[test]
    fn aes_gcm_decrypt_tampered_ciphertext_fails() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"secret message";
        let aad = b"aad";

        let (mut ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        // Tamper with ciphertext
        ciphertext[0] ^= 0xff;
        let result = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag);
        assert_err_contains(result, "decryption failed");
    }

    #[test]
    fn aes_gcm_decrypt_tampered_tag_fails() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"secret message";
        let aad = b"aad";

        let (ciphertext, mut tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();

        // Tamper with tag
        tag[0] ^= 0xff;
        let result = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag);
        assert_err_contains(result, "decryption failed");
    }

    #[test]
    fn aes_gcm_tag_is_correct_size() {
        let key = [0u8; 32];
        let nonce = [0u8; 12];
        let plaintext = b"test";
        let aad = b"";

        let (_, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();
        assert_eq!(tag.len(), 16);
    }

    #[test]
    fn aes_gcm_different_nonces_produce_different_ciphertext() {
        let key = [0u8; 32];
        let plaintext = b"same plaintext";
        let aad = b"same aad";

        let nonce1 = [0u8; 12];
        let nonce2 = [1u8; 12];

        let (ciphertext1, _) = aes_gcm_encrypt(&key, &nonce1, plaintext, aad).unwrap();
        let (ciphertext2, _) = aes_gcm_encrypt(&key, &nonce2, plaintext, aad).unwrap();

        assert_ne!(ciphertext1, ciphertext2);
    }

    // =========================================================================
    // Argon2id Tests
    // =========================================================================

    #[test]
    fn argon2id_hash_produces_deterministic_output() {
        let password = b"password123";
        let salt = b"randomsalt123456"; // 16 bytes
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        let hash1 = argon2id_hash(password, salt, &params).unwrap();
        let hash2 = argon2id_hash(password, salt, &params).unwrap();

        assert_eq!(hash1, hash2);
        assert_eq!(hash1.len(), 32);
    }

    #[test]
    fn argon2id_hash_different_passwords_produce_different_hashes() {
        let salt = b"randomsalt123456";
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        let hash1 = argon2id_hash(b"password1", salt, &params).unwrap();
        let hash2 = argon2id_hash(b"password2", salt, &params).unwrap();

        assert_ne!(hash1, hash2);
    }

    #[test]
    fn argon2id_hash_different_salts_produce_different_hashes() {
        let password = b"samepassword";
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        let hash1 = argon2id_hash(password, b"salt1234567890ab", &params).unwrap();
        let hash2 = argon2id_hash(password, b"salt0987654321xy", &params).unwrap();

        assert_ne!(hash1, hash2);
    }

    #[test]
    fn argon2id_hash_respects_output_length() {
        let password = b"password";
        let salt = b"salt1234567890ab";

        let params_32 = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();
        let params_64 = Argon2Params::new(1024, 1, 1, Some(64)).unwrap();

        let hash_32 = argon2id_hash(password, salt, &params_32).unwrap();
        let hash_64 = argon2id_hash(password, salt, &params_64).unwrap();

        assert_eq!(hash_32.len(), 32);
        assert_eq!(hash_64.len(), 64);
    }

    #[test]
    fn argon2id_hash_empty_password() {
        let password = b"";
        let salt = b"randomsalt123456";
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        let result = argon2id_hash(password, salt, &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 32);
    }

    #[test]
    fn argon2id_hash_unicode_password() {
        let password = "日本語パスワード🔐".as_bytes();
        let salt = b"randomsalt123456";
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        let result = argon2id_hash(password, salt, &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 32);
    }

    // =========================================================================
    // HKDF Tests
    // =========================================================================

    #[test]
    fn hkdf_extract_expand_produces_deterministic_output() {
        let ikm = b"input key material";
        let salt = b"salt value";
        let info = b"context info";

        let okm1 = hkdf_extract_expand(ikm, salt, info, 32).unwrap();
        let okm2 = hkdf_extract_expand(ikm, salt, info, 32).unwrap();

        assert_eq!(okm1, okm2);
        assert_eq!(okm1.len(), 32);
    }

    #[test]
    fn hkdf_extract_expand_respects_output_length() {
        let ikm = b"input key material";
        let salt = b"salt value";
        let info = b"context info";

        let okm_16 = hkdf_extract_expand(ikm, salt, info, 16).unwrap();
        let okm_64 = hkdf_extract_expand(ikm, salt, info, 64).unwrap();

        assert_eq!(okm_16.len(), 16);
        assert_eq!(okm_64.len(), 64);
    }

    #[test]
    fn hkdf_extract_expand_different_info_produces_different_output() {
        let ikm = b"input key material";
        let salt = b"salt value";

        let okm1 = hkdf_extract_expand(ikm, salt, b"info1", 32).unwrap();
        let okm2 = hkdf_extract_expand(ikm, salt, b"info2", 32).unwrap();

        assert_ne!(okm1, okm2);
    }

    #[test]
    fn hkdf_extract_expand_different_salt_produces_different_output() {
        let ikm = b"input key material";
        let info = b"context info";

        let okm1 = hkdf_extract_expand(ikm, b"salt1", info, 32).unwrap();
        let okm2 = hkdf_extract_expand(ikm, b"salt2", info, 32).unwrap();

        assert_ne!(okm1, okm2);
    }

    #[test]
    fn hkdf_extract_expand_empty_inputs() {
        let ikm = b"input key material";

        // Empty salt
        let okm1 = hkdf_extract_expand(ikm, b"", b"info", 32).unwrap();
        assert_eq!(okm1.len(), 32);

        // Empty info
        let okm2 = hkdf_extract_expand(ikm, b"salt", b"", 32).unwrap();
        assert_eq!(okm2.len(), 32);
    }

    #[test]
    fn hkdf_extract_expand_too_long_output_fails() {
        let ikm = b"input key material";
        let salt = b"salt";
        let info = b"info";

        // HKDF-SHA256 max output is 255 * 32 = 8160 bytes
        let result = hkdf_extract_expand(ikm, salt, info, 8161);
        assert!(result.is_err());
    }

    #[test]
    fn hkdf_extract_produces_deterministic_output() {
        let salt = b"salt value";
        let ikm = b"input key material";

        let prk1 = hkdf_extract(salt, ikm);
        let prk2 = hkdf_extract(salt, ikm);

        assert_eq!(prk1, prk2);
        // SHA256 output is 32 bytes
        assert_eq!(prk1.len(), 32);
    }

    #[test]
    fn hkdf_extract_different_ikm_produces_different_output() {
        let salt = b"salt value";

        let prk1 = hkdf_extract(salt, b"ikm1");
        let prk2 = hkdf_extract(salt, b"ikm2");

        assert_ne!(prk1, prk2);
    }

    #[test]
    fn hkdf_extract_different_salt_produces_different_output() {
        let ikm = b"input key material";

        let prk1 = hkdf_extract(b"salt1", ikm);
        let prk2 = hkdf_extract(b"salt2", ikm);

        assert_ne!(prk1, prk2);
    }

    #[test]
    fn hkdf_extract_empty_salt() {
        let ikm = b"input key material";

        let prk = hkdf_extract(b"", ikm);
        assert_eq!(prk.len(), 32);
    }

    #[test]
    fn hkdf_extract_empty_ikm() {
        let salt = b"salt value";

        let prk = hkdf_extract(salt, b"");
        assert_eq!(prk.len(), 32);
    }

    // =========================================================================
    // Integration: Key Derivation + Encryption
    // =========================================================================

    #[test]
    fn integration_argon2_derived_key_for_aes_gcm() {
        let password = b"user_password";
        let salt = b"randomsalt123456";
        let params = Argon2Params::new(1024, 1, 1, Some(32)).unwrap();

        // Derive key from password
        let key = argon2id_hash(password, salt, &params).unwrap();
        assert_eq!(key.len(), 32);

        // Use derived key for encryption
        let nonce = [0u8; 12];
        let plaintext = b"sensitive data";
        let aad = b"";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();
        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();

        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn integration_hkdf_derived_key_for_aes_gcm() {
        let master_secret = b"master_secret";
        let salt = b"application_salt";
        let info = b"encryption_key";

        // Derive key using HKDF
        let key = hkdf_extract_expand(master_secret, salt, info, 32).unwrap();
        assert_eq!(key.len(), 32);

        // Use derived key for encryption
        let nonce = [0u8; 12];
        let plaintext = b"sensitive data";
        let aad = b"";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();
        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();

        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn integration_extract_then_expand() {
        let salt = b"random_salt";
        let ikm = b"initial_key_material";
        let info = b"derived_key";

        // Extract then expand (standard HKDF flow)
        let prk = hkdf_extract(salt, ikm);
        let key = hkdf_extract_expand(&prk, b"", info, 32).unwrap();

        assert_eq!(key.len(), 32);

        // Verify key works for encryption
        let nonce = [0u8; 12];
        let plaintext = b"test data";
        let aad = b"";

        let (ciphertext, tag) = aes_gcm_encrypt(&key, &nonce, plaintext, aad).unwrap();
        let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext, aad, &tag).unwrap();

        assert_eq!(decrypted, plaintext);
    }
}