Skip to content

Commit

Permalink
src,crypto: remove uses of AllocatedBuffer from crypto_ec.cc
Browse files Browse the repository at this point in the history
Refs: #39941
Signed-off-by: Darshan Sen <raisinten@gmail.com>

PR-URL: #42766
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
RaisinTen authored and targos committed Apr 28, 2022
1 parent 4050b0d commit 494650c
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Int32;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 494650c

Please sign in to comment.