可以使用openssl库中的函数将SM2公钥转换为EC_POINT类型。具体步骤如下:

  1. 加载SM2椭圆曲线参数
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_sm2);
  1. 创建EC_POINT对象
EC_POINT *point = EC_POINT_new(group);
  1. 将公钥字节流转换为BIGNUM类型
BIGNUM *x = BN_bin2bn(pubkey_x, pubkey_len/2, NULL);
BIGNUM *y = BN_bin2bn(pubkey_y, pubkey_len/2, NULL);
  1. 将BIGNUM类型的x和y赋值给EC_POINT对象
EC_POINT_set_affine_coordinates_GFp(group, point, x, y, NULL);
  1. 释放内存
BN_free(x);
BN_free(y);

完整的代码示例如下:

#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <openssl/bn.h>

EC_POINT* sm2_pubkey_to_ecpoint(unsigned char *pubkey_x, unsigned char *pubkey_y, size_t pubkey_len)
{
    EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_sm2);
    EC_POINT *point = EC_POINT_new(group);
    BIGNUM *x = BN_bin2bn(pubkey_x, pubkey_len/2, NULL);
    BIGNUM *y = BN_bin2bn(pubkey_y, pubkey_len/2, NULL);
    EC_POINT_set_affine_coordinates_GFp(group, point, x, y, NULL);
    BN_free(x);
    BN_free(y);
    EC_GROUP_free(group);
    return point;
}
openssl C语言 sm2 公钥转为EC_POINT

原文地址: https://www.cveoy.top/t/topic/bSAe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录