Key encapsulation
Agree on a shared secret with a recipient's public key.
Choose an algorithm and call generate() to create a private key from the
operating system CSPRNG. public_key() returns the matching public key.
encapsulate() on the public key returns (ciphertext, shared_secret); the
recipient passes the ciphertext to decapsulate() on the private key and gets
the same shared secret.
from oxcrypto.kem.ml_kem import ML_KEM
private_key = ML_KEM.ML_KEM_768.generate()
public_key = private_key.public_key()
ciphertext, shared_secret = public_key.encapsulate()
assert private_key.decapsulate(ciphertext) == shared_secret
restored = ML_KEM.ML_KEM_768.load_private_key(private_key.to_bytes())
assert restored == private_keyto_bytes() returns the raw key encoding listed on each algorithm page, and
load_private_key() and load_public_key() read it back. Wrong key or
ciphertext lengths, and keys or ciphertexts the upstream crate rejects, raise
ValueError. Inputs that are not bytes raise TypeError.
Lattice and code-based KEMs use implicit rejection: a modified ciphertext of the right length decapsulates to an unrelated pseudorandom secret instead of raising, so a mismatch shows up when the shared secret is used.
Private keys never show key material in repr, cannot be hashed, copied or
pickled, and compare with subtle's constant-time equality over their
encoding. Public keys compare and hash by value. Generation, encapsulation,
decapsulation and key loading release the GIL.
Algorithms
| Module | Variants | Standard | Status |
|---|---|---|---|
| ML-KEM | ML_KEM_512, ML_KEM_768, ML_KEM_1024 | FIPS 203 | Standard |
| X-Wing | X_WING | draft-connolly-cfrg-xwing-kem | Experimental (Internet-Draft) |
| DHKEM | X25519_HKDF_SHA256 | RFC 9180 | Standard |
| HQC | HQC_128, HQC_192, HQC_256 | NIST selection | Experimental (pre-release crate) |
| FrodoKEM | FRODOKEM_*, EFRODOKEM_* | ISO/IEC 18033-2 amendment draft | Experimental |
| sntrup | SNTRUP653 … SNTRUP1277 | draft-josefsson-ntruprime-streamlined | Experimental (pre-release crate) |
None of the wrapped crates has a published security audit. Properties such as constant-time behavior and zeroization come from the upstream crates; oxcrypto has not verified them independently.
Zeroization
A private key object owns its Rust key state. The upstream crates are written
to overwrite that state with zeros on drop; where an upstream type does not,
oxcrypto wraps it in zeroize::Zeroizing. Each algorithm page lists the
retained state and what wipes it. oxcrypto also wipes the Rust copies of
shared secrets, exported private keys and other secret outputs it handles.
Zeroization does not cover:
- Private keys, ciphertexts and shared secrets passed in or returned as Python
bytes. Python owns these objects. oxcrypto does not wipe them, and Python may copy them or leave them in freed memory. - Short-lived copies the upstream crates make on the stack or in CPU registers during a call, listed per algorithm.
- Copies left behind when Rust moves key state into the object.
Treat zeroization as defense in depth. It does not guarantee that secrets are gone from process memory.