1111
1212namespace node {
1313
14+ using v8::ArrayBuffer;
15+ using v8::BackingStore;
1416using v8::ConstructorBehavior;
1517using v8::DontDelete;
1618using v8::FunctionCallback;
@@ -48,10 +50,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
4850 memset (data, 0 , padding);
4951 }
5052}
51- static void ZeroPadDiffieHellmanSecret (size_t remainder_size,
52- AllocatedBuffer* ret) {
53- ZeroPadDiffieHellmanSecret (remainder_size, ret->data (), ret->size ());
54- }
5553} // namespace
5654
5755DiffieHellman::DiffieHellman (Environment* env, Local<Object> wrap)
@@ -273,13 +271,24 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
273271
274272 const BIGNUM* pub_key;
275273 DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
276- const int size = BN_num_bytes (pub_key);
277- CHECK_GE (size, 0 );
278- AllocatedBuffer data = AllocatedBuffer::AllocateManaged (env, size);
279- CHECK_EQ (size,
280- BN_bn2binpad (
281- pub_key, reinterpret_cast <unsigned char *>(data.data ()), size));
282- args.GetReturnValue ().Set (data.ToBuffer ().FromMaybe (Local<Value>()));
274+
275+ std::unique_ptr<BackingStore> bs;
276+ {
277+ const int size = BN_num_bytes (pub_key);
278+ CHECK_GE (size, 0 );
279+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
280+ bs = ArrayBuffer::NewBackingStore (env->isolate (), size);
281+ }
282+
283+ CHECK_EQ (static_cast <int >(bs->ByteLength ()),
284+ BN_bn2binpad (pub_key,
285+ static_cast <unsigned char *>(bs->Data ()),
286+ bs->ByteLength ()));
287+
288+ Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
289+ Local<Value> buffer;
290+ if (!Buffer::New (env, ab, 0 , ab->ByteLength ()).ToLocal (&buffer)) return ;
291+ args.GetReturnValue ().Set (buffer);
283292}
284293
285294
@@ -295,13 +304,23 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
295304 if (num == nullptr )
296305 return THROW_ERR_CRYPTO_INVALID_STATE (env, err_if_null);
297306
298- const int size = BN_num_bytes (num);
299- CHECK_GE (size, 0 );
300- AllocatedBuffer data = AllocatedBuffer::AllocateManaged (env, size);
301- CHECK_EQ (
302- size,
303- BN_bn2binpad (num, reinterpret_cast <unsigned char *>(data.data ()), size));
304- args.GetReturnValue ().Set (data.ToBuffer ().FromMaybe (Local<Value>()));
307+ std::unique_ptr<BackingStore> bs;
308+ {
309+ const int size = BN_num_bytes (num);
310+ CHECK_GE (size, 0 );
311+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
312+ bs = ArrayBuffer::NewBackingStore (env->isolate (), size);
313+ }
314+
315+ CHECK_EQ (static_cast <int >(bs->ByteLength ()),
316+ BN_bn2binpad (num,
317+ static_cast <unsigned char *>(bs->Data ()),
318+ bs->ByteLength ()));
319+
320+ Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
321+ Local<Value> buffer;
322+ if (!Buffer::New (env, ab, 0 , ab->ByteLength ()).ToLocal (&buffer)) return ;
323+ args.GetReturnValue ().Set (buffer);
305324}
306325
307326void DiffieHellman::GetPrime (const FunctionCallbackInfo<Value>& args) {
@@ -350,10 +369,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
350369 return THROW_ERR_OUT_OF_RANGE (env, " secret is too big" );
351370 BignumPointer key (BN_bin2bn (key_buf.data (), key_buf.size (), nullptr ));
352371
353- AllocatedBuffer ret =
354- AllocatedBuffer::AllocateManaged (env, DH_size (diffieHellman->dh_ .get ()));
372+ std::unique_ptr<BackingStore> bs;
373+ {
374+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
375+ bs = ArrayBuffer::NewBackingStore (env->isolate (),
376+ DH_size (diffieHellman->dh_ .get ()));
377+ }
355378
356- int size = DH_compute_key (reinterpret_cast <unsigned char *>(ret. data ()),
379+ int size = DH_compute_key (static_cast <unsigned char *>(bs-> Data ()),
357380 key.get (),
358381 diffieHellman->dh_ .get ());
359382
@@ -381,9 +404,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
381404 }
382405
383406 CHECK_GE (size, 0 );
384- ZeroPadDiffieHellmanSecret (static_cast <size_t >(size), &ret);
385-
386- args.GetReturnValue ().Set (ret.ToBuffer ().FromMaybe (Local<Value>()));
407+ ZeroPadDiffieHellmanSecret (size,
408+ static_cast <char *>(bs->Data ()),
409+ bs->ByteLength ());
410+
411+ Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
412+ Local<Value> buffer;
413+ if (!Buffer::New (env, ab, 0 , ab->ByteLength ()).ToLocal (&buffer)) return ;
414+ args.GetReturnValue ().Set (buffer);
387415}
388416
389417void DiffieHellman::SetKey (const FunctionCallbackInfo<Value>& args,
0 commit comments