wiener.tex 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. \chapter{Wiener's Attack \label{chap:wiener}}
  2. Wiener's attack was first published in 1989 as a result of cryptanalysis on the
  3. use of short RSA secret keys ~\cite{wiener}. It exploited the fact that it is
  4. possible to find the private key in \emph{polynomial time} using continued fractions
  5. expansions whenever a good estimate of the fraction $\frac{e}{N}$ is known.
  6. More specifically, given $d < \frac{1}{3} \sqrt[4]{N}$ one can efficiently
  7. recover $d$ only knowing $\angular{N, e}$.
  8. The scandalous implication behind Wiener's attack is that, even if there are
  9. situations where having a small private exponent may be
  10. particularly tempting with respect to performance (for example, a smart card
  11. communication with a computer), they represent a threat to the security of the
  12. cipher.
  13. Fortunately, ~\cite{wiener} \S 6 presents a couple of precautions that make a
  14. RSA key-pair immune to this attack, namely
  15. (i) making $e > \sqrt{N}$ and
  16. (ii) $gcd(p-1, q-1)$ large.
  17. \section{A background on Continued Fractions \label{sec:wiener:cf}}
  18. Let us call \emph{continued fraction} any expression of the form:
  19. %% why \cfrac sucks this much. |-------------------------|
  20. \begin{align*}
  21. a_0 + \frac{1}{a_1
  22. + \frac{1}{a_2
  23. + \frac{1}{a_3
  24. + \frac{1}{a_4 + \ldots}}}}
  25. \end{align*}
  26. From now on, we eill consider only hereby described as a finite sequence of integers for convenience:
  27. $\angular{a_0, a_1, a_2, a_3, \ \ldots, a_n}$.
  28. Any number $x \in \mathbb{Q}$ can be represented as a finite continued fraction,
  29. and for each $i < n$ there exists a fraction $\rfrac{h_i}{k_i}$ approximating
  30. $x$.
  31. By definition, each new approximation is recursively defined as:
  32. \begin{align}
  33. \label{eq:wiener:cf}
  34. \begin{cases}
  35. a_{-1} = 0 \\
  36. a_i = h_i // k_i \\
  37. \end{cases}
  38. \quad
  39. \begin{cases}
  40. h_{-2} = 0 \\
  41. h_{-1} = 1 \\
  42. h_i = a_i h_{i-1} + h_{i-2}
  43. \end{cases}
  44. \quad
  45. \begin{cases}
  46. k_{-2} = 1 \\
  47. k_{-1} = 0 \\
  48. k_i = a_i k_{i-1} + k_{i-2}
  49. \end{cases}
  50. \end{align}
  51. After a small digression concerning the properties of continued fractions, Wiener, in
  52. ~\cite{wiener}, shows that, if a continued fraction $f'$ is an underestimate of
  53. another one $f$, i.e.
  54. \begin{align}
  55. f' = f(1-\delta)
  56. \end{align}
  57. then it is possible to recover $f$, having $f'$, if $\delta$ is ``small
  58. enough'', where small enough means:
  59. \begin{align}
  60. \label{eq:wiener:cf_approx}
  61. \delta = 1 - \frac{f'}{f} < \frac{1}{\rfrac{3}{2}{h_1}{k_1}}
  62. \end{align}
  63. \\
  64. The ``continued fraction algorithm'' allowing us to recover $f$ is the
  65. following:
  66. \begin{enumerate}[(i)]
  67. \setlength{\itemsep}{1pt}
  68. \setlength{\parskip}{0pt}
  69. \setlength{\parsep}{0pt}
  70. \item generate the next $a_i$ of the continued fraction expansion of $f'$;
  71. \item use ~\ref{eq:wiener:cf} to generate the next fraction $\rfrac{h_i}{k_i}$
  72. equal to $\angular{a_0, a_1, \ldots, a_{i-1}, a_i}$ %% non e` proprio cosi`
  73. \item check whether $\rfrac{h_i}{k_i}$ is equal to $f$
  74. \end{enumerate}
  75. \section{Constructing the attack}
  76. As we saw in ~\ref{sec:preq:rsa}, by construction the two exponents are such that
  77. $ed \equiv 1 \pmod{\varphi(N)}$. This implies that there exists a
  78. $k \in \naturalN \mid ed = k\varphi(N) + 1$. This can be formalized to be
  79. the same problem we formalized in ~\ref{sec:wiener:cf}:
  80. \begin{align*}
  81. ed = k\varphi(N) + 1 \\
  82. \abs{\frac{ed - k\eulerphi{N}}{d\eulerphi{N}}} = \frac{1}{d\eulerphi{N}} \\
  83. \abs{\frac{e}{\eulerphi{N}} - \frac{k}{d}} = \frac{1}{d\eulerphi{N}} \\
  84. \end{align*}
  85. Now we proceed by substituting $\eulerphi{N}$ with $N$, since for large $N$, one
  86. approximates the other. We consider also the difference of the two, limited by
  87. $\abs{\cancel{N} + p + q - 1 - \cancel{N}} < 3\sqrt{N}$.
  88. For the last step, remember that $k < d < \rfrac{1}{3}\sqrt[4]{N}$:
  89. \begin{align*}
  90. \abs{\frac{e}{N} - \frac{k}{d}} &= \abs{\frac{ed - kN}{Nd}} \\
  91. &= \abs{\frac{\cancel{ed} -kN - \cancel{k\eulerphi{N}} + k\eulerphi{N}}{Nd}} \\
  92. &= \abs{\frac{1-k(N-\eulerphi{N})}{Nd}} \\
  93. &\leq \abs{\frac{3k\sqrt{N}}{Nd}}
  94. = \frac{3k}{d\sqrt{N}}
  95. < \frac{3(\rfrac{1}{3}\ \sqrt[4]{N})}{d\sqrt{N}}
  96. = \frac{1}{d\sqrt[4]{N}} < \frac{1}{2d^2}
  97. \end{align*}
  98. This demonstrates the conditions of ~\ref{eq:wiener:cf_approx} holds, and allows
  99. us to proceed with the continued fraction algorithm to converge to a solution
  100. ~\cite{20years}.
  101. \paragraph{}
  102. We start by generating the $\log N$ continued fraction expansions of
  103. $\frac{e}{N}$, and for each convergent $\frac{k}{d}$,
  104. %% XXX. verify this
  105. which by contruction is already at the lowest terms, we verify if it produces a
  106. factorization of $N$.
  107. First we check that $\eulerphi{N} = \frac{ed-1}{k}$ is
  108. an integer. Then we solve ~\ref{eq:wiener:pq} in $x$ in order to find $p, q$:
  109. \begin{align}
  110. \label{eq:wiener:pq}
  111. x^2 - (N - \eulerphi{N} + 1)x + N = 0
  112. \end{align}
  113. The above equation is constructed so that the $x$ coefficient is the sum of the
  114. two primes, while the constant term $N$ is the product of the two. Therefore, if
  115. $\eulerphi{N}$ has been correctly guessed, the two roots will be $p$ and $q$.
  116. \section{Again on the engine™}
  117. The algorithm is pretty straightforward by itself: we just need to apply the
  118. definitions provided in ~\ref{eq:wiener:cf} and test each convergent until
  119. $\log N$ iterations have been reached.
  120. %% XXX. questo viene da 20 years, ma non e` spiegato perche`.
  121. A Continued fraction structure may look like this:
  122. \begin{minted}{c}
  123. typedef struct cf {
  124. bigfraction_t fs[3]; /* holding h_i/k_i, h_i-1/k_i-1, h_i-2/k_i-2 */
  125. short i; /* cycling in range(0, 3) */
  126. bigfraction_t x; /* pointer to the i-th fraction in fs */
  127. BIGNUM* a; /* current a_i */
  128. BN_CTX* ctx;
  129. } cf_t;
  130. \end{minted}
  131. where \texttt{bigfraction\_t} is just a pair of \texttt{BIGNUM} \!s
  132. $\angular{h_i, k_i}$. Whenever we need to produce a new convergent, we increment
  133. $i \pmod{3}$ and apply the definitions given. The fresh convergent must be
  134. tested with very simple algebraic operations. It is worth noting here that
  135. \ref{eq:wiener:pq} can be solved using the reduced discriminant formula, as
  136. $p, q$ are odd primes:
  137. \begin{align*}
  138. \Delta = \left( \frac{N-\eulerphi{N} + 1}{2} \right)^2 - N \\
  139. x_{\angular{p , q}} = - \frac{N - \eulerphi{N} + 1}{2} \pm \sqrt{\Delta}
  140. \end{align*}
  141. Assuming the existence of the procedures \texttt{cf\_init}, initializing a
  142. continued fraction structure, and \texttt{cf\_next} producing the next
  143. convergent, we provide an algorithm for attacking the RSA cipher via Wiener:
  144. \begin{algorithm}[H]
  145. \caption{Wiener's Attack}
  146. \label{alg:wiener}
  147. \begin{algorithmic}[1]
  148. \State $f \gets \texttt{cf\_init}(e, N)$
  149. \State $i \gets \ceil{\log N}$
  150. \While{$i-- > 0$}
  151. \State $k, d \gets \texttt{cf\_next}(f)$
  152. \If{$k \nmid ed-1$} \strong{continue} \EndIf
  153. \State $\eulerphi{N} \gets (ed - 1)\ //\ k$
  154. \If{$\eulerphi{N}$ is odd} \strong{continue} \EndIf
  155. %% XXX. it could be that calling 'b' b/2 and 'delta' sqrt(delta/4) is
  156. %% misleading.
  157. \State $b \gets (N - \eulerphi{N} + 1) \gg 1$
  158. \State $\Delta, r \gets \dsqrt{b^2 - N}$
  159. \If{$r \neq 0$} \strong{continue} \EndIf
  160. \State $p, q \gets b \pm \Delta$
  161. \State \strong{break}
  162. \EndWhile
  163. \State \Return p, q
  164. \end{algorithmic}
  165. \end{algorithm}
  166. \section{Building a distributed version}
  167. %%% Local Variables:
  168. %%% mode: latex
  169. %%% TeX-master: "question_authority"
  170. %%% End: