Skip to content

Commit

Permalink
src: move pbkdf2 to ncrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 6, 2024
1 parent bcdea4d commit 82bbfeb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
23 changes: 23 additions & 0 deletions deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1361,4 +1361,27 @@ DataPointer scrypt(const Buffer<const char>& pass,
return {};
}

DataPointer pbkdf2(const EVP_MD* md,
const Buffer<const char>& pass,
const Buffer<const unsigned char>& salt,
uint32_t iterations,
size_t length) {
ClearErrorOnReturn clearErrorOnReturn;

if (pass.len > INT_MAX ||
salt.len > INT_MAX ||
length > INT_MAX) {
return {};
}

auto dp = DataPointer::Alloc(length);
if (dp && PKCS5_PBKDF2_HMAC(pass.data, pass.len, salt.data, salt.len,
iterations, md, length,
reinterpret_cast<unsigned char*>(dp.get()))) {
return dp;
}

return {};
}

} // namespace ncrypto
6 changes: 6 additions & 0 deletions deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ DataPointer scrypt(const Buffer<const char>& pass,
uint64_t maxmem,
size_t length);

DataPointer pbkdf2(const EVP_MD* md,
const Buffer<const char>& pass,
const Buffer<const unsigned char>& salt,
uint32_t iterations,
size_t length);

// ============================================================================
// Version metadata
#define NCRYPTO_VERSION "0.0.1"
Expand Down
26 changes: 10 additions & 16 deletions src/crypto/crypto_pbkdf2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Maybe<bool> PBKDF2Traits::AdditionalConfig(
}

Utf8Value name(args.GetIsolate(), args[offset + 4]);
params->digest = EVP_get_digestbyname(*name);
params->digest = ncrypto::getDigestByName(name.ToStringView());
if (params->digest == nullptr) {
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *name);
return Nothing<bool>();
Expand All @@ -115,23 +115,17 @@ bool PBKDF2Traits::DeriveBits(
Environment* env,
const PBKDF2Config& params,
ByteSource* out) {
ByteSource::Builder buf(params.length);

// Both pass and salt may be zero length here.
// The generated bytes are stored in buf, which is
// assigned to out on success.

if (PKCS5_PBKDF2_HMAC(params.pass.data<char>(),
params.pass.size(),
params.salt.data<unsigned char>(),
params.salt.size(),
params.iterations,
params.digest,
params.length,
buf.data<unsigned char>()) <= 0) {
return false;
}
*out = std::move(buf).release();
auto dp = ncrypto::pbkdf2(
params.digest,
ncrypto::Buffer<const char>(params.pass.data<char>(), params.pass.size()),
ncrypto::Buffer<const unsigned char>(params.salt.data<unsigned char>(), params.salt.size()),
params.iterations,
params.length);

if (!dp) return false;
*out = ByteSource::Allocated(dp.release());
return true;
}

Expand Down

0 comments on commit 82bbfeb

Please sign in to comment.