updated build system
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
This commit is contained in:
@ -25,31 +25,46 @@
|
||||
|
||||
#include "utils/config.hpp"
|
||||
|
||||
namespace repertory::utils::encryption {
|
||||
using hash_256_t = std::array<unsigned char, 32U>;
|
||||
using hash_256_func_t = std::function<hash_256_t(std::string_view)>;
|
||||
using key_type = std::array<unsigned char, 32U>;
|
||||
#include "utils/hash.hpp"
|
||||
|
||||
namespace repertory::utils::encryption {
|
||||
inline constexpr const std::uint32_t encryption_header_size{
|
||||
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES +
|
||||
crypto_aead_xchacha20poly1305_IETF_ABYTES,
|
||||
};
|
||||
|
||||
[[nodiscard]] auto generate_key(std::string_view encryption_token) -> key_type;
|
||||
template <typename hash_t>
|
||||
[[nodiscard]] inline auto default_create_hash(std::string_view) -> hash_t;
|
||||
|
||||
template <typename hash_t>
|
||||
[[nodiscard]] inline auto default_create_hash(std::wstring_view) -> hash_t;
|
||||
|
||||
template <typename hash_t>
|
||||
inline auto generate_key(
|
||||
std::string_view password,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) -> hash_t;
|
||||
|
||||
template <typename hash_t>
|
||||
inline auto generate_key(
|
||||
std::wstring_view password,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) -> hash_t;
|
||||
|
||||
#if defined(PROJECT_ENABLE_BOOST)
|
||||
[[nodiscard]] auto decrypt_data(
|
||||
std::string_view data, std::string_view password,
|
||||
std::optional<hash_256_func_t> hasher = std::nullopt) -> data_buffer;
|
||||
[[nodiscard]] auto decrypt_data(std::string_view password,
|
||||
std::string_view data) -> data_buffer;
|
||||
|
||||
[[nodiscard]] auto encrypt_data(
|
||||
std::string_view data, std::string_view password,
|
||||
std::optional<hash_256_func_t> hasher = std::nullopt) -> data_buffer;
|
||||
[[nodiscard]] auto encrypt_data(std::string_view password,
|
||||
std::string_view data) -> data_buffer;
|
||||
|
||||
template <typename result>
|
||||
[[nodiscard]] inline auto
|
||||
decrypt_data(const key_type &key, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res) -> bool {
|
||||
template <typename result, typename arr_t, std::size_t arr_size>
|
||||
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
|
||||
const unsigned char *buffer,
|
||||
std::size_t buffer_size,
|
||||
result &res) -> bool {
|
||||
if (buffer_size > encryption_header_size) {
|
||||
const std::uint32_t size =
|
||||
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
|
||||
@ -65,34 +80,43 @@ decrypt_data(const key_type &key, const unsigned char *buffer,
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename buffer, typename result>
|
||||
[[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf,
|
||||
result &res) -> bool {
|
||||
template <typename buffer, typename result, typename arr_t,
|
||||
std::size_t arr_size>
|
||||
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
|
||||
const buffer &buf, result &res) -> bool {
|
||||
return decrypt_data<result>(
|
||||
key, reinterpret_cast<const unsigned char *>(buf.data()), buf.size(),
|
||||
res);
|
||||
}
|
||||
|
||||
template <typename buffer, typename result>
|
||||
[[nodiscard]] inline auto decrypt_data(std::string_view encryption_token,
|
||||
const buffer &buf, result &res) -> bool {
|
||||
return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res);
|
||||
template <typename buffer, typename result, typename hash_t = hash_256_t>
|
||||
[[nodiscard]] inline auto decrypt_data(
|
||||
std::string_view password, const buffer &buf, result &res,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) -> bool {
|
||||
return decrypt_data<buffer, result>(generate_key(password, hasher), buf, res);
|
||||
}
|
||||
|
||||
template <typename result>
|
||||
[[nodiscard]] inline auto
|
||||
decrypt_data(std::string_view encryption_token, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res) -> bool {
|
||||
return decrypt_data<result>(generate_key(encryption_token), buffer,
|
||||
template <typename result, typename hash_t = hash_256_t>
|
||||
[[nodiscard]] inline auto decrypt_data(
|
||||
std::string_view password, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res,
|
||||
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) -> bool {
|
||||
return decrypt_data<result>(generate_key(password, hasher), buffer,
|
||||
buffer_size, res);
|
||||
}
|
||||
|
||||
template <typename result>
|
||||
template <typename result, typename arr_t, std::size_t arr_size>
|
||||
inline void
|
||||
encrypt_data(const std::array<unsigned char,
|
||||
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
|
||||
const key_type &key, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res) {
|
||||
const std::array<arr_t, arr_size> &key,
|
||||
const unsigned char *buffer, std::size_t buffer_size,
|
||||
result &res) {
|
||||
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
|
||||
|
||||
const std::uint32_t size = boost::endian::native_to_big(
|
||||
@ -113,47 +137,116 @@ encrypt_data(const std::array<unsigned char,
|
||||
std::memcpy(&res[iv.size()], mac.data(), mac.size());
|
||||
}
|
||||
|
||||
template <typename result>
|
||||
inline void encrypt_data(const key_type &key, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res) {
|
||||
template <typename result, typename s, std::size_t t>
|
||||
inline void encrypt_data(const std::array<s, t> &key,
|
||||
const unsigned char *buffer, std::size_t buffer_size,
|
||||
result &res) {
|
||||
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{};
|
||||
randombytes_buf(iv.data(), iv.size());
|
||||
|
||||
encrypt_data<result>(iv, key, buffer, buffer_size, res);
|
||||
}
|
||||
|
||||
template <typename result>
|
||||
inline void encrypt_data(std::string_view encryption_token,
|
||||
const unsigned char *buffer, std::size_t buffer_size,
|
||||
result &res) {
|
||||
encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size,
|
||||
template <typename result, typename hash_t = hash_256_t>
|
||||
inline void encrypt_data(
|
||||
std::string_view password, const unsigned char *buffer,
|
||||
std::size_t buffer_size, result &res,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) {
|
||||
encrypt_data<result>(generate_key(password, hasher), buffer, buffer_size,
|
||||
res);
|
||||
}
|
||||
|
||||
template <typename buffer, typename result>
|
||||
inline void encrypt_data(std::string_view encryption_token, const buffer &buf,
|
||||
result &res) {
|
||||
encrypt_data<result>(generate_key(encryption_token),
|
||||
template <typename buffer, typename result, typename hash_t = hash_256_t>
|
||||
inline void encrypt_data(
|
||||
std::string_view password, const buffer &buf, result &res,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher = std::nullopt) {
|
||||
encrypt_data<result>(generate_key(password, hasher),
|
||||
reinterpret_cast<const unsigned char *>(buf.data()),
|
||||
buf.size(), res);
|
||||
}
|
||||
|
||||
template <typename buffer, typename result>
|
||||
inline void encrypt_data(const key_type &key, const buffer &buf, result &res) {
|
||||
template <typename buffer, typename result, typename s, std::size_t t>
|
||||
inline void encrypt_data(const std::array<s, t> &key, const buffer &buf,
|
||||
result &res) {
|
||||
encrypt_data<result>(key, reinterpret_cast<const unsigned char *>(buf.data()),
|
||||
buf.size(), res);
|
||||
}
|
||||
|
||||
template <typename buffer, typename result>
|
||||
template <typename buffer, typename result, typename s, std::size_t t>
|
||||
inline void
|
||||
encrypt_data(const std::array<unsigned char,
|
||||
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
|
||||
const key_type &key, const buffer &buf, result &res) {
|
||||
const std::array<s, t> &key, const buffer &buf, result &res) {
|
||||
encrypt_data<result>(iv, key,
|
||||
reinterpret_cast<const unsigned char *>(buf.data()),
|
||||
buf.size(), res);
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_BOOST)
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_256_t>(std::string_view data) -> hash_256_t {
|
||||
return create_hash_sha256(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_256_t>(std::wstring_view data) -> hash_256_t {
|
||||
return create_hash_sha256(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_384_t>(std::string_view data) -> hash_384_t {
|
||||
return create_hash_blake2b_384(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_384_t>(std::wstring_view data) -> hash_384_t {
|
||||
return create_hash_blake2b_384(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_512_t>(std::string_view data) -> hash_512_t {
|
||||
return create_hash_sha512(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline auto
|
||||
default_create_hash<hash_512_t>(std::wstring_view data) -> hash_512_t {
|
||||
return create_hash_sha512(data);
|
||||
}
|
||||
|
||||
template <typename hash_t>
|
||||
inline auto generate_key(
|
||||
std::string_view password,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher) -> hash_t {
|
||||
return hasher.has_value() ? (*hasher)(reinterpret_cast<const unsigned char *>(
|
||||
password.data()),
|
||||
password.size())
|
||||
: default_create_hash<hash_t>(password);
|
||||
}
|
||||
|
||||
template <typename hash_t>
|
||||
inline auto generate_key(
|
||||
std::wstring_view password,
|
||||
std::optional<
|
||||
std::function<hash_t(const unsigned char *data, std::size_t size)>>
|
||||
hasher) -> hash_t {
|
||||
return hasher.has_value()
|
||||
? (*hasher)(
|
||||
reinterpret_cast<const unsigned char *>(password.data()),
|
||||
password.size() * sizeof(std::wstring_view::value_type))
|
||||
: default_create_hash<hash_t>(password);
|
||||
}
|
||||
} // namespace repertory::utils::encryption
|
||||
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
||||
|
Reference in New Issue
Block a user