os/ossrv/ssl/libcrypto/src/crypto/rsa/rsa_eay.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/rsa/rsa_eay.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/* ====================================================================
sl@0
    59
 * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
sl@0
    60
 *
sl@0
    61
 * Redistribution and use in source and binary forms, with or without
sl@0
    62
 * modification, are permitted provided that the following conditions
sl@0
    63
 * are met:
sl@0
    64
 *
sl@0
    65
 * 1. Redistributions of source code must retain the above copyright
sl@0
    66
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    67
 *
sl@0
    68
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    69
 *    notice, this list of conditions and the following disclaimer in
sl@0
    70
 *    the documentation and/or other materials provided with the
sl@0
    71
 *    distribution.
sl@0
    72
 *
sl@0
    73
 * 3. All advertising materials mentioning features or use of this
sl@0
    74
 *    software must display the following acknowledgment:
sl@0
    75
 *    "This product includes software developed by the OpenSSL Project
sl@0
    76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    77
 *
sl@0
    78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    79
 *    endorse or promote products derived from this software without
sl@0
    80
 *    prior written permission. For written permission, please contact
sl@0
    81
 *    openssl-core@openssl.org.
sl@0
    82
 *
sl@0
    83
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    84
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    85
 *    permission of the OpenSSL Project.
sl@0
    86
 *
sl@0
    87
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    88
 *    acknowledgment:
sl@0
    89
 *    "This product includes software developed by the OpenSSL Project
sl@0
    90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    91
 *
sl@0
    92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
   101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
   102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
   103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
   104
 * ====================================================================
sl@0
   105
 *
sl@0
   106
 * This product includes cryptographic software written by Eric Young
sl@0
   107
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
   108
 * Hudson (tjh@cryptsoft.com).
sl@0
   109
 *
sl@0
   110
 */
sl@0
   111
 /*
sl@0
   112
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
   113
 */
sl@0
   114
sl@0
   115
sl@0
   116
#include <stdio.h>
sl@0
   117
#include "cryptlib.h"
sl@0
   118
#include <openssl/bn.h>
sl@0
   119
#include <openssl/rsa.h>
sl@0
   120
#include <openssl/rand.h>
sl@0
   121
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
   122
#include "libcrypto_wsd_macros.h"
sl@0
   123
#include "libcrypto_wsd.h"
sl@0
   124
#endif
sl@0
   125
sl@0
   126
#ifndef RSA_NULL
sl@0
   127
sl@0
   128
static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
sl@0
   129
		unsigned char *to, RSA *rsa,int padding);
sl@0
   130
static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
sl@0
   131
		unsigned char *to, RSA *rsa,int padding);
sl@0
   132
static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
sl@0
   133
		unsigned char *to, RSA *rsa,int padding);
sl@0
   134
static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
sl@0
   135
		unsigned char *to, RSA *rsa,int padding);
sl@0
   136
static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
sl@0
   137
static int RSA_eay_init(RSA *rsa);
sl@0
   138
static int RSA_eay_finish(RSA *rsa);
sl@0
   139
sl@0
   140
#ifndef EMULATOR
sl@0
   141
static RSA_METHOD rsa_pkcs1_eay_meth={
sl@0
   142
	"Eric Young's PKCS#1 RSA",
sl@0
   143
	RSA_eay_public_encrypt,
sl@0
   144
	RSA_eay_public_decrypt, /* signature verification */
sl@0
   145
	RSA_eay_private_encrypt, /* signing */
sl@0
   146
	RSA_eay_private_decrypt,
sl@0
   147
	RSA_eay_mod_exp,
sl@0
   148
	BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
sl@0
   149
	RSA_eay_init,
sl@0
   150
	RSA_eay_finish,
sl@0
   151
	0, /* flags */
sl@0
   152
	NULL,
sl@0
   153
	0, /* rsa_sign */
sl@0
   154
	0, /* rsa_verify */
sl@0
   155
	NULL /* rsa_keygen */
sl@0
   156
	};
sl@0
   157
#else
sl@0
   158
GET_STATIC_VAR_FROM_TLS(rsa_pkcs1_eay_meth,rsa_eay,RSA_METHOD )
sl@0
   159
#define rsa_pkcs1_eay_meth (*GET_WSD_VAR_NAME(rsa_pkcs1_eay_meth,rsa_eay,s)())
sl@0
   160
const RSA_METHOD temp_s_rsa_pkcs1_eay_meth={
sl@0
   161
	"Eric Young's PKCS#1 RSA",
sl@0
   162
	RSA_eay_public_encrypt,
sl@0
   163
	RSA_eay_public_decrypt, /* signature verification */
sl@0
   164
	RSA_eay_private_encrypt, /* signing */
sl@0
   165
	RSA_eay_private_decrypt,
sl@0
   166
	RSA_eay_mod_exp,
sl@0
   167
	BN_mod_exp_mont, /* XXX probably we should not use Montgomery if  e == 3 */
sl@0
   168
	RSA_eay_init,
sl@0
   169
	RSA_eay_finish,
sl@0
   170
	0, /* flags */
sl@0
   171
	NULL,
sl@0
   172
	0, /* rsa_sign */
sl@0
   173
	0, /* rsa_verify */
sl@0
   174
	NULL /* rsa_keygen */
sl@0
   175
	};
sl@0
   176
sl@0
   177
#endif	
sl@0
   178
EXPORT_C const RSA_METHOD *RSA_PKCS1_SSLeay(void)
sl@0
   179
	{
sl@0
   180
	return(&rsa_pkcs1_eay_meth);
sl@0
   181
	}
sl@0
   182
sl@0
   183
/* Usage example;
sl@0
   184
 *    MONT_HELPER(rsa, bn_ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
sl@0
   185
 */
sl@0
   186
#define MONT_HELPER(rsa, ctx, m, pre_cond, err_instr) \
sl@0
   187
	if((pre_cond) && ((rsa)->_method_mod_##m == NULL) && \
sl@0
   188
			!BN_MONT_CTX_set_locked(&((rsa)->_method_mod_##m), \
sl@0
   189
				CRYPTO_LOCK_RSA, \
sl@0
   190
				(rsa)->m, (ctx))) \
sl@0
   191
		err_instr
sl@0
   192
sl@0
   193
static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
sl@0
   194
	     unsigned char *to, RSA *rsa, int padding)
sl@0
   195
	{
sl@0
   196
	BIGNUM *f,*ret;
sl@0
   197
	int i,j,k,num=0,r= -1;
sl@0
   198
	unsigned char *buf=NULL;
sl@0
   199
	BN_CTX *ctx=NULL;
sl@0
   200
	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
sl@0
   201
		{
sl@0
   202
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
sl@0
   203
		return -1;
sl@0
   204
		}
sl@0
   205
sl@0
   206
	if (BN_ucmp(rsa->n, rsa->e) <= 0)
sl@0
   207
		{
sl@0
   208
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
sl@0
   209
		return -1;
sl@0
   210
		}
sl@0
   211
sl@0
   212
	/* for large moduli, enforce exponent limit */
sl@0
   213
	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
sl@0
   214
		{
sl@0
   215
		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
sl@0
   216
			{
sl@0
   217
			RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
sl@0
   218
			return -1;
sl@0
   219
			}
sl@0
   220
		}
sl@0
   221
	
sl@0
   222
sl@0
   223
	if ((ctx=BN_CTX_new()) == NULL) goto err;
sl@0
   224
	BN_CTX_start(ctx);
sl@0
   225
	f = BN_CTX_get(ctx);
sl@0
   226
	ret = BN_CTX_get(ctx);
sl@0
   227
	num=BN_num_bytes(rsa->n);
sl@0
   228
	buf = OPENSSL_malloc(num);
sl@0
   229
	if (!f || !ret || !buf)
sl@0
   230
		{
sl@0
   231
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
sl@0
   232
		goto err;
sl@0
   233
		}
sl@0
   234
sl@0
   235
	switch (padding)
sl@0
   236
		{
sl@0
   237
	case RSA_PKCS1_PADDING:
sl@0
   238
		i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
sl@0
   239
		break;
sl@0
   240
#ifndef OPENSSL_NO_SHA
sl@0
   241
	case RSA_PKCS1_OAEP_PADDING:
sl@0
   242
	        i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
sl@0
   243
		break;
sl@0
   244
#endif
sl@0
   245
	case RSA_SSLV23_PADDING:
sl@0
   246
		i=RSA_padding_add_SSLv23(buf,num,from,flen);
sl@0
   247
		break;
sl@0
   248
	case RSA_NO_PADDING:
sl@0
   249
		i=RSA_padding_add_none(buf,num,from,flen);
sl@0
   250
		break;
sl@0
   251
	default:
sl@0
   252
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
sl@0
   253
		goto err;
sl@0
   254
		}
sl@0
   255
	if (i <= 0) goto err;
sl@0
   256
sl@0
   257
	if (BN_bin2bn(buf,num,f) == NULL) goto err;
sl@0
   258
	
sl@0
   259
	if (BN_ucmp(f, rsa->n) >= 0)
sl@0
   260
		{	
sl@0
   261
		/* usually the padding functions would catch this */
sl@0
   262
		RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
sl@0
   263
		goto err;
sl@0
   264
		}
sl@0
   265
sl@0
   266
	MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
sl@0
   267
sl@0
   268
	if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
sl@0
   269
		rsa->_method_mod_n)) goto err;
sl@0
   270
sl@0
   271
	/* put in leading 0 bytes if the number is less than the
sl@0
   272
	 * length of the modulus */
sl@0
   273
	j=BN_num_bytes(ret);
sl@0
   274
	i=BN_bn2bin(ret,&(to[num-j]));
sl@0
   275
	for (k=0; k<(num-i); k++)
sl@0
   276
		to[k]=0;
sl@0
   277
sl@0
   278
	r=num;
sl@0
   279
err:
sl@0
   280
	if (ctx != NULL)
sl@0
   281
		{
sl@0
   282
		BN_CTX_end(ctx);
sl@0
   283
		BN_CTX_free(ctx);
sl@0
   284
		}
sl@0
   285
	if (buf != NULL) 
sl@0
   286
		{
sl@0
   287
		OPENSSL_cleanse(buf,num);
sl@0
   288
		OPENSSL_free(buf);
sl@0
   289
		}
sl@0
   290
	return(r);
sl@0
   291
	}
sl@0
   292
sl@0
   293
static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
sl@0
   294
{
sl@0
   295
	BN_BLINDING *ret;
sl@0
   296
	int got_write_lock = 0;
sl@0
   297
sl@0
   298
	CRYPTO_r_lock(CRYPTO_LOCK_RSA);
sl@0
   299
sl@0
   300
	if (rsa->blinding == NULL)
sl@0
   301
		{
sl@0
   302
		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
sl@0
   303
		CRYPTO_w_lock(CRYPTO_LOCK_RSA);
sl@0
   304
		got_write_lock = 1;
sl@0
   305
sl@0
   306
		if (rsa->blinding == NULL)
sl@0
   307
			rsa->blinding = RSA_setup_blinding(rsa, ctx);
sl@0
   308
		}
sl@0
   309
sl@0
   310
	ret = rsa->blinding;
sl@0
   311
	if (ret == NULL)
sl@0
   312
		goto err;
sl@0
   313
sl@0
   314
	if (BN_BLINDING_get_thread_id(ret) == CRYPTO_thread_id())
sl@0
   315
		{
sl@0
   316
		/* rsa->blinding is ours! */
sl@0
   317
sl@0
   318
		*local = 1;
sl@0
   319
		}
sl@0
   320
	else
sl@0
   321
		{
sl@0
   322
		/* resort to rsa->mt_blinding instead */
sl@0
   323
sl@0
   324
		*local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert()
sl@0
   325
		             * that the BN_BLINDING is shared, meaning that accesses
sl@0
   326
		             * require locks, and that the blinding factor must be
sl@0
   327
		             * stored outside the BN_BLINDING
sl@0
   328
		             */
sl@0
   329
sl@0
   330
		if (rsa->mt_blinding == NULL)
sl@0
   331
			{
sl@0
   332
			if (!got_write_lock)
sl@0
   333
				{
sl@0
   334
				CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
sl@0
   335
				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
sl@0
   336
				got_write_lock = 1;
sl@0
   337
				}
sl@0
   338
			
sl@0
   339
			if (rsa->mt_blinding == NULL)
sl@0
   340
				rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
sl@0
   341
			}
sl@0
   342
		ret = rsa->mt_blinding;
sl@0
   343
		}
sl@0
   344
sl@0
   345
 err:
sl@0
   346
	if (got_write_lock)
sl@0
   347
		CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
sl@0
   348
	else
sl@0
   349
		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
sl@0
   350
	return ret;
sl@0
   351
}
sl@0
   352
sl@0
   353
sl@0
   354
static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
sl@0
   355
	BIGNUM *r, BN_CTX *ctx)
sl@0
   356
{
sl@0
   357
	if (local)
sl@0
   358
		return BN_BLINDING_convert_ex(f, NULL, b, ctx);
sl@0
   359
	else
sl@0
   360
		{
sl@0
   361
		int ret;
sl@0
   362
		CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING);
sl@0
   363
		ret = BN_BLINDING_convert_ex(f, r, b, ctx);
sl@0
   364
		CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING);
sl@0
   365
		return ret;
sl@0
   366
		}
sl@0
   367
}
sl@0
   368
sl@0
   369
static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
sl@0
   370
	BIGNUM *r, BN_CTX *ctx)
sl@0
   371
{
sl@0
   372
	if (local)
sl@0
   373
		return BN_BLINDING_invert_ex(f, NULL, b, ctx);
sl@0
   374
	else
sl@0
   375
		{
sl@0
   376
		int ret;
sl@0
   377
		CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
sl@0
   378
		ret = BN_BLINDING_invert_ex(f, r, b, ctx);
sl@0
   379
		CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
sl@0
   380
		return ret;
sl@0
   381
		}
sl@0
   382
}
sl@0
   383
sl@0
   384
/* signing */
sl@0
   385
static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
sl@0
   386
	     unsigned char *to, RSA *rsa, int padding)
sl@0
   387
	{
sl@0
   388
	BIGNUM *f, *ret, *br, *res;
sl@0
   389
	int i,j,k,num=0,r= -1;
sl@0
   390
	unsigned char *buf=NULL;
sl@0
   391
	BN_CTX *ctx=NULL;
sl@0
   392
	int local_blinding = 0;
sl@0
   393
	BN_BLINDING *blinding = NULL;
sl@0
   394
sl@0
   395
	if ((ctx=BN_CTX_new()) == NULL) goto err;
sl@0
   396
	BN_CTX_start(ctx);
sl@0
   397
	f   = BN_CTX_get(ctx);
sl@0
   398
	br  = BN_CTX_get(ctx);
sl@0
   399
	ret = BN_CTX_get(ctx);
sl@0
   400
	num = BN_num_bytes(rsa->n);
sl@0
   401
	buf = OPENSSL_malloc(num);
sl@0
   402
	if(!f || !ret || !buf)
sl@0
   403
		{
sl@0
   404
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
sl@0
   405
		goto err;
sl@0
   406
		}
sl@0
   407
sl@0
   408
	switch (padding)
sl@0
   409
		{
sl@0
   410
	case RSA_PKCS1_PADDING:
sl@0
   411
		i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
sl@0
   412
		break;
sl@0
   413
	case RSA_X931_PADDING:
sl@0
   414
		i=RSA_padding_add_X931(buf,num,from,flen);
sl@0
   415
		break;
sl@0
   416
	case RSA_NO_PADDING:
sl@0
   417
		i=RSA_padding_add_none(buf,num,from,flen);
sl@0
   418
		break;
sl@0
   419
	case RSA_SSLV23_PADDING:
sl@0
   420
	default:
sl@0
   421
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
sl@0
   422
		goto err;
sl@0
   423
		}
sl@0
   424
	if (i <= 0) goto err;
sl@0
   425
sl@0
   426
	if (BN_bin2bn(buf,num,f) == NULL) goto err;
sl@0
   427
	
sl@0
   428
	if (BN_ucmp(f, rsa->n) >= 0)
sl@0
   429
		{	
sl@0
   430
		/* usually the padding functions would catch this */
sl@0
   431
		RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
sl@0
   432
		goto err;
sl@0
   433
		}
sl@0
   434
sl@0
   435
	if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
sl@0
   436
		{
sl@0
   437
				blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
sl@0
   438
sl@0
   439
		if (blinding == NULL)
sl@0
   440
			{
sl@0
   441
			RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
sl@0
   442
			goto err;
sl@0
   443
			}
sl@0
   444
		}
sl@0
   445
	
sl@0
   446
	if (blinding != NULL)
sl@0
   447
		if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
sl@0
   448
			goto err;
sl@0
   449
sl@0
   450
	if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
sl@0
   451
		((rsa->p != NULL) &&
sl@0
   452
		(rsa->q != NULL) &&
sl@0
   453
		(rsa->dmp1 != NULL) &&
sl@0
   454
		(rsa->dmq1 != NULL) &&
sl@0
   455
		(rsa->iqmp != NULL)) )
sl@0
   456
		{ 
sl@0
   457
		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
sl@0
   458
		}
sl@0
   459
	else
sl@0
   460
		{
sl@0
   461
		BIGNUM local_d;
sl@0
   462
		BIGNUM *d = NULL;
sl@0
   463
		
sl@0
   464
		if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   465
			{
sl@0
   466
			BN_init(&local_d);
sl@0
   467
			d = &local_d;
sl@0
   468
			BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
sl@0
   469
			}
sl@0
   470
		else
sl@0
   471
			d = rsa->d;
sl@0
   472
sl@0
   473
		MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
sl@0
   474
sl@0
   475
		if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
sl@0
   476
				rsa->_method_mod_n)) goto err;
sl@0
   477
		}
sl@0
   478
sl@0
   479
	if (blinding)
sl@0
   480
		if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
sl@0
   481
			goto err;
sl@0
   482
sl@0
   483
	if (padding == RSA_X931_PADDING)
sl@0
   484
		{
sl@0
   485
		BN_sub(f, rsa->n, ret);
sl@0
   486
		if (BN_cmp(ret, f))
sl@0
   487
			res = f;
sl@0
   488
		else
sl@0
   489
			res = ret;
sl@0
   490
		}
sl@0
   491
	else
sl@0
   492
		res = ret;
sl@0
   493
sl@0
   494
	/* put in leading 0 bytes if the number is less than the
sl@0
   495
	 * length of the modulus */
sl@0
   496
	j=BN_num_bytes(res);
sl@0
   497
	i=BN_bn2bin(res,&(to[num-j]));
sl@0
   498
	for (k=0; k<(num-i); k++)
sl@0
   499
		to[k]=0;
sl@0
   500
sl@0
   501
	r=num;
sl@0
   502
err:
sl@0
   503
	if (ctx != NULL)
sl@0
   504
		{
sl@0
   505
		BN_CTX_end(ctx);
sl@0
   506
		BN_CTX_free(ctx);
sl@0
   507
		}
sl@0
   508
	if (buf != NULL)
sl@0
   509
		{
sl@0
   510
		OPENSSL_cleanse(buf,num);
sl@0
   511
		OPENSSL_free(buf);
sl@0
   512
		}
sl@0
   513
	return(r);
sl@0
   514
	}
sl@0
   515
sl@0
   516
static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
sl@0
   517
	     unsigned char *to, RSA *rsa, int padding)
sl@0
   518
	{
sl@0
   519
	BIGNUM *f, *ret, *br;
sl@0
   520
	int j,num=0,r= -1;
sl@0
   521
	unsigned char *p;
sl@0
   522
	unsigned char *buf=NULL;
sl@0
   523
	BN_CTX *ctx=NULL;
sl@0
   524
	int local_blinding = 0;
sl@0
   525
	BN_BLINDING *blinding = NULL;
sl@0
   526
sl@0
   527
	if((ctx = BN_CTX_new()) == NULL) goto err;
sl@0
   528
	BN_CTX_start(ctx);
sl@0
   529
	f   = BN_CTX_get(ctx);
sl@0
   530
	br  = BN_CTX_get(ctx);
sl@0
   531
	ret = BN_CTX_get(ctx);
sl@0
   532
	num = BN_num_bytes(rsa->n);
sl@0
   533
	buf = OPENSSL_malloc(num);
sl@0
   534
	if(!f || !ret || !buf)
sl@0
   535
		{
sl@0
   536
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
sl@0
   537
		goto err;
sl@0
   538
		}
sl@0
   539
sl@0
   540
	/* This check was for equality but PGP does evil things
sl@0
   541
	 * and chops off the top '0' bytes */
sl@0
   542
	if (flen > num)
sl@0
   543
		{
sl@0
   544
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
sl@0
   545
		goto err;
sl@0
   546
		}
sl@0
   547
sl@0
   548
	/* make data into a big number */
sl@0
   549
	if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
sl@0
   550
sl@0
   551
	if (BN_ucmp(f, rsa->n) >= 0)
sl@0
   552
		{
sl@0
   553
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
sl@0
   554
		goto err;
sl@0
   555
		}
sl@0
   556
sl@0
   557
	if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
sl@0
   558
		{
sl@0
   559
			blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
sl@0
   560
		if (blinding == NULL)
sl@0
   561
			{
sl@0
   562
			RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
sl@0
   563
			goto err;
sl@0
   564
			}
sl@0
   565
		}
sl@0
   566
	
sl@0
   567
	if (blinding != NULL)
sl@0
   568
		if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
sl@0
   569
			goto err;
sl@0
   570
sl@0
   571
	/* do the decrypt */
sl@0
   572
	if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
sl@0
   573
		((rsa->p != NULL) &&
sl@0
   574
		(rsa->q != NULL) &&
sl@0
   575
		(rsa->dmp1 != NULL) &&
sl@0
   576
		(rsa->dmq1 != NULL) &&
sl@0
   577
		(rsa->iqmp != NULL)) )
sl@0
   578
		{
sl@0
   579
		if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
sl@0
   580
		}
sl@0
   581
	else
sl@0
   582
		{
sl@0
   583
		BIGNUM local_d;
sl@0
   584
		BIGNUM *d = NULL;
sl@0
   585
		
sl@0
   586
		if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   587
			{
sl@0
   588
			d = &local_d;
sl@0
   589
			BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
sl@0
   590
			}
sl@0
   591
		else
sl@0
   592
			d = rsa->d;
sl@0
   593
sl@0
   594
		MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
sl@0
   595
		if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
sl@0
   596
				rsa->_method_mod_n))
sl@0
   597
		  goto err;
sl@0
   598
		}
sl@0
   599
sl@0
   600
	if (blinding)
sl@0
   601
		if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
sl@0
   602
			goto err;
sl@0
   603
sl@0
   604
	p=buf;
sl@0
   605
	j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
sl@0
   606
sl@0
   607
	switch (padding)
sl@0
   608
		{
sl@0
   609
	case RSA_PKCS1_PADDING:
sl@0
   610
		r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
sl@0
   611
		break;
sl@0
   612
#ifndef OPENSSL_NO_SHA
sl@0
   613
        case RSA_PKCS1_OAEP_PADDING:
sl@0
   614
	        r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
sl@0
   615
                break;
sl@0
   616
#endif
sl@0
   617
 	case RSA_SSLV23_PADDING:
sl@0
   618
		r=RSA_padding_check_SSLv23(to,num,buf,j,num);
sl@0
   619
		break;
sl@0
   620
	case RSA_NO_PADDING:
sl@0
   621
		r=RSA_padding_check_none(to,num,buf,j,num);
sl@0
   622
		break;
sl@0
   623
	default:
sl@0
   624
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
sl@0
   625
		goto err;
sl@0
   626
		}
sl@0
   627
	if (r < 0)
sl@0
   628
		RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
sl@0
   629
sl@0
   630
err:
sl@0
   631
	if (ctx != NULL)
sl@0
   632
		{
sl@0
   633
		BN_CTX_end(ctx);
sl@0
   634
		BN_CTX_free(ctx);
sl@0
   635
		}
sl@0
   636
	if (buf != NULL)
sl@0
   637
		{
sl@0
   638
		OPENSSL_cleanse(buf,num);
sl@0
   639
		OPENSSL_free(buf);
sl@0
   640
		}
sl@0
   641
	return(r);
sl@0
   642
	}
sl@0
   643
sl@0
   644
/* signature verification */
sl@0
   645
static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
sl@0
   646
	     unsigned char *to, RSA *rsa, int padding)
sl@0
   647
	{
sl@0
   648
	BIGNUM *f,*ret;
sl@0
   649
	int i,num=0,r= -1;
sl@0
   650
	unsigned char *p;
sl@0
   651
	unsigned char *buf=NULL;
sl@0
   652
	BN_CTX *ctx=NULL;
sl@0
   653
	if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
sl@0
   654
		{
sl@0
   655
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
sl@0
   656
		return -1;
sl@0
   657
		}
sl@0
   658
sl@0
   659
	if (BN_ucmp(rsa->n, rsa->e) <= 0)
sl@0
   660
		{
sl@0
   661
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
sl@0
   662
		return -1;
sl@0
   663
		}
sl@0
   664
sl@0
   665
	/* for large moduli, enforce exponent limit */
sl@0
   666
	if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
sl@0
   667
		{
sl@0
   668
		if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
sl@0
   669
			{
sl@0
   670
			RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
sl@0
   671
			return -1;
sl@0
   672
			}
sl@0
   673
		}
sl@0
   674
	if((ctx = BN_CTX_new()) == NULL) goto err;
sl@0
   675
	BN_CTX_start(ctx);
sl@0
   676
	f = BN_CTX_get(ctx);
sl@0
   677
	ret = BN_CTX_get(ctx);
sl@0
   678
	num=BN_num_bytes(rsa->n);
sl@0
   679
	buf = OPENSSL_malloc(num);
sl@0
   680
	if(!f || !ret || !buf)
sl@0
   681
		{
sl@0
   682
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
sl@0
   683
		goto err;
sl@0
   684
		}
sl@0
   685
sl@0
   686
	/* This check was for equality but PGP does evil things
sl@0
   687
	 * and chops off the top '0' bytes */
sl@0
   688
	if (flen > num)
sl@0
   689
		{
sl@0
   690
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
sl@0
   691
		goto err;
sl@0
   692
		}
sl@0
   693
sl@0
   694
	if (BN_bin2bn(from,flen,f) == NULL) goto err;
sl@0
   695
sl@0
   696
	if (BN_ucmp(f, rsa->n) >= 0)
sl@0
   697
		{
sl@0
   698
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
sl@0
   699
		goto err;
sl@0
   700
		}
sl@0
   701
sl@0
   702
	MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
sl@0
   703
sl@0
   704
	if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
sl@0
   705
		rsa->_method_mod_n)) goto err;
sl@0
   706
sl@0
   707
	if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
sl@0
   708
		BN_sub(ret, rsa->n, ret);
sl@0
   709
sl@0
   710
	p=buf;
sl@0
   711
	i=BN_bn2bin(ret,p);
sl@0
   712
sl@0
   713
	switch (padding)
sl@0
   714
		{
sl@0
   715
	case RSA_PKCS1_PADDING:
sl@0
   716
		r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
sl@0
   717
		break;
sl@0
   718
	case RSA_X931_PADDING:
sl@0
   719
		r=RSA_padding_check_X931(to,num,buf,i,num);
sl@0
   720
		break;
sl@0
   721
	case RSA_NO_PADDING:
sl@0
   722
		r=RSA_padding_check_none(to,num,buf,i,num);
sl@0
   723
		break;
sl@0
   724
	default:
sl@0
   725
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
sl@0
   726
		goto err;
sl@0
   727
		}
sl@0
   728
	if (r < 0)
sl@0
   729
		RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
sl@0
   730
sl@0
   731
err:
sl@0
   732
	if (ctx != NULL)
sl@0
   733
		{
sl@0
   734
		BN_CTX_end(ctx);
sl@0
   735
		BN_CTX_free(ctx);
sl@0
   736
		}
sl@0
   737
	if (buf != NULL)
sl@0
   738
		{
sl@0
   739
		OPENSSL_cleanse(buf,num);
sl@0
   740
		OPENSSL_free(buf);
sl@0
   741
		}
sl@0
   742
	return(r);
sl@0
   743
	}
sl@0
   744
sl@0
   745
static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
sl@0
   746
	{
sl@0
   747
	BIGNUM *r1,*m1,*vrfy;
sl@0
   748
	BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
sl@0
   749
	BIGNUM *dmp1,*dmq1,*c,*pr1;
sl@0
   750
	int bn_flags;
sl@0
   751
	int ret=0;
sl@0
   752
sl@0
   753
	BN_CTX_start(ctx);
sl@0
   754
	r1 = BN_CTX_get(ctx);
sl@0
   755
	m1 = BN_CTX_get(ctx);
sl@0
   756
	vrfy = BN_CTX_get(ctx);
sl@0
   757
sl@0
   758
	/* Make sure mod_inverse in montgomerey intialization use correct 
sl@0
   759
	 * BN_FLG_CONSTTIME flag.
sl@0
   760
	 */
sl@0
   761
	bn_flags = rsa->p->flags;
sl@0
   762
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   763
		{
sl@0
   764
		rsa->p->flags |= BN_FLG_CONSTTIME;
sl@0
   765
		}
sl@0
   766
	MONT_HELPER(rsa, ctx, p, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
sl@0
   767
	/* We restore bn_flags back */
sl@0
   768
	rsa->p->flags = bn_flags;
sl@0
   769
sl@0
   770
        /* Make sure mod_inverse in montgomerey intialization use correct
sl@0
   771
         * BN_FLG_CONSTTIME flag.
sl@0
   772
         */
sl@0
   773
	bn_flags = rsa->q->flags;
sl@0
   774
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   775
		{
sl@0
   776
		rsa->q->flags |= BN_FLG_CONSTTIME;
sl@0
   777
		}
sl@0
   778
	MONT_HELPER(rsa, ctx, q, rsa->flags & RSA_FLAG_CACHE_PRIVATE, goto err);
sl@0
   779
	/* We restore bn_flags back */
sl@0
   780
	rsa->q->flags = bn_flags;	
sl@0
   781
	MONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);
sl@0
   782
sl@0
   783
	/* compute I mod q */
sl@0
   784
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   785
		{
sl@0
   786
		c = &local_c;
sl@0
   787
		BN_with_flags(c, I, BN_FLG_CONSTTIME);
sl@0
   788
		if (!BN_mod(r1,c,rsa->q,ctx)) goto err;
sl@0
   789
		}
sl@0
   790
	else
sl@0
   791
		{
sl@0
   792
		if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
sl@0
   793
		}
sl@0
   794
	/* compute r1^dmq1 mod q */
sl@0
   795
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   796
		{
sl@0
   797
		dmq1 = &local_dmq1;
sl@0
   798
		BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
sl@0
   799
		}
sl@0
   800
	else
sl@0
   801
		dmq1 = rsa->dmq1;
sl@0
   802
	if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
sl@0
   803
		rsa->_method_mod_q)) goto err;
sl@0
   804
sl@0
   805
	/* compute I mod p */
sl@0
   806
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   807
		{
sl@0
   808
		c = &local_c;
sl@0
   809
		BN_with_flags(c, I, BN_FLG_CONSTTIME);
sl@0
   810
		if (!BN_mod(r1,c,rsa->p,ctx)) goto err;
sl@0
   811
		}
sl@0
   812
	else
sl@0
   813
		{
sl@0
   814
		if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
sl@0
   815
		}
sl@0
   816
	/* compute r1^dmp1 mod p */
sl@0
   817
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   818
		{
sl@0
   819
		dmp1 = &local_dmp1;
sl@0
   820
		BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
sl@0
   821
		}
sl@0
   822
	else
sl@0
   823
		dmp1 = rsa->dmp1;
sl@0
   824
	if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
sl@0
   825
		rsa->_method_mod_p)) goto err;
sl@0
   826
sl@0
   827
	if (!BN_sub(r0,r0,m1)) goto err;
sl@0
   828
	/* This will help stop the size of r0 increasing, which does
sl@0
   829
	 * affect the multiply if it optimised for a power of 2 size */
sl@0
   830
	if (BN_is_negative(r0))
sl@0
   831
		if (!BN_add(r0,r0,rsa->p)) goto err;
sl@0
   832
sl@0
   833
	if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
sl@0
   834
sl@0
   835
	/* Turn BN_FLG_CONSTTIME flag on before division operation */
sl@0
   836
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   837
		{
sl@0
   838
		pr1 = &local_r1;
sl@0
   839
		BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
sl@0
   840
		}
sl@0
   841
	else
sl@0
   842
		pr1 = r1;
sl@0
   843
	if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;
sl@0
   844
sl@0
   845
	/* If p < q it is occasionally possible for the correction of
sl@0
   846
         * adding 'p' if r0 is negative above to leave the result still
sl@0
   847
	 * negative. This can break the private key operations: the following
sl@0
   848
	 * second correction should *always* correct this rare occurrence.
sl@0
   849
	 * This will *never* happen with OpenSSL generated keys because
sl@0
   850
         * they ensure p > q [steve]
sl@0
   851
         */
sl@0
   852
	if (BN_is_negative(r0))
sl@0
   853
		if (!BN_add(r0,r0,rsa->p)) goto err;
sl@0
   854
	if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
sl@0
   855
	if (!BN_add(r0,r1,m1)) goto err;
sl@0
   856
sl@0
   857
	if (rsa->e && rsa->n)
sl@0
   858
		{
sl@0
   859
		if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
sl@0
   860
		/* If 'I' was greater than (or equal to) rsa->n, the operation
sl@0
   861
		 * will be equivalent to using 'I mod n'. However, the result of
sl@0
   862
		 * the verify will *always* be less than 'n' so we don't check
sl@0
   863
		 * for absolute equality, just congruency. */
sl@0
   864
		if (!BN_sub(vrfy, vrfy, I)) goto err;
sl@0
   865
		if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
sl@0
   866
		if (BN_is_negative(vrfy))
sl@0
   867
			if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
sl@0
   868
		if (!BN_is_zero(vrfy))
sl@0
   869
			{
sl@0
   870
			/* 'I' and 'vrfy' aren't congruent mod n. Don't leak
sl@0
   871
			 * miscalculated CRT output, just do a raw (slower)
sl@0
   872
			 * mod_exp and return that instead. */
sl@0
   873
sl@0
   874
			BIGNUM local_d;
sl@0
   875
			BIGNUM *d = NULL;
sl@0
   876
		
sl@0
   877
			if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   878
				{
sl@0
   879
				d = &local_d;
sl@0
   880
				BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
sl@0
   881
				}
sl@0
   882
			else
sl@0
   883
				d = rsa->d;
sl@0
   884
			if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
sl@0
   885
						   rsa->_method_mod_n)) goto err;
sl@0
   886
			}
sl@0
   887
		}
sl@0
   888
	ret=1;
sl@0
   889
err:
sl@0
   890
	BN_CTX_end(ctx);
sl@0
   891
	return(ret);
sl@0
   892
	}
sl@0
   893
sl@0
   894
static int RSA_eay_init(RSA *rsa)
sl@0
   895
	{
sl@0
   896
	rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
sl@0
   897
	return(1);
sl@0
   898
	}
sl@0
   899
sl@0
   900
static int RSA_eay_finish(RSA *rsa)
sl@0
   901
	{
sl@0
   902
	if (rsa->_method_mod_n != NULL)
sl@0
   903
		BN_MONT_CTX_free(rsa->_method_mod_n);
sl@0
   904
	if (rsa->_method_mod_p != NULL)
sl@0
   905
		BN_MONT_CTX_free(rsa->_method_mod_p);
sl@0
   906
	if (rsa->_method_mod_q != NULL)
sl@0
   907
		BN_MONT_CTX_free(rsa->_method_mod_q);
sl@0
   908
	return(1);
sl@0
   909
	}
sl@0
   910
sl@0
   911
#endif