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 void RSA_clear_flags(RSA *r, int flags)
592 int RSA_test_flags(const RSA *r, int flags)
594 return r->flags & flags;
597 void RSA_set_flags(RSA *r, int flags)
602 int RSA_get_version(RSA *r)
604 /* { two-prime(0), multi(1) } */
608 ENGINE *RSA_get0_engine(const RSA *r)
613 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
615 /* If key type not RSA or RSA-PSS return error */
616 if (ctx != NULL && ctx->pmeth != NULL
617 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
618 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
620 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
623 DEFINE_STACK_OF(BIGNUM)
625 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
626 const STACK_OF(BIGNUM) *exps,
627 const STACK_OF(BIGNUM) *coeffs)
629 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
632 if (primes == NULL || exps == NULL || coeffs == NULL)
635 pnum = sk_BIGNUM_num(primes);
637 || pnum != sk_BIGNUM_num(exps)
638 || pnum != sk_BIGNUM_num(coeffs) + 1)
641 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
642 sk_BIGNUM_value(primes, 1))
643 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
644 sk_BIGNUM_value(exps, 1),
645 sk_BIGNUM_value(coeffs, 0)))
648 old_infos = r->prime_infos;
653 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
654 if (prime_infos == NULL)
657 for (i = 2; i < pnum; i++) {
658 BIGNUM *prime = sk_BIGNUM_value(primes, i);
659 BIGNUM *exp = sk_BIGNUM_value(exps, i);
660 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
661 RSA_PRIME_INFO *pinfo = NULL;
663 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
666 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
667 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
668 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
675 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
676 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
677 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
678 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
681 r->prime_infos = prime_infos;
683 if (!rsa_multip_calc_product(r)) {
684 r->prime_infos = old_infos;
689 if (old_infos != NULL) {
691 * This is hard to deal with, since the old infos could
692 * also be set by this function and r, d, t should not
693 * be freed in that case. So currently, stay consistent
694 * with other *set0* functions: just free it...
696 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
699 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
704 /* r, d, t should not be freed */
705 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
709 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
711 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
712 STACK_OF(BIGNUM_const) *exps,
713 STACK_OF(BIGNUM_const) *coeffs)
715 RSA_PRIME_INFO *pinfo;
721 pnum = RSA_get_multi_prime_extra_count(r);
723 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
724 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
725 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
726 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
727 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
728 for (i = 0; i < pnum; i++) {
729 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
730 sk_BIGNUM_const_push(primes, pinfo->r);
731 sk_BIGNUM_const_push(exps, pinfo->d);
732 sk_BIGNUM_const_push(coeffs, pinfo->t);
738 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
740 OSSL_PARAM pad_params[2], *p = pad_params;
743 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
744 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
748 /* If key type not RSA or RSA-PSS return error */
749 if (ctx->pmeth != NULL
750 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
751 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
754 /* TODO(3.0): Remove this eventually when no more legacy */
755 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
756 || ctx->op.ciph.ciphprovctx == NULL)
757 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
760 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode);
761 *p++ = OSSL_PARAM_construct_end();
763 return EVP_PKEY_CTX_set_params(ctx, pad_params);
766 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
768 OSSL_PARAM pad_params[2], *p = pad_params;
770 if (ctx == NULL || pad_mode == NULL) {
771 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
772 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
776 /* If key type not RSA or RSA-PSS return error */
777 if (ctx->pmeth != NULL
778 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
779 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
782 /* TODO(3.0): Remove this eventually when no more legacy */
783 if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
784 || ctx->op.ciph.ciphprovctx == NULL)
785 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
788 *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode);
789 *p++ = OSSL_PARAM_construct_end();
791 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
798 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
802 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
803 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
804 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
808 /* If key type not RSA return error */
809 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
812 /* TODO(3.0): Remove this eventually when no more legacy */
813 if (ctx->op.ciph.ciphprovctx == NULL)
814 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
815 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
817 name = (md == NULL) ? "" : EVP_MD_name(md);
819 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
822 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
825 OSSL_PARAM rsa_params[3], *p = rsa_params;
827 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
828 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
829 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
833 /* If key type not RSA return error */
834 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
838 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
840 * Cast away the const. This is read
841 * only so should be safe
845 if (mdprops != NULL) {
846 *p++ = OSSL_PARAM_construct_utf8_string(
847 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
849 * Cast away the const. This is read
850 * only so should be safe
853 strlen(mdprops) + 1);
855 *p++ = OSSL_PARAM_construct_end();
857 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
860 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
863 OSSL_PARAM rsa_params[2], *p = rsa_params;
865 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
866 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
867 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
871 /* If key type not RSA return error */
872 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
875 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
877 *p++ = OSSL_PARAM_construct_end();
879 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
885 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
887 /* 80 should be big enough */
890 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
891 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
892 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
896 /* If key type not RSA return error */
897 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
900 /* TODO(3.0): Remove this eventually when no more legacy */
901 if (ctx->op.ciph.ciphprovctx == NULL)
902 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
903 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
905 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
908 /* May be NULL meaning "unknown" */
909 *md = EVP_get_digestbyname(name);
914 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
919 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
920 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
921 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
922 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
926 /* If key type not RSA return error */
927 if (ctx->pmeth != NULL
928 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
929 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
932 /* TODO(3.0): Remove this eventually when no more legacy */
933 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
934 && ctx->op.ciph.ciphprovctx == NULL)
935 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
936 && ctx->op.sig.sigprovctx == NULL))
937 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
938 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
939 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
941 name = (md == NULL) ? "" : EVP_MD_name(md);
943 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
946 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
949 OSSL_PARAM rsa_params[3], *p = rsa_params;
953 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
954 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
955 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
956 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
960 /* If key type not RSA return error */
961 if (ctx->pmeth != NULL
962 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
963 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
966 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
968 * Cast away the const. This is read
969 * only so should be safe
973 if (mdprops != NULL) {
974 *p++ = OSSL_PARAM_construct_utf8_string(
975 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
977 * Cast away the const. This is read
978 * only so should be safe
981 strlen(mdprops) + 1);
983 *p++ = OSSL_PARAM_construct_end();
985 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
988 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
991 OSSL_PARAM rsa_params[2], *p = rsa_params;
994 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
995 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
996 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
997 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1001 /* If key type not RSA or RSA-PSS return error */
1002 if (ctx->pmeth != NULL
1003 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1004 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1007 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
1009 *p++ = OSSL_PARAM_construct_end();
1011 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1017 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1019 /* 80 should be big enough */
1023 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1024 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1025 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1026 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1030 /* If key type not RSA or RSA-PSS return error */
1031 if (ctx->pmeth != NULL
1032 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1033 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1036 /* TODO(3.0): Remove this eventually when no more legacy */
1037 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1038 && ctx->op.ciph.ciphprovctx == NULL)
1039 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1040 && ctx->op.sig.sigprovctx == NULL))
1041 return EVP_PKEY_CTX_ctrl(ctx, -1,
1042 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1043 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1045 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1048 /* May be NULL meaning "unknown" */
1049 *md = EVP_get_digestbyname(name);
1054 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1056 OSSL_PARAM rsa_params[2], *p = rsa_params;
1058 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1059 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1060 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1064 /* If key type not RSA return error */
1065 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1068 /* TODO(3.0): Remove this eventually when no more legacy */
1069 if (ctx->op.ciph.ciphprovctx == NULL)
1070 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1071 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1074 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1076 * Cast away the const. This is read
1077 * only so should be safe
1081 *p++ = OSSL_PARAM_construct_end();
1083 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1086 OPENSSL_free(label);
1090 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1092 OSSL_PARAM rsa_params[3], *p = rsa_params;
1095 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1096 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1097 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1101 /* If key type not RSA return error */
1102 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1105 /* TODO(3.0): Remove this eventually when no more legacy */
1106 if (ctx->op.ciph.ciphprovctx == NULL)
1107 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1108 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1111 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1113 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1115 *p++ = OSSL_PARAM_construct_end();
1117 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1120 if (labellen > INT_MAX)
1123 return (int)labellen;