src: implement MemoryRetainer protocol for ByteSource

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64660
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-08-05 11:15:02 +02:00
1 parent 915cabbfcf
commit 6cd643acaa
32 files changed
+134 -200

No files matched your search

+3 -10
View File
@@ -491,8 +491,7 @@ void UseDefaultIV(AESCipherConfig* params) {
} // namespace
AESCipherConfig::AESCipherConfig(AESCipherConfig&& other) noexcept
: mode(other.mode),
variant(other.variant),
: variant(other.variant),
cipher(other.cipher),
length(other.length),
iv(std::move(other.iv)),
@@ -505,12 +504,8 @@ AESCipherConfig& AESCipherConfig::operator=(AESCipherConfig&& other) noexcept {
}
void AESCipherConfig::MemoryInfo(MemoryTracker* tracker) const {
// If mode is sync, then the data in each of these properties
// is not owned by the AESCipherConfig, so we ignore it.
if (IsCryptoJobAsync(mode)) {
tracker->TrackFieldWithSize("iv", iv.size());
tracker->TrackFieldWithSize("additional_data", additional_data.size());
}
tracker->TraitTrackInline(iv, "iv");
tracker->TraitTrackInline(additional_data, "additional_data");
}
Maybe<void> AESCipherTraits::AdditionalConfig(
@@ -521,8 +516,6 @@ Maybe<void> AESCipherTraits::AdditionalConfig(
AESCipherConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
CHECK(args[offset]->IsUint32()); // Key Variant
params->variant =
static_cast<AESKeyVariant>(args[offset].As<Uint32>()->Value());
-1
View File
@@ -58,7 +58,6 @@ enum class AESKeyVariant {
};
struct AESCipherConfig final : public MemoryRetainer {
CryptoJobMode mode;
AESKeyVariant variant;
ncrypto::Cipher cipher;
size_t length;
+8 -11
View File
@@ -21,8 +21,7 @@ using v8::Uint32;
using v8::Value;
Argon2Config::Argon2Config(Argon2Config&& other) noexcept
: mode{other.mode},
key{std::move(other.key)},
: key{std::move(other.key)},
pass{std::move(other.pass)},
salt{std::move(other.salt)},
secret{std::move(other.secret)},
@@ -40,13 +39,13 @@ Argon2Config& Argon2Config::operator=(Argon2Config&& other) noexcept {
}
void Argon2Config::MemoryInfo(MemoryTracker* tracker) const {
if (key) tracker->TrackField("key", key);
if (IsCryptoJobAsync(mode)) {
if (!key) tracker->TrackFieldWithSize("pass", pass.size());
tracker->TrackFieldWithSize("salt", salt.size());
tracker->TrackFieldWithSize("secret", secret.size());
tracker->TrackFieldWithSize("ad", ad.size());
}
if (key)
tracker->TrackField("key", key);
else
tracker->TraitTrackInline(pass, "pass");
tracker->TraitTrackInline(salt, "salt");
tracker->TraitTrackInline(secret, "secret");
tracker->TraitTrackInline(ad, "ad");
}
MaybeLocal<Value> Argon2Traits::EncodeOutput(Environment* env,
@@ -62,8 +61,6 @@ Maybe<void> Argon2Traits::AdditionalConfig(
Argon2Config* config) {
Environment* env = Environment::GetCurrent(args);
config->mode = mode;
CHECK(KeyObjectHandle::HasInstance(env, args[offset]) ||
IsAnyBufferSource(args[offset])); // pass
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
-1
View File
@@ -22,7 +22,6 @@ namespace node::crypto {
// at least 16 bytes in length.
struct Argon2Config final : public MemoryRetainer {
CryptoJobMode mode;
KeyObjectData key;
ByteSource pass;
ByteSource salt;
+3 -9
View File
@@ -81,8 +81,7 @@ bool ValidateAdditionalData(Environment* env,
ChaCha20Poly1305CipherConfig::ChaCha20Poly1305CipherConfig(
ChaCha20Poly1305CipherConfig&& other) noexcept
: mode(other.mode),
cipher(other.cipher),
: cipher(other.cipher),
iv(std::move(other.iv)),
additional_data(std::move(other.additional_data)) {}
@@ -94,12 +93,8 @@ ChaCha20Poly1305CipherConfig& ChaCha20Poly1305CipherConfig::operator=(
}
void ChaCha20Poly1305CipherConfig::MemoryInfo(MemoryTracker* tracker) const {
// If mode is sync, then the data in each of these properties
// is not owned by the ChaCha20Poly1305CipherConfig, so we ignore it.
if (IsCryptoJobAsync(mode)) {
tracker->TrackFieldWithSize("iv", iv.size());
tracker->TrackFieldWithSize("additional_data", additional_data.size());
}
tracker->TraitTrackInline(iv, "iv");
tracker->TraitTrackInline(additional_data, "additional_data");
}
Maybe<void> ChaCha20Poly1305CipherTraits::AdditionalConfig(
@@ -110,7 +105,6 @@ Maybe<void> ChaCha20Poly1305CipherTraits::AdditionalConfig(
ChaCha20Poly1305CipherConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
params->cipher = ncrypto::Cipher::CHACHA20_POLY1305;
#ifndef OPENSSL_IS_BORINGSSL
-1
View File
@@ -13,7 +13,6 @@ namespace node::crypto {
constexpr unsigned kChaCha20Poly1305AuthTagLength = 16;
struct ChaCha20Poly1305CipherConfig final : public MemoryRetainer {
CryptoJobMode mode;
ncrypto::Cipher cipher;
ByteSource iv;
ByteSource additional_data;
+2 -3
View File
@@ -268,9 +268,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
SET_SELF_SIZE(CipherJob)
void MemoryInfo(MemoryTracker* tracker) const override {
if (IsCryptoJobAsync(CryptoJob<CipherTraits>::mode()))
tracker->TrackFieldWithSize("in", in_.size());
tracker->TrackFieldWithSize("out", out_.size());
tracker->TraitTrackInline(in_, "in");
tracker->TraitTrackInline(out_, "out");
CryptoJob<CipherTraits>::MemoryInfo(tracker);
}
+7 -19
View File
@@ -58,7 +58,7 @@ Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
void Hash::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0);
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0);
tracker->TraitTrackInline(digest_, "md");
}
#if NCRYPTO_USE_BORINGSSL_EVP_DO_ALL_FALLBACK
@@ -528,10 +528,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
}
HashConfig::HashConfig(HashConfig&& other) noexcept
: mode(other.mode),
in(std::move(other.in)),
digest(other.digest),
length(other.length) {}
: in(std::move(other.in)), digest(other.digest), length(other.length) {}
HashConfig& HashConfig::operator=(HashConfig&& other) noexcept {
if (&other == this) return *this;
@@ -540,8 +537,7 @@ HashConfig& HashConfig::operator=(HashConfig&& other) noexcept {
}
void HashConfig::MemoryInfo(MemoryTracker* tracker) const {
// If the Job is sync, then the HashConfig does not own the data.
if (IsCryptoJobAsync(mode)) tracker->TrackFieldWithSize("in", in.size());
tracker->TraitTrackInline(in, "in");
}
MaybeLocal<Value> HashTraits::EncodeOutput(Environment* env,
@@ -557,8 +553,6 @@ Maybe<void> HashTraits::AdditionalConfig(
HashConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
CHECK(args[offset]->IsString()); // Hash algorithm
Utf8Value digest(env->isolate(), args[offset]);
params->digest = ncrypto::getDigestByName(*digest);
@@ -787,8 +781,7 @@ bool DigestUpdateBytepad(ncrypto::EVPMDCtxPointer* ctx,
} // namespace
CShakeConfig::CShakeConfig(CShakeConfig&& other) noexcept
: mode(other.mode),
in(std::move(other.in)),
: in(std::move(other.in)),
function_name(std::move(other.function_name)),
customization(std::move(other.customization)),
variant(other.variant),
@@ -801,12 +794,9 @@ CShakeConfig& CShakeConfig::operator=(CShakeConfig&& other) noexcept {
}
void CShakeConfig::MemoryInfo(MemoryTracker* tracker) const {
// If the Job is sync, then the CShakeConfig does not own the data.
if (IsCryptoJobAsync(mode)) {
tracker->TrackFieldWithSize("in", in.size());
tracker->TrackFieldWithSize("function_name", function_name.size());
tracker->TrackFieldWithSize("customization", customization.size());
}
tracker->TraitTrackInline(in, "in");
tracker->TraitTrackInline(function_name, "function_name");
tracker->TraitTrackInline(customization, "customization");
}
MaybeLocal<Value> CShakeTraits::EncodeOutput(Environment* env,
@@ -822,8 +812,6 @@ Maybe<void> CShakeTraits::AdditionalConfig(
CShakeConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
CHECK(args[offset]->IsString()); // Algorithm name
Utf8Value algorithm_name(env->isolate(), args[offset]);
std::string_view algorithm_str = algorithm_name.ToStringView();
-2
View File
@@ -42,7 +42,6 @@ class Hash final : public BaseObject {
};
struct HashConfig final : public MemoryRetainer {
CryptoJobMode mode;
ByteSource in;
const EVP_MD* digest;
unsigned int length;
@@ -108,7 +107,6 @@ struct CShakeParams final {
bool DeriveCShakeBits(const CShakeParams& params, ByteSource* out);
struct CShakeConfig final : public MemoryRetainer {
CryptoJobMode mode;
ByteSource in;
ByteSource function_name;
ByteSource customization;
+7 -11
View File
@@ -20,8 +20,7 @@ using v8::Value;
namespace crypto {
HKDFConfig::HKDFConfig(HKDFConfig&& other) noexcept
: mode(other.mode),
length(other.length),
: length(other.length),
digest(other.digest),
key(std::move(other.key)),
key_data(std::move(other.key_data)),
@@ -47,8 +46,6 @@ Maybe<void> HKDFTraits::AdditionalConfig(
HKDFConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
CHECK(args[offset]->IsString()); // Hash
CHECK(KeyObjectHandle::HasInstance(env, args[offset + 1]) ||
IsAnyBufferSource(args[offset + 1])); // Key
@@ -140,13 +137,12 @@ bool HKDFTraits::DeriveBits(Environment* env,
}
void HKDFConfig::MemoryInfo(MemoryTracker* tracker) const {
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());
}
if (key)
tracker->TrackField("key", key);
else
tracker->TraitTrackInline(key_data, "key");
tracker->TraitTrackInline(salt, "salt");
tracker->TraitTrackInline(info, "info");
}
} // namespace crypto
-1
View File
@@ -12,7 +12,6 @@
namespace node {
namespace crypto {
struct HKDFConfig final : public MemoryRetainer {
CryptoJobMode mode;
size_t length;
ncrypto::Digest digest;
KeyObjectData key;
+3 -9
View File
@@ -154,8 +154,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
}
HmacConfig::HmacConfig(HmacConfig&& other) noexcept
: job_mode(other.job_mode),
mode(other.mode),
: mode(other.mode),
key(std::move(other.key)),
data(std::move(other.data)),
signature(std::move(other.signature)),
@@ -169,11 +168,8 @@ HmacConfig& HmacConfig::operator=(HmacConfig&& other) noexcept {
void HmacConfig::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("key", key);
// If the job is sync, then the HmacConfig does not own the data
if (IsCryptoJobAsync(job_mode)) {
tracker->TrackFieldWithSize("data", data.size());
tracker->TrackFieldWithSize("signature", signature.size());
}
tracker->TraitTrackInline(data, "data");
tracker->TraitTrackInline(signature, "signature");
}
Maybe<void> HmacTraits::AdditionalConfig(
@@ -183,8 +179,6 @@ Maybe<void> HmacTraits::AdditionalConfig(
HmacConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
CHECK(args[offset]->IsUint32()); // SignConfiguration::Mode
params->mode =
static_cast<SignConfiguration::Mode>(args[offset].As<Uint32>()->Value());
-1
View File
@@ -40,7 +40,6 @@ class Hmac : public BaseObject {
};
struct HmacConfig final : public MemoryRetainer {
CryptoJobMode job_mode;
SignConfiguration::Mode mode;
KeyObjectData key;
ByteSource data;
+12 -19
View File
@@ -27,10 +27,7 @@ using v8::Value;
namespace crypto {
KEMConfiguration::KEMConfiguration(KEMConfiguration&& other) noexcept
: job_mode(other.job_mode),
mode(other.mode),
key(std::move(other.key)),
ciphertext(std::move(other.ciphertext)) {}
: key(std::move(other.key)), ciphertext(std::move(other.ciphertext)) {}
KEMConfiguration& KEMConfiguration::operator=(
KEMConfiguration&& other) noexcept {
@@ -41,9 +38,7 @@ KEMConfiguration& KEMConfiguration::operator=(
void KEMConfiguration::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("key", key);
if (IsCryptoJobAsync(job_mode)) {
tracker->TrackFieldWithSize("ciphertext", ciphertext.size());
}
tracker->TraitTrackInline(ciphertext, "ciphertext");
}
namespace {
@@ -75,9 +70,6 @@ Maybe<void> KEMEncapsulateTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
KEMConfiguration* params) {
params->job_mode = mode;
params->mode = KEMMode::Encapsulate;
unsigned int key_offset = offset;
auto public_key_data =
KeyObjectData::GetPublicOrPrivateKeyFromJs(args, &key_offset);
@@ -126,8 +118,12 @@ void KEMEncapsulateJob::DoThreadPoolWork() {
ncrypto::ClearErrorOnReturn clear_error_on_return;
AdditionalParams* params = CryptoJob<KEMEncapsulateTraits>::params();
Mutex::ScopedLock lock(params->key.mutex());
out_ = ncrypto::KEM::Encapsulate(params->key.GetAsymmetricKey());
if (!out_) {
auto result = ncrypto::KEM::Encapsulate(params->key.GetAsymmetricKey());
if (result) {
out_.emplace();
out_->ciphertext = ByteSource::Allocated(result->ciphertext.release());
out_->shared_key = ByteSource::Allocated(result->shared_key.release());
} else {
CryptoErrorStore* errors = CryptoJob<KEMEncapsulateTraits>::errors();
errors->Insert(NodeCryptoError::ENCAPSULATION_FAILED);
errors->SetNodeErrorCode("ERR_CRYPTO_OPERATION_FAILED");
@@ -149,8 +145,8 @@ Maybe<void> KEMEncapsulateJob::ToResult(Local<Value>* err,
CHECK(errors->Empty());
*err = v8::Undefined(env->isolate());
ByteSource ciphertext = ByteSource::Allocated(out_->ciphertext.release());
ByteSource shared_key = ByteSource::Allocated(out_->shared_key.release());
ByteSource ciphertext = std::move(out_->ciphertext);
ByteSource shared_key = std::move(out_->shared_key);
if (mode() == kCryptoJobWebCrypto) {
Local<Object> output = Object::New(env->isolate());
@@ -188,8 +184,8 @@ Maybe<void> KEMEncapsulateJob::ToResult(Local<Value>* err,
void KEMEncapsulateJob::MemoryInfo(MemoryTracker* tracker) const {
if (out_) {
tracker->TrackFieldWithSize("ciphertext", out_->ciphertext.size());
tracker->TrackFieldWithSize("shared_key", out_->shared_key.size());
tracker->TraitTrackInline(out_->ciphertext, "ciphertext");
tracker->TraitTrackInline(out_->shared_key, "shared_key");
}
CryptoJob<KEMEncapsulateTraits>::MemoryInfo(tracker);
}
@@ -202,9 +198,6 @@ Maybe<void> KEMDecapsulateTraits::AdditionalConfig(
KEMConfiguration* params) {
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
params->mode = KEMMode::Decapsulate;
unsigned int key_offset = offset;
auto private_key_data =
KeyObjectData::GetPrivateKeyFromJs(args, &key_offset, true);
+6 -3
View File
@@ -18,8 +18,6 @@ namespace crypto {
enum class KEMMode { Encapsulate, Decapsulate };
struct KEMConfiguration final : public MemoryRetainer {
CryptoJobMode job_mode;
KEMMode mode;
KeyObjectData key;
ByteSource ciphertext;
@@ -67,7 +65,12 @@ class KEMEncapsulateJob final : public CryptoJob<KEMEncapsulateTraits> {
void MemoryInfo(MemoryTracker* tracker) const override;
private:
std::optional<ncrypto::KEM::EncapsulateResult> out_;
struct Output {
ByteSource ciphertext;
ByteSource shared_key;
};
std::optional<Output> out_;
};
struct KEMDecapsulateTraits final {
+1 -1
View File
@@ -54,7 +54,7 @@ EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) {
}
void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const {
if (out) tracker->TrackFieldWithSize("out", length);
tracker->TraitTrackInline(out, "out");
}
Maybe<void> SecretKeyGenTraits::AdditionalConfig(
+12 -23
View File
@@ -957,32 +957,21 @@ KeyObjectData::KeyObjectData(ByteSource symmetric_key)
KeyObjectData::KeyObjectData(KeyType type, EVPKeyPointer&& pkey)
: key_type_(type), data_(std::make_shared<Data>(std::move(pkey))) {}
void KeyObjectData::MemoryInfo(MemoryTracker* tracker) const {
if (!*this) return;
switch (GetKeyType()) {
case kKeyTypeSecret: {
if (data_->symmetric_key) {
tracker->TrackFieldWithSize("symmetric_key",
data_->symmetric_key.size());
}
break;
}
case kKeyTypePrivate:
// Fall through
case kKeyTypePublic: {
if (data_->asymmetric_key) {
tracker->TrackFieldWithSize(
"key",
kSizeOf_EVP_PKEY + data_->asymmetric_key.rawPublicKeySize() +
data_->asymmetric_key.rawPrivateKeySize());
}
break;
}
default:
UNREACHABLE();
void KeyObjectData::Data::MemoryInfo(MemoryTracker* tracker) const {
if (asymmetric_key) {
tracker->TrackFieldWithSize("key",
kSizeOf_EVP_PKEY +
asymmetric_key.rawPublicKeySize() +
asymmetric_key.rawPrivateKeySize());
} else {
tracker->TraitTrackInline(symmetric_key, "symmetric_key");
}
}
void KeyObjectData::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("data", data_);
}
Mutex& KeyObjectData::mutex() const {
if (!mutex_) mutex_ = std::make_shared<Mutex>();
return *mutex_.get();
+5 -1
View File
@@ -115,13 +115,17 @@ class KeyObjectData final : public MemoryRetainer {
KeyType key_type_;
mutable std::shared_ptr<Mutex> mutex_;
struct Data {
struct Data final : public MemoryRetainer {
const ByteSource symmetric_key;
const ncrypto::EVPKeyPointer asymmetric_key;
explicit Data(ByteSource symmetric_key)
: symmetric_key(std::move(symmetric_key)) {}
explicit Data(ncrypto::EVPKeyPointer asymmetric_key)
: asymmetric_key(std::move(asymmetric_key)) {}
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(KeyObjectData::Data)
SET_SELF_SIZE(Data)
};
std::shared_ptr<Data> data_;
+4 -10
View File
@@ -32,8 +32,7 @@ using v8::Uint32;
using v8::Value;
KmacConfig::KmacConfig(KmacConfig&& other) noexcept
: job_mode(other.job_mode),
mode(other.mode),
: mode(other.mode),
key(std::move(other.key)),
data(std::move(other.data)),
signature(std::move(other.signature)),
@@ -50,12 +49,9 @@ KmacConfig& KmacConfig::operator=(KmacConfig&& other) noexcept {
void KmacConfig::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("key", key);
// If the job is sync, then the KmacConfig does not own the data.
if (IsCryptoJobAsync(job_mode)) {
tracker->TrackFieldWithSize("data", data.size());
tracker->TrackFieldWithSize("signature", signature.size());
tracker->TrackFieldWithSize("customization", customization.size());
}
tracker->TraitTrackInline(data, "data");
tracker->TraitTrackInline(signature, "signature");
tracker->TraitTrackInline(customization, "customization");
}
Maybe<void> KmacTraits::AdditionalConfig(
@@ -65,8 +61,6 @@ Maybe<void> KmacTraits::AdditionalConfig(
KmacConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
CHECK(args[offset]->IsUint32()); // SignConfiguration::Mode
params->mode =
static_cast<SignConfiguration::Mode>(args[offset].As<Uint32>()->Value());
-1
View File
@@ -15,7 +15,6 @@ namespace node::crypto {
enum class KmacVariant { KMAC128, KMAC256 };
struct KmacConfig final : public MemoryRetainer {
CryptoJobMode job_mode;
SignConfiguration::Mode mode;
KeyObjectData key;
ByteSource data;
+6 -10
View File
@@ -22,8 +22,7 @@ using v8::Value;
namespace crypto {
PBKDF2Config::PBKDF2Config(PBKDF2Config&& other) noexcept
: mode(other.mode),
key(std::move(other.key)),
: key(std::move(other.key)),
pass(std::move(other.pass)),
salt(std::move(other.salt)),
iterations(other.iterations),
@@ -37,12 +36,11 @@ PBKDF2Config& PBKDF2Config::operator=(PBKDF2Config&& other) noexcept {
}
void PBKDF2Config::MemoryInfo(MemoryTracker* tracker) const {
// If the job is sync, PBKDF2Config does not own the data.
if (key) tracker->TrackField("key", key);
if (IsCryptoJobAsync(mode)) {
if (!key) tracker->TrackFieldWithSize("pass", pass.size());
tracker->TrackFieldWithSize("salt", salt.size());
}
if (key)
tracker->TrackField("key", key);
else
tracker->TraitTrackInline(pass, "pass");
tracker->TraitTrackInline(salt, "salt");
}
MaybeLocal<Value> PBKDF2Traits::EncodeOutput(Environment* env,
@@ -65,8 +63,6 @@ Maybe<void> PBKDF2Traits::AdditionalConfig(
PBKDF2Config* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
CHECK(KeyObjectHandle::HasInstance(env, args[offset]) ||
IsAnyBufferSource(args[offset])); // pass
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
-1
View File
@@ -26,7 +26,6 @@ namespace crypto {
// The iteration count should be as high as possible.
struct PBKDF2Config final : public MemoryRetainer {
CryptoJobMode mode;
KeyObjectData key;
ByteSource pass;
ByteSource salt;
+2 -5
View File
@@ -219,14 +219,12 @@ WebCryptoCipherStatus RSA_Cipher(Environment* env,
} // namespace
RSACipherConfig::RSACipherConfig(RSACipherConfig&& other) noexcept
: mode(other.mode),
label(std::move(other.label)),
: label(std::move(other.label)),
padding(other.padding),
digest(other.digest) {}
void RSACipherConfig::MemoryInfo(MemoryTracker* tracker) const {
if (IsCryptoJobAsync(mode))
tracker->TrackFieldWithSize("label", label.size());
tracker->TraitTrackInline(label, "label");
}
Maybe<void> RSACipherTraits::AdditionalConfig(
@@ -237,7 +235,6 @@ Maybe<void> RSACipherTraits::AdditionalConfig(
RSACipherConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
params->padding = RSA_PKCS1_OAEP_PADDING;
CHECK(args[offset]->IsUint32());
-1
View File
@@ -53,7 +53,6 @@ struct RsaKeyGenTraits final {
using RSAKeyPairGenJob = KeyGenJob<KeyPairGenTraits<RsaKeyGenTraits>>;
struct RSACipherConfig final : public MemoryRetainer {
CryptoJobMode mode = kCryptoJobAsync;
ByteSource label;
int padding = 0;
ncrypto::Digest digest;
+10 -15
View File
@@ -22,14 +22,13 @@ namespace crypto {
#ifndef OPENSSL_NO_SCRYPT
ScryptConfig::ScryptConfig(ScryptConfig&& other) noexcept
: mode(other.mode),
pass(std::move(other.pass)),
salt(std::move(other.salt)),
N(other.N),
r(other.r),
p(other.p),
maxmem(other.maxmem),
length(other.length) {}
: pass(std::move(other.pass)),
salt(std::move(other.salt)),
N(other.N),
r(other.r),
p(other.p),
maxmem(other.maxmem),
length(other.length) {}
ScryptConfig& ScryptConfig::operator=(ScryptConfig&& other) noexcept {
if (&other == this) return *this;
@@ -38,10 +37,8 @@ ScryptConfig& ScryptConfig::operator=(ScryptConfig&& other) noexcept {
}
void ScryptConfig::MemoryInfo(MemoryTracker* tracker) const {
if (IsCryptoJobAsync(mode)) {
tracker->TrackFieldWithSize("pass", pass.size());
tracker->TrackFieldWithSize("salt", salt.size());
}
tracker->TraitTrackInline(pass, "pass");
tracker->TraitTrackInline(salt, "salt");
}
MaybeLocal<Value> ScryptTraits::EncodeOutput(Environment* env,
@@ -57,8 +54,6 @@ Maybe<void> ScryptTraits::AdditionalConfig(
ScryptConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->mode = mode;
ArrayBufferOrViewContents<char> pass(args[offset]);
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
@@ -80,7 +75,7 @@ Maybe<void> ScryptTraits::AdditionalConfig(
CHECK(args[offset + 3]->IsUint32()); // r
CHECK(args[offset + 4]->IsUint32()); // p
CHECK(args[offset + 5]->IsNumber()); // maxmem
CHECK(args[offset + 6]->IsInt32()); // length
CHECK(args[offset + 6]->IsInt32()); // length
params->N = args[offset + 2].As<Uint32>()->Value();
params->r = args[offset + 3].As<Uint32>()->Value();
-1
View File
@@ -25,7 +25,6 @@ namespace crypto {
// at least 16 bytes in length.
struct ScryptConfig final : public MemoryRetainer {
CryptoJobMode mode;
ByteSource pass;
ByteSource salt;
uint32_t N;
+4 -9
View File
@@ -684,8 +684,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
}
SignConfiguration::SignConfiguration(SignConfiguration&& other) noexcept
: job_mode(other.job_mode),
mode(other.mode),
: mode(other.mode),
key(std::move(other.key)),
data(std::move(other.data)),
signature(std::move(other.signature)),
@@ -705,11 +704,9 @@ SignConfiguration& SignConfiguration::operator=(
void SignConfiguration::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("key", key);
if (IsCryptoJobAsync(job_mode)) {
tracker->TrackFieldWithSize("data", data.size());
tracker->TrackFieldWithSize("signature", signature.size());
tracker->TrackFieldWithSize("context_string", context_string.size());
}
tracker->TraitTrackInline(data, "data");
tracker->TraitTrackInline(signature, "signature");
tracker->TraitTrackInline(context_string, "context_string");
}
Maybe<void> SignTraits::AdditionalConfig(
@@ -720,8 +717,6 @@ Maybe<void> SignTraits::AdditionalConfig(
ClearErrorOnReturn clear_error_on_return;
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
CHECK(args[offset]->IsUint32()); // Sign Mode
params->mode =
-1
View File
@@ -104,7 +104,6 @@ struct SignConfiguration final : public MemoryRetainer {
kHasContextString = 4
};
CryptoJobMode job_mode;
Mode mode;
KeyObjectData key;
ByteSource data;
+5 -17
View File
@@ -405,8 +405,7 @@ void KT256(const uint8_t* message,
// ============================================================================
TurboShakeConfig::TurboShakeConfig(TurboShakeConfig&& other) noexcept
: job_mode(other.job_mode),
variant(other.variant),
: variant(other.variant),
output_length(other.output_length),
domain_separation(other.domain_separation),
data(std::move(other.data)) {}
@@ -419,10 +418,7 @@ TurboShakeConfig& TurboShakeConfig::operator=(
}
void TurboShakeConfig::MemoryInfo(MemoryTracker* tracker) const {
if (IsCryptoJobAsync(job_mode)) {
// TODO(addaleax): Implement MemoryRetainer protocol for ByteSource
tracker->TrackFieldWithSize("data", data.size());
}
tracker->TraitTrackInline(data, "data");
}
Maybe<void> TurboShakeTraits::AdditionalConfig(
@@ -432,8 +428,6 @@ Maybe<void> TurboShakeTraits::AdditionalConfig(
TurboShakeConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
// args[offset + 0] = algorithm name (string)
CHECK(args[offset]->IsString());
Utf8Value algorithm_name(env->isolate(), args[offset]);
@@ -513,8 +507,7 @@ MaybeLocal<Value> TurboShakeTraits::EncodeOutput(Environment* env,
KangarooTwelveConfig::KangarooTwelveConfig(
KangarooTwelveConfig&& other) noexcept
: job_mode(other.job_mode),
variant(other.variant),
: variant(other.variant),
output_length(other.output_length),
data(std::move(other.data)),
customization(std::move(other.customization)) {}
@@ -527,11 +520,8 @@ KangarooTwelveConfig& KangarooTwelveConfig::operator=(
}
void KangarooTwelveConfig::MemoryInfo(MemoryTracker* tracker) const {
if (IsCryptoJobAsync(job_mode)) {
// TODO(addaleax): Implement MemoryRetainer protocol for ByteSource
tracker->TrackFieldWithSize("data", data.size());
tracker->TrackFieldWithSize("customization", customization.size());
}
tracker->TraitTrackInline(data, "data");
tracker->TraitTrackInline(customization, "customization");
}
Maybe<void> KangarooTwelveTraits::AdditionalConfig(
@@ -541,8 +531,6 @@ Maybe<void> KangarooTwelveTraits::AdditionalConfig(
KangarooTwelveConfig* params) {
Environment* env = Environment::GetCurrent(args);
params->job_mode = mode;
// args[offset + 0] = algorithm name (string)
CHECK(args[offset]->IsString());
Utf8Value algorithm_name(env->isolate(), args[offset]);
-2
View File
@@ -10,7 +10,6 @@ namespace node::crypto {
enum class TurboShakeVariant { TurboSHAKE128, TurboSHAKE256 };
struct TurboShakeConfig final : public MemoryRetainer {
CryptoJobMode job_mode;
TurboShakeVariant variant;
uint32_t output_length; // Output length in bytes
uint8_t domain_separation; // Domain separation byte (0x01–0x7F)
@@ -55,7 +54,6 @@ using TurboShakeJob = DeriveBitsJob<TurboShakeTraits>;
enum class KangarooTwelveVariant { KT128, KT256 };
struct KangarooTwelveConfig final : public MemoryRetainer {
CryptoJobMode job_mode;
KangarooTwelveVariant variant;
uint32_t output_length; // Output length in bytes
ByteSource data;
+18
View File
@@ -56,6 +56,24 @@ using v8::Uint32;
using v8::Uint8Array;
using v8::Value;
void MemoryRetainerTraits<crypto::ByteSource>::MemoryInfo(
MemoryTracker* tracker, const crypto::ByteSource& value) {
// Foreign ByteSources do not own the memory that they reference.
if (value.allocated_data_ != nullptr) {
tracker->TrackFieldWithSize("data", value.size_);
}
}
const char* MemoryRetainerTraits<crypto::ByteSource>::MemoryInfoName(
const crypto::ByteSource& value) {
return "ByteSource";
}
size_t MemoryRetainerTraits<crypto::ByteSource>::SelfSize(
const crypto::ByteSource& value) {
return sizeof(value);
}
namespace crypto {
int PasswordCallback(char* buf, int size, int rwflag, void* u) {
+16 -1
View File
@@ -26,6 +26,20 @@
#define NODE_OPENSSL_HAS_CERT_COMP 1
#endif
namespace node {
namespace crypto {
class ByteSource;
}
template <>
struct MemoryRetainerTraits<crypto::ByteSource> {
static void MemoryInfo(MemoryTracker* tracker,
const crypto::ByteSource& value);
static const char* MemoryInfoName(const crypto::ByteSource& value);
static size_t SelfSize(const crypto::ByteSource& value);
};
} // namespace node
namespace node::crypto {
// Currently known sizes of commonly used OpenSSL struct sizes.
// OpenSSL considers it's various structs to be opaque and the
@@ -250,6 +264,7 @@ class ByteSource final {
Environment* env, v8::Local<v8::Value> value);
private:
friend struct node::MemoryRetainerTraits<ByteSource>;
friend void TruncateToBitLength(size_t length_bits, ByteSource* bytes);
const void* data_ = nullptr;
@@ -610,7 +625,7 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
SET_SELF_SIZE(DeriveBitsJob)
void MemoryInfo(MemoryTracker* tracker) const override {
tracker->TrackFieldWithSize("out", out_.size());
tracker->TraitTrackInline(out_, "out");
CryptoJob<DeriveBitsTraits>::MemoryInfo(tracker);
}