mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
src: move hkdf, scrypto, pbkdf2 impl to ncrypto
PR-URL: https://github.com/nodejs/node/pull/54651 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
This commit is contained in:
1 parent
16e6747c35
commit
dcc2ed944f
6 files changed
+229
-101
No files matched your search
+19
-50
@@ -56,7 +56,7 @@ Maybe<bool> HKDFTraits::AdditionalConfig(
|
||||
CHECK(args[offset + 4]->IsUint32()); // Length
|
||||
|
||||
Utf8Value hash(env->isolate(), args[offset]);
|
||||
params->digest = EVP_get_digestbyname(*hash);
|
||||
params->digest = ncrypto::getDigestByName(hash.ToStringView());
|
||||
if (params->digest == nullptr) {
|
||||
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *hash);
|
||||
return Nothing<bool>();
|
||||
@@ -90,9 +90,7 @@ Maybe<bool> HKDFTraits::AdditionalConfig(
|
||||
// HKDF-Expand computes up to 255 HMAC blocks, each having as many bits as the
|
||||
// output of the hash function. 255 is a hard limit because HKDF appends an
|
||||
// 8-bit counter to each HMAC'd message, starting at 1.
|
||||
constexpr size_t kMaxDigestMultiplier = 255;
|
||||
size_t max_length = EVP_MD_size(params->digest) * kMaxDigestMultiplier;
|
||||
if (params->length > max_length) {
|
||||
if (!ncrypto::checkHkdfLength(params->digest, params->length)) {
|
||||
THROW_ERR_CRYPTO_INVALID_KEYLEN(env);
|
||||
return Nothing<bool>();
|
||||
}
|
||||
@@ -104,53 +102,24 @@ bool HKDFTraits::DeriveBits(
|
||||
Environment* env,
|
||||
const HKDFConfig& params,
|
||||
ByteSource* out) {
|
||||
EVPKeyCtxPointer ctx =
|
||||
EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr));
|
||||
if (!ctx || !EVP_PKEY_derive_init(ctx.get()) ||
|
||||
!EVP_PKEY_CTX_set_hkdf_md(ctx.get(), params.digest) ||
|
||||
!EVP_PKEY_CTX_add1_hkdf_info(
|
||||
ctx.get(), params.info.data<unsigned char>(), params.info.size())) {
|
||||
return false;
|
||||
}
|
||||
auto dp = ncrypto::hkdf(params.digest,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = reinterpret_cast<const unsigned char*>(
|
||||
params.key->GetSymmetricKey()),
|
||||
.len = params.key->GetSymmetricKeySize(),
|
||||
},
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.info.data<const unsigned char>(),
|
||||
.len = params.info.size(),
|
||||
},
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.salt.data<const unsigned char>(),
|
||||
.len = params.salt.size(),
|
||||
},
|
||||
params.length);
|
||||
if (!dp) return false;
|
||||
|
||||
// TODO(panva): Once support for OpenSSL 1.1.1 is dropped the whole
|
||||
// of HKDFTraits::DeriveBits can be refactored to use
|
||||
// EVP_KDF which does handle zero length key.
|
||||
|
||||
std::string_view salt;
|
||||
if (params.salt.size() != 0) {
|
||||
salt = {params.salt.data<char>(), params.salt.size()};
|
||||
} else {
|
||||
static const char default_salt[EVP_MAX_MD_SIZE] = {0};
|
||||
salt = {default_salt, static_cast<unsigned>(EVP_MD_size(params.digest))};
|
||||
}
|
||||
|
||||
// We do not use EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND and instead implement
|
||||
// the extraction step ourselves because EVP_PKEY_derive does not handle
|
||||
// zero-length keys, which are required for Web Crypto.
|
||||
unsigned char pseudorandom_key[EVP_MAX_MD_SIZE];
|
||||
unsigned int prk_len = sizeof(pseudorandom_key);
|
||||
if (HMAC(
|
||||
params.digest,
|
||||
salt.data(),
|
||||
salt.size(),
|
||||
reinterpret_cast<const unsigned char*>(params.key->GetSymmetricKey()),
|
||||
params.key->GetSymmetricKeySize(),
|
||||
pseudorandom_key,
|
||||
&prk_len) == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (!EVP_PKEY_CTX_hkdf_mode(ctx.get(), EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) ||
|
||||
!EVP_PKEY_CTX_set1_hkdf_key(ctx.get(), pseudorandom_key, prk_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t length = params.length;
|
||||
ByteSource::Builder buf(length);
|
||||
if (EVP_PKEY_derive(ctx.get(), buf.data<unsigned char>(), &length) <= 0)
|
||||
return false;
|
||||
|
||||
*out = std::move(buf).release();
|
||||
*out = ByteSource::Allocated(dp.release());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+17
-20
@@ -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>();
|
||||
@@ -111,27 +111,24 @@ Maybe<bool> PBKDF2Traits::AdditionalConfig(
|
||||
return Just(true);
|
||||
}
|
||||
|
||||
bool PBKDF2Traits::DeriveBits(
|
||||
Environment* env,
|
||||
const PBKDF2Config& params,
|
||||
ByteSource* out) {
|
||||
ByteSource::Builder buf(params.length);
|
||||
|
||||
bool PBKDF2Traits::DeriveBits(Environment* env,
|
||||
const PBKDF2Config& params,
|
||||
ByteSource* out) {
|
||||
// Both pass and salt may be zero length here.
|
||||
// The generated bytes are stored in buf, which is
|
||||
// assigned to out on success.
|
||||
auto dp = ncrypto::pbkdf2(params.digest,
|
||||
ncrypto::Buffer<const char>{
|
||||
.data = params.pass.data<const char>(),
|
||||
.len = params.pass.size(),
|
||||
},
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.salt.data<unsigned char>(),
|
||||
.len = params.salt.size(),
|
||||
},
|
||||
params.iterations,
|
||||
params.length);
|
||||
|
||||
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();
|
||||
if (!dp) return false;
|
||||
*out = ByteSource::Allocated(dp.release());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+28
-30
@@ -93,17 +93,11 @@ Maybe<bool> ScryptTraits::AdditionalConfig(
|
||||
params->p = args[offset + 4].As<Uint32>()->Value();
|
||||
params->maxmem = args[offset + 5]->IntegerValue(env->context()).ToChecked();
|
||||
|
||||
if (EVP_PBE_scrypt(
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
0,
|
||||
params->N,
|
||||
params->r,
|
||||
params->p,
|
||||
params->maxmem,
|
||||
nullptr,
|
||||
0) != 1) {
|
||||
params->length = args[offset + 6].As<Int32>()->Value();
|
||||
CHECK_GE(params->length, 0);
|
||||
|
||||
if (!ncrypto::checkScryptParams(
|
||||
params->N, params->r, params->p, params->maxmem)) {
|
||||
// Do not use CryptoErrorStore or ThrowCryptoError here in order to maintain
|
||||
// backward compatibility with ERR_CRYPTO_INVALID_SCRYPT_PARAMS.
|
||||
uint32_t err = ERR_peek_last_error();
|
||||
@@ -118,9 +112,6 @@ Maybe<bool> ScryptTraits::AdditionalConfig(
|
||||
return Nothing<bool>();
|
||||
}
|
||||
|
||||
params->length = args[offset + 6].As<Int32>()->Value();
|
||||
CHECK_GE(params->length, 0);
|
||||
|
||||
return Just(true);
|
||||
}
|
||||
|
||||
@@ -128,23 +119,30 @@ bool ScryptTraits::DeriveBits(
|
||||
Environment* env,
|
||||
const ScryptConfig& params,
|
||||
ByteSource* out) {
|
||||
ByteSource::Builder buf(params.length);
|
||||
|
||||
// Both the pass and salt may be zero-length at this point
|
||||
|
||||
if (!EVP_PBE_scrypt(params.pass.data<char>(),
|
||||
params.pass.size(),
|
||||
params.salt.data<unsigned char>(),
|
||||
params.salt.size(),
|
||||
params.N,
|
||||
params.r,
|
||||
params.p,
|
||||
params.maxmem,
|
||||
buf.data<unsigned char>(),
|
||||
params.length)) {
|
||||
return false;
|
||||
// If the params.length is zero-length, just return an empty buffer.
|
||||
// It's useless, yes, but allowed via the API.
|
||||
if (params.length == 0) {
|
||||
*out = ByteSource();
|
||||
return true;
|
||||
}
|
||||
*out = std::move(buf).release();
|
||||
|
||||
auto dp = ncrypto::scrypt(
|
||||
ncrypto::Buffer<const char>{
|
||||
.data = params.pass.data<char>(),
|
||||
.len = params.pass.size(),
|
||||
},
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.salt.data<unsigned char>(),
|
||||
.len = params.salt.size(),
|
||||
},
|
||||
params.N,
|
||||
params.r,
|
||||
params.p,
|
||||
params.maxmem,
|
||||
params.length);
|
||||
|
||||
if (!dp) return false;
|
||||
*out = ByteSource::Allocated(dp.release());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user