# Wiener's attack

The Wiener's attack, named after cryptologist Michael J. Wiener, is a type of cryptographic attack against RSA. The attack uses continued fraction representation to expose the private key d when d is small.

## Background on RSA

Fictional characters Alice and Bob are people who want to communicate securely. More specifically, Alice wants to send a message to Bob which only Bob can read. First Bob chooses two secret primes p and q. Then he calculates the RSA modulus N = pq. This RSA modulus is made public together with the encryption exponent e. N and e form the public key pair (e, N). By making this information public, anyone can encrypt messages to Bob. The decryption exponent d satisfies ed ≡ 1 (mod λ(N)), where λ(N) denotes the Carmichael function, though sometimes φ(N), the Euler's totient function, is used (note: this is the order of the multiplicative group (Z‍/‍NZ)$^×$, which is not necessarily a cyclic group). The encryption exponent e and λ(N) also must be relatively prime so that there is a modular inverse. The factorization of N and the private key d are kept secret, so that only Bob can decrypt the message. We denote the private key pair as (d, N). The encryption of the message M is given by C ≡ M$^e$ (mod N) and the decryption of cipher text C is given by C$^d$ ≡ (M$^e$)$^d$ ≡ M$^{ed}$ ≡ M (mod N) (using Fermat's little theorem).
Using the Euclidean algorithm, one can efficiently recover the secret key d if one knows the factorization of N. By having the secret key d, one can efficiently factor the modulus of N.

## Small private key

In the RSA cryptosystem, Bob might tend to use a small value of d, rather than a large random number to improve the RSA decryption performance. However, Wiener's attack shows that choosing a small value for d will result in an insecure system in which an attacker can recover all secret information, i.e., break the RSA system. This break is based on Wiener's theorem, which holds for small values of d. Wiener has proved that the attacker may efficiently find d when d < ⁠1/3⁠ N$^{1/4}$.
Wiener's paper also presented some countermeasures against his attack that allow fast decryption. Two techniques are described as follows.
Choosing large public key: Replace e by e′, where e′ = e + k ⋅ λ(N) for some large of k. When e′ is large enough, i.e. e′ > N$^{3/2}$, then Wiener's attack cannot be applied regardless of how small d is.
Using the Chinese remainder theorem: Suppose one chooses d such that both d$_p$ ≡ d (mod (p − 1)) and d$_q$ ≡ d (mod (q − 1)) are small but d itself is not, then a fast decryption of C can be done as follows:
* First compute M$_p$ ≡ C$^{dp}$ (mod p) and M$_q$ ≡ C$^{dq}$ (mod q).
* Use the Chinese remainder theorem to compute the unique value of 0 ≤ M < N that satisfies M ≡ M$_p$ (mod p) and M ≡ M$_q$ (mod q. The result of M satisfies M ≡ C$^d$ (mod N) as needed. The point is that Wiener's attack does not apply here because the value of d mod λ(N) can be large.

## How the attack works

Note that
    $\lambda (N)=\operatorname {lcm} (p-1,q-1)={\frac {(p-1)(q-1)}{G}}={\frac {\varphi (N)}{G}}$
where G = gcd(p − 1, q − 1).
Since ed ≡ 1 (mod λ(N)), there exists an integer K such that
    $ed=K\times \lambda (N)+1$
    $ed={\frac {K}{G}}(p-1)(q-1)+1$
Defining k = ⁠K/gcd(K, G)⁠ and g = ⁠G/gcd(K, G)⁠, and substituting into the above gives:
    $ed={\frac {k}{g}}(p-1)(q-1)+1$.
Divided by dpq:
    ${\frac {e}{pq}}={\frac {k}{dg}}(1-\delta )$, where $\delta ={\frac {p+q-1-{\frac {g}{k}}}{pq}}$.
So, ⁠e/pq⁠ is slightly smaller than ⁠k/dg⁠, and the former is composed entirely of public information. However, a method of checking and guess is still required.
By using simple algebraic manipulations and identities, a guess can be checked for accuracy.

## Wiener's theorem

Let N = pq with q < p < 2q. Let d < ⁠1/3⁠ N$^{1/4}$.
Given ⟨N, e⟩ with ed ≡ 1 (mod λ(N)), the attacker can efficiently recover d.

## Example

Suppose that the public keys are ⟨N, e⟩ = ⟨90581, 17993⟩. The attack should determine d. By using Wiener's theorem and continued fractions to approximate d, first we try to find the continued fractions expansion of ⁠e/N⁠. Note that this algorithm finds fractions in their lowest terms. We know that
    ${\frac {e}{N}}={\frac {17993}{90581}}={\cfrac {1}{5+{\cfrac {1}{29+\dots +{\cfrac {1}{3}}}}}}=\left[0,5,29,4,1,3,2,4,3\right]$
According to the continued fractions expansion of ⁠e/N⁠, all convergents ⁠k/d⁠ are:
    ${\frac {k}{d}}=0,{\frac {1}{5}},{\frac {29}{146}},{\frac {117}{589}},{\frac {146}{735}},{\frac {555}{2794}},{\frac {1256}{6323}},{\frac {5579}{28086}},{\frac {17993}{90581}}$
We can verify that the first convergent does not produce a factorization of N. However, the convergent ⁠1/5⁠ yields
    $\varphi (N)={\frac {ed-1}{k}}={\frac {17993\times 5-1}{1}}=89964$
Now, if we solve the equation
    $x^{2}-\left(\left(N-\varphi (N)\right)+1\right)x+N=0$
    $x^{2}-\left(\left(90581-89964\right)+1\right)x+90581=0$
    $x^{2}-618x+90581=0$
then we find the roots which are x = 379; 239. Therefore we have found the factorization
    $N=90581=379\times 239=p\times q$.
Notice that, for the modulus N = 90581, Wiener's theorem will work if
    $d<{\frac {N^{1/4}}{3}}\approx 5.7828$.

## Proof of Wiener's theorem

The proof is based on approximations using continued fractions.
Since ed = 1 (mod λ(N)), there exists a k such that ed − kλ(N) = 1. Therefore
    $\left|{\frac {e}{\lambda (N)}}-{\frac {k}{d}}\right\vert ={\frac {1}{d\lambda (N)}}$.
Let G = gcd(p − 1, q − 1); note that if φ(N) is used instead of λ(N), then the proof can be replaced with G = 1 and φ(N) replaced with λ(N).
Then multiplying by ⁠1/G⁠,
    $\left|{\frac {e}{\varphi (N)}}-{\frac {k}{Gd}}\right\vert ={\frac {1}{d\varphi (N)}}$
Hence, ⁠k/Gd⁠ is an approximation of ⁠e/φ(N)⁠. Although the attacker does not know φ(N), he may use N to approximate it. Indeed, since φ(N) = N − p − q + 1 and p + q − 1 < 3√N, we have:
    $\left\vert p+q-1\right\vert <3{\sqrt {N}}$
    $\left\vert N-\varphi (N)\right\vert <3{\sqrt {N}}$
Using N in place of φ(N) we obtain:
    ${\begin{aligned}\left\vert {\frac {e}{N}}-{\frac {k}{Gd}}\right\vert &=\left\vert {\frac {edG-kN}{NGd}}\right\vert \\&=\left\vert {\frac {edG-k\varphi (N)-kN+k\varphi (N)}{NGd}}\right\vert \\&=\left\vert {\frac {1-k(N-\varphi (N))}{NGd}}\right\vert \\&<\left\vert {\frac {-k(N-\varphi (N))}{NGd}}\right\vert (\because 0<|N-\varphi (N)|)\\&<\left\vert {\frac {3k{\sqrt {N}}}{NGd}}\right\vert ={\frac {3k{\sqrt {N}}}{{\sqrt {N}}{\sqrt {N}}Gd}}\leq {\frac {3k}{d{\sqrt {N}}}}\end{aligned}}$
Now, kλ(N) = ed − 1 < ed, so kλ(N) < ed. Since e < λ(N), so kλ(N) < ed < λ(N)d, then we obtain:
    $k\lambda (N)<\lambda (N)d$
          $k<d$
Since k < d and d < ⁠1/3⁠ N$^{1/4}$. Hence we obtain:
    (1) $\left\vert {\frac {e}{N}}-{\frac {k}{Gd}}\right\vert <{\frac {1}{dN^{\frac {1}{4}}}}$
Since d < ⁠1/3⁠ N$^{1/4}$, 2d < 3d, then 2d < 3d < N$^{1/4}$, we obtain:
    $2d<N^{1/4}$, so (2) ${\frac {1}{2d}}>{\frac {1}{N^{1/4}}}$
From (1) and (2), we can conclude that
    $\left\vert {\frac {e}{N}}-{\frac {k}{Gd}}\right\vert <{\frac {3k}{d{\sqrt {N}}}}<{\frac {1}{d\cdot 2d}}={\frac {1}{2d^{2}}}$
If |x − ⁠a/b⁠| < ⁠1/2b$^2$⁠, then ⁠a/b⁠ is a convergent of x, thus ⁠k/Gd⁠ appears among the convergents of ⁠e/N⁠. Therefore the algorithm will indeed eventually find ⁠k/Gd⁠.