如何一次访问原始ECDH公共密钥,OpenSSL的EVP_PKEY结构内部的私有密钥,而params? [英] How does one access the raw ECDH public key, private key and params inside OpenSSL's EVP_PKEY structure?

查看:3512
本文介绍了如何一次访问原始ECDH公共密钥,OpenSSL的EVP_PKEY结构内部的私有密钥,而params?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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:


  1. 怎么会 get_peerkey()实际提取 p键发送到对等只是公钥?

  2. 将如何code提取 p键私钥,而params将它们存储供以后使用密钥交换后?

  3. 怎么会 get_peerkey()生成从对等的原始公钥一个新的 EVP_PKEY 结构?

  1. How would get_peerkey() actually extract just the public key from pkey for sending to the peer?
  2. How would the code extract the private key and params from pKey to store them for later use after the key exchange?
  3. How would get_peerkey() generate a new EVP_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.

要序列化的公共密钥:


  1. 传递EVP_PKEY到EVP_PKEY_get1_EC_KEY()得到一个EC_KEY。

  2. 传递EC_KEY到EC_KEY_get0_public_key()得到一个EC_POINT。

  3. 传递EC_POINT到EC_POINT_point2oct()来获取八位组,这仅仅是无符号字符*。

要反序列化的公共密钥:

To deserialize the public key:


  1. 传递八位字节EC_POINT_oct2point()得到一个EC_POINT。

  2. 传递EC_POINT到EC_KEY_set_public_key()得到一个EC_KEY。

  3. 传递EC_KEY到EVP_PKEY_set1_EC_KEY得到一个EVP_KEY。

要序列化私有密钥:


  1. 传递EVP_PKEY到EVP_PKEY_get1_EC_KEY()得到一个EC_KEY。

  2. 传递EC_KEY到EC_KEY_get0_private_key()得到一个BIGNUM。

  3. 传递到BIGNUM BN_bn2mpi()得到一个MPI,这是写入格式
    无符号字符*。

要反序列化的私有密钥:

To deserialize the private key:


  1. 通过MPI到BN_mpi2bn()得到一个BIGNUM。

  2. 传递到BIGNUM EC_KEY_set_private_key()得到一个EC_KEY。

  3. 传递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屋!

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