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