1 /* crypto/rsa/rsa_pmeth.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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).
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
63 #include <openssl/rsa.h>
64 #include <openssl/evp.h>
68 /* RSA pkey context structure */
72 /* Key gen parameters */
75 /* Keygen callback info */
77 /* RSA padding mode */
81 /* PSS/OAEP salt length */
87 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
90 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
95 rctx->pad_mode = RSA_PKCS1_PADDING;
102 ctx->keygen_info = rctx->gentmp;
103 ctx->keygen_info_count = 2;
108 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
110 RSA_PKEY_CTX *dctx, *sctx;
111 if (!pkey_rsa_init(dst))
115 dctx->nbits = sctx->nbits;
118 dctx->pub_exp = BN_dup(sctx->pub_exp);
122 dctx->pad_mode = sctx->pad_mode;
127 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
131 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
137 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
139 RSA_PKEY_CTX *rctx = ctx->data;
143 BN_free(rctx->pub_exp);
145 OPENSSL_free(rctx->tbuf);
150 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
151 const unsigned char *tbs, size_t tbslen)
154 RSA_PKEY_CTX *rctx = ctx->data;
155 RSA *rsa = ctx->pkey->pkey.rsa;
159 if (tbslen != (size_t)EVP_MD_size(rctx->md))
161 RSAerr(RSA_F_PKEY_RSA_SIGN,
162 RSA_R_INVALID_DIGEST_LENGTH);
165 if (rctx->pad_mode == RSA_X931_PADDING)
167 if (!setup_tbuf(rctx, ctx))
169 memcpy(rctx->tbuf, tbs, tbslen);
171 RSA_X931_hash_id(EVP_MD_type(rctx->md));
172 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
173 sig, rsa, RSA_X931_PADDING);
175 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
178 ret = RSA_sign(EVP_MD_type(rctx->md),
179 tbs, tbslen, sig, &sltmp, rsa);
184 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
186 if (!setup_tbuf(rctx, ctx))
188 if (!RSA_padding_add_PKCS1_PSS(rsa, rctx->tbuf, tbs,
189 rctx->md, rctx->saltlen))
191 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
192 sig, rsa, RSA_NO_PADDING);
198 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
207 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
208 unsigned char *rout, size_t *routlen,
209 const unsigned char *sig, size_t siglen)
212 RSA_PKEY_CTX *rctx = ctx->data;
216 if (rctx->pad_mode == RSA_X931_PADDING)
218 if (!setup_tbuf(rctx, ctx))
220 ret = RSA_public_decrypt(siglen, sig,
221 rctx->tbuf, ctx->pkey->pkey.rsa,
226 if (rctx->tbuf[ret] !=
227 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
229 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
230 RSA_R_ALGORITHM_MISMATCH);
233 if (ret != EVP_MD_size(rctx->md))
235 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
236 RSA_R_INVALID_DIGEST_LENGTH);
240 memcpy(rout, rctx->tbuf, ret);
242 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
245 ret = int_rsa_verify(EVP_MD_type(rctx->md),
246 NULL, 0, rout, &sltmp,
247 sig, siglen, ctx->pkey->pkey.rsa);
254 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
262 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
263 const unsigned char *sig, size_t siglen,
264 const unsigned char *tbs, size_t tbslen)
266 RSA_PKEY_CTX *rctx = ctx->data;
267 RSA *rsa = ctx->pkey->pkey.rsa;
271 if (rctx->pad_mode == RSA_PKCS1_PADDING)
272 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
274 if (rctx->pad_mode == RSA_X931_PADDING)
276 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
280 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
283 if (!setup_tbuf(rctx, ctx))
285 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
286 rsa, RSA_NO_PADDING);
289 ret = RSA_verify_PKCS1_PSS(rsa, tbs, rctx->md,
290 rctx->tbuf, rctx->saltlen);
300 if (!setup_tbuf(rctx, ctx))
302 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
303 rsa, rctx->pad_mode);
308 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
316 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
317 unsigned char *out, size_t *outlen,
318 const unsigned char *in, size_t inlen)
321 RSA_PKEY_CTX *rctx = ctx->data;
322 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;
336 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
344 static int check_padding_md(const EVP_MD *md, int padding)
349 if (padding == RSA_NO_PADDING)
351 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
355 if (padding == RSA_X931_PADDING)
357 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
359 RSAerr(RSA_F_CHECK_PADDING_MD,
360 RSA_R_INVALID_X931_DIGEST);
370 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
372 RSA_PKEY_CTX *rctx = ctx->data;
375 case EVP_PKEY_CTRL_RSA_PADDING:
376 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
378 if (!check_padding_md(rctx->md, p1))
380 if (p1 == RSA_PKCS1_PSS_PADDING)
382 if (!(ctx->operation &
383 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
386 rctx->md = EVP_sha1();
388 if (p1 == RSA_PKCS1_OAEP_PADDING)
390 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
393 rctx->md = EVP_sha1();
399 RSAerr(RSA_F_PKEY_RSA_CTRL,
400 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
403 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
406 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
408 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
414 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
417 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
423 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
429 case EVP_PKEY_CTRL_MD:
430 if (!check_padding_md(p2, rctx->pad_mode))
435 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
436 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
437 case EVP_PKEY_CTRL_PKCS7_SIGN:
438 #ifndef OPENSSL_NO_CMS
439 case EVP_PKEY_CTRL_CMS_ENCRYPT:
440 case EVP_PKEY_CTRL_CMS_DECRYPT:
441 case EVP_PKEY_CTRL_CMS_SIGN:
444 case EVP_PKEY_CTRL_PEER_KEY:
445 RSAerr(RSA_F_PKEY_RSA_CTRL,
446 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
455 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
456 const char *type, const char *value)
460 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
463 if (!strcmp(type, "rsa_padding_mode"))
466 if (!strcmp(value, "pkcs1"))
467 pm = RSA_PKCS1_PADDING;
468 else if (!strcmp(value, "sslv23"))
469 pm = RSA_SSLV23_PADDING;
470 else if (!strcmp(value, "none"))
472 else if (!strcmp(value, "oeap"))
473 pm = RSA_PKCS1_OAEP_PADDING;
474 else if (!strcmp(value, "x931"))
475 pm = RSA_X931_PADDING;
476 else if (!strcmp(value, "pss"))
477 pm = RSA_PKCS1_PSS_PADDING;
480 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
481 RSA_R_UNKNOWN_PADDING_TYPE);
484 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
487 if (!strcmp(type, "rsa_pss_saltlen"))
490 saltlen = atoi(value);
491 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
494 if (!strcmp(type, "rsa_keygen_bits"))
498 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
501 if (!strcmp(type, "rsa_keygen_pubexp"))
504 BIGNUM *pubexp = NULL;
505 if (!BN_asc2bn(&pubexp, value))
507 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
516 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
519 RSA_PKEY_CTX *rctx = ctx->data;
524 rctx->pub_exp = BN_new();
525 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
534 evp_pkey_set_cb_translate(pcb, ctx);
538 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
540 EVP_PKEY_assign_RSA(pkey, rsa);
546 const EVP_PKEY_METHOD rsa_pkey_meth =
549 EVP_PKEY_FLAG_AUTOARGLEN,
566 pkey_rsa_verifyrecover,