使用OpenSSL对类型'struct ec_key_st'的错误定义不完整 [英] Error incomplete definition of type 'struct ec_key_st' using OpenSSL

查看:1279
本文介绍了使用OpenSSL对类型'struct ec_key_st'的错误定义不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过openssl从给定的秘密中计算公钥.我收到此错误:

I am trying to compute public key from given secret by openssl. I get this error:

main.c:27: error: incomplete definition of type 'struct ec_key_st'
  printf("d: %s\n", BN_bn2hex(eckey->priv_key));
                              ~~~~~^

这是我的代码:

#include <stdio.h>

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

int main()
{
  BN_CTX *ctx = BN_CTX_new();

  EC_KEY *eckey = EC_KEY_new();
  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
  EC_KEY_set_group(eckey, group);

  BIGNUM *prv = BN_new();
  BN_hex2bn(&prv, "b14fac12b3fa7dd6f2562a18d554fcd6818137ebb7e0d119ab0776d6407664f9");
  EC_KEY_set_private_key(eckey, prv);

  EC_POINT *Q = EC_POINT_new(group);
  EC_POINT_mul(group, Q, prv, NULL, NULL, ctx);
  EC_KEY_set_public_key(eckey, Q);

  if (EC_KEY_check_key(eckey))
    printf("Key succesfully checked.\n");

  printf("d: %s\n", BN_bn2hex(eckey->priv_key));
  printf("X: %s\n", BN_bn2hex(&eckey->pub_key->X));
  printf("Y: %s\n", BN_bn2hex(&eckey->pub_key->Y));

  EC_GROUP_free (group); group = NULL;
  EC_KEY_free (eckey); eckey = NULL;
  return 0;
}

上面的代码有什么问题?如果我删除printf行,它工作正常.如果有人帮助我摆脱此错误,我将不胜感激.

What is wrong with the above code? If I remove printf lines, it works fine. I would appreciate if anybody helps me getting rid of this error.

推荐答案

您正在使用OpenSSL 1.1,他们已经决定您不应该再去研究它们的结构内幕了.

You're using OpenSSL 1.1, and they have decided that you shouldn't be poking about at the innards of their structures anymore.

eckey->priv_key是访问OpenSSL 1.0.x中私钥的有效方法,但是现在唯一正确的方法是EC_KEY_get0_private_key(eckey)

eckey->priv_key was a valid way of accessing the private key in OpenSSL 1.0.x, but now the only correct way is EC_KEY_get0_private_key(eckey)

同样,对于公钥,它是EC_KEY_get0_public_key(eckey).

Likewise, for the public key it's EC_KEY_get0_public_key(eckey).

这两个函数都是在OpenSSL 1.0.x期间声明的,因此您可以编写相同的代码.

Both of these functions were declared during OpenSSL 1.0.x, so you can write the code to be the same between them.

所以

printf("d: %s\n", BN_bn2hex(eckey->priv_key));
printf("X: %s\n", BN_bn2hex(&eckey->pub_key->X));
printf("Y: %s\n", BN_bn2hex(&eckey->pub_key->Y));

将成为

{
    const BIGNUM* d = EC_KEY_get0_private_key(eckey);
    const EC_POINT* Q = EC_KEY_get0_public_key(eckey);
    const EC_GROUP* group = EC_KEY_get0_group(eckey);
    BIGNUM* x = BN_new();
    BIGNUM* y = BN_new();

    if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, null))
    {
        error();
    }

    printf("d: %s\n", BN_bn2hex(d));
    printf("X: %s\n", BN_bn2hex(x));
    printf("Y: %s\n", BN_bn2hex(y));

    BN_free(x);
    BN_free(y);
}

即使OpenSSL 1.1.1决定重做ec_lcl.h中隐藏的结构布局,这也可以使您的代码正常工作

This keeps your code working, even when OpenSSL 1.1.1 decides to redo the struct layout which was hidden within ec_lcl.h

这篇关于使用OpenSSL对类型'struct ec_key_st'的错误定义不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆