You have to associate an EC_GROUP
object to the EC_KEY
before calling EC_KEY_generate_key
:
EC_KEY *ec_key = EC_KEY_new();
EC_GROUP *ec_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
EC_KEY_set_group(ec_key, ec_group);
EC_KEY_generate_key(ec_key);
then print the public key:
const EC_POINT *pub = EC_KEY_get0_public_key(ec_key);
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
if (EC_POINT_get_affine_coordinates_GFp(ec_group, pub, x, y, NULL)) {
BN_print_fp(stdout, x);
putc('\n', stdout);
BN_print_fp(stdout, y);
putc('\n', stdout);
}
Don't forget to add error and memory handling, the sample code above leaks.