Skip to content

Commit

Permalink
fix several move-nits in ecc constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Jul 23, 2024
1 parent 329f500 commit 220ee66
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/lib/prov/pkcs11/p11_ecc_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, ObjectHandle handle)

EC_Group group(ec_parameters);
auto pt = decode_public_point(group, pt_bytes);
m_public_key = std::make_shared<EC_PublicKey_Data>(group, pt);
m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
}

PKCS11_EC_PublicKey::PKCS11_EC_PublicKey(Session& session, const EC_PublicKeyImportProperties& props) :
Object(session, props) {
EC_Group group(props.ec_params());
auto pt = decode_public_point(group, props.ec_point());
m_public_key = std::make_shared<EC_PublicKey_Data>(group, pt);
m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
}

EC_PrivateKeyImportProperties::EC_PrivateKeyImportProperties(const std::vector<uint8_t>& ec_params,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/pubkey/ec_group/ec_inner_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_one() const {
return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
}

std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(const BigInt& bn) const {
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(BigInt bn) const {
// Assumed to have been already checked as in range
return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(bn));
}

std::unique_ptr<EC_Scalar_Data> EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/ec_group/ec_inner_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class EC_Group_Data final : public std::enable_shared_from_this<EC_Group_Data> {

std::unique_ptr<EC_Scalar_Data> scalar_from_bytes_mod_order(std::span<const uint8_t> bytes) const;

std::unique_ptr<EC_Scalar_Data> scalar_from_bigint(const BigInt& bn) const;
std::unique_ptr<EC_Scalar_Data> scalar_from_bigint(BigInt bn) const;

std::unique_ptr<EC_Scalar_Data> scalar_random(RandomNumberGenerator& rng) const;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/pubkey/ec_group/ec_scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ EC_Scalar EC_Scalar::one(const EC_Group& group) {
return EC_Scalar(group._data()->scalar_one());
}

EC_Scalar EC_Scalar::from_bigint(const EC_Group& group, const BigInt& bn) {
EC_Scalar EC_Scalar::from_bigint(const EC_Group& group, BigInt bn) {
BOTAN_ARG_CHECK(bn.is_positive() && bn <= group._data()->order(), "EC_Scalar::from_bigint out of range");
return EC_Scalar(group._data()->scalar_from_bigint(bn));
return EC_Scalar(group._data()->scalar_from_bigint(std::move(bn)));
}

BigInt EC_Scalar::to_bigint() const {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/ec_group/ec_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class BOTAN_UNSTABLE_API EC_Scalar final {
*
* Throws an exception if the provided bn is negative or too large
*/
static EC_Scalar from_bigint(const EC_Group& group, const BigInt& bn);
static EC_Scalar from_bigint(const EC_Group& group, BigInt bn);

/**
* Compute the elliptic curve scalar multiplication (g*k) where g is the
Expand Down
25 changes: 14 additions & 11 deletions src/lib/pubkey/ecc_key/ec_key_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "botan/ec_apoint.h"
#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(group, bytes)), m_legacy_point(m_point.to_legacy_point()) {}
EC_PublicKey_Data::EC_PublicKey_Data(EC_Group group, std::span<const uint8_t> bytes) :
m_group(std::move(group)), m_point(m_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(EC_Group group, RandomNumberGenerator& rng) :
m_group(std::move(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(EC_Group group, BigInt x) :
m_group(std::move(group)),
m_scalar(EC_Scalar::from_bigint(m_group, std::move(x))),
m_legacy_x(m_scalar.to_bigint()) {}

EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, const EC_Scalar& x) :
m_group(group), m_scalar(x), m_legacy_x(m_scalar.to_bigint()) {}
EC_PrivateKey_Data::EC_PrivateKey_Data(EC_Group group, EC_Scalar x) :
m_group(std::move(group)), m_scalar(std::move(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(m_group, bytes)), m_legacy_x(m_scalar.to_bigint()) {}
EC_PrivateKey_Data::EC_PrivateKey_Data(EC_Group group, std::span<const uint8_t> bytes) :
m_group(std::move(group)), m_scalar(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 = [&]() {
auto public_point = [&] {
std::vector<BigInt> ws;
if(with_modular_inverse) {
return EC_AffinePoint::g_mul(m_scalar.invert(), rng, ws);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/pubkey/ecc_key/ec_key_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EC_PublicKey_Data final {
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);
EC_PublicKey_Data(EC_Group group, std::span<const uint8_t> bytes);

const EC_Group& group() const { return m_group; }

Expand All @@ -39,13 +39,13 @@ class EC_PublicKey_Data final {

class EC_PrivateKey_Data final {
public:
EC_PrivateKey_Data(const EC_Group& group, RandomNumberGenerator& rng);
EC_PrivateKey_Data(EC_Group group, RandomNumberGenerator& rng);

EC_PrivateKey_Data(const EC_Group& group, const BigInt& x);
EC_PrivateKey_Data(EC_Group group, BigInt x);

EC_PrivateKey_Data(const EC_Group& group, const EC_Scalar& x);
EC_PrivateKey_Data(EC_Group group, EC_Scalar x);

EC_PrivateKey_Data(const EC_Group& group, std::span<const uint8_t> bytes);
EC_PrivateKey_Data(EC_Group group, std::span<const uint8_t> bytes);

std::shared_ptr<EC_PublicKey_Data> public_key(RandomNumberGenerator& rng, bool with_modular_inverse) const;

Expand Down
25 changes: 11 additions & 14 deletions src/lib/pubkey/ecc_key/ecc_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ EC_Group_Encoding default_encoding_for(const EC_Group& group) {

} // namespace

EC_PublicKey::EC_PublicKey(const EC_Group& group, const EC_Point& pub_point) {
EC_PublicKey::EC_PublicKey(EC_Group group, const EC_Point& pub_point) {
auto pt = EC_AffinePoint(group, pub_point);
m_public_key = std::make_shared<EC_PublicKey_Data>(group, pt);
m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pt));
m_domain_encoding = default_encoding_for(domain());
}

EC_PublicKey::EC_PublicKey(const EC_Group& group, const EC_AffinePoint& pub_point) {
m_public_key = std::make_shared<EC_PublicKey_Data>(group, pub_point);
EC_PublicKey::EC_PublicKey(EC_Group group, EC_AffinePoint pub_point) {
m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), std::move(pub_point));
m_domain_encoding = default_encoding_for(domain());
}

Expand Down Expand Up @@ -123,28 +123,25 @@ const EC_Scalar& EC_PrivateKey::_private_key() const {
/**
* EC_PrivateKey constructor
*/
EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
const EC_Group& ec_group,
const BigInt& x,
bool with_modular_inverse) {
EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, EC_Group ec_group, BigInt x, bool with_modular_inverse) {
if(x == 0) {
m_private_key = std::make_shared<EC_PrivateKey_Data>(ec_group, rng);
m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), rng);
} else {
m_private_key = std::make_shared<EC_PrivateKey_Data>(ec_group, x);
m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), std::move(x));
}

m_public_key = m_private_key->public_key(rng, with_modular_inverse);
m_domain_encoding = default_encoding_for(domain());
}

EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& ec_group, bool with_modular_inverse) {
m_private_key = std::make_shared<EC_PrivateKey_Data>(ec_group, rng);
EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, EC_Group ec_group, bool with_modular_inverse) {
m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), rng);
m_public_key = m_private_key->public_key(rng, with_modular_inverse);
m_domain_encoding = default_encoding_for(domain());
}

EC_PrivateKey::EC_PrivateKey(const EC_Group& ec_group, const EC_Scalar& x, bool with_modular_inverse) {
m_private_key = std::make_shared<EC_PrivateKey_Data>(ec_group, x);
EC_PrivateKey::EC_PrivateKey(EC_Group ec_group, EC_Scalar x, bool with_modular_inverse) {
m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), std::move(x));
m_public_key = m_private_key->public_key(with_modular_inverse);
m_domain_encoding = default_encoding_for(domain());
}
Expand Down
13 changes: 5 additions & 8 deletions src/lib/pubkey/ecc_key/ecc_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ class BOTAN_PUBLIC_API(2, 0) EC_PublicKey : public virtual Public_Key {
* @param group EC domain parameters
* @param pub_point public point on the curve
*/
EC_PublicKey(const EC_Group& group, const EC_Point& pub_point);
EC_PublicKey(EC_Group group, const EC_Point& pub_point);

/**
* Load a public key from the point.
*
* @param group EC domain parameters
* @param pub_point public point on the curve
*/
EC_PublicKey(const EC_Group& group, const EC_AffinePoint& pub_point);
EC_PublicKey(EC_Group group, EC_AffinePoint pub_point);

/**
* Load a public key.
Expand Down Expand Up @@ -170,10 +170,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
* x (as in ECGDSA and ECKCDSA), otherwise by
* multiplying directly with x (as in ECDSA).
*/
EC_PrivateKey(RandomNumberGenerator& rng,
const EC_Group& domain,
const BigInt& x,
bool with_modular_inverse = false);
EC_PrivateKey(RandomNumberGenerator& rng, EC_Group domain, BigInt x, bool with_modular_inverse = false);

/*
* Creates a new private key
Expand All @@ -182,7 +179,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
* multiplying the base point with the modular inverse of x (as in ECGDSA
* and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
*/
EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, bool with_modular_inverse = false);
EC_PrivateKey(RandomNumberGenerator& rng, EC_Group group, bool with_modular_inverse = false);

/*
* Load a EC private key from the secret scalar
Expand All @@ -191,7 +188,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
* multiplying the base point with the modular inverse of x (as in ECGDSA
* and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
*/
EC_PrivateKey(const EC_Group& group, const EC_Scalar& scalar, bool with_modular_inverse = false);
EC_PrivateKey(EC_Group group, EC_Scalar scalar, bool with_modular_inverse = false);

/*
* Creates a new private key object from the
Expand Down
17 changes: 7 additions & 10 deletions src/lib/pubkey/eckcdsa/eckcdsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ class ECKCDSA_Signature_Operation final : public PK_Ops::Signature {
m_group(eckcdsa.domain()),
m_x(eckcdsa._private_key()),
m_hash(eckcdsa_signature_hash(padding)),
m_prefix_used(false) {
m_prefix = eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size());
}
m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
m_prefix_used(false) {}

void update(std::span<const uint8_t> input) override {
if(!m_prefix_used) {
Expand Down Expand Up @@ -190,17 +189,15 @@ class ECKCDSA_Verification_Operation final : public PK_Ops::Verification {
m_group(eckcdsa.domain()),
m_gy_mul(eckcdsa._public_key()),
m_hash(eckcdsa_signature_hash(padding)),
m_prefix_used(false) {
m_prefix = eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size());
}
m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
m_prefix_used(false) {}

ECKCDSA_Verification_Operation(const ECKCDSA_PublicKey& eckcdsa, const AlgorithmIdentifier& alg_id) :
m_group(eckcdsa.domain()),
m_gy_mul(eckcdsa._public_key()),
m_hash(eckcdsa_signature_hash(alg_id)),
m_prefix_used(false) {
m_prefix = eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size());
}
m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
m_prefix_used(false) {}

void update(std::span<const uint8_t> msg) override;

Expand All @@ -213,8 +210,8 @@ class ECKCDSA_Verification_Operation final : public PK_Ops::Verification {

const EC_Group m_group;
const EC_Group::Mul2Table m_gy_mul;
std::vector<uint8_t> m_prefix;
std::unique_ptr<HashFunction> m_hash;
std::vector<uint8_t> m_prefix;
bool m_prefix_used;
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/gost_3410/gost_3410.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ GOST_3410_PublicKey::GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, std:
encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);

m_public_key = std::make_shared<EC_PublicKey_Data>(group, encoding);
m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), encoding);
}

GOST_3410_PrivateKey::GOST_3410_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x) :
Expand Down
6 changes: 3 additions & 3 deletions src/lib/pubkey/sm2/sm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<cons
m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
m_da_inv_legacy(m_da_inv.to_bigint()) {}

SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x) :
EC_PrivateKey(rng, domain, x),
m_da_inv((this->_private_key() + EC_Scalar::one(domain)).invert()),
SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, EC_Group group, BigInt x) :
EC_PrivateKey(rng, std::move(group), std::move(x)),
m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
m_da_inv_legacy(m_da_inv.to_bigint()) {}

namespace {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/sm2/sm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BOTAN_PUBLIC_API(2, 2) SM2_PrivateKey final : public SM2_PublicKey,
* @param domain parameters to used for this key
* @param x the private key (if zero, generate a new random key)
*/
SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x = BigInt::zero());
SM2_PrivateKey(RandomNumberGenerator& rng, EC_Group domain, BigInt x = BigInt::zero());

bool check_key(RandomNumberGenerator& rng, bool) const override;

Expand Down

0 comments on commit 220ee66

Please sign in to comment.