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
10 #include "internal/constant_time.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/asn1t.h>
15 #include <openssl/x509.h>
16 #include <openssl/rsa.h>
17 #include <openssl/bn.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509v3.h>
20 #include <openssl/cms.h>
21 #include "crypto/evp.h"
22 #include "rsa_local.h"
24 /* RSA pkey context structure */
27 /* Key gen parameters */
31 /* Keygen callback info */
33 /* RSA padding mode */
37 /* message digest for MGF1 */
41 /* Minimum salt length or -1 if no PSS parameter restriction */
46 unsigned char *oaep_label
;
50 /* True if PSS parameters are restricted */
51 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
53 static int pkey_rsa_init(EVP_PKEY_CTX
*ctx
)
55 RSA_PKEY_CTX
*rctx
= OPENSSL_zalloc(sizeof(*rctx
));
60 rctx
->primes
= RSA_DEFAULT_PRIME_NUM
;
61 if (pkey_ctx_is_pss(ctx
))
62 rctx
->pad_mode
= RSA_PKCS1_PSS_PADDING
;
64 rctx
->pad_mode
= RSA_PKCS1_PADDING
;
65 /* Maximum for sign, auto for verify */
66 rctx
->saltlen
= RSA_PSS_SALTLEN_AUTO
;
67 rctx
->min_saltlen
= -1;
69 ctx
->keygen_info
= rctx
->gentmp
;
70 ctx
->keygen_info_count
= 2;
75 static int pkey_rsa_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
77 RSA_PKEY_CTX
*dctx
, *sctx
;
79 if (!pkey_rsa_init(dst
))
83 dctx
->nbits
= sctx
->nbits
;
85 dctx
->pub_exp
= BN_dup(sctx
->pub_exp
);
89 dctx
->pad_mode
= sctx
->pad_mode
;
91 dctx
->mgf1md
= sctx
->mgf1md
;
92 dctx
->saltlen
= sctx
->saltlen
;
93 if (sctx
->oaep_label
) {
94 OPENSSL_free(dctx
->oaep_label
);
95 dctx
->oaep_label
= OPENSSL_memdup(sctx
->oaep_label
, sctx
->oaep_labellen
);
96 if (!dctx
->oaep_label
)
98 dctx
->oaep_labellen
= sctx
->oaep_labellen
;
103 static int setup_tbuf(RSA_PKEY_CTX
*ctx
, EVP_PKEY_CTX
*pk
)
105 if (ctx
->tbuf
!= NULL
)
107 if ((ctx
->tbuf
= OPENSSL_malloc(EVP_PKEY_size(pk
->pkey
))) == NULL
) {
108 RSAerr(RSA_F_SETUP_TBUF
, ERR_R_MALLOC_FAILURE
);
114 static void pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
116 RSA_PKEY_CTX
*rctx
= ctx
->data
;
118 BN_free(rctx
->pub_exp
);
119 OPENSSL_free(rctx
->tbuf
);
120 OPENSSL_free(rctx
->oaep_label
);
125 static int pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
,
126 size_t *siglen
, const unsigned char *tbs
,
130 RSA_PKEY_CTX
*rctx
= ctx
->data
;
131 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
134 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
135 RSAerr(RSA_F_PKEY_RSA_SIGN
, RSA_R_INVALID_DIGEST_LENGTH
);
139 if (EVP_MD_type(rctx
->md
) == NID_mdc2
) {
141 if (rctx
->pad_mode
!= RSA_PKCS1_PADDING
)
143 ret
= RSA_sign_ASN1_OCTET_STRING(0,
144 tbs
, tbslen
, sig
, &sltmp
, rsa
);
149 } else if (rctx
->pad_mode
== RSA_X931_PADDING
) {
150 if ((size_t)EVP_PKEY_size(ctx
->pkey
) < tbslen
+ 1) {
151 RSAerr(RSA_F_PKEY_RSA_SIGN
, RSA_R_KEY_SIZE_TOO_SMALL
);
154 if (!setup_tbuf(rctx
, ctx
)) {
155 RSAerr(RSA_F_PKEY_RSA_SIGN
, ERR_R_MALLOC_FAILURE
);
158 memcpy(rctx
->tbuf
, tbs
, tbslen
);
159 rctx
->tbuf
[tbslen
] = RSA_X931_hash_id(EVP_MD_type(rctx
->md
));
160 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
,
161 sig
, rsa
, RSA_X931_PADDING
);
162 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
164 ret
= RSA_sign(EVP_MD_type(rctx
->md
),
165 tbs
, tbslen
, sig
, &sltmp
, rsa
);
169 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
170 if (!setup_tbuf(rctx
, ctx
))
172 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa
,
174 rctx
->md
, rctx
->mgf1md
,
177 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
178 sig
, rsa
, RSA_NO_PADDING
);
183 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, ctx
->pkey
->pkey
.rsa
,
192 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX
*ctx
,
193 unsigned char *rout
, size_t *routlen
,
194 const unsigned char *sig
, size_t siglen
)
197 RSA_PKEY_CTX
*rctx
= ctx
->data
;
200 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
201 if (!setup_tbuf(rctx
, ctx
))
203 ret
= RSA_public_decrypt(siglen
, sig
,
204 rctx
->tbuf
, ctx
->pkey
->pkey
.rsa
,
209 if (rctx
->tbuf
[ret
] != RSA_X931_hash_id(EVP_MD_type(rctx
->md
))) {
210 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
211 RSA_R_ALGORITHM_MISMATCH
);
214 if (ret
!= EVP_MD_size(rctx
->md
)) {
215 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
216 RSA_R_INVALID_DIGEST_LENGTH
);
220 memcpy(rout
, rctx
->tbuf
, ret
);
221 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
223 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
),
224 NULL
, 0, rout
, &sltmp
,
225 sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
233 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
242 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
243 const unsigned char *sig
, size_t siglen
,
244 const unsigned char *tbs
, size_t tbslen
)
246 RSA_PKEY_CTX
*rctx
= ctx
->data
;
247 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
251 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
252 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
254 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
255 RSAerr(RSA_F_PKEY_RSA_VERIFY
, RSA_R_INVALID_DIGEST_LENGTH
);
258 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
259 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
, siglen
) <= 0)
261 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
263 if (!setup_tbuf(rctx
, ctx
))
265 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
266 rsa
, RSA_NO_PADDING
);
269 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
,
270 rctx
->md
, rctx
->mgf1md
,
271 rctx
->tbuf
, rctx
->saltlen
);
279 if (!setup_tbuf(rctx
, ctx
))
281 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
282 rsa
, rctx
->pad_mode
);
287 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
294 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
,
295 unsigned char *out
, size_t *outlen
,
296 const unsigned char *in
, size_t inlen
)
299 RSA_PKEY_CTX
*rctx
= ctx
->data
;
301 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
302 int klen
= RSA_size(ctx
->pkey
->pkey
.rsa
);
303 if (!setup_tbuf(rctx
, ctx
))
305 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx
->tbuf
, klen
,
309 rctx
->md
, rctx
->mgf1md
))
311 ret
= RSA_public_encrypt(klen
, rctx
->tbuf
, out
,
312 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
314 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
323 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
,
324 unsigned char *out
, size_t *outlen
,
325 const unsigned char *in
, size_t inlen
)
328 RSA_PKEY_CTX
*rctx
= ctx
->data
;
330 if (rctx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
331 if (!setup_tbuf(rctx
, ctx
))
333 ret
= RSA_private_decrypt(inlen
, in
, rctx
->tbuf
,
334 ctx
->pkey
->pkey
.rsa
, RSA_NO_PADDING
);
337 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, ret
, rctx
->tbuf
,
341 rctx
->md
, rctx
->mgf1md
);
343 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
346 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
347 ret
= constant_time_select_int(constant_time_msb(ret
), ret
, 1);
351 static int check_padding_md(const EVP_MD
*md
, int padding
)
358 mdnid
= EVP_MD_type(md
);
360 if (padding
== RSA_NO_PADDING
) {
361 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_PADDING_MODE
);
365 if (padding
== RSA_X931_PADDING
) {
366 if (RSA_X931_hash_id(mdnid
) == -1) {
367 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_X931_DIGEST
);
372 /* List of all supported RSA digests */
391 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_DIGEST
);
400 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
402 RSA_PKEY_CTX
*rctx
= ctx
->data
;
405 case EVP_PKEY_CTRL_RSA_PADDING
:
406 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
)) {
407 if (!check_padding_md(rctx
->md
, p1
))
409 if (p1
== RSA_PKCS1_PSS_PADDING
) {
410 if (!(ctx
->operation
&
411 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
414 rctx
->md
= EVP_sha1();
415 } else if (pkey_ctx_is_pss(ctx
)) {
418 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
419 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
422 rctx
->md
= EVP_sha1();
428 RSAerr(RSA_F_PKEY_RSA_CTRL
,
429 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
432 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
433 *(int *)p2
= rctx
->pad_mode
;
436 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
437 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
438 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
439 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PSS_SALTLEN
);
442 if (type
== EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
) {
443 *(int *)p2
= rctx
->saltlen
;
445 if (p1
< RSA_PSS_SALTLEN_MAX
)
447 if (rsa_pss_restricted(rctx
)) {
448 if (p1
== RSA_PSS_SALTLEN_AUTO
449 && ctx
->operation
== EVP_PKEY_OP_VERIFY
) {
450 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PSS_SALTLEN
);
453 if ((p1
== RSA_PSS_SALTLEN_DIGEST
454 && rctx
->min_saltlen
> EVP_MD_size(rctx
->md
))
455 || (p1
>= 0 && p1
< rctx
->min_saltlen
)) {
456 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_PSS_SALTLEN_TOO_SMALL
);
464 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
465 if (p1
< RSA_MIN_MODULUS_BITS
) {
466 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_KEY_SIZE_TOO_SMALL
);
472 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
473 if (p2
== NULL
|| !BN_is_odd((BIGNUM
*)p2
) || BN_is_one((BIGNUM
*)p2
)) {
474 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_BAD_E_VALUE
);
477 BN_free(rctx
->pub_exp
);
481 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
482 if (p1
< RSA_DEFAULT_PRIME_NUM
|| p1
> RSA_MAX_PRIME_NUM
) {
483 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_KEY_PRIME_NUM_INVALID
);
489 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
490 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
491 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
492 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
495 if (type
== EVP_PKEY_CTRL_GET_RSA_OAEP_MD
)
496 *(const EVP_MD
**)p2
= rctx
->md
;
501 case EVP_PKEY_CTRL_MD
:
502 if (!check_padding_md(p2
, rctx
->pad_mode
))
504 if (rsa_pss_restricted(rctx
)) {
505 if (EVP_MD_type(rctx
->md
) == EVP_MD_type(p2
))
507 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_DIGEST_NOT_ALLOWED
);
513 case EVP_PKEY_CTRL_GET_MD
:
514 *(const EVP_MD
**)p2
= rctx
->md
;
517 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
518 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
519 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
520 && rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
521 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_MGF1_MD
);
524 if (type
== EVP_PKEY_CTRL_GET_RSA_MGF1_MD
) {
526 *(const EVP_MD
**)p2
= rctx
->mgf1md
;
528 *(const EVP_MD
**)p2
= rctx
->md
;
530 if (rsa_pss_restricted(rctx
)) {
531 if (EVP_MD_type(rctx
->mgf1md
) == EVP_MD_type(p2
))
533 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_MGF1_DIGEST_NOT_ALLOWED
);
540 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
541 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
542 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
545 OPENSSL_free(rctx
->oaep_label
);
547 rctx
->oaep_label
= p2
;
548 rctx
->oaep_labellen
= p1
;
550 rctx
->oaep_label
= NULL
;
551 rctx
->oaep_labellen
= 0;
555 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
556 if (rctx
->pad_mode
!= RSA_PKCS1_OAEP_PADDING
) {
557 RSAerr(RSA_F_PKEY_RSA_CTRL
, RSA_R_INVALID_PADDING_MODE
);
560 *(unsigned char **)p2
= rctx
->oaep_label
;
561 return rctx
->oaep_labellen
;
563 case EVP_PKEY_CTRL_DIGESTINIT
:
564 case EVP_PKEY_CTRL_PKCS7_SIGN
:
565 #ifndef OPENSSL_NO_CMS
566 case EVP_PKEY_CTRL_CMS_SIGN
:
570 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
571 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
572 #ifndef OPENSSL_NO_CMS
573 case EVP_PKEY_CTRL_CMS_DECRYPT
:
574 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
576 if (!pkey_ctx_is_pss(ctx
))
579 case EVP_PKEY_CTRL_PEER_KEY
:
580 RSAerr(RSA_F_PKEY_RSA_CTRL
,
581 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
590 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
591 const char *type
, const char *value
)
594 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_VALUE_MISSING
);
597 if (strcmp(type
, "rsa_padding_mode") == 0) {
600 if (strcmp(value
, "pkcs1") == 0) {
601 pm
= RSA_PKCS1_PADDING
;
602 } else if (strcmp(value
, "sslv23") == 0) {
603 pm
= RSA_SSLV23_PADDING
;
604 } else if (strcmp(value
, "none") == 0) {
606 } else if (strcmp(value
, "oeap") == 0) {
607 pm
= RSA_PKCS1_OAEP_PADDING
;
608 } else if (strcmp(value
, "oaep") == 0) {
609 pm
= RSA_PKCS1_OAEP_PADDING
;
610 } else if (strcmp(value
, "x931") == 0) {
611 pm
= RSA_X931_PADDING
;
612 } else if (strcmp(value
, "pss") == 0) {
613 pm
= RSA_PKCS1_PSS_PADDING
;
615 RSAerr(RSA_F_PKEY_RSA_CTRL_STR
, RSA_R_UNKNOWN_PADDING_TYPE
);
618 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
621 if (strcmp(type
, "rsa_pss_saltlen") == 0) {
624 if (!strcmp(value
, "digest"))
625 saltlen
= RSA_PSS_SALTLEN_DIGEST
;
626 else if (!strcmp(value
, "max"))
627 saltlen
= RSA_PSS_SALTLEN_MAX
;
628 else if (!strcmp(value
, "auto"))
629 saltlen
= RSA_PSS_SALTLEN_AUTO
;
631 saltlen
= atoi(value
);
632 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
635 if (strcmp(type
, "rsa_keygen_bits") == 0) {
636 int nbits
= atoi(value
);
638 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
641 if (strcmp(type
, "rsa_keygen_pubexp") == 0) {
644 BIGNUM
*pubexp
= NULL
;
645 if (!BN_asc2bn(&pubexp
, value
))
647 ret
= EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, pubexp
);
653 if (strcmp(type
, "rsa_keygen_primes") == 0) {
654 int nprimes
= atoi(value
);
656 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, nprimes
);
659 if (strcmp(type
, "rsa_mgf1_md") == 0)
660 return EVP_PKEY_CTX_md(ctx
,
661 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
662 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
664 if (pkey_ctx_is_pss(ctx
)) {
666 if (strcmp(type
, "rsa_pss_keygen_mgf1_md") == 0)
667 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
668 EVP_PKEY_CTRL_RSA_MGF1_MD
, value
);
670 if (strcmp(type
, "rsa_pss_keygen_md") == 0)
671 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_KEYGEN
,
672 EVP_PKEY_CTRL_MD
, value
);
674 if (strcmp(type
, "rsa_pss_keygen_saltlen") == 0) {
675 int saltlen
= atoi(value
);
677 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx
, saltlen
);
681 if (strcmp(type
, "rsa_oaep_md") == 0)
682 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_CRYPT
,
683 EVP_PKEY_CTRL_RSA_OAEP_MD
, value
);
685 if (strcmp(type
, "rsa_oaep_label") == 0) {
690 lab
= OPENSSL_hexstr2buf(value
, &lablen
);
693 ret
= EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, lab
, lablen
);
702 /* Set PSS parameters when generating a key, if necessary */
703 static int rsa_set_pss_param(RSA
*rsa
, EVP_PKEY_CTX
*ctx
)
705 RSA_PKEY_CTX
*rctx
= ctx
->data
;
707 if (!pkey_ctx_is_pss(ctx
))
709 /* If all parameters are default values don't set pss */
710 if (rctx
->md
== NULL
&& rctx
->mgf1md
== NULL
&& rctx
->saltlen
== -2)
712 rsa
->pss
= rsa_pss_params_create(rctx
->md
, rctx
->mgf1md
,
713 rctx
->saltlen
== -2 ? 0 : rctx
->saltlen
);
714 if (rsa
->pss
== NULL
)
719 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
722 RSA_PKEY_CTX
*rctx
= ctx
->data
;
726 if (rctx
->pub_exp
== NULL
) {
727 rctx
->pub_exp
= BN_new();
728 if (rctx
->pub_exp
== NULL
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
734 if (ctx
->pkey_gencb
) {
735 pcb
= BN_GENCB_new();
740 evp_pkey_set_cb_translate(pcb
, ctx
);
744 ret
= RSA_generate_multi_prime_key(rsa
, rctx
->nbits
, rctx
->primes
,
747 if (ret
> 0 && !rsa_set_pss_param(rsa
, ctx
)) {
752 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, rsa
);
758 static const EVP_PKEY_METHOD rsa_pkey_meth
= {
760 EVP_PKEY_FLAG_AUTOARGLEN
,
777 pkey_rsa_verifyrecover
,
793 const EVP_PKEY_METHOD
*rsa_pkey_method(void)
795 return &rsa_pkey_meth
;
799 * Called for PSS sign or verify initialisation: checks PSS parameter
800 * sanity and sets any restrictions on key usage.
803 static int pkey_pss_init(EVP_PKEY_CTX
*ctx
)
806 RSA_PKEY_CTX
*rctx
= ctx
->data
;
808 const EVP_MD
*mgf1md
;
809 int min_saltlen
, max_saltlen
;
811 /* Should never happen */
812 if (!pkey_ctx_is_pss(ctx
))
814 rsa
= ctx
->pkey
->pkey
.rsa
;
815 /* If no restrictions just return */
816 if (rsa
->pss
== NULL
)
818 /* Get and check parameters */
819 if (!rsa_pss_get_param(rsa
->pss
, &md
, &mgf1md
, &min_saltlen
))
822 /* See if minimum salt length exceeds maximum possible */
823 max_saltlen
= RSA_size(rsa
) - EVP_MD_size(md
);
824 if ((RSA_bits(rsa
) & 0x7) == 1)
826 if (min_saltlen
> max_saltlen
) {
827 RSAerr(RSA_F_PKEY_PSS_INIT
, RSA_R_INVALID_SALT_LENGTH
);
831 rctx
->min_saltlen
= min_saltlen
;
834 * Set PSS restrictions as defaults: we can then block any attempt to
835 * use invalid values in pkey_rsa_ctrl
839 rctx
->mgf1md
= mgf1md
;
840 rctx
->saltlen
= min_saltlen
;
845 static const EVP_PKEY_METHOD rsa_pss_pkey_meth
= {
847 EVP_PKEY_FLAG_AUTOARGLEN
,
863 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
869 const EVP_PKEY_METHOD
*rsa_pss_pkey_method(void)
871 return &rsa_pss_pkey_meth
;