os/ossrv/ssl/libcrypto/src/crypto/rsa/rsa_lib.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/rsa/rsa_lib.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
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
sl@0
    62
#include <stdio.h>
sl@0
    63
#include <openssl/crypto.h>
sl@0
    64
#include "cryptlib.h"
sl@0
    65
#include <openssl/lhash.h>
sl@0
    66
#include <openssl/bn.h>
sl@0
    67
#include <openssl/rsa.h>
sl@0
    68
#include <openssl/rand.h>
sl@0
    69
#ifndef OPENSSL_NO_ENGINE
sl@0
    70
#include <openssl/engine.h>
sl@0
    71
#endif
sl@0
    72
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    73
#include "libcrypto_wsd_macros.h"
sl@0
    74
#include "libcrypto_wsd.h"
sl@0
    75
#endif
sl@0
    76
sl@0
    77
sl@0
    78
const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT;
sl@0
    79
sl@0
    80
#ifndef EMULATOR
sl@0
    81
static const RSA_METHOD *default_RSA_meth=NULL;
sl@0
    82
#else
sl@0
    83
GET_STATIC_VAR_FROM_TLS(default_RSA_meth,rsa_lib,const RSA_METHOD *)
sl@0
    84
#define default_RSA_meth (*GET_WSD_VAR_NAME(default_RSA_meth,rsa_lib, s)())
sl@0
    85
#endif
sl@0
    86
sl@0
    87
EXPORT_C RSA *RSA_new(void)
sl@0
    88
	{
sl@0
    89
	RSA *r=RSA_new_method(NULL);
sl@0
    90
sl@0
    91
	return r;
sl@0
    92
	}
sl@0
    93
sl@0
    94
EXPORT_C void RSA_set_default_method(const RSA_METHOD *meth)
sl@0
    95
	{
sl@0
    96
	default_RSA_meth = meth;
sl@0
    97
	}
sl@0
    98
sl@0
    99
EXPORT_C const RSA_METHOD *RSA_get_default_method(void)
sl@0
   100
	{
sl@0
   101
	if (default_RSA_meth == NULL)
sl@0
   102
		{
sl@0
   103
#ifdef RSA_NULL
sl@0
   104
		default_RSA_meth=RSA_null_method();
sl@0
   105
#else
sl@0
   106
#if 0 /* was: #ifdef RSAref */
sl@0
   107
		default_RSA_meth=RSA_PKCS1_RSAref();
sl@0
   108
#else
sl@0
   109
		default_RSA_meth=RSA_PKCS1_SSLeay();
sl@0
   110
#endif
sl@0
   111
#endif
sl@0
   112
		}
sl@0
   113
sl@0
   114
	return default_RSA_meth;
sl@0
   115
	}
sl@0
   116
sl@0
   117
EXPORT_C const RSA_METHOD *RSA_get_method(const RSA *rsa)
sl@0
   118
	{
sl@0
   119
	return rsa->meth;
sl@0
   120
	}
sl@0
   121
sl@0
   122
EXPORT_C int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
sl@0
   123
	{
sl@0
   124
	/* NB: The caller is specifically setting a method, so it's not up to us
sl@0
   125
	 * to deal with which ENGINE it comes from. */
sl@0
   126
	const RSA_METHOD *mtmp;
sl@0
   127
	mtmp = rsa->meth;
sl@0
   128
	if (mtmp->finish) mtmp->finish(rsa);
sl@0
   129
#ifndef OPENSSL_NO_ENGINE
sl@0
   130
	if (rsa->engine)
sl@0
   131
		{
sl@0
   132
		ENGINE_finish(rsa->engine);
sl@0
   133
		rsa->engine = NULL;
sl@0
   134
		}
sl@0
   135
#endif
sl@0
   136
	rsa->meth = meth;
sl@0
   137
	if (meth->init) meth->init(rsa);
sl@0
   138
	return 1;
sl@0
   139
	}
sl@0
   140
sl@0
   141
EXPORT_C RSA *RSA_new_method(ENGINE *engine)
sl@0
   142
	{
sl@0
   143
	RSA *ret;
sl@0
   144
sl@0
   145
	ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
sl@0
   146
	if (ret == NULL)
sl@0
   147
		{
sl@0
   148
		RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
sl@0
   149
		return NULL;
sl@0
   150
		}
sl@0
   151
sl@0
   152
	ret->meth = RSA_get_default_method();
sl@0
   153
#ifndef OPENSSL_NO_ENGINE
sl@0
   154
	if (engine)
sl@0
   155
		{
sl@0
   156
		if (!ENGINE_init(engine))
sl@0
   157
			{
sl@0
   158
			RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
sl@0
   159
			OPENSSL_free(ret);
sl@0
   160
			return NULL;
sl@0
   161
			}
sl@0
   162
		ret->engine = engine;
sl@0
   163
		}
sl@0
   164
	else
sl@0
   165
		ret->engine = ENGINE_get_default_RSA();
sl@0
   166
	if(ret->engine)
sl@0
   167
		{
sl@0
   168
		ret->meth = ENGINE_get_RSA(ret->engine);
sl@0
   169
		if(!ret->meth)
sl@0
   170
			{
sl@0
   171
			RSAerr(RSA_F_RSA_NEW_METHOD,
sl@0
   172
				ERR_R_ENGINE_LIB);
sl@0
   173
			ENGINE_finish(ret->engine);
sl@0
   174
			OPENSSL_free(ret);
sl@0
   175
			return NULL;
sl@0
   176
			}
sl@0
   177
		}
sl@0
   178
#endif
sl@0
   179
sl@0
   180
	ret->pad=0;
sl@0
   181
	ret->version=0;
sl@0
   182
	ret->n=NULL;
sl@0
   183
	ret->e=NULL;
sl@0
   184
	ret->d=NULL;
sl@0
   185
	ret->p=NULL;
sl@0
   186
	ret->q=NULL;
sl@0
   187
	ret->dmp1=NULL;
sl@0
   188
	ret->dmq1=NULL;
sl@0
   189
	ret->iqmp=NULL;
sl@0
   190
	ret->references=1;
sl@0
   191
	ret->_method_mod_n=NULL;
sl@0
   192
	ret->_method_mod_p=NULL;
sl@0
   193
	ret->_method_mod_q=NULL;
sl@0
   194
	ret->blinding=NULL;
sl@0
   195
	ret->mt_blinding=NULL;
sl@0
   196
	ret->bignum_data=NULL;
sl@0
   197
	ret->flags=ret->meth->flags;
sl@0
   198
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
sl@0
   199
	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
sl@0
   200
		{
sl@0
   201
#ifndef OPENSSL_NO_ENGINE
sl@0
   202
		if (ret->engine)
sl@0
   203
			ENGINE_finish(ret->engine);
sl@0
   204
#endif
sl@0
   205
		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
sl@0
   206
		OPENSSL_free(ret);
sl@0
   207
		ret=NULL;
sl@0
   208
		}
sl@0
   209
	return(ret);
sl@0
   210
	}
sl@0
   211
sl@0
   212
EXPORT_C void RSA_free(RSA *r)
sl@0
   213
	{
sl@0
   214
	int i;
sl@0
   215
sl@0
   216
	if (r == NULL) return;
sl@0
   217
sl@0
   218
	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
sl@0
   219
#ifdef REF_PRINT
sl@0
   220
	REF_PRINT("RSA",r);
sl@0
   221
#endif
sl@0
   222
	if (i > 0) return;
sl@0
   223
#ifdef REF_CHECK
sl@0
   224
	if (i < 0)
sl@0
   225
		{
sl@0
   226
		fprintf(stderr,"RSA_free, bad reference count\n");
sl@0
   227
		abort();
sl@0
   228
		}
sl@0
   229
#endif
sl@0
   230
sl@0
   231
	if (r->meth->finish)
sl@0
   232
		r->meth->finish(r);
sl@0
   233
#ifndef OPENSSL_NO_ENGINE
sl@0
   234
	if (r->engine)
sl@0
   235
		ENGINE_finish(r->engine);
sl@0
   236
#endif
sl@0
   237
sl@0
   238
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
sl@0
   239
sl@0
   240
	if (r->n != NULL) BN_clear_free(r->n);
sl@0
   241
	if (r->e != NULL) BN_clear_free(r->e);
sl@0
   242
	if (r->d != NULL) BN_clear_free(r->d);
sl@0
   243
	if (r->p != NULL) BN_clear_free(r->p);
sl@0
   244
	if (r->q != NULL) BN_clear_free(r->q);
sl@0
   245
	if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
sl@0
   246
	if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
sl@0
   247
	if (r->iqmp != NULL) BN_clear_free(r->iqmp);
sl@0
   248
	if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
sl@0
   249
	if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
sl@0
   250
	if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
sl@0
   251
	OPENSSL_free(r);
sl@0
   252
	}
sl@0
   253
sl@0
   254
EXPORT_C int RSA_up_ref(RSA *r)
sl@0
   255
	{
sl@0
   256
	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
sl@0
   257
#ifdef REF_PRINT
sl@0
   258
	REF_PRINT("RSA",r);
sl@0
   259
#endif
sl@0
   260
#ifdef REF_CHECK
sl@0
   261
	if (i < 2)
sl@0
   262
		{
sl@0
   263
		fprintf(stderr, "RSA_up_ref, bad reference count\n");
sl@0
   264
		abort();
sl@0
   265
		}
sl@0
   266
#endif
sl@0
   267
	return ((i > 1) ? 1 : 0);
sl@0
   268
	}
sl@0
   269
sl@0
   270
EXPORT_C int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
sl@0
   271
	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
sl@0
   272
        {
sl@0
   273
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
sl@0
   274
				new_func, dup_func, free_func);
sl@0
   275
        }
sl@0
   276
sl@0
   277
EXPORT_C int RSA_set_ex_data(RSA *r, int idx, void *arg)
sl@0
   278
	{
sl@0
   279
	return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
sl@0
   280
	}
sl@0
   281
sl@0
   282
EXPORT_C void *RSA_get_ex_data(const RSA *r, int idx)
sl@0
   283
	{
sl@0
   284
	return(CRYPTO_get_ex_data(&r->ex_data,idx));
sl@0
   285
	}
sl@0
   286
sl@0
   287
EXPORT_C int RSA_size(const RSA *r)
sl@0
   288
	{
sl@0
   289
	return(BN_num_bytes(r->n));
sl@0
   290
	}
sl@0
   291
sl@0
   292
EXPORT_C int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
sl@0
   293
	     RSA *rsa, int padding)
sl@0
   294
	{
sl@0
   295
	return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
sl@0
   296
	}
sl@0
   297
sl@0
   298
EXPORT_C int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
sl@0
   299
	     RSA *rsa, int padding)
sl@0
   300
	{
sl@0
   301
	return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
sl@0
   302
	}
sl@0
   303
sl@0
   304
EXPORT_C int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
sl@0
   305
	     RSA *rsa, int padding)
sl@0
   306
	{
sl@0
   307
	return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
sl@0
   308
	}
sl@0
   309
sl@0
   310
EXPORT_C int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
sl@0
   311
	     RSA *rsa, int padding)
sl@0
   312
	{
sl@0
   313
	return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
sl@0
   314
	}
sl@0
   315
sl@0
   316
EXPORT_C int RSA_flags(const RSA *r)
sl@0
   317
	{
sl@0
   318
	return((r == NULL)?0:r->meth->flags);
sl@0
   319
	}
sl@0
   320
sl@0
   321
EXPORT_C void RSA_blinding_off(RSA *rsa)
sl@0
   322
	{
sl@0
   323
	if (rsa->blinding != NULL)
sl@0
   324
		{
sl@0
   325
		BN_BLINDING_free(rsa->blinding);
sl@0
   326
		rsa->blinding=NULL;
sl@0
   327
		}
sl@0
   328
	rsa->flags &= ~RSA_FLAG_BLINDING;
sl@0
   329
	rsa->flags |= RSA_FLAG_NO_BLINDING;
sl@0
   330
	}
sl@0
   331
sl@0
   332
EXPORT_C int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
sl@0
   333
	{
sl@0
   334
	int ret=0;
sl@0
   335
sl@0
   336
	if (rsa->blinding != NULL)
sl@0
   337
		RSA_blinding_off(rsa);
sl@0
   338
sl@0
   339
	rsa->blinding = RSA_setup_blinding(rsa, ctx);
sl@0
   340
	if (rsa->blinding == NULL)
sl@0
   341
		goto err;
sl@0
   342
sl@0
   343
	rsa->flags |= RSA_FLAG_BLINDING;
sl@0
   344
	rsa->flags &= ~RSA_FLAG_NO_BLINDING;
sl@0
   345
	ret=1;
sl@0
   346
err:
sl@0
   347
	return(ret);
sl@0
   348
	}
sl@0
   349
sl@0
   350
static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
sl@0
   351
	const BIGNUM *q, BN_CTX *ctx)
sl@0
   352
{
sl@0
   353
	BIGNUM *ret = NULL, *r0, *r1, *r2;
sl@0
   354
sl@0
   355
	if (d == NULL || p == NULL || q == NULL)
sl@0
   356
		return NULL;
sl@0
   357
sl@0
   358
	BN_CTX_start(ctx);
sl@0
   359
	r0 = BN_CTX_get(ctx);
sl@0
   360
	r1 = BN_CTX_get(ctx);
sl@0
   361
	r2 = BN_CTX_get(ctx);
sl@0
   362
	if (r2 == NULL)
sl@0
   363
		goto err;
sl@0
   364
sl@0
   365
	if (!BN_sub(r1, p, BN_value_one())) goto err;
sl@0
   366
	if (!BN_sub(r2, q, BN_value_one())) goto err;
sl@0
   367
	if (!BN_mul(r0, r1, r2, ctx)) goto err;
sl@0
   368
sl@0
   369
	ret = BN_mod_inverse(NULL, d, r0, ctx);
sl@0
   370
err:
sl@0
   371
	BN_CTX_end(ctx);
sl@0
   372
	return ret;
sl@0
   373
}
sl@0
   374
sl@0
   375
EXPORT_C BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
sl@0
   376
{
sl@0
   377
	BIGNUM local_n;
sl@0
   378
	BIGNUM *e,*n;
sl@0
   379
	BN_CTX *ctx;
sl@0
   380
	BN_BLINDING *ret = NULL;
sl@0
   381
sl@0
   382
	if (in_ctx == NULL)
sl@0
   383
		{
sl@0
   384
		if ((ctx = BN_CTX_new()) == NULL) return 0;
sl@0
   385
		}
sl@0
   386
	else
sl@0
   387
		ctx = in_ctx;
sl@0
   388
sl@0
   389
	BN_CTX_start(ctx);
sl@0
   390
	e  = BN_CTX_get(ctx);
sl@0
   391
	if (e == NULL)
sl@0
   392
		{
sl@0
   393
		RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
sl@0
   394
		goto err;
sl@0
   395
		}
sl@0
   396
sl@0
   397
	if (rsa->e == NULL)
sl@0
   398
		{
sl@0
   399
		e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
sl@0
   400
		if (e == NULL)
sl@0
   401
			{
sl@0
   402
			RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT);
sl@0
   403
			goto err;
sl@0
   404
			}
sl@0
   405
		}
sl@0
   406
	else
sl@0
   407
		e = rsa->e;
sl@0
   408
sl@0
   409
	
sl@0
   410
	if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
sl@0
   411
		{
sl@0
   412
		/* if PRNG is not properly seeded, resort to secret
sl@0
   413
		 * exponent as unpredictable seed */
sl@0
   414
		RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);
sl@0
   415
		}
sl@0
   416
sl@0
   417
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   418
		{
sl@0
   419
		/* Set BN_FLG_CONSTTIME flag */
sl@0
   420
		n = &local_n;
sl@0
   421
		BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
sl@0
   422
		}
sl@0
   423
	else
sl@0
   424
		n = rsa->n;
sl@0
   425
sl@0
   426
	ret = BN_BLINDING_create_param(NULL, e, n, ctx,
sl@0
   427
			rsa->meth->bn_mod_exp, rsa->_method_mod_n);
sl@0
   428
	if (ret == NULL)
sl@0
   429
		{
sl@0
   430
		RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);
sl@0
   431
		goto err;
sl@0
   432
		}
sl@0
   433
	BN_BLINDING_set_thread_id(ret, CRYPTO_thread_id());
sl@0
   434
err:
sl@0
   435
	BN_CTX_end(ctx);
sl@0
   436
	if (in_ctx == NULL)
sl@0
   437
		BN_CTX_free(ctx);
sl@0
   438
	if(rsa->e == NULL)
sl@0
   439
		BN_free(e);
sl@0
   440
sl@0
   441
	return ret;
sl@0
   442
}
sl@0
   443
sl@0
   444
EXPORT_C int RSA_memory_lock(RSA *r)
sl@0
   445
	{
sl@0
   446
	int i,j,k,off;
sl@0
   447
	char *p;
sl@0
   448
	BIGNUM *bn,**t[6],*b;
sl@0
   449
	BN_ULONG *ul;
sl@0
   450
sl@0
   451
	if (r->d == NULL) return(1);
sl@0
   452
	t[0]= &r->d;
sl@0
   453
	t[1]= &r->p;
sl@0
   454
	t[2]= &r->q;
sl@0
   455
	t[3]= &r->dmp1;
sl@0
   456
	t[4]= &r->dmq1;
sl@0
   457
	t[5]= &r->iqmp;
sl@0
   458
	k=sizeof(BIGNUM)*6;
sl@0
   459
	off=k/sizeof(BN_ULONG)+1;
sl@0
   460
	j=1;
sl@0
   461
	for (i=0; i<6; i++)
sl@0
   462
		j+= (*t[i])->top;
sl@0
   463
	if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
sl@0
   464
		{
sl@0
   465
		RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
sl@0
   466
		return(0);
sl@0
   467
		}
sl@0
   468
	bn=(BIGNUM *)p;
sl@0
   469
	ul=(BN_ULONG *)&(p[off]);
sl@0
   470
	for (i=0; i<6; i++)
sl@0
   471
		{
sl@0
   472
		b= *(t[i]);
sl@0
   473
		*(t[i])= &(bn[i]);
sl@0
   474
		memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
sl@0
   475
		bn[i].flags=BN_FLG_STATIC_DATA;
sl@0
   476
		bn[i].d=ul;
sl@0
   477
		memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
sl@0
   478
		ul+=b->top;
sl@0
   479
		BN_clear_free(b);
sl@0
   480
		}
sl@0
   481
	
sl@0
   482
	/* I should fix this so it can still be done */
sl@0
   483
	r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
sl@0
   484
sl@0
   485
	r->bignum_data=p;
sl@0
   486
	return(1);
sl@0
   487
	}