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