crypto/dsa/dsa_ossl.c:251: error: INTEGER_OVERFLOW_L2
  ([0, +oo] - 1):unsigned32 by call to `BN_MONT_CTX_set_locked`.
Showing all 41 steps of the trace


crypto/dsa/dsa_ossl.c:216:1: Parameter `ctx_in->pool.used`
214.   	}
215.   
216. > static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
217.   	{
218.   	BN_CTX *ctx;

crypto/dsa/dsa_ossl.c:251:8: Call
249.   	if (dsa->flags & DSA_FLAG_CACHE_MONT_P)
250.   		{
251.   		if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
              ^
252.   						CRYPTO_LOCK_DSA,
253.   						dsa->p, ctx))

crypto/bn/bn_mont.c:478:1: Parameter `ctx->pool.used`
  476.   	}
  477.   
  478. > BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
  479.   					const BIGNUM *mod, BN_CTX *ctx)
  480.   	{

crypto/bn/bn_mont.c:494:16: Call
  492.   			{
  493.   			ret = BN_MONT_CTX_new();
  494.   			if (ret && !BN_MONT_CTX_set(ret, mod, ctx))
                        ^
  495.   				BN_MONT_CTX_free(ret);
  496.   			else

crypto/bn/bn_mont.c:355:1: Parameter `ctx->pool.used`
    353.   	}
    354.   
    355. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
    356.   	{
    357.   	int ret = 0;

crypto/bn/bn_mont.c:361:11: Call
    359.   
    360.   	BN_CTX_start(ctx);
    361.   	if((Ri = BN_CTX_get(ctx)) == NULL) goto err;
                     ^
    362.   	R= &(mont->RR);					/* grab RR as a temp */
    363.   	if (!BN_copy(&(mont->N),mod)) goto err;		/* Set N */

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_mont.c:421:8: Call
    419.   		tmod.top = buf[0] != 0 ? 1 : 0;
    420.   							/* Ri = R^-1 mod N*/
    421.   		if ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)
                  ^
    422.   			goto err;
    423.   		if (!BN_lshift(Ri,Ri,BN_BITS2)) goto err; /* R*Ri */

crypto/bn/bn_gcd.c:209:1: Parameter `ctx->pool.used`
      207.           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
      208.   
      209. > BIGNUM *BN_mod_inverse(BIGNUM *in,
      210.   	const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
      211.   	{

crypto/bn/bn_gcd.c:218:10: Call
      216.   	if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))
      217.   		{
      218.   		return BN_mod_inverse_no_branch(in, a, n, ctx);
                      ^
      219.   		}
      220.   

crypto/bn/bn_gcd.c:507:1: Parameter `ctx->pool.used`
        505.    * It does not contain branches that may leak sensitive information.
        506.    */
        507. > static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
        508.   	const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
        509.   	{

crypto/bn/bn_gcd.c:520:6: Call
        518.   
        519.   	BN_CTX_start(ctx);
        520.   	A = BN_CTX_get(ctx);
                    ^
        521.   	B = BN_CTX_get(ctx);
        522.   	X = 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_gcd.c:521:6: Call
        519.   	BN_CTX_start(ctx);
        520.   	A = BN_CTX_get(ctx);
        521.   	B = BN_CTX_get(ctx);
                    ^
        522.   	X = BN_CTX_get(ctx);
        523.   	D = 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_gcd.c:522:6: Call
        520.   	A = BN_CTX_get(ctx);
        521.   	B = BN_CTX_get(ctx);
        522.   	X = BN_CTX_get(ctx);
                    ^
        523.   	D = BN_CTX_get(ctx);
        524.   	M = 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_gcd.c:523:6: Call
        521.   	B = BN_CTX_get(ctx);
        522.   	X = BN_CTX_get(ctx);
        523.   	D = BN_CTX_get(ctx);
                    ^
        524.   	M = BN_CTX_get(ctx);
        525.   	Y = 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_gcd.c:524:6: Call
        522.   	X = BN_CTX_get(ctx);
        523.   	D = BN_CTX_get(ctx);
        524.   	M = BN_CTX_get(ctx);
                    ^
        525.   	Y = BN_CTX_get(ctx);
        526.   	T = 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_gcd.c:525:6: Call
        523.   	D = BN_CTX_get(ctx);
        524.   	M = BN_CTX_get(ctx);
        525.   	Y = BN_CTX_get(ctx);
                    ^
        526.   	T = BN_CTX_get(ctx);
        527.   	if (T == NULL) 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_gcd.c:526:6: Call
        524.   	M = BN_CTX_get(ctx);
        525.   	Y = BN_CTX_get(ctx);
        526.   	T = BN_CTX_get(ctx);
                    ^
        527.   	if (T == NULL) goto err;
        528.   

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_gcd.c:548:8: Call
        546.   		pB = &local_B;
        547.   		BN_with_flags(pB, B, BN_FLG_CONSTTIME);	
        548.   		if (!BN_nnmod(B, pB, A, ctx)) goto err;
                      ^
        549.   		}
        550.   	sign = -1;

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:181:1: Parameter `ctx->pool.used`
            179.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
            180.    */
            181. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
            182.   	   BN_CTX *ctx)
            183.   	{

crypto/bn/bn_div.c:227:6: Call
            225.   
            226.   	BN_CTX_start(ctx);
            227.   	tmp=BN_CTX_get(ctx);
                        ^
            228.   	snum=BN_CTX_get(ctx);
            229.   	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:228:7: Call
            226.   	BN_CTX_start(ctx);
            227.   	tmp=BN_CTX_get(ctx);
            228.   	snum=BN_CTX_get(ctx);
                         ^
            229.   	sdiv=BN_CTX_get(ctx);
            230.   	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:229:7: Call
            227.   	tmp=BN_CTX_get(ctx);
            228.   	snum=BN_CTX_get(ctx);
            229.   	sdiv=BN_CTX_get(ctx);
                         ^
            230.   	if (dv == NULL)
            231.   		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:441:2: Call
            439.   		}
            440.   	if (no_branch)	bn_correct_top(res);
            441.   	BN_CTX_end(ctx);
                    ^
            442.   	return(1);
            443.   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_MONT_CTX_set_locked` 
                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--;
		}
	}
['static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM k,kq,*K,*kinv=NULL,*r=NULL;\n\tint ret=0;\n\tif (!dsa->p || !dsa->q || !dsa->g)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS);\n\t\treturn 0;\n\t\t}\n\tBN_init(&k);\n\tBN_init(&kq);\n\tif (ctx_in == NULL)\n\t\t{\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tctx=ctx_in;\n\tif ((r=BN_new()) == NULL) goto err;\n\tdo\n\t\tif (!BN_rand_range(&k, dsa->q)) goto err;\n\twhile (BN_is_zero(&k));\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tBN_set_flags(&k, BN_FLG_CONSTTIME);\n\t\t}\n\tif (dsa->flags & DSA_FLAG_CACHE_MONT_P)\n\t\t{\n\t\tif (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n\t\t\t\t\t\tCRYPTO_LOCK_DSA,\n\t\t\t\t\t\tdsa->p, ctx))\n\t\t\tgoto err;\n\t\t}\n\tif ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t{\n\t\tif (!BN_copy(&kq, &k)) goto err;\n\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\tif (BN_num_bits(&kq) <= BN_num_bits(dsa->q))\n\t\t\t{\n\t\t\tif (!BN_add(&kq, &kq, dsa->q)) goto err;\n\t\t\t}\n\t\tK = &kq;\n\t\t}\n\telse\n\t\t{\n\t\tK = &k;\n\t\t}\n\tDSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx,\n\t\t\tdsa->method_mont_p);\n\tif (!BN_mod(r,r,dsa->q,ctx)) goto err;\n\tif ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err;\n\tif (*kinvp != NULL) BN_clear_free(*kinvp);\n\t*kinvp=kinv;\n\tkinv=NULL;\n\tif (*rp != NULL) BN_clear_free(*rp);\n\t*rp=r;\n\tret=1;\nerr:\n\tif (!ret)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);\n\t\tif (r != NULL)\n\t\t\tBN_clear_free(r);\n\t\t}\n\tif (ctx_in == NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&k);\n\tBN_clear_free(&kq);\n\treturn(ret);\n\t}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,\n\t\t\t\t\tconst BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint got_write_lock = 0;\n\tBN_MONT_CTX *ret;\n\tCRYPTO_r_lock(lock);\n\tif (!*pmont)\n\t\t{\n\t\tCRYPTO_r_unlock(lock);\n\t\tCRYPTO_w_lock(lock);\n\t\tgot_write_lock = 1;\n\t\tif (!*pmont)\n\t\t\t{\n\t\t\tret = BN_MONT_CTX_new();\n\t\t\tif (ret && !BN_MONT_CTX_set(ret, mod, ctx))\n\t\t\t\tBN_MONT_CTX_free(ret);\n\t\t\telse\n\t\t\t\t*pmont = ret;\n\t\t\t}\n\t\t}\n\tret = *pmont;\n\tif (got_write_lock)\n\t\tCRYPTO_w_unlock(lock);\n\telse\n\t\tCRYPTO_r_unlock(lock);\n\treturn ret;\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tBN_init(&tmod);\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\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}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\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\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\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 (!no_branch && 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 || tmp == NULL || snum == NULL)\n\t\tgoto 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 (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\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-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\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;\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\t{\n\t\t\tBN_ULONG ql, qh;\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\t\t\t}\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\tif (no_branch)\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}']