Update copyright year
[openssl.git] / crypto / rsa / rsa_lib.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  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/crypto.h>
18 #include <openssl/core_names.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "openssl/param_build.h"
24 #include "crypto/bn.h"
25 #include "crypto/evp.h"
26 #include "crypto/rsa.h"
27 #include "crypto/security_bits.h"
28 #include "rsa_local.h"
29
30 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
31
32 #ifndef FIPS_MODE
33 RSA *RSA_new(void)
34 {
35     return rsa_new_intern(NULL, NULL);
36 }
37
38 const RSA_METHOD *RSA_get_method(const RSA *rsa)
39 {
40     return rsa->meth;
41 }
42
43 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
44 {
45     /*
46      * NB: The caller is specifically setting a method, so it's not up to us
47      * to deal with which ENGINE it comes from.
48      */
49     const RSA_METHOD *mtmp;
50     mtmp = rsa->meth;
51     if (mtmp->finish)
52         mtmp->finish(rsa);
53 #ifndef OPENSSL_NO_ENGINE
54     ENGINE_finish(rsa->engine);
55     rsa->engine = NULL;
56 #endif
57     rsa->meth = meth;
58     if (meth->init)
59         meth->init(rsa);
60     return 1;
61 }
62
63 RSA *RSA_new_method(ENGINE *engine)
64 {
65     return rsa_new_intern(engine, NULL);
66 }
67 #endif
68
69 RSA *rsa_new_with_ctx(OPENSSL_CTX *libctx)
70 {
71     return rsa_new_intern(NULL, libctx);
72 }
73
74 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
75 {
76     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
77
78     if (ret == NULL) {
79         RSAerr(0, ERR_R_MALLOC_FAILURE);
80         return NULL;
81     }
82
83     ret->references = 1;
84     ret->lock = CRYPTO_THREAD_lock_new();
85     if (ret->lock == NULL) {
86         RSAerr(0, ERR_R_MALLOC_FAILURE);
87         OPENSSL_free(ret);
88         return NULL;
89     }
90
91     ret->libctx = libctx;
92     ret->meth = RSA_get_default_method();
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
94     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
95     if (engine) {
96         if (!ENGINE_init(engine)) {
97             RSAerr(0, ERR_R_ENGINE_LIB);
98             goto err;
99         }
100         ret->engine = engine;
101     } else {
102         ret->engine = ENGINE_get_default_RSA();
103     }
104     if (ret->engine) {
105         ret->meth = ENGINE_get_RSA(ret->engine);
106         if (ret->meth == NULL) {
107             RSAerr(0, ERR_R_ENGINE_LIB);
108             goto err;
109         }
110     }
111 #endif
112
113     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
114 #ifndef FIPS_MODE
115     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
116         goto err;
117     }
118 #endif
119
120     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
121         RSAerr(0, ERR_R_INIT_FAIL);
122         goto err;
123     }
124
125     return ret;
126
127  err:
128     RSA_free(ret);
129     return NULL;
130 }
131
132 void RSA_free(RSA *r)
133 {
134     int i;
135
136     if (r == NULL)
137         return;
138
139     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
140     REF_PRINT_COUNT("RSA", r);
141     if (i > 0)
142         return;
143     REF_ASSERT_ISNT(i < 0);
144
145     if (r->meth != NULL && r->meth->finish != NULL)
146         r->meth->finish(r);
147 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
148     ENGINE_finish(r->engine);
149 #endif
150
151 #ifndef FIPS_MODE
152     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
153 #endif
154
155     CRYPTO_THREAD_lock_free(r->lock);
156
157     BN_free(r->n);
158     BN_free(r->e);
159     BN_clear_free(r->d);
160     BN_clear_free(r->p);
161     BN_clear_free(r->q);
162     BN_clear_free(r->dmp1);
163     BN_clear_free(r->dmq1);
164     BN_clear_free(r->iqmp);
165     /* TODO(3.0): Support PSS in FIPS_MODE */
166 #ifndef FIPS_MODE
167     RSA_PSS_PARAMS_free(r->pss);
168     sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
169 #endif
170     BN_BLINDING_free(r->blinding);
171     BN_BLINDING_free(r->mt_blinding);
172     OPENSSL_free(r->bignum_data);
173     OPENSSL_free(r);
174 }
175
176 int RSA_up_ref(RSA *r)
177 {
178     int i;
179
180     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
181         return 0;
182
183     REF_PRINT_COUNT("RSA", r);
184     REF_ASSERT_ISNT(i < 2);
185     return i > 1 ? 1 : 0;
186 }
187
188 #ifndef FIPS_MODE
189 int RSA_set_ex_data(RSA *r, int idx, void *arg)
190 {
191     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
192 }
193
194 void *RSA_get_ex_data(const RSA *r, int idx)
195 {
196     return CRYPTO_get_ex_data(&r->ex_data, idx);
197 }
198 #endif
199
200 /*
201  * Define a scaling constant for our fixed point arithmetic.
202  * This value must be a power of two because the base two logarithm code
203  * makes this assumption.  The exponent must also be a multiple of three so
204  * that the scale factor has an exact cube root.  Finally, the scale factor
205  * should not be so large that a multiplication of two scaled numbers
206  * overflows a 64 bit unsigned integer.
207  */
208 static const unsigned int scale = 1 << 18;
209 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
210
211 /* Define some constants, none exceed 32 bits */
212 static const unsigned int log_2  = 0x02c5c8;    /* scale * log(2) */
213 static const unsigned int log_e  = 0x05c551;    /* scale * log2(M_E) */
214 static const unsigned int c1_923 = 0x07b126;    /* scale * 1.923 */
215 static const unsigned int c4_690 = 0x12c28f;    /* scale * 4.690 */
216
217 /*
218  * Multiply two scaled integers together and rescale the result.
219  */
220 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
221 {
222     return a * b / scale;
223 }
224
225 /*
226  * Calculate the cube root of a 64 bit scaled integer.
227  * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
228  * integer, this is not guaranteed after scaling, so this function has a
229  * 64 bit return.  This uses the shifting nth root algorithm with some
230  * algebraic simplifications.
231  */
232 static uint64_t icbrt64(uint64_t x)
233 {
234     uint64_t r = 0;
235     uint64_t b;
236     int s;
237
238     for (s = 63; s >= 0; s -= 3) {
239         r <<= 1;
240         b = 3 * r * (r + 1) + 1;
241         if ((x >> s) >= b) {
242             x -= b << s;
243             r++;
244         }
245     }
246     return r * cbrt_scale;
247 }
248
249 /*
250  * Calculate the natural logarithm of a 64 bit scaled integer.
251  * This is done by calculating a base two logarithm and scaling.
252  * The maximum logarithm (base 2) is 64 and this reduces base e, so
253  * a 32 bit result should not overflow.  The argument passed must be
254  * greater than unity so we don't need to handle negative results.
255  */
256 static uint32_t ilog_e(uint64_t v)
257 {
258     uint32_t i, r = 0;
259
260     /*
261      * Scale down the value into the range 1 .. 2.
262      *
263      * If fractional numbers need to be processed, another loop needs
264      * to go here that checks v < scale and if so multiplies it by 2 and
265      * reduces r by scale.  This also means making r signed.
266      */
267     while (v >= 2 * scale) {
268         v >>= 1;
269         r += scale;
270     }
271     for (i = scale / 2; i != 0; i /= 2) {
272         v = mul2(v, v);
273         if (v >= 2 * scale) {
274             v >>= 1;
275             r += i;
276         }
277     }
278     r = (r * (uint64_t)scale) / log_e;
279     return r;
280 }
281
282 /*
283  * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
284  * Modulus Lengths.
285  *
286  * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
287  * for FFC safe prime groups for modp and ffdhe.
288  * After Table 25 and Table 26 it refers to
289  * "The maximum security strength estimates were calculated using the formula in
290  * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
291  * bits".
292  *
293  * The formula is:
294  *
295  * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
296  *           \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
297  * The two cube roots are merged together here.
298  */
299 uint16_t ifc_ffc_compute_security_bits(int n)
300 {
301     uint64_t x;
302     uint32_t lx;
303     uint16_t y;
304
305     /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
306     switch (n) {
307     case 2048:
308         return 112;
309     case 3072:
310         return 128;
311     case 4096:
312         return 152;
313     case 6144:
314         return 176;
315     case 8192:
316         return 200;
317     }
318     /*
319      * The first incorrect result (i.e. not accurate or off by one low) occurs
320      * for n = 699668.  The true value here is 1200.  Instead of using this n
321      * as the check threshold, the smallest n such that the correct result is
322      * 1200 is used instead.
323      */
324     if (n >= 687737)
325         return 1200;
326     if (n < 8)
327         return 0;
328
329     x = n * (uint64_t)log_2;
330     lx = ilog_e(x);
331     y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
332                    / log_2);
333     return (y + 4) & ~7;
334 }
335
336
337
338 int RSA_security_bits(const RSA *rsa)
339 {
340     int bits = BN_num_bits(rsa->n);
341
342 #ifndef FIPS_MODE
343     if (rsa->version == RSA_ASN1_VERSION_MULTI) {
344         /* This ought to mean that we have private key at hand. */
345         int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
346
347         if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
348             return 0;
349     }
350 #endif
351     return ifc_ffc_compute_security_bits(bits);
352 }
353
354 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
355 {
356     /* If the fields n and e in r are NULL, the corresponding input
357      * parameters MUST be non-NULL for n and e.  d may be
358      * left NULL (in case only the public key is used).
359      */
360     if ((r->n == NULL && n == NULL)
361         || (r->e == NULL && e == NULL))
362         return 0;
363
364     if (n != NULL) {
365         BN_free(r->n);
366         r->n = n;
367     }
368     if (e != NULL) {
369         BN_free(r->e);
370         r->e = e;
371     }
372     if (d != NULL) {
373         BN_clear_free(r->d);
374         r->d = d;
375         BN_set_flags(r->d, BN_FLG_CONSTTIME);
376     }
377     r->dirty_cnt++;
378
379     return 1;
380 }
381
382 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
383 {
384     /* If the fields p and q in r are NULL, the corresponding input
385      * parameters MUST be non-NULL.
386      */
387     if ((r->p == NULL && p == NULL)
388         || (r->q == NULL && q == NULL))
389         return 0;
390
391     if (p != NULL) {
392         BN_clear_free(r->p);
393         r->p = p;
394         BN_set_flags(r->p, BN_FLG_CONSTTIME);
395     }
396     if (q != NULL) {
397         BN_clear_free(r->q);
398         r->q = q;
399         BN_set_flags(r->q, BN_FLG_CONSTTIME);
400     }
401     r->dirty_cnt++;
402
403     return 1;
404 }
405
406 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
407 {
408     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
409      * parameters MUST be non-NULL.
410      */
411     if ((r->dmp1 == NULL && dmp1 == NULL)
412         || (r->dmq1 == NULL && dmq1 == NULL)
413         || (r->iqmp == NULL && iqmp == NULL))
414         return 0;
415
416     if (dmp1 != NULL) {
417         BN_clear_free(r->dmp1);
418         r->dmp1 = dmp1;
419         BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
420     }
421     if (dmq1 != NULL) {
422         BN_clear_free(r->dmq1);
423         r->dmq1 = dmq1;
424         BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
425     }
426     if (iqmp != NULL) {
427         BN_clear_free(r->iqmp);
428         r->iqmp = iqmp;
429         BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
430     }
431     r->dirty_cnt++;
432
433     return 1;
434 }
435
436 #ifndef FIPS_MODE
437 /*
438  * Is it better to export RSA_PRIME_INFO structure
439  * and related functions to let user pass a triplet?
440  */
441 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
442                                 BIGNUM *coeffs[], int pnum)
443 {
444     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
445     RSA_PRIME_INFO *pinfo;
446     int i;
447
448     if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
449         return 0;
450
451     prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
452     if (prime_infos == NULL)
453         return 0;
454
455     if (r->prime_infos != NULL)
456         old = r->prime_infos;
457
458     for (i = 0; i < pnum; i++) {
459         pinfo = rsa_multip_info_new();
460         if (pinfo == NULL)
461             goto err;
462         if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
463             BN_clear_free(pinfo->r);
464             BN_clear_free(pinfo->d);
465             BN_clear_free(pinfo->t);
466             pinfo->r = primes[i];
467             pinfo->d = exps[i];
468             pinfo->t = coeffs[i];
469             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
470             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
471             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
472         } else {
473             rsa_multip_info_free(pinfo);
474             goto err;
475         }
476         (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
477     }
478
479     r->prime_infos = prime_infos;
480
481     if (!rsa_multip_calc_product(r)) {
482         r->prime_infos = old;
483         goto err;
484     }
485
486     if (old != NULL) {
487         /*
488          * This is hard to deal with, since the old infos could
489          * also be set by this function and r, d, t should not
490          * be freed in that case. So currently, stay consistent
491          * with other *set0* functions: just free it...
492          */
493         sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
494     }
495
496     r->version = RSA_ASN1_VERSION_MULTI;
497     r->dirty_cnt++;
498
499     return 1;
500  err:
501     /* r, d, t should not be freed */
502     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
503     return 0;
504 }
505 #endif
506
507 void RSA_get0_key(const RSA *r,
508                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
509 {
510     if (n != NULL)
511         *n = r->n;
512     if (e != NULL)
513         *e = r->e;
514     if (d != NULL)
515         *d = r->d;
516 }
517
518 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
519 {
520     if (p != NULL)
521         *p = r->p;
522     if (q != NULL)
523         *q = r->q;
524 }
525
526 #ifndef FIPS_MODE
527 int RSA_get_multi_prime_extra_count(const RSA *r)
528 {
529     int pnum;
530
531     pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
532     if (pnum <= 0)
533         pnum = 0;
534     return pnum;
535 }
536
537 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
538 {
539     int pnum, i;
540     RSA_PRIME_INFO *pinfo;
541
542     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
543         return 0;
544
545     /*
546      * return other primes
547      * it's caller's responsibility to allocate oth_primes[pnum]
548      */
549     for (i = 0; i < pnum; i++) {
550         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
551         primes[i] = pinfo->r;
552     }
553
554     return 1;
555 }
556 #endif
557
558 void RSA_get0_crt_params(const RSA *r,
559                          const BIGNUM **dmp1, const BIGNUM **dmq1,
560                          const BIGNUM **iqmp)
561 {
562     if (dmp1 != NULL)
563         *dmp1 = r->dmp1;
564     if (dmq1 != NULL)
565         *dmq1 = r->dmq1;
566     if (iqmp != NULL)
567         *iqmp = r->iqmp;
568 }
569
570 #ifndef FIPS_MODE
571 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
572                                     const BIGNUM *coeffs[])
573 {
574     int pnum;
575
576     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
577         return 0;
578
579     /* return other primes */
580     if (exps != NULL || coeffs != NULL) {
581         RSA_PRIME_INFO *pinfo;
582         int i;
583
584         /* it's the user's job to guarantee the buffer length */
585         for (i = 0; i < pnum; i++) {
586             pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
587             if (exps != NULL)
588                 exps[i] = pinfo->d;
589             if (coeffs != NULL)
590                 coeffs[i] = pinfo->t;
591         }
592     }
593
594     return 1;
595 }
596 #endif
597
598 const BIGNUM *RSA_get0_n(const RSA *r)
599 {
600     return r->n;
601 }
602
603 const BIGNUM *RSA_get0_e(const RSA *r)
604 {
605     return r->e;
606 }
607
608 const BIGNUM *RSA_get0_d(const RSA *r)
609 {
610     return r->d;
611 }
612
613 const BIGNUM *RSA_get0_p(const RSA *r)
614 {
615     return r->p;
616 }
617
618 const BIGNUM *RSA_get0_q(const RSA *r)
619 {
620     return r->q;
621 }
622
623 const BIGNUM *RSA_get0_dmp1(const RSA *r)
624 {
625     return r->dmp1;
626 }
627
628 const BIGNUM *RSA_get0_dmq1(const RSA *r)
629 {
630     return r->dmq1;
631 }
632
633 const BIGNUM *RSA_get0_iqmp(const RSA *r)
634 {
635     return r->iqmp;
636 }
637
638 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
639 {
640     return r->pss;
641 }
642
643 void RSA_clear_flags(RSA *r, int flags)
644 {
645     r->flags &= ~flags;
646 }
647
648 int RSA_test_flags(const RSA *r, int flags)
649 {
650     return r->flags & flags;
651 }
652
653 void RSA_set_flags(RSA *r, int flags)
654 {
655     r->flags |= flags;
656 }
657
658 int RSA_get_version(RSA *r)
659 {
660     /* { two-prime(0), multi(1) } */
661     return r->version;
662 }
663
664 #ifndef FIPS_MODE
665 ENGINE *RSA_get0_engine(const RSA *r)
666 {
667     return r->engine;
668 }
669
670 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
671 {
672     /* If key type not RSA or RSA-PSS return error */
673     if (ctx != NULL && ctx->pmeth != NULL
674         && ctx->pmeth->pkey_id != EVP_PKEY_RSA
675         && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
676         return -1;
677      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
678 }
679 #endif
680
681 DEFINE_STACK_OF(BIGNUM)
682
683 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
684                         const STACK_OF(BIGNUM) *exps,
685                         const STACK_OF(BIGNUM) *coeffs)
686 {
687 #ifndef FIPS_MODE
688     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
689 #endif
690     int pnum;
691
692     if (primes == NULL || exps == NULL || coeffs == NULL)
693         return 0;
694
695     pnum = sk_BIGNUM_num(primes);
696     if (pnum < 2
697         || pnum != sk_BIGNUM_num(exps)
698         || pnum != sk_BIGNUM_num(coeffs) + 1)
699         return 0;
700
701     if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
702                           sk_BIGNUM_value(primes, 1))
703         || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
704                                 sk_BIGNUM_value(exps, 1),
705                                 sk_BIGNUM_value(coeffs, 0)))
706         return 0;
707
708 #ifndef FIPS_MODE
709     old_infos = r->prime_infos;
710 #endif
711
712     if (pnum > 2) {
713 #ifndef FIPS_MODE
714         int i;
715
716         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
717         if (prime_infos == NULL)
718             return 0;
719
720         for (i = 2; i < pnum; i++) {
721             BIGNUM *prime = sk_BIGNUM_value(primes, i);
722             BIGNUM *exp = sk_BIGNUM_value(exps, i);
723             BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
724             RSA_PRIME_INFO *pinfo = NULL;
725
726             if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
727                 goto err;
728
729             /* Using rsa_multip_info_new() is wasteful, so allocate directly */
730             if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
731                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
732                 goto err;
733             }
734
735             pinfo->r = prime;
736             pinfo->d = exp;
737             pinfo->t = coeff;
738             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
739             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
740             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
741             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
742         }
743
744         r->prime_infos = prime_infos;
745
746         if (!rsa_multip_calc_product(r)) {
747             r->prime_infos = old_infos;
748             goto err;
749         }
750 #else
751         return 0;
752 #endif
753     }
754
755 #ifndef FIPS_MODE
756     if (old_infos != NULL) {
757         /*
758          * This is hard to deal with, since the old infos could
759          * also be set by this function and r, d, t should not
760          * be freed in that case. So currently, stay consistent
761          * with other *set0* functions: just free it...
762          */
763         sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
764     }
765 #endif
766
767     r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
768     r->dirty_cnt++;
769
770     return 1;
771 #ifndef FIPS_MODE
772  err:
773     /* r, d, t should not be freed */
774     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
775     return 0;
776 #endif
777 }
778
779 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
780
781 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
782                         STACK_OF(BIGNUM_const) *exps,
783                         STACK_OF(BIGNUM_const) *coeffs)
784 {
785 #ifndef FIPS_MODE
786     RSA_PRIME_INFO *pinfo;
787     int i, pnum;
788 #endif
789
790     if (r == NULL)
791         return 0;
792
793     /* If |p| is NULL, there are no CRT parameters */
794     if (RSA_get0_p(r) == NULL)
795         return 1;
796
797     sk_BIGNUM_const_push(primes, RSA_get0_p(r));
798     sk_BIGNUM_const_push(primes, RSA_get0_q(r));
799     sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
800     sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
801     sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
802
803 #ifndef FIPS_MODE
804     pnum = RSA_get_multi_prime_extra_count(r);
805     for (i = 0; i < pnum; i++) {
806         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
807         sk_BIGNUM_const_push(primes, pinfo->r);
808         sk_BIGNUM_const_push(exps, pinfo->d);
809         sk_BIGNUM_const_push(coeffs, pinfo->t);
810     }
811 #endif
812
813     return 1;
814 }
815
816 #ifndef FIPS_MODE
817 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
818 {
819     OSSL_PARAM pad_params[2], *p = pad_params;
820
821     if (ctx == NULL) {
822         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
823         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
824         return -2;
825     }
826
827     /* If key type not RSA or RSA-PSS return error */
828     if (ctx->pmeth != NULL
829             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
830             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
831         return -1;
832
833     /* TODO(3.0): Remove this eventually when no more legacy */
834     if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
835          || ctx->op.ciph.ciphprovctx == NULL)
836         && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
837             || ctx->op.sig.sigprovctx == NULL))
838         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
839                                  pad_mode, NULL);
840
841     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
842     *p++ = OSSL_PARAM_construct_end();
843
844     return EVP_PKEY_CTX_set_params(ctx, pad_params);
845 }
846
847 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
848 {
849     OSSL_PARAM pad_params[2], *p = pad_params;
850
851     if (ctx == NULL || pad_mode == NULL) {
852         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
853         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
854         return -2;
855     }
856
857     /* If key type not RSA or RSA-PSS return error */
858     if (ctx->pmeth != NULL
859             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
860             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
861         return -1;
862
863     /* TODO(3.0): Remove this eventually when no more legacy */
864     if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
865          || ctx->op.ciph.ciphprovctx == NULL)
866         && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
867             || ctx->op.sig.sigprovctx == NULL))
868         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
869                                  pad_mode);
870
871     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
872     *p++ = OSSL_PARAM_construct_end();
873
874     if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
875         return 0;
876
877     return 1;
878
879 }
880
881 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
882 {
883     const char *name;
884
885     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
886         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
887         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
888         return -2;
889     }
890
891     /* If key type not RSA return error */
892     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
893         return -1;
894
895     /* TODO(3.0): Remove this eventually when no more legacy */
896     if (ctx->op.ciph.ciphprovctx == NULL)
897         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
898                                  EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
899
900     name = (md == NULL) ? "" : EVP_MD_name(md);
901
902     return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
903 }
904
905 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
906                                       const char *mdprops)
907 {
908     OSSL_PARAM rsa_params[3], *p = rsa_params;
909
910     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
911         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
912         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
913         return -2;
914     }
915
916     /* If key type not RSA return error */
917     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
918         return -1;
919
920
921     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
922                                             /*
923                                              * Cast away the const. This is read
924                                              * only so should be safe
925                                              */
926                                             (char *)mdname, 0);
927     if (mdprops != NULL) {
928         *p++ = OSSL_PARAM_construct_utf8_string(
929                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
930                     /*
931                      * Cast away the const. This is read
932                      * only so should be safe
933                      */
934                     (char *)mdprops, 0);
935     }
936     *p++ = OSSL_PARAM_construct_end();
937
938     return EVP_PKEY_CTX_set_params(ctx, rsa_params);
939 }
940
941 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
942                                       size_t namelen)
943 {
944     OSSL_PARAM rsa_params[2], *p = rsa_params;
945
946     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
947         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
948         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
949         return -2;
950     }
951
952     /* If key type not RSA return error */
953     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
954         return -1;
955
956     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
957                                             name, namelen);
958     *p++ = OSSL_PARAM_construct_end();
959
960     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
961         return -1;
962
963     return 1;
964 }
965
966 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
967 {
968     /* 80 should be big enough */
969     char name[80] = "";
970
971     if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
972         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
973         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
974         return -2;
975     }
976
977     /* If key type not RSA return error */
978     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
979         return -1;
980
981     /* TODO(3.0): Remove this eventually when no more legacy */
982     if (ctx->op.ciph.ciphprovctx == NULL)
983         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
984                                  EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
985
986     if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
987         return -1;
988
989     /* May be NULL meaning "unknown" */
990     *md = EVP_get_digestbyname(name);
991
992     return 1;
993 }
994
995 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
996 {
997     const char *name;
998
999     if (ctx == NULL
1000             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1001                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1002         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1003         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1004         return -2;
1005     }
1006
1007     /* If key type not RSA return error */
1008     if (ctx->pmeth != NULL
1009             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1010             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1011         return -1;
1012
1013     /* TODO(3.0): Remove this eventually when no more legacy */
1014     if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1015                 && ctx->op.ciph.ciphprovctx == NULL)
1016             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1017                 && ctx->op.sig.sigprovctx == NULL))
1018         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1019                                  EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1020                                  EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1021
1022     name = (md == NULL) ? "" : EVP_MD_name(md);
1023
1024     return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1025 }
1026
1027 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1028                                       const char *mdprops)
1029 {
1030     OSSL_PARAM rsa_params[3], *p = rsa_params;
1031
1032     if (ctx == NULL
1033             || mdname == NULL
1034             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1035                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1036         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1037         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1038         return -2;
1039     }
1040
1041     /* If key type not RSA return error */
1042     if (ctx->pmeth != NULL
1043             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1044             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1045         return -1;
1046
1047     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1048                                             /*
1049                                              * Cast away the const. This is
1050                                              * read only so should be safe
1051                                              */
1052                                             (char *)mdname, 0);
1053     if (mdprops != NULL) {
1054         *p++ =
1055             OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1056                                              /*
1057                                               * Cast away the const. This is
1058                                               * read only so should be safe
1059                                               */
1060                                              (char *)mdprops, 0);
1061     }
1062     *p++ = OSSL_PARAM_construct_end();
1063
1064     return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1065 }
1066
1067 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1068                                       size_t namelen)
1069 {
1070     OSSL_PARAM rsa_params[2], *p = rsa_params;
1071
1072     if (ctx == NULL
1073             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1074                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1075         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1076         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1077         return -2;
1078     }
1079
1080     /* If key type not RSA or RSA-PSS return error */
1081     if (ctx->pmeth != NULL
1082             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1083             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1084         return -1;
1085
1086     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1087                                             name, namelen);
1088     *p++ = OSSL_PARAM_construct_end();
1089
1090     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1091         return -1;
1092
1093     return 1;
1094 }
1095
1096 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1097 {
1098     /* 80 should be big enough */
1099     char name[80] = "";
1100
1101     if (ctx == NULL
1102             || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1103                 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1104         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1105         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1106         return -2;
1107     }
1108
1109     /* If key type not RSA or RSA-PSS return error */
1110     if (ctx->pmeth != NULL
1111             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1112             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1113         return -1;
1114
1115     /* TODO(3.0): Remove this eventually when no more legacy */
1116     if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1117                 && ctx->op.ciph.ciphprovctx == NULL)
1118             || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1119                 && ctx->op.sig.sigprovctx == NULL))
1120         return EVP_PKEY_CTX_ctrl(ctx, -1,
1121                                  EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1122                                  EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1123
1124     if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1125         return -1;
1126
1127     /* May be NULL meaning "unknown" */
1128     *md = EVP_get_digestbyname(name);
1129
1130     return 1;
1131 }
1132
1133 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1134 {
1135     OSSL_PARAM rsa_params[2], *p = rsa_params;
1136
1137     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1138         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1139         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1140         return -2;
1141     }
1142
1143     /* If key type not RSA return error */
1144     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1145         return -1;
1146
1147     /* TODO(3.0): Remove this eventually when no more legacy */
1148     if (ctx->op.ciph.ciphprovctx == NULL)
1149         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1150                                  EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1151                                  (void *)label);
1152
1153     *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1154                                              /*
1155                                               * Cast away the const. This is
1156                                               * read only so should be safe
1157                                               */
1158                                              (void *)label,
1159                                              (size_t)llen);
1160     *p++ = OSSL_PARAM_construct_end();
1161
1162     if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1163         return 0;
1164
1165     OPENSSL_free(label);
1166     return 1;
1167 }
1168
1169 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1170 {
1171     OSSL_PARAM rsa_params[3], *p = rsa_params;
1172     size_t labellen;
1173
1174     if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1175         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1176         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1177         return -2;
1178     }
1179
1180     /* If key type not RSA return error */
1181     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1182         return -1;
1183
1184     /* TODO(3.0): Remove this eventually when no more legacy */
1185     if (ctx->op.ciph.ciphprovctx == NULL)
1186         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1187                                  EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1188                                  (void *)label);
1189
1190     *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1191                                           (void **)label, 0);
1192     *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1193                                        &labellen);
1194     *p++ = OSSL_PARAM_construct_end();
1195
1196     if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1197         return -1;
1198
1199     if (labellen > INT_MAX)
1200         return -1;
1201
1202     return (int)labellen;
1203 }
1204
1205 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1206 {
1207     OSSL_PARAM pad_params[2], *p = pad_params;
1208
1209     if (ctx == NULL) {
1210         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1211         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1212         return -2;
1213     }
1214
1215     /* If key type not RSA or RSA-PSS return error */
1216     if (ctx->pmeth != NULL
1217             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1218             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1219         return -1;
1220
1221     /* TODO(3.0): Remove this eventually when no more legacy */
1222     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1223         || ctx->op.sig.sigprovctx == NULL)
1224         return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1225                                  saltlen, NULL);
1226
1227     *p++ =
1228         OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1229     *p++ = OSSL_PARAM_construct_end();
1230
1231     return EVP_PKEY_CTX_set_params(ctx, pad_params);
1232 }
1233
1234 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1235 {
1236     OSSL_PARAM pad_params[2], *p = pad_params;
1237
1238     if (ctx == NULL || saltlen == NULL) {
1239         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1240         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1241         return -2;
1242     }
1243
1244     /* If key type not RSA or RSA-PSS return error */
1245     if (ctx->pmeth != NULL
1246             && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1247             && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1248         return -1;
1249
1250     /* TODO(3.0): Remove this eventually when no more legacy */
1251     if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1252         || ctx->op.sig.sigprovctx == NULL)
1253         return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1254                                  EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1255                                  0, saltlen);
1256
1257     *p++ =
1258         OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1259     *p++ = OSSL_PARAM_construct_end();
1260
1261     if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1262         return 0;
1263
1264     return 1;
1265
1266 }
1267
1268 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1269 {
1270     OSSL_PARAM params[2], *p = params;
1271     size_t bits2 = bits;
1272
1273     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1274         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1275         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1276         return -2;
1277     }
1278
1279     /* If key type not RSA return error */
1280     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1281         return -1;
1282
1283     /* TODO(3.0): Remove this eventually when no more legacy */
1284     if (ctx->op.keymgmt.genctx == NULL)
1285         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1286                                  EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1287
1288     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1289     *p++ = OSSL_PARAM_construct_end();
1290
1291     if (!EVP_PKEY_CTX_set_params(ctx, params))
1292         return 0;
1293
1294     return 1;
1295 }
1296
1297 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1298 {
1299     OSSL_PARAM_BLD *tmpl;
1300     OSSL_PARAM *params;
1301     int ret;
1302
1303     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1304         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1305         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1306         return -2;
1307     }
1308
1309     /* If key type not RSA return error */
1310     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1311         return -1;
1312
1313     /* TODO(3.0): Remove this eventually when no more legacy */
1314     if (ctx->op.keymgmt.genctx == NULL)
1315         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1316                                  EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1317
1318     if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1319         return 0;
1320     if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1321         || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1322         OSSL_PARAM_BLD_free(tmpl);
1323         return 0;
1324     }
1325     OSSL_PARAM_BLD_free(tmpl);
1326
1327     ret = EVP_PKEY_CTX_set_params(ctx, params);
1328     OSSL_PARAM_BLD_free_params(params);
1329     return ret;
1330 }
1331
1332 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1333 {
1334     OSSL_PARAM params[2], *p = params;
1335     size_t primes2 = primes;
1336
1337     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1338         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1339         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1340         return -2;
1341     }
1342
1343     /* If key type not RSA return error */
1344     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1345         return -1;
1346
1347     /* TODO(3.0): Remove this eventually when no more legacy */
1348     if (ctx->op.keymgmt.genctx == NULL)
1349         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1350                                  EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1351                                  NULL);
1352
1353     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1354     *p++ = OSSL_PARAM_construct_end();
1355
1356     if (!EVP_PKEY_CTX_set_params(ctx, params))
1357         return 0;
1358
1359     return 1;
1360 }
1361 #endif