crypto/rsa/rsa_chk.c:78: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: 12 by call to `BN_is_prime`.
Showing all 42 steps of the trace


crypto/rsa/rsa_chk.c:68:8: Call
66.   	l = BN_new();
67.   	m = BN_new();
68.   	ctx = BN_CTX_new();
             ^
69.   	if (i == NULL || j == NULL || k == NULL || l == NULL ||
70.   		m == NULL || ctx == NULL)

crypto/bn/bn_ctx.c:79:2: Call
  77.   		}
  78.   
  79.   	BN_CTX_init(ret);
         ^
  80.   	ret->flags=BN_FLG_MALLOCED;
  81.   	return(ret);

crypto/bn/bn_ctx.c:89:2: Assignment
    87.   	ctx->tos = 0;
    88.   	ctx->flags = 0;
    89.   	ctx->depth = 0;
           ^
    90.   	ctx->too_many = 0;
    91.   	for (i = 0; i < BN_CTX_NUM; i++)

crypto/rsa/rsa_chk.c:78:6: Call
76.   	
77.   	/* p prime? */
78.   	r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);
           ^
79.   	if (r != 1)
80.   		{

crypto/bn/bn_prime.c:211:1: Parameter `ctx_passed->depth`
  209.   	}
  210.   
  211. > int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int,int,void *),
  212.   	BN_CTX *ctx_passed, void *cb_arg)
  213.   	{

crypto/bn/bn_prime.c:214:9: Call
  212.   	BN_CTX *ctx_passed, void *cb_arg)
  213.   	{
  214.   	return BN_is_prime_fasttest(a, checks, callback, ctx_passed, cb_arg, 0);
                 ^
  215.   	}
  216.   

crypto/bn/bn_prime.c:217:1: Parameter `ctx_passed->depth`
    215.   	}
    216.   
    217. > int BN_is_prime_fasttest(const BIGNUM *a, int checks,
    218.   		void (*callback)(int,int,void *),
    219.   		BN_CTX *ctx_passed, void *cb_arg,

crypto/bn/bn_prime.c:248:2: Call
    246.   		if ((ctx=BN_CTX_new()) == NULL)
    247.   			goto err;
    248.   	BN_CTX_start(ctx);
            ^
    249.   
    250.   	/* A := abs(a) */

crypto/bn/bn_ctx.c:108:1: Parameter `ctx->depth`
      106.   	}
      107.   
      108. > void BN_CTX_start(BN_CTX *ctx)
      109.   	{
      110.   	if (ctx->depth < BN_CTX_NUM_POS)

crypto/bn/bn_ctx.c:112:2: Assignment
      110.   	if (ctx->depth < BN_CTX_NUM_POS)
      111.   		ctx->pos[ctx->depth] = ctx->tos;
      112.   	ctx->depth++;
              ^
      113.   	}
      114.   

crypto/bn/bn_prime.c:261:7: Call
    259.   	else
    260.   		A = a;
    261.   	A1 = BN_CTX_get(ctx);
                 ^
    262.   	A1_odd = BN_CTX_get(ctx);
    263.   	check = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
      114.   
      115.   
      116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
      117.   	{
      118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_prime.c:262:11: Call
    260.   		A = a;
    261.   	A1 = BN_CTX_get(ctx);
    262.   	A1_odd = BN_CTX_get(ctx);
                     ^
    263.   	check = BN_CTX_get(ctx);
    264.   	if (check == NULL) goto err;

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
      114.   
      115.   
      116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
      117.   	{
      118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_prime.c:263:10: Call
    261.   	A1 = BN_CTX_get(ctx);
    262.   	A1_odd = BN_CTX_get(ctx);
    263.   	check = BN_CTX_get(ctx);
                    ^
    264.   	if (check == NULL) goto err;
    265.   

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
      114.   
      115.   
      116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
      117.   	{
      118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_prime.c:288:7: Call
    286.   	if (mont == NULL)
    287.   		goto err;
    288.   	if (!BN_MONT_CTX_set(mont, A, ctx))
                 ^
    289.   		goto err;
    290.   	

crypto/bn/bn_mont.c:270:1: Parameter `ctx->depth`
      268.   	}
      269.   
      270. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
      271.   	{
      272.   	BIGNUM Ri,*R;

crypto/bn/bn_mont.c:294:8: Call
      292.   		tmod.neg=mod->neg;
      293.   							/* Ri = R^-1 mod N*/
      294.   		if ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)
                    ^
      295.   			goto err;
      296.   		BN_lshift(&Ri,&Ri,BN_BITS2);		/* R*Ri */

crypto/bn/bn_gcd.c:147:1: Parameter `ctx->depth`
        145.   
        146.   /* solves ax == 1 (mod n) */
        147. > BIGNUM *BN_mod_inverse(BIGNUM *in,
        148.   	const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
        149.   	{

crypto/bn/bn_gcd.c:157:2: Call
        155.   	bn_check_top(n);
        156.   
        157.   	BN_CTX_start(ctx);
                ^
        158.   	A = BN_CTX_get(ctx);
        159.   	B = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:108:1: Parameter `ctx->depth`
          106.   	}
          107.   
          108. > void BN_CTX_start(BN_CTX *ctx)
          109.   	{
          110.   	if (ctx->depth < BN_CTX_NUM_POS)

crypto/bn/bn_ctx.c:112:2: Assignment
          110.   	if (ctx->depth < BN_CTX_NUM_POS)
          111.   		ctx->pos[ctx->depth] = ctx->tos;
          112.   	ctx->depth++;
                  ^
          113.   	}
          114.   

crypto/bn/bn_gcd.c:158:6: Call
        156.   
        157.   	BN_CTX_start(ctx);
        158.   	A = BN_CTX_get(ctx);
                    ^
        159.   	B = BN_CTX_get(ctx);
        160.   	X = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:159:6: Call
        157.   	BN_CTX_start(ctx);
        158.   	A = BN_CTX_get(ctx);
        159.   	B = BN_CTX_get(ctx);
                    ^
        160.   	X = BN_CTX_get(ctx);
        161.   	D = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:160:6: Call
        158.   	A = BN_CTX_get(ctx);
        159.   	B = BN_CTX_get(ctx);
        160.   	X = BN_CTX_get(ctx);
                    ^
        161.   	D = BN_CTX_get(ctx);
        162.   	M = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:161:6: Call
        159.   	B = BN_CTX_get(ctx);
        160.   	X = BN_CTX_get(ctx);
        161.   	D = BN_CTX_get(ctx);
                    ^
        162.   	M = BN_CTX_get(ctx);
        163.   	Y = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:162:6: Call
        160.   	X = BN_CTX_get(ctx);
        161.   	D = BN_CTX_get(ctx);
        162.   	M = BN_CTX_get(ctx);
                    ^
        163.   	Y = BN_CTX_get(ctx);
        164.   	if (Y == NULL) goto err;

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:163:6: Call
        161.   	D = BN_CTX_get(ctx);
        162.   	M = BN_CTX_get(ctx);
        163.   	Y = BN_CTX_get(ctx);
                    ^
        164.   	if (Y == NULL) goto err;
        165.   

crypto/bn/bn_ctx.c:116:1: Parameter `ctx->depth`
          114.   
          115.   
          116. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
          117.   	{
          118.   	/* Note: If BN_CTX_get is ever changed to allocate BIGNUMs dynamically,

crypto/bn/bn_gcd.c:208:2: Call
        206.   err:
        207.   	if ((ret == NULL) && (in == NULL)) BN_free(R);
        208.   	BN_CTX_end(ctx);
                ^
        209.   	return(ret);
        210.   	}

crypto/bn/bn_ctx.c:136:1: <Offset trace>
134.   	}
135.   
136. > void BN_CTX_end(BN_CTX *ctx)
137.   	{
138.   	if (ctx == NULL) return;

crypto/bn/bn_ctx.c:136:1: Parameter `ctx->depth`
          134.   	}
          135.   
          136. > void BN_CTX_end(BN_CTX *ctx)
          137.   	{
          138.   	if (ctx == NULL) return;

crypto/bn/bn_ctx.c:147:2: Assignment
          145.   
          146.   	ctx->too_many = 0;
          147.   	ctx->depth--;
                  ^
          148.   	if (ctx->depth < BN_CTX_NUM_POS)
          149.   		ctx->tos = ctx->pos[ctx->depth];

crypto/bn/bn_ctx.c:136:1: <Length trace>
134.   	}
135.   
136. > void BN_CTX_end(BN_CTX *ctx)
137.   	{
138.   	if (ctx == NULL) return;

crypto/bn/bn_ctx.c:136:1: Parameter `ctx->pos[*]`
          134.   	}
          135.   
          136. > void BN_CTX_end(BN_CTX *ctx)
          137.   	{
          138.   	if (ctx == NULL) return;

crypto/bn/bn_ctx.c:149:14: Array access: Offset: [-1, +oo] Size: 12 by call to `BN_is_prime` 
          147.   	ctx->depth--;
          148.   	if (ctx->depth < BN_CTX_NUM_POS)
          149.   		ctx->tos = ctx->pos[ctx->depth];
                              ^
          150.   	}

void BN_CTX_end(BN_CTX *ctx)
	{
	if (ctx == NULL) return;
	assert(ctx->depth > 0);
	if (ctx->depth == 0)
		BN_CTX_start(ctx);
	ctx->too_many = 0;
	ctx->depth--;
	if (ctx->depth < BN_CTX_NUM_POS)
		ctx->tos = ctx->pos[ctx->depth];
	}
['int RSA_check_key(const RSA *key)\n\t{\n\tBIGNUM *i, *j, *k, *l, *m;\n\tBN_CTX *ctx;\n\tint r;\n\tint ret=1;\n\ti = BN_new();\n\tj = BN_new();\n\tk = BN_new();\n\tl = BN_new();\n\tm = BN_new();\n\tctx = BN_CTX_new();\n\tif (i == NULL || j == NULL || k == NULL || l == NULL ||\n\t\tm == NULL || ctx == NULL)\n\t\t{\n\t\tret = -1;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tr = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);\n\tif (r != 1)\n\t\t{\n\t\tret = r;\n\t\tif (r != 0)\n\t\t\tgoto err;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);\n\t\t}\n\tr = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL);\n\tif (r != 1)\n\t\t{\n\t\tret = r;\n\t\tif (r != 0)\n\t\t\tgoto err;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);\n\t\t}\n\tr = BN_mul(i, key->p, key->q, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tif (BN_cmp(i, key->n) != 0)\n\t\t{\n\t\tret = 0;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n\t\t}\n\tr = BN_sub(i, key->p, BN_value_one());\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_sub(j, key->q, BN_value_one());\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_mul(l, i, j, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_gcd(m, i, j, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_div(k, NULL, l, m, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_mod_mul(i, key->d, key->e, k, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tif (!BN_is_one(i))\n\t\t{\n\t\tret = 0;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n\t\t}\n\tif (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL)\n\t\t{\n\t\tr = BN_sub(i, key->p, BN_value_one());\n\t\tif (!r) { ret = -1; goto err; }\n\t\tr = BN_mod(j, key->d, i, ctx);\n\t\tif (!r) { ret = -1; goto err; }\n\t\tif (BN_cmp(j, key->dmp1) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_DMP1_NOT_CONGRUENT_TO_D);\n\t\t\t}\n\t\tr = BN_sub(i, key->q, BN_value_one());\n\t\tif (!r) { ret = -1; goto err; }\n\t\tr = BN_mod(j, key->d, i, ctx);\n\t\tif (!r) { ret = -1; goto err; }\n\t\tif (BN_cmp(j, key->dmq1) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n\t\t\t}\n\t\tif(!BN_mod_inverse(i, key->q, key->p, ctx))\n\t\t\t{\n\t\t\tret = -1;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (BN_cmp(i, key->iqmp) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_IQMP_NOT_INVERSE_OF_Q);\n\t\t\t}\n\t\t}\n err:\n\tif (i != NULL) BN_free(i);\n\tif (j != NULL) BN_free(j);\n\tif (k != NULL) BN_free(k);\n\tif (l != NULL) BN_free(l);\n\tif (m != NULL) BN_free(m);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn (ret);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)OPENSSL_malloc(sizeof(BN_CTX));\n\tif (ret == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBN_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'void BN_CTX_init(BN_CTX *ctx)\n\t{\n\tint i;\n\tctx->tos = 0;\n\tctx->flags = 0;\n\tctx->depth = 0;\n\tctx->too_many = 0;\n\tfor (i = 0; i < BN_CTX_NUM; i++)\n\t\tBN_init(&(ctx->bn[i]));\n\t}', 'int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int,int,void *),\n\tBN_CTX *ctx_passed, void *cb_arg)\n\t{\n\treturn BN_is_prime_fasttest(a, checks, callback, ctx_passed, cb_arg, 0);\n\t}', 'int BN_is_prime_fasttest(const BIGNUM *a, int checks,\n\t\tvoid (*callback)(int,int,void *),\n\t\tBN_CTX *ctx_passed, void *cb_arg,\n\t\tint do_trial_division)\n\t{\n\tint i, j, ret = -1;\n\tint k;\n\tBN_CTX *ctx = NULL;\n\tBIGNUM *A1, *A1_odd, *check;\n\tBN_MONT_CTX *mont = NULL;\n\tconst BIGNUM *A = NULL;\n\tif (checks == BN_prime_checks)\n\t\tchecks = BN_prime_checks_for_size(BN_num_bits(a));\n\tif (!BN_is_odd(a))\n\t\treturn(0);\n\tif (do_trial_division)\n\t\t{\n\t\tfor (i = 1; i < NUMPRIMES; i++)\n\t\t\tif (BN_mod_word(a, primes[i]) == 0)\n\t\t\t\treturn 0;\n\t\tif (callback != NULL) callback(1, -1, cb_arg);\n\t\t}\n\tif (ctx_passed != NULL)\n\t\tctx = ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL)\n\t\t\tgoto err;\n\tBN_CTX_start(ctx);\n\tif (a->neg)\n\t\t{\n\t\tBIGNUM *t;\n\t\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\t\tBN_copy(t, a);\n\t\tt->neg = 0;\n\t\tA = t;\n\t\t}\n\telse\n\t\tA = a;\n\tA1 = BN_CTX_get(ctx);\n\tA1_odd = BN_CTX_get(ctx);\n\tcheck = BN_CTX_get(ctx);\n\tif (check == NULL) goto err;\n\tif (!BN_copy(A1, A))\n\t\tgoto err;\n\tif (!BN_sub_word(A1, 1))\n\t\tgoto err;\n\tif (BN_is_zero(A1))\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tk = 1;\n\twhile (!BN_is_bit_set(A1, k))\n\t\tk++;\n\tif (!BN_rshift(A1_odd, A1, k))\n\t\tgoto err;\n\tmont = BN_MONT_CTX_new();\n\tif (mont == NULL)\n\t\tgoto err;\n\tif (!BN_MONT_CTX_set(mont, A, ctx))\n\t\tgoto err;\n\tfor (i = 0; i < checks; i++)\n\t\t{\n\t\tif (!BN_pseudo_rand(check, BN_num_bits(A1), 0, 0))\n\t\t\tgoto err;\n\t\tif (BN_cmp(check, A1) >= 0)\n\t\t\tif (!BN_sub(check, check, A1))\n\t\t\t\tgoto err;\n\t\tif (!BN_add_word(check, 1))\n\t\t\tgoto err;\n\t\tj = witness(check, A, A1, A1_odd, k, ctx, mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (callback != NULL) callback(1,i,cb_arg);\n\t\t}\n\tret=1;\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tif (ctx_passed == NULL)\n\t\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (mont != NULL)\n\t\tBN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->pos[ctx->depth] = ctx->tos;\n\tctx->depth++;\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tif (ctx->depth > BN_CTX_NUM_POS || ctx->tos >= BN_CTX_NUM)\n\t\t{\n\t\tif (!ctx->too_many)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\t\tctx->too_many = 1;\n\t\t\t}\n\t\treturn NULL;\n\t\t}\n\treturn (&(ctx->bn[ctx->tos++]));\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\tBN_sub_word(&Ri,1);\n\t\telse\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(mod);\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n\t\tBN_sub_word(&Ri,1);\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\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,*R=NULL;\n\tBIGNUM *T,*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\tif (Y == 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_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\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{ if (!BN_mod(R,Y,n,ctx)) goto err; }\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\treturn(ret);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tif (ctx == NULL) return;\n\tassert(ctx->depth > 0);\n\tif (ctx->depth == 0)\n\t\tBN_CTX_start(ctx);\n\tctx->too_many = 0;\n\tctx->depth--;\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->tos = ctx->pos[ctx->depth];\n\t}']