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
)
114 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(pk
->pkey
->pkey
.rsa
))) == NULL
) {
115 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
121 static void pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
123 RSA_PKEY_CTX
*rctx
= ctx
->data
;
125 BN_free(rctx
->pub_exp
);
126 OPENSSL_free(rctx
->tbuf
);
127 OPENSSL_free(rctx
->oaep_label
);
132 static int pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
,
133 size_t *siglen
, const unsigned char *tbs
,
137 RSA_PKEY_CTX
*rctx
= ctx
->data
;
138 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
141 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
142 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
146 if (EVP_MD_type(rctx
->md
) == NID_mdc2
) {
148 if (rctx
->pad_mode
!= RSA_PKCS1_PADDING
)
150 ret
= RSA_sign_ASN1_OCTET_STRING(0,
151 tbs
, tbslen
, sig
, &sltmp
, rsa
);
156 } else if (rctx
->pad_mode
== RSA_X931_PADDING
) {
157 if ((size_t)RSA_size(rsa
) < tbslen
+ 1) {
158 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
161 if (!setup_tbuf(rctx
, ctx
)) {
162 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
165 memcpy(rctx
->tbuf
, tbs
, tbslen
);
166 rctx
->tbuf
[tbslen
] = RSA_X931_hash_id(EVP_MD_type(rctx
->md
));
167 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
,
168 sig
, rsa
, RSA_X931_PADDING
);
169 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
171 ret
= RSA_sign(EVP_MD_type(rctx
->md
),
172 tbs
, tbslen
, sig
, &sltmp
, rsa
);
176 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
177 if (!setup_tbuf(rctx
, ctx
))
179 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa
,
181 rctx
->md
, rctx
->mgf1md
,
184 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
185 sig
, rsa
, RSA_NO_PADDING
);
190 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, ctx
->pkey
->pkey
.rsa
,
199 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX
*ctx
,
200 unsigned char *rout
, size_t *routlen
,
201 const unsigned char *sig
, size_t siglen
)
204 RSA_PKEY_CTX
*rctx
= ctx
->data
;
207 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
208 if (!setup_tbuf(rctx
, ctx
))
210 ret
= RSA_public_decrypt(siglen
, sig
,
211 rctx
->tbuf
, ctx
->pkey
->pkey
.rsa
,
216 if (rctx
->tbuf
[ret
] != RSA_X931_hash_id(EVP_MD_type(rctx
->md
))) {
217 ERR_raise(ERR_LIB_RSA
, RSA_R_ALGORITHM_MISMATCH
);
220 if (ret
!= EVP_MD_size(rctx
->md
)) {
221 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
225 memcpy(rout
, rctx
->tbuf
, ret
);
226 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
228 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
),
229 NULL
, 0, rout
, &sltmp
,
230 sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
238 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
247 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
248 const unsigned char *sig
, size_t siglen
,
249 const unsigned char *tbs
, size_t tbslen
)
251 RSA_PKEY_CTX
*rctx
= ctx
->data
;
252 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
256 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
257 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
259 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
260 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST_LENGTH
);
263 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
264 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
, siglen
) <= 0)
266 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
268 if (!setup_tbuf(rctx
, ctx
))
270 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
271 rsa
, RSA_NO_PADDING
);
274 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
,
275 rctx
->md
, rctx
->mgf1md
,
276 rctx
->tbuf
, rctx
->saltlen
);
284 if (!setup_tbuf(rctx
, ctx
))
286 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
287 rsa
, rctx
->pad_mode
);
292 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
299 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
,
300 unsigned char *out
, size_t *outlen
,
301 const unsigned char *in
, size_t inlen
)
304 RSA_PKEY_CTX
*rctx
= ctx
->data
;
306 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
307 int klen
= RSA_size(ctx
->pkey
->pkey
.rsa
);
308 if (!setup_tbuf(rctx
, ctx
))
310 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx
->tbuf
, klen
,
314 rctx
->md
, rctx
->mgf1md
))
316 ret
= RSA_public_encrypt(klen
, rctx
->tbuf
, out
,
317 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
319 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
328 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
,
329 unsigned char *out
, size_t *outlen
,
330 const unsigned char *in
, size_t inlen
)
333 RSA_PKEY_CTX
*rctx
= ctx
->data
;
335 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
336 if (!setup_tbuf(rctx
, ctx
))
338 ret
= RSA_private_decrypt(inlen
, in
, rctx
->tbuf
,
339 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
342 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, ret
, rctx
->tbuf
,
346 rctx
->md
, rctx
->mgf1md
);
348 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
351 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
352 ret
= constant_time_select_int(constant_time_msb(ret
), ret
, 1);
356 static int check_padding_md(const EVP_MD
*md
, int padding
)
363 mdnid
= EVP_MD_type(md
);
365 if (padding
== RSA_NO_PADDING
) {
366 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
370 if (padding
== RSA_X931_PADDING
) {
371 if (RSA_X931_hash_id(mdnid
) == -1) {
372 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_X931_DIGEST
);
377 /* List of all supported RSA digests */
398 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_DIGEST
);
407 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
409 RSA_PKEY_CTX
*rctx
= ctx
->data
;
412 case EVP_PKEY_CTRL_RSA_PADDING
:
413 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
)) {
414 if (!check_padding_md(rctx
->md
, p1
))
416 if (p1
== RSA_PKCS1_PSS_PADDING
) {
417 if (!(ctx
->operation
&
418 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
421 rctx
->md
= EVP_sha1();
422 } else if (pkey_ctx_is_pss(ctx
)) {
425 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
426 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
429 rctx
->md
= EVP_sha1();
435 ERR_raise(ERR_LIB_RSA
, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
438 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
439 *(int *)p2
= rctx
->pad_mode
;
442 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
443 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
444 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
445 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_SALTLEN
);
448 if (type
== EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
) {
449 *(int *)p2
= rctx
->saltlen
;
451 if (p1
< RSA_PSS_SALTLEN_MAX
)
453 if (rsa_pss_restricted(rctx
)) {
454 if (p1
== RSA_PSS_SALTLEN_AUTO
455 && ctx
->operation
== EVP_PKEY_OP_VERIFY
) {
456 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PSS_SALTLEN
);
459 if ((p1
== RSA_PSS_SALTLEN_DIGEST
460 && rctx
->min_saltlen
> EVP_MD_size(rctx
->md
))
461 || (p1
>= 0 && p1
< rctx
->min_saltlen
)) {
462 ERR_raise(ERR_LIB_RSA
, RSA_R_PSS_SALTLEN_TOO_SMALL
);
470 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
471 if (p1
< RSA_MIN_MODULUS_BITS
) {
472 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_SIZE_TOO_SMALL
);
478 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
479 if (p2
== NULL
|| !BN_is_odd((BIGNUM
*)p2
) || BN_is_one((BIGNUM
*)p2
)) {
480 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
483 BN_free(rctx
->pub_exp
);
487 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
488 if (p1
< RSA_DEFAULT_PRIME_NUM
|| p1
> RSA_MAX_PRIME_NUM
) {
489 ERR_raise(ERR_LIB_RSA
, RSA_R_KEY_PRIME_NUM_INVALID
);
495 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
496 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
497 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
498 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
501 if (type
== EVP_PKEY_CTRL_GET_RSA_OAEP_MD
)
502 *(const EVP_MD
**)p2
= rctx
->md
;
507 case EVP_PKEY_CTRL_MD
:
508 if (!check_padding_md(p2
, rctx
->pad_mode
))
510 if (rsa_pss_restricted(rctx
)) {
511 if (EVP_MD_type(rctx
->md
) == EVP_MD_type(p2
))
513 ERR_raise(ERR_LIB_RSA
, RSA_R_DIGEST_NOT_ALLOWED
);
519 case EVP_PKEY_CTRL_GET_MD
:
520 *(const EVP_MD
**)p2
= rctx
->md
;
523 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
524 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
525 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
526 && rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
527 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_MGF1_MD
);
530 if (type
== EVP_PKEY_CTRL_GET_RSA_MGF1_MD
) {
532 *(const EVP_MD
**)p2
= rctx
->mgf1md
;
534 *(const EVP_MD
**)p2
= rctx
->md
;
536 if (rsa_pss_restricted(rctx
)) {
537 if (EVP_MD_type(rctx
->mgf1md
) == EVP_MD_type(p2
))
539 ERR_raise(ERR_LIB_RSA
, RSA_R_MGF1_DIGEST_NOT_ALLOWED
);
546 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
547 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
548 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
551 OPENSSL_free(rctx
->oaep_label
);
553 rctx
->oaep_label
= p2
;
554 rctx
->oaep_labellen
= p1
;
556 rctx
->oaep_label
= NULL
;
557 rctx
->oaep_labellen
= 0;
561 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
562 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
563 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_PADDING_MODE
);
566 *(unsigned char **)p2
= rctx
->oaep_label
;
567 return rctx
->oaep_labellen
;
569 case EVP_PKEY_CTRL_DIGESTINIT
:
570 case EVP_PKEY_CTRL_PKCS7_SIGN
:
571 #ifndef OPENSSL_NO_CMS
572 case EVP_PKEY_CTRL_CMS_SIGN
:
576 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
577 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
578 #ifndef OPENSSL_NO_CMS
579 case EVP_PKEY_CTRL_CMS_DECRYPT
:
580 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
582 if (!pkey_ctx_is_pss(ctx
))
585 case EVP_PKEY_CTRL_PEER_KEY
:
586 ERR_raise(ERR_LIB_RSA
, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
595 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
596 const char *type
, const char *value
)
599 ERR_raise(ERR_LIB_RSA
, RSA_R_VALUE_MISSING
);
602 if (strcmp(type
, "rsa_padding_mode") == 0) {
605 if (strcmp(value
, "pkcs1") == 0) {
606 pm
= RSA_PKCS1_PADDING
;
607 } else if (strcmp(value
, "none") == 0) {
609 } else if (strcmp(value
, "oeap") == 0) {
610 pm
= RSA_PKCS1_OAEP_PADDING
;
611 } else if (strcmp(value
, "oaep") == 0) {
612 pm
= RSA_PKCS1_OAEP_PADDING
;
613 } else if (strcmp(value
, "x931") == 0) {
614 pm
= RSA_X931_PADDING
;
615 } else if (strcmp(value
, "pss") == 0) {
616 pm
= RSA_PKCS1_PSS_PADDING
;
618 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
621 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
624 if (strcmp(type
, "rsa_pss_saltlen") == 0) {
627 if (!strcmp(value
, "digest"))
628 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
629 else if (!strcmp(value
, "max"))
630 saltlen
= RSA_PSS_SALTLEN_MAX
;
631 else if (!strcmp(value
, "auto"))
632 saltlen
= RSA_PSS_SALTLEN_AUTO
;
634 saltlen
= atoi(value
);
635 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
638 if (strcmp(type
, "rsa_keygen_bits") == 0) {
639 int nbits
= atoi(value
);
641 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
644 if (strcmp(type
, "rsa_keygen_pubexp") == 0) {
647 BIGNUM
*pubexp
= NULL
;
648 if (!BN_asc2bn(&pubexp
, value
))
650 ret
= EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx
, pubexp
);
655 if (strcmp(type
, "rsa_keygen_primes") == 0) {
656 int nprimes
= atoi(value
);
658 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, nprimes
);
661 if (strcmp(type
, "rsa_mgf1_md") == 0)
662 return EVP_PKEY_CTX_md(ctx
,
663 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
664 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
666 if (pkey_ctx_is_pss(ctx
)) {
668 if (strcmp(type
, "rsa_pss_keygen_mgf1_md") == 0)
669 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
670 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
672 if (strcmp(type
, "rsa_pss_keygen_md") == 0)
673 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
674 EVP_PKEY_CTRL_MD
, value
);
676 if (strcmp(type
, "rsa_pss_keygen_saltlen") == 0) {
677 int saltlen
= atoi(value
);
679 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx
, saltlen
);
683 if (strcmp(type
, "rsa_oaep_md") == 0)
684 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_CRYPT
,
685 EVP_PKEY_CTRL_RSA_OAEP_MD
, value
);
687 if (strcmp(type
, "rsa_oaep_label") == 0) {
692 lab
= OPENSSL_hexstr2buf(value
, &lablen
);
695 ret
= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, lab
, lablen
);
704 /* Set PSS parameters when generating a key, if necessary */
705 static int rsa_set_pss_param(RSA
*rsa
, EVP_PKEY_CTX
*ctx
)
707 RSA_PKEY_CTX
*rctx
= ctx
->data
;
709 if (!pkey_ctx_is_pss(ctx
))
711 /* If all parameters are default values don't set pss */
712 if (rctx
->md
== NULL
&& rctx
->mgf1md
== NULL
&& rctx
->saltlen
== -2)
714 rsa
->pss
= rsa_pss_params_create(rctx
->md
, rctx
->mgf1md
,
715 rctx
->saltlen
== -2 ? 0 : rctx
->saltlen
);
716 if (rsa
->pss
== NULL
)
721 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
724 RSA_PKEY_CTX
*rctx
= ctx
->data
;
728 if (rctx
->pub_exp
== NULL
) {
729 rctx
->pub_exp
= BN_new();
730 if (rctx
->pub_exp
== NULL
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
736 if (ctx
->pkey_gencb
) {
737 pcb
= BN_GENCB_new();
742 evp_pkey_set_cb_translate(pcb
, ctx
);
746 ret
= RSA_generate_multi_prime_key(rsa
, rctx
->nbits
, rctx
->primes
,
749 if (ret
> 0 && !rsa_set_pss_param(rsa
, ctx
)) {
754 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, rsa
);
760 static const EVP_PKEY_METHOD rsa_pkey_meth
= {
762 EVP_PKEY_FLAG_AUTOARGLEN
,
779 pkey_rsa_verifyrecover
,
795 const EVP_PKEY_METHOD
*ossl_rsa_pkey_method(void)
797 return &rsa_pkey_meth
;
801 * Called for PSS sign or verify initialisation: checks PSS parameter
802 * sanity and sets any restrictions on key usage.
805 static int pkey_pss_init(EVP_PKEY_CTX
*ctx
)
808 RSA_PKEY_CTX
*rctx
= ctx
->data
;
810 const EVP_MD
*mgf1md
;
811 int min_saltlen
, max_saltlen
;
813 /* Should never happen */
814 if (!pkey_ctx_is_pss(ctx
))
816 rsa
= ctx
->pkey
->pkey
.rsa
;
817 /* If no restrictions just return */
818 if (rsa
->pss
== NULL
)
820 /* Get and check parameters */
821 if (!rsa_pss_get_param(rsa
->pss
, &md
, &mgf1md
, &min_saltlen
))
824 /* See if minimum salt length exceeds maximum possible */
825 max_saltlen
= RSA_size(rsa
) - EVP_MD_size(md
);
826 if ((RSA_bits(rsa
) & 0x7) == 1)
828 if (min_saltlen
> max_saltlen
) {
829 ERR_raise(ERR_LIB_RSA
, RSA_R_INVALID_SALT_LENGTH
);
833 rctx
->min_saltlen
= min_saltlen
;
836 * Set PSS restrictions as defaults: we can then block any attempt to
837 * use invalid values in pkey_rsa_ctrl
841 rctx
->mgf1md
= mgf1md
;
842 rctx
->saltlen
= min_saltlen
;
847 static const EVP_PKEY_METHOD rsa_pss_pkey_meth
= {
849 EVP_PKEY_FLAG_AUTOARGLEN
,
865 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
871 const EVP_PKEY_METHOD
*ossl_rsa_pss_pkey_method(void)
873 return &rsa_pss_pkey_meth
;