Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/bn/bn_gcd.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2020 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
51.6k
{
24
51.6k
    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
25
51.6k
    BIGNUM *ret = NULL;
26
51.6k
    int sign;
27
28
51.6k
    bn_check_top(a);
29
51.6k
    bn_check_top(n);
30
31
51.6k
    BN_CTX_start(ctx);
32
51.6k
    A = BN_CTX_get(ctx);
33
51.6k
    B = BN_CTX_get(ctx);
34
51.6k
    X = BN_CTX_get(ctx);
35
51.6k
    D = BN_CTX_get(ctx);
36
51.6k
    M = BN_CTX_get(ctx);
37
51.6k
    Y = BN_CTX_get(ctx);
38
51.6k
    T = BN_CTX_get(ctx);
39
51.6k
    if (T == NULL)
40
0
        goto err;
41
42
51.6k
    if (in == NULL)
43
0
        R = BN_new();
44
51.6k
    else
45
51.6k
        R = in;
46
51.6k
    if (R == NULL)
47
0
        goto err;
48
49
51.6k
    if (!BN_one(X))
50
0
        goto err;
51
51.6k
    BN_zero(Y);
52
51.6k
    if (BN_copy(B, a) == NULL)
53
0
        goto err;
54
51.6k
    if (BN_copy(A, n) == NULL)
55
0
        goto err;
56
51.6k
    A->neg = 0;
57
58
51.6k
    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
34.5k
        {
64
34.5k
            BIGNUM local_B;
65
34.5k
            bn_init(&local_B);
66
34.5k
            BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
67
34.5k
            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
34.5k
        }
71
34.5k
    }
72
51.6k
    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
20.9M
    while (!BN_is_zero(B)) {
82
20.8M
        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
20.8M
        {
95
20.8M
            BIGNUM local_A;
96
20.8M
            bn_init(&local_A);
97
20.8M
            BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
98
99
            /* (D, M) := (A/B, A%B) ... */
100
20.8M
            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
20.8M
        }
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
20.8M
        tmp = A; /* keep the BIGNUM object, the value does not
113
                  * matter */
114
115
        /* (A, B) := (B, A mod B) ... */
116
20.8M
        A = B;
117
20.8M
        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
20.8M
        if (!BN_mul(tmp, D, X, ctx))
141
0
            goto err;
142
20.8M
        if (!BN_add(tmp, tmp, Y))
143
0
            goto err;
144
145
20.8M
        M = Y; /* keep the BIGNUM object, the value does not
146
                * matter */
147
20.8M
        Y = X;
148
20.8M
        X = tmp;
149
20.8M
        sign = -sign;
150
20.8M
    }
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
51.6k
    if (sign < 0) {
161
15.5k
        if (!BN_sub(Y, n, Y))
162
0
            goto err;
163
15.5k
    }
164
    /* Now  Y*a  ==  A  (mod |n|).  */
165
166
51.6k
    if (BN_is_one(A)) {
167
        /* Y*a == 1  (mod |n|) */
168
51.2k
        if (!Y->neg && BN_ucmp(Y, n) < 0) {
169
51.2k
            if (!BN_copy(R, Y))
170
0
                goto err;
171
51.2k
        } else {
172
0
            if (!BN_nnmod(R, Y, n, ctx))
173
0
                goto err;
174
0
        }
175
51.2k
    } else {
176
470
        *pnoinv = 1;
177
        /* caller sets the BN_R_NO_INVERSE error */
178
470
        goto err;
179
470
    }
180
181
51.2k
    ret = R;
182
51.2k
    *pnoinv = 0;
183
184
51.6k
err:
185
51.6k
    if ((ret == NULL) && (in == NULL))
186
0
        BN_free(R);
187
51.6k
    BN_CTX_end(ctx);
188
51.6k
    bn_check_top(ret);
189
51.6k
    return ret;
190
51.2k
}
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.09M
{
200
1.09M
    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
201
1.09M
    BIGNUM *ret = NULL;
202
1.09M
    int sign;
203
204
    /* This is invalid input so we don't worry about constant time here */
205
1.09M
    if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
206
521
        *pnoinv = 1;
207
521
        return NULL;
208
521
    }
209
210
1.09M
    *pnoinv = 0;
211
212
1.09M
    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
213
1.09M
        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
214
51.6k
        return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv);
215
51.6k
    }
216
217
1.04M
    bn_check_top(a);
218
1.04M
    bn_check_top(n);
219
220
1.04M
    BN_CTX_start(ctx);
221
1.04M
    A = BN_CTX_get(ctx);
222
1.04M
    B = BN_CTX_get(ctx);
223
1.04M
    X = BN_CTX_get(ctx);
224
1.04M
    D = BN_CTX_get(ctx);
225
1.04M
    M = BN_CTX_get(ctx);
226
1.04M
    Y = BN_CTX_get(ctx);
227
1.04M
    T = BN_CTX_get(ctx);
228
1.04M
    if (T == NULL)
229
0
        goto err;
230
231
1.04M
    if (in == NULL)
232
0
        R = BN_new();
233
1.04M
    else
234
1.04M
        R = in;
235
1.04M
    if (R == NULL)
236
0
        goto err;
237
238
1.04M
    if (!BN_one(X))
239
0
        goto err;
240
1.04M
    BN_zero(Y);
241
1.04M
    if (BN_copy(B, a) == NULL)
242
0
        goto err;
243
1.04M
    if (BN_copy(A, n) == NULL)
244
0
        goto err;
245
1.04M
    A->neg = 0;
246
1.04M
    if (B->neg || (BN_ucmp(B, A) >= 0)) {
247
1.03M
        if (!BN_nnmod(B, B, A, ctx))
248
0
            goto err;
249
1.03M
    }
250
1.04M
    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
1.04M
    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
1.03M
        int shift;
267
268
63.6M
        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
62.6M
            shift = 0;
282
87.4M
            while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */
283
24.7M
                shift++;
284
285
24.7M
                if (BN_is_odd(X)) {
286
12.6M
                    if (!BN_uadd(X, X, n))
287
0
                        goto err;
288
12.6M
                }
289
                /*
290
                 * now X is even, so we can easily divide it by two
291
                 */
292
24.7M
                if (!BN_rshift1(X, X))
293
0
                    goto err;
294
24.7M
            }
295
62.6M
            if (shift > 0) {
296
23.4M
                if (!BN_rshift(B, B, shift))
297
0
                    goto err;
298
23.4M
            }
299
300
            /*
301
             * Same for A and Y.  Afterwards, (2) still holds.
302
             */
303
62.6M
            shift = 0;
304
101M
            while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */
305
39.3M
                shift++;
306
307
39.3M
                if (BN_is_odd(Y)) {
308
24.8M
                    if (!BN_uadd(Y, Y, n))
309
0
                        goto err;
310
24.8M
                }
311
                /* now Y is even */
312
39.3M
                if (!BN_rshift1(Y, Y))
313
0
                    goto err;
314
39.3M
            }
315
62.6M
            if (shift > 0) {
316
38.5M
                if (!BN_rshift(A, A, shift))
317
0
                    goto err;
318
38.5M
            }
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
62.6M
            if (BN_ucmp(B, A) >= 0) {
333
                /* -sign*(X + Y)*a == B - A  (mod |n|) */
334
24.0M
                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
24.0M
                if (!BN_usub(B, B, A))
341
0
                    goto err;
342
38.5M
            } else {
343
                /*  sign*(X + Y)*a == A - B  (mod |n|) */
344
38.5M
                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
38.5M
                if (!BN_usub(A, A, B))
350
0
                    goto err;
351
38.5M
            }
352
62.6M
        }
353
1.03M
    } else {
354
        /* general inversion algorithm */
355
356
306k
        while (!BN_is_zero(B)) {
357
298k
            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
298k
            if (BN_num_bits(A) == BN_num_bits(B)) {
367
64.9k
                if (!BN_one(D))
368
0
                    goto err;
369
64.9k
                if (!BN_sub(M, A, B))
370
0
                    goto err;
371
233k
            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
372
                /* A/B is 1, 2, or 3 */
373
105k
                if (!BN_lshift1(T, B))
374
0
                    goto err;
375
105k
                if (BN_ucmp(A, T) < 0) {
376
                    /* A < 2*B, so D=1 */
377
59.4k
                    if (!BN_one(D))
378
0
                        goto err;
379
59.4k
                    if (!BN_sub(M, A, B))
380
0
                        goto err;
381
59.4k
                } else {
382
                    /* A >= 2*B, so D=2 or D=3 */
383
45.5k
                    if (!BN_sub(M, A, T))
384
0
                        goto err;
385
45.5k
                    if (!BN_add(D, T, B))
386
0
                        goto err; /* use D (:= 3*B) as temp */
387
45.5k
                    if (BN_ucmp(A, D) < 0) {
388
                        /* A < 3*B, so D=2 */
389
37.3k
                        if (!BN_set_word(D, 2))
390
0
                            goto err;
391
                        /*
392
                         * M (= A - 2*B) already has the correct value
393
                         */
394
37.3k
                    } else {
395
                        /* only D=3 remains */
396
8.23k
                        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.23k
                        if (!BN_sub(M, M, B))
402
0
                            goto err;
403
8.23k
                    }
404
45.5k
                }
405
128k
            } else {
406
128k
                if (!BN_div(D, M, A, B, ctx))
407
0
                    goto err;
408
128k
            }
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
298k
            tmp = A; /* keep the BIGNUM object, the value does not matter */
418
419
            /* (A, B) := (B, A mod B) ... */
420
298k
            A = B;
421
298k
            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
298k
            if (BN_is_one(D)) {
448
124k
                if (!BN_add(tmp, X, Y))
449
0
                    goto err;
450
173k
            } else {
451
173k
                if (BN_is_word(D, 2)) {
452
53.2k
                    if (!BN_lshift1(tmp, X))
453
0
                        goto err;
454
120k
                } else if (BN_is_word(D, 4)) {
455
17.7k
                    if (!BN_lshift(tmp, X, 2))
456
0
                        goto err;
457
102k
                } else if (D->top == 1) {
458
102k
                    if (!BN_copy(tmp, X))
459
0
                        goto err;
460
102k
                    if (!BN_mul_word(tmp, D->d[0]))
461
0
                        goto err;
462
102k
                } else {
463
229
                    if (!BN_mul(tmp, D, X, ctx))
464
0
                        goto err;
465
229
                }
466
173k
                if (!BN_add(tmp, tmp, Y))
467
0
                    goto err;
468
173k
            }
469
470
298k
            M = Y; /* keep the BIGNUM object, the value does not matter */
471
298k
            Y = X;
472
298k
            X = tmp;
473
298k
            sign = -sign;
474
298k
        }
475
8.02k
    }
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
1.04M
    if (sign < 0) {
486
1.03M
        if (!BN_sub(Y, n, Y))
487
0
            goto err;
488
1.03M
    }
489
    /* Now  Y*a  ==  A  (mod |n|).  */
490
491
1.04M
    if (BN_is_one(A)) {
492
        /* Y*a == 1  (mod |n|) */
493
1.03M
        if (!Y->neg && BN_ucmp(Y, n) < 0) {
494
240k
            if (!BN_copy(R, Y))
495
0
                goto err;
496
794k
        } else {
497
794k
            if (!BN_nnmod(R, Y, n, ctx))
498
0
                goto err;
499
794k
        }
500
1.03M
    } else {
501
6.63k
        *pnoinv = 1;
502
6.63k
        goto err;
503
6.63k
    }
504
1.03M
    ret = R;
505
1.04M
err:
506
1.04M
    if ((ret == NULL) && (in == NULL))
507
0
        BN_free(R);
508
1.04M
    BN_CTX_end(ctx);
509
1.04M
    bn_check_top(ret);
510
1.04M
    return ret;
511
1.03M
}
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.07M
{
517
1.07M
    BN_CTX *new_ctx = NULL;
518
1.07M
    BIGNUM *rv;
519
1.07M
    int noinv = 0;
520
521
1.07M
    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_BN_LIB);
525
0
            return NULL;
526
0
        }
527
0
    }
528
529
1.07M
    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
530
1.07M
    if (noinv)
531
1.07M
        ERR_raise(ERR_LIB_BN, BN_R_NO_INVERSE);
532
1.07M
    BN_CTX_free(new_ctx);
533
1.07M
    return rv;
534
1.07M
}
535
536
/*
537
 * The numbers a and b are coprime if the only positive integer that is a
538
 * divisor of both of them is 1.
539
 * i.e. gcd(a,b) = 1.
540
 *
541
 * Coprimes have the property: b has a multiplicative inverse modulo a
542
 * i.e there is some value x such that bx = 1 (mod a).
543
 *
544
 * Testing the modulo inverse is currently much faster than the constant
545
 * time version of BN_gcd().
546
 */
547
int BN_are_coprime(BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
548
0
{
549
0
    int ret = 0;
550
0
    BIGNUM *tmp;
551
552
0
    BN_CTX_start(ctx);
553
0
    tmp = BN_CTX_get(ctx);
554
0
    if (tmp == NULL)
555
0
        goto end;
556
557
0
    ERR_set_mark();
558
0
    BN_set_flags(a, BN_FLG_CONSTTIME);
559
0
    ret = (BN_mod_inverse(tmp, a, b, ctx) != NULL);
560
    /* Clear any errors (an error is returned if there is no inverse) */
561
0
    ERR_pop_to_mark();
562
0
end:
563
0
    BN_CTX_end(ctx);
564
0
    return ret;
565
0
}
566
567
/*-
568
 * This function is based on the constant-time GCD work by Bernstein and Yang:
569
 * https://eprint.iacr.org/2019/266
570
 * Generalized fast GCD function to allow even inputs.
571
 * The algorithm first finds the shared powers of 2 between
572
 * the inputs, and removes them, reducing at least one of the
573
 * inputs to an odd value. Then it proceeds to calculate the GCD.
574
 * Before returning the resulting GCD, we take care of adding
575
 * back the powers of two removed at the beginning.
576
 * Note 1: we assume the bit length of both inputs is public information,
577
 * since access to top potentially leaks this information.
578
 */
579
int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
580
1.44k
{
581
1.44k
    BIGNUM *g, *temp = NULL;
582
1.44k
    BN_ULONG mask = 0;
583
1.44k
    int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0;
584
585
    /* Note 2: zero input corner cases are not constant-time since they are
586
     * handled immediately. An attacker can run an attack under this
587
     * assumption without the need of side-channel information. */
588
1.44k
    if (BN_is_zero(in_b)) {
589
5
        ret = BN_copy(r, in_a) != NULL;
590
5
        r->neg = 0;
591
5
        return ret;
592
5
    }
593
1.44k
    if (BN_is_zero(in_a)) {
594
9
        ret = BN_copy(r, in_b) != NULL;
595
9
        r->neg = 0;
596
9
        return ret;
597
9
    }
598
599
1.43k
    bn_check_top(in_a);
600
1.43k
    bn_check_top(in_b);
601
602
1.43k
    BN_CTX_start(ctx);
603
1.43k
    temp = BN_CTX_get(ctx);
604
1.43k
    g = BN_CTX_get(ctx);
605
606
    /* make r != 0, g != 0 even, so BN_rshift is not a potential nop */
607
1.43k
    if (g == NULL
608
1.43k
        || !BN_lshift1(g, in_b)
609
1.43k
        || !BN_lshift1(r, in_a))
610
0
        goto err;
611
612
    /* find shared powers of two, i.e. "shifts" >= 1 */
613
38.5k
    for (i = 0; i < r->dmax && i < g->dmax; i++) {
614
37.1k
        mask = ~(r->d[i] | g->d[i]);
615
2.41M
        for (j = 0; j < BN_BITS2; j++) {
616
2.37M
            bit &= mask;
617
2.37M
            shifts += bit;
618
2.37M
            mask >>= 1;
619
2.37M
        }
620
37.1k
    }
621
622
    /* subtract shared powers of two; shifts >= 1 */
623
1.43k
    if (!BN_rshift(r, r, shifts)
624
1.43k
        || !BN_rshift(g, g, shifts))
625
0
        goto err;
626
627
    /* expand to biggest nword, with room for a possible extra word */
628
1.43k
    top = 1 + ((r->top >= g->top) ? r->top : g->top);
629
1.43k
    if (bn_wexpand(r, top) == NULL
630
1.43k
        || bn_wexpand(g, top) == NULL
631
1.43k
        || bn_wexpand(temp, top) == NULL)
632
0
        goto err;
633
634
    /* re arrange inputs s.t. r is odd */
635
1.43k
    BN_consttime_swap((~r->d[0]) & 1, r, g, top);
636
637
    /* compute the number of iterations */
638
1.43k
    rlen = BN_num_bits(r);
639
1.43k
    glen = BN_num_bits(g);
640
1.43k
    m = 4 + 3 * ((rlen >= glen) ? rlen : glen);
641
642
11.7M
    for (i = 0; i < m; i++) {
643
        /* conditionally flip signs if delta is positive and g is odd */
644
11.7M
        cond = ((unsigned int)-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1
645
            /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
646
11.7M
            & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1)));
647
11.7M
        delta = (-cond & -delta) | ((cond - 1) & delta);
648
11.7M
        r->neg ^= cond;
649
        /* swap */
650
11.7M
        BN_consttime_swap(cond, r, g, top);
651
652
        /* elimination step */
653
11.7M
        delta++;
654
11.7M
        if (!BN_add(temp, g, r))
655
0
            goto err;
656
11.7M
        BN_consttime_swap(g->d[0] & 1 /* g is odd */
657
                /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
658
11.7M
                & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))),
659
11.7M
            g, temp, top);
660
11.7M
        if (!BN_rshift1(g, g))
661
0
            goto err;
662
11.7M
    }
663
664
    /* remove possible negative sign */
665
1.43k
    r->neg = 0;
666
    /* add powers of 2 removed, then correct the artificial shift */
667
1.43k
    if (!BN_lshift(r, r, shifts)
668
1.43k
        || !BN_rshift1(r, r))
669
0
        goto err;
670
671
1.43k
    ret = 1;
672
673
1.43k
err:
674
1.43k
    BN_CTX_end(ctx);
675
1.43k
    bn_check_top(r);
676
1.43k
    return ret;
677
1.43k
}