62 #if defined( _WIN32_WCE ) && _WIN32_WCE < 400
69 #if defined( INC_ALL )
75 #ifdef USE_BN_DEBUG_MALLOC
77 void *clBnAllocFn(
const char *
fileName,
const char *fnName,
78 const int lineNo,
size_t size )
80 printf(
"BNDEBUG: %s:%s:%d %d bytes.\n", fileName, fnName, lineNo, size );
81 return( malloc( size ) );
88 #ifndef OPENSSL_NO_DEPRECATED
98 static int bn_limit_bits=0;
99 static int bn_limit_num=8;
100 static int bn_limit_bits_low=0;
101 static int bn_limit_num_low=8;
102 static int bn_limit_bits_high=0;
103 static int bn_limit_num_high=8;
104 static int bn_limit_bits_mont=0;
105 static int bn_limit_num_mont=8;
111 if (mult > (
int)(
sizeof(
int)*8)-1)
112 mult=
sizeof(int)*8-1;
114 bn_limit_num=1<<mult;
118 if (high > (
int)(
sizeof(int)*8)-1)
119 high=
sizeof(
int)*8-1;
120 bn_limit_bits_high=high;
121 bn_limit_num_high=1<<high;
125 if (low > (
int)(
sizeof(int)*8)-1)
127 bn_limit_bits_low=low;
128 bn_limit_num_low=1<<low;
132 if (mont > (
int)(
sizeof(int)*8)-1)
133 mont=
sizeof(
int)*8-1;
134 bn_limit_bits_mont=mont;
135 bn_limit_num_mont=1<<mont;
141 if (which == 0)
return(bn_limit_bits);
142 else if (which == 1)
return(bn_limit_bits_high);
143 else if (which == 2)
return(bn_limit_bits_low);
144 else if (which == 3)
return(bn_limit_bits_mont);
160 static char data[16];
167 BIO_snprintf(data,
sizeof data,
"bn(%d,%d)",
168 (
int)
sizeof(BN_ULLONG)*8,(
int)
sizeof(
BN_ULONG)*8);
170 BIO_snprintf(data,
sizeof data,
"bn(%d,%d)",
180 static const char bits[256]={
181 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
182 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
183 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
184 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
185 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
186 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
187 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
188 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
189 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
190 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
191 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
192 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
193 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
194 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
195 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
196 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
199 #if defined(SIXTY_FOUR_BIT_LONG)
200 if (l & 0xffffffff00000000L)
202 if (l & 0xffff000000000000L)
204 if (l & 0xff00000000000000L)
206 return(bits[(
int)(l>>56)]+56);
208 else return(bits[(
int)(l>>48)]+48);
212 if (l & 0x0000ff0000000000L)
214 return(bits[(
int)(l>>40)]+40);
216 else return(bits[(
int)(l>>32)]+32);
221 #ifdef SIXTY_FOUR_BIT
222 if (l & 0xffffffff00000000LL)
224 if (l & 0xffff000000000000LL)
226 if (l & 0xff00000000000000LL)
228 return(bits[(
int)(l>>56)]+56);
230 else return(bits[(
int)(l>>48)]+48);
234 if (l & 0x0000ff0000000000LL)
236 return(bits[(
int)(l>>40)]+40);
238 else return(bits[(
int)(l>>32)]+32);
245 #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
249 return(bits[(
int)(l>>24L)]+24);
250 else return(bits[(
int)(l>>16L)]+16);
255 #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
257 return(bits[(
int)(l>>8)]+8);
260 return(bits[(
int)(l )] );
278 if (a == NULL)
return;
294 if (a == NULL)
return;
302 #ifndef OPENSSL_NO_DEPRECATED
311 memset(a,0,
sizeof(
BIGNUM));
324 memset( ret, 0,
sizeof(
BIGNUM ) );
365 for (i=b->
top>>2; i>0; i--,A+=4,B+=4)
377 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
378 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
394 memcpy(A,b->
d,
sizeof(b->
d[0])*b->
top);
411 #ifndef OPENSSL_NO_DEPRECATED
426 BN_ULONG *a = bn_expand_internal(b, words);
469 BN_ULONG *a = bn_expand_internal(b, words);
483 for (i=(b->
dmax - b->
top)>>3; i>0; i--,A+=8)
485 A[0]=0; A[1]=0; A[2]=0; A[3]=0;
486 A[4]=0; A[5]=0; A[6]=0; A[7]=0;
488 for (i=(b->
dmax - b->
top)&7; i>0; i--,A++)
490 assert(A == &(b->
d[b->
dmax]));
501 if (a == NULL)
return NULL;
505 if (t == NULL)
return NULL;
523 if (a == b)
return(a);
529 for (i=b->
top>>2; i>0; i--,A+=4,B+=4)
532 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
533 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
543 memcpy(a->
d,b->
d,
sizeof(b->
d[0])*b->
top);
554 int flags_old_a, flags_old_b;
556 int tmp_top, tmp_dmax, tmp_neg;
561 flags_old_a = a->
flags;
562 flags_old_b = b->
flags;
589 memset(a->
d,0,a->
dmax*
sizeof(a->
d[0]));
598 else if (a->
top == 1)
610 a->
top = (w ? 1 : 0);
624 if (ret == NULL)
return(NULL);
669 *(to++)=(
unsigned char)((l>>(8*(i%
BN_BYTES)))&0xff);
683 if (i != 0)
return(i);
686 for (i=a->
top-1; i>=0; i--)
691 return((t1 > t2) ? 1 : -1);
702 if ((a == NULL) || (b == NULL))
723 else { gt= -1; lt=1; }
725 if (a->
top > b->
top)
return(gt);
726 if (a->
top < b->
top)
return(lt);
727 for (i=a->
top-1; i>=0; i--)
731 if (t1 > t2)
return(gt);
732 if (t1 < t2)
return(lt);
749 for(k=a->
top; k<i+1; k++)
768 if (a->
top <= i)
return(0);
783 if (a->
top <= i)
return 0;
784 return((a->
d[i]&(((
BN_ULONG)1)<<j))?1:0);
796 if (w >= a->
top)
return 0;
823 if (aa != bb)
return((aa > bb)?1:-1);
824 for (i=n-2; i>=0; i--)
828 if (aa != bb)
return((aa > bb)?1:-1);
874 return((l>>(8*(i%
BN_BYTES)))&0x80?1:0);
882 for (i=a->
top-1; i>=0; i--)