diff options
Diffstat (limited to 'src/lib/pubkey')
-rw-r--r-- | src/lib/pubkey/pk_algs.cpp | 22 | ||||
-rw-r--r-- | src/lib/pubkey/sm2/info.txt | 2 | ||||
-rw-r--r-- | src/lib/pubkey/sm2/sm2.cpp | 35 | ||||
-rw-r--r-- | src/lib/pubkey/sm2/sm2.h | 50 | ||||
-rw-r--r-- | src/lib/pubkey/sm2/sm2_enc.cpp | 40 | ||||
-rw-r--r-- | src/lib/pubkey/sm2/sm2_enc.h | 79 |
6 files changed, 71 insertions, 157 deletions
diff --git a/src/lib/pubkey/pk_algs.cpp b/src/lib/pubkey/pk_algs.cpp index 89ef7c708..9ea89c59f 100644 --- a/src/lib/pubkey/pk_algs.cpp +++ b/src/lib/pubkey/pk_algs.cpp @@ -71,7 +71,6 @@ #if defined(BOTAN_HAS_SM2) #include <botan/sm2.h> - #include <botan/sm2_enc.h> #endif #if defined(BOTAN_HAS_OPENSSL) @@ -152,10 +151,8 @@ load_public_key(const AlgorithmIdentifier& alg_id, #endif #if defined(BOTAN_HAS_SM2) - if(alg_name == "SM2_Sig") - return std::unique_ptr<Public_Key>(new SM2_Signature_PublicKey(alg_id, key_bits)); - if(alg_name == "SM2_Enc") - return std::unique_ptr<Public_Key>(new SM2_Encryption_PublicKey(alg_id, key_bits)); + if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") + return std::unique_ptr<Public_Key>(new SM2_PublicKey(alg_id, key_bits)); #endif #if defined(BOTAN_HAS_XMSS) @@ -230,10 +227,8 @@ load_private_key(const AlgorithmIdentifier& alg_id, #endif #if defined(BOTAN_HAS_SM2) - if(alg_name == "SM2_Sig") - return std::unique_ptr<Private_Key>(new SM2_Signature_PrivateKey(alg_id, key_bits)); - if(alg_name == "SM2_Enc") - return std::unique_ptr<Private_Key>(new SM2_Encryption_PrivateKey(alg_id, key_bits)); + if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") + return std::unique_ptr<Private_Key>(new SM2_PrivateKey(alg_id, key_bits)); #endif #if defined(BOTAN_HAS_ELGAMAL) @@ -255,7 +250,7 @@ namespace { std::string default_ec_group_for(const std::string& alg_name) { - if(alg_name == "SM2_Enc" || alg_name == "SM2_Sig") + if(alg_name == "SM2" || alg_name == "SM2_Enc" || alg_name == "SM2_Sig") return "sm2p256v1"; if(alg_name == "GOST-34.10") return "gost_256A"; @@ -341,6 +336,7 @@ create_private_key(const std::string& alg_name, alg_name == "ECDH" || alg_name == "ECKCDSA" || alg_name == "ECGDSA" || + alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc" || alg_name == "GOST-34.10") @@ -368,10 +364,8 @@ create_private_key(const std::string& alg_name, #endif #if defined(BOTAN_HAS_SM2) - if(alg_name == "SM2_Sig") - return std::unique_ptr<Private_Key>(new SM2_Signature_PrivateKey(rng, ec_group)); - if(alg_name == "SM2_Enc") - return std::unique_ptr<Private_Key>(new SM2_Encryption_PrivateKey(rng, ec_group)); + if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") + return std::unique_ptr<Private_Key>(new SM2_PrivateKey(rng, ec_group)); #endif #if defined(BOTAN_HAS_ECGDSA) diff --git a/src/lib/pubkey/sm2/info.txt b/src/lib/pubkey/sm2/info.txt index b4475525d..a3f756820 100644 --- a/src/lib/pubkey/sm2/info.txt +++ b/src/lib/pubkey/sm2/info.txt @@ -1,5 +1,5 @@ <defines> -SM2 -> 20170907 +SM2 -> 20180801 </defines> <requires> diff --git a/src/lib/pubkey/sm2/sm2.cpp b/src/lib/pubkey/sm2/sm2.cpp index 1096ea99f..c042693b3 100644 --- a/src/lib/pubkey/sm2/sm2.cpp +++ b/src/lib/pubkey/sm2/sm2.cpp @@ -15,8 +15,13 @@ namespace Botan { -bool SM2_Signature_PrivateKey::check_key(RandomNumberGenerator& rng, - bool strong) const +std::string SM2_PublicKey::algo_name() const + { + return "SM2"; + } + +bool SM2_PrivateKey::check_key(RandomNumberGenerator& rng, + bool strong) const { if(!public_point().on_the_curve()) return false; @@ -24,19 +29,19 @@ bool SM2_Signature_PrivateKey::check_key(RandomNumberGenerator& rng, if(!strong) return true; - return KeyPair::signature_consistency_check(rng, *this, "SM3"); + return KeyPair::signature_consistency_check(rng, *this, "[email protected],SM3"); } -SM2_Signature_PrivateKey::SM2_Signature_PrivateKey(const AlgorithmIdentifier& alg_id, - const secure_vector<uint8_t>& key_bits) : +SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, + const secure_vector<uint8_t>& key_bits) : EC_PrivateKey(alg_id, key_bits) { m_da_inv = domain().inverse_mod_order(m_private_key + 1); } -SM2_Signature_PrivateKey::SM2_Signature_PrivateKey(RandomNumberGenerator& rng, - const EC_Group& domain, - const BigInt& x) : +SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, + const EC_Group& domain, + const BigInt& x) : EC_PrivateKey(rng, domain, x) { m_da_inv = domain.inverse_mod_order(m_private_key + 1); @@ -80,7 +85,7 @@ class SM2_Signature_Operation final : public PK_Ops::Signature { public: - SM2_Signature_Operation(const SM2_Signature_PrivateKey& sm2, + SM2_Signature_Operation(const SM2_PrivateKey& sm2, const std::string& ident, const std::string& hash) : m_group(sm2.domain()), @@ -133,7 +138,7 @@ SM2_Signature_Operation::sign(RandomNumberGenerator& rng) class SM2_Verification_Operation final : public PK_Ops::Verification { public: - SM2_Verification_Operation(const SM2_Signature_PublicKey& sm2, + SM2_Verification_Operation(const SM2_PublicKey& sm2, const std::string& ident, const std::string& hash) : m_group(sm2.domain()), @@ -191,8 +196,8 @@ bool SM2_Verification_Operation::is_valid_signature(const uint8_t sig[], size_t } std::unique_ptr<PK_Ops::Verification> -SM2_Signature_PublicKey::create_verification_op(const std::string& params, - const std::string& provider) const +SM2_PublicKey::create_verification_op(const std::string& params, + const std::string& provider) const { if(provider == "base" || provider.empty()) { @@ -221,9 +226,9 @@ SM2_Signature_PublicKey::create_verification_op(const std::string& params, } std::unique_ptr<PK_Ops::Signature> -SM2_Signature_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/, - const std::string& params, - const std::string& provider) const +SM2_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/, + const std::string& params, + const std::string& provider) const { if(provider == "base" || provider.empty()) { diff --git a/src/lib/pubkey/sm2/sm2.h b/src/lib/pubkey/sm2/sm2.h index c6895cd6a..7b5f38858 100644 --- a/src/lib/pubkey/sm2/sm2.h +++ b/src/lib/pubkey/sm2/sm2.h @@ -13,9 +13,9 @@ namespace Botan { /** -* This class represents SM2 Signature public keys +* This class represents SM2 public keys */ -class BOTAN_PUBLIC_API(2,2) SM2_Signature_PublicKey : public virtual EC_PublicKey +class BOTAN_PUBLIC_API(2,2) SM2_PublicKey : public virtual EC_PublicKey { public: @@ -24,8 +24,8 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PublicKey : public virtual EC_PublicKe * @param dom_par the domain parameters associated with this key * @param public_point the public point defining this key */ - SM2_Signature_PublicKey(const EC_Group& dom_par, - const PointGFp& public_point) : + SM2_PublicKey(const EC_Group& dom_par, + const PointGFp& public_point) : EC_PublicKey(dom_par, public_point) {} /** @@ -33,15 +33,15 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PublicKey : public virtual EC_PublicKe * @param alg_id the X.509 algorithm identifier * @param key_bits DER encoded public key bits */ - SM2_Signature_PublicKey(const AlgorithmIdentifier& alg_id, - const std::vector<uint8_t>& key_bits) : + SM2_PublicKey(const AlgorithmIdentifier& alg_id, + const std::vector<uint8_t>& key_bits) : EC_PublicKey(alg_id, key_bits) {} /** * Get this keys algorithm name. * @result this keys algorithm name */ - std::string algo_name() const override { return "SM2_Sig"; } + std::string algo_name() const override; size_t message_parts() const override { return 2; } @@ -51,15 +51,21 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PublicKey : public virtual EC_PublicKe std::unique_ptr<PK_Ops::Verification> create_verification_op(const std::string& params, const std::string& provider) const override; + + std::unique_ptr<PK_Ops::Encryption> + create_encryption_op(RandomNumberGenerator& rng, + const std::string& params, + const std::string& provider) const override; + protected: - SM2_Signature_PublicKey() = default; + SM2_PublicKey() = default; }; /** -* This class represents SM2 Signature private keys +* This class represents SM2 private keys */ -class BOTAN_PUBLIC_API(2,2) SM2_Signature_PrivateKey final : - public SM2_Signature_PublicKey, public EC_PrivateKey +class BOTAN_PUBLIC_API(2,2) SM2_PrivateKey final : + public SM2_PublicKey, public EC_PrivateKey { public: @@ -68,8 +74,8 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PrivateKey final : * @param alg_id the X.509 algorithm identifier * @param key_bits ECPrivateKey bits */ - SM2_Signature_PrivateKey(const AlgorithmIdentifier& alg_id, - const secure_vector<uint8_t>& key_bits); + SM2_PrivateKey(const AlgorithmIdentifier& alg_id, + const secure_vector<uint8_t>& key_bits); /** * Create a private key. @@ -77,9 +83,9 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PrivateKey final : * @param domain parameters to used for this key * @param x the private key (if zero, generate a new random key) */ - SM2_Signature_PrivateKey(RandomNumberGenerator& rng, - const EC_Group& domain, - const BigInt& x = 0); + SM2_PrivateKey(RandomNumberGenerator& rng, + const EC_Group& domain, + const BigInt& x = 0); bool check_key(RandomNumberGenerator& rng, bool) const override; @@ -88,6 +94,11 @@ class BOTAN_PUBLIC_API(2,2) SM2_Signature_PrivateKey final : const std::string& params, const std::string& provider) const override; + std::unique_ptr<PK_Ops::Decryption> + create_decryption_op(RandomNumberGenerator& rng, + const std::string& params, + const std::string& provider) const override; + const BigInt& get_da_inv() const { return m_da_inv; } private: BigInt m_da_inv; @@ -101,6 +112,13 @@ BOTAN_PUBLIC_API(2,5) sm2_compute_za(HashFunction& hash, const EC_Group& domain, const PointGFp& pubkey); +// For compat with versions 2.2 - 2.7 +typedef SM2_PublicKey SM2_Signature_PublicKey; +typedef SM2_PublicKey SM2_Encryption_PublicKey; + +typedef SM2_PrivateKey SM2_Signature_PrivateKey; +typedef SM2_PrivateKey SM2_Encryption_PrivateKey; + } #endif diff --git a/src/lib/pubkey/sm2/sm2_enc.cpp b/src/lib/pubkey/sm2/sm2_enc.cpp index 4d27ec40b..3da4275b3 100644 --- a/src/lib/pubkey/sm2/sm2_enc.cpp +++ b/src/lib/pubkey/sm2/sm2_enc.cpp @@ -5,10 +5,9 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/sm2_enc.h> +#include <botan/sm2.h> #include <botan/internal/point_mul.h> #include <botan/pk_ops.h> -#include <botan/keypair.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/kdf.h> @@ -16,31 +15,6 @@ namespace Botan { -bool SM2_Encryption_PrivateKey::check_key(RandomNumberGenerator& rng, - bool strong) const - { - if(!public_point().on_the_curve()) - return false; - - if(!strong) - return true; - - return KeyPair::encryption_consistency_check(rng, *this, "SM3"); - } - -SM2_Encryption_PrivateKey::SM2_Encryption_PrivateKey(const AlgorithmIdentifier& alg_id, - const secure_vector<uint8_t>& key_bits) : - EC_PrivateKey(alg_id, key_bits) - { - } - -SM2_Encryption_PrivateKey::SM2_Encryption_PrivateKey(RandomNumberGenerator& rng, - const EC_Group& domain, - const BigInt& x) : - EC_PrivateKey(rng, domain, x) - { - } - namespace { class SM2_Encryption_Operation final : public PK_Ops::Encryption @@ -233,9 +207,9 @@ class SM2_Decryption_Operation final : public PK_Ops::Decryption } std::unique_ptr<PK_Ops::Encryption> -SM2_Encryption_PublicKey::create_encryption_op(RandomNumberGenerator& rng, - const std::string& params, - const std::string& provider) const +SM2_PublicKey::create_encryption_op(RandomNumberGenerator& rng, + const std::string& params, + const std::string& provider) const { if(provider == "base" || provider.empty()) { @@ -247,9 +221,9 @@ SM2_Encryption_PublicKey::create_encryption_op(RandomNumberGenerator& rng, } std::unique_ptr<PK_Ops::Decryption> -SM2_Encryption_PrivateKey::create_decryption_op(RandomNumberGenerator& rng, - const std::string& params, - const std::string& provider) const +SM2_PrivateKey::create_decryption_op(RandomNumberGenerator& rng, + const std::string& params, + const std::string& provider) const { if(provider == "base" || provider.empty()) { diff --git a/src/lib/pubkey/sm2/sm2_enc.h b/src/lib/pubkey/sm2/sm2_enc.h index cc8d2cacb..1384d3726 100644 --- a/src/lib/pubkey/sm2/sm2_enc.h +++ b/src/lib/pubkey/sm2/sm2_enc.h @@ -8,83 +8,6 @@ #ifndef BOTAN_SM2_ENC_KEY_H_ #define BOTAN_SM2_ENC_KEY_H_ -#include <botan/ecc_key.h> - -namespace Botan { - -/** -* This class represents a public key used for SM2 encryption -*/ -class BOTAN_PUBLIC_API(2,2) SM2_Encryption_PublicKey : public virtual EC_PublicKey - { - public: - - /** - * Create a public key from a given public point. - * @param dom_par the domain parameters associated with this key - * @param public_point the public point defining this key - */ - SM2_Encryption_PublicKey(const EC_Group& dom_par, - const PointGFp& public_point) : - EC_PublicKey(dom_par, public_point) {} - - /** - * Load a public key. - * @param alg_id the X.509 algorithm identifier - * @param key_bits DER encoded public key bits - */ - SM2_Encryption_PublicKey(const AlgorithmIdentifier& alg_id, - const std::vector<uint8_t>& key_bits) : - EC_PublicKey(alg_id, key_bits) {} - - /** - * Get this keys algorithm name. - * @result this keys algorithm name - */ - std::string algo_name() const override { return "SM2_Enc"; } - - std::unique_ptr<PK_Ops::Encryption> - create_encryption_op(RandomNumberGenerator& rng, - const std::string& params, - const std::string& provider) const override; - protected: - SM2_Encryption_PublicKey() = default; - }; - -/** -* This class represents a private key used for SM2 encryption -*/ -class BOTAN_PUBLIC_API(2,2) SM2_Encryption_PrivateKey final : - public SM2_Encryption_PublicKey, public EC_PrivateKey - { - public: - - /** - * Load a private key - * @param alg_id the X.509 algorithm identifier - * @param key_bits ECPrivateKey bits - */ - SM2_Encryption_PrivateKey(const AlgorithmIdentifier& alg_id, - const secure_vector<uint8_t>& key_bits); - - /** - * Create a private key. - * @param rng a random number generator - * @param domain parameters to used for this key - * @param x the private key (if zero, generate a new random key) - */ - SM2_Encryption_PrivateKey(RandomNumberGenerator& rng, - const EC_Group& domain, - const BigInt& x = 0); - - bool check_key(RandomNumberGenerator& rng, bool) const override; - - std::unique_ptr<PK_Ops::Decryption> - create_decryption_op(RandomNumberGenerator& rng, - const std::string& params, - const std::string& provider) const override; - }; - -} +#include <botan/sm2.h> #endif |