1 /* crypto/rsa/rsa_pmeth.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/asn1t.h>
63 #include <openssl/x509.h>
64 #include <openssl/rsa.h>
65 #include <openssl/bn.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509v3.h>
68 #ifndef OPENSSL_NO_CMS
69 # include <openssl/cms.h>
71 #include "internal/evp_int.h"
74 /* RSA pkey context structure */
77 /* Key gen parameters */
80 /* Keygen callback info */
82 /* RSA padding mode */
86 /* message digest for MGF1 */
93 unsigned char *oaep_label;
97 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
100 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
104 rctx->pub_exp = NULL;
105 rctx->pad_mode = RSA_PKCS1_PADDING;
112 rctx->oaep_label = NULL;
113 rctx->oaep_labellen = 0;
116 ctx->keygen_info = rctx->gentmp;
117 ctx->keygen_info_count = 2;
122 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
124 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;
137 dctx->mgf1md = sctx->mgf1md;
138 if (sctx->oaep_label) {
139 if (dctx->oaep_label)
140 OPENSSL_free(dctx->oaep_label);
141 dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
142 if (!dctx->oaep_label)
144 dctx->oaep_labellen = sctx->oaep_labellen;
149 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
153 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
159 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
161 RSA_PKEY_CTX *rctx = ctx->data;
163 BN_free(rctx->pub_exp);
165 OPENSSL_free(rctx->tbuf);
166 if (rctx->oaep_label)
167 OPENSSL_free(rctx->oaep_label);
172 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
173 size_t *siglen, const unsigned char *tbs,
177 RSA_PKEY_CTX *rctx = ctx->data;
178 RSA *rsa = ctx->pkey->pkey.rsa;
181 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
182 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
186 if (EVP_MD_type(rctx->md) == NID_mdc2) {
188 if (rctx->pad_mode != RSA_PKCS1_PADDING)
190 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
191 tbs, tbslen, sig, &sltmp, rsa);
196 } else if (rctx->pad_mode == RSA_X931_PADDING) {
197 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
198 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
201 if (!setup_tbuf(rctx, ctx)) {
202 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
205 memcpy(rctx->tbuf, tbs, tbslen);
206 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
207 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
208 sig, rsa, RSA_X931_PADDING);
209 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
211 ret = RSA_sign(EVP_MD_type(rctx->md),
212 tbs, tbslen, sig, &sltmp, rsa);
216 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
217 if (!setup_tbuf(rctx, ctx))
219 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
221 rctx->md, rctx->mgf1md,
224 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
225 sig, rsa, RSA_NO_PADDING);
229 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
237 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
238 unsigned char *rout, size_t *routlen,
239 const unsigned char *sig, size_t siglen)
242 RSA_PKEY_CTX *rctx = ctx->data;
245 if (rctx->pad_mode == RSA_X931_PADDING) {
246 if (!setup_tbuf(rctx, ctx))
248 ret = RSA_public_decrypt(siglen, sig,
249 rctx->tbuf, ctx->pkey->pkey.rsa,
254 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
255 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
256 RSA_R_ALGORITHM_MISMATCH);
259 if (ret != EVP_MD_size(rctx->md)) {
260 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
261 RSA_R_INVALID_DIGEST_LENGTH);
265 memcpy(rout, rctx->tbuf, ret);
266 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
268 ret = int_rsa_verify(EVP_MD_type(rctx->md),
269 NULL, 0, rout, &sltmp,
270 sig, siglen, ctx->pkey->pkey.rsa);
277 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
285 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
286 const unsigned char *sig, size_t siglen,
287 const unsigned char *tbs, size_t tbslen)
289 RSA_PKEY_CTX *rctx = ctx->data;
290 RSA *rsa = ctx->pkey->pkey.rsa;
293 if (rctx->pad_mode == RSA_PKCS1_PADDING)
294 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
296 if (rctx->pad_mode == RSA_X931_PADDING) {
297 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
299 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
301 if (!setup_tbuf(rctx, ctx))
303 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
304 rsa, RSA_NO_PADDING);
307 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
308 rctx->md, rctx->mgf1md,
309 rctx->tbuf, rctx->saltlen);
316 if (!setup_tbuf(rctx, ctx))
318 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
319 rsa, rctx->pad_mode);
324 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
331 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
332 unsigned char *out, size_t *outlen,
333 const unsigned char *in, size_t inlen)
336 RSA_PKEY_CTX *rctx = ctx->data;
337 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
338 int klen = RSA_size(ctx->pkey->pkey.rsa);
339 if (!setup_tbuf(rctx, ctx))
341 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
345 rctx->md, rctx->mgf1md))
347 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
348 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
350 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
358 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
359 unsigned char *out, size_t *outlen,
360 const unsigned char *in, size_t inlen)
363 RSA_PKEY_CTX *rctx = ctx->data;
364 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
366 if (!setup_tbuf(rctx, ctx))
368 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
369 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
372 for (i = 0; i < ret; i++) {
376 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
380 rctx->md, rctx->mgf1md);
382 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
390 static int check_padding_md(const EVP_MD *md, int padding)
395 if (padding == RSA_NO_PADDING) {
396 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
400 if (padding == RSA_X931_PADDING) {
401 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
402 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
411 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
413 RSA_PKEY_CTX *rctx = ctx->data;
415 case EVP_PKEY_CTRL_RSA_PADDING:
416 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
417 if (!check_padding_md(rctx->md, p1))
419 if (p1 == RSA_PKCS1_PSS_PADDING) {
420 if (!(ctx->operation &
421 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
424 rctx->md = EVP_sha1();
426 if (p1 == RSA_PKCS1_OAEP_PADDING) {
427 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
430 rctx->md = EVP_sha1();
436 RSAerr(RSA_F_PKEY_RSA_CTRL,
437 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
440 case EVP_PKEY_CTRL_GET_RSA_PADDING:
441 *(int *)p2 = rctx->pad_mode;
444 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
445 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
446 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
447 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
450 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
451 *(int *)p2 = rctx->saltlen;
459 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
461 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
467 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
470 BN_free(rctx->pub_exp);
474 case EVP_PKEY_CTRL_RSA_OAEP_MD:
475 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
476 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
477 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
480 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
481 *(const EVP_MD **)p2 = rctx->md;
486 case EVP_PKEY_CTRL_MD:
487 if (!check_padding_md(p2, rctx->pad_mode))
492 case EVP_PKEY_CTRL_GET_MD:
493 *(const EVP_MD **)p2 = rctx->md;
496 case EVP_PKEY_CTRL_RSA_MGF1_MD:
497 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
498 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
499 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
500 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
503 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
505 *(const EVP_MD **)p2 = rctx->mgf1md;
507 *(const EVP_MD **)p2 = rctx->md;
512 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
513 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
514 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
517 if (rctx->oaep_label)
518 OPENSSL_free(rctx->oaep_label);
520 rctx->oaep_label = p2;
521 rctx->oaep_labellen = p1;
523 rctx->oaep_label = NULL;
524 rctx->oaep_labellen = 0;
528 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
529 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
530 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
533 *(unsigned char **)p2 = rctx->oaep_label;
534 return rctx->oaep_labellen;
536 case EVP_PKEY_CTRL_DIGESTINIT:
537 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
538 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
539 case EVP_PKEY_CTRL_PKCS7_SIGN:
541 #ifndef OPENSSL_NO_CMS
542 case EVP_PKEY_CTRL_CMS_DECRYPT:
543 case EVP_PKEY_CTRL_CMS_ENCRYPT:
544 case EVP_PKEY_CTRL_CMS_SIGN:
547 case EVP_PKEY_CTRL_PEER_KEY:
548 RSAerr(RSA_F_PKEY_RSA_CTRL,
549 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
558 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
559 const char *type, const char *value)
562 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
565 if (!strcmp(type, "rsa_padding_mode")) {
567 if (!strcmp(value, "pkcs1"))
568 pm = RSA_PKCS1_PADDING;
569 else if (!strcmp(value, "sslv23"))
570 pm = RSA_SSLV23_PADDING;
571 else if (!strcmp(value, "none"))
573 else if (!strcmp(value, "oeap"))
574 pm = RSA_PKCS1_OAEP_PADDING;
575 else if (!strcmp(value, "oaep"))
576 pm = RSA_PKCS1_OAEP_PADDING;
577 else if (!strcmp(value, "x931"))
578 pm = RSA_X931_PADDING;
579 else if (!strcmp(value, "pss"))
580 pm = RSA_PKCS1_PSS_PADDING;
582 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
585 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
588 if (!strcmp(type, "rsa_pss_saltlen")) {
590 saltlen = atoi(value);
591 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
594 if (!strcmp(type, "rsa_keygen_bits")) {
597 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
600 if (!strcmp(type, "rsa_keygen_pubexp")) {
602 BIGNUM *pubexp = NULL;
603 if (!BN_asc2bn(&pubexp, value))
605 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
611 if (!strcmp(type, "rsa_mgf1_md")) {
613 if (!(md = EVP_get_digestbyname(value))) {
614 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
617 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
620 if (!strcmp(type, "rsa_oaep_md")) {
622 if (!(md = EVP_get_digestbyname(value))) {
623 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
626 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
628 if (!strcmp(type, "rsa_oaep_label")) {
632 lab = string_to_hex(value, &lablen);
635 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
644 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
647 RSA_PKEY_CTX *rctx = ctx->data;
650 if (!rctx->pub_exp) {
651 rctx->pub_exp = BN_new();
652 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
658 if (ctx->pkey_gencb) {
659 pcb = BN_GENCB_new();
664 evp_pkey_set_cb_translate(pcb, ctx);
667 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
670 EVP_PKEY_assign_RSA(pkey, rsa);
676 const EVP_PKEY_METHOD rsa_pkey_meth = {
678 EVP_PKEY_FLAG_AUTOARGLEN,
695 pkey_rsa_verifyrecover,