Crypto: add a built-in Ed25519 backend (closes #52)

This commit is contained in:
faumaray committed 2026-12-03 17:58:13 +03:00
1 parent 06641947bd
commit 86cb94c844
3 files changed
+539 -66

No files matched your search

+55 -24
View File
@@ -1,7 +1,9 @@
#include <Void/Crypto/Crypto.hpp>
#include "BuiltinEd25519.hpp"
#include "BuiltinHash.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
@@ -9,6 +11,7 @@
#include <fstream>
#include <limits>
#include <optional>
#include <random>
#include <utility>
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
@@ -217,9 +220,9 @@ namespace Void::Crypto
return result == 1;
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::verifyEd25519: built without OpenSSL backend")};
return Detail::Ed25519::verify(
reinterpret_cast<const std::uint8_t*>(public_key.data()),
reinterpret_cast<const std::uint8_t*>(signature.data()), message);
#endif
}
@@ -771,10 +774,27 @@ namespace Void::Crypto
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
EvpPkeyPtr pkey{nullptr, &EVP_PKEY_free};
#else
std::uint8_t unused{0};
std::array<std::byte, Ed25519SeedSize> seed{};
std::array<std::byte, Ed25519PublicKeySize> public_key{};
#endif
};
#if !defined(VOID_CRYPTO_HAS_OPENSSL) || !VOID_CRYPTO_HAS_OPENSSL
namespace
{
void derive_ed25519_public_key(
const std::array<std::byte, Ed25519SeedSize>& seed,
std::array<std::byte, Ed25519PublicKeySize>& public_key)
{
Detail::Ed25519::Sha512 digest{};
Detail::Ed25519::expand_seed(
reinterpret_cast<const std::uint8_t*>(seed.data()), digest,
reinterpret_cast<std::uint8_t*>(public_key.data()));
Detail::Ed25519::wipe(digest);
}
} // namespace
#endif
Ed25519PrivateKey::Ed25519PrivateKey(std::unique_ptr<Impl> impl) noexcept
: m_impl(std::move(impl))
{
@@ -790,7 +810,7 @@ namespace Void::Crypto
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
return m_impl != nullptr && m_impl->pkey != nullptr;
#else
return false;
return m_impl != nullptr;
#endif
}
@@ -822,10 +842,20 @@ namespace Void::Crypto
return Ed25519PrivateKey{std::move(impl)};
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::generate: built without OpenSSL "
"backend")};
std::random_device random;
std::array<std::byte, Ed25519SeedSize> seed{};
for (std::size_t offset = 0; offset < seed.size();) {
const std::uint32_t word = random();
const std::size_t count =
std::min<std::size_t>(sizeof(word), seed.size() - offset);
std::memcpy(seed.data() + offset, &word, count);
offset += count;
}
auto impl = std::make_unique<Impl>();
impl->seed = seed;
derive_ed25519_public_key(impl->seed, impl->public_key);
secure_wipe(seed);
return Ed25519PrivateKey{std::move(impl)};
#endif
}
@@ -868,10 +898,10 @@ namespace Void::Crypto
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")};
auto impl = std::make_unique<Impl>();
std::memcpy(impl->seed.data(), seed.data(), Ed25519SeedSize);
derive_ed25519_public_key(impl->seed, impl->public_key);
return Ed25519PrivateKey{std::move(impl)};
#endif
}
@@ -897,10 +927,10 @@ namespace Void::Crypto
}
return pub;
#else
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::public_key: built without OpenSSL "
"backend")};
Ed25519PublicKey pub;
std::memcpy(pub.bytes.data(), m_impl->public_key.data(),
Ed25519PublicKeySize);
return pub;
#endif
}
@@ -928,10 +958,7 @@ namespace Void::Crypto
secure_wipe(raw);
return result;
#else
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::export_raw_seed: built without OpenSSL "
"backend")};
return Ed25519Seed::from_bytes(m_impl->seed);
#endif
}
@@ -994,9 +1021,13 @@ namespace Void::Crypto
return signature;
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::sign_ed25519: built without OpenSSL backend")};
Ed25519Signature signature;
Detail::Ed25519::sign(
reinterpret_cast<const std::uint8_t*>(private_key.m_impl->seed.data()),
reinterpret_cast<const std::uint8_t*>(
private_key.m_impl->public_key.data()),
message, reinterpret_cast<std::uint8_t*>(signature.bytes.data()));
return signature;
#endif
}