-
Notifications
You must be signed in to change notification settings - Fork 574
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
Comments
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));
} |
^ Not really a hack, that's how it's supposed to work! For public key from (x,y) similarly construct an |
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 |
Thanks to you! Claus |
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 |
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
This does come up as usage questions occasionally (randombit#3137, randombit#4224)
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
The text was updated successfully, but these errors were encountered: