ECDSA:如何使用openssl从解压缩x获得y坐标

我试图用ECDSA做的是从外部ECDSA硬件(它实际上是一个公钥)中取出一个x“压缩”坐标和一个“位”,然后尝试用OpenSSL函数调用恢复y坐标(未压缩) .

我正在尝试以下代码,但它不能像我预期的那样工作(我的xy []数组没有填充正确的数据).有人可以帮忙吗?

我是ECDSA的新手,甚至是OpenSSL的新手,但我想我可以做到以下几点:

enter code here


EC_GROUP *curve_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime192v1);
EC_POINT *point;
BIGNUM *x_compressed = BN_new();
int y_chooser_bit = 1;
int results = 0;
size_t returnsize = 0;

unsigned char x_compressed_byte_array[25] = {0x02, 0x71, 0xc0, 0x73, 0x73, 
                                             0x9b, 0xbf, 0xc2, 0x0a, 
                                             0x81, 0xcd, 0xdd, 0xf4, 
                                             0xcf, 0xca, 0xc7, 0xb5, 
                                             0xa9, 0x99, 0x61, 0x23, 
                                             0x2c, 0x5c, 0x63, 0x7a};

unsigned char xy[49];

// create a big number from the unsigned char array
BN_bin2bn(&x_compressed_byte_array[0],200,NULL); // length is in bits?

point = EC_POINT_new(curve_group);

results = EC_POINT_set_compressed_coordinates_GFp(curve_group, point, 
                                                  x_compressed,                                                               
                                                  y_chooser_bit, NULL);

returnsize = EC_POINT_point2oct(curve_group, point, 
                                POINT_CONVERSION_UNCOMPRESSED, 
                                &xy[0], 49, NULL); // 49

// clean up allocated memory
BN_free(x_compressed);
EC_POINT_free(point);
EC_GROUP_free(curve_group);

最佳答案 在这行代码中

BN_bin2bn(&x_compressed_byte_array[0],200,NULL); // length is in bits?

您应该传递bn_compressed(您创建的BIGNUM)来保存转换后的结果.长度以字节为单位.

BN_bin2bn(&x_compressed_byte_array[0],sizeof(x_compressed_byte_array),x_compressed);

在set_compressed_coordinates之后,您可以验证您刚创建的点是否在组中正确定位.

if (!EC_POINT_is_on_curve(curve_group,point,NULL)) return 0;

要进一步检查点坐标(x,y),可以将其放在代码中.

if (!EC_POINT_get_affine_coordinates_GFp(curve_group, point, x, y, NULL)) return 0;
fprintf(stdout, "\point = (x,y)\n");
fprintf(stdout, "     x = 0x");
BN_print_fp(stdout, x);
fprintf(stdout, "\n     y = 0x");
BN_print_fp(stdout, y);
fprintf(stdout, "\n");

只需记住功能的第一个/最后一个新的/免费的BIGNUM x和y.

BIGNUM *x, *y;
x = BN_new();
y = BN_new();

/* your code */

if(x) BN_free(x);
if(y) BN_free(y);

它应该工作.结果xy [49]我有

0x0471C073739BBFC20A81CDDDF4CFCAC7B7A99961232C5C637C388AB06AA7E43A2B5CFE2D7F3AC2DF910D6F8D8F209CD817

点赞