2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/crypto.h>
17 #include <openssl/core_names.h>
19 # include <openssl/engine.h>
21 #include <openssl/evp.h>
22 #include <openssl/param_build.h>
23 #include "internal/cryptlib.h"
24 #include "internal/refcount.h"
25 #include "crypto/bn.h"
26 #include "crypto/evp.h"
27 #include "crypto/rsa.h"
28 #include "crypto/sparse_array.h"
29 #include "crypto/security_bits.h"
30 #include "rsa_local.h"
32 static RSA
*rsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
);
37 return rsa_new_intern(NULL
, NULL
);
40 const RSA_METHOD
*RSA_get_method(const RSA
*rsa
)
45 int RSA_set_method(RSA
*rsa
, const RSA_METHOD
*meth
)
48 * NB: The caller is specifically setting a method, so it's not up to us
49 * to deal with which ENGINE it comes from.
51 const RSA_METHOD
*mtmp
;
55 #ifndef OPENSSL_NO_ENGINE
56 ENGINE_finish(rsa
->engine
);
65 RSA
*RSA_new_method(ENGINE
*engine
)
67 return rsa_new_intern(engine
, NULL
);
71 RSA
*ossl_rsa_new_with_ctx(OSSL_LIB_CTX
*libctx
)
73 return rsa_new_intern(NULL
, libctx
);
76 static RSA
*rsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
)
78 RSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
83 ret
->lock
= CRYPTO_THREAD_lock_new();
84 if (ret
->lock
== NULL
) {
85 ERR_raise(ERR_LIB_RSA
, ERR_R_CRYPTO_LIB
);
90 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
91 CRYPTO_THREAD_lock_free(ret
->lock
);
96 ret
->blindings_sa
= ossl_rsa_alloc_blinding();
97 if (ret
->blindings_sa
== NULL
)
100 ret
->libctx
= libctx
;
101 ret
->meth
= RSA_get_default_method();
102 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
103 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
105 if (!ENGINE_init(engine
)) {
106 ERR_raise(ERR_LIB_RSA
, ERR_R_ENGINE_LIB
);
109 ret
->engine
= engine
;
111 ret
->engine
= ENGINE_get_default_RSA();
114 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
115 if (ret
->meth
== NULL
) {
116 ERR_raise(ERR_LIB_RSA
, ERR_R_ENGINE_LIB
);
122 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
124 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
129 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
130 ERR_raise(ERR_LIB_RSA
, ERR_R_INIT_FAIL
);
141 void RSA_free(RSA
*r
)
148 CRYPTO_DOWN_REF(&r
->references
, &i
);
149 REF_PRINT_COUNT("RSA", i
, r
);
152 REF_ASSERT_ISNT(i
< 0);
154 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
156 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
157 ENGINE_finish(r
->engine
);
161 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
164 CRYPTO_THREAD_lock_free(r
->lock
);
165 CRYPTO_FREE_REF(&r
->references
);
167 #ifdef OPENSSL_PEDANTIC_ZEROIZATION
177 BN_clear_free(r
->dmp1
);
178 BN_clear_free(r
->dmq1
);
179 BN_clear_free(r
->iqmp
);
181 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
182 ossl_rsa_acvp_test_free(r
->acvp_test
);
186 RSA_PSS_PARAMS_free(r
->pss
);
187 sk_RSA_PRIME_INFO_pop_free(r
->prime_infos
, ossl_rsa_multip_info_free
);
189 ossl_rsa_free_blinding(r
);
193 int RSA_up_ref(RSA
*r
)
197 if (CRYPTO_UP_REF(&r
->references
, &i
) <= 0)
200 REF_PRINT_COUNT("RSA", i
, r
);
201 REF_ASSERT_ISNT(i
< 2);
202 return i
> 1 ? 1 : 0;
205 OSSL_LIB_CTX
*ossl_rsa_get0_libctx(RSA
*r
)
210 void ossl_rsa_set0_libctx(RSA
*r
, OSSL_LIB_CTX
*libctx
)
216 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
218 return CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
);
221 void *RSA_get_ex_data(const RSA
*r
, int idx
)
223 return CRYPTO_get_ex_data(&r
->ex_data
, idx
);
228 * Define a scaling constant for our fixed point arithmetic.
229 * This value must be a power of two because the base two logarithm code
230 * makes this assumption. The exponent must also be a multiple of three so
231 * that the scale factor has an exact cube root. Finally, the scale factor
232 * should not be so large that a multiplication of two scaled numbers
233 * overflows a 64 bit unsigned integer.
235 static const unsigned int scale
= 1 << 18;
236 static const unsigned int cbrt_scale
= 1 << (2 * 18 / 3);
238 /* Define some constants, none exceed 32 bits */
239 static const unsigned int log_2
= 0x02c5c8; /* scale * log(2) */
240 static const unsigned int log_e
= 0x05c551; /* scale * log2(M_E) */
241 static const unsigned int c1_923
= 0x07b126; /* scale * 1.923 */
242 static const unsigned int c4_690
= 0x12c28f; /* scale * 4.690 */
245 * Multiply two scaled integers together and rescale the result.
247 static ossl_inline
uint64_t mul2(uint64_t a
, uint64_t b
)
249 return a
* b
/ scale
;
253 * Calculate the cube root of a 64 bit scaled integer.
254 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
255 * integer, this is not guaranteed after scaling, so this function has a
256 * 64 bit return. This uses the shifting nth root algorithm with some
257 * algebraic simplifications.
259 static uint64_t icbrt64(uint64_t x
)
265 for (s
= 63; s
>= 0; s
-= 3) {
267 b
= 3 * r
* (r
+ 1) + 1;
273 return r
* cbrt_scale
;
277 * Calculate the natural logarithm of a 64 bit scaled integer.
278 * This is done by calculating a base two logarithm and scaling.
279 * The maximum logarithm (base 2) is 64 and this reduces base e, so
280 * a 32 bit result should not overflow. The argument passed must be
281 * greater than unity so we don't need to handle negative results.
283 static uint32_t ilog_e(uint64_t v
)
288 * Scale down the value into the range 1 .. 2.
290 * If fractional numbers need to be processed, another loop needs
291 * to go here that checks v < scale and if so multiplies it by 2 and
292 * reduces r by scale. This also means making r signed.
294 while (v
>= 2 * scale
) {
298 for (i
= scale
/ 2; i
!= 0; i
/= 2) {
300 if (v
>= 2 * scale
) {
305 r
= (r
* (uint64_t)scale
) / log_e
;
310 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
313 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
314 * for FFC safe prime groups for modp and ffdhe.
315 * After Table 25 and Table 26 it refers to
316 * "The maximum security strength estimates were calculated using the formula in
317 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
322 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
323 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
324 * The two cube roots are merged together here.
326 uint16_t ossl_ifc_ffc_compute_security_bits(int n
)
333 * Look for common values as listed in standards.
334 * These values are not exactly equal to the results from the formulae in
335 * the standards but are defined to be canonical.
338 case 2048: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
340 case 3072: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
342 case 4096: /* SP 800-56B rev 2 Appendix D */
344 case 6144: /* SP 800-56B rev 2 Appendix D */
346 case 7680: /* FIPS 140-2 IG 7.5 */
348 case 8192: /* SP 800-56B rev 2 Appendix D */
350 case 15360: /* FIPS 140-2 IG 7.5 */
355 * The first incorrect result (i.e. not accurate or off by one low) occurs
356 * for n = 699668. The true value here is 1200. Instead of using this n
357 * as the check threshold, the smallest n such that the correct result is
358 * 1200 is used instead.
366 * To ensure that the output is non-decreasing with respect to n,
367 * a cap needs to be applied to the two values where the function over
368 * estimates the strength (according to the above fast path).
377 x
= n
* (uint64_t)log_2
;
379 y
= (uint16_t)((mul2(c1_923
, icbrt64(mul2(mul2(x
, lx
), lx
))) - c4_690
)
389 int RSA_security_bits(const RSA
*rsa
)
391 int bits
= BN_num_bits(rsa
->n
);
394 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
) {
395 /* This ought to mean that we have private key at hand. */
396 int ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
);
398 if (ex_primes
<= 0 || (ex_primes
+ 2) > ossl_rsa_multip_cap(bits
))
402 return ossl_ifc_ffc_compute_security_bits(bits
);
405 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
407 /* If the fields n and e in r are NULL, the corresponding input
408 * parameters MUST be non-NULL for n and e. d may be
409 * left NULL (in case only the public key is used).
411 if ((r
->n
== NULL
&& n
== NULL
)
412 || (r
->e
== NULL
&& e
== NULL
))
426 BN_set_flags(r
->d
, BN_FLG_CONSTTIME
);
433 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
435 /* If the fields p and q in r are NULL, the corresponding input
436 * parameters MUST be non-NULL.
438 if ((r
->p
== NULL
&& p
== NULL
)
439 || (r
->q
== NULL
&& q
== NULL
))
445 BN_set_flags(r
->p
, BN_FLG_CONSTTIME
);
450 BN_set_flags(r
->q
, BN_FLG_CONSTTIME
);
457 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
459 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
460 * parameters MUST be non-NULL.
462 if ((r
->dmp1
== NULL
&& dmp1
== NULL
)
463 || (r
->dmq1
== NULL
&& dmq1
== NULL
)
464 || (r
->iqmp
== NULL
&& iqmp
== NULL
))
468 BN_clear_free(r
->dmp1
);
470 BN_set_flags(r
->dmp1
, BN_FLG_CONSTTIME
);
473 BN_clear_free(r
->dmq1
);
475 BN_set_flags(r
->dmq1
, BN_FLG_CONSTTIME
);
478 BN_clear_free(r
->iqmp
);
480 BN_set_flags(r
->iqmp
, BN_FLG_CONSTTIME
);
489 * Is it better to export RSA_PRIME_INFO structure
490 * and related functions to let user pass a triplet?
492 int RSA_set0_multi_prime_params(RSA
*r
, BIGNUM
*primes
[], BIGNUM
*exps
[],
493 BIGNUM
*coeffs
[], int pnum
)
495 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old
= NULL
;
496 RSA_PRIME_INFO
*pinfo
;
499 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
|| pnum
== 0)
502 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
503 if (prime_infos
== NULL
)
506 if (r
->prime_infos
!= NULL
)
507 old
= r
->prime_infos
;
509 for (i
= 0; i
< pnum
; i
++) {
510 pinfo
= ossl_rsa_multip_info_new();
513 if (primes
[i
] != NULL
&& exps
[i
] != NULL
&& coeffs
[i
] != NULL
) {
514 BN_clear_free(pinfo
->r
);
515 BN_clear_free(pinfo
->d
);
516 BN_clear_free(pinfo
->t
);
517 pinfo
->r
= primes
[i
];
519 pinfo
->t
= coeffs
[i
];
520 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
521 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
522 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
524 ossl_rsa_multip_info_free(pinfo
);
527 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
530 r
->prime_infos
= prime_infos
;
532 if (!ossl_rsa_multip_calc_product(r
)) {
533 r
->prime_infos
= old
;
539 * This is hard to deal with, since the old infos could
540 * also be set by this function and r, d, t should not
541 * be freed in that case. So currently, stay consistent
542 * with other *set0* functions: just free it...
544 sk_RSA_PRIME_INFO_pop_free(old
, ossl_rsa_multip_info_free
);
547 r
->version
= RSA_ASN1_VERSION_MULTI
;
552 /* r, d, t should not be freed */
553 sk_RSA_PRIME_INFO_pop_free(prime_infos
, ossl_rsa_multip_info_free_ex
);
558 void RSA_get0_key(const RSA
*r
,
559 const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
569 void RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
578 int RSA_get_multi_prime_extra_count(const RSA
*r
)
582 pnum
= sk_RSA_PRIME_INFO_num(r
->prime_infos
);
588 int RSA_get0_multi_prime_factors(const RSA
*r
, const BIGNUM
*primes
[])
591 RSA_PRIME_INFO
*pinfo
;
593 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
597 * return other primes
598 * it's caller's responsibility to allocate oth_primes[pnum]
600 for (i
= 0; i
< pnum
; i
++) {
601 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
602 primes
[i
] = pinfo
->r
;
609 void RSA_get0_crt_params(const RSA
*r
,
610 const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
622 int RSA_get0_multi_prime_crt_params(const RSA
*r
, const BIGNUM
*exps
[],
623 const BIGNUM
*coeffs
[])
627 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
630 /* return other primes */
631 if (exps
!= NULL
|| coeffs
!= NULL
) {
632 RSA_PRIME_INFO
*pinfo
;
635 /* it's the user's job to guarantee the buffer length */
636 for (i
= 0; i
< pnum
; i
++) {
637 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
641 coeffs
[i
] = pinfo
->t
;
649 const BIGNUM
*RSA_get0_n(const RSA
*r
)
654 const BIGNUM
*RSA_get0_e(const RSA
*r
)
659 const BIGNUM
*RSA_get0_d(const RSA
*r
)
664 const BIGNUM
*RSA_get0_p(const RSA
*r
)
669 const BIGNUM
*RSA_get0_q(const RSA
*r
)
674 const BIGNUM
*RSA_get0_dmp1(const RSA
*r
)
679 const BIGNUM
*RSA_get0_dmq1(const RSA
*r
)
684 const BIGNUM
*RSA_get0_iqmp(const RSA
*r
)
689 const RSA_PSS_PARAMS
*RSA_get0_pss_params(const RSA
*r
)
699 int ossl_rsa_set0_pss_params(RSA
*r
, RSA_PSS_PARAMS
*pss
)
704 RSA_PSS_PARAMS_free(r
->pss
);
711 RSA_PSS_PARAMS_30
*ossl_rsa_get0_pss_params_30(RSA
*r
)
713 return &r
->pss_params
;
716 void RSA_clear_flags(RSA
*r
, int flags
)
721 int RSA_test_flags(const RSA
*r
, int flags
)
723 return r
->flags
& flags
;
726 void RSA_set_flags(RSA
*r
, int flags
)
731 int RSA_get_version(RSA
*r
)
733 /* { two-prime(0), multi(1) } */
738 ENGINE
*RSA_get0_engine(const RSA
*r
)
743 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, int p1
, void *p2
)
745 /* If key type not RSA or RSA-PSS return error */
746 if (ctx
!= NULL
&& ctx
->pmeth
!= NULL
747 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
748 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
750 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, p1
, p2
);
754 DEFINE_STACK_OF(BIGNUM
)
757 * Note: This function deletes values from the parameter
758 * stack values as they are consumed and set in the RSA key.
760 int ossl_rsa_set0_all_params(RSA
*r
, STACK_OF(BIGNUM
) *primes
,
761 STACK_OF(BIGNUM
) *exps
,
762 STACK_OF(BIGNUM
) *coeffs
)
765 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old_infos
= NULL
;
769 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
772 pnum
= sk_BIGNUM_num(primes
);
774 /* we need at least 2 primes */
778 if (!RSA_set0_factors(r
, sk_BIGNUM_value(primes
, 0),
779 sk_BIGNUM_value(primes
, 1)))
783 * if we managed to set everything above, remove those elements from the
785 * Note, we do this after the above all to ensure that we have taken
786 * ownership of all the elements in the RSA key to avoid memory leaks
787 * we also use delete 0 here as we are grabbing items from the end of the
788 * stack rather than the start, otherwise we could use pop
790 sk_BIGNUM_delete(primes
, 0);
791 sk_BIGNUM_delete(primes
, 0);
793 if (pnum
== sk_BIGNUM_num(exps
)
794 && pnum
== sk_BIGNUM_num(coeffs
) + 1) {
796 if (!RSA_set0_crt_params(r
, sk_BIGNUM_value(exps
, 0),
797 sk_BIGNUM_value(exps
, 1),
798 sk_BIGNUM_value(coeffs
, 0)))
801 /* as above, once we consume the above params, delete them from the list */
802 sk_BIGNUM_delete(exps
, 0);
803 sk_BIGNUM_delete(exps
, 0);
804 sk_BIGNUM_delete(coeffs
, 0);
808 old_infos
= r
->prime_infos
;
815 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
816 if (prime_infos
== NULL
)
819 for (i
= 2; i
< pnum
; i
++) {
820 BIGNUM
*prime
= sk_BIGNUM_pop(primes
);
821 BIGNUM
*exp
= sk_BIGNUM_pop(exps
);
822 BIGNUM
*coeff
= sk_BIGNUM_pop(coeffs
);
823 RSA_PRIME_INFO
*pinfo
= NULL
;
825 if (!ossl_assert(prime
!= NULL
&& exp
!= NULL
&& coeff
!= NULL
))
828 /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
829 if ((pinfo
= OPENSSL_zalloc(sizeof(*pinfo
))) == NULL
)
835 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
836 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
837 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
838 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
841 r
->prime_infos
= prime_infos
;
843 if (!ossl_rsa_multip_calc_product(r
)) {
844 r
->prime_infos
= old_infos
;
853 if (old_infos
!= NULL
) {
855 * This is hard to deal with, since the old infos could
856 * also be set by this function and r, d, t should not
857 * be freed in that case. So currently, stay consistent
858 * with other *set0* functions: just free it...
860 sk_RSA_PRIME_INFO_pop_free(old_infos
, ossl_rsa_multip_info_free
);
864 r
->version
= pnum
> 2 ? RSA_ASN1_VERSION_MULTI
: RSA_ASN1_VERSION_DEFAULT
;
870 /* r, d, t should not be freed */
871 sk_RSA_PRIME_INFO_pop_free(prime_infos
, ossl_rsa_multip_info_free_ex
);
876 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
878 int ossl_rsa_get0_all_params(RSA
*r
, STACK_OF(BIGNUM_const
) *primes
,
879 STACK_OF(BIGNUM_const
) *exps
,
880 STACK_OF(BIGNUM_const
) *coeffs
)
883 RSA_PRIME_INFO
*pinfo
;
890 /* If |p| is NULL, there are no CRT parameters */
891 if (RSA_get0_p(r
) == NULL
)
894 sk_BIGNUM_const_push(primes
, RSA_get0_p(r
));
895 sk_BIGNUM_const_push(primes
, RSA_get0_q(r
));
896 sk_BIGNUM_const_push(exps
, RSA_get0_dmp1(r
));
897 sk_BIGNUM_const_push(exps
, RSA_get0_dmq1(r
));
898 sk_BIGNUM_const_push(coeffs
, RSA_get0_iqmp(r
));
901 pnum
= RSA_get_multi_prime_extra_count(r
);
902 for (i
= 0; i
< pnum
; i
++) {
903 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
904 sk_BIGNUM_const_push(primes
, pinfo
->r
);
905 sk_BIGNUM_const_push(exps
, pinfo
->d
);
906 sk_BIGNUM_const_push(coeffs
, pinfo
->t
);
913 #define safe_BN_num_bits(_k_) (((_k_) == NULL) ? 0 : BN_num_bits((_k_)))
914 int ossl_rsa_check_factors(RSA
*r
)
918 STACK_OF(BIGNUM_const
) *factors
= sk_BIGNUM_const_new_null();
919 STACK_OF(BIGNUM_const
) *exps
= sk_BIGNUM_const_new_null();
920 STACK_OF(BIGNUM_const
) *coeffs
= sk_BIGNUM_const_new_null();
922 if (factors
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
926 * Simple sanity check for RSA key. All RSA key parameters
927 * must be less-than/equal-to RSA parameter n.
929 ossl_rsa_get0_all_params(r
, factors
, exps
, coeffs
);
930 n
= safe_BN_num_bits(RSA_get0_n(r
));
932 if (safe_BN_num_bits(RSA_get0_d(r
)) > n
)
935 for (i
= 0; i
< sk_BIGNUM_const_num(exps
); i
++) {
936 bits
= safe_BN_num_bits(sk_BIGNUM_const_value(exps
, i
));
941 for (i
= 0; i
< sk_BIGNUM_const_num(factors
); i
++) {
942 bits
= safe_BN_num_bits(sk_BIGNUM_const_value(factors
, i
));
947 for (i
= 0; i
< sk_BIGNUM_const_num(coeffs
); i
++) {
948 bits
= safe_BN_num_bits(sk_BIGNUM_const_value(coeffs
, i
));
956 sk_BIGNUM_const_free(factors
);
957 sk_BIGNUM_const_free(exps
);
958 sk_BIGNUM_const_free(coeffs
);
964 /* Helpers to set or get diverse hash algorithm names */
965 static int int_set_rsa_md_name(EVP_PKEY_CTX
*ctx
,
967 int keytype
, int optype
,
968 /* For EVP_PKEY_CTX_set_params() */
969 const char *mdkey
, const char *mdname
,
970 const char *propkey
, const char *mdprops
)
972 OSSL_PARAM params
[3], *p
= params
;
974 if (ctx
== NULL
|| mdname
== NULL
|| (ctx
->operation
& optype
) == 0) {
975 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
976 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
980 /* If key type not RSA return error */
983 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
984 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
988 if (!EVP_PKEY_CTX_is_a(ctx
, evp_pkey_type2name(keytype
)))
993 /* Cast away the const. This is read only so should be safe */
994 *p
++ = OSSL_PARAM_construct_utf8_string(mdkey
, (char *)mdname
, 0);
995 if (evp_pkey_ctx_is_provided(ctx
) && mdprops
!= NULL
) {
996 /* Cast away the const. This is read only so should be safe */
997 *p
++ = OSSL_PARAM_construct_utf8_string(propkey
, (char *)mdprops
, 0);
999 *p
++ = OSSL_PARAM_construct_end();
1001 return evp_pkey_ctx_set_params_strict(ctx
, params
);
1004 /* Helpers to set or get diverse hash algorithm names */
1005 static int int_get_rsa_md_name(EVP_PKEY_CTX
*ctx
,
1007 int keytype
, int optype
,
1008 /* For EVP_PKEY_CTX_get_params() */
1010 char *mdname
, size_t mdnamesize
)
1012 OSSL_PARAM params
[2], *p
= params
;
1014 if (ctx
== NULL
|| mdname
== NULL
|| (ctx
->operation
& optype
) == 0) {
1015 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1016 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1020 /* If key type not RSA return error */
1023 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
1024 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1028 if (!EVP_PKEY_CTX_is_a(ctx
, evp_pkey_type2name(keytype
)))
1033 /* Cast away the const. This is read only so should be safe */
1034 *p
++ = OSSL_PARAM_construct_utf8_string(mdkey
, (char *)mdname
, mdnamesize
);
1035 *p
++ = OSSL_PARAM_construct_end();
1037 return evp_pkey_ctx_get_params_strict(ctx
, params
);
1041 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1042 * simply because that's easier.
1044 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX
*ctx
, int pad_mode
)
1046 return RSA_pkey_ctx_ctrl(ctx
, -1, EVP_PKEY_CTRL_RSA_PADDING
,
1051 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1052 * simply because that's easier.
1054 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX
*ctx
, int *pad_mode
)
1056 return RSA_pkey_ctx_ctrl(ctx
, -1, EVP_PKEY_CTRL_GET_RSA_PADDING
,
1061 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1062 * simply because that's easier.
1064 int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1066 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1067 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
1070 int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX
*ctx
,
1072 const char *mdprops
)
1074 return int_set_rsa_md_name(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1075 OSSL_PKEY_PARAM_RSA_DIGEST
, mdname
,
1076 OSSL_PKEY_PARAM_RSA_DIGEST_PROPS
, mdprops
);
1080 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1081 * simply because that's easier.
1083 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1085 /* If key type not RSA return error */
1086 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1089 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1090 EVP_PKEY_CTRL_RSA_OAEP_MD
, 0, (void *)(md
));
1093 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
1094 const char *mdprops
)
1097 int_set_rsa_md_name(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1098 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, mdname
,
1099 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
, mdprops
);
1102 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
1105 return int_get_rsa_md_name(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1106 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
1111 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1112 * simply because that's easier.
1114 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1116 /* If key type not RSA return error */
1117 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1120 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1121 EVP_PKEY_CTRL_GET_RSA_OAEP_MD
, 0, (void *)md
);
1125 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1126 * simply because that's easier.
1128 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1130 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1131 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)(md
));
1134 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
1135 const char *mdprops
)
1137 return int_set_rsa_md_name(ctx
, -1,
1138 EVP_PKEY_OP_TYPE_CRYPT
| EVP_PKEY_OP_TYPE_SIG
,
1139 OSSL_PKEY_PARAM_MGF1_DIGEST
, mdname
,
1140 OSSL_PKEY_PARAM_MGF1_PROPERTIES
, mdprops
);
1143 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
1146 return int_get_rsa_md_name(ctx
, -1,
1147 EVP_PKEY_OP_TYPE_CRYPT
| EVP_PKEY_OP_TYPE_SIG
,
1148 OSSL_PKEY_PARAM_MGF1_DIGEST
, name
, namesize
);
1152 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1153 * simply because that's easier.
1155 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1157 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1158 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)(md
));
1161 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX
*ctx
,
1164 return int_set_rsa_md_name(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1165 OSSL_PKEY_PARAM_MGF1_DIGEST
, mdname
,
1170 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1171 * simply because that's easier.
1173 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1175 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1176 EVP_PKEY_CTRL_GET_RSA_MGF1_MD
, 0, (void *)(md
));
1179 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, void *label
, int llen
)
1181 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1182 const char *empty
= "";
1184 * Needed as we swap label with empty if it is NULL, and label is
1185 * freed at the end of this function.
1187 void *plabel
= label
;
1190 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1191 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1192 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1196 /* If key type not RSA return error */
1197 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1200 /* Accept NULL for backward compatibility */
1201 if (label
== NULL
&& llen
== 0)
1202 plabel
= (void *)empty
;
1204 /* Cast away the const. This is read only so should be safe */
1205 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1206 (void *)plabel
, (size_t)llen
);
1207 *p
++ = OSSL_PARAM_construct_end();
1209 ret
= evp_pkey_ctx_set_params_strict(ctx
, rsa_params
);
1213 /* Ownership is supposed to be transferred to the callee. */
1214 OPENSSL_free(label
);
1218 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, unsigned char **label
)
1220 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1223 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1224 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1225 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1229 /* If key type not RSA return error */
1230 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1233 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1235 *p
++ = OSSL_PARAM_construct_end();
1237 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1240 labellen
= rsa_params
[0].return_size
;
1241 if (labellen
> INT_MAX
)
1244 return (int)labellen
;
1248 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1249 * simply because that's easier.
1251 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int saltlen
)
1254 * For some reason, the optype was set to this:
1256 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1258 * However, we do use RSA-PSS with the whole gamut of diverse signature
1259 * and verification operations, so the optype gets upgraded to this:
1261 * EVP_PKEY_OP_TYPE_SIG
1263 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
,
1264 EVP_PKEY_CTRL_RSA_PSS_SALTLEN
, saltlen
, NULL
);
1268 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1269 * simply because that's easier.
1271 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int *saltlen
)
1274 * Because of circumstances, the optype is updated from:
1276 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1280 * EVP_PKEY_OP_TYPE_SIG
1282 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
,
1283 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
, 0, saltlen
);
1286 int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX
*ctx
, int saltlen
)
1288 OSSL_PARAM pad_params
[2], *p
= pad_params
;
1290 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1291 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1292 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1296 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1299 *p
++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
,
1301 *p
++ = OSSL_PARAM_construct_end();
1303 return evp_pkey_ctx_set_params_strict(ctx
, pad_params
);
1306 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX
*ctx
, int bits
)
1308 OSSL_PARAM params
[2], *p
= params
;
1309 size_t bits2
= bits
;
1311 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1312 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1313 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1317 /* If key type not RSA return error */
1318 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
1319 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1322 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS
, &bits2
);
1323 *p
++ = OSSL_PARAM_construct_end();
1325 return evp_pkey_ctx_set_params_strict(ctx
, params
);
1328 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX
*ctx
, BIGNUM
*pubexp
)
1330 int ret
= RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_KEYGEN
,
1331 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
, 0, pubexp
);
1334 * Satisfy memory semantics for pre-3.0 callers of
1335 * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1336 * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1338 if (ret
> 0 && evp_pkey_ctx_is_provided(ctx
)) {
1339 BN_free(ctx
->rsa_pubexp
);
1340 ctx
->rsa_pubexp
= pubexp
;
1346 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX
*ctx
, BIGNUM
*pubexp
)
1351 * When we're dealing with a provider, there's no need to duplicate
1352 * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
1354 if (evp_pkey_ctx_is_legacy(ctx
)) {
1355 pubexp
= BN_dup(pubexp
);
1359 ret
= EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_KEYGEN
,
1360 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
, 0, pubexp
);
1361 if (evp_pkey_ctx_is_legacy(ctx
) && ret
<= 0)
1366 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX
*ctx
, int primes
)
1368 OSSL_PARAM params
[2], *p
= params
;
1369 size_t primes2
= primes
;
1371 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1372 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1373 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1377 /* If key type not RSA return error */
1378 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
1379 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1382 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES
, &primes2
);
1383 *p
++ = OSSL_PARAM_construct_end();
1385 return evp_pkey_ctx_set_params_strict(ctx
, params
);