diff options
author | Darshan Sen <raisinten@gmail.com> | 2022-04-24 18:03:03 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2022-04-28 07:57:23 +0300 |
commit | 494650c09f77c4f8bed6270cdec37601f46fc7c0 (patch) | |
tree | e63d842ec4dd08c30ffdb2de347f0b57766a9cbb /src | |
parent | 4050b0d64f2a5b92b54a2de346f301a60d8553ba (diff) |
src,crypto: remove uses of AllocatedBuffer from crypto_ec.cc
Refs: https://github.com/nodejs/node/pull/39941
Signed-off-by: Darshan Sen <raisinten@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/42766
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/crypto/crypto_ec.cc | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 2d738bb9b62..e05b5388f04 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<Value>& 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<BackingStore> 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<Value>())); + Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local<Value> buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) { @@ -270,13 +278,19 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& 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<unsigned char*>(out.data()), - size)); + std::unique_ptr<BackingStore> bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), BN_num_bytes(b)); + } + CHECK_EQ(static_cast<int>(bs->ByteLength()), + BN_bn2binpad( + b, static_cast<unsigned char*>(bs->Data()), bs->ByteLength())); - args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>())); + Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local<Value> buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) { |