Attacks on RSA

tl;dr: I discuss the issues related to small exponents, how to improve the success rate of an attack on RSA, how the square root relates to the hardness of the RSA problem, and its connection with Shor’s algorithm. Many of the materials discussed below are thanks to the lecture notes and coursework that I have taken in Introduction to Cryptography.

Prerequisites

Small Exponent Attack

Alice wants to send a (k − 1)-bit message, m m , to Bob, Cathy, and Dave. Bob, Cathy, and Dave have distinct k-bit RSA keys, NB N_B , NC N_C , and ND N_D , and they all use the same exponent, e=3 e = 3 . Alice uses plain textbook RSA to encrypt m m under each key, resulting in three ciphertexts cB c_B , cC c_C , and cD c_D , which she sends to them. Eve intercepts all three ciphertexts. Show that Eve can recover the plaintext m m .


There are two cases to consider:

  1. NA N_A , NB N_B , and NC N_C are not pairwise coprime.

    Without loss of generality, suppose NA N_A and NB N_B are not coprime. Then one can use gcd(NA,NB) \gcd(N_A, N_B) to factor both NA N_A and NB N_B . Once factored, computing ϕ(NA) \phi(N_A) is straightforward, which then enables decryption of the ciphertext memodNA m^e \bmod N_A .

  2. NA N_A , NB N_B , and NC N_C are pairwise coprime.

    In this case, we have:

    cA=m3modNAcB=m3modNBcC=m3modNC\begin{align*} c_A &= m^3 \bmod N_A \\ c_B &= m^3 \bmod N_B \\ c_C &= m^3 \bmod N_C \\ \end{align*}

    Using the Chinese Remainder Theorem (CRT), we can compute a value c c' such that

    c=m3mod(NANBNC)c' = m^3 \bmod (N_A N_B N_C)

    Since m m is smaller than any of the moduli, m3<NANBNC m^3 < N_A N_B N_C . Therefore, the cube root of c c' can be taken directly to recover m m .

Improve RSA Attack Success Rate

Let (N,e) (N, e) be an RSA key, where 2<e<N 2 < e < N . Suppose A \mathcal{A} is a probabilistic algorithm that runs in time t t with

PrrZN[A(N,e,remodN)=r]=1%.\Pr_{r \leftarrow \mathbb{Z}_N} \left[ \mathcal{A}(N, e, r^e \bmod N) = r \right] = 1\%.

Show that A \mathcal{A} can be used as a subroutine to construct an algorithm B \mathcal{B} such that

PrrZN[B(N,e,remodN)=r]=99%.\Pr_{r \leftarrow \mathbb{Z}_N} \left[ \mathcal{B}(N, e, r^e \bmod N) = r \right] = 99\%.

To build such an algorithm B \mathcal{B} , we rely on the fact that RSA is multiplicatively homomorphic. With this in mind, one can devise an algorithm B(N,e,y) \mathcal{B}(N, e, y) as follows:

  • Repeat:
    • Sample s$ZN s \xleftarrow{\$} \mathbb{Z}_N .
    • Compute xA(N,e,ysemodN) x \leftarrow \mathcal{A}(N, e, ys^e \bmod N) .
  • Until xe=yse x^e = ys^e .

Then, by using the homomorphic property, recover r r as:

rxs1modN.r \leftarrow x \cdot s^{-1} \bmod N.

By repeating the algorithm A \mathcal{A} a bounded number of times, we can design B \mathcal{B} with a success probability of 99%.

Square Root and RSA

Let’s begin by examining an interesting attack.

RSA Key Leakage Vulnerability

Suppose Bob uses the RSA cryptosystem and has a key pair with public key e e and private key d d . If Bob accidentally leaks his private key and decides to generate a new public/private key pair without generating a new modulus, show that this approach is insecure.


To prove that this approach is insecure, we will show that, given d d , one can factor N N (assuming N N is the product of two odd primes).

  1. Given d d , compute k=ed1 k = ed - 1 using the public key e e .
  2. By definition, k k is divisible by ϕ(N) \phi(N) . Since Euler’s totient function ϕ(N) \phi(N) is even, k k is even, and hence k2 \frac{k}{2} is an integer.
  3. By Euler’s theorem, for every gZN g \in \mathbb{Z}_N^* , we have gk=1 g^k = 1 . Therefore, gk2 g^{\frac{k}{2}} is a square root of 1modN 1 \bmod N .

Properties of Square Roots

Before proceeding with the proof, let’s study the properties of the square roots of 1 1 , which we denote by x x .

Using the CRT, since N=pq N = pq where p p and q q are primes, solving

x21(modN)x^2 \equiv 1 \pmod{N}

is equivalent to solving

x21(modp)andx21(modq).x^2 \equiv 1 \pmod{p} \quad \text{and} \quad x^2 \equiv 1 \pmod{q}.

For a prime p p , the solutions to x21(modp) x^2 \equiv 1 \pmod{p} are x±1(modp) x \equiv \pm 1 \pmod{p} . Similarly for q q . Hence, there are four solutions modulo N N , determined by the combinations of ±1(modp) \pm 1 \pmod{p} and ±1(modq) \pm 1 \pmod{q} :

  1. n11(modp) n_1 \equiv 1 \pmod{p} and n11(modq) n_1 \equiv 1 \pmod{q} , i.e., n11(modN) n_1 \equiv 1 \pmod{N} .
  2. n21(modp) n_2 \equiv -1 \pmod{p} and n21(modq) n_2 \equiv -1 \pmod{q} , i.e., n21(modN) n_2 \equiv -1 \pmod{N} .
  3. n31(modp) n_3 \equiv 1 \pmod{p} and n31(modq) n_3 \equiv -1 \pmod{q} .
  4. n41(modp) n_4 \equiv -1 \pmod{p} and n41(modq) n_4 \equiv 1 \pmod{q} .

Each ni n_i satisfies ni21(modN) n_i^2 \equiv 1 \pmod{N} because ni21 n_i^2 \equiv 1 modulo both p p and q q .

The nontrivial solutions n3 n_3 and n4 n_4 are especially important for factoring N N . Consider n3 n_3 :

  • Since n31(modp) n_3 \equiv 1 \pmod{p} , we have n310(modp) n_3 - 1 \equiv 0 \pmod{p} ; hence, p p divides n31 n_3 - 1 .
  • Since n31(modq) n_3 \equiv -1 \pmod{q} , we have n312(modq) n_3 - 1 \equiv -2 \pmod{q} , and since q q is odd, 2≢0(modq) -2 \not\equiv 0 \pmod{q} . Thus, q q does not divide n31 n_3 - 1 .

Therefore, gcd(n31,N)=p \gcd(n_3 - 1, N) = p . A similar argument applies to n4 n_4 , except it yields q q .

However, finding a nontrivial square root of 1 is not straightforward in RSA. In fact, it can be shown that for every gZN g \in \mathbb{Z}_N^* , gk2=1modN g^{\frac{k}{2}} = 1 \bmod N . To explain this, express p1=2tprp p-1 = 2^{\tp} r_p and q1=2tqrq q-1 = 2^{\tq} r_q with tp,tq1 \tp, \tq \geq 1 . Then,

k=α2tp+tqrprq,k = \alpha \cdot 2^{\tp + \tq} r_p r_q,

where α \alpha is a positive integer and tp+tq2 \tp + \tq \geq 2 . This implies that p1 p - 1 divides k2 \frac{k}{2} and similarly for q1 q - 1 . By the CRT and Euler’s Theorem, it follows that

gZN,  gk21modN.\forall g \in \mathbb{Z}_N^*, \; g^{\frac{k}{2}} \equiv 1 \bmod N.

On the bright side, if k2 \frac{k}{2} is even, we can iterate and consider gk4 g^{\frac{k}{4}} , gk8 g^{\frac{k}{8}} , and so on to search for a nontrivial square root of 1. Formally, writing k=2tr k = 2^t r where ttp+tq t \geq \tp + \tq and r r is an odd integer, we can examine the sequence:

gk2, gk4, , gk2t.g^{\frac{k}{2}},\ g^{\frac{k}{4}},\ \dots,\ g^{\frac{k}{2^t}}.

If, for any i i , the value gcd(gk2i1,N) \gcd\left(g^{\frac{k}{2^i}} - 1, N\right) is neither 1 nor N N , then we can successfully factor N N 1 The sequence computation and gcd calculation can be done in polynomial time. See Proof of Fact 1..

But what is the probability that such an event occurs when we randomly select g g ?

We will now prove a lower bound: such an event occurs with probability at least 12 \frac{1}{2} . Let tmax=max(tp,tq) t_{\text{max}} = \max(\tp, \tq) .

A strawman example: First, consider k=2tr k' = 2^{t'} r where ttmax t' \geq t_{\text{max}} . Since ttmax t' \geq t_{\text{max}} , both p1 p - 1 and q1 q - 1 divide k k' . Thus, for any lttmax l \leq t - t_{\text{max}} , gk2l g^{\frac{k}{2^l}} will always be 1. This suggests that we should instead look at the case when t<tmax t' < t_{\text{max}} .


Before proceeding further, we prove the following lemma:

Lemma

  1. The multiplicative group Zp \mathbb{Z}_p^* (with p p prime) is cyclic; that is, it has a generator.
  2. Zp \mathbb{Z}_p^* has a subgroup S S of order p12 \frac{p-1}{2} , and for every eZp e \in \mathbb{Z}_p^* , we have ep121modp e^{\frac{p-1}{2}} \equiv 1 \bmod p if and only if eS e \in S .

Proof.

  1. This follows from the standard result that the multiplicative group of a finite field is cyclic. Let g g denote a generator of this group.

  2. The subgroup S S exists and can be generated by g2 g^2 . Its order is p12 \frac{p-1}{2} because p12 \frac{p-1}{2} is the smallest integer satisfying (g2)p12=gp11modp \left(g^2\right)^{\frac{p-1}{2}} = g^{p-1} \equiv 1 \bmod p . The rest of the lemma follows:

    • (\Leftarrow) If eS e \in S , then e=(g2)c e = (g^2)^c for some integer c c , so

      ep12=(g2)cp12=1c=1modp.e^{\frac{p-1}{2}} = \left(g^2\right)^{c \frac{p-1}{2}} = 1^c = 1 \bmod p.
    • (\Rightarrow) Conversely, if ep121modp e^{\frac{p-1}{2}} \equiv 1 \bmod p and e=gc e = g^c , then

      gcp121modp.g^{c \cdot \frac{p-1}{2}} \equiv 1 \bmod p.

      Since g g has order p1 p-1 , it must be that cp12 c \cdot \frac{p-1}{2} is a multiple of p1 p-1 ; hence, c c is even. Writing c=2k c = 2k , we have e=g2k=(g2)k e = g^{2k} = (g^2)^k , so eS e \in S . This also implies that if eS e \notin S , then ep121modp e^{\frac{p-1}{2}} \equiv -1 \bmod p .


Now, consider the case t=tmax1 t' = t_{\text{max}} - 1 . There are two cases to analyze:

  1. Case 1: tp<tq \tp < \tq (without loss of generality).

    Since tmax=tq t_{\text{max}} = \tq , we have tpt \tp \leq t' and thus p1 p - 1 divides k k' . This implies gk1modp g^{k'} \equiv 1 \bmod p . To obtain a nontrivial square root, we require gk1modq g^{k'} \equiv -1 \bmod q . Writing k=q12r k' = \frac{q-1}{2}r' (with r r' odd), by the lemma, if g g is in the subgroup S S of Zq \mathbb{Z}_q^* (which has order q12 \frac{q-1}{2} ), then gk1modq g^{k'} \equiv 1 \bmod q ; if gS g \notin S , then gk(1)r=1modq g^{k'} \equiv (-1)^{r'} = -1 \bmod q . Therefore, for a randomly sampled g g ,

    Pr[gk1modq]=1Pr[gS]=1q12q1=12.\Pr[g^{k'} \equiv -1 \bmod q] = 1 - \Pr[g \in S] = 1 - \frac{\frac{q-1}{2}}{q-1} = \frac{1}{2}.
  2. Case 2: tp=tq \tp = \tq .

    In this case, to obtain a nontrivial square root of 1, either

    • gk1modp g^{k'} \equiv 1 \bmod p and gk1modq g^{k'} \equiv -1 \bmod q , or
    • gk1modp g^{k'} \equiv -1 \bmod p and gk1modq g^{k'} \equiv 1 \bmod q .

    By a similar analysis as in Case 1, the total probability of a nontrivial square root occurring is

    1212+1212=12.\frac{1}{2} \cdot \frac{1}{2} + \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{2}.

Thus, regardless of tp \tp and tq \tq , we have:

Pr[gcd(gk1,N) is a factor of N]=12.\Pr\left[\gcd\left(g^{k'} - 1, N\right) \text{ is a factor of } N\right] = \frac{1}{2}.

Therefore,

Pr[i=1lgcd(gk2i1,N) is a factor of N]12.\Pr\left[\bigcup_{i=1}^{l} \gcd\left(g^{\frac{k}{2^i}} - 1, N\right) \text{ is a factor of } N\right] \geq \frac{1}{2}.

This completes the proof.

You can also programmatically verify the above proof using this simulation. The simulation verifies the above proof and additionally provides another method to simulate the probability of factorizing N N by checking all the elements in the sequence.

Proof (Continued)

To factor N N , we can randomly sample group elements g g until we find one that yields a nontrivial square root of 1. Based on the analysis above, this happens with a high probability after only a few tries. Ultimately, the nontrivial square root is used to factor N N 2 This is not the only method to attack RSA when both e e and d d are known. This post details an alternative approach that estimates ed1ϕ(N) \frac{ed - 1}{\phi(N)} to compute ϕ(N) \phi(N) , which then allows one to deduce p+q=Nϕ(N)+1 p+q = N - \phi(N) + 1 and factor N N ..

Shor’s Algorithm

The idea behind Shor’s algorithm (period finding) is also connected to using square roots to factor N N .

Suppose we use Shor’s algorithm to find an r r such that

f(x+r)=f(x),f(x + r) = f(x),

that is,

(gx)r=gxgr11modN.(g^x)^r = g^x \quad \Rightarrow \quad g^{r-1} \equiv 1 \bmod N.

Then, by the previous arguments, with probability at least 12 \frac{1}{2} , we can use gr12, , gr12l g^{\frac{r-1}{2}},\ \dots,\ g^{\frac{r-1}{2^l}} to factor N N . Consequently, one can repeatedly sample elements and apply Shor’s algorithm until success is achieved.

Another perspective on how a nontrivial square root aids in factoring N N is to note that for an element with gr11modN g^{r-1} \equiv 1 \bmod N , we have:

(gr121)(gr12+1)0modN.\left(g^{\frac{r-1}{2}} - 1\right) \left(g^{\frac{r-1}{2}} + 1\right) \equiv 0 \bmod N.

Thus, one of the factors in the parenthesis will be a prime factor of N N (provided gr12±1 g^{\frac{r-1}{2}} \neq \pm 1 ).

Next Steps