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