aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/ecc_key/ecc_key.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2020-12-15 07:37:53 -0500
committerJack Lloyd <[email protected]>2020-12-15 07:53:31 -0500
commit457415bff9872e22052a8f5981163c19b1d8e81a (patch)
tree2f2c98528038a6b34ca4016f36b5b0ce942f761a /src/lib/pubkey/ecc_key/ecc_key.cpp
parent3cbb45c94ddc6137b7031ead0974cf388d5c9e7e (diff)
Convert EC_Group_Encoding to enum class
Diffstat (limited to 'src/lib/pubkey/ecc_key/ecc_key.cpp')
-rw-r--r--src/lib/pubkey/ecc_key/ecc_key.cpp51
1 files changed, 21 insertions, 30 deletions
diff --git a/src/lib/pubkey/ecc_key/ecc_key.cpp b/src/lib/pubkey/ecc_key/ecc_key.cpp
index 09748b847..90ffb260b 100644
--- a/src/lib/pubkey/ecc_key/ecc_key.cpp
+++ b/src/lib/pubkey/ecc_key/ecc_key.cpp
@@ -27,15 +27,24 @@ size_t EC_PublicKey::estimated_strength() const
return ecp_work_factor(key_length());
}
-EC_PublicKey::EC_PublicKey(const EC_Group& dom_par,
- const PointGFp& pub_point) :
- m_domain_params(dom_par), m_public_key(pub_point)
+namespace {
+
+EC_Group_Encoding default_encoding_for(EC_Group& group)
{
- if (!dom_par.get_curve_oid().empty())
- m_domain_encoding = EC_DOMPAR_ENC_OID;
+ if(group.get_curve_oid().empty())
+ return EC_Group_Encoding::Explicit;
else
- m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
+ return EC_Group_Encoding::NamedCurve;
+ }
+}
+
+EC_PublicKey::EC_PublicKey(const EC_Group& dom_par,
+ const PointGFp& pub_point) :
+ m_domain_params(dom_par),
+ m_public_key(pub_point),
+ m_domain_encoding(default_encoding_for(m_domain_params))
+ {
#if 0
if(domain().get_curve() != public_point().get_curve())
throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
@@ -45,12 +54,9 @@ EC_PublicKey::EC_PublicKey(const EC_Group& dom_par,
EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits) :
m_domain_params{EC_Group(alg_id.get_parameters())},
- m_public_key{domain().OS2ECP(key_bits)}
+ m_public_key{domain().OS2ECP(key_bits)},
+ m_domain_encoding(default_encoding_for(m_domain_params))
{
- if (!domain().get_curve_oid().empty())
- m_domain_encoding = EC_DOMPAR_ENC_OID;
- else
- m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
}
bool EC_PublicKey::check_key(RandomNumberGenerator& rng,
@@ -83,15 +89,8 @@ void EC_PublicKey::set_point_encoding(PointGFp::Compression_Type enc)
void EC_PublicKey::set_parameter_encoding(EC_Group_Encoding form)
{
- if(form != EC_DOMPAR_ENC_EXPLICIT &&
- form != EC_DOMPAR_ENC_IMPLICITCA &&
- form != EC_DOMPAR_ENC_OID)
- throw Invalid_Argument("Invalid encoding form for EC-key object specified");
-
- if((form == EC_DOMPAR_ENC_OID) && (m_domain_params.get_curve_oid().empty()))
- throw Invalid_Argument("Invalid encoding form OID specified for "
- "EC-key object whose corresponding domain "
- "parameters are without oid");
+ if(form == EC_Group_Encoding::NamedCurve && m_domain_params.get_curve_oid().empty())
+ throw Invalid_Argument("Cannot used NamedCurve encoding for a curve without an OID");
m_domain_encoding = form;
}
@@ -113,10 +112,7 @@ EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
bool with_modular_inverse)
{
m_domain_params = ec_group;
- if (!ec_group.get_curve_oid().empty())
- m_domain_encoding = EC_DOMPAR_ENC_OID;
- else
- m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
+ m_domain_encoding = default_encoding_for(m_domain_params);
if(x == 0)
{
@@ -162,12 +158,7 @@ EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id,
bool with_modular_inverse)
{
m_domain_params = EC_Group(alg_id.get_parameters());
- m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
-
- if (!domain().get_curve_oid().empty())
- m_domain_encoding = EC_DOMPAR_ENC_OID;
- else
- m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
+ m_domain_encoding = default_encoding_for(m_domain_params);
OID key_parameters;
secure_vector<uint8_t> public_key_bits;