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 "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_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 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
643 void RSA_clear_flags(RSA *r, int flags)
648 int RSA_test_flags(const RSA *r, int flags)
650 return r->flags & flags;
653 void RSA_set_flags(RSA *r, int flags)
658 int RSA_get_version(RSA *r)
660 /* { two-prime(0), multi(1) } */
665 ENGINE *RSA_get0_engine(const RSA *r)
670 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
672 /* If key type not RSA or RSA-PSS return error */
673 if (ctx != NULL && ctx->pmeth != NULL
674 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
675 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
677 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
681 DEFINE_STACK_OF(BIGNUM)
683 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
684 const STACK_OF(BIGNUM) *exps,
685 const STACK_OF(BIGNUM) *coeffs)
688 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
692 if (primes == NULL || exps == NULL || coeffs == NULL)
695 pnum = sk_BIGNUM_num(primes);
697 || pnum != sk_BIGNUM_num(exps)
698 || pnum != sk_BIGNUM_num(coeffs) + 1)
701 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
702 sk_BIGNUM_value(primes, 1))
703 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
704 sk_BIGNUM_value(exps, 1),
705 sk_BIGNUM_value(coeffs, 0)))
709 old_infos = r->prime_infos;
716 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
717 if (prime_infos == NULL)
720 for (i = 2; i < pnum; i++) {
721 BIGNUM *prime = sk_BIGNUM_value(primes, i);
722 BIGNUM *exp = sk_BIGNUM_value(exps, i);
723 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
724 RSA_PRIME_INFO *pinfo = NULL;
726 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
729 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
730 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
731 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
738 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
739 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
740 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
741 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
744 r->prime_infos = prime_infos;
746 if (!rsa_multip_calc_product(r)) {
747 r->prime_infos = old_infos;
756 if (old_infos != NULL) {
758 * This is hard to deal with, since the old infos could
759 * also be set by this function and r, d, t should not
760 * be freed in that case. So currently, stay consistent
761 * with other *set0* functions: just free it...
763 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
767 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
773 /* r, d, t should not be freed */
774 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
779 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
781 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
782 STACK_OF(BIGNUM_const) *exps,
783 STACK_OF(BIGNUM_const) *coeffs)
786 RSA_PRIME_INFO *pinfo;
793 /* If |p| is NULL, there are no CRT parameters */
794 if (RSA_get0_p(r) == NULL)
797 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
798 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
799 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
800 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
801 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
804 pnum = RSA_get_multi_prime_extra_count(r);
805 for (i = 0; i < pnum; i++) {
806 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
807 sk_BIGNUM_const_push(primes, pinfo->r);
808 sk_BIGNUM_const_push(exps, pinfo->d);
809 sk_BIGNUM_const_push(coeffs, pinfo->t);
817 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
819 OSSL_PARAM pad_params[2], *p = pad_params;
822 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
823 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
827 /* If key type not RSA or RSA-PSS return error */
828 if (ctx->pmeth != NULL
829 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
830 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
833 /* TODO(3.0): Remove this eventually when no more legacy */
834 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
835 || ctx->op.ciph.ciphprovctx == NULL)
836 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
837 || ctx->op.sig.sigprovctx == NULL))
838 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
841 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
842 *p++ = OSSL_PARAM_construct_end();
844 return EVP_PKEY_CTX_set_params(ctx, pad_params);
847 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
849 OSSL_PARAM pad_params[2], *p = pad_params;
851 if (ctx == NULL || pad_mode == NULL) {
852 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
853 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
857 /* If key type not RSA or RSA-PSS return error */
858 if (ctx->pmeth != NULL
859 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
860 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
863 /* TODO(3.0): Remove this eventually when no more legacy */
864 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
865 || ctx->op.ciph.ciphprovctx == NULL)
866 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
867 || ctx->op.sig.sigprovctx == NULL))
868 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
871 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
872 *p++ = OSSL_PARAM_construct_end();
874 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
881 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
885 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
886 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
887 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
891 /* If key type not RSA return error */
892 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
895 /* TODO(3.0): Remove this eventually when no more legacy */
896 if (ctx->op.ciph.ciphprovctx == NULL)
897 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
898 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
900 name = (md == NULL) ? "" : EVP_MD_name(md);
902 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
905 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
908 OSSL_PARAM rsa_params[3], *p = rsa_params;
910 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
911 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
912 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
916 /* If key type not RSA return error */
917 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
921 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
923 * Cast away the const. This is read
924 * only so should be safe
927 if (mdprops != NULL) {
928 *p++ = OSSL_PARAM_construct_utf8_string(
929 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
931 * Cast away the const. This is read
932 * only so should be safe
936 *p++ = OSSL_PARAM_construct_end();
938 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
941 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
944 OSSL_PARAM rsa_params[2], *p = rsa_params;
946 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
947 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
948 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
952 /* If key type not RSA return error */
953 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
956 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
958 *p++ = OSSL_PARAM_construct_end();
960 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
966 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
968 /* 80 should be big enough */
971 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
972 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
973 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
977 /* If key type not RSA return error */
978 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
981 /* TODO(3.0): Remove this eventually when no more legacy */
982 if (ctx->op.ciph.ciphprovctx == NULL)
983 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
984 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
986 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
989 /* May be NULL meaning "unknown" */
990 *md = EVP_get_digestbyname(name);
995 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1000 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1001 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1002 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1003 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1007 /* If key type not RSA return error */
1008 if (ctx->pmeth != NULL
1009 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1010 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1013 /* TODO(3.0): Remove this eventually when no more legacy */
1014 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1015 && ctx->op.ciph.ciphprovctx == NULL)
1016 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1017 && ctx->op.sig.sigprovctx == NULL))
1018 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1019 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1020 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1022 name = (md == NULL) ? "" : EVP_MD_name(md);
1024 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1027 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1028 const char *mdprops)
1030 OSSL_PARAM rsa_params[3], *p = rsa_params;
1034 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1035 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1036 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1037 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1041 /* If key type not RSA return error */
1042 if (ctx->pmeth != NULL
1043 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1044 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1047 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1049 * Cast away the const. This is
1050 * read only so should be safe
1053 if (mdprops != NULL) {
1055 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1057 * Cast away the const. This is
1058 * read only so should be safe
1060 (char *)mdprops, 0);
1062 *p++ = OSSL_PARAM_construct_end();
1064 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1067 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1070 OSSL_PARAM rsa_params[2], *p = rsa_params;
1073 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1074 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1075 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1076 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1080 /* If key type not RSA or RSA-PSS return error */
1081 if (ctx->pmeth != NULL
1082 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1083 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1086 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1088 *p++ = OSSL_PARAM_construct_end();
1090 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1096 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1098 /* 80 should be big enough */
1102 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1103 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1104 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1105 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1109 /* If key type not RSA or RSA-PSS return error */
1110 if (ctx->pmeth != NULL
1111 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1112 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1115 /* TODO(3.0): Remove this eventually when no more legacy */
1116 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1117 && ctx->op.ciph.ciphprovctx == NULL)
1118 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1119 && ctx->op.sig.sigprovctx == NULL))
1120 return EVP_PKEY_CTX_ctrl(ctx, -1,
1121 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1122 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1124 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1127 /* May be NULL meaning "unknown" */
1128 *md = EVP_get_digestbyname(name);
1133 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1135 OSSL_PARAM rsa_params[2], *p = rsa_params;
1137 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_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 return error */
1144 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1147 /* TODO(3.0): Remove this eventually when no more legacy */
1148 if (ctx->op.ciph.ciphprovctx == NULL)
1149 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1150 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1153 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1155 * Cast away the const. This is
1156 * read only so should be safe
1160 *p++ = OSSL_PARAM_construct_end();
1162 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1165 OPENSSL_free(label);
1169 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1171 OSSL_PARAM rsa_params[3], *p = rsa_params;
1174 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1175 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1176 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1180 /* If key type not RSA return error */
1181 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1184 /* TODO(3.0): Remove this eventually when no more legacy */
1185 if (ctx->op.ciph.ciphprovctx == NULL)
1186 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1187 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1190 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1192 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1194 *p++ = OSSL_PARAM_construct_end();
1196 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1199 if (labellen > INT_MAX)
1202 return (int)labellen;
1205 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1207 OSSL_PARAM pad_params[2], *p = pad_params;
1210 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1211 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1215 /* If key type not RSA or RSA-PSS return error */
1216 if (ctx->pmeth != NULL
1217 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1218 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1221 /* TODO(3.0): Remove this eventually when no more legacy */
1222 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1223 || ctx->op.sig.sigprovctx == NULL)
1224 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1228 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1229 *p++ = OSSL_PARAM_construct_end();
1231 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1234 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1236 OSSL_PARAM pad_params[2], *p = pad_params;
1238 if (ctx == NULL || saltlen == NULL) {
1239 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1240 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1244 /* If key type not RSA or RSA-PSS return error */
1245 if (ctx->pmeth != NULL
1246 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1247 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1250 /* TODO(3.0): Remove this eventually when no more legacy */
1251 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1252 || ctx->op.sig.sigprovctx == NULL)
1253 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1254 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1258 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1259 *p++ = OSSL_PARAM_construct_end();
1261 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1268 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1270 OSSL_PARAM params[2], *p = params;
1271 size_t bits2 = bits;
1273 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1274 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1275 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1279 /* If key type not RSA return error */
1280 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1283 /* TODO(3.0): Remove this eventually when no more legacy */
1284 if (ctx->op.keymgmt.genctx == NULL)
1285 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1286 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
1288 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1289 *p++ = OSSL_PARAM_construct_end();
1291 if (!EVP_PKEY_CTX_set_params(ctx, params))
1297 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1299 OSSL_PARAM_BLD *tmpl;
1303 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1304 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1305 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1309 /* If key type not RSA return error */
1310 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1313 /* TODO(3.0): Remove this eventually when no more legacy */
1314 if (ctx->op.keymgmt.genctx == NULL)
1315 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1316 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1318 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
1320 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
1321 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
1322 OSSL_PARAM_BLD_free(tmpl);
1325 OSSL_PARAM_BLD_free(tmpl);
1327 ret = EVP_PKEY_CTX_set_params(ctx, params);
1328 OSSL_PARAM_BLD_free_params(params);
1332 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1334 OSSL_PARAM params[2], *p = params;
1335 size_t primes2 = primes;
1337 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1338 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1339 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1343 /* If key type not RSA return error */
1344 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1347 /* TODO(3.0): Remove this eventually when no more legacy */
1348 if (ctx->op.keymgmt.genctx == NULL)
1349 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1350 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
1353 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1354 *p++ = OSSL_PARAM_construct_end();
1356 if (!EVP_PKEY_CTX_set_params(ctx, params))