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>
67 extern int int_rsa_verify(int dtype
, const unsigned char *m
, unsigned int m_len
,
68 unsigned char *rm
, unsigned int *prm_len
,
69 const unsigned char *sigbuf
, unsigned int siglen
,
72 /* RSA pkey context structure */
76 /* Key gen parameters */
79 /* Keygen callback info */
81 /* RSA padding mode */
85 /* PSS/OAEP salt length */
91 static int pkey_rsa_init(EVP_PKEY_CTX
*ctx
)
94 rctx
= OPENSSL_malloc(sizeof(RSA_PKEY_CTX
));
99 rctx
->pad_mode
= RSA_PKCS1_PADDING
;
106 ctx
->keygen_info
= rctx
->gentmp
;
107 ctx
->keygen_info_count
= 2;
112 static int setup_tbuf(RSA_PKEY_CTX
*ctx
, EVP_PKEY_CTX
*pk
)
116 ctx
->tbuf
= OPENSSL_malloc(EVP_PKEY_size(pk
->pkey
));
122 static void pkey_rsa_cleanup(EVP_PKEY_CTX
*ctx
)
124 RSA_PKEY_CTX
*rctx
= ctx
->data
;
128 BN_free(rctx
->pub_exp
);
130 OPENSSL_free(rctx
->tbuf
);
135 static int pkey_rsa_sign(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, int *siglen
,
136 const unsigned char *tbs
, int tbslen
)
139 RSA_PKEY_CTX
*rctx
= ctx
->data
;
140 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
144 if (tbslen
!= EVP_MD_size(rctx
->md
))
146 RSAerr(RSA_F_PKEY_RSA_SIGN
,
147 RSA_R_INVALID_DIGEST_LENGTH
);
150 if (rctx
->pad_mode
== RSA_X931_PADDING
)
152 if (!setup_tbuf(rctx
, ctx
))
154 memcpy(rctx
->tbuf
, tbs
, tbslen
);
156 RSA_X931_hash_id(EVP_MD_type(rctx
->md
));
157 ret
= RSA_private_encrypt(tbslen
+ 1, rctx
->tbuf
,
158 sig
, rsa
, RSA_X931_PADDING
);
160 else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
163 ret
= RSA_sign(EVP_MD_type(rctx
->md
),
164 tbs
, tbslen
, sig
, &sltmp
, rsa
);
169 else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
)
171 if (!setup_tbuf(rctx
, ctx
))
173 if (!RSA_padding_add_PKCS1_PSS(rsa
, rctx
->tbuf
, tbs
,
174 rctx
->md
, rctx
->saltlen
))
176 ret
= RSA_private_encrypt(RSA_size(rsa
), rctx
->tbuf
,
177 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
, int *routlen
,
194 const unsigned char *sig
, int siglen
)
197 RSA_PKEY_CTX
*rctx
= ctx
->data
;
201 if (rctx
->pad_mode
== RSA_X931_PADDING
)
203 if (!setup_tbuf(rctx
, ctx
))
205 ret
= RSA_public_decrypt(siglen
, sig
,
206 rctx
->tbuf
, ctx
->pkey
->pkey
.rsa
,
211 if (rctx
->tbuf
[ret
] !=
212 RSA_X931_hash_id(EVP_MD_type(rctx
->md
)))
214 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
215 RSA_R_ALGORITHM_MISMATCH
);
218 if (ret
!= EVP_MD_size(rctx
->md
))
220 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER
,
221 RSA_R_INVALID_DIGEST_LENGTH
);
225 memcpy(rout
, rctx
->tbuf
, ret
);
227 else if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
230 ret
= int_rsa_verify(EVP_MD_type(rctx
->md
),
231 NULL
, 0, rout
, &sltmp
,
232 sig
, siglen
, ctx
->pkey
->pkey
.rsa
);
239 ret
= RSA_public_decrypt(siglen
, sig
, rout
, ctx
->pkey
->pkey
.rsa
,
247 static int pkey_rsa_verify(EVP_PKEY_CTX
*ctx
,
248 const unsigned char *sig
, int siglen
,
249 const unsigned char *tbs
, int tbslen
)
251 RSA_PKEY_CTX
*rctx
= ctx
->data
;
252 RSA
*rsa
= ctx
->pkey
->pkey
.rsa
;
256 if (rctx
->pad_mode
== RSA_PKCS1_PADDING
)
257 return RSA_verify(EVP_MD_type(rctx
->md
), tbs
, tbslen
,
259 if (rctx
->pad_mode
== RSA_X931_PADDING
)
261 if (pkey_rsa_verifyrecover(ctx
, NULL
, &rslen
,
265 else if (rctx
->pad_mode
== RSA_PKCS1_PSS_PADDING
)
268 if (!setup_tbuf(rctx
, ctx
))
270 ret
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
271 rsa
, RSA_NO_PADDING
);
274 ret
= RSA_verify_PKCS1_PSS(rsa
, tbs
, rctx
->md
,
275 rctx
->tbuf
, rctx
->saltlen
);
285 if (!setup_tbuf(rctx
, ctx
))
287 rslen
= RSA_public_decrypt(siglen
, sig
, rctx
->tbuf
,
288 rsa
, rctx
->pad_mode
);
293 if ((rslen
!= tbslen
) || memcmp(tbs
, rctx
->tbuf
, rslen
))
301 static int pkey_rsa_encrypt(EVP_PKEY_CTX
*ctx
, unsigned char *out
, int *outlen
,
302 const unsigned char *in
, int inlen
)
305 RSA_PKEY_CTX
*rctx
= ctx
->data
;
306 ret
= RSA_public_encrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
314 static int pkey_rsa_decrypt(EVP_PKEY_CTX
*ctx
, unsigned char *out
, int *outlen
,
315 const unsigned char *in
, int inlen
)
318 RSA_PKEY_CTX
*rctx
= ctx
->data
;
319 ret
= RSA_private_decrypt(inlen
, in
, out
, ctx
->pkey
->pkey
.rsa
,
327 static int check_padding_md(const EVP_MD
*md
, int padding
)
332 if (padding
== RSA_NO_PADDING
)
334 RSAerr(RSA_F_CHECK_PADDING_MD
, RSA_R_INVALID_PADDING_MODE
);
338 if (padding
== RSA_X931_PADDING
)
340 if (RSA_X931_hash_id(EVP_MD_type(md
)) == -1)
342 RSAerr(RSA_F_CHECK_PADDING_MD
,
343 RSA_R_INVALID_X931_DIGEST
);
353 static int pkey_rsa_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
355 RSA_PKEY_CTX
*rctx
= ctx
->data
;
358 case EVP_PKEY_CTRL_RSA_PADDING
:
359 if ((p1
>= RSA_PKCS1_PADDING
) && (p1
<= RSA_PKCS1_PSS_PADDING
))
361 if (!check_padding_md(rctx
->md
, p1
))
363 if (p1
== RSA_PKCS1_PSS_PADDING
)
365 if (ctx
->operation
== EVP_PKEY_OP_VERIFYRECOVER
)
368 rctx
->md
= EVP_sha1();
370 if (p1
== RSA_PKCS1_OAEP_PADDING
)
372 if (!(ctx
->operation
& EVP_PKEY_OP_TYPE_CRYPT
))
375 rctx
->md
= EVP_sha1();
382 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
385 if (rctx
->pad_mode
!= RSA_PKCS1_PSS_PADDING
)
390 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
396 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
402 case EVP_PKEY_CTRL_MD
:
403 if (!check_padding_md(p2
, rctx
->pad_mode
))
414 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX
*ctx
,
415 const char *type
, const char *value
)
417 if (!strcmp(type
, "rsa_padding_mode"))
422 if (!strcmp(value
, "pkcs1"))
423 pm
= RSA_PKCS1_PADDING
;
424 else if (!strcmp(value
, "sslv23"))
425 pm
= RSA_SSLV23_PADDING
;
426 else if (!strcmp(value
, "none"))
428 else if (!strcmp(value
, "oeap"))
429 pm
= RSA_PKCS1_OAEP_PADDING
;
430 else if (!strcmp(value
, "x931"))
431 pm
= RSA_X931_PADDING
;
432 else if (!strcmp(value
, "pss"))
433 pm
= RSA_PKCS1_PSS_PADDING
;
436 return EVP_PKEY_CTX_set_rsa_padding(ctx
, pm
);
439 if (!strcmp(type
, "rsa_pss_saltlen"))
442 saltlen
= atoi(value
);
443 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, saltlen
);
446 if (!strcmp(type
, "rsa_keygen_bits"))
450 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, nbits
);
453 if (!strcmp(type
, "rsa_keygen_pubexp"))
456 BIGNUM
*pubexp
= NULL
;
457 if (!BN_asc2bn(&pubexp
, value
))
459 ret
= EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, pubexp
);
468 static int pkey_rsa_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
471 RSA_PKEY_CTX
*rctx
= ctx
->data
;
476 rctx
->pub_exp
= BN_new();
477 if (!rctx
->pub_exp
|| !BN_set_word(rctx
->pub_exp
, RSA_F4
))
486 evp_pkey_set_cb_translate(pcb
, ctx
);
490 ret
= RSA_generate_key_ex(rsa
, rctx
->nbits
, rctx
->pub_exp
, pcb
);
492 EVP_PKEY_assign_RSA(pkey
, rsa
);
498 const EVP_PKEY_METHOD rsa_pkey_meth
=
501 EVP_PKEY_FLAG_AUTOARGLEN
,
517 pkey_rsa_verifyrecover
,