mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
crypto: pass CryptoKey handles to KDF jobs
Pass CryptoKey handles directly into KDF jobs instead of exporting secret bytes in lib. Normalize HKDF, PBKDF2, and Argon2 around the same job construction pattern so WebCrypto derivation paths avoid extra key material copies and keep operation failures in native job handling. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: https://github.com/nodejs/node/pull/63363 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
This commit is contained in:
1 parent
64ba74d847
commit
e04cd17dc0
9 files changed
+95
-74
No files matched your search
@@ -211,28 +211,16 @@ function argon2DeriveBits(algorithm, baseKey, length) {
|
||||
'Argon2id': kTypeArgon2id,
|
||||
}[algorithm.name];
|
||||
|
||||
let message;
|
||||
try {
|
||||
// TODO(panva): call the job directly without needing to re-export the handle
|
||||
message = getCryptoKeyHandle(baseKey).export();
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
}
|
||||
|
||||
const empty = new Uint8Array(0);
|
||||
|
||||
return jobPromise(() => new Argon2Job(
|
||||
kCryptoJobWebCrypto,
|
||||
message,
|
||||
getCryptoKeyHandle(baseKey),
|
||||
algorithm.nonce,
|
||||
algorithm.parallelism,
|
||||
length / 8,
|
||||
algorithm.memory,
|
||||
algorithm.passes,
|
||||
algorithm.secretValue === undefined ? empty : algorithm.secretValue,
|
||||
algorithm.associatedData === undefined ? empty : algorithm.associatedData,
|
||||
algorithm.secretValue === undefined ? new Uint8Array() : algorithm.secretValue,
|
||||
algorithm.associatedData === undefined ? new Uint8Array() : algorithm.associatedData,
|
||||
type));
|
||||
}
|
||||
|
||||
|
||||
@@ -29,10 +29,9 @@ const {
|
||||
} = require('internal/crypto/util');
|
||||
|
||||
const {
|
||||
createSecretKey,
|
||||
getCryptoKeyHandle,
|
||||
getKeyObjectHandle,
|
||||
isKeyObject,
|
||||
prepareSecretKey,
|
||||
} = require('internal/crypto/keys');
|
||||
|
||||
const {
|
||||
@@ -78,10 +77,10 @@ const validateParameters = hideStackFrames((hash, key, salt, info, length) => {
|
||||
|
||||
function prepareKey(key) {
|
||||
if (isKeyObject(key))
|
||||
return getKeyObjectHandle(key);
|
||||
return prepareSecretKey(key);
|
||||
|
||||
if (isAnyArrayBuffer(key))
|
||||
return getKeyObjectHandle(createSecretKey(key));
|
||||
return key;
|
||||
|
||||
key = toBuf(key);
|
||||
|
||||
@@ -99,7 +98,7 @@ function prepareKey(key) {
|
||||
key);
|
||||
}
|
||||
|
||||
return getKeyObjectHandle(createSecretKey(key));
|
||||
return key;
|
||||
}
|
||||
|
||||
function hkdf(hash, key, salt, info, length, callback) {
|
||||
@@ -157,18 +156,9 @@ function hkdfDeriveBits(algorithm, baseKey, length) {
|
||||
if (length === 0)
|
||||
return PromiseResolve(new ArrayBuffer(0));
|
||||
|
||||
let normalizedHash;
|
||||
try {
|
||||
normalizedHash = normalizeHashName(hash.name);
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
}
|
||||
|
||||
return jobPromise(() => new HKDFJob(
|
||||
kCryptoJobWebCrypto,
|
||||
normalizedHash,
|
||||
normalizeHashName(hash.name),
|
||||
getCryptoKeyHandle(baseKey),
|
||||
salt,
|
||||
info,
|
||||
|
||||
@@ -114,25 +114,13 @@ function pbkdf2DeriveBits(algorithm, baseKey, length) {
|
||||
if (length === 0)
|
||||
return PromiseResolve(new ArrayBuffer(0));
|
||||
|
||||
let password;
|
||||
let normalizedHash;
|
||||
try {
|
||||
// TODO(panva): call the job directly without needing to re-export the handle
|
||||
password = getCryptoKeyHandle(baseKey).export();
|
||||
normalizedHash = normalizeHashName(hash.name);
|
||||
} catch (err) {
|
||||
throw lazyDOMException(
|
||||
'The operation failed for an operation-specific reason',
|
||||
{ name: 'OperationError', cause: err });
|
||||
}
|
||||
|
||||
return jobPromise(() => new PBKDF2Job(
|
||||
kCryptoJobWebCrypto,
|
||||
password,
|
||||
getCryptoKeyHandle(baseKey),
|
||||
salt,
|
||||
iterations,
|
||||
length / 8,
|
||||
normalizedHash));
|
||||
normalizeHashName(hash.name)));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "crypto/crypto_argon2.h"
|
||||
#include "async_wrap-inl.h"
|
||||
#include "base_object-inl.h"
|
||||
#include "crypto/crypto_keys.h"
|
||||
#include "memory_tracker-inl.h"
|
||||
#include "threadpoolwork-inl.h"
|
||||
|
||||
#if OPENSSL_WITH_ARGON2
|
||||
@@ -19,6 +22,7 @@ using v8::Value;
|
||||
|
||||
Argon2Config::Argon2Config(Argon2Config&& other) noexcept
|
||||
: mode{other.mode},
|
||||
key{std::move(other.key)},
|
||||
pass{std::move(other.pass)},
|
||||
salt{std::move(other.salt)},
|
||||
secret{std::move(other.secret)},
|
||||
@@ -36,8 +40,9 @@ Argon2Config& Argon2Config::operator=(Argon2Config&& other) noexcept {
|
||||
}
|
||||
|
||||
void Argon2Config::MemoryInfo(MemoryTracker* tracker) const {
|
||||
if (key) tracker->TrackField("key", key);
|
||||
if (IsCryptoJobAsync(mode)) {
|
||||
tracker->TrackFieldWithSize("pass", pass.size());
|
||||
if (!key) tracker->TrackFieldWithSize("pass", pass.size());
|
||||
tracker->TrackFieldWithSize("salt", salt.size());
|
||||
tracker->TrackFieldWithSize("secret", secret.size());
|
||||
tracker->TrackFieldWithSize("ad", ad.size());
|
||||
@@ -59,14 +64,23 @@ Maybe<void> Argon2Traits::AdditionalConfig(
|
||||
|
||||
config->mode = mode;
|
||||
|
||||
ArrayBufferOrViewContents<char> pass(args[offset]);
|
||||
CHECK(KeyObjectHandle::HasInstance(env, args[offset]) ||
|
||||
IsAnyBufferSource(args[offset])); // pass
|
||||
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
|
||||
ArrayBufferOrViewContents<char> secret(args[offset + 6]);
|
||||
ArrayBufferOrViewContents<char> ad(args[offset + 7]);
|
||||
|
||||
if (!pass.CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
|
||||
return Nothing<void>();
|
||||
if (KeyObjectHandle::HasInstance(env, args[offset])) {
|
||||
KeyObjectHandle* key;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset], Nothing<void>());
|
||||
config->key = key->Data().addRef();
|
||||
} else {
|
||||
ArrayBufferOrViewContents<char> pass(args[offset]);
|
||||
if (!pass.CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
|
||||
return Nothing<void>();
|
||||
}
|
||||
config->pass = IsCryptoJobAsync(mode) ? pass.ToCopy() : pass.ToByteSource();
|
||||
}
|
||||
|
||||
if (!salt.CheckSizeInt32()) [[unlikely]] {
|
||||
@@ -85,7 +99,6 @@ Maybe<void> Argon2Traits::AdditionalConfig(
|
||||
}
|
||||
|
||||
const bool isAsync = IsCryptoJobAsync(mode);
|
||||
config->pass = isAsync ? pass.ToCopy() : pass.ToByteSource();
|
||||
config->salt = isAsync ? salt.ToCopy() : salt.ToByteSource();
|
||||
config->secret = isAsync ? secret.ToCopy() : secret.ToByteSource();
|
||||
config->ad = isAsync ? ad.ToCopy() : ad.ToByteSource();
|
||||
@@ -119,7 +132,13 @@ bool Argon2Traits::DeriveBits(Environment* env,
|
||||
}
|
||||
|
||||
// Both the pass and salt may be zero-length at this point
|
||||
auto dp = ncrypto::argon2(config.pass,
|
||||
const ncrypto::Buffer<const char> pass{
|
||||
.data = config.key ? config.key.GetSymmetricKey()
|
||||
: config.pass.data<const char>(),
|
||||
.len = config.key ? config.key.GetSymmetricKeySize() : config.pass.size(),
|
||||
};
|
||||
|
||||
auto dp = ncrypto::argon2(pass,
|
||||
config.salt,
|
||||
config.lanes,
|
||||
config.keylen,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
#include "crypto/crypto_keys.h"
|
||||
#include "crypto/crypto_util.h"
|
||||
|
||||
namespace node::crypto {
|
||||
@@ -22,6 +23,7 @@ namespace node::crypto {
|
||||
|
||||
struct Argon2Config final : public MemoryRetainer {
|
||||
CryptoJobMode mode;
|
||||
KeyObjectData key;
|
||||
ByteSource pass;
|
||||
ByteSource salt;
|
||||
ByteSource secret;
|
||||
|
||||
+27
-10
@@ -24,6 +24,7 @@ HKDFConfig::HKDFConfig(HKDFConfig&& other) noexcept
|
||||
length(other.length),
|
||||
digest(other.digest),
|
||||
key(std::move(other.key)),
|
||||
key_data(std::move(other.key_data)),
|
||||
salt(std::move(other.salt)),
|
||||
info(std::move(other.info)) {}
|
||||
|
||||
@@ -49,7 +50,8 @@ Maybe<void> HKDFTraits::AdditionalConfig(
|
||||
params->mode = mode;
|
||||
|
||||
CHECK(args[offset]->IsString()); // Hash
|
||||
CHECK(args[offset + 1]->IsObject()); // Key
|
||||
CHECK(KeyObjectHandle::HasInstance(env, args[offset + 1]) ||
|
||||
IsAnyBufferSource(args[offset + 1])); // Key
|
||||
CHECK(IsAnyBufferSource(args[offset + 2])); // Salt
|
||||
CHECK(IsAnyBufferSource(args[offset + 3])); // Info
|
||||
CHECK(args[offset + 4]->IsUint32()); // Length
|
||||
@@ -61,9 +63,19 @@ Maybe<void> HKDFTraits::AdditionalConfig(
|
||||
return Nothing<void>();
|
||||
}
|
||||
|
||||
KeyObjectHandle* key;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 1], Nothing<void>());
|
||||
params->key = key->Data().addRef();
|
||||
if (KeyObjectHandle::HasInstance(env, args[offset + 1])) {
|
||||
KeyObjectHandle* key;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 1], Nothing<void>());
|
||||
params->key = key->Data().addRef();
|
||||
} else {
|
||||
ArrayBufferOrViewContents<char> key_data(args[offset + 1]);
|
||||
if (!key_data.CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "key is too big");
|
||||
return Nothing<void>();
|
||||
}
|
||||
params->key_data =
|
||||
IsCryptoJobAsync(mode) ? key_data.ToCopy() : key_data.ToByteSource();
|
||||
}
|
||||
|
||||
ArrayBufferOrViewContents<char> salt(args[offset + 2]);
|
||||
ArrayBufferOrViewContents<char> info(args[offset + 3]);
|
||||
@@ -98,12 +110,16 @@ bool HKDFTraits::DeriveBits(Environment* env,
|
||||
ByteSource* out,
|
||||
CryptoJobMode mode,
|
||||
CryptoErrorStore* errors) {
|
||||
const ncrypto::Buffer<const unsigned char> key_data{
|
||||
.data = params.key ? reinterpret_cast<const unsigned char*>(
|
||||
params.key.GetSymmetricKey())
|
||||
: params.key_data.data<unsigned char>(),
|
||||
.len = params.key ? params.key.GetSymmetricKeySize()
|
||||
: params.key_data.size(),
|
||||
};
|
||||
|
||||
auto dp = ncrypto::hkdf(params.digest,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = reinterpret_cast<const unsigned char*>(
|
||||
params.key.GetSymmetricKey()),
|
||||
.len = params.key.GetSymmetricKeySize(),
|
||||
},
|
||||
key_data,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.info.data<const unsigned char>(),
|
||||
.len = params.info.size(),
|
||||
@@ -124,9 +140,10 @@ bool HKDFTraits::DeriveBits(Environment* env,
|
||||
}
|
||||
|
||||
void HKDFConfig::MemoryInfo(MemoryTracker* tracker) const {
|
||||
tracker->TrackField("key", key);
|
||||
if (key) tracker->TrackField("key", key);
|
||||
// If the job is sync, then the HKDFConfig does not own the data
|
||||
if (IsCryptoJobAsync(mode)) {
|
||||
if (!key) tracker->TrackFieldWithSize("key", key_data.size());
|
||||
tracker->TrackFieldWithSize("salt", salt.size());
|
||||
tracker->TrackFieldWithSize("info", info.size());
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ struct HKDFConfig final : public MemoryRetainer {
|
||||
size_t length;
|
||||
ncrypto::Digest digest;
|
||||
KeyObjectData key;
|
||||
ByteSource key_data;
|
||||
ByteSource salt;
|
||||
ByteSource info;
|
||||
|
||||
|
||||
+26
-12
@@ -1,5 +1,7 @@
|
||||
#include "crypto/crypto_pbkdf2.h"
|
||||
#include "async_wrap-inl.h"
|
||||
#include "base_object-inl.h"
|
||||
#include "crypto/crypto_keys.h"
|
||||
#include "crypto/crypto_util.h"
|
||||
#include "env-inl.h"
|
||||
#include "memory_tracker-inl.h"
|
||||
@@ -21,6 +23,7 @@ using v8::Value;
|
||||
namespace crypto {
|
||||
PBKDF2Config::PBKDF2Config(PBKDF2Config&& other) noexcept
|
||||
: mode(other.mode),
|
||||
key(std::move(other.key)),
|
||||
pass(std::move(other.pass)),
|
||||
salt(std::move(other.salt)),
|
||||
iterations(other.iterations),
|
||||
@@ -34,9 +37,10 @@ PBKDF2Config& PBKDF2Config::operator=(PBKDF2Config&& other) noexcept {
|
||||
}
|
||||
|
||||
void PBKDF2Config::MemoryInfo(MemoryTracker* tracker) const {
|
||||
// The job is sync, the PBKDF2Config does not own the data.
|
||||
// If the job is sync, PBKDF2Config does not own the data.
|
||||
if (key) tracker->TrackField("key", key);
|
||||
if (IsCryptoJobAsync(mode)) {
|
||||
tracker->TrackFieldWithSize("pass", pass.size());
|
||||
if (!key) tracker->TrackFieldWithSize("pass", pass.size());
|
||||
tracker->TrackFieldWithSize("salt", salt.size());
|
||||
}
|
||||
}
|
||||
@@ -63,12 +67,21 @@ Maybe<void> PBKDF2Traits::AdditionalConfig(
|
||||
|
||||
params->mode = mode;
|
||||
|
||||
ArrayBufferOrViewContents<char> pass(args[offset]);
|
||||
CHECK(KeyObjectHandle::HasInstance(env, args[offset]) ||
|
||||
IsAnyBufferSource(args[offset])); // pass
|
||||
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
|
||||
|
||||
if (!pass.CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
|
||||
return Nothing<void>();
|
||||
if (KeyObjectHandle::HasInstance(env, args[offset])) {
|
||||
KeyObjectHandle* key;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset], Nothing<void>());
|
||||
params->key = key->Data().addRef();
|
||||
} else {
|
||||
ArrayBufferOrViewContents<char> pass(args[offset]);
|
||||
if (!pass.CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
|
||||
return Nothing<void>();
|
||||
}
|
||||
params->pass = IsCryptoJobAsync(mode) ? pass.ToCopy() : pass.ToByteSource();
|
||||
}
|
||||
|
||||
if (!salt.CheckSizeInt32()) [[unlikely]] {
|
||||
@@ -76,8 +89,6 @@ Maybe<void> PBKDF2Traits::AdditionalConfig(
|
||||
return Nothing<void>();
|
||||
}
|
||||
|
||||
params->pass = IsCryptoJobAsync(mode) ? pass.ToCopy() : pass.ToByteSource();
|
||||
|
||||
params->salt = IsCryptoJobAsync(mode) ? salt.ToCopy() : salt.ToByteSource();
|
||||
|
||||
CHECK(args[offset + 2]->IsInt32()); // iteration_count
|
||||
@@ -112,11 +123,14 @@ bool PBKDF2Traits::DeriveBits(Environment* env,
|
||||
CryptoJobMode mode,
|
||||
CryptoErrorStore* errors) {
|
||||
// Both pass and salt may be zero length here.
|
||||
const ncrypto::Buffer<const char> pass{
|
||||
.data = params.key ? params.key.GetSymmetricKey()
|
||||
: params.pass.data<const char>(),
|
||||
.len = params.key ? params.key.GetSymmetricKeySize() : params.pass.size(),
|
||||
};
|
||||
|
||||
auto dp = ncrypto::pbkdf2(params.digest,
|
||||
ncrypto::Buffer<const char>{
|
||||
.data = params.pass.data<const char>(),
|
||||
.len = params.pass.size(),
|
||||
},
|
||||
pass,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = params.salt.data<unsigned char>(),
|
||||
.len = params.salt.size(),
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
|
||||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
#include "crypto/crypto_util.h"
|
||||
#include "async_wrap.h"
|
||||
#include "crypto/crypto_keys.h"
|
||||
#include "crypto/crypto_util.h"
|
||||
#include "env.h"
|
||||
#include "memory_tracker.h"
|
||||
#include "v8.h"
|
||||
@@ -26,6 +27,7 @@ namespace crypto {
|
||||
|
||||
struct PBKDF2Config final : public MemoryRetainer {
|
||||
CryptoJobMode mode;
|
||||
KeyObjectData key;
|
||||
ByteSource pass;
|
||||
ByteSource salt;
|
||||
int32_t iterations;
|
||||
|
||||
Reference in new issue
Block a user