wiener.tex 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 9 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{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, Legendre in 1768 discovered that,
  53. if a continued fraction $f' = \frac{\theta'}{\kappa'}$ is
  54. an underestimate of another one $f = \frac{\theta}{\kappa}$, i.e.
  55. \begin{align}
  56. \abs{f - f'} = \delta
  57. \end{align}
  58. then for a $\delta$ sufficiently small, $f$ is \emph{equal} to the $n$-th
  59. continued fraction expansion of $f'$ (\cite{smeets} \S 2). Formally,
  60. \begin{theorem*}[Legendre]
  61. If $f = \frac{\theta}{\kappa}$, $f' = \frac{\theta'}{\kappa'}$ and
  62. $\gcd(\theta, \kappa) = 1$, then
  63. \begin{align}
  64. \label{eq:wiener:cf_approx}
  65. \abs{f' - \frac{\theta}{\kappa}} < \delta = \frac{1}{2\kappa^2}
  66. \quad
  67. \text{ implies that }
  68. \quad
  69. \begin{bmatrix}
  70. \theta \\ \kappa
  71. \end{bmatrix}
  72. =
  73. \begin{bmatrix}
  74. \theta'_n \\ \kappa'_n
  75. \end{bmatrix},
  76. \quad
  77. \text{ for some } n \geq 0
  78. \end{align}
  79. \end{theorem*}
  80. Two centuries later, first Wiener \cite{wiener} and later Dan Boneh
  81. \cite{20years} leveraged this theorem in order to produce an algorithm able to
  82. recover $f$, having $f'$.
  83. The \emph{continued fraction algorithm} is the following:
  84. \begin{enumerate}[(i)]
  85. \setlength{\itemsep}{1pt}
  86. \setlength{\parskip}{0pt}
  87. \setlength{\parsep}{0pt}
  88. \item generate the next $a_i$ of the continued fraction expansion of $f'$;
  89. \item use ~\ref{eq:wiener:cf} to generate the next fraction $\rfrac{h_i}{k_i}$
  90. equal to $\angular{a_0, a_1, \ldots, a_{i-1}, a_i}$ %% non e` proprio cosi`
  91. \item check whether $\rfrac{h_i}{k_i}$ is equal to $f$
  92. \end{enumerate}
  93. \section{Constructing the attack}
  94. As we saw in ~\ref{sec:preq:rsa}, by construction the two exponents are such that
  95. $ed \equiv 1 \pmod{\varphi(N)}$. This implies that there exists a
  96. $k \in \naturalN \mid ed = k\varphi(N) + 1$. This can be formalized to be
  97. the same problem we formalized in ~\ref{eq:wiener:cf_approx}:
  98. \begin{align*}
  99. ed = k\varphi(N) + 1 \\
  100. \abs{\frac{ed - k\eulerphi{N}}{d\eulerphi{N}}} = \frac{1}{d\eulerphi{N}} \\
  101. \abs{\frac{e}{\eulerphi{N}} - \frac{k}{d}} = \frac{1}{d\eulerphi{N}} \\
  102. \end{align*}
  103. Now we proceed by substituting $\eulerphi{N}$ with $N$, since for large $N$, one
  104. approximates the other. We consider also the difference of the two, limited by
  105. $\abs{\cancel{N} + p + q - 1 - \cancel{N}} < 3\sqrt{N}$.
  106. For the last step, remember that $k < d < \rfrac{1}{3}\sqrt[4]{N}$:
  107. \begin{align*}
  108. \abs{\frac{e}{N} - \frac{k}{d}} &= \abs{\frac{ed - kN}{Nd}} \\
  109. &= \abs{\frac{\cancel{ed} -kN - \cancel{k\eulerphi{N}} + k\eulerphi{N}}{Nd}} \\
  110. &= \abs{\frac{1-k(N-\eulerphi{N})}{Nd}} \\
  111. &\leq \abs{\frac{3k\sqrt{N}}{Nd}}
  112. = \frac{3k}{d\sqrt{N}}
  113. < \frac{3(\rfrac{1}{3}\ \sqrt[4]{N})}{d\sqrt{N}}
  114. = \frac{1}{d\sqrt[4]{N}} < \frac{1}{2d^2}
  115. \end{align*}
  116. This demonstrates that the hypotesis of ~\ref{eq:wiener:cf_approx} is satisfied,
  117. and allows us to proceed with the continued fraction algorithm to converge to a
  118. solution ~\cite{20years}.
  119. \paragraph{}
  120. We start by generating the $\log N$ continued fraction expansions of
  121. $\frac{e}{N}$, and for each convergent $\frac{k}{d}$,
  122. %% XXX. verify this
  123. which by contruction is already at the lowest terms, we verify if it produces a
  124. factorization of $N$.
  125. First we check that $\eulerphi{N} = \frac{ed-1}{k}$ is
  126. an integer. Then we solve ~\ref{eq:wiener:pq} in $x$ in order to find $p, q$:
  127. \begin{align}
  128. \label{eq:wiener:pq}
  129. x^2 - (N - \eulerphi{N} + 1)x + N = 0
  130. \end{align}
  131. The above equation is constructed so that the $x$ coefficient is the sum of the
  132. two primes, while the constant term $N$ is the product of the two. Therefore, if
  133. $\eulerphi{N}$ has been correctly guessed, the two roots will be $p$ and $q$.
  134. \section{Again on the engine™}
  135. The algorithm is pretty straightforward by itself: we just need to apply the
  136. definitions provided in ~\ref{eq:wiener:cf} and test each convergent until
  137. $\log N$ iterations have been reached.
  138. %% XXX. questo viene da 20 years, ma non e` spiegato perche`.
  139. A Continued fraction structure may look like this:
  140. \begin{minted}{c}
  141. typedef struct cf {
  142. bigfraction_t fs[3]; /* holding h_i/k_i, h_i-1/k_i-1, h_i-2/k_i-2 */
  143. short i; /* cycling in range(0, 3) */
  144. bigfraction_t x; /* pointer to the i-th fraction in fs */
  145. BIGNUM* a; /* current a_i */
  146. BN_CTX* ctx;
  147. } cf_t;
  148. \end{minted}
  149. where \texttt{bigfraction\_t} is just a pair of \texttt{BIGNUM} \!s
  150. $\angular{h_i, k_i}$. Whenever we need to produce a new convergent, we increment
  151. $i \pmod{3}$ and apply the definitions given. The fresh convergent must be
  152. tested with very simple algebraic operations. It is worth noting here that
  153. \ref{eq:wiener:pq} can be solved using the reduced discriminant formula, as
  154. $p, q$ are odd primes:
  155. \begin{align*}
  156. \Delta = \left( \frac{N-\eulerphi{N} + 1}{2} \right)^2 - N \\
  157. x_{\angular{p , q}} = - \frac{N - \eulerphi{N} + 1}{2} \pm \sqrt{\Delta}
  158. \end{align*}
  159. Assuming the existence of the procedures \texttt{cf\_init}, initializing a
  160. continued fraction structure, and \texttt{cf\_next} producing the next
  161. convergent, we provide an algorithm for attacking the RSA cipher via Wiener:
  162. \begin{algorithm}[H]
  163. \caption{Wiener's Attack}
  164. \label{alg:wiener}
  165. \begin{algorithmic}[1]
  166. \Function{wiener}{\PKArg}
  167. \State $f \gets \texttt{cf\_init}(e, N)$
  168. \For{$\ceil{\log N} \strong{ times }$}
  169. \State $k, d \gets \texttt{cf\_next}(f)$
  170. \If{$k \nmid ed-1$} \strong{continue} \EndIf
  171. \State $\eulerphi{N} \gets (ed - 1)\ //\ k$
  172. \If{$\eulerphi{N}$ is odd} \strong{continue} \EndIf
  173. %% XXX. it could be that calling 'b' b/2 and 'delta' sqrt(delta/4) is
  174. %% misleading.
  175. \State $b \gets (N - \eulerphi{N} + 1) \gg 1$
  176. \State $\Delta, r \gets \dsqrt{b^2 - N}$
  177. \If{$r \neq 0$} \strong{continue} \EndIf
  178. \State $p \gets b + \Delta$
  179. \State $q \gets b - \Delta$
  180. \State \strong{break}
  181. \EndFor
  182. \State \Return p, q
  183. \EndFunction
  184. \end{algorithmic}
  185. \end{algorithm}
  186. \paragraph{Parallelism}
  187. Parallel implementation of this specific version of Wiener's Attack is
  188. difficult, because the inner loop is inherently serial. At best, parallelism
  189. could be employed to construct a constructor process, building the $f_n$
  190. convergents, and consumers receiving each of those and processing them
  191. seperatedly. The first one arriving to a solution, broadcasts a stop message to
  192. the others.
  193. %%% Local Variables:
  194. %%% mode: latex
  195. %%% TeX-master: "question_authority"
  196. %%% End: