secure_data 0.1.2

Secret wrappers, envelope encryption, KMS providers, crypto agility, and password hashing.
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
// Envelope encryption and decryption.
//
// Application code calls encrypt_for_storage() and decrypt_for_use().
// This module manages data key generation, wrapping, versioning, nonce generation,
// authenticated encryption, and AAD binding internally.

use aes_gcm::{
    aead::{Aead, KeyInit, Payload},
    Aes256Gcm, Key, Nonce,
};
#[cfg(feature = "pq")]
use base64::{engine::general_purpose::STANDARD as B64, Engine};
use chacha20poly1305::XChaCha20Poly1305;
#[cfg(feature = "pq")]
use hkdf::Hkdf;
#[cfg(feature = "pq")]
use rand::rngs::OsRng;
use rand::RngCore;
use serde::{Deserialize, Serialize};
#[cfg(feature = "pq")]
use sha2::Sha256;
#[cfg(feature = "pq")]
use zeroize::Zeroizing;

use crate::algorithm::{AlgorithmPolicy, CryptoAlgorithm};
use crate::error::DataError;
use crate::kms::KeyProvider;

/// The current envelope format version.
const ENVELOPE_VERSION: &str = "1";
#[cfg(feature = "pq")]
const HYBRID_ENVELOPE_VERSION: &str = "2";
#[cfg(feature = "pq")]
const PQ_KEY_VERSION_PREFIX: &str = "pq-seed-v1";

/// Encrypted data blob with all metadata required for decryption.
///
/// This struct is `#[must_use]` — callers must not silently discard encrypted output.
///
/// # Examples
///
/// ```
/// # async fn example() -> Result<(), secure_data::error::DataError> {
/// use secure_data::envelope::{encrypt_for_storage, decrypt_for_use};
/// use secure_data::kms::StaticDevKeyProvider;
///
/// let provider = StaticDevKeyProvider::new();
/// let envelope = encrypt_for_storage(b"hello", "default", &provider).await?;
/// assert_eq!(envelope.version, "1");
///
/// let plain = decrypt_for_use(&envelope, &provider).await?;
/// assert_eq!(plain, b"hello");
/// # Ok(())
/// # }
/// ```
#[must_use]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvelopeEncrypted {
    /// Envelope format version.
    pub version: String,
    /// AEAD algorithm identifier.
    pub algorithm: String,
    /// Logical alias of the key-encryption key.
    pub key_alias: String,
    /// Version of the key-encryption key used.
    pub key_version: String,
    /// The data-encryption key, wrapped by the KEK (base64-encoded).
    pub wrapped_data_key: Vec<u8>,
    /// Random nonce used for this encryption (raw bytes).
    pub nonce: Vec<u8>,
    /// AEAD-authenticated ciphertext.
    pub ciphertext: Vec<u8>,
    /// Additional authenticated data bound to this ciphertext.
    pub aad: Vec<u8>,
    /// Hybrid PQ KEM combiner identifier.
    ///
    /// `None` for classical (v1) envelopes — every existing `Aes256Gcm` and
    /// `XChaCha20Poly1305` envelope serialized before pq-readiness M1 has
    /// this field absent on the wire (default-deserialized to `None`).
    ///
    /// `Some(0x01)` for hybrid X25519 + ML-KEM-768 / HKDF-SHA-256 envelopes
    /// produced by the pq-readiness M2 implementation. `Some(other)` is
    /// rejected by [`Self::validate_structure`] until a future combiner is
    /// explicitly added — fail-closed by design.
    ///
    /// See `docs/slo/design/pq-migration-plan.md` and [`crate::pq`] for the
    /// full table of combiner identifiers and the wire-format reasoning.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub combiner_id: Option<u8>,
}

impl EnvelopeEncrypted {
    /// Validates the envelope's structural invariants without performing
    /// any cryptographic operation. Returns
    /// [`DataError::EnvelopeMalformed`] if the metadata is internally
    /// inconsistent, or [`DataError::PqFeatureRequired`] if a hybrid PQ
    /// envelope was decoded on a build without `--features pq`, or
    /// [`DataError::AlgorithmRejectedByPolicy`] if the combiner identifier
    /// is reserved-future or the fail-closed sentinel.
    ///
    /// Called from [`decrypt_for_use`] before any AEAD work — fails fast
    /// when an attacker has tampered with envelope metadata in a way that
    /// would otherwise reach the cryptographic primitive.
    ///
    /// # Errors
    ///
    /// - [`DataError::EnvelopeMalformed`] — `combiner_id` is set on a
    ///   classical (v1) envelope, or contains an unrecognised value.
    /// - [`DataError::PqFeatureRequired`] — envelope's algorithm is the
    ///   hybrid PQ KEM but this build does not have the `pq` feature.
    /// - [`DataError::AlgorithmRejectedByPolicy`] — `combiner_id` is the
    ///   `0xFF` fail-closed sentinel.
    pub fn validate_structure(&self) -> Result<(), DataError> {
        let algorithm = crate::algorithm::CryptoAlgorithm::from_envelope_str(&self.algorithm)?;

        match (algorithm.is_post_quantum(), self.combiner_id) {
            // Classical envelope with no combiner: v1; nothing to do.
            (false, None) => Ok(()),
            // Classical envelope with combiner_id present and zero: silently
            // accept zero (some serialisers emit Some(0) instead of None).
            (false, Some(0)) => Ok(()),
            // Classical envelope with non-zero combiner: malformed.
            (false, Some(id)) => Err(DataError::EnvelopeMalformed {
                reason: format!(
                    "classical envelope (algorithm={}) carries combiner_id=0x{:02x}; \
                     classical envelopes must have combiner_id absent or zero",
                    self.algorithm, id
                ),
            }),
            // Post-quantum envelope but pq feature is off: refuse without
            // attempting any crypto.
            #[cfg(not(feature = "pq"))]
            (true, _) => Err(DataError::PqFeatureRequired),
            // Post-quantum envelope with pq feature on: validate combiner.
            #[cfg(feature = "pq")]
            (true, None) => Err(DataError::EnvelopeMalformed {
                reason: format!(
                    "post-quantum envelope (algorithm={}) is missing combiner_id; \
                     hybrid envelopes must carry an explicit combiner_id",
                    self.algorithm
                ),
            }),
            #[cfg(feature = "pq")]
            (true, Some(id)) => {
                if self.version != HYBRID_ENVELOPE_VERSION {
                    return Err(DataError::EnvelopeMalformed {
                        reason: format!(
                            "post-quantum envelope version must be {}, got {}",
                            HYBRID_ENVELOPE_VERSION, self.version
                        ),
                    });
                }
                if id == crate::pq::COMBINER_ID_FAIL_CLOSED {
                    return Err(DataError::AlgorithmRejectedByPolicy {
                        reason: format!(
                            "combiner_id=0x{:02x} is the permanent fail-closed sentinel",
                            id
                        ),
                    });
                }
                if !crate::pq::is_recognised_combiner(id) {
                    return Err(DataError::AlgorithmRejectedByPolicy {
                        reason: format!(
                            "combiner_id=0x{:02x} is not a recognised combiner in this build",
                            id
                        ),
                    });
                }
                Ok(())
            }
        }
    }
}

/// Encrypts `plaintext` under the key identified by `key_alias` using the provided `KeyProvider`.
///
/// Uses the default algorithm (AES-256-GCM) for backward compatibility.
/// For algorithm selection, use [`encrypt_with_policy`].
///
/// Returns an [`EnvelopeEncrypted`] blob containing all metadata required for decryption.
///
/// # Errors
/// Returns [`DataError`] if key generation or encryption fails.
pub async fn encrypt_for_storage<P: KeyProvider>(
    plaintext: &[u8],
    key_alias: &str,
    provider: &P,
) -> Result<EnvelopeEncrypted, DataError> {
    encrypt_with_policy(plaintext, key_alias, provider, &AlgorithmPolicy::default()).await
}

/// Encrypts `plaintext` using the algorithm specified by `policy`.
///
/// The algorithm tag is stored in the envelope so that [`decrypt_for_use`] can
/// select the correct primitive regardless of the current system default.
///
/// # Errors
///
/// Returns [`DataError::AlgorithmBelowPolicyMinimum`] if the preferred algorithm
/// is below the policy minimum. Returns [`DataError`] if key generation or
/// encryption fails.
///
/// # Examples
///
/// ```
/// # async fn example() -> Result<(), secure_data::error::DataError> {
/// use secure_data::algorithm::{AlgorithmPolicy, CryptoAlgorithm};
/// use secure_data::envelope::{encrypt_with_policy, decrypt_for_use};
/// use secure_data::kms::StaticDevKeyProvider;
///
/// let provider = StaticDevKeyProvider::new();
/// let policy = AlgorithmPolicy::prefer(CryptoAlgorithm::XChaCha20Poly1305);
///
/// let envelope = encrypt_with_policy(b"secret", "my-key", &provider, &policy).await?;
/// assert_eq!(envelope.algorithm, "XChaCha20-Poly1305");
///
/// let decrypted = decrypt_for_use(&envelope, &provider).await?;
/// assert_eq!(decrypted, b"secret");
/// # Ok(())
/// # }
/// ```
pub async fn encrypt_with_policy<P: KeyProvider>(
    plaintext: &[u8],
    key_alias: &str,
    provider: &P,
    policy: &AlgorithmPolicy,
) -> Result<EnvelopeEncrypted, DataError> {
    // Validate algorithm policy
    policy.validate()?;

    let algorithm = policy.preferred();

    // Post-quantum dispatch: the hybrid KEM is only available when the
    // optional `pq` dependencies are compiled in. Builds without the feature
    // fail fast with a structured error and never silently fall back to v1.
    if algorithm.is_post_quantum() {
        #[cfg(feature = "pq")]
        {
            return encrypt_hybrid(plaintext, key_alias, provider).await;
        }
        #[cfg(not(feature = "pq"))]
        {
            return Err(DataError::PqUnavailable);
        }
    }

    encrypt_classical(plaintext, key_alias, provider, algorithm).await
}

async fn encrypt_classical<P: KeyProvider>(
    plaintext: &[u8],
    key_alias: &str,
    provider: &P,
    algorithm: CryptoAlgorithm,
) -> Result<EnvelopeEncrypted, DataError> {
    if algorithm.is_post_quantum() {
        return Err(DataError::PqUnavailable);
    }

    // 1. Generate a fresh data-encryption key via the provider.
    let (dek, wrapped_data_key, key_version) = provider.generate_data_key(key_alias).await?;

    // 2. Generate a random nonce of the correct length for the algorithm.
    let nonce_len = algorithm.nonce_len();
    let mut nonce_bytes = vec![0u8; nonce_len];
    rand::rngs::OsRng.fill_bytes(&mut nonce_bytes);

    // 3. Build AAD from envelope metadata.
    let aad = build_aad(
        ENVELOPE_VERSION,
        algorithm.as_str(),
        key_alias,
        &key_version,
        None,
    );

    // 4. Encrypt with the selected algorithm.
    let ciphertext = encrypt_with_algorithm(algorithm, &dek, &nonce_bytes, plaintext, &aad)?;

    Ok(EnvelopeEncrypted {
        version: ENVELOPE_VERSION.to_string(),
        algorithm: algorithm.as_str().to_string(),
        key_alias: key_alias.to_string(),
        key_version,
        wrapped_data_key,
        nonce: nonce_bytes,
        ciphertext,
        aad,
        // Classical envelopes have no combiner. Hybrid v2 envelopes use the
        // separate `encrypt_hybrid` path.
        combiner_id: None,
    })
}

/// Decrypts an [`EnvelopeEncrypted`] blob using the provided `KeyProvider`.
///
/// The algorithm is selected based on the `algorithm` field in the envelope,
/// enabling transparent decryption of data encrypted with different algorithms.
///
/// # Errors
/// Returns [`DataError`] if the algorithm is unsupported, key unwrapping fails,
/// or AEAD verification fails.
pub async fn decrypt_for_use<P: KeyProvider>(
    envelope: &EnvelopeEncrypted,
    provider: &P,
) -> Result<Vec<u8>, DataError> {
    // Pre-flight: validate envelope structural invariants before any
    // cryptographic operation. Catches tampered metadata (e.g., a v1
    // envelope carrying a non-zero combiner_id) and rejects v2 hybrid
    // envelopes on a non-PQ build with a structured PqFeatureRequired error.
    envelope.validate_structure()?;

    // 0. Parse the algorithm from the envelope.
    let algorithm = CryptoAlgorithm::from_envelope_str(&envelope.algorithm)?;

    // Post-quantum dispatch: at this point `validate_structure` has already
    // returned PqFeatureRequired on a non-PQ build, so reaching this branch
    // with the feature enabled routes through the hybrid unwrap path.
    if algorithm.is_post_quantum() {
        #[cfg(feature = "pq")]
        {
            return decrypt_hybrid(envelope, provider).await;
        }
        #[cfg(not(feature = "pq"))]
        {
            return Err(DataError::PqFeatureRequired);
        }
    }

    // 1. Unwrap the data-encryption key.
    let dek = provider
        .unwrap_data_key(
            &envelope.wrapped_data_key,
            &envelope.key_alias,
            &envelope.key_version,
        )
        .await?;

    // 2. Validate nonce length for the algorithm.
    let expected_nonce_len = algorithm.nonce_len();
    if envelope.nonce.len() != expected_nonce_len {
        return Err(DataError::InvalidNonce {
            expected: expected_nonce_len,
            actual: envelope.nonce.len(),
        });
    }

    // 3. Recompute AAD from envelope header fields to detect metadata tampering.
    //    If an attacker modifies key_version, algorithm, or key_alias without
    //    updating the authenticated AAD, AEAD verification will fail.
    let recomputed_aad = build_aad(
        &envelope.version,
        &envelope.algorithm,
        &envelope.key_alias,
        &envelope.key_version,
        normalise_classical_combiner(envelope.combiner_id),
    );

    // 4. Decrypt with the correct algorithm using the original AAD bound during encryption.
    //    We use the stored AAD (which was authenticated) — if metadata fields were tampered
    //    post-encryption, the recomputed AAD won't match the stored one.
    if recomputed_aad != envelope.aad {
        return Err(DataError::AuthenticationFailure);
    }

    decrypt_with_algorithm(
        algorithm,
        &dek,
        &envelope.nonce,
        &envelope.ciphertext,
        &envelope.aad,
    )
}

#[cfg(feature = "pq")]
async fn encrypt_hybrid<P: KeyProvider>(
    plaintext: &[u8],
    key_alias: &str,
    provider: &P,
) -> Result<EnvelopeEncrypted, DataError> {
    let (recipient_seed, wrapped_recipient_seed, provider_key_version) =
        provider.generate_data_key(key_alias).await?;
    let recipient = derive_hybrid_recipient_material(&recipient_seed)?;

    let mut dek = Zeroizing::new(vec![0u8; 32]);
    OsRng.fill_bytes(&mut dek);

    let mut nonce_bytes = vec![0u8; CryptoAlgorithm::HybridX25519MlKem768.nonce_len()];
    OsRng.fill_bytes(&mut nonce_bytes);

    let key_version = encode_hybrid_key_version(&provider_key_version, &wrapped_recipient_seed);
    let combiner_id = crate::pq::COMBINER_ID_X25519_ML_KEM_768;
    let aad = build_aad(
        HYBRID_ENVELOPE_VERSION,
        CryptoAlgorithm::HybridX25519MlKem768.as_str(),
        key_alias,
        &key_version,
        Some(combiner_id),
    );

    let encapsulation =
        crate::pq::hybrid_encapsulate(&recipient.ml_kem_public_key, &recipient.x25519_public_key)?;
    let wrapped_dek =
        wrap_data_key_with_hybrid(&encapsulation.derived_key, &nonce_bytes, &dek, &aad)?;

    let mut wrapped_data_key = Vec::with_capacity(
        encapsulation.kem_ciphertext.len() + encapsulation.x25519_share.len() + wrapped_dek.len(),
    );
    wrapped_data_key.extend_from_slice(&encapsulation.kem_ciphertext);
    wrapped_data_key.extend_from_slice(&encapsulation.x25519_share);
    wrapped_data_key.extend_from_slice(&wrapped_dek);

    let ciphertext = encrypt_with_algorithm(
        CryptoAlgorithm::Aes256Gcm,
        &dek,
        &nonce_bytes,
        plaintext,
        &aad,
    )?;

    Ok(EnvelopeEncrypted {
        version: HYBRID_ENVELOPE_VERSION.to_string(),
        algorithm: CryptoAlgorithm::HybridX25519MlKem768.as_str().to_string(),
        key_alias: key_alias.to_string(),
        key_version,
        wrapped_data_key,
        nonce: nonce_bytes,
        ciphertext,
        aad,
        combiner_id: Some(combiner_id),
    })
}

#[cfg(feature = "pq")]
async fn decrypt_hybrid<P: KeyProvider>(
    envelope: &EnvelopeEncrypted,
    provider: &P,
) -> Result<Vec<u8>, DataError> {
    let expected_nonce_len = CryptoAlgorithm::HybridX25519MlKem768.nonce_len();
    if envelope.nonce.len() != expected_nonce_len {
        return Err(DataError::InvalidNonce {
            expected: expected_nonce_len,
            actual: envelope.nonce.len(),
        });
    }

    let combiner_id = envelope
        .combiner_id
        .ok_or_else(|| DataError::EnvelopeMalformed {
            reason: "post-quantum envelope missing combiner_id".to_string(),
        })?;
    let recomputed_aad = build_aad(
        &envelope.version,
        &envelope.algorithm,
        &envelope.key_alias,
        &envelope.key_version,
        Some(combiner_id),
    );
    if recomputed_aad != envelope.aad {
        return Err(DataError::AuthenticationFailure);
    }

    let (provider_key_version, wrapped_recipient_seed) =
        decode_hybrid_key_version(&envelope.key_version)?;
    let recipient_seed = provider
        .unwrap_data_key(
            &wrapped_recipient_seed,
            &envelope.key_alias,
            &provider_key_version,
        )
        .await?;
    let recipient = derive_hybrid_recipient_material(&recipient_seed)?;

    let parts = split_hybrid_wrapped_data_key(&envelope.wrapped_data_key)?;
    let derived_key = crate::pq::hybrid_decapsulate(
        parts.kem_ciphertext,
        parts.x25519_share,
        &recipient.ml_kem_secret_seed,
        &recipient.x25519_secret_key,
    )?;
    let dek = unwrap_data_key_with_hybrid(
        &derived_key,
        &envelope.nonce,
        parts.wrapped_dek,
        &envelope.aad,
    )?;

    decrypt_with_algorithm(
        CryptoAlgorithm::Aes256Gcm,
        &dek,
        &envelope.nonce,
        &envelope.ciphertext,
        &envelope.aad,
    )
}

// --- Helpers ----------------------------------------------------------------

fn aes_cipher_from_dek(dek: &[u8]) -> Result<Aes256Gcm, DataError> {
    if dek.len() != 32 {
        return Err(DataError::WrappedKeyLengthMismatch);
    }
    let key = Key::<Aes256Gcm>::from_slice(dek);
    Ok(Aes256Gcm::new(key))
}

fn xchacha_cipher_from_dek(dek: &[u8]) -> Result<XChaCha20Poly1305, DataError> {
    if dek.len() != 32 {
        return Err(DataError::WrappedKeyLengthMismatch);
    }
    let key = chacha20poly1305::Key::from_slice(dek);
    Ok(XChaCha20Poly1305::new(key))
}

/// Decrypts an [`EnvelopeEncrypted`] under an explicit [`AlgorithmPolicy`].
///
/// Identical to [`decrypt_for_use`] except the policy is checked against
/// the envelope's wire-format version *before* any cryptographic
/// operation. When the envelope's version is below the configured
/// `min_envelope_version`, returns
/// [`DataError::AlgorithmRejectedByPolicy`] — the downgrade-attack
/// defence (`tm-pqd-abuse-6`).
///
/// # Errors
///
/// - [`DataError::AlgorithmRejectedByPolicy`] when the envelope's
///   version is below the configured `min_envelope_version`.
/// - All errors that `decrypt_for_use` returns (envelope-malformed,
///   pq-feature-required, AEAD authentication failure, etc.).
///
/// # Examples
///
/// ```
/// # async fn example() -> Result<(), secure_data::error::DataError> {
/// use secure_data::algorithm::{AlgorithmPolicy, CryptoAlgorithm};
/// use secure_data::envelope::{decrypt_with_policy, encrypt_for_storage};
/// use secure_data::kms::StaticDevKeyProvider;
///
/// let provider = StaticDevKeyProvider::new();
/// let envelope = encrypt_for_storage(b"hello", "default", &provider).await?;
///
/// // Default policy — accepts every envelope version.
/// let lax = AlgorithmPolicy::default();
/// let plain = decrypt_with_policy(&envelope, &provider, &lax).await?;
/// assert_eq!(plain, b"hello");
///
/// // Strict policy — rejects v1 envelopes.
/// let strict = AlgorithmPolicy::prefer(CryptoAlgorithm::HybridX25519MlKem768)
///     .with_min_envelope_version(2);
/// let result = decrypt_with_policy(&envelope, &provider, &strict).await;
/// assert!(matches!(
///     result,
///     Err(secure_data::error::DataError::AlgorithmRejectedByPolicy { .. })
/// ));
/// # Ok(())
/// # }
/// ```
pub async fn decrypt_with_policy<P: KeyProvider>(
    envelope: &EnvelopeEncrypted,
    provider: &P,
    policy: &AlgorithmPolicy,
) -> Result<Vec<u8>, DataError> {
    // Pre-flight: validate the envelope's wire-format version against
    // the policy *before* any AEAD work. Catches downgrade attacks
    // (`tm-pqd-abuse-6`) at the structural boundary.
    policy.validate_envelope_version(&envelope.version)?;

    decrypt_for_use(envelope, provider).await
}

/// Encrypts using the specified algorithm.
fn encrypt_with_algorithm(
    algorithm: CryptoAlgorithm,
    dek: &[u8],
    nonce_bytes: &[u8],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>, DataError> {
    match algorithm {
        CryptoAlgorithm::Aes256Gcm => {
            let cipher = aes_cipher_from_dek(dek)?;
            let nonce = Nonce::from_slice(nonce_bytes);
            let payload = Payload {
                msg: plaintext,
                aad,
            };
            cipher
                .encrypt(nonce, payload)
                .map_err(|_| DataError::EncryptionFailed {
                    reason: "AES-256-GCM encryption failed".to_string(),
                })
        }
        CryptoAlgorithm::XChaCha20Poly1305 => {
            let cipher = xchacha_cipher_from_dek(dek)?;
            let nonce = chacha20poly1305::XNonce::from_slice(nonce_bytes);
            let payload = Payload {
                msg: plaintext,
                aad,
            };
            cipher
                .encrypt(nonce, payload)
                .map_err(|_| DataError::EncryptionFailed {
                    reason: "XChaCha20-Poly1305 encryption failed".to_string(),
                })
        }
        // Defense-in-depth: hybrid PQ should be filtered out by the caller
        // (`encrypt_with_policy` / `decrypt_for_use`) via `is_post_quantum()`
        // before reaching this helper. Reaching here on any build means a
        // future caller forgot the dispatch — fail closed with a structured
        // error rather than panic.
        CryptoAlgorithm::HybridX25519MlKem768 => Err(DataError::PqUnavailable),
    }
}

/// Decrypts using the specified algorithm.
fn decrypt_with_algorithm(
    algorithm: CryptoAlgorithm,
    dek: &[u8],
    nonce_bytes: &[u8],
    ciphertext: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>, DataError> {
    match algorithm {
        CryptoAlgorithm::Aes256Gcm => {
            let cipher = aes_cipher_from_dek(dek)?;
            let nonce = Nonce::from_slice(nonce_bytes);
            let payload = Payload {
                msg: ciphertext,
                aad,
            };
            cipher
                .decrypt(nonce, payload)
                .map_err(|_| DataError::AuthenticationFailure)
        }
        CryptoAlgorithm::XChaCha20Poly1305 => {
            let cipher = xchacha_cipher_from_dek(dek)?;
            let nonce = chacha20poly1305::XNonce::from_slice(nonce_bytes);
            let payload = Payload {
                msg: ciphertext,
                aad,
            };
            cipher
                .decrypt(nonce, payload)
                .map_err(|_| DataError::AuthenticationFailure)
        }
        // Defense-in-depth: hybrid PQ should be filtered out by the caller
        // before reaching this helper. See the encrypt-side comment.
        CryptoAlgorithm::HybridX25519MlKem768 => Err(DataError::PqUnavailable),
    }
}

#[cfg(feature = "pq")]
struct HybridRecipientMaterial {
    ml_kem_public_key: Vec<u8>,
    ml_kem_secret_seed: [u8; 64],
    x25519_public_key: [u8; 32],
    x25519_secret_key: [u8; 32],
}

#[cfg(feature = "pq")]
struct HybridWrappedDataKeyParts<'a> {
    kem_ciphertext: &'a [u8],
    x25519_share: &'a [u8],
    wrapped_dek: &'a [u8],
}

#[cfg(feature = "pq")]
fn derive_hybrid_recipient_material(
    seed_material: &[u8],
) -> Result<HybridRecipientMaterial, DataError> {
    if seed_material.is_empty() {
        return Err(DataError::EnvelopeMalformed {
            reason: "provider returned empty hybrid recipient seed".to_string(),
        });
    }

    let hk = Hkdf::<Sha256>::new(None, seed_material);
    let mut material = [0u8; 96];
    hk.expand(b"sunlit-pq-recipient-key-material/v1", &mut material)
        .map_err(|_| DataError::EncryptionFailed {
            reason: "HKDF-SHA-256 recipient key derivation failed".to_string(),
        })?;

    let (ml_kem_public_key, ml_kem_secret_seed) =
        crate::pq::kem::ml_kem_keypair_from_seed(&material[..64])?;
    let (x25519_secret_key, x25519_public_key) =
        crate::pq::kem::x25519_keypair_from_seed(&material[64..])?;

    Ok(HybridRecipientMaterial {
        ml_kem_public_key,
        ml_kem_secret_seed,
        x25519_public_key,
        x25519_secret_key,
    })
}

#[cfg(feature = "pq")]
fn encode_hybrid_key_version(provider_key_version: &str, wrapped_recipient_seed: &[u8]) -> String {
    format!(
        "{}:{}:{}",
        PQ_KEY_VERSION_PREFIX,
        B64.encode(provider_key_version.as_bytes()),
        B64.encode(wrapped_recipient_seed)
    )
}

#[cfg(feature = "pq")]
fn decode_hybrid_key_version(key_version: &str) -> Result<(String, Vec<u8>), DataError> {
    let mut parts = key_version.splitn(3, ':');
    let prefix = parts.next().ok_or_else(|| DataError::EnvelopeMalformed {
        reason: "hybrid key_version missing prefix".to_string(),
    })?;
    if prefix != PQ_KEY_VERSION_PREFIX {
        return Err(DataError::EnvelopeMalformed {
            reason: "hybrid key_version has unsupported prefix".to_string(),
        });
    }

    let provider_version_b64 = parts.next().ok_or_else(|| DataError::EnvelopeMalformed {
        reason: "hybrid key_version missing provider version".to_string(),
    })?;
    let wrapped_seed_b64 = parts.next().ok_or_else(|| DataError::EnvelopeMalformed {
        reason: "hybrid key_version missing wrapped recipient seed".to_string(),
    })?;

    let provider_version =
        B64.decode(provider_version_b64)
            .map_err(|_| DataError::EnvelopeMalformed {
                reason: "hybrid provider key version is not valid base64".to_string(),
            })?;
    let provider_version =
        String::from_utf8(provider_version).map_err(|_| DataError::EnvelopeMalformed {
            reason: "hybrid provider key version is not UTF-8".to_string(),
        })?;
    let wrapped_seed = B64
        .decode(wrapped_seed_b64)
        .map_err(|_| DataError::EnvelopeMalformed {
            reason: "hybrid wrapped recipient seed is not valid base64".to_string(),
        })?;

    Ok((provider_version, wrapped_seed))
}

#[cfg(feature = "pq")]
fn split_hybrid_wrapped_data_key(
    wrapped: &[u8],
) -> Result<HybridWrappedDataKeyParts<'_>, DataError> {
    let kem_len = crate::pq::sizes::ML_KEM_768_CIPHERTEXT_LEN;
    let x25519_len = crate::pq::sizes::X25519_SHARE_LEN;
    let min_len = kem_len + x25519_len + 16;
    if wrapped.len() < min_len {
        return Err(DataError::EnvelopeMalformed {
            reason: format!(
                "hybrid wrapped_data_key too short: expected at least {}, got {}",
                min_len,
                wrapped.len()
            ),
        });
    }

    let (kem_ciphertext, rest) = wrapped.split_at(kem_len);
    let (x25519_share, wrapped_dek) = rest.split_at(x25519_len);
    Ok(HybridWrappedDataKeyParts {
        kem_ciphertext,
        x25519_share,
        wrapped_dek,
    })
}

#[cfg(feature = "pq")]
fn wrap_data_key_with_hybrid(
    wrap_key: &[u8; 32],
    nonce_bytes: &[u8],
    dek: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>, DataError> {
    let cipher = aes_cipher_from_dek(wrap_key)?;
    let nonce = Nonce::from_slice(nonce_bytes);
    let payload = Payload { msg: dek, aad };
    cipher
        .encrypt(nonce, payload)
        .map_err(|_| DataError::EncryptionFailed {
            reason: "hybrid AES-256-GCM data-key wrap failed".to_string(),
        })
}

#[cfg(feature = "pq")]
fn unwrap_data_key_with_hybrid(
    wrap_key: &[u8; 32],
    nonce_bytes: &[u8],
    wrapped_dek: &[u8],
    aad: &[u8],
) -> Result<Zeroizing<Vec<u8>>, DataError> {
    let cipher = aes_cipher_from_dek(wrap_key)?;
    let nonce = Nonce::from_slice(nonce_bytes);
    let payload = Payload {
        msg: wrapped_dek,
        aad,
    };
    let dek = cipher
        .decrypt(nonce, payload)
        .map_err(|_| DataError::AuthenticationFailure)?;
    if dek.len() != 32 {
        return Err(DataError::WrappedKeyLengthMismatch);
    }
    Ok(Zeroizing::new(dek))
}

/// Builds deterministic AAD bytes from envelope header fields.
fn build_aad(
    version: &str,
    algorithm: &str,
    key_alias: &str,
    key_version: &str,
    combiner_id: Option<u8>,
) -> Vec<u8> {
    let mut aad =
        format!("v={version};alg={algorithm};alias={key_alias};kver={key_version}").into_bytes();
    if let Some(id) = combiner_id {
        #[cfg(feature = "pq")]
        crate::pq::combiner::bind_combiner_id_to_aad(&mut aad, id);
        #[cfg(not(feature = "pq"))]
        aad.extend_from_slice(format!(";combiner=0x{id:02x}").as_bytes());
    }
    aad
}

fn normalise_classical_combiner(combiner_id: Option<u8>) -> Option<u8> {
    match combiner_id {
        Some(0) | None => None,
        Some(id) => Some(id),
    }
}