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

__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

increment_seq()[source]

Increment the sequence number.

Raises:

MessageLimitReachedError – If sequence number would overflow.

Return type:

None

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]