当前位置: 首页>>代码示例>>C++>>正文


C++ DH_compute_key函数代码示例

本文整理汇总了C++中DH_compute_key函数的典型用法代码示例。如果您正苦于以下问题:C++ DH_compute_key函数的具体用法?C++ DH_compute_key怎么用?C++ DH_compute_key使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了DH_compute_key函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: openssl_dh_crypt

void openssl_dh_crypt()
{
	BIO *b;
	DH *d1, *d2;
	int i, len1, len2;
	unsigned char skey1[COMM_LEN], skey2[COMM_LEN];

	d1 = DH_new();
	d2 = DH_new();
	DH_generate_parameters_ex(d1, 64, DH_GENERATOR_2, NULL);
	DH_check(d1, &i);
	printf("\nDH key size: %d\n", DH_size(d1));
	DH_generate_key(d1);

	d2->p = BN_dup(d1->p);
	d2->g = BN_dup(d1->g);
	DH_generate_key(d2);
	DH_check_pub_key(d1, d1->pub_key, &i);
	len1 = DH_compute_key(skey1, d2->pub_key, d1);
	len2 = DH_compute_key(skey2, d1->pub_key, d2);
	if ((len1 != len2) || (memcmp(skey1, skey2, len1) != 0)) {
		printf("DH_compute_key err!\n");
		DH_free(d1);
		DH_free(d2);
		return;
	}

	b = BIO_new(BIO_s_file());
	BIO_set_fp(b, stdout, BIO_NOCLOSE);
	DHparams_print(b, d1);

	BIO_free(b);
	DH_free(d1);
	DH_free(d2);
}
开发者ID:beike2020,项目名称:source,代码行数:35,代码来源:openssl_base.c

示例2: run_rfc5114_tests

static int run_rfc5114_tests(void)
{
    int i;
    for (i = 0; i < (int)(sizeof(rfctd) / sizeof(rfc5114_td)); i++) {
        DH *dhA, *dhB;
        unsigned char *Z1 = NULL, *Z2 = NULL;
        const rfc5114_td *td = rfctd + i;
        /* Set up DH structures setting key components */
        dhA = td->get_param();
        dhB = td->get_param();
        if (!dhA || !dhB)
            goto bad_err;

        dhA->priv_key = BN_bin2bn(td->xA, td->xA_len, NULL);
        dhA->pub_key = BN_bin2bn(td->yA, td->yA_len, NULL);

        dhB->priv_key = BN_bin2bn(td->xB, td->xB_len, NULL);
        dhB->pub_key = BN_bin2bn(td->yB, td->yB_len, NULL);

        if (!dhA->priv_key || !dhA->pub_key
            || !dhB->priv_key || !dhB->pub_key)
            goto bad_err;

        if ((td->Z_len != (size_t)DH_size(dhA))
            || (td->Z_len != (size_t)DH_size(dhB)))
            goto err;

        Z1 = OPENSSL_malloc(DH_size(dhA));
        Z2 = OPENSSL_malloc(DH_size(dhB));
        /*
         * Work out shared secrets using both sides and compare with expected
         * values.
         */
        if (!DH_compute_key(Z1, dhB->pub_key, dhA))
            goto bad_err;
        if (!DH_compute_key(Z2, dhA->pub_key, dhB))
            goto bad_err;

        if (memcmp(Z1, td->Z, td->Z_len))
            goto err;
        if (memcmp(Z2, td->Z, td->Z_len))
            goto err;

        printf("RFC5114 parameter test %d OK\n", i + 1);

        DH_free(dhA);
        DH_free(dhB);
        OPENSSL_free(Z1);
        OPENSSL_free(Z2);

    }
    return 1;
 bad_err:
    fprintf(stderr, "Initalisation error RFC5114 set %d\n", i + 1);
    ERR_print_errors_fp(stderr);
    return 0;
 err:
    fprintf(stderr, "Test failed RFC5114 set %d\n", i + 1);
    return 0;
}
开发者ID:Adallom,项目名称:openssl,代码行数:60,代码来源:dhtest.c

示例3: DH_compute_key

 int SrsDH::copy_shared_key(const char* ppkey, int32_t ppkey_size, char* skey, int32_t& skey_size)
 {
     int ret = ERROR_SUCCESS;
     
     BIGNUM* ppk = NULL;
     if ((ppk = BN_bin2bn((const unsigned char*)ppkey, ppkey_size, 0)) == NULL) {
         ret = ERROR_OpenSslGetPeerPublicKey;
         return ret;
     }
     
     // if failed, donot return, do cleanup, @see ./test/dhtest.c:168
     // maybe the key_size is 127, but dh will write all 128bytes skey,
     // so, donot need to set/initialize the skey.
     // @see https://github.com/winlinvip/simple-rtmp-server/issues/165
     int32_t key_size = DH_compute_key((unsigned char*)skey, ppk, pdh);
     
     if (key_size < ppkey_size) {
         srs_warn("shared key size=%d, ppk_size=%d", key_size, ppkey_size);
     }
     
     if (key_size < 0 || key_size > skey_size) {
         ret = ERROR_OpenSslComputeSharedKey;
     } else {
         skey_size = key_size;
     }
     
     if (ppk) {
         BN_free(ppk);
     }
     
     return ret;
 }
开发者ID:WilliamRen,项目名称:simple-rtmp-server,代码行数:32,代码来源:srs_protocol_handshake.cpp

示例4: tr_cryptoComputeSecret

const uint8_t*
tr_cryptoComputeSecret( tr_crypto *     crypto,
                        const uint8_t * peerPublicKey )
{
    int      len;
    uint8_t  secret[KEY_LEN];
    BIGNUM * bn = BN_bin2bn( peerPublicKey, KEY_LEN, NULL );
    DH *     dh;

    ensureKeyExists( crypto );
    dh = crypto->dh;

    assert( DH_size( dh ) == KEY_LEN );

    len = DH_compute_key( secret, bn, dh );
    if( len == -1 )
        logErrorFromSSL( );
    else {
        int offset;
        assert( len <= KEY_LEN );
        offset = KEY_LEN - len;
        memset( crypto->mySecret, 0, offset );
        memcpy( crypto->mySecret + offset, secret, len );
        crypto->mySecretIsSet = 1;
    }

    BN_free( bn );
    return crypto->mySecret;
}
开发者ID:miracle2k,项目名称:transmission,代码行数:29,代码来源:crypto.c

示例5: DH_new

   bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) {
        ssl_dh dh = DH_new();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
        auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
        auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );
        auto bn_priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL );
        auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
        DH_set0_pqg(dh.obj, bn_p, NULL, bn_g);
        DH_set0_key(dh.obj, bn_pub_key, bn_priv_key);
#else
        dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
        dh->pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );
        dh->priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL );
        dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
#endif

        int check;
        DH_check(dh,&check);
        if( !fc::validate( dh, valid ) )
        {
            return false;
        }

        ssl_bignum pk;
        BN_bin2bn( (unsigned char*)buf, s, pk );
        shared_key.resize( DH_size(dh) ); 
        DH_compute_key( (unsigned char*)&shared_key.front(), pk, dh );

        return true;
   }
开发者ID:BestSilent,项目名称:eos,代码行数:30,代码来源:dh.cpp

示例6: pkey_dh_derive

static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
	{
	int ret;
	DH *dh;
	DH_PKEY_CTX *dctx = ctx->data;
	BIGNUM *dhpub;
	if (!ctx->pkey || !ctx->peerkey)
		{
		DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
		return 0;
		}
	dh = ctx->pkey->pkey.dh;
	dhpub = ctx->peerkey->pkey.dh->pub_key;
	if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE)
		{
		if (key == NULL)
			{
			*keylen = DH_size(dh);
			return 1;
			}
		ret = DH_compute_key(key, dhpub, dh);
		if (ret < 0)
			return ret;
		*keylen = ret;
		return 1;
		}
	else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42)
		{
		unsigned char *Z = NULL;
		size_t Zlen = 0;
		if (!dctx->kdf_outlen || !dctx->kdf_oid)
			return 0;
		if (key == NULL)
			{
			*keylen = dctx->kdf_outlen;
			return 1;
			}
		if (*keylen != dctx->kdf_outlen)
			return 0;
		ret = 0;
		Zlen = DH_size(dh);
		Z = OPENSSL_malloc(Zlen);
		if (DH_compute_key_padded(Z, dhpub, dh) <= 0)
			goto err;
		if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
					dctx->kdf_ukm, dctx->kdf_ukmlen,
					dctx->kdf_md))
			goto err;
		*keylen = dctx->kdf_outlen;
		ret = 1;
		err:
		if (Z)
			{
			OPENSSL_cleanse(Z, Zlen);
			OPENSSL_free(Z);
			}
		return ret;
		}
	return 1;
	}
开发者ID:0culus,项目名称:openssl,代码行数:60,代码来源:dh_pmeth.c

示例7: s2n_dh_compute_shared_secret_as_server

int s2n_dh_compute_shared_secret_as_server(struct s2n_dh_params *server_dh_params, struct s2n_stuffer *Yc_in, struct s2n_blob *shared_key)
{
    uint16_t Yc_length;
    struct s2n_blob Yc;
    int shared_key_size;
    BIGNUM *pub_key;

    GUARD(s2n_check_all_dh_params(server_dh_params));

    GUARD(s2n_stuffer_read_uint16(Yc_in, &Yc_length));
    Yc.size = Yc_length;
    Yc.data = s2n_stuffer_raw_read(Yc_in, Yc.size);
    notnull_check(Yc.data);

    pub_key = BN_bin2bn((const unsigned char *)Yc.data, Yc.size, NULL);
    notnull_check(pub_key);
    GUARD(s2n_alloc(shared_key, DH_size(server_dh_params->dh)));

    shared_key_size = DH_compute_key(shared_key->data, pub_key, server_dh_params->dh);
    if (shared_key_size <= 0) {
        BN_free(pub_key);
        S2N_ERROR(S2N_ERR_DH_SHARED_SECRET);
    }

    shared_key->size = shared_key_size;

    BN_free(pub_key);

    return 0;
}
开发者ID:raycoll,项目名称:s2n,代码行数:30,代码来源:s2n_dhe.c

示例8: dh_compute_key_nif

ERL_NIF_TERM dh_compute_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (OthersPublicKey, MyPrivateKey, DHParams=[P,G]) */
    BIGNUM *other_pub_key = NULL,
        *dh_p = NULL,
        *dh_g = NULL;
    DH *dh_priv = DH_new();

    /* Check the arguments and get
          my private key (dh_priv),
          the peer's public key (other_pub_key),
          the parameters p & q
    */

    {
        BIGNUM *dummy_pub_key = NULL,
               *priv_key = NULL;
        ERL_NIF_TERM head, tail;

        if (!get_bn_from_bin(env, argv[0], &other_pub_key)
            || !get_bn_from_bin(env, argv[1], &priv_key)
            || !enif_get_list_cell(env, argv[2], &head, &tail)
            || !get_bn_from_bin(env, head, &dh_p)
            || !enif_get_list_cell(env, tail, &head, &tail)
            || !get_bn_from_bin(env, head, &dh_g)
            || !enif_is_empty_list(env, tail)

            /* Note: DH_set0_key() does not allow setting only the
             * private key, although DH_compute_key() does not use the
             * public key. Work around this limitation by setting
             * the public key to a copy of the private key.
             */
            || !(dummy_pub_key = BN_dup(priv_key))
            || !DH_set0_key(dh_priv, dummy_pub_key, priv_key)
            || !DH_set0_pqg(dh_priv, dh_p, NULL, dh_g)
            ) {
            if (dh_p) BN_free(dh_p);
            if (dh_g) BN_free(dh_g);
            if (other_pub_key) BN_free(other_pub_key);
            if (dummy_pub_key) BN_free(dummy_pub_key);
            if (priv_key) BN_free(priv_key);
            return enif_make_badarg(env);
        }
    }
    {
        ErlNifBinary ret_bin;
        int size;

        enif_alloc_binary(DH_size(dh_priv), &ret_bin);
        size = DH_compute_key(ret_bin.data, other_pub_key, dh_priv);
        BN_free(other_pub_key);
        DH_free(dh_priv);
        if (size<=0) {
            enif_release_binary(&ret_bin);
            return atom_error;
        }

        if (size != ret_bin.size) enif_realloc_binary(&ret_bin, size);
        return enif_make_binary(env, &ret_bin);
    }
}
开发者ID:KennethL,项目名称:otp,代码行数:60,代码来源:dh.c

示例9: openssldh_computesecret

static isc_result_t
openssldh_computesecret(const dst_key_t *pub, const dst_key_t *priv,
			isc_buffer_t *secret)
{
	DH *dhpub, *dhpriv;
	int ret;
	isc_region_t r;
	unsigned int len;

	REQUIRE(pub->keydata.dh != NULL);
	REQUIRE(priv->keydata.dh != NULL);

	dhpub = pub->keydata.dh;
	dhpriv = priv->keydata.dh;

	len = DH_size(dhpriv);
	isc_buffer_availableregion(secret, &r);
	if (r.length < len)
		return (ISC_R_NOSPACE);
	ret = DH_compute_key(r.base, dhpub->pub_key, dhpriv);
	if (ret <= 0)
		return (dst__openssl_toresult2("DH_compute_key",
					       DST_R_COMPUTESECRETFAILURE));
	isc_buffer_add(secret, len);
	return (ISC_R_SUCCESS);
}
开发者ID:krichter722,项目名称:bind9,代码行数:26,代码来源:openssldh_link.c

示例10: x4_dh_shared

x4s_buf x4_dh_shared(const uint8 * g, size_t glen,
                     const uint8 * p, size_t plen,
                     const uint8 * x, size_t xlen,
                     const uint8 * gx, size_t gxlen,
                     const uint8 * gy, size_t gylen)
{
  DH     * dh;
  BIGNUM * bn;
  x4s_buf gxy = { 0 };

  /*  */
  x4_assert(gx && gxlen && gy && gylen);

  /*  */
  dh = _dh_init(g,glen, p,plen, x,xlen);
  x4_assert(dh);

  dh->pub_key = BN_bin2bn(gx, gxlen, 0);
  x4_assert(dh->pub_key);

  bn = BN_bin2bn(gy, gylen, 0);
  x4_assert(bn);

  x4_buf_resize(&gxy, DH_size(dh));
  DH_compute_key(gxy.data, bn, dh);

  DH_free(dh);
  return gxy;
}
开发者ID:apankrat,项目名称:libike,代码行数:29,代码来源:dh.c

示例11: performDH

struct sec performDH(char *pubkeyRec,DH *privkey){
	
	struct sec s;
	int secret_size;
	/* Send the public key to the peer.
	* How this occurs will be specific to your situation (see main text below) */


	/* Receive the public key from the peer. In this example we're just hard coding a value */
	BIGNUM *pubkey = NULL;
	if(0 == (BN_dec2bn(&pubkey, pubkeyRec))) handleErrors();

	/* Compute the shared secret */
	unsigned char *secret;
	if(NULL == (secret = OPENSSL_malloc(sizeof(unsigned char) * (DH_size(privkey))))) handleErrors();

	if(0 > (secret_size = DH_compute_key(secret, pubkey, privkey))) handleErrors();

	/* Do something with the shared secret */
	/* Note secret_size may be less than DH_size(privkey) */
	printf("The shared secret is:\n");
	
	strcpy(s.value,secret);
	s.length=secret_size;
	/* Clean up */
	OPENSSL_free(secret);
	BN_free(pubkey);
	DH_free(privkey);

	return s;
}
开发者ID:deepanshululla,项目名称:Network-security,代码行数:31,代码来源:client.c

示例12: tr_compute_dh_key

int tr_compute_dh_key(unsigned char **pbuf,
		      BIGNUM *pub_key,
		      DH *priv_dh) {
  size_t buflen;
  unsigned char *buf = NULL;;
  int rc = 0;

  if ((!pbuf) ||
      (!pub_key) ||
      (!priv_dh)) {
    tr_debug("tr_compute_dh_key: Invalid parameters.");
    return(-1);
  }
  *pbuf = NULL;
  buflen = DH_size(priv_dh);
  buf = malloc(buflen);
  if (buf == NULL) {
    tr_crit("tr_compute_dh_key: out of memory");
    return -1;
  }


  rc = DH_compute_key(buf, pub_key, priv_dh);
  if (0 <= rc) {
    *pbuf = buf;
  }else {
    free(buf);
  }
  return rc;
}
开发者ID:spaetow,项目名称:trust_router,代码行数:30,代码来源:tr_dh.c

示例13: usmDHSetKey

int
usmDHSetKey(struct usmUser *user, int for_auth_key,
            u_char *val, size_t val_len)
{
    DH             *dh;
    BIGNUM         *other_pub;
    u_char         *key;
    size_t          key_len;

    DEBUGMSGTL(("verbose:usmDHUserKeyTable:usmDHSetKey", "called\n"));
    /*
     * XXX: mem leaks on errors abound 
     */

    dh = usmDHGetUserDHptr(user, for_auth_key);
    if (!dh)
        return MFD_ERROR;

    other_pub = BN_bin2bn(val + val_len / 2, val_len / 2, NULL);
    if (!other_pub)
        return MFD_ERROR;

    /*
     * Set the new key for a user 
     */
    key_len = DH_size(dh);
    key = malloc(DH_size(dh));
    if (!key)
        return MFD_ERROR;

    if (DH_compute_key(key, other_pub, dh)) {
        u_char        **replkey;
        size_t          replkey_size;

        if (for_auth_key) {
            replkey_size = user->authKeyLen;
            replkey = &user->authKey;
        } else {
            replkey_size = user->privKeyLen;
            replkey = &user->privKey;
        }

        /*
         * is it large enough? 
         */
        if (key_len < replkey_size)
            return MFD_ERROR;

        /*
         * copy right most bits, per the object requirements 
         */
        SNMP_FREE(*replkey);
        memdup(replkey, key + key_len - replkey_size, replkey_size);

        return MFD_SUCCESS;
    }

    return MFD_ERROR;
}
开发者ID:schestowitz,项目名称:Cyrus,代码行数:59,代码来源:usmDHUserKeyTable_data_set.c

示例14: generate_dh_keyblock

static krb5_error_code
generate_dh_keyblock(krb5_context context, pk_client_params *client_params,
                     krb5_enctype enctype, krb5_keyblock *reply_key)
{
    unsigned char *dh_gen_key = NULL;
    krb5_keyblock key;
    krb5_error_code ret;
    size_t dh_gen_keylen, size;

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

    if (!DH_generate_key(client_params->dh)) {
	ret = KRB5KRB_ERR_GENERIC;
	krb5_set_error_message(context, ret, "Can't generate Diffie-Hellman keys");
	goto out;
    }
    if (client_params->dh_public_key == NULL) {
	ret = KRB5KRB_ERR_GENERIC;
	krb5_set_error_message(context, ret, "dh_public_key");
	goto out;
    }

    dh_gen_keylen = DH_size(client_params->dh);
    size = BN_num_bytes(client_params->dh->p);
    if (size < dh_gen_keylen)
	size = dh_gen_keylen;

    dh_gen_key = malloc(size);
    if (dh_gen_key == NULL) {
	ret = ENOMEM;
	krb5_set_error_message(context, ret, "malloc: out of memory");
	goto out;
    }
    memset(dh_gen_key, 0, size - dh_gen_keylen);

    dh_gen_keylen = DH_compute_key(dh_gen_key + (size - dh_gen_keylen),
				   client_params->dh_public_key,
				   client_params->dh);
    if (dh_gen_keylen == -1) {
	ret = KRB5KRB_ERR_GENERIC;
	krb5_set_error_message(context, ret, "Can't compute Diffie-Hellman key");
	goto out;
    }

    ret = _krb5_pk_octetstring2key(context,
				   enctype,
				   dh_gen_key, dh_gen_keylen,
				   NULL, NULL,
				   reply_key);

 out:
    if (dh_gen_key)
	free(dh_gen_key);
    if (key.keyvalue.data)
	krb5_free_keyblock_contents(context, &key);

    return ret;
}
开发者ID:gojdic,项目名称:samba,代码行数:58,代码来源:pkinit.c

示例15: assert

	// compute shared secret given remote public key
	void DH_key_exchange::compute_secret (char const* remote_pubkey)
	{
		assert (remote_pubkey);
		BIGNUM* bn_remote_pubkey = BN_bin2bn ((unsigned char*)remote_pubkey, 96, NULL);

		int ret = 
			DH_compute_key ( (unsigned char*)m_dh_secret, bn_remote_pubkey, m_DH); // TODO Check for errors

		BN_free (bn_remote_pubkey);
	}
开发者ID:codeboost,项目名称:libertv,代码行数:11,代码来源:pe_crypto.cpp


注:本文中的DH_compute_key函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。