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 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
784 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
785 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
786 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
787 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
790 pnum = RSA_get_multi_prime_extra_count(r);
791 for (i = 0; i < pnum; i++) {
792 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
793 sk_BIGNUM_const_push(primes, pinfo->r);
794 sk_BIGNUM_const_push(exps, pinfo->d);
795 sk_BIGNUM_const_push(coeffs, pinfo->t);
803 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
805 OSSL_PARAM pad_params[2], *p = pad_params;
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 or RSA-PSS return error */
814 if (ctx->pmeth != NULL
815 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
816 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
819 /* TODO(3.0): Remove this eventually when no more legacy */
820 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
821 || ctx->op.ciph.ciphprovctx == NULL)
822 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
825 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode);
826 *p++ = OSSL_PARAM_construct_end();
828 return EVP_PKEY_CTX_set_params(ctx, pad_params);
831 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
833 OSSL_PARAM pad_params[2], *p = pad_params;
835 if (ctx == NULL || pad_mode == NULL) {
836 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
837 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
841 /* If key type not RSA or RSA-PSS return error */
842 if (ctx->pmeth != NULL
843 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
844 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
847 /* TODO(3.0): Remove this eventually when no more legacy */
848 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
849 || ctx->op.ciph.ciphprovctx == NULL)
850 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
853 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode);
854 *p++ = OSSL_PARAM_construct_end();
856 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
863 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
867 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
868 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
869 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
873 /* If key type not RSA return error */
874 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
877 /* TODO(3.0): Remove this eventually when no more legacy */
878 if (ctx->op.ciph.ciphprovctx == NULL)
879 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
880 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
882 name = (md == NULL) ? "" : EVP_MD_name(md);
884 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
887 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
890 OSSL_PARAM rsa_params[3], *p = rsa_params;
892 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
893 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
894 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
898 /* If key type not RSA return error */
899 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
903 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
905 * Cast away the const. This is read
906 * only so should be safe
909 if (mdprops != NULL) {
910 *p++ = OSSL_PARAM_construct_utf8_string(
911 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
913 * Cast away the const. This is read
914 * only so should be safe
918 *p++ = OSSL_PARAM_construct_end();
920 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
923 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
926 OSSL_PARAM rsa_params[2], *p = rsa_params;
928 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
929 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
930 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
934 /* If key type not RSA return error */
935 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
938 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
940 *p++ = OSSL_PARAM_construct_end();
942 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
948 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
950 /* 80 should be big enough */
953 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
954 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
955 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
959 /* If key type not RSA return error */
960 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
963 /* TODO(3.0): Remove this eventually when no more legacy */
964 if (ctx->op.ciph.ciphprovctx == NULL)
965 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
966 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
968 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
971 /* May be NULL meaning "unknown" */
972 *md = EVP_get_digestbyname(name);
977 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
982 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
983 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
984 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
985 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
989 /* If key type not RSA return error */
990 if (ctx->pmeth != NULL
991 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
992 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
995 /* TODO(3.0): Remove this eventually when no more legacy */
996 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
997 && ctx->op.ciph.ciphprovctx == NULL)
998 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
999 && ctx->op.sig.sigprovctx == NULL))
1000 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1001 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1002 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1004 name = (md == NULL) ? "" : EVP_MD_name(md);
1006 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1009 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1010 const char *mdprops)
1012 OSSL_PARAM rsa_params[3], *p = rsa_params;
1016 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1017 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1018 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1019 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1023 /* If key type not RSA return error */
1024 if (ctx->pmeth != NULL
1025 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1026 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1029 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
1031 * Cast away the const. This is read
1032 * only so should be safe
1035 if (mdprops != NULL) {
1036 *p++ = OSSL_PARAM_construct_utf8_string(
1037 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
1039 * Cast away the const. This is read
1040 * only so should be safe
1042 (char *)mdprops, 0);
1044 *p++ = OSSL_PARAM_construct_end();
1046 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1049 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1052 OSSL_PARAM rsa_params[2], *p = rsa_params;
1055 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1056 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1057 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1058 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1062 /* If key type not RSA or RSA-PSS return error */
1063 if (ctx->pmeth != NULL
1064 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1065 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1068 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
1070 *p++ = OSSL_PARAM_construct_end();
1072 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1078 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1080 /* 80 should be big enough */
1084 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1085 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1086 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1087 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1091 /* If key type not RSA or RSA-PSS return error */
1092 if (ctx->pmeth != NULL
1093 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1094 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1097 /* TODO(3.0): Remove this eventually when no more legacy */
1098 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1099 && ctx->op.ciph.ciphprovctx == NULL)
1100 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1101 && ctx->op.sig.sigprovctx == NULL))
1102 return EVP_PKEY_CTX_ctrl(ctx, -1,
1103 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1104 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1106 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1109 /* May be NULL meaning "unknown" */
1110 *md = EVP_get_digestbyname(name);
1115 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1117 OSSL_PARAM rsa_params[2], *p = rsa_params;
1119 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1120 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1121 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1125 /* If key type not RSA return error */
1126 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1129 /* TODO(3.0): Remove this eventually when no more legacy */
1130 if (ctx->op.ciph.ciphprovctx == NULL)
1131 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1132 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1135 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1137 * Cast away the const. This is read
1138 * only so should be safe
1142 *p++ = OSSL_PARAM_construct_end();
1144 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1147 OPENSSL_free(label);
1151 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1153 OSSL_PARAM rsa_params[3], *p = rsa_params;
1156 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1157 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1158 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1162 /* If key type not RSA return error */
1163 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1166 /* TODO(3.0): Remove this eventually when no more legacy */
1167 if (ctx->op.ciph.ciphprovctx == NULL)
1168 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1169 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1172 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1174 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1176 *p++ = OSSL_PARAM_construct_end();
1178 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1181 if (labellen > INT_MAX)
1184 return (int)labellen;