Skip to content

Commit cf1543e

Browse files
RaisinTenjuanarbol
authored andcommitted
src,crypto: remove uses of AllocatedBuffer from crypto_dh.cc
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]>
1 parent 838b143 commit cf1543e

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

src/crypto/crypto_dh.cc

+52-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace node {
1515

16+
using v8::ArrayBuffer;
17+
using v8::BackingStore;
1618
using v8::ConstructorBehavior;
1719
using v8::DontDelete;
1820
using v8::FunctionCallback;
@@ -50,10 +52,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
5052
memset(data, 0, padding);
5153
}
5254
}
53-
static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
54-
AllocatedBuffer* ret) {
55-
ZeroPadDiffieHellmanSecret(remainder_size, ret->data(), ret->size());
56-
}
5755
} // namespace
5856

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

276274
const BIGNUM* pub_key;
277275
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
278-
const int size = BN_num_bytes(pub_key);
279-
CHECK_GE(size, 0);
280-
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size);
281-
CHECK_EQ(size,
282-
BN_bn2binpad(
283-
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
284-
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
276+
277+
std::unique_ptr<BackingStore> bs;
278+
{
279+
const int size = BN_num_bytes(pub_key);
280+
CHECK_GE(size, 0);
281+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
282+
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
283+
}
284+
285+
CHECK_EQ(static_cast<int>(bs->ByteLength()),
286+
BN_bn2binpad(pub_key,
287+
static_cast<unsigned char*>(bs->Data()),
288+
bs->ByteLength()));
289+
290+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
291+
Local<Value> buffer;
292+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
293+
args.GetReturnValue().Set(buffer);
285294
}
286295

287296

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

300-
const int size = BN_num_bytes(num);
301-
CHECK_GE(size, 0);
302-
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size);
303-
CHECK_EQ(
304-
size,
305-
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size));
306-
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
309+
std::unique_ptr<BackingStore> bs;
310+
{
311+
const int size = BN_num_bytes(num);
312+
CHECK_GE(size, 0);
313+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
314+
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
315+
}
316+
317+
CHECK_EQ(static_cast<int>(bs->ByteLength()),
318+
BN_bn2binpad(num,
319+
static_cast<unsigned char*>(bs->Data()),
320+
bs->ByteLength()));
321+
322+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
323+
Local<Value> buffer;
324+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
325+
args.GetReturnValue().Set(buffer);
307326
}
308327

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

355-
AllocatedBuffer ret =
356-
AllocatedBuffer::AllocateManaged(env, DH_size(diffieHellman->dh_.get()));
374+
std::unique_ptr<BackingStore> bs;
375+
{
376+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
377+
bs = ArrayBuffer::NewBackingStore(env->isolate(),
378+
DH_size(diffieHellman->dh_.get()));
379+
}
357380

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

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

385408
CHECK_GE(size, 0);
386-
ZeroPadDiffieHellmanSecret(static_cast<size_t>(size), &ret);
387-
388-
args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>()));
409+
ZeroPadDiffieHellmanSecret(size,
410+
static_cast<char*>(bs->Data()),
411+
bs->ByteLength());
412+
413+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
414+
Local<Value> buffer;
415+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
416+
args.GetReturnValue().Set(buffer);
389417
}
390418

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

0 commit comments

Comments
 (0)