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, OPENSSL_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 *rsa_new_with_ctx(OPENSSL_CTX *libctx)
71 return rsa_new_intern(NULL, libctx);
74 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_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 RSA_PSS_PARAMS_free(r->pss);
167 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
169 BN_BLINDING_free(r->blinding);
170 BN_BLINDING_free(r->mt_blinding);
171 OPENSSL_free(r->bignum_data);
175 int RSA_up_ref(RSA *r)
179 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
182 REF_PRINT_COUNT("RSA", r);
183 REF_ASSERT_ISNT(i < 2);
184 return i > 1 ? 1 : 0;
187 OPENSSL_CTX *rsa_get0_libctx(RSA *r)
193 int RSA_set_ex_data(RSA *r, int idx, void *arg)
195 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
198 void *RSA_get_ex_data(const RSA *r, int idx)
200 return CRYPTO_get_ex_data(&r->ex_data, idx);
205 * Define a scaling constant for our fixed point arithmetic.
206 * This value must be a power of two because the base two logarithm code
207 * makes this assumption. The exponent must also be a multiple of three so
208 * that the scale factor has an exact cube root. Finally, the scale factor
209 * should not be so large that a multiplication of two scaled numbers
210 * overflows a 64 bit unsigned integer.
212 static const unsigned int scale = 1 << 18;
213 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
215 /* Define some constants, none exceed 32 bits */
216 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
217 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
218 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
219 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
222 * Multiply two scaled integers together and rescale the result.
224 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
226 return a * b / scale;
230 * Calculate the cube root of a 64 bit scaled integer.
231 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
232 * integer, this is not guaranteed after scaling, so this function has a
233 * 64 bit return. This uses the shifting nth root algorithm with some
234 * algebraic simplifications.
236 static uint64_t icbrt64(uint64_t x)
242 for (s = 63; s >= 0; s -= 3) {
244 b = 3 * r * (r + 1) + 1;
250 return r * cbrt_scale;
254 * Calculate the natural logarithm of a 64 bit scaled integer.
255 * This is done by calculating a base two logarithm and scaling.
256 * The maximum logarithm (base 2) is 64 and this reduces base e, so
257 * a 32 bit result should not overflow. The argument passed must be
258 * greater than unity so we don't need to handle negative results.
260 static uint32_t ilog_e(uint64_t v)
265 * Scale down the value into the range 1 .. 2.
267 * If fractional numbers need to be processed, another loop needs
268 * to go here that checks v < scale and if so multiplies it by 2 and
269 * reduces r by scale. This also means making r signed.
271 while (v >= 2 * scale) {
275 for (i = scale / 2; i != 0; i /= 2) {
277 if (v >= 2 * scale) {
282 r = (r * (uint64_t)scale) / log_e;
287 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
290 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
291 * for FFC safe prime groups for modp and ffdhe.
292 * After Table 25 and Table 26 it refers to
293 * "The maximum security strength estimates were calculated using the formula in
294 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
299 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
300 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
301 * The two cube roots are merged together here.
303 uint16_t ifc_ffc_compute_security_bits(int n)
309 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
323 * The first incorrect result (i.e. not accurate or off by one low) occurs
324 * for n = 699668. The true value here is 1200. Instead of using this n
325 * as the check threshold, the smallest n such that the correct result is
326 * 1200 is used instead.
333 x = n * (uint64_t)log_2;
335 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
342 int RSA_security_bits(const RSA *rsa)
344 int bits = BN_num_bits(rsa->n);
347 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
348 /* This ought to mean that we have private key at hand. */
349 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
351 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
355 return ifc_ffc_compute_security_bits(bits);
358 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
360 /* If the fields n and e in r are NULL, the corresponding input
361 * parameters MUST be non-NULL for n and e. d may be
362 * left NULL (in case only the public key is used).
364 if ((r->n == NULL && n == NULL)
365 || (r->e == NULL && e == NULL))
379 BN_set_flags(r->d, BN_FLG_CONSTTIME);
386 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
388 /* If the fields p and q in r are NULL, the corresponding input
389 * parameters MUST be non-NULL.
391 if ((r->p == NULL && p == NULL)
392 || (r->q == NULL && q == NULL))
398 BN_set_flags(r->p, BN_FLG_CONSTTIME);
403 BN_set_flags(r->q, BN_FLG_CONSTTIME);
410 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
412 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
413 * parameters MUST be non-NULL.
415 if ((r->dmp1 == NULL && dmp1 == NULL)
416 || (r->dmq1 == NULL && dmq1 == NULL)
417 || (r->iqmp == NULL && iqmp == NULL))
421 BN_clear_free(r->dmp1);
423 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
426 BN_clear_free(r->dmq1);
428 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
431 BN_clear_free(r->iqmp);
433 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
442 * Is it better to export RSA_PRIME_INFO structure
443 * and related functions to let user pass a triplet?
445 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
446 BIGNUM *coeffs[], int pnum)
448 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
449 RSA_PRIME_INFO *pinfo;
452 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
455 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
456 if (prime_infos == NULL)
459 if (r->prime_infos != NULL)
460 old = r->prime_infos;
462 for (i = 0; i < pnum; i++) {
463 pinfo = rsa_multip_info_new();
466 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
467 BN_clear_free(pinfo->r);
468 BN_clear_free(pinfo->d);
469 BN_clear_free(pinfo->t);
470 pinfo->r = primes[i];
472 pinfo->t = coeffs[i];
473 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
474 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
475 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
477 rsa_multip_info_free(pinfo);
480 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
483 r->prime_infos = prime_infos;
485 if (!rsa_multip_calc_product(r)) {
486 r->prime_infos = old;
492 * This is hard to deal with, since the old infos could
493 * also be set by this function and r, d, t should not
494 * be freed in that case. So currently, stay consistent
495 * with other *set0* functions: just free it...
497 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
500 r->version = RSA_ASN1_VERSION_MULTI;
505 /* r, d, t should not be freed */
506 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
511 void RSA_get0_key(const RSA *r,
512 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
522 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
531 int RSA_get_multi_prime_extra_count(const RSA *r)
535 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
541 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
544 RSA_PRIME_INFO *pinfo;
546 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
550 * return other primes
551 * it's caller's responsibility to allocate oth_primes[pnum]
553 for (i = 0; i < pnum; i++) {
554 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
555 primes[i] = pinfo->r;
562 void RSA_get0_crt_params(const RSA *r,
563 const BIGNUM **dmp1, const BIGNUM **dmq1,
575 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
576 const BIGNUM *coeffs[])
580 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
583 /* return other primes */
584 if (exps != NULL || coeffs != NULL) {
585 RSA_PRIME_INFO *pinfo;
588 /* it's the user's job to guarantee the buffer length */
589 for (i = 0; i < pnum; i++) {
590 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
594 coeffs[i] = pinfo->t;
602 const BIGNUM *RSA_get0_n(const RSA *r)
607 const BIGNUM *RSA_get0_e(const RSA *r)
612 const BIGNUM *RSA_get0_d(const RSA *r)
617 const BIGNUM *RSA_get0_p(const RSA *r)
622 const BIGNUM *RSA_get0_q(const RSA *r)
627 const BIGNUM *RSA_get0_dmp1(const RSA *r)
632 const BIGNUM *RSA_get0_dmq1(const RSA *r)
637 const BIGNUM *RSA_get0_iqmp(const RSA *r)
642 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
652 RSA_PSS_PARAMS_30 *rsa_get0_pss_params_30(RSA *r)
654 return &r->pss_params;
657 void RSA_clear_flags(RSA *r, int flags)
662 int RSA_test_flags(const RSA *r, int flags)
664 return r->flags & flags;
667 void RSA_set_flags(RSA *r, int flags)
672 int RSA_get_version(RSA *r)
674 /* { two-prime(0), multi(1) } */
679 ENGINE *RSA_get0_engine(const RSA *r)
684 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
686 /* If key type not RSA or RSA-PSS return error */
687 if (ctx != NULL && ctx->pmeth != NULL
688 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
689 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
691 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
695 DEFINE_STACK_OF(BIGNUM)
697 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
698 const STACK_OF(BIGNUM) *exps,
699 const STACK_OF(BIGNUM) *coeffs)
702 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
706 if (primes == NULL || exps == NULL || coeffs == NULL)
709 pnum = sk_BIGNUM_num(primes);
711 || pnum != sk_BIGNUM_num(exps)
712 || pnum != sk_BIGNUM_num(coeffs) + 1)
715 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
716 sk_BIGNUM_value(primes, 1))
717 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
718 sk_BIGNUM_value(exps, 1),
719 sk_BIGNUM_value(coeffs, 0)))
723 old_infos = r->prime_infos;
730 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
731 if (prime_infos == NULL)
734 for (i = 2; i < pnum; i++) {
735 BIGNUM *prime = sk_BIGNUM_value(primes, i);
736 BIGNUM *exp = sk_BIGNUM_value(exps, i);
737 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
738 RSA_PRIME_INFO *pinfo = NULL;
740 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
743 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
744 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
745 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
752 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
753 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
754 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
755 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
758 r->prime_infos = prime_infos;
760 if (!rsa_multip_calc_product(r)) {
761 r->prime_infos = old_infos;
770 if (old_infos != NULL) {
772 * This is hard to deal with, since the old infos could
773 * also be set by this function and r, d, t should not
774 * be freed in that case. So currently, stay consistent
775 * with other *set0* functions: just free it...
777 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
781 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
787 /* r, d, t should not be freed */
788 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
793 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
795 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
796 STACK_OF(BIGNUM_const) *exps,
797 STACK_OF(BIGNUM_const) *coeffs)
800 RSA_PRIME_INFO *pinfo;
807 /* If |p| is NULL, there are no CRT parameters */
808 if (RSA_get0_p(r) == NULL)
811 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
812 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
813 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
814 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
815 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
818 pnum = RSA_get_multi_prime_extra_count(r);
819 for (i = 0; i < pnum; i++) {
820 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
821 sk_BIGNUM_const_push(primes, pinfo->r);
822 sk_BIGNUM_const_push(exps, pinfo->d);
823 sk_BIGNUM_const_push(coeffs, pinfo->t);
831 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
833 OSSL_PARAM pad_params[2], *p = pad_params;
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 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
851 || ctx->op.sig.sigprovctx == NULL))
852 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
855 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
856 *p++ = OSSL_PARAM_construct_end();
858 return EVP_PKEY_CTX_set_params(ctx, pad_params);
861 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
863 OSSL_PARAM pad_params[2], *p = pad_params;
865 if (ctx == NULL || pad_mode == NULL) {
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 or RSA-PSS return error */
872 if (ctx->pmeth != NULL
873 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
874 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
877 /* TODO(3.0): Remove this eventually when no more legacy */
878 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
879 || ctx->op.ciph.ciphprovctx == NULL)
880 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
881 || ctx->op.sig.sigprovctx == NULL))
882 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
885 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
886 *p++ = OSSL_PARAM_construct_end();
888 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
895 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
899 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
900 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
901 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
905 /* If key type not RSA return error */
906 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
909 /* TODO(3.0): Remove this eventually when no more legacy */
910 if (ctx->op.ciph.ciphprovctx == NULL)
911 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
912 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
914 name = (md == NULL) ? "" : EVP_MD_name(md);
916 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
919 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
922 OSSL_PARAM rsa_params[3], *p = rsa_params;
924 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
925 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
926 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
930 /* If key type not RSA return error */
931 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
935 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
937 * Cast away the const. This is read
938 * only so should be safe
941 if (mdprops != NULL) {
942 *p++ = OSSL_PARAM_construct_utf8_string(
943 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
945 * Cast away the const. This is read
946 * only so should be safe
950 *p++ = OSSL_PARAM_construct_end();
952 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
955 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
958 OSSL_PARAM rsa_params[2], *p = rsa_params;
960 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
961 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
962 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
966 /* If key type not RSA return error */
967 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
970 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
972 *p++ = OSSL_PARAM_construct_end();
974 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
980 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
982 /* 80 should be big enough */
985 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
986 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
987 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
991 /* If key type not RSA return error */
992 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
995 /* TODO(3.0): Remove this eventually when no more legacy */
996 if (ctx->op.ciph.ciphprovctx == NULL)
997 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
998 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
1000 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
1003 /* May be NULL meaning "unknown" */
1004 *md = EVP_get_digestbyname(name);
1009 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1014 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1015 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1016 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1017 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1021 /* If key type not RSA return error */
1022 if (ctx->pmeth != NULL
1023 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1024 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1027 /* TODO(3.0): Remove this eventually when no more legacy */
1028 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1029 && ctx->op.ciph.ciphprovctx == NULL)
1030 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1031 && ctx->op.sig.sigprovctx == NULL))
1032 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1033 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1034 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1036 name = (md == NULL) ? "" : EVP_MD_name(md);
1038 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1041 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1042 const char *mdprops)
1044 OSSL_PARAM rsa_params[3], *p = rsa_params;
1048 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1049 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1050 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1051 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1055 /* If key type not RSA return error */
1056 if (ctx->pmeth != NULL
1057 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1058 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1061 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1063 * Cast away the const. This is
1064 * read only so should be safe
1067 if (mdprops != NULL) {
1069 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1071 * Cast away the const. This is
1072 * read only so should be safe
1074 (char *)mdprops, 0);
1076 *p++ = OSSL_PARAM_construct_end();
1078 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1081 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1084 OSSL_PARAM rsa_params[2], *p = rsa_params;
1087 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1088 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1089 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1090 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1094 /* If key type not RSA or RSA-PSS return error */
1095 if (ctx->pmeth != NULL
1096 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1097 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1100 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1102 *p++ = OSSL_PARAM_construct_end();
1104 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1110 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1112 /* 80 should be big enough */
1116 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1117 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1118 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1119 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1123 /* If key type not RSA or RSA-PSS return error */
1124 if (ctx->pmeth != NULL
1125 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1126 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1129 /* TODO(3.0): Remove this eventually when no more legacy */
1130 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1131 && ctx->op.ciph.ciphprovctx == NULL)
1132 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1133 && ctx->op.sig.sigprovctx == NULL))
1134 return EVP_PKEY_CTX_ctrl(ctx, -1,
1135 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1136 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1138 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1141 /* May be NULL meaning "unknown" */
1142 *md = EVP_get_digestbyname(name);
1147 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1149 OSSL_PARAM rsa_params[2], *p = rsa_params;
1151 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1152 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1153 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1157 /* If key type not RSA return error */
1158 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1161 /* TODO(3.0): Remove this eventually when no more legacy */
1162 if (ctx->op.ciph.ciphprovctx == NULL)
1163 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1164 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1167 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1169 * Cast away the const. This is
1170 * read only so should be safe
1174 *p++ = OSSL_PARAM_construct_end();
1176 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1179 OPENSSL_free(label);
1183 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1185 OSSL_PARAM rsa_params[3], *p = rsa_params;
1188 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1189 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1190 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1194 /* If key type not RSA return error */
1195 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1198 /* TODO(3.0): Remove this eventually when no more legacy */
1199 if (ctx->op.ciph.ciphprovctx == NULL)
1200 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1201 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1204 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1206 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1208 *p++ = OSSL_PARAM_construct_end();
1210 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1213 if (labellen > INT_MAX)
1216 return (int)labellen;
1219 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1221 OSSL_PARAM pad_params[2], *p = pad_params;
1224 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1225 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1229 /* If key type not RSA or RSA-PSS return error */
1230 if (ctx->pmeth != NULL
1231 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1232 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1235 /* TODO(3.0): Remove this eventually when no more legacy */
1236 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1237 || ctx->op.sig.sigprovctx == NULL)
1238 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1242 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1243 *p++ = OSSL_PARAM_construct_end();
1245 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1248 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1250 OSSL_PARAM pad_params[2], *p = pad_params;
1252 if (ctx == NULL || saltlen == NULL) {
1253 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1254 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1258 /* If key type not RSA or RSA-PSS return error */
1259 if (ctx->pmeth != NULL
1260 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1261 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1264 /* TODO(3.0): Remove this eventually when no more legacy */
1265 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1266 || ctx->op.sig.sigprovctx == NULL)
1267 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1268 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1272 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1273 *p++ = OSSL_PARAM_construct_end();
1275 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1282 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1284 OSSL_PARAM params[2], *p = params;
1285 size_t bits2 = bits;
1287 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1288 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1289 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1293 /* If key type not RSA return error */
1294 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1297 /* TODO(3.0): Remove this eventually when no more legacy */
1298 if (ctx->op.keymgmt.genctx == NULL)
1299 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1300 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1302 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1303 *p++ = OSSL_PARAM_construct_end();
1305 if (!EVP_PKEY_CTX_set_params(ctx, params))
1311 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1313 OSSL_PARAM_BLD *tmpl;
1317 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1318 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1319 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1323 /* If key type not RSA return error */
1324 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1327 /* TODO(3.0): Remove this eventually when no more legacy */
1328 if (ctx->op.keymgmt.genctx == NULL)
1329 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1330 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1332 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1334 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1335 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1336 OSSL_PARAM_BLD_free(tmpl);
1339 OSSL_PARAM_BLD_free(tmpl);
1341 ret = EVP_PKEY_CTX_set_params(ctx, params);
1342 OSSL_PARAM_BLD_free_params(params);
1346 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1348 OSSL_PARAM params[2], *p = params;
1349 size_t primes2 = primes;
1351 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1352 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1353 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1357 /* If key type not RSA return error */
1358 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1361 /* TODO(3.0): Remove this eventually when no more legacy */
1362 if (ctx->op.keymgmt.genctx == NULL)
1363 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1364 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1367 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1368 *p++ = OSSL_PARAM_construct_end();
1370 if (!EVP_PKEY_CTX_set_params(ctx, params))