xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_mont.c (revision 5004:ed80345a3522)
10Sstevel@tonic-gate /* crypto/bn/bn_mont.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Details about Montgomery multiplication algorithms can be found at
610Sstevel@tonic-gate  * http://security.ece.orst.edu/publications.html, e.g.
620Sstevel@tonic-gate  * http://security.ece.orst.edu/koc/papers/j37acmon.pdf and
630Sstevel@tonic-gate  * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <stdio.h>
670Sstevel@tonic-gate #include "cryptlib.h"
680Sstevel@tonic-gate #include "bn_lcl.h"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define MONT_WORD /* use the faster word-based algorithm */
710Sstevel@tonic-gate 
BN_mod_mul_montgomery(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,BN_MONT_CTX * mont,BN_CTX * ctx)720Sstevel@tonic-gate int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
730Sstevel@tonic-gate 			  BN_MONT_CTX *mont, BN_CTX *ctx)
740Sstevel@tonic-gate 	{
750Sstevel@tonic-gate 	BIGNUM *tmp;
760Sstevel@tonic-gate 	int ret=0;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	BN_CTX_start(ctx);
790Sstevel@tonic-gate 	tmp = BN_CTX_get(ctx);
800Sstevel@tonic-gate 	if (tmp == NULL) goto err;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	bn_check_top(tmp);
830Sstevel@tonic-gate 	if (a == b)
840Sstevel@tonic-gate 		{
850Sstevel@tonic-gate 		if (!BN_sqr(tmp,a,ctx)) goto err;
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	else
880Sstevel@tonic-gate 		{
890Sstevel@tonic-gate 		if (!BN_mul(tmp,a,b,ctx)) goto err;
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 	/* reduce from aRR to aR */
920Sstevel@tonic-gate 	if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;
932139Sjp161948 	bn_check_top(r);
940Sstevel@tonic-gate 	ret=1;
950Sstevel@tonic-gate err:
960Sstevel@tonic-gate 	BN_CTX_end(ctx);
970Sstevel@tonic-gate 	return(ret);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
BN_from_montgomery(BIGNUM * ret,const BIGNUM * a,BN_MONT_CTX * mont,BN_CTX * ctx)1000Sstevel@tonic-gate int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
1010Sstevel@tonic-gate 	     BN_CTX *ctx)
1020Sstevel@tonic-gate 	{
1030Sstevel@tonic-gate 	int retn=0;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #ifdef MONT_WORD
1060Sstevel@tonic-gate 	BIGNUM *n,*r;
1070Sstevel@tonic-gate 	BN_ULONG *ap,*np,*rp,n0,v,*nrp;
1080Sstevel@tonic-gate 	int al,nl,max,i,x,ri;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	BN_CTX_start(ctx);
1110Sstevel@tonic-gate 	if ((r = BN_CTX_get(ctx)) == NULL) goto err;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (!BN_copy(r,a)) goto err;
1140Sstevel@tonic-gate 	n= &(mont->N);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	ap=a->d;
1170Sstevel@tonic-gate 	/* mont->ri is the size of mont->N in bits (rounded up
1180Sstevel@tonic-gate 	   to the word size) */
1190Sstevel@tonic-gate 	al=ri=mont->ri/BN_BITS2;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	nl=n->top;
1220Sstevel@tonic-gate 	if ((al == 0) || (nl == 0)) { r->top=0; return(1); }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	max=(nl+al+1); /* allow for overflow (no?) XXX */
1250Sstevel@tonic-gate 	if (bn_wexpand(r,max) == NULL) goto err;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	r->neg=a->neg^n->neg;
1280Sstevel@tonic-gate 	np=n->d;
1290Sstevel@tonic-gate 	rp=r->d;
1300Sstevel@tonic-gate 	nrp= &(r->d[nl]);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/* clear the top words of T */
1330Sstevel@tonic-gate #if 1
1340Sstevel@tonic-gate 	for (i=r->top; i<max; i++) /* memset? XXX */
1350Sstevel@tonic-gate 		r->d[i]=0;
1360Sstevel@tonic-gate #else
1370Sstevel@tonic-gate 	memset(&(r->d[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	r->top=max;
1410Sstevel@tonic-gate 	n0=mont->n0;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #ifdef BN_COUNT
1440Sstevel@tonic-gate 	fprintf(stderr,"word BN_from_montgomery %d * %d\n",nl,nl);
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate 	for (i=0; i<nl; i++)
1470Sstevel@tonic-gate 		{
1480Sstevel@tonic-gate #ifdef __TANDEM
1490Sstevel@tonic-gate                 {
1500Sstevel@tonic-gate                    long long t1;
1510Sstevel@tonic-gate                    long long t2;
1520Sstevel@tonic-gate                    long long t3;
1530Sstevel@tonic-gate                    t1 = rp[0] * (n0 & 0177777);
1540Sstevel@tonic-gate                    t2 = 037777600000l;
1550Sstevel@tonic-gate                    t2 = n0 & t2;
1560Sstevel@tonic-gate                    t3 = rp[0] & 0177777;
1570Sstevel@tonic-gate                    t2 = (t3 * t2) & BN_MASK2;
1580Sstevel@tonic-gate                    t1 = t1 + t2;
1590Sstevel@tonic-gate                    v=bn_mul_add_words(rp,np,nl,(BN_ULONG) t1);
1600Sstevel@tonic-gate                 }
1610Sstevel@tonic-gate #else
1620Sstevel@tonic-gate 		v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 		nrp++;
1650Sstevel@tonic-gate 		rp++;
1660Sstevel@tonic-gate 		if (((nrp[-1]+=v)&BN_MASK2) >= v)
1670Sstevel@tonic-gate 			continue;
1680Sstevel@tonic-gate 		else
1690Sstevel@tonic-gate 			{
1700Sstevel@tonic-gate 			if (((++nrp[0])&BN_MASK2) != 0) continue;
1710Sstevel@tonic-gate 			if (((++nrp[1])&BN_MASK2) != 0) continue;
1720Sstevel@tonic-gate 			for (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ;
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 		}
1752139Sjp161948 	bn_correct_top(r);
1760Sstevel@tonic-gate 
177*5004Sjp161948 	/* mont->ri will be a multiple of the word size and below code
178*5004Sjp161948 	 * is kind of BN_rshift(ret,r,mont->ri) equivalent */
179*5004Sjp161948 	if (r->top <= ri)
180*5004Sjp161948 		{
181*5004Sjp161948 		ret->top=0;
182*5004Sjp161948 		retn=1;
183*5004Sjp161948 		goto err;
184*5004Sjp161948 		}
185*5004Sjp161948 	al=r->top-ri;
186*5004Sjp161948 
187*5004Sjp161948 # define BRANCH_FREE 1
188*5004Sjp161948 # if BRANCH_FREE
189*5004Sjp161948 	if (bn_wexpand(ret,ri) == NULL) goto err;
190*5004Sjp161948 	x=0-(((al-ri)>>(sizeof(al)*8-1))&1);
191*5004Sjp161948 	ret->top=x=(ri&~x)|(al&x);	/* min(ri,al) */
192*5004Sjp161948 	ret->neg=r->neg;
193*5004Sjp161948 
1940Sstevel@tonic-gate 	rp=ret->d;
195*5004Sjp161948 	ap=&(r->d[ri]);
196*5004Sjp161948 
197*5004Sjp161948 	{
198*5004Sjp161948 	size_t m1,m2;
199*5004Sjp161948 
200*5004Sjp161948 	v=bn_sub_words(rp,ap,np,ri);
201*5004Sjp161948 	/* this ----------------^^ works even in al<ri case
202*5004Sjp161948 	 * thanks to zealous zeroing of top of the vector in the
203*5004Sjp161948 	 * beginning. */
204*5004Sjp161948 
205*5004Sjp161948 	/* if (al==ri && !v) || al>ri) nrp=rp; else nrp=ap; */
206*5004Sjp161948 	/* in other words if subtraction result is real, then
207*5004Sjp161948 	 * trick unconditional memcpy below to perform in-place
208*5004Sjp161948 	 * "refresh" instead of actual copy. */
209*5004Sjp161948 	m1=0-(size_t)(((al-ri)>>(sizeof(al)*8-1))&1);	/* al<ri */
210*5004Sjp161948 	m2=0-(size_t)(((ri-al)>>(sizeof(al)*8-1))&1);	/* al>ri */
211*5004Sjp161948 	m1|=m2;			/* (al!=ri) */
212*5004Sjp161948 	m1|=(0-(size_t)v);	/* (al!=ri || v) */
213*5004Sjp161948 	m1&=~m2;		/* (al!=ri || v) && !al>ri */
214*5004Sjp161948 	nrp=(BN_ULONG *)(((size_t)rp&~m1)|((size_t)ap&m1));
215*5004Sjp161948 	}
216*5004Sjp161948 
217*5004Sjp161948 	/* 'i<ri' is chosen to eliminate dependency on input data, even
218*5004Sjp161948 	 * though it results in redundant copy in al<ri case. */
219*5004Sjp161948 	for (i=0,ri-=4; i<ri; i+=4)
220*5004Sjp161948 		{
221*5004Sjp161948 		BN_ULONG t1,t2,t3,t4;
222*5004Sjp161948 
223*5004Sjp161948 		t1=nrp[i+0];
224*5004Sjp161948 		t2=nrp[i+1];
225*5004Sjp161948 		t3=nrp[i+2];	ap[i+0]=0;
226*5004Sjp161948 		t4=nrp[i+3];	ap[i+1]=0;
227*5004Sjp161948 		rp[i+0]=t1;	ap[i+2]=0;
228*5004Sjp161948 		rp[i+1]=t2;	ap[i+3]=0;
229*5004Sjp161948 		rp[i+2]=t3;
230*5004Sjp161948 		rp[i+3]=t4;
231*5004Sjp161948 		}
232*5004Sjp161948 	for (ri+=4; i<ri; i++)
233*5004Sjp161948 		rp[i]=nrp[i], ap[i]=0;
234*5004Sjp161948 # else
235*5004Sjp161948 	if (bn_wexpand(ret,al) == NULL) goto err;
2360Sstevel@tonic-gate 	ret->top=al;
237*5004Sjp161948 	ret->neg=r->neg;
238*5004Sjp161948 
239*5004Sjp161948 	rp=ret->d;
240*5004Sjp161948 	ap=&(r->d[ri]);
2410Sstevel@tonic-gate 	al-=4;
2420Sstevel@tonic-gate 	for (i=0; i<al; i+=4)
2430Sstevel@tonic-gate 		{
2440Sstevel@tonic-gate 		BN_ULONG t1,t2,t3,t4;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		t1=ap[i+0];
2470Sstevel@tonic-gate 		t2=ap[i+1];
2480Sstevel@tonic-gate 		t3=ap[i+2];
2490Sstevel@tonic-gate 		t4=ap[i+3];
2500Sstevel@tonic-gate 		rp[i+0]=t1;
2510Sstevel@tonic-gate 		rp[i+1]=t2;
2520Sstevel@tonic-gate 		rp[i+2]=t3;
2530Sstevel@tonic-gate 		rp[i+3]=t4;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	al+=4;
2560Sstevel@tonic-gate 	for (; i<al; i++)
2570Sstevel@tonic-gate 		rp[i]=ap[i];
258*5004Sjp161948 # endif
2590Sstevel@tonic-gate #else /* !MONT_WORD */
2600Sstevel@tonic-gate 	BIGNUM *t1,*t2;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	BN_CTX_start(ctx);
2630Sstevel@tonic-gate 	t1 = BN_CTX_get(ctx);
2640Sstevel@tonic-gate 	t2 = BN_CTX_get(ctx);
2650Sstevel@tonic-gate 	if (t1 == NULL || t2 == NULL) goto err;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (!BN_copy(t1,a)) goto err;
2680Sstevel@tonic-gate 	BN_mask_bits(t1,mont->ri);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (!BN_mul(t2,t1,&mont->Ni,ctx)) goto err;
2710Sstevel@tonic-gate 	BN_mask_bits(t2,mont->ri);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (!BN_mul(t1,t2,&mont->N,ctx)) goto err;
2740Sstevel@tonic-gate 	if (!BN_add(t2,a,t1)) goto err;
2750Sstevel@tonic-gate 	if (!BN_rshift(ret,t2,mont->ri)) goto err;
2760Sstevel@tonic-gate #endif /* MONT_WORD */
2770Sstevel@tonic-gate 
278*5004Sjp161948 #if !defined(BRANCH_FREE) || BRANCH_FREE==0
2790Sstevel@tonic-gate 	if (BN_ucmp(ret, &(mont->N)) >= 0)
2800Sstevel@tonic-gate 		{
2810Sstevel@tonic-gate 		if (!BN_usub(ret,ret,&(mont->N))) goto err;
2820Sstevel@tonic-gate 		}
283*5004Sjp161948 #endif
2840Sstevel@tonic-gate 	retn=1;
2852139Sjp161948 	bn_check_top(ret);
2860Sstevel@tonic-gate  err:
2870Sstevel@tonic-gate 	BN_CTX_end(ctx);
2880Sstevel@tonic-gate 	return(retn);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
BN_MONT_CTX_new(void)2910Sstevel@tonic-gate BN_MONT_CTX *BN_MONT_CTX_new(void)
2920Sstevel@tonic-gate 	{
2930Sstevel@tonic-gate 	BN_MONT_CTX *ret;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if ((ret=(BN_MONT_CTX *)OPENSSL_malloc(sizeof(BN_MONT_CTX))) == NULL)
2960Sstevel@tonic-gate 		return(NULL);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	BN_MONT_CTX_init(ret);
2990Sstevel@tonic-gate 	ret->flags=BN_FLG_MALLOCED;
3000Sstevel@tonic-gate 	return(ret);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
BN_MONT_CTX_init(BN_MONT_CTX * ctx)3030Sstevel@tonic-gate void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
3040Sstevel@tonic-gate 	{
3050Sstevel@tonic-gate 	ctx->ri=0;
3060Sstevel@tonic-gate 	BN_init(&(ctx->RR));
3070Sstevel@tonic-gate 	BN_init(&(ctx->N));
3080Sstevel@tonic-gate 	BN_init(&(ctx->Ni));
3090Sstevel@tonic-gate 	ctx->flags=0;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
BN_MONT_CTX_free(BN_MONT_CTX * mont)3120Sstevel@tonic-gate void BN_MONT_CTX_free(BN_MONT_CTX *mont)
3130Sstevel@tonic-gate 	{
3140Sstevel@tonic-gate 	if(mont == NULL)
3150Sstevel@tonic-gate 	    return;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	BN_free(&(mont->RR));
3180Sstevel@tonic-gate 	BN_free(&(mont->N));
3190Sstevel@tonic-gate 	BN_free(&(mont->Ni));
3200Sstevel@tonic-gate 	if (mont->flags & BN_FLG_MALLOCED)
3210Sstevel@tonic-gate 		OPENSSL_free(mont);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
BN_MONT_CTX_set(BN_MONT_CTX * mont,const BIGNUM * mod,BN_CTX * ctx)3240Sstevel@tonic-gate int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
3250Sstevel@tonic-gate 	{
3262139Sjp161948 	int ret = 0;
3272139Sjp161948 	BIGNUM *Ri,*R;
3280Sstevel@tonic-gate 
3292139Sjp161948 	BN_CTX_start(ctx);
3302139Sjp161948 	if((Ri = BN_CTX_get(ctx)) == NULL) goto err;
3310Sstevel@tonic-gate 	R= &(mont->RR);					/* grab RR as a temp */
3322139Sjp161948 	if (!BN_copy(&(mont->N),mod)) goto err;		/* Set N */
3330Sstevel@tonic-gate 	mont->N.neg = 0;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate #ifdef MONT_WORD
3360Sstevel@tonic-gate 		{
3370Sstevel@tonic-gate 		BIGNUM tmod;
3380Sstevel@tonic-gate 		BN_ULONG buf[2];
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		mont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;
3412139Sjp161948 		BN_zero(R);
3420Sstevel@tonic-gate 		if (!(BN_set_bit(R,BN_BITS2))) goto err;	/* R */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		buf[0]=mod->d[0]; /* tmod = N mod word size */
3450Sstevel@tonic-gate 		buf[1]=0;
3460Sstevel@tonic-gate 		tmod.d=buf;
3472139Sjp161948 		tmod.top = buf[0] != 0 ? 1 : 0;
3480Sstevel@tonic-gate 		tmod.dmax=2;
3490Sstevel@tonic-gate 		tmod.neg=0;
3500Sstevel@tonic-gate 							/* Ri = R^-1 mod N*/
3512139Sjp161948 		if ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)
3520Sstevel@tonic-gate 			goto err;
3532139Sjp161948 		if (!BN_lshift(Ri,Ri,BN_BITS2)) goto err; /* R*Ri */
3542139Sjp161948 		if (!BN_is_zero(Ri))
3550Sstevel@tonic-gate 			{
3562139Sjp161948 			if (!BN_sub_word(Ri,1)) goto err;
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 		else /* if N mod word size == 1 */
3590Sstevel@tonic-gate 			{
3602139Sjp161948 			if (!BN_set_word(Ri,BN_MASK2)) goto err;  /* Ri-- (mod word size) */
3610Sstevel@tonic-gate 			}
3622139Sjp161948 		if (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;
3630Sstevel@tonic-gate 		/* Ni = (R*Ri-1)/N,
3640Sstevel@tonic-gate 		 * keep only least significant word: */
3652139Sjp161948 		mont->n0 = (Ri->top > 0) ? Ri->d[0] : 0;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate #else /* !MONT_WORD */
3680Sstevel@tonic-gate 		{ /* bignum version */
3690Sstevel@tonic-gate 		mont->ri=BN_num_bits(&mont->N);
3702139Sjp161948 		BN_zero(R);
3710Sstevel@tonic-gate 		if (!BN_set_bit(R,mont->ri)) goto err;  /* R = 2^ri */
3720Sstevel@tonic-gate 		                                        /* Ri = R^-1 mod N*/
3732139Sjp161948 		if ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)
3740Sstevel@tonic-gate 			goto err;
3752139Sjp161948 		if (!BN_lshift(Ri,Ri,mont->ri)) goto err; /* R*Ri */
3762139Sjp161948 		if (!BN_sub_word(Ri,1)) goto err;
3770Sstevel@tonic-gate 							/* Ni = (R*Ri-1) / N */
3782139Sjp161948 		if (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate #endif
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/* setup RR for conversions */
3832139Sjp161948 	BN_zero(&(mont->RR));
3840Sstevel@tonic-gate 	if (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;
3850Sstevel@tonic-gate 	if (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;
3860Sstevel@tonic-gate 
3872139Sjp161948 	ret = 1;
3880Sstevel@tonic-gate err:
3892139Sjp161948 	BN_CTX_end(ctx);
3902139Sjp161948 	return ret;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
BN_MONT_CTX_copy(BN_MONT_CTX * to,BN_MONT_CTX * from)3930Sstevel@tonic-gate BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
3940Sstevel@tonic-gate 	{
3950Sstevel@tonic-gate 	if (to == from) return(to);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (!BN_copy(&(to->RR),&(from->RR))) return NULL;
3980Sstevel@tonic-gate 	if (!BN_copy(&(to->N),&(from->N))) return NULL;
3990Sstevel@tonic-gate 	if (!BN_copy(&(to->Ni),&(from->Ni))) return NULL;
4000Sstevel@tonic-gate 	to->ri=from->ri;
4010Sstevel@tonic-gate 	to->n0=from->n0;
4020Sstevel@tonic-gate 	return(to);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
BN_MONT_CTX_set_locked(BN_MONT_CTX ** pmont,int lock,const BIGNUM * mod,BN_CTX * ctx)4052139Sjp161948 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
4062139Sjp161948 					const BIGNUM *mod, BN_CTX *ctx)
4072139Sjp161948 	{
4082139Sjp161948 	if (*pmont)
4092139Sjp161948 		return *pmont;
4102139Sjp161948 	CRYPTO_w_lock(lock);
4112139Sjp161948 	if (!*pmont)
4122139Sjp161948 		{
4132139Sjp161948 		*pmont = BN_MONT_CTX_new();
4142139Sjp161948 		if (*pmont && !BN_MONT_CTX_set(*pmont, mod, ctx))
4152139Sjp161948 			{
4162139Sjp161948 			BN_MONT_CTX_free(*pmont);
4172139Sjp161948 			*pmont = NULL;
4182139Sjp161948 			}
4192139Sjp161948 		}
4202139Sjp161948 	CRYPTO_w_unlock(lock);
4212139Sjp161948 	return *pmont;
4222139Sjp161948 	}
423