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 "crypto/security_bits.h"
27 #include "rsa_local.h"
29 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
34 return rsa_new_intern(NULL, NULL);
37 const RSA_METHOD *RSA_get_method(const RSA *rsa)
42 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
45 * NB: The caller is specifically setting a method, so it's not up to us
46 * to deal with which ENGINE it comes from.
48 const RSA_METHOD *mtmp;
52 #ifndef OPENSSL_NO_ENGINE
53 ENGINE_finish(rsa->engine);
62 RSA *RSA_new_method(ENGINE *engine)
64 return rsa_new_intern(engine, NULL);
68 RSA *rsa_new_with_ctx(OPENSSL_CTX *libctx)
70 return rsa_new_intern(NULL, libctx);
73 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
75 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
78 RSAerr(0, ERR_R_MALLOC_FAILURE);
83 ret->lock = CRYPTO_THREAD_lock_new();
84 if (ret->lock == NULL) {
85 RSAerr(0, ERR_R_MALLOC_FAILURE);
91 ret->meth = RSA_get_default_method();
92 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
93 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
95 if (!ENGINE_init(engine)) {
96 RSAerr(0, ERR_R_ENGINE_LIB);
101 ret->engine = ENGINE_get_default_RSA();
104 ret->meth = ENGINE_get_RSA(ret->engine);
105 if (ret->meth == NULL) {
106 RSAerr(0, ERR_R_ENGINE_LIB);
112 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
114 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
119 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
120 RSAerr(0, ERR_R_INIT_FAIL);
131 void RSA_free(RSA *r)
138 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
139 REF_PRINT_COUNT("RSA", r);
142 REF_ASSERT_ISNT(i < 0);
144 if (r->meth != NULL && r->meth->finish != NULL)
146 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
147 ENGINE_finish(r->engine);
151 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
154 CRYPTO_THREAD_lock_free(r->lock);
161 BN_clear_free(r->dmp1);
162 BN_clear_free(r->dmq1);
163 BN_clear_free(r->iqmp);
164 /* TODO(3.0): Support PSS in FIPS_MODE */
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;
188 int RSA_set_ex_data(RSA *r, int idx, void *arg)
190 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
193 void *RSA_get_ex_data(const RSA *r, int idx)
195 return CRYPTO_get_ex_data(&r->ex_data, idx);
200 * Define a scaling constant for our fixed point arithmetic.
201 * This value must be a power of two because the base two logarithm code
202 * makes this assumption. The exponent must also be a multiple of three so
203 * that the scale factor has an exact cube root. Finally, the scale factor
204 * should not be so large that a multiplication of two scaled numbers
205 * overflows a 64 bit unsigned integer.
207 static const unsigned int scale = 1 << 18;
208 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
210 /* Define some constants, none exceed 32 bits */
211 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
212 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
213 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
214 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
217 * Multiply two scaled integers together and rescale the result.
219 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
221 return a * b / scale;
225 * Calculate the cube root of a 64 bit scaled integer.
226 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
227 * integer, this is not guaranteed after scaling, so this function has a
228 * 64 bit return. This uses the shifting nth root algorithm with some
229 * algebraic simplifications.
231 static uint64_t icbrt64(uint64_t x)
237 for (s = 63; s >= 0; s -= 3) {
239 b = 3 * r * (r + 1) + 1;
245 return r * cbrt_scale;
249 * Calculate the natural logarithm of a 64 bit scaled integer.
250 * This is done by calculating a base two logarithm and scaling.
251 * The maximum logarithm (base 2) is 64 and this reduces base e, so
252 * a 32 bit result should not overflow. The argument passed must be
253 * greater than unity so we don't need to handle negative results.
255 static uint32_t ilog_e(uint64_t v)
260 * Scale down the value into the range 1 .. 2.
262 * If fractional numbers need to be processed, another loop needs
263 * to go here that checks v < scale and if so multiplies it by 2 and
264 * reduces r by scale. This also means making r signed.
266 while (v >= 2 * scale) {
270 for (i = scale / 2; i != 0; i /= 2) {
272 if (v >= 2 * scale) {
277 r = (r * (uint64_t)scale) / log_e;
282 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
285 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
286 * for FFC safe prime groups for modp and ffdhe.
287 * After Table 25 and Table 26 it refers to
288 * "The maximum security strength estimates were calculated using the formula in
289 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
294 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
295 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
296 * The two cube roots are merged together here.
298 uint16_t ifc_ffc_compute_security_bits(int n)
304 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
318 * The first incorrect result (i.e. not accurate or off by one low) occurs
319 * for n = 699668. The true value here is 1200. Instead of using this n
320 * as the check threshold, the smallest n such that the correct result is
321 * 1200 is used instead.
328 x = n * (uint64_t)log_2;
330 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
337 int RSA_security_bits(const RSA *rsa)
339 int bits = BN_num_bits(rsa->n);
342 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
343 /* This ought to mean that we have private key at hand. */
344 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
346 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
350 return ifc_ffc_compute_security_bits(bits);
353 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
355 /* If the fields n and e in r are NULL, the corresponding input
356 * parameters MUST be non-NULL for n and e. d may be
357 * left NULL (in case only the public key is used).
359 if ((r->n == NULL && n == NULL)
360 || (r->e == NULL && e == NULL))
374 BN_set_flags(r->d, BN_FLG_CONSTTIME);
381 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
383 /* If the fields p and q in r are NULL, the corresponding input
384 * parameters MUST be non-NULL.
386 if ((r->p == NULL && p == NULL)
387 || (r->q == NULL && q == NULL))
393 BN_set_flags(r->p, BN_FLG_CONSTTIME);
398 BN_set_flags(r->q, BN_FLG_CONSTTIME);
405 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
407 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
408 * parameters MUST be non-NULL.
410 if ((r->dmp1 == NULL && dmp1 == NULL)
411 || (r->dmq1 == NULL && dmq1 == NULL)
412 || (r->iqmp == NULL && iqmp == NULL))
416 BN_clear_free(r->dmp1);
418 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
421 BN_clear_free(r->dmq1);
423 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
426 BN_clear_free(r->iqmp);
428 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
437 * Is it better to export RSA_PRIME_INFO structure
438 * and related functions to let user pass a triplet?
440 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
441 BIGNUM *coeffs[], int pnum)
443 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
444 RSA_PRIME_INFO *pinfo;
447 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
450 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
451 if (prime_infos == NULL)
454 if (r->prime_infos != NULL)
455 old = r->prime_infos;
457 for (i = 0; i < pnum; i++) {
458 pinfo = rsa_multip_info_new();
461 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
462 BN_clear_free(pinfo->r);
463 BN_clear_free(pinfo->d);
464 BN_clear_free(pinfo->t);
465 pinfo->r = primes[i];
467 pinfo->t = coeffs[i];
468 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
469 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
470 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
472 rsa_multip_info_free(pinfo);
475 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
478 r->prime_infos = prime_infos;
480 if (!rsa_multip_calc_product(r)) {
481 r->prime_infos = old;
487 * This is hard to deal with, since the old infos could
488 * also be set by this function and r, d, t should not
489 * be freed in that case. So currently, stay consistent
490 * with other *set0* functions: just free it...
492 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
495 r->version = RSA_ASN1_VERSION_MULTI;
500 /* r, d, t should not be freed */
501 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
506 void RSA_get0_key(const RSA *r,
507 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
517 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
526 int RSA_get_multi_prime_extra_count(const RSA *r)
530 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
536 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
539 RSA_PRIME_INFO *pinfo;
541 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
545 * return other primes
546 * it's caller's responsibility to allocate oth_primes[pnum]
548 for (i = 0; i < pnum; i++) {
549 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
550 primes[i] = pinfo->r;
557 void RSA_get0_crt_params(const RSA *r,
558 const BIGNUM **dmp1, const BIGNUM **dmq1,
570 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
571 const BIGNUM *coeffs[])
575 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
578 /* return other primes */
579 if (exps != NULL || coeffs != NULL) {
580 RSA_PRIME_INFO *pinfo;
583 /* it's the user's job to guarantee the buffer length */
584 for (i = 0; i < pnum; i++) {
585 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
589 coeffs[i] = pinfo->t;
597 const BIGNUM *RSA_get0_n(const RSA *r)
602 const BIGNUM *RSA_get0_e(const RSA *r)
607 const BIGNUM *RSA_get0_d(const RSA *r)
612 const BIGNUM *RSA_get0_p(const RSA *r)
617 const BIGNUM *RSA_get0_q(const RSA *r)
622 const BIGNUM *RSA_get0_dmp1(const RSA *r)
627 const BIGNUM *RSA_get0_dmq1(const RSA *r)
632 const BIGNUM *RSA_get0_iqmp(const RSA *r)
637 /* TODO(3.0): Temporary until we move PSS support into the FIPS module */
639 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
645 void RSA_clear_flags(RSA *r, int flags)
650 int RSA_test_flags(const RSA *r, int flags)
652 return r->flags & flags;
655 void RSA_set_flags(RSA *r, int flags)
660 int RSA_get_version(RSA *r)
662 /* { two-prime(0), multi(1) } */
667 ENGINE *RSA_get0_engine(const RSA *r)
672 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
674 /* If key type not RSA or RSA-PSS return error */
675 if (ctx != NULL && ctx->pmeth != NULL
676 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
677 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
679 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
683 DEFINE_STACK_OF(BIGNUM)
685 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
686 const STACK_OF(BIGNUM) *exps,
687 const STACK_OF(BIGNUM) *coeffs)
690 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
694 if (primes == NULL || exps == NULL || coeffs == NULL)
697 pnum = sk_BIGNUM_num(primes);
699 || pnum != sk_BIGNUM_num(exps)
700 || pnum != sk_BIGNUM_num(coeffs) + 1)
703 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
704 sk_BIGNUM_value(primes, 1))
705 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
706 sk_BIGNUM_value(exps, 1),
707 sk_BIGNUM_value(coeffs, 0)))
711 old_infos = r->prime_infos;
718 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
719 if (prime_infos == NULL)
722 for (i = 2; i < pnum; i++) {
723 BIGNUM *prime = sk_BIGNUM_value(primes, i);
724 BIGNUM *exp = sk_BIGNUM_value(exps, i);
725 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
726 RSA_PRIME_INFO *pinfo = NULL;
728 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
731 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
732 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
733 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
740 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
741 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
742 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
743 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
746 r->prime_infos = prime_infos;
748 if (!rsa_multip_calc_product(r)) {
749 r->prime_infos = old_infos;
758 if (old_infos != NULL) {
760 * This is hard to deal with, since the old infos could
761 * also be set by this function and r, d, t should not
762 * be freed in that case. So currently, stay consistent
763 * with other *set0* functions: just free it...
765 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
769 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
775 /* r, d, t should not be freed */
776 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
781 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
783 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
784 STACK_OF(BIGNUM_const) *exps,
785 STACK_OF(BIGNUM_const) *coeffs)
788 RSA_PRIME_INFO *pinfo;
795 /* If |p| is NULL, there are no CRT parameters */
796 if (RSA_get0_p(r) == NULL)
799 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
800 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
801 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
802 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
803 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
806 pnum = RSA_get_multi_prime_extra_count(r);
807 for (i = 0; i < pnum; i++) {
808 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
809 sk_BIGNUM_const_push(primes, pinfo->r);
810 sk_BIGNUM_const_push(exps, pinfo->d);
811 sk_BIGNUM_const_push(coeffs, pinfo->t);
819 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
821 OSSL_PARAM pad_params[2], *p = pad_params;
824 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
825 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
829 /* If key type not RSA or RSA-PSS return error */
830 if (ctx->pmeth != NULL
831 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
832 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
835 /* TODO(3.0): Remove this eventually when no more legacy */
836 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
837 || ctx->op.ciph.ciphprovctx == NULL)
838 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
839 || ctx->op.sig.sigprovctx == NULL))
840 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
843 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
844 *p++ = OSSL_PARAM_construct_end();
846 return EVP_PKEY_CTX_set_params(ctx, pad_params);
849 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
851 OSSL_PARAM pad_params[2], *p = pad_params;
853 if (ctx == NULL || pad_mode == NULL) {
854 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
855 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
859 /* If key type not RSA or RSA-PSS return error */
860 if (ctx->pmeth != NULL
861 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
862 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
865 /* TODO(3.0): Remove this eventually when no more legacy */
866 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
867 || ctx->op.ciph.ciphprovctx == NULL)
868 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
869 || ctx->op.sig.sigprovctx == NULL))
870 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
873 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
874 *p++ = OSSL_PARAM_construct_end();
876 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
883 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
887 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
888 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
889 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
893 /* If key type not RSA return error */
894 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
897 /* TODO(3.0): Remove this eventually when no more legacy */
898 if (ctx->op.ciph.ciphprovctx == NULL)
899 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
900 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
902 name = (md == NULL) ? "" : EVP_MD_name(md);
904 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
907 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
910 OSSL_PARAM rsa_params[3], *p = rsa_params;
912 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
913 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
914 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
918 /* If key type not RSA return error */
919 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
923 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
925 * Cast away the const. This is read
926 * only so should be safe
929 if (mdprops != NULL) {
930 *p++ = OSSL_PARAM_construct_utf8_string(
931 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
933 * Cast away the const. This is read
934 * only so should be safe
938 *p++ = OSSL_PARAM_construct_end();
940 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
943 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
946 OSSL_PARAM rsa_params[2], *p = rsa_params;
948 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
949 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
950 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
954 /* If key type not RSA return error */
955 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
958 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
960 *p++ = OSSL_PARAM_construct_end();
962 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
968 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
970 /* 80 should be big enough */
973 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
974 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
975 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
979 /* If key type not RSA return error */
980 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
983 /* TODO(3.0): Remove this eventually when no more legacy */
984 if (ctx->op.ciph.ciphprovctx == NULL)
985 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
986 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
988 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
991 /* May be NULL meaning "unknown" */
992 *md = EVP_get_digestbyname(name);
997 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1002 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1003 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1004 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1005 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1009 /* If key type not RSA return error */
1010 if (ctx->pmeth != NULL
1011 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1012 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1015 /* TODO(3.0): Remove this eventually when no more legacy */
1016 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1017 && ctx->op.ciph.ciphprovctx == NULL)
1018 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1019 && ctx->op.sig.sigprovctx == NULL))
1020 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1021 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1022 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1024 name = (md == NULL) ? "" : EVP_MD_name(md);
1026 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1029 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1030 const char *mdprops)
1032 OSSL_PARAM rsa_params[3], *p = rsa_params;
1036 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1037 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1038 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1039 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1043 /* If key type not RSA return error */
1044 if (ctx->pmeth != NULL
1045 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1046 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1049 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1051 * Cast away the const. This is
1052 * read only so should be safe
1055 if (mdprops != NULL) {
1057 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1059 * Cast away the const. This is
1060 * read only so should be safe
1062 (char *)mdprops, 0);
1064 *p++ = OSSL_PARAM_construct_end();
1066 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1069 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1072 OSSL_PARAM rsa_params[2], *p = rsa_params;
1075 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1076 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1077 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1078 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1082 /* If key type not RSA or RSA-PSS return error */
1083 if (ctx->pmeth != NULL
1084 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1085 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1088 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1090 *p++ = OSSL_PARAM_construct_end();
1092 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1098 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1100 /* 80 should be big enough */
1104 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1105 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1106 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1107 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1111 /* If key type not RSA or RSA-PSS return error */
1112 if (ctx->pmeth != NULL
1113 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1114 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1117 /* TODO(3.0): Remove this eventually when no more legacy */
1118 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1119 && ctx->op.ciph.ciphprovctx == NULL)
1120 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1121 && ctx->op.sig.sigprovctx == NULL))
1122 return EVP_PKEY_CTX_ctrl(ctx, -1,
1123 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1124 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1126 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1129 /* May be NULL meaning "unknown" */
1130 *md = EVP_get_digestbyname(name);
1135 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1137 OSSL_PARAM rsa_params[2], *p = rsa_params;
1139 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1140 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1141 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1145 /* If key type not RSA return error */
1146 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1149 /* TODO(3.0): Remove this eventually when no more legacy */
1150 if (ctx->op.ciph.ciphprovctx == NULL)
1151 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1152 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1155 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1157 * Cast away the const. This is
1158 * read only so should be safe
1162 *p++ = OSSL_PARAM_construct_end();
1164 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1167 OPENSSL_free(label);
1171 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1173 OSSL_PARAM rsa_params[3], *p = rsa_params;
1176 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1177 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1178 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1182 /* If key type not RSA return error */
1183 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1186 /* TODO(3.0): Remove this eventually when no more legacy */
1187 if (ctx->op.ciph.ciphprovctx == NULL)
1188 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1189 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1192 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1194 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1196 *p++ = OSSL_PARAM_construct_end();
1198 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1201 if (labellen > INT_MAX)
1204 return (int)labellen;
1207 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1209 OSSL_PARAM pad_params[2], *p = pad_params;
1212 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1213 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1217 /* If key type not RSA or RSA-PSS return error */
1218 if (ctx->pmeth != NULL
1219 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1220 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1223 /* TODO(3.0): Remove this eventually when no more legacy */
1224 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1225 || ctx->op.sig.sigprovctx == NULL)
1226 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1230 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1231 *p++ = OSSL_PARAM_construct_end();
1233 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1236 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1238 OSSL_PARAM pad_params[2], *p = pad_params;
1240 if (ctx == NULL || saltlen == NULL) {
1241 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1242 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1246 /* If key type not RSA or RSA-PSS return error */
1247 if (ctx->pmeth != NULL
1248 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1249 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1252 /* TODO(3.0): Remove this eventually when no more legacy */
1253 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1254 || ctx->op.sig.sigprovctx == NULL)
1255 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1256 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1260 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1261 *p++ = OSSL_PARAM_construct_end();
1263 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))