aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2020-12-15 07:20:39 -0500
committerJack Lloyd <[email protected]>2020-12-15 07:20:39 -0500
commit603ec4de6c08475264fcd9909870312044ec0fea (patch)
tree57867f061335bbe291f401e2e701d195c3e15f83 /src/lib/pubkey
parent972266811936d5e32860302542bfadc1c0d3e553 (diff)
Convert DL_Group::Format to enum class
Diffstat (limited to 'src/lib/pubkey')
-rw-r--r--src/lib/pubkey/dh/dh.cpp2
-rw-r--r--src/lib/pubkey/dh/dh.h4
-rw-r--r--src/lib/pubkey/dl_algo/dl_algo.cpp4
-rw-r--r--src/lib/pubkey/dl_algo/dl_algo.h6
-rw-r--r--src/lib/pubkey/dl_group/dl_group.cpp48
-rw-r--r--src/lib/pubkey/dl_group/dl_group.h40
-rw-r--r--src/lib/pubkey/dsa/dsa.cpp2
-rw-r--r--src/lib/pubkey/dsa/dsa.h4
-rw-r--r--src/lib/pubkey/elgamal/elgamal.cpp2
-rw-r--r--src/lib/pubkey/elgamal/elgamal.h4
10 files changed, 59 insertions, 57 deletions
diff --git a/src/lib/pubkey/dh/dh.cpp b/src/lib/pubkey/dh/dh.cpp
index 77ac19da4..98a05260e 100644
--- a/src/lib/pubkey/dh/dh.cpp
+++ b/src/lib/pubkey/dh/dh.cpp
@@ -58,7 +58,7 @@ DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng,
*/
DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id,
const secure_vector<uint8_t>& key_bits) :
- DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_42)
{
if(m_y.is_zero())
{
diff --git a/src/lib/pubkey/dh/dh.h b/src/lib/pubkey/dh/dh.h
index bb6d5dae4..3b81bbf5d 100644
--- a/src/lib/pubkey/dh/dh.h
+++ b/src/lib/pubkey/dh/dh.h
@@ -22,7 +22,7 @@ class BOTAN_PUBLIC_API(2,0) DH_PublicKey : public virtual DL_Scheme_PublicKey
std::vector<uint8_t> public_value() const;
- DL_Group::Format group_format() const override { return DL_Group::ANSI_X9_42; }
+ DL_Group_Format group_format() const override { return DL_Group_Format::ANSI_X9_42; }
/**
* Create a public key.
@@ -31,7 +31,7 @@ class BOTAN_PUBLIC_API(2,0) DH_PublicKey : public virtual DL_Scheme_PublicKey
*/
DH_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits) :
- DL_Scheme_PublicKey(alg_id, key_bits, DL_Group::ANSI_X9_42) {}
+ DL_Scheme_PublicKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_42) {}
/**
* Construct a public key with the specified parameters.
diff --git a/src/lib/pubkey/dl_algo/dl_algo.cpp b/src/lib/pubkey/dl_algo/dl_algo.cpp
index 15b0b175e..66efe7456 100644
--- a/src/lib/pubkey/dl_algo/dl_algo.cpp
+++ b/src/lib/pubkey/dl_algo/dl_algo.cpp
@@ -43,7 +43,7 @@ DL_Scheme_PublicKey::DL_Scheme_PublicKey(const DL_Group& group, const BigInt& y)
DL_Scheme_PublicKey::DL_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits,
- DL_Group::Format format) :
+ DL_Group_Format format) :
m_group(alg_id.get_parameters(), format)
{
BER_Decoder(key_bits).decode(m_y);
@@ -56,7 +56,7 @@ secure_vector<uint8_t> DL_Scheme_PrivateKey::private_key_bits() const
DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
const secure_vector<uint8_t>& key_bits,
- DL_Group::Format format)
+ DL_Group_Format format)
{
m_group.BER_decode(alg_id.get_parameters(), format);
diff --git a/src/lib/pubkey/dl_algo/dl_algo.h b/src/lib/pubkey/dl_algo/dl_algo.h
index af01bc217..d3edd6bb2 100644
--- a/src/lib/pubkey/dl_algo/dl_algo.h
+++ b/src/lib/pubkey/dl_algo/dl_algo.h
@@ -64,7 +64,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Scheme_PublicKey : public virtual Public_Key
* Get the underlying groups encoding format.
* @return encoding format
*/
- virtual DL_Group::Format group_format() const = 0;
+ virtual DL_Group_Format group_format() const = 0;
size_t key_length() const override;
size_t estimated_strength() const override;
@@ -82,7 +82,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Scheme_PublicKey : public virtual Public_Key
*/
DL_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits,
- DL_Group::Format group_format);
+ DL_Group_Format group_format);
DL_Scheme_PublicKey(const DL_Group& group, const BigInt& y);
@@ -125,7 +125,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Scheme_PrivateKey : public virtual DL_Scheme_Publ
*/
DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
const secure_vector<uint8_t>& key_bits,
- DL_Group::Format group_format);
+ DL_Group_Format group_format);
DL_Scheme_PrivateKey() = default;
diff --git a/src/lib/pubkey/dl_group/dl_group.cpp b/src/lib/pubkey/dl_group/dl_group.cpp
index 7ff0a1958..05fe9dcf3 100644
--- a/src/lib/pubkey/dl_group/dl_group.cpp
+++ b/src/lib/pubkey/dl_group/dl_group.cpp
@@ -123,7 +123,7 @@ class DL_Group_Data final
//static
std::shared_ptr<DL_Group_Data> DL_Group::BER_decode_DL_group(const uint8_t data[], size_t data_len,
- DL_Group::Format format,
+ DL_Group_Format format,
DL_Group_Source source)
{
BigInt p, q, g;
@@ -131,21 +131,21 @@ std::shared_ptr<DL_Group_Data> DL_Group::BER_decode_DL_group(const uint8_t data[
BER_Decoder decoder(data, data_len);
BER_Decoder ber = decoder.start_cons(SEQUENCE);
- if(format == DL_Group::ANSI_X9_57)
+ if(format == DL_Group_Format::ANSI_X9_57)
{
ber.decode(p)
.decode(q)
.decode(g)
.verify_end();
}
- else if(format == DL_Group::ANSI_X9_42)
+ else if(format == DL_Group_Format::ANSI_X9_42)
{
ber.decode(p)
.decode(g)
.decode(q)
.discard_remaining();
}
- else if(format == DL_Group::PKCS_3)
+ else if(format == DL_Group_Format::PKCS_3)
{
// q is left as zero
ber.decode(p)
@@ -153,7 +153,7 @@ std::shared_ptr<DL_Group_Data> DL_Group::BER_decode_DL_group(const uint8_t data[
.discard_remaining();
}
else
- throw Invalid_Argument("Unknown DL_Group encoding " + std::to_string(format));
+ throw Invalid_Argument("Unknown DL_Group encoding");
return std::make_shared<DL_Group_Data>(p, q, g, source);
}
@@ -185,14 +185,14 @@ DL_Group::load_DL_group_info(const char* p_str,
namespace {
-DL_Group::Format pem_label_to_dl_format(const std::string& label)
+DL_Group_Format pem_label_to_dl_format(const std::string& label)
{
if(label == "DH PARAMETERS")
- return DL_Group::PKCS_3;
+ return DL_Group_Format::PKCS_3;
else if(label == "DSA PARAMETERS")
- return DL_Group::ANSI_X9_57;
+ return DL_Group_Format::ANSI_X9_57;
else if(label == "X942 DH PARAMETERS" || label == "X9.42 DH PARAMETERS")
- return DL_Group::ANSI_X9_42;
+ return DL_Group_Format::ANSI_X9_42;
else
throw Decoding_Error("DL_Group: Invalid PEM label " + label);
}
@@ -213,7 +213,7 @@ DL_Group::DL_Group(const std::string& str)
{
std::string label;
const std::vector<uint8_t> ber = unlock(PEM_Code::decode(str, label));
- Format format = pem_label_to_dl_format(label);
+ DL_Group_Format format = pem_label_to_dl_format(label);
m_data = BER_decode_DL_group(ber.data(), ber.size(), format, DL_Group_Source::ExternalSource);
}
@@ -589,15 +589,15 @@ DL_Group_Source DL_Group::source() const
/*
* DER encode the parameters
*/
-std::vector<uint8_t> DL_Group::DER_encode(Format format) const
+std::vector<uint8_t> DL_Group::DER_encode(DL_Group_Format format) const
{
- if(get_q().is_zero() && (format == ANSI_X9_57 || format == ANSI_X9_42))
+ if(get_q().is_zero() && (format != DL_Group_Format::PKCS_3))
throw Encoding_Error("Cannot encode DL_Group in ANSI formats when q param is missing");
std::vector<uint8_t> output;
DER_Encoder der(output);
- if(format == ANSI_X9_57)
+ if(format == DL_Group_Format::ANSI_X9_57)
{
der.start_cons(SEQUENCE)
.encode(get_p())
@@ -605,7 +605,7 @@ std::vector<uint8_t> DL_Group::DER_encode(Format format) const
.encode(get_g())
.end_cons();
}
- else if(format == ANSI_X9_42)
+ else if(format == DL_Group_Format::ANSI_X9_42)
{
der.start_cons(SEQUENCE)
.encode(get_p())
@@ -613,7 +613,7 @@ std::vector<uint8_t> DL_Group::DER_encode(Format format) const
.encode(get_q())
.end_cons();
}
- else if(format == PKCS_3)
+ else if(format == DL_Group_Format::PKCS_3)
{
der.start_cons(SEQUENCE)
.encode(get_p())
@@ -621,7 +621,7 @@ std::vector<uint8_t> DL_Group::DER_encode(Format format) const
.end_cons();
}
else
- throw Invalid_Argument("Unknown DL_Group encoding " + std::to_string(format));
+ throw Invalid_Argument("Unknown DL_Group encoding");
return output;
}
@@ -629,26 +629,26 @@ std::vector<uint8_t> DL_Group::DER_encode(Format format) const
/*
* PEM encode the parameters
*/
-std::string DL_Group::PEM_encode(Format format) const
+std::string DL_Group::PEM_encode(DL_Group_Format format) const
{
const std::vector<uint8_t> encoding = DER_encode(format);
- if(format == PKCS_3)
+ if(format == DL_Group_Format::PKCS_3)
return PEM_Code::encode(encoding, "DH PARAMETERS");
- else if(format == ANSI_X9_57)
+ else if(format == DL_Group_Format::ANSI_X9_57)
return PEM_Code::encode(encoding, "DSA PARAMETERS");
- else if(format == ANSI_X9_42)
+ else if(format == DL_Group_Format::ANSI_X9_42)
return PEM_Code::encode(encoding, "X9.42 DH PARAMETERS");
else
- throw Invalid_Argument("Unknown DL_Group encoding " + std::to_string(format));
+ throw Invalid_Argument("Unknown DL_Group encoding");
}
-DL_Group::DL_Group(const uint8_t ber[], size_t ber_len, Format format)
+DL_Group::DL_Group(const uint8_t ber[], size_t ber_len, DL_Group_Format format)
{
m_data = BER_decode_DL_group(ber, ber_len, format, DL_Group_Source::ExternalSource);
}
-void DL_Group::BER_decode(const std::vector<uint8_t>& ber, Format format)
+void DL_Group::BER_decode(const std::vector<uint8_t>& ber, DL_Group_Format format)
{
m_data = BER_decode_DL_group(ber.data(), ber.size(), format, DL_Group_Source::ExternalSource);
}
@@ -658,7 +658,7 @@ DL_Group DL_Group::DL_Group_from_PEM(const std::string& pem)
{
std::string label;
const std::vector<uint8_t> ber = unlock(PEM_Code::decode(pem, label));
- Format format = pem_label_to_dl_format(label);
+ DL_Group_Format format = pem_label_to_dl_format(label);
return DL_Group(ber, format);
}
diff --git a/src/lib/pubkey/dl_group/dl_group.h b/src/lib/pubkey/dl_group/dl_group.h
index c3b9443b7..b9355da6f 100644
--- a/src/lib/pubkey/dl_group/dl_group.h
+++ b/src/lib/pubkey/dl_group/dl_group.h
@@ -22,6 +22,20 @@ enum class DL_Group_Source {
};
/**
+* The DL group encoding format variants.
+*/
+enum class DL_Group_Format {
+ ANSI_X9_42,
+ ANSI_X9_57,
+ PKCS_3,
+
+ DSA_PARAMETERS = ANSI_X9_57,
+ DH_PARAMETERS = ANSI_X9_42,
+ ANSI_X9_42_DH_PARAMETERS = ANSI_X9_42,
+ PKCS3_DH_PARAMETERS = PKCS_3
+};
+
+/**
* This class represents discrete logarithm groups. It holds a prime
* modulus p, a generator g, and (optionally) a prime q which is a
* factor of (p-1). In most cases g generates the order-q subgroup.
@@ -34,19 +48,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final
*/
enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer };
- /**
- * The DL group encoding format variants.
- */
- enum Format {
- ANSI_X9_42,
- ANSI_X9_57,
- PKCS_3,
-
- DSA_PARAMETERS = ANSI_X9_57,
- DH_PARAMETERS = ANSI_X9_42,
- ANSI_X9_42_DH_PARAMETERS = ANSI_X9_42,
- PKCS3_DH_PARAMETERS = PKCS_3
- };
+ using Format = DL_Group_Format;
/**
* Construct a DL group with uninitialized internal value.
@@ -115,13 +117,13 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final
/**
* Decode a BER-encoded DL group param
*/
- DL_Group(const uint8_t ber[], size_t ber_len, Format format);
+ DL_Group(const uint8_t ber[], size_t ber_len, DL_Group_Format format);
/**
* Decode a BER-encoded DL group param
*/
template<typename Alloc>
- DL_Group(const std::vector<uint8_t, Alloc>& ber, Format format) :
+ DL_Group(const std::vector<uint8_t, Alloc>& ber, DL_Group_Format format) :
DL_Group(ber.data(), ber.size(), format) {}
/**
@@ -170,14 +172,14 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final
* @param format the encoding format
* @return string holding the PEM encoded group
*/
- std::string PEM_encode(Format format) const;
+ std::string PEM_encode(DL_Group_Format format) const;
/**
* Encode this group into a string using DER encoding.
* @param format the encoding format
* @return string holding the DER encoded group
*/
- std::vector<uint8_t> DER_encode(Format format) const;
+ std::vector<uint8_t> DER_encode(DL_Group_Format format) const;
/**
* Reduce an integer modulo p
@@ -324,7 +326,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final
*
* @warning avoid this. Instead use the DL_Group constructor
*/
- void BER_decode(const std::vector<uint8_t>& ber, Format format);
+ void BER_decode(const std::vector<uint8_t>& ber, DL_Group_Format format);
DL_Group_Source source() const;
@@ -343,7 +345,7 @@ class BOTAN_PUBLIC_API(2,0) DL_Group final
static std::shared_ptr<DL_Group_Data>
BER_decode_DL_group(const uint8_t data[], size_t data_len,
- DL_Group::Format format,
+ DL_Group_Format format,
DL_Group_Source source);
const DL_Group_Data& data() const;
diff --git a/src/lib/pubkey/dsa/dsa.cpp b/src/lib/pubkey/dsa/dsa.cpp
index c2c7e8194..103cb8115 100644
--- a/src/lib/pubkey/dsa/dsa.cpp
+++ b/src/lib/pubkey/dsa/dsa.cpp
@@ -48,7 +48,7 @@ DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng,
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id,
const secure_vector<uint8_t>& key_bits) :
- DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_57)
{
m_y = m_group.power_g_p(m_x, m_group.q_bits());
}
diff --git a/src/lib/pubkey/dsa/dsa.h b/src/lib/pubkey/dsa/dsa.h
index 1834737b5..75513a78d 100644
--- a/src/lib/pubkey/dsa/dsa.h
+++ b/src/lib/pubkey/dsa/dsa.h
@@ -20,7 +20,7 @@ class BOTAN_PUBLIC_API(2,0) DSA_PublicKey : public virtual DL_Scheme_PublicKey
public:
std::string algo_name() const override { return "DSA"; }
- DL_Group::Format group_format() const override { return DL_Group::ANSI_X9_57; }
+ DL_Group_Format group_format() const override { return DL_Group_Format::ANSI_X9_57; }
size_t message_parts() const override { return 2; }
size_t message_part_size() const override { return group_q().bytes(); }
@@ -31,7 +31,7 @@ class BOTAN_PUBLIC_API(2,0) DSA_PublicKey : public virtual DL_Scheme_PublicKey
*/
DSA_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits) :
- DL_Scheme_PublicKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
+ DL_Scheme_PublicKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_57)
{
}
diff --git a/src/lib/pubkey/elgamal/elgamal.cpp b/src/lib/pubkey/elgamal/elgamal.cpp
index 73e091d0a..017f57073 100644
--- a/src/lib/pubkey/elgamal/elgamal.cpp
+++ b/src/lib/pubkey/elgamal/elgamal.cpp
@@ -45,7 +45,7 @@ ElGamal_PrivateKey::ElGamal_PrivateKey(RandomNumberGenerator& rng,
ElGamal_PrivateKey::ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id,
const secure_vector<uint8_t>& key_bits) :
- DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_42)
{
m_y = m_group.power_g_p(m_x, m_group.p_bits());
}
diff --git a/src/lib/pubkey/elgamal/elgamal.h b/src/lib/pubkey/elgamal/elgamal.h
index 4147f5037..0c440df00 100644
--- a/src/lib/pubkey/elgamal/elgamal.h
+++ b/src/lib/pubkey/elgamal/elgamal.h
@@ -19,7 +19,7 @@ class BOTAN_PUBLIC_API(2,0) ElGamal_PublicKey : public virtual DL_Scheme_PublicK
{
public:
std::string algo_name() const override { return "ElGamal"; }
- DL_Group::Format group_format() const override { return DL_Group::ANSI_X9_42; }
+ DL_Group_Format group_format() const override { return DL_Group_Format::ANSI_X9_42; }
/**
* Load a public key.
@@ -28,7 +28,7 @@ class BOTAN_PUBLIC_API(2,0) ElGamal_PublicKey : public virtual DL_Scheme_PublicK
*/
ElGamal_PublicKey(const AlgorithmIdentifier& alg_id,
const std::vector<uint8_t>& key_bits) :
- DL_Scheme_PublicKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
+ DL_Scheme_PublicKey(alg_id, key_bits, DL_Group_Format::ANSI_X9_42)
{}
/**