From 494650c09f77c4f8bed6270cdec37601f46fc7c0 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 24 Apr 2022 20:33:03 +0530 Subject: [PATCH] src,crypto: remove uses of AllocatedBuffer from crypto_ec.cc Refs: https://github.com/nodejs/node/pull/39941 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42766 Reviewed-By: James M Snell --- src/crypto/crypto_ec.cc | 42 +++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 2d738bb9b62e11..e05b5388f0450a 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -20,6 +20,8 @@ namespace node { using v8::Array; +using v8::ArrayBuffer; +using v8::BackingStore; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Int32; @@ -220,17 +222,23 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo& args) { return; } - // NOTE: field_size is in bits - int field_size = EC_GROUP_get_degree(ecdh->group_); - size_t out_len = (field_size + 7) / 8; - AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, out_len); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + // NOTE: field_size is in bits + int field_size = EC_GROUP_get_degree(ecdh->group_); + size_t out_len = (field_size + 7) / 8; + bs = ArrayBuffer::NewBackingStore(env->isolate(), out_len); + } - int r = ECDH_compute_key( - out.data(), out_len, pub.get(), ecdh->key_.get(), nullptr); - if (!r) + if (!ECDH_compute_key( + bs->Data(), bs->ByteLength(), pub.get(), ecdh->key_.get(), nullptr)) return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key"); - args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local())); + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void ECDH::GetPublicKey(const FunctionCallbackInfo& args) { @@ -270,13 +278,19 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo& args) { return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get ECDH private key"); - const int size = BN_num_bytes(b); - AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, size); - CHECK_EQ(size, BN_bn2binpad(b, - reinterpret_cast(out.data()), - size)); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), BN_num_bytes(b)); + } + CHECK_EQ(static_cast(bs->ByteLength()), + BN_bn2binpad( + b, static_cast(bs->Data()), bs->ByteLength())); - args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local())); + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void ECDH::SetPrivateKey(const FunctionCallbackInfo& args) {