255 lines
9.8 KiB
C++
255 lines
9.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Crypto/Types.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#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 = {});
|
|
|
|
/// 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<Ed25519KeyPair>
|
|
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<Ed25519Signature>
|
|
ed25519_sign(std::span<const std::byte> secret_key,
|
|
std::span<const std::byte> 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<bool>
|
|
ed25519_verify(std::span<const std::byte> public_key,
|
|
std::span<const std::byte> message,
|
|
std::span<const std::byte> 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<void>
|
|
hmac_sha1_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_sha1(std::span<const std::byte> secret_key,
|
|
std::span<const std::byte> 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<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 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<void>
|
|
pbkdf2_hmac_sha256_into(std::span<const std::byte> password,
|
|
std::span<const std::byte> salt,
|
|
std::uint32_t iterations,
|
|
std::span<std::byte> output,
|
|
const ProviderOptions& provider = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<Digest>
|
|
pbkdf2_hmac_sha256(std::span<const std::byte> password,
|
|
std::span<const std::byte> 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<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
|