c - sgx_tcrypto 和 OpenSSL libcrypt 之间明显不兼容

标签 c openssl cryptography elliptic-curve sgx

我正在尝试将从 SGX enclave 获取的公钥加载到 OpenSSL 椭圆曲线公钥对象中。

SGX SDK 内置的加密库使用 SECP256R1 上的点作为公钥,并将它们表示为 (x,y) 对。所以我尝试执行以下操作:

1) 从SGX对象中提取仿射坐标(x,y)。

2) 在 SECP256R1 上创建 OpenSSL 公钥对象。

3) 将其设置为 (x,y)。

但是,最后一次调用失败并显示错误消息:“错误:1007C06B:椭圆曲线例程:EC_POINT_set_affine_coordinates_GFp:点不在曲线上”。这哪里出了问题?这可能是字节序问题吗?

#include <stdio.h>

#include <sgx_tcrypto.h>

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

const sgx_ec256_public_t sgx_pk;
const sgx_ec256_private_t sgx_sk;

int main()
{
    //init openssl objects
    EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); //assuming this the same as secp256r1
    BN_CTX *bn_ctx = BN_CTX_new();

    //extract affine coordinates from sgx object
    BIGNUM *x = BN_bin2bn((uint8_t*)&sgx_pk.gx, sizeof(sgx_pk) / 2, NULL);
    BIGNUM *y = BN_bin2bn((uint8_t*)&sgx_pk.gy, sizeof(sgx_pk) / 2, NULL);

    //create openssl key and load the coordinates into it
    EC_KEY *ec_key = EC_KEY_new();
    EC_KEY_set_group(ec_key, group);
    EC_KEY_set_public_key_affine_coordinates(ec_key, x, y);

    //last call fails. extract error
    long error_code = ERR_get_error();
    char error_string[300];
    ERR_error_string_n(error_code, error_string, sizeof(error_string));
    puts(error_string);

    return 0;
}

作为引用,此处定义了示例 key 对:

const sgx_ec256_private_t sgx_sk =
{
    0xc1, 0xe7, 0x59, 0x90, 0x4e, 0x80, 0xa5, 0x52,
    0x45, 0x25, 0xec, 0x2a, 0xc, 0x98, 0x89, 0x6e,
    0x63, 0x96, 0x4d, 0x5d, 0x58, 0x48, 0x86, 0xf4,
    0x9b, 0x70, 0xad, 0xb5, 0xa2, 0x56, 0xe9, 0x13
};

const sgx_ec256_public_t sgx_pk =
{
    0x82, 0xcb, 0x6f, 0x41, 0x3a, 0xd4, 0xfa, 0x57,
    0x6c, 0xc4, 0x1b, 0x77, 0xf6, 0xd9, 0x51, 0xc1,
    0xbc, 0x17, 0x7a, 0x88, 0xd0, 0x2e, 0x94, 0xd6,
    0x91, 0xa3, 0x1d, 0x75, 0xc, 0xbf, 0xa9, 0xca,
    0x8, 0x6c, 0xf3, 0x78, 0x92, 0xdb, 0x2f, 0x52,
    0x0, 0x44, 0x20, 0xd6, 0xa, 0xd3, 0x58, 0x3,
    0xb2, 0x35, 0xda, 0xe2, 0x1b, 0xdb, 0x2b, 0xd2,
    0xb0, 0xaf, 0x5e, 0x29, 0xc8, 0xb4, 0x93, 0x41
};

最佳答案

这确实是一个字节顺序问题。以下 C++ 代码有效:

#include <stdio.h>
#include <algorithm>

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

#include <sgx_tcrypto.h>

extern const sgx_ec256_public_t sgx_pk;
extern const sgx_ec256_private_t sgx_sk;

int main()
{
  //init openssl objects
  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); 
  //assuming this the same as secp256r1
  BN_CTX *bn_ctx = BN_CTX_new();

  //extract affine coordinates from sgx object
  sgx_ec256_public_t sgx_pk_reversed;
  constexpr size_t COORDINATE_SIZE = sizeof(sgx_ec256_public_t) / 2;
  std::reverse_copy(sgx_pk.gx, sgx_pk.gx + COORDINATE_SIZE, sgx_pk_reversed.gx);
  std::reverse_copy(sgx_pk.gy, sgx_pk.gy + COORDINATE_SIZE, sgx_pk_reversed.gy);

  BIGNUM *x = BN_bin2bn((uint8_t*)&sgx_pk_reversed.gx, COORDINATE_SIZE, NULL);
  BIGNUM *y = BN_bin2bn((uint8_t*)&sgx_pk_reversed.gy, COORDINATE_SIZE, NULL);

  //create openssl key and load the coordinates into it
  EC_KEY *ec_key = EC_KEY_new();
  EC_KEY_set_group(ec_key, group);

  if (1 == EC_KEY_set_public_key_affine_coordinates(ec_key, x, y))
    puts("Holy shit it worked.");

  return 0;
}

关于c - sgx_tcrypto 和 OpenSSL libcrypt 之间明显不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681399/

相关文章:

c - 使用 openssl 的 MD4 哈希,将结果保存到 char 数组中

c++ - 使用 openssl 库的编译问题

cryptography - PKCS #11 C_FindObjects 中的内存所有权,其中 ulMaxObjectCount != 1

php - 我可以从 HTTP 客户端获取 TLS secret 来解密我自己的 HTTPS 对话吗?

php - 确保持卡人数据的临时存储符合 PCI-DSS 要求?

java - cipher.update在java中做什么?

c - 当给定字母的字典顺序时,合并排序的时间复杂度是多少?

c - 在单个链表中存储多个数据项

c - 当可以使用 struct - C 完成相同的事情时使用 union 的优点

c - 为什么我的空字符数组以长度 6 开头?