Crypto: add HMAC-SHA1 and PBKDF2-HMAC-SHA256 (#41)

This commit is contained in:
faumaray committed 2026-12-03 15:50:15 +03:00
1 parent b3c566ed4d
commit 74bd01aac5
6 files changed
+826 -46

No files matched your search

+33
View File
@@ -5,6 +5,7 @@
#include <Void/Functional/Result.hpp>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <memory>
#include <span>
@@ -166,6 +167,19 @@ namespace Void::Crypto
const Ed25519PublicKey& public_key,
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>
@@ -179,6 +193,25 @@ namespace Void::Crypto
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.
+6 -1
View File
@@ -16,12 +16,16 @@ namespace Void::Crypto
inline constexpr std::size_t Ed25519PublicKeySize = 32;
inline constexpr std::size_t Ed25519SignatureSize = 64;
inline constexpr std::size_t Ed25519SeedSize = 32;
inline constexpr std::size_t HmacSha1Size = 20;
inline constexpr std::size_t HmacSha256Size = 32;
struct CryptoLimits {
static constexpr std::size_t kMaxOneShotMessageBytes =
16ULL * 1024ULL * 1024ULL;
static constexpr std::size_t kMaxHkdfOutputBytes = 4ULL * 1024ULL;
static constexpr std::uint32_t kMaxPbkdf2Iterations = 10'000'000U;
static constexpr std::size_t kMaxPbkdf2SaltBytes = 64ULL * 1024ULL;
static constexpr std::size_t kMaxPbkdf2OutputBytes = 1024ULL;
static constexpr std::size_t kStreamChunkBytes = 64ULL * 1024ULL;
static constexpr std::size_t kDefaultMaxConcurrentFileHashes = 8;
static constexpr std::size_t kDefaultMaxWaitingFileHashes = 32;
@@ -256,7 +260,8 @@ namespace Void::Crypto
ed25519_sign,
ed25519_verify,
hmac,
hkdf
hkdf,
pbkdf2
};
struct FaultConfig {