summaryrefslogtreecommitdiff
path: root/src/lib/libssl/src/crypto/bn/bn_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/src/crypto/bn/bn_lib.c')
-rw-r--r--src/lib/libssl/src/crypto/bn/bn_lib.c492
1 files changed, 334 insertions, 158 deletions
diff --git a/src/lib/libssl/src/crypto/bn/bn_lib.c b/src/lib/libssl/src/crypto/bn/bn_lib.c
index bfe7628ad4..5d62d88e8b 100644
--- a/src/lib/libssl/src/crypto/bn/bn_lib.c
+++ b/src/lib/libssl/src/crypto/bn/bn_lib.c
@@ -60,9 +60,68 @@
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "bn_lcl.h" 61#include "bn_lcl.h"
62 62
63char *BN_version="Big Number part of SSLeay 0.9.0b 29-Jun-1998"; 63const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT;
64
65/* For a 32 bit machine
66 * 2 - 4 == 128
67 * 3 - 8 == 256
68 * 4 - 16 == 512
69 * 5 - 32 == 1024
70 * 6 - 64 == 2048
71 * 7 - 128 == 4096
72 * 8 - 256 == 8192
73 */
74OPENSSL_GLOBAL int bn_limit_bits=0;
75OPENSSL_GLOBAL int bn_limit_num=8; /* (1<<bn_limit_bits) */
76OPENSSL_GLOBAL int bn_limit_bits_low=0;
77OPENSSL_GLOBAL int bn_limit_num_low=8; /* (1<<bn_limit_bits_low) */
78OPENSSL_GLOBAL int bn_limit_bits_high=0;
79OPENSSL_GLOBAL int bn_limit_num_high=8; /* (1<<bn_limit_bits_high) */
80OPENSSL_GLOBAL int bn_limit_bits_mont=0;
81OPENSSL_GLOBAL int bn_limit_num_mont=8; /* (1<<bn_limit_bits_mont) */
82
83void BN_set_params(int mult, int high, int low, int mont)
84 {
85 if (mult >= 0)
86 {
87 if (mult > (sizeof(int)*8)-1)
88 mult=sizeof(int)*8-1;
89 bn_limit_bits=mult;
90 bn_limit_num=1<<mult;
91 }
92 if (high >= 0)
93 {
94 if (high > (sizeof(int)*8)-1)
95 high=sizeof(int)*8-1;
96 bn_limit_bits_high=high;
97 bn_limit_num_high=1<<high;
98 }
99 if (low >= 0)
100 {
101 if (low > (sizeof(int)*8)-1)
102 low=sizeof(int)*8-1;
103 bn_limit_bits_low=low;
104 bn_limit_num_low=1<<low;
105 }
106 if (mont >= 0)
107 {
108 if (mont > (sizeof(int)*8)-1)
109 mont=sizeof(int)*8-1;
110 bn_limit_bits_mont=mont;
111 bn_limit_num_mont=1<<mont;
112 }
113 }
64 114
65BIGNUM *BN_value_one() 115int BN_get_params(int which)
116 {
117 if (which == 0) return(bn_limit_bits);
118 else if (which == 1) return(bn_limit_bits_high);
119 else if (which == 2) return(bn_limit_bits_low);
120 else if (which == 3) return(bn_limit_bits_mont);
121 else return(0);
122 }
123
124BIGNUM *BN_value_one(void)
66 { 125 {
67 static BN_ULONG data_one=1L; 126 static BN_ULONG data_one=1L;
68 static BIGNUM const_one={&data_one,1,1,0}; 127 static BIGNUM const_one={&data_one,1,1,0};
@@ -70,7 +129,7 @@ BIGNUM *BN_value_one()
70 return(&const_one); 129 return(&const_one);
71 } 130 }
72 131
73char *BN_options() 132char *BN_options(void)
74 { 133 {
75 static int init=0; 134 static int init=0;
76 static char data[16]; 135 static char data[16];
@@ -89,10 +148,9 @@ char *BN_options()
89 return(data); 148 return(data);
90 } 149 }
91 150
92int BN_num_bits_word(l) 151int BN_num_bits_word(BN_ULONG l)
93BN_ULONG l;
94 { 152 {
95 static char bits[256]={ 153 static const char bits[256]={
96 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, 154 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
97 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 155 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
98 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 156 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
@@ -111,24 +169,24 @@ BN_ULONG l;
111 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 169 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
112 }; 170 };
113 171
114#ifdef SIXTY_FOUR_BIT_LONG 172#if defined(SIXTY_FOUR_BIT_LONG)
115 if (l & 0xffffffff00000000L) 173 if (l & 0xffffffff00000000L)
116 { 174 {
117 if (l & 0xffff000000000000L) 175 if (l & 0xffff000000000000L)
118 { 176 {
119 if (l & 0xff00000000000000L) 177 if (l & 0xff00000000000000L)
120 { 178 {
121 return(bits[l>>56]+56); 179 return(bits[(int)(l>>56)]+56);
122 } 180 }
123 else return(bits[l>>48]+48); 181 else return(bits[(int)(l>>48)]+48);
124 } 182 }
125 else 183 else
126 { 184 {
127 if (l & 0x0000ff0000000000L) 185 if (l & 0x0000ff0000000000L)
128 { 186 {
129 return(bits[l>>40]+40); 187 return(bits[(int)(l>>40)]+40);
130 } 188 }
131 else return(bits[l>>32]+32); 189 else return(bits[(int)(l>>32)]+32);
132 } 190 }
133 } 191 }
134 else 192 else
@@ -140,17 +198,17 @@ BN_ULONG l;
140 { 198 {
141 if (l & 0xff00000000000000LL) 199 if (l & 0xff00000000000000LL)
142 { 200 {
143 return(bits[l>>56]+56); 201 return(bits[(int)(l>>56)]+56);
144 } 202 }
145 else return(bits[l>>48]+48); 203 else return(bits[(int)(l>>48)]+48);
146 } 204 }
147 else 205 else
148 { 206 {
149 if (l & 0x0000ff0000000000LL) 207 if (l & 0x0000ff0000000000LL)
150 { 208 {
151 return(bits[l>>40]+40); 209 return(bits[(int)(l>>40)]+40);
152 } 210 }
153 else return(bits[l>>32]+32); 211 else return(bits[(int)(l>>32)]+32);
154 } 212 }
155 } 213 }
156 else 214 else
@@ -161,28 +219,29 @@ BN_ULONG l;
161 if (l & 0xffff0000L) 219 if (l & 0xffff0000L)
162 { 220 {
163 if (l & 0xff000000L) 221 if (l & 0xff000000L)
164 return(bits[l>>24L]+24); 222 return(bits[(int)(l>>24L)]+24);
165 else return(bits[l>>16L]+16); 223 else return(bits[(int)(l>>16L)]+16);
166 } 224 }
167 else 225 else
168#endif 226#endif
169 { 227 {
170#if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) 228#if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
171 if (l & 0xff00L) 229 if (l & 0xff00L)
172 return(bits[l>>8]+8); 230 return(bits[(int)(l>>8)]+8);
173 else 231 else
174#endif 232#endif
175 return(bits[l ] ); 233 return(bits[(int)(l )] );
176 } 234 }
177 } 235 }
178 } 236 }
179 237
180int BN_num_bits(a) 238int BN_num_bits(const BIGNUM *a)
181BIGNUM *a;
182 { 239 {
183 BN_ULONG l; 240 BN_ULONG l;
184 int i; 241 int i;
185 242
243 bn_check_top(a);
244
186 if (a->top == 0) return(0); 245 if (a->top == 0) return(0);
187 l=a->d[a->top-1]; 246 l=a->d[a->top-1];
188 i=(a->top-1)*BN_BITS2; 247 i=(a->top-1)*BN_BITS2;
@@ -196,126 +255,256 @@ BIGNUM *a;
196 return(i+BN_num_bits_word(l)); 255 return(i+BN_num_bits_word(l));
197 } 256 }
198 257
199void BN_clear_free(a) 258void BN_clear_free(BIGNUM *a)
200BIGNUM *a;
201 { 259 {
260 int i;
261
202 if (a == NULL) return; 262 if (a == NULL) return;
203 if (a->d != NULL) 263 if (a->d != NULL)
204 { 264 {
205 memset(a->d,0,a->max*sizeof(a->d[0])); 265 memset(a->d,0,a->max*sizeof(a->d[0]));
206 Free(a->d); 266 if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
267 Free(a->d);
207 } 268 }
269 i=BN_get_flags(a,BN_FLG_MALLOCED);
208 memset(a,0,sizeof(BIGNUM)); 270 memset(a,0,sizeof(BIGNUM));
209 Free(a); 271 if (i)
272 Free(a);
210 } 273 }
211 274
212void BN_free(a) 275void BN_free(BIGNUM *a)
213BIGNUM *a;
214 { 276 {
215 if (a == NULL) return; 277 if (a == NULL) return;
216 if (a->d != NULL) Free(a->d); 278 if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))
217 Free(a); 279 Free(a->d);
280 a->flags|=BN_FLG_FREE; /* REMOVE? */
281 if (a->flags & BN_FLG_MALLOCED)
282 Free(a);
283 }
284
285void BN_init(BIGNUM *a)
286 {
287 memset(a,0,sizeof(BIGNUM));
218 } 288 }
219 289
220BIGNUM *BN_new() 290BIGNUM *BN_new(void)
221 { 291 {
222 BIGNUM *ret; 292 BIGNUM *ret;
223 BN_ULONG *p;
224 293
225 ret=(BIGNUM *)Malloc(sizeof(BIGNUM)); 294 if ((ret=(BIGNUM *)Malloc(sizeof(BIGNUM))) == NULL)
226 if (ret == NULL) goto err; 295 {
296 BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
297 return(NULL);
298 }
299 ret->flags=BN_FLG_MALLOCED;
227 ret->top=0; 300 ret->top=0;
228 ret->neg=0; 301 ret->neg=0;
229 ret->max=(BN_DEFAULT_BITS/BN_BITS2); 302 ret->max=0;
230 p=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(ret->max+1)); 303 ret->d=NULL;
231 if (p == NULL) goto err;
232 ret->d=p;
233
234 memset(p,0,(ret->max+1)*sizeof(p[0]));
235 return(ret); 304 return(ret);
236err:
237 BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);
238 return(NULL);
239 } 305 }
240 306
241BN_CTX *BN_CTX_new() 307
308BN_CTX *BN_CTX_new(void)
242 { 309 {
243 BN_CTX *ret; 310 BN_CTX *ret;
244 BIGNUM *n;
245 int i,j;
246 311
247 ret=(BN_CTX *)Malloc(sizeof(BN_CTX)); 312 ret=(BN_CTX *)Malloc(sizeof(BN_CTX));
248 if (ret == NULL) goto err2; 313 if (ret == NULL)
249
250 for (i=0; i<BN_CTX_NUM; i++)
251 { 314 {
252 n=BN_new(); 315 BNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);
253 if (n == NULL) goto err; 316 return(NULL);
254 ret->bn[i]=n;
255 } 317 }
256 318
257 /* There is actually an extra one, this is for debugging my 319 BN_CTX_init(ret);
258 * stuff */ 320 ret->flags=BN_FLG_MALLOCED;
259 ret->bn[BN_CTX_NUM]=NULL;
260
261 ret->tos=0;
262 return(ret); 321 return(ret);
263err:
264 for (j=0; j<i; j++)
265 BN_free(ret->bn[j]);
266 Free(ret);
267err2:
268 BNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);
269 return(NULL);
270 } 322 }
271 323
272void BN_CTX_free(c) 324void BN_CTX_init(BN_CTX *ctx)
273BN_CTX *c; 325 {
326 memset(ctx,0,sizeof(BN_CTX));
327 ctx->tos=0;
328 ctx->flags=0;
329 }
330
331void BN_CTX_free(BN_CTX *c)
274 { 332 {
275 int i; 333 int i;
276 334
335 if(c == NULL)
336 return;
337
277 for (i=0; i<BN_CTX_NUM; i++) 338 for (i=0; i<BN_CTX_NUM; i++)
278 BN_clear_free(c->bn[i]); 339 BN_clear_free(&(c->bn[i]));
279 Free(c); 340 if (c->flags & BN_FLG_MALLOCED)
341 Free(c);
280 } 342 }
281 343
282BIGNUM *bn_expand2(b, words) 344BIGNUM *bn_expand2(BIGNUM *b, int words)
283BIGNUM *b;
284int words;
285 { 345 {
286 BN_ULONG *p; 346 BN_ULONG *A,*a;
347 const BN_ULONG *B;
348 int i;
349
350 bn_check_top(b);
287 351
288 if (words > b->max) 352 if (words > b->max)
289 { 353 {
290 p=(BN_ULONG *)Realloc(b->d,sizeof(BN_ULONG)*(words+1)); 354 bn_check_top(b);
291 if (p == NULL) 355 if (BN_get_flags(b,BN_FLG_STATIC_DATA))
356 {
357 BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
358 return(NULL);
359 }
360 a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));
361 if (A == NULL)
292 { 362 {
293 BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); 363 BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
294 return(NULL); 364 return(NULL);
295 } 365 }
296 b->d=p; 366#if 1
297 memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG)); 367 B=b->d;
368 /* Check if the previous number needs to be copied */
369 if (B != NULL)
370 {
371#if 0
372 /* This lot is an unrolled loop to copy b->top
373 * BN_ULONGs from B to A
374 */
375/*
376 * I have nothing against unrolling but it's usually done for
377 * several reasons, namely:
378 * - minimize percentage of decision making code, i.e. branches;
379 * - avoid cache trashing;
380 * - make it possible to schedule loads earlier;
381 * Now let's examine the code below. The cornerstone of C is
382 * "programmer is always right" and that's what we love it for:-)
383 * For this very reason C compilers have to be paranoid when it
384 * comes to data aliasing and assume the worst. Yeah, but what
385 * does it mean in real life? This means that loop body below will
386 * be compiled to sequence of loads immediately followed by stores
387 * as compiler assumes the worst, something in A==B+1 style. As a
388 * result CPU pipeline is going to starve for incoming data. Secondly
389 * if A and B happen to share same cache line such code is going to
390 * cause severe cache trashing. Both factors have severe impact on
391 * performance of modern CPUs and this is the reason why this
392 * particulare piece of code is #ifdefed away and replaced by more
393 * "friendly" version found in #else section below. This comment
394 * also applies to BN_copy function.
395 *
396 * <appro@fy.chalmers.se>
397 */
398 for (i=b->top&(~7); i>0; i-=8)
399 {
400 A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
401 A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
402 A+=8;
403 B+=8;
404 }
405 switch (b->top&7)
406 {
407 case 7:
408 A[6]=B[6];
409 case 6:
410 A[5]=B[5];
411 case 5:
412 A[4]=B[4];
413 case 4:
414 A[3]=B[3];
415 case 3:
416 A[2]=B[2];
417 case 2:
418 A[1]=B[1];
419 case 1:
420 A[0]=B[0];
421 case 0:
422 /* I need the 'case 0' entry for utrix cc.
423 * If the optimiser is turned on, it does the
424 * switch table by doing
425 * a=top&7
426 * a--;
427 * goto jump_table[a];
428 * If top is 0, this makes us jump to 0xffffffc
429 * which is rather bad :-(.
430 * eric 23-Apr-1998
431 */
432 ;
433 }
434#else
435 for (i=b->top>>2; i>0; i--,A+=4,B+=4)
436 {
437 /*
438 * The fact that the loop is unrolled
439 * 4-wise is a tribute to Intel. It's
440 * the one that doesn't have enough
441 * registers to accomodate more data.
442 * I'd unroll it 8-wise otherwise:-)
443 *
444 * <appro@fy.chalmers.se>
445 */
446 BN_ULONG a0,a1,a2,a3;
447 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
448 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
449 }
450 switch (b->top&3)
451 {
452 case 3: A[2]=B[2];
453 case 2: A[1]=B[1];
454 case 1: A[0]=B[0];
455 case 0: ; /* ultrix cc workaround, see above */
456 }
457#endif
458 Free(b->d);
459 }
460
461 b->d=a;
298 b->max=words; 462 b->max=words;
463
464 /* Now need to zero any data between b->top and b->max */
465
466 A= &(b->d[b->top]);
467 for (i=(b->max - b->top)>>3; i>0; i--,A+=8)
468 {
469 A[0]=0; A[1]=0; A[2]=0; A[3]=0;
470 A[4]=0; A[5]=0; A[6]=0; A[7]=0;
471 }
472 for (i=(b->max - b->top)&7; i>0; i--,A++)
473 A[0]=0;
474#else
475 memset(A,0,sizeof(BN_ULONG)*(words+1));
476 memcpy(A,b->d,sizeof(b->d[0])*b->top);
477 b->d=a;
478 b->max=words;
479#endif
480
481/* memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG)); */
482/* { int i; for (i=b->max; i<words+1; i++) p[i]=i;} */
483
299 } 484 }
300 return(b); 485 return(b);
301 } 486 }
302 487
303BIGNUM *BN_dup(a) 488BIGNUM *BN_dup(const BIGNUM *a)
304BIGNUM *a;
305 { 489 {
306 BIGNUM *r; 490 BIGNUM *r;
307 491
492 if (a == NULL) return NULL;
493
494 bn_check_top(a);
495
308 r=BN_new(); 496 r=BN_new();
309 if (r == NULL) return(NULL); 497 if (r == NULL) return(NULL);
310 return((BIGNUM *)BN_copy(r,a)); 498 return((BIGNUM *)BN_copy(r,a));
311 } 499 }
312 500
313BIGNUM *BN_copy(a, b) 501BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
314BIGNUM *a;
315BIGNUM *b;
316 { 502 {
317 int i; 503 int i;
318 BN_ULONG *A,*B; 504 BN_ULONG *A;
505 const BN_ULONG *B;
506
507 bn_check_top(b);
319 508
320 if (a == b) return(a); 509 if (a == b) return(a);
321 if (bn_wexpand(a,b->top) == NULL) return(NULL); 510 if (bn_wexpand(a,b->top) == NULL) return(NULL);
@@ -323,35 +512,18 @@ BIGNUM *b;
323#if 1 512#if 1
324 A=a->d; 513 A=a->d;
325 B=b->d; 514 B=b->d;
326 for (i=b->top&(~7); i>0; i-=8) 515 for (i=b->top>>2; i>0; i--,A+=4,B+=4)
327 { 516 {
328 A[0]=B[0]; 517 BN_ULONG a0,a1,a2,a3;
329 A[1]=B[1]; 518 a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
330 A[2]=B[2]; 519 A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
331 A[3]=B[3];
332 A[4]=B[4];
333 A[5]=B[5];
334 A[6]=B[6];
335 A[7]=B[7];
336 A+=8;
337 B+=8;
338 } 520 }
339 switch (b->top&7) 521 switch (b->top&3)
340 { 522 {
341 case 7: 523 case 3: A[2]=B[2];
342 A[6]=B[6]; 524 case 2: A[1]=B[1];
343 case 6: 525 case 1: A[0]=B[0];
344 A[5]=B[5]; 526 case 0: ; /* ultrix cc workaround, see comments in bn_expand2 */
345 case 5:
346 A[4]=B[4];
347 case 4:
348 A[3]=B[3];
349 case 3:
350 A[2]=B[2];
351 case 2:
352 A[1]=B[1];
353 case 1:
354 A[0]=B[0];
355 } 527 }
356#else 528#else
357 memcpy(a->d,b->d,sizeof(b->d[0])*b->top); 529 memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
@@ -359,52 +531,47 @@ BIGNUM *b;
359 531
360/* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/ 532/* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
361 a->top=b->top; 533 a->top=b->top;
362 if (a->top == 0) 534 if ((a->top == 0) && (a->d != NULL))
363 a->d[0]=0; 535 a->d[0]=0;
364 a->neg=b->neg; 536 a->neg=b->neg;
365 return(a); 537 return(a);
366 } 538 }
367 539
368void BN_clear(a) 540void BN_clear(BIGNUM *a)
369BIGNUM *a;
370 { 541 {
371 memset(a->d,0,a->max*sizeof(a->d[0])); 542 if (a->d != NULL)
543 memset(a->d,0,a->max*sizeof(a->d[0]));
372 a->top=0; 544 a->top=0;
373 a->neg=0; 545 a->neg=0;
374 } 546 }
375 547
376unsigned long BN_get_word(a) 548BN_ULONG BN_get_word(BIGNUM *a)
377BIGNUM *a;
378 { 549 {
379 int i,n; 550 int i,n;
380 unsigned long ret=0; 551 BN_ULONG ret=0;
381 552
382 n=BN_num_bytes(a); 553 n=BN_num_bytes(a);
383 if (n > sizeof(unsigned long)) 554 if (n > sizeof(BN_ULONG))
384#ifdef SIXTY_FOUR_BIT_LONG
385 return(BN_MASK2); 555 return(BN_MASK2);
386#else
387 return(0xFFFFFFFFL);
388#endif
389 for (i=a->top-1; i>=0; i--) 556 for (i=a->top-1; i>=0; i--)
390 { 557 {
391#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ 558#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
392 ret<<=BN_BITS4; /* stops the compiler complaining */ 559 ret<<=BN_BITS4; /* stops the compiler complaining */
393 ret<<=BN_BITS4; 560 ret<<=BN_BITS4;
561#else
562 ret=0;
394#endif 563#endif
395 ret|=a->d[i]; 564 ret|=a->d[i];
396 } 565 }
397 return(ret); 566 return(ret);
398 } 567 }
399 568
400int BN_set_word(a,w) 569int BN_set_word(BIGNUM *a, BN_ULONG w)
401BIGNUM *a;
402unsigned long w;
403 { 570 {
404 int i,n; 571 int i,n;
405 if (bn_expand(a,sizeof(unsigned long)*8) == NULL) return(0); 572 if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);
406 573
407 n=sizeof(unsigned long)/BN_BYTES; 574 n=sizeof(BN_ULONG)/BN_BYTES;
408 a->neg=0; 575 a->neg=0;
409 a->top=0; 576 a->top=0;
410 a->d[0]=(BN_ULONG)w&BN_MASK2; 577 a->d[0]=(BN_ULONG)w&BN_MASK2;
@@ -417,6 +584,8 @@ unsigned long w;
417#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ 584#ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
418 w>>=BN_BITS4; 585 w>>=BN_BITS4;
419 w>>=BN_BITS4; 586 w>>=BN_BITS4;
587#else
588 w=0;
420#endif 589#endif
421 a->d[i]=(BN_ULONG)w&BN_MASK2; 590 a->d[i]=(BN_ULONG)w&BN_MASK2;
422 if (a->d[i] != 0) a->top=i+1; 591 if (a->d[i] != 0) a->top=i+1;
@@ -425,10 +594,7 @@ unsigned long w;
425 } 594 }
426 595
427/* ignore negative */ 596/* ignore negative */
428BIGNUM *BN_bin2bn(s, len, ret) 597BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
429unsigned char *s;
430int len;
431BIGNUM *ret;
432 { 598 {
433 unsigned int i,m; 599 unsigned int i,m;
434 unsigned int n; 600 unsigned int n;
@@ -465,9 +631,7 @@ BIGNUM *ret;
465 } 631 }
466 632
467/* ignore negative */ 633/* ignore negative */
468int BN_bn2bin(a, to) 634int BN_bn2bin(const BIGNUM *a, unsigned char *to)
469BIGNUM *a;
470unsigned char *to;
471 { 635 {
472 int n,i; 636 int n,i;
473 BN_ULONG l; 637 BN_ULONG l;
@@ -481,13 +645,14 @@ unsigned char *to;
481 return(n); 645 return(n);
482 } 646 }
483 647
484int BN_ucmp(a, b) 648int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
485BIGNUM *a;
486BIGNUM *b;
487 { 649 {
488 int i; 650 int i;
489 BN_ULONG t1,t2,*ap,*bp; 651 BN_ULONG t1,t2,*ap,*bp;
490 652
653 bn_check_top(a);
654 bn_check_top(b);
655
491 i=a->top-b->top; 656 i=a->top-b->top;
492 if (i != 0) return(i); 657 if (i != 0) return(i);
493 ap=a->d; 658 ap=a->d;
@@ -502,9 +667,7 @@ BIGNUM *b;
502 return(0); 667 return(0);
503 } 668 }
504 669
505int BN_cmp(a, b) 670int BN_cmp(const BIGNUM *a, const BIGNUM *b)
506BIGNUM *a;
507BIGNUM *b;
508 { 671 {
509 int i; 672 int i;
510 int gt,lt; 673 int gt,lt;
@@ -519,6 +682,10 @@ BIGNUM *b;
519 else 682 else
520 return(0); 683 return(0);
521 } 684 }
685
686 bn_check_top(a);
687 bn_check_top(b);
688
522 if (a->neg != b->neg) 689 if (a->neg != b->neg)
523 { 690 {
524 if (a->neg) 691 if (a->neg)
@@ -541,27 +708,25 @@ BIGNUM *b;
541 return(0); 708 return(0);
542 } 709 }
543 710
544int BN_set_bit(a, n) 711int BN_set_bit(BIGNUM *a, int n)
545BIGNUM *a;
546int n;
547 { 712 {
548 int i,j; 713 int i,j,k;
549 714
550 i=n/BN_BITS2; 715 i=n/BN_BITS2;
551 j=n%BN_BITS2; 716 j=n%BN_BITS2;
552 if (a->top <= i) 717 if (a->top <= i)
553 { 718 {
554 if (bn_expand(a,n) == NULL) return(0); 719 if (bn_wexpand(a,i+1) == NULL) return(0);
720 for(k=a->top; k<i+1; k++)
721 a->d[k]=0;
555 a->top=i+1; 722 a->top=i+1;
556 } 723 }
557 724
558 a->d[i]|=(1L<<j); 725 a->d[i]|=(((BN_ULONG)1)<<j);
559 return(1); 726 return(1);
560 } 727 }
561 728
562int BN_clear_bit(a, n) 729int BN_clear_bit(BIGNUM *a, int n)
563BIGNUM *a;
564int n;
565 { 730 {
566 int i,j; 731 int i,j;
567 732
@@ -569,13 +734,12 @@ int n;
569 j=n%BN_BITS2; 734 j=n%BN_BITS2;
570 if (a->top <= i) return(0); 735 if (a->top <= i) return(0);
571 736
572 a->d[i]&=(~(1L<<j)); 737 a->d[i]&=(~(((BN_ULONG)1)<<j));
738 bn_fix_top(a);
573 return(1); 739 return(1);
574 } 740 }
575 741
576int BN_is_bit_set(a, n) 742int BN_is_bit_set(const BIGNUM *a, int n)
577BIGNUM *a;
578int n;
579 { 743 {
580 int i,j; 744 int i,j;
581 745
@@ -586,9 +750,7 @@ int n;
586 return((a->d[i]&(((BN_ULONG)1)<<j))?1:0); 750 return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
587 } 751 }
588 752
589int BN_mask_bits(a,n) 753int BN_mask_bits(BIGNUM *a, int n)
590BIGNUM *a;
591int n;
592 { 754 {
593 int b,w; 755 int b,w;
594 756
@@ -601,11 +763,25 @@ int n;
601 { 763 {
602 a->top=w+1; 764 a->top=w+1;
603 a->d[w]&= ~(BN_MASK2<<b); 765 a->d[w]&= ~(BN_MASK2<<b);
604 while ((w >= 0) && (a->d[w] == 0))
605 {
606 a->top--;
607 w--;
608 }
609 } 766 }
767 bn_fix_top(a);
610 return(1); 768 return(1);
611 } 769 }
770
771int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n)
772 {
773 int i;
774 BN_ULONG aa,bb;
775
776 aa=a[n-1];
777 bb=b[n-1];
778 if (aa != bb) return((aa > bb)?1:-1);
779 for (i=n-2; i>=0; i--)
780 {
781 aa=a[i];
782 bb=b[i];
783 if (aa != bb) return((aa > bb)?1:-1);
784 }
785 return(0);
786 }
787