diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_lib.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 225 |
1 files changed, 102 insertions, 123 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index 2649b8c538..e1660450bc 100644 --- a/src/lib/libcrypto/bn/bn_lib.c +++ b/src/lib/libcrypto/bn/bn_lib.c | |||
@@ -67,10 +67,8 @@ | |||
67 | #include "cryptlib.h" | 67 | #include "cryptlib.h" |
68 | #include "bn_lcl.h" | 68 | #include "bn_lcl.h" |
69 | 69 | ||
70 | const char BN_version[]="Big Number" OPENSSL_VERSION_PTEXT; | 70 | const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT; |
71 | 71 | ||
72 | /* This stuff appears to be completely unused, so is deprecated */ | ||
73 | #ifndef OPENSSL_NO_DEPRECATED | ||
74 | /* For a 32 bit machine | 72 | /* For a 32 bit machine |
75 | * 2 - 4 == 128 | 73 | * 2 - 4 == 128 |
76 | * 3 - 8 == 256 | 74 | * 3 - 8 == 256 |
@@ -93,28 +91,28 @@ void BN_set_params(int mult, int high, int low, int mont) | |||
93 | { | 91 | { |
94 | if (mult >= 0) | 92 | if (mult >= 0) |
95 | { | 93 | { |
96 | if (mult > (int)(sizeof(int)*8)-1) | 94 | if (mult > (sizeof(int)*8)-1) |
97 | mult=sizeof(int)*8-1; | 95 | mult=sizeof(int)*8-1; |
98 | bn_limit_bits=mult; | 96 | bn_limit_bits=mult; |
99 | bn_limit_num=1<<mult; | 97 | bn_limit_num=1<<mult; |
100 | } | 98 | } |
101 | if (high >= 0) | 99 | if (high >= 0) |
102 | { | 100 | { |
103 | if (high > (int)(sizeof(int)*8)-1) | 101 | if (high > (sizeof(int)*8)-1) |
104 | high=sizeof(int)*8-1; | 102 | high=sizeof(int)*8-1; |
105 | bn_limit_bits_high=high; | 103 | bn_limit_bits_high=high; |
106 | bn_limit_num_high=1<<high; | 104 | bn_limit_num_high=1<<high; |
107 | } | 105 | } |
108 | if (low >= 0) | 106 | if (low >= 0) |
109 | { | 107 | { |
110 | if (low > (int)(sizeof(int)*8)-1) | 108 | if (low > (sizeof(int)*8)-1) |
111 | low=sizeof(int)*8-1; | 109 | low=sizeof(int)*8-1; |
112 | bn_limit_bits_low=low; | 110 | bn_limit_bits_low=low; |
113 | bn_limit_num_low=1<<low; | 111 | bn_limit_num_low=1<<low; |
114 | } | 112 | } |
115 | if (mont >= 0) | 113 | if (mont >= 0) |
116 | { | 114 | { |
117 | if (mont > (int)(sizeof(int)*8)-1) | 115 | if (mont > (sizeof(int)*8)-1) |
118 | mont=sizeof(int)*8-1; | 116 | mont=sizeof(int)*8-1; |
119 | bn_limit_bits_mont=mont; | 117 | bn_limit_bits_mont=mont; |
120 | bn_limit_num_mont=1<<mont; | 118 | bn_limit_num_mont=1<<mont; |
@@ -129,12 +127,11 @@ int BN_get_params(int which) | |||
129 | else if (which == 3) return(bn_limit_bits_mont); | 127 | else if (which == 3) return(bn_limit_bits_mont); |
130 | else return(0); | 128 | else return(0); |
131 | } | 129 | } |
132 | #endif | ||
133 | 130 | ||
134 | const BIGNUM *BN_value_one(void) | 131 | const BIGNUM *BN_value_one(void) |
135 | { | 132 | { |
136 | static BN_ULONG data_one=1L; | 133 | static BN_ULONG data_one=1L; |
137 | static BIGNUM const_one={&data_one,1,1,0,BN_FLG_STATIC_DATA}; | 134 | static BIGNUM const_one={&data_one,1,1,0}; |
138 | 135 | ||
139 | return(&const_one); | 136 | return(&const_one); |
140 | } | 137 | } |
@@ -247,11 +244,16 @@ int BN_num_bits_word(BN_ULONG l) | |||
247 | 244 | ||
248 | int BN_num_bits(const BIGNUM *a) | 245 | int BN_num_bits(const BIGNUM *a) |
249 | { | 246 | { |
250 | int i = a->top - 1; | 247 | BN_ULONG l; |
248 | int i; | ||
249 | |||
251 | bn_check_top(a); | 250 | bn_check_top(a); |
252 | 251 | ||
253 | if (BN_is_zero(a)) return 0; | 252 | if (a->top == 0) return(0); |
254 | return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); | 253 | l=a->d[a->top-1]; |
254 | assert(l != 0); | ||
255 | i=(a->top-1)*BN_BITS2; | ||
256 | return(i+BN_num_bits_word(l)); | ||
255 | } | 257 | } |
256 | 258 | ||
257 | void BN_clear_free(BIGNUM *a) | 259 | void BN_clear_free(BIGNUM *a) |
@@ -259,7 +261,6 @@ void BN_clear_free(BIGNUM *a) | |||
259 | int i; | 261 | int i; |
260 | 262 | ||
261 | if (a == NULL) return; | 263 | if (a == NULL) return; |
262 | bn_check_top(a); | ||
263 | if (a->d != NULL) | 264 | if (a->d != NULL) |
264 | { | 265 | { |
265 | OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0])); | 266 | OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0])); |
@@ -275,24 +276,16 @@ void BN_clear_free(BIGNUM *a) | |||
275 | void BN_free(BIGNUM *a) | 276 | void BN_free(BIGNUM *a) |
276 | { | 277 | { |
277 | if (a == NULL) return; | 278 | if (a == NULL) return; |
278 | bn_check_top(a); | ||
279 | if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) | 279 | if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) |
280 | OPENSSL_free(a->d); | 280 | OPENSSL_free(a->d); |
281 | a->flags|=BN_FLG_FREE; /* REMOVE? */ | ||
281 | if (a->flags & BN_FLG_MALLOCED) | 282 | if (a->flags & BN_FLG_MALLOCED) |
282 | OPENSSL_free(a); | 283 | OPENSSL_free(a); |
283 | else | ||
284 | { | ||
285 | #ifndef OPENSSL_NO_DEPRECATED | ||
286 | a->flags|=BN_FLG_FREE; | ||
287 | #endif | ||
288 | a->d = NULL; | ||
289 | } | ||
290 | } | 284 | } |
291 | 285 | ||
292 | void BN_init(BIGNUM *a) | 286 | void BN_init(BIGNUM *a) |
293 | { | 287 | { |
294 | memset(a,0,sizeof(BIGNUM)); | 288 | memset(a,0,sizeof(BIGNUM)); |
295 | bn_check_top(a); | ||
296 | } | 289 | } |
297 | 290 | ||
298 | BIGNUM *BN_new(void) | 291 | BIGNUM *BN_new(void) |
@@ -309,7 +302,6 @@ BIGNUM *BN_new(void) | |||
309 | ret->neg=0; | 302 | ret->neg=0; |
310 | ret->dmax=0; | 303 | ret->dmax=0; |
311 | ret->d=NULL; | 304 | ret->d=NULL; |
312 | bn_check_top(ret); | ||
313 | return(ret); | 305 | return(ret); |
314 | } | 306 | } |
315 | 307 | ||
@@ -321,19 +313,19 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) | |||
321 | const BN_ULONG *B; | 313 | const BN_ULONG *B; |
322 | int i; | 314 | int i; |
323 | 315 | ||
324 | bn_check_top(b); | ||
325 | |||
326 | if (words > (INT_MAX/(4*BN_BITS2))) | 316 | if (words > (INT_MAX/(4*BN_BITS2))) |
327 | { | 317 | { |
328 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG); | 318 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG); |
329 | return NULL; | 319 | return NULL; |
330 | } | 320 | } |
321 | |||
322 | bn_check_top(b); | ||
331 | if (BN_get_flags(b,BN_FLG_STATIC_DATA)) | 323 | if (BN_get_flags(b,BN_FLG_STATIC_DATA)) |
332 | { | 324 | { |
333 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); | 325 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
334 | return(NULL); | 326 | return(NULL); |
335 | } | 327 | } |
336 | a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*words); | 328 | a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1)); |
337 | if (A == NULL) | 329 | if (A == NULL) |
338 | { | 330 | { |
339 | BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE); | 331 | BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE); |
@@ -371,8 +363,19 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) | |||
371 | } | 363 | } |
372 | } | 364 | } |
373 | 365 | ||
366 | /* Now need to zero any data between b->top and b->max */ | ||
367 | /* XXX Why? */ | ||
368 | |||
369 | A= &(a[b->top]); | ||
370 | for (i=(words - b->top)>>3; i>0; i--,A+=8) | ||
371 | { | ||
372 | A[0]=0; A[1]=0; A[2]=0; A[3]=0; | ||
373 | A[4]=0; A[5]=0; A[6]=0; A[7]=0; | ||
374 | } | ||
375 | for (i=(words - b->top)&7; i>0; i--,A++) | ||
376 | A[0]=0; | ||
374 | #else | 377 | #else |
375 | memset(A,0,sizeof(BN_ULONG)*words); | 378 | memset(A,0,sizeof(BN_ULONG)*(words+1)); |
376 | memcpy(A,b->d,sizeof(b->d[0])*b->top); | 379 | memcpy(A,b->d,sizeof(b->d[0])*b->top); |
377 | #endif | 380 | #endif |
378 | 381 | ||
@@ -390,19 +393,16 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) | |||
390 | * while bn_dup_expand() makes sure allocation is made only once. | 393 | * while bn_dup_expand() makes sure allocation is made only once. |
391 | */ | 394 | */ |
392 | 395 | ||
393 | #ifndef OPENSSL_NO_DEPRECATED | ||
394 | BIGNUM *bn_dup_expand(const BIGNUM *b, int words) | 396 | BIGNUM *bn_dup_expand(const BIGNUM *b, int words) |
395 | { | 397 | { |
396 | BIGNUM *r = NULL; | 398 | BIGNUM *r = NULL; |
397 | 399 | ||
398 | bn_check_top(b); | ||
399 | |||
400 | /* This function does not work if | 400 | /* This function does not work if |
401 | * words <= b->dmax && top < words | 401 | * words <= b->dmax && top < words |
402 | * because BN_dup() does not preserve 'dmax'! | 402 | * because BN_dup() does not preserve 'dmax'! |
403 | * (But bn_dup_expand() is not used anywhere yet.) | 403 | * (But bn_dup_expand() is not used anywhere yet.) |
404 | */ | 404 | */ |
405 | 405 | ||
406 | if (words > b->dmax) | 406 | if (words > b->dmax) |
407 | { | 407 | { |
408 | BN_ULONG *a = bn_expand_internal(b, words); | 408 | BN_ULONG *a = bn_expand_internal(b, words); |
@@ -431,67 +431,48 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, int words) | |||
431 | r = BN_dup(b); | 431 | r = BN_dup(b); |
432 | } | 432 | } |
433 | 433 | ||
434 | bn_check_top(r); | ||
435 | return r; | 434 | return r; |
436 | } | 435 | } |
437 | #endif | ||
438 | 436 | ||
439 | /* This is an internal function that should not be used in applications. | 437 | /* This is an internal function that should not be used in applications. |
440 | * It ensures that 'b' has enough room for a 'words' word number | 438 | * It ensures that 'b' has enough room for a 'words' word number number. |
441 | * and initialises any unused part of b->d with leading zeros. | ||
442 | * It is mostly used by the various BIGNUM routines. If there is an error, | 439 | * It is mostly used by the various BIGNUM routines. If there is an error, |
443 | * NULL is returned. If not, 'b' is returned. */ | 440 | * NULL is returned. If not, 'b' is returned. */ |
444 | 441 | ||
445 | BIGNUM *bn_expand2(BIGNUM *b, int words) | 442 | BIGNUM *bn_expand2(BIGNUM *b, int words) |
446 | { | 443 | { |
447 | bn_check_top(b); | ||
448 | |||
449 | if (words > b->dmax) | 444 | if (words > b->dmax) |
450 | { | 445 | { |
451 | BN_ULONG *a = bn_expand_internal(b, words); | 446 | BN_ULONG *a = bn_expand_internal(b, words); |
452 | if(!a) return NULL; | ||
453 | if(b->d) OPENSSL_free(b->d); | ||
454 | b->d=a; | ||
455 | b->dmax=words; | ||
456 | } | ||
457 | 447 | ||
458 | /* None of this should be necessary because of what b->top means! */ | 448 | if (a) |
459 | #if 0 | ||
460 | /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */ | ||
461 | if (b->top < b->dmax) | ||
462 | { | ||
463 | int i; | ||
464 | BN_ULONG *A = &(b->d[b->top]); | ||
465 | for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8) | ||
466 | { | 449 | { |
467 | A[0]=0; A[1]=0; A[2]=0; A[3]=0; | 450 | if (b->d) |
468 | A[4]=0; A[5]=0; A[6]=0; A[7]=0; | 451 | OPENSSL_free(b->d); |
452 | b->d=a; | ||
453 | b->dmax=words; | ||
469 | } | 454 | } |
470 | for (i=(b->dmax - b->top)&7; i>0; i--,A++) | 455 | else |
471 | A[0]=0; | 456 | b = NULL; |
472 | assert(A == &(b->d[b->dmax])); | ||
473 | } | 457 | } |
474 | #endif | ||
475 | bn_check_top(b); | ||
476 | return b; | 458 | return b; |
477 | } | 459 | } |
478 | 460 | ||
479 | BIGNUM *BN_dup(const BIGNUM *a) | 461 | BIGNUM *BN_dup(const BIGNUM *a) |
480 | { | 462 | { |
481 | BIGNUM *t; | 463 | BIGNUM *r, *t; |
482 | 464 | ||
483 | if (a == NULL) return NULL; | 465 | if (a == NULL) return NULL; |
466 | |||
484 | bn_check_top(a); | 467 | bn_check_top(a); |
485 | 468 | ||
486 | t = BN_new(); | 469 | t = BN_new(); |
487 | if (t == NULL) return NULL; | 470 | if (t == NULL) return(NULL); |
488 | if(!BN_copy(t, a)) | 471 | r = BN_copy(t, a); |
489 | { | 472 | /* now r == t || r == NULL */ |
473 | if (r == NULL) | ||
490 | BN_free(t); | 474 | BN_free(t); |
491 | return NULL; | 475 | return r; |
492 | } | ||
493 | bn_check_top(t); | ||
494 | return t; | ||
495 | } | 476 | } |
496 | 477 | ||
497 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) | 478 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) |
@@ -525,9 +506,11 @@ BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) | |||
525 | memcpy(a->d,b->d,sizeof(b->d[0])*b->top); | 506 | memcpy(a->d,b->d,sizeof(b->d[0])*b->top); |
526 | #endif | 507 | #endif |
527 | 508 | ||
509 | /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/ | ||
528 | a->top=b->top; | 510 | a->top=b->top; |
511 | if ((a->top == 0) && (a->d != NULL)) | ||
512 | a->d[0]=0; | ||
529 | a->neg=b->neg; | 513 | a->neg=b->neg; |
530 | bn_check_top(a); | ||
531 | return(a); | 514 | return(a); |
532 | } | 515 | } |
533 | 516 | ||
@@ -537,9 +520,6 @@ void BN_swap(BIGNUM *a, BIGNUM *b) | |||
537 | BN_ULONG *tmp_d; | 520 | BN_ULONG *tmp_d; |
538 | int tmp_top, tmp_dmax, tmp_neg; | 521 | int tmp_top, tmp_dmax, tmp_neg; |
539 | 522 | ||
540 | bn_check_top(a); | ||
541 | bn_check_top(b); | ||
542 | |||
543 | flags_old_a = a->flags; | 523 | flags_old_a = a->flags; |
544 | flags_old_b = b->flags; | 524 | flags_old_b = b->flags; |
545 | 525 | ||
@@ -560,13 +540,11 @@ void BN_swap(BIGNUM *a, BIGNUM *b) | |||
560 | 540 | ||
561 | a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA); | 541 | a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA); |
562 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA); | 542 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA); |
563 | bn_check_top(a); | ||
564 | bn_check_top(b); | ||
565 | } | 543 | } |
566 | 544 | ||
545 | |||
567 | void BN_clear(BIGNUM *a) | 546 | void BN_clear(BIGNUM *a) |
568 | { | 547 | { |
569 | bn_check_top(a); | ||
570 | if (a->d != NULL) | 548 | if (a->d != NULL) |
571 | memset(a->d,0,a->dmax*sizeof(a->d[0])); | 549 | memset(a->d,0,a->dmax*sizeof(a->d[0])); |
572 | a->top=0; | 550 | a->top=0; |
@@ -575,22 +553,49 @@ void BN_clear(BIGNUM *a) | |||
575 | 553 | ||
576 | BN_ULONG BN_get_word(const BIGNUM *a) | 554 | BN_ULONG BN_get_word(const BIGNUM *a) |
577 | { | 555 | { |
578 | if (a->top > 1) | 556 | int i,n; |
579 | return BN_MASK2; | 557 | BN_ULONG ret=0; |
580 | else if (a->top == 1) | 558 | |
581 | return a->d[0]; | 559 | n=BN_num_bytes(a); |
582 | /* a->top == 0 */ | 560 | if (n > sizeof(BN_ULONG)) |
583 | return 0; | 561 | return(BN_MASK2); |
562 | for (i=a->top-1; i>=0; i--) | ||
563 | { | ||
564 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ | ||
565 | ret<<=BN_BITS4; /* stops the compiler complaining */ | ||
566 | ret<<=BN_BITS4; | ||
567 | #else | ||
568 | ret=0; | ||
569 | #endif | ||
570 | ret|=a->d[i]; | ||
571 | } | ||
572 | return(ret); | ||
584 | } | 573 | } |
585 | 574 | ||
586 | int BN_set_word(BIGNUM *a, BN_ULONG w) | 575 | int BN_set_word(BIGNUM *a, BN_ULONG w) |
587 | { | 576 | { |
588 | bn_check_top(a); | 577 | int i,n; |
589 | if (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0); | 578 | if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0); |
590 | a->neg = 0; | 579 | |
591 | a->d[0] = w; | 580 | n=sizeof(BN_ULONG)/BN_BYTES; |
592 | a->top = (w ? 1 : 0); | 581 | a->neg=0; |
593 | bn_check_top(a); | 582 | a->top=0; |
583 | a->d[0]=(BN_ULONG)w&BN_MASK2; | ||
584 | if (a->d[0] != 0) a->top=1; | ||
585 | for (i=1; i<n; i++) | ||
586 | { | ||
587 | /* the following is done instead of | ||
588 | * w>>=BN_BITS2 so compilers don't complain | ||
589 | * on builds where sizeof(long) == BN_TYPES */ | ||
590 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ | ||
591 | w>>=BN_BITS4; | ||
592 | w>>=BN_BITS4; | ||
593 | #else | ||
594 | w=0; | ||
595 | #endif | ||
596 | a->d[i]=(BN_ULONG)w&BN_MASK2; | ||
597 | if (a->d[i] != 0) a->top=i+1; | ||
598 | } | ||
594 | return(1); | 599 | return(1); |
595 | } | 600 | } |
596 | 601 | ||
@@ -599,12 +604,9 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) | |||
599 | unsigned int i,m; | 604 | unsigned int i,m; |
600 | unsigned int n; | 605 | unsigned int n; |
601 | BN_ULONG l; | 606 | BN_ULONG l; |
602 | BIGNUM *bn = NULL; | ||
603 | 607 | ||
604 | if (ret == NULL) | 608 | if (ret == NULL) ret=BN_new(); |
605 | ret = bn = BN_new(); | ||
606 | if (ret == NULL) return(NULL); | 609 | if (ret == NULL) return(NULL); |
607 | bn_check_top(ret); | ||
608 | l=0; | 610 | l=0; |
609 | n=len; | 611 | n=len; |
610 | if (n == 0) | 612 | if (n == 0) |
@@ -612,16 +614,13 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) | |||
612 | ret->top=0; | 614 | ret->top=0; |
613 | return(ret); | 615 | return(ret); |
614 | } | 616 | } |
617 | if (bn_expand(ret,(int)(n+2)*8) == NULL) | ||
618 | return(NULL); | ||
615 | i=((n-1)/BN_BYTES)+1; | 619 | i=((n-1)/BN_BYTES)+1; |
616 | m=((n-1)%(BN_BYTES)); | 620 | m=((n-1)%(BN_BYTES)); |
617 | if (bn_wexpand(ret, (int)i) == NULL) | ||
618 | { | ||
619 | if (bn) BN_free(bn); | ||
620 | return NULL; | ||
621 | } | ||
622 | ret->top=i; | 621 | ret->top=i; |
623 | ret->neg=0; | 622 | ret->neg=0; |
624 | while (n--) | 623 | while (n-- > 0) |
625 | { | 624 | { |
626 | l=(l<<8L)| *(s++); | 625 | l=(l<<8L)| *(s++); |
627 | if (m-- == 0) | 626 | if (m-- == 0) |
@@ -633,7 +632,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) | |||
633 | } | 632 | } |
634 | /* need to call this due to clear byte at top if avoiding | 633 | /* need to call this due to clear byte at top if avoiding |
635 | * having the top bit set (-ve number) */ | 634 | * having the top bit set (-ve number) */ |
636 | bn_correct_top(ret); | 635 | bn_fix_top(ret); |
637 | return(ret); | 636 | return(ret); |
638 | } | 637 | } |
639 | 638 | ||
@@ -643,9 +642,8 @@ int BN_bn2bin(const BIGNUM *a, unsigned char *to) | |||
643 | int n,i; | 642 | int n,i; |
644 | BN_ULONG l; | 643 | BN_ULONG l; |
645 | 644 | ||
646 | bn_check_top(a); | ||
647 | n=i=BN_num_bytes(a); | 645 | n=i=BN_num_bytes(a); |
648 | while (i--) | 646 | while (i-- > 0) |
649 | { | 647 | { |
650 | l=a->d[i/BN_BYTES]; | 648 | l=a->d[i/BN_BYTES]; |
651 | *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff; | 649 | *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff; |
@@ -670,7 +668,7 @@ int BN_ucmp(const BIGNUM *a, const BIGNUM *b) | |||
670 | t1= ap[i]; | 668 | t1= ap[i]; |
671 | t2= bp[i]; | 669 | t2= bp[i]; |
672 | if (t1 != t2) | 670 | if (t1 != t2) |
673 | return((t1 > t2) ? 1 : -1); | 671 | return(t1 > t2?1:-1); |
674 | } | 672 | } |
675 | return(0); | 673 | return(0); |
676 | } | 674 | } |
@@ -720,9 +718,6 @@ int BN_set_bit(BIGNUM *a, int n) | |||
720 | { | 718 | { |
721 | int i,j,k; | 719 | int i,j,k; |
722 | 720 | ||
723 | if (n < 0) | ||
724 | return 0; | ||
725 | |||
726 | i=n/BN_BITS2; | 721 | i=n/BN_BITS2; |
727 | j=n%BN_BITS2; | 722 | j=n%BN_BITS2; |
728 | if (a->top <= i) | 723 | if (a->top <= i) |
@@ -734,7 +729,6 @@ int BN_set_bit(BIGNUM *a, int n) | |||
734 | } | 729 | } |
735 | 730 | ||
736 | a->d[i]|=(((BN_ULONG)1)<<j); | 731 | a->d[i]|=(((BN_ULONG)1)<<j); |
737 | bn_check_top(a); | ||
738 | return(1); | 732 | return(1); |
739 | } | 733 | } |
740 | 734 | ||
@@ -742,15 +736,12 @@ int BN_clear_bit(BIGNUM *a, int n) | |||
742 | { | 736 | { |
743 | int i,j; | 737 | int i,j; |
744 | 738 | ||
745 | bn_check_top(a); | ||
746 | if (n < 0) return 0; | ||
747 | |||
748 | i=n/BN_BITS2; | 739 | i=n/BN_BITS2; |
749 | j=n%BN_BITS2; | 740 | j=n%BN_BITS2; |
750 | if (a->top <= i) return(0); | 741 | if (a->top <= i) return(0); |
751 | 742 | ||
752 | a->d[i]&=(~(((BN_ULONG)1)<<j)); | 743 | a->d[i]&=(~(((BN_ULONG)1)<<j)); |
753 | bn_correct_top(a); | 744 | bn_fix_top(a); |
754 | return(1); | 745 | return(1); |
755 | } | 746 | } |
756 | 747 | ||
@@ -758,24 +749,20 @@ int BN_is_bit_set(const BIGNUM *a, int n) | |||
758 | { | 749 | { |
759 | int i,j; | 750 | int i,j; |
760 | 751 | ||
761 | bn_check_top(a); | 752 | if (n < 0) return(0); |
762 | if (n < 0) return 0; | ||
763 | i=n/BN_BITS2; | 753 | i=n/BN_BITS2; |
764 | j=n%BN_BITS2; | 754 | j=n%BN_BITS2; |
765 | if (a->top <= i) return 0; | 755 | if (a->top <= i) return(0); |
766 | return(((a->d[i])>>j)&((BN_ULONG)1)); | 756 | return((a->d[i]&(((BN_ULONG)1)<<j))?1:0); |
767 | } | 757 | } |
768 | 758 | ||
769 | int BN_mask_bits(BIGNUM *a, int n) | 759 | int BN_mask_bits(BIGNUM *a, int n) |
770 | { | 760 | { |
771 | int b,w; | 761 | int b,w; |
772 | 762 | ||
773 | bn_check_top(a); | ||
774 | if (n < 0) return 0; | ||
775 | |||
776 | w=n/BN_BITS2; | 763 | w=n/BN_BITS2; |
777 | b=n%BN_BITS2; | 764 | b=n%BN_BITS2; |
778 | if (w >= a->top) return 0; | 765 | if (w >= a->top) return(0); |
779 | if (b == 0) | 766 | if (b == 0) |
780 | a->top=w; | 767 | a->top=w; |
781 | else | 768 | else |
@@ -783,18 +770,10 @@ int BN_mask_bits(BIGNUM *a, int n) | |||
783 | a->top=w+1; | 770 | a->top=w+1; |
784 | a->d[w]&= ~(BN_MASK2<<b); | 771 | a->d[w]&= ~(BN_MASK2<<b); |
785 | } | 772 | } |
786 | bn_correct_top(a); | 773 | bn_fix_top(a); |
787 | return(1); | 774 | return(1); |
788 | } | 775 | } |
789 | 776 | ||
790 | void BN_set_negative(BIGNUM *a, int b) | ||
791 | { | ||
792 | if (b && !BN_is_zero(a)) | ||
793 | a->neg = 1; | ||
794 | else | ||
795 | a->neg = 0; | ||
796 | } | ||
797 | |||
798 | int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) | 777 | int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) |
799 | { | 778 | { |
800 | int i; | 779 | int i; |