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 #include <openssl/crypto.h>
12 #include <openssl/core_names.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include "internal/cryptlib.h"
16 #include "internal/refcount.h"
17 #include "crypto/bn.h"
18 #include "crypto/evp.h"
19 #include "crypto/rsa.h"
20 #include "rsa_local.h"
24 return RSA_new_method(NULL
);
27 const RSA_METHOD
*RSA_get_method(const RSA
*rsa
)
32 int RSA_set_method(RSA
*rsa
, const RSA_METHOD
*meth
)
35 * NB: The caller is specifically setting a method, so it's not up to us
36 * to deal with which ENGINE it comes from.
38 const RSA_METHOD
*mtmp
;
42 #ifndef OPENSSL_NO_ENGINE
43 ENGINE_finish(rsa
->engine
);
52 RSA
*RSA_new_method(ENGINE
*engine
)
54 RSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
57 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
62 ret
->lock
= CRYPTO_THREAD_lock_new();
63 if (ret
->lock
== NULL
) {
64 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
69 ret
->meth
= RSA_get_default_method();
70 #ifndef OPENSSL_NO_ENGINE
71 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
73 if (!ENGINE_init(engine
)) {
74 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
79 ret
->engine
= ENGINE_get_default_RSA();
82 ret
->meth
= ENGINE_get_RSA(ret
->engine
);
83 if (ret
->meth
== NULL
) {
84 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
90 ret
->flags
= ret
->meth
->flags
& ~RSA_FLAG_NON_FIPS_ALLOW
;
91 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA
, ret
, &ret
->ex_data
)) {
95 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
96 RSAerr(RSA_F_RSA_NEW_METHOD
, ERR_R_INIT_FAIL
);
107 void RSA_free(RSA
*r
)
114 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
115 REF_PRINT_COUNT("RSA", r
);
118 REF_ASSERT_ISNT(i
< 0);
120 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
122 #ifndef OPENSSL_NO_ENGINE
123 ENGINE_finish(r
->engine
);
126 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA
, r
, &r
->ex_data
);
128 CRYPTO_THREAD_lock_free(r
->lock
);
135 BN_clear_free(r
->dmp1
);
136 BN_clear_free(r
->dmq1
);
137 BN_clear_free(r
->iqmp
);
138 RSA_PSS_PARAMS_free(r
->pss
);
139 sk_RSA_PRIME_INFO_pop_free(r
->prime_infos
, rsa_multip_info_free
);
140 BN_BLINDING_free(r
->blinding
);
141 BN_BLINDING_free(r
->mt_blinding
);
142 OPENSSL_free(r
->bignum_data
);
146 int RSA_up_ref(RSA
*r
)
150 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
153 REF_PRINT_COUNT("RSA", r
);
154 REF_ASSERT_ISNT(i
< 2);
155 return i
> 1 ? 1 : 0;
158 int RSA_set_ex_data(RSA
*r
, int idx
, void *arg
)
160 return CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
);
163 void *RSA_get_ex_data(const RSA
*r
, int idx
)
165 return CRYPTO_get_ex_data(&r
->ex_data
, idx
);
169 * Define a scaling constant for our fixed point arithmetic.
170 * This value must be a power of two because the base two logarithm code
171 * makes this assumption. The exponent must also be a multiple of three so
172 * that the scale factor has an exact cube root. Finally, the scale factor
173 * should not be so large that a multiplication of two scaled numbers
174 * overflows a 64 bit unsigned integer.
176 static const unsigned int scale
= 1 << 18;
177 static const unsigned int cbrt_scale
= 1 << (2 * 18 / 3);
179 /* Define some constants, none exceed 32 bits */
180 static const unsigned int log_2
= 0x02c5c8; /* scale * log(2) */
181 static const unsigned int log_e
= 0x05c551; /* scale * log2(M_E) */
182 static const unsigned int c1_923
= 0x07b126; /* scale * 1.923 */
183 static const unsigned int c4_690
= 0x12c28f; /* scale * 4.690 */
186 * Multiply two scaled integers together and rescale the result.
188 static ossl_inline
uint64_t mul2(uint64_t a
, uint64_t b
)
190 return a
* b
/ scale
;
194 * Calculate the cube root of a 64 bit scaled integer.
195 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
196 * integer, this is not guaranteed after scaling, so this function has a
197 * 64 bit return. This uses the shifting nth root algorithm with some
198 * algebraic simplifications.
200 static uint64_t icbrt64(uint64_t x
)
206 for (s
= 63; s
>= 0; s
-= 3) {
208 b
= 3 * r
* (r
+ 1) + 1;
214 return r
* cbrt_scale
;
218 * Calculate the natural logarithm of a 64 bit scaled integer.
219 * This is done by calculating a base two logarithm and scaling.
220 * The maximum logarithm (base 2) is 64 and this reduces base e, so
221 * a 32 bit result should not overflow. The argument passed must be
222 * greater than unity so we don't need to handle negative results.
224 static uint32_t ilog_e(uint64_t v
)
229 * Scale down the value into the range 1 .. 2.
231 * If fractional numbers need to be processed, another loop needs
232 * to go here that checks v < scale and if so multiplies it by 2 and
233 * reduces r by scale. This also means making r signed.
235 while (v
>= 2 * scale
) {
239 for (i
= scale
/ 2; i
!= 0; i
/= 2) {
241 if (v
>= 2 * scale
) {
246 r
= (r
* (uint64_t)scale
) / log_e
;
251 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
254 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
255 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
256 * The two cube roots are merged together here.
258 uint16_t rsa_compute_security_bits(int n
)
264 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
278 * The first incorrect result (i.e. not accurate or off by one low) occurs
279 * for n = 699668. The true value here is 1200. Instead of using this n
280 * as the check threshold, the smallest n such that the correct result is
281 * 1200 is used instead.
288 x
= n
* (uint64_t)log_2
;
290 y
= (uint16_t)((mul2(c1_923
, icbrt64(mul2(mul2(x
, lx
), lx
))) - c4_690
)
295 int RSA_security_bits(const RSA
*rsa
)
297 int bits
= BN_num_bits(rsa
->n
);
299 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
) {
300 /* This ought to mean that we have private key at hand. */
301 int ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
);
303 if (ex_primes
<= 0 || (ex_primes
+ 2) > rsa_multip_cap(bits
))
306 return rsa_compute_security_bits(bits
);
309 int RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
311 /* If the fields n and e in r are NULL, the corresponding input
312 * parameters MUST be non-NULL for n and e. d may be
313 * left NULL (in case only the public key is used).
315 if ((r
->n
== NULL
&& n
== NULL
)
316 || (r
->e
== NULL
&& e
== NULL
))
330 BN_set_flags(r
->d
, BN_FLG_CONSTTIME
);
337 int RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
339 /* If the fields p and q in r are NULL, the corresponding input
340 * parameters MUST be non-NULL.
342 if ((r
->p
== NULL
&& p
== NULL
)
343 || (r
->q
== NULL
&& q
== NULL
))
349 BN_set_flags(r
->p
, BN_FLG_CONSTTIME
);
354 BN_set_flags(r
->q
, BN_FLG_CONSTTIME
);
361 int RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
363 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
364 * parameters MUST be non-NULL.
366 if ((r
->dmp1
== NULL
&& dmp1
== NULL
)
367 || (r
->dmq1
== NULL
&& dmq1
== NULL
)
368 || (r
->iqmp
== NULL
&& iqmp
== NULL
))
372 BN_clear_free(r
->dmp1
);
374 BN_set_flags(r
->dmp1
, BN_FLG_CONSTTIME
);
377 BN_clear_free(r
->dmq1
);
379 BN_set_flags(r
->dmq1
, BN_FLG_CONSTTIME
);
382 BN_clear_free(r
->iqmp
);
384 BN_set_flags(r
->iqmp
, BN_FLG_CONSTTIME
);
392 * Is it better to export RSA_PRIME_INFO structure
393 * and related functions to let user pass a triplet?
395 int RSA_set0_multi_prime_params(RSA
*r
, BIGNUM
*primes
[], BIGNUM
*exps
[],
396 BIGNUM
*coeffs
[], int pnum
)
398 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old
= NULL
;
399 RSA_PRIME_INFO
*pinfo
;
402 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
|| pnum
== 0)
405 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
406 if (prime_infos
== NULL
)
409 if (r
->prime_infos
!= NULL
)
410 old
= r
->prime_infos
;
412 for (i
= 0; i
< pnum
; i
++) {
413 pinfo
= rsa_multip_info_new();
416 if (primes
[i
] != NULL
&& exps
[i
] != NULL
&& coeffs
[i
] != NULL
) {
417 BN_clear_free(pinfo
->r
);
418 BN_clear_free(pinfo
->d
);
419 BN_clear_free(pinfo
->t
);
420 pinfo
->r
= primes
[i
];
422 pinfo
->t
= coeffs
[i
];
423 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
424 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
425 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
427 rsa_multip_info_free(pinfo
);
430 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
433 r
->prime_infos
= prime_infos
;
435 if (!rsa_multip_calc_product(r
)) {
436 r
->prime_infos
= old
;
442 * This is hard to deal with, since the old infos could
443 * also be set by this function and r, d, t should not
444 * be freed in that case. So currently, stay consistent
445 * with other *set0* functions: just free it...
447 sk_RSA_PRIME_INFO_pop_free(old
, rsa_multip_info_free
);
450 r
->version
= RSA_ASN1_VERSION_MULTI
;
455 /* r, d, t should not be freed */
456 sk_RSA_PRIME_INFO_pop_free(prime_infos
, rsa_multip_info_free_ex
);
460 void RSA_get0_key(const RSA
*r
,
461 const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
471 void RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
479 int RSA_get_multi_prime_extra_count(const RSA
*r
)
483 pnum
= sk_RSA_PRIME_INFO_num(r
->prime_infos
);
489 int RSA_get0_multi_prime_factors(const RSA
*r
, const BIGNUM
*primes
[])
492 RSA_PRIME_INFO
*pinfo
;
494 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
498 * return other primes
499 * it's caller's responsibility to allocate oth_primes[pnum]
501 for (i
= 0; i
< pnum
; i
++) {
502 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
503 primes
[i
] = pinfo
->r
;
509 void RSA_get0_crt_params(const RSA
*r
,
510 const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
521 int RSA_get0_multi_prime_crt_params(const RSA
*r
, const BIGNUM
*exps
[],
522 const BIGNUM
*coeffs
[])
526 if ((pnum
= RSA_get_multi_prime_extra_count(r
)) == 0)
529 /* return other primes */
530 if (exps
!= NULL
|| coeffs
!= NULL
) {
531 RSA_PRIME_INFO
*pinfo
;
534 /* it's the user's job to guarantee the buffer length */
535 for (i
= 0; i
< pnum
; i
++) {
536 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
540 coeffs
[i
] = pinfo
->t
;
547 const BIGNUM
*RSA_get0_n(const RSA
*r
)
552 const BIGNUM
*RSA_get0_e(const RSA
*r
)
557 const BIGNUM
*RSA_get0_d(const RSA
*r
)
562 const BIGNUM
*RSA_get0_p(const RSA
*r
)
567 const BIGNUM
*RSA_get0_q(const RSA
*r
)
572 const BIGNUM
*RSA_get0_dmp1(const RSA
*r
)
577 const BIGNUM
*RSA_get0_dmq1(const RSA
*r
)
582 const BIGNUM
*RSA_get0_iqmp(const RSA
*r
)
587 const RSA_PSS_PARAMS
*RSA_get0_pss_params(const RSA
*r
)
592 void RSA_clear_flags(RSA
*r
, int flags
)
597 int RSA_test_flags(const RSA
*r
, int flags
)
599 return r
->flags
& flags
;
602 void RSA_set_flags(RSA
*r
, int flags
)
607 int RSA_get_version(RSA
*r
)
609 /* { two-prime(0), multi(1) } */
613 ENGINE
*RSA_get0_engine(const RSA
*r
)
618 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, int p1
, void *p2
)
620 /* If key type not RSA or RSA-PSS return error */
621 if (ctx
!= NULL
&& ctx
->pmeth
!= NULL
622 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
623 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
625 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, p1
, p2
);
628 DEFINE_STACK_OF(BIGNUM
)
630 int rsa_set0_all_params(RSA
*r
, const STACK_OF(BIGNUM
) *primes
,
631 const STACK_OF(BIGNUM
) *exps
,
632 const STACK_OF(BIGNUM
) *coeffs
)
634 STACK_OF(RSA_PRIME_INFO
) *prime_infos
, *old_infos
= NULL
;
637 if (primes
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
640 pnum
= sk_BIGNUM_num(primes
);
642 || pnum
!= sk_BIGNUM_num(exps
)
643 || pnum
!= sk_BIGNUM_num(coeffs
) + 1)
646 if (!RSA_set0_factors(r
, sk_BIGNUM_value(primes
, 0),
647 sk_BIGNUM_value(primes
, 1))
648 || !RSA_set0_crt_params(r
, sk_BIGNUM_value(exps
, 0),
649 sk_BIGNUM_value(exps
, 1),
650 sk_BIGNUM_value(coeffs
, 0)))
653 old_infos
= r
->prime_infos
;
658 prime_infos
= sk_RSA_PRIME_INFO_new_reserve(NULL
, pnum
);
659 if (prime_infos
== NULL
)
662 for (i
= 2; i
< pnum
; i
++) {
663 BIGNUM
*prime
= sk_BIGNUM_value(primes
, i
);
664 BIGNUM
*exp
= sk_BIGNUM_value(exps
, i
);
665 BIGNUM
*coeff
= sk_BIGNUM_value(coeffs
, i
- 1);
666 RSA_PRIME_INFO
*pinfo
= NULL
;
668 if (!ossl_assert(prime
!= NULL
&& exp
!= NULL
&& coeff
!= NULL
))
671 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
672 if ((pinfo
= OPENSSL_zalloc(sizeof(*pinfo
))) == NULL
) {
673 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
680 BN_set_flags(pinfo
->r
, BN_FLG_CONSTTIME
);
681 BN_set_flags(pinfo
->d
, BN_FLG_CONSTTIME
);
682 BN_set_flags(pinfo
->t
, BN_FLG_CONSTTIME
);
683 (void)sk_RSA_PRIME_INFO_push(prime_infos
, pinfo
);
686 r
->prime_infos
= prime_infos
;
688 if (!rsa_multip_calc_product(r
)) {
689 r
->prime_infos
= old_infos
;
694 if (old_infos
!= NULL
) {
696 * This is hard to deal with, since the old infos could
697 * also be set by this function and r, d, t should not
698 * be freed in that case. So currently, stay consistent
699 * with other *set0* functions: just free it...
701 sk_RSA_PRIME_INFO_pop_free(old_infos
, rsa_multip_info_free
);
704 r
->version
= pnum
> 2 ? RSA_ASN1_VERSION_MULTI
: RSA_ASN1_VERSION_DEFAULT
;
709 /* r, d, t should not be freed */
710 sk_RSA_PRIME_INFO_pop_free(prime_infos
, rsa_multip_info_free_ex
);
714 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
716 int rsa_get0_all_params(RSA
*r
, STACK_OF(BIGNUM_const
) *primes
,
717 STACK_OF(BIGNUM_const
) *exps
,
718 STACK_OF(BIGNUM_const
) *coeffs
)
720 RSA_PRIME_INFO
*pinfo
;
726 pnum
= RSA_get_multi_prime_extra_count(r
);
728 sk_BIGNUM_const_push(primes
, RSA_get0_p(r
));
729 sk_BIGNUM_const_push(primes
, RSA_get0_q(r
));
730 sk_BIGNUM_const_push(exps
, RSA_get0_dmp1(r
));
731 sk_BIGNUM_const_push(exps
, RSA_get0_dmq1(r
));
732 sk_BIGNUM_const_push(coeffs
, RSA_get0_iqmp(r
));
733 for (i
= 0; i
< pnum
; i
++) {
734 pinfo
= sk_RSA_PRIME_INFO_value(r
->prime_infos
, i
);
735 sk_BIGNUM_const_push(primes
, pinfo
->r
);
736 sk_BIGNUM_const_push(exps
, pinfo
->d
);
737 sk_BIGNUM_const_push(coeffs
, pinfo
->t
);
743 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX
*ctx
, int pad_mode
)
745 OSSL_PARAM pad_params
[2], *p
= pad_params
;
748 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
749 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
753 /* If key type not RSA or RSA-PSS return error */
754 if (ctx
->pmeth
!= NULL
755 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
756 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
759 /* TODO(3.0): Remove this eventually when no more legacy */
760 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
761 || ctx
->op
.ciph
.ciphprovctx
== NULL
)
762 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_RSA_PADDING
,
765 *p
++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, &pad_mode
);
766 *p
++ = OSSL_PARAM_construct_end();
768 return EVP_PKEY_CTX_set_params(ctx
, pad_params
);
771 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX
*ctx
, int *pad_mode
)
773 OSSL_PARAM pad_params
[2], *p
= pad_params
;
775 if (ctx
== NULL
|| pad_mode
== NULL
) {
776 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
777 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
781 /* If key type not RSA or RSA-PSS return error */
782 if (ctx
->pmeth
!= NULL
783 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
784 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
787 /* TODO(3.0): Remove this eventually when no more legacy */
788 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
789 || ctx
->op
.ciph
.ciphprovctx
== NULL
)
790 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING
, 0,
793 *p
++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, pad_mode
);
794 *p
++ = OSSL_PARAM_construct_end();
796 if (!EVP_PKEY_CTX_get_params(ctx
, pad_params
))
803 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
807 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
808 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
809 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
813 /* If key type not RSA return error */
814 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
817 /* TODO(3.0): Remove this eventually when no more legacy */
818 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
819 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
820 EVP_PKEY_CTRL_RSA_OAEP_MD
, 0, (void *)md
);
822 name
= (md
== NULL
) ? "" : EVP_MD_name(md
);
824 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx
, name
, NULL
);
827 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
830 OSSL_PARAM rsa_params
[3], *p
= rsa_params
;
832 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
833 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
834 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
838 /* If key type not RSA return error */
839 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
843 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
845 * Cast away the const. This is read
846 * only so should be safe
850 if (mdprops
!= NULL
) {
851 *p
++ = OSSL_PARAM_construct_utf8_string(
852 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
,
854 * Cast away the const. This is read
855 * only so should be safe
858 strlen(mdprops
) + 1);
860 *p
++ = OSSL_PARAM_construct_end();
862 return EVP_PKEY_CTX_set_params(ctx
, rsa_params
);
865 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
868 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
870 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
871 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
872 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
876 /* If key type not RSA return error */
877 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
880 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
,
882 *p
++ = OSSL_PARAM_construct_end();
884 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
890 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
892 /* 80 should be big enough */
895 if (ctx
== NULL
|| md
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
896 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
897 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
901 /* If key type not RSA return error */
902 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
905 /* TODO(3.0): Remove this eventually when no more legacy */
906 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
907 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
908 EVP_PKEY_CTRL_GET_RSA_OAEP_MD
, 0, (void *)md
);
910 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx
, name
, sizeof(name
)) <= 0)
913 /* May be NULL meaning "unknown" */
914 *md
= EVP_get_digestbyname(name
);
919 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
924 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
925 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
926 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
927 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
931 /* If key type not RSA return error */
932 if (ctx
->pmeth
!= NULL
933 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
934 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
937 /* TODO(3.0): Remove this eventually when no more legacy */
938 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
939 && ctx
->op
.ciph
.ciphprovctx
== NULL
)
940 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
941 && ctx
->op
.sig
.sigprovctx
== NULL
))
942 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
,
943 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
944 EVP_PKEY_CTRL_RSA_MGF1_MD
, 0, (void *)md
);
946 name
= (md
== NULL
) ? "" : EVP_MD_name(md
);
948 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx
, name
, NULL
);
951 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, const char *mdname
,
954 OSSL_PARAM rsa_params
[3], *p
= rsa_params
;
958 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
959 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
960 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
961 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
965 /* If key type not RSA return error */
966 if (ctx
->pmeth
!= NULL
967 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
968 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
971 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
,
973 * Cast away the const. This is read
974 * only so should be safe
978 if (mdprops
!= NULL
) {
979 *p
++ = OSSL_PARAM_construct_utf8_string(
980 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS
,
982 * Cast away the const. This is read
983 * only so should be safe
986 strlen(mdprops
) + 1);
988 *p
++ = OSSL_PARAM_construct_end();
990 return EVP_PKEY_CTX_set_params(ctx
, rsa_params
);
993 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX
*ctx
, char *name
,
996 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
999 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1000 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
1001 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1002 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1006 /* If key type not RSA or RSA-PSS return error */
1007 if (ctx
->pmeth
!= NULL
1008 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1009 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1012 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
,
1014 *p
++ = OSSL_PARAM_construct_end();
1016 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1022 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
1024 /* 80 should be big enough */
1028 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1029 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
))) {
1030 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1031 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1035 /* If key type not RSA or RSA-PSS return error */
1036 if (ctx
->pmeth
!= NULL
1037 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
1038 && ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA_PSS
)
1041 /* TODO(3.0): Remove this eventually when no more legacy */
1042 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
1043 && ctx
->op
.ciph
.ciphprovctx
== NULL
)
1044 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
1045 && ctx
->op
.sig
.sigprovctx
== NULL
))
1046 return EVP_PKEY_CTX_ctrl(ctx
, -1,
1047 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1048 EVP_PKEY_CTRL_GET_RSA_MGF1_MD
, 0, (void *)md
);
1050 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx
, name
, sizeof(name
)) <= 0)
1053 /* May be NULL meaning "unknown" */
1054 *md
= EVP_get_digestbyname(name
);
1059 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, void *label
, int llen
)
1061 OSSL_PARAM rsa_params
[2], *p
= rsa_params
;
1063 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1064 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1065 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1069 /* If key type not RSA return error */
1070 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
1073 /* TODO(3.0): Remove this eventually when no more legacy */
1074 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
1075 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1076 EVP_PKEY_CTRL_RSA_OAEP_LABEL
, llen
,
1079 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1081 * Cast away the const. This is read
1082 * only so should be safe
1086 *p
++ = OSSL_PARAM_construct_end();
1088 if (!EVP_PKEY_CTX_set_params(ctx
, rsa_params
))
1091 OPENSSL_free(label
);
1095 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX
*ctx
, unsigned char **label
)
1097 OSSL_PARAM rsa_params
[3], *p
= rsa_params
;
1100 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
1101 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1102 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1106 /* If key type not RSA return error */
1107 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_RSA
)
1110 /* TODO(3.0): Remove this eventually when no more legacy */
1111 if (ctx
->op
.ciph
.ciphprovctx
== NULL
)
1112 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_RSA
, EVP_PKEY_OP_TYPE_CRYPT
,
1113 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
, 0,
1116 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
,
1118 *p
++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
,
1120 *p
++ = OSSL_PARAM_construct_end();
1122 if (!EVP_PKEY_CTX_get_params(ctx
, rsa_params
))
1125 if (labellen
> INT_MAX
)
1128 return (int)labellen
;