120 # define alloca _alloca
122 #elif defined(__GNUC__)
124 # define alloca(s) __builtin_alloca((s))
129 #define TABLE_SIZE 32
145 if ((r == a) || (r == p))
150 if (rr == NULL || v == NULL)
goto err;
152 if (
BN_copy(v,a) == NULL)
goto err;
156 {
if (
BN_copy(rr,a) == NULL)
goto err; }
157 else {
if (!
BN_one(rr))
goto err; }
159 for (i=1; i<
bits; i++)
161 if (!
BN_sqr(v,v,ctx))
goto err;
164 if (!
BN_mul(rr,rr,v,ctx))
goto err;
216 #define MONT_EXP_WORD
228 # ifdef MONT_EXP_WORD
231 BN_ULONG A = a->
d[0];
254 int i,j,
bits,ret=0,wstart,wend,window,wvalue;
279 if(!aa || !val[0])
goto err;
294 if (!
BN_nnmod(val[0],a,m,ctx))
goto err;
333 if (wstart == 0)
break;
344 for (i=1; i<window; i++)
346 if (wstart-i < 0)
break;
373 if (wstart < 0)
break;
387 int i,j,
bits,ret=0,wstart,wend,window,wvalue;
420 if (!d || !r || !val[0])
goto err;
480 if (wstart == 0)
break;
491 for (i=1; i<window; i++)
493 if (wstart-i < 0)
break;
520 if (wstart < 0)
break;
537 static int MOD_EXP_CTIME_COPY_TO_PREBUF(
const BIGNUM *
b,
int top,
unsigned char *buf,
int idx,
int width)
543 for (i = 0, j=idx; i < top *
sizeof b->
d[0]; i++, j+=width)
545 buf[j] = ((
unsigned char*)b->
d)[i];
551 static int MOD_EXP_CTIME_COPY_FROM_PREBUF(
BIGNUM *b,
int top,
unsigned char *buf,
int idx,
int width)
558 for (i=0, j=idx; i < top *
sizeof b->
d[0]; i++, j+=width)
560 ((
unsigned char*)b->
d)[i] = buf[j];
569 #define MOD_EXP_CTIME_ALIGN(x_) \
570 ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
581 int i,
bits,ret=0,window,wvalue;
586 unsigned char *powerbufFree=NULL;
588 unsigned char *powerbuf=NULL;
624 #if defined(OPENSSL_BN_ASM_MONT5)
625 if (window==6 && bits<=1024) window=5;
631 numPowers = 1 << window;
632 powerbufLen =
sizeof(m->
d[0])*(top*numPowers +
633 ((2*top)>numPowers?(2*top):numPowers));
635 if (powerbufLen < 3072)
643 memset(powerbuf, 0, powerbufLen);
646 if (powerbufLen < 3072)
651 tmp.
d = (BN_ULONG *)(powerbuf +
sizeof(m->
d[0])*top*numPowers);
662 tmp.
d[0] = (0-m->
d[0])&BN_MASK2;
664 tmp.
d[i] = (~m->
d[i])&BN_MASK2;
671 if (!
BN_mod(&am,a,m,ctx))
goto err;
676 #if defined(OPENSSL_BN_ASM_MONT5)
685 void bn_mul_mont_gather5(BN_ULONG *rp,
const BN_ULONG *ap,
686 const void *table,
const BN_ULONG *np,
687 const BN_ULONG *n0,
int num,
int power);
688 void bn_scatter5(
const BN_ULONG *inp,
size_t num,
689 void *table,
size_t power);
690 void bn_gather5(BN_ULONG *
out,
size_t num,
691 void *table,
size_t power);
693 BN_ULONG *np=mont->
N.
d, *n0=mont->
n0;
697 for (i=am.
top; i<top; i++) am.
d[i]=0;
698 for (i=tmp.
top; i<top; i++) tmp.
d[i]=0;
700 bn_scatter5(tmp.
d,top,powerbuf,0);
701 bn_scatter5(am.
d,am.
top,powerbuf,1);
703 bn_scatter5(tmp.
d,top,powerbuf,2);
709 bn_mul_mont_gather5(tmp.
d,am.
d,powerbuf,np,n0,top,i-1);
710 bn_scatter5(tmp.
d,top,powerbuf,i);
714 for (i=4; i<32; i*=2)
717 bn_scatter5(tmp.
d,top,powerbuf,i);
722 bn_mul_mont_gather5(tmp.
d,am.
d,powerbuf,np,n0,top,i-1);
723 bn_scatter5(tmp.
d,top,powerbuf,i);
724 for (j=2*i; j<32; j*=2)
727 bn_scatter5(tmp.
d,top,powerbuf,j);
732 bn_mul_mont_gather5(tmp.
d,am.
d,powerbuf,np,n0,top,i-1);
733 bn_scatter5(tmp.
d,top,powerbuf,i);
735 bn_scatter5(tmp.
d,top,powerbuf,2*i);
739 bn_mul_mont_gather5(tmp.
d,am.
d,powerbuf,np,n0,top,i-1);
740 bn_scatter5(tmp.
d,top,powerbuf,i);
744 for (wvalue=0, i=bits%5; i>=0; i--,bits--)
746 bn_gather5(tmp.
d,top,powerbuf,wvalue);
753 for (wvalue=0, i=0; i<5; i++,bits--)
761 bn_mul_mont_gather5(tmp.
d,tmp.
d,powerbuf,np,n0,top,wvalue);
770 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, numPowers))
goto err;
771 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, numPowers))
goto err;
781 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2, numPowers))
goto err;
782 for (i=3; i<numPowers; i++)
787 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i, numPowers))
goto err;
792 for (wvalue=0, i=bits%window; i>=0; i--,bits--)
794 if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp,top,powerbuf,wvalue,numPowers))
goto err;
804 for (i=0; i<window; i++,bits--)
811 if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue, numPowers))
goto err;
841 #define BN_MOD_MUL_WORD(r, w, m) \
842 (BN_mul_word(r, (w)) && \
844 (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
853 #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
854 (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
891 if (d == NULL || r == NULL || t == NULL)
goto err;
907 for (b = bits-2; b >= 0; b--)
967 if (!
BN_one(rr))
goto err;
986 int i,j,
bits,ret=0,wstart,wend,window,wvalue;
1010 if(!d || !val[0])
goto err;
1012 if (!
BN_nnmod(val[0],a,m,ctx))
goto err;
1041 if (!
BN_one(r))
goto err;
1050 if (wstart == 0)
break;
1061 for (i=1; i<window; i++)
1063 if (wstart-i < 0)
break;
1090 if (wstart < 0)
break;