本文整理匯總了C++中EC_KEY_check_key函數的典型用法代碼示例。如果您正苦於以下問題:C++ EC_KEY_check_key函數的具體用法?C++ EC_KEY_check_key怎麽用?C++ EC_KEY_check_key使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EC_KEY_check_key函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: test_builtin
int test_builtin(BIO *out)
{
EC_builtin_curve *curves = NULL;
size_t crv_len = 0, n = 0;
EC_KEY *eckey = NULL, *wrong_eckey = NULL;
EC_GROUP *group;
ECDSA_SIG *ecdsa_sig = NULL;
unsigned char digest[20], wrong_digest[20];
unsigned char *signature = NULL;
const unsigned char *sig_ptr;
unsigned char *sig_ptr2;
unsigned char *raw_buf = NULL;
unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
int nid, ret = 0;
/* fill digest values with some random data */
if (!RAND_pseudo_bytes(digest, 20) ||
!RAND_pseudo_bytes(wrong_digest, 20)) {
BIO_printf(out, "ERROR: unable to get random data\n");
goto builtin_err;
}
/*
* create and verify a ecdsa signature with every availble curve (with )
*/
BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
"with some internal curves:\n");
/* get a list of all internal curves */
crv_len = EC_get_builtin_curves(NULL, 0);
curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
if (curves == NULL) {
BIO_printf(out, "malloc error\n");
goto builtin_err;
}
if (!EC_get_builtin_curves(curves, crv_len)) {
BIO_printf(out, "unable to get internal curves\n");
goto builtin_err;
}
/* now create and verify a signature for every curve */
for (n = 0; n < crv_len; n++) {
unsigned char dirt, offset;
nid = curves[n].nid;
if (nid == NID_ipsec4)
continue;
/* create new ecdsa key (== EC_KEY) */
if ((eckey = EC_KEY_new()) == NULL)
goto builtin_err;
group = EC_GROUP_new_by_curve_name(nid);
if (group == NULL)
goto builtin_err;
if (EC_KEY_set_group(eckey, group) == 0)
goto builtin_err;
EC_GROUP_free(group);
degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
if (degree < 160)
/* drop the curve */
{
EC_KEY_free(eckey);
eckey = NULL;
continue;
}
BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
/* create key */
if (!EC_KEY_generate_key(eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
/* create second key */
if ((wrong_eckey = EC_KEY_new()) == NULL)
goto builtin_err;
group = EC_GROUP_new_by_curve_name(nid);
if (group == NULL)
goto builtin_err;
if (EC_KEY_set_group(wrong_eckey, group) == 0)
goto builtin_err;
EC_GROUP_free(group);
if (!EC_KEY_generate_key(wrong_eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
BIO_printf(out, ".");
(void)BIO_flush(out);
/* check key */
if (!EC_KEY_check_key(eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
BIO_printf(out, ".");
(void)BIO_flush(out);
/* create signature */
sig_len = ECDSA_size(eckey);
if ((signature = OPENSSL_malloc(sig_len)) == NULL)
goto builtin_err;
//.........這裏部分代碼省略.........
示例2: key_get_pubkey_int
static bool key_get_pubkey_int(struct key *k, uint8 **pub, size_t *len) {
uint8 *data;
ASSERT(pub);
*pub = NULL;
*len = 0;
if (!EC_KEY_check_key(k->key)) {
NOT_TESTED();
return 0;
}
*len = i2o_ECPublicKey(k->key, 0);
ASSERT(*len <= 65);
data = safe_malloc(*len);
*pub = data;
i2o_ECPublicKey(k->key, &data);
return 1;
}
示例3: TEST_P
TEST_P(KeymasterGenerateECTest, GenerateKeyPair_EC_Success) {
keymaster_keypair_t key_type = TYPE_EC;
keymaster_ec_keygen_params_t params = {
.field_size = GetParam(),
};
uint8_t* key_blob;
size_t key_blob_length;
ASSERT_EQ(0,
sDevice->generate_keypair(sDevice, key_type, ¶ms, &key_blob, &key_blob_length))
<< "Should generate an EC key with " << GetParam() << " field size";
UniqueKey key(&sDevice, key_blob, key_blob_length);
uint8_t* x509_data = NULL;
size_t x509_data_length;
ASSERT_EQ(0,
sDevice->get_keypair_public(sDevice, key_blob, key_blob_length,
&x509_data, &x509_data_length))
<< "Should be able to retrieve EC public key successfully";
UniqueBlob x509_blob(x509_data, x509_data_length);
ASSERT_FALSE(x509_blob.get() == NULL)
<< "X509 data should be allocated";
const unsigned char *tmp = static_cast<const unsigned char*>(x509_blob.get());
Unique_EVP_PKEY actual(d2i_PUBKEY((EVP_PKEY**) NULL, &tmp,
static_cast<long>(x509_blob.length())));
ASSERT_EQ(EVP_PKEY_EC, EVP_PKEY_type(actual.get()->type))
<< "Generated key type should be of type EC";
Unique_EC_KEY ecKey(EVP_PKEY_get1_EC_KEY(actual.get()));
ASSERT_FALSE(ecKey.get() == NULL)
<< "Should be able to extract EC key from EVP_PKEY";
ASSERT_FALSE(EC_KEY_get0_group(ecKey.get()) == NULL)
<< "EC key should have a EC_GROUP";
ASSERT_TRUE(EC_KEY_check_key(ecKey.get()))
<< "EC key should check correctly";
}
示例4: get_PrivateKey
STDMETHODIMP CBECC::get_PrivateKey(VARIANT *pVal)
{
if (m_pECC == NULL)
return E_NOTIMPL;
if (!EC_KEY_check_key((EC_KEY*)m_pECC))
return E_NOTIMPL;
int nSize;
if((nSize = i2d_ECPrivateKey((EC_KEY*)m_pECC, NULL)) < 0)
return E_NOTIMPL;
CBVarPtr varPtr;
varPtr.Create(nSize);
if (!i2d_ECPrivateKey((EC_KEY*)m_pECC, (unsigned char **)&varPtr.m_pData))
return E_INVALIDARG;
return varPtr.GetVariant(pVal);
}
示例5: DSASign
STDMETHODIMP CBECC::DSASign(VARIANT varData, VARIANT *pVal)
{
if(m_pECC == NULL)return E_NOTIMPL;
if (!EC_KEY_check_key((EC_KEY*)m_pECC))
return E_NOTIMPL;
CBVarPtr varPtr;
HRESULT hr = varPtr.Attach(varData);
if(FAILED(hr))return hr;
int nSize = ECDSA_size((EC_KEY*)m_pECC);
CBVarPtr varVal;
varVal.Create(nSize);
if (!ECDSA_sign(0, varPtr.m_pData, varPtr.m_nSize, varVal.m_pData, (unsigned int *)&nSize, (EC_KEY*)m_pECC))
return E_INVALIDARG;
return varVal.GetVariant(pVal, nSize);
}
示例6: ossl_ec_key_to_string
static VALUE ossl_ec_key_to_string(VALUE self, VALUE ciph, VALUE pass, int format)
{
EC_KEY *ec;
BIO *out;
int i = -1;
int private = 0;
VALUE str;
Require_EC_KEY(self, ec);
if (EC_KEY_get0_public_key(ec) == NULL)
ossl_raise(eECError, "can't export - no public key set");
if (EC_KEY_check_key(ec) != 1)
ossl_raise(eECError, "can't export - EC_KEY_check_key failed");
if (EC_KEY_get0_private_key(ec))
private = 1;
if (!(out = BIO_new(BIO_s_mem())))
ossl_raise(eECError, "BIO_new(BIO_s_mem())");
switch(format) {
case EXPORT_PEM:
if (private) {
const EVP_CIPHER *cipher = NULL;
if (!NIL_P(ciph)) {
cipher = GetCipherPtr(ciph);
pass = ossl_pem_passwd_value(pass);
}
i = PEM_write_bio_ECPrivateKey(out, ec, cipher, NULL, 0, ossl_pem_passwd_cb, (void *)pass);
} else {
i = PEM_write_bio_EC_PUBKEY(out, ec);
}
break;
case EXPORT_DER:
if (private) {
i = i2d_ECPrivateKey_bio(out, ec);
} else {
示例7: el_create_context
el_context_t el_create_context(el_curve_t curve,
const uint8_t *publicKeyData, int publicKeyLength)
{
EC_KEY *key = NULL;
int digestLength = 0;
switch (curve)
{
case el_curve_secp112r1:
key = EC_KEY_new_by_curve_name(NID_secp112r1);
digestLength = 14;
break;
case el_curve_secp128r1:
key = EC_KEY_new_by_curve_name(NID_secp128r1);
digestLength = 16;
break;
case el_curve_secp160r1:
key = EC_KEY_new_by_curve_name(NID_secp160r1);
digestLength = 20;
break;
}
if (!key)
return NULL;
key = o2i_ECPublicKey(&key, &publicKeyData, publicKeyLength);
if (!key)
return NULL;
if (!EC_KEY_check_key(key))
{
EC_KEY_free(key);
return NULL;
}
el_context_t ctxt = malloc(sizeof(struct el_context));
ctxt->ecKey = key;
ctxt->curve = curve;
ctxt->digestLength = digestLength;
return ctxt;
}
示例8: SetPrivKey
bool CKey::SetPrivKey(const CPrivKey& vchPrivKey)
{
const unsigned char* pbegin = &vchPrivKey[0];
if (d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size()))
{
// In testing, d2i_ECPrivateKey can return true
// but fill in pkey with a key that fails
// EC_KEY_check_key, so:
if (EC_KEY_check_key(pkey))
{
fSet = true;
return true;
}
}
// If vchPrivKey data is bad d2i_ECPrivateKey() can
// leave pkey in a state where calling EC_KEY_free()
// crashes. To avoid that, set pkey to NULL and
// leak the memory (a leak is better than a crash)
pkey = NULL;
Reset();
return false;
}
示例9: DSAVerify
STDMETHODIMP CBECC::DSAVerify(VARIANT varData, VARIANT varSig, VARIANT_BOOL *retVal)
{
if(m_pECC == NULL)return E_NOTIMPL;
if (!EC_KEY_check_key((EC_KEY*)m_pECC))
return E_NOTIMPL;
CBVarPtr varPtrData, varPtrSig;
HRESULT hr = varPtrData.Attach(varData);
if(FAILED(hr))return hr;
hr = varPtrSig.Attach(varSig);
if(FAILED(hr))return hr;
int n = ECDSA_verify(0, varPtrData.m_pData, varPtrData.m_nSize, varPtrSig.m_pData, varPtrSig.m_nSize, (EC_KEY*)m_pECC);
if (n == -1)
return E_INVALIDARG;
*retVal = n ? VARIANT_TRUE : VARIANT_FALSE;
return S_OK;
}
示例10: main
int main(int argc, const char **argv)
{
EC_KEY *pub;
char workbuf[BUFSIZE];
const unsigned char *workbuf_p;
size_t len, i;
if (argv[1] == NULL)
{
fprintf(stderr, "usage: %s [base64key]\n", argv[0]);
return EXIT_FAILURE;
}
memset(workbuf, '\0', sizeof workbuf);
pub = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
EC_KEY_set_conv_form(pub, POINT_CONVERSION_COMPRESSED);
len = base64_decode(argv[1], workbuf, sizeof workbuf);
workbuf_p = (unsigned char *) workbuf;
if (len == (size_t) -1)
{
fprintf(stderr, "Failed to decode key!\n");
return EXIT_FAILURE;
}
o2i_ECPublicKey(&pub, &workbuf_p, len);
if (!EC_KEY_check_key(pub))
{
fprintf(stderr, "Key data provided on commandline is inconsistent.\n");
return EXIT_FAILURE;
}
printf("Public key (reassembled):\n");
EC_KEY_print_fp(stdout, pub, 4);
return EXIT_SUCCESS;
}
示例11: EC_KEY_new_by_curve_name
EC_KEY *helper_gateway_key(const tal_t *ctx)
{
const unsigned char *p = gateway_key;
EC_KEY *priv = EC_KEY_new_by_curve_name(NID_secp256k1);
EC_KEY **ptr;
if (!d2i_ECPrivateKey(&priv, &p, sizeof(gateway_key)))
abort();
if (!EC_KEY_check_key(priv))
abort();
/* We *always* used compressed form keys. */
EC_KEY_set_conv_form(priv, POINT_CONVERSION_COMPRESSED);
/* To get tal to clean it up... */
ptr = tal(ctx, EC_KEY *);
*ptr = priv;
tal_add_destructor(ptr, free_gateway_key);
return priv;
}
示例12: BN_new
// Get the AlphaCrypt default PEER public Key
EC_POINT * CAlphaCrypt::GetAlphaCryptPublicKey() {
EC_KEY * lpPublicCurve = NULL; // Curve that contains the public key
EC_POINT * pubKey = NULL; // Public key generated from the 2 coordinates
const LPSTR XCoordHex = "46668077A4449322CA896BD64901DE333156B6FEAE75ABE5D4922A039B3CD013";
const LPSTR YCoordHex = "304AB8B3F15F498094F14058A1D1EBE823BEF512D44210CC50BBD94128D2CD05";
BIGNUM * pBnX = NULL, * pBnY = NULL;
int iRet = 0;
// Allocate the 2 points structures
pBnX = BN_new(); pBnY = BN_new();
// Get X and Y Coordinate
BN_hex2bn(&pBnX, XCoordHex);
BN_hex2bn(&pBnY, YCoordHex);
// Create the curve that contains the public key
lpPublicCurve = EC_KEY_new_by_curve_name(NID_secp256k1);
// Create the generator
pubKey = EC_POINT_new(lpPublicCurve->group);
// Generate the Public key and verify it
EC_POINT_set_affine_coordinates_GFp(lpPublicCurve->group, pubKey, pBnX, pBnY, NULL);
EC_KEY_set_public_key(lpPublicCurve, pubKey);
iRet = EC_KEY_check_key(lpPublicCurve);
// Cleanup
EC_KEY_free(lpPublicCurve);
BN_free(pBnX); BN_free(pBnY);
if (iRet)
return pubKey;
else
EC_POINT_free(pubKey);
return NULL;
}
示例13: key_get_privkey
bool key_get_privkey(struct key *k, uint8 **priv, size_t *len) {
ASSERT(priv);
*priv = NULL;
*len = 0;
if (!EC_KEY_check_key(k->key)) {
return 0;
}
const BIGNUM *bn = EC_KEY_get0_private_key(k->key);
if (bn == NULL) {
return 0;
}
*len = BN_num_bytes(bn) + 1;
*priv = safe_malloc(*len);
BN_bn2bin(bn, *priv);
/*
* Compressed key.
*/
(*priv)[*len - 1] = 1;
return 1;
}
示例14: EC_KEY_set_public_key_affine_coordinates
int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
BIGNUM *y)
{
BN_CTX *ctx = NULL;
BIGNUM *tx, *ty;
EC_POINT *point = NULL;
int ok = 0;
#ifndef OPENSSL_NO_EC2M
int tmp_nid, is_char_two = 0;
#endif
if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
ctx = BN_CTX_new();
if (ctx == NULL)
return 0;
BN_CTX_start(ctx);
point = EC_POINT_new(key->group);
if (point == NULL)
goto err;
tx = BN_CTX_get(ctx);
ty = BN_CTX_get(ctx);
if (ty == NULL)
goto err;
#ifndef OPENSSL_NO_EC2M
tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
if (tmp_nid == NID_X9_62_characteristic_two_field)
is_char_two = 1;
if (is_char_two) {
if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
x, y, ctx))
goto err;
if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
tx, ty, ctx))
goto err;
} else
#endif
{
if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
x, y, ctx))
goto err;
if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
tx, ty, ctx))
goto err;
}
/*
* Check if retrieved coordinates match originals and are less than field
* order: if not values are out of range.
*/
if (BN_cmp(x, tx) || BN_cmp(y, ty)
|| (BN_cmp(x, key->group->field) >= 0)
|| (BN_cmp(y, key->group->field) >= 0)) {
ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
EC_R_COORDINATES_OUT_OF_RANGE);
goto err;
}
if (!EC_KEY_set_public_key(key, point))
goto err;
if (EC_KEY_check_key(key) == 0)
goto err;
ok = 1;
err:
BN_CTX_end(ctx);
BN_CTX_free(ctx);
EC_POINT_free(point);
return ok;
}
示例15: test_builtin
int test_builtin(BIO *out) {
size_t n = 0;
EC_KEY *eckey = NULL, *wrong_eckey = NULL;
EC_GROUP *group;
BIGNUM *order = NULL;
ECDSA_SIG *ecdsa_sig = NULL;
unsigned char digest[20], wrong_digest[20];
unsigned char *signature = NULL;
const unsigned char *sig_ptr;
unsigned char *sig_ptr2;
unsigned char *raw_buf = NULL;
unsigned int sig_len, r_len, s_len, bn_len, buf_len;
int nid, ret = 0;
/* fill digest values with some random data */
if (!RAND_pseudo_bytes(digest, 20) || !RAND_pseudo_bytes(wrong_digest, 20)) {
BIO_printf(out, "ERROR: unable to get random data\n");
goto builtin_err;
}
order = BN_new();
if (order == NULL) {
goto builtin_err;
}
/* create and verify a ecdsa signature with every availble curve
* (with ) */
BIO_printf(out,
"\ntesting ECDSA_sign() and ECDSA_verify() "
"with some internal curves:\n");
static const int kCurveNIDs[] = {NID_secp224r1, NID_X9_62_prime256v1,
NID_secp384r1, NID_secp521r1, NID_undef};
/* now create and verify a signature for every curve */
for (n = 0; kCurveNIDs[n] != NID_undef; n++) {
unsigned char dirt, offset;
nid = kCurveNIDs[n];
/* create new ecdsa key (== EC_KEY) */
eckey = EC_KEY_new();
if (eckey == NULL) {
goto builtin_err;
}
group = EC_GROUP_new_by_curve_name(nid);
if (group == NULL) {
goto builtin_err;
}
if (!EC_KEY_set_group(eckey, group)) {
goto builtin_err;
}
EC_GROUP_free(group);
if (!EC_GROUP_get_order(EC_KEY_get0_group(eckey), order, NULL)) {
goto builtin_err;
}
if (BN_num_bits(order) < 160) {
/* Too small to test. */
EC_KEY_free(eckey);
eckey = NULL;
continue;
}
BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
/* create key */
if (!EC_KEY_generate_key(eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
/* create second key */
wrong_eckey = EC_KEY_new();
if (wrong_eckey == NULL) {
goto builtin_err;
}
group = EC_GROUP_new_by_curve_name(nid);
if (group == NULL) {
goto builtin_err;
}
if (EC_KEY_set_group(wrong_eckey, group) == 0) {
goto builtin_err;
}
EC_GROUP_free(group);
if (!EC_KEY_generate_key(wrong_eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
BIO_printf(out, ".");
(void)BIO_flush(out);
/* check key */
if (!EC_KEY_check_key(eckey)) {
BIO_printf(out, " failed\n");
goto builtin_err;
}
BIO_printf(out, ".");
(void)BIO_flush(out);
/* create signature */
sig_len = ECDSA_size(eckey);
signature = OPENSSL_malloc(sig_len);
if (signature == NULL) {
goto builtin_err;
//.........這裏部分代碼省略.........