2 * Copyright 2006-2019 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 "rsa_local.h"
30 /* RSA pkey context structure */
33 /* Key gen parameters */
37 /* Keygen callback info */
39 /* RSA padding mode */
43 /* message digest for MGF1 */
47 /* Minimum salt length or -1 if no PSS parameter restriction */
52 unsigned char *oaep_label
;
56 /* True if PSS parameters are restricted */
57 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
59 static int pkey_rsa_init(EVP_PKEY_CTX
*ctx
)
61 RSA_PKEY_CTX
*rctx
= OPENSSL_zalloc(sizeof(*rctx
));
66 rctx
->primes
= RSA_DEFAULT_PRIME_NUM
;
67 if (pkey_ctx_is_pss(ctx
))
68 rctx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
70 rctx
->pad_mode
= RSA_PKCS1_PADDING
;
71 /* Maximum for sign, auto for verify */
72 rctx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
73 rctx
->min_saltlen
= -1;
75 ctx
->keygen_info
= rctx
->gentmp
;
76 ctx
->keygen_info_count
= 2;
81 static int pkey_rsa_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
83 RSA_PKEY_CTX
*dctx
, *sctx
;
85 if (!pkey_rsa_init(dst
))
89 dctx
->nbits
= sctx
->nbits
;
91 dctx
->pub_exp
= BN_dup(sctx
->pub_exp
);
95 dctx
->pad_mode
= sctx
->pad_mode
;
97 dctx
->mgf1md
= sctx
->mgf1md
;
98 dctx
->saltlen
= sctx
->saltlen
;
99 if (sctx
->oaep_label
) {
100 OPENSSL_free(dctx
->oaep_label
);
101 dctx
->oaep_label
= OPENSSL_memdup(sctx
->oaep_label
, sctx
->oaep_labellen
);
102 if (!dctx
->oaep_label
)
104 dctx
->oaep_labellen
= sctx
->oaep_labellen
;
109 static int setup_tbuf(RSA_PKEY_CTX
*ctx
, EVP_PKEY_CTX
*pk
)
111 if (ctx
->tbuf
!= NULL
)
113 if ((ctx
->tbuf
= OPENSSL_malloc(RSA_size(pk
->pkey
->pkey
.rsa
))) == NULL
) {
114 RSAerr(RSA_F_SETUP_TBUF
, ERR_R_MALLOC_FAILURE
);
120 static void pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
122 RSA_PKEY_CTX
*rctx
= ctx
->data
;
124 BN_free(rctx
->pub_exp
);
125 OPENSSL_free(rctx
->tbuf
);
126 OPENSSL_free(rctx
->oaep_label
);
131 static int pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
,
132 size_t *siglen
, const unsigned char *tbs
,
136 RSA_PKEY_CTX
*rctx
= ctx
->data
;
137 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
140 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
141 RSAerr(RSA_F_PKEY_RSA_SIGN
, RSA_R_INVALID_DIGEST_LENGTH
);
145 if (EVP_MD_type(rctx
->md
) == NID_mdc2
) {
147 if (rctx
->pad_mode
!= RSA_PKCS1_PADDING
)
149 ret
= RSA_sign_ASN1_OCTET_STRING(0,
150 tbs
, tbslen
, sig
, &sltmp
, rsa
);
155 } else if (rctx
->pad_mode
== RSA_X931_PADDING
) {
156 if ((size_t)RSA_size(rsa
) < tbslen
+ 1) {
157 RSAerr(RSA_F_PKEY_RSA_SIGN
, RSA_R_KEY_SIZE_TOO_SMALL
);
160 if (!setup_tbuf(rctx
, ctx
)) {
161 RSAerr(RSA_F_PKEY_RSA_SIGN
, ERR_R_MALLOC_FAILURE
);
164 memcpy(rctx
->tbuf
, tbs
, tbslen
);
165 rctx
->tbuf
[tbslen
] = RSA_X931_hash_id(EVP_MD_type(rctx
->md
));
166 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
,
167 sig
, rsa
, RSA_X931_PADDING
);
168 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
170 ret
= RSA_sign(EVP_MD_type(rctx
->md
),
171 tbs
, tbslen
, sig
, &sltmp
, rsa
);
175 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
176 if (!setup_tbuf(rctx
, ctx
))
178 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa
,
180 rctx
->md
, rctx
->mgf1md
,
183 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
184 sig
, rsa
, RSA_NO_PADDING
);
189 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, ctx
->pkey
->pkey
.rsa
,
198 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX
*ctx
,
199 unsigned char *rout
, size_t *routlen
,
200 const unsigned char *sig
, size_t siglen
)
203 RSA_PKEY_CTX
*rctx
= ctx
->data
;
206 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
207 if (!setup_tbuf(rctx
, ctx
))
209 ret
= RSA_public_decrypt(siglen
, sig
,
210 rctx
->tbuf
, ctx
->pkey
->pkey
.rsa
,
215 if (rctx
->tbuf
[ret
] != RSA_X931_hash_id(EVP_MD_type(rctx
->md
))) {
216 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
217 RSA_R_ALGORITHM_MISMATCH
);
220 if (ret
!= EVP_MD_size(rctx
->md
)) {
221 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
222 RSA_R_INVALID_DIGEST_LENGTH
);
226 memcpy(rout
, rctx
->tbuf
, ret
);
227 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
229 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
),
230 NULL
, 0, rout
, &sltmp
,
231 sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
239 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
248 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
249 const unsigned char *sig
, size_t siglen
,
250 const unsigned char *tbs
, size_t tbslen
)
252 RSA_PKEY_CTX
*rctx
= ctx
->data
;
253 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
257 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
258 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
260 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
261 RSAerr(RSA_F_PKEY_RSA_VERIFY
, RSA_R_INVALID_DIGEST_LENGTH
);
264 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
265 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
, siglen
) <= 0)
267 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
269 if (!setup_tbuf(rctx
, ctx
))
271 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
272 rsa
, RSA_NO_PADDING
);
275 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
,
276 rctx
->md
, rctx
->mgf1md
,
277 rctx
->tbuf
, rctx
->saltlen
);
285 if (!setup_tbuf(rctx
, ctx
))
287 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
288 rsa
, rctx
->pad_mode
);
293 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
300 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
,
301 unsigned char *out
, size_t *outlen
,
302 const unsigned char *in
, size_t inlen
)
305 RSA_PKEY_CTX
*rctx
= ctx
->data
;
307 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
308 int klen
= RSA_size(ctx
->pkey
->pkey
.rsa
);
309 if (!setup_tbuf(rctx
, ctx
))
311 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx
->tbuf
, klen
,
315 rctx
->md
, rctx
->mgf1md
))
317 ret
= RSA_public_encrypt(klen
, rctx
->tbuf
, out
,
318 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
320 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
329 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
,
330 unsigned char *out
, size_t *outlen
,
331 const unsigned char *in
, size_t inlen
)
334 RSA_PKEY_CTX
*rctx
= ctx
->data
;
336 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
337 if (!setup_tbuf(rctx
, ctx
))
339 ret
= RSA_private_decrypt(inlen
, in
, rctx
->tbuf
,
340 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
343 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, ret
, rctx
->tbuf
,
347 rctx
->md
, rctx
->mgf1md
);
349 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
352 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
353 ret
= constant_time_select_int(constant_time_msb(ret
), ret
, 1);
357 static int check_padding_md(const EVP_MD
*md
, int padding
)
364 mdnid
= EVP_MD_type(md
);
366 if (padding
== RSA_NO_PADDING
) {
367 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_PADDING_MODE
);
371 if (padding
== RSA_X931_PADDING
) {
372 if (RSA_X931_hash_id(mdnid
) == -1) {
373 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_X931_DIGEST
);
378 /* List of all supported RSA digests */
397 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_DIGEST
);
406 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
408 RSA_PKEY_CTX
*rctx
= ctx
->data
;
411 case EVP_PKEY_CTRL_RSA_PADDING
:
412 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
)) {
413 if (!check_padding_md(rctx
->md
, p1
))
415 if (p1
== RSA_PKCS1_PSS_PADDING
) {
416 if (!(ctx
->operation
&
417 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
420 rctx
->md
= EVP_sha1();
421 } else if (pkey_ctx_is_pss(ctx
)) {
424 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
425 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
428 rctx
->md
= EVP_sha1();
434 RSAerr(RSA_F_PKEY_RSA_CTRL
,
435 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_PSS_SALTLEN_TOO_SMALL
);
470 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
471 if (p1
< RSA_MIN_MODULUS_BITS
) {
472 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_MGF1_DIGEST_NOT_ALLOWED
);
546 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
547 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
548 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
, 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 RSAerr(RSA_F_PKEY_RSA_CTRL
,
587 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
596 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
597 const char *type
, const char *value
)
600 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_VALUE_MISSING
);
603 if (strcmp(type
, "rsa_padding_mode") == 0) {
606 if (strcmp(value
, "pkcs1") == 0) {
607 pm
= RSA_PKCS1_PADDING
;
608 } else if (strcmp(value
, "sslv23") == 0) {
609 pm
= RSA_SSLV23_PADDING
;
610 } else if (strcmp(value
, "none") == 0) {
612 } else if (strcmp(value
, "oeap") == 0) {
613 pm
= RSA_PKCS1_OAEP_PADDING
;
614 } else if (strcmp(value
, "oaep") == 0) {
615 pm
= RSA_PKCS1_OAEP_PADDING
;
616 } else if (strcmp(value
, "x931") == 0) {
617 pm
= RSA_X931_PADDING
;
618 } else if (strcmp(value
, "pss") == 0) {
619 pm
= RSA_PKCS1_PSS_PADDING
;
621 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_UNKNOWN_PADDING_TYPE
);
624 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
627 if (strcmp(type
, "rsa_pss_saltlen") == 0) {
630 if (!strcmp(value
, "digest"))
631 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
632 else if (!strcmp(value
, "max"))
633 saltlen
= RSA_PSS_SALTLEN_MAX
;
634 else if (!strcmp(value
, "auto"))
635 saltlen
= RSA_PSS_SALTLEN_AUTO
;
637 saltlen
= atoi(value
);
638 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
641 if (strcmp(type
, "rsa_keygen_bits") == 0) {
642 int nbits
= atoi(value
);
644 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
647 if (strcmp(type
, "rsa_keygen_pubexp") == 0) {
650 BIGNUM
*pubexp
= NULL
;
651 if (!BN_asc2bn(&pubexp
, value
))
653 ret
= EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, pubexp
);
659 if (strcmp(type
, "rsa_keygen_primes") == 0) {
660 int nprimes
= atoi(value
);
662 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, nprimes
);
665 if (strcmp(type
, "rsa_mgf1_md") == 0)
666 return EVP_PKEY_CTX_md(ctx
,
667 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
668 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
670 if (pkey_ctx_is_pss(ctx
)) {
672 if (strcmp(type
, "rsa_pss_keygen_mgf1_md") == 0)
673 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
674 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
676 if (strcmp(type
, "rsa_pss_keygen_md") == 0)
677 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
678 EVP_PKEY_CTRL_MD
, value
);
680 if (strcmp(type
, "rsa_pss_keygen_saltlen") == 0) {
681 int saltlen
= atoi(value
);
683 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx
, saltlen
);
687 if (strcmp(type
, "rsa_oaep_md") == 0)
688 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_CRYPT
,
689 EVP_PKEY_CTRL_RSA_OAEP_MD
, value
);
691 if (strcmp(type
, "rsa_oaep_label") == 0) {
696 lab
= OPENSSL_hexstr2buf(value
, &lablen
);
699 ret
= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, lab
, lablen
);
708 /* Set PSS parameters when generating a key, if necessary */
709 static int rsa_set_pss_param(RSA
*rsa
, EVP_PKEY_CTX
*ctx
)
711 RSA_PKEY_CTX
*rctx
= ctx
->data
;
713 if (!pkey_ctx_is_pss(ctx
))
715 /* If all parameters are default values don't set pss */
716 if (rctx
->md
== NULL
&& rctx
->mgf1md
== NULL
&& rctx
->saltlen
== -2)
718 rsa
->pss
= rsa_pss_params_create(rctx
->md
, rctx
->mgf1md
,
719 rctx
->saltlen
== -2 ? 0 : rctx
->saltlen
);
720 if (rsa
->pss
== NULL
)
725 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
728 RSA_PKEY_CTX
*rctx
= ctx
->data
;
732 if (rctx
->pub_exp
== NULL
) {
733 rctx
->pub_exp
= BN_new();
734 if (rctx
->pub_exp
== NULL
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
740 if (ctx
->pkey_gencb
) {
741 pcb
= BN_GENCB_new();
746 evp_pkey_set_cb_translate(pcb
, ctx
);
750 ret
= RSA_generate_multi_prime_key(rsa
, rctx
->nbits
, rctx
->primes
,
753 if (ret
> 0 && !rsa_set_pss_param(rsa
, ctx
)) {
758 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, rsa
);
764 static const EVP_PKEY_METHOD rsa_pkey_meth
= {
766 EVP_PKEY_FLAG_AUTOARGLEN
,
783 pkey_rsa_verifyrecover
,
799 const EVP_PKEY_METHOD
*rsa_pkey_method(void)
801 return &rsa_pkey_meth
;
805 * Called for PSS sign or verify initialisation: checks PSS parameter
806 * sanity and sets any restrictions on key usage.
809 static int pkey_pss_init(EVP_PKEY_CTX
*ctx
)
812 RSA_PKEY_CTX
*rctx
= ctx
->data
;
814 const EVP_MD
*mgf1md
;
815 int min_saltlen
, max_saltlen
;
817 /* Should never happen */
818 if (!pkey_ctx_is_pss(ctx
))
820 rsa
= ctx
->pkey
->pkey
.rsa
;
821 /* If no restrictions just return */
822 if (rsa
->pss
== NULL
)
824 /* Get and check parameters */
825 if (!rsa_pss_get_param(rsa
->pss
, &md
, &mgf1md
, &min_saltlen
))
828 /* See if minimum salt length exceeds maximum possible */
829 max_saltlen
= RSA_size(rsa
) - EVP_MD_size(md
);
830 if ((RSA_bits(rsa
) & 0x7) == 1)
832 if (min_saltlen
> max_saltlen
) {
833 RSAerr(RSA_F_PKEY_PSS_INIT
, RSA_R_INVALID_SALT_LENGTH
);
837 rctx
->min_saltlen
= min_saltlen
;
840 * Set PSS restrictions as defaults: we can then block any attempt to
841 * use invalid values in pkey_rsa_ctrl
845 rctx
->mgf1md
= mgf1md
;
846 rctx
->saltlen
= min_saltlen
;
851 static const EVP_PKEY_METHOD rsa_pss_pkey_meth
= {
853 EVP_PKEY_FLAG_AUTOARGLEN
,
869 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
875 const EVP_PKEY_METHOD
*rsa_pss_pkey_method(void)
877 return &rsa_pss_pkey_meth
;