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

+251 -45
View File
@@ -220,6 +220,135 @@ namespace Void::Crypto
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::verifyEd25519: built without OpenSSL backend")};
#endif
}
[[maybe_unused]] [[nodiscard]] const char*
hmac_digest_name(HashAlgorithm algorithm) noexcept
{
switch (algorithm) {
case HashAlgorithm::Sha1:
return "SHA1";
case HashAlgorithm::Sha256:
return "SHA256";
default:
return nullptr;
}
}
[[nodiscard]] Void::Result::Result<void>
hmac_into_impl(HashAlgorithm algorithm,
std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output,
const ProviderOptions& provider,
std::string_view operation)
{
const std::size_t expected_size = algorithm == HashAlgorithm::Sha1
? HmacSha1Size
: HmacSha256Size;
if (output.size() != expected_size) {
return std::unexpected{
make_crypto_error(CryptoErrorCode::invalid_argument,
std::format("{}: output must be {} bytes",
operation, expected_size))};
}
if (spans_overlap(secret_key, output) ||
spans_overlap(message, output)) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::overlapping_buffers,
std::format("{}: output buffer overlaps input",
operation))};
}
if (auto fault =
maybe_inject_fault(FaultOperation::hmac, operation);
fault.has_value()) {
secure_wipe(output);
return std::unexpected{std::move(*fault)};
}
if (use_builtin_hash(algorithm, provider)) {
if (!provider.property_query.empty()) {
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::provider_error,
std::format("{}: property_query unavailable on "
"built-in backend",
operation))};
}
if (algorithm == HashAlgorithm::Sha1) {
Detail::builtin_hmac_sha1(secret_key, message, output);
} else {
Detail::builtin_hmac_sha256(secret_key, message, output);
}
return {};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
const char* name = hmac_digest_name(algorithm);
if (name == nullptr) {
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
std::format("{}: unsupported HMAC algorithm", operation))};
}
ERR_clear_error();
EvpMacPtr mac{
EVP_MAC_fetch(nullptr, "HMAC", propq_or_null(provider)),
&EVP_MAC_free};
if (!mac) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::provider_error,
std::format("{}: EVP_MAC_fetch failed", operation))};
}
EvpMacCtxPtr ctx{EVP_MAC_CTX_new(mac.get()), &EVP_MAC_CTX_free};
if (!ctx) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
std::format("{}: EVP_MAC_CTX_new failed", operation))};
}
std::array<char, 8> digest_name{};
std::strncpy(digest_name.data(), name, digest_name.size() - 1);
std::array<OSSL_PARAM, 2> params = {
OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
digest_name.data(), 0),
OSSL_PARAM_construct_end(),
};
if (EVP_MAC_init(ctx.get(), non_null_bytes(secret_key),
secret_key.size(), params.data()) != 1) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::provider_error,
std::format("{}: EVP_MAC_init failed", operation))};
}
if (!message.empty() &&
EVP_MAC_update(ctx.get(), non_null_bytes(message),
message.size()) != 1) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
std::format("{}: EVP_MAC_update failed", operation))};
}
std::size_t out_len = 0;
if (EVP_MAC_final(ctx.get(),
reinterpret_cast<unsigned char*>(output.data()),
&out_len, output.size()) != 1 ||
out_len != expected_size) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
std::format("{}: EVP_MAC_final failed", operation))};
}
return {};
#else
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
std::format("{}: OpenSSL backend unavailable", operation))};
#endif
}
} // namespace
@@ -891,26 +1020,98 @@ namespace Void::Crypto
provider);
}
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)
{
return hmac_into_impl(HashAlgorithm::Sha1, secret_key, message, output,
provider, "Crypto::hmac_sha1_into");
}
Void::Result::Result<Digest>
hmac_sha1(std::span<const std::byte> secret_key,
std::span<const std::byte>
message,
const ProviderOptions& provider)
{
Digest out(HmacSha1Size);
if (auto res = hmac_sha1_into(secret_key, message, out, provider);
!res.has_value()) {
secure_wipe(out);
return std::unexpected{std::move(res.error())};
}
return out;
}
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)
{
if (output.size() != HmacSha256Size) {
return hmac_into_impl(HashAlgorithm::Sha256, secret_key, message,
output, provider, "Crypto::hmac_sha256_into");
}
Void::Result::Result<Digest> hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
const ProviderOptions& provider)
{
Digest out(HmacSha256Size);
if (auto res = hmac_sha256_into(secret_key, message, out, provider);
!res.has_value()) {
secure_wipe(out);
return std::unexpected{std::move(res.error())};
}
return out;
}
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)
{
if (iterations == 0 ||
iterations > CryptoLimits::kMaxPbkdf2Iterations) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_argument,
std::format("Crypto::hmac_sha256_into: output must be {} bytes",
HmacSha256Size))};
std::format("Crypto::pbkdf2_hmac_sha256: iterations must be "
"1..{}",
CryptoLimits::kMaxPbkdf2Iterations))};
}
if (spans_overlap(secret_key, output) ||
spans_overlap(message, output)) {
if (salt.size() > CryptoLimits::kMaxPbkdf2SaltBytes) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::message_too_large,
std::format("Crypto::pbkdf2_hmac_sha256: salt exceeds {} byte "
"cap",
CryptoLimits::kMaxPbkdf2SaltBytes))};
}
if (output.empty()) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_argument,
"Crypto::pbkdf2_hmac_sha256: output buffer must be non-empty")};
}
if (output.size() > CryptoLimits::kMaxPbkdf2OutputBytes) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::output_too_large,
std::format(
"Crypto::pbkdf2_hmac_sha256: output size exceeds {} byte "
"cap",
CryptoLimits::kMaxPbkdf2OutputBytes))};
}
if (spans_overlap(password, output) || spans_overlap(salt, output)) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::overlapping_buffers,
"Crypto::hmac_sha256_into: output buffer overlaps input")};
"Crypto::pbkdf2_hmac_sha256: output buffer overlaps input")};
}
if (auto fault = maybe_inject_fault(FaultOperation::hmac,
"Crypto::hmac_sha256_into");
if (auto fault = maybe_inject_fault(FaultOperation::pbkdf2,
"Crypto::pbkdf2_hmac_sha256");
fault.has_value()) {
secure_wipe(output);
return std::unexpected{std::move(*fault)};
@@ -921,78 +1122,83 @@ namespace Void::Crypto
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::provider_error,
"Crypto::hmac_sha256_into: property_query unavailable on "
"Crypto::pbkdf2_hmac_sha256: property_query unavailable on "
"built-in backend")};
}
Detail::builtin_hmac_sha256(secret_key, message, output);
Detail::builtin_pbkdf2_hmac_sha256(password, salt, iterations,
output);
return {};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
ERR_clear_error();
EvpMacPtr mac{EVP_MAC_fetch(nullptr, "HMAC", propq_or_null(provider)),
&EVP_MAC_free};
if (!mac) {
EvpKdfPtr kdf{EVP_KDF_fetch(nullptr, "PBKDF2", propq_or_null(provider)),
&EVP_KDF_free};
if (!kdf) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::provider_error,
"Crypto::hmac_sha256_into: EVP_MAC_fetch failed")};
"Crypto::pbkdf2_hmac_sha256: EVP_KDF_fetch failed")};
}
EvpMacCtxPtr ctx{EVP_MAC_CTX_new(mac.get()), &EVP_MAC_CTX_free};
EvpKdfCtxPtr ctx{EVP_KDF_CTX_new(kdf.get()), &EVP_KDF_CTX_free};
if (!ctx) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
"Crypto::hmac_sha256_into: EVP_MAC_CTX_new failed")};
"Crypto::pbkdf2_hmac_sha256: EVP_KDF_CTX_new failed")};
}
char digest_name[] = "SHA256";
std::array<OSSL_PARAM, 2> params = {
OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,
int iteration_count = static_cast<int>(iterations);
std::array<OSSL_PARAM, 5> params = {
OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest_name,
0),
OSSL_PARAM_construct_octet_string(
OSSL_KDF_PARAM_PASSWORD,
const_cast<unsigned char*>(non_null_bytes(password)),
password.size()),
OSSL_PARAM_construct_octet_string(
OSSL_KDF_PARAM_SALT,
const_cast<unsigned char*>(non_null_bytes(salt)), salt.size()),
OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iteration_count),
OSSL_PARAM_construct_end(),
};
if (EVP_MAC_init(ctx.get(), non_null_bytes(secret_key),
secret_key.size(), params.data()) != 1) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::provider_error,
"Crypto::hmac_sha256_into: EVP_MAC_init failed")};
}
if (!message.empty() &&
EVP_MAC_update(ctx.get(), non_null_bytes(message),
message.size()) != 1) {
if (EVP_KDF_derive(ctx.get(),
reinterpret_cast<unsigned char*>(output.data()),
output.size(), params.data()) != 1) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
"Crypto::hmac_sha256_into: EVP_MAC_update failed")};
}
std::size_t out_len = 0;
if (EVP_MAC_final(ctx.get(),
reinterpret_cast<unsigned char*>(output.data()),
&out_len, output.size()) != 1 ||
out_len != HmacSha256Size) {
secure_wipe(output);
return std::unexpected{make_openssl_error(
CryptoErrorCode::backend_error,
"Crypto::hmac_sha256_into: EVP_MAC_final failed")};
"Crypto::pbkdf2_hmac_sha256: EVP_KDF_derive failed")};
}
return {};
#else
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::hmac_sha256_into: OpenSSL backend unavailable")};
"Crypto::pbkdf2_hmac_sha256: OpenSSL backend unavailable")};
#endif
}
Void::Result::Result<Digest> hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
const ProviderOptions& provider)
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)
{
Digest out(HmacSha256Size);
if (auto res = hmac_sha256_into(secret_key, message, out, provider);
if (output_length > CryptoLimits::kMaxPbkdf2OutputBytes) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::output_too_large,
std::format(
"Crypto::pbkdf2_hmac_sha256: output size exceeds {} byte "
"cap",
CryptoLimits::kMaxPbkdf2OutputBytes))};
}
Digest out(output_length);
if (auto res = pbkdf2_hmac_sha256_into(password, salt, iterations, out,
provider);
!res.has_value()) {
secure_wipe(out);
return std::unexpected{std::move(res.error())};