How does one access the raw ECDH public key, private key and params inside OpenSSL's EVP_PKEY structure?

五迷三道 提交于 2019-12-02 20:42:17
Bob Whiteman

To answer my own question, there's a different path for the private key and the public key.

To serialize the public key:

  1. Pass the EVP_PKEY to EVP_PKEY_get1_EC_KEY() to get an EC_KEY.
  2. Pass the EC_KEY to EC_KEY_get0_public_key() to get an EC_POINT.
  3. Pass the EC_POINT to EC_POINT_point2oct() to get octets, which are just unsigned char *.

To deserialize the public key:

  1. Pass the octets to EC_POINT_oct2point() to get an EC_POINT.
  2. Pass the EC_POINT to EC_KEY_set_public_key() to get an EC_KEY.
  3. Pass the EC_KEY to EVP_PKEY_set1_EC_KEY to get an EVP_KEY.

To serialize the private key:

  1. Pass the EVP_PKEY to EVP_PKEY_get1_EC_KEY() to get an EC_KEY.
  2. Pass the EC_KEY to EC_KEY_get0_private_key() to get a BIGNUM.
  3. Pass the BIGNUM to BN_bn2mpi() to get an mpi, which is a format written to unsigned char *.

To deserialize the private key:

  1. Pass the mpi to BN_mpi2bn() to get a BIGNUM.
  2. Pass the BIGNUM to EC_KEY_set_private_key() to get an EC_KEY.
  3. Pass the EC_KEY to EVP_PKEY_set1_EC_KEY to get an EVP_KEY.

It is also possible to convert the BIGNUM to hex, decimal, or "bin", although I think that mpi used the fewest bytes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!