wiener.tex 8.4 KB

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