Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/hmac_cpp/hmac_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
#include <string>
#include <vector>

#ifndef HMAC_CPP_MAX_PBKDF2_ITERATIONS
#define HMAC_CPP_MAX_PBKDF2_ITERATIONS 1000000u
#endif

namespace hmac_cpp {

static constexpr uint32_t MAX_PBKDF2_ITERATIONS = HMAC_CPP_MAX_PBKDF2_ITERATIONS;

/// \brief Compares two byte arrays in constant time
/// \param a Pointer to first array
/// \param a_len Length of the first array
Expand Down
5 changes: 4 additions & 1 deletion src/hmac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace hmac_cpp {
throw std::invalid_argument("Null pointer with non-zero length");
if (iterations < 1)
throw std::invalid_argument("PBKDF2: iterations must be >= 1");
if (iterations > MAX_PBKDF2_ITERATIONS)
throw std::invalid_argument("PBKDF2: iterations too large");
if (dk_len == 0)
throw std::invalid_argument("PBKDF2: dk_len must be positive");
if (salt_len < 16)
Expand Down Expand Up @@ -115,7 +117,8 @@ namespace hmac_cpp {
(salt_len > 0 && salt_ptr == nullptr) ||
out_ptr == nullptr)
return false;
if (iterations < 1 || dk_len == 0 || salt_len < 16)
if (iterations < 1 || dk_len == 0 || salt_len < 16 ||
iterations > MAX_PBKDF2_ITERATIONS)
return false;

const size_t hlen = hmac_hash::SHA256::DIGEST_SIZE;
Expand Down
15 changes: 15 additions & 0 deletions test_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ TEST(PBKDF2Validation, TooLargeDkLenThrows) {
EXPECT_THROW(hmac::pbkdf2("password", salt, 1, too_large, hmac::Pbkdf2Hash::Sha1), std::invalid_argument);
}

TEST(PBKDF2Validation, IterationsLimit) {
std::string salt(16, 'a');
uint32_t limit = hmac::MAX_PBKDF2_ITERATIONS;
EXPECT_NO_THROW(hmac::pbkdf2("password", salt, limit - 1, 32, hmac::Pbkdf2Hash::Sha256));
EXPECT_THROW(hmac::pbkdf2("password", salt, limit + 1, 32, hmac::Pbkdf2Hash::Sha256), std::invalid_argument);
}

TEST(PBKDF2Test, SHA256WithValidSalt) {
auto salt = from_hex("000102030405060708090a0b0c0d0e0f");
std::string salt_str(salt.begin(), salt.end());
Expand All @@ -256,6 +263,14 @@ TEST(PBKDF2BufferApiTest, SHA256ArrayOutput) {
EXPECT_TRUE(std::equal(out.begin(), out.end(), ref.begin()));
}

TEST(PBKDF2BufferApiTest, IterationsLimit) {
std::string salt(16, 'a');
std::array<uint8_t,32> out{};
uint32_t limit = hmac::MAX_PBKDF2_ITERATIONS;
EXPECT_TRUE(hmac::pbkdf2_hmac_sha256(std::string("password"), salt, limit - 1, out));
EXPECT_FALSE(hmac::pbkdf2_hmac_sha256(std::string("password"), salt, limit + 1, out));
}

// SHA512 vector from BoringSSL pbkdf_test.cc
TEST(PBKDF2Test, BoringSSL_SHA512) {
auto dk = hmac::pbkdf2("passwordPASSWORDpassword",
Expand Down
Loading