crypto/bn/bn_prime.c:324: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: [1, +oo] by call to `BN_mod_mul`.
Showing all 46 steps of the trace


crypto/bn/bn_prime.c:249:1: Parameter `ctx->stack.depth`
247.    * returns 0 if there was an error, otherwise it returns 1.
248.    */
249. > int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
250.                                BN_GENCB *cb, int enhanced, int *status)
251.   {

crypto/bn/bn_prime.c:260:5: Call
258.           return 0;
259.   
260.       BN_CTX_start(ctx);
           ^
261.       g = BN_CTX_get(ctx);
262.       w1 = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:171:1: Parameter `ctx->stack.depth`
  169.   }
  170.   
  171. > void BN_CTX_start(BN_CTX *ctx)
  172.   {
  173.       CTXDBG("ENTER BN_CTX_start()", ctx);

crypto/bn/bn_prime.c:261:9: Call
259.   
260.       BN_CTX_start(ctx);
261.       g = BN_CTX_get(ctx);
               ^
262.       w1 = BN_CTX_get(ctx);
263.       w3 = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:262:10: Call
260.       BN_CTX_start(ctx);
261.       g = BN_CTX_get(ctx);
262.       w1 = BN_CTX_get(ctx);
                ^
263.       w3 = BN_CTX_get(ctx);
264.       x = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:263:10: Call
261.       g = BN_CTX_get(ctx);
262.       w1 = BN_CTX_get(ctx);
263.       w3 = BN_CTX_get(ctx);
                ^
264.       x = BN_CTX_get(ctx);
265.       m = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:264:9: Call
262.       w1 = BN_CTX_get(ctx);
263.       w3 = BN_CTX_get(ctx);
264.       x = BN_CTX_get(ctx);
               ^
265.       m = BN_CTX_get(ctx);
266.       z = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:265:9: Call
263.       w3 = BN_CTX_get(ctx);
264.       x = BN_CTX_get(ctx);
265.       m = BN_CTX_get(ctx);
               ^
266.       z = BN_CTX_get(ctx);
267.       b = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:266:9: Call
264.       x = BN_CTX_get(ctx);
265.       m = BN_CTX_get(ctx);
266.       z = BN_CTX_get(ctx);
               ^
267.       b = BN_CTX_get(ctx);
268.   

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:267:9: Call
265.       m = BN_CTX_get(ctx);
266.       z = BN_CTX_get(ctx);
267.       b = BN_CTX_get(ctx);
               ^
268.   
269.       if (!(b != NULL

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
  200.   }
  201.   
  202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  203.   {
  204.       BIGNUM *ret;

crypto/bn/bn_prime.c:292:26: Call
290.       /* Montgomery setup for computations mod a */
291.       mont = BN_MONT_CTX_new();
292.       if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
                                ^
293.           goto err;
294.   

crypto/bn/bn_mont.c:263:1: Parameter `ctx->stack.depth`
  261.   }
  262.   
  263. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  264.   {
  265.       int i, ret = 0;

crypto/bn/bn_prime.c:316:14: Call
314.           }
315.           /* (Step 4.5) z = b^m mod w */
316.           if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
                    ^
317.               goto err;
318.           /* (Step 4.6) if (z = 1 or z = w-1) */

crypto/bn/bn_exp.c:296:1: Parameter `ctx->stack.depth`
  294.   }
  295.   
  296. > int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  297.                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  298.   {

crypto/bn/bn_prime.c:324:36: Call
322.           for (j = 1; j < a ; ++j) {
323.               /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
324.               if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
                                          ^
325.                   goto err;
326.               /* (Step 4.7.3) */

crypto/bn/bn_mod.c:193:1: Parameter `ctx->stack.depth`
  191.   
  192.   /* slow but works */
  193. > int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  194.                  BN_CTX *ctx)
  195.   {

crypto/bn/bn_mod.c:203:5: Call
  201.       bn_check_top(m);
  202.   
  203.       BN_CTX_start(ctx);
             ^
  204.       if ((t = BN_CTX_get(ctx)) == NULL)
  205.           goto err;

crypto/bn/bn_ctx.c:171:1: Parameter `ctx->stack.depth`
    169.   }
    170.   
    171. > void BN_CTX_start(BN_CTX *ctx)
    172.   {
    173.       CTXDBG("ENTER BN_CTX_start()", ctx);

crypto/bn/bn_mod.c:204:14: Call
  202.   
  203.       BN_CTX_start(ctx);
  204.       if ((t = BN_CTX_get(ctx)) == NULL)
                      ^
  205.           goto err;
  206.       if (a == b) {

crypto/bn/bn_ctx.c:202:1: Parameter `ctx->stack.depth`
    200.   }
    201.   
    202. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
    203.   {
    204.       BIGNUM *ret;

crypto/bn/bn_mod.c:207:14: Call
  205.           goto err;
  206.       if (a == b) {
  207.           if (!BN_sqr(t, a, ctx))
                      ^
  208.               goto err;
  209.       } else {

crypto/bn/bn_sqr.c:17:1: Parameter `ctx->stack.depth`
    15.    * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
    16.    */
    17. > int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
    18.   {
    19.       int ret = bn_sqr_fixed_top(r, a, ctx);

crypto/bn/bn_sqr.c:19:15: Call
    17.   int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
    18.   {
    19.       int ret = bn_sqr_fixed_top(r, a, ctx);
                        ^
    20.   
    21.       bn_correct_top(r);

crypto/bn/bn_sqr.c:27:1: Parameter `ctx->stack.depth`
      25.   }
      26.   
      27. > int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
      28.   {
      29.       int max, al;

crypto/bn/bn_mod.c:213:10: Call
  211.               goto err;
  212.       }
  213.       if (!BN_nnmod(r, t, m, ctx))
                  ^
  214.           goto err;
  215.       bn_check_top(r);

crypto/bn/bn_mod.c:13:1: Parameter `ctx->stack.depth`
    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:209:1: Parameter `ctx->stack.depth`
      207.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
      208.    */
      209. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
      210.              BN_CTX *ctx)
      211.   {

crypto/bn/bn_div.c:229:11: Call
      227.       }
      228.   
      229.       ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);
                       ^
      230.   
      231.       if (ret) {

crypto/bn/bn_div.c:280:5: Call
        278.       bn_check_top(rm);
        279.   
        280.       BN_CTX_start(ctx);
                   ^
        281.       res = (dv == NULL) ? BN_CTX_get(ctx) : dv;
        282.       tmp = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:171:1: Parameter `*ctx->stack.indexes`
          169.   }
          170.   
          171. > void BN_CTX_start(BN_CTX *ctx)
          172.   {
          173.       CTXDBG("ENTER BN_CTX_start()", ctx);

crypto/bn/bn_div.c:450:5: Call
        448.       if (rm != NULL)
        449.           bn_rshift_fixed_top(rm, snum, norm_shift);
        450.       BN_CTX_end(ctx);
                   ^
        451.       return 1;
        452.    err:

crypto/bn/bn_ctx.c:185:1: Parameter `*ctx->stack.indexes`
          183.   }
          184.   
          185. > void BN_CTX_end(BN_CTX *ctx)
          186.   {
          187.       CTXDBG("ENTER BN_CTX_end()", ctx);

crypto/bn/bn_ctx.c:191:27: Call
          189.           ctx->err_stack--;
          190.       else {
          191.           unsigned int fp = BN_STACK_pop(&ctx->stack);
                                           ^
          192.           /* Does this stack frame have anything to release? */
          193.           if (fp < ctx->used)

crypto/bn/bn_ctx.c:266:1: <Offset trace>
264.   }
265.   
266. > static unsigned int BN_STACK_pop(BN_STACK *st)
267.   {
268.       return st->indexes[--(st->depth)];

crypto/bn/bn_ctx.c:266:1: Parameter `st->depth`
            264.   }
            265.   
            266. > static unsigned int BN_STACK_pop(BN_STACK *st)
            267.   {
            268.       return st->indexes[--(st->depth)];

crypto/bn/bn_ctx.c:266:1: <Length trace>
264.   }
265.   
266. > static unsigned int BN_STACK_pop(BN_STACK *st)
267.   {
268.       return st->indexes[--(st->depth)];

crypto/bn/bn_ctx.c:266:1: Parameter `*st->indexes`
            264.   }
            265.   
            266. > static unsigned int BN_STACK_pop(BN_STACK *st)
            267.   {
            268.       return st->indexes[--(st->depth)];

crypto/bn/bn_ctx.c:268:12: Array access: Offset: [-1, +oo] Size: [1, +oo] by call to `BN_mod_mul` 
            266.   static unsigned int BN_STACK_pop(BN_STACK *st)
            267.   {
            268.       return st->indexes[--(st->depth)];
                              ^
            269.   }
            270.   

static unsigned int BN_STACK_pop(BN_STACK *st)
{
    return st->indexes[--(st->depth)];
}
['int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,\n                             BN_GENCB *cb, int enhanced, int *status)\n{\n    int i, j, a, ret = 0;\n    BIGNUM *g, *w1, *w3, *x, *m, *z, *b;\n    BN_MONT_CTX *mont = NULL;\n    if (!BN_is_odd(w))\n        return 0;\n    BN_CTX_start(ctx);\n    g = BN_CTX_get(ctx);\n    w1 = BN_CTX_get(ctx);\n    w3 = BN_CTX_get(ctx);\n    x = BN_CTX_get(ctx);\n    m = BN_CTX_get(ctx);\n    z = BN_CTX_get(ctx);\n    b = BN_CTX_get(ctx);\n    if (!(b != NULL\n            && BN_copy(w1, w)\n            && BN_sub_word(w1, 1)\n            && BN_copy(w3, w)\n            && BN_sub_word(w3, 3)))\n        goto err;\n    if (BN_is_zero(w3) || BN_is_negative(w3))\n        goto err;\n    a = 1;\n    while (!BN_is_bit_set(w1, a))\n        a++;\n    if (!BN_rshift(m, w1, a))\n        goto err;\n    mont = BN_MONT_CTX_new();\n    if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))\n        goto err;\n    if (iterations == BN_prime_checks)\n        iterations = BN_prime_checks_for_size(BN_num_bits(w));\n    for (i = 0; i < iterations; ++i) {\n        if (!BN_priv_rand_range(b, w3) || !BN_add_word(b, 2))\n            goto err;\n        if (enhanced) {\n            if (!BN_gcd(g, b, w, ctx))\n                goto err;\n            if (!BN_is_one(g)) {\n                *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n                ret = 1;\n                goto err;\n            }\n        }\n        if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))\n            goto err;\n        if (BN_is_one(z) || BN_cmp(z, w1) == 0)\n            goto outer_loop;\n        for (j = 1; j < a ; ++j) {\n            if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n                goto err;\n            if (BN_cmp(z, w1) == 0)\n                goto outer_loop;\n            if (BN_is_one(z))\n                goto composite;\n        }\n        if (!BN_GENCB_call(cb, 1, i))\n            goto err;\n        if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n            goto err;\n        if (BN_is_one(z))\n            goto composite;\n        if (!BN_copy(x, z))\n            goto err;\ncomposite:\n        if (enhanced) {\n            if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))\n                goto err;\n            if (BN_is_one(g))\n                *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;\n            else\n                *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n        } else {\n            *status = BN_PRIMETEST_COMPOSITE;\n        }\n        ret = 1;\n        goto err;\nouter_loop: ;\n    }\n    *status = BN_PRIMETEST_PROBABLY_PRIME;\n    ret = 1;\nerr:\n    BN_clear(g);\n    BN_clear(w1);\n    BN_clear(w3);\n    BN_clear(x);\n    BN_clear(m);\n    BN_clear(z);\n    BN_clear(b);\n    BN_CTX_end(ctx);\n    BN_MONT_CTX_free(mont);\n    return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n    CTXDBG("ENTER BN_CTX_start()", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        ctx->err_stack++;\n    else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n        BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        ctx->err_stack++;\n    }\n    CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG("ENTER 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    ret->flags &= (~BN_FLG_CONSTTIME);\n    ctx->used++;\n    CTXDBG("LEAVE BN_CTX_get()", ctx);\n    return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n    int i, ret = 0;\n    BIGNUM *Ri, *R;\n    if (BN_is_zero(mod))\n        return 0;\n    BN_CTX_start(ctx);\n    if ((Ri = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    R = &(mont->RR);\n    if (!BN_copy(&(mont->N), mod))\n        goto err;\n    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n        BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n    mont->N.neg = 0;\n#ifdef MONT_WORD\n    {\n        BIGNUM tmod;\n        BN_ULONG buf[2];\n        bn_init(&tmod);\n        tmod.d = buf;\n        tmod.dmax = 2;\n        tmod.neg = 0;\n        if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n            BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n        mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n        BN_zero(R);\n        if (!(BN_set_bit(R, 2 * BN_BITS2)))\n            goto err;\n        tmod.top = 0;\n        if ((buf[0] = mod->d[0]))\n            tmod.top = 1;\n        if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n            tmod.top = 2;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n                goto err;\n            Ri->neg = 0;\n            Ri->d[0] = BN_MASK2;\n            Ri->d[1] = BN_MASK2;\n            Ri->top = 2;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n        BN_zero(R);\n        if (!(BN_set_bit(R, BN_BITS2)))\n            goto err;\n        buf[0] = mod->d[0];\n        buf[1] = 0;\n        tmod.top = buf[0] != 0 ? 1 : 0;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (!BN_set_word(Ri, BN_MASK2))\n                goto err;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = 0;\n# endif\n    }\n#else\n    {\n        mont->ri = BN_num_bits(&mont->N);\n        BN_zero(R);\n        if (!BN_set_bit(R, mont->ri))\n            goto err;\n        if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, mont->ri))\n            goto err;\n        if (!BN_sub_word(Ri, 1))\n            goto err;\n        if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n            goto err;\n    }\n#endif\n    BN_zero(&(mont->RR));\n    if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n        goto err;\n    if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n        goto err;\n    for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n        mont->RR.d[i] = 0;\n    mont->RR.top = ret;\n    mont->RR.flags |= BN_FLG_FIXED_TOP;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n    int start = 1;\n    BIGNUM *d, *r;\n    const BIGNUM *aa;\n    BIGNUM *val[TABLE_SIZE];\n    BN_MONT_CTX *mont = NULL;\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n        return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n    }\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return 0;\n    }\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_abs_is_word(m, 1)) {\n            ret = 1;\n            BN_zero(rr);\n        } else {\n            ret = BN_one(rr);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    d = BN_CTX_get(ctx);\n    r = BN_CTX_get(ctx);\n    val[0] = BN_CTX_get(ctx);\n    if (val[0] == NULL)\n        goto err;\n    if (in_mont != NULL)\n        mont = in_mont;\n    else {\n        if ((mont = BN_MONT_CTX_new()) == NULL)\n            goto err;\n        if (!BN_MONT_CTX_set(mont, m, ctx))\n            goto err;\n    }\n    if (a->neg || BN_ucmp(a, m) >= 0) {\n        if (!BN_nnmod(val[0], a, m, ctx))\n            goto err;\n        aa = val[0];\n    } else\n        aa = a;\n    if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n        goto err;\n    window = BN_window_bits_for_exponent_size(bits);\n    if (window > 1) {\n        if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n            goto err;\n        j = 1 << (window - 1);\n        for (i = 1; i < j; i++) {\n            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n                !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    start = 1;\n    wvalue = 0;\n    wstart = bits - 1;\n    wend = 0;\n#if 1\n    j = m->top;\n    if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n        if (bn_wexpand(r, j) == NULL)\n            goto err;\n        r->d[0] = (0 - m->d[0]) & BN_MASK2;\n        for (i = 1; i < j; i++)\n            r->d[i] = (~m->d[i]) & BN_MASK2;\n        r->top = j;\n        r->flags |= BN_FLG_FIXED_TOP;\n    } else\n#endif\n    if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n        goto err;\n    for (;;) {\n        if (BN_is_bit_set(p, wstart) == 0) {\n            if (!start) {\n                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n                    goto err;\n            }\n            if (wstart == 0)\n                break;\n            wstart--;\n            continue;\n        }\n        j = wstart;\n        wvalue = 1;\n        wend = 0;\n        for (i = 1; i < window; i++) {\n            if (wstart - i < 0)\n                break;\n            if (BN_is_bit_set(p, wstart - i)) {\n                wvalue <<= (i - wend);\n                wvalue |= 1;\n                wend = i;\n            }\n        }\n        j = wend + 1;\n        if (!start)\n            for (i = 0; i < j; i++) {\n                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n                    goto err;\n            }\n        if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n            goto err;\n        wstart -= wend + 1;\n        wvalue = 0;\n        start = 0;\n        if (wstart < 0)\n            break;\n    }\n#if defined(SPARC_T4_MONT)\n    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n        j = mont->N.top;\n        val[0]->d[0] = 1;\n        for (i = 1; i < j; i++)\n            val[0]->d[i] = 0;\n        val[0]->top = j;\n        if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n            goto err;\n    } else\n#endif\n    if (!BN_from_montgomery(rr, r, mont, ctx))\n        goto err;\n    ret = 1;\n err:\n    if (in_mont == NULL)\n        BN_MONT_CTX_free(mont);\n    BN_CTX_end(ctx);\n    bn_check_top(rr);\n    return ret;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    BIGNUM *t;\n    int ret = 0;\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(m);\n    BN_CTX_start(ctx);\n    if ((t = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (a == b) {\n        if (!BN_sqr(t, a, ctx))\n            goto err;\n    } else {\n        if (!BN_mul(t, a, b, ctx))\n            goto err;\n    }\n    if (!BN_nnmod(r, t, m, ctx))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int ret = bn_sqr_fixed_top(r, a, ctx);\n    bn_correct_top(r);\n    bn_check_top(r);\n    return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int max, al;\n    int ret = 0;\n    BIGNUM *tmp, *rr;\n    bn_check_top(a);\n    al = a->top;\n    if (al <= 0) {\n        r->top = 0;\n        r->neg = 0;\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    rr = (a != r) ? r : BN_CTX_get(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (rr == NULL || tmp == NULL)\n        goto err;\n    max = 2 * al;\n    if (bn_wexpand(rr, max) == NULL)\n        goto err;\n    if (al == 4) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[8];\n        bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n        bn_sqr_comba4(rr->d, a->d);\n#endif\n    } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[16];\n        bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n        bn_sqr_comba8(rr->d, a->d);\n#endif\n    } else {\n#if defined(BN_RECURSION)\n        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n            bn_sqr_normal(rr->d, a->d, al, t);\n        } else {\n            int j, k;\n            j = BN_num_bits_word((BN_ULONG)al);\n            j = 1 << (j - 1);\n            k = j + j;\n            if (al == j) {\n                if (bn_wexpand(tmp, k * 2) == NULL)\n                    goto err;\n                bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n            } else {\n                if (bn_wexpand(tmp, max) == NULL)\n                    goto err;\n                bn_sqr_normal(rr->d, a->d, al, tmp->d);\n            }\n        }\n#else\n        if (bn_wexpand(tmp, max) == NULL)\n            goto err;\n        bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n    }\n    rr->neg = 0;\n    rr->top = max;\n    rr->flags |= BN_FLG_FIXED_TOP;\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(rr);\n    bn_check_top(tmp);\n    BN_CTX_end(ctx);\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 ret;\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return 0;\n    }\n    if (divisor->d[divisor->top - 1] == 0) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n    if (ret) {\n        if (dv != NULL)\n            bn_correct_top(dv);\n        if (rm != NULL)\n            bn_correct_top(rm);\n    }\n    return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n                     const BIGNUM *divisor, BN_CTX *ctx)\n{\n    int norm_shift, i, j, loop;\n    BIGNUM *tmp, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnum, *wnumtop;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n    bn_check_top(num);\n    bn_check_top(divisor);\n    bn_check_top(dv);\n    bn_check_top(rm);\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    if (!BN_copy(sdiv, divisor))\n        goto err;\n    norm_shift = bn_left_align(sdiv);\n    sdiv->neg = 0;\n    if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n        goto err;\n    div_n = sdiv->top;\n    num_n = snum->top;\n    if (num_n <= div_n) {\n        if (bn_wexpand(snum, div_n + 1) == NULL)\n            goto err;\n        memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n        snum->top = num_n = div_n + 1;\n    }\n    loop = num_n - div_n;\n    wnum = &(snum->d[loop]);\n    wnumtop = &(snum->d[num_n - 1]);\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    if (!bn_wexpand(res, loop))\n        goto err;\n    res->neg = (num->neg ^ divisor->neg);\n    res->top = loop;\n    res->flags |= BN_FLG_FIXED_TOP;\n    resp = &(res->d[loop]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    for (i = 0; i < loop; i++, wnumtop--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n        q = bn_div_3_words(wnumtop, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnumtop[0];\n        n1 = wnumtop[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n            BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\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) | n2))\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 <= n2)))\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--;\n        l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n        q -= l0;\n        for (l0 = 0 - l0, j = 0; j < div_n; j++)\n            tmp->d[j] = sdiv->d[j] & l0;\n        l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n        (*wnumtop) += l0;\n        assert((*wnumtop) == 0);\n        *--resp = q;\n    }\n    snum->neg = num->neg;\n    snum->top = div_n;\n    snum->flags |= BN_FLG_FIXED_TOP;\n    if (rm != NULL)\n        bn_rshift_fixed_top(rm, snum, norm_shift);\n    BN_CTX_end(ctx);\n    return 1;\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n    CTXDBG("ENTER BN_CTX_end()", ctx);\n    if (ctx->err_stack)\n        ctx->err_stack--;\n    else {\n        unsigned int fp = BN_STACK_pop(&ctx->stack);\n        if (fp < ctx->used)\n            BN_POOL_release(&ctx->pool, ctx->used - fp);\n        ctx->used = fp;\n        ctx->too_many = 0;\n    }\n    CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n    return st->indexes[--(st->depth)];\n}']