Skip to content
Broken by quantumelgamal

ElGamal

A discrete-logarithm public-key encryption scheme, mostly seen in older OpenPGP encryption subkeys.

Why it's at risk

ElGamal rests on the same discrete logarithm problem as Diffie-Hellman, which Shor's algorithm solves. Data encrypted with it today (for example old PGP messages) can be decrypted later, so stored ciphertexts are a harvest-now-decrypt-later concern.

Used for
Public-key encryption
Family
Finite field or elliptic curve (discrete logarithm)
Classical security
112 bits
Against a quantum computer
Broken
Standards
RFC 4880 (OpenPGP, legacy), RFC 9580 (OpenPGP)
Read the guideHow public-key cryptography works

Migration guides

Concrete code changes to move off ElGamal. Pick your stack.

node:crypto · javascriptNode.js: replace RSA/ECDH key wrapping with ML-KEM-768 using node:crypto (Node 24.7+)Production-ready

Why

If your application encrypts data to someone's public key (RSA-OAEP key wrapping, ECIES, "sealed boxes", ECDH followed by AES), anyone who stores the ciphertext can decrypt it once a large quantum computer exists. ML-KEM (FIPS 203) is the standard replacement: it is a key encapsulation mechanism. Instead of encrypting your data key with the public key, you ask ML-KEM for a fresh 32-byte shared secret plus a ciphertext that only the private-key holder can open. You then derive a symmetric key from that secret and encrypt the data with AES-256-GCM.

Node.js has native, OpenSSL-backed ML-KEM in node:crypto: key generation since 24.7.0, plus crypto.encapsulate() / crypto.decapsulate() added in 24.7.0. It requires a Node built with OpenSSL 3.5 or later (all official Node 24 and 26 builds). Node 22 does not have these APIs; use the @noble/post-quantum guide there.

Steps

  1. Require Node 24.7+ (check node -v) and OpenSSL 3.5+ (process.versions.openssl).
  2. Generate an ML-KEM-768 key pair for each recipient; publish the public key (SPKI DER or PEM).
  3. Sender: encapsulate(publicKey) gives { sharedKey, ciphertext }. Derive an AES key from sharedKey with HKDF and encrypt the payload with AES-256-GCM. Send ciphertext, the IV, the GCM tag and the encrypted payload.
  4. Recipient: decapsulate(privateKey, ciphertext) gives the same sharedKey; derive the same AES key and decrypt.
  5. During the transition, consider a hybrid: also run X25519 and feed both secrets into HKDF (see the @noble/post-quantum guide for a worked example), so security never drops below today's.

Before (RSA-OAEP key wrapping)

js
import { generateKeyPairSync, publicEncrypt, privateDecrypt, randomBytes, constants } from 'node:crypto';

const { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 });
const dataKey = randomBytes(32);
const wrapped = publicEncrypt({ key: publicKey, padding: constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, dataKey);
const unwrapped = privateDecrypt({ key: privateKey, padding: constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, wrapped);

After (ML-KEM-768 + HKDF + AES-256-GCM)

js
import {
  generateKeyPairSync, encapsulate, decapsulate, hkdfSync,
  createCipheriv, createDecipheriv, randomBytes,
} from 'node:crypto';

// Recipient, once: generate and publish the public key.
const { publicKey, privateKey } = generateKeyPairSync('ml-kem-768');
const publicPem = publicKey.export({ type: 'spki', format: 'pem' });

// Sender
function seal(recipientPublicKey, plaintext, aad = Buffer.alloc(0)) {
  const { sharedKey, ciphertext: kemCt } = encapsulate(recipientPublicKey);
  const key = Buffer.from(hkdfSync('sha256', sharedKey, Buffer.alloc(0), 'myapp/seal/v1', 32));
  const iv = randomBytes(12);
  const c = createCipheriv('aes-256-gcm', key, iv);
  c.setAAD(aad);
  const body = Buffer.concat([c.update(plaintext), c.final()]);
  return { kemCt, iv, tag: c.getAuthTag(), body };
}

// Recipient
function open(privateKey, { kemCt, iv, tag, body }, aad = Buffer.alloc(0)) {
  const sharedKey = decapsulate(privateKey, kemCt);
  const key = Buffer.from(hkdfSync('sha256', sharedKey, Buffer.alloc(0), 'myapp/seal/v1', 32));
  const d = createDecipheriv('aes-256-gcm', key, iv);
  d.setAAD(aad);
  d.setAuthTag(tag);
  return Buffer.concat([d.update(body), d.final()]);
}

Gotchas

  • Sizes. ML-KEM-768 encapsulation (public) key: 1,184 bytes. Ciphertext: 1,088 bytes (versus 256 for RSA-2048 OAEP). Shared secret: 32 bytes. Check database columns, message size limits, QR codes and URL parameters.
  • A KEM is not "encrypt with the public key". You cannot choose the secret; you get one. Wrap your existing data keys by encrypting them with the KEM-derived key if you need to keep them.
  • Decapsulation never throws on a wrong ciphertext. ML-KEM uses implicit rejection: a tampered or mismatched ciphertext yields a random-looking secret, so the failure shows up as an AES-GCM authentication error. That is by design.
  • Bind context. Put a protocol label (and ideally the recipient key ID) in the HKDF info or the AES-GCM AAD so ciphertexts cannot be replayed into another context.
  • Private key storage. privateKey.export({ type: 'pkcs8', format: 'pem' }) works. Node 26.0+ also supports a 64-byte raw-seed export and writes seed-only PKCS#8 by default when a seed is available; Node 24 does not have raw-seed. Most HSMs and cloud KMSs are still adding ML-KEM; check before designing around them.
  • Runtime mismatch. Code that must also run on Node 22, Deno, Bun or browsers should use @noble/post-quantum or WebCrypto where available.

Verify

js
import assert from 'node:assert';
const msg = Buffer.from('hello');
assert.deepEqual(open(privateKey, seal(publicKey, msg)), msg);
console.log(encapsulate(publicKey).ciphertext.length); // 1088

Run npx -y pqe-cli scan . afterwards: remaining publicEncrypt/RSA-OAEP call sites are listed as findings.

@noble/post-quantum · typescriptJavaScript/TypeScript (any runtime): hybrid X25519 + ML-KEM-768 with @noble/post-quantumExperimental

Why

@noble/post-quantum is a small pure-JavaScript implementation of ML-KEM (FIPS 203), ML-DSA (FIPS 204) and SLH-DSA (FIPS 205). It runs anywhere JavaScript runs: Node 20/22, Deno, Bun, browsers and edge workers, which makes it the easiest option when you cannot rely on Node 24's native node:crypto support.

During the transition the recommended pattern is hybrid: combine a classical X25519 exchange with ML-KEM-768 so the result is secure if either holds. You can use the library's ready-made X-Wing construction, or build the combiner yourself with HKDF.

Maturity: the library is widely used and carefully written, but its README states it has not been independently audited. Prefer an OpenSSL/AWS-LC-backed API (Node 24.7+ node:crypto, WebCrypto where available) for high-assurance server code.

Steps

  1. npm install @noble/post-quantum @noble/curves @noble/hashes
  2. Decide between X-Wing (option A, a fixed, specified combiner; recommended) and a custom HKDF combiner (option B, if you must interoperate with an existing protocol).
  3. Store the recipient's private key as securely as any other private key; publish its public key.
  4. Use the 32-byte shared secret only as input to a KDF, then encrypt with AES-256-GCM or ChaCha20-Poly1305.

Before (X25519 / ECIES-style)

ts
import { x25519 } from '@noble/curves/ed25519.js';

const recipient = x25519.keygen();
const eph = x25519.keygen();
const shared = x25519.getSharedSecret(eph.secretKey, recipient.publicKey); // quantum-vulnerable

After, option A: X-Wing (X25519 + ML-KEM-768)

ts
import { XWing } from '@noble/post-quantum/hybrid.js';

const recipient = XWing.keygen();                 // publicKey: 1216 bytes
const { cipherText, sharedSecret } = XWing.encapsulate(recipient.publicKey); // cipherText: 1120 bytes
const recovered = XWing.decapsulate(cipherText, recipient.secretKey);
// sharedSecret and recovered are equal 32-byte keys

X-Wing is specified in the IETF CFRG draft draft-connolly-cfrg-xwing-kem. Record the draft/library version you deployed so the other side can match it.

After, option B: explicit HKDF combiner

ts
import { ml_kem768 } from '@noble/post-quantum/ml-kem.js';
import { x25519 } from '@noble/curves/ed25519.js';
import { hkdf } from '@noble/hashes/hkdf.js';
import { sha256 } from '@noble/hashes/sha2.js';
import { concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';

// Recipient long-term keys
const pq = ml_kem768.keygen();   // publicKey 1184 B, secretKey 2400 B
const ec = x25519.keygen();

// Sender
const { cipherText, sharedSecret: ssPq } = ml_kem768.encapsulate(pq.publicKey);
const eph = x25519.keygen();
const ssEc = x25519.getSharedSecret(eph.secretKey, ec.publicKey);
// Bind every public value into the derivation (domain separation + transcript)
const info = concatBytes(utf8ToBytes('myapp/hybrid-kem/v1'), eph.publicKey, ec.publicKey, cipherText, pq.publicKey);
const key = hkdf(sha256, concatBytes(ssPq, ssEc), undefined, info, 32);
// send: eph.publicKey (32 B) + cipherText (1088 B)

// Recipient
const ssPq2 = ml_kem768.decapsulate(cipherText, pq.secretKey);
const ssEc2 = x25519.getSharedSecret(ec.secretKey, eph.publicKey);
const key2 = hkdf(sha256, concatBytes(ssPq2, ssEc2), undefined, info, 32);

Gotchas

  • API versions. Code above is for @noble/post-quantum 0.5.x and @noble/curves/@noble/hashes 2.x (note the .js subpath imports). Argument orders and names have changed between major releases; check the README of the version you install.
  • Do not just XOR or concatenate secrets without a KDF, and include the ciphertext and public keys in the KDF input. Hand-made combiners are a classic source of subtle bugs, which is why option A exists.
  • Implicit rejection. decapsulate returns a pseudo-random secret instead of throwing on a bad ciphertext; you detect tampering when the AEAD fails.
  • Sizes. Hybrid ciphertext ≈ 1.1 KB and public key ≈ 1.2 KB, versus 32 bytes for X25519 alone.
  • Constant time. JavaScript engines give weaker side-channel guarantees than native code. That is fine for most client-side and server-side uses, but HSM-grade requirements need a native or hardware implementation.
  • Pre-standard Kyber is not ML-KEM. Libraries or data created with "Kyber768" round-3 code are not interoperable with FIPS 203 ML-KEM.

Verify

ts
console.assert(Buffer.from(key).equals(Buffer.from(key2)));
console.assert(cipherText.length === 1088 && pq.publicKey.length === 1184);
cryptography (pyca) · pythonPython: replace RSA-OAEP / ECDH key wrapping with ML-KEM-768 in pyca/cryptography (48+)Production-ready

Why

Data encrypted to an RSA or elliptic-curve public key can be stored now and decrypted once a large quantum computer exists. ML-KEM (FIPS 203) replaces that step. cryptography (pyca) added an ML-KEM module in 47.0.0 (AWS-LC and BoringSSL backends) and extended it to the regular OpenSSL backend (OpenSSL 3.5.0 or later) in 48.0.0. The PyPI wheels bundle a recent OpenSSL, so pip install "cryptography>=48" gives you ML-KEM-768 and ML-KEM-1024 on Linux, macOS and Windows.

Steps

  1. pip install -U "cryptography>=48" and pin it.
  2. Recipient: MLKEM768PrivateKey.generate(); publish public_key().public_bytes_raw() (1,184 bytes) or the SPKI encoding.
  3. Sender: shared_secret, ciphertext = public_key.encapsulate(); derive an AES key with HKDF; encrypt with AES-256-GCM.
  4. Recipient: private_key.decapsulate(ciphertext) gives the same secret.
  5. For a hybrid (recommended during the transition), also do an X25519 exchange and feed both secrets into HKDF, as below.

Before

python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
import os

priv = rsa.generate_private_key(public_exponent=65537, key_size=2048)
data_key = os.urandom(32)
oaep = padding.OAEP(mgf=padding.MGF1(hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
wrapped = priv.public_key().encrypt(data_key, oaep)
assert priv.decrypt(wrapped, oaep) == data_key

After (hybrid ML-KEM-768 + X25519 → HKDF → AES-256-GCM)

python
import os
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.mlkem import MLKEM768PrivateKey
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF

# Recipient key pairs (store the private keys like any other secret)
pq_priv = MLKEM768PrivateKey.generate()
ec_priv = X25519PrivateKey.generate()
pq_pub, ec_pub = pq_priv.public_key(), ec_priv.public_key()

def derive(ss_pq: bytes, ss_ec: bytes, transcript: bytes) -> bytes:
    return HKDF(algorithm=hashes.SHA256(), length=32, salt=None,
                info=b"myapp/hybrid-seal/v1" + transcript).derive(ss_pq + ss_ec)

def seal(plaintext: bytes, aad: bytes = b"") -> dict:
    ss_pq, kem_ct = pq_pub.encapsulate()                     # (shared_secret, ciphertext)
    eph = X25519PrivateKey.generate()
    eph_pub = eph.public_key().public_bytes_raw()
    ss_ec = eph.exchange(ec_pub)
    key = derive(ss_pq, ss_ec, eph_pub + kem_ct)
    nonce = os.urandom(12)
    return {"kem_ct": kem_ct, "eph_pub": eph_pub, "nonce": nonce,
            "body": AESGCM(key).encrypt(nonce, plaintext, aad)}

def open_(msg: dict, aad: bytes = b"") -> bytes:
    ss_pq = pq_priv.decapsulate(msg["kem_ct"])
    ss_ec = ec_priv.exchange(X25519PublicKey.from_public_bytes(msg["eph_pub"]))
    key = derive(ss_pq, ss_ec, msg["eph_pub"] + msg["kem_ct"])
    return AESGCM(key).decrypt(msg["nonce"], msg["body"], aad)

assert open_(seal(b"hello")) == b"hello"

Gotchas

  • Return order. encapsulate() returns (shared_secret, ciphertext); mixing them up gives silently wrong keys.
  • Implicit rejection. decapsulate() does not raise on a tampered ciphertext; you find out when AES-GCM raises InvalidTag.
  • Sizes. ML-KEM-768 ciphertext 1,088 bytes, public key 1,184 bytes; ML-KEM-1024: 1,568 and 1,568. Private keys serialise as a 64-byte seed (private_bytes_raw()).
  • Backend support. If your cryptography is built against a system OpenSSL older than 3.5 (some distro packages), generate() raises UnsupportedAlgorithm. Check in CI.
  • Hazmat. These are low-level primitives; the HKDF info label and transcript binding above matter. Prefer a reviewed higher-level protocol (HPKE, TLS) when one fits.
  • No ML-KEM-512 class in this module; use 768 (recommended) or 1024 (CNSA 2.0).

Verify

python
from cryptography.hazmat.primitives.asymmetric.mlkem import MLKEM768PrivateKey
k = MLKEM768PrivateKey.generate()
ss, ct = k.public_key().encapsulate()
assert k.decapsulate(ct) == ss and len(ct) == 1088