rsa_eay.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* crypto/rsa/rsa_eay.c */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>
#include "cryptlib.h"
61 62 63
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
64
#include <openssl/engine.h>
65

66 67
#ifndef RSA_NULL

R
Richard Levitte 已提交
68
static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
69
		unsigned char *to, RSA *rsa,int padding);
R
Richard Levitte 已提交
70
static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
71
		unsigned char *to, RSA *rsa,int padding);
R
Richard Levitte 已提交
72
static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
73
		unsigned char *to, RSA *rsa,int padding);
R
Richard Levitte 已提交
74
static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
75
		unsigned char *to, RSA *rsa,int padding);
R
Richard Levitte 已提交
76
static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa);
77 78 79 80 81
static int RSA_eay_init(RSA *rsa);
static int RSA_eay_finish(RSA *rsa);
static RSA_METHOD rsa_pkcs1_eay_meth={
	"Eric Young's PKCS#1 RSA",
	RSA_eay_public_encrypt,
B
Bodo Möller 已提交
82 83
	RSA_eay_public_decrypt, /* signature verification */
	RSA_eay_private_encrypt, /* signing */
84 85
	RSA_eay_private_decrypt,
	RSA_eay_mod_exp,
B
Bodo Möller 已提交
86
	BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
87 88
	RSA_eay_init,
	RSA_eay_finish,
B
Bodo Möller 已提交
89
	0, /* flags */
90
	NULL,
B
Bodo Möller 已提交
91 92
	0, /* rsa_sign */
	0  /* rsa_verify */
93 94
	};

95
const RSA_METHOD *RSA_PKCS1_SSLeay(void)
96 97 98 99
	{
	return(&rsa_pkcs1_eay_meth);
	}

R
Richard Levitte 已提交
100
static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
U
Ulf Möller 已提交
101
	     unsigned char *to, RSA *rsa, int padding)
102
	{
103
	const RSA_METHOD *meth;
104
	BIGNUM f,ret;
105 106 107 108
	int i,j,k,num=0,r= -1;
	unsigned char *buf=NULL;
	BN_CTX *ctx=NULL;

109
	meth = ENGINE_get_RSA(rsa->engine);
110 111
	BN_init(&f);
	BN_init(&ret);
112 113
	if ((ctx=BN_CTX_new()) == NULL) goto err;
	num=BN_num_bytes(rsa->n);
114
	if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
115 116 117 118 119 120 121 122 123 124
		{
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
		goto err;
		}

	switch (padding)
		{
	case RSA_PKCS1_PADDING:
		i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
		break;
125
#ifndef OPENSSL_NO_SHA
B
Ben Laurie 已提交
126 127 128
	case RSA_PKCS1_OAEP_PADDING:
	        i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
		break;
129
#endif
130 131 132 133 134 135 136 137 138 139 140 141
	case RSA_SSLV23_PADDING:
		i=RSA_padding_add_SSLv23(buf,num,from,flen);
		break;
	case RSA_NO_PADDING:
		i=RSA_padding_add_none(buf,num,from,flen);
		break;
	default:
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
		goto err;
		}
	if (i <= 0) goto err;

142
	if (BN_bin2bn(buf,num,&f) == NULL) goto err;
143
	
B
Bodo Möller 已提交
144 145 146 147 148 149 150
	if (BN_ucmp(&f, rsa->n) >= 0)
		{	
		/* usually the padding functions would catch this */
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
		goto err;
		}

151
	if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
152
		{
153 154 155 156
		BN_MONT_CTX* bn_mont_ctx;
		if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
			goto err;
		if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
157
			{
158 159 160 161 162 163 164
			BN_MONT_CTX_free(bn_mont_ctx);
			goto err;
			}
		if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
			{
			CRYPTO_w_lock(CRYPTO_LOCK_RSA);
			if (rsa->_method_mod_n == NULL)
165
				{
166 167
				rsa->_method_mod_n = bn_mont_ctx;
				bn_mont_ctx = NULL;
168
				}
169
			CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
170
			}
171 172
		if (bn_mont_ctx)
			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 179

	/* put in leading 0 bytes if the number is less than the
	 * length of the modulus */
180 181
	j=BN_num_bytes(&ret);
	i=BN_bn2bin(&ret,&(to[num-j]));
182 183 184 185 186 187
	for (k=0; k<(num-i); k++)
		to[k]=0;

	r=num;
err:
	if (ctx != NULL) BN_CTX_free(ctx);
188 189
	BN_clear_free(&f);
	BN_clear_free(&ret);
190 191 192
	if (buf != NULL) 
		{
		memset(buf,0,num);
193
		OPENSSL_free(buf);
194 195 196 197
		}
	return(r);
	}

B
Bodo Möller 已提交
198
/* signing */
R
Richard Levitte 已提交
199
static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
U
Ulf Möller 已提交
200
	     unsigned char *to, RSA *rsa, int padding)
201
	{
202
	const RSA_METHOD *meth;
203
	BIGNUM f,ret;
204 205 206 207
	int i,j,k,num=0,r= -1;
	unsigned char *buf=NULL;
	BN_CTX *ctx=NULL;

208
	meth = ENGINE_get_RSA(rsa->engine);
209 210 211
	BN_init(&f);
	BN_init(&ret);

212 213
	if ((ctx=BN_CTX_new()) == NULL) goto err;
	num=BN_num_bytes(rsa->n);
214
	if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		{
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
		goto err;
		}

	switch (padding)
		{
	case RSA_PKCS1_PADDING:
		i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
		break;
	case RSA_NO_PADDING:
		i=RSA_padding_add_none(buf,num,from,flen);
		break;
	case RSA_SSLV23_PADDING:
	default:
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
		goto err;
		}
	if (i <= 0) goto err;

235
	if (BN_bin2bn(buf,num,&f) == NULL) goto err;
B
Bodo Möller 已提交
236 237 238 239 240 241 242
	
	if (BN_ucmp(&f, rsa->n) >= 0)
		{	
		/* usually the padding functions would catch this */
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
		goto err;
		}
243 244 245 246

	if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
		RSA_blinding_on(rsa,ctx);
	if (rsa->flags & RSA_FLAG_BLINDING)
247
		if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
248

249 250
	if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
		((rsa->p != NULL) &&
251 252 253
		(rsa->q != NULL) &&
		(rsa->dmp1 != NULL) &&
		(rsa->dmq1 != NULL) &&
254
		(rsa->iqmp != NULL)) )
255
		{ if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
256 257
	else
		{
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 265

	/* put in leading 0 bytes if the number is less than the
	 * length of the modulus */
266 267
	j=BN_num_bytes(&ret);
	i=BN_bn2bin(&ret,&(to[num-j]));
268 269 270 271 272 273
	for (k=0; k<(num-i); k++)
		to[k]=0;

	r=num;
err:
	if (ctx != NULL) BN_CTX_free(ctx);
274 275
	BN_clear_free(&ret);
	BN_clear_free(&f);
276 277 278
	if (buf != NULL)
		{
		memset(buf,0,num);
279
		OPENSSL_free(buf);
280 281 282 283
		}
	return(r);
	}

R
Richard Levitte 已提交
284
static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
U
Ulf Möller 已提交
285
	     unsigned char *to, RSA *rsa, int padding)
286
	{
287
	const RSA_METHOD *meth;
288
	BIGNUM f,ret;
289 290 291 292 293
	int j,num=0,r= -1;
	unsigned char *p;
	unsigned char *buf=NULL;
	BN_CTX *ctx=NULL;

294
	meth = ENGINE_get_RSA(rsa->engine);
295 296
	BN_init(&f);
	BN_init(&ret);
297 298 299 300 301
	ctx=BN_CTX_new();
	if (ctx == NULL) goto err;

	num=BN_num_bytes(rsa->n);

302
	if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
303 304 305 306 307
		{
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
		goto err;
		}

U
Ulf Möller 已提交
308
	/* This check was for equality but PGP does evil things
309 310 311 312 313 314 315 316
	 * and chops off the top '0' bytes */
	if (flen > num)
		{
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
		goto err;
		}

	/* make data into a big number */
317
	if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
318

B
Bodo Möller 已提交
319 320 321 322 323 324
	if (BN_ucmp(&f, rsa->n) >= 0)
		{
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
		goto err;
		}

325 326 327
	if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
		RSA_blinding_on(rsa,ctx);
	if (rsa->flags & RSA_FLAG_BLINDING)
328
		if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
329 330

	/* do the decrypt */
331 332
	if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
		((rsa->p != NULL) &&
333 334 335
		(rsa->q != NULL) &&
		(rsa->dmp1 != NULL) &&
		(rsa->dmq1 != NULL) &&
336
		(rsa->iqmp != NULL)) )
337
		{ if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
338 339
	else
		{
340
		if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
341 342 343 344
			goto err;
		}

	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 351 352

	switch (padding)
		{
	case RSA_PKCS1_PADDING:
353
		r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
354
		break;
355
#ifndef OPENSSL_NO_SHA
B
Ben Laurie 已提交
356 357 358
        case RSA_PKCS1_OAEP_PADDING:
	        r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
                break;
359
#endif
B
Ben Laurie 已提交
360
 	case RSA_SSLV23_PADDING:
361
		r=RSA_padding_check_SSLv23(to,num,buf,j,num);
362 363
		break;
	case RSA_NO_PADDING:
364
		r=RSA_padding_check_none(to,num,buf,j,num);
365 366 367 368 369 370 371 372 373 374
		break;
	default:
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
		goto err;
		}
	if (r < 0)
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);

err:
	if (ctx != NULL) BN_CTX_free(ctx);
375 376
	BN_clear_free(&f);
	BN_clear_free(&ret);
377 378 379
	if (buf != NULL)
		{
		memset(buf,0,num);
380
		OPENSSL_free(buf);
381 382 383 384
		}
	return(r);
	}

B
Bodo Möller 已提交
385
/* signature verification */
R
Richard Levitte 已提交
386
static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
U
Ulf Möller 已提交
387
	     unsigned char *to, RSA *rsa, int padding)
388
	{
389
	const RSA_METHOD *meth;
390
	BIGNUM f,ret;
391 392 393 394 395
	int i,num=0,r= -1;
	unsigned char *p;
	unsigned char *buf=NULL;
	BN_CTX *ctx=NULL;

396
	meth = ENGINE_get_RSA(rsa->engine);
397 398
	BN_init(&f);
	BN_init(&ret);
399 400 401 402
	ctx=BN_CTX_new();
	if (ctx == NULL) goto err;

	num=BN_num_bytes(rsa->n);
403
	buf=(unsigned char *)OPENSSL_malloc(num);
404 405 406 407 408 409
	if (buf == NULL)
		{
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
		goto err;
		}

U
Ulf Möller 已提交
410
	/* This check was for equality but PGP does evil things
411 412 413 414 415 416 417
	 * and chops off the top '0' bytes */
	if (flen > num)
		{
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
		goto err;
		}

418
	if (BN_bin2bn(from,flen,&f) == NULL) goto err;
B
Bodo Möller 已提交
419 420 421 422 423 424 425

	if (BN_ucmp(&f, rsa->n) >= 0)
		{
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
		goto err;
		}

426
	/* do the decrypt */
427
	if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
428
		{
429 430 431 432
		BN_MONT_CTX* bn_mont_ctx;
		if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
			goto err;
		if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
433
			{
434 435 436 437 438 439 440
			BN_MONT_CTX_free(bn_mont_ctx);
			goto err;
			}
		if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
			{
			CRYPTO_w_lock(CRYPTO_LOCK_RSA);
			if (rsa->_method_mod_n == NULL)
441
				{
442 443
				rsa->_method_mod_n = bn_mont_ctx;
				bn_mont_ctx = NULL;
444
				}
445
			CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
446
			}
447 448
		if (bn_mont_ctx)
			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 458 459

	switch (padding)
		{
	case RSA_PKCS1_PADDING:
460
		r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
461 462
		break;
	case RSA_NO_PADDING:
463
		r=RSA_padding_check_none(to,num,buf,i,num);
464 465 466 467 468 469 470 471 472 473
		break;
	default:
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
		goto err;
		}
	if (r < 0)
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);

err:
	if (ctx != NULL) BN_CTX_free(ctx);
474 475
	BN_clear_free(&f);
	BN_clear_free(&ret);
476 477 478
	if (buf != NULL)
		{
		memset(buf,0,num);
479
		OPENSSL_free(buf);
480 481 482 483
		}
	return(r);
	}

R
Richard Levitte 已提交
484
static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
485
	{
486
	const RSA_METHOD *meth;
U
Ulf Möller 已提交
487
	BIGNUM r1,m1,vrfy;
488 489 490
	int ret=0;
	BN_CTX *ctx;

491
	meth = ENGINE_get_RSA(rsa->engine);
492
	if ((ctx=BN_CTX_new()) == NULL) goto err;
493 494
	BN_init(&m1);
	BN_init(&r1);
U
Ulf Möller 已提交
495
	BN_init(&vrfy);
496 497 498

	if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
		{
499
		if (rsa->_method_mod_p == NULL)
500
			{
501 502 503 504
			BN_MONT_CTX* bn_mont_ctx;
			if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
				goto err;
			if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx))
505
				{
506 507 508 509 510 511 512
				BN_MONT_CTX_free(bn_mont_ctx);
				goto err;
				}
			if (rsa->_method_mod_p == NULL) /* other thread may have finished first */
				{
				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
				if (rsa->_method_mod_p == NULL)
513
					{
514 515
					rsa->_method_mod_p = bn_mont_ctx;
					bn_mont_ctx = NULL;
516 517
					}
				CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
518 519 520
				}
			if (bn_mont_ctx)
				BN_MONT_CTX_free(bn_mont_ctx);
521
			}
522

523
		if (rsa->_method_mod_q == NULL)
524
			{
525 526 527 528
			BN_MONT_CTX* bn_mont_ctx;
			if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
				goto err;
			if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx))
529
				{
530
				BN_MONT_CTX_free(bn_mont_ctx);
B
Bodo Möller 已提交
531
				goto err;
532 533 534 535 536
				}
			if (rsa->_method_mod_q == NULL) /* other thread may have finished first */
				{
				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
				if (rsa->_method_mod_q == NULL)
537
					{
538 539
					rsa->_method_mod_q = bn_mont_ctx;
					bn_mont_ctx = NULL;
540
					}
541
				CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
542
				}
543 544
			if (bn_mont_ctx)
				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 557 558 559 560
	if (!BN_sub(r0,r0,&m1)) goto err;
	/* This will help stop the size of r0 increasing, which does
	 * affect the multiply if it optimised for a power of 2 size */
	if (r0->neg)
		if (!BN_add(r0,r0,rsa->p)) goto err;
561

562 563
	if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
	if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
564 565 566 567 568 569 570 571 572
	/* If p < q it is occasionally possible for the correction of
         * adding 'p' if r0 is negative above to leave the result still
	 * negative. This can break the private key operations: the following
	 * second correction should *always* correct this rare occurrence.
	 * This will *never* happen with OpenSSL generated keys because
         * they ensure p > q [steve]
         */
	if (r0->neg)
		if (!BN_add(r0,r0,rsa->p)) goto err;
573 574
	if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
	if (!BN_add(r0,&r1,&m1)) goto err;
575

U
Ulf Möller 已提交
576 577
	if (rsa->e && rsa->n)
		{
U
Ulf Möller 已提交
578
		if (!meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err;
579 580 581 582 583 584 585 586 587 588 589 590
		/* If 'I' was greater than (or equal to) rsa->n, the operation
		 * will be equivalent to using 'I mod n'. However, the result of
		 * the verify will *always* be less than 'n' so we don't check
		 * for absolute equality, just congruency. */
		if (!BN_sub(&vrfy, &vrfy, I)) goto err;
		if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err;
		if (vrfy.neg)
			if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err;
		if (!BN_is_zero(&vrfy))
			/* 'I' and 'vrfy' aren't congruent mod n. Don't leak
			 * miscalculated CRT output, just do a raw (slower)
			 * mod_exp and return that instead. */
U
Ulf Möller 已提交
591
			if (!meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,NULL)) goto err;
U
Ulf Möller 已提交
592
		}
593 594
	ret=1;
err:
595 596
	BN_clear_free(&m1);
	BN_clear_free(&r1);
U
Ulf Möller 已提交
597
	BN_clear_free(&vrfy);
B
Ben Laurie 已提交
598
	BN_CTX_free(ctx);
599 600 601
	return(ret);
	}

U
Ulf Möller 已提交
602
static int RSA_eay_init(RSA *rsa)
603 604 605 606 607
	{
	rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
	return(1);
	}

U
Ulf Möller 已提交
608
static int RSA_eay_finish(RSA *rsa)
609
	{
610 611 612 613 614 615
	if (rsa->_method_mod_n != NULL)
		BN_MONT_CTX_free(rsa->_method_mod_n);
	if (rsa->_method_mod_p != NULL)
		BN_MONT_CTX_free(rsa->_method_mod_p);
	if (rsa->_method_mod_q != NULL)
		BN_MONT_CTX_free(rsa->_method_mod_q);
616 617 618
	return(1);
	}

619
#endif