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 "internal/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_MODE)
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_MODE)
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);
165 /* TODO(3.0): Support PSS in FIPS_MODE */
167 RSA_PSS_PARAMS_free(r->pss);
168 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
170 BN_BLINDING_free(r->blinding);
171 BN_BLINDING_free(r->mt_blinding);
172 OPENSSL_free(r->bignum_data);
176 int RSA_up_ref(RSA *r)
180 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
183 REF_PRINT_COUNT("RSA", r);
184 REF_ASSERT_ISNT(i < 2);
185 return i > 1 ? 1 : 0;
189 int RSA_set_ex_data(RSA *r, int idx, void *arg)
191 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
194 void *RSA_get_ex_data(const RSA *r, int idx)
196 return CRYPTO_get_ex_data(&r->ex_data, idx);
201 * Define a scaling constant for our fixed point arithmetic.
202 * This value must be a power of two because the base two logarithm code
203 * makes this assumption. The exponent must also be a multiple of three so
204 * that the scale factor has an exact cube root. Finally, the scale factor
205 * should not be so large that a multiplication of two scaled numbers
206 * overflows a 64 bit unsigned integer.
208 static const unsigned int scale = 1 << 18;
209 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
211 /* Define some constants, none exceed 32 bits */
212 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
213 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
214 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
215 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
218 * Multiply two scaled integers together and rescale the result.
220 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
222 return a * b / scale;
226 * Calculate the cube root of a 64 bit scaled integer.
227 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
228 * integer, this is not guaranteed after scaling, so this function has a
229 * 64 bit return. This uses the shifting nth root algorithm with some
230 * algebraic simplifications.
232 static uint64_t icbrt64(uint64_t x)
238 for (s = 63; s >= 0; s -= 3) {
240 b = 3 * r * (r + 1) + 1;
246 return r * cbrt_scale;
250 * Calculate the natural logarithm of a 64 bit scaled integer.
251 * This is done by calculating a base two logarithm and scaling.
252 * The maximum logarithm (base 2) is 64 and this reduces base e, so
253 * a 32 bit result should not overflow. The argument passed must be
254 * greater than unity so we don't need to handle negative results.
256 static uint32_t ilog_e(uint64_t v)
261 * Scale down the value into the range 1 .. 2.
263 * If fractional numbers need to be processed, another loop needs
264 * to go here that checks v < scale and if so multiplies it by 2 and
265 * reduces r by scale. This also means making r signed.
267 while (v >= 2 * scale) {
271 for (i = scale / 2; i != 0; i /= 2) {
273 if (v >= 2 * scale) {
278 r = (r * (uint64_t)scale) / log_e;
283 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
286 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
287 * for FFC safe prime groups for modp and ffdhe.
288 * After Table 25 and Table 26 it refers to
289 * "The maximum security strength estimates were calculated using the formula in
290 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
295 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
296 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
297 * The two cube roots are merged together here.
299 uint16_t ifc_ffc_compute_security_bits(int n)
305 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
319 * The first incorrect result (i.e. not accurate or off by one low) occurs
320 * for n = 699668. The true value here is 1200. Instead of using this n
321 * as the check threshold, the smallest n such that the correct result is
322 * 1200 is used instead.
329 x = n * (uint64_t)log_2;
331 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
338 int RSA_security_bits(const RSA *rsa)
340 int bits = BN_num_bits(rsa->n);
343 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
344 /* This ought to mean that we have private key at hand. */
345 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
347 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
351 return ifc_ffc_compute_security_bits(bits);
354 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
356 /* If the fields n and e in r are NULL, the corresponding input
357 * parameters MUST be non-NULL for n and e. d may be
358 * left NULL (in case only the public key is used).
360 if ((r->n == NULL && n == NULL)
361 || (r->e == NULL && e == NULL))
375 BN_set_flags(r->d, BN_FLG_CONSTTIME);
382 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
384 /* If the fields p and q in r are NULL, the corresponding input
385 * parameters MUST be non-NULL.
387 if ((r->p == NULL && p == NULL)
388 || (r->q == NULL && q == NULL))
394 BN_set_flags(r->p, BN_FLG_CONSTTIME);
399 BN_set_flags(r->q, BN_FLG_CONSTTIME);
406 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
408 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
409 * parameters MUST be non-NULL.
411 if ((r->dmp1 == NULL && dmp1 == NULL)
412 || (r->dmq1 == NULL && dmq1 == NULL)
413 || (r->iqmp == NULL && iqmp == NULL))
417 BN_clear_free(r->dmp1);
419 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
422 BN_clear_free(r->dmq1);
424 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
427 BN_clear_free(r->iqmp);
429 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
438 * Is it better to export RSA_PRIME_INFO structure
439 * and related functions to let user pass a triplet?
441 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
442 BIGNUM *coeffs[], int pnum)
444 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
445 RSA_PRIME_INFO *pinfo;
448 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
451 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
452 if (prime_infos == NULL)
455 if (r->prime_infos != NULL)
456 old = r->prime_infos;
458 for (i = 0; i < pnum; i++) {
459 pinfo = rsa_multip_info_new();
462 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
463 BN_clear_free(pinfo->r);
464 BN_clear_free(pinfo->d);
465 BN_clear_free(pinfo->t);
466 pinfo->r = primes[i];
468 pinfo->t = coeffs[i];
469 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
470 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
471 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
473 rsa_multip_info_free(pinfo);
476 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
479 r->prime_infos = prime_infos;
481 if (!rsa_multip_calc_product(r)) {
482 r->prime_infos = old;
488 * This is hard to deal with, since the old infos could
489 * also be set by this function and r, d, t should not
490 * be freed in that case. So currently, stay consistent
491 * with other *set0* functions: just free it...
493 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
496 r->version = RSA_ASN1_VERSION_MULTI;
501 /* r, d, t should not be freed */
502 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
507 void RSA_get0_key(const RSA *r,
508 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
518 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
527 int RSA_get_multi_prime_extra_count(const RSA *r)
531 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
537 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
540 RSA_PRIME_INFO *pinfo;
542 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
546 * return other primes
547 * it's caller's responsibility to allocate oth_primes[pnum]
549 for (i = 0; i < pnum; i++) {
550 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
551 primes[i] = pinfo->r;
558 void RSA_get0_crt_params(const RSA *r,
559 const BIGNUM **dmp1, const BIGNUM **dmq1,
571 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
572 const BIGNUM *coeffs[])
576 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
579 /* return other primes */
580 if (exps != NULL || coeffs != NULL) {
581 RSA_PRIME_INFO *pinfo;
584 /* it's the user's job to guarantee the buffer length */
585 for (i = 0; i < pnum; i++) {
586 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
590 coeffs[i] = pinfo->t;
598 const BIGNUM *RSA_get0_n(const RSA *r)
603 const BIGNUM *RSA_get0_e(const RSA *r)
608 const BIGNUM *RSA_get0_d(const RSA *r)
613 const BIGNUM *RSA_get0_p(const RSA *r)
618 const BIGNUM *RSA_get0_q(const RSA *r)
623 const BIGNUM *RSA_get0_dmp1(const RSA *r)
628 const BIGNUM *RSA_get0_dmq1(const RSA *r)
633 const BIGNUM *RSA_get0_iqmp(const RSA *r)
638 /* TODO(3.0): Temporary until we move PSS support into the FIPS module */
640 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
646 void RSA_clear_flags(RSA *r, int flags)
651 int RSA_test_flags(const RSA *r, int flags)
653 return r->flags & flags;
656 void RSA_set_flags(RSA *r, int flags)
661 int RSA_get_version(RSA *r)
663 /* { two-prime(0), multi(1) } */
668 ENGINE *RSA_get0_engine(const RSA *r)
673 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
675 /* If key type not RSA or RSA-PSS return error */
676 if (ctx != NULL && ctx->pmeth != NULL
677 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
678 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
680 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
684 DEFINE_STACK_OF(BIGNUM)
686 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
687 const STACK_OF(BIGNUM) *exps,
688 const STACK_OF(BIGNUM) *coeffs)
691 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
695 if (primes == NULL || exps == NULL || coeffs == NULL)
698 pnum = sk_BIGNUM_num(primes);
700 || pnum != sk_BIGNUM_num(exps)
701 || pnum != sk_BIGNUM_num(coeffs) + 1)
704 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
705 sk_BIGNUM_value(primes, 1))
706 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
707 sk_BIGNUM_value(exps, 1),
708 sk_BIGNUM_value(coeffs, 0)))
712 old_infos = r->prime_infos;
719 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
720 if (prime_infos == NULL)
723 for (i = 2; i < pnum; i++) {
724 BIGNUM *prime = sk_BIGNUM_value(primes, i);
725 BIGNUM *exp = sk_BIGNUM_value(exps, i);
726 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
727 RSA_PRIME_INFO *pinfo = NULL;
729 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
732 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
733 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
734 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
741 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
742 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
743 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
744 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
747 r->prime_infos = prime_infos;
749 if (!rsa_multip_calc_product(r)) {
750 r->prime_infos = old_infos;
759 if (old_infos != NULL) {
761 * This is hard to deal with, since the old infos could
762 * also be set by this function and r, d, t should not
763 * be freed in that case. So currently, stay consistent
764 * with other *set0* functions: just free it...
766 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
770 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
776 /* r, d, t should not be freed */
777 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
782 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
784 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
785 STACK_OF(BIGNUM_const) *exps,
786 STACK_OF(BIGNUM_const) *coeffs)
789 RSA_PRIME_INFO *pinfo;
796 /* If |p| is NULL, there are no CRT parameters */
797 if (RSA_get0_p(r) == NULL)
800 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
801 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
802 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
803 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
804 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
807 pnum = RSA_get_multi_prime_extra_count(r);
808 for (i = 0; i < pnum; i++) {
809 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
810 sk_BIGNUM_const_push(primes, pinfo->r);
811 sk_BIGNUM_const_push(exps, pinfo->d);
812 sk_BIGNUM_const_push(coeffs, pinfo->t);
820 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
822 OSSL_PARAM pad_params[2], *p = pad_params;
825 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
826 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
830 /* If key type not RSA or RSA-PSS return error */
831 if (ctx->pmeth != NULL
832 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
833 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
836 /* TODO(3.0): Remove this eventually when no more legacy */
837 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
838 || ctx->op.ciph.ciphprovctx == NULL)
839 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
840 || ctx->op.sig.sigprovctx == NULL))
841 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
844 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
845 *p++ = OSSL_PARAM_construct_end();
847 return EVP_PKEY_CTX_set_params(ctx, pad_params);
850 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
852 OSSL_PARAM pad_params[2], *p = pad_params;
854 if (ctx == NULL || pad_mode == NULL) {
855 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
856 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
860 /* If key type not RSA or RSA-PSS return error */
861 if (ctx->pmeth != NULL
862 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
863 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
866 /* TODO(3.0): Remove this eventually when no more legacy */
867 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
868 || ctx->op.ciph.ciphprovctx == NULL)
869 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
870 || ctx->op.sig.sigprovctx == NULL))
871 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
874 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
875 *p++ = OSSL_PARAM_construct_end();
877 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
884 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
888 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
889 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
890 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
894 /* If key type not RSA return error */
895 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
898 /* TODO(3.0): Remove this eventually when no more legacy */
899 if (ctx->op.ciph.ciphprovctx == NULL)
900 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
901 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
903 name = (md == NULL) ? "" : EVP_MD_name(md);
905 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
908 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
911 OSSL_PARAM rsa_params[3], *p = rsa_params;
913 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
914 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
915 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
919 /* If key type not RSA return error */
920 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
924 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
926 * Cast away the const. This is read
927 * only so should be safe
930 if (mdprops != NULL) {
931 *p++ = OSSL_PARAM_construct_utf8_string(
932 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
934 * Cast away the const. This is read
935 * only so should be safe
939 *p++ = OSSL_PARAM_construct_end();
941 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
944 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
947 OSSL_PARAM rsa_params[2], *p = rsa_params;
949 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
950 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
951 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
955 /* If key type not RSA return error */
956 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
959 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
961 *p++ = OSSL_PARAM_construct_end();
963 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
969 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
971 /* 80 should be big enough */
974 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
975 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
976 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
980 /* If key type not RSA return error */
981 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
984 /* TODO(3.0): Remove this eventually when no more legacy */
985 if (ctx->op.ciph.ciphprovctx == NULL)
986 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
987 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
989 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
992 /* May be NULL meaning "unknown" */
993 *md = EVP_get_digestbyname(name);
998 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1003 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1004 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1005 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1006 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1010 /* If key type not RSA return error */
1011 if (ctx->pmeth != NULL
1012 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1013 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1016 /* TODO(3.0): Remove this eventually when no more legacy */
1017 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1018 && ctx->op.ciph.ciphprovctx == NULL)
1019 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1020 && ctx->op.sig.sigprovctx == NULL))
1021 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1022 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1023 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1025 name = (md == NULL) ? "" : EVP_MD_name(md);
1027 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1030 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1031 const char *mdprops)
1033 OSSL_PARAM rsa_params[3], *p = rsa_params;
1037 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1038 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1039 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1040 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1044 /* If key type not RSA return error */
1045 if (ctx->pmeth != NULL
1046 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1047 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1050 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1052 * Cast away the const. This is
1053 * read only so should be safe
1056 if (mdprops != NULL) {
1058 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1060 * Cast away the const. This is
1061 * read only so should be safe
1063 (char *)mdprops, 0);
1065 *p++ = OSSL_PARAM_construct_end();
1067 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1070 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1073 OSSL_PARAM rsa_params[2], *p = rsa_params;
1076 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1077 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1078 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1079 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1083 /* If key type not RSA or RSA-PSS return error */
1084 if (ctx->pmeth != NULL
1085 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1086 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1089 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1091 *p++ = OSSL_PARAM_construct_end();
1093 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1099 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1101 /* 80 should be big enough */
1105 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1106 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1107 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1108 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1112 /* If key type not RSA or RSA-PSS return error */
1113 if (ctx->pmeth != NULL
1114 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1115 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1118 /* TODO(3.0): Remove this eventually when no more legacy */
1119 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1120 && ctx->op.ciph.ciphprovctx == NULL)
1121 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1122 && ctx->op.sig.sigprovctx == NULL))
1123 return EVP_PKEY_CTX_ctrl(ctx, -1,
1124 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1125 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1127 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1130 /* May be NULL meaning "unknown" */
1131 *md = EVP_get_digestbyname(name);
1136 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1138 OSSL_PARAM rsa_params[2], *p = rsa_params;
1140 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1141 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1142 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1146 /* If key type not RSA return error */
1147 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1150 /* TODO(3.0): Remove this eventually when no more legacy */
1151 if (ctx->op.ciph.ciphprovctx == NULL)
1152 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1153 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1156 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1158 * Cast away the const. This is
1159 * read only so should be safe
1163 *p++ = OSSL_PARAM_construct_end();
1165 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1168 OPENSSL_free(label);
1172 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1174 OSSL_PARAM rsa_params[3], *p = rsa_params;
1177 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1178 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1179 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1183 /* If key type not RSA return error */
1184 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1187 /* TODO(3.0): Remove this eventually when no more legacy */
1188 if (ctx->op.ciph.ciphprovctx == NULL)
1189 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1190 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1193 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1195 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1197 *p++ = OSSL_PARAM_construct_end();
1199 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1202 if (labellen > INT_MAX)
1205 return (int)labellen;
1208 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1210 OSSL_PARAM pad_params[2], *p = pad_params;
1213 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1214 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1218 /* If key type not RSA or RSA-PSS return error */
1219 if (ctx->pmeth != NULL
1220 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1221 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1224 /* TODO(3.0): Remove this eventually when no more legacy */
1225 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1226 || ctx->op.sig.sigprovctx == NULL)
1227 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1231 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1232 *p++ = OSSL_PARAM_construct_end();
1234 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1237 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1239 OSSL_PARAM pad_params[2], *p = pad_params;
1241 if (ctx == NULL || saltlen == NULL) {
1242 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1243 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1247 /* If key type not RSA or RSA-PSS return error */
1248 if (ctx->pmeth != NULL
1249 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1250 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1253 /* TODO(3.0): Remove this eventually when no more legacy */
1254 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1255 || ctx->op.sig.sigprovctx == NULL)
1256 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1257 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1261 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1262 *p++ = OSSL_PARAM_construct_end();
1264 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1271 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1273 OSSL_PARAM params[2], *p = params;
1274 size_t bits2 = bits;
1276 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1277 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1278 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1282 /* If key type not RSA return error */
1283 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1286 /* TODO(3.0): Remove this eventually when no more legacy */
1287 if (ctx->op.keymgmt.genctx == NULL)
1288 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1289 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1291 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1292 *p++ = OSSL_PARAM_construct_end();
1294 if (!EVP_PKEY_CTX_set_params(ctx, params))
1300 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1302 OSSL_PARAM_BLD tmpl;
1306 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1307 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1308 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1312 /* If key type not RSA return error */
1313 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1316 /* TODO(3.0): Remove this eventually when no more legacy */
1317 if (ctx->op.keymgmt.genctx == NULL)
1318 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1319 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1321 ossl_param_bld_init(&tmpl);
1322 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1323 || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
1326 ret = EVP_PKEY_CTX_set_params(ctx, params);
1327 ossl_param_bld_free(params);
1331 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1333 OSSL_PARAM params[2], *p = params;
1334 size_t primes2 = primes;
1336 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1337 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1338 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1342 /* If key type not RSA return error */
1343 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1346 /* TODO(3.0): Remove this eventually when no more legacy */
1347 if (ctx->op.keymgmt.genctx == NULL)
1348 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1349 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1352 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1353 *p++ = OSSL_PARAM_construct_end();
1355 if (!EVP_PKEY_CTX_set_params(ctx, params))