sshrsag.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * RSA key generation.
  3. */
  4. #include <assert.h>
  5. #include "ssh.h"
  6. #define RSA_EXPONENT 37 /* we like this prime */
  7. int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
  8. void *pfnparam)
  9. {
  10. Bignum pm1, qm1, phi_n;
  11. unsigned pfirst, qfirst;
  12. /*
  13. * Set up the phase limits for the progress report. We do this
  14. * by passing minus the phase number.
  15. *
  16. * For prime generation: our initial filter finds things
  17. * coprime to everything below 2^16. Computing the product of
  18. * (p-1)/p for all prime p below 2^16 gives about 20.33; so
  19. * among B-bit integers, one in every 20.33 will get through
  20. * the initial filter to be a candidate prime.
  21. *
  22. * Meanwhile, we are searching for primes in the region of 2^B;
  23. * since pi(x) ~ x/log(x), when x is in the region of 2^B, the
  24. * prime density will be d/dx pi(x) ~ 1/log(B), i.e. about
  25. * 1/0.6931B. So the chance of any given candidate being prime
  26. * is 20.33/0.6931B, which is roughly 29.34 divided by B.
  27. *
  28. * So now we have this probability P, we're looking at an
  29. * exponential distribution with parameter P: we will manage in
  30. * one attempt with probability P, in two with probability
  31. * P(1-P), in three with probability P(1-P)^2, etc. The
  32. * probability that we have still not managed to find a prime
  33. * after N attempts is (1-P)^N.
  34. *
  35. * We therefore inform the progress indicator of the number B
  36. * (29.34/B), so that it knows how much to increment by each
  37. * time. We do this in 16-bit fixed point, so 29.34 becomes
  38. * 0x1D.57C4.
  39. */
  40. pfn(pfnparam, PROGFN_PHASE_EXTENT, 1, 0x10000);
  41. pfn(pfnparam, PROGFN_EXP_PHASE, 1, -0x1D57C4 / (bits / 2));
  42. pfn(pfnparam, PROGFN_PHASE_EXTENT, 2, 0x10000);
  43. pfn(pfnparam, PROGFN_EXP_PHASE, 2, -0x1D57C4 / (bits - bits / 2));
  44. pfn(pfnparam, PROGFN_PHASE_EXTENT, 3, 0x4000);
  45. pfn(pfnparam, PROGFN_LIN_PHASE, 3, 5);
  46. pfn(pfnparam, PROGFN_READY, 0, 0);
  47. /*
  48. * We don't generate e; we just use a standard one always.
  49. */
  50. key->exponent = bignum_from_long(RSA_EXPONENT);
  51. /*
  52. * Generate p and q: primes with combined length `bits', not
  53. * congruent to 1 modulo e. (Strictly speaking, we wanted (p-1)
  54. * and e to be coprime, and (q-1) and e to be coprime, but in
  55. * general that's slightly more fiddly to arrange. By choosing
  56. * a prime e, we can simplify the criterion.)
  57. */
  58. invent_firstbits(&pfirst, &qfirst);
  59. key->p = primegen(bits / 2, RSA_EXPONENT, 1, NULL,
  60. 1, pfn, pfnparam, pfirst);
  61. key->q = primegen(bits - bits / 2, RSA_EXPONENT, 1, NULL,
  62. 2, pfn, pfnparam, qfirst);
  63. /*
  64. * Ensure p > q, by swapping them if not.
  65. */
  66. if (bignum_cmp(key->p, key->q) < 0) {
  67. Bignum t = key->p;
  68. key->p = key->q;
  69. key->q = t;
  70. }
  71. /*
  72. * Now we have p, q and e. All we need to do now is work out
  73. * the other helpful quantities: n=pq, d=e^-1 mod (p-1)(q-1),
  74. * and (q^-1 mod p).
  75. */
  76. pfn(pfnparam, PROGFN_PROGRESS, 3, 1);
  77. key->modulus = bigmul(key->p, key->q);
  78. pfn(pfnparam, PROGFN_PROGRESS, 3, 2);
  79. pm1 = copybn(key->p);
  80. decbn(pm1);
  81. qm1 = copybn(key->q);
  82. decbn(qm1);
  83. phi_n = bigmul(pm1, qm1);
  84. pfn(pfnparam, PROGFN_PROGRESS, 3, 3);
  85. freebn(pm1);
  86. freebn(qm1);
  87. key->private_exponent = modinv(key->exponent, phi_n);
  88. assert(key->private_exponent);
  89. pfn(pfnparam, PROGFN_PROGRESS, 3, 4);
  90. key->iqmp = modinv(key->q, key->p);
  91. assert(key->iqmp);
  92. pfn(pfnparam, PROGFN_PROGRESS, 3, 5);
  93. /*
  94. * Clean up temporary numbers.
  95. */
  96. freebn(phi_n);
  97. return 1;
  98. }