2

[Generated public key following (https://jameshfisher.com/2017/04/14/openssl-ecc/)

Alice generates her private key:

openssl ecparam -name secp256k1 -genkey -noout -out alice_priv_key.pem

Alice extracts her public key from her private key:

openssl ec -in alice_priv_key.pem -pubout -out alice_pub_key.pem

I have a application in C when I need to send encrypted data using public key. I am trying to load the data from public (alice_pub_key.pem) file.

I converted PEM file to hex data from (https://holtstrom.com/michael/tools/hextopem.php).

So seems I am not providing data in proper format. Can anyone suggest whats going wrong?

key = EC_KEY_new_by_curve_name(NID_secp256k1)
group = EC_KEY_get0_group(key)
pub_key = EC_POINT_new(group)
EC_POINT * point = EC_POINT_hex2point(group, ptr, pub_key, NULL)

This point its returning null.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • 1
    Why are you using hexadecimals as intermediate format in the first place? Why not just load the PEM? – Maarten Bodewes May 30 '21 at 12:53
  • @MaartenBodewes Thanks for quick resonse `-----BEGIN PUBLIC KEY----- MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEjWrT7F97QrSqGrlIgPK8dphNBicNO6gD LfOIMjhF2MiLuuzd7L7BP+bLCuNtKKe/2dOkgPqgXv4BFWqgp6PZXQ== -----END PUBLIC KEY-----` This is public key generated. When I passed this as buffer it failed. Then I checked some document where its mentioned that data need to be passed as hex. So I am not able to find how I can pass peer public key to get X and Y cordinate or load shared public key to ECC private key I generate, – Baboo Patel May 30 '21 at 13:57
  • What you have is a SubjectPublicKeyInfo structure in PEM format containing an EC public key. Now that's all fine and dandy, but I cannot seem to find myself how to parse that structure e.g. using the EVP BIO functions. It should be possible, but dang if I can find it. Note that your code above is not complete, posting a more complete example should help, because currently we don't know what `ptr` contains. – Maarten Bodewes May 30 '21 at 15:10
  • Uh, could you try [`PEM_read_bio_EC_PUBKEY`](https://www.openssl.org/docs/man1.1.0/man3/PEM_read_EC_PUBKEY.html)? I'm not sure that it supports `PUBLIC KEY` instead of `EC PUBLIC KEY` (because the OpenSSL documentation sucks) but that can be fixed. – Maarten Bodewes May 30 '21 at 15:22
  • @MaartenBodewes `ptr is pem2Hex value char *ptr = "3056301006072A8648CE3D020106052B8104000A034200048D6AD3EC5F7B42B4AA1AB94880F2BC76984D06270D3BA8032DF388323845D8C88BBAECDDECBEC13FE6CB0AE36D28A7BFD9D3A480FAA05EFE01156AA0A7A3D95D"; EC_KEY *key = NULL; EC_POINT *pub_key; const EC_GROUP *group; key = EC_KEY_new_by_curve_name(NID_secp256k1); group = EC_KEY_get0_group(key); pub_key = EC_POINT_new(group); EC_POINT * point = EC_POINT_hex2point(group, ptr, pub_key, NULL);EC_KEY_set_public_key(key, pub_key); if (!EC_KEY_check_key(key)) { printf(" failed") else printf("OK") – Baboo Patel May 30 '21 at 15:25
  • @MaartenBodewes Due to space constraint I could not update full. – Baboo Patel May 30 '21 at 15:26
  • Your point in `ptr` contains the full SubjectPublicKeyInfo structure instead of just the point. The point is just the value of the BITSTRING at the end, see [here](https://lapo.it/asn1js/#MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEjWrT7F97QrSqGrlIgPK8dphNBicNO6gDLfOIMjhF2MiLuuzd7L7BP-bLCuNtKKe_2dOkgPqgXv4BFWqgp6PZXQ). But trying to use the function I put down above would be the best option instead of copying out the data. – Maarten Bodewes May 30 '21 at 15:32
  • @MaartenBodewes Thanks I am able to load PEM format file with PEM_read_bio_EC_PUBKEY. I am receiving one public key which does not have header/footer. How can I load that public key? Its only base64 data. I have poster another request https://stackoverflow.com/questions/67832949/parse-ec-public-key. If you can guide on that it will be of great help. – Baboo Patel Jun 04 '21 at 08:56

1 Answers1

1

You can try PEM_read_EC_PUBKEY to read the public key. This avoids having to parse or re-encode the public key itself.

Note that you may need a recent OpenSSL for this; it is hard to find out from the documentation when this function was introduced.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263