Context

HPKE encryption contexts (RFC 9180 §5.2). Sender and recipient contexts for seal/open and export.

class rfc9180.context.Context(role, aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]

Bases: object

HPKE Encryption Context (RFC 9180 §5.2).

Manages the encryption/decryption state for HPKE operations, including sequence number tracking and key export functionality.

Parameters:
  • role (Role) – Context role (‘S’ for sender, ‘R’ for recipient).

  • aead (AEADBase) – AEAD algorithm instance.

  • kdf (KDFBase) – KDF algorithm instance.

  • key (bytes) – AEAD encryption key.

  • base_nonce (bytes) – Base nonce for nonce generation.

  • exporter_secret (bytes) – Secret for key export operations.

  • suite_id (bytes) – HPKE suite identifier.

role

Context role.

Type:

Role

aead

AEAD algorithm instance.

Type:

AEADBase

kdf

KDF algorithm instance.

Type:

KDFBase

key

AEAD encryption key.

Type:

bytes

base_nonce

Base nonce for nonce generation.

Type:

bytes

exporter_secret

Secret for key export operations.

Type:

bytes

suite_id

HPKE suite identifier.

Type:

bytes

seq

Current sequence number.

Type:

int

``seal`` and ``open`` hold ``_seq_lock`` across the limit check, nonce
allocation, the AEAD call, and the sequence increment. The AEAD stays
inside the lock so two callers cannot be handed the same nonce; moving
encryption outside the lock would reintroduce nonce reuse. A rejected
call does not encrypt and does not advance ``seq``. AES-GCM stops at
2^32 messages. That cap follows the invocation bound in NIST SP 800-38D
§8.3, which is written for random nonces. HPKE nonces are a counter XOR
a secret base, so they are unique, and the cap is conservative hardening
beyond RFC 9180 §5.2. ChaCha20-Poly1305 rejects ``seq >= 2^96 - 1`` before
sealing, so the last nonce value is unused
Type:

2^96 - 1 messages, one short

of the RFC pseudocode, which seals with that nonce and then raises.
Python ``bytes`` are not zeroized on deletion.
__init__(role, aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]
compute_nonce(seq)[source]

Compute nonce for a given sequence number.

Parameters:

seq (int) – Sequence number.

Returns:

Computed nonce.

Return type:

bytes

seal(aad, pt)[source]

Seal (encrypt) a message.

Parameters:
  • aad (bytes) – Additional authenticated data.

  • pt (bytes) – Plaintext to encrypt.

Returns:

Ciphertext.

Return type:

bytes

Raises:
open(aad, ct)[source]

Open (decrypt) a message.

Parameters:
  • aad (bytes) – Additional authenticated data.

  • ct (bytes) – Ciphertext to decrypt.

Returns:

Decrypted plaintext.

Return type:

bytes

Raises:
export(exporter_context, L)[source]

Export a secret value.

Parameters:
  • exporter_context (bytes) – Exporter context.

  • L (int) – Length of exported secret in bytes.

Returns:

Exported secret.

Return type:

bytes

Raises:

ValueError – If export length exceeds maximum.

class rfc9180.context.ContextSender(aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]

Bases: Context

Sender encryption context.

A specialized Context for senders that can only seal (encrypt) messages.

__init__(aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]
class rfc9180.context.ContextRecipient(aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]

Bases: Context

Recipient decryption context.

A specialized Context for recipients that can only open (decrypt) messages.

__init__(aead, kdf, key, base_nonce, exporter_secret, suite_id)[source]