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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

94.83
/src/lib/pubkey/sm2/sm2.cpp
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

11
#include <botan/hash.h>
12
#include <botan/numthry.h>
13
#include <botan/internal/keypair.h>
14
#include <botan/internal/loadstor.h>
15
#include <botan/internal/parsing.h>
16
#include <botan/internal/pk_ops_impl.h>
17
#include <botan/internal/point_mul.h>
18

19
namespace Botan {
20

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

23
std::unique_ptr<Public_Key> SM2_PrivateKey::public_key() const {
5✔
24
   return std::make_unique<SM2_Signature_PublicKey>(domain(), public_point());
5✔
25
}
26

27
bool SM2_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
4✔
28
   if(!public_point().on_the_curve())
4✔
29
      return false;
30

31
   if(!strong)
4✔
32
      return true;
33

34
   return KeyPair::signature_consistency_check(rng, *this, "user@example.com,SM3");
2✔
35
}
36

37
SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
20✔
38
      EC_PrivateKey(alg_id, key_bits) {
20✔
39
   m_da_inv = domain().inverse_mod_order(m_private_key + 1);
40✔
40
}
20✔
41

42
SM2_PrivateKey::SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x) :
26✔
43
      EC_PrivateKey(rng, domain, x) {
26✔
44
   m_da_inv = domain.inverse_mod_order(m_private_key + 1);
52✔
45
}
26✔
46

47
std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
36✔
48
                                    std::string_view user_id,
49
                                    const EC_Group& domain,
50
                                    const EC_Point& pubkey) {
51
   if(user_id.size() >= 8192)
36✔
52
      throw Invalid_Argument("SM2 user id too long to represent");
×
53

54
   const uint16_t uid_len = static_cast<uint16_t>(8 * user_id.size());
36✔
55

56
   hash.update(get_byte<0>(uid_len));
36✔
57
   hash.update(get_byte<1>(uid_len));
36✔
58
   hash.update(user_id);
36✔
59

60
   const size_t p_bytes = domain.get_p_bytes();
36✔
61

62
   hash.update(BigInt::encode_1363(domain.get_a(), p_bytes));
36✔
63
   hash.update(BigInt::encode_1363(domain.get_b(), p_bytes));
36✔
64
   hash.update(BigInt::encode_1363(domain.get_g_x(), p_bytes));
36✔
65
   hash.update(BigInt::encode_1363(domain.get_g_y(), p_bytes));
36✔
66
   hash.update(BigInt::encode_1363(pubkey.get_affine_x(), p_bytes));
72✔
67
   hash.update(BigInt::encode_1363(pubkey.get_affine_y(), p_bytes));
72✔
68

69
   std::vector<uint8_t> za(hash.output_length());
36✔
70
   hash.final(za.data());
36✔
71

72
   return za;
36✔
73
}
×
74

75
namespace {
76

77
/**
78
* SM2 signature operation
79
*/
80
class SM2_Signature_Operation final : public PK_Ops::Signature {
×
81
   public:
82
      SM2_Signature_Operation(const SM2_PrivateKey& sm2, std::string_view ident, std::string_view hash) :
18✔
83
            m_group(sm2.domain()), m_x(sm2.private_value()), m_da_inv(sm2.get_da_inv()) {
18✔
84
         if(hash == "Raw") {
33✔
85
            // m_hash is null, m_za is empty
86
         } else {
87
            m_hash = HashFunction::create_or_throw(hash);
17✔
88
            // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
89
            m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
17✔
90
            m_hash->update(m_za);
17✔
91
         }
92
      }
18✔
93

94
      size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
10✔
95

96
      void update(const uint8_t msg[], size_t msg_len) override {
19✔
97
         if(m_hash) {
19✔
98
            m_hash->update(msg, msg_len);
18✔
99
         } else {
100
            m_digest.insert(m_digest.end(), msg, msg + msg_len);
1✔
101
         }
102
      }
19✔
103

104
      secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
105

106
      std::string hash_function() const override { return m_hash->name(); }
×
107

108
   private:
109
      const EC_Group m_group;
110
      const BigInt& m_x;
111
      const BigInt& m_da_inv;
112

113
      std::vector<uint8_t> m_za;
114
      secure_vector<uint8_t> m_digest;
115
      std::unique_ptr<HashFunction> m_hash;
116
      std::vector<BigInt> m_ws;
117
};
118

119
secure_vector<uint8_t> SM2_Signature_Operation::sign(RandomNumberGenerator& rng) {
20✔
120
   BigInt e;
20✔
121
   if(m_hash) {
20✔
122
      e = BigInt::decode(m_hash->final());
38✔
123
      // prepend ZA for next signature if any
124
      m_hash->update(m_za);
19✔
125
   } else {
126
      e = BigInt::decode(m_digest);
2✔
127
      m_digest.clear();
1✔
128
   }
129

130
   const BigInt k = m_group.random_scalar(rng);
20✔
131

132
   const BigInt r = m_group.mod_order(m_group.blinded_base_point_multiply_x(k, rng, m_ws) + e);
40✔
133
   const BigInt s = m_group.multiply_mod_order(m_da_inv, m_group.mod_order(k - r * m_x));
60✔
134

135
   return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order().bytes());
20✔
136
}
80✔
137

138
/**
139
* SM2 verification operation
140
*/
141
class SM2_Verification_Operation final : public PK_Ops::Verification {
×
142
   public:
143
      SM2_Verification_Operation(const SM2_PublicKey& sm2, std::string_view ident, std::string_view hash) :
18✔
144
            m_group(sm2.domain()), m_gy_mul(m_group.get_base_point(), sm2.public_point()) {
18✔
145
         if(hash == "Raw") {
33✔
146
            // m_hash is null, m_za is empty
147
         } else {
148
            m_hash = HashFunction::create_or_throw(hash);
17✔
149
            // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
150
            m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
17✔
151
            m_hash->update(m_za);
17✔
152
         }
153
      }
18✔
154

155
      void update(const uint8_t msg[], size_t msg_len) override {
184✔
156
         if(m_hash) {
184✔
157
            m_hash->update(msg, msg_len);
161✔
158
         } else {
159
            m_digest.insert(m_digest.end(), msg, msg + msg_len);
23✔
160
         }
161
      }
184✔
162

163
      bool is_valid_signature(const uint8_t sig[], size_t sig_len) override;
164

165
      std::string hash_function() const override { return m_hash->name(); }
×
166

167
   private:
168
      const EC_Group m_group;
169
      const EC_Point_Multi_Point_Precompute m_gy_mul;
170
      secure_vector<uint8_t> m_digest;
171
      std::vector<uint8_t> m_za;
172
      std::unique_ptr<HashFunction> m_hash;
173
};
174

175
bool SM2_Verification_Operation::is_valid_signature(const uint8_t sig[], size_t sig_len) {
185✔
176
   BigInt e;
185✔
177
   if(m_hash) {
185✔
178
      e = BigInt::decode(m_hash->final());
324✔
179
      // prepend ZA for next signature if any
180
      m_hash->update(m_za);
162✔
181
   } else {
182
      e = BigInt::decode(m_digest);
46✔
183
      m_digest.clear();
23✔
184
   }
185

186
   if(sig_len != m_group.get_order().bytes() * 2)
185✔
187
      return false;
188

189
   const BigInt r(sig, sig_len / 2);
185✔
190
   const BigInt s(sig + sig_len / 2, sig_len / 2);
185✔
191

192
   if(r <= 0 || r >= m_group.get_order() || s <= 0 || s >= m_group.get_order())
363✔
193
      return false;
7✔
194

195
   const BigInt t = m_group.mod_order(r + s);
178✔
196

197
   if(t == 0)
178✔
198
      return false;
199

200
   const EC_Point R = m_gy_mul.multi_exp(s, t);
178✔
201

202
   // ???
203
   if(R.is_zero())
356✔
204
      return false;
205

206
   return (m_group.mod_order(R.get_affine_x() + e) == r);
890✔
207
}
918✔
208

209
void parse_sm2_param_string(std::string_view params, std::string& userid, std::string& hash) {
36✔
210
   // GM/T 0009-2012 specifies this as the default userid
211
   const std::string default_userid = "1234567812345678";
36✔
212

213
   // defaults:
214
   userid = default_userid;
36✔
215
   hash = "SM3";
36✔
216

217
   /*
218
   * SM2 parameters have the following possible formats:
219
   * Ident [since 2.2.0]
220
   * Ident,Hash [since 2.3.0]
221
   */
222

223
   auto comma = params.find(',');
36✔
224
   if(comma == std::string::npos) {
36✔
225
      userid = params;
54✔
226
   } else {
227
      userid = params.substr(0, comma);
18✔
228
      hash = params.substr(comma + 1, std::string::npos);
36✔
229
   }
230
}
36✔
231

232
}
233

234
std::unique_ptr<PK_Ops::Verification> SM2_PublicKey::create_verification_op(std::string_view params,
39✔
235
                                                                            std::string_view provider) const {
236
   if(provider == "base" || provider.empty()) {
46✔
237
      std::string userid, hash;
18✔
238
      parse_sm2_param_string(params, userid, hash);
18✔
239
      return std::make_unique<SM2_Verification_Operation>(*this, userid, hash);
18✔
240
   }
36✔
241

242
   throw Provider_Not_Found(algo_name(), provider);
42✔
243
}
244

245
std::unique_ptr<PK_Ops::Signature> SM2_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
39✔
246
                                                                       std::string_view params,
247
                                                                       std::string_view provider) const {
248
   if(provider == "base" || provider.empty()) {
46✔
249
      std::string userid, hash;
18✔
250
      parse_sm2_param_string(params, userid, hash);
18✔
251
      return std::make_unique<SM2_Signature_Operation>(*this, userid, hash);
18✔
252
   }
36✔
253

254
   throw Provider_Not_Found(algo_name(), provider);
42✔
255
}
256

257
}
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