Skip to content

Commit bddf8c2

Browse files
RaisinTentargos
authored andcommitted
src,crypto: eliminate code duplication between StatelessDiffieHellman*
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40084 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 6bf6790 commit bddf8c2

File tree

1 file changed

+14
-38
lines changed

1 file changed

+14
-38
lines changed

src/crypto/crypto_dh.cc

+14-38
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace node {
1313

14+
using v8::ArrayBuffer;
1415
using v8::ConstructorBehavior;
1516
using v8::DontDelete;
1617
using v8::FunctionCallback;
@@ -28,6 +29,7 @@ using v8::ReadOnly;
2829
using v8::SideEffectType;
2930
using v8::Signature;
3031
using v8::String;
32+
using v8::Uint8Array;
3133
using v8::Value;
3234

3335
namespace crypto {
@@ -539,41 +541,9 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
539541
}
540542

541543
namespace {
542-
AllocatedBuffer StatelessDiffieHellman(
543-
Environment* env,
544-
ManagedEVPPKey our_key,
545-
ManagedEVPPKey their_key) {
546-
size_t out_size;
547-
548-
EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(our_key.get(), nullptr));
549-
if (!ctx ||
550-
EVP_PKEY_derive_init(ctx.get()) <= 0 ||
551-
EVP_PKEY_derive_set_peer(ctx.get(), their_key.get()) <= 0 ||
552-
EVP_PKEY_derive(ctx.get(), nullptr, &out_size) <= 0)
553-
return AllocatedBuffer();
554-
555-
AllocatedBuffer result = AllocatedBuffer::AllocateManaged(env, out_size);
556-
CHECK_NOT_NULL(result.data());
557-
558-
unsigned char* data = reinterpret_cast<unsigned char*>(result.data());
559-
if (EVP_PKEY_derive(ctx.get(), data, &out_size) <= 0)
560-
return AllocatedBuffer();
561-
562-
ZeroPadDiffieHellmanSecret(out_size, &result);
563-
return result;
564-
}
565-
566-
// The version of StatelessDiffieHellman that returns an AllocatedBuffer
567-
// is not threadsafe because of the AllocatedBuffer allocation of a
568-
// v8::BackingStore (it'll cause much crashing if we call it from a
569-
// libuv worker thread). This version allocates a ByteSource instead,
570-
// which we can convert into a v8::BackingStore later.
571-
// TODO(@jasnell): Eliminate the code duplication between these two
572-
// versions of the function.
573544
ByteSource StatelessDiffieHellmanThreadsafe(
574-
Environment* env,
575-
ManagedEVPPKey our_key,
576-
ManagedEVPPKey their_key) {
545+
const ManagedEVPPKey& our_key,
546+
const ManagedEVPPKey& their_key) {
577547
size_t out_size;
578548

579549
EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(our_key.get(), nullptr));
@@ -612,11 +582,18 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) {
612582
ManagedEVPPKey our_key = our_key_object->Data()->GetAsymmetricKey();
613583
ManagedEVPPKey their_key = their_key_object->Data()->GetAsymmetricKey();
614584

615-
AllocatedBuffer out = StatelessDiffieHellman(env, our_key, their_key);
616-
if (out.size() == 0)
585+
Local<Value> out;
586+
{
587+
Local<ArrayBuffer> ab = StatelessDiffieHellmanThreadsafe(our_key, their_key)
588+
.ToArrayBuffer(env);
589+
out = Buffer::New(env, ab, 0, ab->ByteLength())
590+
.FromMaybe(Local<Uint8Array>());
591+
}
592+
593+
if (Buffer::Length(out) == 0)
617594
return ThrowCryptoError(env, ERR_get_error(), "diffieHellman failed");
618595

619-
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
596+
args.GetReturnValue().Set(out);
620597
}
621598

622599
Maybe<bool> DHBitsTraits::AdditionalConfig(
@@ -661,7 +638,6 @@ bool DHBitsTraits::DeriveBits(
661638
const DHBitsConfig& params,
662639
ByteSource* out) {
663640
*out = StatelessDiffieHellmanThreadsafe(
664-
env,
665641
params.private_key->GetAsymmetricKey(),
666642
params.public_key->GetAsymmetricKey());
667643
return true;

0 commit comments

Comments
 (0)