Flag RSA secret BNs as consttime on keygen and checks
[openssl.git] / crypto / rsa / rsa_gen.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * NB: these functions have been "upgraded", the deprecated versions (which
12  * are compatibility wrappers using these functions) are in rsa_depr.c. -
13  * Geoff
14  */
15
16 /*
17  * RSA low level APIs are deprecated for public use, but still ok for
18  * internal use.
19  */
20 #include "internal/deprecated.h"
21
22 #include <stdio.h>
23 #include <time.h>
24 #include "internal/cryptlib.h"
25 #include <openssl/bn.h>
26 #include <openssl/self_test.h>
27 #include "rsa_local.h"
28
29 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg);
30 static int rsa_keygen(OPENSSL_CTX *libctx, RSA *rsa, int bits, int primes,
31                       BIGNUM *e_value, BN_GENCB *cb, int pairwise_test);
32
33 /*
34  * NB: this wrapper would normally be placed in rsa_lib.c and the static
35  * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
36  * so that we don't introduce a new linker dependency. Eg. any application
37  * that wasn't previously linking object code related to key-generation won't
38  * have to now just because key-generation is part of RSA_METHOD.
39  */
40 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
41 {
42     if (rsa->meth->rsa_keygen != NULL)
43         return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
44
45     return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM,
46                                         e_value, cb);
47 }
48
49 int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
50                                  BIGNUM *e_value, BN_GENCB *cb)
51 {
52 #ifndef FIPS_MODULE
53     /* multi-prime is only supported with the builtin key generation */
54     if (rsa->meth->rsa_multi_prime_keygen != NULL) {
55         return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,
56                                                  e_value, cb);
57     } else if (rsa->meth->rsa_keygen != NULL) {
58         /*
59          * However, if rsa->meth implements only rsa_keygen, then we
60          * have to honour it in 2-prime case and assume that it wouldn't
61          * know what to do with multi-prime key generated by builtin
62          * subroutine...
63          */
64         if (primes == 2)
65             return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
66         else
67             return 0;
68     }
69 #endif /* FIPS_MODUKE */
70     return rsa_keygen(rsa->libctx, rsa, bits, primes, e_value, cb, 0);
71 }
72
73 #ifndef FIPS_MODULE
74 static int rsa_multiprime_keygen(RSA *rsa, int bits, int primes,
75                                  BIGNUM *e_value, BN_GENCB *cb)
76 {
77     BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;
78     int n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
79     int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
80     RSA_PRIME_INFO *pinfo = NULL;
81     STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;
82     BN_CTX *ctx = NULL;
83     BN_ULONG bitst = 0;
84     unsigned long error = 0;
85     int ok = -1;
86
87     if (bits < RSA_MIN_MODULUS_BITS) {
88         ok = 0;             /* we set our own err */
89         RSAerr(0, RSA_R_KEY_SIZE_TOO_SMALL);
90         goto err;
91     }
92
93     /* A bad value for e can cause infinite loops */
94     if (e_value != NULL && !rsa_check_public_exponent(e_value)) {
95         RSAerr(0, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
96         return 0;
97     }
98
99     if (primes < RSA_DEFAULT_PRIME_NUM || primes > rsa_multip_cap(bits)) {
100         ok = 0;             /* we set our own err */
101         RSAerr(0, RSA_R_KEY_PRIME_NUM_INVALID);
102         goto err;
103     }
104
105     ctx = BN_CTX_new();
106     if (ctx == NULL)
107         goto err;
108     BN_CTX_start(ctx);
109     r0 = BN_CTX_get(ctx);
110     r1 = BN_CTX_get(ctx);
111     r2 = BN_CTX_get(ctx);
112     if (r2 == NULL)
113         goto err;
114
115     /* divide bits into 'primes' pieces evenly */
116     quo = bits / primes;
117     rmd = bits % primes;
118
119     for (i = 0; i < primes; i++)
120         bitsr[i] = (i < rmd) ? quo + 1 : quo;
121
122     rsa->dirty_cnt++;
123
124     /* We need the RSA components non-NULL */
125     if (!rsa->n && ((rsa->n = BN_new()) == NULL))
126         goto err;
127     if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))
128         goto err;
129     BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
130     if (!rsa->e && ((rsa->e = BN_new()) == NULL))
131         goto err;
132     if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))
133         goto err;
134     BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
135     if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))
136         goto err;
137     BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
138     if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))
139         goto err;
140     BN_set_flags(rsa->dmp1, BN_FLG_CONSTTIME);
141     if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))
142         goto err;
143     BN_set_flags(rsa->dmq1, BN_FLG_CONSTTIME);
144     if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))
145         goto err;
146     BN_set_flags(rsa->iqmp, BN_FLG_CONSTTIME);
147
148     /* initialize multi-prime components */
149     if (primes > RSA_DEFAULT_PRIME_NUM) {
150         rsa->version = RSA_ASN1_VERSION_MULTI;
151         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);
152         if (prime_infos == NULL)
153             goto err;
154         if (rsa->prime_infos != NULL) {
155             /* could this happen? */
156             sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, rsa_multip_info_free);
157         }
158         rsa->prime_infos = prime_infos;
159
160         /* prime_info from 2 to |primes| -1 */
161         for (i = 2; i < primes; i++) {
162             pinfo = rsa_multip_info_new();
163             if (pinfo == NULL)
164                 goto err;
165             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
166         }
167     }
168
169     if (BN_copy(rsa->e, e_value) == NULL)
170         goto err;
171
172     /* generate p, q and other primes (if any) */
173     for (i = 0; i < primes; i++) {
174         adj = 0;
175         retries = 0;
176
177         if (i == 0) {
178             prime = rsa->p;
179         } else if (i == 1) {
180             prime = rsa->q;
181         } else {
182             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
183             prime = pinfo->r;
184         }
185         BN_set_flags(prime, BN_FLG_CONSTTIME);
186
187         for (;;) {
188  redo:
189             if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))
190                 goto err;
191             /*
192              * prime should not be equal to p, q, r_3...
193              * (those primes prior to this one)
194              */
195             {
196                 int j;
197
198                 for (j = 0; j < i; j++) {
199                     BIGNUM *prev_prime;
200
201                     if (j == 0)
202                         prev_prime = rsa->p;
203                     else if (j == 1)
204                         prev_prime = rsa->q;
205                     else
206                         prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
207                                                              j - 2)->r;
208
209                     if (!BN_cmp(prime, prev_prime)) {
210                         goto redo;
211                     }
212                 }
213             }
214             if (!BN_sub(r2, prime, BN_value_one()))
215                 goto err;
216             ERR_set_mark();
217             BN_set_flags(r2, BN_FLG_CONSTTIME);
218             if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
219                /* GCD == 1 since inverse exists */
220                 break;
221             }
222             error = ERR_peek_last_error();
223             if (ERR_GET_LIB(error) == ERR_LIB_BN
224                 && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
225                 /* GCD != 1 */
226                 ERR_pop_to_mark();
227             } else {
228                 goto err;
229             }
230             if (!BN_GENCB_call(cb, 2, n++))
231                 goto err;
232         }
233
234         bitse += bitsr[i];
235
236         /* calculate n immediately to see if it's sufficient */
237         if (i == 1) {
238             /* we get at least 2 primes */
239             if (!BN_mul(r1, rsa->p, rsa->q, ctx))
240                 goto err;
241         } else if (i != 0) {
242             /* modulus n = p * q * r_3 * r_4 ... */
243             if (!BN_mul(r1, rsa->n, prime, ctx))
244                 goto err;
245         } else {
246             /* i == 0, do nothing */
247             if (!BN_GENCB_call(cb, 3, i))
248                 goto err;
249             continue;
250         }
251         /*
252          * if |r1|, product of factors so far, is not as long as expected
253          * (by checking the first 4 bits are less than 0x9 or greater than
254          * 0xF). If so, re-generate the last prime.
255          *
256          * NOTE: This actually can't happen in two-prime case, because of
257          * the way factors are generated.
258          *
259          * Besides, another consideration is, for multi-prime case, even the
260          * length modulus is as long as expected, the modulus could start at
261          * 0x8, which could be utilized to distinguish a multi-prime private
262          * key by using the modulus in a certificate. This is also covered
263          * by checking the length should not be less than 0x9.
264          */
265         if (!BN_rshift(r2, r1, bitse - 4))
266             goto err;
267         bitst = BN_get_word(r2);
268
269         if (bitst < 0x9 || bitst > 0xF) {
270             /*
271              * For keys with more than 4 primes, we attempt longer factor to
272              * meet length requirement.
273              *
274              * Otherwise, we just re-generate the prime with the same length.
275              *
276              * This strategy has the following goals:
277              *
278              * 1. 1024-bit factors are efficient when using 3072 and 4096-bit key
279              * 2. stay the same logic with normal 2-prime key
280              */
281             bitse -= bitsr[i];
282             if (!BN_GENCB_call(cb, 2, n++))
283                 goto err;
284             if (primes > 4) {
285                 if (bitst < 0x9)
286                     adj++;
287                 else
288                     adj--;
289             } else if (retries == 4) {
290                 /*
291                  * re-generate all primes from scratch, mainly used
292                  * in 4 prime case to avoid long loop. Max retry times
293                  * is set to 4.
294                  */
295                 i = -1;
296                 bitse = 0;
297                 continue;
298             }
299             retries++;
300             goto redo;
301         }
302         /* save product of primes for further use, for multi-prime only */
303         if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
304             goto err;
305         if (BN_copy(rsa->n, r1) == NULL)
306             goto err;
307         if (!BN_GENCB_call(cb, 3, i))
308             goto err;
309     }
310
311     if (BN_cmp(rsa->p, rsa->q) < 0) {
312         tmp = rsa->p;
313         rsa->p = rsa->q;
314         rsa->q = tmp;
315     }
316
317     /* calculate d */
318
319     /* p - 1 */
320     if (!BN_sub(r1, rsa->p, BN_value_one()))
321         goto err;
322     /* q - 1 */
323     if (!BN_sub(r2, rsa->q, BN_value_one()))
324         goto err;
325     /* (p - 1)(q - 1) */
326     if (!BN_mul(r0, r1, r2, ctx))
327         goto err;
328     /* multi-prime */
329     for (i = 2; i < primes; i++) {
330         pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
331         /* save r_i - 1 to pinfo->d temporarily */
332         if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
333             goto err;
334         if (!BN_mul(r0, r0, pinfo->d, ctx))
335             goto err;
336     }
337
338     {
339         BIGNUM *pr0 = BN_new();
340
341         if (pr0 == NULL)
342             goto err;
343
344         BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
345         if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
346             BN_free(pr0);
347             goto err;               /* d */
348         }
349         /* We MUST free pr0 before any further use of r0 */
350         BN_free(pr0);
351     }
352
353     {
354         BIGNUM *d = BN_new();
355
356         if (d == NULL)
357             goto err;
358
359         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
360
361         /* calculate d mod (p-1) and d mod (q - 1) */
362         if (!BN_mod(rsa->dmp1, d, r1, ctx)
363             || !BN_mod(rsa->dmq1, d, r2, ctx)) {
364             BN_free(d);
365             goto err;
366         }
367
368         /* calculate CRT exponents */
369         for (i = 2; i < primes; i++) {
370             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
371             /* pinfo->d == r_i - 1 */
372             if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {
373                 BN_free(d);
374                 goto err;
375             }
376         }
377
378         /* We MUST free d before any further use of rsa->d */
379         BN_free(d);
380     }
381
382     {
383         BIGNUM *p = BN_new();
384
385         if (p == NULL)
386             goto err;
387         BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
388
389         /* calculate inverse of q mod p */
390         if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
391             BN_free(p);
392             goto err;
393         }
394
395         /* calculate CRT coefficient for other primes */
396         for (i = 2; i < primes; i++) {
397             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
398             BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);
399             if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {
400                 BN_free(p);
401                 goto err;
402             }
403         }
404
405         /* We MUST free p before any further use of rsa->p */
406         BN_free(p);
407     }
408
409     ok = 1;
410  err:
411     if (ok == -1) {
412         RSAerr(0, ERR_LIB_BN);
413         ok = 0;
414     }
415     BN_CTX_end(ctx);
416     BN_CTX_free(ctx);
417     return ok;
418 }
419 #endif /* FIPS_MODULE */
420
421 static int rsa_keygen(OPENSSL_CTX *libctx, RSA *rsa, int bits, int primes,
422                       BIGNUM *e_value, BN_GENCB *cb, int pairwise_test)
423 {
424     int ok = 0;
425
426     /*
427      * Only multi-prime keys or insecure keys with a small key length will use
428      * the older rsa_multiprime_keygen().
429      */
430     if (primes == 2 && bits >= 2048)
431         ok = rsa_sp800_56b_generate_key(rsa, bits, e_value, cb);
432 #ifndef FIPS_MODULE
433     else
434         ok = rsa_multiprime_keygen(rsa, bits, primes, e_value, cb);
435 #endif /* FIPS_MODULE */
436
437 #ifdef FIPS_MODULE
438     pairwise_test = 1; /* FIPS MODE needs to always run the pairwise test */
439 #endif
440     if (pairwise_test && ok > 0) {
441         OSSL_CALLBACK *stcb = NULL;
442         void *stcbarg = NULL;
443
444         OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
445         ok = rsa_keygen_pairwise_test(rsa, stcb, stcbarg);
446         if (!ok) {
447             /* Clear intermediate results */
448             BN_clear_free(rsa->d);
449             BN_clear_free(rsa->p);
450             BN_clear_free(rsa->q);
451             BN_clear_free(rsa->dmp1);
452             BN_clear_free(rsa->dmq1);
453             BN_clear_free(rsa->iqmp);
454         }
455     }
456     return ok;
457 }
458
459 /*
460  * For RSA key generation it is not known whether the key pair will be used
461  * for key transport or signatures. FIPS 140-2 IG 9.9 states that in this case
462  * either a signature verification OR an encryption operation may be used to
463  * perform the pairwise consistency check. The simpler encrypt/decrypt operation
464  * has been chosen for this case.
465  */
466 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
467 {
468     int ret = 0;
469     unsigned int ciphertxt_len;
470     unsigned char *ciphertxt = NULL;
471     const unsigned char plaintxt[16] = {0};
472     unsigned char decoded[256];
473     unsigned int decoded_len;
474     unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len);
475     int padding = RSA_PKCS1_PADDING;
476     OSSL_SELF_TEST *st = NULL;
477
478     st = OSSL_SELF_TEST_new(cb, cbarg);
479     if (st == NULL)
480         goto err;
481     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
482                            OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1);
483
484     ciphertxt_len = RSA_size(rsa);
485     ciphertxt = OPENSSL_zalloc(ciphertxt_len);
486     if (ciphertxt == NULL)
487         goto err;
488
489     ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
490                                        padding);
491     if (ciphertxt_len <= 0)
492         goto err;
493     if (ciphertxt_len == plaintxt_len
494         && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0)
495         goto err;
496
497     OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
498
499     decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
500                                       padding);
501     if (decoded_len != plaintxt_len
502         || memcmp(decoded, plaintxt,  decoded_len) != 0)
503         goto err;
504
505     ret = 1;
506 err:
507     OSSL_SELF_TEST_onend(st, ret);
508     OSSL_SELF_TEST_free(st);
509     OPENSSL_free(ciphertxt);
510
511     return ret;
512 }