1 parent
8583a479dd
commit
3851aeb703
42 files changed
+6948
-759
No files matched your search
@@ -0,0 +1,257 @@
|
||||
#include <Void/Crypto/Crypto.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <utility>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
namespace Void::Crypto
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] const EVP_MD*
|
||||
algorithm_impl(HashAlgorithm algorithm) noexcept
|
||||
{
|
||||
switch (algorithm) {
|
||||
case HashAlgorithm::Sha1:
|
||||
return EVP_sha1();
|
||||
case HashAlgorithm::Sha256:
|
||||
return EVP_sha256();
|
||||
case HashAlgorithm::Sha512:
|
||||
return EVP_sha512();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string openssl_error(std::string_view operation)
|
||||
{
|
||||
const unsigned long code = ERR_get_error();
|
||||
if (code == 0) {
|
||||
return std::string{operation};
|
||||
}
|
||||
std::array<char, 256> buffer{};
|
||||
ERR_error_string_n(code, buffer.data(), buffer.size());
|
||||
return std::format("{}: {}", operation, buffer.data());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct Hasher::Impl {
|
||||
EVP_MD_CTX* context{nullptr};
|
||||
const EVP_MD* algorithm{nullptr};
|
||||
bool finalized{false};
|
||||
~Impl() { EVP_MD_CTX_free(context); }
|
||||
};
|
||||
|
||||
Hasher::Hasher(std::unique_ptr<Impl> impl) noexcept
|
||||
: m_impl(std::move(impl))
|
||||
{
|
||||
}
|
||||
Hasher::~Hasher() = default;
|
||||
Hasher::Hasher(Hasher&&) noexcept = default;
|
||||
Hasher& Hasher::operator=(Hasher&&) noexcept = default;
|
||||
|
||||
Void::Result::Result<Hasher> Hasher::create(HashAlgorithm algorithm)
|
||||
{
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->algorithm = algorithm_impl(algorithm);
|
||||
if (impl->algorithm == nullptr) {
|
||||
return Void::Result::fail("Crypto::Hasher: unsupported algorithm");
|
||||
}
|
||||
impl->context = EVP_MD_CTX_new();
|
||||
if (impl->context == nullptr) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::Hasher: EVP_MD_CTX_new failed"));
|
||||
}
|
||||
if (EVP_DigestInit_ex(impl->context, impl->algorithm, nullptr) != 1) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::Hasher: digest initialization failed"));
|
||||
}
|
||||
return Hasher{std::move(impl)};
|
||||
}
|
||||
|
||||
Void::Result::Result<void> Hasher::update(std::span<const std::byte> data)
|
||||
{
|
||||
if (!m_impl || m_impl->finalized) {
|
||||
return Void::Result::fail(
|
||||
"Crypto::Hasher::update: hasher is finalized or moved-from");
|
||||
}
|
||||
if (!data.empty() &&
|
||||
EVP_DigestUpdate(m_impl->context, data.data(), data.size()) != 1) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::Hasher::update failed"));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<void> Hasher::update(std::string_view data)
|
||||
{
|
||||
return update(std::as_bytes(std::span{data.data(), data.size()}));
|
||||
}
|
||||
|
||||
Void::Result::Result<Digest> Hasher::finalize()
|
||||
{
|
||||
if (!m_impl || m_impl->finalized) {
|
||||
return Void::Result::fail(
|
||||
"Crypto::Hasher::finalize: hasher is finalized or moved-from");
|
||||
}
|
||||
const int digest_size = EVP_MD_get_size(m_impl->algorithm);
|
||||
if (digest_size <= 0) {
|
||||
return Void::Result::fail(
|
||||
"Crypto::Hasher::finalize: invalid digest size");
|
||||
}
|
||||
Digest digest(static_cast<std::size_t>(digest_size));
|
||||
unsigned int written = 0;
|
||||
if (EVP_DigestFinal_ex(m_impl->context,
|
||||
reinterpret_cast<unsigned char*>(digest.data()),
|
||||
&written) != 1) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::Hasher::finalize failed"));
|
||||
}
|
||||
m_impl->finalized = true;
|
||||
digest.resize(written);
|
||||
return digest;
|
||||
}
|
||||
|
||||
auto hash(HashAlgorithm algorithm, std::span<const std::byte> data)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
auto hasher = Hasher::create(algorithm);
|
||||
if (!hasher) {
|
||||
return std::unexpected{hasher.error()};
|
||||
}
|
||||
if (auto updated = hasher->update(data); !updated) {
|
||||
return std::unexpected{updated.error()};
|
||||
}
|
||||
return hasher->finalize();
|
||||
}
|
||||
|
||||
auto hash(HashAlgorithm algorithm, std::string_view data)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
return hash(algorithm,
|
||||
std::as_bytes(std::span{data.data(), data.size()}));
|
||||
}
|
||||
|
||||
auto hashFile(HashAlgorithm algorithm, const std::filesystem::path& path)
|
||||
-> Void::Result::Result<Digest>
|
||||
{
|
||||
std::ifstream input{path, std::ios::binary};
|
||||
if (!input.is_open()) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Crypto::hashFile: cannot open '{}'", path.string()));
|
||||
}
|
||||
auto hasher = Hasher::create(algorithm);
|
||||
if (!hasher) {
|
||||
return std::unexpected{hasher.error()};
|
||||
}
|
||||
std::array<char, 64 * 1024> buffer{};
|
||||
while (input) {
|
||||
input.read(buffer.data(),
|
||||
static_cast<std::streamsize>(buffer.size()));
|
||||
const auto count = input.gcount();
|
||||
if (count > 0) {
|
||||
const auto bytes = std::as_bytes(
|
||||
std::span{buffer.data(), static_cast<std::size_t>(count)});
|
||||
if (auto updated = hasher->update(bytes); !updated) {
|
||||
return std::unexpected{updated.error()};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (input.bad()) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Crypto::hashFile: I/O error reading '{}'", path.string()));
|
||||
}
|
||||
return hasher->finalize();
|
||||
}
|
||||
|
||||
Void::Result::Result<Digest> sha1(std::span<const std::byte> data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha1, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha1(std::string_view data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha1, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha256(std::span<const std::byte> data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha256, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha256(std::string_view data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha256, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha512(std::span<const std::byte> data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha512, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha512(std::string_view data)
|
||||
{
|
||||
return hash(HashAlgorithm::Sha512, data);
|
||||
}
|
||||
Void::Result::Result<Digest> sha1File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha1, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha256File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha256, path);
|
||||
}
|
||||
Void::Result::Result<Digest> sha512File(const std::filesystem::path& path)
|
||||
{
|
||||
return hashFile(HashAlgorithm::Sha512, path);
|
||||
}
|
||||
|
||||
Void::Result::Result<bool> verifyEd25519(std::span<const std::byte> message,
|
||||
std::span<const std::byte>
|
||||
signature,
|
||||
std::span<const std::byte>
|
||||
publicKey)
|
||||
{
|
||||
if (signature.size() != Ed25519SignatureSize) {
|
||||
return Void::Result::fail(
|
||||
std::format("Crypto::verifyEd25519: signature must be {} bytes",
|
||||
Ed25519SignatureSize));
|
||||
}
|
||||
if (publicKey.size() != Ed25519PublicKeySize) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Crypto::verifyEd25519: public key must be {} bytes",
|
||||
Ed25519PublicKeySize));
|
||||
}
|
||||
|
||||
using Key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
|
||||
using Context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
|
||||
Key key{EVP_PKEY_new_raw_public_key(
|
||||
EVP_PKEY_ED25519, nullptr,
|
||||
reinterpret_cast<const unsigned char*>(publicKey.data()),
|
||||
publicKey.size()),
|
||||
&EVP_PKEY_free};
|
||||
if (!key) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::verifyEd25519: invalid public key"));
|
||||
}
|
||||
Context context{EVP_MD_CTX_new(), &EVP_MD_CTX_free};
|
||||
if (!context) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::verifyEd25519: EVP_MD_CTX_new failed"));
|
||||
}
|
||||
if (EVP_DigestVerifyInit(context.get(), nullptr, nullptr, nullptr,
|
||||
key.get()) != 1) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::verifyEd25519: initialization failed"));
|
||||
}
|
||||
const int result = EVP_DigestVerify(
|
||||
context.get(),
|
||||
reinterpret_cast<const unsigned char*>(signature.data()),
|
||||
signature.size(),
|
||||
reinterpret_cast<const unsigned char*>(message.data()),
|
||||
message.size());
|
||||
if (result < 0) {
|
||||
return Void::Result::fail(
|
||||
openssl_error("Crypto::verifyEd25519 failed"));
|
||||
}
|
||||
return result == 1;
|
||||
}
|
||||
} // namespace Void::Crypto
|
||||
Reference in new issue
Block a user