2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * NB: these functions have been "upgraded", the deprecated versions (which
12 * are compatibility wrappers using these functions) are in rsa_depr.c. -
17 * RSA low level APIs are deprecated for public use, but still ok for
20 #include "internal/deprecated.h"
24 #include "internal/cryptlib.h"
25 #include <openssl/bn.h>
26 #include "rsa_local.h"
28 static int rsa_builtin_keygen(RSA
*rsa
, int bits
, int primes
, BIGNUM
*e_value
,
32 * NB: this wrapper would normally be placed in rsa_lib.c and the static
33 * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
34 * so that we don't introduce a new linker dependency. Eg. any application
35 * that wasn't previously linking object code related to key-generation won't
36 * have to now just because key-generation is part of RSA_METHOD.
38 int RSA_generate_key_ex(RSA
*rsa
, int bits
, BIGNUM
*e_value
, BN_GENCB
*cb
)
40 if (rsa
->meth
->rsa_keygen
!= NULL
)
41 return rsa
->meth
->rsa_keygen(rsa
, bits
, e_value
, cb
);
43 return RSA_generate_multi_prime_key(rsa
, bits
, RSA_DEFAULT_PRIME_NUM
,
47 int RSA_generate_multi_prime_key(RSA
*rsa
, int bits
, int primes
,
48 BIGNUM
*e_value
, BN_GENCB
*cb
)
51 /* multi-prime is only supported with the builtin key generation */
52 if (rsa
->meth
->rsa_multi_prime_keygen
!= NULL
) {
53 return rsa
->meth
->rsa_multi_prime_keygen(rsa
, bits
, primes
,
55 } else if (rsa
->meth
->rsa_keygen
!= NULL
) {
57 * However, if rsa->meth implements only rsa_keygen, then we
58 * have to honour it in 2-prime case and assume that it wouldn't
59 * know what to do with multi-prime key generated by builtin
63 return rsa
->meth
->rsa_keygen(rsa
, bits
, e_value
, cb
);
67 #endif /* FIPS_MODE */
68 return rsa_builtin_keygen(rsa
, bits
, primes
, e_value
, cb
);
71 static int rsa_builtin_keygen(RSA
*rsa
, int bits
, int primes
, BIGNUM
*e_value
,
77 return rsa_sp800_56b_generate_key(rsa
, bits
, e_value
, cb
);
79 BIGNUM
*r0
= NULL
, *r1
= NULL
, *r2
= NULL
, *tmp
, *prime
;
80 int ok
= -1, n
= 0, bitsr
[RSA_MAX_PRIME_NUM
], bitse
= 0;
81 int i
= 0, quo
= 0, rmd
= 0, adj
= 0, retries
= 0;
82 RSA_PRIME_INFO
*pinfo
= NULL
;
83 STACK_OF(RSA_PRIME_INFO
) *prime_infos
= NULL
;
86 unsigned long error
= 0;
88 if (bits
< RSA_MIN_MODULUS_BITS
) {
89 ok
= 0; /* we set our own err */
90 RSAerr(RSA_F_RSA_BUILTIN_KEYGEN
, RSA_R_KEY_SIZE_TOO_SMALL
);
94 if (primes
< RSA_DEFAULT_PRIME_NUM
|| primes
> rsa_multip_cap(bits
)) {
95 ok
= 0; /* we set our own err */
96 RSAerr(RSA_F_RSA_BUILTIN_KEYGEN
, RSA_R_KEY_PRIME_NUM_INVALID
);
104 r0
= BN_CTX_get(ctx
);
105 r1
= BN_CTX_get(ctx
);
106 r2
= BN_CTX_get(ctx
);
110 /* divide bits into 'primes' pieces evenly */
114 for (i
= 0; i
< primes
; i
++)
115 bitsr
[i
] = (i
< rmd
) ? quo
+ 1 : quo
;
119 /* We need the RSA components non-NULL */
120 if (!rsa
->n
&& ((rsa
->n
= BN_new()) == NULL
))
122 if (!rsa
->d
&& ((rsa
->d
= BN_secure_new()) == NULL
))
124 if (!rsa
->e
&& ((rsa
->e
= BN_new()) == NULL
))
126 if (!rsa
->p
&& ((rsa
->p
= BN_secure_new()) == NULL
))
128 if (!rsa
->q
&& ((rsa
->q
= BN_secure_new()) == NULL
))
130 if (!rsa
->dmp1
&& ((rsa
->dmp1
= BN_secure_new()) == NULL
))
132 if (!rsa
->dmq1
&& ((rsa
->dmq1
= BN_secure_new()) == NULL
))
134 if (!rsa
->iqmp
&& ((rsa
->iqmp
= BN_secure_new()) == NULL
))
137 /* initialize multi-prime components */
138 if (primes
> RSA_DEFAULT_PRIME_NUM
) {
139 rsa
->version
= RSA_ASN1_VERSION_MULTI
;
140 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, primes
- 2);
141 if (prime_infos
== NULL
)
143 if (rsa
->prime_infos
!= NULL
) {
144 /* could this happen? */
145 sk_RSA_PRIME_INFO_pop_free(rsa
->prime_infos
, rsa_multip_info_free
);
147 rsa
->prime_infos
= prime_infos
;
149 /* prime_info from 2 to |primes| -1 */
150 for (i
= 2; i
< primes
; i
++) {
151 pinfo
= rsa_multip_info_new();
154 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
158 if (BN_copy(rsa
->e
, e_value
) == NULL
)
161 /* generate p, q and other primes (if any) */
162 for (i
= 0; i
< primes
; i
++) {
171 pinfo
= sk_RSA_PRIME_INFO_value(prime_infos
, i
- 2);
174 BN_set_flags(prime
, BN_FLG_CONSTTIME
);
178 if (!BN_generate_prime_ex(prime
, bitsr
[i
] + adj
, 0, NULL
, NULL
, cb
))
181 * prime should not be equal to p, q, r_3...
182 * (those primes prior to this one)
187 for (j
= 0; j
< i
; j
++) {
195 prev_prime
= sk_RSA_PRIME_INFO_value(prime_infos
,
198 if (!BN_cmp(prime
, prev_prime
)) {
203 if (!BN_sub(r2
, prime
, BN_value_one()))
206 BN_set_flags(r2
, BN_FLG_CONSTTIME
);
207 if (BN_mod_inverse(r1
, r2
, rsa
->e
, ctx
) != NULL
) {
208 /* GCD == 1 since inverse exists */
211 error
= ERR_peek_last_error();
212 if (ERR_GET_LIB(error
) == ERR_LIB_BN
213 && ERR_GET_REASON(error
) == BN_R_NO_INVERSE
) {
219 if (!BN_GENCB_call(cb
, 2, n
++))
225 /* calculate n immediately to see if it's sufficient */
227 /* we get at least 2 primes */
228 if (!BN_mul(r1
, rsa
->p
, rsa
->q
, ctx
))
231 /* modulus n = p * q * r_3 * r_4 ... */
232 if (!BN_mul(r1
, rsa
->n
, prime
, ctx
))
235 /* i == 0, do nothing */
236 if (!BN_GENCB_call(cb
, 3, i
))
241 * if |r1|, product of factors so far, is not as long as expected
242 * (by checking the first 4 bits are less than 0x9 or greater than
243 * 0xF). If so, re-generate the last prime.
245 * NOTE: This actually can't happen in two-prime case, because of
246 * the way factors are generated.
248 * Besides, another consideration is, for multi-prime case, even the
249 * length modulus is as long as expected, the modulus could start at
250 * 0x8, which could be utilized to distinguish a multi-prime private
251 * key by using the modulus in a certificate. This is also covered
252 * by checking the length should not be less than 0x9.
254 if (!BN_rshift(r2
, r1
, bitse
- 4))
256 bitst
= BN_get_word(r2
);
258 if (bitst
< 0x9 || bitst
> 0xF) {
260 * For keys with more than 4 primes, we attempt longer factor to
261 * meet length requirement.
263 * Otherwise, we just re-generate the prime with the same length.
265 * This strategy has the following goals:
267 * 1. 1024-bit factors are efficient when using 3072 and 4096-bit key
268 * 2. stay the same logic with normal 2-prime key
271 if (!BN_GENCB_call(cb
, 2, n
++))
278 } else if (retries
== 4) {
280 * re-generate all primes from scratch, mainly used
281 * in 4 prime case to avoid long loop. Max retry times
291 /* save product of primes for further use, for multi-prime only */
292 if (i
> 1 && BN_copy(pinfo
->pp
, rsa
->n
) == NULL
)
294 if (BN_copy(rsa
->n
, r1
) == NULL
)
296 if (!BN_GENCB_call(cb
, 3, i
))
300 if (BN_cmp(rsa
->p
, rsa
->q
) < 0) {
309 if (!BN_sub(r1
, rsa
->p
, BN_value_one()))
312 if (!BN_sub(r2
, rsa
->q
, BN_value_one()))
315 if (!BN_mul(r0
, r1
, r2
, ctx
))
318 for (i
= 2; i
< primes
; i
++) {
319 pinfo
= sk_RSA_PRIME_INFO_value(prime_infos
, i
- 2);
320 /* save r_i - 1 to pinfo->d temporarily */
321 if (!BN_sub(pinfo
->d
, pinfo
->r
, BN_value_one()))
323 if (!BN_mul(r0
, r0
, pinfo
->d
, ctx
))
328 BIGNUM
*pr0
= BN_new();
333 BN_with_flags(pr0
, r0
, BN_FLG_CONSTTIME
);
334 if (!BN_mod_inverse(rsa
->d
, rsa
->e
, pr0
, ctx
)) {
338 /* We MUST free pr0 before any further use of r0 */
343 BIGNUM
*d
= BN_new();
348 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
350 /* calculate d mod (p-1) and d mod (q - 1) */
351 if (!BN_mod(rsa
->dmp1
, d
, r1
, ctx
)
352 || !BN_mod(rsa
->dmq1
, d
, r2
, ctx
)) {
357 /* calculate CRT exponents */
358 for (i
= 2; i
< primes
; i
++) {
359 pinfo
= sk_RSA_PRIME_INFO_value(prime_infos
, i
- 2);
360 /* pinfo->d == r_i - 1 */
361 if (!BN_mod(pinfo
->d
, d
, pinfo
->d
, ctx
)) {
367 /* We MUST free d before any further use of rsa->d */
372 BIGNUM
*p
= BN_new();
376 BN_with_flags(p
, rsa
->p
, BN_FLG_CONSTTIME
);
378 /* calculate inverse of q mod p */
379 if (!BN_mod_inverse(rsa
->iqmp
, rsa
->q
, p
, ctx
)) {
384 /* calculate CRT coefficient for other primes */
385 for (i
= 2; i
< primes
; i
++) {
386 pinfo
= sk_RSA_PRIME_INFO_value(prime_infos
, i
- 2);
387 BN_with_flags(p
, pinfo
->r
, BN_FLG_CONSTTIME
);
388 if (!BN_mod_inverse(pinfo
->t
, pinfo
->pp
, p
, ctx
)) {
394 /* We MUST free p before any further use of rsa->p */
401 RSAerr(RSA_F_RSA_BUILTIN_KEYGEN
, ERR_LIB_BN
);
407 #endif /* FIPS_MODE */