diff --git a/src/lib/prov/pkcs11/p11_ecc_key.cpp b/src/lib/prov/pkcs11/p11_ecc_key.cpp index 9a35b9f0543..6162e69bf2a 100644 --- a/src/lib/prov/pkcs11/p11_ecc_key.cpp +++ b/src/lib/prov/pkcs11/p11_ecc_key.cpp @@ -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(group, pt); + m_public_key = std::make_shared(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(group, pt); + m_public_key = std::make_shared(std::move(group), std::move(pt)); } EC_PrivateKeyImportProperties::EC_PrivateKeyImportProperties(const std::vector& ec_params, diff --git a/src/lib/pubkey/ec_group/ec_inner_data.cpp b/src/lib/pubkey/ec_group/ec_inner_data.cpp index e13d15ddde9..df34b864aa5 100644 --- a/src/lib/pubkey/ec_group/ec_inner_data.cpp +++ b/src/lib/pubkey/ec_group/ec_inner_data.cpp @@ -93,9 +93,9 @@ std::unique_ptr EC_Group_Data::scalar_one() const { return std::make_unique(shared_from_this(), BigInt::one()); } -std::unique_ptr EC_Group_Data::scalar_from_bigint(const BigInt& bn) const { +std::unique_ptr EC_Group_Data::scalar_from_bigint(BigInt bn) const { // Assumed to have been already checked as in range - return std::make_unique(shared_from_this(), bn); + return std::make_unique(shared_from_this(), std::move(bn)); } std::unique_ptr EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar, diff --git a/src/lib/pubkey/ec_group/ec_inner_data.h b/src/lib/pubkey/ec_group/ec_inner_data.h index c80ebafd5ba..2cb39ec1fb3 100644 --- a/src/lib/pubkey/ec_group/ec_inner_data.h +++ b/src/lib/pubkey/ec_group/ec_inner_data.h @@ -185,7 +185,7 @@ class EC_Group_Data final : public std::enable_shared_from_this { std::unique_ptr scalar_from_bytes_mod_order(std::span bytes) const; - std::unique_ptr scalar_from_bigint(const BigInt& bn) const; + std::unique_ptr scalar_from_bigint(BigInt bn) const; std::unique_ptr scalar_random(RandomNumberGenerator& rng) const; diff --git a/src/lib/pubkey/ec_group/ec_scalar.cpp b/src/lib/pubkey/ec_group/ec_scalar.cpp index e805d9d61ab..9fc982a3f57 100644 --- a/src/lib/pubkey/ec_group/ec_scalar.cpp +++ b/src/lib/pubkey/ec_group/ec_scalar.cpp @@ -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 { diff --git a/src/lib/pubkey/ec_group/ec_scalar.h b/src/lib/pubkey/ec_group/ec_scalar.h index 6ee1e5342db..a820b95de71 100644 --- a/src/lib/pubkey/ec_group/ec_scalar.h +++ b/src/lib/pubkey/ec_group/ec_scalar.h @@ -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 diff --git a/src/lib/pubkey/ecc_key/ec_key_data.cpp b/src/lib/pubkey/ecc_key/ec_key_data.cpp index 246ec51e2ab..3b3a29230c5 100644 --- a/src/lib/pubkey/ecc_key/ec_key_data.cpp +++ b/src/lib/pubkey/ecc_key/ec_key_data.cpp @@ -4,30 +4,33 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#include "botan/ec_apoint.h" #include #include namespace Botan { -EC_PublicKey_Data::EC_PublicKey_Data(const EC_Group& group, std::span 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 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 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 bytes) : + m_group(std::move(group)), m_scalar(m_group, bytes), m_legacy_x(m_scalar.to_bigint()) {} std::shared_ptr EC_PrivateKey_Data::public_key(RandomNumberGenerator& rng, bool with_modular_inverse) const { - auto public_point = [&]() { + auto public_point = [&] { std::vector ws; if(with_modular_inverse) { return EC_AffinePoint::g_mul(m_scalar.invert(), rng, ws); diff --git a/src/lib/pubkey/ecc_key/ec_key_data.h b/src/lib/pubkey/ecc_key/ec_key_data.h index d3c16bfb21b..9c4382562ec 100644 --- a/src/lib/pubkey/ecc_key/ec_key_data.h +++ b/src/lib/pubkey/ecc_key/ec_key_data.h @@ -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 bytes); + EC_PublicKey_Data(EC_Group group, std::span bytes); const EC_Group& group() const { return m_group; } @@ -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 bytes); + EC_PrivateKey_Data(EC_Group group, std::span bytes); std::shared_ptr public_key(RandomNumberGenerator& rng, bool with_modular_inverse) const; diff --git a/src/lib/pubkey/ecc_key/ecc_key.cpp b/src/lib/pubkey/ecc_key/ecc_key.cpp index 207ac218533..6932e18ee94 100644 --- a/src/lib/pubkey/ecc_key/ecc_key.cpp +++ b/src/lib/pubkey/ecc_key/ecc_key.cpp @@ -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(group, pt); + m_public_key = std::make_shared(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(group, pub_point); +EC_PublicKey::EC_PublicKey(EC_Group group, EC_AffinePoint pub_point) { + m_public_key = std::make_shared(std::move(group), std::move(pub_point)); m_domain_encoding = default_encoding_for(domain()); } @@ -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_group, rng); + m_private_key = std::make_shared(std::move(ec_group), rng); } else { - m_private_key = std::make_shared(ec_group, x); + m_private_key = std::make_shared(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_group, rng); +EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, EC_Group ec_group, bool with_modular_inverse) { + m_private_key = std::make_shared(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_group, x); +EC_PrivateKey::EC_PrivateKey(EC_Group ec_group, EC_Scalar x, bool with_modular_inverse) { + m_private_key = std::make_shared(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()); } diff --git a/src/lib/pubkey/ecc_key/ecc_key.h b/src/lib/pubkey/ecc_key/ecc_key.h index ead44039d54..cdee56e87cc 100644 --- a/src/lib/pubkey/ecc_key/ecc_key.h +++ b/src/lib/pubkey/ecc_key/ecc_key.h @@ -107,7 +107,7 @@ 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. @@ -115,7 +115,7 @@ 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_AffinePoint& pub_point); + EC_PublicKey(EC_Group group, EC_AffinePoint pub_point); /** * Load a public key. @@ -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 @@ -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 @@ -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 diff --git a/src/lib/pubkey/eckcdsa/eckcdsa.cpp b/src/lib/pubkey/eckcdsa/eckcdsa.cpp index 51e30153c75..d424143a7d1 100644 --- a/src/lib/pubkey/eckcdsa/eckcdsa.cpp +++ b/src/lib/pubkey/eckcdsa/eckcdsa.cpp @@ -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 input) override { if(!m_prefix_used) { @@ -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 msg) override; @@ -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 m_prefix; std::unique_ptr m_hash; + std::vector m_prefix; bool m_prefix_used; }; diff --git a/src/lib/pubkey/gost_3410/gost_3410.cpp b/src/lib/pubkey/gost_3410/gost_3410.cpp index 5d0d34b3135..1747b321fd3 100644 --- a/src/lib/pubkey/gost_3410/gost_3410.cpp +++ b/src/lib/pubkey/gost_3410/gost_3410.cpp @@ -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(group, encoding); + m_public_key = std::make_shared(std::move(group), encoding); } GOST_3410_PrivateKey::GOST_3410_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x) : diff --git a/src/lib/pubkey/sm2/sm2.cpp b/src/lib/pubkey/sm2/sm2.cpp index 13c481e8229..5b67d85297c 100644 --- a/src/lib/pubkey/sm2/sm2.cpp +++ b/src/lib/pubkey/sm2/sm2.cpp @@ -51,9 +51,9 @@ SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span_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 { diff --git a/src/lib/pubkey/sm2/sm2.h b/src/lib/pubkey/sm2/sm2.h index 9cf3733d55f..e8911d197cc 100644 --- a/src/lib/pubkey/sm2/sm2.h +++ b/src/lib/pubkey/sm2/sm2.h @@ -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;