2 * Copyright 2006-2020 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 RSAerr(RSA_F_SETUP_TBUF
, 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 RSAerr(RSA_F_PKEY_RSA_SIGN
, 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 RSAerr(RSA_F_PKEY_RSA_SIGN
, RSA_R_KEY_SIZE_TOO_SMALL
);
161 if (!setup_tbuf(rctx
, ctx
)) {
162 RSAerr(RSA_F_PKEY_RSA_SIGN
, 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 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
218 RSA_R_ALGORITHM_MISMATCH
);
221 if (ret
!= EVP_MD_size(rctx
->md
)) {
222 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
223 RSA_R_INVALID_DIGEST_LENGTH
);
227 memcpy(rout
, rctx
->tbuf
, ret
);
228 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
230 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
),
231 NULL
, 0, rout
, &sltmp
,
232 sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
240 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
249 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
250 const unsigned char *sig
, size_t siglen
,
251 const unsigned char *tbs
, size_t tbslen
)
253 RSA_PKEY_CTX
*rctx
= ctx
->data
;
254 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
258 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
259 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
261 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
262 RSAerr(RSA_F_PKEY_RSA_VERIFY
, RSA_R_INVALID_DIGEST_LENGTH
);
265 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
266 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
, siglen
) <= 0)
268 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
270 if (!setup_tbuf(rctx
, ctx
))
272 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
273 rsa
, RSA_NO_PADDING
);
276 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
,
277 rctx
->md
, rctx
->mgf1md
,
278 rctx
->tbuf
, rctx
->saltlen
);
286 if (!setup_tbuf(rctx
, ctx
))
288 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
289 rsa
, rctx
->pad_mode
);
294 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
301 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
,
302 unsigned char *out
, size_t *outlen
,
303 const unsigned char *in
, size_t inlen
)
306 RSA_PKEY_CTX
*rctx
= ctx
->data
;
308 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
309 int klen
= RSA_size(ctx
->pkey
->pkey
.rsa
);
310 if (!setup_tbuf(rctx
, ctx
))
312 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx
->tbuf
, klen
,
316 rctx
->md
, rctx
->mgf1md
))
318 ret
= RSA_public_encrypt(klen
, rctx
->tbuf
, out
,
319 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
321 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
330 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
,
331 unsigned char *out
, size_t *outlen
,
332 const unsigned char *in
, size_t inlen
)
335 RSA_PKEY_CTX
*rctx
= ctx
->data
;
337 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
338 if (!setup_tbuf(rctx
, ctx
))
340 ret
= RSA_private_decrypt(inlen
, in
, rctx
->tbuf
,
341 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
344 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, ret
, rctx
->tbuf
,
348 rctx
->md
, rctx
->mgf1md
);
350 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
353 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
354 ret
= constant_time_select_int(constant_time_msb(ret
), ret
, 1);
358 static int check_padding_md(const EVP_MD
*md
, int padding
)
365 mdnid
= EVP_MD_type(md
);
367 if (padding
== RSA_NO_PADDING
) {
368 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_PADDING_MODE
);
372 if (padding
== RSA_X931_PADDING
) {
373 if (RSA_X931_hash_id(mdnid
) == -1) {
374 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_X931_DIGEST
);
379 /* List of all supported RSA digests */
400 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_DIGEST
);
409 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
411 RSA_PKEY_CTX
*rctx
= ctx
->data
;
414 case EVP_PKEY_CTRL_RSA_PADDING
:
415 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
)) {
416 if (!check_padding_md(rctx
->md
, p1
))
418 if (p1
== RSA_PKCS1_PSS_PADDING
) {
419 if (!(ctx
->operation
&
420 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
423 rctx
->md
= EVP_sha1();
424 } else if (pkey_ctx_is_pss(ctx
)) {
427 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
428 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
431 rctx
->md
= EVP_sha1();
437 RSAerr(RSA_F_PKEY_RSA_CTRL
,
438 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
441 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
442 *(int *)p2
= rctx
->pad_mode
;
445 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
446 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
447 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
448 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PSS_SALTLEN
);
451 if (type
== EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
) {
452 *(int *)p2
= rctx
->saltlen
;
454 if (p1
< RSA_PSS_SALTLEN_MAX
)
456 if (rsa_pss_restricted(rctx
)) {
457 if (p1
== RSA_PSS_SALTLEN_AUTO
458 && ctx
->operation
== EVP_PKEY_OP_VERIFY
) {
459 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PSS_SALTLEN
);
462 if ((p1
== RSA_PSS_SALTLEN_DIGEST
463 && rctx
->min_saltlen
> EVP_MD_size(rctx
->md
))
464 || (p1
>= 0 && p1
< rctx
->min_saltlen
)) {
465 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_PSS_SALTLEN_TOO_SMALL
);
473 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
474 if (p1
< RSA_MIN_MODULUS_BITS
) {
475 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_KEY_SIZE_TOO_SMALL
);
481 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
482 if (p2
== NULL
|| !BN_is_odd((BIGNUM
*)p2
) || BN_is_one((BIGNUM
*)p2
)) {
483 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_BAD_E_VALUE
);
486 BN_free(rctx
->pub_exp
);
490 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
491 if (p1
< RSA_DEFAULT_PRIME_NUM
|| p1
> RSA_MAX_PRIME_NUM
) {
492 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_KEY_PRIME_NUM_INVALID
);
498 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
499 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
500 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
501 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
504 if (type
== EVP_PKEY_CTRL_GET_RSA_OAEP_MD
)
505 *(const EVP_MD
**)p2
= rctx
->md
;
510 case EVP_PKEY_CTRL_MD
:
511 if (!check_padding_md(p2
, rctx
->pad_mode
))
513 if (rsa_pss_restricted(rctx
)) {
514 if (EVP_MD_type(rctx
->md
) == EVP_MD_type(p2
))
516 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_DIGEST_NOT_ALLOWED
);
522 case EVP_PKEY_CTRL_GET_MD
:
523 *(const EVP_MD
**)p2
= rctx
->md
;
526 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
527 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
528 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
529 && rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
530 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_MGF1_MD
);
533 if (type
== EVP_PKEY_CTRL_GET_RSA_MGF1_MD
) {
535 *(const EVP_MD
**)p2
= rctx
->mgf1md
;
537 *(const EVP_MD
**)p2
= rctx
->md
;
539 if (rsa_pss_restricted(rctx
)) {
540 if (EVP_MD_type(rctx
->mgf1md
) == EVP_MD_type(p2
))
542 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_MGF1_DIGEST_NOT_ALLOWED
);
549 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
550 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
551 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
554 OPENSSL_free(rctx
->oaep_label
);
556 rctx
->oaep_label
= p2
;
557 rctx
->oaep_labellen
= p1
;
559 rctx
->oaep_label
= NULL
;
560 rctx
->oaep_labellen
= 0;
564 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
565 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
566 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
569 *(unsigned char **)p2
= rctx
->oaep_label
;
570 return rctx
->oaep_labellen
;
572 case EVP_PKEY_CTRL_DIGESTINIT
:
573 case EVP_PKEY_CTRL_PKCS7_SIGN
:
574 #ifndef OPENSSL_NO_CMS
575 case EVP_PKEY_CTRL_CMS_SIGN
:
579 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
580 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
581 #ifndef OPENSSL_NO_CMS
582 case EVP_PKEY_CTRL_CMS_DECRYPT
:
583 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
585 if (!pkey_ctx_is_pss(ctx
))
588 case EVP_PKEY_CTRL_PEER_KEY
:
589 RSAerr(RSA_F_PKEY_RSA_CTRL
,
590 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
599 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
600 const char *type
, const char *value
)
603 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_VALUE_MISSING
);
606 if (strcmp(type
, "rsa_padding_mode") == 0) {
609 if (strcmp(value
, "pkcs1") == 0) {
610 pm
= RSA_PKCS1_PADDING
;
611 } else if (strcmp(value
, "sslv23") == 0) {
612 pm
= RSA_SSLV23_PADDING
;
613 } else if (strcmp(value
, "none") == 0) {
615 } else if (strcmp(value
, "oeap") == 0) {
616 pm
= RSA_PKCS1_OAEP_PADDING
;
617 } else if (strcmp(value
, "oaep") == 0) {
618 pm
= RSA_PKCS1_OAEP_PADDING
;
619 } else if (strcmp(value
, "x931") == 0) {
620 pm
= RSA_X931_PADDING
;
621 } else if (strcmp(value
, "pss") == 0) {
622 pm
= RSA_PKCS1_PSS_PADDING
;
624 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_UNKNOWN_PADDING_TYPE
);
627 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
630 if (strcmp(type
, "rsa_pss_saltlen") == 0) {
633 if (!strcmp(value
, "digest"))
634 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
635 else if (!strcmp(value
, "max"))
636 saltlen
= RSA_PSS_SALTLEN_MAX
;
637 else if (!strcmp(value
, "auto"))
638 saltlen
= RSA_PSS_SALTLEN_AUTO
;
640 saltlen
= atoi(value
);
641 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
644 if (strcmp(type
, "rsa_keygen_bits") == 0) {
645 int nbits
= atoi(value
);
647 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
650 if (strcmp(type
, "rsa_keygen_pubexp") == 0) {
653 BIGNUM
*pubexp
= NULL
;
654 if (!BN_asc2bn(&pubexp
, value
))
656 ret
= EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx
, pubexp
);
661 if (strcmp(type
, "rsa_keygen_primes") == 0) {
662 int nprimes
= atoi(value
);
664 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, nprimes
);
667 if (strcmp(type
, "rsa_mgf1_md") == 0)
668 return EVP_PKEY_CTX_md(ctx
,
669 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
670 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
672 if (pkey_ctx_is_pss(ctx
)) {
674 if (strcmp(type
, "rsa_pss_keygen_mgf1_md") == 0)
675 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
676 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
678 if (strcmp(type
, "rsa_pss_keygen_md") == 0)
679 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
680 EVP_PKEY_CTRL_MD
, value
);
682 if (strcmp(type
, "rsa_pss_keygen_saltlen") == 0) {
683 int saltlen
= atoi(value
);
685 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx
, saltlen
);
689 if (strcmp(type
, "rsa_oaep_md") == 0)
690 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_CRYPT
,
691 EVP_PKEY_CTRL_RSA_OAEP_MD
, value
);
693 if (strcmp(type
, "rsa_oaep_label") == 0) {
698 lab
= OPENSSL_hexstr2buf(value
, &lablen
);
701 ret
= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, lab
, lablen
);
710 /* Set PSS parameters when generating a key, if necessary */
711 static int rsa_set_pss_param(RSA
*rsa
, EVP_PKEY_CTX
*ctx
)
713 RSA_PKEY_CTX
*rctx
= ctx
->data
;
715 if (!pkey_ctx_is_pss(ctx
))
717 /* If all parameters are default values don't set pss */
718 if (rctx
->md
== NULL
&& rctx
->mgf1md
== NULL
&& rctx
->saltlen
== -2)
720 rsa
->pss
= rsa_pss_params_create(rctx
->md
, rctx
->mgf1md
,
721 rctx
->saltlen
== -2 ? 0 : rctx
->saltlen
);
722 if (rsa
->pss
== NULL
)
727 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
730 RSA_PKEY_CTX
*rctx
= ctx
->data
;
734 if (rctx
->pub_exp
== NULL
) {
735 rctx
->pub_exp
= BN_new();
736 if (rctx
->pub_exp
== NULL
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
742 if (ctx
->pkey_gencb
) {
743 pcb
= BN_GENCB_new();
748 evp_pkey_set_cb_translate(pcb
, ctx
);
752 ret
= RSA_generate_multi_prime_key(rsa
, rctx
->nbits
, rctx
->primes
,
755 if (ret
> 0 && !rsa_set_pss_param(rsa
, ctx
)) {
760 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, rsa
);
766 static const EVP_PKEY_METHOD rsa_pkey_meth
= {
768 EVP_PKEY_FLAG_AUTOARGLEN
,
785 pkey_rsa_verifyrecover
,
801 const EVP_PKEY_METHOD
*rsa_pkey_method(void)
803 return &rsa_pkey_meth
;
807 * Called for PSS sign or verify initialisation: checks PSS parameter
808 * sanity and sets any restrictions on key usage.
811 static int pkey_pss_init(EVP_PKEY_CTX
*ctx
)
814 RSA_PKEY_CTX
*rctx
= ctx
->data
;
816 const EVP_MD
*mgf1md
;
817 int min_saltlen
, max_saltlen
;
819 /* Should never happen */
820 if (!pkey_ctx_is_pss(ctx
))
822 rsa
= ctx
->pkey
->pkey
.rsa
;
823 /* If no restrictions just return */
824 if (rsa
->pss
== NULL
)
826 /* Get and check parameters */
827 if (!rsa_pss_get_param(rsa
->pss
, &md
, &mgf1md
, &min_saltlen
))
830 /* See if minimum salt length exceeds maximum possible */
831 max_saltlen
= RSA_size(rsa
) - EVP_MD_size(md
);
832 if ((RSA_bits(rsa
) & 0x7) == 1)
834 if (min_saltlen
> max_saltlen
) {
835 RSAerr(RSA_F_PKEY_PSS_INIT
, RSA_R_INVALID_SALT_LENGTH
);
839 rctx
->min_saltlen
= min_saltlen
;
842 * Set PSS restrictions as defaults: we can then block any attempt to
843 * use invalid values in pkey_rsa_ctrl
847 rctx
->mgf1md
= mgf1md
;
848 rctx
->saltlen
= min_saltlen
;
853 static const EVP_PKEY_METHOD rsa_pss_pkey_meth
= {
855 EVP_PKEY_FLAG_AUTOARGLEN
,
871 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
877 const EVP_PKEY_METHOD
*rsa_pss_pkey_method(void)
879 return &rsa_pss_pkey_meth
;