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