ML-KEM Implicit Rejection and Re-Encryption
Break a naive CPA LWE decryptor in two chosen-ciphertext queries, then trace how FIPS 203 Fujisaki–Okamoto re-encryption blocks reaction attacks.
Prerequisite: Kyber / ML-KEM
Engineers reading FIPS 203 for the first time are often surprised that ML-KEM.Decaps runs K-PKE.Encrypt inside decryption. Why spend roughly half of decapsulation re-encrypting the message you just decrypted — and why return a plausible-looking 32-byte key instead of an error when the check fails? A two-query attack on raw LWE decryption makes both design choices concrete.
Break naive CPA decryption in two queries
Start with the modulo- public-key scheme from One Bit by Hand, where a secret key decrypts a ciphertext pair by computing and decoding bit when is closer to than to (residues ) and bit otherwise ().
Suppose a server uses a static secret key and an attacker submits two crafted, malformed ciphertexts that set — isolating — while placing right on the decision boundary at and :
| Secret | Residual at | Decoded bit () | Residual at | Decoded bit () |
|---|---|---|---|---|
s₀ = −1 | 5 (dist 3 to 8) | 1 | 6 (dist 2 to 8) | 1 |
s₀ = 0 | 4 (dist 4 to 0) | 0 | 5 (dist 3 to 8) | 1 |
s₀ = +1 | 3 (dist 3 to 0) | 0 | 4 (dist 4 to 0) | 0 |
The pair of decoded bits — (1, 1), (0, 1), or (0, 0) — uniquely identifies ! Repeating with recovers . In dimension with centered-binomial secrets in , a few thousand chosen ciphertexts whose session keys either match or mismatch the attacker's guess (a key-mismatch / reaction attack) recover the entire long-term secret key.
How Fujisaki–Okamoto re-encryption stops crafted ciphertexts
The attack worked because the decryptor blindly trusted , even though no honest execution of Encrypt could ever produce without knowing a valid randomizer vector. FIPS 203 closes that gap with the Fujisaki–Okamoto transform:
- Encapsulation (
ML-KEM.Encaps): Sample 32 random bytes . Hash together with the encapsulation-key hash using SHA3-512: . Compute ciphertext deterministically from coins , and output shared secret and ciphertext . - Decapsulation (
ML-KEM.Decaps): Decrypt . Recompute where is stored in . Re-run . - Constant-time ciphertext check: Compute fallback secret via SHAKE256 using secret rejection seed stored in . If , return ; otherwise return in constant time.
c ──▶ K-PKE.Decrypt ──▶ m′ ──▶ G(m′ ‖ H(ek)) ──▶ (K′, r′)
│
J(z ‖ c) ──▶ K̄ K-PKE.Encrypt(ek, m′, r′) ──▶ c′
└──── constant-time select (c′ == c ? K′ : K̄) ────┘When the attacker submits , K-PKE.Decrypt still produces some , which hashes to a fresh deterministic seed . Re-encrypting under produces a legitimate pseudorandom LWE vector , so with overwhelming probability (), and the attacker learns nothing about whether flipped from 0 to 1.
Why implicit rejection instead of returning an error?
Why return instead of Err(InvalidCiphertext) (explicit rejection)? Two practical reasons motivated NIST's choice:
- No explicit error side channel: In network protocols like TLS, returning an immediate alert before symmetric key derivation often creates measurable timing or error-code differences if any branch happens early. Returning a deterministic 32-byte key forces failure to occur downstream as an ordinary AEAD authentication failure, identical to a corrupted Finished MAC.
- Tighter multi-target / quantum ROM reductions: Binding secret seed and full ciphertext via SHAKE256 makes every invalid ciphertext map to an independent pseudorandom key that the attacker cannot compute on their own, simplifying QROM security proofs.
Run the reaction attack and FO defense in Python
import hashlib
q = 17
def decode_bit(res: int) -> int:
d0 = min(res % q, (-res) % q)
d1 = min((res - 8) % q, (8 - res) % q)
return 0 if d0 <= d1 else 1
def naive_decrypt(s: tuple[int, int], u: tuple[int, int], v: int) -> int:
res = (v - (u[0] * s[0] + u[1] * s[1])) % q
return decode_bit(res)
# 1. Reaction attack recovers any s in {-1, 0, +1}^2 in 4 queries:
lookup = {(1, 1): -1, (0, 1): 0, (0, 0): +1}
secret = (-1, +1)
rec_s0 = lookup[(naive_decrypt(secret, (1, 0), 4), naive_decrypt(secret, (1, 0), 5))]
rec_s1 = lookup[(naive_decrypt(secret, (0, 1), 4), naive_decrypt(secret, (0, 1), 5))]
assert (rec_s0, rec_s1) == secret
print("Recovered secret from naive CPA oracle:", (rec_s0, rec_s1))
# 2. Toy FO implicit rejection check:
def pke_encrypt(A_rows, b_pub, bit: int, r_coins: bytes) -> tuple[tuple[int, int], int]:
mask = [r_coins[i] & 1 for i in range(len(A_rows))]
u = (sum(A_rows[i][0] * mask[i] for i in range(3)) % q,
sum(A_rows[i][1] * mask[i] for i in range(3)) % q)
v = (sum(b_pub[i] * mask[i] for i in range(3)) + 8 * bit) % q
return u, v
def kem_decaps(s, A_rows, b_pub, z_secret: bytes, ct: tuple[tuple[int, int], int]) -> bytes:
m_prime = bytes([naive_decrypt(s, ct[0], ct[1])])
digest = hashlib.sha3_512(m_prime + b"ek_hash").digest()
k_prime, r_prime = digest[:32], digest[32:]
ct_check = pke_encrypt(A_rows, b_pub, m_prime[0], r_prime)
k_bar = hashlib.shake_256(z_secret + repr(ct).encode()).digest(32)
return k_prime if ct_check == ct else k_bar # implement with constant-time cmov in C/Rust!
A = [(2, 1), (4, 3), (1, 6)]
b = [(row[0] * secret[0] + row[1] * secret[1]) % q for row in A]
malformed = ((1, 0), 4)
print("FO decaps on malformed ct returns pseudorandom fallback:", kem_decaps(secret, A, b, b"z"*32, malformed).hex()[:16])Audit warning: In C, Rust, or Go, never compare with a short-circuit memcmp or branch on the boolean result! An attacker who can time where the byte comparison stops learns whether matched an expected value, reviving timing-based chosen-ciphertext attacks (such as Guo–Johansson–Nilsson style side-channel attacks). Always accumulate byte differences with bitwise OR and select between and with constant-time conditional masking. Compare wire-level integration in Reading a Hybrid TLS Handshake.