crypto/bn/bn_blind.c:255: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `int_bn_mod_inverse`.
Showing all 28 steps of the trace


crypto/bn/bn_blind.c:255:14: Call
253.           if (!BN_priv_rand_range(ret->A, ret->mod))
254.               goto err;
255.           if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {
                    ^
256.               /*
257.                * this should almost never happen for good RSA keys

crypto/bn/bn_gcd.c:135:1: Parameter `in->top`
  133.   }
  134.   
  135. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  136.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  137.                              int *pnoinv)

crypto/bn/bn_blind.c:255:14: Call
253.           if (!BN_priv_rand_range(ret->A, ret->mod))
254.               goto err;
255.           if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {
                    ^
256.               /*
257.                * this should almost never happen for good RSA keys

crypto/bn/bn_gcd.c:135:1: Parameter `n->top`
  133.   }
  134.   
  135. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  136.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  137.                              int *pnoinv)

crypto/bn/bn_gcd.c:144:9: Call
  142.   
  143.       /* This is invalid input so we don't worry about constant time here */
  144.       if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
                 ^
  145.           if (pnoinv != NULL)
  146.               *pnoinv = 1;

crypto/bn/bn_lib.c:840:1: Parameter `a->top`
    838.   }
    839.   
    840. > int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
    841.   {
    842.       return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));

crypto/bn/bn_gcd.c:144:33: Call
  142.   
  143.       /* This is invalid input so we don't worry about constant time here */
  144.       if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
                                         ^
  145.           if (pnoinv != NULL)
  146.               *pnoinv = 1;

crypto/bn/bn_lib.c:845:1: Parameter `a->top`
    843.   }
    844.   
    845. > int BN_is_zero(const BIGNUM *a)
    846.   {
    847.       return a->top == 0;

crypto/bn/bn_gcd.c:155:16: Call
  153.       if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
  154.           || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
  155.           return BN_mod_inverse_no_branch(in, a, n, ctx);
                        ^
  156.       }
  157.   

crypto/bn/bn_gcd.c:458:1: Parameter `n->top`
    456.    * not contain branches that may leak sensitive information.
    457.    */
    458. > static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
    459.                                           const BIGNUM *a, const BIGNUM *n,
    460.                                           BN_CTX *ctx)

crypto/bn/bn_gcd.c:491:9: Call
    489.       if (BN_copy(B, a) == NULL)
    490.           goto err;
    491.       if (BN_copy(A, n) == NULL)
                   ^
    492.           goto err;
    493.       A->neg = 0;

crypto/bn/bn_lib.c:285:1: Parameter `b->top`
      283.   }
      284.   
      285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
      286.   {
      287.       bn_check_top(b);

crypto/bn/bn_gcd.c:609:18: Call
    607.                   goto err;
    608.           } else {
    609.               if (!BN_nnmod(R, Y, n, ctx))
                            ^
    610.                   goto err;
    611.           }

crypto/bn/bn_mod.c:13:1: Parameter `d->top`
      11.   #include "bn_lcl.h"
      12.   
      13. > int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
      14.   {
      15.       /*

crypto/bn/bn_mod.c:20:11: Call
      18.        */
      19.   
      20.       if (!(BN_mod(r, m, d, ctx)))
                      ^
      21.           return 0;
      22.       if (!r->neg)

crypto/bn/bn_div.c:199:31: Call
        197.   
        198.       /* First we normalise the numbers */
        199.       norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
                                             ^
        200.       if (!(BN_lshift(sdiv, divisor, norm_shift)))
        201.           goto err;

crypto/bn/bn_lib.c:140:9: Assignment
          138.   
          139.       if (BN_is_zero(a))
          140.           return 0;
                         ^
          141.       return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
          142.   }

crypto/bn/bn_div.c:199:5: Assignment
        197.   
        198.       /* First we normalise the numbers */
        199.       norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
                   ^
        200.       if (!(BN_lshift(sdiv, divisor, norm_shift)))
        201.           goto err;

crypto/bn/bn_div.c:200:11: Call
        198.       /* First we normalise the numbers */
        199.       norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
        200.       if (!(BN_lshift(sdiv, divisor, norm_shift)))
                         ^
        201.           goto err;
        202.       sdiv->neg = 0;

crypto/bn/bn_shift.c:83:1: <Offset trace>
81.   }
82.   
83. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
84.   {
85.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:83:1: Parameter `n`
          81.   }
          82.   
          83. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
          84.   {
          85.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:97:5: Assignment
          95.       }
          96.   
          97.       nw = n / BN_BITS2;
                     ^
          98.       if (bn_wexpand(r, a->top + nw + 1) == NULL)
          99.           return 0;

crypto/bn/bn_shift.c:83:1: <Length trace>
81.   }
82.   
83. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
84.   {
85.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:83:1: Parameter `*r->d`
          81.   }
          82.   
          83. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
          84.   {
          85.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:98:9: Call
          96.   
          97.       nw = n / BN_BITS2;
          98.       if (bn_wexpand(r, a->top + nw + 1) == NULL)
                         ^
          99.           return 0;
          100.       r->neg = a->neg;

crypto/bn/bn_lib.c:941:1: Parameter `*a->d`
            939.   }
            940.   
            941. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
            942.   {
            943.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_shift.c:104:5: Assignment
          102.       rb = BN_BITS2 - lb;
          103.       f = a->d;
          104.       t = r->d;
                     ^
          105.       t[a->top + nw] = 0;
          106.       if (lb == 0)

crypto/bn/bn_shift.c:112:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `int_bn_mod_inverse` 
          110.           for (i = a->top - 1; i >= 0; i--) {
          111.               l = f[i];
          112.               t[nw + i + 1] |= (l >> rb) & BN_MASK2;
                             ^
          113.               t[nw + i] = (l << lb) & BN_MASK2;
          114.           }

int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
    int i, nw, lb, rb;
    BN_ULONG *t, *f;
    BN_ULONG l;
    bn_check_top(r);
    bn_check_top(a);
    if (n < 0) {
        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
        return 0;
    }
    nw = n / BN_BITS2;
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
        return 0;
    r->neg = a->neg;
    lb = n % BN_BITS2;
    rb = BN_BITS2 - lb;
    f = a->d;
    t = r->d;
    t[a->top + nw] = 0;
    if (lb == 0)
        for (i = a->top - 1; i >= 0; i--)
            t[nw + i] = f[i];
    else
        for (i = a->top - 1; i >= 0; i--) {
            l = f[i];
            t[nw + i + 1] |= (l >> rb) & BN_MASK2;
            t[nw + i] = (l << lb) & BN_MASK2;
        }
    memset(t, 0, sizeof(*t) * nw);
    r->top = a->top + nw + 1;
    bn_correct_top(r);
    bn_check_top(r);
    return 1;
}
['BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,\n                                      const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,\n                                      int (*bn_mod_exp) (BIGNUM *r,\n                                                         const BIGNUM *a,\n                                                         const BIGNUM *p,\n                                                         const BIGNUM *m,\n                                                         BN_CTX *ctx,\n                                                         BN_MONT_CTX *m_ctx),\n                                      BN_MONT_CTX *m_ctx)\n{\n    int retry_counter = 32;\n    BN_BLINDING *ret = NULL;\n    if (b == NULL)\n        ret = BN_BLINDING_new(NULL, NULL, m);\n    else\n        ret = b;\n    if (ret == NULL)\n        goto err;\n    if (ret->A == NULL && (ret->A = BN_new()) == NULL)\n        goto err;\n    if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)\n        goto err;\n    if (e != NULL) {\n        BN_free(ret->e);\n        ret->e = BN_dup(e);\n    }\n    if (ret->e == NULL)\n        goto err;\n    if (bn_mod_exp != NULL)\n        ret->bn_mod_exp = bn_mod_exp;\n    if (m_ctx != NULL)\n        ret->m_ctx = m_ctx;\n    do {\n        int rv;\n        if (!BN_priv_rand_range(ret->A, ret->mod))\n            goto err;\n        if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {\n            if (rv) {\n                if (retry_counter-- == 0) {\n                    BNerr(BN_F_BN_BLINDING_CREATE_PARAM,\n                          BN_R_TOO_MANY_ITERATIONS);\n                    goto err;\n                }\n            } else\n                goto err;\n        } else\n            break;\n    } while (1);\n    if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {\n        if (!ret->bn_mod_exp\n            (ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))\n            goto err;\n    } else {\n        if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))\n            goto err;\n    }\n    return ret;\n err:\n    if (b == NULL) {\n        BN_BLINDING_free(ret);\n        ret = NULL;\n    }\n    return ret;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n                           int *pnoinv)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n        if (pnoinv != NULL)\n            *pnoinv = 1;\n        return NULL;\n    }\n    if (pnoinv != NULL)\n        *pnoinv = 0;\n    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n        return BN_mod_inverse_no_branch(in, a, n, ctx);\n    }\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n        if (!BN_nnmod(B, B, A, ctx))\n            goto err;\n    }\n    sign = -1;\n    if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n        int shift;\n        while (!BN_is_zero(B)) {\n            shift = 0;\n            while (!BN_is_bit_set(B, shift)) {\n                shift++;\n                if (BN_is_odd(X)) {\n                    if (!BN_uadd(X, X, n))\n                        goto err;\n                }\n                if (!BN_rshift1(X, X))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(B, B, shift))\n                    goto err;\n            }\n            shift = 0;\n            while (!BN_is_bit_set(A, shift)) {\n                shift++;\n                if (BN_is_odd(Y)) {\n                    if (!BN_uadd(Y, Y, n))\n                        goto err;\n                }\n                if (!BN_rshift1(Y, Y))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(A, A, shift))\n                    goto err;\n            }\n            if (BN_ucmp(B, A) >= 0) {\n                if (!BN_uadd(X, X, Y))\n                    goto err;\n                if (!BN_usub(B, B, A))\n                    goto err;\n            } else {\n                if (!BN_uadd(Y, Y, X))\n                    goto err;\n                if (!BN_usub(A, A, B))\n                    goto err;\n            }\n        }\n    } else {\n        while (!BN_is_zero(B)) {\n            BIGNUM *tmp;\n            if (BN_num_bits(A) == BN_num_bits(B)) {\n                if (!BN_one(D))\n                    goto err;\n                if (!BN_sub(M, A, B))\n                    goto err;\n            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n                if (!BN_lshift1(T, B))\n                    goto err;\n                if (BN_ucmp(A, T) < 0) {\n                    if (!BN_one(D))\n                        goto err;\n                    if (!BN_sub(M, A, B))\n                        goto err;\n                } else {\n                    if (!BN_sub(M, A, T))\n                        goto err;\n                    if (!BN_add(D, T, B))\n                        goto err;\n                    if (BN_ucmp(A, D) < 0) {\n                        if (!BN_set_word(D, 2))\n                            goto err;\n                    } else {\n                        if (!BN_set_word(D, 3))\n                            goto err;\n                        if (!BN_sub(M, M, B))\n                            goto err;\n                    }\n                }\n            } else {\n                if (!BN_div(D, M, A, B, ctx))\n                    goto err;\n            }\n            tmp = A;\n            A = B;\n            B = M;\n            if (BN_is_one(D)) {\n                if (!BN_add(tmp, X, Y))\n                    goto err;\n            } else {\n                if (BN_is_word(D, 2)) {\n                    if (!BN_lshift1(tmp, X))\n                        goto err;\n                } else if (BN_is_word(D, 4)) {\n                    if (!BN_lshift(tmp, X, 2))\n                        goto err;\n                } else if (D->top == 1) {\n                    if (!BN_copy(tmp, X))\n                        goto err;\n                    if (!BN_mul_word(tmp, D->d[0]))\n                        goto err;\n                } else {\n                    if (!BN_mul(tmp, D, X, ctx))\n                        goto err;\n                }\n                if (!BN_add(tmp, tmp, Y))\n                    goto err;\n            }\n            M = Y;\n            Y = X;\n            X = tmp;\n            sign = -sign;\n        }\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        if (pnoinv)\n            *pnoinv = 1;\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n    return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n    return a->top == 0;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n                                        const BIGNUM *a, const BIGNUM *n,\n                                        BN_CTX *ctx)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n         {\n            BIGNUM local_B;\n            bn_init(&local_B);\n            BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n            if (!BN_nnmod(B, &local_B, A, ctx))\n                goto err;\n        }\n    }\n    sign = -1;\n    while (!BN_is_zero(B)) {\n        BIGNUM *tmp;\n        {\n            BIGNUM local_A;\n            bn_init(&local_A);\n            BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n            if (!BN_div(D, M, &local_A, B, ctx))\n                goto err;\n        }\n        tmp = A;\n        A = B;\n        B = M;\n        if (!BN_mul(tmp, D, X, ctx))\n            goto err;\n        if (!BN_add(tmp, tmp, Y))\n            goto err;\n        M = Y;\n        Y = X;\n        X = tmp;\n        sign = -sign;\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n    if (!(BN_mod(r, m, d, ctx)))\n        return 0;\n    if (!r->neg)\n        return 1;\n    return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int norm_shift, i, loop;\n    BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnump;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    int no_branch = 0;\n    if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    bn_check_top(num);\n    bn_check_top(divisor);\n    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n        no_branch = 1;\n    }\n    bn_check_top(dv);\n    bn_check_top(rm);\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return 0;\n    }\n    if (!no_branch && BN_ucmp(num, divisor) < 0) {\n        if (rm != NULL) {\n            if (BN_copy(rm, num) == NULL)\n                return 0;\n        }\n        if (dv != NULL)\n            BN_zero(dv);\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (sdiv == NULL)\n        goto err;\n    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n    if (!(BN_lshift(sdiv, divisor, norm_shift)))\n        goto err;\n    sdiv->neg = 0;\n    norm_shift += BN_BITS2;\n    if (!(BN_lshift(snum, num, norm_shift)))\n        goto err;\n    snum->neg = 0;\n    if (no_branch) {\n        if (snum->top <= sdiv->top + 1) {\n            if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n                goto err;\n            for (i = snum->top; i < sdiv->top + 2; i++)\n                snum->d[i] = 0;\n            snum->top = sdiv->top + 2;\n        } else {\n            if (bn_wexpand(snum, snum->top + 1) == NULL)\n                goto err;\n            snum->d[snum->top] = 0;\n            snum->top++;\n        }\n    }\n    div_n = sdiv->top;\n    num_n = snum->top;\n    loop = num_n - div_n;\n    wnum.neg = 0;\n    wnum.d = &(snum->d[loop]);\n    wnum.top = div_n;\n    wnum.dmax = snum->dmax - loop;\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    wnump = &(snum->d[num_n - 1]);\n    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\n    res->neg = (num->neg ^ divisor->neg);\n    res->top = loop - no_branch;\n    resp = &(res->d[loop - 1]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    if (!no_branch) {\n        if (BN_ucmp(&wnum, sdiv) >= 0) {\n            bn_clear_top2max(&wnum);\n            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n            *resp = 1;\n        } else\n            res->top--;\n    }\n    resp++;\n    if (res->top == 0)\n        res->neg = 0;\n    else\n        resp--;\n    for (i = 0; i < loop - 1; i++, wnump--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n        BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n        q = bn_div_3_words(wnump, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnump[0];\n        n1 = wnump[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n#  ifdef BN_LLONG\n            BN_ULLONG t2;\n#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n#   else\n            q = bn_div_words(n0, n1, d0);\n#   endif\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n            t2 = (BN_ULLONG) d1 *q;\n            for (;;) {\n                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                t2 -= d1;\n            }\n#  else\n            BN_ULONG t2l, t2h;\n            q = bn_div_words(n0, n1, d0);\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n#   if defined(BN_UMULT_LOHI)\n            BN_UMULT_LOHI(t2l, t2h, d1, q);\n#   elif defined(BN_UMULT_HIGH)\n            t2l = d1 * q;\n            t2h = BN_UMULT_HIGH(d1, q);\n#   else\n            {\n                BN_ULONG ql, qh;\n                t2l = LBITS(d1);\n                t2h = HBITS(d1);\n                ql = LBITS(q);\n                qh = HBITS(q);\n                mul64(t2l, t2h, ql, qh);\n            }\n#   endif\n            for (;;) {\n                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                if (t2l < d1)\n                    t2h--;\n                t2l -= d1;\n            }\n#  endif\n        }\n# endif\n        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n        tmp->d[div_n] = l0;\n        wnum.d--;\n        if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n            q--;\n            if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n                (*wnump)++;\n        }\n        resp--;\n        *resp = q;\n    }\n    bn_correct_top(snum);\n    if (rm != NULL) {\n        int neg = num->neg;\n        BN_rshift(rm, snum, norm_shift);\n        if (!BN_is_zero(rm))\n            rm->neg = neg;\n        bn_check_top(rm);\n    }\n    if (no_branch)\n        bn_correct_top(res);\n    BN_CTX_end(ctx);\n    return 1;\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return 0;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n    int i = a->top - 1;\n    bn_check_top(a);\n    if (BN_is_zero(a))\n        return 0;\n    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n    int i, nw, lb, rb;\n    BN_ULONG *t, *f;\n    BN_ULONG l;\n    bn_check_top(r);\n    bn_check_top(a);\n    if (n < 0) {\n        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n        return 0;\n    }\n    nw = n / BN_BITS2;\n    if (bn_wexpand(r, a->top + nw + 1) == NULL)\n        return 0;\n    r->neg = a->neg;\n    lb = n % BN_BITS2;\n    rb = BN_BITS2 - lb;\n    f = a->d;\n    t = r->d;\n    t[a->top + nw] = 0;\n    if (lb == 0)\n        for (i = a->top - 1; i >= 0; i--)\n            t[nw + i] = f[i];\n    else\n        for (i = a->top - 1; i >= 0; i--) {\n            l = f[i];\n            t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n            t[nw + i] = (l << lb) & BN_MASK2;\n        }\n    memset(t, 0, sizeof(*t) * nw);\n    r->top = a->top + nw + 1;\n    bn_correct_top(r);\n    bn_check_top(r);\n    return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']