COSE-HPKE

COSE (RFC 9052) is the encryption and signing format of constrained devices, FIDO and WebAuthn, mobile driver’s licences (ISO 18013-5), EU digital identity wallets, SUIT firmware updates (RFC 9124), and C2PA. cryptography_suite.cose reads and writes HPKE-encrypted COSE messages as defined by draft-ietf-cose-hpke (revision 27), so systems built on this SDK can exchange encrypted data with that ecosystem.

It is separate from the v4 envelope format and uses the same pyca/cryptography keys as the HPKE recipients.

Note

The algorithm identifiers (35 to 53) and the ek and psk_id header labels are the draft’s assumed values. They will change, in one place, when IANA assigns final ones.

One recipient: COSE_Encrypt0

In Integrated Encryption, HPKE encrypts the content directly:

from cryptography.hazmat.primitives.asymmetric import ec

from cryptography_suite import cose

device = ec.generate_private_key(ec.SECP384R1())

message = cose.encrypt0(b"ping", device.public_key(), algorithm=cose.HpkeAlgorithm.HPKE_1)
assert cose.decrypt0(message, device) == b"ping"

Several recipients: COSE_Encrypt

In Key Encryption, a random content key encrypts the content once, and HPKE wraps the content key for each recipient:

gateway = ec.generate_private_key(ec.SECP384R1())

message = cose.encrypt(
    b'{"reading": 4711, "unit": "kWh"}',
    [
        cose.Recipient(device.public_key(), cose.HpkeAlgorithm.HPKE_1_KE, kid=b"meter-17"),
        cose.Recipient(gateway.public_key(), cose.HpkeAlgorithm.HPKE_1_KE, kid=b"gw-3"),
    ],
    external_aad=b"firmware-channel-v2",
)
reading = cose.decrypt(message, device, kid=b"meter-17", external_aad=b"firmware-channel-v2")
assert reading == b'{"reading": 4711, "unit": "kWh"}'

external_aad is authenticated but not transmitted, like the v4 encryption context: both sides must supply the same bytes. The content algorithm is AES-256-GCM unless content_algorithm says otherwise (cose.ContentAlgorithm).

Keys: COSE_Key

Publish and read keys in their COSE form:

published = cose.encode_key(device.public_key(), algorithm=cose.HpkeAlgorithm.HPKE_1_KE, kid=b"meter-17")

recipient = cose.decode_key(published)
assert recipient.algorithm is cose.HpkeAlgorithm.HPKE_1_KE and recipient.kid == b"meter-17"

decode_key checks what the draft requires: the key type and curve match the algorithm, a private key’s key_ops is exactly derive bits, and the public part of a private key matches it.

Ciphersuites

Integrated

Key Encryption

KEM

KDF

AEAD

HPKE_0 (35)

HPKE_0_KE (46)

DHKEM(P-256)

HKDF-SHA256

AES-128-GCM

HPKE_1 (37)

HPKE_1_KE (47)

DHKEM(P-384)

HKDF-SHA384

AES-256-GCM

HPKE_2 (39)

HPKE_2_KE (48)

DHKEM(P-521)

HKDF-SHA512

AES-256-GCM

HPKE_3 (41)

HPKE_3_KE (49)

DHKEM(X25519)

HKDF-SHA256

AES-128-GCM

HPKE_4 (42)

HPKE_4_KE (50)

DHKEM(X25519)

HKDF-SHA256

ChaCha20-Poly1305

HPKE_5 (43)

HPKE_5_KE (51)

DHKEM(X448)

HKDF-SHA512

AES-256-GCM

HPKE_6 (44)

HPKE_6_KE (52)

DHKEM(X448)

HKDF-SHA512

ChaCha20-Poly1305

HPKE_7 (45)

HPKE_7_KE (53)

DHKEM(P-256)

HKDF-SHA256

AES-256-GCM

Each identifier is accepted only in its own mode. The pre-shared-key mode of HPKE is available through psk and psk_id (at least 32 bytes of PSK).

Reading untrusted messages

Messages up to 64 MiB of content are accepted. The decoder rejects indefinite lengths, duplicate map keys, unknown critical headers, wrong IV sizes, and trailing data, with EnvelopeError. A message that the given key and inputs cannot open raises AuthenticationError, without saying why. Like the v4 envelope, COSE-HPKE authenticates the recipient, not the sender: sign the message when its origin matters.

Conformance

The implementation is checked against the draft’s own examples, the CFRG test vectors for RFC 9180 in every suite and both modes, and pyca/cryptography’s HPKE in both directions (RFC-0030). The draft’s section 5.2 example uses a 128-bit AES-GCM IV, which RFC 9053 does not allow; that message is rejected, and its HPKE layer is reproduced exactly.