10#include <botan/ecies.h>
12#include <botan/cipher_mode.h>
13#include <botan/ecdh.h>
16#include <botan/mem_ops.h>
18#include <botan/internal/concat_util.h>
19#include <botan/internal/ct_utils.h>
20#include <botan/internal/pk_ops_impl.h>
21#include <botan/internal/pk_options_impl.h>
39 explicit ECIES_PrivateKey(
const ECDH_PrivateKey& private_key) :
41 EC_PublicKey(private_key), EC_PrivateKey(private_key), PK_Key_Agreement_Key(), m_key(private_key) {}
43 std::vector<uint8_t> public_value()
const override {
return m_key.public_value(); }
45 std::string algo_name()
const override {
return "ECIES"; }
47 std::unique_ptr<Public_Key> public_key()
const override {
return m_key.public_key(); }
51 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng)
const override {
52 return m_key.generate_another(rng);
55 std::unique_ptr<PK_Ops::Key_Agreement> _create_key_agreement_op(
56 RandomNumberGenerator& rng,
const PK_Key_Agreement_Options_Reader& options)
const override;
59 ECDH_PrivateKey m_key;
71 ECIES_ECDH_KA_Operation(
const ECIES_PrivateKey& private_key,
72 const PK_Key_Agreement_Options_Reader& options,
73 RandomNumberGenerator& rng) :
74 PK_Ops::Key_Agreement_with_KDF(options), m_key(private_key), m_rng(rng) {}
76 size_t agreed_value_size()
const override {
return m_key.domain().get_p_bytes(); }
79 const EC_Group& group = m_key.domain();
85 throw Decoding_Error(
"ECIES - Invalid elliptic curve point");
90 ECIES_PrivateKey m_key;
91 RandomNumberGenerator& m_rng;
94std::unique_ptr<PK_Ops::Key_Agreement> ECIES_PrivateKey::_create_key_agreement_op(
98 return std::make_unique<ECIES_ECDH_KA_Operation>(*
this, options, rng);
112PK_Key_Agreement create_key_agreement(
const PK_Key_Agreement_Key& private_key,
113 const ECIES_KA_Params& ecies_params,
115 RandomNumberGenerator& rng) {
116 const ECDH_PrivateKey* ecdh_key =
dynamic_cast<const ECDH_PrivateKey*
>(&private_key);
118 if(ecdh_key ==
nullptr &&
119 (ecies_params.cofactor_mode() || ecies_params.old_cofactor_mode() || ecies_params.check_mode())) {
124 throw Invalid_Argument(
"ECIES: cofactor, old cofactor and check mode are only supported for ECDH_PrivateKey");
127 if(ecdh_key !=
nullptr && (for_encryption || !ecies_params.cofactor_mode())) {
129 return PK_Key_Agreement(ECIES_PrivateKey(*ecdh_key), rng, PK_Key_Agreement_Options().with_raw_shared_key());
133 return PK_Key_Agreement(private_key, rng, PK_Key_Agreement_Options().with_raw_shared_key());
141 m_ka(create_key_agreement(private_key, ecies_params, for_encryption, rng)), m_params(ecies_params) {}
143#if defined(BOTAN_HAS_LEGACY_EC_POINT)
148 const EC_Point& other_public_key_point)
const {
149 if(other_public_key_point.
is_zero()) {
155 EC_Point other_point = other_public_key_point;
166 if(!m_params.single_hash_mode()) {
167 derivation_input += eph_public_key_bin;
171 std::vector<uint8_t> other_public_key_bin = other_point.
encode(m_params.point_format());
175 m_ka.derive_key(m_params.group().get_order_bytes(), other_public_key_bin.data(), other_public_key_bin.size());
176 derivation_input.insert(derivation_input.end(), peh.begin(), peh.end());
179 return SymmetricKey(kdf->derive_key(m_params.secret_length(), derivation_input));
192 auto other_point = other_public_key_point;
194 const auto& group = m_params.
group();
198 if(m_params.old_cofactor_mode() && group.has_cofactor()) {
201 other_point = other_point.mul(cofactor, null_rng);
207 if(!m_params.single_hash_mode()) {
208 derivation_input.assign(eph_public_key_bin.begin(), eph_public_key_bin.end());
212 std::vector<uint8_t> other_public_key_bin = other_point.serialize(m_params.point_format());
216 m_ka.derive_key(m_params.group().get_order_bytes(), other_public_key_bin.data(), other_public_key_bin.size());
217 derivation_input.insert(derivation_input.end(), peh.
begin(), peh.
end());
220 return SymmetricKey(kdf->derive_key(m_params.secret_length(), derivation_input));
242 m_cofactor_mode(false),
243 m_old_cofactor_mode(false) {}
246 std::string_view
kdf,
247 std::string_view dem_algo_spec,
249 std::string_view mac_spec,
254 m_dem_spec(dem_algo_spec),
255 m_dem_keylen(dem_key_len),
256 m_mac_spec(mac_spec),
257 m_mac_keylen(mac_key_len) {
260 throw Invalid_Argument(
"ECIES: only one of cofactor_mode, old_cofactor_mode and check_mode can be set");
265 std::string_view
kdf,
266 std::string_view dem_algo_spec,
268 std::string_view mac_spec,
273 m_dem_spec(dem_algo_spec),
274 m_dem_keylen(dem_key_len),
275 m_mac_spec(mac_spec),
276 m_mac_keylen(mac_key_len) {}
292 m_ka(private_key, ecies_params, true, rng),
293 m_params(ecies_params),
294 m_eph_public_key_bin(private_key.public_value()) {
298 m_eph_public_key_bin =
299 EC_AffinePoint(m_params.group(), m_eph_public_key_bin).serialize(ecies_params.point_format());
301 m_mac = m_params.create_mac();
319size_t ECIES_Encryptor::ciphertext_length(
size_t ptext_len)
const {
320 return m_eph_public_key_bin.size() + m_mac->output_length() + m_cipher->output_length(ptext_len);
326std::vector<uint8_t> ECIES_Encryptor::enc(
const uint8_t data[],
329 if(!m_other_point.has_value()) {
330 throw Invalid_State(
"ECIES_Encryptor: peer key invalid or not set");
332 if(!m_iv.has_value()) {
333 throw Invalid_State(
"ECIES requires a fresh IV be provided for each message");
335 if(m_iv->empty() && !m_cipher->valid_nonce_length(m_iv->size())) {
336 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
339 const SymmetricKey secret_key = m_ka.derive_secret(m_eph_public_key_bin, m_other_point.value());
341 BOTAN_ASSERT_NOMSG(secret_key.size() == m_params.dem_keylen() + m_params.mac_keylen());
345 m_cipher->set_key(
SymmetricKey(secret_key.begin(), m_params.dem_keylen()));
347 m_cipher->start(m_iv->bits_of());
351 m_cipher->finish(encrypted_data);
354 m_mac->set_key(secret_key.begin() + m_params.dem_keylen(), m_params.mac_keylen());
355 m_mac->update(encrypted_data);
356 if(!m_label.empty()) {
357 m_mac->update(m_label);
359 const auto mac = m_mac->final();
362 return concat(m_eph_public_key_bin, encrypted_data, mac);
368 m_ka(key, ecies_params, false, rng), m_params(ecies_params) {
382 m_mac = m_params.create_mac();
393 return 1 + 2 * fe_bytes;
399size_t ECIES_Decryptor::plaintext_length(
size_t ctext_len)
const {
400 const size_t point_size = compute_point_size(m_params.group(), m_params.point_format());
401 const size_t overhead = point_size + m_mac->output_length();
403 if(ctext_len < overhead) {
407 return m_cipher->output_length(ctext_len - overhead);
410size_t ECIES_Decryptor::ciphertext_length(
size_t ptext_len)
const {
415 const size_t point_size = compute_point_size(m_params.group(), m_params.point_format());
416 const size_t mac_size = m_mac->output_length();
417 const size_t ct_size_bound = cipher->output_length(ptext_len);
418 return point_size + mac_size + ct_size_bound;
424size_t ECIES_Decryptor::do_decrypt(uint8_t& valid_mask,
425 std::span<uint8_t> ptext,
426 std::span<const uint8_t> ctext)
const {
427 const uint8_t* in = ctext.data();
428 const size_t in_len = ctext.size();
429 const size_t point_size = compute_point_size(m_params.group(), m_params.point_format());
431 if(in_len < point_size + m_mac->output_length()) {
432 throw Decoding_Error(
"ECIES decryption: ciphertext is too short");
435 if(!m_iv.has_value()) {
436 throw Invalid_State(
"ECIES requires a fresh IV be provided for each message");
438 if(m_iv->empty() && !m_cipher->valid_nonce_length(m_iv->size())) {
439 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
443 const std::vector<uint8_t> other_public_key_bin(in, in + point_size);
444 const std::vector<uint8_t> encrypted_data(in + point_size, in + in_len - m_mac->output_length());
445 const std::vector<uint8_t> mac_data(in + in_len - m_mac->output_length(), in + in_len);
448 auto other_public_key =
449 [](
const EC_Group& group,
EC_Point_Format format, std::span<const uint8_t> key) -> std::optional<EC_AffinePoint> {
456 if(key.empty() || (key[0] != 0x06 && key[0] != 0x07)) {
462 }(m_params.group(), m_params.point_format(), other_public_key_bin);
464 if(!other_public_key) {
465 throw Decoding_Error(
"Invalid ECIES public point");
472 const SymmetricKey secret_key = m_ka.derive_secret(other_public_key_bin, *other_public_key);
474 BOTAN_ASSERT_NOMSG(secret_key.size() == m_params.dem_keylen() + m_params.mac_keylen());
477 m_mac->set_key(secret_key.begin() + m_params.dem_keylen(), m_params.mac_keylen());
478 m_mac->update(encrypted_data);
479 if(!m_label.empty()) {
480 m_mac->update(m_label);
486 auto iv = m_iv->bits_of();
489 if(valid_mask == 0xFF) {
492 m_cipher->set_key(
SymmetricKey(secret_key.begin(), m_params.dem_keylen()));
499 m_cipher->finish(decrypted_data);
500 BOTAN_ARG_CHECK(ptext.size() >= decrypted_data.size(),
"Output buffer too small for decryption");
501 copy_mem(ptext.first(decrypted_data.size()), decrypted_data);
502 return decrypted_data.size();
#define BOTAN_DIAGNOSTIC_POP
#define BOTAN_DIAGNOSTIC_PUSH
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static std::unique_ptr< Cipher_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
ECIES_Decryptor(const PK_Key_Agreement_Key &private_key, const ECIES_System_Params &ecies_params, RandomNumberGenerator &rng)
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)
SymmetricKey derive_secret(std::span< const uint8_t > eph_public_key_bin, const EC_AffinePoint &other_public_key_point) const
bool old_cofactor_mode() const
ECIES_KA_Params(const EC_Group &group, std::string_view kdf_spec, size_t length, EC_Point_Format point_format=EC_Point_Format::Uncompressed, bool single_hash_mode=true)
bool cofactor_mode() const
bool single_hash_mode() const
const EC_Group & group() const
const std::string & kdf() const
EC_Point_Format point_format() const
std::unique_ptr< Cipher_Mode > create_cipher(Cipher_Dir direction) const
creates an instance of the data encryption method
std::unique_ptr< MessageAuthenticationCode > create_mac() const
creates an instance of the message authentication code
ECIES_System_Params(const EC_Group &group, std::string_view kdf_spec, std::string_view dem_algo_spec, size_t dem_key_len, std::string_view mac_spec, size_t mac_key_len, EC_Point_Format point_format=EC_Point_Format::Uncompressed, bool single_hash_mode=false)
EC_Group group() const
Return the elliptic curve group this point is associated with.
bool is_identity() const
Return true if this point is the identity element.
static std::optional< EC_AffinePoint > deserialize_uncompressed(const EC_Group &group, std::span< const uint8_t > bytes)
static std::optional< EC_AffinePoint > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
static std::optional< EC_AffinePoint > deserialize_compressed(const EC_Group &group, std::span< const uint8_t > bytes)
const BigInt & get_cofactor() const
bool has_cofactor() const
size_t get_p_bytes() const
std::vector< uint8_t > encode(EC_Point_Format format) const
static EC_Scalar from_bigint(const EC_Group &group, const BigInt &bn)
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
const uint8_t * end() const
const uint8_t * begin() const
virtual size_t maximum_input_size() const =0
constexpr decltype(auto) driveby_unpoison(T &&v)
constexpr auto scoped_poison(const Ts &... xs)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
@ SingleHashMode
if set: do NOT prefix the input of the (ecdh) key agreement with the encoded (ephemeral) public key
constexpr void copy_mem(T *out, const T *in, size_t n)
void require_software_provider(const OptionsT &options, std::string_view algo_name)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector