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 #ifndef OPENSSL_NO_CMS
68 # include <openssl/cms.h>
71 # include <openssl/fips.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 */
96 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
99 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
103 rctx->pub_exp = NULL;
104 rctx->pad_mode = RSA_PKCS1_PADDING;
112 ctx->keygen_info = rctx->gentmp;
113 ctx->keygen_info_count = 2;
118 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
120 RSA_PKEY_CTX *dctx, *sctx;
121 if (!pkey_rsa_init(dst))
125 dctx->nbits = sctx->nbits;
127 dctx->pub_exp = BN_dup(sctx->pub_exp);
131 dctx->pad_mode = sctx->pad_mode;
136 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
140 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
146 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
148 RSA_PKEY_CTX *rctx = ctx->data;
151 BN_free(rctx->pub_exp);
153 OPENSSL_free(rctx->tbuf);
160 * FIP checker. Return value indicates status of context parameters: 1 :
161 * redirect to FIPS. 0 : don't redirect to FIPS. -1 : illegal operation in
165 static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
167 RSA_PKEY_CTX *rctx = ctx->data;
168 RSA *rsa = ctx->pkey->pkey.rsa;
172 if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
174 if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
176 if (rctx->md && !(rctx->md->flags & EVP_MD_FLAG_FIPS))
178 if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS))
184 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
185 size_t *siglen, const unsigned char *tbs,
189 RSA_PKEY_CTX *rctx = ctx->data;
190 RSA *rsa = ctx->pkey->pkey.rsa;
193 ret = pkey_fips_check_ctx(ctx);
195 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
201 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
202 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
208 ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
211 rctx->mgf1md, sig, &slen);
220 if (EVP_MD_type(rctx->md) == NID_mdc2) {
222 if (rctx->pad_mode != RSA_PKCS1_PADDING)
224 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
225 tbs, tbslen, sig, &sltmp, rsa);
230 } else if (rctx->pad_mode == RSA_X931_PADDING) {
231 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
232 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
235 if (!setup_tbuf(rctx, ctx)) {
236 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
239 memcpy(rctx->tbuf, tbs, tbslen);
240 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
241 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
242 sig, rsa, RSA_X931_PADDING);
243 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
245 ret = RSA_sign(EVP_MD_type(rctx->md),
246 tbs, tbslen, sig, &sltmp, rsa);
250 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
251 if (!setup_tbuf(rctx, ctx))
253 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
255 rctx->md, rctx->mgf1md,
258 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
259 sig, rsa, RSA_NO_PADDING);
263 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
271 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
272 unsigned char *rout, size_t *routlen,
273 const unsigned char *sig, size_t siglen)
276 RSA_PKEY_CTX *rctx = ctx->data;
279 if (rctx->pad_mode == RSA_X931_PADDING) {
280 if (!setup_tbuf(rctx, ctx))
282 ret = RSA_public_decrypt(siglen, sig,
283 rctx->tbuf, ctx->pkey->pkey.rsa,
288 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
289 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
290 RSA_R_ALGORITHM_MISMATCH);
293 if (ret != EVP_MD_size(rctx->md)) {
294 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
295 RSA_R_INVALID_DIGEST_LENGTH);
299 memcpy(rout, rctx->tbuf, ret);
300 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
302 ret = int_rsa_verify(EVP_MD_type(rctx->md),
303 NULL, 0, rout, &sltmp,
304 sig, siglen, ctx->pkey->pkey.rsa);
311 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
319 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
320 const unsigned char *sig, size_t siglen,
321 const unsigned char *tbs, size_t tbslen)
323 RSA_PKEY_CTX *rctx = ctx->data;
324 RSA *rsa = ctx->pkey->pkey.rsa;
328 rv = pkey_fips_check_ctx(ctx);
330 RSAerr(RSA_F_PKEY_RSA_VERIFY,
331 RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
338 return FIPS_rsa_verify_digest(rsa,
343 rctx->mgf1md, sig, siglen);
347 if (rctx->pad_mode == RSA_PKCS1_PADDING)
348 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
350 if (rctx->pad_mode == RSA_X931_PADDING) {
351 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
353 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
355 if (!setup_tbuf(rctx, ctx))
357 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
358 rsa, RSA_NO_PADDING);
361 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
362 rctx->md, rctx->mgf1md,
363 rctx->tbuf, rctx->saltlen);
370 if (!setup_tbuf(rctx, ctx))
372 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
373 rsa, rctx->pad_mode);
378 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
385 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
386 unsigned char *out, size_t *outlen,
387 const unsigned char *in, size_t inlen)
390 RSA_PKEY_CTX *rctx = ctx->data;
391 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
399 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
400 unsigned char *out, size_t *outlen,
401 const unsigned char *in, size_t inlen)
404 RSA_PKEY_CTX *rctx = ctx->data;
405 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
413 static int check_padding_md(const EVP_MD *md, int padding)
418 if (padding == RSA_NO_PADDING) {
419 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
423 if (padding == RSA_X931_PADDING) {
424 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
425 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
434 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
436 RSA_PKEY_CTX *rctx = ctx->data;
438 case EVP_PKEY_CTRL_RSA_PADDING:
439 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
440 if (!check_padding_md(rctx->md, p1))
442 if (p1 == RSA_PKCS1_PSS_PADDING) {
443 if (!(ctx->operation &
444 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
447 rctx->md = EVP_sha1();
449 if (p1 == RSA_PKCS1_OAEP_PADDING) {
450 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
453 rctx->md = EVP_sha1();
459 RSAerr(RSA_F_PKEY_RSA_CTRL,
460 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
463 case EVP_PKEY_CTRL_GET_RSA_PADDING:
464 *(int *)p2 = rctx->pad_mode;
467 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
468 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
469 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
470 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
473 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
474 *(int *)p2 = rctx->saltlen;
482 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
484 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
490 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
496 case EVP_PKEY_CTRL_MD:
497 if (!check_padding_md(p2, rctx->pad_mode))
502 case EVP_PKEY_CTRL_RSA_MGF1_MD:
503 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
504 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
505 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
508 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
510 *(const EVP_MD **)p2 = rctx->mgf1md;
512 *(const EVP_MD **)p2 = rctx->md;
517 case EVP_PKEY_CTRL_DIGESTINIT:
518 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
519 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
520 case EVP_PKEY_CTRL_PKCS7_SIGN:
522 #ifndef OPENSSL_NO_CMS
523 case EVP_PKEY_CTRL_CMS_DECRYPT:
525 X509_ALGOR *alg = NULL;
526 ASN1_OBJECT *encalg = NULL;
528 CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
530 X509_ALGOR_get0(&encalg, NULL, NULL, alg);
531 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
532 rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
534 case EVP_PKEY_CTRL_CMS_ENCRYPT:
535 case EVP_PKEY_CTRL_CMS_SIGN:
538 case EVP_PKEY_CTRL_PEER_KEY:
539 RSAerr(RSA_F_PKEY_RSA_CTRL,
540 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
549 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
550 const char *type, const char *value)
553 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
556 if (!strcmp(type, "rsa_padding_mode")) {
558 if (!strcmp(value, "pkcs1"))
559 pm = RSA_PKCS1_PADDING;
560 else if (!strcmp(value, "sslv23"))
561 pm = RSA_SSLV23_PADDING;
562 else if (!strcmp(value, "none"))
564 else if (!strcmp(value, "oeap"))
565 pm = RSA_PKCS1_OAEP_PADDING;
566 else if (!strcmp(value, "oaep"))
567 pm = RSA_PKCS1_OAEP_PADDING;
568 else if (!strcmp(value, "x931"))
569 pm = RSA_X931_PADDING;
570 else if (!strcmp(value, "pss"))
571 pm = RSA_PKCS1_PSS_PADDING;
573 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
576 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
579 if (!strcmp(type, "rsa_pss_saltlen")) {
581 saltlen = atoi(value);
582 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
585 if (!strcmp(type, "rsa_keygen_bits")) {
588 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
591 if (!strcmp(type, "rsa_keygen_pubexp")) {
593 BIGNUM *pubexp = NULL;
594 if (!BN_asc2bn(&pubexp, value))
596 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
605 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
608 RSA_PKEY_CTX *rctx = ctx->data;
611 if (!rctx->pub_exp) {
612 rctx->pub_exp = BN_new();
613 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
619 if (ctx->pkey_gencb) {
621 evp_pkey_set_cb_translate(pcb, ctx);
624 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
626 EVP_PKEY_assign_RSA(pkey, rsa);
632 const EVP_PKEY_METHOD rsa_pkey_meth = {
634 EVP_PKEY_FLAG_AUTOARGLEN,
651 pkey_rsa_verifyrecover,