112 #if defined( INC_ALL )
117 #if defined( _MSC_VER )
118 #pragma warning( disable: 4311 )
122 #define TABLE_SIZE 32
138 if ((r == a) || (r == p))
141 if( rr == NULL )
goto err;
147 if (
BN_copy(v,a) == NULL)
goto err;
151 {
if (
BN_copy(rr,a) == NULL)
goto err; }
152 else {
if (!
BN_one(rr))
goto err; }
154 for (i=1; i<bits; i++)
156 if (!
BN_sqr(v,v,ctx))
goto err;
159 if (!
BN_mul(rr,rr,v,ctx))
goto err;
211 #define MONT_EXP_WORD
223 # ifdef MONT_EXP_WORD
249 int i,j,bits,ret=0,wstart,wend,window,wvalue;
274 if(!aa || !val[0])
goto err;
289 if (!
BN_nnmod(val[0],a,m,ctx))
goto err;
328 if (wstart == 0)
break;
339 for (i=1; i<window; i++)
341 if (wstart-i < 0)
break;
368 if (wstart < 0)
break;
382 int i,j,bits,ret=0,wstart,wend,window,wvalue;
415 if (!d || !r || !val[0])
goto err;
475 if (wstart == 0)
break;
486 for (i=1; i<window; i++)
488 if (wstart-i < 0)
break;
515 if (wstart < 0)
break;
532 static int MOD_EXP_CTIME_COPY_TO_PREBUF(
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];
552 static int MOD_EXP_CTIME_COPY_FROM_PREBUF(
BIGNUM *b,
int top,
unsigned char *buf,
int idx,
int width)
559 for (i=0, j=idx; i < top *
sizeof b->
d[0]; i++, j+=width)
561 ((
unsigned char*)b->
d)[i] = buf[j];
570 #define MOD_EXP_CTIME_ALIGN(x_) \
571 ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((BN_ULONG)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
582 int i,bits,ret=0,
idx,window,wvalue;
589 unsigned char *powerbufFree=NULL;
591 unsigned char *powerbuf=NULL;
592 BIGNUM *computeTemp=NULL, *am=NULL;
615 if (r == NULL)
goto err;
634 numPowers = 1 << window;
635 powerbufLen =
sizeof(m->
d[0])*top*numPowers;
636 if ((powerbufFree=(
unsigned char*)
clBnAlloc(
"BN_mod_exp_mont_consttime",
641 memset(powerbuf, 0, powerbufLen);
647 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers))
goto err;
652 if (computeTemp==NULL || am==NULL)
goto err;
663 if (!
BN_copy(computeTemp, am))
goto err;
664 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers))
goto err;
673 for (i=2; i<numPowers; i++)
678 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers))
goto err;
693 bits = ((bits+window-1)/window)*window;
704 for (i=0; i<window; i++,idx--)
711 if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers))
goto err;
728 if (computeTemp!=NULL)
BN_clear(computeTemp);
742 #define BN_MOD_MUL_WORD(r, w, m) \
743 (BN_mul_word(r, (w)) && \
745 (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
754 #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
755 (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
792 if (d == NULL || r == NULL || t == NULL)
goto err;
808 for (b = bits-2; b >= 0; b--)
868 if (!
BN_one(rr))
goto err;
887 int i,j,bits,ret=0,wstart,wend,window,wvalue;
911 if(!d || !val[0])
goto err;
913 if (!
BN_nnmod(val[0],a,m,ctx))
goto err;
951 if (wstart == 0)
break;
962 for (i=1; i<window; i++)
964 if (wstart-i < 0)
break;
991 if (wstart < 0)
break;