dixon.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "config.h"
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include <time.h>
  23. #include <openssl/bn.h>
  24. #ifdef HAVE_OPENMPI
  25. #include <mpi.h>
  26. #endif
  27. #include "qa/questions/questions.h"
  28. #include "qa/questions/primes.h"
  29. #include "qa/questions/qarith.h"
  30. #include "qa/questions/qstrings.h"
  31. #include "qa/questions/qdixon.h"
  32. #ifdef HAVE_OPENMPI
  33. #define ENCLEN 2048
  34. MPI_Datatype MPI_BNPAIR;
  35. #endif
  36. matrix_t*
  37. identity_matrix_new(int d)
  38. {
  39. size_t i;
  40. matrix_t *m = matrix_new(d, d);
  41. for (i=0; i!=d; i++) {
  42. bzero(m->M[i], sizeof(**(m->M)) * d);
  43. m->M[i][i] = 1;
  44. }
  45. return m;
  46. }
  47. matrix_t*
  48. matrix_new(int r, int c)
  49. {
  50. matrix_t *m;
  51. size_t i;
  52. m = malloc(sizeof(matrix_t));
  53. m->f = r;
  54. m->r = c;
  55. m->M = malloc(sizeof(BIGNUM **) * m->f);
  56. for (i=0; i!=r; i++)
  57. m->M[i] = malloc(sizeof(BIGNUM*) * m->r);
  58. return m;
  59. }
  60. void
  61. matrix_free(matrix_t *m)
  62. {
  63. size_t i;
  64. for (i=0; i!= m->f; i++)
  65. free(m->M[i]);
  66. free(m->M);
  67. free(m);
  68. }
  69. /*
  70. * \ref Kernel into a binary matrix.
  71. *
  72. * Discover linear dependencies using a refined version of the Gauss-Jordan
  73. * algorithm, from Brillhart and Morrison.
  74. *
  75. * \return h, the history matrix
  76. *
  77. */
  78. matrix_t *
  79. kernel(matrix_t *m)
  80. {
  81. int i, j, rg;
  82. matrix_t *h = identity_matrix_new(m->f);
  83. rg = 0;
  84. for (j=0; j!=m->r; j++)
  85. for (i=rg; i != m->f; i++)
  86. if (m->M[i][j]) {
  87. vswap(m->M[i], m->M[rg], m->r);
  88. vswap(h->M[i], h->M[rg], h->r);
  89. for (i=rg+1; i < m->f; i++)
  90. if (m->M[i][j]) {
  91. vxor(m->M[i], m->M[i], m->M[rg], m->r);
  92. vxor(h->M[i], h->M[i], h->M[rg], h->r);
  93. }
  94. rg++;
  95. break;
  96. }
  97. return h;
  98. }
  99. /**
  100. * \brief Check for smoothness, incuding negative numbers.
  101. *
  102. * 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.
  103. */
  104. int dixon_smooth(BIGNUM *y, BN_CTX *ctx, char *v, size_t len)
  105. {
  106. short neg, ret;
  107. /* is yᵢ smooth? */
  108. neg = BN_is_negative(y);
  109. if (neg) BN_set_negative(y, 0);
  110. ret = smooth(y, ctx, v+1, len-1);
  111. if (neg) BN_set_negative(y, 1);
  112. v[0] = neg;
  113. return ret;
  114. }
  115. /**
  116. * \brief Discover a number x such that x² - n is smooth.
  117. *
  118. */
  119. inline void
  120. discover_smooth(BIGNUM *y, BIGNUM *x, BIGNUM *n,
  121. BN_CTX *ctx, char *v, size_t len)
  122. {
  123. do {
  124. BN_pseudo_rand_range(x, n);
  125. /* yᵢ = xᵢ² - N */
  126. BN_sqr(y, x, ctx);
  127. BN_sub(y, y, n);
  128. } while (!dixon_smooth(y, ctx, v, len));
  129. }
  130. RSA*
  131. dixon_factorize(const RSA *rsa)
  132. {
  133. /*
  134. * take exp(sqrt(ln N ln ln N))
  135. * ≅ 1.44 * 2^(sqrt(lg N lg lg N))
  136. * ≅ ³/₂ * 2^(sqrt(lg N 10)) for keys of 1024 bits.
  137. */
  138. size_t primes = 3 * (1 << (BN_num_bits(rsa->n) * 5)) / 2;
  139. size_t f = primes + 5;
  140. size_t r = primes + 1;
  141. size_t i, j;
  142. RSA *ret = NULL;
  143. BIGNUM
  144. *x = BN_new(),
  145. *y = BN_new(),
  146. *sqy = BN_new(),
  147. *rem = BN_new(),
  148. *gcd = BN_new();
  149. BN_CTX *ctx = BN_CTX_new();
  150. struct bnpair {
  151. BIGNUM *x;
  152. BIGNUM *y;
  153. } *R = NULL;
  154. matrix_t *m = NULL;
  155. matrix_t *h;
  156. #ifndef HAVE_OPENMPI
  157. /** STEP 1: initialization **/
  158. /* plus one for the sign */
  159. m = matrix_new(f, r);
  160. R = malloc(sizeof(struct bnpair) * f);
  161. for (i=0; i!=f; i++) {
  162. R[i].x = BN_new();
  163. R[i].y = BN_new();
  164. }
  165. /** STEP 2 generating R */
  166. for (i=0; i < m->f; i++) {
  167. fprintf(stderr, "[!] Discovering %zdth smooth number\n", i);
  168. discover_smooth(R[i].y, R[i].x, rsa->n,
  169. ctx, m->M[i], m->r);
  170. }
  171. #else
  172. int procs, proc;
  173. int count;
  174. MPI_Comm_rank(MPI_COMM_WORLD, &proc);
  175. MPI_Comm_size(MPI_COMM_WORLD, &procs);
  176. struct {
  177. char x[ENCLEN];
  178. char y[ENCLEN];
  179. char v[r];
  180. } to;
  181. MPI_Aint offsets[3] = {0, ENCLEN, 2*ENCLEN};
  182. MPI_Datatype types[3] = {MPI_CHAR, MPI_CHAR, MPI_CHAR};
  183. int lengths[3] = {ENCLEN, ENCLEN, r};
  184. MPI_Type_struct(3, lengths, offsets, types, &MPI_BNPAIR);
  185. MPI_Type_commit(&MPI_BNPAIR);
  186. /* root node fetches, child nodes discovery */
  187. if (proc == 0) {
  188. /** STEP 1: initialization **/
  189. m = matrix_new(f, r);
  190. R = malloc(sizeof(struct bnpair) * f);
  191. /** STEP 2 generating R */
  192. count = procs > 1 ? f % (procs-1) : f;
  193. for (i=0; i != f - count; i++) {
  194. MPI_Recv(&to, 1, MPI_BNPAIR, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  195. R[i].x = BN_new();
  196. R[i].y = BN_new();
  197. BN_hex2bn(&R[i].x, to.x);
  198. BN_hex2bn(&R[i].y, to.y);
  199. memcpy(m->M[i], to.v, r);
  200. fprintf(stderr, "[!] discovering relations (%zu/%zu)\r", i, f);
  201. }
  202. for (;i < f; i++) {
  203. R[i].x = BN_new();
  204. R[i].y = BN_new();
  205. discover_smooth(R[i].y, R[i].x, rsa->n, ctx, m->M[i], r);
  206. }
  207. } else {
  208. BIGNUM *x = BN_new(), *y = BN_new();
  209. char *s;
  210. for (count = f / (procs-1); count; count--) {
  211. discover_smooth(y, x, rsa->n, ctx, to.v, r);
  212. s = BN_bn2hex(x);
  213. strcpy(to.x, s);
  214. OPENSSL_free(s);
  215. s = BN_bn2hex(y);
  216. strcpy(to.y, s);
  217. OPENSSL_free(s);
  218. /* fprintf(stderr, "generated: %s (%d)", to.x, count); */
  219. MPI_Send(&to, 1, MPI_BNPAIR, 0, 0, MPI_COMM_WORLD);
  220. }
  221. BN_free(x);
  222. BN_free(y);
  223. }
  224. if (proc != 0) {
  225. MPI_Finalize();
  226. exit(EXIT_SUCCESS);
  227. }
  228. #endif
  229. fprintf(stderr, "\n[!] breaching the kernel\n");
  230. /** STEP 3: break & enter. */
  231. h = kernel(m);
  232. for (i = h->f - 1;
  233. !ret && is_vzero(m->M[i], h->r);
  234. i--) {
  235. BN_one(x);
  236. BN_one(sqy);
  237. /* if we found an even power */
  238. fprintf(stderr, "[!] Examining relation\n");
  239. /* compute x, y² */
  240. for (j=0; j!=h->r; j++)
  241. if (h->M[i][j]) {
  242. BN_mul(x, x, R[j].x, ctx);
  243. BN_mul(sqy, sqy, R[j].y, ctx);
  244. }
  245. BN_sqrtmod(y, rem, sqy, ctx);
  246. // assert(!BN_is_zero(rem));
  247. BN_gcd(gcd, x, y, ctx);
  248. if (BN_cmp(gcd, rsa->n) < 0 &&
  249. BN_cmp(gcd, BN_value_one()) > 0)
  250. ret = qa_RSA_recover(rsa, gcd, ctx);
  251. }
  252. /* free all the shit */
  253. for (i=0; i!=f; i++) {
  254. BN_free(R[i].x);
  255. BN_free(R[i].y);
  256. }
  257. free(R);
  258. BN_free(x);
  259. BN_free(y);
  260. BN_free(sqy);
  261. BN_free(rem);
  262. BN_free(gcd);
  263. BN_CTX_free(ctx);
  264. matrix_free(m);
  265. return ret;
  266. }
  267. /**
  268. * \brief Measuring the strength of a RSA key.
  269. *
  270. * Note that I assume this function to be shit. I did not have had enough time
  271. * to discuss this with my professor, and furthermore I believe this to be an
  272. * incorrect measure: inaffidable, defective, and misleading. It is being
  273. * written anyway in order to make him happy.
  274. */
  275. static RSA*
  276. dixon_question_ask_rsa(const RSA* rsa)
  277. {
  278. #ifdef HAVE_OPENMPI
  279. return dixon_factorize(rsa);
  280. #else
  281. time_t start, end;
  282. /* see factorization function */
  283. int primes = 3 * (BN_num_bits(rsa->n) * 10) / 2;
  284. BIGNUM
  285. *x = BN_new(),
  286. *y = BN_new();
  287. BN_CTX *ctx = BN_CTX_new();
  288. char *v;
  289. int count;
  290. static const int wait = 60;
  291. v = malloc(sizeof(char) * primes);
  292. /* XXX. control error (return -1) here. */
  293. time(&start);
  294. end = start;
  295. for (count = 0; end - wait < start; time(&end)) {
  296. fprintf(stderr, "discovering: %dth..\r", count);
  297. BN_pseudo_rand_range(x, rsa->n);
  298. /* yᵢ = xᵢ² - N */
  299. BN_sqr(y, x, ctx);
  300. BN_sub(y, y, rsa->n);
  301. if (dixon_smooth(y, ctx, v, primes)) count++;
  302. }
  303. fprintf(stderr,
  304. "\nIn %.2f minutes %d smooth numbers"
  305. "have been discovered\n", (float) end - start, count);
  306. BN_CTX_free(ctx);
  307. BN_free(x);
  308. BN_free(y);
  309. return NULL;
  310. #endif
  311. }
  312. qa_question_t DixonQuestion = {
  313. .name = "dixon",
  314. .pretty_name = "Dixon's Factorization",
  315. .ask_rsa = dixon_question_ask_rsa
  316. };