libfilezilla
Loading...
Searching...
No Matches
encryption.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_ENCRYPTION_HEADER
2#define LIBFILEZILLA_ENCRYPTION_HEADER
3
12
13#include "libfilezilla.hpp"
14
15#include <vector>
16#include <string>
17#include <cstdint>
18
19namespace fz {
20
25class FZ_PUBLIC_SYMBOL public_key
26{
27public:
29 enum {
30 key_size = 32,
31 salt_size = 32
32 };
33
34 explicit operator bool() const {
35 return key_.size() == key_size && salt_.size() == salt_size;
36 }
37
38 bool operator==(public_key const& rhs) const {
39 return key_ == rhs.key_ && salt_ == rhs.salt_;
40 }
41
42 bool operator!=(public_key const& rhs) const {
43 return !(*this == rhs);
44 }
45
46 bool operator<(public_key const& rhs) const {
47 return key_ < rhs.key_ || (key_ == rhs.key_ && salt_ < rhs.salt_);
48 }
49
50 std::string to_base64(bool pad = true) const;
51 static public_key from_base64(std::string_view const& base64);
52 static public_key from_base64(std::wstring_view const& base64);
53
54 std::vector<uint8_t> key_;
55 std::vector<uint8_t> salt_;
56};
57
62class FZ_PUBLIC_SYMBOL private_key
63{
64public:
66 enum {
67 key_size = 32,
68 salt_size = 32
69 };
70
73
74 enum {
75 min_iterations = 100000
76 };
77
82 static private_key from_password(std::vector<uint8_t> const& password, std::vector<uint8_t> const& salt, unsigned int iterations = min_iterations);
83 static private_key from_password(std::string_view const& password, std::vector<uint8_t> const& salt, unsigned int iterations = min_iterations)
84 {
85 return from_password(std::vector<uint8_t>(password.begin(), password.end()), salt, iterations);
86 }
87
88 explicit operator bool() const {
89 return key_.size() == key_size && salt_.size() == salt_size;
90 }
91
92 std::vector<uint8_t> const& salt() const {
93 return salt_;
94 }
95
98
100 std::vector<uint8_t> shared_secret(public_key const& pub) const;
101
102 std::string to_base64(bool pad = true) const;
103 static private_key from_base64(std::string_view const& base64);
104
105private:
106 std::vector<uint8_t> key_;
107 std::vector<uint8_t> salt_;
108};
109
131std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::vector<uint8_t> const& plain, public_key const& pub, bool authenticated = true);
132std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::string_view const& plain, public_key const& pub, bool authenticated = true);
133std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(uint8_t const* plain, size_t size, public_key const& pub, bool authenticated = true);
134std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::vector<uint8_t> const& plain, public_key const& pub, std::vector<uint8_t> const& authenticated_data);
135std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::string_view const& plain, public_key const& pub, std::string_view const& authenticated_data);
136std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(uint8_t const* plain, size_t size, public_key const& pub, uint8_t const* authenticated_data, size_t authenticated_data_size);
137
163std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::vector<uint8_t> const& chiper, private_key const& priv, bool authenticated = true);
164std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::string_view const& chiper, private_key const& priv, bool authenticated = true);
165std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(uint8_t const* cipher, size_t size, private_key const& priv, bool authenticated = true);
166std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::vector<uint8_t> const& cipher, private_key const& priv, std::vector<uint8_t> const& authenticated_data);
167std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::string_view const& cipher, private_key const& priv, std::string_view const& authenticated_data);
168std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(uint8_t const* cipher, size_t size, private_key const& priv, uint8_t const* authenticated_data, size_t authenticated_data_size);
169
173class FZ_PUBLIC_SYMBOL symmetric_key
174{
175public:
177 enum {
178 key_size = 32,
179 salt_size = 32
180 };
181
184
185 enum {
186 min_iterations = 100000
187 };
188
193 static symmetric_key from_password(std::vector<uint8_t> const& password, std::vector<uint8_t> const& salt, unsigned int iterations = min_iterations);
194 static symmetric_key from_password(std::string_view const& password, std::vector<uint8_t> const& salt, unsigned int iterations = min_iterations)
195 {
196 return from_password(std::vector<uint8_t>(password.begin(), password.end()), salt, iterations);
197 }
198
199 explicit operator bool() const {
200 return key_.size() == key_size && salt_.size() == salt_size;
201 }
202
203 std::vector<uint8_t> const& salt() const {
204 return salt_;
205 }
206
207 std::string to_base64(bool pad = true) const;
208 static symmetric_key from_base64(std::string_view const& base64);
209 static symmetric_key from_base64(std::wstring_view const& base64);
210
211 std::vector<uint8_t> encrypt_key(fz::public_key const& kek);
212 static symmetric_key decrypt_key(std::vector<uint8_t> const& encrypted, fz::private_key const& kek);
213
214 std::vector<uint8_t> const& key() const;
215
216 static size_t encryption_overhead();
217private:
218 std::vector<uint8_t> key_;
219 std::vector<uint8_t> salt_;
220};
221
223bool FZ_PUBLIC_SYMBOL operator==(symmetric_key const& lhs, symmetric_key const& rhs);
224inline bool FZ_PUBLIC_SYMBOL operator!=(symmetric_key const& lhs, symmetric_key const& rhs) {
225 return !(lhs == rhs);
226}
227
242std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::vector<uint8_t> const& plain, symmetric_key const& key);
243std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::string_view const& plain, symmetric_key const& key);
244std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(uint8_t const* plain, size_t size, symmetric_key const& key);
245std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::vector<uint8_t> const& plain, symmetric_key const& key, std::vector<uint8_t> const& authenticated_data);
246std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(std::string_view const& plain, symmetric_key const& key, std::string_view const& authenticated_data);
247std::vector<uint8_t> FZ_PUBLIC_SYMBOL encrypt(uint8_t const* plain, size_t size, symmetric_key const& key, uint8_t const* authenticated_data, size_t authenticated_data_size);
248
268std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::vector<uint8_t> const& chiper, symmetric_key const& key);
269std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::string_view const& chiper, symmetric_key const& key);
270std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(uint8_t const* cipher, size_t size, symmetric_key const& key);
271std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::vector<uint8_t> const& cipher, symmetric_key const& key, std::vector<uint8_t> const& authenticated_data);
272std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(std::string_view const& cipher, symmetric_key const& key, std::string_view const& authenticated_data);
273std::vector<uint8_t> FZ_PUBLIC_SYMBOL decrypt(uint8_t const* cipher, size_t size, symmetric_key const& key, uint8_t const* authenticated_data, size_t authenticated_data_size);
274
275}
276#endif
Represents a X25519 private key with associated salt.
Definition encryption.hpp:63
public_key pubkey() const
Calculates the public key corresponding to the private key.
static private_key from_password(std::vector< uint8_t > const &password, std::vector< uint8_t > const &salt, unsigned int iterations=min_iterations)
Derives a symmetric key using PBKDF2-SHA256 from the given password and salt.
static private_key generate()
Generates a random private key.
std::vector< uint8_t > shared_secret(public_key const &pub) const
Calculates a shared secret using Elliptic Curve Diffie-Hellman on Curve25519 (X25519)
Represents a X25519 public key with associated salt.
Definition encryption.hpp:26
Symmetric encryption key with associated salt.
Definition encryption.hpp:174
static symmetric_key from_password(std::vector< uint8_t > const &password, std::vector< uint8_t > const &salt, unsigned int iterations=min_iterations)
Derives a symmetric key using PBKDF2-SHA256 from the given password and salt.
static symmetric_key generate()
Generates a random symmetric key.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
std::vector< uint8_t > encrypt(std::vector< uint8_t > const &plain, public_key const &pub, bool authenticated=true)
Encrypt the plaintext to the given public key.
std::vector< uint8_t > decrypt(std::vector< uint8_t > const &chiper, private_key const &priv, bool authenticated=true)
Decrypt the ciphertext using the given private key.
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.