iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Post-Quantum (Kyber, Dilithium)

Post-quantum cryptography (PQC) is the set of algorithms believed to resist attack by a large-scale quantum computer. NIST standardised ML-KEM (key encapsulation) and ML-DSA (signatures) in 2024, and the migration story for most services is hybrid: classical + PQC running in parallel during the transition.

Hybrid X25519+ML-KEM key exchange via liboqs

EXAMPLE
# Requires: pip install oqs-python  (wraps Open Quantum Safe liboqs)
from oqs import KeyEncapsulation

# 1) Server publishes a PQC public key
kem = KeyEncapsulation('ML-KEM-768')
server_public = kem.generate_keypair()

# 2) Client encapsulates a shared secret to that public key
client_kem = KeyEncapsulation('ML-KEM-768')
ciphertext, client_secret = client_kem.encap_secret(server_public)

# 3) Server decapsulates to recover the same secret
server_secret = kem.decap_secret(ciphertext)

assert client_secret == server_secret
print(f'shared secret: {client_secret.hex()[:32]}... ({len(client_secret)} bytes)')

# --- Hybrid pattern: combine with X25519 so a flaw in *either* algorithm
# does not break the session ---
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes

# Each side runs both ECDH and ML-KEM, concatenates the secrets, then HKDFs.
ec_priv = X25519PrivateKey.generate()
ec_peer_pub = X25519PrivateKey.generate().public_key()  # placeholder
ec_secret = ec_priv.exchange(ec_peer_pub)

combined = ec_secret + client_secret
session_key = HKDF(algorithm=hashes.SHA256(), length=32,
                   salt=None, info=b'hybrid-v1').derive(combined)
print(f'hybrid session key: {session_key.hex()}')

Why it matters

"Harvest now, decrypt later" is the threat model that justifies migrating long-lived secrets to PQC today. Traffic an adversary captures in 2026 can be decrypted in 2035 if you protected it with only RSA or ECDH and a CRQC arrives.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// NIST standards (2024):
//   ML-KEM (Kyber) — key encapsulation.
//   ML-DSA (Dilithium) — signatures.
// Hybrid (X25519 + Kyber) is rolling out in browsers and TLS libs today.
Try it Yourself »

Discussion

Loading…