From 09d213dead4d3519bbd9aa8083e8c784a4eb9c4f Mon Sep 17 00:00:00 2001 From: Never Date: Fri, 24 Feb 2017 17:52:13 +0100 Subject: Add ec_group verify function --- src/lib/pubkey/ec_group/ec_group.cpp | 41 ++++++++++++++++++++++++++++++++++++ src/lib/pubkey/ec_group/ec_group.h | 9 +++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/pubkey/ec_group/ec_group.cpp b/src/lib/pubkey/ec_group/ec_group.cpp index cbc628195..e8a9672ab 100644 --- a/src/lib/pubkey/ec_group/ec_group.cpp +++ b/src/lib/pubkey/ec_group/ec_group.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Botan { @@ -130,4 +131,44 @@ std::string EC_Group::PEM_encode() const return PEM_Code::encode(der, "EC PARAMETERS"); } +bool EC_Group::verify_group(RandomNumberGenerator& rng, + bool) const + { + //compute the discriminant + Modular_Reducer p(m_curve.get_p()); + BigInt discriminant = p.multiply(4, m_curve.get_a()); + discriminant += p.multiply(27, m_curve.get_b()); + discriminant = p.reduce(discriminant); + //check the discriminant + if(discriminant == 0) + { + return false; + } + //check for valid cofactor + if(m_cofactor < 1) + { + return false; + } + //check if the base point is on the curve + if(!m_base_point.on_the_curve()) + { + return false; + } + if((m_base_point * m_cofactor).is_zero()) + { + return false; + } + //check if order is prime + if(!is_prime(m_order, rng, 128)) + { + return false; + } + //check if order of the base point is correct + if(!(m_base_point * m_order).is_zero()) + { + return false; + } + return true; + } + } diff --git a/src/lib/pubkey/ec_group/ec_group.h b/src/lib/pubkey/ec_group/ec_group.h index 15c09a54d..a2cd4d719 100644 --- a/src/lib/pubkey/ec_group/ec_group.h +++ b/src/lib/pubkey/ec_group/ec_group.h @@ -113,7 +113,14 @@ class BOTAN_DLL EC_Group * @result the OID */ std::string get_oid() const { return m_oid; } - + + /** + * Verify EC_Group domain + * @returns true if group is valid. false otherwise + */ + bool verify_group(RandomNumberGenerator& rng, + bool strong = false) const; + bool operator==(const EC_Group& other) const { return ((get_curve() == other.get_curve()) && -- cgit v1.2.3