Skip to content

sm2_z256_point_to_compressed_octets是否存在问题? #1878

@weiy-zhang

Description

@weiy-zhang

代码运行出错,用agent搜索得到如下:在 sm2_z256_point_to_compressed_octets 函数中,第 1868 行代码错误地将 y 坐标 写入了 out + 1 位置:sm2_z256_to_bytes(y, out + 1); // 错误:写入了 y 坐标
而正确的实现应该是写入 x 坐标 :
sm2_z256_to_bytes(x, out + 1); // 正确:应写入 x 坐标

导致的问题

当您使用这个函数生成压缩格式公钥后,再调用 sm2_z256_point_from_octets 解析时:

  1. sm2_z256_point_from_octets 会根据类型标记(0x02 或 0x03)进入对应的处理分支
  2. 然后调用 sm2_z256_point_from_x_bytes 函数,将 in + 1 处的数据当作 x 坐标处理
  3. 由于 in + 1 处实际是 y 坐标,会导致:
    • x 坐标值可能超出曲线阶范围
    • 无法计算对应的 y 坐标(因为使用了错误的 x 值)
    • 最终导致解析失败,函数报错
      以上问题是真实存在的吗?还是我code有问题呢?求指教

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions