9f40b32a7134b0521afb476d56a0cbb82b771638
3 * @brief Functions for RSA key generation
7 * Copyright (C) 1999, 2000, 2001 Henry Spencer.
8 * Copyright (C) 2005 Jan Hutter, Martin Willi
9 * Hochschule fuer Technik Rapperswil
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * $Id: rsakey.c,v 1.5 2006/01/04 21:16:30 as Exp $
26 #include <sys/types.h>
35 #include "../pluto/constants.h"
36 #include "../pluto/defs.h"
37 #include "../pluto/mp_defs.h"
38 #include "../pluto/log.h"
39 #include "../pluto/pkcs1.h"
43 /* Number of times the probabilistic primality test is applied */
44 #define PRIMECHECK_ROUNDS 30
46 /* Public exponent used for signature key generation */
47 #define PUBLIC_EXPONENT 0x10001
50 #define DEV_RANDOM "/dev/random"
55 * @brief Reads a specific number of bytes from a given device/file
57 * @param[in] nbytes number of bytes to read from random device
58 * @param[out] buf pointer to buffer where to write the data in.
59 * size of buffer has to be at least nbytes.
60 * @return TRUE, if succeeded, FALSE otherwise
64 get_true_random_bytes(size_t nbytes
, char *buf
)
68 char *device
= DEV_RANDOM
;
70 int dev
= open(DEV_RANDOM
, 0);
74 fprintf(stderr
, "could not open random device %s", device
);
79 DBG_log("getting %d bytes from %s...", (int) nbytes
, device
)
83 while (ndone
< nbytes
)
85 got
= read(dev
, buf
+ ndone
, nbytes
- ndone
);
88 fprintf(stderr
, "read error on %s", device
);
93 fprintf(stderr
, "eof on %s", device
);
103 * @brief initialize an mpz_t to a random number, specified bit count
105 * Converting the random value in a value of type mpz_t is done
106 * by creating a hexbuffer.
107 * Converting via hex is a bit weird, but it's the best route GMP gives us.
108 * Note that highmost and lowmost bits are forced on -- highmost to give a
109 * number of exactly the specified length, lowmost so it is an odd number.
111 * @param[out] var uninitialized mpz_t to store th random number in
112 * @param[in] nbits length of var in bits (known to be a multiple of BITS_PER_BYTE)
113 * @return TRUE on success, FALSE otherwise
116 init_random(mpz_t var
, int nbits
)
118 size_t nbytes
= (size_t)(nbits
/BITS_PER_BYTE
);
119 char random_buf
[RSA_MAX_OCTETS
/2];
121 assert(nbytes
<= sizeof(random_buf
));
123 if (!get_true_random_bytes(nbytes
, random_buf
))
126 random_buf
[0] |= 01 << (BITS_PER_BYTE
-1); /* force high bit on */
127 random_buf
[nbytes
-1] |= 01; /* force low bit on */
128 n_to_mpz(var
, random_buf
, nbytes
);
133 * @brief initialize an mpz_t to a random prime of specified size
135 * Efficiency tweak: we reject candidates that are 1 higher than a multiple
136 * of e, since they will make the internal modulus not relatively prime to e.
138 * @param[out] var mpz_t variable to initialize
139 * @param[in] nbits length of given prime in bits (known to be a multiple of BITS_PER_BYTE)
140 * @param[in] eval E-Value, 0 means don't bother w. tweak
141 * @return 1 on success, 0 otherwise
144 init_prime(mpz_t var
, int nbits
, int eval
)
149 /* get a random value of nbits length */
150 if (!init_random(var
, nbits
))
153 /* check if odd number */
154 assert(mpz_fdiv_ui(var
, 2) == 1);
156 DBG_log("looking for a prime starting there (can take a while)...")
160 while (mpz_fdiv_ui(var
, eval
) == 1
161 || !mpz_probab_prime_p(var
, PRIMECHECK_ROUNDS
))
163 /* not a prime, increase by 2 */
164 mpz_add_ui(var
, var
, 2);
168 len
= mpz_sizeinbase(var
, 2);
170 /* check bit length of primee */
171 assert(len
== (size_t)nbits
|| len
== (size_t)(nbits
+1));
173 if (len
== (size_t)(nbits
+1))
176 DBG_log("carry out occurred (!), retrying...")
180 return init_prime(var
, nbits
, eval
);
183 DBG_log("found it after %lu tries.",tries
)
189 * @brief Generate a RSA key usable for encryption
191 * Generate an RSA key usable for encryption. All the
192 * values of the RSA key are filled into mpz_t parameters.
193 * These mpz_t parameters must not be initialized and have
194 * to be cleared with mpz_clear after using.
196 * @param[in] nbits size of rsa key in bits
197 * @return RSA_public_key_t containing the generated RSA key
200 generate_rsa_private_key(int nbits
, RSA_private_key_t
*key
)
202 mpz_t p
, q
, n
, e
, d
, exp1
, exp2
, coeff
;
203 mpz_t m
, q1
, t
; /* temporary variables*/
206 DBG_log("generating %d bit RSA key:", nbits
)
210 return "negative rsa key length!";
212 /* Get values of primes p and q */
214 DBG_log("initialize prime p")
216 if (!init_prime(p
, nbits
/2, PUBLIC_EXPONENT
))
217 return "could not generate prime p";
220 DBG_log("initialize prime q")
222 if (!init_prime(q
, nbits
/2, PUBLIC_EXPONENT
))
223 return "could not generate prime q";
227 /* Swapping primes so p is larger then q */
228 if (mpz_cmp(p
, q
) < 0)
231 DBG_log("swapping primes so p is the larger...")
239 DBG_log("computing modulus...")
245 /* Assign e the value of defined PUBLIC_EXPONENT */
246 mpz_init_set_ui(e
, PUBLIC_EXPONENT
);
249 DBG_log("computing lcm(p-1, q-1)...")
258 mpz_sub_ui(q1
, q1
, 1);
259 /* t = gcd(p-1, q-1) */
261 /* m = (p-1)*(q-1) */
264 mpz_divexact(m
, m
, t
);
265 /* t = gcd(m, e) (greatest common divisor) */
267 /* m and e relatively prime */
268 assert(mpz_cmp_ui(t
, 1) == 0);
272 DBG_log("computing d...")
275 /* e has an inverse mod m */
276 assert(mpz_invert(d
, e
, m
));
278 /* make sure d is positive */
279 if (mpz_cmp_ui(d
, 0) < 0)
282 /* d has to be positive */
283 assert(mpz_cmp(d
, m
) < 0);
285 /* the speedup hacks */
287 DBG_log("computing exp1, exp1, coeff...")
292 /* exp1 = d mod p-1 */
298 /* exp2 = d mod q-1 */
302 /* coeff = q^-1 mod p */
303 mpz_invert(coeff
, q
, p
);
305 /* make sure coeff is positive */
306 if (mpz_cmp_ui(coeff
, 0) < 0)
307 mpz_add(coeff
, coeff
, p
);
309 /* coeff has to be positive */
310 assert(mpz_cmp(coeff
, p
) < 0);
312 /* Clear temporary variables */
317 /* form FreeS/WAN keyid */
319 size_t e_len
= (mpz_sizeinbase(e
,2)+BITS_PER_BYTE
-1)/BITS_PER_BYTE
;
320 size_t n_len
= (mpz_sizeinbase(n
,2)+BITS_PER_BYTE
-1)/BITS_PER_BYTE
;
321 chunk_t e_ch
= mpz_to_n(e
, e_len
);
322 chunk_t n_ch
= mpz_to_n(n
, n_len
);
324 form_keyid(e_ch
, n_ch
, key
->pub
.keyid
, &key
->pub
.k
);
329 /* fill in the elements of the RSA private key */
340 DBG_log("RSA key *%s generated with %d bits", key
->pub
.keyid
341 , (int)mpz_sizeinbase(n
,2))
346 RSA_show_private_key(key
)