xref: /freebsd/crypto/openssl/crypto/rsa/rsa_gen.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2025 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 "prov/providercommon.h"
28 #include "rsa_local.h"
29 
30 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg);
31 static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes,
32     BIGNUM *e_value, BN_GENCB *cb, int pairwise_test);
33 
34 /*
35  * NB: this wrapper would normally be placed in rsa_lib.c and the static
36  * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
37  * so that we don't introduce a new linker dependency. Eg. any application
38  * that wasn't previously linking object code related to key-generation won't
39  * have to now just because key-generation is part of RSA_METHOD.
40  */
RSA_generate_key_ex(RSA * rsa,int bits,BIGNUM * e_value,BN_GENCB * cb)41 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
42 {
43     if (rsa->meth->rsa_keygen != NULL)
44         return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
45 
46     return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM,
47         e_value, cb);
48 }
49 
RSA_generate_multi_prime_key(RSA * rsa,int bits,int primes,BIGNUM * e_value,BN_GENCB * cb)50 int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
51     BIGNUM *e_value, BN_GENCB *cb)
52 {
53 #ifndef FIPS_MODULE
54     /* multi-prime is only supported with the builtin key generation */
55     if (rsa->meth->rsa_multi_prime_keygen != NULL) {
56         return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,
57             e_value, cb);
58     } else if (rsa->meth->rsa_keygen != NULL) {
59         /*
60          * However, if rsa->meth implements only rsa_keygen, then we
61          * have to honour it in 2-prime case and assume that it wouldn't
62          * know what to do with multi-prime key generated by builtin
63          * subroutine...
64          */
65         if (primes == 2)
66             return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
67         else
68             return 0;
69     }
70 #endif /* FIPS_MODULE */
71     return rsa_keygen(rsa->libctx, rsa, bits, primes, e_value, cb, 0);
72 }
73 
DEFINE_STACK_OF(BIGNUM)74 DEFINE_STACK_OF(BIGNUM)
75 
76 /*
77  * Given input values, q, p, n, d and e, derive the exponents
78  * and coefficients for each prime in this key, placing the result
79  * on their respective exps and coeffs stacks
80  */
81 #ifndef FIPS_MODULE
82 int ossl_rsa_multiprime_derive(RSA *rsa, int bits, int primes,
83     BIGNUM *e_value,
84     STACK_OF(BIGNUM) *factors,
85     STACK_OF(BIGNUM) *exps,
86     STACK_OF(BIGNUM) *coeffs)
87 {
88     STACK_OF(BIGNUM) *pplist = NULL, *pdlist = NULL;
89     BIGNUM *factor = NULL, *newpp = NULL, *newpd = NULL;
90     BIGNUM *dval = NULL, *newexp = NULL, *newcoeff = NULL;
91     BIGNUM *p = NULL, *q = NULL;
92     BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
93     BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL;
94     BN_CTX *ctx = NULL;
95     BIGNUM *tmp = NULL;
96     int i;
97     int ret = 0;
98 
99     ctx = BN_CTX_new_ex(rsa->libctx);
100     if (ctx == NULL)
101         goto err;
102 
103     BN_CTX_start(ctx);
104 
105     pplist = sk_BIGNUM_new_null();
106     if (pplist == NULL)
107         goto err;
108 
109     pdlist = sk_BIGNUM_new_null();
110     if (pdlist == NULL)
111         goto err;
112 
113     r0 = BN_CTX_get(ctx);
114     r1 = BN_CTX_get(ctx);
115     r2 = BN_CTX_get(ctx);
116 
117     if (r2 == NULL)
118         goto err;
119 
120     BN_set_flags(r0, BN_FLG_CONSTTIME);
121     BN_set_flags(r1, BN_FLG_CONSTTIME);
122     BN_set_flags(r2, BN_FLG_CONSTTIME);
123 
124     if (BN_copy(r1, rsa->n) == NULL)
125         goto err;
126 
127     p = sk_BIGNUM_value(factors, 0);
128     q = sk_BIGNUM_value(factors, 1);
129 
130     /* Build list of partial products of primes */
131     for (i = 0; i < sk_BIGNUM_num(factors); i++) {
132         switch (i) {
133         case 0:
134             /* our first prime, p */
135             if (!BN_sub(r2, p, BN_value_one()))
136                 goto err;
137             BN_set_flags(r2, BN_FLG_CONSTTIME);
138             if (BN_mod_inverse(r1, r2, rsa->e, ctx) == NULL)
139                 goto err;
140             break;
141         case 1:
142             /* second prime q */
143             if (!BN_mul(r1, p, q, ctx))
144                 goto err;
145             tmp = BN_dup(r1);
146             if (tmp == NULL)
147                 goto err;
148             if (!sk_BIGNUM_insert(pplist, tmp, sk_BIGNUM_num(pplist)))
149                 goto err;
150             tmp = NULL;
151             break;
152         default:
153             factor = sk_BIGNUM_value(factors, i);
154             /* all other primes */
155             if (!BN_mul(r1, r1, factor, ctx))
156                 goto err;
157             tmp = BN_dup(r1);
158             if (tmp == NULL)
159                 goto err;
160             if (!sk_BIGNUM_insert(pplist, tmp, sk_BIGNUM_num(pplist)))
161                 goto err;
162             tmp = NULL;
163             break;
164         }
165     }
166 
167     /* build list of relative d values */
168     /* p -1 */
169     if (!BN_sub(r1, p, BN_value_one()))
170         goto err;
171     if (!BN_sub(r2, q, BN_value_one()))
172         goto err;
173     if (!BN_mul(r0, r1, r2, ctx))
174         goto err;
175     for (i = 2; i < sk_BIGNUM_num(factors); i++) {
176         factor = sk_BIGNUM_value(factors, i);
177         dval = BN_new();
178         if (dval == NULL)
179             goto err;
180         BN_set_flags(dval, BN_FLG_CONSTTIME);
181         if (!BN_sub(dval, factor, BN_value_one()))
182             goto err;
183         if (!BN_mul(r0, r0, dval, ctx))
184             goto err;
185         if (!sk_BIGNUM_insert(pdlist, dval, sk_BIGNUM_num(pdlist)))
186             goto err;
187         dval = NULL;
188     }
189 
190     /* Calculate dmp1, dmq1 and additional exponents */
191     dmp1 = BN_secure_new();
192     if (dmp1 == NULL)
193         goto err;
194     dmq1 = BN_secure_new();
195     if (dmq1 == NULL)
196         goto err;
197 
198     if (!BN_mod(dmp1, rsa->d, r1, ctx))
199         goto err;
200     if (!sk_BIGNUM_insert(exps, dmp1, sk_BIGNUM_num(exps)))
201         goto err;
202     dmp1 = NULL;
203 
204     if (!BN_mod(dmq1, rsa->d, r2, ctx))
205         goto err;
206     if (!sk_BIGNUM_insert(exps, dmq1, sk_BIGNUM_num(exps)))
207         goto err;
208     dmq1 = NULL;
209 
210     for (i = 2; i < sk_BIGNUM_num(factors); i++) {
211         newpd = sk_BIGNUM_value(pdlist, i - 2);
212         newexp = BN_new();
213         if (newexp == NULL)
214             goto err;
215         if (!BN_mod(newexp, rsa->d, newpd, ctx))
216             goto err;
217         if (!sk_BIGNUM_insert(exps, newexp, sk_BIGNUM_num(exps)))
218             goto err;
219         newexp = NULL;
220     }
221 
222     /* Calculate iqmp and additional coefficients */
223     iqmp = BN_new();
224     if (iqmp == NULL)
225         goto err;
226 
227     if (BN_mod_inverse(iqmp, sk_BIGNUM_value(factors, 1),
228             sk_BIGNUM_value(factors, 0), ctx)
229         == NULL)
230         goto err;
231     if (!sk_BIGNUM_insert(coeffs, iqmp, sk_BIGNUM_num(coeffs)))
232         goto err;
233     iqmp = NULL;
234 
235     for (i = 2; i < sk_BIGNUM_num(factors); i++) {
236         newpp = sk_BIGNUM_value(pplist, i - 2);
237         newcoeff = BN_new();
238         if (newcoeff == NULL)
239             goto err;
240         if (BN_mod_inverse(newcoeff, newpp, sk_BIGNUM_value(factors, i),
241                 ctx)
242             == NULL)
243             goto err;
244         if (!sk_BIGNUM_insert(coeffs, newcoeff, sk_BIGNUM_num(coeffs)))
245             goto err;
246         newcoeff = NULL;
247     }
248 
249     ret = 1;
250 err:
251     BN_free(newcoeff);
252     BN_free(newexp);
253     BN_free(dval);
254     BN_free(tmp);
255     sk_BIGNUM_pop_free(pplist, BN_free);
256     sk_BIGNUM_pop_free(pdlist, BN_free);
257     BN_CTX_end(ctx);
258     BN_CTX_free(ctx);
259     BN_clear_free(dmp1);
260     BN_clear_free(dmq1);
261     BN_clear_free(iqmp);
262     return ret;
263 }
264 
rsa_multiprime_keygen(RSA * rsa,int bits,int primes,BIGNUM * e_value,BN_GENCB * cb)265 static int rsa_multiprime_keygen(RSA *rsa, int bits, int primes,
266     BIGNUM *e_value, BN_GENCB *cb)
267 {
268     BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *tmp2, *prime;
269     int n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
270     int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
271     RSA_PRIME_INFO *pinfo = NULL;
272     STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;
273     STACK_OF(BIGNUM) *factors = NULL;
274     STACK_OF(BIGNUM) *exps = NULL;
275     STACK_OF(BIGNUM) *coeffs = NULL;
276     BN_CTX *ctx = NULL;
277     BN_ULONG bitst = 0;
278     unsigned long error = 0;
279     int ok = -1;
280 
281     if (bits < RSA_MIN_MODULUS_BITS) {
282         ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
283         return 0;
284     }
285     if (e_value == NULL) {
286         ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
287         return 0;
288     }
289     /* A bad value for e can cause infinite loops */
290     if (!ossl_rsa_check_public_exponent(e_value)) {
291         ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
292         return 0;
293     }
294 
295     if (primes < RSA_DEFAULT_PRIME_NUM || primes > ossl_rsa_multip_cap(bits)) {
296         ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
297         return 0;
298     }
299 
300     factors = sk_BIGNUM_new_null();
301     if (factors == NULL)
302         return 0;
303 
304     exps = sk_BIGNUM_new_null();
305     if (exps == NULL)
306         goto err;
307 
308     coeffs = sk_BIGNUM_new_null();
309     if (coeffs == NULL)
310         goto err;
311 
312     ctx = BN_CTX_new_ex(rsa->libctx);
313     if (ctx == NULL)
314         goto err;
315     BN_CTX_start(ctx);
316     r0 = BN_CTX_get(ctx);
317     r1 = BN_CTX_get(ctx);
318     r2 = BN_CTX_get(ctx);
319     if (r2 == NULL)
320         goto err;
321 
322     /* divide bits into 'primes' pieces evenly */
323     quo = bits / primes;
324     rmd = bits % primes;
325 
326     for (i = 0; i < primes; i++)
327         bitsr[i] = (i < rmd) ? quo + 1 : quo;
328 
329     rsa->dirty_cnt++;
330 
331     /* We need the RSA components non-NULL */
332     if (!rsa->n && ((rsa->n = BN_new()) == NULL))
333         goto err;
334     if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))
335         goto err;
336     BN_set_flags(rsa->d, BN_FLG_CONSTTIME);
337     if (!rsa->e && ((rsa->e = BN_new()) == NULL))
338         goto err;
339     if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))
340         goto err;
341     BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
342     if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))
343         goto err;
344     BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
345 
346     /* initialize multi-prime components */
347     if (primes > RSA_DEFAULT_PRIME_NUM) {
348         rsa->version = RSA_ASN1_VERSION_MULTI;
349         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);
350         if (prime_infos == NULL)
351             goto err;
352         if (rsa->prime_infos != NULL) {
353             /* could this happen? */
354             sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos,
355                 ossl_rsa_multip_info_free);
356         }
357         rsa->prime_infos = prime_infos;
358 
359         /* prime_info from 2 to |primes| -1 */
360         for (i = 2; i < primes; i++) {
361             pinfo = ossl_rsa_multip_info_new();
362             if (pinfo == NULL)
363                 goto err;
364             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
365         }
366     }
367 
368     if (BN_copy(rsa->e, e_value) == NULL)
369         goto err;
370 
371     /* generate p, q and other primes (if any) */
372     for (i = 0; i < primes; i++) {
373         adj = 0;
374         retries = 0;
375 
376         if (i == 0) {
377             prime = rsa->p;
378         } else if (i == 1) {
379             prime = rsa->q;
380         } else {
381             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
382             prime = pinfo->r;
383         }
384         BN_set_flags(prime, BN_FLG_CONSTTIME);
385 
386         for (;;) {
387         redo:
388             if (!BN_generate_prime_ex2(prime, bitsr[i] + adj, 0, NULL, NULL,
389                     cb, ctx))
390                 goto err;
391             /*
392              * prime should not be equal to p, q, r_3...
393              * (those primes prior to this one)
394              */
395             {
396                 int j;
397 
398                 for (j = 0; j < i; j++) {
399                     BIGNUM *prev_prime;
400 
401                     if (j == 0)
402                         prev_prime = rsa->p;
403                     else if (j == 1)
404                         prev_prime = rsa->q;
405                     else
406                         prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
407                             j - 2)
408                                          ->r;
409 
410                     if (!BN_cmp(prime, prev_prime)) {
411                         goto redo;
412                     }
413                 }
414             }
415             if (!BN_sub(r2, prime, BN_value_one()))
416                 goto err;
417             ERR_set_mark();
418             BN_set_flags(r2, BN_FLG_CONSTTIME);
419             if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
420                 /* GCD == 1 since inverse exists */
421                 break;
422             }
423             error = ERR_peek_last_error();
424             if (ERR_GET_LIB(error) == ERR_LIB_BN
425                 && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
426                 /* GCD != 1 */
427                 ERR_pop_to_mark();
428             } else {
429                 goto err;
430             }
431             if (!BN_GENCB_call(cb, 2, n++))
432                 goto err;
433         }
434 
435         bitse += bitsr[i];
436 
437         /* calculate n immediately to see if it's sufficient */
438         if (i == 1) {
439             /* we get at least 2 primes */
440             if (!BN_mul(r1, rsa->p, rsa->q, ctx))
441                 goto err;
442         } else if (i != 0) {
443             /* modulus n = p * q * r_3 * r_4 ... */
444             if (!BN_mul(r1, rsa->n, prime, ctx))
445                 goto err;
446         } else {
447             /* i == 0, do nothing */
448             if (!BN_GENCB_call(cb, 3, i))
449                 goto err;
450             tmp = BN_dup(prime);
451             if (tmp == NULL)
452                 goto err;
453             if (!sk_BIGNUM_insert(factors, tmp, sk_BIGNUM_num(factors)))
454                 goto err;
455             continue;
456         }
457 
458         /*
459          * if |r1|, product of factors so far, is not as long as expected
460          * (by checking the first 4 bits are less than 0x9 or greater than
461          * 0xF). If so, re-generate the last prime.
462          *
463          * NOTE: This actually can't happen in two-prime case, because of
464          * the way factors are generated.
465          *
466          * Besides, another consideration is, for multi-prime case, even the
467          * length modulus is as long as expected, the modulus could start at
468          * 0x8, which could be utilized to distinguish a multi-prime private
469          * key by using the modulus in a certificate. This is also covered
470          * by checking the length should not be less than 0x9.
471          */
472         if (!BN_rshift(r2, r1, bitse - 4))
473             goto err;
474         bitst = BN_get_word(r2);
475 
476         if (bitst < 0x9 || bitst > 0xF) {
477             /*
478              * For keys with more than 4 primes, we attempt longer factor to
479              * meet length requirement.
480              *
481              * Otherwise, we just re-generate the prime with the same length.
482              *
483              * This strategy has the following goals:
484              *
485              * 1. 1024-bit factors are efficient when using 3072 and 4096-bit key
486              * 2. stay the same logic with normal 2-prime key
487              */
488             bitse -= bitsr[i];
489             if (!BN_GENCB_call(cb, 2, n++))
490                 goto err;
491             if (primes > 4) {
492                 if (bitst < 0x9)
493                     adj++;
494                 else
495                     adj--;
496             } else if (retries == 4) {
497                 /*
498                  * re-generate all primes from scratch, mainly used
499                  * in 4 prime case to avoid long loop. Max retry times
500                  * is set to 4.
501                  */
502                 i = -1;
503                 bitse = 0;
504                 sk_BIGNUM_pop_free(factors, BN_clear_free);
505                 factors = sk_BIGNUM_new_null();
506                 if (factors == NULL)
507                     goto err;
508                 continue;
509             }
510             retries++;
511             goto redo;
512         }
513         /* save product of primes for further use, for multi-prime only */
514         if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
515             goto err;
516         if (BN_copy(rsa->n, r1) == NULL)
517             goto err;
518         if (!BN_GENCB_call(cb, 3, i))
519             goto err;
520         tmp = BN_dup(prime);
521         if (tmp == NULL)
522             goto err;
523         if (!sk_BIGNUM_insert(factors, tmp, sk_BIGNUM_num(factors)))
524             goto err;
525     }
526 
527     if (BN_cmp(rsa->p, rsa->q) < 0) {
528         tmp = rsa->p;
529         rsa->p = rsa->q;
530         rsa->q = tmp;
531         /* mirror this in our factor stack */
532         if (!sk_BIGNUM_insert(factors, sk_BIGNUM_delete(factors, 0), 1))
533             goto err;
534     }
535 
536     /* calculate d */
537 
538     /* p - 1 */
539     if (!BN_sub(r1, rsa->p, BN_value_one()))
540         goto err;
541     /* q - 1 */
542     if (!BN_sub(r2, rsa->q, BN_value_one()))
543         goto err;
544     /* (p - 1)(q - 1) */
545     if (!BN_mul(r0, r1, r2, ctx))
546         goto err;
547     /* multi-prime */
548     for (i = 2; i < primes; i++) {
549         pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
550         /* save r_i - 1 to pinfo->d temporarily */
551         if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
552             goto err;
553         if (!BN_mul(r0, r0, pinfo->d, ctx))
554             goto err;
555     }
556 
557     BN_set_flags(r0, BN_FLG_CONSTTIME);
558     if (BN_mod_inverse(rsa->d, rsa->e, r0, ctx) == NULL) {
559         goto err; /* d */
560     }
561 
562     /* derive any missing exponents and coefficients */
563     if (!ossl_rsa_multiprime_derive(rsa, bits, primes, e_value,
564             factors, exps, coeffs))
565         goto err;
566 
567     /*
568      * first 2 factors/exps are already tracked in p/q/dmq1/dmp1
569      * and the first coeff is in iqmp, so pop those off the stack
570      * Note, the first 2 factors/exponents are already tracked by p and q
571      * assign dmp1/dmq1 and iqmp
572      * the remaining pinfo values are separately allocated, so copy and delete
573      * those
574      */
575     BN_clear_free(sk_BIGNUM_delete(factors, 0));
576     BN_clear_free(sk_BIGNUM_delete(factors, 0));
577     rsa->dmp1 = sk_BIGNUM_delete(exps, 0);
578     rsa->dmq1 = sk_BIGNUM_delete(exps, 0);
579     rsa->iqmp = sk_BIGNUM_delete(coeffs, 0);
580     for (i = 2; i < primes; i++) {
581         pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
582         tmp = sk_BIGNUM_delete(factors, 0);
583         BN_copy(pinfo->r, tmp);
584         BN_clear_free(tmp);
585         tmp = sk_BIGNUM_delete(exps, 0);
586         tmp2 = BN_copy(pinfo->d, tmp);
587         BN_clear_free(tmp);
588         if (tmp2 == NULL)
589             goto err;
590         tmp = sk_BIGNUM_delete(coeffs, 0);
591         tmp2 = BN_copy(pinfo->t, tmp);
592         BN_clear_free(tmp);
593         if (tmp2 == NULL)
594             goto err;
595     }
596     ok = 1;
597 err:
598     sk_BIGNUM_free(factors);
599     sk_BIGNUM_free(exps);
600     sk_BIGNUM_free(coeffs);
601     if (ok == -1) {
602         ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
603         ok = 0;
604     }
605     BN_CTX_end(ctx);
606     BN_CTX_free(ctx);
607     return ok;
608 }
609 #endif /* FIPS_MODULE */
610 
rsa_keygen(OSSL_LIB_CTX * libctx,RSA * rsa,int bits,int primes,BIGNUM * e_value,BN_GENCB * cb,int pairwise_test)611 static int rsa_keygen(OSSL_LIB_CTX *libctx, RSA *rsa, int bits, int primes,
612     BIGNUM *e_value, BN_GENCB *cb, int pairwise_test)
613 {
614     int ok = 0;
615 
616 #ifdef FIPS_MODULE
617     ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb);
618     pairwise_test = 1; /* FIPS MODE needs to always run the pairwise test */
619 #else
620     /*
621      * Only multi-prime keys or insecure keys with a small key length or a
622      * public exponent <= 2^16 will use the older rsa_multiprime_keygen().
623      */
624     if (primes == 2
625         && bits >= 2048
626         && (e_value == NULL || BN_num_bits(e_value) > 16))
627         ok = ossl_rsa_sp800_56b_generate_key(rsa, bits, e_value, cb);
628     else
629         ok = rsa_multiprime_keygen(rsa, bits, primes, e_value, cb);
630 #endif /* FIPS_MODULE */
631 
632     if (pairwise_test && ok > 0) {
633         OSSL_CALLBACK *stcb = NULL;
634         void *stcbarg = NULL;
635 
636         OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
637         ok = rsa_keygen_pairwise_test(rsa, stcb, stcbarg);
638         if (!ok) {
639             ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
640             /* Clear intermediate results */
641             BN_clear_free(rsa->d);
642             BN_clear_free(rsa->p);
643             BN_clear_free(rsa->q);
644             BN_clear_free(rsa->dmp1);
645             BN_clear_free(rsa->dmq1);
646             BN_clear_free(rsa->iqmp);
647             rsa->d = NULL;
648             rsa->p = NULL;
649             rsa->q = NULL;
650             rsa->dmp1 = NULL;
651             rsa->dmq1 = NULL;
652             rsa->iqmp = NULL;
653         }
654     }
655     return ok;
656 }
657 
658 /*
659  * AS10.35 (and its VEs/TEs) of the FIPS 140-3 standard requires a PCT for every
660  * generated key pair. There are 3 options:
661  * 1) If the key pair is to be used for key transport (asymmetric cipher), the
662  *    PCT consists of encrypting a plaintext, verifying that the result
663  *    (ciphertext) is not equal to the plaintext, decrypting the ciphertext, and
664  *    verifying that the result is equal to the plaintext.
665  * 2) If the key pair is to be used for digital signatures, the PCT consists of
666  *    computing and verifying a signature.
667  * 3) If the key pair is to be used for key agreement, the exact PCT is defined
668  *    in the applicable standards. For RSA-based schemes, this is defined in
669  *    SP 800-56Br2 (Section 6.4.1.1) as:
670  *    "The owner shall perform a pair-wise consistency test by verifying that m
671  *    = (m^e)^d mod n for some integer m satisfying 1 < m < (n - 1)."
672  *
673  * OpenSSL implements all three use cases: RSA-OAEP for key transport,
674  * RSA signatures with PKCS#1 v1.5 or PSS padding, and KAS-IFC-SSC (KAS1/KAS2)
675  * using RSASVE.
676  *
677  * According to FIPS 140-3 IG 10.3.A, if at the time when the PCT is performed
678  * the keys' intended usage is not known, then any of the three PCTs described
679  * in AS10.35 shall be performed on this key pair.
680  *
681  * Because of this allowance from the IG, the simplest option is 3, i.e.
682  * RSA_public_encrypt() and RSA_private_decrypt() with RSA_NO_PADDING.
683  */
rsa_keygen_pairwise_test(RSA * rsa,OSSL_CALLBACK * cb,void * cbarg)684 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
685 {
686     int ret = 0;
687     unsigned char *plaintxt = NULL;
688     unsigned char *ciphertxt = NULL;
689     unsigned char *decoded = NULL;
690     int plaintxt_len;
691     int ciphertxt_len;
692     int decoded_len;
693     int padding = RSA_NO_PADDING;
694     OSSL_SELF_TEST *st = NULL;
695 
696     st = OSSL_SELF_TEST_new(cb, cbarg);
697     if (st == NULL)
698         goto err;
699     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
700         OSSL_SELF_TEST_DESC_PCT_RSA);
701 
702     /*
703      * For RSA_NO_PADDING, RSA_public_encrypt() and RSA_private_decrypt()
704      * require the 'to' and 'from' parameters to have equal length and a
705      * maximum of RSA_size() - allocate space for plaintxt, ciphertxt, and
706      * decoded.
707      */
708     plaintxt_len = RSA_size(rsa);
709     plaintxt = OPENSSL_zalloc(plaintxt_len * 3);
710     if (plaintxt == NULL)
711         goto err;
712     ciphertxt = plaintxt + plaintxt_len;
713     decoded = ciphertxt + plaintxt_len;
714 
715     /* SP 800-56Br2 Section 6.4.1.1 requires that plaintext is greater than 1 */
716     plaintxt[plaintxt_len - 1] = 2;
717 
718     ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
719         padding);
720     if (ciphertxt_len <= 0)
721         goto err;
722 
723     OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
724 
725     decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
726         padding);
727     if (decoded_len != plaintxt_len
728         || memcmp(decoded, plaintxt, decoded_len) != 0)
729         goto err;
730 
731     ret = 1;
732 err:
733     OSSL_SELF_TEST_onend(st, ret);
734     OSSL_SELF_TEST_free(st);
735     OPENSSL_free(plaintxt);
736 
737     return ret;
738 }
739