Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSSL: print X and Y of EC_POINT

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?

like image 829
Tech163 Avatar asked Aug 28 '13 19:08

Tech163


1 Answers

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.

like image 148
eik Avatar answered Sep 20 '22 00:09

eik