#pragma once #include #include #include #include #include #include #include #include #include #include namespace Void::Crypto { enum class HashAlgorithm { Sha1, Sha256, Sha512, Blake2b256, Blake2b512 }; using Digest = std::vector; /// 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 create(HashAlgorithm algorithm); [[nodiscard]] static Void::Result::Result 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 update(std::span data); [[nodiscard]] Void::Result::Result 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 finalize(); private: struct Impl; std::unique_ptr m_impl; explicit Hasher(std::unique_ptr impl) noexcept; }; [[nodiscard]] Void::Result::Result hash(HashAlgorithm algorithm, std::span data); [[nodiscard]] Void::Result::Result hash(HashAlgorithm algorithm, std::span data, const ProviderOptions& provider); [[nodiscard]] Void::Result::Result hash(HashAlgorithm algorithm, std::string_view data); [[nodiscard]] Void::Result::Result hashFile(HashAlgorithm algorithm, const std::filesystem::path& path); [[nodiscard]] Void::Result::Result hashFile(HashAlgorithm algorithm, const std::filesystem::path& path, const ProviderOptions& provider); [[nodiscard]] Void::Result::Result hash_file(HashAlgorithm algorithm, const std::filesystem::path& path); [[nodiscard]] Void::Result::Result hash_file(HashAlgorithm algorithm, const std::filesystem::path& path, const ProviderOptions& provider); [[nodiscard]] Void::Result::Result sha1(std::span data); [[nodiscard]] Void::Result::Result sha1(std::string_view data); [[nodiscard]] Void::Result::Result sha256(std::span data); [[nodiscard]] Void::Result::Result sha256(std::string_view data); [[nodiscard]] Void::Result::Result sha512(std::span data); [[nodiscard]] Void::Result::Result sha512(std::string_view data); [[nodiscard]] Void::Result::Result blake2b256(std::span data); [[nodiscard]] Void::Result::Result blake2b256(std::string_view data); [[nodiscard]] Void::Result::Result blake2b512(std::span data); [[nodiscard]] Void::Result::Result blake2b512(std::string_view data); [[nodiscard]] Void::Result::Result sha1File(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result sha256File(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result sha512File(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result sha1_file(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result sha256_file(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result sha512_file(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result blake2b256_file(const std::filesystem::path& path); [[nodiscard]] Void::Result::Result 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 generate(const ProviderOptions& provider = {}); [[nodiscard]] static Void::Result::Result from_raw_seed(std::span seed, const ProviderOptions& provider = {}); [[nodiscard]] bool valid() const noexcept; [[nodiscard]] Void::Result::Result public_key() const; /// Explicit secret export into a move-only self-wiping seed handle. [[nodiscard]] Void::Result::Result export_raw_seed() const; private: struct Impl; std::unique_ptr m_impl; explicit Ed25519PrivateKey(std::unique_ptr impl) noexcept; friend Void::Result::Result sign_ed25519(std::span 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 sign_ed25519(std::span 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 verifyEd25519(std::span message, std::span signature, std::span publicKey); [[nodiscard]] Void::Result::Result verify_ed25519(std::span message, const Ed25519Signature& signature, const Ed25519PublicKey& public_key, const ProviderOptions& provider = {}); /// Convenience key pair returned by ed25519_keypair(). The secret seed is /// a move-only, self-wiping handle; prefer the typed Ed25519PrivateKey APIs /// when the key can stay inside an owning handle. struct Ed25519KeyPair { Ed25519Seed secret_key; Ed25519PublicKey public_key; }; /// Generate a fresh Ed25519 key pair. Requires the OpenSSL backend. [[nodiscard]] Void::Result::Result ed25519_keypair(const ProviderOptions& provider = {}); /// Sign exact message bytes with a 32-byte Ed25519 seed/secret key. The /// temporary private-key handle is wiped when it goes out of scope; prefer /// sign_ed25519() when the caller already owns an Ed25519PrivateKey. [[nodiscard]] Void::Result::Result ed25519_sign(std::span secret_key, std::span message, const ProviderOptions& provider = {}); /// Verify a detached Ed25519 signature over exact message bytes. /// Malformed key/signature sizes are errors; a well-formed but invalid /// signature returns false. [[nodiscard]] Void::Result::Result ed25519_verify(std::span public_key, std::span message, std::span signature, const ProviderOptions& provider = {}); /// Compute HMAC-SHA-1 (RFC 2104) into a caller-owned 20-byte buffer. /// Wipes output on backend failure and rejects overlapping buffers. [[nodiscard]] Void::Result::Result hmac_sha1_into(std::span secret_key, std::span message, std::span output, const ProviderOptions& provider = {}); [[nodiscard]] Void::Result::Result hmac_sha1(std::span secret_key, std::span message, 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 hmac_sha256_into(std::span secret_key, std::span message, std::span output, const ProviderOptions& provider = {}); [[nodiscard]] Void::Result::Result hmac_sha256(std::span secret_key, std::span message, const ProviderOptions& provider = {}); /// Derive key material with RFC 8018 PBKDF2-HMAC-SHA-256 into a /// caller-owned buffer. iterations must be 1..kMaxPbkdf2Iterations, salt /// at most kMaxPbkdf2SaltBytes and output 1..kMaxPbkdf2OutputBytes. /// Rejects overlapping buffers and wipes output on any failure after /// validation. [[nodiscard]] Void::Result::Result pbkdf2_hmac_sha256_into(std::span password, std::span salt, std::uint32_t iterations, std::span output, const ProviderOptions& provider = {}); [[nodiscard]] Void::Result::Result pbkdf2_hmac_sha256(std::span password, std::span salt, std::uint32_t iterations, std::size_t output_length, 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 hkdf_sha256(std::span secret_input, std::span salt, std::span context, std::span secret_output, const ProviderOptions& provider = {}); } // namespace Void::Crypto