Botan  2.11.0
Crypto and TLS for C++11
sm2.cpp
Go to the documentation of this file.
1 /*
2 * SM2 Signatures
3 * (C) 2017,2018 Ribose Inc
4 * (C) 2018 Jack Lloyd
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/sm2.h>
10 #include <botan/internal/pk_ops_impl.h>
11 #include <botan/internal/point_mul.h>
12 #include <botan/numthry.h>
13 #include <botan/keypair.h>
14 #include <botan/hash.h>
15 #include <botan/parsing.h>
16 
17 namespace Botan {
18 
19 std::string SM2_PublicKey::algo_name() const
20  {
21  return "SM2";
22  }
23 
25  bool strong) const
26  {
27  if(!public_point().on_the_curve())
28  return false;
29 
30  if(!strong)
31  return true;
32 
33  return KeyPair::signature_consistency_check(rng, *this, "user@example.com,SM3");
34  }
35 
37  const secure_vector<uint8_t>& key_bits) :
38  EC_PrivateKey(alg_id, key_bits)
39  {
40  m_da_inv = domain().inverse_mod_order(m_private_key + 1);
41  }
42 
44  const EC_Group& domain,
45  const BigInt& x) :
46  EC_PrivateKey(rng, domain, x)
47  {
48  m_da_inv = domain.inverse_mod_order(m_private_key + 1);
49  }
50 
51 std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
52  const std::string& user_id,
53  const EC_Group& domain,
54  const PointGFp& pubkey)
55  {
56  if(user_id.size() >= 8192)
57  throw Invalid_Argument("SM2 user id too long to represent");
58 
59  const uint16_t uid_len = static_cast<uint16_t>(8 * user_id.size());
60 
61  hash.update(get_byte(0, uid_len));
62  hash.update(get_byte(1, uid_len));
63  hash.update(user_id);
64 
65  const size_t p_bytes = domain.get_p_bytes();
66 
67  hash.update(BigInt::encode_1363(domain.get_a(), p_bytes));
68  hash.update(BigInt::encode_1363(domain.get_b(), p_bytes));
69  hash.update(BigInt::encode_1363(domain.get_g_x(), p_bytes));
70  hash.update(BigInt::encode_1363(domain.get_g_y(), p_bytes));
71  hash.update(BigInt::encode_1363(pubkey.get_affine_x(), p_bytes));
72  hash.update(BigInt::encode_1363(pubkey.get_affine_y(), p_bytes));
73 
74  std::vector<uint8_t> za(hash.output_length());
75  hash.final(za.data());
76 
77  return za;
78  }
79 
80 namespace {
81 
82 /**
83 * SM2 signature operation
84 */
85 class SM2_Signature_Operation final : public PK_Ops::Signature
86  {
87  public:
88 
89  SM2_Signature_Operation(const SM2_PrivateKey& sm2,
90  const std::string& ident,
91  const std::string& hash) :
92  m_group(sm2.domain()),
93  m_x(sm2.private_value()),
94  m_da_inv(sm2.get_da_inv())
95  {
96  if(hash == "Raw")
97  {
98  // m_hash is null, m_za is empty
99  }
100  else
101  {
103  // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
104  m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
105  m_hash->update(m_za);
106  }
107  }
108 
109  size_t signature_length() const override { return 2*m_group.get_order_bytes(); }
110 
111  void update(const uint8_t msg[], size_t msg_len) override
112  {
113  if(m_hash)
114  {
115  m_hash->update(msg, msg_len);
116  }
117  else
118  {
119  m_digest.insert(m_digest.end(), msg, msg + msg_len);
120  }
121  }
122 
123  secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
124 
125  private:
126  const EC_Group m_group;
127  const BigInt& m_x;
128  const BigInt& m_da_inv;
129 
130  std::vector<uint8_t> m_za;
131  secure_vector<uint8_t> m_digest;
132  std::unique_ptr<HashFunction> m_hash;
133  std::vector<BigInt> m_ws;
134  };
135 
136 secure_vector<uint8_t>
137 SM2_Signature_Operation::sign(RandomNumberGenerator& rng)
138  {
139  BigInt e;
140  if(m_hash)
141  {
142  e = BigInt::decode(m_hash->final());
143  // prepend ZA for next signature if any
144  m_hash->update(m_za);
145  }
146  else
147  {
148  e = BigInt::decode(m_digest);
149  m_digest.clear();
150  }
151 
152  const BigInt k = m_group.random_scalar(rng);
153 
154  const BigInt r = m_group.mod_order(
155  m_group.blinded_base_point_multiply_x(k, rng, m_ws) + e);
156  const BigInt s = m_group.multiply_mod_order(m_da_inv, m_group.mod_order(k - r*m_x));
157 
158  return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order().bytes());
159  }
160 
161 /**
162 * SM2 verification operation
163 */
164 class SM2_Verification_Operation final : public PK_Ops::Verification
165  {
166  public:
167  SM2_Verification_Operation(const SM2_PublicKey& sm2,
168  const std::string& ident,
169  const std::string& hash) :
170  m_group(sm2.domain()),
171  m_gy_mul(m_group.get_base_point(), sm2.public_point())
172  {
173  if(hash == "Raw")
174  {
175  // m_hash is null, m_za is empty
176  }
177  else
178  {
180  // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
181  m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
182  m_hash->update(m_za);
183  }
184  }
185 
186  void update(const uint8_t msg[], size_t msg_len) override
187  {
188  if(m_hash)
189  {
190  m_hash->update(msg, msg_len);
191  }
192  else
193  {
194  m_digest.insert(m_digest.end(), msg, msg + msg_len);
195  }
196  }
197 
198  bool is_valid_signature(const uint8_t sig[], size_t sig_len) override;
199  private:
200  const EC_Group m_group;
201  const PointGFp_Multi_Point_Precompute m_gy_mul;
202  secure_vector<uint8_t> m_digest;
203  std::vector<uint8_t> m_za;
204  std::unique_ptr<HashFunction> m_hash;
205  };
206 
207 bool SM2_Verification_Operation::is_valid_signature(const uint8_t sig[], size_t sig_len)
208  {
209  BigInt e;
210  if(m_hash)
211  {
212  e = BigInt::decode(m_hash->final());
213  // prepend ZA for next signature if any
214  m_hash->update(m_za);
215  }
216  else
217  {
218  e = BigInt::decode(m_digest);
219  m_digest.clear();
220  }
221 
222  if(sig_len != m_group.get_order().bytes()*2)
223  return false;
224 
225  const BigInt r(sig, sig_len / 2);
226  const BigInt s(sig + sig_len / 2, sig_len / 2);
227 
228  if(r <= 0 || r >= m_group.get_order() || s <= 0 || s >= m_group.get_order())
229  return false;
230 
231  const BigInt t = m_group.mod_order(r + s);
232 
233  if(t == 0)
234  return false;
235 
236  const PointGFp R = m_gy_mul.multi_exp(s, t);
237 
238  // ???
239  if(R.is_zero())
240  return false;
241 
242  return (m_group.mod_order(R.get_affine_x() + e) == r);
243  }
244 
245 void parse_sm2_param_string(const std::string& params,
246  std::string& userid,
247  std::string& hash)
248  {
249  // GM/T 0009-2012 specifies this as the default userid
250  const std::string default_userid = "1234567812345678";
251 
252  // defaults:
253  userid = default_userid;
254  hash = "SM3";
255 
256  /*
257  * SM2 parameters have the following possible formats:
258  * Ident [since 2.2.0]
259  * Ident,Hash [since 2.3.0]
260  */
261 
262  auto comma = params.find(',');
263  if(comma == std::string::npos)
264  {
265  userid = params;
266  }
267  else
268  {
269  userid = params.substr(0, comma);
270  hash = params.substr(comma+1, std::string::npos);
271  }
272  }
273 
274 }
275 
276 std::unique_ptr<PK_Ops::Verification>
277 SM2_PublicKey::create_verification_op(const std::string& params,
278  const std::string& provider) const
279  {
280  if(provider == "base" || provider.empty())
281  {
282  std::string userid, hash;
283  parse_sm2_param_string(params, userid, hash);
284  return std::unique_ptr<PK_Ops::Verification>(new SM2_Verification_Operation(*this, userid, hash));
285  }
286 
287  throw Provider_Not_Found(algo_name(), provider);
288  }
289 
290 std::unique_ptr<PK_Ops::Signature>
292  const std::string& params,
293  const std::string& provider) const
294  {
295  if(provider == "base" || provider.empty())
296  {
297  std::string userid, hash;
298  parse_sm2_param_string(params, userid, hash);
299  return std::unique_ptr<PK_Ops::Signature>(new SM2_Signature_Operation(*this, userid, hash));
300  }
301 
302  throw Provider_Not_Found(algo_name(), provider);
303  }
304 
305 }
BigInt m_private_key
Definition: ecc_key.h:167
size_t get_p_bytes() const
Definition: ec_group.cpp:444
PointGFp multi_exp(const BigInt &k1, const BigInt &k2) const
Definition: point_mul.cpp:373
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:359
const PointGFp & public_point() const
Definition: ecc_key.h:57
int(* final)(unsigned char *, CTX *)
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
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
SM2_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: sm2.cpp:36
BigInt get_affine_x() const
Definition: point_gfp.cpp:499
std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const override
Definition: sm2.cpp:277
BigInt get_affine_y() const
Definition: point_gfp.cpp:518
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:49
const EC_Group & domain() const
Definition: ecc_key.h:72
Definition: alg_id.cpp:13
std::string algo_name() const override
Definition: sm2.cpp:19
const BigInt & get_b() const
Definition: ec_group.cpp:469
const BigInt & get_g_x() const
Definition: ec_group.cpp:484
const BigInt & get_a() const
Definition: ec_group.cpp:464
const BigInt & get_g_y() const
Definition: ec_group.cpp:489
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: sm2.cpp:291
int(* update)(CTX *, const void *, CC_LONG len)
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: sm2.cpp:24
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:803
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:111
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
static secure_vector< uint8_t > encode_fixed_length_int_pair(const BigInt &n1, const BigInt &n2, size_t bytes)
Definition: big_code.cpp:133
MechanismType hash
BigInt inverse_mod_order(const BigInt &x) const
Definition: ec_group.cpp:519