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