Merge issue-41-hmac-pbkdf2: HMAC-SHA1 and PBKDF2-HMAC-SHA256 (closes #41)

This commit is contained in:
faumaray committed 2026-12-03 15:51:04 +03:00
commit 9b773a4553
6 files changed
+826 -46

No files matched your search

+131
View File
@@ -789,6 +789,46 @@ namespace Void::Crypto::Detail
return out;
}
void builtin_hmac_sha1(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_20) noexcept
{
std::array<std::byte, 64> key_block{};
if (secret_key.size() > 64) {
Sha1State kh;
kh.update(secret_key);
kh.finalize_into(std::span{key_block}.subspan(0, 20));
} else if (!secret_key.empty()) {
std::memcpy(key_block.data(), secret_key.data(), secret_key.size());
}
std::array<std::byte, 64> ipad{};
std::array<std::byte, 64> opad{};
for (std::size_t i = 0; i < 64; ++i) {
ipad[i] = key_block[i] ^ std::byte{0x36};
opad[i] = key_block[i] ^ std::byte{0x5C};
}
std::array<std::byte, 20> inner_hash{};
{
Sha1State inner;
inner.update(ipad);
inner.update(message);
inner.finalize_into(inner_hash);
}
{
Sha1State outer;
outer.update(opad);
outer.update(inner_hash);
outer.finalize_into(output_20);
}
builtin_secure_wipe(key_block);
builtin_secure_wipe(ipad);
builtin_secure_wipe(opad);
builtin_secure_wipe(inner_hash);
}
void builtin_hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_32) noexcept
@@ -892,4 +932,95 @@ namespace Void::Crypto::Detail
builtin_secure_wipe(t);
}
void builtin_pbkdf2_hmac_sha256(std::span<const std::byte> password,
std::span<const std::byte> salt,
std::uint32_t iterations,
std::span<std::byte> secret_output) noexcept
{
std::array<std::byte, 64> key_block{};
if (password.size() > 64) {
Sha256State kh;
kh.update(password);
kh.finalize_into(std::span{key_block}.subspan(0, 32));
} else if (!password.empty()) {
std::memcpy(key_block.data(), password.data(), password.size());
}
std::array<std::byte, 64> ipad{};
std::array<std::byte, 64> opad{};
for (std::size_t i = 0; i < 64; ++i) {
ipad[i] = key_block[i] ^ std::byte{0x36};
opad[i] = key_block[i] ^ std::byte{0x5C};
}
auto hmac = [&ipad, &opad](std::span<const std::byte> data,
std::span<std::byte> out) noexcept {
std::array<std::byte, 32> inner_hash{};
{
Sha256State inner;
inner.update(ipad);
inner.update(data);
inner.finalize_into(inner_hash);
}
{
Sha256State outer;
outer.update(opad);
outer.update(inner_hash);
outer.finalize_into(out);
}
builtin_secure_wipe(inner_hash);
};
// Callers cap the output length, so this cannot overflow.
const std::size_t blocks = (secret_output.size() + 31) / 32;
std::size_t written = 0;
for (std::size_t block = 1; block <= blocks; ++block) {
std::array<std::byte, 4> counter{};
store_be32(counter.data(), static_cast<std::uint32_t>(block));
std::array<std::byte, 32> u{};
{
std::array<std::byte, 32> inner_hash{};
{
Sha256State inner;
inner.update(ipad);
inner.update(salt);
inner.update(counter);
inner.finalize_into(inner_hash);
}
{
Sha256State outer;
outer.update(opad);
outer.update(inner_hash);
outer.finalize_into(u);
}
builtin_secure_wipe(inner_hash);
}
std::array<std::byte, 32> t = u;
for (std::uint32_t iteration = 1; iteration < iterations;
++iteration) {
std::array<std::byte, 32> next{};
hmac(u, next);
u = next;
for (std::size_t i = 0; i < t.size(); ++i) {
t[i] ^= u[i];
}
builtin_secure_wipe(next);
}
const std::size_t copy_n =
std::min<std::size_t>(32, secret_output.size() - written);
std::memcpy(secret_output.data() + written, t.data(), copy_n);
written += copy_n;
builtin_secure_wipe(u);
builtin_secure_wipe(t);
}
builtin_secure_wipe(key_block);
builtin_secure_wipe(ipad);
builtin_secure_wipe(opad);
}
} // namespace Void::Crypto::Detail
+10
View File
@@ -4,6 +4,7 @@
#include <Void/Crypto/Types.hpp>
#include <cstddef>
#include <cstdint>
#include <span>
namespace Void::Crypto::Detail
@@ -37,10 +38,19 @@ namespace Void::Crypto::Detail
void destroy() noexcept;
};
void builtin_hmac_sha1(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_20) noexcept;
void builtin_hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_32) noexcept;
void builtin_pbkdf2_hmac_sha256(std::span<const std::byte> password,
std::span<const std::byte> salt,
std::uint32_t iterations,
std::span<std::byte> secret_output) noexcept;
void builtin_hkdf_sha256(std::span<const std::byte> secret_input,
std::span<const std::byte> salt,
std::span<const std::byte> context,
+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
@@ -939,26 +1068,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)};
@@ -969,78 +1170,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())};