2 * Copyright 2006-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 "internal/constant_time.h"
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include <openssl/x509.h>
22 #include <openssl/rsa.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509v3.h>
26 #include <openssl/cms.h>
27 #include "crypto/evp.h"
28 #include "crypto/rsa.h"
29 #include "rsa_local.h"
31 /* RSA pkey context structure */
34 /* Key gen parameters */
38 /* Keygen callback info */
40 /* RSA padding mode */
44 /* message digest for MGF1 */
48 /* Minimum salt length or -1 if no PSS parameter restriction */
53 unsigned char *oaep_label
;
57 /* True if PSS parameters are restricted */
58 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
60 static int pkey_rsa_init(EVP_PKEY_CTX
*ctx
)
62 RSA_PKEY_CTX
*rctx
= OPENSSL_zalloc(sizeof(*rctx
));
67 rctx
->primes
= RSA_DEFAULT_PRIME_NUM
;
68 if (pkey_ctx_is_pss(ctx
))
69 rctx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
71 rctx
->pad_mode
= RSA_PKCS1_PADDING
;
72 /* Maximum for sign, auto for verify */
73 rctx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
74 rctx
->min_saltlen
= -1;
76 ctx
->keygen_info
= rctx
->gentmp
;
77 ctx
->keygen_info_count
= 2;
82 static int pkey_rsa_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
84 RSA_PKEY_CTX
*dctx
, *sctx
;
86 if (!pkey_rsa_init(dst
))
90 dctx
->nbits
= sctx
->nbits
;
92 dctx
->pub_exp
= BN_dup(sctx
->pub_exp
);
96 dctx
->pad_mode
= sctx
->pad_mode
;
98 dctx
->mgf1md
= sctx
->mgf1md
;
99 dctx
->saltlen
= sctx
->saltlen
;
100 if (sctx
->oaep_label
) {
101 OPENSSL_free(dctx
->oaep_label
);
102 dctx
->oaep_label
= OPENSSL_memdup(sctx
->oaep_label
, sctx
->oaep_labellen
);
103 if (!dctx
->oaep_label
)
105 dctx
->oaep_labellen
= sctx
->oaep_labellen
;
110 static int setup_tbuf(RSA_PKEY_CTX
*ctx
, EVP_PKEY_CTX
*pk
)
112 if (ctx
->tbuf
!= NULL
)
115 OPENSSL_malloc(RSA_size(EVP_PKEY_get0_RSA(pk
->pkey
)))) == NULL
) {
116 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
122 static void pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
124 RSA_PKEY_CTX
*rctx
= ctx
->data
;
126 BN_free(rctx
->pub_exp
);
127 OPENSSL_free(rctx
->tbuf
);
128 OPENSSL_free(rctx
->oaep_label
);
133 static int pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
,
134 size_t *siglen
, const unsigned char *tbs
,
138 RSA_PKEY_CTX
*rctx
= ctx
->data
;
140 * Discard const. Its marked as const because this may be a cached copy of
141 * the "real" key. These calls don't make any modifications that need to
142 * be reflected back in the "original" key.
144 RSA
*rsa
= (RSA
*)EVP_PKEY_get0_RSA(ctx
->pkey
);
147 if (tbslen
!= (size_t)EVP_MD_get_size(rctx
->md
)) {
148 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
152 if (EVP_MD_get_type(rctx
->md
) == NID_mdc2
) {
154 if (rctx
->pad_mode
!= RSA_PKCS1_PADDING
)
156 ret
= RSA_sign_ASN1_OCTET_STRING(0, tbs
, tbslen
, sig
, &sltmp
, rsa
);
161 } else if (rctx
->pad_mode
== RSA_X931_PADDING
) {
162 if ((size_t)RSA_size(rsa
) < tbslen
+ 1) {
163 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
166 if (!setup_tbuf(rctx
, ctx
)) {
167 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
170 memcpy(rctx
->tbuf
, tbs
, tbslen
);
171 rctx
->tbuf
[tbslen
] = RSA_X931_hash_id(EVP_MD_get_type(rctx
->md
));
172 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
,
173 sig
, rsa
, RSA_X931_PADDING
);
174 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
176 ret
= RSA_sign(EVP_MD_get_type(rctx
->md
),
177 tbs
, tbslen
, sig
, &sltmp
, rsa
);
181 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
182 if (!setup_tbuf(rctx
, ctx
))
184 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa
,
186 rctx
->md
, rctx
->mgf1md
,
189 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
190 sig
, rsa
, RSA_NO_PADDING
);
195 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, rsa
, rctx
->pad_mode
);
203 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX
*ctx
,
204 unsigned char *rout
, size_t *routlen
,
205 const unsigned char *sig
, size_t siglen
)
208 RSA_PKEY_CTX
*rctx
= ctx
->data
;
210 * Discard const. Its marked as const because this may be a cached copy of
211 * the "real" key. These calls don't make any modifications that need to
212 * be reflected back in the "original" key.
214 RSA
*rsa
= (RSA
*)EVP_PKEY_get0_RSA(ctx
->pkey
);
217 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
218 if (!setup_tbuf(rctx
, ctx
))
220 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
, rsa
,
225 if (rctx
->tbuf
[ret
] != RSA_X931_hash_id(EVP_MD_get_type(rctx
->md
))) {
226 ERR_raise(ERR_LIB_RSA
, RSA_R_ALGORITHM_MISMATCH
);
229 if (ret
!= EVP_MD_get_size(rctx
->md
)) {
230 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
234 memcpy(rout
, rctx
->tbuf
, ret
);
235 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
237 ret
= ossl_rsa_verify(EVP_MD_get_type(rctx
->md
),
238 NULL
, 0, rout
, &sltmp
,
247 ret
= RSA_public_decrypt(siglen
, sig
, rout
, rsa
, rctx
->pad_mode
);
255 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
256 const unsigned char *sig
, size_t siglen
,
257 const unsigned char *tbs
, size_t tbslen
)
259 RSA_PKEY_CTX
*rctx
= ctx
->data
;
261 * Discard const. Its marked as const because this may be a cached copy of
262 * the "real" key. These calls don't make any modifications that need to
263 * be reflected back in the "original" key.
265 RSA
*rsa
= (RSA
*)EVP_PKEY_get0_RSA(ctx
->pkey
);
269 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
270 return RSA_verify(EVP_MD_get_type(rctx
->md
), tbs
, tbslen
,
272 if (tbslen
!= (size_t)EVP_MD_get_size(rctx
->md
)) {
273 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
276 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
277 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
, siglen
) <= 0)
279 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
281 if (!setup_tbuf(rctx
, ctx
))
283 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
284 rsa
, RSA_NO_PADDING
);
287 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
,
288 rctx
->md
, rctx
->mgf1md
,
289 rctx
->tbuf
, rctx
->saltlen
);
297 if (!setup_tbuf(rctx
, ctx
))
299 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
300 rsa
, rctx
->pad_mode
);
305 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
312 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
,
313 unsigned char *out
, size_t *outlen
,
314 const unsigned char *in
, size_t inlen
)
317 RSA_PKEY_CTX
*rctx
= ctx
->data
;
319 * Discard const. Its marked as const because this may be a cached copy of
320 * the "real" key. These calls don't make any modifications that need to
321 * be reflected back in the "original" key.
323 RSA
*rsa
= (RSA
*)EVP_PKEY_get0_RSA(ctx
->pkey
);
325 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
326 int klen
= RSA_size(rsa
);
327 if (!setup_tbuf(rctx
, ctx
))
329 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx
->tbuf
, klen
,
333 rctx
->md
, rctx
->mgf1md
))
335 ret
= RSA_public_encrypt(klen
, rctx
->tbuf
, out
, rsa
, RSA_NO_PADDING
);
337 ret
= RSA_public_encrypt(inlen
, in
, out
, rsa
, rctx
->pad_mode
);
345 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
,
346 unsigned char *out
, size_t *outlen
,
347 const unsigned char *in
, size_t inlen
)
350 RSA_PKEY_CTX
*rctx
= ctx
->data
;
352 * Discard const. Its marked as const because this may be a cached copy of
353 * the "real" key. These calls don't make any modifications that need to
354 * be reflected back in the "original" key.
356 RSA
*rsa
= (RSA
*)EVP_PKEY_get0_RSA(ctx
->pkey
);
358 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
359 if (!setup_tbuf(rctx
, ctx
))
361 ret
= RSA_private_decrypt(inlen
, in
, rctx
->tbuf
, rsa
, RSA_NO_PADDING
);
364 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, ret
, rctx
->tbuf
,
368 rctx
->md
, rctx
->mgf1md
);
370 ret
= RSA_private_decrypt(inlen
, in
, out
, rsa
, rctx
->pad_mode
);
372 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
373 ret
= constant_time_select_int(constant_time_msb(ret
), ret
, 1);
377 static int check_padding_md(const EVP_MD
*md
, int padding
)
384 mdnid
= EVP_MD_get_type(md
);
386 if (padding
== RSA_NO_PADDING
) {
387 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
391 if (padding
== RSA_X931_PADDING
) {
392 if (RSA_X931_hash_id(mdnid
) == -1) {
393 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_X931_DIGEST
);
398 /* List of all supported RSA digests */
419 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST
);
428 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
430 RSA_PKEY_CTX
*rctx
= ctx
->data
;
433 case EVP_PKEY_CTRL_RSA_PADDING
:
434 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
)) {
435 if (!check_padding_md(rctx
->md
, p1
))
437 if (p1
== RSA_PKCS1_PSS_PADDING
) {
438 if (!(ctx
->operation
&
439 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
442 rctx
->md
= EVP_sha1();
443 } else if (pkey_ctx_is_pss(ctx
)) {
446 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
447 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
450 rctx
->md
= EVP_sha1();
456 ERR_raise(ERR_LIB_RSA
, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
459 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
460 *(int *)p2
= rctx
->pad_mode
;
463 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
464 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
465 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
466 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_SALTLEN
);
469 if (type
== EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
) {
470 *(int *)p2
= rctx
->saltlen
;
472 if (p1
< RSA_PSS_SALTLEN_MAX
)
474 if (rsa_pss_restricted(rctx
)) {
475 if (p1
== RSA_PSS_SALTLEN_AUTO
476 && ctx
->operation
== EVP_PKEY_OP_VERIFY
) {
477 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_SALTLEN
);
480 if ((p1
== RSA_PSS_SALTLEN_DIGEST
481 && rctx
->min_saltlen
> EVP_MD_get_size(rctx
->md
))
482 || (p1
>= 0 && p1
< rctx
->min_saltlen
)) {
483 ERR_raise(ERR_LIB_RSA
, RSA_R_PSS_SALTLEN_TOO_SMALL
);
491 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
492 if (p1
< RSA_MIN_MODULUS_BITS
) {
493 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
499 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
500 if (p2
== NULL
|| !BN_is_odd((BIGNUM
*)p2
) || BN_is_one((BIGNUM
*)p2
)) {
501 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
504 BN_free(rctx
->pub_exp
);
508 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
509 if (p1
< RSA_DEFAULT_PRIME_NUM
|| p1
> RSA_MAX_PRIME_NUM
) {
510 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_PRIME_NUM_INVALID
);
516 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
517 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
518 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
519 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
522 if (type
== EVP_PKEY_CTRL_GET_RSA_OAEP_MD
)
523 *(const EVP_MD
**)p2
= rctx
->md
;
528 case EVP_PKEY_CTRL_MD
:
529 if (!check_padding_md(p2
, rctx
->pad_mode
))
531 if (rsa_pss_restricted(rctx
)) {
532 if (EVP_MD_get_type(rctx
->md
) == EVP_MD_get_type(p2
))
534 ERR_raise(ERR_LIB_RSA
, RSA_R_DIGEST_NOT_ALLOWED
);
540 case EVP_PKEY_CTRL_GET_MD
:
541 *(const EVP_MD
**)p2
= rctx
->md
;
544 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
545 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
546 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
547 && rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
548 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_MGF1_MD
);
551 if (type
== EVP_PKEY_CTRL_GET_RSA_MGF1_MD
) {
553 *(const EVP_MD
**)p2
= rctx
->mgf1md
;
555 *(const EVP_MD
**)p2
= rctx
->md
;
557 if (rsa_pss_restricted(rctx
)) {
558 if (EVP_MD_get_type(rctx
->mgf1md
) == EVP_MD_get_type(p2
))
560 ERR_raise(ERR_LIB_RSA
, RSA_R_MGF1_DIGEST_NOT_ALLOWED
);
567 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
568 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
569 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
572 OPENSSL_free(rctx
->oaep_label
);
574 rctx
->oaep_label
= p2
;
575 rctx
->oaep_labellen
= p1
;
577 rctx
->oaep_label
= NULL
;
578 rctx
->oaep_labellen
= 0;
582 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
583 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
584 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
587 *(unsigned char **)p2
= rctx
->oaep_label
;
588 return rctx
->oaep_labellen
;
590 case EVP_PKEY_CTRL_DIGESTINIT
:
591 case EVP_PKEY_CTRL_PKCS7_SIGN
:
592 #ifndef OPENSSL_NO_CMS
593 case EVP_PKEY_CTRL_CMS_SIGN
:
597 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
598 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
599 #ifndef OPENSSL_NO_CMS
600 case EVP_PKEY_CTRL_CMS_DECRYPT
:
601 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
603 if (!pkey_ctx_is_pss(ctx
))
606 case EVP_PKEY_CTRL_PEER_KEY
:
607 ERR_raise(ERR_LIB_RSA
, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
616 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
617 const char *type
, const char *value
)
620 ERR_raise(ERR_LIB_RSA
, RSA_R_VALUE_MISSING
);
623 if (strcmp(type
, "rsa_padding_mode") == 0) {
626 if (strcmp(value
, "pkcs1") == 0) {
627 pm
= RSA_PKCS1_PADDING
;
628 } else if (strcmp(value
, "none") == 0) {
630 } else if (strcmp(value
, "oeap") == 0) {
631 pm
= RSA_PKCS1_OAEP_PADDING
;
632 } else if (strcmp(value
, "oaep") == 0) {
633 pm
= RSA_PKCS1_OAEP_PADDING
;
634 } else if (strcmp(value
, "x931") == 0) {
635 pm
= RSA_X931_PADDING
;
636 } else if (strcmp(value
, "pss") == 0) {
637 pm
= RSA_PKCS1_PSS_PADDING
;
639 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
642 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
645 if (strcmp(type
, "rsa_pss_saltlen") == 0) {
648 if (!strcmp(value
, "digest"))
649 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
650 else if (!strcmp(value
, "max"))
651 saltlen
= RSA_PSS_SALTLEN_MAX
;
652 else if (!strcmp(value
, "auto"))
653 saltlen
= RSA_PSS_SALTLEN_AUTO
;
655 saltlen
= atoi(value
);
656 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
659 if (strcmp(type
, "rsa_keygen_bits") == 0) {
660 int nbits
= atoi(value
);
662 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
665 if (strcmp(type
, "rsa_keygen_pubexp") == 0) {
668 BIGNUM
*pubexp
= NULL
;
669 if (!BN_asc2bn(&pubexp
, value
))
671 ret
= EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx
, pubexp
);
676 if (strcmp(type
, "rsa_keygen_primes") == 0) {
677 int nprimes
= atoi(value
);
679 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, nprimes
);
682 if (strcmp(type
, "rsa_mgf1_md") == 0)
683 return EVP_PKEY_CTX_md(ctx
,
684 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
685 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
687 if (pkey_ctx_is_pss(ctx
)) {
689 if (strcmp(type
, "rsa_pss_keygen_mgf1_md") == 0)
690 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
691 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
693 if (strcmp(type
, "rsa_pss_keygen_md") == 0)
694 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
695 EVP_PKEY_CTRL_MD
, value
);
697 if (strcmp(type
, "rsa_pss_keygen_saltlen") == 0) {
698 int saltlen
= atoi(value
);
700 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx
, saltlen
);
704 if (strcmp(type
, "rsa_oaep_md") == 0)
705 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_CRYPT
,
706 EVP_PKEY_CTRL_RSA_OAEP_MD
, value
);
708 if (strcmp(type
, "rsa_oaep_label") == 0) {
713 lab
= OPENSSL_hexstr2buf(value
, &lablen
);
716 ret
= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, lab
, lablen
);
725 /* Set PSS parameters when generating a key, if necessary */
726 static int rsa_set_pss_param(RSA
*rsa
, EVP_PKEY_CTX
*ctx
)
728 RSA_PKEY_CTX
*rctx
= ctx
->data
;
730 if (!pkey_ctx_is_pss(ctx
))
732 /* If all parameters are default values don't set pss */
733 if (rctx
->md
== NULL
&& rctx
->mgf1md
== NULL
&& rctx
->saltlen
== -2)
735 rsa
->pss
= ossl_rsa_pss_params_create(rctx
->md
, rctx
->mgf1md
,
737 ? 0 : rctx
->saltlen
);
738 if (rsa
->pss
== NULL
)
743 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
746 RSA_PKEY_CTX
*rctx
= ctx
->data
;
750 if (rctx
->pub_exp
== NULL
) {
751 rctx
->pub_exp
= BN_new();
752 if (rctx
->pub_exp
== NULL
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
758 if (ctx
->pkey_gencb
) {
759 pcb
= BN_GENCB_new();
764 evp_pkey_set_cb_translate(pcb
, ctx
);
768 ret
= RSA_generate_multi_prime_key(rsa
, rctx
->nbits
, rctx
->primes
,
771 if (ret
> 0 && !rsa_set_pss_param(rsa
, ctx
)) {
776 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, rsa
);
782 static const EVP_PKEY_METHOD rsa_pkey_meth
= {
784 EVP_PKEY_FLAG_AUTOARGLEN
,
801 pkey_rsa_verifyrecover
,
817 const EVP_PKEY_METHOD
*ossl_rsa_pkey_method(void)
819 return &rsa_pkey_meth
;
823 * Called for PSS sign or verify initialisation: checks PSS parameter
824 * sanity and sets any restrictions on key usage.
827 static int pkey_pss_init(EVP_PKEY_CTX
*ctx
)
830 RSA_PKEY_CTX
*rctx
= ctx
->data
;
832 const EVP_MD
*mgf1md
;
833 int min_saltlen
, max_saltlen
;
835 /* Should never happen */
836 if (!pkey_ctx_is_pss(ctx
))
838 rsa
= EVP_PKEY_get0_RSA(ctx
->pkey
);
839 /* If no restrictions just return */
840 if (rsa
->pss
== NULL
)
842 /* Get and check parameters */
843 if (!ossl_rsa_pss_get_param(rsa
->pss
, &md
, &mgf1md
, &min_saltlen
))
846 /* See if minimum salt length exceeds maximum possible */
847 max_saltlen
= RSA_size(rsa
) - EVP_MD_get_size(md
);
848 if ((RSA_bits(rsa
) & 0x7) == 1)
850 if (min_saltlen
> max_saltlen
) {
851 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_SALT_LENGTH
);
855 rctx
->min_saltlen
= min_saltlen
;
858 * Set PSS restrictions as defaults: we can then block any attempt to
859 * use invalid values in pkey_rsa_ctrl
863 rctx
->mgf1md
= mgf1md
;
864 rctx
->saltlen
= min_saltlen
;
869 static const EVP_PKEY_METHOD rsa_pss_pkey_meth
= {
871 EVP_PKEY_FLAG_AUTOARGLEN
,
887 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
893 const EVP_PKEY_METHOD
*ossl_rsa_pss_pkey_method(void)
895 return &rsa_pss_pkey_meth
;