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
15 changes: 15 additions & 0 deletions include/hmac_cpp/hmac_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ namespace hmac_cpp {
uint32_t iterations, size_t dk_len,
Pbkdf2Hash prf = Pbkdf2Hash::Sha256);

/// \brief Derives a key into a page-locked secure buffer using PBKDF2
/// \param password_ptr Pointer to the password buffer
/// \param password_len Length of the password in bytes
/// \param salt_ptr Pointer to the salt buffer
/// \param salt_len Length of the salt in bytes
/// \param iterations Number of iterations, must be positive
/// \param dk_len Desired length of the derived key in bytes, must be positive
/// \param prf Hash function to use (SHA1, SHA256, SHA512)
/// \return Derived key as a page-locked secure buffer
HMAC_CPP_API secure_buffer<uint8_t, true> pbkdf2_secure(
const void* password_ptr, size_t password_len,
const void* salt_ptr, size_t salt_len,
uint32_t iterations, size_t dk_len,
Pbkdf2Hash prf = Pbkdf2Hash::Sha256);

/// \brief Derive key using PBKDF2 from vector-based password and salt.
/// \tparam T Byte type; must be char or uint8_t.
/// \param password Password bytes.
Expand Down
32 changes: 23 additions & 9 deletions src/hmac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ namespace hmac_cpp {
salt_block[salt_len + 2] = static_cast<uint8_t>((i >> 8) & 0xFF);
salt_block[salt_len + 3] = static_cast<uint8_t>(i & 0xFF);

secure_buffer<uint8_t> u(std::move(get_hmac(password_ptr, password_len,
salt_block.data(), salt_block.size(),
hash_type)));
secure_buffer<uint8_t> t = u;
secure_buffer<uint8_t, true> u(std::move(get_hmac(password_ptr, password_len,
salt_block.data(), salt_block.size(),
hash_type)));
secure_buffer<uint8_t, true> t = u;
for (uint32_t j = 1; j < iterations; ++j) {
u = secure_buffer<uint8_t>(get_hmac(password_ptr, password_len,
u.data(), u.size(), hash_type));
u = secure_buffer<uint8_t, true>(get_hmac(password_ptr, password_len,
u.data(), u.size(), hash_type));
for (size_t k = 0; k < t.size(); ++k) {
t[k] ^= u[k];
}
Expand All @@ -125,6 +125,20 @@ namespace hmac_cpp {
return derived;
}

secure_buffer<uint8_t, true> pbkdf2_secure(
const void* password_ptr, size_t password_len,
const void* salt_ptr, size_t salt_len,
uint32_t iterations, size_t dk_len,
Pbkdf2Hash prf) {
auto derived = pbkdf2(password_ptr, password_len,
salt_ptr, salt_len,
iterations, dk_len, prf);
PageLockGuard lock(derived.data(), derived.size());
secure_buffer<uint8_t, true> out(std::move(derived));
lock.locked = false;
return out;
}

bool pbkdf2(Pbkdf2Hash prf,
const void* password_ptr, size_t password_len,
const void* salt_ptr, size_t salt_len,
Expand Down Expand Up @@ -175,8 +189,8 @@ namespace hmac_cpp {
salt_block[salt_len + 2] = static_cast<uint8_t>((i >> 8) & 0xFF);
salt_block[salt_len + 3] = static_cast<uint8_t>(i & 0xFF);

secure_buffer<uint8_t> u(hlen);
secure_buffer<uint8_t> t(hlen);
secure_buffer<uint8_t, true> u(hlen);
secure_buffer<uint8_t, true> t(hlen);
HmacContext ctx(hash_type);
ctx.init(password_ptr, password_len);
ctx.update(salt_block.data(), salt_block.size());
Expand Down Expand Up @@ -221,7 +235,7 @@ namespace hmac_cpp {
Pbkdf2Hash prf) {
TypeHash hash_type = to_type_hash(prf);
auto pwd_prime = get_hmac(pepper_ptr, pepper_len, password_ptr, password_len, hash_type);
secure_buffer<uint8_t> tmp(std::move(pwd_prime));
secure_buffer<uint8_t, true> tmp(std::move(pwd_prime));
auto dk = pbkdf2(tmp.data(), tmp.size(), salt_ptr, salt_len, iterations, dk_len, prf);
secure_zero(tmp.data(), tmp.size());
return dk;
Expand Down
Loading