10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/hash.h>
14#include <botan/pk_ops.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/fmt.h>
22class SM2_Encryption_Operation
final :
public PK_Ops::Encryption {
25 m_group(key.domain()), m_peer(key._public_key()), m_ws(EC_Point::WORKSPACE_SIZE) {
30 size_t max_input_bits()
const override {
35 size_t ciphertext_length(
size_t ptext_len)
const override {
36 const size_t elem_size = m_group.get_order_bytes();
37 const size_t der_overhead = 16;
39 return der_overhead + 2 * elem_size + m_hash->output_length() + ptext_len;
42 std::vector<uint8_t>
encrypt(std::span<const uint8_t> msg, RandomNumberGenerator& rng)
override {
47 const EC_AffinePoint kPB = m_peer.mul(k, rng, m_ws);
49 const auto x2_bytes = kPB.x_bytes();
50 const auto y2_bytes = kPB.y_bytes();
53 kdf_input += x2_bytes;
54 kdf_input += y2_bytes;
56 const auto kdf_output = m_kdf->derive_key(msg.size(), kdf_input);
58 std::vector<uint8_t> masked_msg(msg.size());
59 xor_buf(masked_msg, msg, kdf_output);
61 m_hash->update(x2_bytes);
63 m_hash->update(y2_bytes);
64 const auto C3 = m_hash->final<std::vector<uint8_t>>();
66 std::vector<uint8_t> ctext;
69 .encode(BigInt(C1.x_bytes()))
70 .encode(BigInt(C1.y_bytes()))
79 const EC_Group m_group;
80 const EC_AffinePoint m_peer;
81 std::unique_ptr<HashFunction> m_hash;
82 std::unique_ptr<KDF> m_kdf;
83 std::vector<BigInt> m_ws;
86class SM2_Decryption_Operation
final :
public PK_Ops::Decryption {
89 RandomNumberGenerator& rng,
90 std::string_view kdf_hash) :
91 m_group(key.domain()), m_x(key._private_key()), m_rng(rng) {
94 const std::string kdf_name =
fmt(
"KDF2({})", kdf_hash);
98 size_t plaintext_length(
size_t ptext_len)
const override {
103 const size_t elem_size = m_group.get_order_bytes();
105 if(ptext_len < 2 * elem_size + m_hash->output_length()) {
109 return ptext_len - (2 * elem_size + m_hash->output_length());
113 const size_t p_bytes = m_group.get_p_bytes();
118 if(ctext.size() < 1 + p_bytes * 2 + m_hash->output_length()) {
134 std::vector<uint8_t> recode_ctext;
135 DER_Encoder(recode_ctext)
143 if(recode_ctext.size() != ctext.size()) {
147 if(
CT::is_equal(recode_ctext.data(), ctext.data(), ctext.size()).as_bool() ==
false) {
158 const auto dbC1 = C1->mul(m_x, m_rng, m_ws);
159 const auto x2_bytes = dbC1.x_bytes();
160 const auto y2_bytes = dbC1.y_bytes();
162 const auto kdf_output = m_kdf->derive_key(masked_msg.size(), dbC1.xy_bytes());
164 xor_buf(masked_msg.data(), kdf_output.data(), kdf_output.size());
166 m_hash->update(x2_bytes);
167 m_hash->update(masked_msg);
168 m_hash->update(y2_bytes);
169 const auto u = m_hash->final();
171 if(!
CT::is_equal(u.data(), C3.data(), m_hash->output_length()).as_bool()) {
180 const EC_Group m_group;
182 RandomNumberGenerator& m_rng;
183 std::vector<BigInt> m_ws;
184 std::unique_ptr<HashFunction> m_hash;
185 std::unique_ptr<KDF> m_kdf;
191 std::string_view params,
192 std::string_view provider)
const {
195 if(provider ==
"base" || provider.empty()) {
197 return std::make_unique<SM2_Encryption_Operation>(*
this,
"SM3");
199 return std::make_unique<SM2_Encryption_Operation>(*
this, params);
207 std::string_view params,
208 std::string_view provider)
const {
209 if(provider ==
"base" || provider.empty()) {
211 return std::make_unique<SM2_Decryption_Operation>(*
this, rng,
"SM3");
213 return std::make_unique<SM2_Decryption_Operation>(*
this, rng, params);
static std::optional< EC_AffinePoint > from_bigint_xy(const EC_Group &group, const BigInt &x, const BigInt &y)
static EC_AffinePoint g_mul(const EC_Scalar &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws)
static EC_Scalar random(const EC_Group &group, RandomNumberGenerator &rng)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::string algo_name() const override
int(* final)(unsigned char *, CTX *)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase)
SM2_PublicKey SM2_Encryption_PublicKey
std::string fmt(std::string_view format, const T &... args)
SM2_PrivateKey SM2_Encryption_PrivateKey
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector