本文整理汇总了C++中DSA_generate_key函数的典型用法代码示例。如果您正苦于以下问题:C++ DSA_generate_key函数的具体用法?C++ DSA_generate_key怎么用?C++ DSA_generate_key使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DSA_generate_key函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: get_dsa1024
dsa160_key::dsa160_key(int bits)
{
if (bits == 0) {
bits = 2048;
}
// Choose an appropriate set of DSA parameters for the new key.
if (bits <= 1024) {
dsa_ = get_dsa1024();
}
else if (bits <= 2048) {
dsa_ = get_dsa2048();
}
else if (bits <= 3072) {
dsa_ = get_dsa3072();
}
else {
logger::fatal() << "Can't currently produce DSA keys with more than 3072 bits";
}
assert(dsa_);
// Generate a new DSA key given those parameters
int rc = DSA_generate_key(dsa_);
assert(rc == 1);
assert(dsa_->priv_key != nullptr);
if (rc != 1 or dsa_->priv_key == nullptr) {
throw std::runtime_error("Cannot generate DSA private key");
}
set_type(public_and_private);
}
示例2: openssldsa_generate
static isc_result_t
openssldsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
DSA *dsa;
unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
isc_result_t result;
#if OPENSSL_VERSION_NUMBER > 0x00908000L
BN_GENCB cb;
union {
void *dptr;
void (*fptr)(int);
} u;
#else
UNUSED(callback);
#endif
UNUSED(unused);
result = dst__entropy_getdata(rand_array, sizeof(rand_array),
ISC_FALSE);
if (result != ISC_R_SUCCESS)
return (result);
#if OPENSSL_VERSION_NUMBER > 0x00908000L
dsa = DSA_new();
if (dsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
if (callback == NULL) {
BN_GENCB_set_old(&cb, NULL, NULL);
} else {
u.fptr = callback;
BN_GENCB_set(&cb, &progress_cb, u.dptr);
}
if (!DSA_generate_parameters_ex(dsa, key->key_size, rand_array,
ISC_SHA1_DIGESTLENGTH, NULL, NULL,
&cb))
{
DSA_free(dsa);
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
}
#else
dsa = DSA_generate_parameters(key->key_size, rand_array,
ISC_SHA1_DIGESTLENGTH, NULL, NULL,
NULL, NULL);
if (dsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#endif
if (DSA_generate_key(dsa) == 0) {
DSA_free(dsa);
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
}
dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
key->keydata.dsa = dsa;
return (ISC_R_SUCCESS);
}
示例3: crypto_sign_keypair
int crypto_sign_keypair(unsigned char *pk,unsigned char *sk)
{
DSA *x;
int len;
x = DSA_new();
if (!x) return -1;
memset(sk,0,SECRETKEY_BYTES);
memset(pk,0,PUBLICKEY_BYTES);
x->p = BN_new(); if (!x->p) goto error;
x->q = BN_new(); if (!x->q) goto error;
x->g = BN_new(); if (!x->g) goto error;
if (!BN_bin2bn(prime,sizeof prime,x->p)) goto error;
if (!BN_bin2bn(prime_q,sizeof prime_q,x->q)) goto error;
if (!BN_bin2bn(prime_g,sizeof prime_g,x->g)) goto error;
if (!DSA_generate_key(x)) goto error;
len = BN_num_bytes(x->pub_key); if (len > PUBLICKEY_BYTES) goto error;
BN_bn2bin(x->pub_key,pk + PUBLICKEY_BYTES - len);
BN_bn2bin(x->pub_key,sk + PUBLICKEY_BYTES - len);
len = BN_num_bytes(x->priv_key); if (len > SECRETKEY_BYTES - PUBLICKEY_BYTES) goto error;
BN_bn2bin(x->priv_key,sk + SECRETKEY_BYTES - len);
DSA_free(x);
return 0;
error:
DSA_free(x);
return -1;
}
示例4: openssl_dsa_crypt
void openssl_dsa_crypt()
{
DSA *d;
unsigned int size, len;
unsigned char inputs[COMM_LEN] = "dsa crypt";
unsigned char outputs[MAX1_LEN] = { 0 };
printf("\nDSA generate key:\n");
d = DSA_new();
DSA_generate_parameters_ex(d, LINE_LEN, NULL, 0, NULL, NULL, NULL);
DSA_generate_key(d);
DSA_print_fp(stdout, d, 0);
DSA_sign(NID_md5_sha1, inputs, 20, outputs, &len, d);
printf("DSA_sign(%s) = ", inputs);
for (size = 0; size < len; size++)
printf("%.02x", outputs[size]);
printf("\n");
DSA_verify(NID_md5_sha1, inputs, 20, outputs, len, d);
printf("DSA_verify(");
for (size = 0; size < len; size++)
printf("%.02x", outputs[size]);
printf(") = %s\n", inputs);
DSA_free(d);
}
示例5: keypair
void keypair()
{
char buf[1024];
int nmod=0;
while(fgets(buf,sizeof buf,stdin) != NULL)
{
if(!strncmp(buf,"[mod = ",7))
nmod=atoi(buf+7);
else if(!strncmp(buf,"N = ",4))
{
DSA *dsa;
int n=atoi(buf+4);
printf("[mod = %d]\n\n",nmod);
dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL);
pbn("P",dsa->p);
pbn("Q",dsa->q);
pbn("G",dsa->g);
putc('\n',stdout);
while(n--)
{
DSA_generate_key(dsa);
pbn("X",dsa->priv_key);
pbn("Y",dsa->pub_key);
putc('\n',stdout);
}
}
}
}
示例6: generate_dsa_key
static EP_STAT
generate_dsa_key(EP_CRYPTO_KEY *key, int keylen)
{
DSA *dsakey;
// generate new parameter block
dsakey = DSA_new();
if (DSA_generate_parameters_ex(dsakey, keylen,
NULL, 0, NULL, NULL, NULL) != 1)
{
_ep_crypto_error("cannot initialize DSA parameters");
goto fail0;
}
if (DSA_generate_key(dsakey) != 1)
{
_ep_crypto_error("cannot generate DSA key");
goto fail0;
}
if (EVP_PKEY_assign_DSA(key, dsakey) != 1)
{
_ep_crypto_error("cannot save DSA key");
goto fail0;
}
return EP_STAT_OK;
fail0:
return EP_STAT_CRYPTO_KEYCREATE;
}
示例7: keypair
static void keypair(FILE *in, FILE *out)
{
char buf[1024];
char lbuf[1024];
char *keyword, *value;
int dsa2, L, N;
while(fgets(buf,sizeof buf,in) != NULL)
{
if (!parse_line(&keyword, &value, lbuf, buf))
{
continue;
}
if(!strcmp(keyword,"[mod"))
{
if (!parse_mod(value, &dsa2, &L, &N, NULL))
{
fprintf(stderr, "Mod Parse Error\n");
exit (1);
}
fputs(buf,out);
}
else if(!strcmp(keyword,"N"))
{
DSA *dsa;
int n=atoi(value);
dsa = FIPS_dsa_new();
if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, NULL, NULL, 0,
NULL, NULL, NULL, NULL))
{
fprintf(stderr, "Parameter Generation error\n");
exit(1);
}
if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, NULL, NULL, 0, -1,
NULL, NULL, NULL, NULL) <= 0)
{
fprintf(stderr, "Parameter Generation error\n");
exit(1);
}
do_bn_print_name(out, "P",dsa->p);
do_bn_print_name(out, "Q",dsa->q);
do_bn_print_name(out, "G",dsa->g);
fputs(RESP_EOL, out);
while(n--)
{
if (!DSA_generate_key(dsa))
exit(1);
do_bn_print_name(out, "X",dsa->priv_key);
do_bn_print_name(out, "Y",dsa->pub_key);
fputs(RESP_EOL, out);
}
if (dsa)
FIPS_dsa_free(dsa);
}
}
}
示例8: ERROR_MSG
// Key factory
bool OSSLDSA::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */)
{
// Check parameters
if ((ppKeyPair == NULL) ||
(parameters == NULL))
{
return false;
}
if (!parameters->areOfType(DSAParameters::type))
{
ERROR_MSG("Invalid parameters supplied for DSA key generation");
return false;
}
DSAParameters* params = (DSAParameters*) parameters;
// Generate the key-pair
DSA* dsa = DSA_new();
if (dsa == NULL)
{
ERROR_MSG("Failed to instantiate OpenSSL DSA object");
return false;
}
// Use the OpenSSL implementation and not any engine
DSA_set_method(dsa, DSA_get_default_method());
dsa->p = OSSL::byteString2bn(params->getP());
dsa->q = OSSL::byteString2bn(params->getQ());
dsa->g = OSSL::byteString2bn(params->getG());
if (DSA_generate_key(dsa) != 1)
{
ERROR_MSG("DSA key generation failed (0x%08X)", ERR_get_error());
DSA_free(dsa);
return false;
}
// Create an asymmetric key-pair object to return
OSSLDSAKeyPair* kp = new OSSLDSAKeyPair();
((OSSLDSAPublicKey*) kp->getPublicKey())->setFromOSSL(dsa);
((OSSLDSAPrivateKey*) kp->getPrivateKey())->setFromOSSL(dsa);
*ppKeyPair = kp;
// Release the key
DSA_free(dsa);
return true;
}
示例9: genkeys
static int genkeys(void)
{
if (!TEST_ptr(dsakey = load_dsa_params()))
return 0;
if (!TEST_int_eq(DSA_generate_key(dsakey), 1))
return 0;
return 1;
}
示例10: main
int main(int argc, char **argv)
{
DSA *dsa;
unsigned char* input_string;
unsigned char* sign_string;
unsigned int sig_len;
unsigned int i;
if ( argc != 2 )
{
fprintf(stderr, "%s <plain text>\n", argv[0]);
exit(-1);
}
input_string = (unsigned char*)calloc(strlen(argv[1]) + 1, sizeof(unsigned char));
if ( input_string == NULL )
{
fprintf(stderr, "Unable to allocate memory for input_string\n");
exit(-1);
}
strncpy((char *)input_string, argv[1], strlen(argv[1]));
dsa = DSA_generate_parameters(1024, NULL, 0, NULL, NULL, NULL, NULL);
DSA_generate_key(dsa);
sign_string = (unsigned char *)calloc(DSA_size(dsa), sizeof(unsigned char));
if ( sign_string == NULL )
{
fprintf(stderr, "Unable to allocate memory for sign_string\n");
exit(-1);
}
if ( DSA_sign(0, input_string, strlen((char *)input_string), sign_string, &sig_len, dsa) == 0 )
{
fprintf(stderr, "Sign error\n");
exit(-1);
}
int is_valid_signature = DSA_verify(0, input_string, strlen((char*)input_string), sign_string, sig_len, dsa);
DSAparams_print_fp(stdout, dsa);
printf("input_string = %s\n", input_string);
printf("signed string = ");
for ( i = 0; i < sig_len; i++ )
printf("%x%x", (sign_string[i] >> 4) & 0xf, sign_string[i] & 0xf);
printf("\n");
printf("is_valid_signature? = %d\n", is_valid_signature);
return 0;
}
示例11: main
int main()
{
DSA *key;
FILE *fp1, *fp2;
unsigned char digest[8] = "1234567";
int siglen;
unsigned char signature[1000];
int retcode;
key = DSA_generate_parameters(1024, NULL, 0, NULL, NULL, NULL, NULL);
if (key == NULL)
{
printf("\nFailed to generate parameters\n");
exit(1);
}
fp1 = fopen("params.dat", "w");
DSAparams_print_fp(fp1, key);
fclose(fp1);
DSA_generate_key(key);
if (key == NULL)
{
printf("\nFailed to generate key\n");
exit(1);
}
fp2 = fopen("key.dat", "w");
DSA_print_fp(fp2, key, 0);
fclose(fp2);
retcode = DSA_sign(0, digest, 8, signature, &siglen, key);
if (retcode == 0)
{
printf("\n *** Error in signing ***\n\n");
exit(1);
}
printf("\n%s\n",signature);
retcode = DSA_verify(0, digest, 8, signature, siglen, key);
if (retcode == 1)
printf("\n *** Valid signature ***\n\n");
if (retcode == 0)
printf("\n *** Incorrect signature ***\n\n");
if (retcode == -1)
printf("\n *** Error in verifying ***\n\n");
DSA_free(key);
return 0;
}
示例12: dsa_test
static int dsa_test(void)
{
BN_GENCB *cb;
DSA *dsa = NULL;
int counter, ret = 0, i, j;
unsigned char buf[256];
unsigned long h;
unsigned char sig[256];
unsigned int siglen;
const BIGNUM *p = NULL, *q = NULL, *g = NULL;
if (!TEST_ptr(cb = BN_GENCB_new()))
goto end;
BN_GENCB_set(cb, dsa_cb, NULL);
if (!TEST_ptr(dsa = DSA_new())
|| !TEST_true(DSA_generate_parameters_ex(dsa, 512, seed, 20,
&counter, &h, cb)))
goto end;
if (!TEST_int_eq(counter, 105))
goto end;
if (!TEST_int_eq(h, 2))
goto end;
DSA_get0_pqg(dsa, &p, &q, &g);
i = BN_bn2bin(q, buf);
j = sizeof(out_q);
if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_q, i))
goto end;
i = BN_bn2bin(p, buf);
j = sizeof(out_p);
if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_p, i))
goto end;
i = BN_bn2bin(g, buf);
j = sizeof(out_g);
if (!TEST_int_eq(i, j) || !TEST_mem_eq(buf, i, out_g, i))
goto end;
DSA_generate_key(dsa);
DSA_sign(0, str1, 20, sig, &siglen, dsa);
if (TEST_true(DSA_verify(0, str1, 20, sig, siglen, dsa)))
ret = 1;
end:
DSA_free(dsa);
BN_GENCB_free(cb);
return ret;
}
示例13: switch
void pki_evp::generate(int bits, int type, QProgressBar *progress, int curve_nid)
{
RSA *rsakey;
DSA *dsakey;
EC_KEY *eckey;
progress->setMinimum(0);
progress->setMaximum(100);
progress->setValue(50);
switch (type) {
case EVP_PKEY_RSA:
rsakey = RSA_generate_key(bits, 0x10001, inc_progress_bar,
progress);
if (rsakey)
EVP_PKEY_assign_RSA(key, rsakey);
break;
case EVP_PKEY_DSA:
progress->setMaximum(500);
dsakey = DSA_generate_parameters(bits, NULL, 0, NULL, NULL,
inc_progress_bar, progress);
DSA_generate_key(dsakey);
if (dsakey)
EVP_PKEY_assign_DSA(key, dsakey);
break;
case EVP_PKEY_EC:
EC_GROUP *group = EC_GROUP_new_by_curve_name(curve_nid);
if (!group)
break;
eckey = EC_KEY_new();
if (eckey == NULL) {
EC_GROUP_free(group);
break;
}
EC_GROUP_set_asn1_flag(group, 1);
if (EC_KEY_set_group(eckey, group)) {
if (EC_KEY_generate_key(eckey)) {
EVP_PKEY_assign_EC_KEY(key, eckey);
EC_GROUP_free(group);
break;
}
}
EC_KEY_free(eckey);
EC_GROUP_free(group);
break;
}
pki_openssl_error();
encryptKey();
}
示例14: generate_dsa_keypair
static int generate_dsa_keypair(EVP_PKEY* pkey, const keymaster_dsa_keygen_params_t* dsa_params) {
if (dsa_params->key_size < 512) {
ALOGI("Requested DSA key size is too small (<512)");
return -1;
}
Unique_DSA dsa(DSA_new());
if (dsa_params->generator_len == 0 || dsa_params->prime_p_len == 0 ||
dsa_params->prime_q_len == 0 || dsa_params->generator == NULL ||
dsa_params->prime_p == NULL || dsa_params->prime_q == NULL) {
if (DSA_generate_parameters_ex(dsa.get(), dsa_params->key_size, NULL, 0, NULL, NULL,
NULL) != 1) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
} else {
dsa->g = BN_bin2bn(dsa_params->generator, dsa_params->generator_len, NULL);
if (dsa->g == NULL) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
dsa->p = BN_bin2bn(dsa_params->prime_p, dsa_params->prime_p_len, NULL);
if (dsa->p == NULL) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
dsa->q = BN_bin2bn(dsa_params->prime_q, dsa_params->prime_q_len, NULL);
if (dsa->q == NULL) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
}
if (DSA_generate_key(dsa.get()) != 1) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
if (EVP_PKEY_assign_DSA(pkey, dsa.get()) == 0) {
logOpenSSLError("generate_dsa_keypair");
return -1;
}
release_because_ownership_transferred(dsa);
return 0;
}
示例15: openssldsa_generate
static isc_result_t
openssldsa_generate(dst_key_t *key, int unused) {
#if OPENSSL_VERSION_NUMBER > 0x00908000L
BN_GENCB cb;
#endif
DSA *dsa;
unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
isc_result_t result;
UNUSED(unused);
result = dst__entropy_getdata(rand_array, sizeof(rand_array),
ISC_FALSE);
if (result != ISC_R_SUCCESS)
return (result);
#if OPENSSL_VERSION_NUMBER > 0x00908000L
dsa = DSA_new();
if (dsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
BN_GENCB_set_old(&cb, NULL, NULL);
if (!DSA_generate_parameters_ex(dsa, key->key_size, rand_array,
ISC_SHA1_DIGESTLENGTH, NULL, NULL,
&cb))
{
DSA_free(dsa);
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
}
#else
dsa = DSA_generate_parameters(key->key_size, rand_array,
ISC_SHA1_DIGESTLENGTH, NULL, NULL,
NULL, NULL);
if (dsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#endif
if (DSA_generate_key(dsa) == 0) {
DSA_free(dsa);
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
}
dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
key->opaque = dsa;
return (ISC_R_SUCCESS);
}