@@ -483,49 +483,11 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
483483 }
484484}
485485
486- namespace {
487- ByteSource StatelessDiffieHellmanThreadsafe (const EVPKeyPointer& our_key,
488- const EVPKeyPointer& their_key) {
489- auto dp = DHPointer::stateless (our_key, their_key);
490- if (!dp) return {};
491- DCHECK (!dp.isSecure ());
492-
493- return ByteSource::Allocated (dp.release ());
494- }
495-
496- void Stateless (const FunctionCallbackInfo<Value>& args) {
497- Environment* env = Environment::GetCurrent (args);
498-
499- CHECK (args[0 ]->IsObject () && args[1 ]->IsObject ());
500- KeyObjectHandle* our_key_object;
501- ASSIGN_OR_RETURN_UNWRAP (&our_key_object, args[0 ].As <Object>());
502- CHECK_EQ (our_key_object->Data ().GetKeyType (), kKeyTypePrivate );
503- KeyObjectHandle* their_key_object;
504- ASSIGN_OR_RETURN_UNWRAP (&their_key_object, args[1 ].As <Object>());
505- CHECK_NE (their_key_object->Data ().GetKeyType (), kKeyTypeSecret );
506-
507- const auto & our_key = our_key_object->Data ().GetAsymmetricKey ();
508- const auto & their_key = their_key_object->Data ().GetAsymmetricKey ();
509-
510- Local<Value> out;
511- if (!StatelessDiffieHellmanThreadsafe (our_key, their_key)
512- .ToBuffer (env)
513- .ToLocal (&out)) return ;
514-
515- if (Buffer::Length (out) == 0 )
516- return ThrowCryptoError (env, ERR_get_error (), " diffieHellman failed" );
517-
518- args.GetReturnValue ().Set (out);
519- }
520- } // namespace
521-
522486Maybe<void > DHBitsTraits::AdditionalConfig (
523487 CryptoJobMode mode,
524488 const FunctionCallbackInfo<Value>& args,
525489 unsigned int offset,
526490 DHBitsConfig* params) {
527- Environment* env = Environment::GetCurrent (args);
528-
529491 CHECK (args[offset]->IsObject ()); // public key
530492 CHECK (args[offset + 1 ]->IsObject ()); // private key
531493
@@ -535,11 +497,8 @@ Maybe<void> DHBitsTraits::AdditionalConfig(
535497 ASSIGN_OR_RETURN_UNWRAP (&public_key, args[offset], Nothing<void >());
536498 ASSIGN_OR_RETURN_UNWRAP (&private_key, args[offset + 1 ], Nothing<void >());
537499
538- if (private_key->Data ().GetKeyType () != kKeyTypePrivate ||
539- public_key->Data ().GetKeyType () != kKeyTypePublic ) {
540- THROW_ERR_CRYPTO_INVALID_KEYTYPE (env);
541- return Nothing<void >();
542- }
500+ CHECK (private_key->Data ().GetKeyType () == kKeyTypePrivate );
501+ CHECK (public_key->Data ().GetKeyType () != kKeyTypeSecret );
543502
544503 params->public_key = public_key->Data ().addRef ();
545504 params->private_key = private_key->Data ().addRef ();
@@ -557,8 +516,20 @@ bool DHBitsTraits::DeriveBits(
557516 Environment* env,
558517 const DHBitsConfig& params,
559518 ByteSource* out) {
560- *out = StatelessDiffieHellmanThreadsafe (params.private_key .GetAsymmetricKey (),
561- params.public_key .GetAsymmetricKey ());
519+ auto dp = DHPointer::stateless (params.private_key .GetAsymmetricKey (),
520+ params.public_key .GetAsymmetricKey ());
521+ if (!dp) {
522+ bool can_throw =
523+ per_process::v8_initialized && Isolate::TryGetCurrent () != nullptr ;
524+ if (can_throw) {
525+ unsigned long err = ERR_get_error (); // NOLINT(runtime/int)
526+ if (err) ThrowCryptoError (env, err, " diffieHellman failed" );
527+ }
528+ return false ;
529+ }
530+
531+ *out = ByteSource::Allocated (dp.release ());
532+ CHECK (!out->empty ());
562533 return true ;
563534}
564535
@@ -611,7 +582,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
611582 make (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellmanGroup" ),
612583 DiffieHellmanGroup);
613584
614- SetMethodNoSideEffect (context, target, " statelessDH" , Stateless);
615585 DHKeyPairGenJob::Initialize (env, target);
616586 DHKeyExportJob::Initialize (env, target);
617587 DHBitsJob::Initialize (env, target);
@@ -632,7 +602,6 @@ void DiffieHellman::RegisterExternalReferences(
632602 registry->Register (SetPrivateKey);
633603
634604 registry->Register (Check);
635- registry->Register (Stateless);
636605
637606 DHKeyPairGenJob::RegisterExternalReferences (registry);
638607 DHKeyExportJob::RegisterExternalReferences (registry);
0 commit comments