3 Purpose: Generate keys for the RSA cryptosystem.
4 Author: M. J. Fromberger
6 Usage: rsakey [-e <expt>] <modbits> [<outfile>]
8 Generates an RSA key pair with a modulus having <modbits> significant bits,
9 and writes it to the specified output file, or to the standard output. The
10 -e option allows the user to specify an encryption exponent; otherwise, an
11 encryption exponent is chosen at random.
13 Primes p and q are obtained by reading random bits from /dev/random, setting
14 the low-order bit, and testing for primality. If the first candidate is not
15 prime, successive odd candidates are tried until a probable prime is found.
17 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
19 Permission is hereby granted, free of charge, to any person obtaining a copy
20 of this software and associated documentation files (the "Software"), to deal
21 in the Software without restriction, including without limitation the rights
22 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 copies of the Software, and to permit persons to whom the Software is
24 furnished to do so, subject to the following conditions:
26 The above copyright notice and this permission notice shall be included in
27 all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58 /* Load the specified buffer with random bytes */
59 int randomize(unsigned char *buf
, size_t len
);
61 /* Overwrite the specified value with n_bits random bits */
62 mp_result
mp_int_randomize(mp_int a
, mp_size n_bits
);
64 /* Find a prime starting from the given odd seed */
65 mp_result
find_prime(mp_int seed
, FILE *fb
);
67 /* Initialize/destroy an rsa_key structure */
68 mp_result
rsa_key_init(rsa_key
*kp
);
69 void rsa_key_clear(rsa_key
*kp
);
70 void rsa_key_write(rsa_key
*kp
, FILE *ofp
);
72 int main(int argc
, char *argv
[]) {
79 /* Process command-line arguments */
80 while ((opt
= getopt(argc
, argv
, "e:")) != EOF
) {
86 fprintf(stderr
, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n");
92 fprintf(stderr
, "Error: You must specify the number of modulus bits.\n");
93 fprintf(stderr
, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n");
96 modbits
= (int)strtol(argv
[optind
++], NULL
, 0);
97 if (modbits
< CHAR_BIT
) {
98 fprintf(stderr
, "Error: Invalid value for number of modulus bits.\n");
101 if (modbits
% 2 == 1) ++modbits
;
103 /* Check if output file is specified */
105 if ((ofp
= fopen(argv
[optind
], "wt")) == NULL
) {
107 "Error: Unable to open output file for writing.\n"
110 argv
[optind
], strerror(errno
));
115 if ((res
= rsa_key_init(&the_key
)) != MP_OK
) {
117 "Error initializing RSA key structure:\n"
119 mp_error_string(res
), res
);
123 /* If specified, try to load the key exponent */
125 if ((res
= mp_int_read_string(&(the_key
.e
), 10, expt
)) != MP_OK
) {
127 "Error: Invalid value for encryption exponent.\n"
129 mp_error_string(res
), res
);
134 if ((res
= mp_int_randomize(&(the_key
.p
), (modbits
/ 2))) != MP_OK
) {
136 "Error: Unable to randomize first prime.\n"
138 mp_error_string(res
), res
);
141 fprintf(stderr
, "p: ");
142 find_prime(&(the_key
.p
), stderr
);
144 if ((res
= mp_int_randomize(&(the_key
.q
), (modbits
/ 2))) != MP_OK
) {
146 "Error: Unable to randomize second prime.\n"
148 mp_error_string(res
), res
);
151 fprintf(stderr
, "\nq: ");
152 find_prime(&(the_key
.q
), stderr
);
155 /* Temporarily, the key's "n" field will be (p - 1) * (q - 1) for
156 purposes of computing the decryption exponent.
158 mp_int_mul(&(the_key
.p
), &(the_key
.q
), &(the_key
.n
));
159 mp_int_sub(&(the_key
.n
), &(the_key
.p
), &(the_key
.n
));
160 mp_int_sub(&(the_key
.n
), &(the_key
.q
), &(the_key
.n
));
161 mp_int_add_value(&(the_key
.n
), 1, &(the_key
.n
));
164 (res
= mp_int_randomize(&(the_key
.e
), (modbits
/ 2))) != MP_OK
) {
166 "Error: Unable to randomize encryption exponent.\n"
168 mp_error_string(res
), res
);
171 while ((res
= mp_int_invmod(&(the_key
.e
), &(the_key
.n
), &(the_key
.d
))) !=
175 "Error: Unable to compute decryption exponent.\n"
177 mp_error_string(res
), res
);
180 if ((res
= mp_int_randomize(&(the_key
.e
), (modbits
/ 2))) != MP_OK
) {
182 "Error: Unable to re-randomize encryption exponent.\n"
184 mp_error_string(res
), res
);
189 /* Recompute the real modulus, now that exponents are done. */
190 mp_int_mul(&(the_key
.p
), &(the_key
.q
), &(the_key
.n
));
192 /* Write completed key to the specified output file */
193 rsa_key_write(&the_key
, ofp
);
197 rsa_key_clear(&the_key
);
201 int randomize(unsigned char *buf
, size_t len
) {
202 FILE *rnd
= fopen("/dev/random", "rb");
205 if (rnd
== NULL
) return -1;
207 nr
= fread(buf
, sizeof(*buf
), len
, rnd
);
213 mp_result
mp_int_randomize(mp_int a
, mp_size n_bits
) {
214 mp_size n_bytes
= (n_bits
+ CHAR_BIT
- 1) / CHAR_BIT
;
216 mp_result res
= MP_OK
;
218 if ((buf
= malloc(n_bytes
)) == NULL
) return MP_MEMORY
;
220 if ((mp_size
)randomize(buf
, n_bytes
) != n_bytes
) {
225 /* Clear bits beyond the number requested */
226 if (n_bits
% CHAR_BIT
!= 0) {
227 unsigned char b_mask
= (1 << (n_bits
% CHAR_BIT
)) - 1;
228 unsigned char t_mask
= (1 << (n_bits
% CHAR_BIT
)) >> 1;
234 /* Set low-order bit to insure value is odd */
235 buf
[n_bytes
- 1] |= 1;
237 res
= mp_int_read_unsigned(a
, buf
, n_bytes
);
240 memset(buf
, 0, n_bytes
);
246 mp_result
find_prime(mp_int seed
, FILE *fb
) {
250 if (mp_int_is_even(seed
))
251 if ((res
= mp_int_add_value(seed
, 1, seed
)) != MP_OK
) return res
;
253 while ((res
= mp_int_is_prime(seed
)) == MP_FALSE
) {
256 if (fb
!= NULL
&& (count
% 50) == 0) fputc('.', fb
);
258 if ((res
= mp_int_add_value(seed
, 2, seed
)) != MP_OK
) return res
;
261 if (res
== MP_TRUE
&& fb
!= NULL
) fputc('+', fb
);
266 mp_result
rsa_key_init(rsa_key
*kp
) {
267 mp_int_init(&(kp
->p
));
268 mp_int_init(&(kp
->q
));
269 mp_int_init(&(kp
->n
));
270 mp_int_init(&(kp
->e
));
271 mp_int_init(&(kp
->d
));
276 void rsa_key_clear(rsa_key
*kp
) {
277 mp_int_clear(&(kp
->p
));
278 mp_int_clear(&(kp
->q
));
279 mp_int_clear(&(kp
->n
));
280 mp_int_clear(&(kp
->e
));
281 mp_int_clear(&(kp
->d
));
284 void rsa_key_write(rsa_key
*kp
, FILE *ofp
) {
288 len
= mp_int_string_len(&(kp
->n
), 10);
290 mp_int_to_string(&(kp
->p
), 10, obuf
, len
);
291 fprintf(ofp
, "p = %s\n", obuf
);
292 mp_int_to_string(&(kp
->q
), 10, obuf
, len
);
293 fprintf(ofp
, "q = %s\n", obuf
);
294 mp_int_to_string(&(kp
->e
), 10, obuf
, len
);
295 fprintf(ofp
, "e = %s\n", obuf
);
296 mp_int_to_string(&(kp
->d
), 10, obuf
, len
);
297 fprintf(ofp
, "d = %s\n", obuf
);
298 mp_int_to_string(&(kp
->n
), 10, obuf
, len
);
299 fprintf(ofp
, "n = %s\n", obuf
);
304 /* Here there be dragons */