feat(crypto): harden Crypto primitives and add optional CryptoAsync adapter
This commit is contained in:
1 parent
8a1c15f9c0
commit
eb42228604
14 files changed
+2448
-90
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Crypto/Types.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
@@ -21,18 +22,23 @@ namespace Void::Crypto
|
||||
|
||||
[[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.
|
||||
/// error. Backend update/finalize failures poison the hasher.
|
||||
[[nodiscard]] Void::Result::Result<Digest> finalize();
|
||||
|
||||
private:
|
||||
@@ -44,10 +50,16 @@ namespace Void::Crypto
|
||||
|
||||
[[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>
|
||||
sha1(std::span<const std::byte> data);
|
||||
@@ -65,14 +77,89 @@ namespace Void::Crypto
|
||||
[[nodiscard]] Void::Result::Result<Digest>
|
||||
sha512File(const std::filesystem::path& path);
|
||||
|
||||
inline constexpr std::size_t Ed25519PublicKeySize = 32;
|
||||
inline constexpr std::size_t Ed25519SignatureSize = 64;
|
||||
/// 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 are errors; a well-formed but invalid
|
||||
/// signature returns false.
|
||||
/// 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
|
||||
Reference in new issue
Block a user