There was a need to support thread ID types that couldn't be reliably cast
[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
118 #ifndef RSA_NULL
119
120 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
121                 unsigned char *to, RSA *rsa,int padding);
122 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
123                 unsigned char *to, RSA *rsa,int padding);
124 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
125                 unsigned char *to, RSA *rsa,int padding);
126 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
127                 unsigned char *to, RSA *rsa,int padding);
128 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
129 static int RSA_eay_init(RSA *rsa);
130 static int RSA_eay_finish(RSA *rsa);
131 static RSA_METHOD rsa_pkcs1_eay_meth={
132         "Eric Young's PKCS#1 RSA",
133         RSA_eay_public_encrypt,
134         RSA_eay_public_decrypt, /* signature verification */
135         RSA_eay_private_encrypt, /* signing */
136         RSA_eay_private_decrypt,
137         RSA_eay_mod_exp,
138         BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
139         RSA_eay_init,
140         RSA_eay_finish,
141         0, /* flags */
142         NULL,
143         0, /* rsa_sign */
144         0, /* rsa_verify */
145         NULL /* rsa_keygen */
146         };
147
148 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
149         {
150         return(&rsa_pkcs1_eay_meth);
151         }
152
153 /* Usage example;
154  *    MONT_HELPER(rsa->_method_mod_p, bn_ctx, rsa->p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
155  */
156 #define MONT_HELPER(method_mod, ctx, m, pre_cond, err_instr) \
157         if ((pre_cond) && ((method_mod) == NULL) && \
158                         !BN_MONT_CTX_set_locked(&(method_mod), \
159                                 CRYPTO_LOCK_RSA, \
160                                 (m), (ctx))) \
161                 err_instr
162
163 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
164              unsigned char *to, RSA *rsa, int padding)
165         {
166         BIGNUM *f,*ret;
167         int i,j,k,num=0,r= -1;
168         unsigned char *buf=NULL;
169         BN_CTX *ctx=NULL;
170
171         if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
172                 {
173                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
174                 return -1;
175                 }
176
177         if (BN_ucmp(rsa->n, rsa->e) <= 0)
178                 {
179                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
180                 return -1;
181                 }
182
183         /* for large moduli, enforce exponent limit */
184         if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
185                 {
186                 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
187                         {
188                         RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
189                         return -1;
190                         }
191                 }
192         
193         if ((ctx=BN_CTX_new()) == NULL) goto err;
194         BN_CTX_start(ctx);
195         f = BN_CTX_get(ctx);
196         ret = BN_CTX_get(ctx);
197         num=BN_num_bytes(rsa->n);
198         buf = OPENSSL_malloc(num);
199         if (!f || !ret || !buf)
200                 {
201                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
202                 goto err;
203                 }
204
205         switch (padding)
206                 {
207         case RSA_PKCS1_PADDING:
208                 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
209                 break;
210 #ifndef OPENSSL_NO_SHA
211         case RSA_PKCS1_OAEP_PADDING:
212                 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
213                 break;
214 #endif
215         case RSA_SSLV23_PADDING:
216                 i=RSA_padding_add_SSLv23(buf,num,from,flen);
217                 break;
218         case RSA_NO_PADDING:
219                 i=RSA_padding_add_none(buf,num,from,flen);
220                 break;
221         default:
222                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
223                 goto err;
224                 }
225         if (i <= 0) goto err;
226
227         if (BN_bin2bn(buf,num,f) == NULL) goto err;
228         
229         if (BN_ucmp(f, rsa->n) >= 0)
230                 {
231                 /* usually the padding functions would catch this */
232                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
233                 goto err;
234                 }
235
236         MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
237
238         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
239                 rsa->_method_mod_n)) goto err;
240
241         /* put in leading 0 bytes if the number is less than the
242          * length of the modulus */
243         j=BN_num_bytes(ret);
244         i=BN_bn2bin(ret,&(to[num-j]));
245         for (k=0; k<(num-i); k++)
246                 to[k]=0;
247
248         r=num;
249 err:
250         if (ctx != NULL)
251                 {
252                 BN_CTX_end(ctx);
253                 BN_CTX_free(ctx);
254                 }
255         if (buf != NULL) 
256                 {
257                 OPENSSL_cleanse(buf,num);
258                 OPENSSL_free(buf);
259                 }
260         return(r);
261         }
262
263 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
264 {
265         BN_BLINDING *ret;
266         CRYPTO_THREADID tid;
267         int got_write_lock = 0;
268
269         CRYPTO_THREADID_set(&tid);
270         CRYPTO_r_lock(CRYPTO_LOCK_RSA);
271
272         if (rsa->blinding == NULL)
273                 {
274                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
275                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
276                 got_write_lock = 1;
277
278                 if (rsa->blinding == NULL)
279                         rsa->blinding = RSA_setup_blinding(rsa, ctx);
280                 }
281
282         ret = rsa->blinding;
283         if (ret == NULL)
284                 goto err;
285
286         if (!BN_BLINDING_cmp_thread(ret, &tid))
287                 {
288                 /* rsa->blinding is ours! */
289
290                 *local = 1;
291                 }
292         else
293                 {
294                 /* resort to rsa->mt_blinding instead */
295
296                 *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert()
297                              * that the BN_BLINDING is shared, meaning that accesses
298                              * require locks, and that the blinding factor must be
299                              * stored outside the BN_BLINDING
300                              */
301
302                 if (rsa->mt_blinding == NULL)
303                         {
304                         if (!got_write_lock)
305                                 {
306                                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
307                                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
308                                 got_write_lock = 1;
309                                 }
310                         
311                         if (rsa->mt_blinding == NULL)
312                                 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
313                         }
314                 ret = rsa->mt_blinding;
315                 }
316
317  err:
318         if (got_write_lock)
319                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
320         else
321                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
322         return ret;
323 }
324
325 static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
326         BIGNUM *r, BN_CTX *ctx)
327 {
328         if (local)
329                 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
330         else
331                 {
332                 int ret;
333                 CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING);
334                 ret = BN_BLINDING_convert_ex(f, r, b, ctx);
335                 CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING);
336                 return ret;
337                 }
338 }
339
340 static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
341         BIGNUM *r, BN_CTX *ctx)
342 {
343         if (local)
344                 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
345         else
346                 {
347                 int ret;
348                 CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
349                 ret = BN_BLINDING_invert_ex(f, r, b, ctx);
350                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
351                 return ret;
352                 }
353 }
354
355 /* signing */
356 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
357              unsigned char *to, RSA *rsa, int padding)
358         {
359         BIGNUM *f, *ret, *br, *res;
360         int i,j,k,num=0,r= -1;
361         unsigned char *buf=NULL;
362         BN_CTX *ctx=NULL;
363         int local_blinding = 0;
364         BN_BLINDING *blinding = NULL;
365
366         if ((ctx=BN_CTX_new()) == NULL) goto err;
367         BN_CTX_start(ctx);
368         f   = BN_CTX_get(ctx);
369         br  = BN_CTX_get(ctx);
370         ret = BN_CTX_get(ctx);
371         num = BN_num_bytes(rsa->n);
372         buf = OPENSSL_malloc(num);
373         if(!f || !ret || !buf)
374                 {
375                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
376                 goto err;
377                 }
378
379         switch (padding)
380                 {
381         case RSA_PKCS1_PADDING:
382                 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
383                 break;
384         case RSA_X931_PADDING:
385                 i=RSA_padding_add_X931(buf,num,from,flen);
386                 break;
387         case RSA_NO_PADDING:
388                 i=RSA_padding_add_none(buf,num,from,flen);
389                 break;
390         case RSA_SSLV23_PADDING:
391         default:
392                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
393                 goto err;
394                 }
395         if (i <= 0) goto err;
396
397         if (BN_bin2bn(buf,num,f) == NULL) goto err;
398         
399         if (BN_ucmp(f, rsa->n) >= 0)
400                 {       
401                 /* usually the padding functions would catch this */
402                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
403                 goto err;
404                 }
405
406         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
407                 {
408                 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
409                 if (blinding == NULL)
410                         {
411                         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
412                         goto err;
413                         }
414                 }
415         
416         if (blinding != NULL)
417                 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
418                         goto err;
419
420         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
421                 ((rsa->p != NULL) &&
422                 (rsa->q != NULL) &&
423                 (rsa->dmp1 != NULL) &&
424                 (rsa->dmq1 != NULL) &&
425                 (rsa->iqmp != NULL)) )
426                 { 
427                 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
428                 }
429         else
430                 {
431                 BIGNUM local_d;
432                 BIGNUM *d = NULL;
433                 
434                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
435                         {
436                         BN_init(&local_d);
437                         d = &local_d;
438                         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
439                         }
440                 else
441                         d= rsa->d;
442
443                 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
444
445                 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
446                                 rsa->_method_mod_n)) goto err;
447                 }
448
449         if (blinding)
450                 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
451                         goto err;
452
453         if (padding == RSA_X931_PADDING)
454                 {
455                 BN_sub(f, rsa->n, ret);
456                 if (BN_cmp(ret, f))
457                         res = f;
458                 else
459                         res = ret;
460                 }
461         else
462                 res = ret;
463
464         /* put in leading 0 bytes if the number is less than the
465          * length of the modulus */
466         j=BN_num_bytes(res);
467         i=BN_bn2bin(res,&(to[num-j]));
468         for (k=0; k<(num-i); k++)
469                 to[k]=0;
470
471         r=num;
472 err:
473         if (ctx != NULL)
474                 {
475                 BN_CTX_end(ctx);
476                 BN_CTX_free(ctx);
477                 }
478         if (buf != NULL)
479                 {
480                 OPENSSL_cleanse(buf,num);
481                 OPENSSL_free(buf);
482                 }
483         return(r);
484         }
485
486 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
487              unsigned char *to, RSA *rsa, int padding)
488         {
489         BIGNUM *f, *ret, *br;
490         int j,num=0,r= -1;
491         unsigned char *p;
492         unsigned char *buf=NULL;
493         BN_CTX *ctx=NULL;
494         int local_blinding = 0;
495         BN_BLINDING *blinding = NULL;
496
497         if((ctx = BN_CTX_new()) == NULL) goto err;
498         BN_CTX_start(ctx);
499         f   = BN_CTX_get(ctx);
500         br  = BN_CTX_get(ctx);
501         ret = BN_CTX_get(ctx);
502         num = BN_num_bytes(rsa->n);
503         buf = OPENSSL_malloc(num);
504         if(!f || !ret || !buf)
505                 {
506                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
507                 goto err;
508                 }
509
510         /* This check was for equality but PGP does evil things
511          * and chops off the top '0' bytes */
512         if (flen > num)
513                 {
514                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
515                 goto err;
516                 }
517
518         /* make data into a big number */
519         if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
520
521         if (BN_ucmp(f, rsa->n) >= 0)
522                 {
523                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
524                 goto err;
525                 }
526
527         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
528                 {
529                 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
530                 if (blinding == NULL)
531                         {
532                         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
533                         goto err;
534                         }
535                 }
536         
537         if (blinding != NULL)
538                 if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
539                         goto err;
540
541         /* do the decrypt */
542         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
543                 ((rsa->p != NULL) &&
544                 (rsa->q != NULL) &&
545                 (rsa->dmp1 != NULL) &&
546                 (rsa->dmq1 != NULL) &&
547                 (rsa->iqmp != NULL)) )
548                 {
549                 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
550                 }
551         else
552                 {
553                 BIGNUM local_d;
554                 BIGNUM *d = NULL;
555                 
556                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
557                         {
558                         d = &local_d;
559                         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
560                         }
561                 else
562                         d = rsa->d;
563
564                 MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
565                 if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
566                                 rsa->_method_mod_n))
567                   goto err;
568                 }
569
570         if (blinding)
571                 if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
572                         goto err;
573
574         p=buf;
575         j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
576
577         switch (padding)
578                 {
579         case RSA_PKCS1_PADDING:
580                 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
581                 break;
582 #ifndef OPENSSL_NO_SHA
583         case RSA_PKCS1_OAEP_PADDING:
584                 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
585                 break;
586 #endif
587         case RSA_SSLV23_PADDING:
588                 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
589                 break;
590         case RSA_NO_PADDING:
591                 r=RSA_padding_check_none(to,num,buf,j,num);
592                 break;
593         default:
594                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
595                 goto err;
596                 }
597         if (r < 0)
598                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
599
600 err:
601         if (ctx != NULL)
602                 {
603                 BN_CTX_end(ctx);
604                 BN_CTX_free(ctx);
605                 }
606         if (buf != NULL)
607                 {
608                 OPENSSL_cleanse(buf,num);
609                 OPENSSL_free(buf);
610                 }
611         return(r);
612         }
613
614 /* signature verification */
615 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
616              unsigned char *to, RSA *rsa, int padding)
617         {
618         BIGNUM *f,*ret;
619         int i,num=0,r= -1;
620         unsigned char *p;
621         unsigned char *buf=NULL;
622         BN_CTX *ctx=NULL;
623
624         if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
625                 {
626                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
627                 return -1;
628                 }
629
630         if (BN_ucmp(rsa->n, rsa->e) <= 0)
631                 {
632                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
633                 return -1;
634                 }
635
636         /* for large moduli, enforce exponent limit */
637         if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
638                 {
639                 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
640                         {
641                         RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
642                         return -1;
643                         }
644                 }
645         
646         if((ctx = BN_CTX_new()) == NULL) goto err;
647         BN_CTX_start(ctx);
648         f = BN_CTX_get(ctx);
649         ret = BN_CTX_get(ctx);
650         num=BN_num_bytes(rsa->n);
651         buf = OPENSSL_malloc(num);
652         if(!f || !ret || !buf)
653                 {
654                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
655                 goto err;
656                 }
657
658         /* This check was for equality but PGP does evil things
659          * and chops off the top '0' bytes */
660         if (flen > num)
661                 {
662                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
663                 goto err;
664                 }
665
666         if (BN_bin2bn(from,flen,f) == NULL) goto err;
667
668         if (BN_ucmp(f, rsa->n) >= 0)
669                 {
670                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
671                 goto err;
672                 }
673
674         MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
675
676         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
677                 rsa->_method_mod_n)) goto err;
678
679         if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
680                 BN_sub(ret, rsa->n, ret);
681
682         p=buf;
683         i=BN_bn2bin(ret,p);
684
685         switch (padding)
686                 {
687         case RSA_PKCS1_PADDING:
688                 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
689                 break;
690         case RSA_X931_PADDING:
691                 r=RSA_padding_check_X931(to,num,buf,i,num);
692                 break;
693         case RSA_NO_PADDING:
694                 r=RSA_padding_check_none(to,num,buf,i,num);
695                 break;
696         default:
697                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
698                 goto err;
699                 }
700         if (r < 0)
701                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
702
703 err:
704         if (ctx != NULL)
705                 {
706                 BN_CTX_end(ctx);
707                 BN_CTX_free(ctx);
708                 }
709         if (buf != NULL)
710                 {
711                 OPENSSL_cleanse(buf,num);
712                 OPENSSL_free(buf);
713                 }
714         return(r);
715         }
716
717 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
718         {
719         BIGNUM *r1,*m1,*vrfy;
720         BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
721         BIGNUM *dmp1,*dmq1,*c,*pr1;
722         int ret=0;
723
724         BN_CTX_start(ctx);
725         r1 = BN_CTX_get(ctx);
726         m1 = BN_CTX_get(ctx);
727         vrfy = BN_CTX_get(ctx);
728
729         {
730                 BIGNUM local_p, local_q;
731                 BIGNUM *p = NULL, *q = NULL;
732
733                 /* Make sure BN_mod_inverse in Montgomery intialization uses the
734                  * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
735                  */
736                 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
737                         {
738                         BN_init(&local_p);
739                         p = &local_p;
740                         BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
741
742                         BN_init(&local_q);
743                         q = &local_q;
744                         BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
745                         }
746                 else
747                         {
748                         p = rsa->p;
749                         q = rsa->q;
750                         }
751
752                 MONT_HELPER(rsa->_method_mod_p, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
753                 MONT_HELPER(rsa->_method_mod_q, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
754         }
755
756         MONT_HELPER(rsa->_method_mod_n, ctx, rsa->n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
757
758         /* compute I mod q */
759         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
760                 {
761                 c = &local_c;
762                 BN_with_flags(c, I, BN_FLG_CONSTTIME);
763                 if (!BN_mod(r1,c,rsa->q,ctx)) goto err;
764                 }
765         else
766                 {
767                 if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
768                 }
769
770         /* compute r1^dmq1 mod q */
771         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
772                 {
773                 dmq1 = &local_dmq1;
774                 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
775                 }
776         else
777                 dmq1 = rsa->dmq1;
778         if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
779                 rsa->_method_mod_q)) goto err;
780
781         /* compute I mod p */
782         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
783                 {
784                 c = &local_c;
785                 BN_with_flags(c, I, BN_FLG_CONSTTIME);
786                 if (!BN_mod(r1,c,rsa->p,ctx)) goto err;
787                 }
788         else
789                 {
790                 if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
791                 }
792
793         /* compute r1^dmp1 mod p */
794         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
795                 {
796                 dmp1 = &local_dmp1;
797                 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
798                 }
799         else
800                 dmp1 = rsa->dmp1;
801         if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
802                 rsa->_method_mod_p)) goto err;
803
804         if (!BN_sub(r0,r0,m1)) goto err;
805         /* This will help stop the size of r0 increasing, which does
806          * affect the multiply if it optimised for a power of 2 size */
807         if (BN_is_negative(r0))
808                 if (!BN_add(r0,r0,rsa->p)) goto err;
809
810         if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
811
812         /* Turn BN_FLG_CONSTTIME flag on before division operation */
813         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
814                 {
815                 pr1 = &local_r1;
816                 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
817                 }
818         else
819                 pr1 = r1;
820         if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;
821
822         /* If p < q it is occasionally possible for the correction of
823          * adding 'p' if r0 is negative above to leave the result still
824          * negative. This can break the private key operations: the following
825          * second correction should *always* correct this rare occurrence.
826          * This will *never* happen with OpenSSL generated keys because
827          * they ensure p > q [steve]
828          */
829         if (BN_is_negative(r0))
830                 if (!BN_add(r0,r0,rsa->p)) goto err;
831         if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
832         if (!BN_add(r0,r1,m1)) goto err;
833
834         if (rsa->e && rsa->n)
835                 {
836                 if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
837                 /* If 'I' was greater than (or equal to) rsa->n, the operation
838                  * will be equivalent to using 'I mod n'. However, the result of
839                  * the verify will *always* be less than 'n' so we don't check
840                  * for absolute equality, just congruency. */
841                 if (!BN_sub(vrfy, vrfy, I)) goto err;
842                 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
843                 if (BN_is_negative(vrfy))
844                         if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
845                 if (!BN_is_zero(vrfy))
846                         {
847                         /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
848                          * miscalculated CRT output, just do a raw (slower)
849                          * mod_exp and return that instead. */
850
851                         BIGNUM local_d;
852                         BIGNUM *d = NULL;
853                 
854                         if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
855                                 {
856                                 d = &local_d;
857                                 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
858                                 }
859                         else
860                                 d = rsa->d;
861                         if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
862                                                    rsa->_method_mod_n)) goto err;
863                         }
864                 }
865         ret=1;
866 err:
867         BN_CTX_end(ctx);
868         return(ret);
869         }
870
871 static int RSA_eay_init(RSA *rsa)
872         {
873         rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
874         return(1);
875         }
876
877 static int RSA_eay_finish(RSA *rsa)
878         {
879         if (rsa->_method_mod_n != NULL)
880                 BN_MONT_CTX_free(rsa->_method_mod_n);
881         if (rsa->_method_mod_p != NULL)
882                 BN_MONT_CTX_free(rsa->_method_mod_p);
883         if (rsa->_method_mod_q != NULL)
884                 BN_MONT_CTX_free(rsa->_method_mod_q);
885         return(1);
886         }
887
888 #endif