OSDN Git Service

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