sshdssg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * DSS key generation.
  3. */
  4. #include "misc.h"
  5. #include "ssh.h"
  6. int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
  7. void *pfnparam)
  8. {
  9. Bignum qm1, power, g, h, tmp;
  10. unsigned pfirst, qfirst;
  11. int progress;
  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, 0x2800);
  41. pfn(pfnparam, PROGFN_EXP_PHASE, 1, -0x1D57C4 / 160);
  42. pfn(pfnparam, PROGFN_PHASE_EXTENT, 2, 0x40 * bits);
  43. pfn(pfnparam, PROGFN_EXP_PHASE, 2, -0x1D57C4 / bits);
  44. /*
  45. * In phase three we are finding an order-q element of the
  46. * multiplicative group of p, by finding an element whose order
  47. * is _divisible_ by q and raising it to the power of (p-1)/q.
  48. * _Most_ elements will have order divisible by q, since for a
  49. * start phi(p) of them will be primitive roots. So
  50. * realistically we don't need to set this much below 1 (64K).
  51. * Still, we'll set it to 1/2 (32K) to be on the safe side.
  52. */
  53. pfn(pfnparam, PROGFN_PHASE_EXTENT, 3, 0x2000);
  54. pfn(pfnparam, PROGFN_EXP_PHASE, 3, -32768);
  55. /*
  56. * In phase four we are finding an element x between 1 and q-1
  57. * (exclusive), by inventing 160 random bits and hoping they
  58. * come out to a plausible number; so assuming q is uniformly
  59. * distributed between 2^159 and 2^160, the chance of any given
  60. * attempt succeeding is somewhere between 0.5 and 1. Lacking
  61. * the energy to arrange to be able to specify this probability
  62. * _after_ generating q, we'll just set it to 0.75.
  63. */
  64. pfn(pfnparam, PROGFN_PHASE_EXTENT, 4, 0x2000);
  65. pfn(pfnparam, PROGFN_EXP_PHASE, 4, -49152);
  66. pfn(pfnparam, PROGFN_READY, 0, 0);
  67. invent_firstbits(&pfirst, &qfirst);
  68. /*
  69. * Generate q: a prime of length 160.
  70. */
  71. key->q = primegen(160, 2, 2, NULL, 1, pfn, pfnparam, qfirst);
  72. /*
  73. * Now generate p: a prime of length `bits', such that p-1 is
  74. * divisible by q.
  75. */
  76. key->p = primegen(bits-160, 2, 2, key->q, 2, pfn, pfnparam, pfirst);
  77. /*
  78. * Next we need g. Raise 2 to the power (p-1)/q modulo p, and
  79. * if that comes out to one then try 3, then 4 and so on. As
  80. * soon as we hit a non-unit (and non-zero!) one, that'll do
  81. * for g.
  82. */
  83. power = bigdiv(key->p, key->q); /* this is floor(p/q) == (p-1)/q */
  84. h = bignum_from_long(1);
  85. progress = 0;
  86. while (1) {
  87. pfn(pfnparam, PROGFN_PROGRESS, 3, ++progress);
  88. g = modpow(h, power, key->p);
  89. if (bignum_cmp(g, One) > 0)
  90. break; /* got one */
  91. tmp = h;
  92. h = bignum_add_long(h, 1);
  93. freebn(tmp);
  94. }
  95. key->g = g;
  96. freebn(h);
  97. /*
  98. * Now we're nearly done. All we need now is our private key x,
  99. * which should be a number between 1 and q-1 exclusive, and
  100. * our public key y = g^x mod p.
  101. */
  102. qm1 = copybn(key->q);
  103. decbn(qm1);
  104. progress = 0;
  105. while (1) {
  106. int i, v, byte, bitsleft;
  107. Bignum x;
  108. pfn(pfnparam, PROGFN_PROGRESS, 4, ++progress);
  109. x = bn_power_2(159);
  110. byte = 0;
  111. bitsleft = 0;
  112. for (i = 0; i < 160; i++) {
  113. if (bitsleft <= 0)
  114. bitsleft = 8, byte = random_byte();
  115. v = byte & 1;
  116. byte >>= 1;
  117. bitsleft--;
  118. bignum_set_bit(x, i, v);
  119. }
  120. if (bignum_cmp(x, One) <= 0 || bignum_cmp(x, qm1) >= 0) {
  121. freebn(x);
  122. continue;
  123. } else {
  124. key->x = x;
  125. break;
  126. }
  127. }
  128. freebn(qm1);
  129. key->y = modpow(key->g, key->x, key->p);
  130. return 1;
  131. }