• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.49
/src/lib/pubkey/sm2/sm2.cpp
1
/*
2
* SM2 Signatures
3
* (C) 2017,2018 Ribose Inc
4
* (C) 2018,2024 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/sm2.h>
10

11
#include <botan/ec_group.h>
12
#include <botan/hash.h>
13
#include <botan/internal/keypair.h>
14
#include <botan/internal/loadstor.h>
15
#include <botan/internal/pk_ops_impl.h>
16

17
namespace Botan {
18

19
std::string SM2_PublicKey::algo_name() const {
207✔
20
   return "SM2";
207✔
21
}
22

23
std::optional<size_t> SM2_PublicKey::_signature_element_size_for_DER_encoding() const {
87✔
24
   return domain().get_order_bytes();
87✔
25
}
26

27
std::unique_ptr<Public_Key> SM2_PrivateKey::public_key() const {
24✔
28
   return std::make_unique<SM2_Signature_PublicKey>(domain(), _public_ec_point());
24✔
29
}
30

31
bool SM2_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
4✔
32
   if(!EC_PrivateKey::check_key(rng, strong)) {
4✔
33
      return false;
34
   }
35

36
   // SM2 has an oddity in private key generation when compared to
37
   // other EC*DSA style signature algorithms described in ISO14888-3:
38
   // the private key x MUST be in [0, q-1) instead of [0, q).
39
   //
40
   // The lower bound is already checked by the default impl
41
   if(private_value() >= domain().get_order() - 1) {
4✔
42
      return false;
43
   }
44

45
   if(!strong) {
4✔
46
      return true;
47
   }
48

49
   return KeyPair::signature_consistency_check(rng, *this, "user@example.com,SM3");
2✔
50
}
51

52
SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
20✔
53
      EC_PrivateKey(alg_id, key_bits),
54
      m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
20✔
55
      m_da_inv_legacy(m_da_inv.to_bigint()) {}
40✔
56

57
SM2_PrivateKey::SM2_PrivateKey(const EC_Group& group, const EC_Scalar& x) :
×
58
      EC_PrivateKey(group, x),
59
      m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
×
60
      m_da_inv_legacy(m_da_inv.to_bigint()) {}
×
61

62
SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group) :
10✔
63
      EC_PrivateKey(rng, group),
64
      m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
10✔
65
      m_da_inv_legacy(m_da_inv.to_bigint()) {}
20✔
66

67
SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, const BigInt& x) :
15✔
68
      EC_PrivateKey(rng, group, x),
69
      m_da_inv((this->_private_key() + EC_Scalar::one(domain())).invert()),
15✔
70
      m_da_inv_legacy(m_da_inv.to_bigint()) {}
30✔
71

72
std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
64✔
73
                                    std::string_view user_id,
74
                                    const EC_Group& group,
75
                                    const EC_AffinePoint& pubkey) {
76
   if(user_id.size() >= 8192) {
64✔
77
      throw Invalid_Argument("SM2 user id too long to represent");
×
78
   }
79

80
   const uint16_t uid_len = static_cast<uint16_t>(8 * user_id.size());
64✔
81

82
   hash.update(get_byte<0>(uid_len));
64✔
83
   hash.update(get_byte<1>(uid_len));
64✔
84
   hash.update(user_id);
64✔
85

86
   const size_t p_bytes = group.get_p_bytes();
64✔
87

88
   hash.update(group.get_a().serialize(p_bytes));
64✔
89
   hash.update(group.get_b().serialize(p_bytes));
64✔
90
   hash.update(group.get_g_x().serialize(p_bytes));
128✔
91
   hash.update(group.get_g_y().serialize(p_bytes));
64✔
92
   hash.update(pubkey.xy_bytes());
64✔
93

94
   return hash.final<std::vector<uint8_t>>();
64✔
95
}
96

97
namespace {
98

99
/**
100
* SM2 signature operation
101
*/
102
class SM2_Signature_Operation final : public PK_Ops::Signature {
×
103
   public:
104
      SM2_Signature_Operation(const SM2_PrivateKey& sm2, std::string_view ident, std::string_view hash) :
33✔
105
            m_group(sm2.domain()), m_x(sm2._private_key()), m_da_inv(sm2._get_da_inv()) {
33✔
106
         if(hash == "Raw") {
35✔
107
            // m_hash is null, m_za is empty
108
         } else {
109
            m_hash = HashFunction::create_or_throw(hash);
31✔
110
            // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
111
            m_za = sm2_compute_za(*m_hash, ident, m_group, sm2._public_ec_point());
31✔
112
            m_hash->update(m_za);
31✔
113
         }
114
      }
33✔
115

116
      size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
28✔
117

118
      void update(std::span<const uint8_t> input) override {
44✔
119
         if(m_hash) {
44✔
120
            m_hash->update(input);
42✔
121
         } else {
122
            m_digest.insert(m_digest.end(), input.begin(), input.end());
2✔
123
         }
124
      }
44✔
125

126
      std::vector<uint8_t> sign(RandomNumberGenerator& rng) override;
127

128
      std::string hash_function() const override { return m_hash ? m_hash->name() : "Raw"; }
4✔
129

130
   private:
131
      const EC_Group m_group;
132
      const EC_Scalar m_x;
133
      const EC_Scalar m_da_inv;
134

135
      std::vector<uint8_t> m_za;
136
      secure_vector<uint8_t> m_digest;
137
      std::unique_ptr<HashFunction> m_hash;
138
};
139

140
std::vector<uint8_t> SM2_Signature_Operation::sign(RandomNumberGenerator& rng) {
39✔
141
   const auto e = [&]() {
117✔
142
      if(m_hash) {
39✔
143
         auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_hash->final());
37✔
144
         // prepend ZA for next signature if any
145
         m_hash->update(m_za);
37✔
146
         return ie;
37✔
147
      } else {
37✔
148
         auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_digest);
2✔
149
         m_digest.clear();
2✔
150
         return ie;
2✔
151
      }
2✔
152
   }();
39✔
153

154
   const auto k = EC_Scalar::random(m_group, rng);
39✔
155

156
   const auto r = EC_Scalar::gk_x_mod_order(k, rng) + e;
78✔
157
   const auto s = (k - r * m_x) * m_da_inv;
39✔
158

159
   return EC_Scalar::serialize_pair(r, s);
78✔
160
}
39✔
161

162
/**
163
* SM2 verification operation
164
*/
165
class SM2_Verification_Operation final : public PK_Ops::Verification {
×
166
   public:
167
      SM2_Verification_Operation(const SM2_PublicKey& sm2, std::string_view ident, std::string_view hash) :
33✔
168
            m_group(sm2.domain()), m_gy_mul(sm2._public_ec_point()) {
33✔
169
         if(hash == "Raw") {
35✔
170
            // m_hash is null, m_za is empty
171
         } else {
172
            m_hash = HashFunction::create_or_throw(hash);
31✔
173
            // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
174
            m_za = sm2_compute_za(*m_hash, ident, m_group, sm2._public_ec_point());
31✔
175
            m_hash->update(m_za);
31✔
176
         }
177
      }
33✔
178

179
      void update(std::span<const uint8_t> input) override {
196✔
180
         if(m_hash) {
196✔
181
            m_hash->update(input);
172✔
182
         } else {
183
            m_digest.insert(m_digest.end(), input.begin(), input.end());
24✔
184
         }
185
      }
196✔
186

187
      bool is_valid_signature(std::span<const uint8_t> sig) override;
188

189
      std::string hash_function() const override { return m_hash ? m_hash->name() : "Raw"; }
4✔
190

191
   private:
192
      const EC_Group m_group;
193
      const EC_Group::Mul2Table m_gy_mul;
194
      secure_vector<uint8_t> m_digest;
195
      std::vector<uint8_t> m_za;
196
      std::unique_ptr<HashFunction> m_hash;
197
};
198

199
bool SM2_Verification_Operation::is_valid_signature(std::span<const uint8_t> sig) {
211✔
200
   const auto e = [&]() {
633✔
201
      if(m_hash) {
211✔
202
         auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_hash->final());
187✔
203
         // prepend ZA for next signature if any
204
         m_hash->update(m_za);
187✔
205
         return ie;
187✔
206
      } else {
187✔
207
         auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_digest);
24✔
208
         m_digest.clear();
24✔
209
         return ie;
24✔
210
      }
24✔
211
   }();
211✔
212

213
   if(auto rs = EC_Scalar::deserialize_pair(m_group, sig)) {
211✔
214
      const auto& [r, s] = rs.value();
190✔
215

216
      if(r.is_nonzero() && s.is_nonzero()) {
377✔
217
         const auto t = r + s;
187✔
218
         if(t.is_nonzero()) {
187✔
219
            // Check if r - e = x_coord(g*s + y*t) % n
220
            return m_gy_mul.mul2_vartime_x_mod_order_eq(r - e, s, t);
187✔
221
         }
222
      }
187✔
223
   }
187✔
224
   return false;
24✔
225
}
211✔
226

227
void parse_sm2_param_string(std::string_view params, std::string& userid, std::string& hash) {
66✔
228
   // GM/T 0009-2012 specifies this as the default userid
229
   const std::string default_userid = "1234567812345678";
66✔
230

231
   // defaults:
232
   userid = default_userid;
66✔
233
   hash = "SM3";
66✔
234

235
   /*
236
   * SM2 parameters have the following possible formats:
237
   * Ident [since 2.2.0]
238
   * Ident,Hash [since 2.3.0]
239
   */
240

241
   auto comma = params.find(',');
66✔
242
   if(comma == std::string::npos) {
66✔
243
      userid = params;
110✔
244
   } else {
245
      userid = params.substr(0, comma);
22✔
246
      hash = params.substr(comma + 1, std::string::npos);
44✔
247
   }
248
}
66✔
249

250
}  // namespace
251

252
std::unique_ptr<Private_Key> SM2_PublicKey::generate_another(RandomNumberGenerator& rng) const {
2✔
253
   return std::make_unique<SM2_PrivateKey>(rng, domain());
4✔
254
}
255

256
std::unique_ptr<PK_Ops::Verification> SM2_PublicKey::create_verification_op(std::string_view params,
54✔
257
                                                                            std::string_view provider) const {
258
   if(provider == "base" || provider.empty()) {
63✔
259
      std::string userid;
33✔
260
      std::string hash;
33✔
261
      parse_sm2_param_string(params, userid, hash);
33✔
262
      return std::make_unique<SM2_Verification_Operation>(*this, userid, hash);
33✔
263
   }
33✔
264

265
   throw Provider_Not_Found(algo_name(), provider);
42✔
266
}
267

268
std::unique_ptr<PK_Ops::Signature> SM2_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
54✔
269
                                                                       std::string_view params,
270
                                                                       std::string_view provider) const {
271
   if(provider == "base" || provider.empty()) {
63✔
272
      std::string userid;
33✔
273
      std::string hash;
33✔
274
      parse_sm2_param_string(params, userid, hash);
33✔
275
      return std::make_unique<SM2_Signature_Operation>(*this, userid, hash);
33✔
276
   }
33✔
277

278
   throw Provider_Not_Found(algo_name(), provider);
42✔
279
}
280

281
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc