Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ECDH_PrivateKey from raw bits #4224

Open
ClausDampsoft opened this issue Jul 17, 2024 · 5 comments
Open

Create ECDH_PrivateKey from raw bits #4224

ClausDampsoft opened this issue Jul 17, 2024 · 5 comments

Comments

@ClausDampsoft
Copy link

Hello,
I'm new to Botan, having some difficulties.

For test purposes I need to create an ECDH_PrivateKey on P-256 (secp256r1) from raw bits (32 bytes in std::vector<uin8_t>). I tried the ECDH_PrivateKey-Konstruktor with the AlgorithmIdentifier but I always get some exceptions.

Nearly the same question is for an ECDH_PublicKey with given x and y as raw bits.

Thanks for any hints
Claus

@reneme
Copy link
Collaborator

reneme commented Jul 17, 2024

Hi! Typically, Botan does not expect the private key as a raw 256bit vector but wrapped as a PEM container. But there's a hack that might get you there:

#include <botan/bigint.h>
#include <botan/ecdh.h>
#include <botan/hex.h>
#include <botan/rng.h>

int main() {
   const auto private_key_bytes = Botan::hex_decode("D2AC61C35CAEE918E47B0BD5E61DA9B3A5C2964AB317647DEF6DFC042A06C829");

   Botan::Null_RNG null_rng;
   const auto private_key = 
      Botan::ECDH_PrivateKey(null_rng,
                             Botan::EC_Group::from_name("secp256r1"),
                             Botan::BigInt(private_key_bytes));
}

@randombit
Copy link
Owner

^ Not really a hack, that's how it's supposed to work!

For public key from (x,y) similarly construct an EC_Point

@reneme
Copy link
Collaborator

reneme commented Jul 17, 2024

Here's some snippet for the public key:

#include <botan/bigint.h>
#include <botan/ecdh.h>
#include <botan/hex.h>
#include <botan/rng.h>

#include <vector>

int main() {
   const auto pk_x = Botan::hex_decode("278309D4A88ADF89CA0E5328D3B655CF8949F2D9F9B2308AA22FE28202A315EC");
   const auto pk_y = Botan::hex_decode("AC457F18D1F3675D46E98ED2E509EE47AC2CB9A012F73263B30CD7248AEA6020");

   std::vector<uint8_t> public_key_bytes;
   public_key_bytes.reserve(pk_x.size() + pk_y.size() + 1);
   public_key_bytes.push_back(0x04);  // means: uncompressed point encoding
   public_key_bytes.insert(public_key_bytes.end(), pk_x.begin(), pk_x.end());
   public_key_bytes.insert(public_key_bytes.end(), pk_y.begin(), pk_y.end());

   const auto domain = Botan::EC_Group::from_name("secp256r1");
   const auto public_key = Botan::ECDH_PublicKey(domain, domain.OS2ECP(public_key_bytes));
}

@randombit domain.point(BigInt(x), BigInt(y)) would have been nice, but that's deprecated. I didn't see another way than to restore the 65-byte encoding above and then call domain.OS2ECP(). Did I miss something?

@ClausDampsoft
Copy link
Author

Thanks to you!
Private key seems to work. Happy, happy no exception!
I detected myself this thing with the 0x04 for the public key. But it looks wrong (= bad style) to me.

Claus

@randombit
Copy link
Owner

This is an area in need of some cleanup. Right now we've deprecated the stuff we want to remove, but without necessarily introducing (much less documenting...) full replacements. After #4203 lands I'll work on updating the various constructors, etc

@ClausDampsoft ClausDampsoft reopened this Jul 18, 2024
reneme added a commit to Rohde-Schwarz/botan that referenced this issue Jul 18, 2024
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
reneme added a commit to Rohde-Schwarz/botan that referenced this issue Jul 18, 2024
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
reneme added a commit to Rohde-Schwarz/botan that referenced this issue Jul 18, 2024
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants