Documentation

CslibFrontier.Crypto.Computational.RSAAttacks

Attacks on plain RSA, factoring from RSA secrets, and the RSA-to-factoring hierarchy #

Algebraic attacks on plain (textbook) RSA encryption c = m ^ e % N (rsaEval), the elementary steps that recover the factorization of N from secret RSA data, and the polynomial-time reductions connecting RSAOneWay, RSAKeyRecoveryHard, TotientHard, and ModulusFactoringHard.

Main definitions #

Main results #

Implementation notes #

The common-modulus attack is the program commonRoot of RSACommonRoot.lean on the two ciphertexts, so computableInPolyTime_commonRoot is its polynomial-time certificate; its correctness statement requires coprime exponents and m coprime to N.

The deterministic polynomial-time reductions TotientHard gen ↔ ModulusFactoringHard (fun n => (gen n).map RSADesc.modulus) ([KL20], Theorem 9.49) and RSAOneWay gen → RSAKeyRecoveryHard gen → TotientHard gen are proved in full. The two statements on factoring from (N, e, d) are the deterministic ingredients of the reverse direction ModulusFactoringHard → RSAKeyRecoveryHard ([KL20], Theorem 9.50 and Corollary 9.51, Section 9.2.5), which also needs the probability bound that a random unit leads to a nontrivial square root of 1; that probabilistic algorithm and its analysis are not formalized here, so Theorem 9.50 and Corollary 9.51 themselves are not claimed. For x ^ 2 ≡ y ^ 2 [MOD N] ([BS23], Section 16.4.1, p. 697), only the deterministic gcd-factoring conclusion is formalized; the heuristic probability over random x, y is excluded.

References #

Malleability #

theorem Cslib.Crypto.rsaEval_mul (N e m s : ℕ) :
rsaEval N e (m * s) = rsaEval N e m * rsaEval N e s % N

Plain RSA is multiplicatively homomorphic: the encryption of a product is the product of the encryptions, reduced modulo N.

theorem Cslib.Crypto.rsaInvert_rsaEval_mul (desc : RSADesc) (m s : ℕ) :
rsaInvert desc.modulus desc.d (rsaEval desc.modulus desc.e m * rsaEval desc.modulus desc.e s % desc.modulus) = m * s % desc.modulus

Malleability of plain RSA: multiplying a ciphertext of m by the encryption of any s gives a ciphertext that decrypts to m * s % N.

Common modulus #

theorem Cslib.Crypto.commonRoot_rsaEval {N e₁ e₂ m : ℕ} (he : e₁.Coprime e₂) (hm : m.Coprime N) :
commonRoot N e₂ e₁ (rsaEval N e₁ m) (rsaEval N e₂ m) = m % N

The common-modulus attack on plain RSA: from the encryptions c₁ = rsaEval N e₁ m and c₂ = rsaEval N e₂ m of one message m under the same modulus N and coprime exponents e₁, e₂, Shamir's trick commonRoot N e₂ e₁ c₁ c₂ recovers m % N whenever m is coprime to N. The attack is therefore the program commonRoot on the ciphertexts, and computableInPolyTime_commonRoot at (N, e₂, e₁, c₁, c₂) is its polynomial-time certificate.

Small messages and the broadcast attack #

theorem Cslib.Crypto.rsaEval_eq_pow_of_pow_lt {N e m : ℕ} (hsmall : m ^ e < N) :
rsaEval N e m = m ^ e

For a short message, m ^ e < N, plain RSA performs no modular reduction: the ciphertext is m ^ e over the integers.

theorem Cslib.Crypto.eq_of_pow_eq_rsaEval {N e m m' : ℕ} (he : e ≠ 0) (hsmall : m ^ e < N) (hroot : m' ^ e = rsaEval N e m) :
m' = m

A short message is the unique integer e-th root of its ciphertext, so it is recovered by an integer root computation.

theorem Cslib.Crypto.chineseRemainderOfList_rsaEval_eq_pow {ι : Type u_1} {N : ι → ℕ} {e m : ℕ} (l : List ι) (co : List.Pairwise (Function.onFun Nat.Coprime N) l) (hN : ∀ i ∈ l, N i ≠ 0) (hsmall : m ^ e < (List.map N l).prod) :
↑(Nat.chineseRemainderOfList (fun (i : ι) => rsaEval (N i) e m) N l co) = m ^ e

The broadcast attack: if one message m is encrypted under pairwise coprime moduli N i (i ∈ l) with a common exponent e and m ^ e is smaller than the product of the moduli, then the Chinese remainder combination of the ciphertexts is m ^ e over the integers.

theorem Cslib.Crypto.eq_of_pow_eq_chineseRemainderOfList_rsaEval {ι : Type u_1} {N : ι → ℕ} {e m m' : ℕ} (he : e ≠ 0) (l : List ι) (co : List.Pairwise (Function.onFun Nat.Coprime N) l) (hN : ∀ i ∈ l, N i ≠ 0) (hsmall : m ^ e < (List.map N l).prod) (hroot : m' ^ e = ↑(Nat.chineseRemainderOfList (fun (i : ι) => rsaEval (N i) e m) N l co)) :
m' = m

The broadcast attack recovers the message: m is the unique integer e-th root of the Chinese remainder combination of its ciphertexts.

Factoring from φ(N) #

The factors of N computed from N and φ = φ(N): with s = N + 1 - φ (which is p + q) and r = ⌊√(s ^ 2 - 4 * N)⌋ (which is |p - q|), output ((s + r) / 2, (s - r) / 2).

theorem Cslib.Crypto.factorOfTotient_eq {p q : ℕ} (hq : 0 < q) (hqp : q ≤ p) :
factorOfTotient (p * q) ((p - 1) * (q - 1)) = (p, q)

For positive q ≤ p, factorOfTotient recovers (p, q) from p * q and (p - 1) * (q - 1).

Factoring from x ^ 2 ≡ y ^ 2, nontrivial square roots of 1, and (N, e, d) #

theorem Cslib.Crypto.gcd_sub_proper_of_sq_modEq {N x y : ℕ} (hyx : y ≤ x) (hsq : x ^ 2 ≡ y ^ 2 [MOD N]) (hsub : ¬x ≡ y [MOD N]) (hadd : ¬x + y ≡ 0 [MOD N]) :
1 < (x - y).gcd N ∧ (x - y).gcd N < N

Square-congruence factoring (gcd(x - y, N)): for any N and y ≤ x, if x ^ 2 ≡ y ^ 2 [MOD N], x ≢ y [MOD N], and x + y ≢ 0 [MOD N], then gcd(x - y, N) is a proper factor of N Boneh–Shoup, §16.4.1, p. 697.

theorem Cslib.Crypto.gcd_add_proper_of_sq_modEq {N x y : ℕ} (hsq : x ^ 2 ≡ y ^ 2 [MOD N]) (hsub : ¬x ≡ y [MOD N]) (hadd : ¬x + y ≡ 0 [MOD N]) :
1 < (x + y).gcd N ∧ (x + y).gcd N < N

Square-congruence factoring (gcd(x + y, N)): for any N, x, and y, if x ^ 2 ≡ y ^ 2 [MOD N], x ≢ y [MOD N], and x + y ≢ 0 [MOD N], then gcd(x + y, N) is a proper factor of N Boneh–Shoup, §16.4.1, p. 697.

theorem Cslib.Crypto.gcd_sub_one_proper_of_sq_modEq_one {N y : ℕ} (hsq : y ^ 2 ≡ 1 [MOD N]) (hone : ¬y ≡ 1 [MOD N]) (hneg : ¬y + 1 ≡ 0 [MOD N]) :
1 < (y - 1).gcd N ∧ (y - 1).gcd N < N

Nontrivial square root of 1 gives a proper factor (gcd(y - 1, N)): for any N and y, if y ^ 2 ≡ 1 [MOD N], y ≢ 1 [MOD N], and y + 1 ≢ 0 [MOD N], then gcd(y - 1, N) is a proper factor of N Arora–Barak, Lemma 20.24.

theorem Cslib.Crypto.gcd_add_one_proper_of_sq_modEq_one {N y : ℕ} (hsq : y ^ 2 ≡ 1 [MOD N]) (hone : ¬y ≡ 1 [MOD N]) (hneg : ¬y + 1 ≡ 0 [MOD N]) :
1 < (y + 1).gcd N ∧ (y + 1).gcd N < N

Nontrivial square root of 1 gives a proper factor (gcd(y + 1, N)): for any N and y, if y ^ 2 ≡ 1 [MOD N], y ≢ 1 [MOD N], and y + 1 ≢ 0 [MOD N], then gcd(y + 1, N) is a proper factor of N Arora–Barak, Lemma 20.24.

The factorization (max p q, min p q) of an RSA modulus is computed from N and φ(N).

theorem Cslib.Crypto.RSADesc.pow_mul_sub_one_modEq_one (desc : RSADesc) {x : ℕ} (hx : x.Coprime desc.modulus) :
x ^ (desc.e * desc.d - 1) ≡ 1 [MOD desc.modulus]

e * d - 1 is a multiple of the order of every unit modulo N: x ^ (e * d - 1) ≡ 1 for every x coprime to N.

theorem Cslib.Crypto.RSADesc.gcd_sub_one_proper (desc : RSADesc) {y : ℕ} (hsq : y ^ 2 ≡ 1 [MOD desc.modulus]) (hone : ¬y ≡ 1 [MOD desc.modulus]) (hneg : ¬y + 1 ≡ 0 [MOD desc.modulus]) :
1 < (y - 1).gcd desc.modulus ∧ (y - 1).gcd desc.modulus < desc.modulus

A square root y of 1 modulo an RSA modulus N = p * q, other than ±1, gives a proper factor gcd(y - 1, N) of N.

theorem Cslib.Crypto.RSADesc.gcd_add_one_proper (desc : RSADesc) {y : ℕ} (hsq : y ^ 2 ≡ 1 [MOD desc.modulus]) (hone : ¬y ≡ 1 [MOD desc.modulus]) (hneg : ¬y + 1 ≡ 0 [MOD desc.modulus]) :
1 < (y + 1).gcd desc.modulus ∧ (y + 1).gcd desc.modulus < desc.modulus

A square root y of 1 modulo an RSA modulus N = p * q, other than ±1, gives a proper factor gcd(y + 1, N) of N.

Equivalence of computing φ(N) and factoring, and RSA-to-factoring reductions #

noncomputable def Cslib.Crypto.totientGame (gen : RSAGen) (T : ℕ → ℕ → ProbComp ℕ) (n : ℕ) :

The Euler totient computation experiment ([KL20], Section 9.2.5, Theorem 9.49): sample desc ← gen n, run T n desc.modulus, and accept when T returns desc.totient = (desc.p - 1) * (desc.q - 1).

The Euler totient hardness assumption for gen: every uniform PPT algorithm has negligible probability of computing φ(N) = (p - 1) * (q - 1) from N = p * q. This assumption is novel to this library: [KL20], Section 9.2.5, Theorem 9.49 shows that computing φ(N) is equivalent to factoring N but states no separate assumption. It is equivalent to ModulusFactoringHard for the modulus law of gen (totientHard_iff_modulusFactoringHard), so it is exactly as plausible as factoring. Keys come from any law on RSADesc, which need not be efficiently samplable.

Explicit PPT reduction from computing φ(N) to factoring N ([KL20], Theorem 9.49): run the factoring algorithm F n N, and if it returns a nontrivial factorization (a, b) of N, output (a - 1) * (b - 1).

Explicit PPT reduction from factoring N to computing φ(N) ([KL20], Theorem 9.49): run the totient algorithm T n N to obtain φ' and output factorOfTotient N φ'.

totientGame gen (factorToTotient F) n equals modulusFactoringGame on the pushed-forward RSA modulus distribution.

The success probability of T in totientGame is at most the factoring success probability of totientToFactor T.

If computing φ(N) is hard for gen, then factoring N is hard for (gen n).map RSADesc.modulus.

If factoring N is hard for (gen n).map RSADesc.modulus, then computing φ(N) is hard for gen.

After [KL20], Theorem 9.49: computing φ(N) from N = p * q is polynomial-time equivalent to factoring N.

noncomputable def Cslib.Crypto.rsaKeyRecoveryGame (gen : RSAGen) (K : ℕ → RSAPublicKey → ProbComp ℕ) (n : ℕ) :

The RSA private-exponent recovery experiment ([KL20], Section 9.2.5, Theorem 9.50): sample desc ← gen n, run K n desc.publicKey, and accept when K returns an exponent d' with desc.e * d' ≡ 1 [MOD desc.totient].

The RSA private-exponent recovery hardness assumption for gen: every uniform PPT algorithm given (N, e) has negligible probability of finding a valid private exponent d' with e * d' ≡ 1 [MOD φ(N)]. This assumption is novel to this library: [KL20], Section 9.2.5, Theorem 9.50 shows that computing d is as hard as factoring N (a direction not formalized here) but states no separate assumption. It follows from RSAOneWay (RSAOneWay.rsaKeyRecoveryHard), which makes it plausible, and implies TotientHard (RSAKeyRecoveryHard.totientHard). Keys come from any law on RSADesc, which need not be efficiently samplable.

Explicit PPT reduction from RSA private-exponent recovery to computing φ(N): given pk = (N, e), run T n N to obtain φ' and return the modular inverse BinaryGcd.modInv e φ'.

The success probability of T in totientGame is at most the key-recovery success probability of totientToKeyRecovery T.

If RSA private-exponent recovery is hard for gen, then computing φ(N) is hard for gen.

If RSA private-exponent recovery is hard for gen, then factoring N is hard for (gen n).map RSADesc.modulus.

Explicit PPT reduction from RSA inversion to RSA private-exponent recovery: given (n, pk, y), run K n pk to obtain candidate private exponent d' and return rsaInvert pk.N d' y.

Explicit PPT reduction from RSA inversion to integer factoring: factor pk.N, compute φ(N), invert pk.e modulo φ(N) via BinaryGcd.modInv, and raise the challenge y to that exponent modulo pk.N.

The key-recovery success probability of K is at most the RSA inversion success probability of keyRecoveryToRSA K.

The factoring success probability of F is at most the RSA inversion success probability of factorToRSA F.

RSA one-wayness implies private-exponent recovery hardness.

RSA one-wayness implies Euler totient computation hardness.

RSA one-wayness implies integer factoring hardness on (gen n).map RSADesc.modulus.