FIPS mode RSA changes:
[oweals/openssl.git] / crypto / rsa / rsa_eay.c
1 /* crypto/rsa/rsa_eay.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
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.
8  * 
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).
15  * 
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.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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)"
40  * 
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
51  * SUCH DAMAGE.
52  * 
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.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer. 
67  *
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
71  *    distribution.
72  *
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/)"
77  *
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.
82  *
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.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
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  * ====================================================================
105  *
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).
109  *
110  */
111
112 #include <stdio.h>
113 #include "cryptlib.h"
114 #include <openssl/bn.h>
115 #include <openssl/rsa.h>
116 #include <openssl/rand.h>
117 #ifdef OPENSSL_FIPS
118 #include <openssl/fips.h>
119 #endif
120
121 #ifndef RSA_NULL
122
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,
140         RSA_eay_mod_exp,
141         BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
142         RSA_eay_init,
143         RSA_eay_finish,
144         RSA_FLAG_FIPS_METHOD, /* flags */
145         NULL,
146         0, /* rsa_sign */
147         0, /* rsa_verify */
148         NULL /* rsa_keygen */
149         };
150
151 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
152         {
153         return(&rsa_pkcs1_eay_meth);
154         }
155
156 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
157              unsigned char *to, RSA *rsa, int padding)
158         {
159         BIGNUM *f,*ret;
160         int i,j,k,num=0,r= -1;
161         unsigned char *buf=NULL;
162         BN_CTX *ctx=NULL;
163
164 #ifdef OPENSSL_FIPS
165         if(FIPS_selftest_failed())
166                 {
167                 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
168                 goto err;
169                 }
170
171         if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
172                 {
173                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
174                 return -1;
175                 }
176 #endif
177
178         if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
179                 {
180                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
181                 return -1;
182                 }
183
184         if (BN_ucmp(rsa->n, rsa->e) <= 0)
185                 {
186                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
187                 return -1;
188                 }
189
190         /* for large moduli, enforce exponent limit */
191         if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
192                 {
193                 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
194                         {
195                         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
196                         return -1;
197                         }
198                 }
199         
200         if ((ctx=BN_CTX_new()) == NULL) goto err;
201         BN_CTX_start(ctx);
202         f = BN_CTX_get(ctx);
203         ret = BN_CTX_get(ctx);
204         num=BN_num_bytes(rsa->n);
205         buf = OPENSSL_malloc(num);
206         if (!f || !ret || !buf)
207                 {
208                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
209                 goto err;
210                 }
211
212         switch (padding)
213                 {
214         case RSA_PKCS1_PADDING:
215                 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
216                 break;
217 #ifndef OPENSSL_NO_SHA
218         case RSA_PKCS1_OAEP_PADDING:
219                 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
220                 break;
221 #endif
222         case RSA_SSLV23_PADDING:
223                 i=RSA_padding_add_SSLv23(buf,num,from,flen);
224                 break;
225         case RSA_NO_PADDING:
226                 i=RSA_padding_add_none(buf,num,from,flen);
227                 break;
228         default:
229                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
230                 goto err;
231                 }
232         if (i <= 0) goto err;
233
234         if (BN_bin2bn(buf,num,f) == NULL) goto err;
235         
236         if (BN_ucmp(f, rsa->n) >= 0)
237                 {
238                 /* usually the padding functions would catch this */
239                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
240                 goto err;
241                 }
242
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))
245                         goto err;
246
247         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
248                 rsa->_method_mod_n)) goto err;
249
250         /* put in leading 0 bytes if the number is less than the
251          * length of the modulus */
252         j=BN_num_bytes(ret);
253         i=BN_bn2bin(ret,&(to[num-j]));
254         for (k=0; k<(num-i); k++)
255                 to[k]=0;
256
257         r=num;
258 err:
259         if (ctx != NULL)
260                 {
261                 BN_CTX_end(ctx);
262                 BN_CTX_free(ctx);
263                 }
264         if (buf != NULL) 
265                 {
266                 OPENSSL_cleanse(buf,num);
267                 OPENSSL_free(buf);
268                 }
269         return(r);
270         }
271
272 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
273 {
274         BN_BLINDING *ret;
275         int got_write_lock = 0;
276         CRYPTO_THREADID cur;
277
278         CRYPTO_r_lock(CRYPTO_LOCK_RSA);
279
280         if (rsa->blinding == NULL)
281                 {
282                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
283                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
284                 got_write_lock = 1;
285
286                 if (rsa->blinding == NULL)
287                         rsa->blinding = RSA_setup_blinding(rsa, ctx);
288                 }
289
290         ret = rsa->blinding;
291         if (ret == NULL)
292                 goto err;
293
294         CRYPTO_THREADID_current(&cur);
295         if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret)))
296                 {
297                 /* rsa->blinding is ours! */
298
299                 *local = 1;
300                 }
301         else
302                 {
303                 /* resort to rsa->mt_blinding instead */
304
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
309                              */
310
311                 if (rsa->mt_blinding == NULL)
312                         {
313                         if (!got_write_lock)
314                                 {
315                                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
316                                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
317                                 got_write_lock = 1;
318                                 }
319                         
320                         if (rsa->mt_blinding == NULL)
321                                 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
322                         }
323                 ret = rsa->mt_blinding;
324                 }
325
326  err:
327         if (got_write_lock)
328                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
329         else
330                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
331         return ret;
332 }
333
334 static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
335         BIGNUM *r, BN_CTX *ctx)
336 {
337         if (local)
338                 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
339         else
340                 {
341                 int ret;
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);
345                 return ret;
346                 }
347 }
348
349 static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
350         BIGNUM *r, BN_CTX *ctx)
351 {
352         if (local)
353                 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
354         else
355                 {
356                 int ret;
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);
360                 return ret;
361                 }
362 }
363
364 /* signing */
365 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
366              unsigned char *to, RSA *rsa, int padding)
367         {
368         BIGNUM *f, *ret, *br, *res;
369         int i,j,k,num=0,r= -1;
370         unsigned char *buf=NULL;
371         BN_CTX *ctx=NULL;
372         int local_blinding = 0;
373         BN_BLINDING *blinding = NULL;
374
375 #ifdef OPENSSL_FIPS
376         if(FIPS_selftest_failed())
377                 {
378                 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
379                 goto err;
380                 }
381
382         if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
383                 {
384                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
385                 return -1;
386                 }
387 #endif
388
389         if ((ctx=BN_CTX_new()) == NULL) goto err;
390         BN_CTX_start(ctx);
391         f   = BN_CTX_get(ctx);
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)
397                 {
398                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
399                 goto err;
400                 }
401
402         switch (padding)
403                 {
404         case RSA_PKCS1_PADDING:
405                 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
406                 break;
407         case RSA_X931_PADDING:
408                 i=RSA_padding_add_X931(buf,num,from,flen);
409                 break;
410         case RSA_NO_PADDING:
411                 i=RSA_padding_add_none(buf,num,from,flen);
412                 break;
413         case RSA_SSLV23_PADDING:
414         default:
415                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
416                 goto err;
417                 }
418         if (i <= 0) goto err;
419
420         if (BN_bin2bn(buf,num,f) == NULL) goto err;
421         
422         if (BN_ucmp(f, rsa->n) >= 0)
423                 {       
424                 /* usually the padding functions would catch this */
425                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
426                 goto err;
427                 }
428
429         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
430                 {
431                 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
432                 if (blinding == NULL)
433                         {
434                         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
435                         goto err;
436                         }
437                 }
438         
439         if (blinding != NULL)
440                 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
441                         goto err;
442
443         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
444                 ((rsa->p != NULL) &&
445                 (rsa->q != NULL) &&
446                 (rsa->dmp1 != NULL) &&
447                 (rsa->dmq1 != NULL) &&
448                 (rsa->iqmp != NULL)) )
449                 { 
450                 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
451                 }
452         else
453                 {
454                 BIGNUM local_d;
455                 BIGNUM *d = NULL;
456                 
457                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
458                         {
459                         BN_init(&local_d);
460                         d = &local_d;
461                         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
462                         }
463                 else
464                         d= rsa->d;
465
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))
468                                 goto err;
469
470                 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
471                                 rsa->_method_mod_n)) goto err;
472                 }
473
474         if (blinding)
475                 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
476                         goto err;
477
478         if (padding == RSA_X931_PADDING)
479                 {
480                 BN_sub(f, rsa->n, ret);
481                 if (BN_cmp(ret, f))
482                         res = f;
483                 else
484                         res = ret;
485                 }
486         else
487                 res = ret;
488
489         /* put in leading 0 bytes if the number is less than the
490          * length of the modulus */
491         j=BN_num_bytes(res);
492         i=BN_bn2bin(res,&(to[num-j]));
493         for (k=0; k<(num-i); k++)
494                 to[k]=0;
495
496         r=num;
497 err:
498         if (ctx != NULL)
499                 {
500                 BN_CTX_end(ctx);
501                 BN_CTX_free(ctx);
502                 }
503         if (buf != NULL)
504                 {
505                 OPENSSL_cleanse(buf,num);
506                 OPENSSL_free(buf);
507                 }
508         return(r);
509         }
510
511 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
512              unsigned char *to, RSA *rsa, int padding)
513         {
514         BIGNUM *f, *ret, *br;
515         int j,num=0,r= -1;
516         unsigned char *p;
517         unsigned char *buf=NULL;
518         BN_CTX *ctx=NULL;
519         int local_blinding = 0;
520         BN_BLINDING *blinding = NULL;
521
522 #ifdef OPENSSL_FIPS
523         if(FIPS_selftest_failed())
524                 {
525                 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
526                 goto err;
527                 }
528
529         if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
530                 {
531                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
532                 return -1;
533                 }
534 #endif
535
536         if((ctx = BN_CTX_new()) == NULL) goto err;
537         BN_CTX_start(ctx);
538         f   = BN_CTX_get(ctx);
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)
544                 {
545                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
546                 goto err;
547                 }
548
549         /* This check was for equality but PGP does evil things
550          * and chops off the top '0' bytes */
551         if (flen > num)
552                 {
553                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
554                 goto err;
555                 }
556
557         /* make data into a big number */
558         if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
559
560         if (BN_ucmp(f, rsa->n) >= 0)
561                 {
562                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
563                 goto err;
564                 }
565
566         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
567                 {
568                 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
569                 if (blinding == NULL)
570                         {
571                         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
572                         goto err;
573                         }
574                 }
575         
576         if (blinding != NULL)
577                 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
578                         goto err;
579
580         /* do the decrypt */
581         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
582                 ((rsa->p != NULL) &&
583                 (rsa->q != NULL) &&
584                 (rsa->dmp1 != NULL) &&
585                 (rsa->dmq1 != NULL) &&
586                 (rsa->iqmp != NULL)) )
587                 {
588                 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
589                 }
590         else
591                 {
592                 BIGNUM local_d;
593                 BIGNUM *d = NULL;
594                 
595                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
596                         {
597                         d = &local_d;
598                         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
599                         }
600                 else
601                         d = rsa->d;
602
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))
605                                 goto err;
606                 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
607                                 rsa->_method_mod_n))
608                   goto err;
609                 }
610
611         if (blinding)
612                 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
613                         goto err;
614
615         p=buf;
616         j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
617
618         switch (padding)
619                 {
620         case RSA_PKCS1_PADDING:
621                 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
622                 break;
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);
626                 break;
627 #endif
628         case RSA_SSLV23_PADDING:
629                 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
630                 break;
631         case RSA_NO_PADDING:
632                 r=RSA_padding_check_none(to,num,buf,j,num);
633                 break;
634         default:
635                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
636                 goto err;
637                 }
638         if (r < 0)
639                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
640
641 err:
642         if (ctx != NULL)
643                 {
644                 BN_CTX_end(ctx);
645                 BN_CTX_free(ctx);
646                 }
647         if (buf != NULL)
648                 {
649                 OPENSSL_cleanse(buf,num);
650                 OPENSSL_free(buf);
651                 }
652         return(r);
653         }
654
655 /* signature verification */
656 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
657              unsigned char *to, RSA *rsa, int padding)
658         {
659         BIGNUM *f,*ret;
660         int i,num=0,r= -1;
661         unsigned char *p;
662         unsigned char *buf=NULL;
663         BN_CTX *ctx=NULL;
664
665 #ifdef OPENSSL_FIPS
666         if(FIPS_selftest_failed())
667                 {
668                 FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
669                 goto err;
670                 }
671
672         if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
673                 {
674                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
675                 return -1;
676                 }
677 #endif
678
679         if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
680                 {
681                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
682                 return -1;
683                 }
684
685         if (BN_ucmp(rsa->n, rsa->e) <= 0)
686                 {
687                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
688                 return -1;
689                 }
690
691         /* for large moduli, enforce exponent limit */
692         if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
693                 {
694                 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
695                         {
696                         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
697                         return -1;
698                         }
699                 }
700         
701         if((ctx = BN_CTX_new()) == NULL) goto err;
702         BN_CTX_start(ctx);
703         f = BN_CTX_get(ctx);
704         ret = BN_CTX_get(ctx);
705         num=BN_num_bytes(rsa->n);
706         buf = OPENSSL_malloc(num);
707         if(!f || !ret || !buf)
708                 {
709                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
710                 goto err;
711                 }
712
713         /* This check was for equality but PGP does evil things
714          * and chops off the top '0' bytes */
715         if (flen > num)
716                 {
717                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
718                 goto err;
719                 }
720
721         if (BN_bin2bn(from,flen,f) == NULL) goto err;
722
723         if (BN_ucmp(f, rsa->n) >= 0)
724                 {
725                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
726                 goto err;
727                 }
728
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))
731                         goto err;
732
733         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
734                 rsa->_method_mod_n)) goto err;
735
736         if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
737                 if (!BN_sub(ret, rsa->n, ret)) goto err;
738
739         p=buf;
740         i=BN_bn2bin(ret,p);
741
742         switch (padding)
743                 {
744         case RSA_PKCS1_PADDING:
745                 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
746                 break;
747         case RSA_X931_PADDING:
748                 r=RSA_padding_check_X931(to,num,buf,i,num);
749                 break;
750         case RSA_NO_PADDING:
751                 r=RSA_padding_check_none(to,num,buf,i,num);
752                 break;
753         default:
754                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
755                 goto err;
756                 }
757         if (r < 0)
758                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
759
760 err:
761         if (ctx != NULL)
762                 {
763                 BN_CTX_end(ctx);
764                 BN_CTX_free(ctx);
765                 }
766         if (buf != NULL)
767                 {
768                 OPENSSL_cleanse(buf,num);
769                 OPENSSL_free(buf);
770                 }
771         return(r);
772         }
773
774 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
775         {
776         BIGNUM *r1,*m1,*vrfy;
777         BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
778         BIGNUM *dmp1,*dmq1,*c,*pr1;
779         int ret=0;
780
781         BN_CTX_start(ctx);
782         r1 = BN_CTX_get(ctx);
783         m1 = BN_CTX_get(ctx);
784         vrfy = BN_CTX_get(ctx);
785
786         {
787                 BIGNUM local_p, local_q;
788                 BIGNUM *p = NULL, *q = NULL;
789
790                 /* Make sure BN_mod_inverse in Montgomery intialization uses the
791                  * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
792                  */
793                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
794                         {
795                         BN_init(&local_p);
796                         p = &local_p;
797                         BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
798
799                         BN_init(&local_q);
800                         q = &local_q;
801                         BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
802                         }
803                 else
804                         {
805                         p = rsa->p;
806                         q = rsa->q;
807                         }
808
809                 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
810                         {
811                         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))
812                                 goto err;
813                         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))
814                                 goto err;
815                         }
816         }
817
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))
820                         goto err;
821
822         /* compute I mod q */
823         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
824                 {
825                 c = &local_c;
826                 BN_with_flags(c, I, BN_FLG_CONSTTIME);
827                 if (!BN_mod(r1,c,rsa->q,ctx)) goto err;
828                 }
829         else
830                 {
831                 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
832                 }
833
834         /* compute r1^dmq1 mod q */
835         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
836                 {
837                 dmq1 = &local_dmq1;
838                 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
839                 }
840         else
841                 dmq1 = rsa->dmq1;
842         if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
843                 rsa->_method_mod_q)) goto err;
844
845         /* compute I mod p */
846         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
847                 {
848                 c = &local_c;
849                 BN_with_flags(c, I, BN_FLG_CONSTTIME);
850                 if (!BN_mod(r1,c,rsa->p,ctx)) goto err;
851                 }
852         else
853                 {
854                 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
855                 }
856
857         /* compute r1^dmp1 mod p */
858         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
859                 {
860                 dmp1 = &local_dmp1;
861                 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
862                 }
863         else
864                 dmp1 = rsa->dmp1;
865         if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
866                 rsa->_method_mod_p)) goto err;
867
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;
873
874         if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
875
876         /* Turn BN_FLG_CONSTTIME flag on before division operation */
877         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
878                 {
879                 pr1 = &local_r1;
880                 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
881                 }
882         else
883                 pr1 = r1;
884         if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;
885
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]
892          */
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;
897
898         if (rsa->e && rsa->n)
899                 {
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))
910                         {
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. */
914
915                         BIGNUM local_d;
916                         BIGNUM *d = NULL;
917                 
918                         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
919                                 {
920                                 d = &local_d;
921                                 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
922                                 }
923                         else
924                                 d = rsa->d;
925                         if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
926                                                    rsa->_method_mod_n)) goto err;
927                         }
928                 }
929         ret=1;
930 err:
931         BN_CTX_end(ctx);
932         return(ret);
933         }
934
935 static int RSA_eay_init(RSA *rsa)
936         {
937 #ifdef OPENSSL_FIPS
938         FIPS_selftest_check();
939 #endif
940         rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
941         return(1);
942         }
943
944 static int RSA_eay_finish(RSA *rsa)
945         {
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);
952         return(1);
953         }
954
955 #endif