11
11
12
12
namespace node {
13
13
14
+ using v8::ArrayBuffer;
15
+ using v8::BackingStore;
14
16
using v8::ConstructorBehavior;
15
17
using v8::DontDelete;
16
18
using v8::FunctionCallback;
@@ -48,10 +50,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size,
48
50
memset (data, 0 , padding);
49
51
}
50
52
}
51
- static void ZeroPadDiffieHellmanSecret (size_t remainder_size,
52
- AllocatedBuffer* ret) {
53
- ZeroPadDiffieHellmanSecret (remainder_size, ret->data (), ret->size ());
54
- }
55
53
} // namespace
56
54
57
55
DiffieHellman::DiffieHellman (Environment* env, Local<Object> wrap)
@@ -273,13 +271,24 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
273
271
274
272
const BIGNUM* pub_key;
275
273
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);
283
292
}
284
293
285
294
@@ -295,13 +304,23 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
295
304
if (num == nullptr )
296
305
return THROW_ERR_CRYPTO_INVALID_STATE (env, err_if_null);
297
306
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);
305
324
}
306
325
307
326
void DiffieHellman::GetPrime (const FunctionCallbackInfo<Value>& args) {
@@ -350,10 +369,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
350
369
return THROW_ERR_OUT_OF_RANGE (env, " secret is too big" );
351
370
BignumPointer key (BN_bin2bn (key_buf.data (), key_buf.size (), nullptr ));
352
371
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
+ }
355
378
356
- int size = DH_compute_key (reinterpret_cast <unsigned char *>(ret. data ()),
379
+ int size = DH_compute_key (static_cast <unsigned char *>(bs-> Data ()),
357
380
key.get (),
358
381
diffieHellman->dh_ .get ());
359
382
@@ -381,9 +404,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
381
404
}
382
405
383
406
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);
387
415
}
388
416
389
417
void DiffieHellman::SetKey (const FunctionCallbackInfo<Value>& args,
0 commit comments