HPKE
Hybrid Public Key Encryption (RFC 9180) with classical and post-quantum KEMs.
Warning
The ML-KEM, hybrid and X-Wing KEMs and the SHAKE and TurboSHAKE KDFs follow Internet-Drafts and are experimental. Their identifiers, encodings and outputs can still change.
oxcrypto.hpke wraps the hpke crate
0.15.1. Pick a KEM, KDF and AEAD, then seal to a recipient's public key.
from oxcrypto.hpke import AEAD, KDF, KEM, CipherSuite
kem = KEM.DHKEM_X25519_HKDF_SHA256
suite = CipherSuite(kem, KDF.HKDF_SHA256, AEAD.CHACHA20_POLY1305)
recipient = kem.generate()
enc, ciphertext = suite.seal(recipient.public_key(), b"hello", info=b"app v1")
assert suite.open(recipient, enc, ciphertext, info=b"app v1") == b"hello"
enc, sender = suite.setup_sender(recipient.public_key(), info=b"app v1")
receiver = suite.setup_receiver(recipient, enc, info=b"app v1")
first, second = sender.seal(b"one"), sender.seal(b"two")
assert receiver.open(first) == b"one"
assert receiver.open(second) == b"two"
assert sender.export(b"label", 32) == receiver.export(b"label", 32)Modes
The mode follows from the keyword options, which must match on both sides.
| Mode | Sender options | Receiver options |
|---|---|---|
| Base | none | none |
| PSK | psk, psk_id | psk, psk_id |
| Auth | sender_key | sender_public_key |
| Auth-PSK | sender_key, psk, psk_id | sender_public_key, psk, psk_id |
psk and psk_id must be given together and be non-empty. RFC 9180 asks for a
PSK of at least 32 bytes of entropy. Auth modes need a DHKEM; the ML-KEM, hybrid
and X-Wing KEMs raise ValueError when given a sender key. Keys must belong to
the suite's KEM.
API
KEM.generate(),KEM.derive_key_pair(ikm),KEM.load_private_bytes(data)andKEM.load_public_bytes(data)createHpkePrivateKeyandHpkePublicKeyobjects. Both havekemandto_bytes(), and the private key haspublic_key(). Bytes use the RFC 9180SerializePrivateKeyandSerializePublicKeyencodings; X25519 private keys come back clamped.CipherSuite(kem, kdf, aead)has single-shotseal,open,send_exportandreceive_export, andsetup_senderandsetup_receiver, which return(enc, SenderContext)and aReceiverContext.SenderContext.seal(plaintext, *, aad=b""),ReceiverContext.open(ciphertext, *, aad=b"")andexport(exporter_context, length)on both contexts.
State contract
Important
Each seal and each successful open advances the context's sequence
number, so messages must be opened in the order they were sealed.
A context holds the key schedule secrets and a sequence number. A failed open raises
oxcrypto.InvalidTag and leaves the context unchanged, so the next message can
still be opened. export does not change the sequence number.
hpke 0.15.1 counts with a 64-bit sequence number, so the context raises
ValueError after 2^64 − 1 messages. Contexts cannot be copied or pickled, and
concurrent calls on one context are serialized.
With AEAD.EXPORT_ONLY, seal and open raise ValueError and only export
works. Export lengths above the KDF limit (255 times the hash size for HKDF)
raise ValueError. Export lengths above 65535 bytes, and info, psk or psk_id longer than
65535 bytes with a SHAKE or TurboSHAKE KDF, raise ValueError (hpke 0.15.1
would panic on the latter). Wrong key or enc lengths, invalid points and
mismatched KEMs raise ValueError; non-bytes inputs raise TypeError.
Algorithms
| Selector | Standard and status | Experimental |
|---|---|---|
KEM.DHKEM_X25519_HKDF_SHA256 (0x0020) | RFC 9180 | No |
KEM.DHKEM_P256_HKDF_SHA256 (0x0010) | RFC 9180 | No |
KEM.DHKEM_P384_HKDF_SHA384 (0x0011) | RFC 9180 | No |
KEM.DHKEM_P521_HKDF_SHA512 (0x0012) | RFC 9180 | No |
KEM.ML_KEM_768 (0x0041) | ML-KEM is FIPS 203; HPKE use is draft-ietf-hpke-pq-04 | Yes |
KEM.ML_KEM_1024 (0x0042) | as above | Yes |
KEM.MLKEM768_P256 (0x0050) | draft-irtf-cfrg-concrete-hybrid-kems-03, draft-ietf-hpke-pq-04 | Yes |
KEM.MLKEM1024_P384 (0x0051) | as above | Yes |
KEM.X_WING (0x647A) | draft-connolly-cfrg-xwing-kem-10, draft-ietf-hpke-pq-04 | Yes |
KDF.HKDF_SHA256, HKDF_SHA384, HKDF_SHA512 | RFC 9180 | No |
KDF.SHAKE128, SHAKE256, TURBOSHAKE128, TURBOSHAKE256 | One-stage KDFs from draft-ietf-hpke-pq-04 | Yes |
AEAD.AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305, EXPORT_ONLY | RFC 9180 | No |
RFC 9180 is an Informational RFC from the IRTF CFRG. Experimental suites may not interoperate with later versions of their drafts.
CipherSuiteraisesValueErrorwhenEXPORT_ONLYis paired with a SHAKE or TurboSHAKE KDF.
Maturity
hpke 0.15.1 is a stable release with Rust 1.87 as its declared MSRV; it
builds with the oxcrypto MSRV of Rust 1.88. It has no published audit. Its
README links a Cloudflare internal review
of version 0.8. Constant-time behavior comes from the underlying RustCrypto
crates (x25519-dalek, p256, p384, p521, ml-kem, aes-gcm,
chacha20poly1305); oxcrypto has not verified it independently.
Zeroization
hpke 0.15.1 zeroizes private keys, shared secrets, the AEAD key, base nonce,
exporter secret and sequence number of a context on drop. oxcrypto enables the
zeroize feature of aes-gcm so that the AES-GCM key schedule is also wiped;
the ChaCha20-Poly1305 implementation wipes its key on drop. oxcrypto has not
verified this behavior independently.
Note
Zeroization does not cover the HKDF and one-stage KDF working buffers
(including the 108-byte one-stage key schedule output), keys, enc, PSKs,
plaintexts and exported values passed in or returned as Python bytes, or
other short-lived stack and register copies made during a call. It does not
guarantee that secrets are gone from process memory.