9#include <botan/ecies.h>
11#include <botan/cipher_mode.h>
13#include <botan/numthry.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/pk_ops_impl.h>
16#include <botan/internal/stl_util.h>
29class ECIES_PrivateKey
final :
public EC_PrivateKey,
30 public PK_Key_Agreement_Key {
32 explicit ECIES_PrivateKey(
const ECDH_PrivateKey& private_key) :
33 EC_PublicKey(private_key), EC_PrivateKey(private_key), PK_Key_Agreement_Key(), m_key(private_key) {}
35 std::vector<uint8_t> public_value()
const override {
return m_key.public_value(); }
37 std::string algo_name()
const override {
return "ECIES"; }
39 std::unique_ptr<Public_Key> public_key()
const override {
return m_key.public_key(); }
43 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng)
const override {
44 return m_key.generate_another(rng);
47 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
48 std::string_view params,
49 std::string_view provider)
const override;
52 ECDH_PrivateKey m_key;
60class ECIES_ECDH_KA_Operation
final :
public PK_Ops::Key_Agreement_with_KDF {
62 ECIES_ECDH_KA_Operation(
const ECIES_PrivateKey& private_key, RandomNumberGenerator& rng) :
63 PK_Ops::Key_Agreement_with_KDF(
"Raw"), m_key(private_key), m_rng(rng) {}
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 {
68 const EC_Group& group = m_key.domain();
70 EC_Point input_point = group.OS2ECP(w, w_len);
71 input_point.randomize_repr(m_rng);
73 const EC_Point S = group.blinded_var_point_multiply(input_point, m_key.private_value(), m_rng, m_ws);
75 if(S.on_the_curve() ==
false) {
82 ECIES_PrivateKey m_key;
83 RandomNumberGenerator& m_rng;
84 std::vector<BigInt> m_ws;
87std::unique_ptr<PK_Ops::Key_Agreement> ECIES_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
89 std::string_view )
const {
90 return std::make_unique<ECIES_ECDH_KA_Operation>(*
this, rng);
102PK_Key_Agreement create_key_agreement(
const PK_Key_Agreement_Key& private_key,
103 const ECIES_KA_Params& ecies_params,
105 RandomNumberGenerator& rng) {
106 const ECDH_PrivateKey* ecdh_key =
dynamic_cast<const ECDH_PrivateKey*
>(&private_key);
108 if(ecdh_key ==
nullptr &&
109 (ecies_params.cofactor_mode() || ecies_params.old_cofactor_mode() || ecies_params.check_mode())) {
114 throw Invalid_Argument(
"ECIES: cofactor, old cofactor and check mode are only supported for ECDH_PrivateKey");
117 if(ecdh_key && (for_encryption || !ecies_params.cofactor_mode())) {
119 return PK_Key_Agreement(ECIES_PrivateKey(*ecdh_key), rng,
"Raw");
122 return PK_Key_Agreement(private_key, rng,
"Raw");
130 m_ka(create_key_agreement(private_key, ecies_params, for_encryption, rng)), m_params(ecies_params) {}
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());
170 std::string_view kdf_spec,
174 m_domain(domain), m_kdf_spec(kdf_spec), m_length(length), m_compression_mode(compression_type), m_flags(flags) {}
177 std::string_view kdf_spec,
178 std::string_view dem_algo_spec,
180 std::string_view mac_spec,
184 ECIES_KA_Params(domain, kdf_spec, dem_key_len + mac_key_len, compression_type, flags),
185 m_dem_spec(dem_algo_spec),
186 m_dem_keylen(dem_key_len),
187 m_mac_spec(mac_spec),
188 m_mac_keylen(mac_key_len) {
191 throw Invalid_Argument(
"ECIES: only one of cofactor_mode, old_cofactor_mode and check_mode can be set");
196 std::string_view kdf_spec,
197 std::string_view dem_algo_spec,
199 std::string_view mac_spec,
200 size_t mac_key_len) :
224 m_ka(private_key, ecies_params, true, rng),
225 m_params(ecies_params),
226 m_eph_public_key_bin(private_key.public_value()),
245size_t ECIES_Encryptor::maximum_input_size()
const {
253size_t ECIES_Encryptor::ciphertext_length(
size_t ptext_len)
const {
254 return m_eph_public_key_bin.size() + m_mac->output_length() + m_cipher->output_length(ptext_len);
260std::vector<uint8_t> ECIES_Encryptor::enc(
const uint8_t data[],
262 RandomNumberGenerator& )
const {
264 throw Invalid_State(
"ECIES: the other key is zero");
272 if(m_iv.
empty() && !m_cipher->valid_nonce_length(m_iv.
size())) {
273 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
276 m_cipher->start(m_iv.
bits_of());
278 secure_vector<uint8_t> encrypted_data(data, data + length);
279 m_cipher->finish(encrypted_data);
283 m_mac->update(encrypted_data);
284 if(!m_label.empty()) {
285 m_mac->update(m_label);
287 const auto mac = m_mac->final();
290 return concat(m_eph_public_key_bin, encrypted_data, mac);
296 m_ka(key, ecies_params, false, rng), m_params(ecies_params), m_iv(), m_label() {
301 throw Invalid_Argument(
"ECIES: gcd of cofactor and order must be 1 if check_mode is 0");
309size_t ECIES_Decryptor::plaintext_length(
size_t ctext_len)
const {
311 const size_t overhead = point_size + m_mac->output_length();
313 if(ctext_len < overhead) {
317 return m_cipher->output_length(ctext_len - overhead);
323secure_vector<uint8_t> ECIES_Decryptor::do_decrypt(uint8_t& valid_mask,
const uint8_t in[],
size_t in_len)
const {
326 if(in_len < point_size + m_mac->output_length()) {
327 throw Decoding_Error(
"ECIES decryption: ciphertext is too short");
331 const std::vector<uint8_t> other_public_key_bin(in, in + point_size);
332 const std::vector<uint8_t> encrypted_data(in + point_size, in + in_len - m_mac->output_length());
333 const std::vector<uint8_t> mac_data(in + in_len - m_mac->output_length(), in + in_len);
336 EC_Point other_public_key = m_params.
domain().
OS2ECP(other_public_key_bin);
339 if(m_params.
check_mode() && !other_public_key.on_the_curve()) {
340 throw Decoding_Error(
"ECIES decryption: received public key is not on the curve");
349 m_mac->update(encrypted_data);
350 if(!m_label.empty()) {
351 m_mac->update(m_label);
353 const secure_vector<uint8_t> calculated_mac = m_mac->final();
354 valid_mask =
CT::is_equal(mac_data.data(), calculated_mac.data(), mac_data.size()).value();
360 if(m_iv.
empty() && !m_cipher->valid_nonce_length(m_iv.
size())) {
361 throw Invalid_Argument(
"ECIES with " + m_cipher->name() +
" requires an IV be set");
363 m_cipher->start(m_iv.
bits_of());
368 secure_vector<uint8_t> decrypted_data(encrypted_data.begin(), encrypted_data.end());
369 m_cipher->finish(decrypted_data);
370 return decrypted_data;
375 return secure_vector<uint8_t>();
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
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)
SymmetricKey derive_secret(const std::vector< uint8_t > &eph_public_key_bin, const EC_Point &other_public_key_point) const
ECIES_KA_Operation(const PK_Key_Agreement_Key &private_key, const ECIES_KA_Params &ecies_params, bool for_encryption, RandomNumberGenerator &rng)
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
const BigInt & get_cofactor() const
const BigInt & get_order() const
size_t point_size(EC_Point_Format format) const
EC_Point OS2ECP(const uint8_t bits[], size_t len) const
std::vector< uint8_t > encode(EC_Point_Format format) const
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 in[], size_t in_len, const uint8_t params[], size_t params_len) const
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
#define BOTAN_DIAGNOSTIC_PUSH
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
BigInt gcd(const BigInt &a, const BigInt &b)
std::vector< T, secure_allocator< T > > secure_vector
decltype(auto) concat(Ts &&... buffers)