wiener.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * \file wiener.c
  3. *
  4. */
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <openssl/x509.h>
  8. #include <openssl/rsa.h>
  9. #include <openssl/bn.h>
  10. #include "questions.h"
  11. #include "qwiener.h"
  12. cf_t* cf_new(void)
  13. {
  14. cf_t *f;
  15. f = (cf_t *) malloc(sizeof(cf_t));
  16. size_t i;
  17. for (i=0; i!=3; i++) {
  18. f->fs[i].h = BN_new();
  19. f->fs[i].k = BN_new();
  20. }
  21. f->a = BN_new();
  22. f->x.h = BN_new();
  23. f->x.k = BN_new();
  24. f->ctx = BN_CTX_new();
  25. return f;
  26. }
  27. void cf_free(cf_t* f)
  28. {
  29. size_t i;
  30. for (i=0; i!=3; i++) {
  31. BN_free(f->fs[i].h);
  32. BN_free(f->fs[i].k);
  33. }
  34. BN_free(f->a);
  35. BN_free(f->x.h);
  36. BN_free(f->x.k);
  37. free(f);
  38. }
  39. /**
  40. * \brief Initialized a continued fraction.
  41. *
  42. * A continued fraction for a floating number x can be expressed as a series
  43. * <a₀; a₁, a₂…, aₙ>
  44. * such that
  45. * <pre>
  46. *
  47. * 1
  48. * x = a₀ + ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
  49. * 1
  50. * a₁ + ⎽⎽⎽⎽⎽⎽⎽⎽⎽
  51. * a₂ + …
  52. *
  53. * </pre>
  54. * , where for each i < n, there exists an approximation hᵢ / kᵢ.
  55. * By definition,
  56. * a₋₁ = 0
  57. * h₋₁ = 1 h₋₂ = 0
  58. * k₋₁ = 0 k₋₂ = 1
  59. *
  60. * \param f A continued fraction structure. If f is NULL, a new one is
  61. * allocated.
  62. * \param num Numerator to be used as initial numerator for the fraction to be
  63. * approximated.
  64. * \param den Denominator to be used as denominator for the fraction to be
  65. * approximated.
  66. *
  67. * \return the continued fraction fiven as input.
  68. */
  69. cf_t* cf_init(cf_t* f, BIGNUM* num, BIGNUM* den)
  70. {
  71. if (!f) f = cf_new();
  72. BN_zero(f->fs[0].h);
  73. BN_one(f->fs[0].k);
  74. BN_one(f->fs[1].h);
  75. BN_zero(f->fs[1].k);
  76. f->i = 2;
  77. if (!BN_copy(f->x.h, num)) return NULL;
  78. if (!BN_copy(f->x.k, den)) return NULL;
  79. return f;
  80. }
  81. /**
  82. * \brief Produces the next fraction.
  83. *
  84. * Each new approximation hᵢ/kᵢ is defined rec ursively as:
  85. * hᵢ = aᵢhᵢ₋₁ + hᵢ₋₂
  86. * kᵢ = aᵢkᵢ₋₁ + kᵢ₋₂
  87. * Meanwhile each new aᵢ is simply the integer part of x.
  88. *
  89. *
  90. * \param f The continued fraction.
  91. * \return NULL if the previous fraction approximates at its best the number,
  92. * a pointer to the next fraction in the series othw.
  93. */
  94. bigfraction_t* cf_next(cf_t *f)
  95. {
  96. bigfraction_t *ith_fs = &f->fs[f->i];
  97. BIGNUM* rem = BN_new();
  98. if (BN_is_zero(f->x.h)) return NULL;
  99. BN_div(f->a, rem, f->x.h, f->x.k, f->ctx);
  100. /* computing hᵢ */
  101. BN_mul(f->fs[f->i].h , f->a, f->fs[(f->i-1+3) % 3].h, f->ctx);
  102. BN_uadd(f->fs[f->i].h, f->fs[f->i].h, f->fs[(f->i-2+3) % 3].h);
  103. /* computing kᵢ */
  104. BN_mul(f->fs[f->i].k , f->a, f->fs[(f->i-1+3) % 3].k, f->ctx);
  105. BN_uadd(f->fs[f->i].k, f->fs[f->i].k, f->fs[(f->i-2+3) % 3].k);
  106. f->i = (f->i + 1) % 3;
  107. /* update x. */
  108. BN_copy(f->x.h, f->x.k);
  109. BN_copy(f->x.k, rem);
  110. return ith_fs;
  111. }
  112. /**
  113. * \brief Square Root for bignums.
  114. *
  115. * An implementation of Dijkstra's Square Root Algorithm.
  116. * A Discipline of Programming, page 61 - Fifth Exercise.
  117. *
  118. */
  119. int BN_sqrtmod(BIGNUM* dv, BIGNUM* rem, BIGNUM* a, BN_CTX* ctx)
  120. {
  121. BIGNUM *shift;
  122. BIGNUM *adj;
  123. shift = BN_new();
  124. adj = BN_new();
  125. BN_zero(dv);
  126. BN_copy(rem, a);
  127. /* hacking into internal sequence to skip some cycles. */
  128. /* for (BN_one(shift); original */
  129. for (bn_wexpand(shift, a->top+1), shift->top=a->top, shift->d[shift->top-1] = 1;
  130. BN_ucmp(shift, rem) != 1;
  131. /* BN_rshift(shift, shift, 2); */
  132. BN_lshift1(shift, shift), BN_lshift1(shift, shift));
  133. while (!BN_is_one(shift)) {
  134. /* BN_rshift(shift, shift, 2); */
  135. BN_rshift1(shift, shift);
  136. BN_rshift1(shift, shift);
  137. BN_uadd(adj, dv, shift);
  138. BN_rshift1(dv, dv);
  139. if (BN_ucmp(rem, adj) != -1) {
  140. BN_uadd(dv, dv, shift);
  141. BN_usub(rem, rem, adj);
  142. }
  143. }
  144. BN_free(shift);
  145. BN_free(adj);
  146. return BN_is_zero(rem);
  147. }
  148. /*
  149. * Weiner Attack Implementation
  150. */
  151. int wiener_question_setup(void) { return 0; }
  152. int wiener_question_teardown(void) { return 0; }
  153. int wiener_question_test(X509* cert) { return 1; }
  154. int wiener_question_ask(X509* cert)
  155. {
  156. RSA *rsa;
  157. /* key data */
  158. BIGNUM *n, *e, *d, *phi;
  159. BIGNUM *p, *q;
  160. /* continued fractions coefficient, and mod */
  161. cf_t* cf;
  162. bigfraction_t *it;
  163. size_t i;
  164. BIGNUM *t, *tmp, *rem;
  165. /* equation coefficients */
  166. BIGNUM *b2, *delta;
  167. BN_CTX *ctx;
  168. int bits;
  169. rsa = X509_get_pubkey(cert)->pkey.rsa;
  170. phi = BN_new();
  171. tmp = BN_new();
  172. rem = BN_new();
  173. n = rsa->n;
  174. e = rsa->e;
  175. b2 = BN_new();
  176. delta = BN_new();
  177. /*
  178. * Generate the continued fractions approximating e/N
  179. */
  180. bits = BN_num_bits(n);
  181. cf = cf_init(NULL, e, n);
  182. ctx = cf->ctx;
  183. for (i=0, it = cf_next(cf);
  184. // XXX. how many keys shall I test?
  185. i!=bits && it;
  186. i++, it = cf_next(cf)) {
  187. t = it->h;
  188. d = it->k;
  189. /*
  190. * Recovering φ(N) = (ed - 1) / t
  191. * TEST1: obviously the couple {t, d} is correct → (ed-1) | t
  192. */
  193. BN_mul(phi, e, d, cf->ctx);
  194. BN_usub(tmp, phi, BN_value_one());
  195. BN_div(phi, rem, tmp, t, cf->ctx);
  196. if (!BN_is_zero(rem)) continue;
  197. // XXX. check, is it possible to fall here, assuming N, e are valid?
  198. if (BN_is_odd(phi) && BN_cmp(n, phi) == 1) continue;
  199. /*
  200. * Recovering p, q
  201. * Solving the equation
  202. * x² + [N-φ(N)+1]x + N = 0
  203. * which, after a few passages, boils down to:
  204. * x² + (p+q)x + (pq) = 0
  205. *
  206. * TEST2: φ(N) is correct → the two roots of x are integers
  207. */
  208. BN_usub(b2, n, phi);
  209. BN_uadd(b2, b2, BN_value_one());
  210. BN_rshift(b2, b2, 1);
  211. if (BN_is_zero(b2)) continue;
  212. /* delta */
  213. BN_sqr(tmp, b2, ctx);
  214. BN_usub(delta, tmp, n);
  215. if (!BN_sqrtmod(tmp, rem, delta, ctx)) continue;
  216. /* key found :) */
  217. p = BN_new();
  218. q = BN_new();
  219. BN_usub(p, b2, tmp);
  220. BN_uadd(q, b2, tmp);
  221. //printf("Primes: %s %s", BN_bn2dec(p), BN_bn2dec(q));
  222. break;
  223. }
  224. cf_free(cf);
  225. BN_free(rem);
  226. BN_free(tmp);
  227. BN_free(b2);
  228. BN_free(delta);
  229. BN_free(phi);
  230. return i;
  231. }
  232. qa_question_t WienerQuestion = {
  233. .name = "Wiener",
  234. .setup = wiener_question_setup,
  235. .teardown = wiener_question_teardown,
  236. .test = wiener_question_test,
  237. .ask = wiener_question_ask
  238. };