OpenSSL: print X and Y of EC_POINT

文章详细介绍了如何在OpenSSL中正确地打印EC点的x和y坐标,通过关联EC_GROUP对象到EC_KEY之前,并使用EC_POINT_get_affine_coordinates_GFp函数获取坐标。

/*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
int get_shared_secret(unsigned char* privkey, uint16_t privkey_len, unsigned char* pubkey, uint16_t pubkey_len, unsigned char* secret, uint16_t *olen) { EVP_PKEY_CTX* pctx = NULL; size_t secret_len; EC_KEY* mykey_ec = EC_KEY_new(); EC_KEY* peerkey_ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); EVP_PKEY* mykey = EVP_PKEY_new(); EVP_PKEY* peerkey = EVP_PKEY_new(); const EC_GROUP* group = NULL; EC_POINT* point = NULL; BIGNUM* bignum_key = NULL; if (NULL == mykey || NULL == peerkey || NULL == mykey_ec || NULL == peerkey_ec) { goto exit; } // 对端公钥处理 if (EVP_PKEY_set_type(peerkey, EVP_PKEY_EC) <= 0) { goto exit; } group = EC_KEY_get0_group(peerkey_ec); if (NULL == group) { goto exit; } point = EC_POINT_new(group); if (NULL == point) { goto exit; } if (EC_POINT_oct2point(group, point, pubkey, pubkey_len, NULL) != 1) { print_hex("peer pubkey error", pubkey, (int)pubkey_len); goto exit; } EC_KEY_set_public_key(peerkey_ec, point); EVP_PKEY_assign_EC_KEY(peerkey, peerkey_ec); peerkey_ec = NULL; // 自身私钥处理 if (EVP_PKEY_set_type(mykey, EVP_PKEY_EC) <= 0) { goto exit; } EC_KEY_set_group(mykey_ec, group); bignum_key = BN_new(); if (bignum_key == NULL) { goto exit; } if (!BN_bin2bn(privkey, privkey_len, bignum_key)) { goto exit; } if (EC_KEY_set_private_key(mykey_ec, bignum_key) != 1) { print_hex("my privkey error", privkey, (int)privkey_len); goto exit; } EVP_PKEY_assign_EC_KEY(mykey, mykey_ec); mykey_ec = NULL; pctx = EVP_PKEY_CTX_new(mykey, NULL); if (EVP_PKEY_derive_init(pctx) <= 0) { goto exit; } if (EVP_PKEY_derive_set_peer(pctx, peerkey) <= 0) { goto exit; } secret_len = 0; if (EVP_PKEY_derive(pctx, NULL, &secret_len) <= 0) { goto exit; } if (EVP_PKEY_derive(pctx, secret, &secret_len) <= 0) { goto exit; } *olen = secret_len; print_hex("shared secret:", secret, *olen); exit: if (mykey_ec) EC_KEY_free(mykey_ec); if (peerkey_ec) EC_KEY_free(peerkey_ec); if (mykey) EVP_PKEY_free(mykey); if (peerkey) EVP_PKEY_free(peerkey); if (pctx) EVP_PKEY_CTX_free(pctx); if (point) EC_POINT_free(point); if (bignum_key) BN_free(bignum_key); return 0; }这是原openssl的逻辑,上面是我一直到mbedtls的部分,这两个的功能一致吗,是我移植过程出问题了吗
最新发布
10-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值