diff options
author | Jack Lloyd <[email protected]> | 2018-01-31 16:44:28 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-01-31 16:44:28 -0500 |
commit | 1e926cb739a9fd430985f2a60b7a0fba1114c286 (patch) | |
tree | 3f8f6723e584a0f912723022f25439ef78bae537 | |
parent | e5b9ee2345affb56307070298ded9c2d5e1914be (diff) |
Avoid CurveGFp in EC_Group interface
-rw-r--r-- | src/lib/pubkey/ec_group/ec_group.cpp | 93 | ||||
-rw-r--r-- | src/lib/pubkey/ec_group/ec_group.h | 32 | ||||
-rw-r--r-- | src/lib/pubkey/ecc_key/info.txt | 1 | ||||
-rw-r--r-- | src/tests/test_ecies.cpp | 5 | ||||
-rw-r--r-- | src/tests/test_sm2.cpp | 8 | ||||
-rw-r--r-- | src/tests/unit_ecc.cpp | 2 | ||||
-rw-r--r-- | src/tests/unit_ecdsa.cpp | 28 |
7 files changed, 125 insertions, 44 deletions
diff --git a/src/lib/pubkey/ec_group/ec_group.cpp b/src/lib/pubkey/ec_group/ec_group.cpp index 8a3ffa718..978e59985 100644 --- a/src/lib/pubkey/ec_group/ec_group.cpp +++ b/src/lib/pubkey/ec_group/ec_group.cpp @@ -28,6 +28,49 @@ struct EC_Group_Data namespace { +std::shared_ptr<EC_Group_Data> new_EC_group_data(const BigInt& p, + const BigInt& a, + const BigInt& b, + const BigInt& g_x, + const BigInt& g_y, + const BigInt& order, + const BigInt& cofactor, + const OID& oid = OID()) + { + std::shared_ptr<EC_Group_Data> data = std::make_shared<EC_Group_Data>(); + + data->m_curve = CurveGFp(p, a, b); + data->m_base_point = PointGFp(data->m_curve, g_x, g_y); + data->m_order = order; + data->m_cofactor = cofactor; + data->m_oid = oid; + + data->m_p_bits = p.bits(); + data->m_p_bytes = p.bytes(); + return data; + } + +std::shared_ptr<EC_Group_Data> new_EC_group_data(const BigInt& p, + const BigInt& a, + const BigInt& b, + const std::vector<uint8_t>& base_point, + const BigInt& order, + const BigInt& cofactor, + const OID& oid = OID()) + { + std::shared_ptr<EC_Group_Data> data = std::make_shared<EC_Group_Data>(); + + data->m_curve = CurveGFp(p, a, b); + data->m_base_point = Botan::OS2ECP(base_point, data->m_curve); + data->m_order = order; + data->m_cofactor = cofactor; + data->m_oid = oid; + + data->m_p_bits = p.bits(); + data->m_p_bytes = p.bytes(); + return data; + } + std::shared_ptr<EC_Group_Data> lookup_EC_group_by_oid(const OID& oid); std::shared_ptr<EC_Group_Data> BER_decode_EC_group(const uint8_t bits[], size_t len) @@ -47,8 +90,7 @@ std::shared_ptr<EC_Group_Data> BER_decode_EC_group(const uint8_t bits[], size_t } else if(obj.type() == SEQUENCE) { - std::shared_ptr<EC_Group_Data> data = std::make_shared<EC_Group_Data>(); - BigInt p, a, b; + BigInt p, a, b, order, cofactor; std::vector<uint8_t> sv_base_point; BER_Decoder(bits, len) @@ -64,17 +106,12 @@ std::shared_ptr<EC_Group_Data> BER_decode_EC_group(const uint8_t bits[], size_t .decode_octet_string_bigint(b) .end_cons() .decode(sv_base_point, OCTET_STRING) - .decode(data->m_order) - .decode(data->m_cofactor) + .decode(order) + .decode(cofactor) .end_cons() .verify_end(); - data->m_curve = CurveGFp(p, a, b); - data->m_base_point = Botan::OS2ECP(sv_base_point, data->m_curve); - - data->m_p_bits = p.bits(); - data->m_p_bytes = p.bytes(); - return data; + return new_EC_group_data(p, a, b, sv_base_point, order, cofactor); } else { @@ -107,6 +144,15 @@ std::shared_ptr<EC_Group_Data> lookup_EC_group_by_oid(const OID& oid) } +EC_Group::EC_Group() + { + } + +EC_Group::~EC_Group() + { + // shared_ptr possibly freed here + } + EC_Group::EC_Group(const OID& domain_oid) { this->m_data = lookup_EC_group_by_oid(domain_oid); @@ -134,19 +180,30 @@ EC_Group::EC_Group(const std::string& str) } } +EC_Group::EC_Group(const BigInt& p, + const BigInt& a, + const BigInt& b, + const BigInt& base_x, + const BigInt& base_y, + const BigInt& order, + const BigInt& cofactor, + const OID& oid) + { + m_data = new_EC_group_data(p, a, b, base_x, base_y, order, cofactor, oid); + } + EC_Group::EC_Group(const CurveGFp& curve, const PointGFp& base_point, const BigInt& order, const BigInt& cofactor) { - m_data.reset(new EC_Group_Data); - - m_data->m_curve = curve; - m_data->m_base_point = base_point; - m_data->m_order = order; - m_data->m_cofactor = cofactor; - m_data->m_p_bits = curve.get_p().bits(); - m_data->m_p_bytes = curve.get_p().bytes(); + m_data = new_EC_group_data(curve.get_p(), + curve.get_a(), + curve.get_b(), + base_point.get_affine_x(), + base_point.get_affine_y(), + order, + cofactor); } EC_Group::EC_Group(const std::vector<uint8_t>& ber) diff --git a/src/lib/pubkey/ec_group/ec_group.h b/src/lib/pubkey/ec_group/ec_group.h index 3da38a7da..f1a3f8fff 100644 --- a/src/lib/pubkey/ec_group/ec_group.h +++ b/src/lib/pubkey/ec_group/ec_group.h @@ -43,12 +43,33 @@ class BOTAN_PUBLIC_API(2,0) EC_Group final * @param order the order of the base point * @param cofactor the cofactor */ + BOTAN_DEPRECATED("Use version taking all BigInts") EC_Group(const CurveGFp& curve, const PointGFp& base_point, const BigInt& order, const BigInt& cofactor); /** + * Construct Domain paramers from specified parameters + * @param p the elliptic curve p + * @param a the elliptic curve a param + * @param b the elliptic curve b param + * @param base_x the x coordinate of the base point + * @param base_y the y coordinate of the base point + * @param order the order of the base point + * @param cofactor the cofactor + * @param oid an optional OID used to identify this curve + */ + EC_Group(const BigInt& p, + const BigInt& a, + const BigInt& b, + const BigInt& base_x, + const BigInt& base_y, + const BigInt& order, + const BigInt& cofactor, + const OID& oid = OID()); + + /** * Decode a BER encoded ECC domain parameter set * @param ber_encoding the bytes of the BER encoding */ @@ -65,7 +86,14 @@ class BOTAN_PUBLIC_API(2,0) EC_Group final * from an OID name (eg "secp256r1", or "1.2.840.10045.3.1.7") * @param pem_or_oid PEM-encoded data, or an OID */ - explicit EC_Group(const std::string& pem_or_oid = ""); + explicit EC_Group(const std::string& pem_or_oid); + + /** + * Create an uninitialized EC_Group + */ + EC_Group(); + + ~EC_Group(); /** * Create the DER encoding of this domain @@ -154,7 +182,7 @@ class BOTAN_PUBLIC_API(2,0) EC_Group final PointGFp OS2ECP(const uint8_t bits[], size_t len) const; template<typename Alloc> - PointGFp OS2ECP(const std::vector<uint8_t, Alloc>& vec) const + PointGFp OS2ECP(const std::vector<uint8_t, Alloc>& vec) const { return this->OS2ECP(vec.data(), vec.size()); } diff --git a/src/lib/pubkey/ecc_key/info.txt b/src/lib/pubkey/ecc_key/info.txt index ac345a089..f46c9bb54 100644 --- a/src/lib/pubkey/ecc_key/info.txt +++ b/src/lib/pubkey/ecc_key/info.txt @@ -5,7 +5,6 @@ ECC_PUBLIC_KEY_CRYPTO -> 20131128 <requires> asn1 bigint -ec_gfp ec_group numbertheory </requires> diff --git a/src/tests/test_ecies.cpp b/src/tests/test_ecies.cpp index 5c5af0a41..2ce931bae 100644 --- a/src/tests/test_ecies.cpp +++ b/src/tests/test_ecies.cpp @@ -127,12 +127,11 @@ class ECIES_ISO_Tests final : public Text_Based_Test const std::vector<uint8_t> c0 = get_req_bin(vars, "C0"); // expected encoded (ephemeral) public key const std::vector<uint8_t> k = get_req_bin(vars, "K"); // expected derived secret - const Botan::CurveGFp curve(p, a, b); - const Botan::EC_Group domain(curve, Botan::PointGFp(curve, gx, gy), mu, nu); + const Botan::EC_Group domain(p, a, b, gx, gy, mu, nu); // keys of bob const Botan::ECDH_PrivateKey other_private_key(Test::rng(), domain, x); - const Botan::PointGFp other_public_key_point(curve, hx, hy); + const Botan::PointGFp other_public_key_point = domain.point(hx, hy); const Botan::ECDH_PublicKey other_public_key(domain, other_public_key_point); // (ephemeral) keys of alice diff --git a/src/tests/test_sm2.cpp b/src/tests/test_sm2.cpp index 16b82b792..c4ddf6ddf 100644 --- a/src/tests/test_sm2.cpp +++ b/src/tests/test_sm2.cpp @@ -51,9 +51,7 @@ class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test const BigInt cofactor = get_req_bn(vars, "Cofactor"); const BigInt x = get_req_bn(vars, "x"); - Botan::CurveGFp curve(p, a, b); - Botan::PointGFp base_point(curve, xG, yG); - Botan::EC_Group domain(curve, base_point, order, cofactor); + Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor); Botan::Null_RNG null_rng; std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Signature_PrivateKey(null_rng, domain, x)); @@ -97,9 +95,7 @@ class SM2_Encryption_KAT_Tests final : public PK_Encryption_Decryption_Test const BigInt cofactor = get_req_bn(vars, "Cofactor"); const BigInt x = get_req_bn(vars, "x"); - Botan::CurveGFp curve(p, a, b); - Botan::PointGFp base_point(curve, xG, yG); - Botan::EC_Group domain(curve, base_point, order, cofactor); + Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor); Botan::Null_RNG null_rng; std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Encryption_PrivateKey(null_rng, domain, x)); diff --git a/src/tests/unit_ecc.cpp b/src/tests/unit_ecc.cpp index f1eb3dc99..3d62e68f0 100644 --- a/src/tests/unit_ecc.cpp +++ b/src/tests/unit_ecc.cpp @@ -277,6 +277,8 @@ Test::Result test_groups() { const Botan::EC_Group group(group_name); result.confirm("EC_Group is known", !group.get_curve_oid().empty()); + result.test_eq("EC_Group has correct bit size", group.get_p().bits(), group.get_p_bits()); + result.test_eq("EC_Group has byte size", group.get_p().bytes(), group.get_p_bytes()); } return result; } diff --git a/src/tests/unit_ecdsa.cpp b/src/tests/unit_ecdsa.cpp index 5d137bacf..081b686e6 100644 --- a/src/tests/unit_ecdsa.cpp +++ b/src/tests/unit_ecdsa.cpp @@ -233,20 +233,20 @@ Test::Result test_unusual_curve() Test::Result result("ECDSA Unit"); //calc a curve which is not in the registry - const std::string G_secp_comp = - "04081523d03d4f12cd02879dea4bf6a4f3a7df26ed888f10c5b2235a1274c386a2f218300dee6ed217841164533bcdc903f07a096f9fbf4ee95bac098a111f296f5830fe5c35b3e344d5df3a2256985f64fbe6d0edcc4c61d18bef681dd399df3d0194c5a4315e012e0245ecea56365baa9e8be1f7"; - const Botan::BigInt - bi_p_secp("2117607112719756483104013348936480976596328609518055062007450442679169492999007105354629105748524349829824407773719892437896937279095106809"); - const Botan::BigInt - bi_a_secp("0x0a377dede6b523333d36c78e9b0eaa3bf48ce93041f6d4fc34014d08f6833807498deedd4290101c5866e8dfb589485d13357b9e78c2d7fbe9fe"); - const Botan::BigInt - bi_b_secp("0x0a9acf8c8ba617777e248509bcb4717d4db346202bf9e352cd5633731dd92a51b72a4dc3b3d17c823fcc8fbda4da08f25dea89046087342595a7"); - Botan::BigInt bi_order_g("0x0e1a16196e6000000000bc7f1618d867b15bb86474418f"); - Botan::CurveGFp curve(bi_p_secp, bi_a_secp, bi_b_secp); - Botan::PointGFp p_G = Botan::OS2ECP(Botan::hex_decode(G_secp_comp), curve); - - Botan::EC_Group dom_params(curve, p_G, bi_order_g, Botan::BigInt(1)); - if(!result.confirm("point is on curve", p_G.on_the_curve())) + const Botan::BigInt p("2117607112719756483104013348936480976596328609518055062007450442679169492999007105354629105748524349829824407773719892437896937279095106809"); + const Botan::BigInt a("0x0a377dede6b523333d36c78e9b0eaa3bf48ce93041f6d4fc34014d08f6833807498deedd4290101c5866e8dfb589485d13357b9e78c2d7fbe9fe"); + const Botan::BigInt b("0x0a9acf8c8ba617777e248509bcb4717d4db346202bf9e352cd5633731dd92a51b72a4dc3b3d17c823fcc8fbda4da08f25dea89046087342595a7"); + const Botan::BigInt order_g("0x0e1a16196e6000000000bc7f1618d867b15bb86474418f"); + const Botan::BigInt cofactor = 1; + + const BigInt Gx("1503931002566715881584977704503341991763310127581173321974500299341775226206001860606586625324214456299149080935147329869147994265934715820"); + const BigInt Gy("1774988776970033741491814582357926984496972046739476148938345272681378523636129776486407268230155403536112014267092770854858769258781598199"); + + Botan::EC_Group dom_params(p, a, b, Gx, Gy, order_g, cofactor); + + Botan::PointGFp p_G = dom_params.point(Gx, Gy); + + if(!result.confirm("G is on curve", p_G.on_the_curve())) { return result; } |