Files
Library/include/Void/Crypto/Crypto.hpp

193 lines
7.2 KiB
C++

#pragma once
#include <Void/Crypto/Types.hpp>
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <cstddef>
#include <filesystem>
#include <memory>
#include <span>
#include <string_view>
#include <vector>
namespace Void::Crypto
{
enum class HashAlgorithm { Sha1, Sha256, Sha512, Blake2b256, Blake2b512 };
using Digest = std::vector<std::byte>;
/// Returns true if Void::Crypto was built with OpenSSL support (required
/// for Ed25519; hashing/HMAC/HKDF work on the built-in pure-C++ backend
/// with zero external dependencies).
[[nodiscard]] bool has_openssl_backend() noexcept;
class Hasher
{
public:
[[nodiscard]] static Void::Result::Result<Hasher>
create(HashAlgorithm algorithm);
[[nodiscard]] static Void::Result::Result<Hasher>
create(HashAlgorithm algorithm,
const ProviderOptions& provider);
~Hasher();
Hasher(Hasher&&) noexcept;
Hasher& operator=(Hasher&&) noexcept;
Hasher(const Hasher&) = delete;
Hasher& operator=(const Hasher&) = delete;
[[nodiscard]] HasherState state() const noexcept;
[[nodiscard]] Void::Result::Result<void>
update(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<void>
update(std::string_view data);
/// Finalize this hash. Calling finalize or update again is an
/// error. Backend update/finalize failures poison the hasher.
[[nodiscard]] Void::Result::Result<Digest> finalize();
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
explicit Hasher(std::unique_ptr<Impl> impl) noexcept;
};
[[nodiscard]] Void::Result::Result<Digest>
hash(HashAlgorithm algorithm, std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest>
hash(HashAlgorithm algorithm, std::span<const std::byte> data,
const ProviderOptions& provider);
[[nodiscard]] Void::Result::Result<Digest> hash(HashAlgorithm algorithm,
std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
hashFile(HashAlgorithm algorithm, const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
hashFile(HashAlgorithm algorithm, const std::filesystem::path& path,
const ProviderOptions& provider);
[[nodiscard]] Void::Result::Result<Digest>
hash_file(HashAlgorithm algorithm, const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
hash_file(HashAlgorithm algorithm, const std::filesystem::path& path,
const ProviderOptions& provider);
[[nodiscard]] Void::Result::Result<Digest>
sha1(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest> sha1(std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
sha256(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest> sha256(std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
sha512(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest> sha512(std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
blake2b256(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest> blake2b256(std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
blake2b512(std::span<const std::byte> data);
[[nodiscard]] Void::Result::Result<Digest> blake2b512(std::string_view data);
[[nodiscard]] Void::Result::Result<Digest>
sha1File(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
sha256File(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
sha512File(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
sha1_file(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
sha256_file(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
sha512_file(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
blake2b256_file(const std::filesystem::path& path);
[[nodiscard]] Void::Result::Result<Digest>
blake2b512_file(const std::filesystem::path& path);
/// Move-only owning Ed25519 private key handle. Has no implicit copy,
/// string conversion, or secret formatting.
class Ed25519PrivateKey
{
public:
Ed25519PrivateKey() noexcept = default;
~Ed25519PrivateKey();
Ed25519PrivateKey(Ed25519PrivateKey&&) noexcept;
Ed25519PrivateKey& operator=(Ed25519PrivateKey&&) noexcept;
Ed25519PrivateKey(const Ed25519PrivateKey&) = delete;
Ed25519PrivateKey& operator=(const Ed25519PrivateKey&) = delete;
[[nodiscard]] static Void::Result::Result<Ed25519PrivateKey>
generate(const ProviderOptions& provider = {});
[[nodiscard]] static Void::Result::Result<Ed25519PrivateKey>
from_raw_seed(std::span<const std::byte> seed,
const ProviderOptions& provider = {});
[[nodiscard]] bool valid() const noexcept;
[[nodiscard]] Void::Result::Result<Ed25519PublicKey>
public_key() const;
/// Explicit secret export into a move-only self-wiping seed handle.
[[nodiscard]] Void::Result::Result<Ed25519Seed>
export_raw_seed() const;
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
explicit Ed25519PrivateKey(std::unique_ptr<Impl> impl) noexcept;
friend Void::Result::Result<Ed25519Signature>
sign_ed25519(std::span<const std::byte> message,
const Ed25519PrivateKey& private_key,
const ProviderOptions& provider);
};
/// Sign exact message bytes (up to CryptoLimits::kMaxOneShotMessageBytes)
/// with an owning Ed25519 private key.
[[nodiscard]] Void::Result::Result<Ed25519Signature>
sign_ed25519(std::span<const std::byte> message,
const Ed25519PrivateKey& private_key,
const ProviderOptions& provider = {});
/// Verify an Ed25519 detached signature over the exact message bytes.
/// Invalid key/signature sizes or oversized messages are errors; a
/// well-formed but invalid signature returns false.
[[nodiscard]] Void::Result::Result<bool>
verifyEd25519(std::span<const std::byte> message,
std::span<const std::byte> signature,
std::span<const std::byte> publicKey);
[[nodiscard]] Void::Result::Result<bool>
verify_ed25519(std::span<const std::byte> message,
const Ed25519Signature& signature,
const Ed25519PublicKey& public_key,
const ProviderOptions& provider = {});
/// Compute HMAC-SHA-256 into a caller-owned 32-byte buffer.
/// Wipes output on backend failure and rejects overlapping buffers.
[[nodiscard]] Void::Result::Result<void>
hmac_sha256_into(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output,
const ProviderOptions& provider = {});
[[nodiscard]] Void::Result::Result<Digest>
hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
const ProviderOptions& provider = {});
/// Derive key material with RFC 5869 HKDF-SHA-256 into a caller-owned
/// buffer (1..CryptoLimits::kMaxHkdfOutputBytes). Rejects overlapping
/// buffers and wipes secret_output on any failure after validation.
[[nodiscard]] Void::Result::Result<void>
hkdf_sha256(std::span<const std::byte> secret_input,
std::span<const std::byte> salt,
std::span<const std::byte> context,
std::span<std::byte> secret_output,
const ProviderOptions& provider = {});
} // namespace Void::Crypto