wiener.tex 7.3 KB

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