pollard.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * \file pollard.c
  3. *
  4. * \brief Pollard's (p-1) factorization algorithm.
  5. *
  6. * This file contains an implementations of Pollard's (p-1) algorithm, used to
  7. * attack the public modulus of RSA.
  8. *
  9. * Consider the public modulus N = pq. Now,
  10. * (p-1) = q₀ᵉ⁰q₁ᵉ¹… qₖᵉᵏ . q₀ᵉ⁰ < q₁ᵉ¹ < … < qₖᵉᵏ ≤ B
  11. * implies that (p-1) | B! , since all factors of (p-1) belongs to {1, …, B}.
  12. * Consider a ≡ 2^(B!) (mod N)
  13. * a = 2^(B!) + kN = 2^(B!) + kqp → a ≡ 2^(B!) (mod p)
  14. * Since
  15. * <pre>
  16. *
  17. * ⎧ 2ᵖ⁻¹ ≡ 1 (mod p) ⎧ p | (a-1)
  18. * ⎨ → a ≡ 2^(B!) ≡ 1 (mod p) → ⎨ → p | gcd(a-1, N)
  19. * ⎩ p-1 | B! ⎩ p | N
  20. *
  21. * </pre>
  22. * And gcd(a-1, N) is a non-trivial factor of N, unless a = 1.
  23. */
  24. #include <openssl/x509.h>
  25. #include <openssl/err.h>
  26. #include "questions.h"
  27. static BIGNUM *two;
  28. int pollard1_question_setup(void)
  29. {
  30. /* create 2 */
  31. two = BN_new();
  32. BN_one(two);
  33. BN_uadd(two, two, BN_value_one());
  34. return 0;
  35. }
  36. int pollard1_question_teardown(void)
  37. {
  38. BN_free(two);
  39. return 0;
  40. }
  41. int pollard1_question_test(X509 *cert)
  42. {
  43. return 0;
  44. }
  45. int BN_sqrtmod(BIGNUM*, BIGNUM*, BIGNUM*, BN_CTX*);
  46. /**
  47. * \brief Pollard (p-1) factorization.
  48. *
  49. * Trivially the algorithm computes a = 2^(B!) (mod N), and then verifies that
  50. * gcd(a-1, N) is a nontrivial factor of N.
  51. *
  52. * According to Wikipedia™,
  53. * « By Dixon's theorem, the probability that the largest factor of such a
  54. * number is less than (p − 1)^ε is roughly ε^(−ε); so there is a probability of
  55. * about 3^(−3) = 1/27 that a B value of n^(1/6) will yield a factorisation.»
  56. *
  57. */
  58. int pollard1_question_ask(X509 *cert)
  59. {
  60. int ret = 1;
  61. RSA *rsa;
  62. BIGNUM *a, *B, *a1;
  63. BIGNUM *gcd, *rem;
  64. BIGNUM *n;
  65. BIGNUM *p, *q;
  66. BN_CTX *ctx;
  67. rsa = X509_get_pubkey(cert)->pkey.rsa;
  68. n = rsa->n;
  69. a = BN_new();
  70. B = BN_new();
  71. a1 = BN_new();
  72. gcd = BN_new();
  73. rem = BN_new();
  74. ctx = BN_CTX_new();
  75. /* take ⁸√N */
  76. BN_sqrtmod(gcd, rem, n, NULL);
  77. BN_sqrtmod(B, rem, gcd, NULL);
  78. /* compute 2^(B!) */
  79. for (BN_copy(a, two), BN_one(gcd);
  80. !(BN_is_zero(B) || !BN_is_one(gcd) || BN_cmp(gcd, n)==0);
  81. BN_usub(B, B, BN_value_one())) {
  82. BN_mod_exp(a, a, B, n, ctx);
  83. /* p ≟ gcd(a-1, N) */
  84. BN_usub(a1, a, BN_value_one());
  85. BN_gcd(gcd, a1, n, ctx);
  86. }
  87. /* Either p or q found :) */
  88. ret = BN_is_zero(B);
  89. if (!ret) {
  90. p = BN_dup(gcd);
  91. q = BN_new();
  92. BN_div(q, NULL, n, gcd, ctx);
  93. printf("p:%s, q=%s \n", BN_bn2dec(p), BN_bn2dec(q));
  94. }
  95. BN_free(a);
  96. BN_free(B);
  97. BN_free(a1);
  98. BN_free(gcd);
  99. BN_free(rem);
  100. BN_CTX_free(ctx);
  101. return ret;
  102. }
  103. struct qa_question PollardQuestion = {
  104. .name = "Pollard's (p-1) factorization",
  105. .setup = pollard1_question_setup,
  106. .teardown = pollard1_question_teardown,
  107. .test = pollard1_question_test,
  108. .ask = pollard1_question_ask,
  109. };