hpke-ng 0.1.0-rc.3

Clean, fast, RFC 9180 HPKE implementation.
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
//! `hpke-ng` — RFC 9180 HPKE implementation.
//!
//! ## Example
//!
//! ```
//! use hpke_ng::*;
//! use rand_core::{OsRng, TryRngCore as _};
//!
//! type Suite = Hpke<DhKemX25519HkdfSha256, HkdfSha256, ChaCha20Poly1305>;
//!
//! let mut os = OsRng;
//! let mut rng = os.unwrap_mut();
//! let (sk_r, pk_r) = DhKemX25519HkdfSha256::generate(&mut rng).unwrap();
//! let (enc, ct) =
//!     Suite::seal_base(&mut rng, &pk_r, b"info", b"aad", b"hello").unwrap();
//! let pt = Suite::open_base(&enc, &sk_r, b"info", b"aad", &ct).unwrap();
//! assert_eq!(pt, b"hello");
//! ```
//!
//! See the [Readme](https://github.com/symbolicsoft/hpke-ng) for design notes
//! and the constant-time disclosure table.

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code, unstable_features)]
#![deny(
	missing_docs,
	rustdoc::broken_intra_doc_links,
	rustdoc::private_intra_doc_links,
	trivial_casts,
	trivial_numeric_casts,
	unused_must_use,
	unused_import_braces,
	unused_qualifications,
	clippy::pedantic
)]
#![allow(
	clippy::module_name_repetitions,
	clippy::missing_errors_doc,
	clippy::type_complexity,
	unused_extern_crates
)]

extern crate alloc;

mod aead;
mod error;
mod kdf;
mod sealed;

pub mod kem;

pub use aead::{Aead, Aes128Gcm, Aes256Gcm, ChaCha20Poly1305, ExportOnly, SealingAead};
pub use error::HpkeError;
pub use kdf::{HkdfSha256, HkdfSha384, HkdfSha512, Kdf};
pub use kem::{
	AuthKem, Kem,
	dh::{
		DhKemK256HkdfSha256, DhKemP256HkdfSha256, DhKemP384HkdfSha384, DhKemP521HkdfSha512,
		DhKemX448HkdfSha512, DhKemX25519HkdfSha256,
	},
};

#[cfg(feature = "pq")]
pub use kem::pq::{MlKem768, MlKem1024, XWingDraft06};

mod context;

pub use context::Context;

use alloc::vec::Vec;
use core::marker::PhantomData;

use zeroize::Zeroizing;

use crate::kdf::{labeled_expand_pieces, labeled_extract};

/// HPKE configuration parameterized over a KEM, KDF, and AEAD.
///
/// `Hpke` is a zero-sized type. All operations are associated functions; there
/// is no instance state and no PRNG owned by the configuration.
///
/// # Example
///
/// ```no_run
/// use hpke_ng::*;
/// use rand_core::{OsRng, TryRngCore as _};
///
/// type Suite = Hpke<DhKemX25519HkdfSha256, HkdfSha256, ChaCha20Poly1305>;
///
/// let mut os = OsRng;
/// let mut rng = os.unwrap_mut();
/// let (sk_r, pk_r) = DhKemX25519HkdfSha256::generate(&mut rng).unwrap();
/// let (enc, ct) =
///     Suite::seal_base(&mut rng, &pk_r, b"info", b"aad", b"hello").unwrap();
/// let pt = Suite::open_base(&enc, &sk_r, b"info", b"aad", &ct).unwrap();
/// assert_eq!(pt, b"hello");
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Hpke<K: Kem, F: Kdf, A: Aead>(PhantomData<(K, F, A)>);

pub(crate) mod modes {
	pub const BASE: u8 = 0x00;
	pub const PSK: u8 = 0x01;
	pub const AUTH: u8 = 0x02;
	pub const AUTH_PSK: u8 = 0x03;
}

#[inline]
pub(crate) fn ciphersuite<K: Kem, F: Kdf, A: Aead>() -> [u8; 10] {
	let mut s = [0u8; 10];
	s[..4].copy_from_slice(b"HPKE");
	s[4..6].copy_from_slice(&K::ID.to_be_bytes());
	s[6..8].copy_from_slice(&F::ID.to_be_bytes());
	s[8..10].copy_from_slice(&A::ID.to_be_bytes());
	s
}

#[inline]
fn verify_psk_inputs(mode: u8, psk: &[u8], psk_id: &[u8]) -> Result<(), HpkeError> {
	let got_psk = !psk.is_empty();
	let got_psk_id = !psk_id.is_empty();
	if got_psk != got_psk_id {
		return Err(HpkeError::InconsistentPsk);
	}
	if got_psk && (mode == modes::BASE || mode == modes::AUTH) {
		return Err(HpkeError::UnnecessaryPsk);
	}
	if !got_psk && (mode == modes::PSK || mode == modes::AUTH_PSK) {
		return Err(HpkeError::MissingPsk);
	}
	if got_psk && psk.len() < 32 {
		return Err(HpkeError::InsecurePsk);
	}
	Ok(())
}

#[cfg(not(feature = "kat-internals"))]
pub(crate) fn key_schedule<K: Kem, F: Kdf, A: Aead>(
	mode: u8,
	shared_secret: &[u8],
	info: &[u8],
	psk: &[u8],
	psk_id: &[u8],
) -> Result<Context<K, F, A>, HpkeError> {
	key_schedule_inner::<K, F, A>(mode, shared_secret, info, psk, psk_id)
}

#[cfg(feature = "kat-internals")]
#[doc(hidden)]
pub fn key_schedule<K: Kem, F: Kdf, A: Aead>(
	mode: u8,
	shared_secret: &[u8],
	info: &[u8],
	psk: &[u8],
	psk_id: &[u8],
) -> Result<Context<K, F, A>, HpkeError> {
	key_schedule_inner::<K, F, A>(mode, shared_secret, info, psk, psk_id)
}

fn key_schedule_inner<K: Kem, F: Kdf, A: Aead>(
	mode: u8,
	shared_secret: &[u8],
	info: &[u8],
	psk: &[u8],
	psk_id: &[u8],
) -> Result<Context<K, F, A>, HpkeError> {
	verify_psk_inputs(mode, psk, psk_id)?;
	let suite = ciphersuite::<K, F, A>();
	let psk_id_hash = labeled_extract::<F>(&[], &suite, b"psk_id_hash", psk_id);
	let info_hash = labeled_extract::<F>(&[], &suite, b"info_hash", info);

	let mode_arr = [mode];
	// `ks_context = mode || psk_id_hash || info_hash`. Fed piecewise into
	// each `expand_multi_info` call instead of allocating a flat `Vec`.
	let ks_pieces: [&[u8]; 3] = [&mode_arr, &psk_id_hash, &info_hash];

	let secret = Zeroizing::new(labeled_extract::<F>(shared_secret, &suite, b"secret", psk));
	let key = labeled_expand_pieces::<F>(&secret, &suite, b"key", &ks_pieces, A::KEY_LEN)?;
	let base_nonce =
		labeled_expand_pieces::<F>(&secret, &suite, b"base_nonce", &ks_pieces, A::NONCE_LEN)?;
	let exporter_secret =
		labeled_expand_pieces::<F>(&secret, &suite, b"exp", &ks_pieces, F::HASH_LEN)?;

	Context::new(key, base_nonce, exporter_secret)
}

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

	#[test]
	fn psk_validation_matrix() {
		use HpkeError::*;
		let cases: &[(u8, &[u8], &[u8], Result<(), HpkeError>)] = &[
			(modes::PSK, b"", b"some_id", Err(InconsistentPsk)),
			(modes::PSK, &[0u8; 32], b"", Err(InconsistentPsk)),
			(modes::PSK, b"", b"", Err(MissingPsk)),
			(modes::BASE, &[0u8; 32], b"id", Err(UnnecessaryPsk)),
			(modes::PSK, b"too short", b"id", Err(InsecurePsk)),
			(modes::BASE, b"", b"", Ok(())),
			(modes::PSK, &[0u8; 32], b"id", Ok(())),
		];
		for (mode, psk, psk_id, expected) in cases {
			assert_eq!(verify_psk_inputs(*mode, psk, psk_id), *expected);
		}
	}
}

use rand_core::{CryptoRng, RngCore};

impl<K: Kem, F: Kdf, A: Aead> Hpke<K, F, A> {
	/// `SetupBaseS` (RFC 9180 §5.1.1).
	pub fn setup_sender_base<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
	) -> Result<(K::EncappedKey, Context<K, F, A>), HpkeError> {
		let (ss, enc) = K::encap(rng, pk_r)?;
		let ctx = key_schedule::<K, F, A>(modes::BASE, ss.as_ref(), info, &[], &[])?;
		Ok((enc, ctx))
	}

	/// `SetupBaseR` (RFC 9180 §5.1.1).
	pub fn setup_receiver_base(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
	) -> Result<Context<K, F, A>, HpkeError> {
		let ss = K::decap(enc, sk_r)?;
		key_schedule::<K, F, A>(modes::BASE, ss.as_ref(), info, &[], &[])
	}

	/// `SetupPSKS` (RFC 9180 §5.1.2).
	///
	/// `psk` MUST be at least 32 bytes of high-entropy random data. Length is
	/// enforced; entropy is the caller's responsibility — see
	/// [`HpkeError::InsecurePsk`].
	pub fn setup_sender_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
	) -> Result<(K::EncappedKey, Context<K, F, A>), HpkeError> {
		let (ss, enc) = K::encap(rng, pk_r)?;
		let ctx = key_schedule::<K, F, A>(modes::PSK, ss.as_ref(), info, psk, psk_id)?;
		Ok((enc, ctx))
	}

	/// `SetupPSKR` (RFC 9180 §5.1.2).
	///
	/// `psk` MUST be at least 32 bytes of high-entropy random data. Length is
	/// enforced; entropy is the caller's responsibility — see
	/// [`HpkeError::InsecurePsk`].
	pub fn setup_receiver_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
	) -> Result<Context<K, F, A>, HpkeError> {
		let ss = K::decap(enc, sk_r)?;
		key_schedule::<K, F, A>(modes::PSK, ss.as_ref(), info, psk, psk_id)
	}
}

impl<K: Kem, F: Kdf, A: SealingAead> Hpke<K, F, A> {
	/// Single-shot Base-mode encrypt (RFC 9180 §6.1).
	pub fn seal_base<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		aad: &[u8],
		pt: &[u8],
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, mut ctx) = Self::setup_sender_base(rng, pk_r, info)?;
		let ct = ctx.seal(aad, pt)?;
		Ok((enc, ct))
	}

	/// Single-shot Base-mode decrypt (RFC 9180 §6.1).
	pub fn open_base(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		aad: &[u8],
		ct: &[u8],
	) -> Result<Vec<u8>, HpkeError> {
		let mut ctx = Self::setup_receiver_base(enc, sk_r, info)?;
		ctx.open(aad, ct)
	}

	/// Single-shot Psk-mode encrypt (RFC 9180 §6.1).
	#[allow(clippy::too_many_arguments)]
	pub fn seal_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		aad: &[u8],
		pt: &[u8],
		psk: &[u8],
		psk_id: &[u8],
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, mut ctx) = Self::setup_sender_psk(rng, pk_r, info, psk, psk_id)?;
		let ct = ctx.seal(aad, pt)?;
		Ok((enc, ct))
	}

	/// Single-shot Psk-mode decrypt (RFC 9180 §6.1).
	#[allow(clippy::too_many_arguments)]
	pub fn open_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		aad: &[u8],
		ct: &[u8],
		psk: &[u8],
		psk_id: &[u8],
	) -> Result<Vec<u8>, HpkeError> {
		let mut ctx = Self::setup_receiver_psk(enc, sk_r, info, psk, psk_id)?;
		ctx.open(aad, ct)
	}
}

impl<K: AuthKem, F: Kdf, A: SealingAead> Hpke<K, F, A> {
	/// Single-shot Auth-mode encrypt (RFC 9180 §6.1).
	pub fn seal_auth<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		aad: &[u8],
		pt: &[u8],
		sk_s: &K::PrivateKey,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, mut ctx) = Self::setup_sender_auth(rng, pk_r, info, sk_s)?;
		let ct = ctx.seal(aad, pt)?;
		Ok((enc, ct))
	}

	/// Single-shot Auth-mode decrypt (RFC 9180 §6.1).
	pub fn open_auth(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		aad: &[u8],
		ct: &[u8],
		pk_s: &K::PublicKey,
	) -> Result<Vec<u8>, HpkeError> {
		let mut ctx = Self::setup_receiver_auth(enc, sk_r, info, pk_s)?;
		ctx.open(aad, ct)
	}

	/// Single-shot AuthPsk-mode encrypt (RFC 9180 §6.1).
	#[allow(clippy::too_many_arguments)]
	pub fn seal_auth_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		aad: &[u8],
		pt: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		sk_s: &K::PrivateKey,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, mut ctx) = Self::setup_sender_auth_psk(rng, pk_r, info, psk, psk_id, sk_s)?;
		let ct = ctx.seal(aad, pt)?;
		Ok((enc, ct))
	}

	/// Single-shot AuthPsk-mode decrypt (RFC 9180 §6.1).
	#[allow(clippy::too_many_arguments)]
	pub fn open_auth_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		aad: &[u8],
		ct: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		pk_s: &K::PublicKey,
	) -> Result<Vec<u8>, HpkeError> {
		let mut ctx = Self::setup_receiver_auth_psk(enc, sk_r, info, psk, psk_id, pk_s)?;
		ctx.open(aad, ct)
	}
}

impl<K: Kem, F: Kdf, A: Aead> Hpke<K, F, A> {
	/// Sender-side single-shot export — Base mode (RFC 9180 §6.2).
	pub fn send_export_base<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		exporter_context: &[u8],
		length: usize,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, ctx) = Self::setup_sender_base(rng, pk_r, info)?;
		Ok((enc, ctx.export(exporter_context, length)?))
	}

	/// Receiver-side single-shot export — Base mode.
	pub fn receiver_export_base(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		exporter_context: &[u8],
		length: usize,
	) -> Result<Vec<u8>, HpkeError> {
		let ctx = Self::setup_receiver_base(enc, sk_r, info)?;
		ctx.export(exporter_context, length)
	}

	/// Sender-side single-shot export — Psk mode.
	#[allow(clippy::too_many_arguments)]
	pub fn send_export_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		exporter_context: &[u8],
		length: usize,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, ctx) = Self::setup_sender_psk(rng, pk_r, info, psk, psk_id)?;
		Ok((enc, ctx.export(exporter_context, length)?))
	}

	/// Receiver-side single-shot export — Psk mode.
	#[allow(clippy::too_many_arguments)]
	pub fn receiver_export_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		exporter_context: &[u8],
		length: usize,
	) -> Result<Vec<u8>, HpkeError> {
		let ctx = Self::setup_receiver_psk(enc, sk_r, info, psk, psk_id)?;
		ctx.export(exporter_context, length)
	}
}

impl<K: AuthKem, F: Kdf, A: Aead> Hpke<K, F, A> {
	/// Sender-side single-shot export — Auth mode.
	pub fn send_export_auth<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		sk_s: &K::PrivateKey,
		exporter_context: &[u8],
		length: usize,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, ctx) = Self::setup_sender_auth(rng, pk_r, info, sk_s)?;
		Ok((enc, ctx.export(exporter_context, length)?))
	}

	/// Receiver-side single-shot export — Auth mode.
	pub fn receiver_export_auth(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		pk_s: &K::PublicKey,
		exporter_context: &[u8],
		length: usize,
	) -> Result<Vec<u8>, HpkeError> {
		let ctx = Self::setup_receiver_auth(enc, sk_r, info, pk_s)?;
		ctx.export(exporter_context, length)
	}

	/// Sender-side single-shot export — `AuthPsk` mode.
	#[allow(clippy::too_many_arguments)]
	pub fn send_export_auth_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		sk_s: &K::PrivateKey,
		exporter_context: &[u8],
		length: usize,
	) -> Result<(K::EncappedKey, Vec<u8>), HpkeError> {
		let (enc, ctx) = Self::setup_sender_auth_psk(rng, pk_r, info, psk, psk_id, sk_s)?;
		Ok((enc, ctx.export(exporter_context, length)?))
	}

	/// Receiver-side single-shot export — `AuthPsk` mode.
	#[allow(clippy::too_many_arguments)]
	pub fn receiver_export_auth_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		pk_s: &K::PublicKey,
		exporter_context: &[u8],
		length: usize,
	) -> Result<Vec<u8>, HpkeError> {
		let ctx = Self::setup_receiver_auth_psk(enc, sk_r, info, psk, psk_id, pk_s)?;
		ctx.export(exporter_context, length)
	}
}

impl<K: AuthKem, F: Kdf, A: Aead> Hpke<K, F, A> {
	/// `SetupAuthS` (RFC 9180 §5.1.3).
	pub fn setup_sender_auth<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		sk_s: &K::PrivateKey,
	) -> Result<(K::EncappedKey, Context<K, F, A>), HpkeError> {
		let (ss, enc) = K::auth_encap(rng, pk_r, sk_s)?;
		let ctx = key_schedule::<K, F, A>(modes::AUTH, ss.as_ref(), info, &[], &[])?;
		Ok((enc, ctx))
	}

	/// `SetupAuthR` (RFC 9180 §5.1.3).
	pub fn setup_receiver_auth(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		pk_s: &K::PublicKey,
	) -> Result<Context<K, F, A>, HpkeError> {
		let ss = K::auth_decap(enc, sk_r, pk_s)?;
		key_schedule::<K, F, A>(modes::AUTH, ss.as_ref(), info, &[], &[])
	}

	/// `SetupAuthPSKS` (RFC 9180 §5.1.4).
	///
	/// `psk` MUST be at least 32 bytes of high-entropy random data. Length is
	/// enforced; entropy is the caller's responsibility — see
	/// [`HpkeError::InsecurePsk`].
	pub fn setup_sender_auth_psk<R: CryptoRng + RngCore>(
		rng: &mut R,
		pk_r: &K::PublicKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		sk_s: &K::PrivateKey,
	) -> Result<(K::EncappedKey, Context<K, F, A>), HpkeError> {
		let (ss, enc) = K::auth_encap(rng, pk_r, sk_s)?;
		let ctx = key_schedule::<K, F, A>(modes::AUTH_PSK, ss.as_ref(), info, psk, psk_id)?;
		Ok((enc, ctx))
	}

	/// `SetupAuthPSKR` (RFC 9180 §5.1.4).
	///
	/// `psk` MUST be at least 32 bytes of high-entropy random data. Length is
	/// enforced; entropy is the caller's responsibility — see
	/// [`HpkeError::InsecurePsk`].
	pub fn setup_receiver_auth_psk(
		enc: &K::EncappedKey,
		sk_r: &K::PrivateKey,
		info: &[u8],
		psk: &[u8],
		psk_id: &[u8],
		pk_s: &K::PublicKey,
	) -> Result<Context<K, F, A>, HpkeError> {
		let ss = K::auth_decap(enc, sk_r, pk_s)?;
		key_schedule::<K, F, A>(modes::AUTH_PSK, ss.as_ref(), info, psk, psk_id)
	}
}

#[cfg(feature = "kat-internals")]
#[doc(hidden)]
pub mod __test_only {
	pub use crate::key_schedule;
}

#[cfg(test)]
mod hpke_tests {
	use super::*;
	use rand_core::{OsRng, TryRngCore as _};

	/// Type-level check: `ExportOnly` suites compile without a `SealingAead`
	/// bound, exposing only `*_export_*` methods. The full setup/seal/open
	/// matrix lives in `tests/roundtrip.rs`.
	#[test]
	fn export_only_suite_compiles() {
		type ExportSuite = Hpke<DhKemX25519HkdfSha256, HkdfSha256, ExportOnly>;
		let mut os_rng = OsRng;
		let mut rng = os_rng.unwrap_mut();
		let (sk_r, pk_r) = DhKemX25519HkdfSha256::generate(&mut rng).unwrap();
		let (enc, sec) =
			ExportSuite::send_export_base(&mut rng, &pk_r, b"info", b"ctx", 32).unwrap();
		let recv = ExportSuite::receiver_export_base(&enc, &sk_r, b"info", b"ctx", 32).unwrap();
		assert_eq!(sec, recv);
	}
}