diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/bn/bn_gcd.c | 77 |
1 files changed, 28 insertions, 49 deletions
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c index 905178913c..e741ef37dc 100644 --- a/src/lib/libcrypto/bn/bn_gcd.c +++ b/src/lib/libcrypto/bn/bn_gcd.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_gcd.c,v 1.25 2023/04/01 11:10:55 tb Exp $ */ | 1 | /* $OpenBSD: bn_gcd.c,v 1.26 2023/04/03 21:43:43 tb 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 | * |
@@ -119,65 +119,44 @@ euclid(BIGNUM *a, BIGNUM *b) | |||
119 | BIGNUM *t; | 119 | BIGNUM *t; |
120 | int shifts = 0; | 120 | int shifts = 0; |
121 | 121 | ||
122 | 122 | /* Loop invariant: 0 <= b <= a. */ | |
123 | /* 0 <= b <= a */ | ||
124 | while (!BN_is_zero(b)) { | 123 | while (!BN_is_zero(b)) { |
125 | /* 0 < b <= a */ | 124 | if (BN_is_odd(a) && BN_is_odd(b)) { |
126 | 125 | if (!BN_sub(a, a, b)) | |
127 | if (BN_is_odd(a)) { | 126 | goto err; |
128 | if (BN_is_odd(b)) { | 127 | if (!BN_rshift1(a, a)) |
129 | if (!BN_sub(a, a, b)) | 128 | goto err; |
130 | goto err; | 129 | } else if (BN_is_odd(a) && !BN_is_odd(b)) { |
131 | if (!BN_rshift1(a, a)) | 130 | if (!BN_rshift1(b, b)) |
132 | goto err; | 131 | goto err; |
133 | if (BN_cmp(a, b) < 0) { | 132 | } else if (!BN_is_odd(a) && BN_is_odd(b)) { |
134 | t = a; | 133 | if (!BN_rshift1(a, a)) |
135 | a = b; | 134 | goto err; |
136 | b = t; | 135 | } else { |
137 | } | 136 | if (!BN_rshift1(a, a)) |
138 | } | 137 | goto err; |
139 | else /* a odd - b even */ | 138 | if (!BN_rshift1(b, b)) |
140 | { | 139 | goto err; |
141 | if (!BN_rshift1(b, b)) | 140 | shifts++; |
142 | goto err; | 141 | continue; |
143 | if (BN_cmp(a, b) < 0) { | ||
144 | t = a; | ||
145 | a = b; | ||
146 | b = t; | ||
147 | } | ||
148 | } | ||
149 | } | 142 | } |
150 | else /* a is even */ | 143 | |
151 | { | 144 | if (BN_cmp(a, b) < 0) { |
152 | if (BN_is_odd(b)) { | 145 | t = a; |
153 | if (!BN_rshift1(a, a)) | 146 | a = b; |
154 | goto err; | 147 | b = t; |
155 | if (BN_cmp(a, b) < 0) { | ||
156 | t = a; | ||
157 | a = b; | ||
158 | b = t; | ||
159 | } | ||
160 | } | ||
161 | else /* a even - b even */ | ||
162 | { | ||
163 | if (!BN_rshift1(a, a)) | ||
164 | goto err; | ||
165 | if (!BN_rshift1(b, b)) | ||
166 | goto err; | ||
167 | shifts++; | ||
168 | } | ||
169 | } | 148 | } |
170 | /* 0 <= b <= a */ | ||
171 | } | 149 | } |
172 | 150 | ||
173 | if (shifts) { | 151 | if (shifts) { |
174 | if (!BN_lshift(a, a, shifts)) | 152 | if (!BN_lshift(a, a, shifts)) |
175 | goto err; | 153 | goto err; |
176 | } | 154 | } |
177 | return (a); | 155 | |
156 | return a; | ||
178 | 157 | ||
179 | err: | 158 | err: |
180 | return (NULL); | 159 | return NULL; |
181 | } | 160 | } |
182 | 161 | ||
183 | int | 162 | int |