Skip to content

Commit

Permalink
src,crypto: remove uses of AllocatedBuffer from crypto_dh.cc
Browse files Browse the repository at this point in the history
Refs: #39941
Signed-off-by: Darshan Sen <[email protected]>

PR-URL: #42492
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
RaisinTen authored and sbaayel committed Sep 27, 2023
1 parent c5885b5 commit c19c5a6
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace node {

using v8::ArrayBuffer;
using v8::BackingStore;
using v8::ConstructorBehavior;
using v8::DontDelete;
using v8::FunctionCallback;
Expand Down Expand Up @@ -50,10 +52,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
memset(data, 0, padding);
}
}
static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
AllocatedBuffer* ret) {
ZeroPadDiffieHellmanSecret(remainder_size, ret->data(), ret->size());
}
} // namespace

DiffieHellman::DiffieHellman(Environment* env, Local<Object> wrap)
Expand Down Expand Up @@ -275,13 +273,24 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {

const BIGNUM* pub_key;
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
const int size = BN_num_bytes(pub_key);
CHECK_GE(size, 0);
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size);
CHECK_EQ(size,
BN_bn2binpad(
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));

std::unique_ptr<BackingStore> bs;
{
const int size = BN_num_bytes(pub_key);
CHECK_GE(size, 0);
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
}

CHECK_EQ(static_cast<int>(bs->ByteLength()),
BN_bn2binpad(pub_key,
static_cast<unsigned char*>(bs->Data()),
bs->ByteLength()));

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
Local<Value> buffer;
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
args.GetReturnValue().Set(buffer);
}


Expand All @@ -297,13 +306,23 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
if (num == nullptr)
return THROW_ERR_CRYPTO_INVALID_STATE(env, err_if_null);

const int size = BN_num_bytes(num);
CHECK_GE(size, 0);
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size);
CHECK_EQ(
size,
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
std::unique_ptr<BackingStore> bs;
{
const int size = BN_num_bytes(num);
CHECK_GE(size, 0);
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
}

CHECK_EQ(static_cast<int>(bs->ByteLength()),
BN_bn2binpad(num,
static_cast<unsigned char*>(bs->Data()),
bs->ByteLength()));

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
Local<Value> buffer;
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
args.GetReturnValue().Set(buffer);
}

void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -352,10 +371,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
return THROW_ERR_OUT_OF_RANGE(env, "secret is too big");
BignumPointer key(BN_bin2bn(key_buf.data(), key_buf.size(), nullptr));

AllocatedBuffer ret =
AllocatedBuffer::AllocateManaged(env, DH_size(diffieHellman->dh_.get()));
std::unique_ptr<BackingStore> bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(),
DH_size(diffieHellman->dh_.get()));
}

int size = DH_compute_key(reinterpret_cast<unsigned char*>(ret.data()),
int size = DH_compute_key(static_cast<unsigned char*>(bs->Data()),
key.get(),
diffieHellman->dh_.get());

Expand Down Expand Up @@ -383,9 +406,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
}

CHECK_GE(size, 0);
ZeroPadDiffieHellmanSecret(static_cast<size_t>(size), &ret);

args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>()));
ZeroPadDiffieHellmanSecret(size,
static_cast<char*>(bs->Data()),
bs->ByteLength());

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
Local<Value> buffer;
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
args.GetReturnValue().Set(buffer);
}

void DiffieHellman::SetKey(const FunctionCallbackInfo<Value>& args,
Expand Down

0 comments on commit c19c5a6

Please sign in to comment.