73 #ifndef OPENSSL_NO_DEPRECATED
83 static int bn_limit_bits=0;
84 static int bn_limit_num=8;
85 static int bn_limit_bits_low=0;
86 static int bn_limit_num_low=8;
87 static int bn_limit_bits_high=0;
88 static int bn_limit_num_high=8;
89 static int bn_limit_bits_mont=0;
90 static int bn_limit_num_mont=8;
96 if (mult > (
int)(
sizeof(
int)*8)-1)
103 if (high > (
int)(
sizeof(int)*8)-1)
104 high=
sizeof(
int)*8-1;
105 bn_limit_bits_high=high;
106 bn_limit_num_high=1<<high;
110 if (low > (
int)(
sizeof(int)*8)-1)
112 bn_limit_bits_low=low;
113 bn_limit_num_low=1<<low;
117 if (mont > (
int)(
sizeof(int)*8)-1)
118 mont=
sizeof(
int)*8-1;
119 bn_limit_bits_mont=mont;
120 bn_limit_num_mont=1<<mont;
126 if (which == 0)
return(bn_limit_bits);
127 else if (which == 1)
return(bn_limit_bits_high);
128 else if (which == 2)
return(bn_limit_bits_low);
129 else if (which == 3)
return(bn_limit_bits_mont);
136 static const BN_ULONG data_one=1L;
144 static const unsigned char bits[256]={
145 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
146 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
147 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
148 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
149 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
150 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
151 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
152 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
153 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
154 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
155 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
156 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
157 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
158 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
159 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
160 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
163 #if defined(SIXTY_FOUR_BIT_LONG)
164 if (l & 0xffffffff00000000L)
166 if (l & 0xffff000000000000L)
168 if (l & 0xff00000000000000L)
170 return(bits[(
int)(l>>56)]+56);
172 else return(bits[(
int)(l>>48)]+48);
176 if (l & 0x0000ff0000000000L)
178 return(bits[(
int)(l>>40)]+40);
180 else return(bits[(
int)(l>>32)]+32);
185 #ifdef SIXTY_FOUR_BIT
186 if (l & 0xffffffff00000000LL)
188 if (l & 0xffff000000000000LL)
190 if (l & 0xff00000000000000LL)
192 return(bits[(
int)(l>>56)]+56);
194 else return(bits[(
int)(l>>48)]+48);
198 if (l & 0x0000ff0000000000LL)
200 return(bits[(
int)(l>>40)]+40);
202 else return(bits[(
int)(l>>32)]+32);
209 #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
213 return(bits[(
int)(l>>24L)]+24);
214 else return(bits[(
int)(l>>16L)]+16);
219 #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
221 return(bits[(
int)(l>>8)]+8);
224 return(bits[(
int)(l )] );
242 if (a == NULL)
return;
258 if (a == NULL)
return;
266 #ifndef OPENSSL_NO_DEPRECATED
275 memset(a,0,
sizeof(
BIGNUM));
299 static BN_ULONG *bn_expand_internal(
const BIGNUM *
b,
int words)
301 BN_ULONG *A,*
a = NULL;
307 if (words > (INT_MAX/(4*BN_BITS2)))
328 for (i=b->
top>>2; i>0; i--,A+=4,B+=4)
339 BN_ULONG a0,a1,a2,a3;
340 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
341 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
356 memset(A,0,
sizeof(BN_ULONG)*words);
357 memcpy(A,b->
d,
sizeof(b->
d[0])*b->
top);
374 #ifndef OPENSSL_NO_DEPRECATED
389 BN_ULONG *a = bn_expand_internal(b, words);
432 BN_ULONG *a = bn_expand_internal(b, words);
445 BN_ULONG *A = &(b->
d[b->
top]);
446 for (i=(b->
dmax - b->
top)>>3; i>0; i--,A+=8)
448 A[0]=0; A[1]=0; A[2]=0; A[3]=0;
449 A[4]=0; A[5]=0; A[6]=0; A[7]=0;
451 for (i=(b->
dmax - b->
top)&7; i>0; i--,A++)
453 assert(A == &(b->
d[b->
dmax]));
464 if (a == NULL)
return NULL;
468 if (t == NULL)
return NULL;
486 if (a == b)
return(a);
492 for (i=b->
top>>2; i>0; i--,A+=4,B+=4)
494 BN_ULONG a0,a1,a2,a3;
495 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
496 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
506 memcpy(a->
d,b->
d,
sizeof(b->
d[0])*b->
top);
517 int flags_old_a, flags_old_b;
519 int tmp_top, tmp_dmax, tmp_neg;
524 flags_old_a = a->
flags;
525 flags_old_b = b->
flags;
552 memset(a->
d,0,a->
dmax*
sizeof(a->
d[0]));
561 else if (a->
top == 1)
570 if (
bn_expand(a,(
int)
sizeof(BN_ULONG)*8) == NULL)
return(0);
573 a->
top = (w ? 1 : 0);
587 if (ret == NULL)
return(NULL);
596 i=((n-1)/BN_BYTES)+1;
597 m=((n-1)%(BN_BYTES));
632 *(to++)=(
unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
640 BN_ULONG t1,t2,*ap,*
bp;
646 if (i != 0)
return(i);
649 for (i=a->
top-1; i>=0; i--)
654 return((t1 > t2) ? 1 : -1);
665 if ((a == NULL) || (b == NULL))
686 else { gt= -1; lt=1; }
688 if (a->
top > b->
top)
return(gt);
689 if (a->
top < b->
top)
return(lt);
690 for (i=a->
top-1; i>=0; i--)
694 if (t1 > t2)
return(gt);
695 if (t1 < t2)
return(lt);
712 for(k=a->
top; k<i+1; k++)
717 a->
d[i]|=(((BN_ULONG)1)<<j);
731 if (a->
top <= i)
return(0);
733 a->
d[i]&=(~(((BN_ULONG)1)<<j));
746 if (a->
top <= i)
return 0;
747 return (
int)(((a->
d[i])>>j)&((BN_ULONG)1));
759 if (w >= a->
top)
return 0;
765 a->
d[w]&= ~(BN_MASK2<<
b);
786 if (aa != bb)
return((aa > bb)?1:-1);
787 for (i=n-2; i>=0; i--)
791 if (aa != bb)
return((aa > bb)?1:-1);