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-q=17q = 17 public-key scheme from One Bit by Hand, where a secret key s=(s0,s1)∈{−1,0,+1}2s = (s_0, s_1) \in \{-1, 0, +1\}^2 decrypts a ciphertext pair (u,v)(u, v) by computing w=(v−u⋅s) mod 17w = (v - u \cdot s) \bmod 17 and decoding bit 00 when ww is closer to 00 than to 88 (residues {13,14,15,16,0,1,2,3,4}\{13, 14, 15, 16, 0, 1, 2, 3, 4\}) and bit 11 otherwise ({5,6,7,8,9,10,11,12}\{5, 6, 7, 8, 9, 10, 11, 12\}).

Suppose a server uses a static secret key ss and an attacker submits two crafted, malformed ciphertexts that set u=(1,0)u = (1, 0) — isolating u⋅s=s0u \cdot s = s_0 — while placing vv right on the decision boundary at v=4v = 4 and v=5v = 5:

Secret s0s_0Residual at v=4v = 4Decoded bit (v=4v=4)Residual at v=5v = 5Decoded bit (v=5v=5)
s₀ = −15 (dist 3 to 8)16 (dist 2 to 8)1
s₀ = 04 (dist 4 to 0)05 (dist 3 to 8)1
s₀ = +13 (dist 3 to 0)04 (dist 4 to 0)0

The pair of decoded bits — (1, 1), (0, 1), or (0, 0) — uniquely identifies s0∈{−1,0,+1}s_0 \in \{-1, 0, +1\}! Repeating with u=(0,1)u = (0, 1) recovers s1s_1. In dimension n=768n = 768 with centered-binomial secrets in {−2,…,2}\{-2, \dots, 2\}, 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 u=(1,0)u = (1, 0), even though no honest execution of Encrypt could ever produce (1,0)(1, 0) without knowing a valid randomizer vector. FIPS 203 closes that gap with the Fujisaki–Okamoto transform:

  1. Encapsulation (ML-KEM.Encaps): Sample 32 random bytes mm. Hash mm together with the encapsulation-key hash H(ek)H(\mathsf{ek}) using SHA3-512: (K,r)=G(m∥H(ek))(K, r) = G(m \parallel H(\mathsf{ek})). Compute ciphertext c=K-PKE.Encrypt(ek,m,r)c = \mathsf{K\text{-}PKE.Encrypt}(\mathsf{ek}, m, r) deterministically from coins rr, and output shared secret KK and ciphertext cc.
  2. Decapsulation (ML-KEM.Decaps): Decrypt m′=K-PKE.Decrypt(dkPKE,c)m' = \mathsf{K\text{-}PKE.Decrypt}(\mathsf{dk}_{\mathrm{PKE}}, c). Recompute (K′,r′)=G(m′∥h)(K', r') = G(m' \parallel h) where h=H(ek)h = H(\mathsf{ek}) is stored in dk\mathsf{dk}. Re-run c′=K-PKE.Encrypt(ek,m′,r′)c' = \mathsf{K\text{-}PKE.Encrypt}(\mathsf{ek}, m', r').
  3. Constant-time ciphertext check: Compute fallback secret Kˉ=J(z∥c)\bar{K} = J(z \parallel c) via SHAKE256 using secret rejection seed zz stored in dk\mathsf{dk}. If c′=cc' = c, return K′K'; otherwise return Kˉ\bar{K} 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 u=(1,0),v=4u = (1, 0), v = 4, K-PKE.Decrypt still produces some m′m', which hashes to a fresh deterministic seed r′r'. Re-encrypting under r′r' produces a legitimate pseudorandom LWE vector u′≠(1,0)u' \neq (1, 0), so c′≠cc' \neq c with overwhelming probability (1−2−2561 - 2^{-256}), and the attacker learns nothing about whether m′m' flipped from 0 to 1.

Why implicit rejection instead of returning an error?

Why return Kˉ=J(z∥c)\bar{K} = J(z \parallel c) instead of Err(InvalidCiphertext) (explicit rejection)? Two practical reasons motivated NIST's choice:

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 c′==cc' == c with a short-circuit memcmp or branch on the boolean result! An attacker who can time where the byte comparison stops learns whether m′m' 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 K′K' and Kˉ\bar{K} with constant-time conditional masking. Compare wire-level integration in Reading a Hybrid TLS Handshake.