-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dedicated types for containing EC key information
This is analagous to the DL scheme key types added in #3210, but here we have to retain the existing classes as we are constrained by SemVer. The new types contain both our old types (BigInt, EC_Point) and new types (EC_Scalar, EC_AffinePoint). Eventually the legacy types will be removed, but we can't do that until the next major version. GH #4027
- Loading branch information
Showing
13 changed files
with
280 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#include <botan/internal/ec_key_data.h> | ||
|
||
#include <botan/rng.h> | ||
|
||
namespace Botan { | ||
|
||
EC_PublicKey_Data::EC_PublicKey_Data(const EC_Group& group, std::span<const uint8_t> bytes) : | ||
m_group(group), | ||
m_point(EC_AffinePoint::deserialize_or_throw(group, bytes)), | ||
m_legacy_point(m_point.to_legacy_point()) {} | ||
|
||
EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, RandomNumberGenerator& rng) : | ||
m_group(group), m_scalar(EC_Scalar::random(m_group, rng)), m_legacy_x(m_scalar.to_bigint()) {} | ||
|
||
EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, const BigInt& x) : | ||
m_group(group), m_scalar(EC_Scalar::from_bigint(m_group, x)), m_legacy_x(m_scalar.to_bigint()) {} | ||
|
||
EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, std::span<const uint8_t> bytes) : | ||
m_group(group), m_scalar(EC_Scalar::deserialize_or_throw(m_group, bytes)), m_legacy_x(m_scalar.to_bigint()) {} | ||
|
||
std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(RandomNumberGenerator& rng, | ||
bool with_modular_inverse) const { | ||
auto public_point = [&]() { | ||
std::vector<BigInt> ws; | ||
if(with_modular_inverse) { | ||
return EC_AffinePoint::g_mul(m_scalar.invert(), rng, ws); | ||
} else { | ||
return EC_AffinePoint::g_mul(m_scalar, rng, ws); | ||
} | ||
}; | ||
|
||
return std::make_shared<EC_PublicKey_Data>(m_group, public_point()); | ||
} | ||
|
||
std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(bool with_modular_inverse) const { | ||
Null_RNG null_rng; | ||
return this->public_key(null_rng, with_modular_inverse); | ||
} | ||
|
||
void EC_PrivateKey_Data::serialize_to(std::span<uint8_t> output) const { | ||
m_scalar.serialize_to(output); | ||
} | ||
|
||
} // namespace Botan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#ifndef BOTAN_EC_KEY_DATA_H_ | ||
#define BOTAN_EC_KEY_DATA_H_ | ||
|
||
#include <botan/ec_apoint.h> | ||
#include <botan/ec_group.h> | ||
#include <botan/ec_scalar.h> | ||
|
||
#include <botan/bigint.h> | ||
#include <botan/ec_point.h> | ||
|
||
namespace Botan { | ||
|
||
class RandomNumberGenerator; | ||
|
||
class EC_PublicKey_Data final { | ||
public: | ||
EC_PublicKey_Data(EC_Group group, EC_AffinePoint pt) : | ||
m_group(std::move(group)), m_point(std::move(pt)), m_legacy_point(m_point.to_legacy_point()) {} | ||
|
||
EC_PublicKey_Data(const EC_Group& group, std::span<const uint8_t> bytes); | ||
|
||
const EC_Group& group() const { return m_group; } | ||
|
||
const EC_AffinePoint& public_key() const { return m_point; } | ||
|
||
const EC_Point& legacy_point() const { return m_legacy_point; } | ||
|
||
private: | ||
EC_Group m_group; | ||
EC_AffinePoint m_point; | ||
EC_Point m_legacy_point; | ||
}; | ||
|
||
class EC_PrivateKey_Data final { | ||
public: | ||
EC_PrivateKey_Data(const EC_Group& group, RandomNumberGenerator& rng); | ||
|
||
EC_PrivateKey_Data(const EC_Group& group, const BigInt& x); | ||
|
||
EC_PrivateKey_Data(const EC_Group& group, std::span<const uint8_t> bytes); | ||
|
||
std::shared_ptr<EC_PublicKey_Data> public_key(RandomNumberGenerator& rng, bool with_modular_inverse) const; | ||
|
||
std::shared_ptr<EC_PublicKey_Data> public_key(bool with_modular_inverse) const; | ||
|
||
void serialize_to(std::span<uint8_t> output) const; | ||
|
||
template <typename T> | ||
T serialize() const { | ||
T bytes(this->group().get_order_bytes()); | ||
this->serialize_to(bytes); | ||
return bytes; | ||
} | ||
|
||
const EC_Group& group() const { return m_group; } | ||
|
||
const EC_Scalar& private_key() const { return m_scalar; } | ||
|
||
const BigInt& legacy_bigint() const { return m_legacy_x; } | ||
|
||
private: | ||
EC_Group m_group; | ||
|
||
EC_Scalar m_scalar; | ||
BigInt m_legacy_x; | ||
}; | ||
|
||
} // namespace Botan | ||
|
||
#endif |
Oops, something went wrong.