1 /* $OpenBSD: rsa_pmeth.c,v 1.20 2017/08/28 17:41:59 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/opensslconf.h>
65 #include <openssl/asn1t.h>
66 #include <openssl/bn.h>
67 #include <openssl/err.h>
68 #include <openssl/evp.h>
69 #include <openssl/rsa.h>
70 #include <openssl/x509.h>
76 /* RSA pkey context structure */
79 /* Key gen parameters */
82 /* Keygen callback info */
84 /* RSA padding mode */
88 /* message digest for MGF1 */
90 /* PSS/OAEP salt length */
97 pkey_rsa_init(EVP_PKEY_CTX
*ctx
)
101 rctx
= malloc(sizeof(RSA_PKEY_CTX
));
105 rctx
->pub_exp
= NULL
;
106 rctx
->pad_mode
= RSA_PKCS1_PADDING
;
114 ctx
->keygen_info
= rctx
->gentmp
;
115 ctx
->keygen_info_count
= 2;
121 pkey_rsa_copy(EVP_PKEY_CTX
*dst
, EVP_PKEY_CTX
*src
)
123 RSA_PKEY_CTX
*dctx
, *sctx
;
125 if (!pkey_rsa_init(dst
))
129 dctx
->nbits
= sctx
->nbits
;
131 dctx
->pub_exp
= BN_dup(sctx
->pub_exp
);
135 dctx
->pad_mode
= sctx
->pad_mode
;
141 setup_tbuf(RSA_PKEY_CTX
*ctx
, EVP_PKEY_CTX
*pk
)
145 ctx
->tbuf
= malloc(EVP_PKEY_size(pk
->pkey
));
152 pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
154 RSA_PKEY_CTX
*rctx
= ctx
->data
;
157 BN_free(rctx
->pub_exp
);
164 pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
165 const unsigned char *tbs
, size_t tbslen
)
168 RSA_PKEY_CTX
*rctx
= ctx
->data
;
169 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
172 if (tbslen
!= (size_t)EVP_MD_size(rctx
->md
)) {
173 RSAerror(RSA_R_INVALID_DIGEST_LENGTH
);
177 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
178 if (!setup_tbuf(rctx
, ctx
))
180 memcpy(rctx
->tbuf
, tbs
, tbslen
);
182 RSA_X931_hash_id(EVP_MD_type(rctx
->md
));
183 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
, sig
,
184 rsa
, RSA_X931_PADDING
);
185 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
188 ret
= RSA_sign(EVP_MD_type(rctx
->md
), tbs
, tbslen
, sig
,
193 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
194 if (!setup_tbuf(rctx
, ctx
))
196 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa
, rctx
->tbuf
,
197 tbs
, rctx
->md
, rctx
->mgf1md
, rctx
->saltlen
))
199 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
200 sig
, rsa
, RSA_NO_PADDING
);
204 ret
= RSA_private_encrypt(tbslen
, tbs
, sig
, ctx
->pkey
->pkey
.rsa
,
213 pkey_rsa_verifyrecover(EVP_PKEY_CTX
*ctx
, unsigned char *rout
, size_t *routlen
,
214 const unsigned char *sig
, size_t siglen
)
217 RSA_PKEY_CTX
*rctx
= ctx
->data
;
220 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
221 if (!setup_tbuf(rctx
, ctx
))
223 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
224 ctx
->pkey
->pkey
.rsa
, RSA_X931_PADDING
);
228 if (rctx
->tbuf
[ret
] !=
229 RSA_X931_hash_id(EVP_MD_type(rctx
->md
))) {
230 RSAerror(RSA_R_ALGORITHM_MISMATCH
);
233 if (ret
!= EVP_MD_size(rctx
->md
)) {
234 RSAerror(RSA_R_INVALID_DIGEST_LENGTH
);
238 memcpy(rout
, rctx
->tbuf
, ret
);
239 } else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
) {
242 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
), NULL
, 0,
243 rout
, &sltmp
, sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
250 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
259 pkey_rsa_verify(EVP_PKEY_CTX
*ctx
, const unsigned char *sig
, size_t siglen
,
260 const unsigned char *tbs
, size_t tbslen
)
262 RSA_PKEY_CTX
*rctx
= ctx
->data
;
263 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
267 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
268 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
270 if (rctx
->pad_mode
== RSA_X931_PADDING
) {
271 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
, sig
,
274 } else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
) {
277 if (!setup_tbuf(rctx
, ctx
))
279 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
280 rsa
, RSA_NO_PADDING
);
283 ret
= RSA_verify_PKCS1_PSS_mgf1(rsa
, tbs
, rctx
->md
,
284 rctx
->mgf1md
, rctx
->tbuf
, rctx
->saltlen
);
291 if (!setup_tbuf(rctx
, ctx
))
293 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
, rsa
,
299 if (rslen
!= tbslen
|| memcmp(tbs
, rctx
->tbuf
, rslen
))
306 pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
307 const unsigned char *in
, size_t inlen
)
310 RSA_PKEY_CTX
*rctx
= ctx
->data
;
312 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
321 pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
322 const unsigned char *in
, size_t inlen
)
325 RSA_PKEY_CTX
*rctx
= ctx
->data
;
327 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
336 check_padding_md(const EVP_MD
*md
, int padding
)
341 if (padding
== RSA_NO_PADDING
) {
342 RSAerror(RSA_R_INVALID_PADDING_MODE
);
346 if (padding
== RSA_X931_PADDING
) {
347 if (RSA_X931_hash_id(EVP_MD_type(md
)) == -1) {
348 RSAerror(RSA_R_INVALID_X931_DIGEST
);
358 pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
360 RSA_PKEY_CTX
*rctx
= ctx
->data
;
363 case EVP_PKEY_CTRL_RSA_PADDING
:
364 if (p1
>= RSA_PKCS1_PADDING
&& p1
<= RSA_PKCS1_PSS_PADDING
) {
365 if (!check_padding_md(rctx
->md
, p1
))
367 if (p1
== RSA_PKCS1_PSS_PADDING
) {
368 if (!(ctx
->operation
&
369 (EVP_PKEY_OP_SIGN
| EVP_PKEY_OP_VERIFY
)))
372 rctx
->md
= EVP_sha1();
374 if (p1
== RSA_PKCS1_OAEP_PADDING
) {
375 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
378 rctx
->md
= EVP_sha1();
384 RSAerror(RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE
);
387 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
388 *(int *)p2
= rctx
->pad_mode
;
391 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
392 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
393 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
394 RSAerror(RSA_R_INVALID_PSS_SALTLEN
);
397 if (type
== EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
)
398 *(int *)p2
= rctx
->saltlen
;
406 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
408 RSAerror(RSA_R_INVALID_KEYBITS
);
414 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
420 case EVP_PKEY_CTRL_MD
:
421 if (!check_padding_md(p2
, rctx
->pad_mode
))
426 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
427 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
428 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
) {
429 RSAerror(RSA_R_INVALID_MGF1_MD
);
432 if (type
== EVP_PKEY_CTRL_GET_RSA_MGF1_MD
) {
434 *(const EVP_MD
**)p2
= rctx
->mgf1md
;
436 *(const EVP_MD
**)p2
= rctx
->md
;
441 case EVP_PKEY_CTRL_DIGESTINIT
:
442 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
443 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
444 case EVP_PKEY_CTRL_PKCS7_SIGN
:
446 case EVP_PKEY_CTRL_PEER_KEY
:
447 RSAerror(RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
456 pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
, const char *type
, const char *value
)
462 RSAerror(RSA_R_VALUE_MISSING
);
465 if (!strcmp(type
, "rsa_padding_mode")) {
467 if (!strcmp(value
, "pkcs1"))
468 pm
= RSA_PKCS1_PADDING
;
469 else if (!strcmp(value
, "none"))
471 else if (!strcmp(value
, "oeap"))
472 pm
= RSA_PKCS1_OAEP_PADDING
;
473 else if (!strcmp(value
, "oaep"))
474 pm
= RSA_PKCS1_OAEP_PADDING
;
475 else if (!strcmp(value
, "x931"))
476 pm
= RSA_X931_PADDING
;
477 else if (!strcmp(value
, "pss"))
478 pm
= RSA_PKCS1_PSS_PADDING
;
480 RSAerror(RSA_R_UNKNOWN_PADDING_TYPE
);
483 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
486 if (!strcmp(type
, "rsa_pss_saltlen")) {
490 lval
= strtol(value
, &ep
, 10);
491 if (value
[0] == '\0' || *ep
!= '\0')
493 if ((errno
== ERANGE
&&
494 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
495 (lval
> INT_MAX
|| lval
< INT_MIN
))
498 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
501 if (!strcmp(type
, "rsa_keygen_bits")) {
505 lval
= strtol(value
, &ep
, 10);
506 if (value
[0] == '\0' || *ep
!= '\0')
508 if ((errno
== ERANGE
&&
509 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
510 (lval
> INT_MAX
|| lval
< INT_MIN
))
513 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
516 if (!strcmp(type
, "rsa_keygen_pubexp")) {
518 BIGNUM
*pubexp
= NULL
;
520 if (!BN_asc2bn(&pubexp
, value
))
522 ret
= EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, pubexp
);
534 pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
537 RSA_PKEY_CTX
*rctx
= ctx
->data
;
541 if (!rctx
->pub_exp
) {
542 rctx
->pub_exp
= BN_new();
543 if (!rctx
->pub_exp
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
549 if (ctx
->pkey_gencb
) {
551 evp_pkey_set_cb_translate(pcb
, ctx
);
554 ret
= RSA_generate_key_ex(rsa
, rctx
->nbits
, rctx
->pub_exp
, pcb
);
556 EVP_PKEY_assign_RSA(pkey
, rsa
);
562 const EVP_PKEY_METHOD rsa_pkey_meth
= {
563 .pkey_id
= EVP_PKEY_RSA
,
564 .flags
= EVP_PKEY_FLAG_AUTOARGLEN
,
566 .init
= pkey_rsa_init
,
567 .copy
= pkey_rsa_copy
,
568 .cleanup
= pkey_rsa_cleanup
,
570 .keygen
= pkey_rsa_keygen
,
572 .sign
= pkey_rsa_sign
,
574 .verify
= pkey_rsa_verify
,
576 .verify_recover
= pkey_rsa_verifyrecover
,
578 .encrypt
= pkey_rsa_encrypt
,
580 .decrypt
= pkey_rsa_decrypt
,
582 .ctrl
= pkey_rsa_ctrl
,
583 .ctrl_str
= pkey_rsa_ctrl_str