Botan  2.11.0
Crypto and TLS for C++11
sm2.h
Go to the documentation of this file.
1 /*
2 * SM2
3 * (C) 2017 Ribose Inc
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_SM2_KEY_H_
9 #define BOTAN_SM2_KEY_H_
10 
11 #include <botan/ecc_key.h>
12 
13 namespace Botan {
14 
15 /**
16 * This class represents SM2 public keys
17 */
18 class BOTAN_PUBLIC_API(2,2) SM2_PublicKey : public virtual EC_PublicKey
19  {
20  public:
21 
22  /**
23  * Create a public key from a given public point.
24  * @param dom_par the domain parameters associated with this key
25  * @param public_point the public point defining this key
26  */
27  SM2_PublicKey(const EC_Group& dom_par,
28  const PointGFp& public_point) :
29  EC_PublicKey(dom_par, public_point) {}
30 
31  /**
32  * Load a public key.
33  * @param alg_id the X.509 algorithm identifier
34  * @param key_bits DER encoded public key bits
35  */
36  SM2_PublicKey(const AlgorithmIdentifier& alg_id,
37  const std::vector<uint8_t>& key_bits) :
38  EC_PublicKey(alg_id, key_bits) {}
39 
40  /**
41  * Get this keys algorithm name.
42  * @result this keys algorithm name
43  */
44  std::string algo_name() const override;
45 
46  size_t message_parts() const override { return 2; }
47 
48  size_t message_part_size() const override
49  { return domain().get_order().bytes(); }
50 
51  std::unique_ptr<PK_Ops::Verification>
52  create_verification_op(const std::string& params,
53  const std::string& provider) const override;
54 
55  std::unique_ptr<PK_Ops::Encryption>
56  create_encryption_op(RandomNumberGenerator& rng,
57  const std::string& params,
58  const std::string& provider) const override;
59 
60  protected:
61  SM2_PublicKey() = default;
62  };
63 
64 /**
65 * This class represents SM2 private keys
66 */
67 class BOTAN_PUBLIC_API(2,2) SM2_PrivateKey final :
68  public SM2_PublicKey, public EC_PrivateKey
69  {
70  public:
71 
72  /**
73  * Load a private key
74  * @param alg_id the X.509 algorithm identifier
75  * @param key_bits ECPrivateKey bits
76  */
77  SM2_PrivateKey(const AlgorithmIdentifier& alg_id,
78  const secure_vector<uint8_t>& key_bits);
79 
80  /**
81  * Create a private key.
82  * @param rng a random number generator
83  * @param domain parameters to used for this key
84  * @param x the private key (if zero, generate a new random key)
85  */
86  SM2_PrivateKey(RandomNumberGenerator& rng,
87  const EC_Group& domain,
88  const BigInt& x = 0);
89 
90  bool check_key(RandomNumberGenerator& rng, bool) const override;
91 
92  std::unique_ptr<PK_Ops::Signature>
93  create_signature_op(RandomNumberGenerator& rng,
94  const std::string& params,
95  const std::string& provider) const override;
96 
97  std::unique_ptr<PK_Ops::Decryption>
98  create_decryption_op(RandomNumberGenerator& rng,
99  const std::string& params,
100  const std::string& provider) const override;
101 
102  const BigInt& get_da_inv() const { return m_da_inv; }
103  private:
104  BigInt m_da_inv;
105  };
106 
107 class HashFunction;
108 
109 std::vector<uint8_t>
110 BOTAN_PUBLIC_API(2,5) sm2_compute_za(HashFunction& hash,
111  const std::string& user_id,
112  const EC_Group& domain,
113  const PointGFp& pubkey);
114 
115 // For compat with versions 2.2 - 2.7
116 typedef SM2_PublicKey SM2_Signature_PublicKey;
117 typedef SM2_PublicKey SM2_Encryption_PublicKey;
118 
119 typedef SM2_PrivateKey SM2_Signature_PrivateKey;
120 typedef SM2_PrivateKey SM2_Encryption_PrivateKey;
121 
122 }
123 
124 #endif
BigInt const BigInt & x
Definition: numthry.h:139
SM2_PublicKey SM2_Encryption_PublicKey
Definition: sm2.h:117
bool RandomNumberGenerator & rng
Definition: numthry.h:176
std::vector< uint8_t > const std::string const EC_Group & domain
Definition: sm2.h:111
int(* final)(unsigned char *, CTX *)
Definition: bigint.h:1136
SM2_PublicKey SM2_Signature_PublicKey
Definition: sm2.h:116
std::vector< uint8_t > const std::string const EC_Group const PointGFp & pubkey
Definition: sm2.h:111
const char * algo_name
Definition: ffi.h:940
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, const std::string &user_id, const EC_Group &domain, const PointGFp &pubkey)
Definition: sm2.cpp:51
Definition: alg_id.cpp:13
std::vector< uint8_t > const std::string & user_id
Definition: sm2.h:111
SM2_PrivateKey SM2_Signature_PrivateKey
Definition: sm2.h:119
class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final bool BOTAN_PUBLIC_API(2, 0) operator
Name Constraints.
Definition: asn1_obj.h:66
SM2_PrivateKey SM2_Encryption_PrivateKey
Definition: sm2.h:120
RandomNumberGenerator const std::string AlgorithmIdentifier & alg_id
Definition: x509_ca.h:230
bool size_t const std::string & hash
Definition: argon2.h:105
secure_vector< uint8_t > const std::string const std::vector< uint8_t > & params
Definition: pbes2.h:80