Botan 3.6.1
Crypto and TLS for C&
ecies.h
Go to the documentation of this file.
1/*
2* ECIES
3* (C) 2016 Philipp Weber
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_ECIES_H_
9#define BOTAN_ECIES_H_
10
11#include <botan/cipher_mode.h>
12#include <botan/ec_group.h>
13#include <botan/ec_point.h>
14#include <botan/ecdh.h>
15#include <botan/mac.h>
16#include <botan/pubkey.h>
17#include <botan/secmem.h>
18#include <botan/symkey.h>
19#include <memory>
20#include <string>
21#include <vector>
22
23namespace Botan {
24
25class RandomNumberGenerator;
26
27enum class ECIES_Flags : uint32_t {
28 None = 0,
29 /// if set: prefix the input of the (ecdh) key agreement with the encoded (ephemeral) public key
31 /// (decryption only) if set: use cofactor multiplication during (ecdh) key agreement
32 CofactorMode = 2,
33 /// if set: use ecdhc instead of ecdh
35 /// (decryption only) if set: test if the (ephemeral) public key is on the curve
36 CheckMode = 8,
37
38 NONE BOTAN_DEPRECATED("Use None") = None,
39 SINGLE_HASH_MODE BOTAN_DEPRECATED("Use SingleHashMode") = SingleHashMode,
40 COFACTOR_MODE BOTAN_DEPRECATED("Use CofactorMode") = CofactorMode,
41 OLD_COFACTOR_MODE BOTAN_DEPRECATED("Use OldCofactorMode") = OldCofactorMode,
42 CHECK_MODE BOTAN_DEPRECATED("Use CheckMode") = CheckMode,
43};
44
46 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
47 return static_cast<ECIES_Flags>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
48}
49
51 return static_cast<ECIES_Flags>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
52}
53
54/**
55* Parameters for ECIES secret derivation
56*/
58 public:
59 /**
60 * @param domain ec domain parameters of the involved ec keys
61 * @param kdf_spec name of the key derivation function
62 * @param length length of the secret to be derived
63 * @param compression_type format of encoded keys (affects the secret derivation if single_hash_mode is used)
64 * @param flags options, see documentation of ECIES_Flags
65 */
66 ECIES_KA_Params(const EC_Group& domain,
67 std::string_view kdf_spec,
68 size_t length,
69 EC_Point_Format compression_type,
70 ECIES_Flags flags);
71
74
75 virtual ~ECIES_KA_Params() = default;
76
77 inline const EC_Group& domain() const { return m_domain; }
78
79 inline size_t secret_length() const { return m_length; }
80
81 inline bool single_hash_mode() const {
82 return (m_flags & ECIES_Flags::SingleHashMode) == ECIES_Flags::SingleHashMode;
83 }
84
85 inline bool cofactor_mode() const { return (m_flags & ECIES_Flags::CofactorMode) == ECIES_Flags::CofactorMode; }
86
87 inline bool old_cofactor_mode() const {
88 return (m_flags & ECIES_Flags::OldCofactorMode) == ECIES_Flags::OldCofactorMode;
89 }
90
91 inline bool check_mode() const { return (m_flags & ECIES_Flags::CheckMode) == ECIES_Flags::CheckMode; }
92
93 inline EC_Point_Format compression_type() const { return m_compression_mode; }
94
95 const std::string& kdf_spec() const { return m_kdf_spec; }
96
97 private:
98 const EC_Group m_domain;
99 const std::string m_kdf_spec;
100 const size_t m_length;
101 const EC_Point_Format m_compression_mode;
102 const ECIES_Flags m_flags;
103};
104
106 public:
107 /**
108 * @param domain ec domain parameters of the involved ec keys
109 * @param kdf_spec name of the key derivation function
110 * @param dem_algo_spec name of the data encryption method
111 * @param dem_key_len length of the key used for the data encryption method
112 * @param mac_spec name of the message authentication code
113 * @param mac_key_len length of the key used for the message authentication code
114 */
115 ECIES_System_Params(const EC_Group& domain,
116 std::string_view kdf_spec,
117 std::string_view dem_algo_spec,
118 size_t dem_key_len,
119 std::string_view mac_spec,
120 size_t mac_key_len);
121
122 /**
123 * @param domain ec domain parameters of the involved ec keys
124 * @param kdf_spec name of the key derivation function
125 * @param dem_algo_spec name of the data encryption method
126 * @param dem_key_len length of the key used for the data encryption method
127 * @param mac_spec name of the message authentication code
128 * @param mac_key_len length of the key used for the message authentication code
129 * @param compression_type format of encoded keys (affects the secret derivation if single_hash_mode is used)
130 * @param flags options, see documentation of ECIES_Flags
131 */
132 ECIES_System_Params(const EC_Group& domain,
133 std::string_view kdf_spec,
134 std::string_view dem_algo_spec,
135 size_t dem_key_len,
136 std::string_view mac_spec,
137 size_t mac_key_len,
138 EC_Point_Format compression_type,
139 ECIES_Flags flags);
140
143 ~ECIES_System_Params() override = default;
144
145 /// creates an instance of the message authentication code
146 std::unique_ptr<MessageAuthenticationCode> create_mac() const;
147
148 /// creates an instance of the data encryption method
149 std::unique_ptr<Cipher_Mode> create_cipher(Cipher_Dir direction) const;
150
151 /// returns the length of the key used by the data encryption method
152 inline size_t dem_keylen() const { return m_dem_keylen; }
153
154 /// returns the length of the key used by the message authentication code
155 inline size_t mac_keylen() const { return m_mac_keylen; }
156
157 private:
158 const std::string m_dem_spec;
159 const size_t m_dem_keylen;
160 const std::string m_mac_spec;
161 const size_t m_mac_keylen;
162};
163
164/**
165* ECIES secret derivation according to ISO 18033-2
166*/
168 public:
169 /**
170 * @param private_key the (ephemeral) private key which is used to derive the secret
171 * @param ecies_params settings for ecies
172 * @param for_encryption disable cofactor mode if the secret will be used for encryption
173 * (according to ISO 18033 cofactor mode is only used during decryption)
174 * @param rng the RNG to use
175 */
176 ECIES_KA_Operation(const PK_Key_Agreement_Key& private_key,
177 const ECIES_KA_Params& ecies_params,
178 bool for_encryption,
180
181 /**
182 * Performs a key agreement with the provided keys and derives the secret from the result
183 * @param eph_public_key_bin the encoded (ephemeral) public key which belongs to the used (ephemeral) private key
184 * @param other_public_key_point public key point of the other party
185 */
186 SymmetricKey derive_secret(const std::vector<uint8_t>& eph_public_key_bin,
187 const EC_Point& other_public_key_point) const;
188
189 private:
190 const PK_Key_Agreement m_ka;
191 const ECIES_KA_Params m_params;
192};
193
194/**
195* ECIES Encryption according to ISO 18033-2
196*/
198 public:
199 /**
200 * @param private_key the (ephemeral) private key which is used for the key agreement
201 * @param ecies_params settings for ecies
202 * @param rng random generator to use
203 */
204 ECIES_Encryptor(const PK_Key_Agreement_Key& private_key,
205 const ECIES_System_Params& ecies_params,
207
208 /**
209 * Creates an ephemeral private key which is used for the key agreement
210 * @param rng random generator used during private key generation
211 * @param ecies_params settings for ecies
212 */
214
215 /// Set the public key of the other party
216 inline void set_other_key(const EC_Point& public_point) { m_other_point = public_point; }
217
218 /// Set the initialization vector for the data encryption method
219 inline void set_initialization_vector(const InitializationVector& iv) { m_iv = iv; }
220
221 /// Set the label which is appended to the input for the message authentication code
222 inline void set_label(std::string_view label) { m_label = std::vector<uint8_t>(label.begin(), label.end()); }
223
224 private:
225 std::vector<uint8_t> enc(const uint8_t data[], size_t length, RandomNumberGenerator&) const override;
226
227 size_t maximum_input_size() const override;
228
229 size_t ciphertext_length(size_t ptext_len) const override;
230
231 const ECIES_KA_Operation m_ka;
232 const ECIES_System_Params m_params;
233 std::unique_ptr<MessageAuthenticationCode> m_mac;
234 std::unique_ptr<Cipher_Mode> m_cipher;
235 std::vector<uint8_t> m_eph_public_key_bin;
237 EC_Point m_other_point;
238 std::vector<uint8_t> m_label;
239};
240
241/**
242* ECIES Decryption according to ISO 18033-2
243*/
245 public:
246 /**
247 * @param private_key the private key which is used for the key agreement
248 * @param ecies_params settings for ecies
249 * @param rng the random generator to use
250 */
251 ECIES_Decryptor(const PK_Key_Agreement_Key& private_key,
252 const ECIES_System_Params& ecies_params,
254
255 /// Set the initialization vector for the data encryption method
256 inline void set_initialization_vector(const InitializationVector& iv) { m_iv = iv; }
257
258 /// Set the label which is appended to the input for the message authentication code
259 inline void set_label(std::string_view label) { m_label = std::vector<uint8_t>(label.begin(), label.end()); }
260
261 private:
262 secure_vector<uint8_t> do_decrypt(uint8_t& valid_mask, const uint8_t in[], size_t in_len) const override;
263
264 size_t plaintext_length(size_t ctext_len) const override;
265
266 const ECIES_KA_Operation m_ka;
267 const ECIES_System_Params m_params;
268 std::unique_ptr<MessageAuthenticationCode> m_mac;
269 std::unique_ptr<Cipher_Mode> m_cipher;
271 std::vector<uint8_t> m_label;
272};
273
274} // namespace Botan
275
276#endif
void set_label(std::string_view label)
Set the label which is appended to the input for the message authentication code.
Definition ecies.h:259
void set_initialization_vector(const InitializationVector &iv)
Set the initialization vector for the data encryption method.
Definition ecies.h:256
void set_other_key(const EC_Point &public_point)
Set the public key of the other party.
Definition ecies.h:216
void set_initialization_vector(const InitializationVector &iv)
Set the initialization vector for the data encryption method.
Definition ecies.h:219
void set_label(std::string_view label)
Set the label which is appended to the input for the message authentication code.
Definition ecies.h:222
ECIES_KA_Params(const ECIES_KA_Params &)=default
bool check_mode() const
Definition ecies.h:91
size_t secret_length() const
Definition ecies.h:79
bool old_cofactor_mode() const
Definition ecies.h:87
EC_Point_Format compression_type() const
Definition ecies.h:93
bool cofactor_mode() const
Definition ecies.h:85
virtual ~ECIES_KA_Params()=default
bool single_hash_mode() const
Definition ecies.h:81
const std::string & kdf_spec() const
Definition ecies.h:95
const EC_Group & domain() const
Definition ecies.h:77
ECIES_KA_Params & operator=(const ECIES_KA_Params &)=delete
ECIES_System_Params(const ECIES_System_Params &)=default
~ECIES_System_Params() override=default
size_t dem_keylen() const
returns the length of the key used by the data encryption method
Definition ecies.h:152
size_t mac_keylen() const
returns the length of the key used by the message authentication code
Definition ecies.h:155
ECIES_System_Params & operator=(const ECIES_System_Params &)=delete
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
Definition asn1_obj.h:75
EC_Point_Format
Definition ec_point.h:19
ECIES_Flags
Definition ecies.h:27
@ CofactorMode
(decryption only) if set: use cofactor multiplication during (ecdh) key agreement
@ OldCofactorMode
if set: use ecdhc instead of ecdh
@ CheckMode
(decryption only) if set: test if the (ephemeral) public key is on the curve
@ SingleHashMode
if set: prefix the input of the (ecdh) key agreement with the encoded (ephemeral) public key
const SIMD_8x32 & b
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
@ NONE
Definition filter.h:165
ECIES_Flags operator&(ECIES_Flags a, ECIES_Flags b)
Definition ecies.h:50