RSA-CRT inversion and the gcd identity behind its fault attack #
RSA-CRT computes the RSA inverse y ^ d mod n from the private key (p, q, d_p, d_q, q_inv):
it computes x_p = y ^ d_p mod p and x_q = y ^ d_q mod q and recombines them by the Chinese
Remainder Theorem as x_q + (q_inv · (x_p − x_q) mod p) · q. If x_q is correct and x_p is
not, the recombined output σ satisfies gcd(σ ^ e − h, n) = q, where h is the input and
σ ^ e − h is computed in ℤ_n and read in [0, n) ([BS23], §16.4.3, pp. 701–702).
Main definitions #
Cslib.Crypto.RSACRTKey: the RSA-CRT private key(p, q, d_p, d_q, q_inv).Cslib.Crypto.RSACRTKey.powP,Cslib.Crypto.RSACRTKey.powQ,Cslib.Crypto.RSACRTKey.combine,Cslib.Crypto.RSACRTKey.invert: the three steps of RSA-CRT inversion and their composite.Cslib.Crypto.RSADesc.crtKeyWith: the key derived from anRSADesc, withd_p = d mod (p - 1),d_q = d mod (q - 1)and a givenq_inv.Cslib.Crypto.RSADesc.crtQInv,Cslib.Crypto.RSADesc.crtKey: the inverse ofqmodulopcomputed by the extended Euclidean algorithmTuring.MultiTapeTM.BinaryGcd.modInv, and the key that uses it.crtKeyis not exposed;Cslib.Crypto.RSADesc.crtKey_eq_crtKeyWithrewrites it tocrtKeyWithwithq_inv = crtQInv.
Main results #
Cslib.Crypto.RSADesc.crtQInv_eq_zmod_inv_val,Cslib.Crypto.RSADesc.crtQInv_mul_modEq_one:crtQInvis the representative((q : ZMod p)⁻¹).valofq⁻¹inZMod p, and an inverse ofqmodulop.Cslib.Crypto.RSACRTKey.combine_mod_mod,Cslib.Crypto.RSACRTKey.combine_modEq_p,Cslib.Crypto.RSACRTKey.combine_modEq_q: recombination returnsx < p * qfrom its residues, and its output is congruent tox_pmodulopand tox_qmoduloq.Cslib.Crypto.RSADesc.powP_crtKeyWith,Cslib.Crypto.RSADesc.powQ_crtKeyWith: for oddp(respectivelyq), the first two steps computey ^ d mod pandy ^ d mod q.Cslib.Crypto.RSADesc.invert_crtKeyWith,Cslib.Crypto.RSADesc.invert_crtKey: for oddpandq, RSA-CRT inversion equalsrsaInvert n don every input.Cslib.Crypto.RSADesc.gcd_val_pow_sub_eq_q: an outputσthat agrees withh ^ dmoduloqbut not modulopgivesgcd(σ ^ e − h, n) = q.Cslib.Crypto.RSADesc.gcd_val_pow_sub_combine_eq_q: the gcd identity behind the fault attack: recombining a correctx_qwith anx_p'that is not congruent toh ^ dmodulopgives an outputσwithgcd(σ ^ e − h, n) = q.Cslib.Crypto.computableInPolyTime_crtInvert: RSA-CRT inversion is computable in polynomial time from polynomial-time key components and input, as natural numbers onencNat, whenpandqare positive.Cslib.Crypto.computableInPolyTime_crtKeyWith_invert: the same for the keyRSADesc.crtKeyWith desc qInv, from polynomial-timep,q,d,q_invand input.Cslib.Crypto.computableInPolyTime_crtQInv,Cslib.Crypto.computableInPolyTime_crtKey_invert:crtQInvis computable in polynomial time from polynomial-timepandq, and RSA-CRT inversion with the keyRSADesc.crtKeyfrom polynomial-timep,q,dand input.Cslib.Crypto.computableInPolyTime_gcd_val_pow_sub: the gcd step of the fault attack,gcd(σ ^ e − h, n)withσ ^ e − hread in[0, n), is computable in polynomial time whennis positive.
Implementation notes #
Residues are natural numbers. The book's σ̂ ^ e − H(m) ∈ ℤ_n, read as an integer in [0, n), is
((σ : ZMod n) ^ e - h).val, and an arbitrary natural number h takes the place of the hash
value H(m). The theorems hold for every q_inv with q_inv * q ≡ 1 [MOD p];
RSADesc.crtKey uses RSADesc.crtQInv = BinaryGcd.modInv q p from
CslibFrontier/Computability/Machines/Turing/MultiTape/Complexity/PolyTime/Nat/Gcd.lean, which
RSADesc.crtQInv_eq_zmod_inv_val identifies with the inverse in ZMod p. The definition is not
exposed and that import is proof-only: clients compute crtQInv through that theorem. The book
also assumes p > q; no result here needs it.
Recombination is crtCombine q p x_q x_p q_inv from
CslibFrontier/Foundations/Data/Nat/ChineseRemainder.lean; its congruences and correctness come
from crtCombine_modEq_left, crtCombine_modEq_right and crtCombine_eq.
Correctness of RSA-CRT needs p and q odd: RSADesc admits p = 2, where
d_p = d mod 1 = 0, so step 1 returns 1 for every y, which is wrong for even y. The tests
check the counterexample p = 2, q = 5, e = d = 3, y = 2, where RSA-CRT returns 3 and
y ^ d mod n = 8. The fault-attack identity does not need oddness: its hypothesis is that x_q
is congruent to the correct value h ^ d modulo q.
The polynomial-time certificates take their arguments as component functions α → ℕ on a
common input encoding, each computable in polynomial time to encNat, in the style of
computableInPolyTime_rsaInvert. In computableInPolyTime_crtInvert and
computableInPolyTime_crtKeyWith_invert the key component q_inv is such an argument, as in the
book, where it is part of the precomputed key. computableInPolyTime_crtQInv computes crtQInv
from p and q with the certificate ComputableInPolyTime.binary_modInv' of Gcd.lean, and
computableInPolyTime_crtKey_invert composes it with computableInPolyTime_crtKeyWith_invert.
Not formalized: the book's estimate that RSA-CRT is about four times faster, and RSA-CRT as the
signer of a signature scheme (Cslib.Crypto.rsaSignature signs with rsaInvert).
The certificates cover the computations; nothing is proved about an attacker as an adversary in a
security game.
References #
- D. Boneh, V. Shoup, A Graduate Course in Applied Cryptography, Version 0.6, §16.4.3, pp. 701–702.
An RSA-CRT private key (p, q, d_p, d_q, q_inv) ([BS23], §16.4.3, p. 701). The
structure carries no conditions: RSADesc.crtKeyWith derives the key of an RSA key, and each
theorem states the hypotheses it uses.
The inverse of q modulo p, computed by the extended Euclidean algorithm
Turing.MultiTapeTM.BinaryGcd.modInv q p. Its value is given by crtQInv_eq_zmod_inv_val.
crtKey is crtKeyWith with q_inv = crtQInv. The definition of crtKey is not exposed;
this equation lets clients apply the results about crtKeyWith to it.
Correctness of RSA-CRT inversion ([BS23], §16.4.3, p. 701): for odd primes p
and q and any inverse q_inv of q modulo p, RSA-CRT inversion equals y ^ d mod n for
every y. The book does not state that p and q are odd; without it the statement fails (see
the module documentation).
The gcd step of the fault attack ([BS23], §16.4.3, p. 702): if σ agrees with
h ^ d modulo q but not modulo p, then q is the gcd of n and the representative in
[0, n) of σ ^ e − h ∈ ℤ_n. Any natural number h stands in for the hash value H(m).
The gcd identity behind the fault attack on RSA-CRT ([BS23], §16.4.3,
pp. 701–702): recombine any x_q ≡ h ^ d (mod q), such as the correct x_q = h ^ d mod q, with
any x_p' that is not congruent to h ^ d modulo p (a faulty step 1). The output σ
satisfies gcd(σ ^ e − h, n) = q, with σ ^ e − h ∈ ℤ_n read in [0, n). Any natural number
h stands in for the hash value H(m), and q_inv is any inverse of q modulo p. For a
residue x_p' < p the hypothesis says x_p' ≠ h ^ d mod p, as in the book;
RSADesc.powQ_crtKeyWith shows that step 2 computes x_q = h ^ d mod q when q is odd.
Polynomial-time certificates #
RSA-CRT inversion ([BS23], §16.4.3, p. 701) is computable in polynomial time
whenever the key components p, q, d_p, d_q, q_inv and the input y are computable in
polynomial time as natural numbers on encNat, and 0 < p a and 0 < q a. The positivity
hypotheses are those of binary_powMod: for p a = 0, step 1 would compute
y ^ d_p % 0 = y ^ d_p, whose length is exponential in that of d_p.
RSA-CRT inversion with the key RSADesc.crtKeyWith desc qInv is computable in polynomial
time whenever p, q, d, q_inv and the input are computable in polynomial time as natural
numbers on encNat; p and q are positive because they are prime. Here q_inv is an input,
as in the book, where it is part of the precomputed key; computableInPolyTime_crtKey_invert
computes it as RSADesc.crtQInv.
The inverse RSADesc.crtQInv of q modulo p is computable in polynomial time whenever
p and q are computable in polynomial time as natural numbers on encNat. It is
BinaryGcd.modInv q p, certified by ComputableInPolyTime.binary_modInv'.
RSA-CRT inversion with the key RSADesc.crtKey ([BS23], §16.4.3, p. 701) is
computable in polynomial time whenever p, q, d and the input are computable in polynomial
time as natural numbers on encNat: the key component q_inv = RSADesc.crtQInv is computed
from p and q by computableInPolyTime_crtQInv.
The gcd step of the fault attack on RSA-CRT ([BS23], §16.4.3, p. 702),
(n, e, σ, h) ↦ gcd(σ ^ e − h, n) with σ ^ e − h ∈ ℤ_n read in [0, n) as in
RSADesc.gcd_val_pow_sub_combine_eq_q, is computable in polynomial time whenever n, e, σ
and h are computable in polynomial time as natural numbers on encNat, and 0 < n a. The
positivity hypothesis is that of binary_powMod: ZMod 0 is ℤ, where σ ^ e − h is not
reduced. This certifies the computation; nothing is proved about an attacker.