aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-06-19 11:14:10 -0400
committerJack Lloyd <[email protected]>2018-06-19 11:14:10 -0400
commit2de5e91c986699038f2c743d894e1a699452689c (patch)
treebdbeba135339619f043bf03aee6a7b0566772141
parentfc5156247ac9152fbe6f20f2ab6d1b09a9751652 (diff)
Ensure that trying to add points from different groups fails.
Producing garbage instead is asking for trouble.
-rw-r--r--src/lib/pubkey/ec_group/curve_gfp.h29
-rw-r--r--src/lib/pubkey/ec_group/point_gfp.cpp3
-rw-r--r--src/tests/unit_ecc.cpp16
3 files changed, 35 insertions, 13 deletions
diff --git a/src/lib/pubkey/ec_group/curve_gfp.h b/src/lib/pubkey/ec_group/curve_gfp.h
index 865bb68f8..888f87d46 100644
--- a/src/lib/pubkey/ec_group/curve_gfp.h
+++ b/src/lib/pubkey/ec_group/curve_gfp.h
@@ -201,6 +201,22 @@ class BOTAN_UNSTABLE_API CurveGFp final
std::swap(m_repr, other.m_repr);
}
+ /**
+ * Equality operator
+ * @param lhs a curve
+ * @param rhs a curve
+ * @return true iff lhs is the same as rhs
+ */
+ inline bool operator==(const CurveGFp& other) const
+ {
+ if(m_repr.get() == other.m_repr.get())
+ return true;
+
+ return (get_p() == other.get_p()) &&
+ (get_a() == other.get_a()) &&
+ (get_b() == other.get_b());
+ }
+
private:
static std::shared_ptr<CurveGFp_Repr>
choose_repr(const BigInt& p, const BigInt& a, const BigInt& b);
@@ -208,19 +224,6 @@ class BOTAN_UNSTABLE_API CurveGFp final
std::shared_ptr<CurveGFp_Repr> m_repr;
};
-/**
-* Equality operator
-* @param lhs a curve
-* @param rhs a curve
-* @return true iff lhs is the same as rhs
-*/
-inline bool operator==(const CurveGFp& lhs, const CurveGFp& rhs)
- {
- return (lhs.get_p() == rhs.get_p()) &&
- (lhs.get_a() == rhs.get_a()) &&
- (lhs.get_b() == rhs.get_b());
- }
-
inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs)
{
return !(lhs == rhs);
diff --git a/src/lib/pubkey/ec_group/point_gfp.cpp b/src/lib/pubkey/ec_group/point_gfp.cpp
index 8f53bb079..b1c921a51 100644
--- a/src/lib/pubkey/ec_group/point_gfp.cpp
+++ b/src/lib/pubkey/ec_group/point_gfp.cpp
@@ -87,6 +87,7 @@ inline bool all_zeros(const word x[], size_t len)
void PointGFp::add_affine(const PointGFp& rhs, std::vector<BigInt>& workspace)
{
+ BOTAN_ASSERT_NOMSG(m_curve == rhs.m_curve);
BOTAN_DEBUG_ASSERT(rhs.is_affine());
const size_t p_words = m_curve.get_p_words();
@@ -180,6 +181,8 @@ void PointGFp::add_affine(const word x_words[], size_t x_size,
// Point addition
void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn)
{
+ BOTAN_ASSERT_NOMSG(m_curve == rhs.m_curve);
+
if(rhs.is_zero())
return;
diff --git a/src/tests/unit_ecc.cpp b/src/tests/unit_ecc.cpp
index c79307c10..32d521d41 100644
--- a/src/tests/unit_ecc.cpp
+++ b/src/tests/unit_ecc.cpp
@@ -520,6 +520,21 @@ Test::Result test_mult_point()
return result;
}
+Test::Result test_mixed_points()
+ {
+ Test::Result result("ECC Unit");
+
+ Botan::EC_Group secp256r1("secp256r1");
+ Botan::EC_Group secp384r1("secp384r1");
+
+ const Botan::PointGFp& G256 = secp256r1.get_base_point();
+ const Botan::PointGFp& G384 = secp384r1.get_base_point();
+
+ result.test_throws("Mixing points from different groups",
+ [&] { Botan::PointGFp p = G256 + G384; });
+ return result;
+ }
+
Test::Result test_basic_operations()
{
Test::Result result("ECC Unit");
@@ -678,6 +693,7 @@ class ECC_Unit_Tests final : public Test
results.push_back(test_point_mult());
results.push_back(test_point_negative());
results.push_back(test_mult_point());
+ results.push_back(test_mixed_points());
results.push_back(test_basic_operations());
results.push_back(test_enc_dec_compressed_160());
results.push_back(test_enc_dec_compressed_256());