Mercurial > dropbear
comparison genrsa.c @ 4:fe6bca95afa7
Makefile.in contains updated files required
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 01 Jun 2004 02:46:09 +0000 |
parents | |
children | 29a5c7c62350 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 4:fe6bca95afa7 |
---|---|
1 /* | |
2 * Dropbear - a SSH2 server | |
3 * | |
4 * Copyright (c) 2002,2003 Matt Johnston | |
5 * All rights reserved. | |
6 * | |
7 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 * of this software and associated documentation files (the "Software"), to deal | |
9 * in the Software without restriction, including without limitation the rights | |
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 * copies of the Software, and to permit persons to whom the Software is | |
12 * furnished to do so, subject to the following conditions: | |
13 * | |
14 * The above copyright notice and this permission notice shall be included in | |
15 * all copies or substantial portions of the Software. | |
16 * | |
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 * SOFTWARE. */ | |
24 | |
25 #include "includes.h" | |
26 #include "dbutil.h" | |
27 #include "bignum.h" | |
28 #include "random.h" | |
29 #include "rsa.h" | |
30 #include "genrsa.h" | |
31 | |
32 #define RSA_E 65537 | |
33 | |
34 #ifdef DROPBEAR_RSA | |
35 | |
36 static void getrsaprime(mp_int* prime, mp_int *primeminus, | |
37 mp_int* rsa_e, unsigned int size); | |
38 | |
39 /* mostly taken from libtomcrypt's rsa key generation routine */ | |
40 rsa_key * gen_rsa_priv_key(unsigned int size) { | |
41 | |
42 rsa_key * key; | |
43 mp_int pminus, qminus, lcm; | |
44 | |
45 key = (rsa_key*)m_malloc(sizeof(rsa_key)); | |
46 | |
47 key->e = (mp_int*)m_malloc(sizeof(mp_int)); | |
48 key->n = (mp_int*)m_malloc(sizeof(mp_int)); | |
49 key->d = (mp_int*)m_malloc(sizeof(mp_int)); | |
50 key->p = (mp_int*)m_malloc(sizeof(mp_int)); | |
51 key->q = (mp_int*)m_malloc(sizeof(mp_int)); | |
52 | |
53 m_mp_init_multi(key->e, key->n, key->d, key->p, key->q, | |
54 &pminus, &lcm, &qminus, NULL); | |
55 | |
56 seedrandom(); | |
57 | |
58 if (mp_set_int(key->e, RSA_E) != MP_OKAY) { | |
59 fprintf(stderr, "rsa generation failed\n"); | |
60 exit(1); | |
61 } | |
62 | |
63 /* PuTTY doesn't like it if the modulus isn't a multiple of 8 bits, | |
64 * so we just generate them until we get one which is OK */ | |
65 getrsaprime(key->p, &pminus, key->e, size/2); | |
66 do { | |
67 getrsaprime(key->q, &qminus, key->e, size/2); | |
68 | |
69 if (mp_mul(key->p, key->q, key->n) != MP_OKAY) { | |
70 fprintf(stderr, "rsa generation failed\n"); | |
71 exit(1); | |
72 } | |
73 } while (mp_count_bits(key->n) % 8 != 0); | |
74 | |
75 /* lcm(p-1, q-1) */ | |
76 if (mp_lcm(&pminus, &qminus, &lcm) != MP_OKAY) { | |
77 fprintf(stderr, "rsa generation failed\n"); | |
78 exit(1); | |
79 } | |
80 | |
81 /* de = 1 mod lcm(p-1,q-1) */ | |
82 /* therefore d = (e^-1) mod lcm(p-1,q-1) */ | |
83 if (mp_invmod(key->e, &lcm, key->d) != MP_OKAY) { | |
84 fprintf(stderr, "rsa generation failed\n"); | |
85 exit(1); | |
86 } | |
87 | |
88 mp_clear_multi(&pminus, &qminus, &lcm, NULL); | |
89 | |
90 return key; | |
91 } | |
92 | |
93 /* return a prime suitable for p or q */ | |
94 static void getrsaprime(mp_int* prime, mp_int *primeminus, | |
95 mp_int* rsa_e, unsigned int size) { | |
96 | |
97 unsigned char *buf; | |
98 mp_int temp_gcd; | |
99 | |
100 buf = (unsigned char*)m_malloc(size+1); | |
101 | |
102 m_mp_init(&temp_gcd); | |
103 do { | |
104 /* generate a random odd number with MSB set, then find the | |
105 the next prime above it */ | |
106 genrandom(buf, size+1); | |
107 buf[0] |= 0x80; /* MSB set */ | |
108 | |
109 if (mp_read_unsigned_bin(prime, buf, size+1) != MP_OKAY) { | |
110 fprintf(stderr, "rsa generation failed\n"); | |
111 exit(1); | |
112 } | |
113 | |
114 /* find the next integer which is prime, 8 round of miller-rabin */ | |
115 if (mp_prime_next_prime(prime, 8, 0) != MP_OKAY) { | |
116 fprintf(stderr, "rsa generation failed\n"); | |
117 exit(1); | |
118 } | |
119 | |
120 /* subtract one to get p-1 */ | |
121 if (mp_sub_d(prime, 1, primeminus) != MP_OKAY) { | |
122 fprintf(stderr, "rsa generation failed\n"); | |
123 exit(1); | |
124 } | |
125 /* check relative primality to e */ | |
126 if (mp_gcd(primeminus, rsa_e, &temp_gcd) != MP_OKAY) { | |
127 fprintf(stderr, "rsa generation failed\n"); | |
128 exit(1); | |
129 } | |
130 } while (mp_cmp_d(&temp_gcd, 1) != MP_EQ); /* while gcd(p-1, e) != 1 */ | |
131 | |
132 /* now we have a good value for result */ | |
133 mp_clear(&temp_gcd); | |
134 m_burn(buf, size+1); | |
135 m_free(buf); | |
136 } | |
137 | |
138 #endif /* DROPBEAR_RSA */ |