3ee753ec86d1f3027afcfe0f04ce14fbdf0176b4
[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 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/bn.h>
62 #include <openssl/rsa.h>
63 #include <openssl/rand.h>
64
65 #ifndef RSA_NULL
66
67 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
68                 unsigned char *to, RSA *rsa,int padding);
69 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
70                 unsigned char *to, RSA *rsa,int padding);
71 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
72                 unsigned char *to, RSA *rsa,int padding);
73 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
74                 unsigned char *to, RSA *rsa,int padding);
75 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
76 static int RSA_eay_init(RSA *rsa);
77 static int RSA_eay_finish(RSA *rsa);
78 static RSA_METHOD rsa_pkcs1_eay_meth={
79         "Eric Young's PKCS#1 RSA",
80         RSA_eay_public_encrypt,
81         RSA_eay_public_decrypt, /* signature verification */
82         RSA_eay_private_encrypt, /* signing */
83         RSA_eay_private_decrypt,
84         RSA_eay_mod_exp,
85         BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
86         RSA_eay_init,
87         RSA_eay_finish,
88         0, /* flags */
89         NULL,
90         0, /* rsa_sign */
91         0, /* rsa_verify */
92         NULL /* rsa_keygen */
93         };
94
95 const RSA_METHOD *RSA_PKCS1_SSLeay(void)
96         {
97         return(&rsa_pkcs1_eay_meth);
98         }
99
100 /* Static helper to reduce oodles of code duplication. As a slight
101  * optimisation, the "MONT_HELPER() macro must be used as front-end to this
102  * function, to prevent unnecessary function calls - there is an initial test
103  * that is performed by the macro-generated code. */
104 static int rsa_eay_mont_helper(BN_MONT_CTX **ptr, const BIGNUM *modulus, BN_CTX *ctx)
105         {
106         BN_MONT_CTX *bn_mont_ctx;
107         if((bn_mont_ctx = BN_MONT_CTX_new()) == NULL)
108                 return 0;
109         if(!BN_MONT_CTX_set(bn_mont_ctx, modulus, ctx))
110                 {
111                 BN_MONT_CTX_free(bn_mont_ctx);
112                 return 0;
113                 }
114         if (*ptr == NULL) /* other thread may have finished first */
115                 {
116                 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
117                 if (*ptr == NULL) /* check again in the lock to stop races */
118                         {
119                         *ptr = bn_mont_ctx;
120                         bn_mont_ctx = NULL;
121                         }
122                 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
123                 }
124         if (bn_mont_ctx)
125                 BN_MONT_CTX_free(bn_mont_ctx);
126         return 1;
127         }
128 /* Usage example;
129  *    MONT_HELPER(rsa, bn_ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
130  */
131 #define MONT_HELPER(rsa, ctx, m, pre_cond, err_instr) \
132         if((pre_cond) && ((rsa)->_method_mod_##m == NULL) && \
133                         !rsa_eay_mont_helper(&((rsa)->_method_mod_##m), \
134                                 (rsa)->m, (ctx))) \
135                 err_instr
136
137 static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
138              unsigned char *to, RSA *rsa, int padding)
139         {
140         BIGNUM *f,*ret;
141         int i,j,k,num=0,r= -1;
142         unsigned char *buf=NULL;
143         BN_CTX *ctx=NULL;
144
145         if ((ctx=BN_CTX_new()) == NULL) goto err;
146         BN_CTX_start(ctx);
147         f = BN_CTX_get(ctx);
148         ret = BN_CTX_get(ctx);
149         num=BN_num_bytes(rsa->n);
150         buf = OPENSSL_malloc(num);
151         if (!f || !ret || !buf)
152                 {
153                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
154                 goto err;
155                 }
156
157         switch (padding)
158                 {
159         case RSA_PKCS1_PADDING:
160                 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
161                 break;
162 #ifndef OPENSSL_NO_SHA
163         case RSA_PKCS1_OAEP_PADDING:
164                 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
165                 break;
166 #endif
167         case RSA_SSLV23_PADDING:
168                 i=RSA_padding_add_SSLv23(buf,num,from,flen);
169                 break;
170         case RSA_NO_PADDING:
171                 i=RSA_padding_add_none(buf,num,from,flen);
172                 break;
173         default:
174                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
175                 goto err;
176                 }
177         if (i <= 0) goto err;
178
179         if (BN_bin2bn(buf,num,f) == NULL) goto err;
180         
181         if (BN_ucmp(f, rsa->n) >= 0)
182                 {       
183                 /* usually the padding functions would catch this */
184                 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
185                 goto err;
186                 }
187
188         MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
189
190         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
191                 rsa->_method_mod_n)) goto err;
192
193         /* put in leading 0 bytes if the number is less than the
194          * length of the modulus */
195         j=BN_num_bytes(ret);
196         i=BN_bn2bin(ret,&(to[num-j]));
197         for (k=0; k<(num-i); k++)
198                 to[k]=0;
199
200         r=num;
201 err:
202         if (ctx != NULL)
203                 {
204                 BN_CTX_end(ctx);
205                 BN_CTX_free(ctx);
206                 }
207         if (buf != NULL) 
208                 {
209                 OPENSSL_cleanse(buf,num);
210                 OPENSSL_free(buf);
211                 }
212         return(r);
213         }
214
215 static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
216         {
217         int ret = 1;
218         CRYPTO_w_lock(CRYPTO_LOCK_RSA);
219         /* Check again inside the lock - the macro's check is racey */
220         if(rsa->blinding == NULL)
221                 ret = RSA_blinding_on(rsa, ctx);
222         CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
223         return ret;
224         }
225
226 #define BLINDING_HELPER(rsa, ctx, err_instr) \
227         do { \
228                 if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
229                     ((rsa)->blinding == NULL) && \
230                     !rsa_eay_blinding(rsa, ctx)) \
231                         err_instr \
232         } while(0)
233
234 static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
235         {
236         BIGNUM *A, *Ai;
237         BN_BLINDING *ret = NULL;
238
239         /* added in OpenSSL 0.9.6j and 0.9.7b */
240
241         /* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
242          * this should be placed in a new function of its own, but for reasons
243          * of binary compatibility can't */
244
245         BN_CTX_start(ctx);
246         A = BN_CTX_get(ctx);
247         if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
248                 {
249                 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
250                 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);
251                 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
252                 }
253         else
254                 {
255                 if (!BN_rand_range(A,rsa->n)) goto err;
256                 }
257         if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
258
259         if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
260                 goto err;
261         ret = BN_BLINDING_new(A,Ai,rsa->n);
262         BN_free(Ai);
263 err:
264         BN_CTX_end(ctx);
265         return ret;
266         }
267
268 /* signing */
269 static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
270              unsigned char *to, RSA *rsa, int padding)
271         {
272         BIGNUM *f,*ret;
273         int i,j,k,num=0,r= -1;
274         unsigned char *buf=NULL;
275         BN_CTX *ctx=NULL;
276         int local_blinding = 0;
277         BN_BLINDING *blinding = NULL;
278
279         if ((ctx=BN_CTX_new()) == NULL) goto err;
280         BN_CTX_start(ctx);
281         f = BN_CTX_get(ctx);
282         ret = BN_CTX_get(ctx);
283         num=BN_num_bytes(rsa->n);
284         buf = OPENSSL_malloc(num);
285         if(!f || !ret || !buf)
286                 {
287                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
288                 goto err;
289                 }
290
291         switch (padding)
292                 {
293         case RSA_PKCS1_PADDING:
294                 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
295                 break;
296         case RSA_NO_PADDING:
297                 i=RSA_padding_add_none(buf,num,from,flen);
298                 break;
299         case RSA_SSLV23_PADDING:
300         default:
301                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
302                 goto err;
303                 }
304         if (i <= 0) goto err;
305
306         if (BN_bin2bn(buf,num,f) == NULL) goto err;
307         
308         if (BN_ucmp(f, rsa->n) >= 0)
309                 {       
310                 /* usually the padding functions would catch this */
311                 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
312                 goto err;
313                 }
314
315         BLINDING_HELPER(rsa, ctx, goto err;);
316         blinding = rsa->blinding;
317         
318         /* Now unless blinding is disabled, 'blinding' is non-NULL.
319          * But the BN_BLINDING object may be owned by some other thread
320          * (we don't want to keep it constant and we don't want to use
321          * lots of locking to avoid race conditions, so only a single
322          * thread can use it; other threads have to use local blinding
323          * factors) */
324         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
325                 {
326                 if (blinding == NULL)
327                         {
328                         RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
329                         goto err;
330                         }
331                 }
332         
333         if (blinding != NULL)
334                 {
335                 if (blinding->thread_id != CRYPTO_thread_id())
336                         {
337                         /* we need a local one-time blinding factor */
338
339                         blinding = setup_blinding(rsa, ctx);
340                         if (blinding == NULL)
341                                 goto err;
342                         local_blinding = 1;
343                         }
344                 }
345
346         if (blinding)
347                 if (!BN_BLINDING_convert(f, blinding, ctx)) goto err;
348
349         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
350                 ((rsa->p != NULL) &&
351                 (rsa->q != NULL) &&
352                 (rsa->dmp1 != NULL) &&
353                 (rsa->dmq1 != NULL) &&
354                 (rsa->iqmp != NULL)) )
355                 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa,ctx)) goto err; }
356         else
357                 {
358                 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
359                 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx,
360                                 rsa->_method_mod_n)) goto err;
361                 }
362
363         if (blinding)
364                 if (!BN_BLINDING_invert(ret, blinding, ctx)) goto err;
365
366         /* put in leading 0 bytes if the number is less than the
367          * length of the modulus */
368         j=BN_num_bytes(ret);
369         i=BN_bn2bin(ret,&(to[num-j]));
370         for (k=0; k<(num-i); k++)
371                 to[k]=0;
372
373         r=num;
374 err:
375         if (ctx != NULL)
376                 {
377                 BN_CTX_end(ctx);
378                 BN_CTX_free(ctx);
379                 }
380         if (local_blinding)
381                 BN_BLINDING_free(blinding);
382         if (buf != NULL)
383                 {
384                 OPENSSL_cleanse(buf,num);
385                 OPENSSL_free(buf);
386                 }
387         return(r);
388         }
389
390 static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
391              unsigned char *to, RSA *rsa, int padding)
392         {
393         BIGNUM *f,*ret;
394         int j,num=0,r= -1;
395         unsigned char *p;
396         unsigned char *buf=NULL;
397         BN_CTX *ctx=NULL;
398         int local_blinding = 0;
399         BN_BLINDING *blinding = NULL;
400
401         if((ctx = BN_CTX_new()) == NULL) goto err;
402         BN_CTX_start(ctx);
403         f = BN_CTX_get(ctx);
404         ret = BN_CTX_get(ctx);
405         num=BN_num_bytes(rsa->n);
406         buf = OPENSSL_malloc(num);
407         if(!f || !ret || !buf)
408                 {
409                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
410                 goto err;
411                 }
412
413         /* This check was for equality but PGP does evil things
414          * and chops off the top '0' bytes */
415         if (flen > num)
416                 {
417                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
418                 goto err;
419                 }
420
421         /* make data into a big number */
422         if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
423
424         if (BN_ucmp(f, rsa->n) >= 0)
425                 {
426                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
427                 goto err;
428                 }
429
430         BLINDING_HELPER(rsa, ctx, goto err;);
431         blinding = rsa->blinding;
432         
433         /* Now unless blinding is disabled, 'blinding' is non-NULL.
434          * But the BN_BLINDING object may be owned by some other thread
435          * (we don't want to keep it constant and we don't want to use
436          * lots of locking to avoid race conditions, so only a single
437          * thread can use it; other threads have to use local blinding
438          * factors) */
439         if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
440                 {
441                 if (blinding == NULL)
442                         {
443                         RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
444                         goto err;
445                         }
446                 }
447         
448         if (blinding != NULL)
449                 {
450                 if (blinding->thread_id != CRYPTO_thread_id())
451                         {
452                         /* we need a local one-time blinding factor */
453
454                         blinding = setup_blinding(rsa, ctx);
455                         if (blinding == NULL)
456                                 goto err;
457                         local_blinding = 1;
458                         }
459                 }
460
461         if (blinding)
462                 if (!BN_BLINDING_convert(f, blinding, ctx)) goto err;
463
464         /* do the decrypt */
465         if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
466                 ((rsa->p != NULL) &&
467                 (rsa->q != NULL) &&
468                 (rsa->dmp1 != NULL) &&
469                 (rsa->dmq1 != NULL) &&
470                 (rsa->iqmp != NULL)) )
471                 { if (!rsa->meth->rsa_mod_exp(ret,f,rsa,ctx)) goto err; }
472         else
473                 {
474                 MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
475                 if (!rsa->meth->bn_mod_exp(ret,f,rsa->d,rsa->n,ctx,
476                                 rsa->_method_mod_n))
477                         goto err;
478                 }
479
480         if (blinding)
481                 if (!BN_BLINDING_invert(ret, blinding, ctx)) goto err;
482
483         p=buf;
484         j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
485
486         switch (padding)
487                 {
488         case RSA_PKCS1_PADDING:
489                 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
490                 break;
491 #ifndef OPENSSL_NO_SHA
492         case RSA_PKCS1_OAEP_PADDING:
493                 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
494                 break;
495 #endif
496         case RSA_SSLV23_PADDING:
497                 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
498                 break;
499         case RSA_NO_PADDING:
500                 r=RSA_padding_check_none(to,num,buf,j,num);
501                 break;
502         default:
503                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
504                 goto err;
505                 }
506         if (r < 0)
507                 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
508
509 err:
510         if (ctx != NULL)
511                 {
512                 BN_CTX_end(ctx);
513                 BN_CTX_free(ctx);
514                 }
515         if (local_blinding)
516                 BN_BLINDING_free(blinding);
517         if (buf != NULL)
518                 {
519                 OPENSSL_cleanse(buf,num);
520                 OPENSSL_free(buf);
521                 }
522         return(r);
523         }
524
525 /* signature verification */
526 static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
527              unsigned char *to, RSA *rsa, int padding)
528         {
529         BIGNUM *f,*ret;
530         int i,num=0,r= -1;
531         unsigned char *p;
532         unsigned char *buf=NULL;
533         BN_CTX *ctx=NULL;
534
535         if((ctx = BN_CTX_new()) == NULL) goto err;
536         BN_CTX_start(ctx);
537         f = BN_CTX_get(ctx);
538         ret = BN_CTX_get(ctx);
539         num=BN_num_bytes(rsa->n);
540         buf = OPENSSL_malloc(num);
541         if(!f || !ret || !buf)
542                 {
543                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
544                 goto err;
545                 }
546
547         /* This check was for equality but PGP does evil things
548          * and chops off the top '0' bytes */
549         if (flen > num)
550                 {
551                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
552                 goto err;
553                 }
554
555         if (BN_bin2bn(from,flen,f) == NULL) goto err;
556
557         if (BN_ucmp(f, rsa->n) >= 0)
558                 {
559                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
560                 goto err;
561                 }
562
563         MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
564
565         if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
566                 rsa->_method_mod_n)) goto err;
567
568         p=buf;
569         i=BN_bn2bin(ret,p);
570
571         switch (padding)
572                 {
573         case RSA_PKCS1_PADDING:
574                 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
575                 break;
576         case RSA_NO_PADDING:
577                 r=RSA_padding_check_none(to,num,buf,i,num);
578                 break;
579         default:
580                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
581                 goto err;
582                 }
583         if (r < 0)
584                 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
585
586 err:
587         if (ctx != NULL)
588                 {
589                 BN_CTX_end(ctx);
590                 BN_CTX_free(ctx);
591                 }
592         if (buf != NULL)
593                 {
594                 OPENSSL_cleanse(buf,num);
595                 OPENSSL_free(buf);
596                 }
597         return(r);
598         }
599
600 static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
601         {
602         BIGNUM *r1,*m1,*vrfy;
603         int ret=0;
604
605         BN_CTX_start(ctx);
606         r1 = BN_CTX_get(ctx);
607         m1 = BN_CTX_get(ctx);
608         vrfy = BN_CTX_get(ctx);
609
610         MONT_HELPER(rsa, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
611         MONT_HELPER(rsa, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
612         MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
613
614         if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
615         if (!rsa->meth->bn_mod_exp(m1,r1,rsa->dmq1,rsa->q,ctx,
616                 rsa->_method_mod_q)) goto err;
617
618         if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
619         if (!rsa->meth->bn_mod_exp(r0,r1,rsa->dmp1,rsa->p,ctx,
620                 rsa->_method_mod_p)) goto err;
621
622         if (!BN_sub(r0,r0,m1)) goto err;
623         /* This will help stop the size of r0 increasing, which does
624          * affect the multiply if it optimised for a power of 2 size */
625         if (BN_is_negative(r0))
626                 if (!BN_add(r0,r0,rsa->p)) goto err;
627
628         if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
629         if (!BN_mod(r0,r1,rsa->p,ctx)) goto err;
630         /* If p < q it is occasionally possible for the correction of
631          * adding 'p' if r0 is negative above to leave the result still
632          * negative. This can break the private key operations: the following
633          * second correction should *always* correct this rare occurrence.
634          * This will *never* happen with OpenSSL generated keys because
635          * they ensure p > q [steve]
636          */
637         if (BN_is_negative(r0))
638                 if (!BN_add(r0,r0,rsa->p)) goto err;
639         if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
640         if (!BN_add(r0,r1,m1)) goto err;
641
642         if (rsa->e && rsa->n)
643                 {
644                 if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
645                 /* If 'I' was greater than (or equal to) rsa->n, the operation
646                  * will be equivalent to using 'I mod n'. However, the result of
647                  * the verify will *always* be less than 'n' so we don't check
648                  * for absolute equality, just congruency. */
649                 if (!BN_sub(vrfy, vrfy, I)) goto err;
650                 if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
651                 if (BN_is_negative(vrfy))
652                         if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
653                 if (!BN_is_zero(vrfy))
654                         /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
655                          * miscalculated CRT output, just do a raw (slower)
656                          * mod_exp and return that instead. */
657                         if (!rsa->meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,
658                                         rsa->_method_mod_n)) goto err;
659                 }
660         ret=1;
661 err:
662         BN_CTX_end(ctx);
663         return(ret);
664         }
665
666 static int RSA_eay_init(RSA *rsa)
667         {
668         rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
669         return(1);
670         }
671
672 static int RSA_eay_finish(RSA *rsa)
673         {
674         if (rsa->_method_mod_n != NULL)
675                 BN_MONT_CTX_free(rsa->_method_mod_n);
676         if (rsa->_method_mod_p != NULL)
677                 BN_MONT_CTX_free(rsa->_method_mod_p);
678         if (rsa->_method_mod_q != NULL)
679                 BN_MONT_CTX_free(rsa->_method_mod_q);
680         return(1);
681         }
682
683 #endif