From 9a5ab2b4c0a22f02ad4041459e43ea104eeb4c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 27 Aug 2023 10:18:54 +0000 Subject: [PATCH] src: rename IsAnyByteSource to IsAnyBufferSource The current name is somewhat confusing. There is an internal ByteSource class, which is entirely unrelated to what IsAnyByteSource() does, even though both exist in the crypto subsystem of the C++ code. ByteSource objects can also be constructed from strings, for example, for which IsAnyByteSource() returns false. Web IDL calls the types for which this function returns true BufferSource. This type is commonly used across Web Crypto, for example. Thus, rename the function to match the Web IDL naming. Because the function also appears to accept BufferSource objects backed by SharedArrayBuffer instances, the exact Web IDL name would be AllowSharedBufferSource, but that seems unnecessarily long, so I decided to stick with "any". --- src/crypto/crypto_aes.cc | 4 ++-- src/crypto/crypto_ec.cc | 6 +++--- src/crypto/crypto_hkdf.cc | 4 ++-- src/crypto/crypto_keys.cc | 6 +++--- src/crypto/crypto_random.cc | 2 +- src/crypto/crypto_rsa.cc | 2 +- src/crypto/crypto_timing.cc | 4 ++-- src/crypto/crypto_util.cc | 10 +++++----- src/crypto/crypto_util.h | 5 +++-- 9 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/crypto/crypto_aes.cc b/src/crypto/crypto_aes.cc index c1c5bf762a765f..de058d077d1f45 100644 --- a/src/crypto/crypto_aes.cc +++ b/src/crypto/crypto_aes.cc @@ -381,7 +381,7 @@ bool ValidateAuthTag( AESCipherConfig* params) { switch (cipher_mode) { case kWebCryptoCipherDecrypt: { - if (!IsAnyByteSource(value)) { + if (!IsAnyBufferSource(value)) { THROW_ERR_CRYPTO_INVALID_TAG_LENGTH(env); return false; } @@ -419,7 +419,7 @@ bool ValidateAdditionalData( Local value, AESCipherConfig* params) { // Additional Data - if (IsAnyByteSource(value)) { + if (IsAnyBufferSource(value)) { ArrayBufferOrViewContents additional(value); if (UNLIKELY(!additional.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "additionalData is too big"); diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index b3a73f5c9d10a6..860d5048db7611 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -193,7 +193,7 @@ ECPointPointer ECDH::BufferToPoint(Environment* env, void ECDH::ComputeSecret(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK(IsAnyByteSource(args[0])); + CHECK(IsAnyBufferSource(args[0])); ECDH* ecdh; ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); @@ -347,7 +347,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo& args) { ECDH* ecdh; ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); - CHECK(IsAnyByteSource(args[0])); + CHECK(IsAnyBufferSource(args[0])); MarkPopErrorOnReturn mark_pop_error_on_return; @@ -393,7 +393,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK_EQ(args.Length(), 3); - CHECK(IsAnyByteSource(args[0])); + CHECK(IsAnyBufferSource(args[0])); ArrayBufferOrViewContents args0(args[0]); if (UNLIKELY(!args0.CheckSizeInt32())) diff --git a/src/crypto/crypto_hkdf.cc b/src/crypto/crypto_hkdf.cc index 7663dd69374db7..0dd9b42473ca73 100644 --- a/src/crypto/crypto_hkdf.cc +++ b/src/crypto/crypto_hkdf.cc @@ -51,8 +51,8 @@ Maybe HKDFTraits::AdditionalConfig( CHECK(args[offset]->IsString()); // Hash CHECK(args[offset + 1]->IsObject()); // Key - CHECK(IsAnyByteSource(args[offset + 2])); // Salt - CHECK(IsAnyByteSource(args[offset + 3])); // Info + CHECK(IsAnyBufferSource(args[offset + 2])); // Salt + CHECK(IsAnyBufferSource(args[offset + 3])); // Info CHECK(args[offset + 4]->IsUint32()); // Length Utf8Value hash(env->isolate(), args[offset]); diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 1b8e9b25a6991b..8d2774ff61a64c 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -699,7 +699,7 @@ ManagedEVPPKey::GetPrivateKeyEncodingFromJs( (*offset)++; } - if (IsAnyByteSource(args[*offset])) { + if (IsAnyBufferSource(args[*offset])) { CHECK_IMPLIES(context != kKeyContextInput, result.cipher_ != nullptr); ArrayBufferOrViewContents passphrase(args[*offset]); if (UNLIKELY(!passphrase.CheckSizeInt32())) { @@ -730,7 +730,7 @@ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs( const FunctionCallbackInfo& args, unsigned int* offset, bool allow_key_object) { - if (args[*offset]->IsString() || IsAnyByteSource(args[*offset])) { + if (args[*offset]->IsString() || IsAnyBufferSource(args[*offset])) { Environment* env = Environment::GetCurrent(args); ByteSource key = ByteSource::FromStringOrBuffer(env, args[(*offset)++]); NonCopyableMaybe config = @@ -756,7 +756,7 @@ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs( ManagedEVPPKey ManagedEVPPKey::GetPublicOrPrivateKeyFromJs( const FunctionCallbackInfo& args, unsigned int* offset) { - if (IsAnyByteSource(args[*offset])) { + if (IsAnyBufferSource(args[*offset])) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents data(args[(*offset)++]); if (UNLIKELY(!data.CheckSizeInt32())) { diff --git a/src/crypto/crypto_random.cc b/src/crypto/crypto_random.cc index 9850104cd607f8..245f3529186964 100644 --- a/src/crypto/crypto_random.cc +++ b/src/crypto/crypto_random.cc @@ -39,7 +39,7 @@ Maybe RandomBytesTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int offset, RandomBytesConfig* params) { - CHECK(IsAnyByteSource(args[offset])); // Buffer to fill + CHECK(IsAnyBufferSource(args[offset])); // Buffer to fill CHECK(args[offset + 1]->IsUint32()); // Offset CHECK(args[offset + 2]->IsUint32()); // Size diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index 47a42246eddfc7..3f8499457cf107 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -321,7 +321,7 @@ Maybe RSACipherTraits::AdditionalConfig( return Nothing(); } - if (IsAnyByteSource(args[offset + 2])) { + if (IsAnyBufferSource(args[offset + 2])) { ArrayBufferOrViewContents label(args[offset + 2]); if (UNLIKELY(!label.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "label is too big"); diff --git a/src/crypto/crypto_timing.cc b/src/crypto/crypto_timing.cc index 8904f6b140dbb5..3d876fc4c3035f 100644 --- a/src/crypto/crypto_timing.cc +++ b/src/crypto/crypto_timing.cc @@ -21,13 +21,13 @@ void TimingSafeEqual(const FunctionCallbackInfo& args) { // to V8 inlining certain parts of the wrapper. Therefore, keep them in C++. // Refs: https://github.com/nodejs/node/issues/34073. Environment* env = Environment::GetCurrent(args); - if (!IsAnyByteSource(args[0])) { + if (!IsAnyBufferSource(args[0])) { THROW_ERR_INVALID_ARG_TYPE( env, "The \"buf1\" argument must be an instance of " "ArrayBuffer, Buffer, TypedArray, or DataView."); return; } - if (!IsAnyByteSource(args[1])) { + if (!IsAnyBufferSource(args[1])) { THROW_ERR_INVALID_ARG_TYPE( env, "The \"buf2\" argument must be an instance of " "ArrayBuffer, Buffer, TypedArray, or DataView."); diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc index 8cc62279fabbab..5734d8fdc5505e 100644 --- a/src/crypto/crypto_util.cc +++ b/src/crypto/crypto_util.cc @@ -402,8 +402,8 @@ ByteSource ByteSource::FromEncodedString(Environment* env, ByteSource ByteSource::FromStringOrBuffer(Environment* env, Local value) { - return IsAnyByteSource(value) ? FromBuffer(value) - : FromString(env, value.As()); + return IsAnyBufferSource(value) ? FromBuffer(value) + : FromString(env, value.As()); } ByteSource ByteSource::FromString(Environment* env, Local str, @@ -429,9 +429,9 @@ ByteSource ByteSource::FromSecretKeyBytes( // A key can be passed as a string, buffer or KeyObject with type 'secret'. // If it is a string, we need to convert it to a buffer. We are not doing that // in JS to avoid creating an unprotected copy on the heap. - return value->IsString() || IsAnyByteSource(value) ? - ByteSource::FromStringOrBuffer(env, value) : - ByteSource::FromSymmetricKeyObjectHandle(value); + return value->IsString() || IsAnyBufferSource(value) + ? ByteSource::FromStringOrBuffer(env, value) + : ByteSource::FromSymmetricKeyObjectHandle(value); } ByteSource ByteSource::NullTerminatedCopy(Environment* env, diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index bf19334cf61fa4..1ce5f35a70a7c8 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -676,7 +676,8 @@ void array_push_back(const TypeName* evp_ref, } #endif -inline bool IsAnyByteSource(v8::Local arg) { +// WebIDL AllowSharedBufferSource. +inline bool IsAnyBufferSource(v8::Local arg) { return arg->IsArrayBufferView() || arg->IsArrayBuffer() || arg->IsSharedArrayBuffer(); @@ -694,7 +695,7 @@ class ArrayBufferOrViewContents { return; } - CHECK(IsAnyByteSource(buf)); + CHECK(IsAnyBufferSource(buf)); if (buf->IsArrayBufferView()) { auto view = buf.As(); offset_ = view->ByteOffset();