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;
92 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
97 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
98 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
109 void RSA_free(RSA *r)
116 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
117 REF_PRINT_COUNT("RSA", r);
120 REF_ASSERT_ISNT(i < 0);
122 if (r->meth != NULL && r->meth->finish != NULL)
124 #ifndef OPENSSL_NO_ENGINE
125 ENGINE_finish(r->engine);
129 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
132 CRYPTO_THREAD_lock_free(r->lock);
139 BN_clear_free(r->dmp1);
140 BN_clear_free(r->dmq1);
141 BN_clear_free(r->iqmp);
142 RSA_PSS_PARAMS_free(r->pss);
143 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
144 BN_BLINDING_free(r->blinding);
145 BN_BLINDING_free(r->mt_blinding);
146 OPENSSL_free(r->bignum_data);
150 int RSA_up_ref(RSA *r)
154 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
157 REF_PRINT_COUNT("RSA", r);
158 REF_ASSERT_ISNT(i < 2);
159 return i > 1 ? 1 : 0;
163 int RSA_set_ex_data(RSA *r, int idx, void *arg)
165 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
168 void *RSA_get_ex_data(const RSA *r, int idx)
170 return CRYPTO_get_ex_data(&r->ex_data, idx);
175 * Define a scaling constant for our fixed point arithmetic.
176 * This value must be a power of two because the base two logarithm code
177 * makes this assumption. The exponent must also be a multiple of three so
178 * that the scale factor has an exact cube root. Finally, the scale factor
179 * should not be so large that a multiplication of two scaled numbers
180 * overflows a 64 bit unsigned integer.
182 static const unsigned int scale = 1 << 18;
183 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
185 /* Define some constants, none exceed 32 bits */
186 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
187 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
188 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
189 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
192 * Multiply two scaled integers together and rescale the result.
194 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
196 return a * b / scale;
200 * Calculate the cube root of a 64 bit scaled integer.
201 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
202 * integer, this is not guaranteed after scaling, so this function has a
203 * 64 bit return. This uses the shifting nth root algorithm with some
204 * algebraic simplifications.
206 static uint64_t icbrt64(uint64_t x)
212 for (s = 63; s >= 0; s -= 3) {
214 b = 3 * r * (r + 1) + 1;
220 return r * cbrt_scale;
224 * Calculate the natural logarithm of a 64 bit scaled integer.
225 * This is done by calculating a base two logarithm and scaling.
226 * The maximum logarithm (base 2) is 64 and this reduces base e, so
227 * a 32 bit result should not overflow. The argument passed must be
228 * greater than unity so we don't need to handle negative results.
230 static uint32_t ilog_e(uint64_t v)
235 * Scale down the value into the range 1 .. 2.
237 * If fractional numbers need to be processed, another loop needs
238 * to go here that checks v < scale and if so multiplies it by 2 and
239 * reduces r by scale. This also means making r signed.
241 while (v >= 2 * scale) {
245 for (i = scale / 2; i != 0; i /= 2) {
247 if (v >= 2 * scale) {
252 r = (r * (uint64_t)scale) / log_e;
257 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
260 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
261 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
262 * The two cube roots are merged together here.
264 uint16_t rsa_compute_security_bits(int n)
270 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
284 * The first incorrect result (i.e. not accurate or off by one low) occurs
285 * for n = 699668. The true value here is 1200. Instead of using this n
286 * as the check threshold, the smallest n such that the correct result is
287 * 1200 is used instead.
294 x = n * (uint64_t)log_2;
296 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
301 int RSA_security_bits(const RSA *rsa)
303 int bits = BN_num_bits(rsa->n);
305 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
306 /* This ought to mean that we have private key at hand. */
307 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
309 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
312 return rsa_compute_security_bits(bits);
315 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
317 /* If the fields n and e in r are NULL, the corresponding input
318 * parameters MUST be non-NULL for n and e. d may be
319 * left NULL (in case only the public key is used).
321 if ((r->n == NULL && n == NULL)
322 || (r->e == NULL && e == NULL))
336 BN_set_flags(r->d, BN_FLG_CONSTTIME);
343 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
345 /* If the fields p and q in r are NULL, the corresponding input
346 * parameters MUST be non-NULL.
348 if ((r->p == NULL && p == NULL)
349 || (r->q == NULL && q == NULL))
355 BN_set_flags(r->p, BN_FLG_CONSTTIME);
360 BN_set_flags(r->q, BN_FLG_CONSTTIME);
367 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
369 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
370 * parameters MUST be non-NULL.
372 if ((r->dmp1 == NULL && dmp1 == NULL)
373 || (r->dmq1 == NULL && dmq1 == NULL)
374 || (r->iqmp == NULL && iqmp == NULL))
378 BN_clear_free(r->dmp1);
380 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
383 BN_clear_free(r->dmq1);
385 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
388 BN_clear_free(r->iqmp);
390 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
398 * Is it better to export RSA_PRIME_INFO structure
399 * and related functions to let user pass a triplet?
401 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
402 BIGNUM *coeffs[], int pnum)
404 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
405 RSA_PRIME_INFO *pinfo;
408 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
411 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
412 if (prime_infos == NULL)
415 if (r->prime_infos != NULL)
416 old = r->prime_infos;
418 for (i = 0; i < pnum; i++) {
419 pinfo = rsa_multip_info_new();
422 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
423 BN_clear_free(pinfo->r);
424 BN_clear_free(pinfo->d);
425 BN_clear_free(pinfo->t);
426 pinfo->r = primes[i];
428 pinfo->t = coeffs[i];
429 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
430 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
431 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
433 rsa_multip_info_free(pinfo);
436 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
439 r->prime_infos = prime_infos;
441 if (!rsa_multip_calc_product(r)) {
442 r->prime_infos = old;
448 * This is hard to deal with, since the old infos could
449 * also be set by this function and r, d, t should not
450 * be freed in that case. So currently, stay consistent
451 * with other *set0* functions: just free it...
453 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
456 r->version = RSA_ASN1_VERSION_MULTI;
461 /* r, d, t should not be freed */
462 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
466 void RSA_get0_key(const RSA *r,
467 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
477 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
485 int RSA_get_multi_prime_extra_count(const RSA *r)
489 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
495 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
498 RSA_PRIME_INFO *pinfo;
500 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
504 * return other primes
505 * it's caller's responsibility to allocate oth_primes[pnum]
507 for (i = 0; i < pnum; i++) {
508 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
509 primes[i] = pinfo->r;
515 void RSA_get0_crt_params(const RSA *r,
516 const BIGNUM **dmp1, const BIGNUM **dmq1,
527 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
528 const BIGNUM *coeffs[])
532 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
535 /* return other primes */
536 if (exps != NULL || coeffs != NULL) {
537 RSA_PRIME_INFO *pinfo;
540 /* it's the user's job to guarantee the buffer length */
541 for (i = 0; i < pnum; i++) {
542 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
546 coeffs[i] = pinfo->t;
553 const BIGNUM *RSA_get0_n(const RSA *r)
558 const BIGNUM *RSA_get0_e(const RSA *r)
563 const BIGNUM *RSA_get0_d(const RSA *r)
568 const BIGNUM *RSA_get0_p(const RSA *r)
573 const BIGNUM *RSA_get0_q(const RSA *r)
578 const BIGNUM *RSA_get0_dmp1(const RSA *r)
583 const BIGNUM *RSA_get0_dmq1(const RSA *r)
588 const BIGNUM *RSA_get0_iqmp(const RSA *r)
593 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
598 void RSA_clear_flags(RSA *r, int flags)
603 int RSA_test_flags(const RSA *r, int flags)
605 return r->flags & flags;
608 void RSA_set_flags(RSA *r, int flags)
613 int RSA_get_version(RSA *r)
615 /* { two-prime(0), multi(1) } */
619 ENGINE *RSA_get0_engine(const RSA *r)
624 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
626 /* If key type not RSA or RSA-PSS return error */
627 if (ctx != NULL && ctx->pmeth != NULL
628 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
629 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
631 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
634 DEFINE_STACK_OF(BIGNUM)
636 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
637 const STACK_OF(BIGNUM) *exps,
638 const STACK_OF(BIGNUM) *coeffs)
640 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
643 if (primes == NULL || exps == NULL || coeffs == NULL)
646 pnum = sk_BIGNUM_num(primes);
648 || pnum != sk_BIGNUM_num(exps)
649 || pnum != sk_BIGNUM_num(coeffs) + 1)
652 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
653 sk_BIGNUM_value(primes, 1))
654 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
655 sk_BIGNUM_value(exps, 1),
656 sk_BIGNUM_value(coeffs, 0)))
659 old_infos = r->prime_infos;
664 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
665 if (prime_infos == NULL)
668 for (i = 2; i < pnum; i++) {
669 BIGNUM *prime = sk_BIGNUM_value(primes, i);
670 BIGNUM *exp = sk_BIGNUM_value(exps, i);
671 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
672 RSA_PRIME_INFO *pinfo = NULL;
674 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
677 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
678 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
679 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
686 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
687 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
688 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
689 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
692 r->prime_infos = prime_infos;
694 if (!rsa_multip_calc_product(r)) {
695 r->prime_infos = old_infos;
700 if (old_infos != NULL) {
702 * This is hard to deal with, since the old infos could
703 * also be set by this function and r, d, t should not
704 * be freed in that case. So currently, stay consistent
705 * with other *set0* functions: just free it...
707 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
710 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
715 /* r, d, t should not be freed */
716 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
720 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
722 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
723 STACK_OF(BIGNUM_const) *exps,
724 STACK_OF(BIGNUM_const) *coeffs)
726 RSA_PRIME_INFO *pinfo;
732 pnum = RSA_get_multi_prime_extra_count(r);
734 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
735 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
736 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
737 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
738 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
739 for (i = 0; i < pnum; i++) {
740 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
741 sk_BIGNUM_const_push(primes, pinfo->r);
742 sk_BIGNUM_const_push(exps, pinfo->d);
743 sk_BIGNUM_const_push(coeffs, pinfo->t);
749 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
751 OSSL_PARAM pad_params[2], *p = pad_params;
754 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
755 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
759 /* If key type not RSA or RSA-PSS return error */
760 if (ctx->pmeth != NULL
761 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
762 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
765 /* TODO(3.0): Remove this eventually when no more legacy */
766 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
767 || ctx->op.ciph.ciphprovctx == NULL)
768 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
771 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode);
772 *p++ = OSSL_PARAM_construct_end();
774 return EVP_PKEY_CTX_set_params(ctx, pad_params);
777 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
779 OSSL_PARAM pad_params[2], *p = pad_params;
781 if (ctx == NULL || pad_mode == NULL) {
782 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
783 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
787 /* If key type not RSA or RSA-PSS return error */
788 if (ctx->pmeth != NULL
789 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
790 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
793 /* TODO(3.0): Remove this eventually when no more legacy */
794 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
795 || ctx->op.ciph.ciphprovctx == NULL)
796 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
799 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode);
800 *p++ = OSSL_PARAM_construct_end();
802 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
809 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
813 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
814 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
815 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
819 /* If key type not RSA return error */
820 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
823 /* TODO(3.0): Remove this eventually when no more legacy */
824 if (ctx->op.ciph.ciphprovctx == NULL)
825 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
826 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
828 name = (md == NULL) ? "" : EVP_MD_name(md);
830 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
833 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
836 OSSL_PARAM rsa_params[3], *p = rsa_params;
838 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
839 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
840 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
844 /* If key type not RSA return error */
845 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
849 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
851 * Cast away the const. This is read
852 * only so should be safe
856 if (mdprops != NULL) {
857 *p++ = OSSL_PARAM_construct_utf8_string(
858 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
860 * Cast away the const. This is read
861 * only so should be safe
864 strlen(mdprops) + 1);
866 *p++ = OSSL_PARAM_construct_end();
868 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
871 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
874 OSSL_PARAM rsa_params[2], *p = rsa_params;
876 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
877 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
878 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
882 /* If key type not RSA return error */
883 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
886 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
888 *p++ = OSSL_PARAM_construct_end();
890 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
896 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
898 /* 80 should be big enough */
901 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
902 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
903 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
907 /* If key type not RSA return error */
908 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
911 /* TODO(3.0): Remove this eventually when no more legacy */
912 if (ctx->op.ciph.ciphprovctx == NULL)
913 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
914 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
916 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
919 /* May be NULL meaning "unknown" */
920 *md = EVP_get_digestbyname(name);
925 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
930 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
931 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
932 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
933 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
937 /* If key type not RSA return error */
938 if (ctx->pmeth != NULL
939 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
940 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
943 /* TODO(3.0): Remove this eventually when no more legacy */
944 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
945 && ctx->op.ciph.ciphprovctx == NULL)
946 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
947 && ctx->op.sig.sigprovctx == NULL))
948 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
949 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
950 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
952 name = (md == NULL) ? "" : EVP_MD_name(md);
954 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
957 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
960 OSSL_PARAM rsa_params[3], *p = rsa_params;
964 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
965 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
966 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
967 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
971 /* If key type not RSA return error */
972 if (ctx->pmeth != NULL
973 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
974 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
977 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
979 * Cast away the const. This is read
980 * only so should be safe
984 if (mdprops != NULL) {
985 *p++ = OSSL_PARAM_construct_utf8_string(
986 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
988 * Cast away the const. This is read
989 * only so should be safe
992 strlen(mdprops) + 1);
994 *p++ = OSSL_PARAM_construct_end();
996 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
999 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1002 OSSL_PARAM rsa_params[2], *p = rsa_params;
1005 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1006 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1007 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1008 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1012 /* If key type not RSA or RSA-PSS return error */
1013 if (ctx->pmeth != NULL
1014 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1015 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1018 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
1020 *p++ = OSSL_PARAM_construct_end();
1022 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1028 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1030 /* 80 should be big enough */
1034 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1035 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1036 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1037 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1041 /* If key type not RSA or RSA-PSS return error */
1042 if (ctx->pmeth != NULL
1043 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1044 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1047 /* TODO(3.0): Remove this eventually when no more legacy */
1048 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1049 && ctx->op.ciph.ciphprovctx == NULL)
1050 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1051 && ctx->op.sig.sigprovctx == NULL))
1052 return EVP_PKEY_CTX_ctrl(ctx, -1,
1053 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1054 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1056 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1059 /* May be NULL meaning "unknown" */
1060 *md = EVP_get_digestbyname(name);
1065 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1067 OSSL_PARAM rsa_params[2], *p = rsa_params;
1069 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1070 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1071 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1075 /* If key type not RSA return error */
1076 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1079 /* TODO(3.0): Remove this eventually when no more legacy */
1080 if (ctx->op.ciph.ciphprovctx == NULL)
1081 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1082 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1085 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1087 * Cast away the const. This is read
1088 * only so should be safe
1092 *p++ = OSSL_PARAM_construct_end();
1094 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1097 OPENSSL_free(label);
1101 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1103 OSSL_PARAM rsa_params[3], *p = rsa_params;
1106 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1107 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1108 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1112 /* If key type not RSA return error */
1113 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1116 /* TODO(3.0): Remove this eventually when no more legacy */
1117 if (ctx->op.ciph.ciphprovctx == NULL)
1118 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1119 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1122 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1124 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1126 *p++ = OSSL_PARAM_construct_end();
1128 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1131 if (labellen > INT_MAX)
1134 return (int)labellen;