diff options
author | jsing <> | 2022-11-26 13:56:33 +0000 |
---|---|---|
committer | jsing <> | 2022-11-26 13:56:33 +0000 |
commit | bcbac728558eebfaa4404c405e7dc22769585345 (patch) | |
tree | 9f1339c5b70b0cfa4e8a0a5c70345e837a21ce77 /src/lib/libcrypto/bn/bn_lib.c | |
parent | 90d0616c736d954d327f399daa636de8e6a2c4d5 (diff) | |
download | openbsd-bcbac728558eebfaa4404c405e7dc22769585345.tar.gz openbsd-bcbac728558eebfaa4404c405e7dc22769585345.tar.bz2 openbsd-bcbac728558eebfaa4404c405e7dc22769585345.zip |
Remove BIGNUM consistency macros.
Compiling with BN_DEBUG (and if you want to take it further, BN_DEBUG_RAND)
supposedly adds consistency checks to the BN code. These are rarely if ever
used and introduce a bunch of clutter in the code. Furthermore, there are
hacks in place to undo things that the debugging code does.
Remove all of this mess and instead rely on always enabled checks, more
readable code and proper regress coverage to ensure correct behaviour.
"Good riddance." tb@
Diffstat (limited to 'src/lib/libcrypto/bn/bn_lib.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 33 |
1 files changed, 1 insertions, 32 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index e67abf90b1..a3b6811986 100644 --- a/src/lib/libcrypto/bn/bn_lib.c +++ b/src/lib/libcrypto/bn/bn_lib.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_lib.c,v 1.61 2022/11/24 01:30:01 jsing Exp $ */ | 1 | /* $OpenBSD: bn_lib.c,v 1.62 2022/11/26 13:56:33 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -86,7 +86,6 @@ BN_new(void) | |||
86 | ret->neg = 0; | 86 | ret->neg = 0; |
87 | ret->dmax = 0; | 87 | ret->dmax = 0; |
88 | ret->d = NULL; | 88 | ret->d = NULL; |
89 | bn_check_top(ret); | ||
90 | return (ret); | 89 | return (ret); |
91 | } | 90 | } |
92 | 91 | ||
@@ -94,13 +93,11 @@ void | |||
94 | BN_init(BIGNUM *a) | 93 | BN_init(BIGNUM *a) |
95 | { | 94 | { |
96 | memset(a, 0, sizeof(BIGNUM)); | 95 | memset(a, 0, sizeof(BIGNUM)); |
97 | bn_check_top(a); | ||
98 | } | 96 | } |
99 | 97 | ||
100 | void | 98 | void |
101 | BN_clear(BIGNUM *a) | 99 | BN_clear(BIGNUM *a) |
102 | { | 100 | { |
103 | bn_check_top(a); | ||
104 | if (a->d != NULL) | 101 | if (a->d != NULL) |
105 | explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); | 102 | explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); |
106 | a->top = 0; | 103 | a->top = 0; |
@@ -114,7 +111,6 @@ BN_clear_free(BIGNUM *a) | |||
114 | 111 | ||
115 | if (a == NULL) | 112 | if (a == NULL) |
116 | return; | 113 | return; |
117 | bn_check_top(a); | ||
118 | if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) | 114 | if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) |
119 | freezero(a->d, a->dmax * sizeof(a->d[0])); | 115 | freezero(a->d, a->dmax * sizeof(a->d[0])); |
120 | i = BN_get_flags(a, BN_FLG_MALLOCED); | 116 | i = BN_get_flags(a, BN_FLG_MALLOCED); |
@@ -256,7 +252,6 @@ BN_num_bits(const BIGNUM *a) | |||
256 | { | 252 | { |
257 | int i = a->top - 1; | 253 | int i = a->top - 1; |
258 | 254 | ||
259 | bn_check_top(a); | ||
260 | 255 | ||
261 | if (BN_is_zero(a)) | 256 | if (BN_is_zero(a)) |
262 | return 0; | 257 | return 0; |
@@ -271,7 +266,6 @@ bn_expand_internal(const BIGNUM *b, int words) | |||
271 | const BN_ULONG *B; | 266 | const BN_ULONG *B; |
272 | int i; | 267 | int i; |
273 | 268 | ||
274 | bn_check_top(b); | ||
275 | 269 | ||
276 | if (words > (INT_MAX/(4*BN_BITS2))) { | 270 | if (words > (INT_MAX/(4*BN_BITS2))) { |
277 | BNerror(BN_R_BIGNUM_TOO_LONG); | 271 | BNerror(BN_R_BIGNUM_TOO_LONG); |
@@ -337,7 +331,6 @@ bn_expand_internal(const BIGNUM *b, int words) | |||
337 | static int | 331 | static int |
338 | bn_expand2(BIGNUM *b, int words) | 332 | bn_expand2(BIGNUM *b, int words) |
339 | { | 333 | { |
340 | bn_check_top(b); | ||
341 | 334 | ||
342 | if (words > b->dmax) { | 335 | if (words > b->dmax) { |
343 | BN_ULONG *a = bn_expand_internal(b, words); | 336 | BN_ULONG *a = bn_expand_internal(b, words); |
@@ -370,7 +363,6 @@ bn_expand2(BIGNUM *b, int words) | |||
370 | assert(A == &(b->d[b->dmax])); | 363 | assert(A == &(b->d[b->dmax])); |
371 | } | 364 | } |
372 | #endif | 365 | #endif |
373 | bn_check_top(b); | ||
374 | return 1; | 366 | return 1; |
375 | } | 367 | } |
376 | 368 | ||
@@ -408,7 +400,6 @@ BN_dup(const BIGNUM *a) | |||
408 | 400 | ||
409 | if (a == NULL) | 401 | if (a == NULL) |
410 | return NULL; | 402 | return NULL; |
411 | bn_check_top(a); | ||
412 | 403 | ||
413 | t = BN_new(); | 404 | t = BN_new(); |
414 | if (t == NULL) | 405 | if (t == NULL) |
@@ -417,7 +408,6 @@ BN_dup(const BIGNUM *a) | |||
417 | BN_free(t); | 408 | BN_free(t); |
418 | return NULL; | 409 | return NULL; |
419 | } | 410 | } |
420 | bn_check_top(t); | ||
421 | return t; | 411 | return t; |
422 | } | 412 | } |
423 | 413 | ||
@@ -428,7 +418,6 @@ BN_copy(BIGNUM *a, const BIGNUM *b) | |||
428 | BN_ULONG *A; | 418 | BN_ULONG *A; |
429 | const BN_ULONG *B; | 419 | const BN_ULONG *B; |
430 | 420 | ||
431 | bn_check_top(b); | ||
432 | 421 | ||
433 | if (a == b) | 422 | if (a == b) |
434 | return (a); | 423 | return (a); |
@@ -463,7 +452,6 @@ BN_copy(BIGNUM *a, const BIGNUM *b) | |||
463 | 452 | ||
464 | a->top = b->top; | 453 | a->top = b->top; |
465 | a->neg = b->neg; | 454 | a->neg = b->neg; |
466 | bn_check_top(a); | ||
467 | return (a); | 455 | return (a); |
468 | } | 456 | } |
469 | 457 | ||
@@ -474,8 +462,6 @@ BN_swap(BIGNUM *a, BIGNUM *b) | |||
474 | BN_ULONG *tmp_d; | 462 | BN_ULONG *tmp_d; |
475 | int tmp_top, tmp_dmax, tmp_neg; | 463 | int tmp_top, tmp_dmax, tmp_neg; |
476 | 464 | ||
477 | bn_check_top(a); | ||
478 | bn_check_top(b); | ||
479 | 465 | ||
480 | flags_old_a = a->flags; | 466 | flags_old_a = a->flags; |
481 | flags_old_b = b->flags; | 467 | flags_old_b = b->flags; |
@@ -499,8 +485,6 @@ BN_swap(BIGNUM *a, BIGNUM *b) | |||
499 | (flags_old_b & BN_FLG_STATIC_DATA); | 485 | (flags_old_b & BN_FLG_STATIC_DATA); |
500 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | | 486 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | |
501 | (flags_old_a & BN_FLG_STATIC_DATA); | 487 | (flags_old_a & BN_FLG_STATIC_DATA); |
502 | bn_check_top(a); | ||
503 | bn_check_top(b); | ||
504 | } | 488 | } |
505 | 489 | ||
506 | BN_ULONG | 490 | BN_ULONG |
@@ -517,13 +501,11 @@ BN_get_word(const BIGNUM *a) | |||
517 | int | 501 | int |
518 | BN_set_word(BIGNUM *a, BN_ULONG w) | 502 | BN_set_word(BIGNUM *a, BN_ULONG w) |
519 | { | 503 | { |
520 | bn_check_top(a); | ||
521 | if (!bn_wexpand(a, 1)) | 504 | if (!bn_wexpand(a, 1)) |
522 | return (0); | 505 | return (0); |
523 | a->neg = 0; | 506 | a->neg = 0; |
524 | a->d[0] = w; | 507 | a->d[0] = w; |
525 | a->top = (w ? 1 : 0); | 508 | a->top = (w ? 1 : 0); |
526 | bn_check_top(a); | ||
527 | return (1); | 509 | return (1); |
528 | } | 510 | } |
529 | 511 | ||
@@ -541,7 +523,6 @@ BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) | |||
541 | ret = bn = BN_new(); | 523 | ret = bn = BN_new(); |
542 | if (ret == NULL) | 524 | if (ret == NULL) |
543 | return (NULL); | 525 | return (NULL); |
544 | bn_check_top(ret); | ||
545 | l = 0; | 526 | l = 0; |
546 | n = len; | 527 | n = len; |
547 | if (n == 0) { | 528 | if (n == 0) { |
@@ -658,7 +639,6 @@ BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret) | |||
658 | if (ret == NULL) | 639 | if (ret == NULL) |
659 | return NULL; | 640 | return NULL; |
660 | 641 | ||
661 | bn_check_top(ret); | ||
662 | 642 | ||
663 | s += len; | 643 | s += len; |
664 | /* Skip trailing zeroes. */ | 644 | /* Skip trailing zeroes. */ |
@@ -715,8 +695,6 @@ BN_ucmp(const BIGNUM *a, const BIGNUM *b) | |||
715 | int i; | 695 | int i; |
716 | BN_ULONG t1, t2, *ap, *bp; | 696 | BN_ULONG t1, t2, *ap, *bp; |
717 | 697 | ||
718 | bn_check_top(a); | ||
719 | bn_check_top(b); | ||
720 | 698 | ||
721 | i = a->top - b->top; | 699 | i = a->top - b->top; |
722 | if (i != 0) | 700 | if (i != 0) |
@@ -748,8 +726,6 @@ BN_cmp(const BIGNUM *a, const BIGNUM *b) | |||
748 | return (0); | 726 | return (0); |
749 | } | 727 | } |
750 | 728 | ||
751 | bn_check_top(a); | ||
752 | bn_check_top(b); | ||
753 | 729 | ||
754 | if (a->neg != b->neg) { | 730 | if (a->neg != b->neg) { |
755 | if (a->neg) | 731 | if (a->neg) |
@@ -799,7 +775,6 @@ BN_set_bit(BIGNUM *a, int n) | |||
799 | } | 775 | } |
800 | 776 | ||
801 | a->d[i] |= (((BN_ULONG)1) << j); | 777 | a->d[i] |= (((BN_ULONG)1) << j); |
802 | bn_check_top(a); | ||
803 | return (1); | 778 | return (1); |
804 | } | 779 | } |
805 | 780 | ||
@@ -808,7 +783,6 @@ BN_clear_bit(BIGNUM *a, int n) | |||
808 | { | 783 | { |
809 | int i, j; | 784 | int i, j; |
810 | 785 | ||
811 | bn_check_top(a); | ||
812 | if (n < 0) | 786 | if (n < 0) |
813 | return 0; | 787 | return 0; |
814 | 788 | ||
@@ -827,7 +801,6 @@ BN_is_bit_set(const BIGNUM *a, int n) | |||
827 | { | 801 | { |
828 | int i, j; | 802 | int i, j; |
829 | 803 | ||
830 | bn_check_top(a); | ||
831 | if (n < 0) | 804 | if (n < 0) |
832 | return 0; | 805 | return 0; |
833 | i = n / BN_BITS2; | 806 | i = n / BN_BITS2; |
@@ -842,7 +815,6 @@ BN_mask_bits(BIGNUM *a, int n) | |||
842 | { | 815 | { |
843 | int b, w; | 816 | int b, w; |
844 | 817 | ||
845 | bn_check_top(a); | ||
846 | if (n < 0) | 818 | if (n < 0) |
847 | return 0; | 819 | return 0; |
848 | 820 | ||
@@ -932,9 +904,6 @@ BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) | |||
932 | BN_ULONG t; | 904 | BN_ULONG t; |
933 | int i; | 905 | int i; |
934 | 906 | ||
935 | bn_wcheck_size(a, nwords); | ||
936 | bn_wcheck_size(b, nwords); | ||
937 | |||
938 | assert(a != b); | 907 | assert(a != b); |
939 | assert((condition & (condition - 1)) == 0); | 908 | assert((condition & (condition - 1)) == 0); |
940 | assert(sizeof(BN_ULONG) >= sizeof(int)); | 909 | assert(sizeof(BN_ULONG) >= sizeof(int)); |