2 * Copyright 1995-2020 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 "openssl/param_build.h"
24 #include "crypto/bn.h"
25 #include "crypto/evp.h"
26 #include "crypto/rsa.h"
27 #include "crypto/security_bits.h"
28 #include "rsa_local.h"
30 static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
35 return rsa_new_intern(NULL, NULL);
38 const RSA_METHOD *RSA_get_method(const RSA *rsa)
43 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
46 * NB: The caller is specifically setting a method, so it's not up to us
47 * to deal with which ENGINE it comes from.
49 const RSA_METHOD *mtmp;
53 #ifndef OPENSSL_NO_ENGINE
54 ENGINE_finish(rsa->engine);
63 RSA *RSA_new_method(ENGINE *engine)
65 return rsa_new_intern(engine, NULL);
69 RSA *ossl_rsa_new_with_ctx(OSSL_LIB_CTX *libctx)
71 return rsa_new_intern(NULL, libctx);
74 static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
76 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
79 RSAerr(0, ERR_R_MALLOC_FAILURE);
84 ret->lock = CRYPTO_THREAD_lock_new();
85 if (ret->lock == NULL) {
86 RSAerr(0, ERR_R_MALLOC_FAILURE);
92 ret->meth = RSA_get_default_method();
93 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
94 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
96 if (!ENGINE_init(engine)) {
97 RSAerr(0, ERR_R_ENGINE_LIB);
100 ret->engine = engine;
102 ret->engine = ENGINE_get_default_RSA();
105 ret->meth = ENGINE_get_RSA(ret->engine);
106 if (ret->meth == NULL) {
107 RSAerr(0, ERR_R_ENGINE_LIB);
113 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
115 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
120 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
121 RSAerr(0, ERR_R_INIT_FAIL);
132 void RSA_free(RSA *r)
139 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
140 REF_PRINT_COUNT("RSA", r);
143 REF_ASSERT_ISNT(i < 0);
145 if (r->meth != NULL && r->meth->finish != NULL)
147 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148 ENGINE_finish(r->engine);
152 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
155 CRYPTO_THREAD_lock_free(r->lock);
162 BN_clear_free(r->dmp1);
163 BN_clear_free(r->dmq1);
164 BN_clear_free(r->iqmp);
166 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
167 rsa_acvp_test_free(r->acvp_test);
171 RSA_PSS_PARAMS_free(r->pss);
172 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
174 BN_BLINDING_free(r->blinding);
175 BN_BLINDING_free(r->mt_blinding);
176 OPENSSL_free(r->bignum_data);
180 int RSA_up_ref(RSA *r)
184 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
187 REF_PRINT_COUNT("RSA", r);
188 REF_ASSERT_ISNT(i < 2);
189 return i > 1 ? 1 : 0;
192 OSSL_LIB_CTX *ossl_rsa_get0_libctx(RSA *r)
198 int RSA_set_ex_data(RSA *r, int idx, void *arg)
200 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
203 void *RSA_get_ex_data(const RSA *r, int idx)
205 return CRYPTO_get_ex_data(&r->ex_data, idx);
210 * Define a scaling constant for our fixed point arithmetic.
211 * This value must be a power of two because the base two logarithm code
212 * makes this assumption. The exponent must also be a multiple of three so
213 * that the scale factor has an exact cube root. Finally, the scale factor
214 * should not be so large that a multiplication of two scaled numbers
215 * overflows a 64 bit unsigned integer.
217 static const unsigned int scale = 1 << 18;
218 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
220 /* Define some constants, none exceed 32 bits */
221 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
222 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
223 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
224 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
227 * Multiply two scaled integers together and rescale the result.
229 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
231 return a * b / scale;
235 * Calculate the cube root of a 64 bit scaled integer.
236 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
237 * integer, this is not guaranteed after scaling, so this function has a
238 * 64 bit return. This uses the shifting nth root algorithm with some
239 * algebraic simplifications.
241 static uint64_t icbrt64(uint64_t x)
247 for (s = 63; s >= 0; s -= 3) {
249 b = 3 * r * (r + 1) + 1;
255 return r * cbrt_scale;
259 * Calculate the natural logarithm of a 64 bit scaled integer.
260 * This is done by calculating a base two logarithm and scaling.
261 * The maximum logarithm (base 2) is 64 and this reduces base e, so
262 * a 32 bit result should not overflow. The argument passed must be
263 * greater than unity so we don't need to handle negative results.
265 static uint32_t ilog_e(uint64_t v)
270 * Scale down the value into the range 1 .. 2.
272 * If fractional numbers need to be processed, another loop needs
273 * to go here that checks v < scale and if so multiplies it by 2 and
274 * reduces r by scale. This also means making r signed.
276 while (v >= 2 * scale) {
280 for (i = scale / 2; i != 0; i /= 2) {
282 if (v >= 2 * scale) {
287 r = (r * (uint64_t)scale) / log_e;
292 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
295 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
296 * for FFC safe prime groups for modp and ffdhe.
297 * After Table 25 and Table 26 it refers to
298 * "The maximum security strength estimates were calculated using the formula in
299 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
304 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
305 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
306 * The two cube roots are merged together here.
308 uint16_t ifc_ffc_compute_security_bits(int n)
314 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
328 * The first incorrect result (i.e. not accurate or off by one low) occurs
329 * for n = 699668. The true value here is 1200. Instead of using this n
330 * as the check threshold, the smallest n such that the correct result is
331 * 1200 is used instead.
338 x = n * (uint64_t)log_2;
340 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
347 int RSA_security_bits(const RSA *rsa)
349 int bits = BN_num_bits(rsa->n);
352 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
353 /* This ought to mean that we have private key at hand. */
354 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
356 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
360 return ifc_ffc_compute_security_bits(bits);
363 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
365 /* If the fields n and e in r are NULL, the corresponding input
366 * parameters MUST be non-NULL for n and e. d may be
367 * left NULL (in case only the public key is used).
369 if ((r->n == NULL && n == NULL)
370 || (r->e == NULL && e == NULL))
384 BN_set_flags(r->d, BN_FLG_CONSTTIME);
391 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
393 /* If the fields p and q in r are NULL, the corresponding input
394 * parameters MUST be non-NULL.
396 if ((r->p == NULL && p == NULL)
397 || (r->q == NULL && q == NULL))
403 BN_set_flags(r->p, BN_FLG_CONSTTIME);
408 BN_set_flags(r->q, BN_FLG_CONSTTIME);
415 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
417 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
418 * parameters MUST be non-NULL.
420 if ((r->dmp1 == NULL && dmp1 == NULL)
421 || (r->dmq1 == NULL && dmq1 == NULL)
422 || (r->iqmp == NULL && iqmp == NULL))
426 BN_clear_free(r->dmp1);
428 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
431 BN_clear_free(r->dmq1);
433 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
436 BN_clear_free(r->iqmp);
438 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
447 * Is it better to export RSA_PRIME_INFO structure
448 * and related functions to let user pass a triplet?
450 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
451 BIGNUM *coeffs[], int pnum)
453 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
454 RSA_PRIME_INFO *pinfo;
457 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
460 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
461 if (prime_infos == NULL)
464 if (r->prime_infos != NULL)
465 old = r->prime_infos;
467 for (i = 0; i < pnum; i++) {
468 pinfo = rsa_multip_info_new();
471 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
472 BN_clear_free(pinfo->r);
473 BN_clear_free(pinfo->d);
474 BN_clear_free(pinfo->t);
475 pinfo->r = primes[i];
477 pinfo->t = coeffs[i];
478 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
479 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
480 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
482 rsa_multip_info_free(pinfo);
485 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
488 r->prime_infos = prime_infos;
490 if (!rsa_multip_calc_product(r)) {
491 r->prime_infos = old;
497 * This is hard to deal with, since the old infos could
498 * also be set by this function and r, d, t should not
499 * be freed in that case. So currently, stay consistent
500 * with other *set0* functions: just free it...
502 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
505 r->version = RSA_ASN1_VERSION_MULTI;
510 /* r, d, t should not be freed */
511 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
516 void RSA_get0_key(const RSA *r,
517 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
527 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
536 int RSA_get_multi_prime_extra_count(const RSA *r)
540 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
546 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
549 RSA_PRIME_INFO *pinfo;
551 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
555 * return other primes
556 * it's caller's responsibility to allocate oth_primes[pnum]
558 for (i = 0; i < pnum; i++) {
559 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
560 primes[i] = pinfo->r;
567 void RSA_get0_crt_params(const RSA *r,
568 const BIGNUM **dmp1, const BIGNUM **dmq1,
580 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
581 const BIGNUM *coeffs[])
585 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
588 /* return other primes */
589 if (exps != NULL || coeffs != NULL) {
590 RSA_PRIME_INFO *pinfo;
593 /* it's the user's job to guarantee the buffer length */
594 for (i = 0; i < pnum; i++) {
595 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
599 coeffs[i] = pinfo->t;
607 const BIGNUM *RSA_get0_n(const RSA *r)
612 const BIGNUM *RSA_get0_e(const RSA *r)
617 const BIGNUM *RSA_get0_d(const RSA *r)
622 const BIGNUM *RSA_get0_p(const RSA *r)
627 const BIGNUM *RSA_get0_q(const RSA *r)
632 const BIGNUM *RSA_get0_dmp1(const RSA *r)
637 const BIGNUM *RSA_get0_dmq1(const RSA *r)
642 const BIGNUM *RSA_get0_iqmp(const RSA *r)
647 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
657 RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r)
659 return &r->pss_params;
662 void RSA_clear_flags(RSA *r, int flags)
667 int RSA_test_flags(const RSA *r, int flags)
669 return r->flags & flags;
672 void RSA_set_flags(RSA *r, int flags)
677 int RSA_get_version(RSA *r)
679 /* { two-prime(0), multi(1) } */
684 ENGINE *RSA_get0_engine(const RSA *r)
689 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
691 /* If key type not RSA or RSA-PSS return error */
692 if (ctx != NULL && ctx->pmeth != NULL
693 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
694 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
696 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
700 DEFINE_STACK_OF(BIGNUM)
702 int ossl_rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
703 const STACK_OF(BIGNUM) *exps,
704 const STACK_OF(BIGNUM) *coeffs)
707 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
711 if (primes == NULL || exps == NULL || coeffs == NULL)
714 pnum = sk_BIGNUM_num(primes);
716 || pnum != sk_BIGNUM_num(exps)
717 || pnum != sk_BIGNUM_num(coeffs) + 1)
720 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
721 sk_BIGNUM_value(primes, 1))
722 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
723 sk_BIGNUM_value(exps, 1),
724 sk_BIGNUM_value(coeffs, 0)))
728 old_infos = r->prime_infos;
735 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
736 if (prime_infos == NULL)
739 for (i = 2; i < pnum; i++) {
740 BIGNUM *prime = sk_BIGNUM_value(primes, i);
741 BIGNUM *exp = sk_BIGNUM_value(exps, i);
742 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
743 RSA_PRIME_INFO *pinfo = NULL;
745 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
748 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
749 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
750 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
757 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
758 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
759 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
760 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
763 r->prime_infos = prime_infos;
765 if (!rsa_multip_calc_product(r)) {
766 r->prime_infos = old_infos;
775 if (old_infos != NULL) {
777 * This is hard to deal with, since the old infos could
778 * also be set by this function and r, d, t should not
779 * be freed in that case. So currently, stay consistent
780 * with other *set0* functions: just free it...
782 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
786 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
792 /* r, d, t should not be freed */
793 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
798 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
800 int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
801 STACK_OF(BIGNUM_const) *exps,
802 STACK_OF(BIGNUM_const) *coeffs)
805 RSA_PRIME_INFO *pinfo;
812 /* If |p| is NULL, there are no CRT parameters */
813 if (RSA_get0_p(r) == NULL)
816 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
817 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
818 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
819 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
820 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
823 pnum = RSA_get_multi_prime_extra_count(r);
824 for (i = 0; i < pnum; i++) {
825 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
826 sk_BIGNUM_const_push(primes, pinfo->r);
827 sk_BIGNUM_const_push(exps, pinfo->d);
828 sk_BIGNUM_const_push(coeffs, pinfo->t);
836 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
838 OSSL_PARAM pad_params[2], *p = pad_params;
841 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
842 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
846 /* If key type not RSA or RSA-PSS return error */
847 if (ctx->pmeth != NULL
848 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
849 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
852 /* TODO(3.0): Remove this eventually when no more legacy */
853 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
854 || ctx->op.ciph.ciphprovctx == NULL)
855 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
856 || ctx->op.sig.sigprovctx == NULL))
857 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
860 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
861 *p++ = OSSL_PARAM_construct_end();
863 return EVP_PKEY_CTX_set_params(ctx, pad_params);
866 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
868 OSSL_PARAM pad_params[2], *p = pad_params;
870 if (ctx == NULL || pad_mode == NULL) {
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 or RSA-PSS return error */
877 if (ctx->pmeth != NULL
878 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
879 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
882 /* TODO(3.0): Remove this eventually when no more legacy */
883 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
884 || ctx->op.ciph.ciphprovctx == NULL)
885 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
886 || ctx->op.sig.sigprovctx == NULL))
887 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
890 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
891 *p++ = OSSL_PARAM_construct_end();
893 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
900 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
904 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
905 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
906 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
910 /* If key type not RSA return error */
911 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
914 /* TODO(3.0): Remove this eventually when no more legacy */
915 if (ctx->op.ciph.ciphprovctx == NULL)
916 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
917 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
919 name = (md == NULL) ? "" : EVP_MD_name(md);
921 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
924 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
927 OSSL_PARAM rsa_params[3], *p = rsa_params;
929 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
930 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
931 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
935 /* If key type not RSA return error */
936 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
940 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
942 * Cast away the const. This is read
943 * only so should be safe
946 if (mdprops != NULL) {
947 *p++ = OSSL_PARAM_construct_utf8_string(
948 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
950 * Cast away the const. This is read
951 * only so should be safe
955 *p++ = OSSL_PARAM_construct_end();
957 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
960 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
963 OSSL_PARAM rsa_params[2], *p = rsa_params;
965 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_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 && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
975 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
977 *p++ = OSSL_PARAM_construct_end();
979 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
985 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
987 /* 80 should be big enough */
990 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
991 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
992 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
996 /* If key type not RSA return error */
997 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1000 /* TODO(3.0): Remove this eventually when no more legacy */
1001 if (ctx->op.ciph.ciphprovctx == NULL)
1002 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1003 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
1005 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
1008 /* May be NULL meaning "unknown" */
1009 *md = evp_get_digestbyname_ex(ctx->libctx, name);
1014 static int int_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
1015 /* For EVP_PKEY_CTX_ctrl() */
1016 int keytype, int optype, int cmd,
1018 /* For EVP_PKEY_CTX_set_params() */
1019 const char *mdname, const char *mdprops)
1021 OSSL_PARAM rsa_params[3], *p = rsa_params;
1023 if (ctx == NULL || (ctx->operation & optype) == 0) {
1024 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1025 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1029 /* If key type not RSA return error */
1030 if (ctx->pmeth != NULL
1032 ? (ctx->pmeth->pkey_id != EVP_PKEY_RSA
1033 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1034 : ctx->pmeth->pkey_id != keytype))
1037 /* TODO(3.0): Remove this eventually when no more legacy */
1039 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1040 && ctx->op.ciph.ciphprovctx == NULL)
1041 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1042 && ctx->op.sig.sigprovctx == NULL)
1043 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1044 && ctx->op.keymgmt.genctx == NULL))
1045 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, (void *)md);
1047 mdname = (md == NULL) ? "" : EVP_MD_name(md);
1051 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1053 * Cast away the const. This is
1054 * read only so should be safe
1057 if (mdprops != NULL) {
1059 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1061 * Cast away the const. This is
1062 * read only so should be safe
1064 (char *)mdprops, 0);
1066 *p++ = OSSL_PARAM_construct_end();
1068 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1071 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1073 return int_set_rsa_mgf1_md(ctx, -1,
1074 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1075 EVP_PKEY_CTRL_RSA_MGF1_MD, md, NULL, NULL);
1078 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1079 const char *mdprops)
1081 return int_set_rsa_mgf1_md(ctx, -1,
1082 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1083 -1, NULL, mdname, mdprops);
1086 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1088 return int_set_rsa_mgf1_md(ctx, EVP_PKEY_RSA_PSS,
1089 EVP_PKEY_OP_KEYGEN, EVP_PKEY_CTRL_RSA_MGF1_MD,
1093 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
1096 return int_set_rsa_mgf1_md(ctx, EVP_PKEY_RSA_PSS,
1097 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1098 -1, NULL, mdname, NULL);
1101 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1104 OSSL_PARAM rsa_params[2], *p = rsa_params;
1107 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1108 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1109 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1110 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1114 /* If key type not RSA or RSA-PSS return error */
1115 if (ctx->pmeth != NULL
1116 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1117 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1120 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1122 *p++ = OSSL_PARAM_construct_end();
1124 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1130 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1132 /* 80 should be big enough */
1136 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1137 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1138 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1139 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1143 /* If key type not RSA or RSA-PSS return error */
1144 if (ctx->pmeth != NULL
1145 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1146 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1149 /* TODO(3.0): Remove this eventually when no more legacy */
1150 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1151 && ctx->op.ciph.ciphprovctx == NULL)
1152 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1153 && ctx->op.sig.sigprovctx == NULL))
1154 return EVP_PKEY_CTX_ctrl(ctx, -1,
1155 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1156 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1158 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1161 /* May be NULL meaning "unknown" */
1162 *md = evp_get_digestbyname_ex(ctx->libctx, name);
1167 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1169 OSSL_PARAM rsa_params[2], *p = rsa_params;
1171 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1172 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1173 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1177 /* If key type not RSA return error */
1178 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1181 /* TODO(3.0): Remove this eventually when no more legacy */
1182 if (ctx->op.ciph.ciphprovctx == NULL)
1183 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1184 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1187 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1189 * Cast away the const. This is
1190 * read only so should be safe
1194 *p++ = OSSL_PARAM_construct_end();
1196 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1199 OPENSSL_free(label);
1203 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1205 OSSL_PARAM rsa_params[3], *p = rsa_params;
1208 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1209 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1210 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1214 /* If key type not RSA return error */
1215 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1218 /* TODO(3.0): Remove this eventually when no more legacy */
1219 if (ctx->op.ciph.ciphprovctx == NULL)
1220 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1221 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1224 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1226 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1228 *p++ = OSSL_PARAM_construct_end();
1230 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1233 if (labellen > INT_MAX)
1236 return (int)labellen;
1239 static int int_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen,
1240 int keytype, int optype)
1242 OSSL_PARAM pad_params[2], *p = pad_params;
1244 if (ctx == NULL || (ctx->operation & optype) == 0) {
1245 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1246 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1250 /* If key type not RSA or RSA-PSS return error */
1251 if (ctx->pmeth != NULL
1253 ? (ctx->pmeth->pkey_id != EVP_PKEY_RSA
1254 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1255 : ctx->pmeth->pkey_id != keytype))
1258 /* TODO(3.0): Remove this eventually when no more legacy */
1259 if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1260 && ctx->op.sig.sigprovctx == NULL)
1261 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
1262 && ctx->op.keymgmt.genctx == NULL))
1263 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype,
1264 EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1268 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1269 *p++ = OSSL_PARAM_construct_end();
1271 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1274 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1276 return int_set_rsa_pss_saltlen(ctx, saltlen, -1, EVP_PKEY_OP_TYPE_SIG);
1279 int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1281 return int_set_rsa_pss_saltlen(ctx, saltlen, EVP_PKEY_RSA_PSS,
1282 EVP_PKEY_OP_KEYGEN);
1285 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1287 OSSL_PARAM pad_params[2], *p = pad_params;
1289 if (ctx == NULL || saltlen == NULL) {
1290 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1291 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1295 /* If key type not RSA or RSA-PSS return error */
1296 if (ctx->pmeth != NULL
1297 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1298 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1301 /* TODO(3.0): Remove this eventually when no more legacy */
1302 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1303 || ctx->op.sig.sigprovctx == NULL)
1304 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1305 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1309 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1310 *p++ = OSSL_PARAM_construct_end();
1312 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1319 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1321 OSSL_PARAM params[2], *p = params;
1322 size_t bits2 = bits;
1324 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1325 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1326 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1330 /* If key type not RSA return error */
1331 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1334 /* TODO(3.0): Remove this eventually when no more legacy */
1335 if (ctx->op.keymgmt.genctx == NULL)
1336 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1337 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1339 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1340 *p++ = OSSL_PARAM_construct_end();
1342 if (!EVP_PKEY_CTX_set_params(ctx, params))
1348 static int evp_pkey_ctx_set_rsa_keygen_pubexp_intern(EVP_PKEY_CTX *ctx,
1352 OSSL_PARAM_BLD *tmpl;
1356 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1357 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1358 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1362 /* If key type not RSA return error */
1363 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1366 /* TODO(3.0): Remove this eventually when no more legacy */
1367 if (ctx->op.keymgmt.genctx == NULL) {
1369 pubexp = BN_dup(pubexp);
1370 ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1371 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1372 if ((copy == 1) && (ret <= 0))
1377 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1379 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1380 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1381 OSSL_PARAM_BLD_free(tmpl);
1384 OSSL_PARAM_BLD_free(tmpl);
1386 ret = EVP_PKEY_CTX_set_params(ctx, params);
1387 OSSL_PARAM_BLD_free_params(params);
1390 * Satisfy memory semantics for pre-3.0 callers of
1391 * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1392 * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1394 if ((copy == 0) && (ret > 0))
1395 ctx->rsa_pubexp = pubexp;
1400 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1402 return evp_pkey_ctx_set_rsa_keygen_pubexp_intern(ctx, pubexp, 0);
1405 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1407 return evp_pkey_ctx_set_rsa_keygen_pubexp_intern(ctx, pubexp, 1);
1410 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1412 OSSL_PARAM params[2], *p = params;
1413 size_t primes2 = primes;
1415 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1416 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1417 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1421 /* If key type not RSA return error */
1422 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1425 /* TODO(3.0): Remove this eventually when no more legacy */
1426 if (ctx->op.keymgmt.genctx == NULL)
1427 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1428 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1431 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1432 *p++ = OSSL_PARAM_construct_end();
1434 if (!EVP_PKEY_CTX_set_params(ctx, params))