Skip to content

Commit c299d2e

Browse files
panvaaduh95
authored andcommitted
src: implement MemoryRetainer protocol for ByteSource
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64660 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9077d72 commit c299d2e

32 files changed

Lines changed: 134 additions & 200 deletions

src/crypto/crypto_aes.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ void UseDefaultIV(AESCipherConfig* params) {
491491
} // namespace
492492

493493
AESCipherConfig::AESCipherConfig(AESCipherConfig&& other) noexcept
494-
: mode(other.mode),
495-
variant(other.variant),
494+
: variant(other.variant),
496495
cipher(other.cipher),
497496
length(other.length),
498497
iv(std::move(other.iv)),
@@ -505,12 +504,8 @@ AESCipherConfig& AESCipherConfig::operator=(AESCipherConfig&& other) noexcept {
505504
}
506505

507506
void AESCipherConfig::MemoryInfo(MemoryTracker* tracker) const {
508-
// If mode is sync, then the data in each of these properties
509-
// is not owned by the AESCipherConfig, so we ignore it.
510-
if (IsCryptoJobAsync(mode)) {
511-
tracker->TrackFieldWithSize("iv", iv.size());
512-
tracker->TrackFieldWithSize("additional_data", additional_data.size());
513-
}
507+
tracker->TraitTrackInline(iv, "iv");
508+
tracker->TraitTrackInline(additional_data, "additional_data");
514509
}
515510

516511
Maybe<void> AESCipherTraits::AdditionalConfig(
@@ -521,8 +516,6 @@ Maybe<void> AESCipherTraits::AdditionalConfig(
521516
AESCipherConfig* params) {
522517
Environment* env = Environment::GetCurrent(args);
523518

524-
params->mode = mode;
525-
526519
CHECK(args[offset]->IsUint32()); // Key Variant
527520
params->variant =
528521
static_cast<AESKeyVariant>(args[offset].As<Uint32>()->Value());

src/crypto/crypto_aes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ enum class AESKeyVariant {
5858
};
5959

6060
struct AESCipherConfig final : public MemoryRetainer {
61-
CryptoJobMode mode;
6261
AESKeyVariant variant;
6362
ncrypto::Cipher cipher;
6463
size_t length;

src/crypto/crypto_argon2.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ using v8::Uint32;
2121
using v8::Value;
2222

2323
Argon2Config::Argon2Config(Argon2Config&& other) noexcept
24-
: mode{other.mode},
25-
key{std::move(other.key)},
24+
: key{std::move(other.key)},
2625
pass{std::move(other.pass)},
2726
salt{std::move(other.salt)},
2827
secret{std::move(other.secret)},
@@ -40,13 +39,13 @@ Argon2Config& Argon2Config::operator=(Argon2Config&& other) noexcept {
4039
}
4140

4241
void Argon2Config::MemoryInfo(MemoryTracker* tracker) const {
43-
if (key) tracker->TrackField("key", key);
44-
if (IsCryptoJobAsync(mode)) {
45-
if (!key) tracker->TrackFieldWithSize("pass", pass.size());
46-
tracker->TrackFieldWithSize("salt", salt.size());
47-
tracker->TrackFieldWithSize("secret", secret.size());
48-
tracker->TrackFieldWithSize("ad", ad.size());
49-
}
42+
if (key)
43+
tracker->TrackField("key", key);
44+
else
45+
tracker->TraitTrackInline(pass, "pass");
46+
tracker->TraitTrackInline(salt, "salt");
47+
tracker->TraitTrackInline(secret, "secret");
48+
tracker->TraitTrackInline(ad, "ad");
5049
}
5150

5251
MaybeLocal<Value> Argon2Traits::EncodeOutput(Environment* env,
@@ -62,8 +61,6 @@ Maybe<void> Argon2Traits::AdditionalConfig(
6261
Argon2Config* config) {
6362
Environment* env = Environment::GetCurrent(args);
6463

65-
config->mode = mode;
66-
6764
CHECK(KeyObjectHandle::HasInstance(env, args[offset]) ||
6865
IsAnyBufferSource(args[offset])); // pass
6966
ArrayBufferOrViewContents<char> salt(args[offset + 1]);

src/crypto/crypto_argon2.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace node::crypto {
2222
// at least 16 bytes in length.
2323

2424
struct Argon2Config final : public MemoryRetainer {
25-
CryptoJobMode mode;
2625
KeyObjectData key;
2726
ByteSource pass;
2827
ByteSource salt;

src/crypto/crypto_chacha20_poly1305.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ bool ValidateAdditionalData(Environment* env,
8181

8282
ChaCha20Poly1305CipherConfig::ChaCha20Poly1305CipherConfig(
8383
ChaCha20Poly1305CipherConfig&& other) noexcept
84-
: mode(other.mode),
85-
cipher(other.cipher),
84+
: cipher(other.cipher),
8685
iv(std::move(other.iv)),
8786
additional_data(std::move(other.additional_data)) {}
8887

@@ -94,12 +93,8 @@ ChaCha20Poly1305CipherConfig& ChaCha20Poly1305CipherConfig::operator=(
9493
}
9594

9695
void ChaCha20Poly1305CipherConfig::MemoryInfo(MemoryTracker* tracker) const {
97-
// If mode is sync, then the data in each of these properties
98-
// is not owned by the ChaCha20Poly1305CipherConfig, so we ignore it.
99-
if (IsCryptoJobAsync(mode)) {
100-
tracker->TrackFieldWithSize("iv", iv.size());
101-
tracker->TrackFieldWithSize("additional_data", additional_data.size());
102-
}
96+
tracker->TraitTrackInline(iv, "iv");
97+
tracker->TraitTrackInline(additional_data, "additional_data");
10398
}
10499

105100
Maybe<void> ChaCha20Poly1305CipherTraits::AdditionalConfig(
@@ -110,7 +105,6 @@ Maybe<void> ChaCha20Poly1305CipherTraits::AdditionalConfig(
110105
ChaCha20Poly1305CipherConfig* params) {
111106
Environment* env = Environment::GetCurrent(args);
112107

113-
params->mode = mode;
114108
params->cipher = ncrypto::Cipher::CHACHA20_POLY1305;
115109

116110
#ifndef OPENSSL_IS_BORINGSSL

src/crypto/crypto_chacha20_poly1305.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace node::crypto {
1313
constexpr unsigned kChaCha20Poly1305AuthTagLength = 16;
1414

1515
struct ChaCha20Poly1305CipherConfig final : public MemoryRetainer {
16-
CryptoJobMode mode;
1716
ncrypto::Cipher cipher;
1817
ByteSource iv;
1918
ByteSource additional_data;

src/crypto/crypto_cipher.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
268268

269269
SET_SELF_SIZE(CipherJob)
270270
void MemoryInfo(MemoryTracker* tracker) const override {
271-
if (IsCryptoJobAsync(CryptoJob<CipherTraits>::mode()))
272-
tracker->TrackFieldWithSize("in", in_.size());
273-
tracker->TrackFieldWithSize("out", out_.size());
271+
tracker->TraitTrackInline(in_, "in");
272+
tracker->TraitTrackInline(out_, "out");
274273
CryptoJob<CipherTraits>::MemoryInfo(tracker);
275274
}
276275

src/crypto/crypto_hash.cc

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
5858

5959
void Hash::MemoryInfo(MemoryTracker* tracker) const {
6060
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0);
61-
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0);
61+
tracker->TraitTrackInline(digest_, "md");
6262
}
6363

6464
#if NCRYPTO_USE_BORINGSSL_EVP_DO_ALL_FALLBACK
@@ -528,10 +528,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
528528
}
529529

530530
HashConfig::HashConfig(HashConfig&& other) noexcept
531-
: mode(other.mode),
532-
in(std::move(other.in)),
533-
digest(other.digest),
534-
length(other.length) {}
531+
: in(std::move(other.in)), digest(other.digest), length(other.length) {}
535532

536533
HashConfig& HashConfig::operator=(HashConfig&& other) noexcept {
537534
if (&other == this) return *this;
@@ -540,8 +537,7 @@ HashConfig& HashConfig::operator=(HashConfig&& other) noexcept {
540537
}
541538

542539
void HashConfig::MemoryInfo(MemoryTracker* tracker) const {
543-
// If the Job is sync, then the HashConfig does not own the data.
544-
if (IsCryptoJobAsync(mode)) tracker->TrackFieldWithSize("in", in.size());
540+
tracker->TraitTrackInline(in, "in");
545541
}
546542

547543
MaybeLocal<Value> HashTraits::EncodeOutput(Environment* env,
@@ -557,8 +553,6 @@ Maybe<void> HashTraits::AdditionalConfig(
557553
HashConfig* params) {
558554
Environment* env = Environment::GetCurrent(args);
559555

560-
params->mode = mode;
561-
562556
CHECK(args[offset]->IsString()); // Hash algorithm
563557
Utf8Value digest(env->isolate(), args[offset]);
564558
params->digest = ncrypto::getDigestByName(*digest);
@@ -787,8 +781,7 @@ bool DigestUpdateBytepad(ncrypto::EVPMDCtxPointer* ctx,
787781
} // namespace
788782

789783
CShakeConfig::CShakeConfig(CShakeConfig&& other) noexcept
790-
: mode(other.mode),
791-
in(std::move(other.in)),
784+
: in(std::move(other.in)),
792785
function_name(std::move(other.function_name)),
793786
customization(std::move(other.customization)),
794787
variant(other.variant),
@@ -801,12 +794,9 @@ CShakeConfig& CShakeConfig::operator=(CShakeConfig&& other) noexcept {
801794
}
802795

803796
void CShakeConfig::MemoryInfo(MemoryTracker* tracker) const {
804-
// If the Job is sync, then the CShakeConfig does not own the data.
805-
if (IsCryptoJobAsync(mode)) {
806-
tracker->TrackFieldWithSize("in", in.size());
807-
tracker->TrackFieldWithSize("function_name", function_name.size());
808-
tracker->TrackFieldWithSize("customization", customization.size());
809-
}
797+
tracker->TraitTrackInline(in, "in");
798+
tracker->TraitTrackInline(function_name, "function_name");
799+
tracker->TraitTrackInline(customization, "customization");
810800
}
811801

812802
MaybeLocal<Value> CShakeTraits::EncodeOutput(Environment* env,
@@ -822,8 +812,6 @@ Maybe<void> CShakeTraits::AdditionalConfig(
822812
CShakeConfig* params) {
823813
Environment* env = Environment::GetCurrent(args);
824814

825-
params->mode = mode;
826-
827815
CHECK(args[offset]->IsString()); // Algorithm name
828816
Utf8Value algorithm_name(env->isolate(), args[offset]);
829817
std::string_view algorithm_str = algorithm_name.ToStringView();

src/crypto/crypto_hash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Hash final : public BaseObject {
4242
};
4343

4444
struct HashConfig final : public MemoryRetainer {
45-
CryptoJobMode mode;
4645
ByteSource in;
4746
const EVP_MD* digest;
4847
unsigned int length;
@@ -108,7 +107,6 @@ struct CShakeParams final {
108107
bool DeriveCShakeBits(const CShakeParams& params, ByteSource* out);
109108

110109
struct CShakeConfig final : public MemoryRetainer {
111-
CryptoJobMode mode;
112110
ByteSource in;
113111
ByteSource function_name;
114112
ByteSource customization;

src/crypto/crypto_hkdf.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ using v8::Value;
2020

2121
namespace crypto {
2222
HKDFConfig::HKDFConfig(HKDFConfig&& other) noexcept
23-
: mode(other.mode),
24-
length(other.length),
23+
: length(other.length),
2524
digest(other.digest),
2625
key(std::move(other.key)),
2726
key_data(std::move(other.key_data)),
@@ -47,8 +46,6 @@ Maybe<void> HKDFTraits::AdditionalConfig(
4746
HKDFConfig* params) {
4847
Environment* env = Environment::GetCurrent(args);
4948

50-
params->mode = mode;
51-
5249
CHECK(args[offset]->IsString()); // Hash
5350
CHECK(KeyObjectHandle::HasInstance(env, args[offset + 1]) ||
5451
IsAnyBufferSource(args[offset + 1])); // Key
@@ -140,13 +137,12 @@ bool HKDFTraits::DeriveBits(Environment* env,
140137
}
141138

142139
void HKDFConfig::MemoryInfo(MemoryTracker* tracker) const {
143-
if (key) tracker->TrackField("key", key);
144-
// If the job is sync, then the HKDFConfig does not own the data
145-
if (IsCryptoJobAsync(mode)) {
146-
if (!key) tracker->TrackFieldWithSize("key", key_data.size());
147-
tracker->TrackFieldWithSize("salt", salt.size());
148-
tracker->TrackFieldWithSize("info", info.size());
149-
}
140+
if (key)
141+
tracker->TrackField("key", key);
142+
else
143+
tracker->TraitTrackInline(key_data, "key");
144+
tracker->TraitTrackInline(salt, "salt");
145+
tracker->TraitTrackInline(info, "info");
150146
}
151147

152148
} // namespace crypto

0 commit comments

Comments
 (0)