Botan 3.11.0
Crypto and TLS for C&
sm2_enc.cpp
Go to the documentation of this file.
1/*
2* SM2 Encryption
3* (C) 2017 Ribose Inc
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/sm2.h>
9
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/ec_group.h>
13#include <botan/hash.h>
14#include <botan/kdf.h>
15#include <botan/mem_ops.h>
16#include <botan/pk_ops.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/fmt.h>
19
20namespace Botan {
21
22namespace {
23
24class SM2_Encryption_Operation final : public PK_Ops::Encryption {
25 public:
26 SM2_Encryption_Operation(const SM2_Encryption_PublicKey& key, std::string_view kdf_hash) :
27 m_group(key.domain()), m_peer(key._public_ec_point()) {
28 m_hash = HashFunction::create_or_throw(kdf_hash);
29 m_kdf = KDF::create_or_throw(fmt("KDF2({})", kdf_hash));
30 }
31
32 size_t max_input_bits() const override {
33 // This is arbitrary, but assumes SM2 is used for key encapsulation
34 return 512;
35 }
36
37 size_t ciphertext_length(size_t ptext_len) const override {
38 const size_t elem_size = m_group.get_order_bytes();
39 const size_t der_overhead = 16;
40
41 return der_overhead + 2 * elem_size + m_hash->output_length() + ptext_len;
42 }
43
44 std::vector<uint8_t> encrypt(std::span<const uint8_t> msg, RandomNumberGenerator& rng) override {
45 const auto k = EC_Scalar::random(m_group, rng);
46
47 const EC_AffinePoint C1 = EC_AffinePoint::g_mul(k, rng);
48
49 const EC_AffinePoint kPB = m_peer.mul(k, rng);
50
51 const auto x2_bytes = kPB.x_bytes();
52 const auto y2_bytes = kPB.y_bytes();
53
54 secure_vector<uint8_t> kdf_input;
55 kdf_input += x2_bytes;
56 kdf_input += y2_bytes;
57
58 const auto kdf_output = m_kdf->derive_key(msg.size(), kdf_input);
59
60 std::vector<uint8_t> masked_msg(msg.size());
61 xor_buf(masked_msg, msg, kdf_output);
62
63 m_hash->update(x2_bytes);
64 m_hash->update(msg);
65 m_hash->update(y2_bytes);
66 const auto C3 = m_hash->final<std::vector<uint8_t>>();
67
68 std::vector<uint8_t> ctext;
69 DER_Encoder(ctext)
70 .start_sequence()
71 .encode(BigInt(C1.x_bytes()))
72 .encode(BigInt(C1.y_bytes()))
73 .encode(C3, ASN1_Type::OctetString)
74 .encode(masked_msg, ASN1_Type::OctetString)
75 .end_cons();
76
77 return ctext;
78 }
79
80 private:
81 const EC_Group m_group;
82 const EC_AffinePoint m_peer;
83 std::unique_ptr<HashFunction> m_hash;
84 std::unique_ptr<KDF> m_kdf;
85};
86
87class SM2_Decryption_Operation final : public PK_Ops::Decryption {
88 public:
89 SM2_Decryption_Operation(const SM2_Encryption_PrivateKey& key,
90 RandomNumberGenerator& rng,
91 std::string_view kdf_hash) :
92 m_group(key.domain()), m_x(key._private_key()), m_rng(rng) {
93 m_hash = HashFunction::create_or_throw(kdf_hash);
94
95 const std::string kdf_name = fmt("KDF2({})", kdf_hash);
96 m_kdf = KDF::create_or_throw(kdf_name);
97 }
98
99 size_t plaintext_length(size_t ptext_len) const override {
100 /*
101 * This ignores the DER encoding and so overestimates the
102 * plaintext length by 12 bytes or so
103 */
104 const size_t elem_size = m_group.get_order_bytes();
105
106 if(ptext_len < 2 * elem_size + m_hash->output_length()) {
107 return 0;
108 }
109
110 return ptext_len - (2 * elem_size + m_hash->output_length());
111 }
112
113 secure_vector<uint8_t> decrypt(uint8_t& valid_mask, std::span<const uint8_t> ctext) override {
114 const size_t p_bytes = m_group.get_p_bytes();
115
116 valid_mask = 0x00;
117
118 // Too short to be valid - no timing problem from early return
119 if(ctext.size() < 1 + p_bytes * 2 + m_hash->output_length()) {
120 return secure_vector<uint8_t>();
121 }
122
123 BigInt x1;
124 BigInt y1;
126 secure_vector<uint8_t> masked_msg;
127
128 BER_Decoder(ctext)
129 .start_sequence()
130 .decode(x1)
131 .decode(y1)
132 .decode(C3, ASN1_Type::OctetString)
133 .decode(masked_msg, ASN1_Type::OctetString)
134 .end_cons()
135 .verify_end();
136
137 // Wrong length so certainly invalid, reject immediately
138 if(C3.size() != m_hash->output_length()) {
139 return secure_vector<uint8_t>();
140 }
141
142 std::vector<uint8_t> recode_ctext;
143 DER_Encoder(recode_ctext)
144 .start_sequence()
145 .encode(x1)
146 .encode(y1)
147 .encode(C3, ASN1_Type::OctetString)
148 .encode(masked_msg, ASN1_Type::OctetString)
149 .end_cons();
150
151 if(!CT::is_equal<uint8_t>(recode_ctext, ctext).as_bool()) {
152 return secure_vector<uint8_t>();
153 }
154
155 auto C1 = EC_AffinePoint::from_bigint_xy(m_group, x1, y1);
156
157 // Here C1 is publicly invalid, so no problem with early return:
158 if(!C1) {
159 return secure_vector<uint8_t>();
160 }
161
162 const auto dbC1 = C1->mul(m_x, m_rng);
163 const auto x2_bytes = dbC1.x_bytes();
164 const auto y2_bytes = dbC1.y_bytes();
165
166 const auto kdf_output = m_kdf->derive_key(masked_msg.size(), dbC1.xy_bytes());
167
168 xor_buf(masked_msg.data(), kdf_output.data(), kdf_output.size());
169
170 m_hash->update(x2_bytes);
171 m_hash->update(masked_msg);
172 m_hash->update(y2_bytes);
173 const auto u = m_hash->final();
174
175 if(!CT::is_equal<uint8_t>(u, C3).as_bool()) {
176 return secure_vector<uint8_t>();
177 }
178
179 valid_mask = 0xFF;
180 return masked_msg;
181 }
182
183 private:
184 const EC_Group m_group;
185 const EC_Scalar m_x;
186 RandomNumberGenerator& m_rng;
187 std::unique_ptr<HashFunction> m_hash;
188 std::unique_ptr<KDF> m_kdf;
189};
190
191} // namespace
192
193std::unique_ptr<PK_Ops::Encryption> SM2_PublicKey::create_encryption_op(RandomNumberGenerator& rng,
194 std::string_view params,
195 std::string_view provider) const {
196 BOTAN_UNUSED(rng);
197
198 if(provider == "base" || provider.empty()) {
199 if(params.empty()) {
200 return std::make_unique<SM2_Encryption_Operation>(*this, "SM3");
201 } else {
202 return std::make_unique<SM2_Encryption_Operation>(*this, params);
203 }
204 }
205
206 throw Provider_Not_Found(algo_name(), provider);
207}
208
209std::unique_ptr<PK_Ops::Decryption> SM2_PrivateKey::create_decryption_op(RandomNumberGenerator& rng,
210 std::string_view params,
211 std::string_view provider) const {
212 if(provider == "base" || provider.empty()) {
213 if(params.empty()) {
214 return std::make_unique<SM2_Decryption_Operation>(*this, rng, "SM3");
215 } else {
216 return std::make_unique<SM2_Decryption_Operation>(*this, rng, params);
217 }
218 }
219
220 throw Provider_Not_Found(algo_name(), provider);
221}
222
223} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
static std::optional< EC_AffinePoint > from_bigint_xy(const EC_Group &group, const BigInt &x, const BigInt &y)
Definition ec_apoint.cpp:93
static EC_AffinePoint g_mul(const EC_Scalar &scalar, RandomNumberGenerator &rng)
Multiply by the group generator returning a complete point.
static EC_Scalar random(const EC_Group &group, RandomNumberGenerator &rng)
Definition ec_scalar.cpp:61
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition kdf.cpp:204
std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2_enc.cpp:209
std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2_enc.cpp:193
std::string algo_name() const override
Definition sm2.cpp:19
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
std::string decrypt(std::span< const uint8_t > input, std::string_view passphrase)
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)
Definition cryptobox.cpp:43
SM2_PublicKey SM2_Encryption_PublicKey
Definition sm2.h:161
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
SM2_PrivateKey SM2_Encryption_PrivateKey
Definition sm2.h:164
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:341
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68