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>
51 m_da_inv((this->_private_key() +
EC_Scalar::one(domain())).invert()),
52 m_da_inv_legacy(m_da_inv.to_bigint()) {}
56 m_da_inv((this->_private_key() +
EC_Scalar::one(domain())).invert()),
57 m_da_inv_legacy(m_da_inv.to_bigint()) {}
62 std::string_view user_id,
65 if(user_id.size() >= 8192) {
69 const uint16_t uid_len =
static_cast<uint16_t
>(8 * user_id.size());
83 return hash.
final<std::vector<uint8_t>>();
89class SM2_Signature_Operation
final :
public PK_Ops::Signature {
91 SM2_Signature_Operation(
const SM2_PrivateKey& sm2, std::string_view ident, std::string_view hash) :
92 m_group(sm2.domain()), m_x(sm2._private_key()), m_da_inv(sm2._get_da_inv()) {
103 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
105 void update(std::span<const uint8_t> input)
override {
107 m_hash->update(input);
109 m_digest.insert(m_digest.end(), input.begin(), input.end());
113 std::vector<uint8_t> sign(RandomNumberGenerator& rng)
override;
115 std::string hash_function()
const override {
return m_hash ? m_hash->name() :
"Raw"; }
118 const EC_Group m_group;
120 const EC_Scalar m_da_inv;
122 std::vector<uint8_t> m_za;
124 std::unique_ptr<HashFunction> m_hash;
125 std::vector<BigInt> m_ws;
128std::vector<uint8_t> SM2_Signature_Operation::sign(RandomNumberGenerator& rng) {
129 const auto e = [&]() {
131 auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_hash->final());
133 m_hash->update(m_za);
136 auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_digest);
142 const auto k = EC_Scalar::random(m_group, rng);
144 const auto r = EC_Scalar::gk_x_mod_order(k, rng, m_ws) + e;
145 const auto s = (k - r * m_x) * m_da_inv;
147 return EC_Scalar::serialize_pair(r, s);
153class SM2_Verification_Operation
final :
public PK_Ops::Verification {
155 SM2_Verification_Operation(
const SM2_PublicKey& sm2, std::string_view ident, std::string_view hash) :
156 m_group(sm2.domain()), m_gy_mul(sm2._public_key()) {
160 m_hash = HashFunction::create_or_throw(hash);
162 m_za =
sm2_compute_za(*m_hash, ident, m_group, sm2._public_key());
163 m_hash->update(m_za);
167 void update(std::span<const uint8_t> input)
override {
169 m_hash->update(input);
171 m_digest.insert(m_digest.end(), input.begin(), input.end());
175 bool is_valid_signature(std::span<const uint8_t> sig)
override;
177 std::string hash_function()
const override {
return m_hash ? m_hash->name() :
"Raw"; }
180 const EC_Group m_group;
181 const EC_Group::Mul2Table m_gy_mul;
182 secure_vector<uint8_t> m_digest;
183 std::vector<uint8_t> m_za;
184 std::unique_ptr<HashFunction> m_hash;
187bool SM2_Verification_Operation::is_valid_signature(std::span<const uint8_t> sig) {
188 const auto e = [&]() {
190 auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_hash->final());
192 m_hash->update(m_za);
195 auto ie = EC_Scalar::from_bytes_mod_order(m_group, m_digest);
201 if(
auto rs = EC_Scalar::deserialize_pair(m_group, sig)) {
202 const auto& [r, s] = rs.value();
204 if(r.is_nonzero() && s.is_nonzero()) {
205 const auto t = r + s;
215void parse_sm2_param_string(std::string_view params, std::string& userid, std::string& hash) {
217 const std::string default_userid =
"1234567812345678";
220 userid = default_userid;
229 auto comma = params.find(
',');
230 if(comma == std::string::npos) {
233 userid = params.substr(0, comma);
234 hash = params.substr(comma + 1, std::string::npos);
241 return std::make_unique<SM2_PrivateKey>(rng, domain());
245 std::string_view user_id,
253 std::string_view provider)
const {
254 if(provider ==
"base" || provider.empty()) {
255 std::string userid, hash;
256 parse_sm2_param_string(params, userid, hash);
257 return std::make_unique<SM2_Verification_Operation>(*
this, userid, hash);
264 std::string_view params,
265 std::string_view provider)
const {
266 if(provider ==
"base" || provider.empty()) {
267 std::string userid, hash;
268 parse_sm2_param_string(params, userid, hash);
269 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[])
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
const BigInt & get_b() const
const BigInt & get_a() const
const BigInt & get_g_y() const
const BigInt & get_g_x() const
size_t get_p_bytes() const
const BigInt & private_value() 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 &group, const EC_Point &point)
std::vector< T, secure_allocator< T > > secure_vector