13
13
14
14
namespace node {
15
15
16
+ using v8::ArrayBuffer;
17
+ using v8::BackingStore;
16
18
using v8::ConstructorBehavior;
17
19
using v8::DontDelete;
18
20
using v8::FunctionCallback;
@@ -50,10 +52,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
50
52
memset (data, 0 , padding);
51
53
}
52
54
}
53
- static void ZeroPadDiffieHellmanSecret (size_t remainder_size,
54
- AllocatedBuffer* ret) {
55
- ZeroPadDiffieHellmanSecret (remainder_size, ret->data (), ret->size ());
56
- }
57
55
} // namespace
58
56
59
57
DiffieHellman::DiffieHellman (Environment* env, Local<Object> wrap)
@@ -275,13 +273,24 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
275
273
276
274
const BIGNUM* pub_key;
277
275
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);
285
294
}
286
295
287
296
@@ -297,13 +306,23 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
297
306
if (num == nullptr )
298
307
return THROW_ERR_CRYPTO_INVALID_STATE (env, err_if_null);
299
308
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);
307
326
}
308
327
309
328
void DiffieHellman::GetPrime (const FunctionCallbackInfo<Value>& args) {
@@ -352,10 +371,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
352
371
return THROW_ERR_OUT_OF_RANGE (env, " secret is too big" );
353
372
BignumPointer key (BN_bin2bn (key_buf.data (), key_buf.size (), nullptr ));
354
373
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
+ }
357
380
358
- int size = DH_compute_key (reinterpret_cast <unsigned char *>(ret. data ()),
381
+ int size = DH_compute_key (static_cast <unsigned char *>(bs-> Data ()),
359
382
key.get (),
360
383
diffieHellman->dh_ .get ());
361
384
@@ -383,9 +406,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
383
406
}
384
407
385
408
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);
389
417
}
390
418
391
419
void DiffieHellman::SetKey (const FunctionCallbackInfo<Value>& args,
0 commit comments