如何一次访问原始ECDH公共密钥,OpenSSL的EVP_PKEY结构内部的私有密钥,而params? [英] How does one access the raw ECDH public key, private key and params inside OpenSSL's EVP_PKEY structure?
问题描述
我使用OpenSSL的C库来生成一个椭圆曲线的Diffie-Hellman(ECDH)密钥对,第一个code样品的<a href =以下http://wiki.openssl.org/index.php / Elliptic_Curve_Diffie_Hellman>此处。它掩盖了公共密钥与该行的实际汇率:
I'm using OpenSSL's c library to generate an elliptic curve Diffie-Hellman (ECDH) key pair, following the first code sample here. It glosses over the actual exchange of public keys with this line:
peerkey = get_peerkey(pkey);
的 p键
变量和返回值的类型都是 EVP *
。 p键
包含公钥,私钥,而params之前生成,和返回值只包含对方的公用密钥。所以这样就引发了三个问题:
The pkey
variable and the return value are both of type EVP *
. pkey
contains the public key, private key, and params generated earlier, and the return value only contains the peer's public key. So this raises three questions:
- 怎么会
get_peerkey()
实际提取p键
发送到对等只是公钥? - 将如何code提取
p键
私钥,而params将它们存储供以后使用密钥交换后? - 怎么会
get_peerkey()
生成从对等的原始公钥一个新的EVP_PKEY
结构?
- How would
get_peerkey()
actually extract just the public key frompkey
for sending to the peer? - How would the code extract the private key and params from
pKey
to store them for later use after the key exchange? - How would
get_peerkey()
generate a newEVP_PKEY
structure from the peer's raw public key?
我见过的OpenSSL的功能 EVP_PKEY_print_public()
, EVP_PKEY_print_private()
和 EVP_PKEY_print_params()
,但这些都是用于生成可读的输出。而且我还没有发现任何同等用于将人类可读的公钥回一个 EVP_PKEY
结构。
I've seen the OpenSSL functions EVP_PKEY_print_public()
, EVP_PKEY_print_private()
, and EVP_PKEY_print_params()
but these are for generating human-readable output. And I haven't found any equivalent for converting a human-readable public key back into an EVP_PKEY
structure.
推荐答案
要回答我的问题,有私钥和公钥不同的路径。
To answer my own question, there's a different path for the private key and the public key.
要序列化的公共密钥:
- 传递EVP_PKEY到EVP_PKEY_get1_EC_KEY()得到一个EC_KEY。
- 传递EC_KEY到EC_KEY_get0_public_key()得到一个EC_POINT。
- 传递EC_POINT到EC_POINT_point2oct()来获取八位组,这仅仅是无符号字符*。
要反序列化的公共密钥:
To deserialize the public key:
- 传递八位字节EC_POINT_oct2point()得到一个EC_POINT。
- 传递EC_POINT到EC_KEY_set_public_key()得到一个EC_KEY。
- 传递EC_KEY到EVP_PKEY_set1_EC_KEY得到一个EVP_KEY。
要序列化私有密钥:
- 传递EVP_PKEY到EVP_PKEY_get1_EC_KEY()得到一个EC_KEY。
- 传递EC_KEY到EC_KEY_get0_private_key()得到一个BIGNUM。
- 传递到BIGNUM BN_bn2mpi()得到一个MPI,这是写入格式
无符号字符*。
要反序列化的私有密钥:
To deserialize the private key:
- 通过MPI到BN_mpi2bn()得到一个BIGNUM。
- 传递到BIGNUM EC_KEY_set_private_key()得到一个EC_KEY。
- 传递EC_KEY到EVP_PKEY_set1_EC_KEY得到一个EVP_KEY。
也可以到BIGNUM转换为十六进制,十进制或本,虽然我认为MPI使用的字节最少的。
It is also possible to convert the BIGNUM to hex, decimal, or "bin", although I think that mpi used the fewest bytes.
这篇关于如何一次访问原始ECDH公共密钥,OpenSSL的EVP_PKEY结构内部的私有密钥,而params?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!