2 * Copyright 1995-2023 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/security_bits.h"
29 #include "rsa_local.h"
31 static RSA
*rsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
);
36 return rsa_new_intern(NULL
, NULL
);
39 const RSA_METHOD
*RSA_get_method(const RSA
*rsa
)
44 int RSA_set_method(RSA
*rsa
, const RSA_METHOD
*meth
)
47 * NB: The caller is specifically setting a method, so it's not up to us
48 * to deal with which ENGINE it comes from.
50 const RSA_METHOD
*mtmp
;
54 #ifndef OPENSSL_NO_ENGINE
55 ENGINE_finish(rsa
->engine
);
64 RSA
*RSA_new_method(ENGINE
*engine
)
66 return rsa_new_intern(engine
, NULL
);
70 RSA
*ossl_rsa_new_with_ctx(OSSL_LIB_CTX
*libctx
)
72 return rsa_new_intern(NULL
, libctx
);
75 static RSA
*rsa_new_intern(ENGINE
*engine
, OSSL_LIB_CTX
*libctx
)
77 RSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
82 ret
->lock
= CRYPTO_THREAD_lock_new();
83 if (ret
->lock
== NULL
) {
84 ERR_raise(ERR_LIB_RSA
, ERR_R_CRYPTO_LIB
);
89 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
90 CRYPTO_THREAD_lock_free(ret
->lock
);
96 ret
->meth
= RSA_get_default_method();
97 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
98 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
100 if (!ENGINE_init(engine
)) {
101 ERR_raise(ERR_LIB_RSA
, ERR_R_ENGINE_LIB
);
104 ret
->engine
= engine
;
106 ret
->engine
= ENGINE_get_default_RSA();
109 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
110 if (ret
->meth
== NULL
) {
111 ERR_raise(ERR_LIB_RSA
, ERR_R_ENGINE_LIB
);
117 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
119 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
124 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
125 ERR_raise(ERR_LIB_RSA
, ERR_R_INIT_FAIL
);
136 void RSA_free(RSA
*r
)
143 CRYPTO_DOWN_REF(&r
->references
, &i
);
144 REF_PRINT_COUNT("RSA", r
);
147 REF_ASSERT_ISNT(i
< 0);
149 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
151 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
152 ENGINE_finish(r
->engine
);
156 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
159 CRYPTO_THREAD_lock_free(r
->lock
);
160 CRYPTO_FREE_REF(&r
->references
);
167 BN_clear_free(r
->dmp1
);
168 BN_clear_free(r
->dmq1
);
169 BN_clear_free(r
->iqmp
);
171 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
172 ossl_rsa_acvp_test_free(r
->acvp_test
);
176 RSA_PSS_PARAMS_free(r
->pss
);
177 sk_RSA_PRIME_INFO_pop_free(r
->prime_infos
, ossl_rsa_multip_info_free
);
179 BN_BLINDING_free(r
->blinding
);
180 BN_BLINDING_free(r
->mt_blinding
);
184 int RSA_up_ref(RSA
*r
)
188 if (CRYPTO_UP_REF(&r
->references
, &i
) <= 0)
191 REF_PRINT_COUNT("RSA", r
);
192 REF_ASSERT_ISNT(i
< 2);
193 return i
> 1 ? 1 : 0;
196 OSSL_LIB_CTX
*ossl_rsa_get0_libctx(RSA
*r
)
201 void ossl_rsa_set0_libctx(RSA
*r
, OSSL_LIB_CTX
*libctx
)
207 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
209 return CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
);
212 void *RSA_get_ex_data(const RSA
*r
, int idx
)
214 return CRYPTO_get_ex_data(&r
->ex_data
, idx
);
219 * Define a scaling constant for our fixed point arithmetic.
220 * This value must be a power of two because the base two logarithm code
221 * makes this assumption. The exponent must also be a multiple of three so
222 * that the scale factor has an exact cube root. Finally, the scale factor
223 * should not be so large that a multiplication of two scaled numbers
224 * overflows a 64 bit unsigned integer.
226 static const unsigned int scale
= 1 << 18;
227 static const unsigned int cbrt_scale
= 1 << (2 * 18 / 3);
229 /* Define some constants, none exceed 32 bits */
230 static const unsigned int log_2
= 0x02c5c8; /* scale * log(2) */
231 static const unsigned int log_e
= 0x05c551; /* scale * log2(M_E) */
232 static const unsigned int c1_923
= 0x07b126; /* scale * 1.923 */
233 static const unsigned int c4_690
= 0x12c28f; /* scale * 4.690 */
236 * Multiply two scaled integers together and rescale the result.
238 static ossl_inline
uint64_t mul2(uint64_t a
, uint64_t b
)
240 return a
* b
/ scale
;
244 * Calculate the cube root of a 64 bit scaled integer.
245 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
246 * integer, this is not guaranteed after scaling, so this function has a
247 * 64 bit return. This uses the shifting nth root algorithm with some
248 * algebraic simplifications.
250 static uint64_t icbrt64(uint64_t x
)
256 for (s
= 63; s
>= 0; s
-= 3) {
258 b
= 3 * r
* (r
+ 1) + 1;
264 return r
* cbrt_scale
;
268 * Calculate the natural logarithm of a 64 bit scaled integer.
269 * This is done by calculating a base two logarithm and scaling.
270 * The maximum logarithm (base 2) is 64 and this reduces base e, so
271 * a 32 bit result should not overflow. The argument passed must be
272 * greater than unity so we don't need to handle negative results.
274 static uint32_t ilog_e(uint64_t v
)
279 * Scale down the value into the range 1 .. 2.
281 * If fractional numbers need to be processed, another loop needs
282 * to go here that checks v < scale and if so multiplies it by 2 and
283 * reduces r by scale. This also means making r signed.
285 while (v
>= 2 * scale
) {
289 for (i
= scale
/ 2; i
!= 0; i
/= 2) {
291 if (v
>= 2 * scale
) {
296 r
= (r
* (uint64_t)scale
) / log_e
;
301 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
304 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
305 * for FFC safe prime groups for modp and ffdhe.
306 * After Table 25 and Table 26 it refers to
307 * "The maximum security strength estimates were calculated using the formula in
308 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
313 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
314 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
315 * The two cube roots are merged together here.
317 uint16_t ossl_ifc_ffc_compute_security_bits(int n
)
324 * Look for common values as listed in standards.
325 * These values are not exactly equal to the results from the formulae in
326 * the standards but are defined to be canonical.
329 case 2048: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
331 case 3072: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
333 case 4096: /* SP 800-56B rev 2 Appendix D */
335 case 6144: /* SP 800-56B rev 2 Appendix D */
337 case 7680: /* FIPS 140-2 IG 7.5 */
339 case 8192: /* SP 800-56B rev 2 Appendix D */
341 case 15360: /* FIPS 140-2 IG 7.5 */
346 * The first incorrect result (i.e. not accurate or off by one low) occurs
347 * for n = 699668. The true value here is 1200. Instead of using this n
348 * as the check threshold, the smallest n such that the correct result is
349 * 1200 is used instead.
357 * To ensure that the output is non-decreasing with respect to n,
358 * a cap needs to be applied to the two values where the function over
359 * estimates the strength (according to the above fast path).
368 x
= n
* (uint64_t)log_2
;
370 y
= (uint16_t)((mul2(c1_923
, icbrt64(mul2(mul2(x
, lx
), lx
))) - c4_690
)
380 int RSA_security_bits(const RSA
*rsa
)
382 int bits
= BN_num_bits(rsa
->n
);
385 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
) {
386 /* This ought to mean that we have private key at hand. */
387 int ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
);
389 if (ex_primes
<= 0 || (ex_primes
+ 2) > ossl_rsa_multip_cap(bits
))
393 return ossl_ifc_ffc_compute_security_bits(bits
);
396 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
398 /* If the fields n and e in r are NULL, the corresponding input
399 * parameters MUST be non-NULL for n and e. d may be
400 * left NULL (in case only the public key is used).
402 if ((r
->n
== NULL
&& n
== NULL
)
403 || (r
->e
== NULL
&& e
== NULL
))
417 BN_set_flags(r
->d
, BN_FLG_CONSTTIME
);
424 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
426 /* If the fields p and q in r are NULL, the corresponding input
427 * parameters MUST be non-NULL.
429 if ((r
->p
== NULL
&& p
== NULL
)
430 || (r
->q
== NULL
&& q
== NULL
))
436 BN_set_flags(r
->p
, BN_FLG_CONSTTIME
);
441 BN_set_flags(r
->q
, BN_FLG_CONSTTIME
);
448 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
450 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
451 * parameters MUST be non-NULL.
453 if ((r
->dmp1
== NULL
&& dmp1
== NULL
)
454 || (r
->dmq1
== NULL
&& dmq1
== NULL
)
455 || (r
->iqmp
== NULL
&& iqmp
== NULL
))
459 BN_clear_free(r
->dmp1
);
461 BN_set_flags(r
->dmp1
, BN_FLG_CONSTTIME
);
464 BN_clear_free(r
->dmq1
);
466 BN_set_flags(r
->dmq1
, BN_FLG_CONSTTIME
);
469 BN_clear_free(r
->iqmp
);
471 BN_set_flags(r
->iqmp
, BN_FLG_CONSTTIME
);
480 * Is it better to export RSA_PRIME_INFO structure
481 * and related functions to let user pass a triplet?
483 int RSA_set0_multi_prime_params(RSA
*r
, BIGNUM
*primes
[], BIGNUM
*exps
[],
484 BIGNUM
*coeffs
[], int pnum
)
486 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old
= NULL
;
487 RSA_PRIME_INFO
*pinfo
;
490 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
|| pnum
== 0)
493 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
494 if (prime_infos
== NULL
)
497 if (r
->prime_infos
!= NULL
)
498 old
= r
->prime_infos
;
500 for (i
= 0; i
< pnum
; i
++) {
501 pinfo
= ossl_rsa_multip_info_new();
504 if (primes
[i
] != NULL
&& exps
[i
] != NULL
&& coeffs
[i
] != NULL
) {
505 BN_clear_free(pinfo
->r
);
506 BN_clear_free(pinfo
->d
);
507 BN_clear_free(pinfo
->t
);
508 pinfo
->r
= primes
[i
];
510 pinfo
->t
= coeffs
[i
];
511 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
512 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
513 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
515 ossl_rsa_multip_info_free(pinfo
);
518 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
521 r
->prime_infos
= prime_infos
;
523 if (!ossl_rsa_multip_calc_product(r
)) {
524 r
->prime_infos
= old
;
530 * This is hard to deal with, since the old infos could
531 * also be set by this function and r, d, t should not
532 * be freed in that case. So currently, stay consistent
533 * with other *set0* functions: just free it...
535 sk_RSA_PRIME_INFO_pop_free(old
, ossl_rsa_multip_info_free
);
538 r
->version
= RSA_ASN1_VERSION_MULTI
;
543 /* r, d, t should not be freed */
544 sk_RSA_PRIME_INFO_pop_free(prime_infos
, ossl_rsa_multip_info_free_ex
);
549 void RSA_get0_key(const RSA
*r
,
550 const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
560 void RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
569 int RSA_get_multi_prime_extra_count(const RSA
*r
)
573 pnum
= sk_RSA_PRIME_INFO_num(r
->prime_infos
);
579 int RSA_get0_multi_prime_factors(const RSA
*r
, const BIGNUM
*primes
[])
582 RSA_PRIME_INFO
*pinfo
;
584 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
588 * return other primes
589 * it's caller's responsibility to allocate oth_primes[pnum]
591 for (i
= 0; i
< pnum
; i
++) {
592 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
593 primes
[i
] = pinfo
->r
;
600 void RSA_get0_crt_params(const RSA
*r
,
601 const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
613 int RSA_get0_multi_prime_crt_params(const RSA
*r
, const BIGNUM
*exps
[],
614 const BIGNUM
*coeffs
[])
618 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
621 /* return other primes */
622 if (exps
!= NULL
|| coeffs
!= NULL
) {
623 RSA_PRIME_INFO
*pinfo
;
626 /* it's the user's job to guarantee the buffer length */
627 for (i
= 0; i
< pnum
; i
++) {
628 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
632 coeffs
[i
] = pinfo
->t
;
640 const BIGNUM
*RSA_get0_n(const RSA
*r
)
645 const BIGNUM
*RSA_get0_e(const RSA
*r
)
650 const BIGNUM
*RSA_get0_d(const RSA
*r
)
655 const BIGNUM
*RSA_get0_p(const RSA
*r
)
660 const BIGNUM
*RSA_get0_q(const RSA
*r
)
665 const BIGNUM
*RSA_get0_dmp1(const RSA
*r
)
670 const BIGNUM
*RSA_get0_dmq1(const RSA
*r
)
675 const BIGNUM
*RSA_get0_iqmp(const RSA
*r
)
680 const RSA_PSS_PARAMS
*RSA_get0_pss_params(const RSA
*r
)
690 int ossl_rsa_set0_pss_params(RSA
*r
, RSA_PSS_PARAMS
*pss
)
695 RSA_PSS_PARAMS_free(r
->pss
);
702 RSA_PSS_PARAMS_30
*ossl_rsa_get0_pss_params_30(RSA
*r
)
704 return &r
->pss_params
;
707 void RSA_clear_flags(RSA
*r
, int flags
)
712 int RSA_test_flags(const RSA
*r
, int flags
)
714 return r
->flags
& flags
;
717 void RSA_set_flags(RSA
*r
, int flags
)
722 int RSA_get_version(RSA
*r
)
724 /* { two-prime(0), multi(1) } */
729 ENGINE
*RSA_get0_engine(const RSA
*r
)
734 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, int p1
, void *p2
)
736 /* If key type not RSA or RSA-PSS return error */
737 if (ctx
!= NULL
&& ctx
->pmeth
!= NULL
738 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
739 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
741 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, p1
, p2
);
745 DEFINE_STACK_OF(BIGNUM
)
748 * Note: This function deletes values from the parameter
749 * stack values as they are consumed and set in the RSA key.
751 int ossl_rsa_set0_all_params(RSA
*r
, STACK_OF(BIGNUM
) *primes
,
752 STACK_OF(BIGNUM
) *exps
,
753 STACK_OF(BIGNUM
) *coeffs
)
756 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old_infos
= NULL
;
760 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
763 pnum
= sk_BIGNUM_num(primes
);
765 /* we need at least 2 primes */
769 if (!RSA_set0_factors(r
, sk_BIGNUM_value(primes
, 0),
770 sk_BIGNUM_value(primes
, 1)))
774 * if we managed to set everything above, remove those elements from the
776 * Note, we do this after the above all to ensure that we have taken
777 * ownership of all the elements in the RSA key to avoid memory leaks
778 * we also use delete 0 here as we are grabbing items from the end of the
779 * stack rather than the start, otherwise we could use pop
781 sk_BIGNUM_delete(primes
, 0);
782 sk_BIGNUM_delete(primes
, 0);
784 if (pnum
== sk_BIGNUM_num(exps
)
785 && pnum
== sk_BIGNUM_num(coeffs
) + 1) {
787 if (!RSA_set0_crt_params(r
, sk_BIGNUM_value(exps
, 0),
788 sk_BIGNUM_value(exps
, 1),
789 sk_BIGNUM_value(coeffs
, 0)))
792 /* as above, once we consume the above params, delete them from the list */
793 sk_BIGNUM_delete(exps
, 0);
794 sk_BIGNUM_delete(exps
, 0);
795 sk_BIGNUM_delete(coeffs
, 0);
799 old_infos
= r
->prime_infos
;
806 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
807 if (prime_infos
== NULL
)
810 for (i
= 2; i
< pnum
; i
++) {
811 BIGNUM
*prime
= sk_BIGNUM_pop(primes
);
812 BIGNUM
*exp
= sk_BIGNUM_pop(exps
);
813 BIGNUM
*coeff
= sk_BIGNUM_pop(coeffs
);
814 RSA_PRIME_INFO
*pinfo
= NULL
;
816 if (!ossl_assert(prime
!= NULL
&& exp
!= NULL
&& coeff
!= NULL
))
819 /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
820 if ((pinfo
= OPENSSL_zalloc(sizeof(*pinfo
))) == NULL
)
826 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
827 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
828 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
829 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
832 r
->prime_infos
= prime_infos
;
834 if (!ossl_rsa_multip_calc_product(r
)) {
835 r
->prime_infos
= old_infos
;
844 if (old_infos
!= NULL
) {
846 * This is hard to deal with, since the old infos could
847 * also be set by this function and r, d, t should not
848 * be freed in that case. So currently, stay consistent
849 * with other *set0* functions: just free it...
851 sk_RSA_PRIME_INFO_pop_free(old_infos
, ossl_rsa_multip_info_free
);
855 r
->version
= pnum
> 2 ? RSA_ASN1_VERSION_MULTI
: RSA_ASN1_VERSION_DEFAULT
;
861 /* r, d, t should not be freed */
862 sk_RSA_PRIME_INFO_pop_free(prime_infos
, ossl_rsa_multip_info_free_ex
);
867 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
869 int ossl_rsa_get0_all_params(RSA
*r
, STACK_OF(BIGNUM_const
) *primes
,
870 STACK_OF(BIGNUM_const
) *exps
,
871 STACK_OF(BIGNUM_const
) *coeffs
)
874 RSA_PRIME_INFO
*pinfo
;
881 /* If |p| is NULL, there are no CRT parameters */
882 if (RSA_get0_p(r
) == NULL
)
885 sk_BIGNUM_const_push(primes
, RSA_get0_p(r
));
886 sk_BIGNUM_const_push(primes
, RSA_get0_q(r
));
887 sk_BIGNUM_const_push(exps
, RSA_get0_dmp1(r
));
888 sk_BIGNUM_const_push(exps
, RSA_get0_dmq1(r
));
889 sk_BIGNUM_const_push(coeffs
, RSA_get0_iqmp(r
));
892 pnum
= RSA_get_multi_prime_extra_count(r
);
893 for (i
= 0; i
< pnum
; i
++) {
894 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
895 sk_BIGNUM_const_push(primes
, pinfo
->r
);
896 sk_BIGNUM_const_push(exps
, pinfo
->d
);
897 sk_BIGNUM_const_push(coeffs
, pinfo
->t
);
905 /* Helpers to set or get diverse hash algorithm names */
906 static int int_set_rsa_md_name(EVP_PKEY_CTX
*ctx
,
908 int keytype
, int optype
,
909 /* For EVP_PKEY_CTX_set_params() */
910 const char *mdkey
, const char *mdname
,
911 const char *propkey
, const char *mdprops
)
913 OSSL_PARAM params
[3], *p
= params
;
915 if (ctx
== NULL
|| mdname
== NULL
|| (ctx
->operation
& optype
) == 0) {
916 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
917 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
921 /* If key type not RSA return error */
924 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
925 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
929 if (!EVP_PKEY_CTX_is_a(ctx
, evp_pkey_type2name(keytype
)))
934 /* Cast away the const. This is read only so should be safe */
935 *p
++ = OSSL_PARAM_construct_utf8_string(mdkey
, (char *)mdname
, 0);
936 if (evp_pkey_ctx_is_provided(ctx
) && mdprops
!= NULL
) {
937 /* Cast away the const. This is read only so should be safe */
938 *p
++ = OSSL_PARAM_construct_utf8_string(propkey
, (char *)mdprops
, 0);
940 *p
++ = OSSL_PARAM_construct_end();
942 return evp_pkey_ctx_set_params_strict(ctx
, params
);
945 /* Helpers to set or get diverse hash algorithm names */
946 static int int_get_rsa_md_name(EVP_PKEY_CTX
*ctx
,
948 int keytype
, int optype
,
949 /* For EVP_PKEY_CTX_get_params() */
951 char *mdname
, size_t mdnamesize
)
953 OSSL_PARAM params
[2], *p
= params
;
955 if (ctx
== NULL
|| mdname
== NULL
|| (ctx
->operation
& optype
) == 0) {
956 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
957 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
961 /* If key type not RSA return error */
964 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
965 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
969 if (!EVP_PKEY_CTX_is_a(ctx
, evp_pkey_type2name(keytype
)))
974 /* Cast away the const. This is read only so should be safe */
975 *p
++ = OSSL_PARAM_construct_utf8_string(mdkey
, (char *)mdname
, mdnamesize
);
976 *p
++ = OSSL_PARAM_construct_end();
978 return evp_pkey_ctx_get_params_strict(ctx
, params
);
982 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
983 * simply because that's easier.
985 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX
*ctx
, int pad_mode
)
987 return RSA_pkey_ctx_ctrl(ctx
, -1, EVP_PKEY_CTRL_RSA_PADDING
,
992 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
993 * simply because that's easier.
995 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX
*ctx
, int *pad_mode
)
997 return RSA_pkey_ctx_ctrl(ctx
, -1, EVP_PKEY_CTRL_GET_RSA_PADDING
,
1002 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1003 * simply because that's easier.
1005 int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1007 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1008 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
1011 int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX
*ctx
,
1013 const char *mdprops
)
1015 return int_set_rsa_md_name(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1016 OSSL_PKEY_PARAM_RSA_DIGEST
, mdname
,
1017 OSSL_PKEY_PARAM_RSA_DIGEST_PROPS
, mdprops
);
1021 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1022 * simply because that's easier.
1024 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1026 /* If key type not RSA return error */
1027 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1030 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1031 EVP_PKEY_CTRL_RSA_OAEP_MD
, 0, (void *)(md
));
1034 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
1035 const char *mdprops
)
1038 int_set_rsa_md_name(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1039 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, mdname
,
1040 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
, mdprops
);
1043 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
1046 return int_get_rsa_md_name(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1047 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
1052 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1053 * simply because that's easier.
1055 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1057 /* If key type not RSA return error */
1058 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1061 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1062 EVP_PKEY_CTRL_GET_RSA_OAEP_MD
, 0, (void *)md
);
1066 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1067 * simply because that's easier.
1069 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1071 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1072 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)(md
));
1075 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
1076 const char *mdprops
)
1078 return int_set_rsa_md_name(ctx
, -1,
1079 EVP_PKEY_OP_TYPE_CRYPT
| EVP_PKEY_OP_TYPE_SIG
,
1080 OSSL_PKEY_PARAM_MGF1_DIGEST
, mdname
,
1081 OSSL_PKEY_PARAM_MGF1_PROPERTIES
, mdprops
);
1084 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
1087 return int_get_rsa_md_name(ctx
, -1,
1088 EVP_PKEY_OP_TYPE_CRYPT
| EVP_PKEY_OP_TYPE_SIG
,
1089 OSSL_PKEY_PARAM_MGF1_DIGEST
, name
, namesize
);
1093 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1094 * simply because that's easier.
1096 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
1098 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1099 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)(md
));
1102 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX
*ctx
,
1105 return int_set_rsa_md_name(ctx
, EVP_PKEY_RSA_PSS
, EVP_PKEY_OP_KEYGEN
,
1106 OSSL_PKEY_PARAM_MGF1_DIGEST
, mdname
,
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_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1116 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1117 EVP_PKEY_CTRL_GET_RSA_MGF1_MD
, 0, (void *)(md
));
1120 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, void *label
, int llen
)
1122 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1123 const char *empty
= "";
1125 * Needed as we swap label with empty if it is NULL, and label is
1126 * freed at the end of this function.
1128 void *plabel
= label
;
1131 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1132 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1133 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1137 /* If key type not RSA return error */
1138 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1141 /* Accept NULL for backward compatibility */
1142 if (label
== NULL
&& llen
== 0)
1143 plabel
= (void *)empty
;
1145 /* Cast away the const. This is read only so should be safe */
1146 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1147 (void *)plabel
, (size_t)llen
);
1148 *p
++ = OSSL_PARAM_construct_end();
1150 ret
= evp_pkey_ctx_set_params_strict(ctx
, rsa_params
);
1154 /* Ownership is supposed to be transferred to the callee. */
1155 OPENSSL_free(label
);
1159 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, unsigned char **label
)
1161 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1164 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1165 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1166 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1170 /* If key type not RSA return error */
1171 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA"))
1174 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1176 *p
++ = OSSL_PARAM_construct_end();
1178 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1181 labellen
= rsa_params
[0].return_size
;
1182 if (labellen
> INT_MAX
)
1185 return (int)labellen
;
1189 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1190 * simply because that's easier.
1192 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int saltlen
)
1195 * For some reason, the optype was set to this:
1197 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1199 * However, we do use RSA-PSS with the whole gamut of diverse signature
1200 * and verification operations, so the optype gets upgraded to this:
1202 * EVP_PKEY_OP_TYPE_SIG
1204 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
,
1205 EVP_PKEY_CTRL_RSA_PSS_SALTLEN
, saltlen
, NULL
);
1209 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1210 * simply because that's easier.
1212 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int *saltlen
)
1215 * Because of circumstances, the optype is updated from:
1217 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1221 * EVP_PKEY_OP_TYPE_SIG
1223 return RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_TYPE_SIG
,
1224 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
, 0, saltlen
);
1227 int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX
*ctx
, int saltlen
)
1229 OSSL_PARAM pad_params
[2], *p
= pad_params
;
1231 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1232 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1233 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1237 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1240 *p
++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
,
1242 *p
++ = OSSL_PARAM_construct_end();
1244 return evp_pkey_ctx_set_params_strict(ctx
, pad_params
);
1247 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX
*ctx
, int bits
)
1249 OSSL_PARAM params
[2], *p
= params
;
1250 size_t bits2
= bits
;
1252 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1253 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1254 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1258 /* If key type not RSA return error */
1259 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
1260 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1263 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS
, &bits2
);
1264 *p
++ = OSSL_PARAM_construct_end();
1266 return evp_pkey_ctx_set_params_strict(ctx
, params
);
1269 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX
*ctx
, BIGNUM
*pubexp
)
1271 int ret
= RSA_pkey_ctx_ctrl(ctx
, EVP_PKEY_OP_KEYGEN
,
1272 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
, 0, pubexp
);
1275 * Satisfy memory semantics for pre-3.0 callers of
1276 * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1277 * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1279 if (ret
> 0 && evp_pkey_ctx_is_provided(ctx
)) {
1280 BN_free(ctx
->rsa_pubexp
);
1281 ctx
->rsa_pubexp
= pubexp
;
1287 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX
*ctx
, BIGNUM
*pubexp
)
1292 * When we're dealing with a provider, there's no need to duplicate
1293 * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
1295 if (evp_pkey_ctx_is_legacy(ctx
)) {
1296 pubexp
= BN_dup(pubexp
);
1300 ret
= EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_KEYGEN
,
1301 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
, 0, pubexp
);
1302 if (evp_pkey_ctx_is_legacy(ctx
) && ret
<= 0)
1307 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX
*ctx
, int primes
)
1309 OSSL_PARAM params
[2], *p
= params
;
1310 size_t primes2
= primes
;
1312 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1313 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1314 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1318 /* If key type not RSA return error */
1319 if (!EVP_PKEY_CTX_is_a(ctx
, "RSA")
1320 && !EVP_PKEY_CTX_is_a(ctx
, "RSA-PSS"))
1323 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES
, &primes2
);
1324 *p
++ = OSSL_PARAM_construct_end();
1326 return evp_pkey_ctx_set_params_strict(ctx
, params
);