Botan  2.6.0
Crypto and TLS for C++11
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_enc.h>
9 #include <botan/internal/point_mul.h>
10 #include <botan/pk_ops.h>
11 #include <botan/keypair.h>
12 #include <botan/der_enc.h>
13 #include <botan/ber_dec.h>
14 #include <botan/kdf.h>
15 #include <botan/hash.h>
16 
17 namespace Botan {
18 
20  bool strong) const
21  {
22  if(!public_point().on_the_curve())
23  return false;
24 
25  if(!strong)
26  return true;
27 
28  return KeyPair::encryption_consistency_check(rng, *this, "SM3");
29  }
30 
32  const secure_vector<uint8_t>& key_bits) :
33  EC_PrivateKey(alg_id, key_bits)
34  {
35  }
36 
38  const EC_Group& domain,
39  const BigInt& x) :
40  EC_PrivateKey(rng, domain, x)
41  {
42  }
43 
44 namespace {
45 
46 class SM2_Encryption_Operation final : public PK_Ops::Encryption
47  {
48  public:
49  SM2_Encryption_Operation(const SM2_Encryption_PublicKey& key, const std::string& kdf_hash) :
50  m_group(key.domain()),
51  m_mul_public_point(key.public_point()),
52  m_kdf_hash(kdf_hash)
53  {}
54 
55  size_t max_input_bits() const override
56  {
57  // This is arbitrary, but assumes SM2 is used for key encapsulation
58  return 512;
59  }
60 
61  secure_vector<uint8_t> encrypt(const uint8_t msg[],
62  size_t msg_len,
63  RandomNumberGenerator& rng) override
64  {
65  std::unique_ptr<HashFunction> hash = HashFunction::create_or_throw(m_kdf_hash);
66  std::unique_ptr<KDF> kdf = KDF::create_or_throw("KDF2(" + m_kdf_hash + ")");
67 
68  const size_t p_bytes = m_group.get_p_bytes();
69 
70  const BigInt k = m_group.random_scalar(rng);
71 
72  const PointGFp C1 = m_group.blinded_base_point_multiply(k, rng, m_ws);
73  const BigInt x1 = C1.get_affine_x();
74  const BigInt y1 = C1.get_affine_y();
75  std::vector<uint8_t> x1_bytes(p_bytes);
76  std::vector<uint8_t> y1_bytes(p_bytes);
77  BigInt::encode_1363(x1_bytes.data(), x1_bytes.size(), x1);
78  BigInt::encode_1363(y1_bytes.data(), y1_bytes.size(), y1);
79 
80  const PointGFp kPB = m_mul_public_point.mul(k, rng, m_group.get_order(), m_ws);
81 
82  const BigInt x2 = kPB.get_affine_x();
83  const BigInt y2 = kPB.get_affine_y();
84  std::vector<uint8_t> x2_bytes(p_bytes);
85  std::vector<uint8_t> y2_bytes(p_bytes);
86  BigInt::encode_1363(x2_bytes.data(), x2_bytes.size(), x2);
87  BigInt::encode_1363(y2_bytes.data(), y2_bytes.size(), y2);
88 
89  secure_vector<uint8_t> kdf_input;
90  kdf_input += x2_bytes;
91  kdf_input += y2_bytes;
92 
93  const secure_vector<uint8_t> kdf_output =
94  kdf->derive_key(msg_len, kdf_input.data(), kdf_input.size());
95 
96  secure_vector<uint8_t> masked_msg(msg_len);
97  xor_buf(masked_msg.data(), msg, kdf_output.data(), msg_len);
98 
99  hash->update(x2_bytes);
100  hash->update(msg, msg_len);
101  hash->update(y2_bytes);
102  std::vector<uint8_t> C3(hash->output_length());
103  hash->final(C3.data());
104 
105  return DER_Encoder()
106  .start_cons(SEQUENCE)
107  .encode(x1)
108  .encode(y1)
109  .encode(C3, OCTET_STRING)
110  .encode(masked_msg, OCTET_STRING)
111  .end_cons()
112  .get_contents();
113  }
114 
115  private:
116  const EC_Group m_group;
117  PointGFp_Var_Point_Precompute m_mul_public_point;
118  const std::string m_kdf_hash;
119  std::vector<BigInt> m_ws;
120  };
121 
122 class SM2_Decryption_Operation final : public PK_Ops::Decryption
123  {
124  public:
125  SM2_Decryption_Operation(const SM2_Encryption_PrivateKey& key,
126  RandomNumberGenerator& rng,
127  const std::string& kdf_hash) :
128  m_key(key),
129  m_rng(rng),
130  m_kdf_hash(kdf_hash)
131  {}
132 
133  secure_vector<uint8_t> decrypt(uint8_t& valid_mask,
134  const uint8_t ciphertext[],
135  size_t ciphertext_len) override
136  {
137  const EC_Group& group = m_key.domain();
138  const BigInt& cofactor = group.get_cofactor();
139  const size_t p_bytes = group.get_p_bytes();
140 
141  valid_mask = 0x00;
142 
143  std::unique_ptr<HashFunction> hash = HashFunction::create_or_throw(m_kdf_hash);
144  std::unique_ptr<KDF> kdf = KDF::create_or_throw("KDF2(" + m_kdf_hash + ")");
145 
146  // Too short to be valid - no timing problem from early return
147  if(ciphertext_len < 1 + p_bytes*2 + hash->output_length())
148  {
149  return secure_vector<uint8_t>();
150  }
151 
152  BigInt x1, y1;
153  secure_vector<uint8_t> C3, masked_msg;
154 
155  BER_Decoder(ciphertext, ciphertext_len)
156  .start_cons(SEQUENCE)
157  .decode(x1)
158  .decode(y1)
159  .decode(C3, OCTET_STRING)
160  .decode(masked_msg, OCTET_STRING)
161  .end_cons()
162  .verify_end();
163 
164  PointGFp C1 = group.point(x1, y1);
165  C1.randomize_repr(m_rng);
166 
167  if(!C1.on_the_curve())
168  return secure_vector<uint8_t>();
169 
170  if(cofactor > 1 && (C1 * cofactor).is_zero())
171  {
172  return secure_vector<uint8_t>();
173  }
174 
175  const PointGFp dbC1 = group.blinded_var_point_multiply(
176  C1, m_key.private_value(), m_rng, m_ws);
177 
178  const BigInt x2 = dbC1.get_affine_x();
179  const BigInt y2 = dbC1.get_affine_y();
180 
181  std::vector<uint8_t> x2_bytes(p_bytes);
182  std::vector<uint8_t> y2_bytes(p_bytes);
183  BigInt::encode_1363(x2_bytes.data(), x2_bytes.size(), x2);
184  BigInt::encode_1363(y2_bytes.data(), y2_bytes.size(), y2);
185 
186  secure_vector<uint8_t> kdf_input;
187  kdf_input += x2_bytes;
188  kdf_input += y2_bytes;
189 
190  const secure_vector<uint8_t> kdf_output =
191  kdf->derive_key(masked_msg.size(), kdf_input.data(), kdf_input.size());
192 
193  xor_buf(masked_msg.data(), kdf_output.data(), kdf_output.size());
194 
195  hash->update(x2_bytes);
196  hash->update(masked_msg);
197  hash->update(y2_bytes);
198  secure_vector<uint8_t> u = hash->final();
199 
200  if(constant_time_compare(u.data(), C3.data(), hash->output_length()) == false)
201  return secure_vector<uint8_t>();
202 
203  valid_mask = 0xFF;
204  return masked_msg;
205  }
206  private:
207  const SM2_Encryption_PrivateKey& m_key;
208  RandomNumberGenerator& m_rng;
209  const std::string m_kdf_hash;
210  std::vector<BigInt> m_ws;
211  };
212 
213 }
214 
215 std::unique_ptr<PK_Ops::Encryption>
217  const std::string& params,
218  const std::string& provider) const
219  {
220  if(provider == "base" || provider.empty())
221  {
222  const std::string kdf_hash = (params.empty() ? "SM3" : params);
223  return std::unique_ptr<PK_Ops::Encryption>(new SM2_Encryption_Operation(*this, kdf_hash));
224  }
225 
226  throw Provider_Not_Found(algo_name(), provider);
227  }
228 
229 std::unique_ptr<PK_Ops::Decryption>
231  const std::string& params,
232  const std::string& provider) const
233  {
234  if(provider == "base" || provider.empty())
235  {
236  const std::string kdf_hash = (params.empty() ? "SM3" : params);
237  return std::unique_ptr<PK_Ops::Decryption>(new SM2_Decryption_Operation(*this, rng, kdf_hash));
238  }
239 
240  throw Provider_Not_Found(algo_name(), provider);
241  }
242 
243 }
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:345
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: sm2_enc.cpp:19
const PointGFp & public_point() const
Definition: ecc_key.h:57
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:51
std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: sm2_enc.cpp:216
bool encryption_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:19
std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: sm2_enc.cpp:230
SM2_Encryption_PrivateKey(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
Definition: sm2_enc.cpp:31
std::string algo_name() const override
Definition: sm2_enc.h:44
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
std::string decrypt(const uint8_t input[], size_t input_len, const std::string &passphrase)
Definition: cryptobox.cpp:162
Definition: alg_id.cpp:13
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
static std::unique_ptr< KDF > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: kdf.cpp:222
const RSA_PrivateKey & m_key
Definition: rsa.cpp:255
MechanismType hash
std::string encrypt(const uint8_t input[], size_t input_len, const std::string &passphrase, RandomNumberGenerator &rng)
Definition: cryptobox.cpp:43