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