Coverage Report

Created: 2026-04-01 06:39

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