dixon.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * \file dixon.c
  3. * \brief An implementation of Dixon's Attack using bignums.
  4. *
  5. * Given a non-empty set B of primes, called factor-base, and
  6. * given a non-empty set of random numbers R, s.t. ∀ r ∈ R, s ≡ r² (mod N) is B-smooth.
  7. *
  8. * Try to find
  9. * U ⊂ R | (Πᵤ rᵢ)² ≡ Π s (mod N) and by defining x ≝ Πᵤ rᵢ, y ≝ √(Π s)
  10. * x² ≡ y² (mod N)
  11. * Asserting that x ≢ y (mod N) we claim to have found the two non-trivial
  12. * factors of N by computing gcd(x+y, N) and gcd(x-y, N).
  13. *
  14. * \note N = pq is assumed to be the public modulus,
  15. * while e, d . ed ≡ 1 (mod φ(N)) are respectively the public and the private
  16. * exponent.
  17. */
  18. #include <assert.h>
  19. #include <strings.h>
  20. #include <openssl/bn.h>
  21. #include "qa/questions/questions.h"
  22. #include "qa/questions/primes.h"
  23. #include "qa/questions/qarith.h"
  24. #include "qa/questions/qstrings.h"
  25. #include "qa/questions/qdixon.h"
  26. matrix_t*
  27. identity_matrix_new(int d)
  28. {
  29. size_t i;
  30. matrix_t *m = matrix_new(d, d);
  31. for (i=0; i!=d; i++) {
  32. bzero(m->M[i], sizeof(**(m->M)) * d);
  33. m->M[i][i] = 1;
  34. }
  35. return m;
  36. }
  37. matrix_t*
  38. matrix_new(int r, int c)
  39. {
  40. matrix_t *m;
  41. size_t i;
  42. m = malloc(sizeof(matrix_t));
  43. m->f = r;
  44. m->r = c;
  45. m->M = malloc(sizeof(BIGNUM **) * m->f);
  46. for (i=0; i!=r; i++)
  47. m->M[i] = malloc(sizeof(BIGNUM*) * m->r);
  48. return m;
  49. }
  50. void
  51. matrix_free(matrix_t *m)
  52. {
  53. size_t i;
  54. for (i=0; i!= m->f; i++)
  55. free(m->M[i]);
  56. free(m->M);
  57. free(m);
  58. }
  59. /*
  60. * \ref Kernel into a binary matrix.
  61. *
  62. * Discover linear dependencies using a refined version of the Gauss-Jordan
  63. * algorithm, from Brillhart and Morrison.
  64. *
  65. * \return h, the history matrix
  66. *
  67. */
  68. matrix_t *
  69. kernel(matrix_t *m)
  70. {
  71. int i, j, k;
  72. matrix_t *h = identity_matrix_new(m->f);
  73. for (j=0; j!=m->r; j++)
  74. for (i=0; i != m->f; i++)
  75. if (m->M[i][j]) {
  76. for (k=i+1; k != m->f; k++)
  77. if (m->M[k][j]) {
  78. vxor(m->M[k], m->M[k], m->M[i], m->r);
  79. vxor(h->M[k], h->M[k], h->M[i], h->r);
  80. }
  81. break;
  82. }
  83. return h;
  84. }
  85. /**
  86. * \brief Check for smoothness, incuding negative numbers.
  87. *
  88. * As there is no reason to reject negative numbers, provided that the product is positive, we are going to include the sign into the fist element of `v`, as to indicate the sign.
  89. */
  90. int dixon_smooth(BIGNUM *y, BN_CTX *ctx, char *v, size_t len)
  91. {
  92. short neg, ret;
  93. /* is yᵢ smooth? */
  94. neg = BN_is_negative(y);
  95. if (neg) BN_set_negative(y, 0);
  96. ret = smooth(y, ctx, v+1, len-1);
  97. if (neg) BN_set_negative(y, 1);
  98. v[0] = neg;
  99. return ret;
  100. }
  101. /**
  102. * \brief Discover a number x such that x² - n is smooth.
  103. *
  104. */
  105. inline void
  106. discover_smooth(BIGNUM *y, BIGNUM *x, BIGNUM *n,
  107. BN_CTX *ctx, char *v, size_t len)
  108. {
  109. do {
  110. BN_pseudo_rand_range(x, n);
  111. /* yᵢ = xᵢ² - N */
  112. BN_sqr(y, x, ctx);
  113. BN_sub(y, y, n);
  114. } while (!dixon_smooth(y, ctx, v, len));
  115. }
  116. static RSA*
  117. dixon_question_ask_rsa(const RSA *rsa)
  118. {
  119. /*
  120. * take exp(sqrt(ln N ln ln N))
  121. * ≅ 1.44 * 2^(sqrt(lg N lg lg N))
  122. * ≅ ³/₂ * 2^(sqrt(lg N 10)) for keys of 1024 bits.
  123. */
  124. size_t primes = 3 * (1 << (BN_num_bits(rsa->n) * 5)) / 2;
  125. size_t f = primes + 5;
  126. size_t r = primes + 1;
  127. size_t i, j;
  128. RSA *ret = NULL;
  129. BIGNUM
  130. *x = BN_new(),
  131. *y = BN_new(),
  132. *sqy = BN_new(),
  133. *rem = BN_new(),
  134. *gcd = BN_new();
  135. BN_CTX *ctx = BN_CTX_new();
  136. struct bnpair {
  137. BIGNUM *x;
  138. BIGNUM *y;
  139. } *R;
  140. matrix_t *m;
  141. matrix_t *h;
  142. /** STEP 1: initialization **/
  143. /* plus one for the sign */
  144. m = matrix_new(f, r);
  145. R = malloc(sizeof(struct bnpair) * f);
  146. for (i=0; i!=f; i++) {
  147. R[i].x = BN_new();
  148. R[i].y = BN_new();
  149. }
  150. /** STEP 2 generating R */
  151. for (i=0; i < m->f; i++) {
  152. fprintf(stderr, "[!] Discovering %zdth smooth number\n", i);
  153. discover_smooth(R[i].y, R[i].x, rsa->n,
  154. ctx, m->M[i], m->r);
  155. }
  156. /** STEP 3: break & enter. */
  157. h = kernel(m);
  158. BN_one(x);
  159. BN_one(sqy);
  160. for (i=0; i!=f && !ret; i++)
  161. /* if we found an even power */
  162. if (is_vzero(m->M[i], f)) {
  163. /* compute x, y² */
  164. for (j=0; j!=f; j++)
  165. if (h->M[i][j]) {
  166. BN_mul(x, x, R[j].x, ctx);
  167. BN_mul(sqy, sqy, R[j].y, ctx);
  168. }
  169. BN_sqrtmod(y, rem, sqy, ctx);
  170. assert(!BN_is_zero(rem));
  171. BN_gcd(gcd, x, y, ctx);
  172. if (BN_cmp(gcd, rsa->n) < 0 &&
  173. BN_cmp(gcd, BN_value_one()) > 0)
  174. ret = qa_RSA_recover(rsa, gcd, ctx);
  175. }
  176. /* free all the shit */
  177. for (i=0; i!=f; i++) {
  178. BN_free(R[i].x);
  179. BN_free(R[i].y);
  180. }
  181. free(R);
  182. BN_free(x);
  183. BN_free(y);
  184. BN_free(sqy);
  185. BN_free(rem);
  186. BN_free(gcd);
  187. BN_CTX_free(ctx);
  188. matrix_free(m);
  189. return ret;
  190. }
  191. qa_question_t DixonQuestion = {
  192. .name = "dixon",
  193. .pretty_name = "Dixon's Factorization",
  194. .ask_rsa = dixon_question_ask_rsa
  195. };