本文整理汇总了C++中DH_generate_parameters_ex函数的典型用法代码示例。如果您正苦于以下问题:C++ DH_generate_parameters_ex函数的具体用法?C++ DH_generate_parameters_ex怎么用?C++ DH_generate_parameters_ex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DH_generate_parameters_ex函数的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);
}
示例2: dif_hel_setup
DH * dif_hel_setup()
{
DH * new_dh = DH_new();
if ( !new_dh )
{
printf("%s \n","Error:Creating new dh");
error();
}
if ( !DH_generate_parameters_ex(new_dh,2,DH_GENERATOR_2,0))
{
printf("%s \n","Error:Generating paramters");
error();
}
int dh_code = 0;
if( !DH_check(new_dh,&dh_code))
{
printf("%s \n", "Error:Dh_check failed");
error();
}
if(!DH_generate_key(new_dh))
{
printf("%s \n", "Error:Generating key failed");
error();
}
return new_dh;
}
示例3: RandomSeed
/**
Generates DH parameter.
Given generator g, and length of prime number p in bits, this function generates p,
and sets DH context according to value of g and p.
Before this function can be invoked, pseudorandom number generator must be correctly
initialized by RandomSeed().
If DhContext is NULL, then return FALSE.
If Prime is NULL, then return FALSE.
@param[in, out] DhContext Pointer to the DH context.
@param[in] Generator Value of generator.
@param[in] PrimeLength Length in bits of prime to be generated.
@param[out] Prime Pointer to the buffer to receive the generated prime number.
@retval TRUE DH parameter generation succeeded.
@retval FALSE Value of Generator is not supported.
@retval FALSE PRNG fails to generate random prime number with PrimeLength.
**/
BOOLEAN
EFIAPI
DhGenerateParameter (
IN OUT VOID *DhContext,
IN UINTN Generator,
IN UINTN PrimeLength,
OUT UINT8 *Prime
)
{
BOOLEAN RetVal;
BIGNUM *BnP;
//
// Check input parameters.
//
if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {
return FALSE;
}
if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {
return FALSE;
}
RetVal = (BOOLEAN) DH_generate_parameters_ex (DhContext, (UINT32) PrimeLength, (UINT32) Generator, NULL);
if (!RetVal) {
return FALSE;
}
DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL);
BN_bn2bin (BnP, Prime);
return TRUE;
}
示例4: dh_blocking_gen
static void *
dh_blocking_gen(void *arg)
{
struct dh_blocking_gen_arg *gen = (struct dh_blocking_gen_arg *)arg;
gen->result = DH_generate_parameters_ex(gen->dh, gen->size, gen->gen, gen->cb);
return 0;
}
示例5: main
int main(int arc, char *argv[])
{
DH *dh = DH_new();
BN_GENCB *(*cb)(BN_GENCB *, int, int);
cb = &callback;
char buf[400];
char *bufPtr = &buf[0];
/* Load the human readable error strings for libcrypto */
ERR_load_crypto_strings();
/* Load all digest and cipher algorithms */
OpenSSL_add_all_algorithms();
/* Load config file, and other important initialisation */
OPENSSL_config(NULL);
/*
use special PRNG if possible:
* /dev/random
* /dev/hwrng
* ...
*/
/*
---------------------------- PARAMATER GEN ------------------------
*/
printf("start generation\n");
DH_generate_parameters_ex(dh, 256, 2, NULL);
printf("paramters DONE\n");
if(dh->pub_key == NULL)
printf("pubkey init to NULL\n");
DH_generate_key(dh);
printf("key DONE\n");
bufPtr = BN_bn2hex(dh->p);
printf ("prime : %s\n", bufPtr);
bufPtr = BN_bn2hex(dh->g);
printf ("generator: %s\n", bufPtr);
bufPtr = BN_bn2hex(dh->pub_key);
printf ("pubkey : %s\n", bufPtr);
bufPtr = BN_bn2hex(dh->priv_key);
printf ("privkey : %s\n", bufPtr);
/* Clean up */
/* Removes all digests and ciphers */
EVP_cleanup();
/* if you omit the next, a small leak may be left when you make use of the BIO (low level API) for e.g. base64 transformations */
CRYPTO_cleanup_all_ex_data();
/* Remove error strings */
ERR_free_strings();
return 0;
}
示例6: pkey_dh_paramgen
static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
DH *dh = NULL;
DH_PKEY_CTX *dctx = ctx->data;
BN_GENCB *pcb, cb;
int ret;
if (dctx->rfc5114_param) {
switch (dctx->rfc5114_param) {
case 1:
dh = DH_get_1024_160();
break;
case 2:
dh = DH_get_2048_224();
break;
case 3:
dh = DH_get_2048_256();
break;
default:
return -2;
}
EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
return 1;
}
if (ctx->pkey_gencb) {
pcb = &cb;
evp_pkey_set_cb_translate(pcb, ctx);
} else
pcb = NULL;
#ifndef OPENSSL_NO_DSA
if (dctx->use_dsa) {
DSA *dsa_dh;
dsa_dh = dsa_dh_generate(dctx, pcb);
if (!dsa_dh)
return 0;
dh = DSA_dup_DH(dsa_dh);
DSA_free(dsa_dh);
if (!dh)
return 0;
EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
return 1;
}
#endif
dh = DH_new();
if (!dh)
return 0;
ret = DH_generate_parameters_ex(dh,
dctx->prime_len, dctx->generator, pcb);
if (ret)
EVP_PKEY_assign_DH(pkey, dh);
else
DH_free(dh);
return ret;
}
示例7: OpenSSLGenerateDHParams
void OpenSSLGenerateDHParams()
{
CachedDH = DH_new();
if(CachedDH)
{
OpenSSLReseedRandom();
DH_generate_parameters_ex(CachedDH, 512, DH_GENERATOR_5, 0);
//DH_check(CachedDH, &codes);
}
}
示例8: ctx_set_dh
static int ctx_set_dh(SSL_CTX *ctx, const char *dh_file,
const char *dh_special)
{
DH *dh = NULL;
FILE *fp = NULL;
if (dh_special) {
if (strcmp(dh_special, "NULL") == 0)
return 1;
if (strcmp(dh_special, "standard") == 0) {
if ((dh = get_dh512()) == NULL) {
fprintf(stderr, "Error, can't parse 'standard'"
" DH parameters\n");
return 0;
}
fprintf(stderr, "Info, using 'standard' DH parameters\n");
goto do_it;
}
if (strcmp(dh_special, "generate") != 0)
/*
* This shouldn't happen - screening values is handled in main().
*/
abort();
fprintf(stderr, "Info, generating DH parameters ... ");
fflush(stderr);
if (!(dh = DH_new()) || !DH_generate_parameters_ex(dh, 512,
DH_GENERATOR_5,
NULL)) {
fprintf(stderr, "error!\n");
if (dh)
DH_free(dh);
return 0;
}
fprintf(stderr, "complete\n");
goto do_it;
}
/* So, we're loading dh_file */
if ((fp = fopen(dh_file, "r")) == NULL) {
fprintf(stderr, "Error, couldn't open '%s' for DH parameters\n",
dh_file);
return 0;
}
dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
fclose(fp);
if (dh == NULL) {
fprintf(stderr, "Error, could not parse DH parameters from '%s'\n",
dh_file);
return 0;
}
fprintf(stderr, "Info, using DH parameters from file '%s'\n", dh_file);
do_it:
SSL_CTX_set_tmp_dh(ctx, dh);
DH_free(dh);
return 1;
}
示例9: dh_test
/* DH: generate shared parameters
*/
static int dh_test()
{
DH *dh;
ERR_clear_error();
dh = FIPS_dh_new();
if (!dh)
return 0;
if (!DH_generate_parameters_ex(dh, 1024, 2, NULL))
return 0;
FIPS_dh_free(dh);
return 1;
}
示例10: generate_dh_parameters
/*
* Generate DH parameters.
*
* Last resort if we can't load precomputed nor hardcoded
* parameters.
*/
static DH *
generate_dh_parameters(int prime_len, int generator)
{
DH *dh;
if ((dh = DH_new()) == NULL)
return NULL;
if (DH_generate_parameters_ex(dh, prime_len, generator, NULL))
return dh;
DH_free(dh);
return NULL;
}
示例11: void
DH *DH_generate_parameters(int prime_len, int generator,
void (*callback)(int,int,void *), void *cb_arg)
{
BN_GENCB cb;
DH *ret=NULL;
if((ret=DH_new()) == NULL)
return NULL;
BN_GENCB_set_old(&cb, callback, cb_arg);
if(DH_generate_parameters_ex(ret, prime_len, generator, &cb))
return ret;
DH_free(ret);
return NULL;
}
示例12: DH_new
std::string avjackif::async_client_hello(boost::asio::yield_context yield_context)
{
proto::client_hello client_hello;
client_hello.set_client("avim");
client_hello.set_version(0001);
unsigned char to[512];
auto dh = DH_new();
DH_generate_parameters_ex(dh,64,DH_GENERATOR_5,NULL);
DH_generate_key(dh);
// 把 g,p, pubkey 传过去
client_hello.set_random_g((const void*)to, BN_bn2bin(dh->g, to));
client_hello.set_random_p((const void*)to, BN_bn2bin(dh->p, to));
client_hello.set_random_pub_key((const void*)to, BN_bn2bin(dh->pub_key, to));
auto tobesend = av_router::encode(client_hello);
boost::asio::async_write(*m_sock, boost::asio::buffer(tobesend), yield_context);
// 解码
std::unique_ptr<proto::server_hello> server_hello(
(proto::server_hello*)async_read_protobuf_message(*m_sock, yield_context));
m_remote_addr.reset(new proto::av_address(
av_address_from_string(server_hello->server_av_address())));
auto server_pubkey = BN_bin2bn((const unsigned char *) server_hello->random_pub_key().data(),
server_hello->random_pub_key().length(), NULL);
m_shared_key.resize(DH_size(dh));
// 密钥就算出来啦!
DH_compute_key(&m_shared_key[0], server_pubkey, dh);
BN_free(server_pubkey);
std::printf("key = 0x");
for (int i=0; i<DH_size(dh); ++i)
{
std::printf("%x%x", (m_shared_key[i] >> 4) & 0xf, m_shared_key[i] & 0xf);
}
std::printf("\n");
DH_free(dh);
return server_hello->random_pub_key();
}
示例13: generate_dh_parameters
/*
* Generate DH parameters.
*
* Last resort if we can't load precomputed nor hardcoded
* parameters.
*/
static DH *
generate_dh_parameters(int prime_len, int generator)
{
#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
DH *dh;
if ((dh = DH_new()) == NULL)
return NULL;
if (DH_generate_parameters_ex(dh, prime_len, generator, NULL))
return dh;
DH_free(dh);
return NULL;
#else
return DH_generate_parameters(prime_len, generator, NULL, NULL);
#endif
}
示例14: DH_generate_parameters_ex
bool diffie_hellman::generate_params( int s, uint8_t g )
{
ssl_dh dh;
DH_generate_parameters_ex(dh.obj, s, g, NULL);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ssl_bignum bn_p;
DH_get0_pqg(dh.obj, (const BIGNUM**)&bn_p.obj, NULL, NULL);
p.resize( BN_num_bytes( bn_p ) );
if( p.size() )
BN_bn2bin( bn_p, (unsigned char*)&p.front() );
#else
p.resize( BN_num_bytes( dh->p ) );
if( p.size() )
BN_bn2bin( dh->p, (unsigned char*)&p.front() );
#endif
this->g = g;
return fc::validate( dh, valid );
}
示例15: generate_dhparam
int generate_dhparam(int dh_bits)
{
DH * dh ;
char filename[FIELD_SZ+1];
FILE * out;
sprintf(filename, "dh%d.pem", dh_bits);
if ((out=fopen(filename, "wb"))==NULL) {
fprintf(stderr, "Cannot create %s: aborting\n", filename);
return -1;
}
dh = DH_new();
printf("Generating DH parameters (%d bits) -- this can take long\n", dh_bits);
DH_generate_parameters_ex(dh, dh_bits, DH_GENERATOR_2, 0);
PEM_write_DHparams(out, dh);
fclose(out);
printf("done\n");
return 0;
}