RFC-0030: COSE-HPKE Interoperability¶
Status: Proposed.
Owner: Format lead.
Last updated: 2026-11-30
Context¶
The v4 envelope is this SDK’s own format, and the conformance suite (RFC-0029) lets other implementations read it. Many systems that hold or move sensitive data speak COSE (RFC 9052) instead: constrained devices, WebAuthn, ISO 18013-5 mobile driver’s licences, EU digital identity wallets, SUIT firmware updates (RFC 9124), and C2PA. draft-ietf-cose-hpke, now with the IESG, adds HPKE (RFC 9180) public-key encryption to COSE. To exchange encrypted data with that ecosystem, the SDK must produce and consume COSE-HPKE exactly, with the keys its HPKE recipients (RFC-0012, RFC-0017) already use.
pyca/cryptography’s HPKE takes no additional data and has no PSK mode.
COSE-HPKE’s Integrated Encryption passes the COSE Enc_structure as HPKE
additional data, and both modes define the PSK mode. Its private
_encrypt_with_aad entry point is not an API.
Binding decision¶
Module.
cryptography_suite.cose, a layer that imports only_internalanderrorsand that the root never loads. It exposesencrypt0/decrypt0(Integrated Encryption, COSE_Encrypt0, tag 16),encrypt/decrypt(Key Encryption, COSE_Encrypt, tag 96, any number of recipients up toMAX_RECIPIENTS),encode_key/decode_key(COSE_Key, section 3.4), and theHpkeAlgorithmandContentAlgorithm(A128GCM, A192GCM, A256GCM, ChaCha20/Poly1305) identifiers.Suites. All sixteen: HPKE-0 to HPKE-7 and HPKE-0-KE to HPKE-7-KE, over DHKEM(P-256, P-384, P-521, X25519, X448), HKDF-SHA256/384/512, and AES-128-GCM, AES-256-GCM, and ChaCha20-Poly1305. The values are the draft’s assumed algorithm identifiers (35 to 53) and header labels (
ek-4,psk_id-5), the ones its examples use. They change, in one table, when IANA assigns final values. An identifier is accepted only in its own mode.HPKE.
cose/_hpke.pyimplements RFC 9180 sections 4, 5.1, and 7 for single-shot use (sequence number zero) in the base and PSK modes. DH, HKDF, and the AEADs are pyca/cryptography’s. Encapsulated keys must have the exact length, NIST-curve points must be uncompressed and on the curve, and all-zero X25519/X448 results are refused. A PSK must have at least 32 bytes, its identifier must not be empty, and a PSK and its identifier come together or not at all.Writing.
alg,kid, andpsk_idgo in the protected header, so they reach the HPKE key schedule.ekand the 96-bit IV go in the unprotected header. The content key and IV come from the operating system’s CSPRNG, and the content key lives in a wiped buffer. TheRecipient_structurebinds the layer-0 algorithm (next_layer_alg), the recipient’s protected header, and an untransmittedrecipient_extra_info.Reading. Messages up to 64 MiB plus 1 MiB of structure. The CBOR codec (
cose/_cbor.py) is separate from the envelope codec. It accepts any well-formed definite-length item in any key order, because integrity rests on the received protected bytes. It rejects indefinite lengths, duplicate keys (RFC 9052 section 9), reserved simple values, and trailing data, within depth and item bounds. Header rules:no label in both buckets;
critonly protected, non-empty, and fully understood, enforced on the layers this reader processes;kid,IV,Partial IV,ek, andpsk_idare byte strings;psk_idis protected and non-empty, andekis unprotected;the HPKE
algis protected and required;a layer-0
Partial IVor detached content is unsupported;the AES-GCM and ChaCha20-Poly1305 IV is exactly 96 bits (RFC 9053 section 4.1).
Recipients of other content-key-distribution methods are skipped without being judged. HPKE recipients that match the key’s group, the optional
kid, and the caller’s PSK mode are tried in order. A recipient whose content key fails the content layer is passed over, so one lifted from another message cannot block the real one.Errors. Malformed:
EnvelopeError(FORMAT_INVALID,FORMAT_UNSUPPORTED,CRITICAL_FIELD_UNSUPPORTED,TRAILING_DATA,LIMIT_EXCEEDED). Cannot be opened with the given key and inputs:AuthenticationError, without the reason. Bad arguments:TypeErrororValueError.Keys.
encode_keywrites EC2 (x,y,dat the field width) or OKP keys. It addskey_ops: [derive bits]to a private key and analgandkidwhen given.decode_keyenforces figure 1 (kty and curve per KEM) and section 3.4 (key_opsexactly[derive bits]on a private key, empty on a public one). It requires that the public parts of a private key match it and that anyalgbe a COSE-HPKE suite for the key’s curve. A compressed EC2yis unsupported.
Erratum found in the draft¶
Section 5.2’s COSE_Encrypt carries a 16-byte IV for A128GCM, while RFC 9053
section 4.1 fixes the AES-GCM nonce at 96 bits. The reader rejects the
message as published (FORMAT_INVALID). Its HPKE recipient is reproduced
exactly: the private key from the example opens the content key, which
opens the published content under the example’s IV. With the content
re-encrypted under a 96-bit IV and the recipient unchanged byte for byte,
decrypt returns the plaintext. Both examples’ plaintext is
"This is the content.\n", with a trailing newline the prose omits.
Test and validation requirements¶
CFRG vectors.
tests/unit/test_cose_hpke.pychecks RFC 9180 against the CFRG test vectors for every COSE-HPKE triple in both modes, from the shared secret through the key and nonce to the ciphertext. The vectors are pinned at a commit with the upstream file’s SHA-256 and a checksum of the selection (tests/vectors/hpke,tools/update_hpke_vectors.py).Differential. The same file checks all 36 KEM, KDF, and AEAD combinations pyca/cryptography implements, sealing with one implementation and opening with the other, with additional data where pyca/cryptography’s private oracle exists. This stands in for the CFRG vectors for P-384, which they lack.
Messages.
tests/unit/test_cose.pychecks:the draft’s examples and keys;
round trips for every suite, both modes, tagged and untagged, and every content algorithm;
one message to all eight Key Encryption suites, with
kid, extra info, and PSK selection;the SDK’s own HPKE keys;
every single-bit flip of both message kinds;
each header and structure rule;
spliced recipients and content keys of the wrong length;
argument and size limits;
COSE_Key round trips for every curve and every key rule.
CBOR.
tests/unit/test_cose_cbor.pychecks the RFC 8949 examples, argument widths, simple values and floats, and every malformed form.Coverage. The package is at 100% line and branch coverage.
Non-goals¶
Detached content, the Auth modes, and Partial IV.
Post-quantum COSE-HPKE suites, until identifiers are registered.
Countersignatures and COSE_Mac/COSE_Sign wrapping, which callers compose.