@@ -477,41 +477,6 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
477477 }
478478}
479479
480- namespace {
481- ByteSource StatelessDiffieHellmanThreadsafe (const EVPKeyPointer& our_key,
482- const EVPKeyPointer& their_key) {
483- auto dp = DHPointer::stateless (our_key, their_key);
484- if (!dp) return {};
485-
486- return ByteSource::Allocated (dp.release ());
487- }
488-
489- void Stateless (const FunctionCallbackInfo<Value>& args) {
490- Environment* env = Environment::GetCurrent (args);
491-
492- CHECK (args[0 ]->IsObject () && args[1 ]->IsObject ());
493- KeyObjectHandle* our_key_object;
494- ASSIGN_OR_RETURN_UNWRAP (&our_key_object, args[0 ].As <Object>());
495- CHECK_EQ (our_key_object->Data ().GetKeyType (), kKeyTypePrivate );
496- KeyObjectHandle* their_key_object;
497- ASSIGN_OR_RETURN_UNWRAP (&their_key_object, args[1 ].As <Object>());
498- CHECK_NE (their_key_object->Data ().GetKeyType (), kKeyTypeSecret );
499-
500- const auto & our_key = our_key_object->Data ().GetAsymmetricKey ();
501- const auto & their_key = their_key_object->Data ().GetAsymmetricKey ();
502-
503- Local<Value> out;
504- if (!StatelessDiffieHellmanThreadsafe (our_key, their_key)
505- .ToBuffer (env)
506- .ToLocal (&out)) return ;
507-
508- if (Buffer::Length (out) == 0 )
509- return ThrowCryptoError (env, ERR_get_error (), " diffieHellman failed" );
510-
511- args.GetReturnValue ().Set (out);
512- }
513- } // namespace
514-
515480Maybe<void > DHBitsTraits::AdditionalConfig (
516481 CryptoJobMode mode,
517482 const FunctionCallbackInfo<Value>& args,
@@ -529,7 +494,7 @@ Maybe<void> DHBitsTraits::AdditionalConfig(
529494 ASSIGN_OR_RETURN_UNWRAP (&private_key, args[offset + 1 ], Nothing<void >());
530495
531496 if (private_key->Data ().GetKeyType () != kKeyTypePrivate ||
532- public_key->Data ().GetKeyType () != kKeyTypePublic ) {
497+ public_key->Data ().GetKeyType () == kKeyTypeSecret ) {
533498 THROW_ERR_CRYPTO_INVALID_KEYTYPE (env);
534499 return Nothing<void >();
535500 }
@@ -550,8 +515,20 @@ bool DHBitsTraits::DeriveBits(
550515 Environment* env,
551516 const DHBitsConfig& params,
552517 ByteSource* out) {
553- *out = StatelessDiffieHellmanThreadsafe (params.private_key .GetAsymmetricKey (),
554- params.public_key .GetAsymmetricKey ());
518+ auto dp = DHPointer::stateless (params.private_key .GetAsymmetricKey (),
519+ params.public_key .GetAsymmetricKey ());
520+ if (!dp) {
521+ bool can_throw =
522+ per_process::v8_initialized && Isolate::TryGetCurrent () != nullptr ;
523+ if (can_throw) {
524+ unsigned long err = ERR_get_error (); // NOLINT(runtime/int)
525+ if (err) ThrowCryptoError (env, err, " diffieHellman failed" );
526+ }
527+ return false ;
528+ }
529+
530+ *out = ByteSource::Allocated (dp.release ());
531+ CHECK (!out->empty ());
555532 return true ;
556533}
557534
@@ -604,7 +581,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
604581 make (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellmanGroup" ),
605582 DiffieHellmanGroup);
606583
607- SetMethodNoSideEffect (context, target, " statelessDH" , Stateless);
608584 DHKeyPairGenJob::Initialize (env, target);
609585 DHKeyExportJob::Initialize (env, target);
610586 DHBitsJob::Initialize (env, target);
@@ -625,7 +601,6 @@ void DiffieHellman::RegisterExternalReferences(
625601 registry->Register (SetPrivateKey);
626602
627603 registry->Register (Check);
628- registry->Register (Stateless);
629604
630605 DHKeyPairGenJob::RegisterExternalReferences (registry);
631606 DHKeyExportJob::RegisterExternalReferences (registry);
0 commit comments