Helpers

Message encoding helpers for self-describing HPKE messages.

rfc9180.helpers.append_header(enc, ct, kem_id, kdf_id, aead_id, mode)[source]

Encode an HPKE message with a self-describing header.

Format: “HPKE” | kem_id(2) | kdf_id(2) | aead_id(2) | mode(1) | enc | ct

Parameters:
  • enc (bytes) – Encapsulated public key.

  • ct (bytes) – Ciphertext.

  • kem_id (int) – KEM algorithm identifier (2 bytes).

  • kdf_id (int) – KDF algorithm identifier (2 bytes).

  • aead_id (int) – AEAD algorithm identifier (2 bytes).

  • mode (int) – HPKE mode identifier (1 byte).

Returns:

Complete HPKE message with header.

Return type:

bytes

rfc9180.helpers.parse_header(msg, enc_len)[source]

Parse a self-describing HPKE message.

Parameters:
  • msg (bytes) – Complete HPKE message with header.

  • enc_len (int) – Length of the encapsulated public key (Nenc) for the specific KEM.

Returns:

Tuple of (kem_id, kdf_id, aead_id, mode, enc, ct).

Return type:

tuple

Raises:

ValueError – If message is too short or has invalid magic bytes.