/src/openssl30/crypto/bn/bn_gcd.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include "bn_local.h" |
12 | | |
13 | | /* |
14 | | * bn_mod_inverse_no_branch is a special version of BN_mod_inverse. It does |
15 | | * not contain branches that may leak sensitive information. |
16 | | * |
17 | | * This is a static function, we ensure all callers in this file pass valid |
18 | | * arguments: all passed pointers here are non-NULL. |
19 | | */ |
20 | | static ossl_inline BIGNUM *bn_mod_inverse_no_branch(BIGNUM *in, |
21 | | const BIGNUM *a, const BIGNUM *n, |
22 | | BN_CTX *ctx, int *pnoinv) |
23 | 54.7k | { |
24 | 54.7k | BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; |
25 | 54.7k | BIGNUM *ret = NULL; |
26 | 54.7k | int sign; |
27 | | |
28 | 54.7k | bn_check_top(a); |
29 | 54.7k | bn_check_top(n); |
30 | | |
31 | 54.7k | BN_CTX_start(ctx); |
32 | 54.7k | A = BN_CTX_get(ctx); |
33 | 54.7k | B = BN_CTX_get(ctx); |
34 | 54.7k | X = BN_CTX_get(ctx); |
35 | 54.7k | D = BN_CTX_get(ctx); |
36 | 54.7k | M = BN_CTX_get(ctx); |
37 | 54.7k | Y = BN_CTX_get(ctx); |
38 | 54.7k | T = BN_CTX_get(ctx); |
39 | 54.7k | if (T == NULL) |
40 | 0 | goto err; |
41 | | |
42 | 54.7k | if (in == NULL) |
43 | 0 | R = BN_new(); |
44 | 54.7k | else |
45 | 54.7k | R = in; |
46 | 54.7k | if (R == NULL) |
47 | 0 | goto err; |
48 | | |
49 | 54.7k | if (!BN_one(X)) |
50 | 0 | goto err; |
51 | 54.7k | BN_zero(Y); |
52 | 54.7k | if (BN_copy(B, a) == NULL) |
53 | 0 | goto err; |
54 | 54.7k | if (BN_copy(A, n) == NULL) |
55 | 0 | goto err; |
56 | 54.7k | A->neg = 0; |
57 | | |
58 | 54.7k | if (B->neg || (BN_ucmp(B, A) >= 0)) { |
59 | | /* |
60 | | * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, |
61 | | * BN_div_no_branch will be called eventually. |
62 | | */ |
63 | 36.6k | { |
64 | 36.6k | BIGNUM local_B; |
65 | 36.6k | bn_init(&local_B); |
66 | 36.6k | BN_with_flags(&local_B, B, BN_FLG_CONSTTIME); |
67 | 36.6k | if (!BN_nnmod(B, &local_B, A, ctx)) |
68 | 0 | goto err; |
69 | | /* Ensure local_B goes out of scope before any further use of B */ |
70 | 36.6k | } |
71 | 36.6k | } |
72 | 54.7k | sign = -1; |
73 | | /*- |
74 | | * From B = a mod |n|, A = |n| it follows that |
75 | | * |
76 | | * 0 <= B < A, |
77 | | * -sign*X*a == B (mod |n|), |
78 | | * sign*Y*a == A (mod |n|). |
79 | | */ |
80 | | |
81 | 22.0M | while (!BN_is_zero(B)) { |
82 | 21.9M | BIGNUM *tmp; |
83 | | |
84 | | /*- |
85 | | * 0 < B < A, |
86 | | * (*) -sign*X*a == B (mod |n|), |
87 | | * sign*Y*a == A (mod |n|) |
88 | | */ |
89 | | |
90 | | /* |
91 | | * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked, |
92 | | * BN_div_no_branch will be called eventually. |
93 | | */ |
94 | 21.9M | { |
95 | 21.9M | BIGNUM local_A; |
96 | 21.9M | bn_init(&local_A); |
97 | 21.9M | BN_with_flags(&local_A, A, BN_FLG_CONSTTIME); |
98 | | |
99 | | /* (D, M) := (A/B, A%B) ... */ |
100 | 21.9M | if (!BN_div(D, M, &local_A, B, ctx)) |
101 | 0 | goto err; |
102 | | /* Ensure local_A goes out of scope before any further use of A */ |
103 | 21.9M | } |
104 | | |
105 | | /*- |
106 | | * Now |
107 | | * A = D*B + M; |
108 | | * thus we have |
109 | | * (**) sign*Y*a == D*B + M (mod |n|). |
110 | | */ |
111 | | |
112 | 21.9M | tmp = A; /* keep the BIGNUM object, the value does not |
113 | | * matter */ |
114 | | |
115 | | /* (A, B) := (B, A mod B) ... */ |
116 | 21.9M | A = B; |
117 | 21.9M | B = M; |
118 | | /* ... so we have 0 <= B < A again */ |
119 | | |
120 | | /*- |
121 | | * Since the former M is now B and the former B is now A, |
122 | | * (**) translates into |
123 | | * sign*Y*a == D*A + B (mod |n|), |
124 | | * i.e. |
125 | | * sign*Y*a - D*A == B (mod |n|). |
126 | | * Similarly, (*) translates into |
127 | | * -sign*X*a == A (mod |n|). |
128 | | * |
129 | | * Thus, |
130 | | * sign*Y*a + D*sign*X*a == B (mod |n|), |
131 | | * i.e. |
132 | | * sign*(Y + D*X)*a == B (mod |n|). |
133 | | * |
134 | | * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at |
135 | | * -sign*X*a == B (mod |n|), |
136 | | * sign*Y*a == A (mod |n|). |
137 | | * Note that X and Y stay non-negative all the time. |
138 | | */ |
139 | | |
140 | 21.9M | if (!BN_mul(tmp, D, X, ctx)) |
141 | 0 | goto err; |
142 | 21.9M | if (!BN_add(tmp, tmp, Y)) |
143 | 0 | goto err; |
144 | | |
145 | 21.9M | M = Y; /* keep the BIGNUM object, the value does not |
146 | | * matter */ |
147 | 21.9M | Y = X; |
148 | 21.9M | X = tmp; |
149 | 21.9M | sign = -sign; |
150 | 21.9M | } |
151 | | |
152 | | /*- |
153 | | * The while loop (Euclid's algorithm) ends when |
154 | | * A == gcd(a,n); |
155 | | * we have |
156 | | * sign*Y*a == A (mod |n|), |
157 | | * where Y is non-negative. |
158 | | */ |
159 | | |
160 | 54.7k | if (sign < 0) { |
161 | 17.2k | if (!BN_sub(Y, n, Y)) |
162 | 0 | goto err; |
163 | 17.2k | } |
164 | | /* Now Y*a == A (mod |n|). */ |
165 | | |
166 | 54.7k | if (BN_is_one(A)) { |
167 | | /* Y*a == 1 (mod |n|) */ |
168 | 54.3k | if (!Y->neg && BN_ucmp(Y, n) < 0) { |
169 | 54.3k | if (!BN_copy(R, Y)) |
170 | 0 | goto err; |
171 | 54.3k | } else { |
172 | 0 | if (!BN_nnmod(R, Y, n, ctx)) |
173 | 0 | goto err; |
174 | 0 | } |
175 | 54.3k | } else { |
176 | 464 | *pnoinv = 1; |
177 | | /* caller sets the BN_R_NO_INVERSE error */ |
178 | 464 | goto err; |
179 | 464 | } |
180 | | |
181 | 54.3k | ret = R; |
182 | 54.3k | *pnoinv = 0; |
183 | | |
184 | 54.7k | err: |
185 | 54.7k | if ((ret == NULL) && (in == NULL)) |
186 | 0 | BN_free(R); |
187 | 54.7k | BN_CTX_end(ctx); |
188 | 54.7k | bn_check_top(ret); |
189 | 54.7k | return ret; |
190 | 54.3k | } |
191 | | |
192 | | /* |
193 | | * This is an internal function, we assume all callers pass valid arguments: |
194 | | * all pointers passed here are assumed non-NULL. |
195 | | */ |
196 | | BIGNUM *int_bn_mod_inverse(BIGNUM *in, |
197 | | const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, |
198 | | int *pnoinv) |
199 | 1.04M | { |
200 | 1.04M | BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; |
201 | 1.04M | BIGNUM *ret = NULL; |
202 | 1.04M | int sign; |
203 | | |
204 | | /* This is invalid input so we don't worry about constant time here */ |
205 | 1.04M | if (BN_abs_is_word(n, 1) || BN_is_zero(n)) { |
206 | 570 | *pnoinv = 1; |
207 | 570 | return NULL; |
208 | 570 | } |
209 | | |
210 | 1.04M | *pnoinv = 0; |
211 | | |
212 | 1.04M | if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) |
213 | 1.04M | || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) { |
214 | 54.7k | return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv); |
215 | 54.7k | } |
216 | | |
217 | 990k | bn_check_top(a); |
218 | 990k | bn_check_top(n); |
219 | | |
220 | 990k | BN_CTX_start(ctx); |
221 | 990k | A = BN_CTX_get(ctx); |
222 | 990k | B = BN_CTX_get(ctx); |
223 | 990k | X = BN_CTX_get(ctx); |
224 | 990k | D = BN_CTX_get(ctx); |
225 | 990k | M = BN_CTX_get(ctx); |
226 | 990k | Y = BN_CTX_get(ctx); |
227 | 990k | T = BN_CTX_get(ctx); |
228 | 990k | if (T == NULL) |
229 | 0 | goto err; |
230 | | |
231 | 990k | if (in == NULL) |
232 | 0 | R = BN_new(); |
233 | 990k | else |
234 | 990k | R = in; |
235 | 990k | if (R == NULL) |
236 | 0 | goto err; |
237 | | |
238 | 990k | if (!BN_one(X)) |
239 | 0 | goto err; |
240 | 990k | BN_zero(Y); |
241 | 990k | if (BN_copy(B, a) == NULL) |
242 | 0 | goto err; |
243 | 990k | if (BN_copy(A, n) == NULL) |
244 | 0 | goto err; |
245 | 990k | A->neg = 0; |
246 | 990k | if (B->neg || (BN_ucmp(B, A) >= 0)) { |
247 | 987k | if (!BN_nnmod(B, B, A, ctx)) |
248 | 0 | goto err; |
249 | 987k | } |
250 | 990k | sign = -1; |
251 | | /*- |
252 | | * From B = a mod |n|, A = |n| it follows that |
253 | | * |
254 | | * 0 <= B < A, |
255 | | * -sign*X*a == B (mod |n|), |
256 | | * sign*Y*a == A (mod |n|). |
257 | | */ |
258 | | |
259 | 990k | if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) { |
260 | | /* |
261 | | * Binary inversion algorithm; requires odd modulus. This is faster |
262 | | * than the general algorithm if the modulus is sufficiently small |
263 | | * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit |
264 | | * systems) |
265 | | */ |
266 | 981k | int shift; |
267 | | |
268 | 60.4M | while (!BN_is_zero(B)) { |
269 | | /*- |
270 | | * 0 < B < |n|, |
271 | | * 0 < A <= |n|, |
272 | | * (1) -sign*X*a == B (mod |n|), |
273 | | * (2) sign*Y*a == A (mod |n|) |
274 | | */ |
275 | | |
276 | | /* |
277 | | * Now divide B by the maximum possible power of two in the |
278 | | * integers, and divide X by the same value mod |n|. When we're |
279 | | * done, (1) still holds. |
280 | | */ |
281 | 59.4M | shift = 0; |
282 | 82.8M | while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */ |
283 | 23.3M | shift++; |
284 | | |
285 | 23.3M | if (BN_is_odd(X)) { |
286 | 11.8M | if (!BN_uadd(X, X, n)) |
287 | 0 | goto err; |
288 | 11.8M | } |
289 | | /* |
290 | | * now X is even, so we can easily divide it by two |
291 | | */ |
292 | 23.3M | if (!BN_rshift1(X, X)) |
293 | 0 | goto err; |
294 | 23.3M | } |
295 | 59.4M | if (shift > 0) { |
296 | 22.0M | if (!BN_rshift(B, B, shift)) |
297 | 0 | goto err; |
298 | 22.0M | } |
299 | | |
300 | | /* |
301 | | * Same for A and Y. Afterwards, (2) still holds. |
302 | | */ |
303 | 59.4M | shift = 0; |
304 | 97.0M | while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */ |
305 | 37.5M | shift++; |
306 | | |
307 | 37.5M | if (BN_is_odd(Y)) { |
308 | 24.0M | if (!BN_uadd(Y, Y, n)) |
309 | 0 | goto err; |
310 | 24.0M | } |
311 | | /* now Y is even */ |
312 | 37.5M | if (!BN_rshift1(Y, Y)) |
313 | 0 | goto err; |
314 | 37.5M | } |
315 | 59.4M | if (shift > 0) { |
316 | 36.8M | if (!BN_rshift(A, A, shift)) |
317 | 0 | goto err; |
318 | 36.8M | } |
319 | | |
320 | | /*- |
321 | | * We still have (1) and (2). |
322 | | * Both A and B are odd. |
323 | | * The following computations ensure that |
324 | | * |
325 | | * 0 <= B < |n|, |
326 | | * 0 < A < |n|, |
327 | | * (1) -sign*X*a == B (mod |n|), |
328 | | * (2) sign*Y*a == A (mod |n|), |
329 | | * |
330 | | * and that either A or B is even in the next iteration. |
331 | | */ |
332 | 59.4M | if (BN_ucmp(B, A) >= 0) { |
333 | | /* -sign*(X + Y)*a == B - A (mod |n|) */ |
334 | 22.6M | if (!BN_uadd(X, X, Y)) |
335 | 0 | goto err; |
336 | | /* |
337 | | * NB: we could use BN_mod_add_quick(X, X, Y, n), but that |
338 | | * actually makes the algorithm slower |
339 | | */ |
340 | 22.6M | if (!BN_usub(B, B, A)) |
341 | 0 | goto err; |
342 | 36.8M | } else { |
343 | | /* sign*(X + Y)*a == A - B (mod |n|) */ |
344 | 36.8M | if (!BN_uadd(Y, Y, X)) |
345 | 0 | goto err; |
346 | | /* |
347 | | * as above, BN_mod_add_quick(Y, Y, X, n) would slow things down |
348 | | */ |
349 | 36.8M | if (!BN_usub(A, A, B)) |
350 | 0 | goto err; |
351 | 36.8M | } |
352 | 59.4M | } |
353 | 981k | } else { |
354 | | /* general inversion algorithm */ |
355 | | |
356 | 319k | while (!BN_is_zero(B)) { |
357 | 311k | BIGNUM *tmp; |
358 | | |
359 | | /*- |
360 | | * 0 < B < A, |
361 | | * (*) -sign*X*a == B (mod |n|), |
362 | | * sign*Y*a == A (mod |n|) |
363 | | */ |
364 | | |
365 | | /* (D, M) := (A/B, A%B) ... */ |
366 | 311k | if (BN_num_bits(A) == BN_num_bits(B)) { |
367 | 68.1k | if (!BN_one(D)) |
368 | 0 | goto err; |
369 | 68.1k | if (!BN_sub(M, A, B)) |
370 | 0 | goto err; |
371 | 243k | } else if (BN_num_bits(A) == BN_num_bits(B) + 1) { |
372 | | /* A/B is 1, 2, or 3 */ |
373 | 108k | if (!BN_lshift1(T, B)) |
374 | 0 | goto err; |
375 | 108k | if (BN_ucmp(A, T) < 0) { |
376 | | /* A < 2*B, so D=1 */ |
377 | 61.7k | if (!BN_one(D)) |
378 | 0 | goto err; |
379 | 61.7k | if (!BN_sub(M, A, B)) |
380 | 0 | goto err; |
381 | 61.7k | } else { |
382 | | /* A >= 2*B, so D=2 or D=3 */ |
383 | 46.8k | if (!BN_sub(M, A, T)) |
384 | 0 | goto err; |
385 | 46.8k | if (!BN_add(D, T, B)) |
386 | 0 | goto err; /* use D (:= 3*B) as temp */ |
387 | 46.8k | if (BN_ucmp(A, D) < 0) { |
388 | | /* A < 3*B, so D=2 */ |
389 | 38.6k | if (!BN_set_word(D, 2)) |
390 | 0 | goto err; |
391 | | /* |
392 | | * M (= A - 2*B) already has the correct value |
393 | | */ |
394 | 38.6k | } else { |
395 | | /* only D=3 remains */ |
396 | 8.18k | if (!BN_set_word(D, 3)) |
397 | 0 | goto err; |
398 | | /* |
399 | | * currently M = A - 2*B, but we need M = A - 3*B |
400 | | */ |
401 | 8.18k | if (!BN_sub(M, M, B)) |
402 | 0 | goto err; |
403 | 8.18k | } |
404 | 46.8k | } |
405 | 134k | } else { |
406 | 134k | if (!BN_div(D, M, A, B, ctx)) |
407 | 0 | goto err; |
408 | 134k | } |
409 | | |
410 | | /*- |
411 | | * Now |
412 | | * A = D*B + M; |
413 | | * thus we have |
414 | | * (**) sign*Y*a == D*B + M (mod |n|). |
415 | | */ |
416 | | |
417 | 311k | tmp = A; /* keep the BIGNUM object, the value does not matter */ |
418 | | |
419 | | /* (A, B) := (B, A mod B) ... */ |
420 | 311k | A = B; |
421 | 311k | B = M; |
422 | | /* ... so we have 0 <= B < A again */ |
423 | | |
424 | | /*- |
425 | | * Since the former M is now B and the former B is now A, |
426 | | * (**) translates into |
427 | | * sign*Y*a == D*A + B (mod |n|), |
428 | | * i.e. |
429 | | * sign*Y*a - D*A == B (mod |n|). |
430 | | * Similarly, (*) translates into |
431 | | * -sign*X*a == A (mod |n|). |
432 | | * |
433 | | * Thus, |
434 | | * sign*Y*a + D*sign*X*a == B (mod |n|), |
435 | | * i.e. |
436 | | * sign*(Y + D*X)*a == B (mod |n|). |
437 | | * |
438 | | * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at |
439 | | * -sign*X*a == B (mod |n|), |
440 | | * sign*Y*a == A (mod |n|). |
441 | | * Note that X and Y stay non-negative all the time. |
442 | | */ |
443 | | |
444 | | /* |
445 | | * most of the time D is very small, so we can optimize tmp := D*X+Y |
446 | | */ |
447 | 311k | if (BN_is_one(D)) { |
448 | 129k | if (!BN_add(tmp, X, Y)) |
449 | 0 | goto err; |
450 | 181k | } else { |
451 | 181k | if (BN_is_word(D, 2)) { |
452 | 55.2k | if (!BN_lshift1(tmp, X)) |
453 | 0 | goto err; |
454 | 126k | } else if (BN_is_word(D, 4)) { |
455 | 18.4k | if (!BN_lshift(tmp, X, 2)) |
456 | 0 | goto err; |
457 | 107k | } else if (D->top == 1) { |
458 | 107k | if (!BN_copy(tmp, X)) |
459 | 0 | goto err; |
460 | 107k | if (!BN_mul_word(tmp, D->d[0])) |
461 | 0 | goto err; |
462 | 107k | } else { |
463 | 216 | if (!BN_mul(tmp, D, X, ctx)) |
464 | 0 | goto err; |
465 | 216 | } |
466 | 181k | if (!BN_add(tmp, tmp, Y)) |
467 | 0 | goto err; |
468 | 181k | } |
469 | | |
470 | 311k | M = Y; /* keep the BIGNUM object, the value does not matter */ |
471 | 311k | Y = X; |
472 | 311k | X = tmp; |
473 | 311k | sign = -sign; |
474 | 311k | } |
475 | 8.46k | } |
476 | | |
477 | | /*- |
478 | | * The while loop (Euclid's algorithm) ends when |
479 | | * A == gcd(a,n); |
480 | | * we have |
481 | | * sign*Y*a == A (mod |n|), |
482 | | * where Y is non-negative. |
483 | | */ |
484 | | |
485 | 990k | if (sign < 0) { |
486 | 986k | if (!BN_sub(Y, n, Y)) |
487 | 0 | goto err; |
488 | 986k | } |
489 | | /* Now Y*a == A (mod |n|). */ |
490 | | |
491 | 990k | if (BN_is_one(A)) { |
492 | | /* Y*a == 1 (mod |n|) */ |
493 | 983k | if (!Y->neg && BN_ucmp(Y, n) < 0) { |
494 | 241k | if (!BN_copy(R, Y)) |
495 | 0 | goto err; |
496 | 741k | } else { |
497 | 741k | if (!BN_nnmod(R, Y, n, ctx)) |
498 | 0 | goto err; |
499 | 741k | } |
500 | 983k | } else { |
501 | 6.95k | *pnoinv = 1; |
502 | 6.95k | goto err; |
503 | 6.95k | } |
504 | 983k | ret = R; |
505 | 990k | err: |
506 | 990k | if ((ret == NULL) && (in == NULL)) |
507 | 0 | BN_free(R); |
508 | 990k | BN_CTX_end(ctx); |
509 | 990k | bn_check_top(ret); |
510 | 990k | return ret; |
511 | 983k | } |
512 | | |
513 | | /* solves ax == 1 (mod n) */ |
514 | | BIGNUM *BN_mod_inverse(BIGNUM *in, |
515 | | const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) |
516 | 1.02M | { |
517 | 1.02M | BN_CTX *new_ctx = NULL; |
518 | 1.02M | BIGNUM *rv; |
519 | 1.02M | int noinv = 0; |
520 | | |
521 | 1.02M | if (ctx == NULL) { |
522 | 0 | ctx = new_ctx = BN_CTX_new_ex(NULL); |
523 | 0 | if (ctx == NULL) { |
524 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
525 | 0 | return NULL; |
526 | 0 | } |
527 | 0 | } |
528 | | |
529 | 1.02M | rv = int_bn_mod_inverse(in, a, n, ctx, &noinv); |
530 | 1.02M | if (noinv) |
531 | 1.02M | ERR_raise(ERR_LIB_BN, BN_R_NO_INVERSE); |
532 | 1.02M | BN_CTX_free(new_ctx); |
533 | 1.02M | return rv; |
534 | 1.02M | } |
535 | | |
536 | | /*- |
537 | | * This function is based on the constant-time GCD work by Bernstein and Yang: |
538 | | * https://eprint.iacr.org/2019/266 |
539 | | * Generalized fast GCD function to allow even inputs. |
540 | | * The algorithm first finds the shared powers of 2 between |
541 | | * the inputs, and removes them, reducing at least one of the |
542 | | * inputs to an odd value. Then it proceeds to calculate the GCD. |
543 | | * Before returning the resulting GCD, we take care of adding |
544 | | * back the powers of two removed at the beginning. |
545 | | * Note 1: we assume the bit length of both inputs is public information, |
546 | | * since access to top potentially leaks this information. |
547 | | */ |
548 | | int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) |
549 | 1.50k | { |
550 | 1.50k | BIGNUM *g, *temp = NULL; |
551 | 1.50k | BN_ULONG mask = 0; |
552 | 1.50k | int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0; |
553 | | |
554 | | /* Note 2: zero input corner cases are not constant-time since they are |
555 | | * handled immediately. An attacker can run an attack under this |
556 | | * assumption without the need of side-channel information. */ |
557 | 1.50k | if (BN_is_zero(in_b)) { |
558 | 4 | ret = BN_copy(r, in_a) != NULL; |
559 | 4 | r->neg = 0; |
560 | 4 | return ret; |
561 | 4 | } |
562 | 1.49k | if (BN_is_zero(in_a)) { |
563 | 9 | ret = BN_copy(r, in_b) != NULL; |
564 | 9 | r->neg = 0; |
565 | 9 | return ret; |
566 | 9 | } |
567 | | |
568 | 1.49k | bn_check_top(in_a); |
569 | 1.49k | bn_check_top(in_b); |
570 | | |
571 | 1.49k | BN_CTX_start(ctx); |
572 | 1.49k | temp = BN_CTX_get(ctx); |
573 | 1.49k | g = BN_CTX_get(ctx); |
574 | | |
575 | | /* make r != 0, g != 0 even, so BN_rshift is not a potential nop */ |
576 | 1.49k | if (g == NULL |
577 | 1.49k | || !BN_lshift1(g, in_b) |
578 | 1.49k | || !BN_lshift1(r, in_a)) |
579 | 0 | goto err; |
580 | | |
581 | | /* find shared powers of two, i.e. "shifts" >= 1 */ |
582 | 40.6k | for (i = 0; i < r->dmax && i < g->dmax; i++) { |
583 | 39.1k | mask = ~(r->d[i] | g->d[i]); |
584 | 2.54M | for (j = 0; j < BN_BITS2; j++) { |
585 | 2.50M | bit &= mask; |
586 | 2.50M | shifts += bit; |
587 | 2.50M | mask >>= 1; |
588 | 2.50M | } |
589 | 39.1k | } |
590 | | |
591 | | /* subtract shared powers of two; shifts >= 1 */ |
592 | 1.49k | if (!BN_rshift(r, r, shifts) |
593 | 1.49k | || !BN_rshift(g, g, shifts)) |
594 | 0 | goto err; |
595 | | |
596 | | /* expand to biggest nword, with room for a possible extra word */ |
597 | 1.49k | top = 1 + ((r->top >= g->top) ? r->top : g->top); |
598 | 1.49k | if (bn_wexpand(r, top) == NULL |
599 | 1.49k | || bn_wexpand(g, top) == NULL |
600 | 1.49k | || bn_wexpand(temp, top) == NULL) |
601 | 0 | goto err; |
602 | | |
603 | | /* re arrange inputs s.t. r is odd */ |
604 | 1.49k | BN_consttime_swap((~r->d[0]) & 1, r, g, top); |
605 | | |
606 | | /* compute the number of iterations */ |
607 | 1.49k | rlen = BN_num_bits(r); |
608 | 1.49k | glen = BN_num_bits(g); |
609 | 1.49k | m = 4 + 3 * ((rlen >= glen) ? rlen : glen); |
610 | | |
611 | 11.6M | for (i = 0; i < m; i++) { |
612 | | /* conditionally flip signs if delta is positive and g is odd */ |
613 | 11.6M | cond = ((unsigned int)-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1 |
614 | | /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ |
615 | 11.6M | & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))); |
616 | 11.6M | delta = (-cond & -delta) | ((cond - 1) & delta); |
617 | 11.6M | r->neg ^= cond; |
618 | | /* swap */ |
619 | 11.6M | BN_consttime_swap(cond, r, g, top); |
620 | | |
621 | | /* elimination step */ |
622 | 11.6M | delta++; |
623 | 11.6M | if (!BN_add(temp, g, r)) |
624 | 0 | goto err; |
625 | 11.6M | BN_consttime_swap(g->d[0] & 1 /* g is odd */ |
626 | | /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ |
627 | 11.6M | & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))), |
628 | 11.6M | g, temp, top); |
629 | 11.6M | if (!BN_rshift1(g, g)) |
630 | 0 | goto err; |
631 | 11.6M | } |
632 | | |
633 | | /* remove possible negative sign */ |
634 | 1.49k | r->neg = 0; |
635 | | /* add powers of 2 removed, then correct the artificial shift */ |
636 | 1.49k | if (!BN_lshift(r, r, shifts) |
637 | 1.49k | || !BN_rshift1(r, r)) |
638 | 0 | goto err; |
639 | | |
640 | 1.49k | ret = 1; |
641 | | |
642 | 1.49k | err: |
643 | 1.49k | BN_CTX_end(ctx); |
644 | 1.49k | bn_check_top(r); |
645 | 1.49k | return ret; |
646 | 1.49k | } |