lib-q-hpke 0.0.4

HPKE implementation for lib-q
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
//! # lib-Q HPKE - Hybrid Public Key Encryption
//!
//! RFC 9180–aligned HPKE for lib-q using **post-quantum-only** primitives (ML-KEM for `HpkeKem::*`
//! in the default internal provider path; Saturnin / SHAKE256 / optional duplex-sponge AEAD; SHAKE
//! / SHA3 HKDF backends). There is no classical KEM in this stack.
//!
//! ## Features
//!
//! - **RFC 9180–aligned** modes, key schedule, and labeled KDF; default PSK / AuthPSK encapsulated-key
//!   wire format is RFC 9180; optional [`HpkePskWireFormat::LibQCommitmentSuffix`]
//!   when both peers call [`HpkeContext::set_psk_wire_format`]
//! - **Post-quantum algorithms**: ML-KEM, Saturnin, SHAKE/SHA3; optional **duplex-sponge AEAD** via
//!   Cargo feature `duplex-sponge-aead` (umbrella `lib-q` crate: `hpke-duplex-aead`)
//! - **Provider integration**: [`HpkeContext`] holds a `lib_q_core::KemContext`
//!   from [`HpkeContext::with_provider`] (`Box<dyn CryptoProvider>`);
//!   HPKE protocol steps use [`PostQuantumProvider`]
//!   implementing [`crate::providers::KemProvider`], [`crate::providers::KdfProvider`],
//!   and [`crate::providers::AeadProvider`] (see also [`crate::providers::HpkeCryptoProvider`])
//! - **Comprehensive tests** (RFC 9180 suites, modes, integration)
//! - **Security helpers** (validation, constant-time utilities, secure buffers)
//! - **No-std** with `alloc` (required by this crate)
//!
//! ## Supported algorithms
//!
//! ### Key encapsulation (KEM)
//! - **ML-KEM-512**, **ML-KEM-768**, **ML-KEM-1024** — wire sizes per [`HpkeKem`]
//!
//! ### Key derivation (KDF)
//! - **HKDF-SHAKE128**, **HKDF-SHAKE256**, **HKDF-SHA3-256**, **HKDF-SHA3-512** — lengths per [`HpkeKdf`]
//!
//! ### Authenticated encryption (AEAD)
//! - **Saturnin-256** — 32-byte key, 16-byte nonce, 32-byte tag
//! - **SHAKE256** AEAD — per [`HpkeAead::tag_len`]
//! - **Duplex-sponge** (feature `duplex-sponge-aead`) — 32-byte tag via `lib-q-aead`
//! - **Export-only** — exporter secret usage without message AEAD
//!
//! ## Quick start
//!
//! ```rust
//! use lib_q_hpke::HpkeContext;
//!
//! // Default `KemContext` inside (no custom CryptoProvider).
//! let hpke_ctx = HpkeContext::new();
//!
//! // For ML-KEM key generation and `seal`/`open`, supply a `CryptoProvider` (e.g. `libq::LibQCryptoProvider`)
//! // via `HpkeContext::with_provider` and use `lib_q_core::KemContext` with the same provider for keypairs.
//! ```
//!
//! ## Architecture
//!
//! - **[`HpkeContext`]**: cipher suite ([`HpkeCipherSuite`]),
//!   PSK wire policy ([`HpkePskWireFormat`]), single-shot and multi-shot APIs
//! - **[`HpkeSenderContext`] / [`HpkeReceiverContext`]**:
//!   multi-message encrypt/decrypt and export
//! - **`hpke_core`**: RFC 9180 schedule, setup, seal/open
//! - **[`PostQuantumProvider`]**: ML-KEM via `lib-q-kem`,
//!   HKDF via `lib-q-hash`, AEAD via `lib-q-aead`
//!
//! Public `HpkeContext` methods return `lib_q_core::Result`; see `From<HpkeError>` for `lib_q_core::Error`
//! in this crate’s `error` module. More detail: `docs/API_REFERENCE.md` in the crate tree and
//! `docs/hpke-architecture.md` at the workspace root.
//!
//! ## Security considerations
//!
//! - Post-quantum primitives for the stated HPKE path; validate keys and suite agreement between peers
//! - Authenticated encryption for application payloads (except export-only suite)
//! - Constant-time helpers and zeroization where implemented; see `security` module and `docs/SECURITY_CONSIDERATIONS.md`
//!
//! ## Performance
//!
//! Throughput and latency depend on suite, platform, and features. Profile under your target configuration.
//!
//! ## Testing
//!
//! Run `cargo test -p lib-q-hpke` (with the feature set you ship). Compliance and mode coverage live under
//! `tests/` (e.g. `rfc9180_compliance_tests`, PSK/Auth/AuthPSK suites).

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code, unused_must_use, unstable_features)]
#![deny(
    trivial_casts,
    trivial_numeric_casts,
    missing_docs,
    unused_import_braces,
    unused_qualifications
)]

#[cfg(not(feature = "alloc"))]
compile_error!(
    "lib-q-hpke requires the 'alloc' feature to be enabled. This crate cannot function without alloc support."
);

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::{
    boxed::Box,
    sync::Arc,
    vec::Vec,
};

pub use error::*;
// Re-export HPKE types that users need
pub use types::*;

// Internal modules
pub mod error;
pub mod hpke_core;
pub mod hpke_session;
pub mod interop;
pub mod kdf;
pub mod providers;
pub mod types;

// New modular architecture
pub mod aead;
pub mod benchmarking;
pub mod kem;
pub mod protocol;
pub mod security;

pub use hpke_session::{
    HpkeReceiverContext,
    HpkeSenderContext,
};

#[cfg(feature = "wasm")]
mod wasm;

// Security tests module
#[cfg(test)]
mod security_tests;

// Imports used in conditionally compiled code
#[allow(unused_imports)]
use lib_q_core::{
    KemContext,
    KemPublicKey,
    KemSecretKey,
    Result,
};
#[allow(unused_imports)]
use providers::{
    post_quantum::PostQuantumProvider,
    traits::HpkeCryptoProvider,
};

use crate::security::{
    CryptoRng,
    EntropyCryptoRng,
};

/// HPKE Context that integrates with lib-q's provider pattern
///
/// The `HpkeContext` is the main interface for HPKE operations. It provides
/// both single-shot encryption/decryption and context-based operations for
/// encrypting multiple messages with the same key material.
///
/// Cryptography for encapsulation, KDF, and AEAD uses [`HpkeCryptoProvider`] (default:
/// [`PostQuantumProvider`]). [`HpkeContext::with_provider`] configures only the inner
/// [`KemContext`] for ML-KEM key validation and key-generation checks; use
/// [`HpkeContext::with_hpke_crypto`] to replace the HPKE crypto backend.
///
/// Randomness for setup and single-shot `seal` uses [`crate::security::EntropyCryptoRng`] by default
/// (OS-backed entropy via `lib-q-random`). Override with [`HpkeContext::set_rng`] for tests.
pub struct HpkeContext {
    kem_ctx: KemContext,
    cipher_suite: HpkeCipherSuite,
    /// PSK / AuthPSK encapsulated-key wire format (ignored for Base and Auth).
    psk_wire_format: HpkePskWireFormat,
    hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync>,
    rng: Box<dyn CryptoRng + Send>,
}

/// Create a KEM context for internal use
fn create_kem_context() -> KemContext {
    // Create a basic KEM context - this would normally be passed from the main library
    KemContext::new()
}

impl HpkeContext {
    /// Create a new HPKE context with default [`PostQuantumProvider`] and OS-backed RNG.
    pub fn new() -> Self {
        let hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync> =
            Arc::new(PostQuantumProvider::new());
        Self {
            kem_ctx: create_kem_context(),
            cipher_suite: HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            psk_wire_format: HpkePskWireFormat::default(),
            hpke_crypto,
            rng: Box::new(EntropyCryptoRng),
        }
    }

    /// Build an HPKE context with a custom [`HpkeCryptoProvider`] (for example tests or an alternate PQ backend).
    pub fn with_hpke_crypto(hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync>) -> Self {
        Self {
            kem_ctx: create_kem_context(),
            cipher_suite: HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            psk_wire_format: HpkePskWireFormat::default(),
            hpke_crypto,
            rng: Box::new(EntropyCryptoRng),
        }
    }

    /// Configure the inner [`KemContext`] with a [`lib_q_core::CryptoProvider`] for key validation and ML-KEM keygen.
    ///
    /// HPKE encapsulation/KDF/AEAD still use [`Self::hpke_crypto`] (default [`PostQuantumProvider`]).
    pub fn with_provider(provider: Box<dyn lib_q_core::CryptoProvider>) -> Self {
        let hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync> =
            Arc::new(PostQuantumProvider::new());
        Self {
            kem_ctx: KemContext::with_provider(provider),
            cipher_suite: HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            psk_wire_format: HpkePskWireFormat::default(),
            hpke_crypto,
            rng: Box::new(EntropyCryptoRng),
        }
    }

    /// Full wiring: custom [`KemContext`] provider and custom HPKE crypto backend.
    pub fn with_kem_and_hpke_crypto(
        kem_provider: Box<dyn lib_q_core::CryptoProvider>,
        hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync>,
    ) -> Self {
        Self {
            kem_ctx: KemContext::with_provider(kem_provider),
            cipher_suite: HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            psk_wire_format: HpkePskWireFormat::default(),
            hpke_crypto,
            rng: Box::new(EntropyCryptoRng),
        }
    }

    /// Replace the HPKE crypto backend (multi-shot `seal`/`open` on derived contexts use the snapshot from setup).
    pub fn set_hpke_crypto(&mut self, hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync>) {
        self.hpke_crypto = hpke_crypto;
    }

    /// Active HPKE crypto provider reference.
    pub fn hpke_crypto(&self) -> &(dyn HpkeCryptoProvider + Send + Sync) {
        self.hpke_crypto.as_ref()
    }

    /// Replace the RNG used for encapsulation and single-shot `seal` (tests may use [`crate::security::prng::SimpleRng`]).
    pub fn set_rng(&mut self, rng: Box<dyn CryptoRng + Send>) {
        self.rng = rng;
    }
}

impl Default for HpkeContext {
    fn default() -> Self {
        Self::new()
    }
}

impl HpkeContext {
    /// Active cipher suite (KEM, KDF, AEAD) used for subsequent HPKE operations.
    pub fn cipher_suite(&self) -> &HpkeCipherSuite {
        &self.cipher_suite
    }

    /// Set the cipher suite before `setup_sender` / `seal` / `open`.
    pub fn set_cipher_suite(&mut self, cipher_suite: HpkeCipherSuite) {
        self.cipher_suite = cipher_suite;
    }

    /// PSK-mode encapsulated key wire format used for subsequent PSK / AuthPSK operations.
    #[must_use]
    pub fn psk_wire_format(&self) -> HpkePskWireFormat {
        self.psk_wire_format
    }

    /// Set how PSK and AuthPSK modes encode the encapsulated key on the wire.
    ///
    /// **Default:** [`HpkePskWireFormat::Rfc9180`] (RFC 9180 on-the-wire layout for PSK / AuthPSK).
    ///
    /// **libQ extension:** set [`HpkePskWireFormat::LibQCommitmentSuffix`] when both peers support
    /// it to get early rejection of PSK / KEM mismatch before decapsulation. This is not RFC 9180
    /// wire format; do not use it when interoperating with a strict RFC 9180 implementation.
    pub fn set_psk_wire_format(&mut self, format: HpkePskWireFormat) {
        self.psk_wire_format = format;
    }

    /// Setup sender with recipient's public key
    pub fn setup_sender(
        &mut self,
        recipient_pk: &KemPublicKey,
        info: &[u8],
    ) -> Result<HpkeSenderContext> {
        hpke_core::setup_sender(
            &mut self.kem_ctx,
            recipient_pk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.rng.as_mut(),
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup receiver with encapsulated key and private key
    pub fn setup_receiver(
        &mut self,
        encapsulated_key: &[u8],
        recipient_sk: &KemSecretKey,
        info: &[u8],
    ) -> Result<HpkeReceiverContext> {
        hpke_core::setup_receiver(
            &mut self.kem_ctx,
            encapsulated_key,
            recipient_sk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup sender with PSK mode.
    ///
    /// Uses [`HpkeContext::psk_wire_format`]. With the default ([`HpkePskWireFormat::Rfc9180`]),
    /// [`HpkeSenderContext::encapsulated_key`] is exactly the KEM ciphertext (RFC 9180). With
    /// [`HpkePskWireFormat::LibQCommitmentSuffix`], the same ciphertext is followed by a PSK
    /// commitment (`hpke_core::psk_commitment_len` bytes); set via [`Self::set_psk_wire_format`].
    pub fn setup_sender_psk(
        &mut self,
        recipient_pk: &KemPublicKey,
        info: &[u8],
        psk: &[u8],
        psk_id: &[u8],
    ) -> Result<HpkeSenderContext> {
        hpke_core::setup_sender_with_mode(
            &mut self.kem_ctx,
            recipient_pk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.rng.as_mut(),
            HpkeMode::Psk,
            Some(psk),
            Some(psk_id),
            None,
            None,
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup sender with Auth mode
    pub fn setup_sender_auth(
        &mut self,
        recipient_pk: &KemPublicKey,
        info: &[u8],
        sender_sk: &KemSecretKey,
        sender_pk: &KemPublicKey,
    ) -> Result<HpkeSenderContext> {
        hpke_core::setup_sender_with_mode(
            &mut self.kem_ctx,
            recipient_pk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.rng.as_mut(),
            HpkeMode::Auth,
            None,
            None,
            Some(sender_sk),
            Some(sender_pk),
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup sender with AuthPSK mode.
    ///
    /// Wire layout follows [`HpkeContext::psk_wire_format`]: either RFC 9180 (KEM ‖ auth) or the
    /// same with a libQ PSK commitment suffix when using [`HpkePskWireFormat::LibQCommitmentSuffix`].
    pub fn setup_sender_auth_psk(
        &mut self,
        recipient_pk: &KemPublicKey,
        info: &[u8],
        psk: &[u8],
        psk_id: &[u8],
        sender_sk: &KemSecretKey,
        sender_pk: &KemPublicKey,
    ) -> Result<HpkeSenderContext> {
        hpke_core::setup_sender_with_mode(
            &mut self.kem_ctx,
            recipient_pk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.rng.as_mut(),
            HpkeMode::AuthPsk,
            Some(psk),
            Some(psk_id),
            Some(sender_sk),
            Some(sender_pk),
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup receiver with PSK mode.
    ///
    /// `encapsulated_key` must match the sender's [`HpkeContext::psk_wire_format`]. With the
    /// default [`HpkePskWireFormat::Rfc9180`], agreement is implicit (typically AEAD failure on
    /// mismatch). With [`HpkePskWireFormat::LibQCommitmentSuffix`], a wrong PSK, PSK ID, or primary
    /// KEM ciphertext relative to the commitment fails with [`HpkeError::InconsistentPsk`] before
    /// key schedule.
    pub fn setup_receiver_psk(
        &mut self,
        encapsulated_key: &[u8],
        recipient_sk: &KemSecretKey,
        info: &[u8],
        psk: &[u8],
        psk_id: &[u8],
    ) -> Result<HpkeReceiverContext> {
        hpke_core::setup_receiver_with_mode(
            &mut self.kem_ctx,
            encapsulated_key,
            recipient_sk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            HpkeMode::Psk,
            Some(psk),
            Some(psk_id),
            None,
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup receiver with Auth mode
    pub fn setup_receiver_auth(
        &mut self,
        encapsulated_key: &[u8],
        recipient_sk: &KemSecretKey,
        info: &[u8],
        sender_pk: &KemPublicKey,
    ) -> Result<HpkeReceiverContext> {
        hpke_core::setup_receiver_with_mode(
            &mut self.kem_ctx,
            encapsulated_key,
            recipient_sk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            HpkeMode::Auth,
            None,
            None,
            Some(sender_pk),
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Setup receiver with AuthPSK mode.
    ///
    /// `encapsulated_key` layout must match [`HpkeContext::psk_wire_format`] on both peers.
    pub fn setup_receiver_auth_psk(
        &mut self,
        encapsulated_key: &[u8],
        recipient_sk: &KemSecretKey,
        info: &[u8],
        psk: &[u8],
        psk_id: &[u8],
        sender_pk: &KemPublicKey,
    ) -> Result<HpkeReceiverContext> {
        hpke_core::setup_receiver_with_mode(
            &mut self.kem_ctx,
            encapsulated_key,
            recipient_sk,
            info,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            HpkeMode::AuthPsk,
            Some(psk),
            Some(psk_id),
            Some(sender_pk),
            self.psk_wire_format,
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }

    /// Single-shot encryption (seal)
    pub fn seal(
        &mut self,
        recipient_pk: &KemPublicKey,
        info: &[u8],
        aad: &[u8],
        plaintext: &[u8],
    ) -> Result<(Vec<u8>, Vec<u8>)> {
        hpke_core::seal(
            &mut self.kem_ctx,
            recipient_pk,
            info,
            aad,
            plaintext,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.rng.as_mut(),
        )
        .map_err(|e| e.into())
    }

    /// Single-shot decryption (open)
    pub fn open(
        &mut self,
        encapsulated_key: &[u8],
        recipient_sk: &KemSecretKey,
        info: &[u8],
        aad: &[u8],
        ciphertext: &[u8],
    ) -> Result<Vec<u8>> {
        hpke_core::open(
            &mut self.kem_ctx,
            encapsulated_key,
            recipient_sk,
            info,
            aad,
            ciphertext,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
            self.hpke_crypto.clone(),
        )
        .map_err(|e| e.into())
    }
}

/// Context for HPKE sender operations
impl HpkeSenderContext {
    /// Encrypt a message
    pub fn seal(&mut self, aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> {
        // Check if context can be used for encryption
        if !self.can_encrypt() {
            if self.sequence_number >= self.max_sequence_number {
                self.state = HpkeContextState::NeedsRekey;
            }
            return Err(lib_q_core::Error::InternalError {
                operation: "Context validation".into(),
                details: "Context cannot be used for encryption".into(),
            });
        }

        let ciphertext = hpke_core::seal_message(
            self.aead,
            self.key.as_slice(),
            self.nonce.as_slice(),
            self.sequence_number,
            aad,
            plaintext,
            self.hpke_crypto.as_ref(),
        )
        .map_err(lib_q_core::Error::from)?;

        // Increment sequence number with overflow protection
        self.increment_sequence().map_err(lib_q_core::Error::from)?;

        Ok(ciphertext)
    }

    /// Export key material
    pub fn export(&self, exporter_context: &[u8], length: usize) -> Result<Vec<u8>> {
        hpke_core::export(
            self.exporter_secret.as_slice(),
            exporter_context,
            length,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
        )
        .map_err(|e| e.into())
    }
}

/// Context for HPKE receiver operations
impl HpkeReceiverContext {
    /// Decrypt a message
    pub fn open(&mut self, aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> {
        // Check if context can be used for decryption
        if !self.can_decrypt() {
            return Err(lib_q_core::Error::InternalError {
                operation: "Context validation".into(),
                details: "Context cannot be used for decryption".into(),
            });
        }

        let plaintext = hpke_core::open_message(
            self.aead,
            self.key.as_slice(),
            self.nonce.as_slice(),
            self.sequence_number,
            aad,
            ciphertext,
            self.hpke_crypto.as_ref(),
        )
        .map_err(lib_q_core::Error::from)?;

        // Increment sequence number with overflow protection
        self.increment_sequence().map_err(lib_q_core::Error::from)?;

        Ok(plaintext)
    }

    /// Export key material
    pub fn export(&self, exporter_context: &[u8], length: usize) -> Result<Vec<u8>> {
        hpke_core::export(
            self.exporter_secret.as_slice(),
            exporter_context,
            length,
            &self.cipher_suite,
            self.hpke_crypto.as_ref(),
        )
        .map_err(|e| e.into())
    }
}

/// Create a new HPKE context with default configuration
pub fn create_hpke_context() -> HpkeContext {
    HpkeContext::new()
}

/// Convenience function to create HPKE context with specific provider
pub fn create_hpke_context_with_provider(
    provider: Box<dyn lib_q_core::CryptoProvider>,
) -> HpkeContext {
    HpkeContext::with_provider(provider)
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;
    use alloc::sync::Arc;
    use alloc::vec;

    use super::*;

    fn dummy_ml_kem_512_keys() -> (KemPublicKey, KemSecretKey) {
        (
            KemPublicKey::new(vec![1u8; HpkeKem::MlKem512.public_key_len()]),
            KemSecretKey::new(vec![2u8; HpkeKem::MlKem512.secret_key_len()]),
        )
    }

    #[test]
    fn context_constructors_and_cipher_suite_accessors() {
        let mut ctx = HpkeContext::new();
        assert_eq!(ctx.cipher_suite().kem, HpkeKem::MlKem512);
        assert_eq!(ctx.cipher_suite().kdf, HpkeKdf::HkdfShake256);
        assert_eq!(ctx.cipher_suite().aead, HpkeAead::Saturnin256);

        let suite =
            HpkeCipherSuite::new(HpkeKem::MlKem768, HpkeKdf::HkdfSha3_256, HpkeAead::Shake256);
        ctx.set_cipher_suite(suite);
        assert_eq!(ctx.cipher_suite().kem, suite.kem);
        assert_eq!(ctx.cipher_suite().kdf, suite.kdf);
        assert_eq!(ctx.cipher_suite().aead, suite.aead);

        let _default_ctx = HpkeContext::default();
        let _created_ctx = create_hpke_context();
    }

    #[test]
    fn hpke_setup_and_single_shot_paths_are_exercised() {
        let (recipient_pk, recipient_sk) = dummy_ml_kem_512_keys();
        let (sender_pk, sender_sk) = dummy_ml_kem_512_keys();

        let mut ctx = HpkeContext::new();
        let info = b"test-info";
        let psk = b"test-psk";
        let psk_id = b"test-psk-id";
        let aad = b"aad";
        let plaintext = b"plaintext";
        let fake_enc = vec![0u8; HpkeKem::MlKem512.enc_len()];

        let _ = ctx.setup_sender(&recipient_pk, info);
        let _ = ctx.setup_receiver(&fake_enc, &recipient_sk, info);

        let _ = ctx.setup_sender_psk(&recipient_pk, info, psk, psk_id);
        let _ = ctx.setup_receiver_psk(&fake_enc, &recipient_sk, info, psk, psk_id);

        let _ = ctx.setup_sender_auth(&recipient_pk, info, &sender_sk, &sender_pk);
        let _ = ctx.setup_receiver_auth(&fake_enc, &recipient_sk, info, &sender_pk);

        let _ = ctx.setup_sender_auth_psk(&recipient_pk, info, psk, psk_id, &sender_sk, &sender_pk);
        let _ =
            ctx.setup_receiver_auth_psk(&fake_enc, &recipient_sk, info, psk, psk_id, &sender_pk);

        let _ = ctx.seal(&recipient_pk, info, aad, plaintext);
        let _ = ctx.open(&fake_enc, &recipient_sk, info, aad, b"ciphertext");
    }

    #[test]
    fn sender_and_receiver_context_guards_are_exercised() {
        let hpke_crypto: Arc<dyn HpkeCryptoProvider + Send + Sync> =
            Arc::new(PostQuantumProvider::new());
        let mut sender = HpkeSenderContext::new(
            vec![3u8; 32].into(),
            vec![4u8; 32].into(),
            vec![5u8; 32].into(),
            vec![6u8; 16].into(),
            vec![7u8; HpkeKem::MlKem512.enc_len()],
            HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            HpkeAead::Saturnin256,
            hpke_crypto.clone(),
        );
        sender.state = HpkeContextState::Closed;
        let sender_err = sender.seal(b"aad", b"msg").unwrap_err().to_string();
        assert!(sender_err.contains("Context cannot be used for encryption"));

        let mut receiver = HpkeReceiverContext::new(
            vec![8u8; 32].into(),
            vec![9u8; 32].into(),
            vec![10u8; 32].into(),
            vec![11u8; 16].into(),
            HpkeCipherSuite::new(
                HpkeKem::MlKem512,
                HpkeKdf::HkdfShake256,
                HpkeAead::Saturnin256,
            ),
            HpkeAead::Saturnin256,
            hpke_crypto,
        );
        receiver.state = HpkeContextState::Closed;
        let receiver_err = receiver.open(b"aad", b"ct").unwrap_err().to_string();
        assert!(receiver_err.contains("Context cannot be used for decryption"));
    }
}