Botan 3.6.1
Crypto and TLS for C&
ec_key_data.cpp
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/ec_key_data.h>
8
9#include <botan/rng.h>
10
11namespace Botan {
12
13EC_PublicKey_Data::EC_PublicKey_Data(EC_Group group, std::span<const uint8_t> bytes) :
14 m_group(std::move(group)), m_point(m_group, bytes), m_legacy_point(m_point.to_legacy_point()) {}
15
17 m_group(std::move(group)), m_scalar(EC_Scalar::random(m_group, rng)), m_legacy_x(m_scalar.to_bigint()) {}
18
20 m_group(std::move(group)), m_scalar(EC_Scalar::from_bigint(m_group, x)), m_legacy_x(m_scalar.to_bigint()) {}
21
23 m_group(std::move(group)), m_scalar(std::move(x)), m_legacy_x(m_scalar.to_bigint()) {}
24
25EC_PrivateKey_Data::EC_PrivateKey_Data(EC_Group group, std::span<const uint8_t> bytes) :
26 m_group(std::move(group)), m_scalar(m_group, bytes), m_legacy_x(m_scalar.to_bigint()) {}
27
28std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(RandomNumberGenerator& rng,
29 bool with_modular_inverse) const {
30 auto public_point = [&] {
31 std::vector<BigInt> ws;
32 if(with_modular_inverse) {
33 return EC_AffinePoint::g_mul(m_scalar.invert(), rng, ws);
34 } else {
35 return EC_AffinePoint::g_mul(m_scalar, rng, ws);
36 }
37 };
38
39 return std::make_shared<EC_PublicKey_Data>(m_group, public_point());
40}
41
42std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(bool with_modular_inverse) const {
43 Null_RNG null_rng;
44 return this->public_key(null_rng, with_modular_inverse);
45}
46
47void EC_PrivateKey_Data::serialize_to(std::span<uint8_t> output) const {
48 m_scalar.serialize_to(output);
49}
50
51} // namespace Botan
static EC_AffinePoint g_mul(const EC_Scalar &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws)
void serialize_to(std::span< uint8_t > output) const
EC_PrivateKey_Data(EC_Group group, RandomNumberGenerator &rng)
std::shared_ptr< EC_PublicKey_Data > public_key(RandomNumberGenerator &rng, bool with_modular_inverse) const
EC_PublicKey_Data(EC_Group group, EC_AffinePoint pt)
Definition ec_key_data.h:23
void serialize_to(std::span< uint8_t > bytes) const
Definition ec_scalar.cpp:84
EC_Scalar invert() const