3 Generation of RSA keypairs
5 Copyright (C) 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
42 #include "rsa-internal.h"
55 rsa_generate_keypair(struct rsa_public_key *pub,
56 struct rsa_private_key *key,
57 void *random_ctx, nettle_random_func *random,
58 void *progress_ctx, nettle_progress_func *progress,
69 /* We should choose e randomly. Is the size reasonable? */
70 if ((e_size < 16) || (e_size >= n_size) )
75 /* We have a fixed e. Check that it makes sense */
78 if (!mpz_tstbit(pub->e, 0))
82 if (mpz_cmp_ui(pub->e, 3) < 0)
85 /* And size less than n */
86 if (mpz_sizeinbase(pub->e, 2) >= n_size)
90 if (n_size < RSA_MINIMUM_N_BITS)
93 mpz_init(p1); mpz_init(q1); mpz_init(phi); mpz_init(tmp);
98 /* Generate p, such that gcd(p-1, e) = 1 */
101 nettle_random_prime(key->p, (n_size+1)/2, 1,
103 progress_ctx, progress);
105 mpz_sub_ui(p1, key->p, 1);
107 /* If e was given, we must choose p such that p-1 has no factors in
112 mpz_gcd(tmp, pub->e, p1);
114 if (mpz_cmp_ui(tmp, 1) == 0)
116 else if (progress) progress(progress_ctx, 'c');
120 progress(progress_ctx, '\n');
122 /* Generate q, such that gcd(q-1, e) = 1 */
125 nettle_random_prime(key->q, n_size/2, 1,
127 progress_ctx, progress);
129 mpz_sub_ui(q1, key->q, 1);
131 /* If e was given, we must choose q such that q-1 has no factors in
136 mpz_gcd(tmp, pub->e, q1);
138 if (mpz_cmp_ui(tmp, 1) == 0)
140 else if (progress) progress(progress_ctx, 'c');
143 /* Now we have the primes. Is the product of the right size? */
144 mpz_mul(pub->n, key->p, key->q);
146 assert (mpz_sizeinbase(pub->n, 2) == n_size);
149 progress(progress_ctx, '\n');
151 /* c = q^{-1} (mod p) */
152 if (mpz_invert(key->c, key->q, key->p))
153 /* This should succeed everytime. But if it doesn't,
156 else if (progress) progress(progress_ctx, '?');
159 mpz_mul(phi, p1, q1);
161 /* If we didn't have a given e, generate one now. */
167 nettle_mpz_random_size(pub->e,
171 /* Make sure it's odd and that the most significant bit is
173 mpz_setbit(pub->e, 0);
174 mpz_setbit(pub->e, e_size - 1);
176 /* Needs gmp-3, or inverse might be negative. */
177 if (mpz_invert(key->d, pub->e, phi))
180 if (progress) progress(progress_ctx, 'e');
183 if (retried && progress)
184 progress(progress_ctx, '\n');
188 /* Must always succeed, as we already that e
189 * doesn't have any common factor with p-1 or q-1. */
190 int res = mpz_invert(key->d, pub->e, phi);
194 /* Done! Almost, we must compute the auxillary private values. */
196 mpz_fdiv_r(key->a, key->d, p1);
199 mpz_fdiv_r(key->b, key->d, q1);
201 /* c was computed earlier */
203 pub->size = key->size = (n_size + 7) / 8;
204 assert(pub->size >= RSA_MINIMUM_N_OCTETS);
206 mpz_clear(p1); mpz_clear(q1); mpz_clear(phi); mpz_clear(tmp);