Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
In my get_keypair function,I want to create an EC_KEY structure by
EC_KEY_new_by_curve_name(NID_secp256k1)
.By doing so,I got a crash on my vs2019 with no details.Below comes my code.
bool get_keypair(EC_KEY** key, bool getcompressed,uint8_t* rawprikey, uint8_t rawprikeylen) {
BIGNUM* priv; //prikey bignum
BN_CTX* ctx; //hold the operation
const EC_GROUP* group;
EC_POINT* pub; //pubkey point st
*key = EC_KEY_new_by_curve_name(NID_secp256k1); //secp256k1, !!!crashed here
if (*key == 0) {
printf("new ec key failed");
exit(-1);
priv = BN_new(); //create bignum
BN_bin2bn(rawprikey, 32, priv); //fill the bigbum with prikey
EC_KEY_set_private_key(*key, priv); //put prikey bignum to key pair
ctx = BN_CTX_new(); //create context
BN_CTX_start(ctx); //init context
group = EC_KEY_get0_group(*key); //G
pub = EC_POINT_new(group); //create pub st
EC_POINT_mul(group, pub, priv, NULL, NULL, ctx); //P = n * G
EC_KEY_set_public_key(*key, pub); //fill the key pair, !!!crashed here
EC_KEY_set_conv_form(*key, getcompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED); //compressd or not
uint64_t pubkeylen = i2o_ECPublicKey(*key, NULL);
uint8_t *pubkey = calloc(pubkeylen, sizeof(uint8_t));
uint8_t* pub_copy = pubkey;
if (i2o_ECPublicKey(*key, &pub_copy) != pubkeylen) {
puts("Unable to decode public key");
return false;
/*for (int i = 0; i < *pubkeylen; i++) {
printf("%02X", (*pubkey)[i]);
EC_POINT_free(pub);//free all
BN_CTX_end(ctx);
BN_CTX_free(ctx);
BN_clear_free(priv);
return true;
Just a proof of work,No checks was added.
And I keet rerun the program many times,saw that the program will crash at different code.
Below are some pictures captured when the program crashed.
crashed at EC_KEY_new_by_curve_name
crashed at EC_KEY_set_public_key
crashed on main exit
linked librares
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.