10#include <botan/ecies.h>
12#include <botan/cipher_mode.h>
13#include <botan/ecdh.h>
16#include <botan/numthry.h>
18#include <botan/internal/ct_utils.h>
19#include <botan/internal/pk_ops_impl.h>
20#include <botan/internal/stl_util.h>
33class ECIES_PrivateKey
final :
public EC_PrivateKey,
34 public PK_Key_Agreement_Key {
36 explicit ECIES_PrivateKey(
const ECDH_PrivateKey& private_key) :
37 EC_PublicKey(private_key), EC_PrivateKey(private_key), PK_Key_Agreement_Key(), m_key(private_key) {}
39 std::vector<uint8_t> public_value()
const override {
return m_key.public_value(); }
41 std::string algo_name()
const override {
return "ECIES"; }
43 std::unique_ptr<Public_Key> public_key()
const override {
return m_key.public_key(); }
47 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng)
const override {
48 return m_key.generate_another(rng);
51 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
52 std::string_view params,
53 std::string_view provider)
const override;
56 ECDH_PrivateKey m_key;
64class ECIES_ECDH_KA_Operation
final :
public PK_Ops::Key_Agreement_with_KDF {
66 ECIES_ECDH_KA_Operation(
const ECIES_PrivateKey& private_key, RandomNumberGenerator& rng) :
67 PK_Ops::Key_Agreement_with_KDF(
"Raw"), m_key(private_key), m_rng(rng) {}
69 size_t agreed_value_size()
const override {
return m_key.domain().get_p_bytes(); }
72 const EC_Group& group = m_key.domain();
74 return input_point->mul(m_key._private_key(), m_rng, m_ws).x_bytes();
76 throw Decoding_Error(
"ECIES - Invalid elliptic curve point");
81 ECIES_PrivateKey m_key;
82 RandomNumberGenerator& m_rng;
83 std::vector<BigInt> m_ws;
86std::unique_ptr<PK_Ops::Key_Agreement> ECIES_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
88 std::string_view )
const {
89 return std::make_unique<ECIES_ECDH_KA_Operation>(*
this, rng);
101PK_Key_Agreement create_key_agreement(
const PK_Key_Agreement_Key& private_key,
102 const ECIES_KA_Params& ecies_params,
104 RandomNumberGenerator& rng) {
105 const ECDH_PrivateKey* ecdh_key =
dynamic_cast<const ECDH_PrivateKey*
>(&private_key);
107 if(ecdh_key ==
nullptr &&
108 (ecies_params.cofactor_mode() || ecies_params.old_cofactor_mode() || ecies_params.check_mode())) {
113 throw Invalid_Argument(
"ECIES: cofactor, old cofactor and check mode are only supported for ECDH_PrivateKey");
116 if(ecdh_key && (for_encryption || !ecies_params.cofactor_mode())) {
118 return PK_Key_Agreement(ECIES_PrivateKey(*ecdh_key), rng,
"Raw");
121 return PK_Key_Agreement(private_key, rng,
"Raw");
129 m_ka(create_key_agreement(private_key, ecies_params, for_encryption, rng)), m_params(ecies_params) {}
131#if defined(BOTAN_HAS_LEGACY_EC_POINT)
136 const EC_Point& other_public_key_point)
const {
137 if(other_public_key_point.
is_zero()) {
143 EC_Point other_point = other_public_key_point;
154 derivation_input += eph_public_key_bin;
163 derivation_input.insert(derivation_input.end(), peh.begin(), peh.end());
179 auto other_point = other_public_key_point;
183 std::vector<BigInt> ws;
186 other_point = other_point.mul(cofactor, null_rng, ws);
193 derivation_input.assign(eph_public_key_bin.begin(), eph_public_key_bin.end());
202 derivation_input.insert(derivation_input.end(), peh.
begin(), peh.
end());
209 std::string_view kdf_spec,
213 m_domain(domain), m_kdf_spec(kdf_spec), m_length(length), m_compression_mode(compression_type), m_flags(flags) {}
216 std::string_view kdf_spec,
217 std::string_view dem_algo_spec,
219 std::string_view mac_spec,
223 ECIES_KA_Params(domain, kdf_spec, dem_key_len + mac_key_len, compression_type, flags),
224 m_dem_spec(dem_algo_spec),
225 m_dem_keylen(dem_key_len),
226 m_mac_spec(mac_spec),
227 m_mac_keylen(mac_key_len) {
230 throw Invalid_Argument(
"ECIES: only one of cofactor_mode, old_cofactor_mode and check_mode can be set");
235 std::string_view kdf_spec,
236 std::string_view dem_algo_spec,
238 std::string_view mac_spec,
239 size_t mac_key_len) :
263 m_ka(private_key, ecies_params, true, rng),
264 m_params(ecies_params),
265 m_eph_public_key_bin(private_key.public_value()),
272 m_eph_public_key_bin =
273 EC_AffinePoint(m_params.domain(), m_eph_public_key_bin).serialize(ecies_params.compression_type());
285size_t ECIES_Encryptor::maximum_input_size()
const {
293size_t ECIES_Encryptor::ciphertext_length(
size_t ptext_len)
const {
294 return m_eph_public_key_bin.size() + m_mac->output_length() + m_cipher->output_length(ptext_len);
300std::vector<uint8_t> ECIES_Encryptor::enc(
const uint8_t data[],
302 RandomNumberGenerator& )
const {
303 if(!m_other_point.has_value()) {
304 throw Invalid_State(
"ECIES_Encryptor: peer key invalid or not set");
312 if(m_iv.
empty() && !m_cipher->valid_nonce_length(m_iv.
size())) {
313 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
316 m_cipher->start(m_iv.
bits_of());
319 m_cipher->finish(encrypted_data);
323 m_mac->update(encrypted_data);
324 if(!m_label.empty()) {
325 m_mac->update(m_label);
327 const auto mac = m_mac->final();
330 return concat(m_eph_public_key_bin, encrypted_data, mac);
336 m_ka(key, ecies_params, false, rng), m_params(ecies_params), m_iv(), m_label() {
339 const BigInt& cofactor = m_params.domain().get_cofactor();
340 if(cofactor > 1 && gcd(cofactor, m_params.domain().get_order()) != 1) {
341 throw Invalid_Argument(
"ECIES: gcd of cofactor and order must be 1 if check_mode is 0");
345 m_mac = m_params.create_mac();
351size_t compute_point_size(
const EC_Group& group, EC_Point_Format format) {
352 const size_t fe_bytes = group.get_p_bytes();
353 if(format == EC_Point_Format::Compressed) {
356 return 1 + 2 * fe_bytes;
362size_t ECIES_Decryptor::plaintext_length(
size_t ctext_len)
const {
363 const size_t point_size = compute_point_size(m_params.domain(), m_params.compression_type());
364 const size_t overhead = point_size + m_mac->output_length();
366 if(ctext_len < overhead) {
370 return m_cipher->output_length(ctext_len - overhead);
376secure_vector<uint8_t> ECIES_Decryptor::do_decrypt(uint8_t& valid_mask,
const uint8_t in[],
size_t in_len)
const {
377 const size_t point_size = compute_point_size(m_params.domain(), m_params.compression_type());
379 if(in_len < point_size + m_mac->output_length()) {
380 throw Decoding_Error(
"ECIES decryption: ciphertext is too short");
384 const std::vector<uint8_t> other_public_key_bin(in, in + point_size);
385 const std::vector<uint8_t> encrypted_data(in + point_size, in + in_len - m_mac->output_length());
386 const std::vector<uint8_t> mac_data(in + in_len - m_mac->output_length(), in + in_len);
389 auto other_public_key = EC_AffinePoint(m_params.domain(), other_public_key_bin);
396 const SymmetricKey secret_key = m_ka.derive_secret(other_public_key_bin, other_public_key);
399 m_mac->set_key(secret_key.begin() + m_params.dem_keylen(), m_params.mac_keylen());
400 m_mac->update(encrypted_data);
401 if(!m_label.empty()) {
402 m_mac->update(m_label);
404 const secure_vector<uint8_t> calculated_mac = m_mac->final();
405 valid_mask = CT::is_equal(mac_data.data(), calculated_mac.data(), mac_data.size()).value();
410 m_cipher->set_key(
SymmetricKey(secret_key.begin(), m_params.dem_keylen()));
411 if(m_iv.empty() && !m_cipher->valid_nonce_length(m_iv.size())) {
412 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
414 m_cipher->start(m_iv.bits_of());
419 secure_vector<uint8_t> decrypted_data(encrypted_data.begin(), encrypted_data.end());
420 m_cipher->finish(decrypted_data);
421 return decrypted_data;
426 return secure_vector<uint8_t>();
#define BOTAN_DIAGNOSTIC_POP
#define BOTAN_DIAGNOSTIC_PUSH
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
#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
ECIES_KA_Params(const EC_Group &domain, std::string_view kdf_spec, size_t length, EC_Point_Format compression_type, ECIES_Flags flags)
size_t secret_length() const
bool old_cofactor_mode() const
EC_Point_Format compression_type() const
bool cofactor_mode() const
bool single_hash_mode() const
const std::string & kdf_spec() const
const EC_Group & domain() const
size_t dem_keylen() const
returns the length of the key used by the data encryption method
ECIES_System_Params(const EC_Group &domain, 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)
size_t mac_keylen() const
returns the length of the key used by the message authentication code
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
bool is_identity() const
Return true if this point is the identity element.
static std::optional< EC_AffinePoint > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
std::vector< uint8_t > serialize(EC_Point_Format format) const
Return an encoding depending on the requested format.
const BigInt & get_cofactor() const
bool has_cofactor() const
size_t get_order_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="")
secure_vector< uint8_t > bits_of() const
const uint8_t * end() const
const uint8_t * begin() const
SymmetricKey derive_key(size_t key_len, const uint8_t peer_key[], size_t peer_key_len, const uint8_t salt[], size_t salt_len) const
int(* final)(unsigned char *, CTX *)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector