2 * Copyright 1995-2021 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"
16 #include <openssl/crypto.h>
17 #include <openssl/core_names.h>
18 #include <openssl/engine.h>
19 #include <openssl/evp.h>
20 #include <openssl/param_build.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, OSSL_LIB_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 *ossl_rsa_new_with_ctx(OSSL_LIB_CTX *libctx)
70 return rsa_new_intern(NULL, libctx);
73 static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
75 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
78 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
83 ret->lock = CRYPTO_THREAD_lock_new();
84 if (ret->lock == NULL) {
85 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
91 ret->meth = RSA_get_default_method();
92 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
93 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
95 if (!ENGINE_init(engine)) {
96 ERR_raise(ERR_LIB_RSA, 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 ERR_raise(ERR_LIB_RSA, 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 ERR_raise(ERR_LIB_RSA, 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_MODULE)
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);
165 #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
166 rsa_acvp_test_free(r->acvp_test);
170 RSA_PSS_PARAMS_free(r->pss);
171 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
173 BN_BLINDING_free(r->blinding);
174 BN_BLINDING_free(r->mt_blinding);
175 OPENSSL_free(r->bignum_data);
179 int RSA_up_ref(RSA *r)
183 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
186 REF_PRINT_COUNT("RSA", r);
187 REF_ASSERT_ISNT(i < 2);
188 return i > 1 ? 1 : 0;
191 OSSL_LIB_CTX *ossl_rsa_get0_libctx(RSA *r)
196 void ossl_rsa_set0_libctx(RSA *r, OSSL_LIB_CTX *libctx)
202 int RSA_set_ex_data(RSA *r, int idx, void *arg)
204 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
207 void *RSA_get_ex_data(const RSA *r, int idx)
209 return CRYPTO_get_ex_data(&r->ex_data, idx);
214 * Define a scaling constant for our fixed point arithmetic.
215 * This value must be a power of two because the base two logarithm code
216 * makes this assumption. The exponent must also be a multiple of three so
217 * that the scale factor has an exact cube root. Finally, the scale factor
218 * should not be so large that a multiplication of two scaled numbers
219 * overflows a 64 bit unsigned integer.
221 static const unsigned int scale = 1 << 18;
222 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
224 /* Define some constants, none exceed 32 bits */
225 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
226 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
227 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
228 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
231 * Multiply two scaled integers together and rescale the result.
233 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
235 return a * b / scale;
239 * Calculate the cube root of a 64 bit scaled integer.
240 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
241 * integer, this is not guaranteed after scaling, so this function has a
242 * 64 bit return. This uses the shifting nth root algorithm with some
243 * algebraic simplifications.
245 static uint64_t icbrt64(uint64_t x)
251 for (s = 63; s >= 0; s -= 3) {
253 b = 3 * r * (r + 1) + 1;
259 return r * cbrt_scale;
263 * Calculate the natural logarithm of a 64 bit scaled integer.
264 * This is done by calculating a base two logarithm and scaling.
265 * The maximum logarithm (base 2) is 64 and this reduces base e, so
266 * a 32 bit result should not overflow. The argument passed must be
267 * greater than unity so we don't need to handle negative results.
269 static uint32_t ilog_e(uint64_t v)
274 * Scale down the value into the range 1 .. 2.
276 * If fractional numbers need to be processed, another loop needs
277 * to go here that checks v < scale and if so multiplies it by 2 and
278 * reduces r by scale. This also means making r signed.
280 while (v >= 2 * scale) {
284 for (i = scale / 2; i != 0; i /= 2) {
286 if (v >= 2 * scale) {
291 r = (r * (uint64_t)scale) / log_e;
296 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
299 * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
300 * for FFC safe prime groups for modp and ffdhe.
301 * After Table 25 and Table 26 it refers to
302 * "The maximum security strength estimates were calculated using the formula in
303 * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
308 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
309 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
310 * The two cube roots are merged together here.
312 uint16_t ossl_ifc_ffc_compute_security_bits(int n)
318 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
332 * The first incorrect result (i.e. not accurate or off by one low) occurs
333 * for n = 699668. The true value here is 1200. Instead of using this n
334 * as the check threshold, the smallest n such that the correct result is
335 * 1200 is used instead.
342 x = n * (uint64_t)log_2;
344 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
351 int RSA_security_bits(const RSA *rsa)
353 int bits = BN_num_bits(rsa->n);
356 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
357 /* This ought to mean that we have private key at hand. */
358 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
360 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
364 return ossl_ifc_ffc_compute_security_bits(bits);
367 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
369 /* If the fields n and e in r are NULL, the corresponding input
370 * parameters MUST be non-NULL for n and e. d may be
371 * left NULL (in case only the public key is used).
373 if ((r->n == NULL && n == NULL)
374 || (r->e == NULL && e == NULL))
388 BN_set_flags(r->d, BN_FLG_CONSTTIME);
395 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
397 /* If the fields p and q in r are NULL, the corresponding input
398 * parameters MUST be non-NULL.
400 if ((r->p == NULL && p == NULL)
401 || (r->q == NULL && q == NULL))
407 BN_set_flags(r->p, BN_FLG_CONSTTIME);
412 BN_set_flags(r->q, BN_FLG_CONSTTIME);
419 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
421 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
422 * parameters MUST be non-NULL.
424 if ((r->dmp1 == NULL && dmp1 == NULL)
425 || (r->dmq1 == NULL && dmq1 == NULL)
426 || (r->iqmp == NULL && iqmp == NULL))
430 BN_clear_free(r->dmp1);
432 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
435 BN_clear_free(r->dmq1);
437 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
440 BN_clear_free(r->iqmp);
442 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
451 * Is it better to export RSA_PRIME_INFO structure
452 * and related functions to let user pass a triplet?
454 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
455 BIGNUM *coeffs[], int pnum)
457 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
458 RSA_PRIME_INFO *pinfo;
461 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
464 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
465 if (prime_infos == NULL)
468 if (r->prime_infos != NULL)
469 old = r->prime_infos;
471 for (i = 0; i < pnum; i++) {
472 pinfo = rsa_multip_info_new();
475 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
476 BN_clear_free(pinfo->r);
477 BN_clear_free(pinfo->d);
478 BN_clear_free(pinfo->t);
479 pinfo->r = primes[i];
481 pinfo->t = coeffs[i];
482 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
483 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
484 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
486 rsa_multip_info_free(pinfo);
489 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
492 r->prime_infos = prime_infos;
494 if (!rsa_multip_calc_product(r)) {
495 r->prime_infos = old;
501 * This is hard to deal with, since the old infos could
502 * also be set by this function and r, d, t should not
503 * be freed in that case. So currently, stay consistent
504 * with other *set0* functions: just free it...
506 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
509 r->version = RSA_ASN1_VERSION_MULTI;
514 /* r, d, t should not be freed */
515 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
520 void RSA_get0_key(const RSA *r,
521 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
531 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
540 int RSA_get_multi_prime_extra_count(const RSA *r)
544 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
550 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
553 RSA_PRIME_INFO *pinfo;
555 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
559 * return other primes
560 * it's caller's responsibility to allocate oth_primes[pnum]
562 for (i = 0; i < pnum; i++) {
563 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
564 primes[i] = pinfo->r;
571 void RSA_get0_crt_params(const RSA *r,
572 const BIGNUM **dmp1, const BIGNUM **dmq1,
584 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
585 const BIGNUM *coeffs[])
589 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
592 /* return other primes */
593 if (exps != NULL || coeffs != NULL) {
594 RSA_PRIME_INFO *pinfo;
597 /* it's the user's job to guarantee the buffer length */
598 for (i = 0; i < pnum; i++) {
599 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
603 coeffs[i] = pinfo->t;
611 const BIGNUM *RSA_get0_n(const RSA *r)
616 const BIGNUM *RSA_get0_e(const RSA *r)
621 const BIGNUM *RSA_get0_d(const RSA *r)
626 const BIGNUM *RSA_get0_p(const RSA *r)
631 const BIGNUM *RSA_get0_q(const RSA *r)
636 const BIGNUM *RSA_get0_dmp1(const RSA *r)
641 const BIGNUM *RSA_get0_dmq1(const RSA *r)
646 const BIGNUM *RSA_get0_iqmp(const RSA *r)
651 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
661 RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r)
663 return &r->pss_params;
666 void RSA_clear_flags(RSA *r, int flags)
671 int RSA_test_flags(const RSA *r, int flags)
673 return r->flags & flags;
676 void RSA_set_flags(RSA *r, int flags)
681 int RSA_get_version(RSA *r)
683 /* { two-prime(0), multi(1) } */
688 ENGINE *RSA_get0_engine(const RSA *r)
693 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
695 /* If key type not RSA or RSA-PSS return error */
696 if (ctx != NULL && ctx->pmeth != NULL
697 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
698 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
700 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
704 DEFINE_STACK_OF(BIGNUM)
706 int ossl_rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
707 const STACK_OF(BIGNUM) *exps,
708 const STACK_OF(BIGNUM) *coeffs)
711 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
715 if (primes == NULL || exps == NULL || coeffs == NULL)
718 pnum = sk_BIGNUM_num(primes);
720 || pnum != sk_BIGNUM_num(exps)
721 || pnum != sk_BIGNUM_num(coeffs) + 1)
724 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
725 sk_BIGNUM_value(primes, 1))
726 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
727 sk_BIGNUM_value(exps, 1),
728 sk_BIGNUM_value(coeffs, 0)))
732 old_infos = r->prime_infos;
739 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
740 if (prime_infos == NULL)
743 for (i = 2; i < pnum; i++) {
744 BIGNUM *prime = sk_BIGNUM_value(primes, i);
745 BIGNUM *exp = sk_BIGNUM_value(exps, i);
746 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
747 RSA_PRIME_INFO *pinfo = NULL;
749 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
752 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
753 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
754 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
761 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
762 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
763 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
764 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
767 r->prime_infos = prime_infos;
769 if (!rsa_multip_calc_product(r)) {
770 r->prime_infos = old_infos;
779 if (old_infos != NULL) {
781 * This is hard to deal with, since the old infos could
782 * also be set by this function and r, d, t should not
783 * be freed in that case. So currently, stay consistent
784 * with other *set0* functions: just free it...
786 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
790 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
796 /* r, d, t should not be freed */
797 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
802 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
804 int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
805 STACK_OF(BIGNUM_const) *exps,
806 STACK_OF(BIGNUM_const) *coeffs)
809 RSA_PRIME_INFO *pinfo;
816 /* If |p| is NULL, there are no CRT parameters */
817 if (RSA_get0_p(r) == NULL)
820 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
821 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
822 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
823 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
824 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
827 pnum = RSA_get_multi_prime_extra_count(r);
828 for (i = 0; i < pnum; i++) {
829 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
830 sk_BIGNUM_const_push(primes, pinfo->r);
831 sk_BIGNUM_const_push(exps, pinfo->d);
832 sk_BIGNUM_const_push(coeffs, pinfo->t);
840 /* Helpers to set or get diverse hash algorithm names */
841 static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx,
843 int keytype, int optype,
844 /* For EVP_PKEY_CTX_set_params() */
845 const char *mdkey, const char *mdname,
846 const char *propkey, const char *mdprops)
848 OSSL_PARAM params[3], *p = params;
850 if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
851 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
852 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
856 /* If key type not RSA return error */
859 if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
860 && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
864 if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
869 /* Cast away the const. This is read only so should be safe */
870 *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0);
871 if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) {
872 /* Cast away the const. This is read only so should be safe */
873 *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0);
875 *p++ = OSSL_PARAM_construct_end();
877 return evp_pkey_ctx_set_params_strict(ctx, params);
880 /* Helpers to set or get diverse hash algorithm names */
881 static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx,
883 int keytype, int optype,
884 /* For EVP_PKEY_CTX_get_params() */
886 char *mdname, size_t mdnamesize)
888 OSSL_PARAM params[2], *p = params;
890 if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
891 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
892 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
896 /* If key type not RSA return error */
899 if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
900 && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
904 if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
909 /* Cast away the const. This is read only so should be safe */
910 *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize);
911 *p++ = OSSL_PARAM_construct_end();
913 return evp_pkey_ctx_get_params_strict(ctx, params);
917 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
918 * simply because that's easier.
919 * TODO(3.0) Should this be deprecated?
921 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
923 return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING,
928 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
929 * simply because that's easier.
930 * TODO(3.0) Should this be deprecated?
932 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
934 return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
939 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
940 * simply because that's easier.
941 * TODO(3.0) Should this be deprecated in favor of passing a name?
943 int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
945 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
946 EVP_PKEY_CTRL_MD, 0, (void *)(md));
949 int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx,
953 return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
954 OSSL_PKEY_PARAM_RSA_DIGEST, mdname,
955 OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops);
959 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
960 * simply because that's easier.
961 * TODO(3.0) Should this be deprecated in favor of passing a name?
963 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
965 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
966 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
969 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
973 int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
974 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
975 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
978 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
981 return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
982 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
987 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
988 * simply because that's easier.
989 * TODO(3.0) Should this be deprecated in favor of getting a name?
991 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
993 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
994 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
998 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
999 * simply because that's easier.
1000 * TODO(3.0) Should this be deprecated in favor of passing a name?
1002 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1004 return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1005 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1008 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1009 const char *mdprops)
1011 return int_set_rsa_md_name(ctx, -1,
1012 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1013 OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1014 OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
1017 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1020 return int_get_rsa_md_name(ctx, -1,
1021 EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
1022 OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
1026 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1027 * simply because that's easier.
1028 * TODO(3.0) Should this be deprecated in favor of passing a name?
1030 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
1032 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1033 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
1036 int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
1039 return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
1040 OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
1045 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1046 * simply because that's easier.
1047 * TODO(3.0) Should this be deprecated in favor of getting a name?
1049 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1051 return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1052 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
1055 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1057 OSSL_PARAM rsa_params[2], *p = rsa_params;
1059 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1060 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1061 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1065 /* If key type not RSA return error */
1066 if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1069 /* Cast away the const. This is read only so should be safe */
1070 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1071 (void *)label, (size_t)llen);
1072 *p++ = OSSL_PARAM_construct_end();
1074 if (!evp_pkey_ctx_set_params_strict(ctx, rsa_params))
1077 /* Ownership is supposed to be transfered to the callee. */
1078 OPENSSL_free(label);
1082 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1084 OSSL_PARAM rsa_params[2], *p = rsa_params;
1087 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1088 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1089 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1093 /* If key type not RSA return error */
1094 if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
1097 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1099 *p++ = OSSL_PARAM_construct_end();
1101 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1104 labellen = rsa_params[0].return_size;
1105 if (labellen > INT_MAX)
1108 return (int)labellen;
1112 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1113 * simply because that's easier.
1115 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1118 * For some reason, the optype was set to this:
1120 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1122 * However, we do use RSA-PSS with the whole gamut of diverse signature
1123 * and verification operations, so the optype gets upgraded to this:
1125 * EVP_PKEY_OP_TYPE_SIG
1127 return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1128 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
1132 * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
1133 * simply because that's easier.
1135 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1138 * Because of circumstances, the optype is updated from:
1140 * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
1144 * EVP_PKEY_OP_TYPE_SIG
1146 return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
1147 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
1150 int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1152 OSSL_PARAM pad_params[2], *p = pad_params;
1154 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1155 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1156 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1160 if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1163 *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
1165 *p++ = OSSL_PARAM_construct_end();
1167 return evp_pkey_ctx_set_params_strict(ctx, pad_params);
1170 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
1172 OSSL_PARAM params[2], *p = params;
1173 size_t bits2 = bits;
1175 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1176 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1177 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1181 /* If key type not RSA return error */
1182 if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1183 && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1186 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
1187 *p++ = OSSL_PARAM_construct_end();
1189 return evp_pkey_ctx_set_params_strict(ctx, params);
1192 int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1194 int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
1195 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1198 * Satisfy memory semantics for pre-3.0 callers of
1199 * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
1200 * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
1202 if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
1203 BN_free(ctx->rsa_pubexp);
1204 ctx->rsa_pubexp = pubexp;
1210 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
1215 * When we're dealing with a provider, there's no need to duplicate
1216 * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
1218 if (evp_pkey_ctx_is_legacy(ctx))
1219 pubexp = BN_dup(pubexp);
1220 ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
1221 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
1222 if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
1227 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
1229 OSSL_PARAM params[2], *p = params;
1230 size_t primes2 = primes;
1232 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1233 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1234 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1238 /* If key type not RSA return error */
1239 if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
1240 && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
1243 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
1244 *p++ = OSSL_PARAM_construct_end();
1246 return evp_pkey_ctx_set_params_strict(ctx, params);