如何从OpenSSL中的ECDSA私钥获取公钥? [英] How do I obtain the public key from an ECDSA private key in OpenSSL?

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

问题描述

我提供此示例应用程序以显示我的问题

I am providing this sample application to show my problem

#include <stdio.h>
#include <stdlib.h>
#include <openssl/ec.h>
#include <openssl/bn.h>

int main()
{
     EC_KEY *pkey = NULL;
     EC_POINT *pub_key = NULL;
     const EC_GROUP *group = NULL;
     BIGNUM start;
     BIGNUM *res;
     BN_CTX *ctx;

     BN_init(&start);
     ctx = BN_CTX_new();

     res = &start;
     BN_hex2bn(&res,"3D79F601620A6D05DB7FED883AB8BCD08A9101B166BC60166869DA5FC08D936E");
     pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
     group = EC_KEY_get0_group(pkey);
     pub_key = EC_POINT_new(group);

     EC_KEY_set_private_key(pkey, res);

     assert(EC_POINT_bn2point(group,res, pub_key, ctx)); // Null here

     EC_KEY_set_public_key(pkey, pub_key);


    return 0;
}

我想要做的是显示私钥键(应为椭圆私钥)。
我不知道如何做,直到我遇到类似的问题

What I am trying to do, is to display the Public key from a private key(should an elliptic private key). I did not know how to do it until I encountered a similar problem

How do I feed OpenSSL random data for use in ECDSA signing?

这是从哪里指向我如何获取公共密钥,并使用EC_POINT_bn2point而不是hex2point,其内部BN_hex2bn根据OpenSSL源。

Which is from where I pointed myself how to get the public key and to use EC_POINT_bn2point instead of hex2point which internally does BN_hex2bn according to the OpenSSL source.

那么,为什么EC_POINT_bn2point返回NULL?我正在认真考虑重新编译OpenSSL,并让一些调试例程来弄清楚它为什么会失败。

So, why is EC_POINT_bn2point returning NULL? I am seriously considering recompiling OpenSSL and putting some debug routines to figure out why it fails.

推荐答案

ECDSA私钥通过 Q = dG 计算d (整数)和公钥 -secret域参数。 FIB 186-3的Suite B实施者指南
(ECDSA)
详细描述了ECDSA。

An ECDSA private key d (an integer) and public key Q (a point) is computed by Q = dG, where G is a non-secret domain parameter. Suite B Implementer’s Guide to FIPS 186-3 (ECDSA) describes ECDSA in detail.

OpenSSL使用 ECDSA_generate_key 来生成密钥对。它是随机生成一个私钥,然后它进行 Q = dG 乘法计算公钥:

OpenSSL uses ECDSA_generate_key to generate a key pair. What it does is generate a private key randomly, and then it does the Q = dG multiplication to compute the public key:

/* pub_key is a new uninitialized `EC_POINT*`.  priv_key is a `BIGNUM*`. */
if (!EC_POINT_mul(ecdsa->group, pub_key, priv_key, NULL, NULL, ctx)) goto err;

所以你可以做同样的事情。如果我有私钥,我将其设置为 EC_KEY ECDSA 结构中的私钥。然后我将配置域参数。最后,我会通过 EC_POINT_mul 获得公共密钥点。

So you can do the same thing. If I had the private key, I'd set it as the private key in an EC_KEY or ECDSA struct. Then I'd configure the domain parameters on it. And finally I'd do the EC_POINT_mul to get the public key point.

这篇关于如何从OpenSSL中的ECDSA私钥获取公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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