xref: /onnv-gate/usr/src/common/openssl/crypto/rsa/rsa_lib.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/rsa/rsa_lib.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <openssl/crypto.h>
61*0Sstevel@tonic-gate #include "cryptlib.h"
62*0Sstevel@tonic-gate #include <openssl/lhash.h>
63*0Sstevel@tonic-gate #include <openssl/bn.h>
64*0Sstevel@tonic-gate #include <openssl/rsa.h>
65*0Sstevel@tonic-gate #include <openssl/rand.h>
66*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
67*0Sstevel@tonic-gate #include <openssl/engine.h>
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static const RSA_METHOD *default_RSA_meth=NULL;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate RSA *RSA_new(void)
75*0Sstevel@tonic-gate 	{
76*0Sstevel@tonic-gate 	RSA *r=RSA_new_method(NULL);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	return r;
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate void RSA_set_default_method(const RSA_METHOD *meth)
82*0Sstevel@tonic-gate 	{
83*0Sstevel@tonic-gate 	default_RSA_meth = meth;
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate const RSA_METHOD *RSA_get_default_method(void)
87*0Sstevel@tonic-gate 	{
88*0Sstevel@tonic-gate 	if (default_RSA_meth == NULL)
89*0Sstevel@tonic-gate 		{
90*0Sstevel@tonic-gate #ifdef RSA_NULL
91*0Sstevel@tonic-gate 		default_RSA_meth=RSA_null_method();
92*0Sstevel@tonic-gate #else
93*0Sstevel@tonic-gate #if 0 /* was: #ifdef RSAref */
94*0Sstevel@tonic-gate 		default_RSA_meth=RSA_PKCS1_RSAref();
95*0Sstevel@tonic-gate #else
96*0Sstevel@tonic-gate 		default_RSA_meth=RSA_PKCS1_SSLeay();
97*0Sstevel@tonic-gate #endif
98*0Sstevel@tonic-gate #endif
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	return default_RSA_meth;
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate const RSA_METHOD *RSA_get_method(const RSA *rsa)
105*0Sstevel@tonic-gate 	{
106*0Sstevel@tonic-gate 	return rsa->meth;
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
110*0Sstevel@tonic-gate 	{
111*0Sstevel@tonic-gate 	/* NB: The caller is specifically setting a method, so it's not up to us
112*0Sstevel@tonic-gate 	 * to deal with which ENGINE it comes from. */
113*0Sstevel@tonic-gate 	const RSA_METHOD *mtmp;
114*0Sstevel@tonic-gate 	mtmp = rsa->meth;
115*0Sstevel@tonic-gate 	if (mtmp->finish) mtmp->finish(rsa);
116*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
117*0Sstevel@tonic-gate 	if (rsa->engine)
118*0Sstevel@tonic-gate 		{
119*0Sstevel@tonic-gate 		ENGINE_finish(rsa->engine);
120*0Sstevel@tonic-gate 		rsa->engine = NULL;
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate #endif
123*0Sstevel@tonic-gate 	rsa->meth = meth;
124*0Sstevel@tonic-gate 	if (meth->init) meth->init(rsa);
125*0Sstevel@tonic-gate 	return 1;
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate RSA *RSA_new_method(ENGINE *engine)
129*0Sstevel@tonic-gate 	{
130*0Sstevel@tonic-gate 	RSA *ret;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
133*0Sstevel@tonic-gate 	if (ret == NULL)
134*0Sstevel@tonic-gate 		{
135*0Sstevel@tonic-gate 		RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
136*0Sstevel@tonic-gate 		return NULL;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ret->meth = RSA_get_default_method();
140*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
141*0Sstevel@tonic-gate 	if (engine)
142*0Sstevel@tonic-gate 		{
143*0Sstevel@tonic-gate 		if (!ENGINE_init(engine))
144*0Sstevel@tonic-gate 			{
145*0Sstevel@tonic-gate 			RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
146*0Sstevel@tonic-gate 			OPENSSL_free(ret);
147*0Sstevel@tonic-gate 			return NULL;
148*0Sstevel@tonic-gate 			}
149*0Sstevel@tonic-gate 		ret->engine = engine;
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 	else
152*0Sstevel@tonic-gate 		ret->engine = ENGINE_get_default_RSA();
153*0Sstevel@tonic-gate 	if(ret->engine)
154*0Sstevel@tonic-gate 		{
155*0Sstevel@tonic-gate 		ret->meth = ENGINE_get_RSA(ret->engine);
156*0Sstevel@tonic-gate 		if(!ret->meth)
157*0Sstevel@tonic-gate 			{
158*0Sstevel@tonic-gate 			RSAerr(RSA_F_RSA_NEW_METHOD,
159*0Sstevel@tonic-gate 				ERR_R_ENGINE_LIB);
160*0Sstevel@tonic-gate 			ENGINE_finish(ret->engine);
161*0Sstevel@tonic-gate 			OPENSSL_free(ret);
162*0Sstevel@tonic-gate 			return NULL;
163*0Sstevel@tonic-gate 			}
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	ret->pad=0;
168*0Sstevel@tonic-gate 	ret->version=0;
169*0Sstevel@tonic-gate 	ret->n=NULL;
170*0Sstevel@tonic-gate 	ret->e=NULL;
171*0Sstevel@tonic-gate 	ret->d=NULL;
172*0Sstevel@tonic-gate 	ret->p=NULL;
173*0Sstevel@tonic-gate 	ret->q=NULL;
174*0Sstevel@tonic-gate 	ret->dmp1=NULL;
175*0Sstevel@tonic-gate 	ret->dmq1=NULL;
176*0Sstevel@tonic-gate 	ret->iqmp=NULL;
177*0Sstevel@tonic-gate 	ret->references=1;
178*0Sstevel@tonic-gate 	ret->_method_mod_n=NULL;
179*0Sstevel@tonic-gate 	ret->_method_mod_p=NULL;
180*0Sstevel@tonic-gate 	ret->_method_mod_q=NULL;
181*0Sstevel@tonic-gate 	ret->blinding=NULL;
182*0Sstevel@tonic-gate 	ret->bignum_data=NULL;
183*0Sstevel@tonic-gate 	ret->flags=ret->meth->flags;
184*0Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
185*0Sstevel@tonic-gate 	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
186*0Sstevel@tonic-gate 		{
187*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
188*0Sstevel@tonic-gate 		if (ret->engine)
189*0Sstevel@tonic-gate 			ENGINE_finish(ret->engine);
190*0Sstevel@tonic-gate #endif
191*0Sstevel@tonic-gate 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
192*0Sstevel@tonic-gate 		OPENSSL_free(ret);
193*0Sstevel@tonic-gate 		ret=NULL;
194*0Sstevel@tonic-gate 		}
195*0Sstevel@tonic-gate 	return(ret);
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate void RSA_free(RSA *r)
199*0Sstevel@tonic-gate 	{
200*0Sstevel@tonic-gate 	int i;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (r == NULL) return;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
205*0Sstevel@tonic-gate #ifdef REF_PRINT
206*0Sstevel@tonic-gate 	REF_PRINT("RSA",r);
207*0Sstevel@tonic-gate #endif
208*0Sstevel@tonic-gate 	if (i > 0) return;
209*0Sstevel@tonic-gate #ifdef REF_CHECK
210*0Sstevel@tonic-gate 	if (i < 0)
211*0Sstevel@tonic-gate 		{
212*0Sstevel@tonic-gate 		fprintf(stderr,"RSA_free, bad reference count\n");
213*0Sstevel@tonic-gate 		abort();
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (r->meth->finish)
218*0Sstevel@tonic-gate 		r->meth->finish(r);
219*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
220*0Sstevel@tonic-gate 	if (r->engine)
221*0Sstevel@tonic-gate 		ENGINE_finish(r->engine);
222*0Sstevel@tonic-gate #endif
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	if (r->n != NULL) BN_clear_free(r->n);
227*0Sstevel@tonic-gate 	if (r->e != NULL) BN_clear_free(r->e);
228*0Sstevel@tonic-gate 	if (r->d != NULL) BN_clear_free(r->d);
229*0Sstevel@tonic-gate 	if (r->p != NULL) BN_clear_free(r->p);
230*0Sstevel@tonic-gate 	if (r->q != NULL) BN_clear_free(r->q);
231*0Sstevel@tonic-gate 	if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
232*0Sstevel@tonic-gate 	if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
233*0Sstevel@tonic-gate 	if (r->iqmp != NULL) BN_clear_free(r->iqmp);
234*0Sstevel@tonic-gate 	if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
235*0Sstevel@tonic-gate 	if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
236*0Sstevel@tonic-gate 	OPENSSL_free(r);
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate int RSA_up_ref(RSA *r)
240*0Sstevel@tonic-gate 	{
241*0Sstevel@tonic-gate 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
242*0Sstevel@tonic-gate #ifdef REF_PRINT
243*0Sstevel@tonic-gate 	REF_PRINT("RSA",r);
244*0Sstevel@tonic-gate #endif
245*0Sstevel@tonic-gate #ifdef REF_CHECK
246*0Sstevel@tonic-gate 	if (i < 2)
247*0Sstevel@tonic-gate 		{
248*0Sstevel@tonic-gate 		fprintf(stderr, "RSA_up_ref, bad reference count\n");
249*0Sstevel@tonic-gate 		abort();
250*0Sstevel@tonic-gate 		}
251*0Sstevel@tonic-gate #endif
252*0Sstevel@tonic-gate 	return ((i > 1) ? 1 : 0);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
256*0Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
257*0Sstevel@tonic-gate         {
258*0Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
259*0Sstevel@tonic-gate 				new_func, dup_func, free_func);
260*0Sstevel@tonic-gate         }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate int RSA_set_ex_data(RSA *r, int idx, void *arg)
263*0Sstevel@tonic-gate 	{
264*0Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
265*0Sstevel@tonic-gate 	}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate void *RSA_get_ex_data(const RSA *r, int idx)
268*0Sstevel@tonic-gate 	{
269*0Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&r->ex_data,idx));
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate int RSA_size(const RSA *r)
273*0Sstevel@tonic-gate 	{
274*0Sstevel@tonic-gate 	return(BN_num_bytes(r->n));
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
278*0Sstevel@tonic-gate 	     RSA *rsa, int padding)
279*0Sstevel@tonic-gate 	{
280*0Sstevel@tonic-gate 	return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
284*0Sstevel@tonic-gate 	     RSA *rsa, int padding)
285*0Sstevel@tonic-gate 	{
286*0Sstevel@tonic-gate 	return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
290*0Sstevel@tonic-gate 	     RSA *rsa, int padding)
291*0Sstevel@tonic-gate 	{
292*0Sstevel@tonic-gate 	return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
296*0Sstevel@tonic-gate 	     RSA *rsa, int padding)
297*0Sstevel@tonic-gate 	{
298*0Sstevel@tonic-gate 	return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate int RSA_flags(const RSA *r)
302*0Sstevel@tonic-gate 	{
303*0Sstevel@tonic-gate 	return((r == NULL)?0:r->meth->flags);
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate void RSA_blinding_off(RSA *rsa)
307*0Sstevel@tonic-gate 	{
308*0Sstevel@tonic-gate 	if (rsa->blinding != NULL)
309*0Sstevel@tonic-gate 		{
310*0Sstevel@tonic-gate 		BN_BLINDING_free(rsa->blinding);
311*0Sstevel@tonic-gate 		rsa->blinding=NULL;
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 	rsa->flags &= ~RSA_FLAG_BLINDING;
314*0Sstevel@tonic-gate 	rsa->flags |= RSA_FLAG_NO_BLINDING;
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
318*0Sstevel@tonic-gate 	{
319*0Sstevel@tonic-gate 	BIGNUM *A,*Ai = NULL;
320*0Sstevel@tonic-gate 	BN_CTX *ctx;
321*0Sstevel@tonic-gate 	int ret=0;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (p_ctx == NULL)
324*0Sstevel@tonic-gate 		{
325*0Sstevel@tonic-gate 		if ((ctx=BN_CTX_new()) == NULL) goto err;
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 	else
328*0Sstevel@tonic-gate 		ctx=p_ctx;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	/* XXXXX: Shouldn't this be RSA_blinding_off(rsa)? */
331*0Sstevel@tonic-gate 	if (rsa->blinding != NULL)
332*0Sstevel@tonic-gate 		{
333*0Sstevel@tonic-gate 		BN_BLINDING_free(rsa->blinding);
334*0Sstevel@tonic-gate 		rsa->blinding = NULL;
335*0Sstevel@tonic-gate 		}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/* NB: similar code appears in setup_blinding (rsa_eay.c);
338*0Sstevel@tonic-gate 	 * this should be placed in a new function of its own, but for reasons
339*0Sstevel@tonic-gate 	 * of binary compatibility can't */
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	BN_CTX_start(ctx);
342*0Sstevel@tonic-gate 	A = BN_CTX_get(ctx);
343*0Sstevel@tonic-gate 	if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
344*0Sstevel@tonic-gate 		{
345*0Sstevel@tonic-gate 		/* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
346*0Sstevel@tonic-gate 		RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
347*0Sstevel@tonic-gate 		if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 	else
350*0Sstevel@tonic-gate 		{
351*0Sstevel@tonic-gate 		if (!BN_rand_range(A,rsa->n)) goto err;
352*0Sstevel@tonic-gate 		}
353*0Sstevel@tonic-gate 	if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
356*0Sstevel@tonic-gate 		goto err;
357*0Sstevel@tonic-gate 	if ((rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n)) == NULL) goto err;
358*0Sstevel@tonic-gate 	/* to make things thread-safe without excessive locking,
359*0Sstevel@tonic-gate 	 * rsa->blinding will be used just by the current thread: */
360*0Sstevel@tonic-gate 	rsa->blinding->thread_id = CRYPTO_thread_id();
361*0Sstevel@tonic-gate 	rsa->flags |= RSA_FLAG_BLINDING;
362*0Sstevel@tonic-gate 	rsa->flags &= ~RSA_FLAG_NO_BLINDING;
363*0Sstevel@tonic-gate 	ret=1;
364*0Sstevel@tonic-gate err:
365*0Sstevel@tonic-gate 	if (Ai != NULL) BN_free(Ai);
366*0Sstevel@tonic-gate 	BN_CTX_end(ctx);
367*0Sstevel@tonic-gate 	if (ctx != p_ctx) BN_CTX_free(ctx);
368*0Sstevel@tonic-gate 	return(ret);
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate int RSA_memory_lock(RSA *r)
372*0Sstevel@tonic-gate 	{
373*0Sstevel@tonic-gate 	int i,j,k,off;
374*0Sstevel@tonic-gate 	char *p;
375*0Sstevel@tonic-gate 	BIGNUM *bn,**t[6],*b;
376*0Sstevel@tonic-gate 	BN_ULONG *ul;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	if (r->d == NULL) return(1);
379*0Sstevel@tonic-gate 	t[0]= &r->d;
380*0Sstevel@tonic-gate 	t[1]= &r->p;
381*0Sstevel@tonic-gate 	t[2]= &r->q;
382*0Sstevel@tonic-gate 	t[3]= &r->dmp1;
383*0Sstevel@tonic-gate 	t[4]= &r->dmq1;
384*0Sstevel@tonic-gate 	t[5]= &r->iqmp;
385*0Sstevel@tonic-gate 	k=sizeof(BIGNUM)*6;
386*0Sstevel@tonic-gate 	off=k/sizeof(BN_ULONG)+1;
387*0Sstevel@tonic-gate 	j=1;
388*0Sstevel@tonic-gate 	for (i=0; i<6; i++)
389*0Sstevel@tonic-gate 		j+= (*t[i])->top;
390*0Sstevel@tonic-gate 	if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
391*0Sstevel@tonic-gate 		{
392*0Sstevel@tonic-gate 		RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
393*0Sstevel@tonic-gate 		return(0);
394*0Sstevel@tonic-gate 		}
395*0Sstevel@tonic-gate 	bn=(BIGNUM *)p;
396*0Sstevel@tonic-gate 	ul=(BN_ULONG *)&(p[off]);
397*0Sstevel@tonic-gate 	for (i=0; i<6; i++)
398*0Sstevel@tonic-gate 		{
399*0Sstevel@tonic-gate 		b= *(t[i]);
400*0Sstevel@tonic-gate 		*(t[i])= &(bn[i]);
401*0Sstevel@tonic-gate 		memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
402*0Sstevel@tonic-gate 		bn[i].flags=BN_FLG_STATIC_DATA;
403*0Sstevel@tonic-gate 		bn[i].d=ul;
404*0Sstevel@tonic-gate 		memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
405*0Sstevel@tonic-gate 		ul+=b->top;
406*0Sstevel@tonic-gate 		BN_clear_free(b);
407*0Sstevel@tonic-gate 		}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	/* I should fix this so it can still be done */
410*0Sstevel@tonic-gate 	r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	r->bignum_data=p;
413*0Sstevel@tonic-gate 	return(1);
414*0Sstevel@tonic-gate 	}
415