Key establishment¶
Read the implementations in Python and C.
X25519 and HKDF-SHA-256¶
Alice and Bob each generate a key pair. Each combines a private key with the
other party's public key, then passes the resulting shared secret through
HKDF-SHA-256. The example derives 32 bytes, uses the default all-zero salt,
and binds the result to the fixed ASCII application label pce-x25519-v1.
mkdir -p lab
build/pce keygen x25519 lab/alice.pem lab/alice.pub
uv run python examples/pce.py keygen x25519 lab/bob.pem lab/bob.pub
build/pce derive x25519 lab/alice.pem lab/bob.pub lab/alice.key
uv run python examples/pce.py derive x25519 lab/bob.pem lab/alice.pub lab/bob.key
cmp lab/alice.key lab/bob.key
ML-KEM-768¶
For the post-quantum example, only the recipient creates a long-term key pair. A sender uses the recipient's public key to generate a new 32-byte secret and an encapsulation (the KEM ciphertext). The sender transmits the 1,088-byte ciphertext and keeps the secret. The recipient uses the private key to recover the same secret.
mkdir -p lab
uv run python examples/pce.py keygen mlkem768 lab/recipient.pem lab/recipient.pub
build/pce encapsulate mlkem768 lab/recipient.pub lab/kem.ct lab/sender.secret
uv run python examples/pce.py decapsulate mlkem768 lab/recipient.pem lab/kem.ct lab/recipient.secret
cmp lab/sender.secret lab/recipient.secret
ML-KEM uses implicit rejection. Changing a correctly sized ciphertext can produce a different secret without a decapsulation error. A protocol needs key confirmation or authenticated use of the resulting key. Neither example authenticates its peer. For an application connection, use an established authenticated protocol that binds identities, negotiated parameters, and the exchange transcript. The two examples here are independent algorithms.
ML-KEM is standardized in FIPS 203. See also NIST SP 800-227, RFC 7748, and RFC 5869.