Primitives
KEM, KDF, and AEAD primitives used by HPKE.
KEM (Key Encapsulation Mechanism)
- class rfc9180.primitives.kem.KEMBase(kem_id)[source]
Bases:
ABCBase class for DHKEM implementations.
Provides Diffie-Hellman-based Key Encapsulation Mechanism operations as specified in RFC 9180 §7.1.
- Parameters:
kem_id (KEMID) – KEM algorithm identifier.
- kem_id
KEM algorithm identifier.
- Type:
KEMID
- abstractmethod generate_key_pair()[source]
Generate a new key pair.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- abstractmethod derive_key_pair(ikm)[source]
Derive a key pair from input key material.
- Parameters:
ikm (bytes) – Input key material.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- Raises:
ValueError – If IKM is too short.
DeriveKeyPairError – If derivation fails (e.g., rejection sampling exceeded).
- abstractmethod serialize_public_key(pk)[source]
Serialize a public key to bytes.
- Parameters:
pk (Key Object) – Public key.
- Returns:
Serialized public key.
- Return type:
- abstractmethod deserialize_public_key(pkm)[source]
Deserialize a public key from bytes.
- Parameters:
pkm (bytes) – Serialized public key.
- Returns:
Public key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- abstractmethod serialize_private_key(sk)[source]
Serialize a private key to bytes.
- Parameters:
sk (Key Object) – Private key.
- Returns:
Serialized private key.
- Return type:
- abstractmethod deserialize_private_key(skm)[source]
Deserialize a private key from bytes.
- Parameters:
skm (bytes) – Serialized private key.
- Returns:
Private key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- abstractmethod dh(sk, pk)[source]
Perform Diffie-Hellman key exchange.
- Parameters:
sk (Key Object) – Private key.
pk (Key Object) – Public key.
- Returns:
Shared secret.
- Return type:
- Raises:
ValidationError – If DH operation fails or output is invalid.
- encap(pkR)[source]
Base/PSK encapsulation.
- Parameters:
pkR (Key Object) – Recipient’s public key.
- Returns:
Tuple of (shared_secret, encapsulated_key).
- Return type:
- Raises:
EncapError – If encapsulation fails.
- decap(enc, skR)[source]
Base/PSK decapsulation.
- Parameters:
enc (bytes) – Encapsulated public key.
skR (Key Object) – Recipient’s private key.
- Returns:
Shared secret.
- Return type:
- Raises:
DecapError – If decapsulation fails.
- auth_encap(pkR, skS)[source]
Authenticated encapsulation (Auth/AuthPSK).
- Parameters:
pkR (Key Object) – Recipient’s public key.
skS (Key Object) – Sender’s private key.
- Returns:
Tuple of (shared_secret, encapsulated_key).
- Return type:
- Raises:
EncapError – If encapsulation fails.
- auth_decap(enc, skR, pkS)[source]
Authenticated decapsulation (Auth/AuthPSK).
- Parameters:
enc (bytes) – Encapsulated public key.
skR (Key Object) – Recipient’s private key.
pkS (Key Object) – Sender’s public key.
- Returns:
Shared secret.
- Return type:
- Raises:
DecapError – If decapsulation fails.
- class rfc9180.primitives.kem.DHKEM_X25519[source]
Bases:
KEMBaseDHKEM with X25519 and HKDF-SHA256.
Implements DHKEM using the X25519 elliptic curve Diffie-Hellman function and HKDF-SHA256 for key derivation.
- generate_key_pair()[source]
Generate a new X25519 key pair.
- Returns:
Tuple of (X25519PrivateKey, X25519PublicKey).
- Return type:
- derive_key_pair(ikm)[source]
RFC 9180 §7.1.3 - DeriveKeyPair for X25519.
- Parameters:
ikm (bytes) – Input key material (must be at least Nsk bytes).
- Returns:
Tuple of (X25519PrivateKey, X25519PublicKey).
- Return type:
- Raises:
ValueError – If IKM is too short.
- serialize_public_key(pk)[source]
Serialize a public key to bytes.
- Parameters:
pk (Key Object) – Public key.
- Returns:
Serialized public key.
- Return type:
- deserialize_public_key(pkm)[source]
Deserialize a public key from bytes.
- Parameters:
pkm (bytes) – Serialized public key.
- Returns:
Public key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- serialize_private_key(sk)[source]
Serialize a private key to bytes.
- Parameters:
sk (Key Object) – Private key.
- Returns:
Serialized private key.
- Return type:
- deserialize_private_key(skm)[source]
Deserialize a private key from bytes.
- Parameters:
skm (bytes) – Serialized private key.
- Returns:
Private key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- dh(sk, pk)[source]
Perform Diffie-Hellman key exchange.
- Parameters:
sk (Key Object) – Private key.
pk (Key Object) – Public key.
- Returns:
Shared secret.
- Return type:
- Raises:
ValidationError – If DH operation fails or output is invalid.
- class rfc9180.primitives.kem.DHKEM_X448[source]
Bases:
KEMBaseDHKEM with X448 and HKDF-SHA512.
Implements DHKEM using the X448 elliptic curve Diffie-Hellman function and HKDF-SHA512 for key derivation.
- generate_key_pair()[source]
Generate a new key pair.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- derive_key_pair(ikm)[source]
Derive a key pair from input key material.
- Parameters:
ikm (bytes) – Input key material.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- Raises:
ValueError – If IKM is too short.
DeriveKeyPairError – If derivation fails (e.g., rejection sampling exceeded).
- serialize_public_key(pk)[source]
Serialize a public key to bytes.
- Parameters:
pk (Key Object) – Public key.
- Returns:
Serialized public key.
- Return type:
- deserialize_public_key(pkm)[source]
Deserialize a public key from bytes.
- Parameters:
pkm (bytes) – Serialized public key.
- Returns:
Public key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- serialize_private_key(sk)[source]
Serialize a private key to bytes.
- Parameters:
sk (Key Object) – Private key.
- Returns:
Serialized private key.
- Return type:
- deserialize_private_key(skm)[source]
Deserialize a private key from bytes.
- Parameters:
skm (bytes) – Serialized private key.
- Returns:
Private key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- dh(sk, pk)[source]
Perform Diffie-Hellman key exchange.
- Parameters:
sk (Key Object) – Private key.
pk (Key Object) – Public key.
- Returns:
Shared secret.
- Return type:
- Raises:
ValidationError – If DH operation fails or output is invalid.
- class rfc9180.primitives.kem.DHKEM_NIST(kem_id, curve, order, mask=255)[source]
Bases:
KEMBaseBase for NIST P-curves (P-256, P-384, P-521).
Implements DHKEM using NIST elliptic curves with ECDH and rejection sampling for key derivation.
- Parameters:
kem_id (KEMID) – KEM algorithm identifier.
curve (
EllipticCurve) – Elliptic curve instance.order (int) – Curve order for rejection sampling.
- generate_key_pair()[source]
Generate a new key pair.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- derive_key_pair(ikm)[source]
Derive a key pair from input key material.
- Parameters:
ikm (bytes) – Input key material.
- Returns:
Tuple of (private_key, public_key) as Key Objects.
- Return type:
- Raises:
ValueError – If IKM is too short.
DeriveKeyPairError – If derivation fails (e.g., rejection sampling exceeded).
- serialize_public_key(pk)[source]
Serialize a public key to bytes.
- Parameters:
pk (Key Object) – Public key.
- Returns:
Serialized public key.
- Return type:
- deserialize_public_key(pkm)[source]
Deserialize a public key from bytes.
- Parameters:
pkm (bytes) – Serialized public key.
- Returns:
Public key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- serialize_private_key(sk)[source]
Serialize a private key to bytes.
- Parameters:
sk (Key Object) – Private key.
- Returns:
Serialized private key.
- Return type:
- deserialize_private_key(skm)[source]
Deserialize a private key from bytes.
- Parameters:
skm (bytes) – Serialized private key.
- Returns:
Private key.
- Return type:
Key Object
- Raises:
DeserializeError – If deserialization fails.
- dh(sk, pk)[source]
Perform Diffie-Hellman key exchange.
- Parameters:
sk (Key Object) – Private key.
pk (Key Object) – Public key.
- Returns:
Shared secret.
- Return type:
- Raises:
ValidationError – If DH operation fails or output is invalid.
- class rfc9180.primitives.kem.DHKEM_P256[source]
Bases:
DHKEM_NISTDHKEM with P-256 and HKDF-SHA256.
Implements DHKEM using the NIST P-256 curve (secp256r1) and HKDF-SHA256 for key derivation.
- class rfc9180.primitives.kem.DHKEM_P384[source]
Bases:
DHKEM_NISTDHKEM with P-384 and HKDF-SHA384.
Implements DHKEM using the NIST P-384 curve (secp384r1) and HKDF-SHA384 for key derivation.
- class rfc9180.primitives.kem.DHKEM_P521[source]
Bases:
DHKEM_NISTDHKEM with P-521 and HKDF-SHA512.
Implements DHKEM using the NIST P-521 curve (secp521r1) and HKDF-SHA512 for key derivation.
KDF (Key Derivation Function)
- class rfc9180.primitives.kdf.KDFBase(kdf_id)[source]
Bases:
objectBase class for KDF implementations (HKDF variants).
Provides key derivation functions based on HKDF (RFC 5869) with HPKE-specific labeled operations (RFC 9180 §4).
- Parameters:
kdf_id (KDFID) – KDF algorithm identifier.
- kdf_id
KDF algorithm identifier.
- Type:
KDFID
- hash_algorithm
Hash algorithm instance.
- expand(prk, info, L)[source]
HKDF-Expand (RFC 5869).
- Parameters:
- Returns:
Output keying material.
- Return type:
- Raises:
ValueError – If requested length exceeds maximum (255 * Nh).
- labeled_expand(prk, label, info, L, suite_id)[source]
LabeledExpand from RFC 9180 §4.
- Parameters:
- Returns:
Output keying material.
- Return type:
- Raises:
ValueError – If requested length exceeds maximum (255 * Nh).
AEAD
- class rfc9180.primitives.aead.AEADBase(aead_id)[source]
Bases:
objectBase class for AEAD wrappers.
Provides authenticated encryption with associated data (AEAD) operations for HPKE. Supports AES-GCM and ChaCha20-Poly1305.
- Parameters:
aead_id (AEADID) – AEAD algorithm identifier.
- aead_id
AEAD algorithm identifier.
- Type:
AEADID
- cipher
Cipher factory function.
- Type:
callable or None
- seal(key, nonce, aad, pt)[source]
Seal (encrypt and authenticate) a message.
- Parameters:
- Returns:
Ciphertext (includes authentication tag).
- Return type:
- Raises:
ValueError – If AEAD is EXPORT_ONLY, or if key/nonce lengths are invalid.
MessageLimitReachedError – If nonce reuse or limits exceeded.
- open(key, nonce, aad, ct)[source]
Open (decrypt and verify) a message.
- Parameters:
- Returns:
Decrypted plaintext.
- Return type:
- Raises:
ValueError – If AEAD is EXPORT_ONLY, or if key/nonce lengths are invalid.
OpenError – If decryption or authentication fails.