crypto/ec/ecp_smpl.c:1308: error: BUFFER_OVERRUN_L3
  Offset: 0 Size: [0, 8388607] by call to `BN_mod_inverse`.
Showing all 23 steps of the trace


crypto/ec/ecp_smpl.c:1259:11: Call
1257.   
1258.       BN_CTX_start(ctx);
1259.       tmp = BN_CTX_get(ctx);
                  ^
1260.       tmp_Z = BN_CTX_get(ctx);
1261.       if (tmp == NULL || tmp_Z == NULL)

crypto/bn/bn_ctx.c:275:5: Call
  273.       }
  274.       /* OK, make sure the returned bignum is "zero" */
  275.       BN_zero(ret);
             ^
  276.       ctx->used++;
  277.       CTXDBG_RET(ctx, ret);

crypto/bn/bn_lib.c:520:1: Parameter `*a->d`
    518.   }
    519.   
    520. > int BN_set_word(BIGNUM *a, BN_ULONG w)
    521.   {
    522.       bn_check_top(a);

crypto/bn/bn_lib.c:523:9: Call
    521.   {
    522.       bn_check_top(a);
    523.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                   ^
    524.           return (0);
    525.       a->neg = 0;

crypto/bn/bn_lcl.h:777:1: Parameter `*a->d`
      775.   int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx);
      776.   
      777. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
      778.   {
      779.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/ec/ecp_smpl.c:1308:10: Call
1306.        */
1307.   
1308.       if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {
                 ^
1309.           ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1310.           goto err;

crypto/bn/bn_gcd.c:225:1: Parameter `*in->d`
  223.                                           BN_CTX *ctx);
  224.   
  225. > BIGNUM *BN_mod_inverse(BIGNUM *in,
  226.                          const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  227.   {

crypto/bn/bn_gcd.c:230:10: Call
  228.       BIGNUM *rv;
  229.       int noinv;
  230.       rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
                  ^
  231.       if (noinv)
  232.           BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);

crypto/bn/bn_gcd.c:236:1: Parameter `*in->d`
    234.   }
    235.   
    236. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
    237.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
    238.                              int *pnoinv)

crypto/bn/bn_gcd.c:249:16: Call
    247.       if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
    248.           || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
    249.           return BN_mod_inverse_no_branch(in, a, n, ctx);
                          ^
    250.       }
    251.   

crypto/bn/bn_gcd.c:556:1: Parameter `*in->d`
      554.    * not contain branches that may leak sensitive information.
      555.    */
      556. > static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
      557.                                           const BIGNUM *a, const BIGNUM *n,
      558.                                           BN_CTX *ctx)

crypto/bn/bn_gcd.c:707:18: Call
      705.                   goto err;
      706.           } else {
      707.               if (!BN_nnmod(R, Y, n, ctx))
                              ^
      708.                   goto err;
      709.           }

crypto/bn/bn_mod.c:118:1: Parameter `*r->d`
        116.   #include "bn_lcl.h"
        117.   
        118. > int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
        119.   {
        120.       /*

crypto/bn/bn_mod.c:125:11: Call
        123.        */
        124.   
        125.       if (!(BN_mod(r, m, d, ctx)))
                         ^
        126.           return 0;
        127.       if (!r->neg)

crypto/bn/bn_div.c:188:1: Parameter `*rm->d`
          186.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
          187.    */
          188. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
          189.              BN_CTX *ctx)
          190.   {

crypto/bn/bn_div.c:453:9: Call
          451.            */
          452.           int neg = num->neg;
          453.           BN_rshift(rm, snum, norm_shift);
                         ^
          454.           if (!BN_is_zero(rm))
          455.               rm->neg = neg;

crypto/bn/bn_shift.c:168:1: Parameter `*r->d`
            166.   }
            167.   
            168. > int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
            169.   {
            170.       int i, j, nw, lb, rb;

crypto/bn/bn_shift.c:186:9: Call
            184.       lb = BN_BITS2 - rb;
            185.       if (nw >= a->top || a->top == 0) {
            186.           BN_zero(r);
                           ^
            187.           return (1);
            188.       }

crypto/bn/bn_lib.c:520:1: <Length trace>
518.   }
519.   
520. > int BN_set_word(BIGNUM *a, BN_ULONG w)
521.   {
522.       bn_check_top(a);

crypto/bn/bn_lib.c:520:1: Parameter `*a->d`
              518.   }
              519.   
              520. > int BN_set_word(BIGNUM *a, BN_ULONG w)
              521.   {
              522.       bn_check_top(a);

crypto/bn/bn_lib.c:523:9: Call
              521.   {
              522.       bn_check_top(a);
              523.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                             ^
              524.           return (0);
              525.       a->neg = 0;

crypto/bn/bn_lcl.h:777:1: Parameter `*a->d`
                775.   int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx);
                776.   
                777. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
                778.   {
                779.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_lib.c:526:5: Array access: Offset: 0 Size: [0, 8388607] by call to `BN_mod_inverse` 
              524.           return (0);
              525.       a->neg = 0;
              526.       a->d[0] = w;
                         ^
              527.       a->top = (w ? 1 : 0);
              528.       bn_check_top(a);

int BN_set_word(BIGNUM *a, BN_ULONG w)
{
    bn_check_top(a);
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
        return (0);
    a->neg = 0;
    a->d[0] = w;
    a->top = (w ? 1 : 0);
    bn_check_top(a);
    return (1);
}
['int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,\n                                     EC_POINT *points[], BN_CTX *ctx)\n{\n    BN_CTX *new_ctx = NULL;\n    BIGNUM *tmp, *tmp_Z;\n    BIGNUM **prod_Z = NULL;\n    size_t i;\n    int ret = 0;\n    if (num == 0)\n        return 1;\n    if (ctx == NULL) {\n        ctx = new_ctx = BN_CTX_new();\n        if (ctx == NULL)\n            return 0;\n    }\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    tmp_Z = BN_CTX_get(ctx);\n    if (tmp == NULL || tmp_Z == NULL)\n        goto err;\n    prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);\n    if (prod_Z == NULL)\n        goto err;\n    for (i = 0; i < num; i++) {\n        prod_Z[i] = BN_new();\n        if (prod_Z[i] == NULL)\n            goto err;\n    }\n    if (!BN_is_zero(points[0]->Z)) {\n        if (!BN_copy(prod_Z[0], points[0]->Z))\n            goto err;\n    } else {\n        if (group->meth->field_set_to_one != 0) {\n            if (!group->meth->field_set_to_one(group, prod_Z[0], ctx))\n                goto err;\n        } else {\n            if (!BN_one(prod_Z[0]))\n                goto err;\n        }\n    }\n    for (i = 1; i < num; i++) {\n        if (!BN_is_zero(points[i]->Z)) {\n            if (!group->\n                meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z,\n                                ctx))\n                goto err;\n        } else {\n            if (!BN_copy(prod_Z[i], prod_Z[i - 1]))\n                goto err;\n        }\n    }\n    if (!BN_mod_inverse(tmp, prod_Z[num - 1], group->field, ctx)) {\n        ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);\n        goto err;\n    }\n    if (group->meth->field_encode != 0) {\n        if (!group->meth->field_encode(group, tmp, tmp, ctx))\n            goto err;\n        if (!group->meth->field_encode(group, tmp, tmp, ctx))\n            goto err;\n    }\n    for (i = num - 1; i > 0; --i) {\n        if (!BN_is_zero(points[i]->Z)) {\n            if (!group->\n                meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx))\n                goto err;\n            if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx))\n                goto err;\n            if (!BN_copy(points[i]->Z, tmp_Z))\n                goto err;\n        }\n    }\n    if (!BN_is_zero(points[0]->Z)) {\n        if (!BN_copy(points[0]->Z, tmp))\n            goto err;\n    }\n    for (i = 0; i < num; i++) {\n        EC_POINT *p = points[i];\n        if (!BN_is_zero(p->Z)) {\n            if (!group->meth->field_sqr(group, tmp, p->Z, ctx))\n                goto err;\n            if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx))\n                goto err;\n            if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx))\n                goto err;\n            if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx))\n                goto err;\n            if (group->meth->field_set_to_one != 0) {\n                if (!group->meth->field_set_to_one(group, p->Z, ctx))\n                    goto err;\n            } else {\n                if (!BN_one(p->Z))\n                    goto err;\n            }\n            p->Z_is_one = 1;\n        }\n    }\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    BN_CTX_free(new_ctx);\n    if (prod_Z != NULL) {\n        for (i = 0; i < num; i++) {\n            if (prod_Z[i] == NULL)\n                break;\n            BN_clear_free(prod_Z[i]);\n        }\n        OPENSSL_free(prod_Z);\n    }\n    return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG_ENTRY("BN_CTX_get", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        return NULL;\n    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n        ctx->too_many = 1;\n        BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        return NULL;\n    }\n    BN_zero(ret);\n    ctx->used++;\n    CTXDBG_RET(ctx, ret);\n    return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return (0);\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    bn_check_top(a);\n    return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n                       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n    BIGNUM *rv;\n    int noinv;\n    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n    if (noinv)\n        BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n    return rv;\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 (pnoinv)\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}', '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}', '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    res->neg = (num->neg ^ divisor->neg);\n    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\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    if (res->top == 0)\n        res->neg = 0;\n    else\n        resp--;\n    for (i = 0; i < loop - 1; i++, wnump--, resp--) {\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 = 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_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n    int i, j, nw, lb, rb;\n    BN_ULONG *t, *f;\n    BN_ULONG l, tmp;\n    bn_check_top(r);\n    bn_check_top(a);\n    if (n < 0) {\n        BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n        return 0;\n    }\n    nw = n / BN_BITS2;\n    rb = n % BN_BITS2;\n    lb = BN_BITS2 - rb;\n    if (nw >= a->top || a->top == 0) {\n        BN_zero(r);\n        return (1);\n    }\n    i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n    if (r != a) {\n        r->neg = a->neg;\n        if (bn_wexpand(r, i) == NULL)\n            return (0);\n    } else {\n        if (n == 0)\n            return 1;\n    }\n    f = &(a->d[nw]);\n    t = r->d;\n    j = a->top - nw;\n    r->top = i;\n    if (rb == 0) {\n        for (i = j; i != 0; i--)\n            *(t++) = *(f++);\n    } else {\n        l = *(f++);\n        for (i = j - 1; i != 0; i--) {\n            tmp = (l >> rb) & BN_MASK2;\n            l = *(f++);\n            *(t++) = (tmp | (l << lb)) & BN_MASK2;\n        }\n        if ((l = (l >> rb) & BN_MASK2))\n            *(t) = l;\n    }\n    bn_check_top(r);\n    return (1);\n}']