# 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 `_internal` and `errors` and that the root never loads. It exposes `encrypt0`/`decrypt0` (Integrated Encryption, COSE_Encrypt0, tag 16), `encrypt`/`decrypt` (Key Encryption, COSE_Encrypt, tag 96, any number of recipients up to `MAX_RECIPIENTS`), `encode_key`/`decode_key` (COSE_Key, section 3.4), and the `HpkeAlgorithm` and `ContentAlgorithm` (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.py` implements 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`, and `psk_id` go in the protected header, so they reach the HPKE key schedule. `ek` and 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. The `Recipient_structure` binds the layer-0 algorithm (`next_layer_alg`), the recipient's protected header, and an untransmitted `recipient_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; - `crit` only protected, non-empty, and fully understood, enforced on the layers this reader processes; - `kid`, `IV`, `Partial IV`, `ek`, and `psk_id` are byte strings; - `psk_id` is protected and non-empty, and `ek` is unprotected; - the HPKE `alg` is protected and required; - a layer-0 `Partial IV` or 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: `TypeError` or `ValueError`. - **Keys.** `encode_key` writes EC2 (`x`, `y`, `d` at the field width) or OKP keys. It adds `key_ops: [derive bits]` to a private key and an `alg` and `kid` when given. `decode_key` enforces figure 1 (kty and curve per KEM) and section 3.4 (`key_ops` exactly `[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 any `alg` be a COSE-HPKE suite for the key's curve. A compressed EC2 `y` is 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.py` checks 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.py` checks: - 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.py` checks 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.