Documentation

CslibFrontier.Crypto.Computational.RSACRT

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 #

Main results #

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 #

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.

  • p : ℕ

    The first prime factor of the modulus.

  • q : ℕ

    The second prime factor of the modulus.

  • dp : ℕ

    The exponent used modulo p, d mod (p - 1) for a key derived from d.

  • dq : ℕ

    The exponent used modulo q, d mod (q - 1) for a key derived from d.

  • qInv : ℕ

    An inverse of q modulo p.

Step 1 of RSA-CRT inversion: x_p = y ^ d_p mod p.

Step 2 of RSA-CRT inversion: x_q = y ^ d_q mod q.

Step 3 of RSA-CRT inversion: recombine x_p and x_q as x_q + (q_inv · (x_p − x_q) mod p) · q, with x_p − x_q taken in ℤ, which is crtCombine q p x_q x_p q_inv (it computes x_p − x_q as x_p + (p − x_q mod p)).

RSA-CRT inversion ([BS23], §16.4.3, p. 701): recombine y ^ d_p mod p and y ^ d_q mod q.

theorem Cslib.Crypto.RSACRTKey.combine_mod_mod (key : RSACRTKey) (hinv : key.qInv * key.q ≡ 1 [MOD key.p]) {x : ℕ} (hx : x < key.p * key.q) :
key.combine (x % key.p) (x % key.q) = x

CRT recombination ([BS23], §16.4.3, p. 701): for an inverse q_inv of q modulo p and x < p * q, recombining x mod p and x mod q returns x.

theorem Cslib.Crypto.RSACRTKey.combine_modEq_q (key : RSACRTKey) (xp xq : ℕ) :
key.combine xp xq ≡ xq [MOD key.q]

The recombined value is congruent to x_q modulo q, for all x_p and x_q.

theorem Cslib.Crypto.RSACRTKey.combine_modEq_p (key : RSACRTKey) (hp : 0 < key.p) (hinv : key.qInv * key.q ≡ 1 [MOD key.p]) (xp xq : ℕ) :
key.combine xp xq ≡ xp [MOD key.p]

The recombined value is congruent to x_p modulo p ([BS23], §16.4.3, p. 701: "The reader can verify that indeed x ≡ x_p mod p"), for every x_p and x_q.

The RSA-CRT key of desc with a given q_inv ([BS23], §16.4.3, p. 701): d_p = d mod (p - 1) and d_q = d mod (q - 1). The results about it assume qInv * q ≡ 1 [MOD p].

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.

crtQInv is the representative ((q : ZMod p)⁻¹).val of the inverse of q in ZMod p.

crtQInv is an inverse of q modulo p.

The RSA-CRT key of desc ([BS23], §16.4.3, p. 701), with q_inv = crtQInv.

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.

theorem Cslib.Crypto.RSADesc.powP_crtKeyWith (desc : RSADesc) (hp2 : desc.p ≠ 2) (qInv y : ℕ) :
(desc.crtKeyWith qInv).powP y = y ^ desc.d % desc.p

Step 1 of RSA-CRT inversion computes y ^ d mod p ([BS23], §16.4.3, p. 701, "by definition of d_p"). The hypothesis p ≠ 2 is not in the book; it is needed because d_p = d mod 1 = 0 when p = 2.

theorem Cslib.Crypto.RSADesc.powQ_crtKeyWith (desc : RSADesc) (hq2 : desc.q ≠ 2) (qInv y : ℕ) :
(desc.crtKeyWith qInv).powQ y = y ^ desc.d % desc.q

Step 2 of RSA-CRT inversion computes y ^ d mod q ([BS23], §16.4.3, p. 701, "by definition of d_q"). The hypothesis q ≠ 2 is not in the book; it is needed because d_q = d mod 1 = 0 when q = 2.

theorem Cslib.Crypto.RSADesc.invert_crtKeyWith (desc : RSADesc) (hp2 : desc.p ≠ 2) (hq2 : desc.q ≠ 2) {qInv : ℕ} (hinv : qInv * desc.q ≡ 1 [MOD desc.p]) (y : ℕ) :
(desc.crtKeyWith qInv).invert y = rsaInvert desc.modulus desc.d y

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).

theorem Cslib.Crypto.RSADesc.invert_crtKey (desc : RSADesc) (hp2 : desc.p ≠ 2) (hq2 : desc.q ≠ 2) (y : ℕ) :
desc.crtKey.invert y = rsaInvert desc.modulus desc.d y

Correctness of RSA-CRT inversion with the key crtKey: for odd p and q, it equals y ^ d mod n for every y.

theorem Cslib.Crypto.RSADesc.gcd_val_pow_sub_eq_q (desc : RSADesc) {σ h : ℕ} (hσq : σ ≡ h ^ desc.d [MOD desc.q]) (hσp : ¬σ ≡ h ^ desc.d [MOD desc.p]) :
(↑σ ^ desc.e - ↑h).val.gcd desc.modulus = desc.q

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).

theorem Cslib.Crypto.RSADesc.gcd_val_pow_sub_combine_eq_q (desc : RSADesc) {qInv : ℕ} (hinv : qInv * desc.q ≡ 1 [MOD desc.p]) {h xp xq : ℕ} (hxq : xq ≡ h ^ desc.d [MOD desc.q]) (hxp : ¬xp ≡ h ^ desc.d [MOD desc.p]) :
(↑((desc.crtKeyWith qInv).combine xp xq) ^ desc.e - ↑h).val.gcd desc.modulus = desc.q

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.