dsa.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * DSA key generation.
  3. */
  4. #include "misc.h"
  5. #include "ssh.h"
  6. #include "sshkeygen.h"
  7. #include "mpint.h"
  8. int dsa_generate(struct dsa_key *key, int bits, PrimeGenerationContext *pgc,
  9. ProgressReceiver *prog)
  10. {
  11. /*
  12. * Progress-reporting setup.
  13. *
  14. * DSA generation involves three potentially long jobs: inventing
  15. * the small prime q, the large prime p, and finding an order-q
  16. * element of the multiplicative group of p.
  17. *
  18. * The latter is done by finding an element whose order is
  19. * _divisible_ by q and raising it to the power of (p-1)/q. Every
  20. * element whose order is not divisible by q is a qth power of q
  21. * distinct elements whose order _is_ divisible by q, so the
  22. * probability of not finding a suitable element on the first try
  23. * is in the region of 1/q, i.e. at most 2^-159.
  24. *
  25. * (So the probability of success will end up indistinguishable
  26. * from 1 in IEEE standard floating point! But what can you do.)
  27. */
  28. ProgressPhase phase_q = primegen_add_progress_phase(pgc, prog, 160);
  29. ProgressPhase phase_p = primegen_add_progress_phase(pgc, prog, bits);
  30. double g_failure_probability = 1.0
  31. / (double)(1ULL << 53)
  32. / (double)(1ULL << 53)
  33. / (double)(1ULL << 53);
  34. ProgressPhase phase_g = progress_add_probabilistic(
  35. prog, estimate_modexp_cost(bits), 1.0 - g_failure_probability);
  36. progress_ready(prog);
  37. PrimeCandidateSource *pcs;
  38. /*
  39. * Generate q: a prime of length 160.
  40. */
  41. progress_start_phase(prog, phase_q);
  42. pcs = pcs_new(160);
  43. mp_int *q = primegen_generate(pgc, pcs, prog);
  44. progress_report_phase_complete(prog);
  45. /*
  46. * Now generate p: a prime of length `bits', such that p-1 is
  47. * divisible by q.
  48. */
  49. progress_start_phase(prog, phase_p);
  50. pcs = pcs_new(bits);
  51. pcs_require_residue_1_mod_prime(pcs, q);
  52. mp_int *p = primegen_generate(pgc, pcs, prog);
  53. progress_report_phase_complete(prog);
  54. /*
  55. * Next we need g. Raise 2 to the power (p-1)/q modulo p, and
  56. * if that comes out to one then try 3, then 4 and so on. As
  57. * soon as we hit a non-unit (and non-zero!) one, that'll do
  58. * for g.
  59. */
  60. progress_start_phase(prog, phase_g);
  61. mp_int *power = mp_div(p, q); /* this is floor(p/q) == (p-1)/q */
  62. mp_int *h = mp_from_integer(2);
  63. mp_int *g;
  64. while (1) {
  65. progress_report_attempt(prog);
  66. g = mp_modpow(h, power, p);
  67. if (mp_hs_integer(g, 2))
  68. break; /* got one */
  69. mp_free(g);
  70. mp_add_integer_into(h, h, 1);
  71. }
  72. mp_free(h);
  73. mp_free(power);
  74. progress_report_phase_complete(prog);
  75. /*
  76. * Now we're nearly done. All we need now is our private key x,
  77. * which should be a number between 1 and q-1 exclusive, and
  78. * our public key y = g^x mod p.
  79. */
  80. mp_int *two = mp_from_integer(2);
  81. mp_int *qm1 = mp_copy(q);
  82. mp_sub_integer_into(qm1, qm1, 1);
  83. mp_int *x = mp_random_in_range(two, qm1);
  84. mp_free(two);
  85. mp_free(qm1);
  86. key->sshk.vt = &ssh_dsa;
  87. key->p = p;
  88. key->q = q;
  89. key->g = g;
  90. key->x = x;
  91. key->y = mp_modpow(key->g, key->x, key->p);
  92. return 1;
  93. }