6

This is my code:

EC_KEY *eckey = EC_KEY_new();
EC_KEY_generate_key(eckey);
const EC_POINT *pub = EC_KEY_get0_public_key(eckey);
printf("%s", pub->X);

I'm getting an error that says "Incomplete definition of type 'struct ec_point_st'". I also tried:

EC_GROUP *curve = EC_GROUP_new_by_curve_name(NID_secp521r1);
BN_CTX *ecctx= BN_CTX_new();
EC_KEY *eckey = EC_KEY_new();
EC_KEY_generate_key(eckey);
const EC_POINT *pub = EC_KEY_get0_public_key(eckey);
NSLog(@"%s", EC_POINT_point2hex(curve, pub, POINT_CONVERSION_HYBRID, ecctx));

in which case I'm getting an EXC_BAD_ACCESS error. How can I print (for debugging) the x and y points of the public key?

1 Answer 1

8

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.