diff options
-rw-r--r-- | src/lib/pubkey/sm2/sm2.cpp | 44 | ||||
-rw-r--r-- | src/tests/data/pubkey/sm2_sig.vec | 1 | ||||
-rw-r--r-- | src/tests/test_sm2.cpp | 4 |
3 files changed, 40 insertions, 9 deletions
diff --git a/src/lib/pubkey/sm2/sm2.cpp b/src/lib/pubkey/sm2/sm2.cpp index 2882cb0ad..dbb22ca6d 100644 --- a/src/lib/pubkey/sm2/sm2.cpp +++ b/src/lib/pubkey/sm2/sm2.cpp @@ -1,5 +1,5 @@ /* -* SM2 +* SM2 Signatures * (C) 2017 Ribose Inc * * Botan is released under the Simplified BSD License (see license.txt) @@ -79,13 +79,14 @@ class SM2_Signature_Operation : public PK_Ops::Signature public: SM2_Signature_Operation(const SM2_Signature_PrivateKey& sm2, - const std::string& ident) : + const std::string& ident, + const std::string& hash) : m_order(sm2.domain().get_order()), m_base_point(sm2.domain().get_base_point(), m_order), m_x(sm2.private_value()), m_da_inv(sm2.get_da_inv()), m_mod_order(m_order), - m_hash(HashFunction::create_or_throw("SM3")) + m_hash(HashFunction::create_or_throw(hash)) { // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA) m_za = sm2_compute_za(*m_hash, ident, sm2.domain(), sm2.public_point()); @@ -134,12 +135,13 @@ class SM2_Verification_Operation : public PK_Ops::Verification { public: SM2_Verification_Operation(const SM2_Signature_PublicKey& sm2, - const std::string& ident) : + const std::string& ident, + const std::string& hash) : m_base_point(sm2.domain().get_base_point()), m_public_point(sm2.public_point()), m_order(sm2.domain().get_order()), m_mod_order(m_order), - m_hash(HashFunction::create_or_throw("SM3")) + m_hash(HashFunction::create_or_throw(hash)) { // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA) m_za = sm2_compute_za(*m_hash, ident, sm2.domain(), sm2.public_point()); @@ -199,7 +201,21 @@ SM2_Signature_PublicKey::create_verification_op(const std::string& params, const std::string& provider) const { if(provider == "base" || provider.empty()) - return std::unique_ptr<PK_Ops::Verification>(new SM2_Verification_Operation(*this, params)); + { + std::string userid = ""; + std::string hash = "SM3"; + + auto comma = params.find(','); + if(comma == std::string::npos) + userid = params; + else + { + userid = params.substr(0, comma); + hash = params.substr(comma+1, std::string::npos); + } + + return std::unique_ptr<PK_Ops::Verification>(new SM2_Verification_Operation(*this, userid, hash)); + } throw Provider_Not_Found(algo_name(), provider); } @@ -210,7 +226,21 @@ SM2_Signature_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/, const std::string& provider) const { if(provider == "base" || provider.empty()) - return std::unique_ptr<PK_Ops::Signature>(new SM2_Signature_Operation(*this, params)); + { + std::string userid = ""; + std::string hash = "SM3"; + + auto comma = params.find(','); + if(comma == std::string::npos) + userid = params; + else + { + userid = params.substr(0, comma); + hash = params.substr(comma+1, std::string::npos); + } + + return std::unique_ptr<PK_Ops::Signature>(new SM2_Signature_Operation(*this, userid, hash)); + } throw Provider_Not_Found(algo_name(), provider); } diff --git a/src/tests/data/pubkey/sm2_sig.vec b/src/tests/data/pubkey/sm2_sig.vec index d3356cceb..8a86dfc7d 100644 --- a/src/tests/data/pubkey/sm2_sig.vec +++ b/src/tests/data/pubkey/sm2_sig.vec @@ -7,6 +7,7 @@ yG = 0x0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2 Order = 0x8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7 Cofactor = 1 +Hash = SM3 Ident = [email protected] Msg = 6D65737361676520646967657374 x = 0x128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263 diff --git a/src/tests/test_sm2.cpp b/src/tests/test_sm2.cpp index 7172605bf..0497ccedd 100644 --- a/src/tests/test_sm2.cpp +++ b/src/tests/test_sm2.cpp @@ -27,11 +27,11 @@ class SM2_Signature_KAT_Tests : public PK_Signature_Generation_Test "SM2", "pubkey/sm2_sig.vec", "P,A,B,xG,yG,Order,Cofactor,Ident,Msg,x,Nonce,Signature", - "") {} + "Hash") {} virtual std::string default_padding(const VarMap& vars) const override { - return get_req_str(vars, "Ident"); + return get_req_str(vars, "Ident") + "," + get_opt_str(vars, "Hash", "SM3"); } Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override |