Botan 3.3.0
Crypto and TLS for C&
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
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
19namespace Botan {
20
21std::string SM2_PublicKey::algo_name() const {
22 return "SM2";
23}
24
25std::unique_ptr<Public_Key> SM2_PrivateKey::public_key() const {
26 return std::make_unique<SM2_Signature_PublicKey>(domain(), public_point());
27}
28
29bool SM2_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
30 if(!EC_PrivateKey::check_key(rng, strong)) {
31 return false;
32 }
33
34 // SM2 has an oddity in private key generation when compared to
35 // other EC*DSA style signature algorithms described in ISO14888-3:
36 // the private key x MUST be in ]0, q-1[ instead of ]0, q[.
37 if(m_private_key < 1 || m_private_key >= m_domain_params.get_order() - 1) {
38 return false;
39 }
40
41 if(!strong) {
42 return true;
43 }
44
45 return KeyPair::signature_consistency_check(rng, *this, "user@example.com,SM3");
46}
47
48SM2_PrivateKey::SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
49 EC_PrivateKey(alg_id, key_bits) {
50 m_da_inv = domain().inverse_mod_order(m_private_key + 1);
51}
52
54 EC_PrivateKey(rng, domain, x) {
56}
57
58std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
59 std::string_view user_id,
60 const EC_Group& domain,
61 const EC_Point& pubkey) {
62 if(user_id.size() >= 8192) {
63 throw Invalid_Argument("SM2 user id too long to represent");
64 }
65
66 const uint16_t uid_len = static_cast<uint16_t>(8 * user_id.size());
67
68 hash.update(get_byte<0>(uid_len));
69 hash.update(get_byte<1>(uid_len));
70 hash.update(user_id);
71
72 const size_t p_bytes = domain.get_p_bytes();
73
74 hash.update(BigInt::encode_1363(domain.get_a(), p_bytes));
75 hash.update(BigInt::encode_1363(domain.get_b(), p_bytes));
76 hash.update(BigInt::encode_1363(domain.get_g_x(), p_bytes));
77 hash.update(BigInt::encode_1363(domain.get_g_y(), p_bytes));
78 hash.update(BigInt::encode_1363(pubkey.get_affine_x(), p_bytes));
79 hash.update(BigInt::encode_1363(pubkey.get_affine_y(), p_bytes));
80
81 std::vector<uint8_t> za(hash.output_length());
82 hash.final(za.data());
83
84 return za;
85}
86
87namespace {
88
89/**
90* SM2 signature operation
91*/
92class SM2_Signature_Operation final : public PK_Ops::Signature {
93 public:
94 SM2_Signature_Operation(const SM2_PrivateKey& sm2, std::string_view ident, std::string_view hash) :
95 m_group(sm2.domain()), m_x(sm2.private_value()), m_da_inv(sm2.get_da_inv()) {
96 if(hash == "Raw") {
97 // m_hash is null, m_za is empty
98 } else {
99 m_hash = HashFunction::create_or_throw(hash);
100 // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
101 m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
102 m_hash->update(m_za);
103 }
104 }
105
106 size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
107
108 void update(const uint8_t msg[], size_t msg_len) override {
109 if(m_hash) {
110 m_hash->update(msg, msg_len);
111 } else {
112 m_digest.insert(m_digest.end(), msg, msg + msg_len);
113 }
114 }
115
116 secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
117
118 std::string hash_function() const override { return m_hash ? m_hash->name() : "Raw"; }
119
120 private:
121 const EC_Group m_group;
122 const BigInt m_x;
123 const BigInt m_da_inv;
124
125 std::vector<uint8_t> m_za;
126 secure_vector<uint8_t> m_digest;
127 std::unique_ptr<HashFunction> m_hash;
128 std::vector<BigInt> m_ws;
129};
130
131secure_vector<uint8_t> SM2_Signature_Operation::sign(RandomNumberGenerator& rng) {
132 BigInt e;
133 if(m_hash) {
134 e = BigInt::decode(m_hash->final());
135 // prepend ZA for next signature if any
136 m_hash->update(m_za);
137 } else {
138 e = BigInt::decode(m_digest);
139 m_digest.clear();
140 }
141
142 const BigInt k = m_group.random_scalar(rng);
143
144 const BigInt r = m_group.mod_order(m_group.blinded_base_point_multiply_x(k, rng, m_ws) + e);
145 const BigInt s = m_group.multiply_mod_order(m_da_inv, m_group.mod_order(k - r * m_x));
146
147 return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order().bytes());
148}
149
150/**
151* SM2 verification operation
152*/
153class SM2_Verification_Operation final : public PK_Ops::Verification {
154 public:
155 SM2_Verification_Operation(const SM2_PublicKey& sm2, std::string_view ident, std::string_view hash) :
156 m_group(sm2.domain()), m_gy_mul(m_group.get_base_point(), sm2.public_point()) {
157 if(hash == "Raw") {
158 // m_hash is null, m_za is empty
159 } else {
160 m_hash = HashFunction::create_or_throw(hash);
161 // ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
162 m_za = sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
163 m_hash->update(m_za);
164 }
165 }
166
167 void update(const uint8_t msg[], size_t msg_len) override {
168 if(m_hash) {
169 m_hash->update(msg, msg_len);
170 } else {
171 m_digest.insert(m_digest.end(), msg, msg + msg_len);
172 }
173 }
174
175 bool is_valid_signature(const uint8_t sig[], size_t sig_len) override;
176
177 std::string hash_function() const override { return m_hash ? m_hash->name() : "Raw"; }
178
179 private:
180 const EC_Group m_group;
181 const EC_Point_Multi_Point_Precompute m_gy_mul;
182 secure_vector<uint8_t> m_digest;
183 std::vector<uint8_t> m_za;
184 std::unique_ptr<HashFunction> m_hash;
185};
186
187bool SM2_Verification_Operation::is_valid_signature(const uint8_t sig[], size_t sig_len) {
188 BigInt e;
189 if(m_hash) {
190 e = BigInt::decode(m_hash->final());
191 // prepend ZA for next signature if any
192 m_hash->update(m_za);
193 } else {
194 e = BigInt::decode(m_digest);
195 m_digest.clear();
196 }
197
198 if(sig_len != m_group.get_order().bytes() * 2) {
199 return false;
200 }
201
202 const BigInt r(sig, sig_len / 2);
203 const BigInt s(sig + sig_len / 2, sig_len / 2);
204
205 if(r <= 0 || r >= m_group.get_order() || s <= 0 || s >= m_group.get_order()) {
206 return false;
207 }
208
209 const BigInt t = m_group.mod_order(r + s);
210
211 if(t == 0) {
212 return false;
213 }
214
215 const EC_Point R = m_gy_mul.multi_exp(s, t);
216
217 // ???
218 if(R.is_zero()) {
219 return false;
220 }
221
222 return (m_group.mod_order(R.get_affine_x() + e) == r);
223}
224
225void parse_sm2_param_string(std::string_view params, std::string& userid, std::string& hash) {
226 // GM/T 0009-2012 specifies this as the default userid
227 const std::string default_userid = "1234567812345678";
228
229 // defaults:
230 userid = default_userid;
231 hash = "SM3";
232
233 /*
234 * SM2 parameters have the following possible formats:
235 * Ident [since 2.2.0]
236 * Ident,Hash [since 2.3.0]
237 */
238
239 auto comma = params.find(',');
240 if(comma == std::string::npos) {
241 userid = params;
242 } else {
243 userid = params.substr(0, comma);
244 hash = params.substr(comma + 1, std::string::npos);
245 }
246}
247
248} // namespace
249
250std::unique_ptr<Private_Key> SM2_PublicKey::generate_another(RandomNumberGenerator& rng) const {
251 return std::make_unique<SM2_PrivateKey>(rng, domain());
252}
253
254std::unique_ptr<PK_Ops::Verification> SM2_PublicKey::create_verification_op(std::string_view params,
255 std::string_view provider) const {
256 if(provider == "base" || provider.empty()) {
257 std::string userid, hash;
258 parse_sm2_param_string(params, userid, hash);
259 return std::make_unique<SM2_Verification_Operation>(*this, userid, hash);
260 }
261
262 throw Provider_Not_Found(algo_name(), provider);
263}
264
265std::unique_ptr<PK_Ops::Signature> SM2_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
266 std::string_view params,
267 std::string_view provider) const {
268 if(provider == "base" || provider.empty()) {
269 std::string userid, hash;
270 parse_sm2_param_string(params, userid, hash);
271 return std::make_unique<SM2_Signature_Operation>(*this, userid, hash);
272 }
273
274 throw Provider_Not_Found(algo_name(), provider);
275}
276
277} // namespace Botan
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
size_t bytes() const
Definition bigint.cpp:277
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35
virtual size_t output_length() const =0
void final(uint8_t out[])
Definition buf_comp.h:70
BigInt blinded_base_point_multiply_x(const BigInt &k, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
Definition ec_group.cpp:581
const BigInt & get_b() const
Definition ec_group.cpp:500
const BigInt & get_a() const
Definition ec_group.cpp:496
const BigInt & get_g_y() const
Definition ec_group.cpp:516
BigInt mod_order(const BigInt &x) const
Definition ec_group.cpp:524
BigInt multiply_mod_order(const BigInt &x, const BigInt &y) const
Definition ec_group.cpp:532
const BigInt & get_order() const
Definition ec_group.cpp:508
const BigInt & get_g_x() const
Definition ec_group.cpp:512
BigInt inverse_mod_order(const BigInt &x) const
Definition ec_group.cpp:540
size_t get_p_bytes() const
Definition ec_group.cpp:480
BigInt random_scalar(RandomNumberGenerator &rng) const
Definition ec_group.cpp:592
EC_Point multi_exp(const BigInt &k1, const BigInt &k2) const
BigInt get_affine_x() const
Definition ec_point.cpp:469
BigInt get_affine_y() const
Definition ec_point.cpp:489
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:161
const EC_Group & domain() const
Definition ecc_key.h:54
EC_Group m_domain_params
Definition ecc_key.h:108
const EC_Point & public_point() const
Definition ecc_key.h:40
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
std::unique_ptr< Public_Key > public_key() const override
Definition sm2.cpp:25
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2.cpp:265
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition sm2.cpp:29
SM2_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.cpp:48
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
Definition sm2.cpp:254
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition sm2.cpp:250
std::string algo_name() const override
Definition sm2.cpp:21
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
Definition keypair.cpp:49
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &domain, const EC_Point &pubkey)
Definition sm2.cpp:58