9 #include <botan/ecies.h> 10 #include <botan/numthry.h> 11 #include <botan/cipher_mode.h> 12 #include <botan/mac.h> 13 #include <botan/internal/pk_ops_impl.h> 22 class ECIES_PrivateKey
final :
public EC_PrivateKey,
public PK_Key_Agreement_Key
25 explicit ECIES_PrivateKey(
const ECDH_PrivateKey& private_key) :
26 EC_PublicKey(private_key),
27 EC_PrivateKey(private_key),
28 PK_Key_Agreement_Key(),
33 std::vector<uint8_t> public_value()
const override 35 return m_key.public_value();
38 std::string algo_name()
const override 43 std::unique_ptr<PK_Ops::Key_Agreement>
44 create_key_agreement_op(RandomNumberGenerator& rng,
45 const std::string& params,
46 const std::string& provider)
const override;
49 ECDH_PrivateKey m_key;
55 class ECIES_ECDH_KA_Operation
final :
public PK_Ops::Key_Agreement_with_KDF
58 ECIES_ECDH_KA_Operation(
const ECIES_PrivateKey& private_key, RandomNumberGenerator& rng) :
59 PK_Ops::Key_Agreement_with_KDF(
"Raw"),
65 size_t agreed_value_size()
const override {
return m_key.domain().get_p_bytes(); }
67 secure_vector<uint8_t> raw_agree(
const uint8_t w[],
size_t w_len)
override 69 const EC_Group& group = m_key.domain();
71 PointGFp input_point = group.OS2ECP(w, w_len);
72 input_point.randomize_repr(m_rng);
74 const PointGFp S = group.blinded_var_point_multiply(
75 input_point, m_key.private_value(), m_rng, m_ws);
77 if(S.on_the_curve() ==
false)
83 ECIES_PrivateKey m_key;
84 RandomNumberGenerator& m_rng;
85 std::vector<BigInt> m_ws;
88 std::unique_ptr<PK_Ops::Key_Agreement>
89 ECIES_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
91 const std::string& )
const 93 return std::unique_ptr<PK_Ops::Key_Agreement>(
new ECIES_ECDH_KA_Operation(*
this, rng));
105 PK_Key_Agreement create_key_agreement(
const PK_Key_Agreement_Key& private_key,
106 const ECIES_KA_Params& ecies_params,
108 RandomNumberGenerator& rng)
110 const ECDH_PrivateKey* ecdh_key =
dynamic_cast<const ECDH_PrivateKey*
>(&private_key);
112 if(ecdh_key ==
nullptr && (ecies_params.cofactor_mode() || ecies_params.old_cofactor_mode()
113 || ecies_params.check_mode()))
119 throw Invalid_Argument(
"ECIES: cofactor, old cofactor and check mode are only supported for ECDH_PrivateKey");
122 if(ecdh_key && (for_encryption || !ecies_params.cofactor_mode()))
125 return PK_Key_Agreement(ECIES_PrivateKey(*ecdh_key), rng,
"Raw");
128 return PK_Key_Agreement(private_key, rng,
"Raw");
136 m_ka(create_key_agreement(private_key, ecies_params, for_encryption, rng)),
137 m_params(ecies_params)
145 const PointGFp& other_public_key_point)
const 147 if(other_public_key_point.
is_zero())
154 PointGFp other_point = other_public_key_point;
167 derivation_input += eph_public_key_bin;
175 derivation_input.insert(derivation_input.end(), peh.
begin(), peh.
end());
178 return kdf->derive_key(m_params.
secret_length(), derivation_input);
185 m_kdf_spec(kdf_spec),
187 m_compression_mode(compression_type),
193 const std::string& dem_algo_spec,
size_t dem_key_len,
194 const std::string& mac_spec,
size_t mac_key_len,
197 m_dem_spec(dem_algo_spec),
198 m_dem_keylen(dem_key_len),
199 m_mac_spec(mac_spec),
200 m_mac_keylen(mac_key_len)
205 throw Invalid_Argument(
"ECIES: only one of cofactor_mode, old_cofactor_mode and check_mode can be set");
210 const std::string& dem_algo_spec,
size_t dem_key_len,
211 const std::string& mac_spec,
size_t mac_key_len) :
234 m_ka(private_key, ecies_params, true, rng),
235 m_params(ecies_params),
236 m_eph_public_key_bin(private_key.public_value()),
259 size_t ECIES_Encryptor::maximum_input_size()
const 268 size_t ECIES_Encryptor::ciphertext_length(
size_t ptext_len)
const 270 return m_eph_public_key_bin.size() +
271 m_mac->output_length() +
272 m_cipher->output_length(ptext_len);
278 std::vector<uint8_t> ECIES_Encryptor::enc(
const uint8_t data[],
size_t length, RandomNumberGenerator&)
const 282 throw Invalid_State(
"ECIES: the other key is zero");
290 if(m_iv.
size() == 0 && !m_cipher->valid_nonce_length(m_iv.
size()))
291 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
293 m_cipher->start(m_iv.
bits_of());
295 secure_vector<uint8_t> encrypted_data(data, data + length);
296 m_cipher->finish(encrypted_data);
300 std::vector<uint8_t> out(m_eph_public_key_bin.size() + encrypted_data.size() + m_mac->output_length());
302 buffer_insert(out, m_eph_public_key_bin.size(), encrypted_data);
306 m_mac->update(encrypted_data);
309 m_mac->update(m_label);
311 m_mac->final(out.data() + m_eph_public_key_bin.size() + encrypted_data.size());
320 m_ka(key, ecies_params, false, rng),
321 m_params(ecies_params),
331 throw Invalid_Argument(
"ECIES: gcd of cofactor and order must be 1 if check_mode is 0");
339 size_t ECIES_Decryptor::plaintext_length(
size_t ctext_len)
const 342 const size_t overhead = point_size + m_mac->output_length();
344 if(ctext_len < overhead)
347 return m_cipher->output_length(ctext_len - overhead);
353 secure_vector<uint8_t> ECIES_Decryptor::do_decrypt(uint8_t& valid_mask,
const uint8_t in[],
size_t in_len)
const 357 if(in_len < point_size + m_mac->output_length())
359 throw Decoding_Error(
"ECIES decryption: ciphertext is too short");
363 const std::vector<uint8_t> other_public_key_bin(in, in + point_size);
364 const std::vector<uint8_t> encrypted_data(in + point_size, in + in_len - m_mac->output_length());
365 const std::vector<uint8_t> mac_data(in + in_len - m_mac->output_length(), in + in_len);
368 PointGFp other_public_key = m_params.
domain().
OS2ECP(other_public_key_bin);
371 if(m_params.
check_mode() && !other_public_key.on_the_curve())
373 throw Decoding_Error(
"ECIES decryption: received public key is not on the curve");
382 m_mac->update(encrypted_data);
385 m_mac->update(m_label);
387 const secure_vector<uint8_t> calculated_mac = m_mac->final();
388 valid_mask =
ct_compare_u8(mac_data.data(), calculated_mac.data(), mac_data.size());
395 if(m_iv.
size() == 0 && !m_cipher->valid_nonce_length(m_iv.
size()))
396 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
397 m_cipher->start(m_iv.
bits_of());
403 secure_vector<uint8_t> decrypted_data(encrypted_data.begin(), encrypted_data.end());
404 m_cipher->finish(decrypted_data);
405 return decrypted_data;
412 return secure_vector<uint8_t>();
std::unique_ptr< MessageAuthenticationCode > create_mac() const
creates an instance of the message authentication code
ECIES_KA_Params(const EC_Group &domain, const std::string &kdf_spec, size_t length, PointGFp::Compression_Type compression_type, ECIES_Flags flags)
SymmetricKey derive_secret(const std::vector< uint8_t > &eph_public_key_bin, const PointGFp &other_public_key_point) const
BigInt gcd(const BigInt &a, const BigInt &b)
const BigInt & get_order() const
std::vector< uint8_t > encode(PointGFp::Compression_Type format) const
static std::vector< uint8_t > encode(const BigInt &n)
PointGFp::Compression_Type compression_type() const
int(* final)(unsigned char *, CTX *)
size_t dem_keylen() const
returns the length of the key used by the data encryption method
size_t point_size(PointGFp::Compression_Type format) const
const std::string & kdf_spec() const
ECIES_Encryptor(const PK_Key_Agreement_Key &private_key, const ECIES_System_Params &ecies_params, RandomNumberGenerator &rng)
ECIES_KA_Operation(const PK_Key_Agreement_Key &private_key, const ECIES_KA_Params &ecies_params, bool for_encryption, RandomNumberGenerator &rng)
bool single_hash_mode() const
uint8_t ct_compare_u8(const uint8_t x[], const uint8_t y[], size_t len)
bool cofactor_mode() const
ECIES_System_Params(const EC_Group &domain, const std::string &kdf_spec, const std::string &dem_algo_spec, size_t dem_key_len, const std::string &mac_spec, size_t mac_key_len)
size_t mac_keylen() const
returns the length of the key used by the message authentication code
const BigInt & get_cofactor() const
PointGFp OS2ECP(const uint8_t bits[], size_t len) const
SymmetricKey derive_key(size_t key_len, const uint8_t in[], size_t in_len, const uint8_t params[], size_t params_len) const
const uint8_t * end() const
std::unique_ptr< Cipher_Mode > create_cipher(Botan::Cipher_Dir direction) const
creates an instance of the data encryption method
const uint8_t * begin() const
bool old_cofactor_mode() const
const EC_Group & domain() const
ECIES_Decryptor(const PK_Key_Agreement_Key &private_key, const ECIES_System_Params &ecies_params, RandomNumberGenerator &rng)
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
static std::unique_ptr< Cipher_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
std::vector< T, secure_allocator< T > > secure_vector
size_t secret_length() const
secure_vector< uint8_t > bits_of() const
static std::unique_ptr< KDF > create_or_throw(const std::string &algo_spec, const std::string &provider="")