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:
objectHPKE 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
- ``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.
- seal(aad, pt)[source]
Seal (encrypt) a message.
- Parameters:
- Returns:
Ciphertext.
- Return type:
- Raises:
ValueError – If context is not a sender context.
MessageLimitReachedError – If sequence number would overflow.
- open(aad, ct)[source]
Open (decrypt) a message.
- Parameters:
- Returns:
Decrypted plaintext.
- Return type:
- Raises:
ValueError – If context is not a recipient context.
OpenError – If decryption fails.
MessageLimitReachedError – If sequence number would overflow.