Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! src: remove usages of GetBackingStore in crypto
  • Loading branch information
kvakil committed Aug 1, 2022
commit bba5c5ec5c0bd0f264b2befaef819c25d4dd249f
15 changes: 6 additions & 9 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
if (UNLIKELY(key_buf.size() > INT_MAX))
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");

ArrayBufferOrViewContents<unsigned char> iv_buf;
if (!args[2]->IsNull())
iv_buf = ArrayBufferOrViewContents<unsigned char>(args[2]);
ArrayBufferOrViewContents<unsigned char> iv_buf(
!args[2]->IsNull() ? args[2] : Local<Value>());

if (UNLIKELY(!iv_buf.CheckSizeInt32()))
return THROW_ERR_OUT_OF_RANGE(env, "iv is too big");
Expand Down Expand Up @@ -1048,12 +1047,10 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
return THROW_ERR_OSSL_EVP_INVALID_DIGEST(env);
}

ArrayBufferOrViewContents<unsigned char> oaep_label;
if (!args[offset + 3]->IsUndefined()) {
oaep_label = ArrayBufferOrViewContents<unsigned char>(args[offset + 3]);
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
return THROW_ERR_OUT_OF_RANGE(env, "oaep_label is too big");
}
ArrayBufferOrViewContents<unsigned char> oaep_label(
!args[offset + 3]->IsUndefined() ? args[offset + 3] : Local<Value>());
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
return THROW_ERR_OUT_OF_RANGE(env, "oaep_label is too big");

std::unique_ptr<BackingStore> out;
if (!Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
Expand Down
15 changes: 14 additions & 1 deletion src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,14 @@ template <typename T>
class ArrayBufferOrViewContents {
public:
ArrayBufferOrViewContents() = default;
ArrayBufferOrViewContents(const ArrayBufferOrViewContents&) = delete;
void operator=(const ArrayBufferOrViewContents&) = delete;

inline explicit ArrayBufferOrViewContents(v8::Local<v8::Value> buf) {
if (buf.IsEmpty()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why this exists, but it feels somewhat strange to explicitly allow passing in empty Locals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. An alternative would be to allocate an empty ArrayBuffer and pass it into this function. None of the current callsites looked particularly hot, but I wasn't sure.

Do you think that would be acceptable wrt to performance?

(P.S. if your concern here is blocking, feel free to request changes on the PR.)

return;
}

CHECK(IsAnyByteSource(buf));
if (buf->IsArrayBufferView()) {
auto view = buf.As<v8::ArrayBufferView>();
Expand Down Expand Up @@ -772,7 +778,14 @@ class ArrayBufferOrViewContents {
T buf = 0;
size_t offset_ = 0;
size_t length_ = 0;
void* data_ = 0;
void* data_ = nullptr;

// Declaring operator new and delete as deleted is not spec compliant.
// Therefore declare them private instead to disable dynamic alloc
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void*);
void operator delete[](void*);
};

v8::MaybeLocal<v8::Value> EncodeBignum(
Expand Down