1#ifndef LIBFILEZILLA_ENCRYPTION_HEADER
2#define LIBFILEZILLA_ENCRYPTION_HEADER
34 explicit operator bool()
const {
35 return key_.size() == key_size && salt_.size() == salt_size;
39 return key_ == rhs.key_ && salt_ == rhs.salt_;
43 return !(*
this == rhs);
47 return key_ < rhs.key_ || (key_ == rhs.key_ && salt_ < rhs.salt_);
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);
54 std::vector<uint8_t> key_;
55 std::vector<uint8_t> salt_;
75 min_iterations = 100000
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)
85 return from_password(std::vector<uint8_t>(password.begin(), password.end()), salt, iterations);
88 explicit operator bool()
const {
89 return key_.size() == key_size && salt_.size() == salt_size;
92 std::vector<uint8_t>
const& salt()
const {
102 std::string to_base64(
bool pad =
true)
const;
103 static private_key from_base64(std::string_view
const& base64);
106 std::vector<uint8_t> key_;
107 std::vector<uint8_t> salt_;
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);
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);
186 min_iterations = 100000
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)
196 return from_password(std::vector<uint8_t>(password.begin(), password.end()), salt, iterations);
199 explicit operator bool()
const {
200 return key_.size() == key_size && salt_.size() == salt_size;
203 std::vector<uint8_t>
const& salt()
const {
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);
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);
214 std::vector<uint8_t>
const& key()
const;
216 static size_t encryption_overhead();
218 std::vector<uint8_t> key_;
219 std::vector<uint8_t> salt_;
225 return !(lhs == rhs);
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);
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);
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.