Skip to Content
oxcrypto

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_key

to_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

ModuleVariantsStandardStatus
ML-KEMML_KEM_512, ML_KEM_768, ML_KEM_1024FIPS 203Standard
X-WingX_WINGdraft-connolly-cfrg-xwing-kemExperimental (Internet-Draft)
DHKEMX25519_HKDF_SHA256RFC 9180Standard
HQCHQC_128, HQC_192, HQC_256NIST selectionExperimental (pre-release crate)
FrodoKEMFRODOKEM_*, EFRODOKEM_*ISO/IEC 18033-2 amendment draftExperimental
sntrupSNTRUP653 … SNTRUP1277draft-josefsson-ntruprime-streamlinedExperimental (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.

On this page