如何将 ECC 私钥导入 OpenSSL 的 EC_KEY? [英] How do I import an ECC private key into OpenSSL's EC_KEY?

查看:163
本文介绍了如何将 ECC 私钥导入 OpenSSL 的 EC_KEY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制 EOS 的签名过程,该过程使用 OpenSSL 的 ECDSA_sign 方法.

I'm trying to replicate EOS's signing procedure, which uses OpenSSL's ECDSA_sign method.

EOS 使用 secp256k1 曲线进行签名.

EOS uses secp256k1 curve for signing.

ECDSA_sign 的参数之一是 EC_KEY.我假设 EC_KEY 结构包含曲线类型和私钥 - 但是如何创建 EC_KEY 结构以使其包含私钥?

One of the arguments to ECDSA_sign is EC_KEY. I assume that the EC_KEY structure contains the type of curve and the private key - but how does one create the EC_KEY structure so that it contains the private key?

就像比特币一样,加密密钥在 WIF 中编码,我已成功将其转换为二进制(或十六进制,如果需要).不过,我不知道该去哪里.

Just like bitcoin, the crypto key is encoded in WIF, which I've successfully converted into binary (or hex, if need be). I'm at a loss of where to go from here though.

我是否转换为 bignum,并使用 EC_KEY 导入该私钥?还有什么我想做的吗?

Do I convert to bignum, and have an EC_KEY import that private key? Is there anything else I'm meant to do?

推荐答案

OpenSSL 不支持 WIF 格式.如果您可以将解码后的原始私钥放入 BIGNUM 结构中(称为 priv_key),那么您应该可以执行以下操作:

OpenSSL does not support the WIF format. If you can get the decoded raw private key into a BIGNUM structure (call it priv_key) then you should be able to do something like this:

EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
EC_KEY *key = EC_KEY_new();
EC_POINT *pub_key = EC_POINT_new(group);

EC_KEY_set_group(key, group);
EC_KEY_set_private_key(key, priv_key);
EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL);
EC_KEY_set_public_key(key, pub_key);

(注意:以上内容完全未经测试,为了清楚起见,我省略了您应该添加的所有错误检查代码).

(Note: the above is completely untested, and for clarity I've omitted all error checking code, which you should add).

这篇关于如何将 ECC 私钥导入 OpenSSL 的 EC_KEY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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