crypto/ec/ecp_nistz256.c:1379: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `ecp_nistz256_windowed_mul`.
Showing all 20 steps of the trace


crypto/ec/ecp_nistz256.c:1150:8: Parameter `scalar->top`
1148.   
1149.   /* r = scalar*G + sum(scalars[i]*points[i]) */
1150.   __owur static int ecp_nistz256_points_mul(const EC_GROUP *group,
               ^
1151.                                             EC_POINT *r,
1152.                                             const BIGNUM *scalar,

crypto/ec/ecp_nistz256.c:1379:14: Call
1377.               out = &p.p;
1378.   
1379.           if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
                     ^
1380.               goto err;
1381.   

crypto/ec/ecp_nistz256.c:595:8: Parameter `(*scalar)->top`
  593.   
  594.   /* r = sum(scalar[i]*point[i]) */
  595.   __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
                ^
  596.                                               P256_POINT *r,
  597.                                               const BIGNUM **scalar,

crypto/ec/ecp_nistz256.c:630:14: Call
  628.   
  629.           /* This is an unusual input, we don't guarantee constant-timeness. */
  630.           if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
                      ^
  631.               BIGNUM *mod;
  632.   

crypto/bn/bn_lib.c:166:9: Call
    164.       bn_check_top(a);
    165.   
    166.       if (BN_is_zero(a))
                   ^
    167.           return 0;
    168.       return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));

crypto/bn/bn_lib.c:920:1: Parameter `a->top`
      918.   }
      919.   
      920. > int BN_is_zero(const BIGNUM *a)
      921.   {
      922.       return a->top == 0;

crypto/ec/ecp_nistz256.c:635:18: Call
  633.               if ((mod = BN_CTX_get(ctx)) == NULL)
  634.                   goto err;
  635.               if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
                          ^
  636.                   ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
  637.                   goto err;

crypto/bn/bn_mod.c:13:1: Parameter `m->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:140:1: Parameter `num->top`
      138.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
      139.    */
      140. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
      141.              BN_CTX *ctx)
      142.   {

crypto/bn/bn_div.c:210:11: Call
      208.       sdiv->neg = 0;
      209.       norm_shift += BN_BITS2;
      210.       if (!(BN_lshift(snum, num, norm_shift)))
                       ^
      211.           goto err;
      212.       snum->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:1016:1: Parameter `*a->d`
          1014.   }
          1015.   
          1016. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
          1017.   {
          1018.       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 `ecp_nistz256_windowed_mul` 
        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);
}
['static int ecp_nistz256_points_mul(const EC_GROUP *group,\n                                          EC_POINT *r,\n                                          const BIGNUM *scalar,\n                                          size_t num,\n                                          const EC_POINT *points[],\n                                          const BIGNUM *scalars[], BN_CTX *ctx)\n{\n    int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;\n    size_t j;\n    unsigned char p_str[33] = { 0 };\n    const PRECOMP256_ROW *preComputedTable = NULL;\n    const NISTZ256_PRE_COMP *pre_comp = NULL;\n    const EC_POINT *generator = NULL;\n    BN_CTX *new_ctx = NULL;\n    const BIGNUM **new_scalars = NULL;\n    const EC_POINT **new_points = NULL;\n    unsigned int idx = 0;\n    const unsigned int window_size = 7;\n    const unsigned int mask = (1 << (window_size + 1)) - 1;\n    unsigned int wvalue;\n    ALIGN32 union {\n        P256_POINT p;\n        P256_POINT_AFFINE a;\n    } t, p;\n    BIGNUM *tmp_scalar;\n    if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {\n        ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    if (group->meth != r->meth) {\n        ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n        return 0;\n    }\n    if ((scalar == NULL) && (num == 0))\n        return EC_POINT_set_to_infinity(group, r);\n    for (j = 0; j < num; j++) {\n        if (group->meth != points[j]->meth) {\n            ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n            return 0;\n        }\n    }\n    if (ctx == NULL) {\n        ctx = new_ctx = BN_CTX_new();\n        if (ctx == NULL)\n            goto err;\n    }\n    BN_CTX_start(ctx);\n    if (scalar) {\n        generator = EC_GROUP_get0_generator(group);\n        if (generator == NULL) {\n            ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);\n            goto err;\n        }\n        pre_comp = group->pre_comp.nistz256;\n        if (pre_comp) {\n            EC_POINT *pre_comp_generator = EC_POINT_new(group);\n            if (pre_comp_generator == NULL)\n                goto err;\n            if (!ecp_nistz256_set_from_affine(pre_comp_generator,\n                                              group, pre_comp->precomp[0],\n                                              ctx)) {\n                EC_POINT_free(pre_comp_generator);\n                goto err;\n            }\n            if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))\n                preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;\n            EC_POINT_free(pre_comp_generator);\n        }\n        if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {\n            preComputedTable = ecp_nistz256_precomputed;\n        }\n        if (preComputedTable) {\n            if ((BN_num_bits(scalar) > 256)\n                || BN_is_negative(scalar)) {\n                if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)\n                    goto err;\n                if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {\n                    ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);\n                    goto err;\n                }\n                scalar = tmp_scalar;\n            }\n            for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {\n                BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];\n                p_str[i + 0] = (unsigned char)d;\n                p_str[i + 1] = (unsigned char)(d >> 8);\n                p_str[i + 2] = (unsigned char)(d >> 16);\n                p_str[i + 3] = (unsigned char)(d >>= 24);\n                if (BN_BYTES == 8) {\n                    d >>= 8;\n                    p_str[i + 4] = (unsigned char)d;\n                    p_str[i + 5] = (unsigned char)(d >> 8);\n                    p_str[i + 6] = (unsigned char)(d >> 16);\n                    p_str[i + 7] = (unsigned char)(d >> 24);\n                }\n            }\n            for (; i < 33; i++)\n                p_str[i] = 0;\n#if defined(ECP_NISTZ256_AVX2)\n            if (ecp_nistz_avx2_eligible()) {\n                ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);\n            } else\n#endif\n            {\n                BN_ULONG infty;\n                wvalue = (p_str[0] << 1) & mask;\n                idx += window_size;\n                wvalue = _booth_recode_w7(wvalue);\n                ecp_nistz256_gather_w7(&p.a, preComputedTable[0],\n                                       wvalue >> 1);\n                ecp_nistz256_neg(p.p.Z, p.p.Y);\n                copy_conditional(p.p.Y, p.p.Z, wvalue & 1);\n                infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |\n                         p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);\n                if (P256_LIMBS == 8)\n                    infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |\n                              p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);\n                infty = 0 - is_zero(infty);\n                infty = ~infty;\n                p.p.Z[0] = ONE[0] & infty;\n                p.p.Z[1] = ONE[1] & infty;\n                p.p.Z[2] = ONE[2] & infty;\n                p.p.Z[3] = ONE[3] & infty;\n                if (P256_LIMBS == 8) {\n                    p.p.Z[4] = ONE[4] & infty;\n                    p.p.Z[5] = ONE[5] & infty;\n                    p.p.Z[6] = ONE[6] & infty;\n                    p.p.Z[7] = ONE[7] & infty;\n                }\n                for (i = 1; i < 37; i++) {\n                    unsigned int off = (idx - 1) / 8;\n                    wvalue = p_str[off] | p_str[off + 1] << 8;\n                    wvalue = (wvalue >> ((idx - 1) % 8)) & mask;\n                    idx += window_size;\n                    wvalue = _booth_recode_w7(wvalue);\n                    ecp_nistz256_gather_w7(&t.a,\n                                           preComputedTable[i], wvalue >> 1);\n                    ecp_nistz256_neg(t.p.Z, t.a.Y);\n                    copy_conditional(t.a.Y, t.p.Z, wvalue & 1);\n                    ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);\n                }\n            }\n        } else {\n            p_is_infinity = 1;\n            no_precomp_for_generator = 1;\n        }\n    } else\n        p_is_infinity = 1;\n    if (no_precomp_for_generator) {\n        new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));\n        if (new_scalars == NULL) {\n            ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));\n        if (new_points == NULL) {\n            ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));\n        new_scalars[num] = scalar;\n        memcpy(new_points, points, num * sizeof(EC_POINT *));\n        new_points[num] = generator;\n        scalars = new_scalars;\n        points = new_points;\n        num++;\n    }\n    if (num) {\n        P256_POINT *out = &t.p;\n        if (p_is_infinity)\n            out = &p.p;\n        if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))\n            goto err;\n        if (!p_is_infinity)\n            ecp_nistz256_point_add(&p.p, &p.p, out);\n    }\n    if (!bn_set_words(r->X, p.p.X, P256_LIMBS) ||\n        !bn_set_words(r->Y, p.p.Y, P256_LIMBS) ||\n        !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {\n        goto err;\n    }\n    r->Z_is_one = is_one(r->Z) & 1;\n    ret = 1;\nerr:\n    if (ctx)\n        BN_CTX_end(ctx);\n    BN_CTX_free(new_ctx);\n    OPENSSL_free(new_points);\n    OPENSSL_free(new_scalars);\n    return ret;\n}', 'static int ecp_nistz256_windowed_mul(const EC_GROUP *group,\n                                            P256_POINT *r,\n                                            const BIGNUM **scalar,\n                                            const EC_POINT **point,\n                                            size_t num, BN_CTX *ctx)\n{\n    size_t i;\n    int j, ret = 0;\n    unsigned int idx;\n    unsigned char (*p_str)[33] = NULL;\n    const unsigned int window_size = 5;\n    const unsigned int mask = (1 << (window_size + 1)) - 1;\n    unsigned int wvalue;\n    P256_POINT *temp;\n    const BIGNUM **scalars = NULL;\n    P256_POINT (*table)[16] = NULL;\n    void *table_storage = NULL;\n    if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)\n        || (table_storage =\n            OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL\n        || (p_str =\n            OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL\n        || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {\n        ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    table = (void *)ALIGNPTR(table_storage, 64);\n    temp = (P256_POINT *)(table + num);\n    for (i = 0; i < num; i++) {\n        P256_POINT *row = table[i];\n        if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {\n            BIGNUM *mod;\n            if ((mod = BN_CTX_get(ctx)) == NULL)\n                goto err;\n            if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {\n                ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);\n                goto err;\n            }\n            scalars[i] = mod;\n        } else\n            scalars[i] = scalar[i];\n        for (j = 0; j < bn_get_top(scalars[i]) * BN_BYTES; j += BN_BYTES) {\n            BN_ULONG d = bn_get_words(scalars[i])[j / BN_BYTES];\n            p_str[i][j + 0] = (unsigned char)d;\n            p_str[i][j + 1] = (unsigned char)(d >> 8);\n            p_str[i][j + 2] = (unsigned char)(d >> 16);\n            p_str[i][j + 3] = (unsigned char)(d >>= 24);\n            if (BN_BYTES == 8) {\n                d >>= 8;\n                p_str[i][j + 4] = (unsigned char)d;\n                p_str[i][j + 5] = (unsigned char)(d >> 8);\n                p_str[i][j + 6] = (unsigned char)(d >> 16);\n                p_str[i][j + 7] = (unsigned char)(d >> 24);\n            }\n        }\n        for (; j < 33; j++)\n            p_str[i][j] = 0;\n        if (!ecp_nistz256_bignum_to_field_elem(temp[0].X, point[i]->X)\n            || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)\n            || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {\n            ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL,\n                  EC_R_COORDINATES_OUT_OF_RANGE);\n            goto err;\n        }\n        ecp_nistz256_scatter_w5  (row, &temp[0], 1);\n        ecp_nistz256_point_double(&temp[1], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[1], 2);\n        ecp_nistz256_point_add   (&temp[2], &temp[1], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[2], 3);\n        ecp_nistz256_point_double(&temp[1], &temp[1]);\n        ecp_nistz256_scatter_w5  (row, &temp[1], 4);\n        ecp_nistz256_point_double(&temp[2], &temp[2]);\n        ecp_nistz256_scatter_w5  (row, &temp[2], 6);\n        ecp_nistz256_point_add   (&temp[3], &temp[1], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[3], 5);\n        ecp_nistz256_point_add   (&temp[4], &temp[2], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[4], 7);\n        ecp_nistz256_point_double(&temp[1], &temp[1]);\n        ecp_nistz256_scatter_w5  (row, &temp[1], 8);\n        ecp_nistz256_point_double(&temp[2], &temp[2]);\n        ecp_nistz256_scatter_w5  (row, &temp[2], 12);\n        ecp_nistz256_point_double(&temp[3], &temp[3]);\n        ecp_nistz256_scatter_w5  (row, &temp[3], 10);\n        ecp_nistz256_point_double(&temp[4], &temp[4]);\n        ecp_nistz256_scatter_w5  (row, &temp[4], 14);\n        ecp_nistz256_point_add   (&temp[2], &temp[2], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[2], 13);\n        ecp_nistz256_point_add   (&temp[3], &temp[3], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[3], 11);\n        ecp_nistz256_point_add   (&temp[4], &temp[4], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[4], 15);\n        ecp_nistz256_point_add   (&temp[2], &temp[1], &temp[0]);\n        ecp_nistz256_scatter_w5  (row, &temp[2], 9);\n        ecp_nistz256_point_double(&temp[1], &temp[1]);\n        ecp_nistz256_scatter_w5  (row, &temp[1], 16);\n    }\n    idx = 255;\n    wvalue = p_str[0][(idx - 1) / 8];\n    wvalue = (wvalue >> ((idx - 1) % 8)) & mask;\n    ecp_nistz256_gather_w5(&temp[0], table[0], _booth_recode_w5(wvalue) >> 1);\n    memcpy(r, &temp[0], sizeof(temp[0]));\n    while (idx >= 5) {\n        for (i = (idx == 255 ? 1 : 0); i < num; i++) {\n            unsigned int off = (idx - 1) / 8;\n            wvalue = p_str[i][off] | p_str[i][off + 1] << 8;\n            wvalue = (wvalue >> ((idx - 1) % 8)) & mask;\n            wvalue = _booth_recode_w5(wvalue);\n            ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);\n            ecp_nistz256_neg(temp[1].Y, temp[0].Y);\n            copy_conditional(temp[0].Y, temp[1].Y, (wvalue & 1));\n            ecp_nistz256_point_add(r, r, &temp[0]);\n        }\n        idx -= window_size;\n        ecp_nistz256_point_double(r, r);\n        ecp_nistz256_point_double(r, r);\n        ecp_nistz256_point_double(r, r);\n        ecp_nistz256_point_double(r, r);\n        ecp_nistz256_point_double(r, r);\n    }\n    for (i = 0; i < num; i++) {\n        wvalue = p_str[i][0];\n        wvalue = (wvalue << 1) & mask;\n        wvalue = _booth_recode_w5(wvalue);\n        ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);\n        ecp_nistz256_neg(temp[1].Y, temp[0].Y);\n        copy_conditional(temp[0].Y, temp[1].Y, wvalue & 1);\n        ecp_nistz256_point_add(r, r, &temp[0]);\n    }\n    ret = 1;\n err:\n    OPENSSL_free(table_storage);\n    OPENSSL_free(p_str);\n    OPENSSL_free(scalars);\n    return ret;\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_is_zero(const BIGNUM *a)\n{\n    return a->top == 0;\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    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (dv == NULL)\n        res = BN_CTX_get(ctx);\n    else\n        res = dv;\n    if (sdiv == NULL || res == NULL || tmp == NULL || snum == 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_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}']