Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/bn/bn_gcd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
21
BIGNUM *bn_mod_inverse_no_branch(BIGNUM *in,
22
                                 const BIGNUM *a, const BIGNUM *n,
23
                                 BN_CTX *ctx, int *pnoinv)
24
344
{
25
344
    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
26
344
    BIGNUM *ret = NULL;
27
344
    int sign;
28
29
344
    bn_check_top(a);
30
344
    bn_check_top(n);
31
32
344
    BN_CTX_start(ctx);
33
344
    A = BN_CTX_get(ctx);
34
344
    B = BN_CTX_get(ctx);
35
344
    X = BN_CTX_get(ctx);
36
344
    D = BN_CTX_get(ctx);
37
344
    M = BN_CTX_get(ctx);
38
344
    Y = BN_CTX_get(ctx);
39
344
    T = BN_CTX_get(ctx);
40
344
    if (T == NULL)
41
0
        goto err;
42
43
344
    if (in == NULL)
44
0
        R = BN_new();
45
344
    else
46
344
        R = in;
47
344
    if (R == NULL)
48
0
        goto err;
49
50
344
    if (!BN_one(X))
51
0
        goto err;
52
344
    BN_zero(Y);
53
344
    if (BN_copy(B, a) == NULL)
54
0
        goto err;
55
344
    if (BN_copy(A, n) == NULL)
56
0
        goto err;
57
344
    A->neg = 0;
58
59
344
    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
0
         {
65
0
            BIGNUM local_B;
66
0
            bn_init(&local_B);
67
0
            BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
68
0
            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
0
        }
72
0
    }
73
344
    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
36.9k
    while (!BN_is_zero(B)) {
83
36.6k
        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
36.6k
        {
96
36.6k
            BIGNUM local_A;
97
36.6k
            bn_init(&local_A);
98
36.6k
            BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
99
100
            /* (D, M) := (A/B, A%B) ... */
101
36.6k
            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
36.6k
        }
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
36.6k
        tmp = A;                /* keep the BIGNUM object, the value does not
114
                                 * matter */
115
116
        /* (A, B) := (B, A mod B) ... */
117
36.6k
        A = B;
118
36.6k
        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
36.6k
        if (!BN_mul(tmp, D, X, ctx))
142
0
            goto err;
143
36.6k
        if (!BN_add(tmp, tmp, Y))
144
0
            goto err;
145
146
36.6k
        M = Y;                  /* keep the BIGNUM object, the value does not
147
                                 * matter */
148
36.6k
        Y = X;
149
36.6k
        X = tmp;
150
36.6k
        sign = -sign;
151
36.6k
    }
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
344
    if (sign < 0) {
162
182
        if (!BN_sub(Y, n, Y))
163
0
            goto err;
164
182
    }
165
    /* Now  Y*a  ==  A  (mod |n|).  */
166
167
344
    if (BN_is_one(A)) {
168
        /* Y*a == 1  (mod |n|) */
169
339
        if (!Y->neg && BN_ucmp(Y, n) < 0) {
170
339
            if (!BN_copy(R, Y))
171
0
                goto err;
172
339
        } else {
173
0
            if (!BN_nnmod(R, Y, n, ctx))
174
0
                goto err;
175
0
        }
176
339
    } else {
177
5
        *pnoinv = 1;
178
        /* caller sets the BN_R_NO_INVERSE error */
179
5
        goto err;
180
5
    }
181
182
339
    ret = R;
183
339
    *pnoinv = 0;
184
185
344
 err:
186
344
    if ((ret == NULL) && (in == NULL))
187
0
        BN_free(R);
188
344
    BN_CTX_end(ctx);
189
344
    bn_check_top(ret);
190
344
    return ret;
191
339
}
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
6.12k
{
201
6.12k
    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
202
6.12k
    BIGNUM *ret = NULL;
203
6.12k
    int sign;
204
205
    /* This is invalid input so we don't worry about constant time here */
206
6.12k
    if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
207
3
        *pnoinv = 1;
208
3
        return NULL;
209
3
    }
210
211
6.12k
    *pnoinv = 0;
212
213
6.12k
    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
214
6.12k
        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
215
344
        return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv);
216
344
    }
217
218
5.77k
    bn_check_top(a);
219
5.77k
    bn_check_top(n);
220
221
5.77k
    BN_CTX_start(ctx);
222
5.77k
    A = BN_CTX_get(ctx);
223
5.77k
    B = BN_CTX_get(ctx);
224
5.77k
    X = BN_CTX_get(ctx);
225
5.77k
    D = BN_CTX_get(ctx);
226
5.77k
    M = BN_CTX_get(ctx);
227
5.77k
    Y = BN_CTX_get(ctx);
228
5.77k
    T = BN_CTX_get(ctx);
229
5.77k
    if (T == NULL)
230
0
        goto err;
231
232
5.77k
    if (in == NULL)
233
0
        R = BN_new();
234
5.77k
    else
235
5.77k
        R = in;
236
5.77k
    if (R == NULL)
237
0
        goto err;
238
239
5.77k
    if (!BN_one(X))
240
0
        goto err;
241
5.77k
    BN_zero(Y);
242
5.77k
    if (BN_copy(B, a) == NULL)
243
0
        goto err;
244
5.77k
    if (BN_copy(A, n) == NULL)
245
0
        goto err;
246
5.77k
    A->neg = 0;
247
5.77k
    if (B->neg || (BN_ucmp(B, A) >= 0)) {
248
5.77k
        if (!BN_nnmod(B, B, A, ctx))
249
0
            goto err;
250
5.77k
    }
251
5.77k
    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
5.77k
    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
5.68k
        int shift;
268
269
311k
        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
305k
            shift = 0;
283
427k
            while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */
284
121k
                shift++;
285
286
121k
                if (BN_is_odd(X)) {
287
60.1k
                    if (!BN_uadd(X, X, n))
288
0
                        goto err;
289
60.1k
                }
290
                /*
291
                 * now X is even, so we can easily divide it by two
292
                 */
293
121k
                if (!BN_rshift1(X, X))
294
0
                    goto err;
295
121k
            }
296
305k
            if (shift > 0) {
297
112k
                if (!BN_rshift(B, B, shift))
298
0
                    goto err;
299
112k
            }
300
301
            /*
302
             * Same for A and Y.  Afterwards, (2) still holds.
303
             */
304
305k
            shift = 0;
305
503k
            while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */
306
197k
                shift++;
307
308
197k
                if (BN_is_odd(Y)) {
309
132k
                    if (!BN_uadd(Y, Y, n))
310
0
                        goto err;
311
132k
                }
312
                /* now Y is even */
313
197k
                if (!BN_rshift1(Y, Y))
314
0
                    goto err;
315
197k
            }
316
305k
            if (shift > 0) {
317
188k
                if (!BN_rshift(A, A, shift))
318
0
                    goto err;
319
188k
            }
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
305k
            if (BN_ucmp(B, A) >= 0) {
334
                /* -sign*(X + Y)*a == B - A  (mod |n|) */
335
117k
                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
117k
                if (!BN_usub(B, B, A))
342
0
                    goto err;
343
188k
            } else {
344
                /*  sign*(X + Y)*a == A - B  (mod |n|) */
345
188k
                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
188k
                if (!BN_usub(A, A, B))
351
0
                    goto err;
352
188k
            }
353
305k
        }
354
5.68k
    } else {
355
        /* general inversion algorithm */
356
357
1.37k
        while (!BN_is_zero(B)) {
358
1.28k
            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
1.28k
            if (BN_num_bits(A) == BN_num_bits(B)) {
368
232
                if (!BN_one(D))
369
0
                    goto err;
370
232
                if (!BN_sub(M, A, B))
371
0
                    goto err;
372
1.05k
            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
373
                /* A/B is 1, 2, or 3 */
374
490
                if (!BN_lshift1(T, B))
375
0
                    goto err;
376
490
                if (BN_ucmp(A, T) < 0) {
377
                    /* A < 2*B, so D=1 */
378
294
                    if (!BN_one(D))
379
0
                        goto err;
380
294
                    if (!BN_sub(M, A, B))
381
0
                        goto err;
382
294
                } else {
383
                    /* A >= 2*B, so D=2 or D=3 */
384
196
                    if (!BN_sub(M, A, T))
385
0
                        goto err;
386
196
                    if (!BN_add(D, T, B))
387
0
                        goto err; /* use D (:= 3*B) as temp */
388
196
                    if (BN_ucmp(A, D) < 0) {
389
                        /* A < 3*B, so D=2 */
390
143
                        if (!BN_set_word(D, 2))
391
0
                            goto err;
392
                        /*
393
                         * M (= A - 2*B) already has the correct value
394
                         */
395
143
                    } else {
396
                        /* only D=3 remains */
397
53
                        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
53
                        if (!BN_sub(M, M, B))
403
0
                            goto err;
404
53
                    }
405
196
                }
406
563
            } else {
407
563
                if (!BN_div(D, M, A, B, ctx))
408
0
                    goto err;
409
563
            }
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
1.28k
            tmp = A;    /* keep the BIGNUM object, the value does not matter */
419
420
            /* (A, B) := (B, A mod B) ... */
421
1.28k
            A = B;
422
1.28k
            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
1.28k
            if (BN_is_one(D)) {
449
526
                if (!BN_add(tmp, X, Y))
450
0
                    goto err;
451
759
            } else {
452
759
                if (BN_is_word(D, 2)) {
453
211
                    if (!BN_lshift1(tmp, X))
454
0
                        goto err;
455
548
                } else if (BN_is_word(D, 4)) {
456
77
                    if (!BN_lshift(tmp, X, 2))
457
0
                        goto err;
458
471
                } else if (D->top == 1) {
459
471
                    if (!BN_copy(tmp, X))
460
0
                        goto err;
461
471
                    if (!BN_mul_word(tmp, D->d[0]))
462
0
                        goto err;
463
471
                } else {
464
0
                    if (!BN_mul(tmp, D, X, ctx))
465
0
                        goto err;
466
0
                }
467
759
                if (!BN_add(tmp, tmp, Y))
468
0
                    goto err;
469
759
            }
470
471
1.28k
            M = Y;      /* keep the BIGNUM object, the value does not matter */
472
1.28k
            Y = X;
473
1.28k
            X = tmp;
474
1.28k
            sign = -sign;
475
1.28k
        }
476
91
    }
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
5.77k
    if (sign < 0) {
487
5.72k
        if (!BN_sub(Y, n, Y))
488
0
            goto err;
489
5.72k
    }
490
    /* Now  Y*a  ==  A  (mod |n|).  */
491
492
5.77k
    if (BN_is_one(A)) {
493
        /* Y*a == 1  (mod |n|) */
494
5.68k
        if (!Y->neg && BN_ucmp(Y, n) < 0) {
495
1.89k
            if (!BN_copy(R, Y))
496
0
                goto err;
497
3.79k
        } else {
498
3.79k
            if (!BN_nnmod(R, Y, n, ctx))
499
0
                goto err;
500
3.79k
        }
501
5.68k
    } else {
502
91
        *pnoinv = 1;
503
91
        goto err;
504
91
    }
505
5.68k
    ret = R;
506
5.77k
 err:
507
5.77k
    if ((ret == NULL) && (in == NULL))
508
0
        BN_free(R);
509
5.77k
    BN_CTX_end(ctx);
510
5.77k
    bn_check_top(ret);
511
5.77k
    return ret;
512
5.68k
}
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
6.12k
{
518
6.12k
    BN_CTX *new_ctx = NULL;
519
6.12k
    BIGNUM *rv;
520
6.12k
    int noinv = 0;
521
522
6.12k
    if (ctx == NULL) {
523
0
        ctx = new_ctx = BN_CTX_new();
524
0
        if (ctx == NULL) {
525
0
            BNerr(BN_F_BN_MOD_INVERSE, ERR_R_MALLOC_FAILURE);
526
0
            return NULL;
527
0
        }
528
0
    }
529
530
6.12k
    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
531
6.12k
    if (noinv)
532
6.12k
        BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);
533
6.12k
    BN_CTX_free(new_ctx);
534
6.12k
    return rv;
535
6.12k
}
536
537
/*-
538
 * This function is based on the constant-time GCD work by Bernstein and Yang:
539
 * https://eprint.iacr.org/2019/266
540
 * Generalized fast GCD function to allow even inputs.
541
 * The algorithm first finds the shared powers of 2 between
542
 * the inputs, and removes them, reducing at least one of the
543
 * inputs to an odd value. Then it proceeds to calculate the GCD.
544
 * Before returning the resulting GCD, we take care of adding
545
 * back the powers of two removed at the beginning.
546
 * Note 1: we assume the bit length of both inputs is public information,
547
 * since access to top potentially leaks this information.
548
 */
549
int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
550
0
{
551
0
    BIGNUM *g, *temp = NULL;
552
0
    BN_ULONG mask = 0;
553
0
    int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0;
554
555
    /* Note 2: zero input corner cases are not constant-time since they are
556
     * handled immediately. An attacker can run an attack under this
557
     * assumption without the need of side-channel information. */
558
0
    if (BN_is_zero(in_b)) {
559
0
        ret = BN_copy(r, in_a) != NULL;
560
0
        r->neg = 0;
561
0
        return ret;
562
0
    }
563
0
    if (BN_is_zero(in_a)) {
564
0
        ret = BN_copy(r, in_b) != NULL;
565
0
        r->neg = 0;
566
0
        return ret;
567
0
    }
568
569
0
    bn_check_top(in_a);
570
0
    bn_check_top(in_b);
571
572
0
    BN_CTX_start(ctx);
573
0
    temp = BN_CTX_get(ctx);
574
0
    g = BN_CTX_get(ctx);
575
576
    /* make r != 0, g != 0 even, so BN_rshift is not a potential nop */
577
0
    if (g == NULL
578
0
        || !BN_lshift1(g, in_b)
579
0
        || !BN_lshift1(r, in_a))
580
0
        goto err;
581
582
    /* find shared powers of two, i.e. "shifts" >= 1 */
583
0
    for (i = 0; i < r->dmax && i < g->dmax; i++) {
584
0
        mask = ~(r->d[i] | g->d[i]);
585
0
        for (j = 0; j < BN_BITS2; j++) {
586
0
            bit &= mask;
587
0
            shifts += bit;
588
0
            mask >>= 1;
589
0
        }
590
0
    }
591
592
    /* subtract shared powers of two; shifts >= 1 */
593
0
    if (!BN_rshift(r, r, shifts)
594
0
        || !BN_rshift(g, g, shifts))
595
0
        goto err;
596
597
    /* expand to biggest nword, with room for a possible extra word */
598
0
    top = 1 + ((r->top >= g->top) ? r->top : g->top);
599
0
    if (bn_wexpand(r, top) == NULL
600
0
        || bn_wexpand(g, top) == NULL
601
0
        || bn_wexpand(temp, top) == NULL)
602
0
        goto err;
603
604
    /* re arrange inputs s.t. r is odd */
605
0
    BN_consttime_swap((~r->d[0]) & 1, r, g, top);
606
607
    /* compute the number of iterations */
608
0
    rlen = BN_num_bits(r);
609
0
    glen = BN_num_bits(g);
610
0
    m = 4 + 3 * ((rlen >= glen) ? rlen : glen);
611
612
0
    for (i = 0; i < m; i++) {
613
        /* conditionally flip signs if delta is positive and g is odd */
614
0
        cond = (-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1
615
            /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
616
0
            & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1)));
617
0
        delta = (-cond & -delta) | ((cond - 1) & delta);
618
0
        r->neg ^= cond;
619
        /* swap */
620
0
        BN_consttime_swap(cond, r, g, top);
621
622
        /* elimination step */
623
0
        delta++;
624
0
        if (!BN_add(temp, g, r))
625
0
            goto err;
626
0
        BN_consttime_swap(g->d[0] & 1 /* g is odd */
627
                /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
628
0
                & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1))),
629
0
                g, temp, top);
630
0
        if (!BN_rshift1(g, g))
631
0
            goto err;
632
0
    }
633
634
    /* remove possible negative sign */
635
0
    r->neg = 0;
636
    /* add powers of 2 removed, then correct the artificial shift */
637
0
    if (!BN_lshift(r, r, shifts)
638
0
        || !BN_rshift1(r, r))
639
0
        goto err;
640
641
0
    ret = 1;
642
643
0
 err:
644
0
    BN_CTX_end(ctx);
645
0
    bn_check_top(r);
646
0
    return ret;
647
0
}