feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback
This commit is contained in:
1 parent
b50131f52e
commit
22c8538754
19 files changed
+3326
-133
No files matched your search
+297
-24
@@ -1,19 +1,24 @@
|
||||
#include <Void/Crypto/Crypto.hpp>
|
||||
|
||||
#include "BuiltinHash.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/params.h>
|
||||
#endif
|
||||
|
||||
namespace Void::Crypto
|
||||
{
|
||||
@@ -42,6 +47,36 @@ namespace Void::Crypto
|
||||
std::format("{}: injected crypto backend fault", operation));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool spans_overlap(std::span<const std::byte> a,
|
||||
std::span<const std::byte> b) noexcept
|
||||
{
|
||||
if (a.empty() || b.empty()) {
|
||||
return false;
|
||||
}
|
||||
const auto a_begin = reinterpret_cast<std::uintptr_t>(a.data());
|
||||
const auto a_end = a_begin + a.size();
|
||||
const auto b_begin = reinterpret_cast<std::uintptr_t>(b.data());
|
||||
const auto b_end = b_begin + b.size();
|
||||
return a_begin < b_end && b_begin < a_end;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool use_builtin_hash(
|
||||
HashAlgorithm algorithm, const ProviderOptions& provider) noexcept
|
||||
{
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (provider.prefer_builtin ||
|
||||
algorithm == HashAlgorithm::Blake2b256) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
(void)algorithm;
|
||||
(void)provider;
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
[[nodiscard]] const char*
|
||||
algorithm_name(HashAlgorithm algorithm) noexcept
|
||||
{
|
||||
@@ -52,6 +87,10 @@ namespace Void::Crypto
|
||||
return "SHA256";
|
||||
case HashAlgorithm::Sha512:
|
||||
return "SHA512";
|
||||
case HashAlgorithm::Blake2b256:
|
||||
return nullptr;
|
||||
case HashAlgorithm::Blake2b512:
|
||||
return "BLAKE2B512";
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -78,19 +117,6 @@ namespace Void::Crypto
|
||||
code, std::format("{}: {}", operation, buffer.data()));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool spans_overlap(std::span<const std::byte> a,
|
||||
std::span<const std::byte> b) noexcept
|
||||
{
|
||||
if (a.empty() || b.empty()) {
|
||||
return false;
|
||||
}
|
||||
const auto a_begin = reinterpret_cast<std::uintptr_t>(a.data());
|
||||
const auto a_end = a_begin + a.size();
|
||||
const auto b_begin = reinterpret_cast<std::uintptr_t>(b.data());
|
||||
const auto b_end = b_begin + b.size();
|
||||
return a_begin < b_end && b_begin < a_end;
|
||||
}
|
||||
|
||||
using EvpMdPtr = std::unique_ptr<EVP_MD, decltype(&EVP_MD_free)>;
|
||||
using EvpMdCtxPtr =
|
||||
std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
|
||||
@@ -110,6 +136,7 @@ namespace Void::Crypto
|
||||
? &kEmptyByte
|
||||
: reinterpret_cast<const unsigned char*>(span.data());
|
||||
}
|
||||
#endif
|
||||
|
||||
[[nodiscard]] Void::Result::Result<bool>
|
||||
verify_ed25519_impl(std::span<const std::byte> message,
|
||||
@@ -144,6 +171,12 @@ namespace Void::Crypto
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (provider.prefer_builtin) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::verifyEd25519: Ed25519 requires OpenSSL backend")};
|
||||
}
|
||||
ERR_clear_error();
|
||||
const char* propq = propq_or_null(provider);
|
||||
EvpPkeyPtr key{EVP_PKEY_new_raw_public_key_ex(
|
||||
@@ -182,14 +215,33 @@ namespace Void::Crypto
|
||||
}
|
||||
ERR_clear_error();
|
||||
return result == 1;
|
||||
#else
|
||||
(void)provider;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::verifyEd25519: built without OpenSSL backend")};
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool has_openssl_backend() noexcept
|
||||
{
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void secure_wipe(std::span<std::byte> buffer) noexcept
|
||||
{
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (!buffer.empty()) {
|
||||
OPENSSL_cleanse(buffer.data(), buffer.size());
|
||||
}
|
||||
#else
|
||||
Detail::builtin_secure_wipe(buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool constant_time_equal(std::span<const std::byte> lhs,
|
||||
@@ -201,7 +253,11 @@ namespace Void::Crypto
|
||||
if (lhs.empty()) {
|
||||
return true;
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
return CRYPTO_memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
|
||||
#else
|
||||
return Detail::builtin_constant_time_equal(lhs, rhs);
|
||||
#endif
|
||||
}
|
||||
|
||||
ScopedFaultGuard::ScopedFaultGuard(FaultConfig config) noexcept
|
||||
@@ -218,8 +274,12 @@ namespace Void::Crypto
|
||||
}
|
||||
|
||||
struct Hasher::Impl {
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
EvpMdCtxPtr context{nullptr, &EVP_MD_CTX_free};
|
||||
EvpMdPtr algorithm{nullptr, &EVP_MD_free};
|
||||
#endif
|
||||
Detail::BuiltinHasher builtin{};
|
||||
bool using_builtin{false};
|
||||
HasherState state{HasherState::Ready};
|
||||
};
|
||||
|
||||
@@ -244,18 +304,50 @@ namespace Void::Crypto
|
||||
Void::Result::Result<Hasher>
|
||||
Hasher::create(HashAlgorithm algorithm, const ProviderOptions& provider)
|
||||
{
|
||||
const char* md_name = algorithm_name(algorithm);
|
||||
if (md_name == nullptr) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Hasher: unsupported algorithm")};
|
||||
switch (algorithm) {
|
||||
case HashAlgorithm::Sha1:
|
||||
case HashAlgorithm::Sha256:
|
||||
case HashAlgorithm::Sha512:
|
||||
case HashAlgorithm::Blake2b256:
|
||||
case HashAlgorithm::Blake2b512:
|
||||
break;
|
||||
default:
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Hasher: unsupported algorithm")};
|
||||
}
|
||||
|
||||
if (auto fault = maybe_inject_fault(FaultOperation::hash_create,
|
||||
"Crypto::Hasher::create");
|
||||
fault.has_value()) {
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
|
||||
if (use_builtin_hash(algorithm, provider)) {
|
||||
if (!provider.property_query.empty()) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::provider_error,
|
||||
"Crypto::Hasher: property_query unavailable on built-in "
|
||||
"backend")};
|
||||
}
|
||||
auto impl = std::make_unique<Impl>();
|
||||
if (!impl->builtin.init(algorithm)) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::Hasher: built-in initialization failed")};
|
||||
}
|
||||
impl->using_builtin = true;
|
||||
impl->state = HasherState::Ready;
|
||||
return Hasher{std::move(impl)};
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
const char* md_name = algorithm_name(algorithm);
|
||||
if (md_name == nullptr) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Hasher: unsupported OpenSSL digest algorithm")};
|
||||
}
|
||||
ERR_clear_error();
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->algorithm = EvpMdPtr{
|
||||
@@ -278,8 +370,14 @@ namespace Void::Crypto
|
||||
CryptoErrorCode::provider_error,
|
||||
"Crypto::Hasher: digest initialization failed")};
|
||||
}
|
||||
impl->using_builtin = false;
|
||||
impl->state = HasherState::Ready;
|
||||
return Hasher{std::move(impl)};
|
||||
#else
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::Hasher: OpenSSL backend unavailable")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<void> Hasher::update(std::span<const std::byte> data)
|
||||
@@ -301,6 +399,12 @@ namespace Void::Crypto
|
||||
m_impl->state = HasherState::Failed;
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
if (m_impl->using_builtin) {
|
||||
m_impl->builtin.update(data);
|
||||
m_impl->state = HasherState::Updating;
|
||||
return {};
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (!data.empty() &&
|
||||
EVP_DigestUpdate(m_impl->context.get(), data.data(),
|
||||
data.size()) != 1) {
|
||||
@@ -311,6 +415,12 @@ namespace Void::Crypto
|
||||
}
|
||||
m_impl->state = HasherState::Updating;
|
||||
return {};
|
||||
#else
|
||||
m_impl->state = HasherState::Failed;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::Hasher::update: invalid backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<void> Hasher::update(std::string_view data)
|
||||
@@ -337,6 +447,12 @@ namespace Void::Crypto
|
||||
m_impl->state = HasherState::Failed;
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
if (m_impl->using_builtin) {
|
||||
Digest digest = m_impl->builtin.finalize();
|
||||
m_impl->state = HasherState::Finalized;
|
||||
return digest;
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
const int digest_size = EVP_MD_get_size(m_impl->algorithm.get());
|
||||
if (digest_size <= 0) {
|
||||
m_impl->state = HasherState::Failed;
|
||||
@@ -357,6 +473,12 @@ namespace Void::Crypto
|
||||
m_impl->state = HasherState::Finalized;
|
||||
digest.resize(written);
|
||||
return digest;
|
||||
#else
|
||||
m_impl->state = HasherState::Failed;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::Hasher::finalize: invalid backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
auto hash(HashAlgorithm algorithm, std::span<const std::byte> data)
|
||||
@@ -388,18 +510,31 @@ namespace Void::Crypto
|
||||
auto hashFile(HashAlgorithm algorithm, const std::filesystem::path& path)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
return hashFile(algorithm, path, ProviderOptions{});
|
||||
return hash_file(algorithm, path, ProviderOptions{});
|
||||
}
|
||||
|
||||
auto hashFile(HashAlgorithm algorithm, const std::filesystem::path& path,
|
||||
const ProviderOptions& provider)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
return hash_file(algorithm, path, provider);
|
||||
}
|
||||
|
||||
auto hash_file(HashAlgorithm algorithm, const std::filesystem::path& path)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
return hash_file(algorithm, path, ProviderOptions{});
|
||||
}
|
||||
|
||||
auto hash_file(HashAlgorithm algorithm, const std::filesystem::path& path,
|
||||
const ProviderOptions& provider)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
std::ifstream input{path, std::ios::binary};
|
||||
if (!input.is_open()) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::io_error,
|
||||
std::format("Crypto::hashFile: cannot open '{}'",
|
||||
std::format("Crypto::hash_file: cannot open '{}'",
|
||||
path.string()))};
|
||||
}
|
||||
auto hasher = Hasher::create(algorithm, provider);
|
||||
@@ -422,7 +557,7 @@ namespace Void::Crypto
|
||||
if (input.bad() || (!input.eof() && input.fail())) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::io_error,
|
||||
std::format("Crypto::hashFile: I/O error reading '{}'",
|
||||
std::format("Crypto::hash_file: I/O error reading '{}'",
|
||||
path.string()))};
|
||||
}
|
||||
return hasher->finalize();
|
||||
@@ -452,21 +587,63 @@ namespace Void::Crypto
|
||||
{
|
||||
return hash(HashAlgorithm::Sha512, data);
|
||||
}
|
||||
Void::Result::Result<Digest> blake2b256(std::span<const std::byte> data)
|
||||
{
|
||||
return hash(HashAlgorithm::Blake2b256, data);
|
||||
}
|
||||
Void::Result::Result<Digest> blake2b256(std::string_view data)
|
||||
{
|
||||
return hash(HashAlgorithm::Blake2b256, data);
|
||||
}
|
||||
Void::Result::Result<Digest> blake2b512(std::span<const std::byte> data)
|
||||
{
|
||||
return hash(HashAlgorithm::Blake2b512, data);
|
||||
}
|
||||
Void::Result::Result<Digest> blake2b512(std::string_view data)
|
||||
{
|
||||
return hash(HashAlgorithm::Blake2b512, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha1File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha1, path);
|
||||
return hash_file(HashAlgorithm::Sha1, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha256File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha256, path);
|
||||
return hash_file(HashAlgorithm::Sha256, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha512File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha512, path);
|
||||
return hash_file(HashAlgorithm::Sha512, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha1_file(const std::filesystem::path& path)
|
||||
{
|
||||
return hash_file(HashAlgorithm::Sha1, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha256_file(const std::filesystem::path& path)
|
||||
{
|
||||
return hash_file(HashAlgorithm::Sha256, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha512_file(const std::filesystem::path& path)
|
||||
{
|
||||
return hash_file(HashAlgorithm::Sha512, path);
|
||||
}
|
||||
Void::Result::Result<Digest>
|
||||
blake2b256_file(const std::filesystem::path& path)
|
||||
{
|
||||
return hash_file(HashAlgorithm::Blake2b256, path);
|
||||
}
|
||||
Void::Result::Result<Digest>
|
||||
blake2b512_file(const std::filesystem::path& path)
|
||||
{
|
||||
return hash_file(HashAlgorithm::Blake2b512, path);
|
||||
}
|
||||
|
||||
struct Ed25519PrivateKey::Impl {
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
EvpPkeyPtr pkey{nullptr, &EVP_PKEY_free};
|
||||
#else
|
||||
std::uint8_t unused{0};
|
||||
#endif
|
||||
};
|
||||
|
||||
Ed25519PrivateKey::Ed25519PrivateKey(std::unique_ptr<Impl> impl) noexcept
|
||||
@@ -481,7 +658,11 @@ namespace Void::Crypto
|
||||
|
||||
bool Ed25519PrivateKey::valid() const noexcept
|
||||
{
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
return m_impl != nullptr && m_impl->pkey != nullptr;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Ed25519PrivateKey>
|
||||
@@ -492,6 +673,12 @@ namespace Void::Crypto
|
||||
fault.has_value()) {
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (provider.prefer_builtin) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::generate requires OpenSSL backend")};
|
||||
}
|
||||
ERR_clear_error();
|
||||
EvpPkeyPtr key{
|
||||
EVP_PKEY_Q_keygen(nullptr, propq_or_null(provider), "ED25519"),
|
||||
@@ -504,6 +691,13 @@ namespace Void::Crypto
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->pkey = std::move(key);
|
||||
return Ed25519PrivateKey{std::move(impl)};
|
||||
#else
|
||||
(void)provider;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::generate: built without OpenSSL "
|
||||
"backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Ed25519PrivateKey>
|
||||
@@ -523,6 +717,13 @@ namespace Void::Crypto
|
||||
fault.has_value()) {
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (provider.prefer_builtin) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::from_raw_seed requires OpenSSL "
|
||||
"backend")};
|
||||
}
|
||||
ERR_clear_error();
|
||||
EvpPkeyPtr key{EVP_PKEY_new_raw_private_key_ex(
|
||||
nullptr, "ED25519", propq_or_null(provider),
|
||||
@@ -536,6 +737,13 @@ namespace Void::Crypto
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->pkey = std::move(key);
|
||||
return Ed25519PrivateKey{std::move(impl)};
|
||||
#else
|
||||
(void)provider;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::from_raw_seed: built without OpenSSL "
|
||||
"backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Ed25519PublicKey> Ed25519PrivateKey::public_key() const
|
||||
@@ -546,6 +754,7 @@ namespace Void::Crypto
|
||||
"Crypto::Ed25519PrivateKey::public_key: invalid or moved-from "
|
||||
"key")};
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
Ed25519PublicKey pub;
|
||||
std::size_t len = pub.bytes.size();
|
||||
if (EVP_PKEY_get_raw_public_key(
|
||||
@@ -558,6 +767,12 @@ namespace Void::Crypto
|
||||
"Crypto::Ed25519PrivateKey::public_key failed")};
|
||||
}
|
||||
return pub;
|
||||
#else
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::public_key: built without OpenSSL "
|
||||
"backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Ed25519Seed> Ed25519PrivateKey::export_raw_seed() const
|
||||
@@ -568,6 +783,7 @@ namespace Void::Crypto
|
||||
"Crypto::Ed25519PrivateKey::export_raw_seed: invalid or "
|
||||
"moved-from key")};
|
||||
}
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
std::array<std::byte, Ed25519SeedSize> raw{};
|
||||
std::size_t len = raw.size();
|
||||
if (EVP_PKEY_get_raw_private_key(
|
||||
@@ -582,6 +798,12 @@ namespace Void::Crypto
|
||||
auto result = Ed25519Seed::from_bytes(raw);
|
||||
secure_wipe(raw);
|
||||
return result;
|
||||
#else
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::Ed25519PrivateKey::export_raw_seed: built without OpenSSL "
|
||||
"backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Ed25519Signature>
|
||||
@@ -607,6 +829,12 @@ namespace Void::Crypto
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
if (provider.prefer_builtin) {
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::sign_ed25519 requires OpenSSL backend")};
|
||||
}
|
||||
ERR_clear_error();
|
||||
EvpMdCtxPtr context{EVP_MD_CTX_new(), &EVP_MD_CTX_free};
|
||||
if (!context) {
|
||||
@@ -635,6 +863,12 @@ namespace Void::Crypto
|
||||
"Crypto::sign_ed25519 failed")};
|
||||
}
|
||||
return signature;
|
||||
#else
|
||||
(void)provider;
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::unsupported_algorithm,
|
||||
"Crypto::sign_ed25519: built without OpenSSL backend")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<bool> verifyEd25519(std::span<const std::byte> message,
|
||||
@@ -682,6 +916,19 @@ namespace Void::Crypto
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
|
||||
if (use_builtin_hash(HashAlgorithm::Sha256, provider)) {
|
||||
if (!provider.property_query.empty()) {
|
||||
secure_wipe(output);
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::provider_error,
|
||||
"Crypto::hmac_sha256_into: property_query unavailable on "
|
||||
"built-in backend")};
|
||||
}
|
||||
Detail::builtin_hmac_sha256(secret_key, message, output);
|
||||
return {};
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
ERR_clear_error();
|
||||
EvpMacPtr mac{EVP_MAC_fetch(nullptr, "HMAC", propq_or_null(provider)),
|
||||
&EVP_MAC_free};
|
||||
@@ -732,6 +979,12 @@ namespace Void::Crypto
|
||||
"Crypto::hmac_sha256_into: EVP_MAC_final failed")};
|
||||
}
|
||||
return {};
|
||||
#else
|
||||
secure_wipe(output);
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::hmac_sha256_into: OpenSSL backend unavailable")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<Digest> hmac_sha256(std::span<const std::byte> secret_key,
|
||||
@@ -780,6 +1033,20 @@ namespace Void::Crypto
|
||||
return std::unexpected{std::move(*fault)};
|
||||
}
|
||||
|
||||
if (use_builtin_hash(HashAlgorithm::Sha256, provider)) {
|
||||
if (!provider.property_query.empty()) {
|
||||
secure_wipe(secret_output);
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::provider_error,
|
||||
"Crypto::hkdf_sha256: property_query unavailable on "
|
||||
"built-in backend")};
|
||||
}
|
||||
Detail::builtin_hkdf_sha256(secret_input, salt, context,
|
||||
secret_output);
|
||||
return {};
|
||||
}
|
||||
|
||||
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
|
||||
ERR_clear_error();
|
||||
EvpKdfPtr kdf{EVP_KDF_fetch(nullptr, "HKDF", propq_or_null(provider)),
|
||||
&EVP_KDF_free};
|
||||
@@ -825,6 +1092,12 @@ namespace Void::Crypto
|
||||
"Crypto::hkdf_sha256: EVP_KDF_derive failed")};
|
||||
}
|
||||
return {};
|
||||
#else
|
||||
secure_wipe(secret_output);
|
||||
return std::unexpected{make_crypto_error(
|
||||
CryptoErrorCode::backend_error,
|
||||
"Crypto::hkdf_sha256: OpenSSL backend unavailable")};
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace Void::Crypto
|
||||
Reference in new issue
Block a user