crypto/bn/bn_sqrt.c:148: error: INTEGER_OVERFLOW_L2
  ([0, +oo] - 1):unsigned32 by call to `BN_mod_exp`.
Showing all 29 steps of the trace


crypto/bn/bn_sqrt.c:128:7: Call
126.   
127.   	/* A = a mod p */
128.   	if (!BN_nnmod(A, a, p, ctx)) goto end;
             ^
129.   
130.   	/* now write  |p| - 1  as  2^e*q  where  q  is odd */

crypto/bn/bn_mod.c:132:8: Call
  130.   	 * (i.e.,  0 <= r < |d|  always holds) */
  131.   
  132.   	if (!(BN_mod(r,m,d,ctx)))
                ^
  133.   		return 0;
  134.   	if (!r->neg)

crypto/bn/bn_div.c:179:1: Parameter `ctx->pool.used`
    177.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
    178.    */
    179. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
    180.   	   BN_CTX *ctx)
    181.   	{

crypto/bn/bn_sqrt.c:148:8: Call
146.   		q->neg = 0;
147.   		if (!BN_add_word(q, 1)) goto end;
148.   		if (!BN_mod_exp(ret, A, q, p, ctx)) goto end;
              ^
149.   		err = 0;
150.   		goto vrfy;

crypto/bn/bn_exp.c:163:1: Parameter `ctx->pool.used`
  161.   
  162.   
  163. > int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
  164.   	       BN_CTX *ctx)
  165.   	{

crypto/bn/bn_exp.c:228:9: Call
  226.   #endif
  227.   #ifdef RECP_MUL_MOD
  228.   		{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }
                 ^
  229.   #else
  230.   		{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }

crypto/bn/bn_exp.c:238:1: Parameter `ctx->pool.used`
    236.   
    237.   
    238. > int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
    239.   		    const BIGNUM *m, BN_CTX *ctx)
    240.   	{

crypto/bn/bn_exp.c:264:7: Call
    262.   
    263.   	BN_CTX_start(ctx);
    264.   	aa = BN_CTX_get(ctx);
                 ^
    265.   	val[0] = BN_CTX_get(ctx);
    266.   	if(!aa || !val[0]) goto err;

crypto/bn/bn_ctx.c:288:1: Parameter `ctx->pool.used`
      286.   	}
      287.   
      288. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
      289.   	{
      290.   	BIGNUM *ret;

crypto/bn/bn_exp.c:265:11: Call
    263.   	BN_CTX_start(ctx);
    264.   	aa = BN_CTX_get(ctx);
    265.   	val[0] = BN_CTX_get(ctx);
                     ^
    266.   	if(!aa || !val[0]) goto err;
    267.   

crypto/bn/bn_ctx.c:288:1: Parameter `ctx->pool.used`
      286.   	}
      287.   
      288. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
      289.   	{
      290.   	BIGNUM *ret;

crypto/bn/bn_exp.c:281:7: Call
    279.   		}
    280.   
    281.   	if (!BN_nnmod(val[0],a,m,ctx)) goto err;		/* 1 */
                 ^
    282.   	if (BN_is_zero(val[0]))
    283.   		{

crypto/bn/bn_mod.c:127:1: Parameter `ctx->pool.used`
      125.   
      126.   
      127. > int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
      128.   	{
      129.   	/* like BN_mod, but returns non-negative remainder

crypto/bn/bn_mod.c:132:8: Call
      130.   	 * (i.e.,  0 <= r < |d|  always holds) */
      131.   
      132.   	if (!(BN_mod(r,m,d,ctx)))
                    ^
      133.   		return 0;
      134.   	if (!r->neg)

crypto/bn/bn_div.c:179:1: Parameter `ctx->pool.used`
        177.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
        178.    */
        179. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
        180.   	   BN_CTX *ctx)
        181.   	{

crypto/bn/bn_div.c:190:10: Call
        188.   	if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))
        189.   		{
        190.   		return BN_div_no_branch(dv, rm, num, divisor, ctx);
                        ^
        191.   		}
        192.   

crypto/bn/bn_div.c:409:1: Parameter `ctx->pool.used`
          407.    * branches that may leak sensitive information.
          408.    */
          409. > int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, 
          410.   	const BIGNUM *divisor, BN_CTX *ctx)
          411.   	{

crypto/bn/bn_div.c:430:6: Call
          428.   
          429.   	BN_CTX_start(ctx);
          430.   	tmp=BN_CTX_get(ctx);
                      ^
          431.   	snum=BN_CTX_get(ctx);
          432.   	sdiv=BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:288:1: Parameter `ctx->pool.used`
            286.   	}
            287.   
            288. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
            289.   	{
            290.   	BIGNUM *ret;

crypto/bn/bn_div.c:431:7: Call
          429.   	BN_CTX_start(ctx);
          430.   	tmp=BN_CTX_get(ctx);
          431.   	snum=BN_CTX_get(ctx);
                       ^
          432.   	sdiv=BN_CTX_get(ctx);
          433.   	if (dv == NULL)

crypto/bn/bn_ctx.c:288:1: Parameter `ctx->pool.used`
            286.   	}
            287.   
            288. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
            289.   	{
            290.   	BIGNUM *ret;

crypto/bn/bn_div.c:432:7: Call
          430.   	tmp=BN_CTX_get(ctx);
          431.   	snum=BN_CTX_get(ctx);
          432.   	sdiv=BN_CTX_get(ctx);
                       ^
          433.   	if (dv == NULL)
          434.   		res=BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:288:1: Parameter `ctx->pool.used`
            286.   	}
            287.   
            288. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
            289.   	{
            290.   	BIGNUM *ret;

crypto/bn/bn_div.c:622:2: Call
          620.   		}
          621.   	bn_correct_top(res);
          622.   	BN_CTX_end(ctx);
                  ^
          623.   	return(1);
          624.   err:

crypto/bn/bn_ctx.c:270:1: Parameter `ctx->pool.used`
            268.   	}
            269.   
            270. > void BN_CTX_end(BN_CTX *ctx)
            271.   	{
            272.   	CTXDBG_ENTRY("BN_CTX_end", ctx);

crypto/bn/bn_ctx.c:280:4: Call
            278.   		/* Does this stack frame have anything to release? */
            279.   		if(fp < ctx->used)
            280.   			BN_POOL_release(&ctx->pool, ctx->used - fp);
                      ^
            281.   		ctx->used = fp;
            282.   		/* Unjam "too_many" in case "get" had failed */

crypto/bn/bn_ctx.c:438:1: <LHS trace>
436.   	}
437.   
438. > static void BN_POOL_release(BN_POOL *p, unsigned int num)
439.   	{
440.   	unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;

crypto/bn/bn_ctx.c:438:1: Parameter `p->used`
              436.   	}
              437.   
              438. > static void BN_POOL_release(BN_POOL *p, unsigned int num)
              439.   	{
              440.   	unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;

crypto/bn/bn_ctx.c:440:2: Binary operation: ([0, +oo] - 1):unsigned32 by call to `BN_mod_exp` 
              438.   static void BN_POOL_release(BN_POOL *p, unsigned int num)
              439.   	{
              440.   	unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
                      ^
              441.   	p->used -= num;
              442.   	while(num--)

static void BN_POOL_release(BN_POOL *p, unsigned int num)
	{
	unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
	p->used -= num;
	while(num--)
		{
		bn_check_top(p->current->vals + offset);
		if(!offset)
			{
			offset = BN_CTX_POOL_SIZE - 1;
			p->current = p->current->prev;
			}
		else
			offset--;
		}
	}
['BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n\t{\n\tBIGNUM *ret = in;\n\tint err = 1;\n\tint r;\n\tBIGNUM *A, *b, *q, *t, *x, *y;\n\tint e, i, j;\n\tif (!BN_is_odd(p) || BN_abs_is_word(p, 1))\n\t\t{\n\t\tif (BN_abs_is_word(p, 2))\n\t\t\t{\n\t\t\tif (ret == NULL)\n\t\t\t\tret = BN_new();\n\t\t\tif (ret == NULL)\n\t\t\t\tgoto end;\n\t\t\tif (!BN_set_word(ret, BN_is_bit_set(a, 0)))\n\t\t\t\t{\n\t\t\t\tif (ret != in)\n\t\t\t\t\tBN_free(ret);\n\t\t\t\treturn NULL;\n\t\t\t\t}\n\t\t\tbn_check_top(ret);\n\t\t\treturn ret;\n\t\t\t}\n\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n\t\treturn(NULL);\n\t\t}\n\tif (BN_is_zero(a) || BN_is_one(a))\n\t\t{\n\t\tif (ret == NULL)\n\t\t\tret = BN_new();\n\t\tif (ret == NULL)\n\t\t\tgoto end;\n\t\tif (!BN_set_word(ret, BN_is_one(a)))\n\t\t\t{\n\t\t\tif (ret != in)\n\t\t\t\tBN_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tbn_check_top(ret);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tb = BN_CTX_get(ctx);\n\tq = BN_CTX_get(ctx);\n\tt = BN_CTX_get(ctx);\n\tx = BN_CTX_get(ctx);\n\ty = BN_CTX_get(ctx);\n\tif (y == NULL) goto end;\n\tif (ret == NULL)\n\t\tret = BN_new();\n\tif (ret == NULL) goto end;\n\tif (!BN_nnmod(A, a, p, ctx)) goto end;\n\te = 1;\n\twhile (!BN_is_bit_set(p, e))\n\t\te++;\n\tif (e == 1)\n\t\t{\n\t\tif (!BN_rshift(q, p, 2)) goto end;\n\t\tq->neg = 0;\n\t\tif (!BN_add_word(q, 1)) goto end;\n\t\tif (!BN_mod_exp(ret, A, q, p, ctx)) goto end;\n\t\terr = 0;\n\t\tgoto vrfy;\n\t\t}\n\tif (e == 2)\n\t\t{\n\t\tif (!BN_mod_lshift1_quick(t, A, p)) goto end;\n\t\tif (!BN_rshift(q, p, 3)) goto end;\n\t\tq->neg = 0;\n\t\tif (!BN_mod_exp(b, t, q, p, ctx)) goto end;\n\t\tif (!BN_mod_sqr(y, b, p, ctx)) goto end;\n\t\tif (!BN_mod_mul(t, t, y, p, ctx)) goto end;\n\t\tif (!BN_sub_word(t, 1)) goto end;\n\t\tif (!BN_mod_mul(x, A, b, p, ctx)) goto end;\n\t\tif (!BN_mod_mul(x, x, t, p, ctx)) goto end;\n\t\tif (!BN_copy(ret, x)) goto end;\n\t\terr = 0;\n\t\tgoto vrfy;\n\t\t}\n\tif (!BN_copy(q, p)) goto end;\n\tq->neg = 0;\n\ti = 2;\n\tdo\n\t\t{\n\t\tif (i < 22)\n\t\t\t{\n\t\t\tif (!BN_set_word(y, i)) goto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;\n\t\t\tif (BN_ucmp(y, p) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;\n\t\t\t\t}\n\t\t\tif (BN_is_zero(y))\n\t\t\t\tif (!BN_set_word(y, i)) goto end;\n\t\t\t}\n\t\tr = BN_kronecker(y, q, ctx);\n\t\tif (r < -1) goto end;\n\t\tif (r == 0)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\twhile (r == 1 && ++i < 82);\n\tif (r != -1)\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);\n\t\tgoto end;\n\t\t}\n\tif (!BN_rshift(q, q, e)) goto end;\n\tif (!BN_mod_exp(y, y, q, p, ctx)) goto end;\n\tif (BN_is_one(y))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n\t\tgoto end;\n\t\t}\n\tif (!BN_rshift1(t, q)) goto end;\n\tif (BN_is_zero(t))\n\t\t{\n\t\tif (!BN_nnmod(t, A, p, ctx)) goto end;\n\t\tif (BN_is_zero(t))\n\t\t\t{\n\t\t\tBN_zero(ret);\n\t\t\terr = 0;\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\tif (!BN_one(x)) goto end;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mod_exp(x, A, t, p, ctx)) goto end;\n\t\tif (BN_is_zero(x))\n\t\t\t{\n\t\t\tBN_zero(ret);\n\t\t\terr = 0;\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (!BN_mod_sqr(b, x, p, ctx)) goto end;\n\tif (!BN_mod_mul(b, b, A, p, ctx)) goto end;\n\tif (!BN_mod_mul(x, x, A, p, ctx)) goto end;\n\twhile (1)\n\t\t{\n\t\tif (BN_is_one(b))\n\t\t\t{\n\t\t\tif (!BN_copy(ret, x)) goto end;\n\t\t\terr = 0;\n\t\t\tgoto vrfy;\n\t\t\t}\n\t\ti = 1;\n\t\tif (!BN_mod_sqr(t, b, p, ctx)) goto end;\n\t\twhile (!BN_is_one(t))\n\t\t\t{\n\t\t\ti++;\n\t\t\tif (i == e)\n\t\t\t\t{\n\t\t\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif (!BN_mod_mul(t, t, t, p, ctx)) goto end;\n\t\t\t}\n\t\tif (!BN_copy(t, y)) goto end;\n\t\tfor (j = e - i - 1; j > 0; j--)\n\t\t\t{\n\t\t\tif (!BN_mod_sqr(t, t, p, ctx)) goto end;\n\t\t\t}\n\t\tif (!BN_mod_mul(y, t, t, p, ctx)) goto end;\n\t\tif (!BN_mod_mul(x, x, t, p, ctx)) goto end;\n\t\tif (!BN_mod_mul(b, b, y, p, ctx)) goto end;\n\t\te = i;\n\t\t}\n vrfy:\n\tif (!err)\n\t\t{\n\t\tif (!BN_mod_sqr(x, ret, p, ctx))\n\t\t\terr = 1;\n\t\tif (!err && 0 != BN_cmp(x, A))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n\t\t\terr = 1;\n\t\t\t}\n\t\t}\n end:\n\tif (err)\n\t\t{\n\t\tif (ret != NULL && ret != in)\n\t\t\t{\n\t\t\tBN_clear_free(ret);\n\t\t\t}\n\t\tret = NULL;\n\t\t}\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn ret;\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t   BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg   = 0;\n\twnum.d     = &(snum->d[loop]);\n\twnum.top   = div_n;\n\twnum.dmax  = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q);  qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t       BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{\n#  ifdef MONT_EXP_WORD\n\t\tif (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0))\n\t\t\t{\n\t\t\tBN_ULONG A = a->d[0];\n\t\t\tret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);\n\t\t\t}\n\t\telse\n#  endif\n\t\t\tret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);\n\t\t}\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\tbn_check_top(r);\n\treturn(ret);\n\t}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t    const BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_RECP_CTX recp;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_RECP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n\t\treturn -1;\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(r);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\taa = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif(!aa || !val[0]) goto err;\n\tBN_RECP_CTX_init(&recp);\n\tif (m->neg)\n\t\t{\n\t\tif (!BN_copy(aa, m)) goto err;\n\t\taa->neg = 0;\n\t\tif (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;\n\t\t}\n\tif (!BN_nnmod(val[0],a,m,ctx)) goto err;\n\tif (BN_is_zero(val[0]))\n\t\t{\n\t\tBN_zero(r);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_reciprocal(aa,val[0],val[0],&recp,ctx))\n\t\t\tgoto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_reciprocal(val[i],val[i-1],\n\t\t\t\t\t\taa,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_one(r)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],&recp,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\tBN_RECP_CTX_free(&recp);\n\tbn_check_top(r);\n\treturn(ret);\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg   = 0;\n\twnum.d     = &(snum->d[loop]);\n\twnum.top   = div_n;\n\twnum.dmax  = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q);  qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']