當前位置: 首頁>>代碼示例>>C++>>正文


C++ BN_MONT_CTX_set函數代碼示例

本文整理匯總了C++中BN_MONT_CTX_set函數的典型用法代碼示例。如果您正苦於以下問題:C++ BN_MONT_CTX_set函數的具體用法?C++ BN_MONT_CTX_set怎麽用?C++ BN_MONT_CTX_set使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了BN_MONT_CTX_set函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。

示例1: BN_MONT_CTX_set_locked

int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
                           const BIGNUM *mod, BN_CTX *bn_ctx) {
  CRYPTO_MUTEX_lock_read(lock);
  BN_MONT_CTX *ctx = *pmont;
  CRYPTO_MUTEX_unlock_read(lock);

  if (ctx) {
    return 1;
  }

  CRYPTO_MUTEX_lock_write(lock);
  ctx = *pmont;
  if (ctx) {
    goto out;
  }

  ctx = BN_MONT_CTX_new();
  if (ctx == NULL) {
    goto out;
  }
  if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) {
    BN_MONT_CTX_free(ctx);
    ctx = NULL;
    goto out;
  }
  *pmont = ctx;

out:
  CRYPTO_MUTEX_unlock_write(lock);
  return ctx != NULL;
}
開發者ID:LiTianjue,項目名稱:etls,代碼行數:31,代碼來源:montgomery.c

示例2: dsa_sign_setup

static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
	{
	BN_CTX *ctx;
	BIGNUM k,*kinv=NULL,*r=NULL;
	int ret=0;

	if (!dsa->p || !dsa->q || !dsa->g)
		{
		DSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS);
		return 0;
		}
	if (ctx_in == NULL)
		{
		if ((ctx=BN_CTX_new()) == NULL) goto err;
		}
	else
		ctx=ctx_in;

	BN_init(&k);
	if ((r=BN_new()) == NULL) goto err;
	kinv=NULL;

	/* Get random k */
	do
		if (!BN_rand_range(&k, dsa->q)) goto err;
	while (BN_is_zero(&k));

	if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
		{
		if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
			if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,
				dsa->p,ctx)) goto err;
		}

	/* Compute r = (g^k mod p) mod q */
	if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx,
		(BN_MONT_CTX *)dsa->method_mont_p)) goto err;
	if (!BN_mod(r,r,dsa->q,ctx)) goto err;

	/* Compute  part of 's = inv(k) (m + xr) mod q' */
	if ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err;

	if (*kinvp != NULL) BN_clear_free(*kinvp);
	*kinvp=kinv;
	kinv=NULL;
	if (*rp != NULL) BN_clear_free(*rp);
	*rp=r;
	ret=1;
err:
	if (!ret)
		{
		DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);
		if (kinv != NULL) BN_clear_free(kinv);
		if (r != NULL) BN_clear_free(r);
		}
	if (ctx_in == NULL) BN_CTX_free(ctx);
	if (kinv != NULL) BN_clear_free(kinv);
	BN_clear_free(&k);
	return(ret);
	}
開發者ID:aosm,項目名稱:OpenSSL096,代碼行數:60,代碼來源:dsa_ossl.c

示例3: do_mul_exp

void do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
	{
	int i,k;
	double tm;
	long num;
	BN_MONT_CTX m;

	memset(&m,0,sizeof(m));

	num=BASENUM;
	for (i=0; i<NUM_SIZES; i++)
		{
		BN_rand(a,sizes[i],1,0);
		BN_rand(b,sizes[i],1,0);
		BN_rand(c,sizes[i],1,1);
		BN_mod(a,a,c,ctx);
		BN_mod(b,b,c,ctx);

		BN_MONT_CTX_set(&m,c,ctx);

		Time_F(START);
		for (k=0; k<num; k++)
			BN_mod_exp_mont(r,a,b,c,ctx,&m);
		tm=Time_F(STOP);
		printf("mul %4d ^ %4d %% %d -> %8.3fms %5.1f\n",sizes[i],sizes[i],sizes[i],tm*1000.0/num,tm*mul_c[i]/num);
		num/=7;
		if (num <= 0) num=1;
		}

	}
開發者ID:robacklin,項目名稱:uclinux-linux,代碼行數:30,代碼來源:expspeed.c

示例4: compute_key

static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)
	{
	BN_CTX ctx;
	BN_MONT_CTX *mont;
	BIGNUM *tmp;
	int ret= -1;

	BN_CTX_init(&ctx);
	BN_CTX_start(&ctx);
	tmp = BN_CTX_get(&ctx);
	
	if (dh->priv_key == NULL)
		goto err;

	if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
		{
		if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
			if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
				dh->p,&ctx)) goto err;
		}

	mont=(BN_MONT_CTX *)dh->method_mont_p;
	if (!dh->meth->bn_mod_exp(dh, tmp, pub_key,
				dh->priv_key,dh->p,&ctx,mont))
		goto err;

	ret=BN_bn2bin(tmp,key);
err:
	BN_CTX_end(&ctx);
	BN_CTX_free(&ctx);
	return(ret);
	}
開發者ID:robacklin,項目名稱:uclinux-linux,代碼行數:32,代碼來源:dh_key.c

示例5: ec_precompute_mont_data

/*
 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
 * returns one on success. On error it returns zero.
 */
int ec_precompute_mont_data(EC_GROUP *group)
{
    BN_CTX *ctx = BN_CTX_new();
    int ret = 0;

    if (!EC_GROUP_VERSION(group))
        goto err;

    if (group->mont_data) {
        BN_MONT_CTX_free(group->mont_data);
        group->mont_data = NULL;
    }

    if (ctx == NULL)
        goto err;

    group->mont_data = BN_MONT_CTX_new();
    if (!group->mont_data)
        goto err;

    if (!BN_MONT_CTX_set(group->mont_data, &group->order, ctx)) {
        BN_MONT_CTX_free(group->mont_data);
        group->mont_data = NULL;
        goto err;
    }

    ret = 1;

 err:

    if (ctx)
        BN_CTX_free(ctx);
    return ret;
}
開發者ID:03050903,項目名稱:godot,代碼行數:38,代碼來源:ec_lib.c

示例6: BN_MONT_CTX_set_locked

BN_MONT_CTX *
BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, const BIGNUM *mod,
    BN_CTX *ctx)
{
	int got_write_lock = 0;
	BN_MONT_CTX *ret;

	CRYPTO_r_lock(lock);
	if (!*pmont) {
		CRYPTO_r_unlock(lock);
		CRYPTO_w_lock(lock);
		got_write_lock = 1;

		if (!*pmont) {
			ret = BN_MONT_CTX_new();
			if (ret && !BN_MONT_CTX_set(ret, mod, ctx))
				BN_MONT_CTX_free(ret);
			else
				*pmont = ret;
		}
	}

	ret = *pmont;

	if (got_write_lock)
		CRYPTO_w_unlock(lock);
	else
		CRYPTO_r_unlock(lock);

	return ret;
}
開發者ID:mr-moai-2016,項目名稱:znk_project,代碼行數:31,代碼來源:bn_mont.c

示例7: ec_precompute_mont_data

/*
 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
 * returns one on success. On error it returns zero.
 */
static int ec_precompute_mont_data(EC_GROUP *group)
{
    BN_CTX *ctx = BN_CTX_new();
    int ret = 0;

    BN_MONT_CTX_free(group->mont_data);
    group->mont_data = NULL;

    if (ctx == NULL)
        goto err;

    group->mont_data = BN_MONT_CTX_new();
    if (group->mont_data == NULL)
        goto err;

    if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
        BN_MONT_CTX_free(group->mont_data);
        group->mont_data = NULL;
        goto err;
    }

    ret = 1;

 err:

    BN_CTX_free(ctx);
    return ret;
}
開發者ID:RTEMS,項目名稱:rtems-libbsd,代碼行數:32,代碼來源:ec_lib.c

示例8: ec_GFp_mont_group_set_curve

int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
                                const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
{
    BN_CTX *new_ctx = NULL;
    BN_MONT_CTX *mont = NULL;
    BIGNUM *one = NULL;
    int ret = 0;

    if (group->field_data1 != NULL) {
        BN_MONT_CTX_free(group->field_data1);
        group->field_data1 = NULL;
    }
    if (group->field_data2 != NULL) {
        BN_free(group->field_data2);
        group->field_data2 = NULL;
    }

    if (ctx == NULL) {
        ctx = new_ctx = BN_CTX_new();
        if (ctx == NULL)
            return 0;
    }

    mont = BN_MONT_CTX_new();
    if (mont == NULL)
        goto err;
    if (!BN_MONT_CTX_set(mont, p, ctx)) {
        ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
        goto err;
    }
    one = BN_new();
    if (one == NULL)
        goto err;
    if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
        goto err;

    group->field_data1 = mont;
    mont = NULL;
    group->field_data2 = one;
    one = NULL;

    ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);

    if (!ret) {
        BN_MONT_CTX_free(group->field_data1);
        group->field_data1 = NULL;
        BN_free(group->field_data2);
        group->field_data2 = NULL;
    }

 err:
    if (new_ctx != NULL)
        BN_CTX_free(new_ctx);
    if (mont != NULL)
        BN_MONT_CTX_free(mont);
    if (one != NULL)
        BN_free(one);
    return ret;
}
開發者ID:2trill2spill,項目名稱:freebsd,代碼行數:59,代碼來源:ecp_mont.c

示例9: generate_key

static int generate_key(DH *dh)
	{
	int ok=0;
	int generate_new_key=0;
	unsigned l;
	BN_CTX *ctx;
	BN_MONT_CTX *mont;
	BIGNUM *pub_key=NULL,*priv_key=NULL;

	ctx = BN_CTX_new();
	if (ctx == NULL) goto err;

	if (dh->priv_key == NULL)
		{
		priv_key=BN_new();
		if (priv_key == NULL) goto err;
		generate_new_key=1;
		}
	else
		priv_key=dh->priv_key;

	if (dh->pub_key == NULL)
		{
		pub_key=BN_new();
		if (pub_key == NULL) goto err;
		}
	else
		pub_key=dh->pub_key;

	if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
		{
		if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
			if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
				dh->p,ctx)) goto err;
		}
	mont=(BN_MONT_CTX *)dh->method_mont_p;

	if (generate_new_key)
		{
		l = dh->length ? dh->length : BN_num_bits(dh->p)-1; /* secret exponent length */
		if (!BN_rand(priv_key, l, 0, 0)) goto err;
		}
	if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key,dh->p,ctx,mont))
		goto err;
		
	dh->pub_key=pub_key;
	dh->priv_key=priv_key;
	ok=1;
err:
	if (ok != 1)
		DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);

	if ((pub_key != NULL)  && (dh->pub_key == NULL))  BN_free(pub_key);
	if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
	BN_CTX_free(ctx);
	return(ok);
	}
開發者ID:froggatt,項目名稱:edimax-br-6528n,代碼行數:57,代碼來源:dh_key.c

示例10: built_in_curve_scalar_field_monts_init

static void built_in_curve_scalar_field_monts_init(void) {
  unsigned num_built_in_curves;
  for (num_built_in_curves = 0;; num_built_in_curves++) {
    if (OPENSSL_built_in_curves[num_built_in_curves].nid == NID_undef) {
      break;
    }
  }

  assert(0 < num_built_in_curves);

  built_in_curve_scalar_field_monts =
      OPENSSL_malloc(sizeof(BN_MONT_CTX *) * num_built_in_curves);
  if (built_in_curve_scalar_field_monts == NULL) {
    return;
  }

  BIGNUM *order = BN_new();
  BN_CTX *bn_ctx = BN_CTX_new();
  BN_MONT_CTX *mont_ctx = NULL;

  if (bn_ctx == NULL ||
      order == NULL) {
    goto err;
  }

  unsigned i;
  for (i = 0; i < num_built_in_curves; i++) {
    const struct curve_data *curve = OPENSSL_built_in_curves[i].data;
    const unsigned param_len = curve->param_len;
    const uint8_t *params = curve->data;

    mont_ctx = BN_MONT_CTX_new();
    if (mont_ctx == NULL) {
      goto err;
    }

    if (!BN_bin2bn(params + 5 * param_len, param_len, order) ||
        !BN_MONT_CTX_set(mont_ctx, order, bn_ctx)) {
      goto err;
    }

    built_in_curve_scalar_field_monts[i] = mont_ctx;
    mont_ctx = NULL;
  }

  goto out;

err:
  BN_MONT_CTX_free(mont_ctx);
  OPENSSL_free((BN_MONT_CTX**) built_in_curve_scalar_field_monts);
  built_in_curve_scalar_field_monts = NULL;

out:
  BN_free(order);
  BN_CTX_free(bn_ctx);
}
開發者ID:Cyril2004,項目名稱:proto-quic,代碼行數:56,代碼來源:ec.c

示例11: DEFINE_LOCAL_DATA

// built_in_curve_scalar_field_monts contains Montgomery contexts for
// performing inversions in the scalar fields of each of the built-in
// curves. It's protected by |built_in_curve_scalar_field_monts_once|.
DEFINE_LOCAL_DATA(BN_MONT_CTX **, built_in_curve_scalar_field_monts) {
  const struct built_in_curves *const curves = OPENSSL_built_in_curves();

  BN_MONT_CTX **monts =
      OPENSSL_malloc(sizeof(BN_MONT_CTX *) * OPENSSL_NUM_BUILT_IN_CURVES);
  if (monts == NULL) {
    return;
  }

  OPENSSL_memset(monts, 0, sizeof(BN_MONT_CTX *) * OPENSSL_NUM_BUILT_IN_CURVES);

  BIGNUM *order = BN_new();
  BN_CTX *bn_ctx = BN_CTX_new();
  BN_MONT_CTX *mont_ctx = NULL;

  if (bn_ctx == NULL ||
      order == NULL) {
    goto err;
  }

  for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
    const struct built_in_curve *curve = &curves->curves[i];
    const unsigned param_len = curve->param_len;
    const uint8_t *params = curve->params;

    mont_ctx = BN_MONT_CTX_new();
    if (mont_ctx == NULL) {
      goto err;
    }

    if (!BN_bin2bn(params + 5 * param_len, param_len, order) ||
        !BN_MONT_CTX_set(mont_ctx, order, bn_ctx)) {
      goto err;
    }

    monts[i] = mont_ctx;
    mont_ctx = NULL;
  }

  *out = monts;
  goto done;

err:
  BN_MONT_CTX_free(mont_ctx);
  for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
    BN_MONT_CTX_free(monts[i]);
  }
  OPENSSL_free((BN_MONT_CTX**) monts);

done:
  BN_free(order);
  BN_CTX_free(bn_ctx);
}
開發者ID:dseerapu,項目名稱:workmanager,代碼行數:56,代碼來源:ec.c

示例12: ec_GFp_mont_group_set_curve

int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
                                const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  BN_CTX *new_ctx = NULL;
  BN_MONT_CTX *mont = NULL;
  BIGNUM *one = NULL;
  int ret = 0;

  BN_MONT_CTX_free(group->mont);
  group->mont = NULL;
  BN_free(group->one);
  group->one = NULL;

  if (ctx == NULL) {
    ctx = new_ctx = BN_CTX_new();
    if (ctx == NULL) {
      return 0;
    }
  }

  mont = BN_MONT_CTX_new();
  if (mont == NULL) {
    goto err;
  }
  if (!BN_MONT_CTX_set(mont, p, ctx)) {
    OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
    goto err;
  }
  one = BN_new();
  if (one == NULL || !BN_to_montgomery(one, BN_value_one(), mont, ctx)) {
    goto err;
  }

  group->mont = mont;
  mont = NULL;
  group->one = one;
  one = NULL;

  ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);

  if (!ret) {
    BN_MONT_CTX_free(group->mont);
    group->mont = NULL;
    BN_free(group->one);
    group->one = NULL;
  }

err:
  BN_CTX_free(new_ctx);
  BN_MONT_CTX_free(mont);
  BN_free(one);
  return ret;
}
開發者ID:Crawping,項目名稱:chromium_extract,代碼行數:52,代碼來源:ec_montgomery.c

示例13: generate_key

static int generate_key(DH *dh)
	{
	int ok=0;
	BN_CTX ctx;
	BN_MONT_CTX *mont;
	BIGNUM *pub_key=NULL,*priv_key=NULL;

	BN_CTX_init(&ctx);

	if (dh->priv_key == NULL)
		{
		priv_key=BN_new();
		if (priv_key == NULL) goto err;
		do
			if (!BN_rand_range(priv_key, dh->p)) goto err;
		while (BN_is_zero(priv_key));
		}
	else
		priv_key=dh->priv_key;

	if (dh->pub_key == NULL)
		{
		pub_key=BN_new();
		if (pub_key == NULL) goto err;
		}
	else
		pub_key=dh->pub_key;

	if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
		{
		if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
			if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
				dh->p,&ctx)) goto err;
		}
	mont=(BN_MONT_CTX *)dh->method_mont_p;

	if (!dh->meth->bn_mod_exp(dh, pub_key,dh->g,priv_key,dh->p,&ctx,mont))
								goto err;
		
	dh->pub_key=pub_key;
	dh->priv_key=priv_key;
	ok=1;
err:
	if (ok != 1)
		DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);

	if ((pub_key != NULL)  && (dh->pub_key == NULL))  BN_free(pub_key);
	if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
	BN_CTX_free(&ctx);
	return(ok);
	}
開發者ID:unofficial-opensource-apple,項目名稱:Security,代碼行數:51,代碼來源:dh_key.c

示例14: CRYPTO_w_lock

BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
					const BIGNUM *mod, BN_CTX *ctx)
	{
	if (*pmont)
		return *pmont;
	CRYPTO_w_lock(lock);
	if (!*pmont)
		{
		*pmont = BN_MONT_CTX_new();
		if (*pmont && !BN_MONT_CTX_set(*pmont, mod, ctx))
			{
			BN_MONT_CTX_free(*pmont);
			*pmont = NULL;
			}
		}
	CRYPTO_w_unlock(lock);
	return *pmont;
	}
開發者ID:niubl,項目名稱:camera_project,代碼行數:18,代碼來源:bn_mont.c

示例15: CRYPTO_w_lock

BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
					const BIGNUM *mod, BN_CTX *ctx)
	{
	if (*pmont)
		return *pmont;
	CRYPTO_w_lock(lock);
	if (!*pmont)
		{
		BN_MONT_CTX *mtmp;
		mtmp = BN_MONT_CTX_new();
		if (mtmp && !BN_MONT_CTX_set(mtmp, mod, ctx))
			BN_MONT_CTX_free(mtmp);
		else
			*pmont = mtmp;
		}
	CRYPTO_w_unlock(lock);
	return *pmont;
	}
開發者ID:appleorange1,項目名稱:asus-rt-n12-lx,代碼行數:18,代碼來源:bn_mont.c


注:本文中的BN_MONT_CTX_set函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。