Skip to content

Commit 91e804f

Browse files
RaisinTenjuanarbol
authored andcommitted
src,crypto: remove uses of AllocatedBuffer from crypto_ec.cc
Refs: #39941 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #42766 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c3fc44b commit 91e804f

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/crypto/crypto_ec.cc

+28-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
namespace node {
2121

2222
using v8::Array;
23+
using v8::ArrayBuffer;
24+
using v8::BackingStore;
2325
using v8::FunctionCallbackInfo;
2426
using v8::FunctionTemplate;
2527
using v8::Int32;
@@ -220,17 +222,23 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
220222
return;
221223
}
222224

223-
// NOTE: field_size is in bits
224-
int field_size = EC_GROUP_get_degree(ecdh->group_);
225-
size_t out_len = (field_size + 7) / 8;
226-
AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, out_len);
225+
std::unique_ptr<BackingStore> bs;
226+
{
227+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
228+
// NOTE: field_size is in bits
229+
int field_size = EC_GROUP_get_degree(ecdh->group_);
230+
size_t out_len = (field_size + 7) / 8;
231+
bs = ArrayBuffer::NewBackingStore(env->isolate(), out_len);
232+
}
227233

228-
int r = ECDH_compute_key(
229-
out.data(), out_len, pub.get(), ecdh->key_.get(), nullptr);
230-
if (!r)
234+
if (!ECDH_compute_key(
235+
bs->Data(), bs->ByteLength(), pub.get(), ecdh->key_.get(), nullptr))
231236
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key");
232237

233-
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
238+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
239+
Local<Value> buffer;
240+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
241+
args.GetReturnValue().Set(buffer);
234242
}
235243

236244
void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
@@ -270,13 +278,19 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
270278
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
271279
"Failed to get ECDH private key");
272280

273-
const int size = BN_num_bytes(b);
274-
AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, size);
275-
CHECK_EQ(size, BN_bn2binpad(b,
276-
reinterpret_cast<unsigned char*>(out.data()),
277-
size));
281+
std::unique_ptr<BackingStore> bs;
282+
{
283+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
284+
bs = ArrayBuffer::NewBackingStore(env->isolate(), BN_num_bytes(b));
285+
}
286+
CHECK_EQ(static_cast<int>(bs->ByteLength()),
287+
BN_bn2binpad(
288+
b, static_cast<unsigned char*>(bs->Data()), bs->ByteLength()));
278289

279-
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
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);
280294
}
281295

282296
void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)