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