OpenSSL: print X and Y of EC_POINT

/*QUESTION*/

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?


/*ANSWER*/

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.

转载自:http://stackoverflow.com/questions/18496436/openssl-print-x-and-y-of-ec-point
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值