1 /* crypto/rsa/rsa_eay.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include "cryptlib.h"
114 #include <openssl/bn.h>
115 #include <openssl/rsa.h>
116 #include <openssl/rand.h>
118 #include <openssl/fips.h>
123 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
124 unsigned char *to, RSA *rsa,int padding);
125 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
126 unsigned char *to, RSA *rsa,int padding);
127 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
128 unsigned char *to, RSA *rsa,int padding);
129 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
130 unsigned char *to, RSA *rsa,int padding);
131 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
132 static int RSA_eay_init(RSA *rsa);
133 static int RSA_eay_finish(RSA *rsa);
134 static RSA_METHOD rsa_pkcs1_eay_meth={
135 "Eric Young's PKCS#1 RSA",
136 RSA_eay_public_encrypt,
137 RSA_eay_public_decrypt, /* signature verification */
138 RSA_eay_private_encrypt, /* signing */
139 RSA_eay_private_decrypt,
141 BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
144 RSA_FLAG_FIPS_METHOD, /* flags */
148 NULL /* rsa_keygen */
151 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
153 return(&rsa_pkcs1_eay_meth);
156 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
157 unsigned char *to, RSA *rsa, int padding)
160 int i,j,k,num=0,r= -1;
161 unsigned char *buf=NULL;
165 if(FIPS_selftest_failed())
167 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
171 if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
173 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
178 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
180 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
184 if (BN_ucmp(rsa->n, rsa->e) <= 0)
186 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
190 /* for large moduli, enforce exponent limit */
191 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
193 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
195 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
200 if ((ctx=BN_CTX_new()) == NULL) goto err;
203 ret = BN_CTX_get(ctx);
204 num=BN_num_bytes(rsa->n);
205 buf = OPENSSL_malloc(num);
206 if (!f || !ret || !buf)
208 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
214 case RSA_PKCS1_PADDING:
215 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
217 #ifndef OPENSSL_NO_SHA
218 case RSA_PKCS1_OAEP_PADDING:
219 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
222 case RSA_SSLV23_PADDING:
223 i=RSA_padding_add_SSLv23(buf,num,from,flen);
226 i=RSA_padding_add_none(buf,num,from,flen);
229 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
232 if (i <= 0) goto err;
234 if (BN_bin2bn(buf,num,f) == NULL) goto err;
236 if (BN_ucmp(f, rsa->n) >= 0)
238 /* usually the padding functions would catch this */
239 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
243 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
244 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
247 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
248 rsa->_method_mod_n)) goto err;
250 /* put in leading 0 bytes if the number is less than the
251 * length of the modulus */
253 i=BN_bn2bin(ret,&(to[num-j]));
254 for (k=0; k<(num-i); k++)
266 OPENSSL_cleanse(buf,num);
272 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
275 int got_write_lock = 0;
278 CRYPTO_r_lock(CRYPTO_LOCK_RSA);
280 if (rsa->blinding == NULL)
282 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
283 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
286 if (rsa->blinding == NULL)
287 rsa->blinding = RSA_setup_blinding(rsa, ctx);
294 CRYPTO_THREADID_current(&cur);
295 if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret)))
297 /* rsa->blinding is ours! */
303 /* resort to rsa->mt_blinding instead */
305 *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert()
306 * that the BN_BLINDING is shared, meaning that accesses
307 * require locks, and that the blinding factor must be
308 * stored outside the BN_BLINDING
311 if (rsa->mt_blinding == NULL)
315 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
316 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
320 if (rsa->mt_blinding == NULL)
321 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
323 ret = rsa->mt_blinding;
328 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
330 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
334 static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
335 BIGNUM *r, BN_CTX *ctx)
338 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
342 CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING);
343 ret = BN_BLINDING_convert_ex(f, r, b, ctx);
344 CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING);
349 static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
350 BIGNUM *r, BN_CTX *ctx)
353 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
357 CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
358 ret = BN_BLINDING_invert_ex(f, r, b, ctx);
359 CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
365 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
366 unsigned char *to, RSA *rsa, int padding)
368 BIGNUM *f, *ret, *br, *res;
369 int i,j,k,num=0,r= -1;
370 unsigned char *buf=NULL;
372 int local_blinding = 0;
373 BN_BLINDING *blinding = NULL;
376 if(FIPS_selftest_failed())
378 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
382 if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
384 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
389 if ((ctx=BN_CTX_new()) == NULL) goto err;
392 br = BN_CTX_get(ctx);
393 ret = BN_CTX_get(ctx);
394 num = BN_num_bytes(rsa->n);
395 buf = OPENSSL_malloc(num);
396 if(!f || !ret || !buf)
398 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
404 case RSA_PKCS1_PADDING:
405 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
407 case RSA_X931_PADDING:
408 i=RSA_padding_add_X931(buf,num,from,flen);
411 i=RSA_padding_add_none(buf,num,from,flen);
413 case RSA_SSLV23_PADDING:
415 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
418 if (i <= 0) goto err;
420 if (BN_bin2bn(buf,num,f) == NULL) goto err;
422 if (BN_ucmp(f, rsa->n) >= 0)
424 /* usually the padding functions would catch this */
425 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
429 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
431 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
432 if (blinding == NULL)
434 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
439 if (blinding != NULL)
440 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
443 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
446 (rsa->dmp1 != NULL) &&
447 (rsa->dmq1 != NULL) &&
448 (rsa->iqmp != NULL)) )
450 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
457 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
461 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
466 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
467 if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
470 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
471 rsa->_method_mod_n)) goto err;
475 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
478 if (padding == RSA_X931_PADDING)
480 BN_sub(f, rsa->n, ret);
489 /* put in leading 0 bytes if the number is less than the
490 * length of the modulus */
492 i=BN_bn2bin(res,&(to[num-j]));
493 for (k=0; k<(num-i); k++)
505 OPENSSL_cleanse(buf,num);
511 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
512 unsigned char *to, RSA *rsa, int padding)
514 BIGNUM *f, *ret, *br;
517 unsigned char *buf=NULL;
519 int local_blinding = 0;
520 BN_BLINDING *blinding = NULL;
523 if(FIPS_selftest_failed())
525 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
529 if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
531 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
536 if((ctx = BN_CTX_new()) == NULL) goto err;
539 br = BN_CTX_get(ctx);
540 ret = BN_CTX_get(ctx);
541 num = BN_num_bytes(rsa->n);
542 buf = OPENSSL_malloc(num);
543 if(!f || !ret || !buf)
545 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
549 /* This check was for equality but PGP does evil things
550 * and chops off the top '0' bytes */
553 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
557 /* make data into a big number */
558 if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
560 if (BN_ucmp(f, rsa->n) >= 0)
562 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
566 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
568 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
569 if (blinding == NULL)
571 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
576 if (blinding != NULL)
577 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
581 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
584 (rsa->dmp1 != NULL) &&
585 (rsa->dmq1 != NULL) &&
586 (rsa->iqmp != NULL)) )
588 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
595 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
598 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
603 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
604 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
606 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
612 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
616 j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
620 case RSA_PKCS1_PADDING:
621 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
623 #ifndef OPENSSL_NO_SHA
624 case RSA_PKCS1_OAEP_PADDING:
625 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
628 case RSA_SSLV23_PADDING:
629 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
632 r=RSA_padding_check_none(to,num,buf,j,num);
635 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
639 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
649 OPENSSL_cleanse(buf,num);
655 /* signature verification */
656 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
657 unsigned char *to, RSA *rsa, int padding)
662 unsigned char *buf=NULL;
666 if(FIPS_selftest_failed())
668 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
672 if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
674 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
679 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
681 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
685 if (BN_ucmp(rsa->n, rsa->e) <= 0)
687 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
691 /* for large moduli, enforce exponent limit */
692 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
694 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
696 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
701 if((ctx = BN_CTX_new()) == NULL) goto err;
704 ret = BN_CTX_get(ctx);
705 num=BN_num_bytes(rsa->n);
706 buf = OPENSSL_malloc(num);
707 if(!f || !ret || !buf)
709 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
713 /* This check was for equality but PGP does evil things
714 * and chops off the top '0' bytes */
717 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
721 if (BN_bin2bn(from,flen,f) == NULL) goto err;
723 if (BN_ucmp(f, rsa->n) >= 0)
725 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
729 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
730 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
733 if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
734 rsa->_method_mod_n)) goto err;
736 if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
737 if (!BN_sub(ret, rsa->n, ret)) goto err;
744 case RSA_PKCS1_PADDING:
745 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
747 case RSA_X931_PADDING:
748 r=RSA_padding_check_X931(to,num,buf,i,num);
751 r=RSA_padding_check_none(to,num,buf,i,num);
754 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
758 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
768 OPENSSL_cleanse(buf,num);
774 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
776 BIGNUM *r1,*m1,*vrfy;
777 BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
778 BIGNUM *dmp1,*dmq1,*c,*pr1;
782 r1 = BN_CTX_get(ctx);
783 m1 = BN_CTX_get(ctx);
784 vrfy = BN_CTX_get(ctx);
787 BIGNUM local_p, local_q;
788 BIGNUM *p = NULL, *q = NULL;
790 /* Make sure BN_mod_inverse in Montgomery intialization uses the
791 * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
793 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
797 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
801 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
809 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
811 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))
813 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))
818 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
819 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
822 /* compute I mod q */
823 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
826 BN_with_flags(c, I, BN_FLG_CONSTTIME);
827 if (!BN_mod(r1,c,rsa->q,ctx)) goto err;
831 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
834 /* compute r1^dmq1 mod q */
835 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
838 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
842 if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
843 rsa->_method_mod_q)) goto err;
845 /* compute I mod p */
846 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
849 BN_with_flags(c, I, BN_FLG_CONSTTIME);
850 if (!BN_mod(r1,c,rsa->p,ctx)) goto err;
854 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
857 /* compute r1^dmp1 mod p */
858 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
861 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
865 if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
866 rsa->_method_mod_p)) goto err;
868 if (!BN_sub(r0,r0,m1)) goto err;
869 /* This will help stop the size of r0 increasing, which does
870 * affect the multiply if it optimised for a power of 2 size */
871 if (BN_is_negative(r0))
872 if (!BN_add(r0,r0,rsa->p)) goto err;
874 if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
876 /* Turn BN_FLG_CONSTTIME flag on before division operation */
877 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
880 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
884 if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;
886 /* If p < q it is occasionally possible for the correction of
887 * adding 'p' if r0 is negative above to leave the result still
888 * negative. This can break the private key operations: the following
889 * second correction should *always* correct this rare occurrence.
890 * This will *never* happen with OpenSSL generated keys because
891 * they ensure p > q [steve]
893 if (BN_is_negative(r0))
894 if (!BN_add(r0,r0,rsa->p)) goto err;
895 if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
896 if (!BN_add(r0,r1,m1)) goto err;
898 if (rsa->e && rsa->n)
900 if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
901 /* If 'I' was greater than (or equal to) rsa->n, the operation
902 * will be equivalent to using 'I mod n'. However, the result of
903 * the verify will *always* be less than 'n' so we don't check
904 * for absolute equality, just congruency. */
905 if (!BN_sub(vrfy, vrfy, I)) goto err;
906 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
907 if (BN_is_negative(vrfy))
908 if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
909 if (!BN_is_zero(vrfy))
911 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
912 * miscalculated CRT output, just do a raw (slower)
913 * mod_exp and return that instead. */
918 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
921 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
925 if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
926 rsa->_method_mod_n)) goto err;
935 static int RSA_eay_init(RSA *rsa)
938 FIPS_selftest_check();
940 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
944 static int RSA_eay_finish(RSA *rsa)
946 if (rsa->_method_mod_n != NULL)
947 BN_MONT_CTX_free(rsa->_method_mod_n);
948 if (rsa->_method_mod_p != NULL)
949 BN_MONT_CTX_free(rsa->_method_mod_p);
950 if (rsa->_method_mod_q != NULL)
951 BN_MONT_CTX_free(rsa->_method_mod_q);