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>
59 std::string_view user_id,
62 if(user_id.size() >= 8192) {
66 const uint16_t uid_len =
static_cast<uint16_t
>(8 * user_id.size());
80 return hash.
final<std::vector<uint8_t>>();
88class SM2_Signature_Operation
final :
public PK_Ops::Signature {
90 SM2_Signature_Operation(
const SM2_PrivateKey& sm2, std::string_view ident, std::string_view hash) :
91 m_group(sm2.domain()), m_x(sm2.private_value()), m_da_inv(sm2.get_da_inv()) {
97 m_za =
sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
102 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
104 void update(
const uint8_t msg[],
size_t msg_len)
override {
106 m_hash->update(msg, msg_len);
108 m_digest.insert(m_digest.end(), msg, msg + msg_len);
114 std::string hash_function()
const override {
return m_hash ? m_hash->name() :
"Raw"; }
117 const EC_Group m_group;
119 const BigInt m_da_inv;
121 std::vector<uint8_t> m_za;
123 std::unique_ptr<HashFunction> m_hash;
124 std::vector<BigInt> m_ws;
130 e = BigInt::from_bytes(m_hash->final());
132 m_hash->update(m_za);
134 e = BigInt::from_bytes(m_digest);
143 return BigInt::encode_fixed_length_int_pair(r, s, m_group.
get_order_bytes());
149class SM2_Verification_Operation
final :
public PK_Ops::Verification {
151 SM2_Verification_Operation(
const SM2_PublicKey& sm2, std::string_view ident, std::string_view hash) :
152 m_group(sm2.domain()), m_gy_mul(m_group.get_base_point(), sm2.public_point()) {
156 m_hash = HashFunction::create_or_throw(hash);
158 m_za =
sm2_compute_za(*m_hash, ident, m_group, sm2.public_point());
159 m_hash->update(m_za);
163 void update(
const uint8_t msg[],
size_t msg_len)
override {
165 m_hash->update(msg, msg_len);
167 m_digest.insert(m_digest.end(), msg, msg + msg_len);
171 bool is_valid_signature(
const uint8_t sig[],
size_t sig_len)
override;
173 std::string hash_function()
const override {
return m_hash ? m_hash->name() :
"Raw"; }
176 const EC_Group m_group;
177 const EC_Point_Multi_Point_Precompute m_gy_mul;
178 secure_vector<uint8_t> m_digest;
179 std::vector<uint8_t> m_za;
180 std::unique_ptr<HashFunction> m_hash;
183bool SM2_Verification_Operation::is_valid_signature(
const uint8_t sig[],
size_t sig_len) {
186 e = BigInt::from_bytes(m_hash->final());
188 m_hash->update(m_za);
190 e = BigInt::from_bytes(m_digest);
198 const BigInt r(sig, sig_len / 2);
199 const BigInt s(sig + sig_len / 2, sig_len / 2);
205 const BigInt t = m_group.
mod_order(r + s);
211 const EC_Point R = m_gy_mul.
multi_exp(s, t);
218 return (m_group.
mod_order(R.get_affine_x() + e) == r);
221void parse_sm2_param_string(std::string_view params, std::string& userid, std::string& hash) {
223 const std::string default_userid =
"1234567812345678";
226 userid = default_userid;
235 auto comma = params.find(
',');
236 if(comma == std::string::npos) {
239 userid = params.substr(0, comma);
240 hash = params.substr(comma + 1, std::string::npos);
247 return std::make_unique<SM2_PrivateKey>(rng, domain());
251 std::string_view provider)
const {
252 if(provider ==
"base" || provider.empty()) {
253 std::string userid, hash;
254 parse_sm2_param_string(params, userid, hash);
255 return std::make_unique<SM2_Verification_Operation>(*
this, userid, hash);
262 std::string_view params,
263 std::string_view provider)
const {
264 if(provider ==
"base" || provider.empty()) {
265 std::string userid, hash;
266 parse_sm2_param_string(params, userid, hash);
267 return std::make_unique<SM2_Signature_Operation>(*
this, userid, hash);
T serialize(size_t len) const
void update(const uint8_t in[], size_t length)
void final(uint8_t out[])
BigInt blinded_base_point_multiply_x(const BigInt &k, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
const BigInt & get_b() const
const BigInt & get_a() const
const BigInt & get_g_y() const
BigInt mod_order(const BigInt &x) const
BigInt multiply_mod_order(const BigInt &x, const BigInt &y) const
const BigInt & get_order() const
const BigInt & get_g_x() const
BigInt inverse_mod_order(const BigInt &x) const
size_t get_p_bytes() const
BigInt random_scalar(RandomNumberGenerator &rng) const
size_t get_order_bytes() const
EC_Point multi_exp(const BigInt &k1, const BigInt &k2) const
secure_vector< uint8_t > xy_bytes() const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
const EC_Group & domain() const
const EC_Point & public_point() const
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
std::unique_ptr< Public_Key > public_key() const override
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
bool check_key(RandomNumberGenerator &rng, bool) const override
SM2_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::string algo_name() const override
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)
constexpr uint8_t get_byte(T input)
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &domain, const EC_Point &pubkey)
std::vector< T, secure_allocator< T > > secure_vector