本文整理匯總了C++中EC_GROUP_new_by_curve_name函數的典型用法代碼示例。如果您正苦於以下問題:C++ EC_GROUP_new_by_curve_name函數的具體用法?C++ EC_GROUP_new_by_curve_name怎麽用?C++ EC_GROUP_new_by_curve_name使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EC_GROUP_new_by_curve_name函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: generate_ec_keypair
static int generate_ec_keypair(EVP_PKEY* pkey, const keymaster_ec_keygen_params_t* ec_params) {
Unique_EC_GROUP group;
switch (ec_params->field_size) {
case 224:
group.reset(EC_GROUP_new_by_curve_name(NID_secp224r1));
break;
case 256:
group.reset(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
break;
case 384:
group.reset(EC_GROUP_new_by_curve_name(NID_secp384r1));
break;
case 521:
group.reset(EC_GROUP_new_by_curve_name(NID_secp521r1));
break;
default:
break;
}
if (group.get() == NULL) {
logOpenSSLError("generate_ec_keypair");
return -1;
}
#if !defined(OPENSSL_IS_BORINGSSL)
EC_GROUP_set_point_conversion_form(group.get(), POINT_CONVERSION_UNCOMPRESSED);
EC_GROUP_set_asn1_flag(group.get(), OPENSSL_EC_NAMED_CURVE);
#endif
/* initialize EC key */
Unique_EC_KEY eckey(EC_KEY_new());
if (eckey.get() == NULL) {
logOpenSSLError("generate_ec_keypair");
return -1;
}
if (EC_KEY_set_group(eckey.get(), group.get()) != 1) {
logOpenSSLError("generate_ec_keypair");
return -1;
}
if (EC_KEY_generate_key(eckey.get()) != 1 || EC_KEY_check_key(eckey.get()) < 0) {
logOpenSSLError("generate_ec_keypair");
return -1;
}
if (EVP_PKEY_assign_EC_KEY(pkey, eckey.get()) == 0) {
logOpenSSLError("generate_ec_keypair");
return -1;
}
release_because_ownership_transferred(eckey);
return 0;
}
示例2: openssl_ec_crypt
void openssl_ec_crypt()
{
BIO *berr;
EC_KEY *key1, *key2;
unsigned int sig_len;
int clen, len1, len2;
EC_builtin_curve *curves;
EC_GROUP *group1, *group2;
const EC_KEY *key3, *key4;
const EC_GROUP *group3, *group4;
const EC_POINT *pubkey1, *pubkey2;
unsigned char shareKey1[COMM_LEN], shareKey2[COMM_LEN];
unsigned char *signature, cont[COMM_LEN] = "123456";
key1 = EC_KEY_new();
key2 = EC_KEY_new();
clen = EC_get_builtin_curves(NULL, 0);
curves = (EC_builtin_curve *) malloc(sizeof(EC_builtin_curve) * clen);
EC_get_builtin_curves(curves, clen);
group1 = EC_GROUP_new_by_curve_name(curves[25].nid);
group2 = EC_GROUP_new_by_curve_name(curves[25].nid);
group3 = group1;
group4 = group2;
EC_KEY_set_group(key1, group3);
EC_KEY_set_group(key2, group4);
EC_KEY_generate_key(key1);
EC_KEY_generate_key(key2);
EC_KEY_check_key(key1);
key3 = key1;
key4 = key2;
printf("\nECDSA_size: %d\n", ECDSA_size(key3));
signature = (unsigned char *)malloc(ECDSA_size(key3));
ERR_load_crypto_strings();
berr = BIO_new(BIO_s_file());
BIO_set_fp(berr, stdout, BIO_NOCLOSE);
ECDSA_sign(0, cont, 8, signature, &sig_len, key1);
ECDSA_verify(0, cont, 8, signature, sig_len, key1);
pubkey1 = EC_KEY_get0_public_key(key1);
pubkey2 = EC_KEY_get0_public_key(key2);
len1 = ECDH_compute_key(shareKey1, COMM_LEN, pubkey2, key1, NULL);
len2 = ECDH_compute_key(shareKey2, COMM_LEN, pubkey1, key1, NULL);
if (len1 != len2 || memcmp(shareKey1, shareKey2, len1) != 0) {
printf("ECDH_compute_key err!\n");
return;
}
BIO_free(berr);
EC_KEY_free(key1);
EC_KEY_free(key2);
free(signature);
free(curves);
}
示例3: get_sequence
secret_parameter deterministic_wallet::generate_secret(
size_t n, bool for_change) const
{
if (seed_.empty())
return null_hash;
ssl_bignum z;
hash_digest sequence = get_sequence(n, for_change);
BN_bin2bn(sequence.data(), sequence.size(), z);
ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
ssl_bignum order;
bn_ctx ctx(BN_CTX_new());
EC_GROUP_get_order(group, order, ctx);
// secexp = (stretched_seed + z) % order
ssl_bignum secexp;
BN_bin2bn(stretched_seed_.data(), stretched_seed_.size(), secexp);
BN_add(secexp, secexp, z);
BN_mod(secexp, secexp, order, ctx);
secret_parameter secret;
int secexp_bytes_size = BN_num_bytes(secexp);
BITCOIN_ASSERT(secexp_bytes_size >= 0 &&
static_cast<size_t>(BN_num_bytes(secexp)) <= secret.size());
// If bignum value begins with 0x00, then
// SSL will skip to the first significant digit.
size_t copy_offset = secret.size() - BN_num_bytes(secexp);
BN_bn2bin(secexp, secret.data() + copy_offset);
// Zero out beginning 0x00 bytes (if they exist).
std::fill(secret.begin(), secret.begin() + copy_offset, 0x00);
return secret;
}
示例4: ERR_load_BIO_strings
void crypt_ec_helper::save_key_pair(std::string path, EC_KEY *keypair)
{
BIO *out;
int i;
FILE* outfile;
EVP_PKEY *pkey = NULL;
ERR_load_BIO_strings();
ERR_load_crypto_strings();
pkey = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(pkey, keypair);
EC_GROUP *ecgroup = EC_GROUP_new_by_curve_name(NID_secp256k1);
outfile = fopen(path.c_str(), "w");
out = BIO_new(BIO_s_file());
out = BIO_new_fp(outfile, BIO_NOCLOSE);
EC_KEY_set_asn1_flag(keypair, OPENSSL_EC_NAMED_CURVE);
i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, 0, NULL);
fclose(outfile);
EC_GROUP_free(ecgroup);
EVP_PKEY_free(pkey);
BIO_free_all(out);
}
示例5: e
blob WSService::pubkey_from_cert(X509* x509) {
std::runtime_error e("Certificate error");
EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(OBJ_sn2nid("prime256v1"));
BN_CTX* bn_ctx = BN_CTX_new();
std::vector<uint8_t> raw_public(33);
try {
if(ec_group == NULL || bn_ctx == NULL) throw e;
EVP_PKEY* remote_pkey = X509_get_pubkey(x509); if(remote_pkey == NULL) throw e;
EC_KEY* remote_eckey = EVP_PKEY_get1_EC_KEY(remote_pkey); if(remote_eckey == NULL) throw e;
const EC_POINT* remote_pubkey = EC_KEY_get0_public_key(remote_eckey); if(remote_pubkey == NULL) throw e;
EC_POINT_point2oct(ec_group, remote_pubkey, POINT_CONVERSION_COMPRESSED, raw_public.data(), raw_public.size(), bn_ctx);
}catch(...){
BN_CTX_free(bn_ctx); EC_GROUP_free(ec_group);
bn_ctx = NULL; ec_group = NULL;
throw;
}
BN_CTX_free(bn_ctx); EC_GROUP_free(ec_group);
return raw_public;
}
示例6: BN_CTX_new
bool CECKey::TweakSecret(unsigned char vchSecretOut[32], const unsigned char vchSecretIn[32], const unsigned char vchTweak[32])
{
bool ret = true;
BN_CTX *ctx = BN_CTX_new();
BN_CTX_start(ctx);
BIGNUM *bnSecret = BN_CTX_get(ctx);
BIGNUM *bnTweak = BN_CTX_get(ctx);
BIGNUM *bnOrder = BN_CTX_get(ctx);
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
BN_bin2bn(vchTweak, 32, bnTweak);
if (BN_cmp(bnTweak, bnOrder) >= 0)
ret = false; // extremely unlikely
BN_bin2bn(vchSecretIn, 32, bnSecret);
BN_add(bnSecret, bnSecret, bnTweak);
BN_nnmod(bnSecret, bnSecret, bnOrder, ctx);
if (BN_is_zero(bnSecret))
ret = false; // ridiculously unlikely
int nBits = BN_num_bits(bnSecret);
memset(vchSecretOut, 0, 32);
BN_bn2bin(bnSecret, &vchSecretOut[32-(nBits+7)/8]);
EC_GROUP_free(group);
BN_CTX_end(ctx);
BN_CTX_free(ctx);
return ret;
}
示例7: HDW_public_data_from_private_data
int HDW_public_data_from_private_data(uint8_t *key_data, size_t key_data_len, BIGNUM *public_compressed_key) {
int res = 1;
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
BIGNUM *priv = BN_new();
EC_POINT *ec_point = EC_POINT_new(group);
FCHK_SET(BN_bin2bn(key_data, (int) key_data_len, priv), ==0, res, fail, 0);
// Generate public key.
FCHK(EC_POINT_mul(group, ec_point, priv, NULL, NULL, NULL), ==0, res, fail);
FCHK_SET(EC_POINT_point2bn(group, ec_point, POINT_CONVERSION_COMPRESSED, public_compressed_key, NULL), ==0, res,
fail, 0);
FCHK_SET(BN_num_bytes(public_compressed_key), !=33, res, fail, 0);
fail:
if (res == 0) {
BN_zero(public_compressed_key);
}
EC_POINT_free(ec_point);
BN_free(priv);
EC_GROUP_free(group);
return res;
}
示例8: verify_x962_octets_are_on_p256
int verify_x962_octets_are_on_p256(const unsigned char* octets, size_t len) {
assert(octets);
assert(len);
EC_GROUP* p256group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
if (!p256group) {
fprintf(stderr, "error: EC_GROUP_new_by_curve_name() failed.\n");
return 4;
}
EC_POINT* point = EC_POINT_new(p256group);
if (!point) {
fprintf(stderr, "error: EC_POINT_new() failed.\n");
EC_GROUP_free(p256group);
return 4;
}
if (0 == EC_POINT_oct2point(p256group, point, octets, len, NULL)) {
fprintf(stderr, "error: EC_POINT_oct2point() failed.\n");
EC_POINT_free(point);
EC_GROUP_free(p256group);
return 4;
}
if (0 == EC_POINT_is_on_curve(p256group, point, NULL)) {
fprintf(stderr, "error: Public key point isn't on P-256 curve.\n");
EC_POINT_free(point);
EC_GROUP_free(p256group);
return 4;
}
EC_POINT_free(point);
EC_GROUP_free(p256group);
return 0;
}
示例9: EC_GROUP_new_by_curve_name
EC_KEY *EC_KEY_new_by_curve_name_NID_secp256k1(void)
{
static EC_GROUP *group = NULL;
EC_KEY *ret = NULL;
if (group == NULL) {
#ifdef HAVE_NID_secp256k1
group = EC_GROUP_new_by_curve_name(NID_secp256k1);
#else
group = ec_group_new_from_data(&EC_SECG_PRIME_256K1.h);
#endif
if (group == NULL) {
return NULL;
}
}
ret = EC_KEY_new();
if (ret == NULL) {
return NULL;
}
EC_KEY_set_group(ret, group);
return ret;
}
示例10: get_sequence
secret_parameter deterministic_wallet::generate_secret(
size_t n, bool for_change)
{
if (seed_.empty())
return null_hash;
ssl_bignum z;
hash_digest sequence = get_sequence(n, for_change);
BN_bin2bn(sequence.data(), sequence.size(), z);
ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
ssl_bignum order;
bn_ctx ctx(BN_CTX_new());
EC_GROUP_get_order(group, order, ctx);
// secexp = (stretched_seed + z) % order
ssl_bignum secexp;
BN_bin2bn(stretched_seed_.data(), stretched_seed_.size(), secexp);
BN_add(secexp, secexp, z);
BN_mod(secexp, secexp, order, ctx);
secret_parameter secret;
BITCOIN_ASSERT(BN_num_bytes(secexp) == secret.size());
BN_bn2bin(secexp, secret.data());
return secret;
}
示例11: main
int main(void) {
int i;
secp256k1_pubkey pubkey;
secp256k1_ecdsa_signature sig;
benchmark_verify_t data;
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
for (i = 0; i < 32; i++) {
data.msg[i] = 1 + i;
}
for (i = 0; i < 32; i++) {
data.key[i] = 33 + i;
}
data.siglen = 72;
CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
data.pubkeylen = 33;
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
#ifdef ENABLE_OPENSSL_TESTS
data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1);
run_benchmark("ecdsa_verify_openssl", benchmark_verify_openssl, NULL, NULL, &data, 10, 20000);
EC_GROUP_free(data.ec_group);
#endif
secp256k1_context_destroy(data.ctx);
return 0;
}
示例12: verification
int verification(
const unsigned char m[SHORTHASH_BYTES],const unsigned long long mlen,
const unsigned char sm[SIGNATURE_BYTES],const unsigned long long smlen,
const unsigned char pk[PUBLICKEY_BYTES],const unsigned long long pklen
)
{
unsigned char h[20];
EC_GROUP *group;
EC_KEY *k;
EC_POINT *kxy;
BIGNUM *kx;
BIGNUM *ky;
ECDSA_SIG *rs;
int len;
if (smlen != SIGNATURE_BYTES) return -1;
if (mlen > SHORTHASH_BYTES) return -1;
SHA1(m,mlen,h);
group = EC_GROUP_new_by_curve_name(NID);
if (!group) return -1;
kx = BN_new(); if (!kx) return -1;
ky = BN_new(); if (!ky) { BN_free(kx); return -1; }
kxy = EC_POINT_new(group); if (!kxy) { BN_free(ky); BN_free(kx); return -1; }
k = EC_KEY_new(); if (!k) { EC_POINT_free(kxy); BN_free(ky); BN_free(kx); return -1; }
rs = ECDSA_SIG_new(); if (!rs) { EC_KEY_free(k); EC_POINT_free(kxy); BN_free(ky); BN_free(kx); return -1; }
if (!EC_KEY_set_group(k,group)) goto error;
if (!BN_bin2bn(pk,PRIME_BYTES,kx)) goto error; pk += PRIME_BYTES;
if (!BN_bin2bn(pk,PRIME_BYTES,ky)) goto error;
#ifdef PRIME_FIELD
if (!EC_POINT_set_affine_coordinates_GFp(group,kxy,kx,ky,0)) goto error;
#else
if (!EC_POINT_set_affine_coordinates_GF2m(group,kxy,kx,ky,0)) goto error;
#endif
if (!EC_KEY_set_public_key(k,kxy)) goto error;
if (!BN_bin2bn(sm,PRIME_BYTES,rs->r)) goto error; sm += PRIME_BYTES;
if (!BN_bin2bn(sm,PRIME_BYTES,rs->s)) goto error;
len = ECDSA_do_verify(h,20,rs,k);
ECDSA_SIG_free(rs);
EC_KEY_free(k);
EC_POINT_free(kxy);
BN_free(ky);
BN_free(kx);
if (len == 1) return 0;
if (len == 0) return -100;
return -1;
error:
ECDSA_SIG_free(rs);
EC_KEY_free(k);
EC_POINT_free(kxy);
BN_free(ky);
BN_free(kx);
return -1;
}
示例13: ecdsa_private_key_import
static int
ecdsa_private_key_import(hx509_context context,
const AlgorithmIdentifier *keyai,
const void *data,
size_t len,
hx509_key_format_t format,
hx509_private_key private_key)
{
const unsigned char *p = data;
EC_KEY **pkey = NULL;
EC_KEY *key;
if (keyai->parameters) {
EC_GROUP *group;
int groupnid;
int ret;
ret = parse_ECParameters(context, keyai->parameters, &groupnid);
if (ret)
return ret;
key = EC_KEY_new();
if (key == NULL)
return ENOMEM;
group = EC_GROUP_new_by_curve_name(groupnid);
if (group == NULL) {
EC_KEY_free(key);
return ENOMEM;
}
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
if (EC_KEY_set_group(key, group) == 0) {
EC_KEY_free(key);
EC_GROUP_free(group);
return ENOMEM;
}
EC_GROUP_free(group);
pkey = &key;
}
switch (format) {
case HX509_KEY_FORMAT_DER:
private_key->private_key.ecdsa = d2i_ECPrivateKey(pkey, &p, len);
if (private_key->private_key.ecdsa == NULL) {
hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED,
"Failed to parse EC private key");
return HX509_PARSING_KEY_FAILED;
}
private_key->signature_alg = ASN1_OID_ID_ECDSA_WITH_SHA256;
break;
default:
return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED;
}
return 0;
}
示例14: while
const EC_POINT* crypt_ec_helper::from_base58(std::string base58)
{
const char* psz = base58.c_str();
std::vector<unsigned char> vch;
// Skip leading spaces.
while (*psz && isspace(*psz))
psz++;
// Skip and count leading '1's.
int zeroes = 0;
while (*psz == '1') {
zeroes++;
psz++;
}
// Allocate enough space in big-endian base256 representation.
std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
// Process the characters.
while (*psz && !isspace(*psz)) {
// Decode base58 character
const char* ch = strchr(pszBase58, *psz);
if (ch == NULL)
return NULL;
// Apply "b256 = b256 * 58 + ch".
int carry = ch - pszBase58;
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
carry += 58 * (*it);
*it = carry % 256;
carry /= 256;
}
assert(carry == 0);
psz++;
}
// Skip trailing spaces.
while (isspace(*psz))
psz++;
if (*psz != 0)
return NULL;
// Skip leading zeroes in b256.
std::vector<unsigned char>::iterator it = b256.begin();
while (it != b256.end() && *it == 0)
it++;
// Copy result into output vector.
vch.reserve(zeroes + (b256.end() - it));
vch.assign(zeroes, 0x00);
while (it != b256.end())
vch.push_back(*(it++));
EC_GROUP *ecgrp = EC_GROUP_new_by_curve_name(NID_secp256k1);
pub = EC_POINT_new(ecgrp);
size_t len = EC_POINT_oct2point(ecgrp, pub, vch.data(), vch.size(), NULL);
EC_GROUP_free(ecgrp);
return pub;
}
示例15: EC_KEY_new
EC_KEY* crypt_ec_helper::generate_key_pair()
{
eckey = EC_KEY_new();
EC_GROUP *ecgroup = EC_GROUP_new_by_curve_name(NID_secp256k1);
EC_KEY_set_group(eckey, ecgroup);
EC_KEY_generate_key(eckey);
EC_GROUP_free(ecgroup);
return eckey;
}