2 * Copyright 1995-2018 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"
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"
28 static RSA
*rsa_new_intern(ENGINE
*engine
, OPENSSL_CTX
*libctx
);
33 return rsa_new_intern(NULL
, NULL
);
36 const RSA_METHOD
*RSA_get_method(const RSA
*rsa
)
41 int RSA_set_method(RSA
*rsa
, const RSA_METHOD
*meth
)
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.
47 const RSA_METHOD
*mtmp
;
51 #ifndef OPENSSL_NO_ENGINE
52 ENGINE_finish(rsa
->engine
);
61 RSA
*RSA_new_method(ENGINE
*engine
)
63 return rsa_new_intern(engine
, NULL
);
67 RSA
*rsa_new_with_ctx(OPENSSL_CTX
*libctx
)
69 return rsa_new_intern(NULL
, libctx
);
72 static RSA
*rsa_new_intern(ENGINE
*engine
, OPENSSL_CTX
*libctx
)
74 RSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
77 RSAerr(0, ERR_R_MALLOC_FAILURE
);
82 ret
->lock
= CRYPTO_THREAD_lock_new();
83 if (ret
->lock
== NULL
) {
84 RSAerr(0, ERR_R_MALLOC_FAILURE
);
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
;
94 if (!ENGINE_init(engine
)) {
95 RSAerr(0, ERR_R_ENGINE_LIB
);
100 ret
->engine
= ENGINE_get_default_RSA();
103 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
104 if (ret
->meth
== NULL
) {
105 RSAerr(0, ERR_R_ENGINE_LIB
);
111 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
113 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
118 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
119 RSAerr(0, ERR_R_INIT_FAIL
);
130 void RSA_free(RSA
*r
)
137 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
138 REF_PRINT_COUNT("RSA", r
);
141 REF_ASSERT_ISNT(i
< 0);
143 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
145 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
146 ENGINE_finish(r
->engine
);
150 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
153 CRYPTO_THREAD_lock_free(r
->lock
);
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 */
165 RSA_PSS_PARAMS_free(r
->pss
);
166 sk_RSA_PRIME_INFO_pop_free(r
->prime_infos
, rsa_multip_info_free
);
168 BN_BLINDING_free(r
->blinding
);
169 BN_BLINDING_free(r
->mt_blinding
);
170 OPENSSL_free(r
->bignum_data
);
174 int RSA_up_ref(RSA
*r
)
178 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
181 REF_PRINT_COUNT("RSA", r
);
182 REF_ASSERT_ISNT(i
< 2);
183 return i
> 1 ? 1 : 0;
187 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
189 return CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
);
192 void *RSA_get_ex_data(const RSA
*r
, int idx
)
194 return CRYPTO_get_ex_data(&r
->ex_data
, idx
);
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.
206 static const unsigned int scale
= 1 << 18;
207 static const unsigned int cbrt_scale
= 1 << (2 * 18 / 3);
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 */
216 * Multiply two scaled integers together and rescale the result.
218 static ossl_inline
uint64_t mul2(uint64_t a
, uint64_t b
)
220 return a
* b
/ scale
;
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.
230 static uint64_t icbrt64(uint64_t x
)
236 for (s
= 63; s
>= 0; s
-= 3) {
238 b
= 3 * r
* (r
+ 1) + 1;
244 return r
* cbrt_scale
;
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.
254 static uint32_t ilog_e(uint64_t v
)
259 * Scale down the value into the range 1 .. 2.
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.
265 while (v
>= 2 * scale
) {
269 for (i
= scale
/ 2; i
!= 0; i
/= 2) {
271 if (v
>= 2 * scale
) {
276 r
= (r
* (uint64_t)scale
) / log_e
;
281 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
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.
288 uint16_t rsa_compute_security_bits(int n
)
294 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
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.
318 x
= n
* (uint64_t)log_2
;
320 y
= (uint16_t)((mul2(c1_923
, icbrt64(mul2(mul2(x
, lx
), lx
))) - c4_690
)
325 int RSA_security_bits(const RSA
*rsa
)
327 int bits
= BN_num_bits(rsa
->n
);
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
);
334 if (ex_primes
<= 0 || (ex_primes
+ 2) > rsa_multip_cap(bits
))
338 return rsa_compute_security_bits(bits
);
341 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
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).
347 if ((r
->n
== NULL
&& n
== NULL
)
348 || (r
->e
== NULL
&& e
== NULL
))
362 BN_set_flags(r
->d
, BN_FLG_CONSTTIME
);
369 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
371 /* If the fields p and q in r are NULL, the corresponding input
372 * parameters MUST be non-NULL.
374 if ((r
->p
== NULL
&& p
== NULL
)
375 || (r
->q
== NULL
&& q
== NULL
))
381 BN_set_flags(r
->p
, BN_FLG_CONSTTIME
);
386 BN_set_flags(r
->q
, BN_FLG_CONSTTIME
);
393 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
395 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
396 * parameters MUST be non-NULL.
398 if ((r
->dmp1
== NULL
&& dmp1
== NULL
)
399 || (r
->dmq1
== NULL
&& dmq1
== NULL
)
400 || (r
->iqmp
== NULL
&& iqmp
== NULL
))
404 BN_clear_free(r
->dmp1
);
406 BN_set_flags(r
->dmp1
, BN_FLG_CONSTTIME
);
409 BN_clear_free(r
->dmq1
);
411 BN_set_flags(r
->dmq1
, BN_FLG_CONSTTIME
);
414 BN_clear_free(r
->iqmp
);
416 BN_set_flags(r
->iqmp
, BN_FLG_CONSTTIME
);
425 * Is it better to export RSA_PRIME_INFO structure
426 * and related functions to let user pass a triplet?
428 int RSA_set0_multi_prime_params(RSA
*r
, BIGNUM
*primes
[], BIGNUM
*exps
[],
429 BIGNUM
*coeffs
[], int pnum
)
431 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old
= NULL
;
432 RSA_PRIME_INFO
*pinfo
;
435 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
|| pnum
== 0)
438 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
439 if (prime_infos
== NULL
)
442 if (r
->prime_infos
!= NULL
)
443 old
= r
->prime_infos
;
445 for (i
= 0; i
< pnum
; i
++) {
446 pinfo
= rsa_multip_info_new();
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
];
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
);
460 rsa_multip_info_free(pinfo
);
463 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
466 r
->prime_infos
= prime_infos
;
468 if (!rsa_multip_calc_product(r
)) {
469 r
->prime_infos
= old
;
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...
480 sk_RSA_PRIME_INFO_pop_free(old
, rsa_multip_info_free
);
483 r
->version
= RSA_ASN1_VERSION_MULTI
;
488 /* r, d, t should not be freed */
489 sk_RSA_PRIME_INFO_pop_free(prime_infos
, rsa_multip_info_free_ex
);
494 void RSA_get0_key(const RSA
*r
,
495 const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
505 void RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
514 int RSA_get_multi_prime_extra_count(const RSA
*r
)
518 pnum
= sk_RSA_PRIME_INFO_num(r
->prime_infos
);
524 int RSA_get0_multi_prime_factors(const RSA
*r
, const BIGNUM
*primes
[])
527 RSA_PRIME_INFO
*pinfo
;
529 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
533 * return other primes
534 * it's caller's responsibility to allocate oth_primes[pnum]
536 for (i
= 0; i
< pnum
; i
++) {
537 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
538 primes
[i
] = pinfo
->r
;
545 void RSA_get0_crt_params(const RSA
*r
,
546 const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
558 int RSA_get0_multi_prime_crt_params(const RSA
*r
, const BIGNUM
*exps
[],
559 const BIGNUM
*coeffs
[])
563 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
566 /* return other primes */
567 if (exps
!= NULL
|| coeffs
!= NULL
) {
568 RSA_PRIME_INFO
*pinfo
;
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
);
577 coeffs
[i
] = pinfo
->t
;
585 const BIGNUM
*RSA_get0_n(const RSA
*r
)
590 const BIGNUM
*RSA_get0_e(const RSA
*r
)
595 const BIGNUM
*RSA_get0_d(const RSA
*r
)
600 const BIGNUM
*RSA_get0_p(const RSA
*r
)
605 const BIGNUM
*RSA_get0_q(const RSA
*r
)
610 const BIGNUM
*RSA_get0_dmp1(const RSA
*r
)
615 const BIGNUM
*RSA_get0_dmq1(const RSA
*r
)
620 const BIGNUM
*RSA_get0_iqmp(const RSA
*r
)
625 /* TODO(3.0): Temporary until we move PSS support into the FIPS module */
627 const RSA_PSS_PARAMS
*RSA_get0_pss_params(const RSA
*r
)
633 void RSA_clear_flags(RSA
*r
, int flags
)
638 int RSA_test_flags(const RSA
*r
, int flags
)
640 return r
->flags
& flags
;
643 void RSA_set_flags(RSA
*r
, int flags
)
648 int RSA_get_version(RSA
*r
)
650 /* { two-prime(0), multi(1) } */
655 ENGINE
*RSA_get0_engine(const RSA
*r
)
660 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, int p1
, void *p2
)
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
)
667 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, p1
, p2
);
671 DEFINE_STACK_OF(BIGNUM
)
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
)
678 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old_infos
= NULL
;
682 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
685 pnum
= sk_BIGNUM_num(primes
);
687 || pnum
!= sk_BIGNUM_num(exps
)
688 || pnum
!= sk_BIGNUM_num(coeffs
) + 1)
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)))
699 old_infos
= r
->prime_infos
;
706 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
707 if (prime_infos
== NULL
)
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
;
716 if (!ossl_assert(prime
!= NULL
&& exp
!= NULL
&& coeff
!= NULL
))
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
);
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
);
734 r
->prime_infos
= prime_infos
;
736 if (!rsa_multip_calc_product(r
)) {
737 r
->prime_infos
= old_infos
;
746 if (old_infos
!= NULL
) {
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...
753 sk_RSA_PRIME_INFO_pop_free(old_infos
, rsa_multip_info_free
);
757 r
->version
= pnum
> 2 ? RSA_ASN1_VERSION_MULTI
: RSA_ASN1_VERSION_DEFAULT
;
763 /* r, d, t should not be freed */
764 sk_RSA_PRIME_INFO_pop_free(prime_infos
, rsa_multip_info_free_ex
);
769 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
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
)
776 RSA_PRIME_INFO
*pinfo
;
783 /* If |p| is NULL, there are no CRT parameters */
784 if (RSA_get0_p(r
) == NULL
)
787 sk_BIGNUM_const_push(primes
, RSA_get0_p(r
));
788 sk_BIGNUM_const_push(primes
, RSA_get0_q(r
));
789 sk_BIGNUM_const_push(exps
, RSA_get0_dmp1(r
));
790 sk_BIGNUM_const_push(exps
, RSA_get0_dmq1(r
));
791 sk_BIGNUM_const_push(coeffs
, RSA_get0_iqmp(r
));
794 pnum
= RSA_get_multi_prime_extra_count(r
);
795 for (i
= 0; i
< pnum
; i
++) {
796 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
797 sk_BIGNUM_const_push(primes
, pinfo
->r
);
798 sk_BIGNUM_const_push(exps
, pinfo
->d
);
799 sk_BIGNUM_const_push(coeffs
, pinfo
->t
);
807 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX
*ctx
, int pad_mode
)
809 OSSL_PARAM pad_params
[2], *p
= pad_params
;
812 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
813 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
817 /* If key type not RSA or RSA-PSS return error */
818 if (ctx
->pmeth
!= NULL
819 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
820 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
823 /* TODO(3.0): Remove this eventually when no more legacy */
824 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
825 || ctx
->op
.ciph
.ciphprovctx
== NULL
)
826 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
827 || ctx
->op
.sig
.sigprovctx
== NULL
))
828 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_RSA_PADDING
,
831 *p
++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE
, &pad_mode
);
832 *p
++ = OSSL_PARAM_construct_end();
834 return EVP_PKEY_CTX_set_params(ctx
, pad_params
);
837 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX
*ctx
, int *pad_mode
)
839 OSSL_PARAM pad_params
[2], *p
= pad_params
;
841 if (ctx
== NULL
|| pad_mode
== NULL
) {
842 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
843 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
847 /* If key type not RSA or RSA-PSS return error */
848 if (ctx
->pmeth
!= NULL
849 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
850 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
853 /* TODO(3.0): Remove this eventually when no more legacy */
854 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
855 || ctx
->op
.ciph
.ciphprovctx
== NULL
)
856 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
857 || ctx
->op
.sig
.sigprovctx
== NULL
))
858 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING
, 0,
861 *p
++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE
, pad_mode
);
862 *p
++ = OSSL_PARAM_construct_end();
864 if (!EVP_PKEY_CTX_get_params(ctx
, pad_params
))
871 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
875 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
876 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
877 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
881 /* If key type not RSA return error */
882 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
885 /* TODO(3.0): Remove this eventually when no more legacy */
886 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
887 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
888 EVP_PKEY_CTRL_RSA_OAEP_MD
, 0, (void *)md
);
890 name
= (md
== NULL
) ? "" : EVP_MD_name(md
);
892 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx
, name
, NULL
);
895 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
898 OSSL_PARAM rsa_params
[3], *p
= rsa_params
;
900 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
901 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
902 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
906 /* If key type not RSA return error */
907 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
911 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
913 * Cast away the const. This is read
914 * only so should be safe
917 if (mdprops
!= NULL
) {
918 *p
++ = OSSL_PARAM_construct_utf8_string(
919 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
,
921 * Cast away the const. This is read
922 * only so should be safe
926 *p
++ = OSSL_PARAM_construct_end();
928 return EVP_PKEY_CTX_set_params(ctx
, rsa_params
);
931 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
934 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
936 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
937 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
938 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
942 /* If key type not RSA return error */
943 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
946 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
948 *p
++ = OSSL_PARAM_construct_end();
950 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
956 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
958 /* 80 should be big enough */
961 if (ctx
== NULL
|| md
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
962 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
963 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
967 /* If key type not RSA return error */
968 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
971 /* TODO(3.0): Remove this eventually when no more legacy */
972 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
973 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
974 EVP_PKEY_CTRL_GET_RSA_OAEP_MD
, 0, (void *)md
);
976 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx
, name
, sizeof(name
)) <= 0)
979 /* May be NULL meaning "unknown" */
980 *md
= EVP_get_digestbyname(name
);
985 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
990 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
991 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
992 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
993 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
997 /* If key type not RSA return error */
998 if (ctx
->pmeth
!= NULL
999 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1000 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1003 /* TODO(3.0): Remove this eventually when no more legacy */
1004 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1005 && ctx
->op
.ciph
.ciphprovctx
== NULL
)
1006 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
1007 && ctx
->op
.sig
.sigprovctx
== NULL
))
1008 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
,
1009 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1010 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)md
);
1012 name
= (md
== NULL
) ? "" : EVP_MD_name(md
);
1014 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx
, name
, NULL
);
1017 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
1018 const char *mdprops
)
1020 OSSL_PARAM rsa_params
[3], *p
= rsa_params
;
1024 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1025 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
1026 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1027 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1031 /* If key type not RSA return error */
1032 if (ctx
->pmeth
!= NULL
1033 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1034 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1037 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST
,
1039 * Cast away the const. This is
1040 * read only so should be safe
1043 if (mdprops
!= NULL
) {
1045 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES
,
1047 * Cast away the const. This is
1048 * read only so should be safe
1050 (char *)mdprops
, 0);
1052 *p
++ = OSSL_PARAM_construct_end();
1054 return EVP_PKEY_CTX_set_params(ctx
, rsa_params
);
1057 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
1060 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1063 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1064 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
1065 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1066 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1070 /* If key type not RSA or RSA-PSS return error */
1071 if (ctx
->pmeth
!= NULL
1072 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1073 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1076 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST
,
1078 *p
++ = OSSL_PARAM_construct_end();
1080 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1086 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1088 /* 80 should be big enough */
1092 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1093 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
1094 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1095 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1099 /* If key type not RSA or RSA-PSS return error */
1100 if (ctx
->pmeth
!= NULL
1101 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1102 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1105 /* TODO(3.0): Remove this eventually when no more legacy */
1106 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1107 && ctx
->op
.ciph
.ciphprovctx
== NULL
)
1108 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
1109 && ctx
->op
.sig
.sigprovctx
== NULL
))
1110 return EVP_PKEY_CTX_ctrl(ctx
, -1,
1111 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1112 EVP_PKEY_CTRL_GET_RSA_MGF1_MD
, 0, (void *)md
);
1114 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx
, name
, sizeof(name
)) <= 0)
1117 /* May be NULL meaning "unknown" */
1118 *md
= EVP_get_digestbyname(name
);
1123 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, void *label
, int llen
)
1125 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1127 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1128 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1129 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1133 /* If key type not RSA return error */
1134 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
1137 /* TODO(3.0): Remove this eventually when no more legacy */
1138 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
1139 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1140 EVP_PKEY_CTRL_RSA_OAEP_LABEL
, llen
,
1143 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1145 * Cast away the const. This is
1146 * read only so should be safe
1150 *p
++ = OSSL_PARAM_construct_end();
1152 if (!EVP_PKEY_CTX_set_params(ctx
, rsa_params
))
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
[3], *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 (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
1174 /* TODO(3.0): Remove this eventually when no more legacy */
1175 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
1176 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1177 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
, 0,
1180 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1182 *p
++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
,
1184 *p
++ = OSSL_PARAM_construct_end();
1186 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1189 if (labellen
> INT_MAX
)
1192 return (int)labellen
;
1195 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int saltlen
)
1197 OSSL_PARAM pad_params
[2], *p
= pad_params
;
1200 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1201 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1205 /* If key type not RSA or RSA-PSS return error */
1206 if (ctx
->pmeth
!= NULL
1207 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1208 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1211 /* TODO(3.0): Remove this eventually when no more legacy */
1212 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
1213 || ctx
->op
.sig
.sigprovctx
== NULL
)
1214 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN
,
1218 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, &saltlen
);
1219 *p
++ = OSSL_PARAM_construct_end();
1221 return EVP_PKEY_CTX_set_params(ctx
, pad_params
);
1224 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX
*ctx
, int *saltlen
)
1226 OSSL_PARAM pad_params
[2], *p
= pad_params
;
1228 if (ctx
== NULL
|| saltlen
== NULL
) {
1229 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1230 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1234 /* If key type not RSA or RSA-PSS return error */
1235 if (ctx
->pmeth
!= NULL
1236 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1237 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1240 /* TODO(3.0): Remove this eventually when no more legacy */
1241 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
1242 || ctx
->op
.sig
.sigprovctx
== NULL
)
1243 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1244 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
,
1248 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN
, saltlen
);
1249 *p
++ = OSSL_PARAM_construct_end();
1251 if (!EVP_PKEY_CTX_get_params(ctx
, pad_params
))