diff options
author | jsing <> | 2022-12-23 03:15:35 +0000 |
---|---|---|
committer | jsing <> | 2022-12-23 03:15:35 +0000 |
commit | baf6f3e2cd652bf21f854e7aaad1e05f88077c25 (patch) | |
tree | 10949b74c3014c60c478fedad7cc924359851c6b /src | |
parent | 8d2a4066281adb06dfb03f958f7b178286156e32 (diff) | |
download | openbsd-baf6f3e2cd652bf21f854e7aaad1e05f88077c25.tar.gz openbsd-baf6f3e2cd652bf21f854e7aaad1e05f88077c25.tar.bz2 openbsd-baf6f3e2cd652bf21f854e7aaad1e05f88077c25.zip |
Simplify BN_cmp() and BN_ucmp().
The only real difference between BN_cmp() and BN_ucmp() is that one has
to respect the sign of the BN (although BN_cmp() also gets to deal with
some insanity from accepting NULLs). Rewrite/cleanup BN_ucmp() and turn
BN_cmp() into code that handles differences in sign, before calling
BN_ucmp().
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 61 |
1 files changed, 15 insertions, 46 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index c47f2fa024..eed7377cd9 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.67 2022/12/17 15:56:25 jsing Exp $ */ | 1 | /* $OpenBSD: bn_lib.c,v 1.68 2022/12/23 03:15:35 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 | * |
@@ -695,69 +695,38 @@ int | |||
695 | BN_ucmp(const BIGNUM *a, const BIGNUM *b) | 695 | BN_ucmp(const BIGNUM *a, const BIGNUM *b) |
696 | { | 696 | { |
697 | int i; | 697 | int i; |
698 | BN_ULONG t1, t2, *ap, *bp; | ||
699 | |||
700 | 698 | ||
701 | if (a->top < b->top) | 699 | if (a->top < b->top) |
702 | return -1; | 700 | return -1; |
703 | if (a->top > b->top) | 701 | if (a->top > b->top) |
704 | return 1; | 702 | return 1; |
705 | 703 | ||
706 | ap = a->d; | ||
707 | bp = b->d; | ||
708 | for (i = a->top - 1; i >= 0; i--) { | 704 | for (i = a->top - 1; i >= 0; i--) { |
709 | t1 = ap[i]; | 705 | if (a->d[i] != b->d[i]) |
710 | t2 = bp[i]; | 706 | return (a->d[i] > b->d[i] ? 1 : -1); |
711 | if (t1 != t2) | ||
712 | return ((t1 > t2) ? 1 : -1); | ||
713 | } | 707 | } |
714 | return (0); | 708 | |
709 | return 0; | ||
715 | } | 710 | } |
716 | 711 | ||
717 | int | 712 | int |
718 | BN_cmp(const BIGNUM *a, const BIGNUM *b) | 713 | BN_cmp(const BIGNUM *a, const BIGNUM *b) |
719 | { | 714 | { |
720 | int i; | 715 | if (a == NULL || b == NULL) { |
721 | int gt, lt; | ||
722 | BN_ULONG t1, t2; | ||
723 | |||
724 | if ((a == NULL) || (b == NULL)) { | ||
725 | if (a != NULL) | 716 | if (a != NULL) |
726 | return (-1); | 717 | return -1; |
727 | else if (b != NULL) | 718 | if (b != NULL) |
728 | return (1); | 719 | return 1; |
729 | else | 720 | return 0; |
730 | return (0); | ||
731 | } | 721 | } |
732 | 722 | ||
723 | if (a->neg != b->neg) | ||
724 | return b->neg - a->neg; | ||
733 | 725 | ||
734 | if (a->neg != b->neg) { | 726 | if (a->neg) |
735 | if (a->neg) | 727 | return BN_ucmp(b, a); |
736 | return (-1); | ||
737 | else | ||
738 | return (1); | ||
739 | } | ||
740 | if (a->neg == 0) { | ||
741 | gt = 1; | ||
742 | lt = -1; | ||
743 | } else { | ||
744 | gt = -1; | ||
745 | lt = 1; | ||
746 | } | ||
747 | 728 | ||
748 | if (a->top > b->top) | 729 | return BN_ucmp(a, b); |
749 | return (gt); | ||
750 | if (a->top < b->top) | ||
751 | return (lt); | ||
752 | for (i = a->top - 1; i >= 0; i--) { | ||
753 | t1 = a->d[i]; | ||
754 | t2 = b->d[i]; | ||
755 | if (t1 > t2) | ||
756 | return (gt); | ||
757 | if (t1 < t2) | ||
758 | return (lt); | ||
759 | } | ||
760 | return (0); | ||
761 | } | 730 | } |
762 | 731 | ||
763 | int | 732 | int |