xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_blind.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_blind.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
4*2139Sjp161948  *
5*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
6*2139Sjp161948  * modification, are permitted provided that the following conditions
7*2139Sjp161948  * are met:
8*2139Sjp161948  *
9*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
10*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
11*2139Sjp161948  *
12*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
13*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in
14*2139Sjp161948  *    the documentation and/or other materials provided with the
15*2139Sjp161948  *    distribution.
16*2139Sjp161948  *
17*2139Sjp161948  * 3. All advertising materials mentioning features or use of this
18*2139Sjp161948  *    software must display the following acknowledgment:
19*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
20*2139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21*2139Sjp161948  *
22*2139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*2139Sjp161948  *    endorse or promote products derived from this software without
24*2139Sjp161948  *    prior written permission. For written permission, please contact
25*2139Sjp161948  *    openssl-core@openssl.org.
26*2139Sjp161948  *
27*2139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
28*2139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
29*2139Sjp161948  *    permission of the OpenSSL Project.
30*2139Sjp161948  *
31*2139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
32*2139Sjp161948  *    acknowledgment:
33*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
34*2139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35*2139Sjp161948  *
36*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*2139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*2139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*2139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*2139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*2139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*2139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*2139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*2139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*2139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*2139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
48*2139Sjp161948  * ====================================================================
49*2139Sjp161948  *
50*2139Sjp161948  * This product includes cryptographic software written by Eric Young
51*2139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
52*2139Sjp161948  * Hudson (tjh@cryptsoft.com).
53*2139Sjp161948  *
54*2139Sjp161948  */
550Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
560Sstevel@tonic-gate  * All rights reserved.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * This package is an SSL implementation written
590Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
600Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
630Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
640Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
650Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
660Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
670Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
700Sstevel@tonic-gate  * the code are not to be removed.
710Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
720Sstevel@tonic-gate  * as the author of the parts of the library used.
730Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
740Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
770Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
780Sstevel@tonic-gate  * are met:
790Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
800Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
810Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
820Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
830Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
840Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
850Sstevel@tonic-gate  *    must display the following acknowledgement:
860Sstevel@tonic-gate  *    "This product includes cryptographic software written by
870Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
880Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
890Sstevel@tonic-gate  *    being used are not cryptographic related :-).
900Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
910Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
920Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
950Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
960Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
970Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
980Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
990Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1000Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1010Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1020Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1030Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1040Sstevel@tonic-gate  * SUCH DAMAGE.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
1070Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
1080Sstevel@tonic-gate  * copied and put under another distribution licence
1090Sstevel@tonic-gate  * [including the GNU Public Licence.]
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include "cryptlib.h"
1140Sstevel@tonic-gate #include "bn_lcl.h"
1150Sstevel@tonic-gate 
116*2139Sjp161948 #define BN_BLINDING_COUNTER	32
117*2139Sjp161948 
118*2139Sjp161948 struct bn_blinding_st
119*2139Sjp161948 	{
120*2139Sjp161948 	BIGNUM *A;
121*2139Sjp161948 	BIGNUM *Ai;
122*2139Sjp161948 	BIGNUM *e;
123*2139Sjp161948 	BIGNUM *mod; /* just a reference */
124*2139Sjp161948 	unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b;
125*2139Sjp161948 				  * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */
126*2139Sjp161948 	unsigned int  counter;
127*2139Sjp161948 	unsigned long flags;
128*2139Sjp161948 	BN_MONT_CTX *m_ctx;
129*2139Sjp161948 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
130*2139Sjp161948 			  const BIGNUM *m, BN_CTX *ctx,
131*2139Sjp161948 			  BN_MONT_CTX *m_ctx);
132*2139Sjp161948 	};
133*2139Sjp161948 
BN_BLINDING_new(const BIGNUM * A,const BIGNUM * Ai,BIGNUM * mod)134*2139Sjp161948 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
1350Sstevel@tonic-gate 	{
1360Sstevel@tonic-gate 	BN_BLINDING *ret=NULL;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	bn_check_top(mod);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((ret=(BN_BLINDING *)OPENSSL_malloc(sizeof(BN_BLINDING))) == NULL)
1410Sstevel@tonic-gate 		{
1420Sstevel@tonic-gate 		BNerr(BN_F_BN_BLINDING_NEW,ERR_R_MALLOC_FAILURE);
1430Sstevel@tonic-gate 		return(NULL);
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	memset(ret,0,sizeof(BN_BLINDING));
146*2139Sjp161948 	if (A != NULL)
147*2139Sjp161948 		{
148*2139Sjp161948 		if ((ret->A  = BN_dup(A))  == NULL) goto err;
149*2139Sjp161948 		}
150*2139Sjp161948 	if (Ai != NULL)
151*2139Sjp161948 		{
152*2139Sjp161948 		if ((ret->Ai = BN_dup(Ai)) == NULL) goto err;
153*2139Sjp161948 		}
154*2139Sjp161948 	ret->mod = mod;
155*2139Sjp161948 	ret->counter = BN_BLINDING_COUNTER;
1560Sstevel@tonic-gate 	return(ret);
1570Sstevel@tonic-gate err:
1580Sstevel@tonic-gate 	if (ret != NULL) BN_BLINDING_free(ret);
1590Sstevel@tonic-gate 	return(NULL);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
BN_BLINDING_free(BN_BLINDING * r)1620Sstevel@tonic-gate void BN_BLINDING_free(BN_BLINDING *r)
1630Sstevel@tonic-gate 	{
1640Sstevel@tonic-gate 	if(r == NULL)
1650Sstevel@tonic-gate 	    return;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (r->A  != NULL) BN_free(r->A );
1680Sstevel@tonic-gate 	if (r->Ai != NULL) BN_free(r->Ai);
169*2139Sjp161948 	if (r->e  != NULL) BN_free(r->e );
1700Sstevel@tonic-gate 	OPENSSL_free(r);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
BN_BLINDING_update(BN_BLINDING * b,BN_CTX * ctx)1730Sstevel@tonic-gate int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
1740Sstevel@tonic-gate 	{
1750Sstevel@tonic-gate 	int ret=0;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((b->A == NULL) || (b->Ai == NULL))
1780Sstevel@tonic-gate 		{
1790Sstevel@tonic-gate 		BNerr(BN_F_BN_BLINDING_UPDATE,BN_R_NOT_INITIALIZED);
1800Sstevel@tonic-gate 		goto err;
1810Sstevel@tonic-gate 		}
182*2139Sjp161948 
183*2139Sjp161948 	if (--(b->counter) == 0 && b->e != NULL &&
184*2139Sjp161948 		!(b->flags & BN_BLINDING_NO_RECREATE))
185*2139Sjp161948 		{
186*2139Sjp161948 		/* re-create blinding parameters */
187*2139Sjp161948 		if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
188*2139Sjp161948 			goto err;
189*2139Sjp161948 		}
190*2139Sjp161948 	else if (!(b->flags & BN_BLINDING_NO_UPDATE))
191*2139Sjp161948 		{
192*2139Sjp161948 		if (!BN_mod_mul(b->A,b->A,b->A,b->mod,ctx)) goto err;
193*2139Sjp161948 		if (!BN_mod_mul(b->Ai,b->Ai,b->Ai,b->mod,ctx)) goto err;
194*2139Sjp161948 		}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	ret=1;
1970Sstevel@tonic-gate err:
198*2139Sjp161948 	if (b->counter == 0)
199*2139Sjp161948 		b->counter = BN_BLINDING_COUNTER;
2000Sstevel@tonic-gate 	return(ret);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
BN_BLINDING_convert(BIGNUM * n,BN_BLINDING * b,BN_CTX * ctx)2030Sstevel@tonic-gate int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
2040Sstevel@tonic-gate 	{
205*2139Sjp161948 	return BN_BLINDING_convert_ex(n, NULL, b, ctx);
206*2139Sjp161948 	}
207*2139Sjp161948 
BN_BLINDING_convert_ex(BIGNUM * n,BIGNUM * r,BN_BLINDING * b,BN_CTX * ctx)208*2139Sjp161948 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
209*2139Sjp161948 	{
210*2139Sjp161948 	int ret = 1;
211*2139Sjp161948 
2120Sstevel@tonic-gate 	bn_check_top(n);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if ((b->A == NULL) || (b->Ai == NULL))
2150Sstevel@tonic-gate 		{
216*2139Sjp161948 		BNerr(BN_F_BN_BLINDING_CONVERT_EX,BN_R_NOT_INITIALIZED);
2170Sstevel@tonic-gate 		return(0);
2180Sstevel@tonic-gate 		}
219*2139Sjp161948 
220*2139Sjp161948 	if (r != NULL)
221*2139Sjp161948 		{
222*2139Sjp161948 		if (!BN_copy(r, b->Ai)) ret=0;
223*2139Sjp161948 		}
224*2139Sjp161948 
225*2139Sjp161948 	if (!BN_mod_mul(n,n,b->A,b->mod,ctx)) ret=0;
226*2139Sjp161948 
227*2139Sjp161948 	return ret;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
BN_BLINDING_invert(BIGNUM * n,BN_BLINDING * b,BN_CTX * ctx)2300Sstevel@tonic-gate int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
2310Sstevel@tonic-gate 	{
232*2139Sjp161948 	return BN_BLINDING_invert_ex(n, NULL, b, ctx);
233*2139Sjp161948 	}
234*2139Sjp161948 
BN_BLINDING_invert_ex(BIGNUM * n,const BIGNUM * r,BN_BLINDING * b,BN_CTX * ctx)235*2139Sjp161948 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
236*2139Sjp161948 	{
2370Sstevel@tonic-gate 	int ret;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	bn_check_top(n);
2400Sstevel@tonic-gate 	if ((b->A == NULL) || (b->Ai == NULL))
2410Sstevel@tonic-gate 		{
242*2139Sjp161948 		BNerr(BN_F_BN_BLINDING_INVERT_EX,BN_R_NOT_INITIALIZED);
2430Sstevel@tonic-gate 		return(0);
2440Sstevel@tonic-gate 		}
245*2139Sjp161948 
246*2139Sjp161948 	if (r != NULL)
247*2139Sjp161948 		ret = BN_mod_mul(n, n, r, b->mod, ctx);
248*2139Sjp161948 	else
249*2139Sjp161948 		ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
250*2139Sjp161948 
251*2139Sjp161948 	if (ret >= 0)
2520Sstevel@tonic-gate 		{
2530Sstevel@tonic-gate 		if (!BN_BLINDING_update(b,ctx))
2540Sstevel@tonic-gate 			return(0);
2550Sstevel@tonic-gate 		}
256*2139Sjp161948 	bn_check_top(n);
2570Sstevel@tonic-gate 	return(ret);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
BN_BLINDING_get_thread_id(const BN_BLINDING * b)260*2139Sjp161948 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *b)
261*2139Sjp161948 	{
262*2139Sjp161948 	return b->thread_id;
263*2139Sjp161948 	}
264*2139Sjp161948 
BN_BLINDING_set_thread_id(BN_BLINDING * b,unsigned long n)265*2139Sjp161948 void BN_BLINDING_set_thread_id(BN_BLINDING *b, unsigned long n)
266*2139Sjp161948 	{
267*2139Sjp161948 	b->thread_id = n;
268*2139Sjp161948 	}
269*2139Sjp161948 
BN_BLINDING_get_flags(const BN_BLINDING * b)270*2139Sjp161948 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
271*2139Sjp161948 	{
272*2139Sjp161948 	return b->flags;
273*2139Sjp161948 	}
274*2139Sjp161948 
BN_BLINDING_set_flags(BN_BLINDING * b,unsigned long flags)275*2139Sjp161948 void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
276*2139Sjp161948 	{
277*2139Sjp161948 	b->flags = flags;
278*2139Sjp161948 	}
279*2139Sjp161948 
BN_BLINDING_create_param(BN_BLINDING * b,const BIGNUM * e,BIGNUM * m,BN_CTX * ctx,int (* bn_mod_exp)(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx),BN_MONT_CTX * m_ctx)280*2139Sjp161948 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
281*2139Sjp161948 	const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
282*2139Sjp161948 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
283*2139Sjp161948 			  const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
284*2139Sjp161948 	BN_MONT_CTX *m_ctx)
285*2139Sjp161948 {
286*2139Sjp161948 	int    retry_counter = 32;
287*2139Sjp161948 	BN_BLINDING *ret = NULL;
288*2139Sjp161948 
289*2139Sjp161948 	if (b == NULL)
290*2139Sjp161948 		ret = BN_BLINDING_new(NULL, NULL, m);
291*2139Sjp161948 	else
292*2139Sjp161948 		ret = b;
293*2139Sjp161948 
294*2139Sjp161948 	if (ret == NULL)
295*2139Sjp161948 		goto err;
296*2139Sjp161948 
297*2139Sjp161948 	if (ret->A  == NULL && (ret->A  = BN_new()) == NULL)
298*2139Sjp161948 		goto err;
299*2139Sjp161948 	if (ret->Ai == NULL && (ret->Ai	= BN_new()) == NULL)
300*2139Sjp161948 		goto err;
301*2139Sjp161948 
302*2139Sjp161948 	if (e != NULL)
303*2139Sjp161948 		{
304*2139Sjp161948 		if (ret->e != NULL)
305*2139Sjp161948 			BN_free(ret->e);
306*2139Sjp161948 		ret->e = BN_dup(e);
307*2139Sjp161948 		}
308*2139Sjp161948 	if (ret->e == NULL)
309*2139Sjp161948 		goto err;
310*2139Sjp161948 
311*2139Sjp161948 	if (bn_mod_exp != NULL)
312*2139Sjp161948 		ret->bn_mod_exp = bn_mod_exp;
313*2139Sjp161948 	if (m_ctx != NULL)
314*2139Sjp161948 		ret->m_ctx = m_ctx;
315*2139Sjp161948 
316*2139Sjp161948 	do {
317*2139Sjp161948 		if (!BN_rand_range(ret->A, ret->mod)) goto err;
318*2139Sjp161948 		if (BN_mod_inverse(ret->Ai, ret->A, ret->mod, ctx) == NULL)
319*2139Sjp161948 			{
320*2139Sjp161948 			/* this should almost never happen for good RSA keys */
321*2139Sjp161948 			unsigned long error = ERR_peek_last_error();
322*2139Sjp161948 			if (ERR_GET_REASON(error) == BN_R_NO_INVERSE)
323*2139Sjp161948 				{
324*2139Sjp161948 				if (retry_counter-- == 0)
325*2139Sjp161948 				{
326*2139Sjp161948 					BNerr(BN_F_BN_BLINDING_CREATE_PARAM,
327*2139Sjp161948 						BN_R_TOO_MANY_ITERATIONS);
328*2139Sjp161948 					goto err;
329*2139Sjp161948 				}
330*2139Sjp161948 				ERR_clear_error();
331*2139Sjp161948 				}
332*2139Sjp161948 			else
333*2139Sjp161948 				goto err;
334*2139Sjp161948 			}
335*2139Sjp161948 		else
336*2139Sjp161948 			break;
337*2139Sjp161948 	} while (1);
338*2139Sjp161948 
339*2139Sjp161948 	if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL)
340*2139Sjp161948 		{
341*2139Sjp161948 		if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
342*2139Sjp161948 			goto err;
343*2139Sjp161948 		}
344*2139Sjp161948 	else
345*2139Sjp161948 		{
346*2139Sjp161948 		if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
347*2139Sjp161948 			goto err;
348*2139Sjp161948 		}
349*2139Sjp161948 
350*2139Sjp161948 	return ret;
351*2139Sjp161948 err:
352*2139Sjp161948 	if (b == NULL && ret != NULL)
353*2139Sjp161948 		{
354*2139Sjp161948 		BN_BLINDING_free(ret);
355*2139Sjp161948 		ret = NULL;
356*2139Sjp161948 		}
357*2139Sjp161948 
358*2139Sjp161948 	return ret;
359*2139Sjp161948 }
360