Skip to content

Commit

Permalink
crypto: DRY Diffie-Hellman initialization code
Browse files Browse the repository at this point in the history
PR-URL: #23657
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
bnoordhuis authored and jasnell committed Oct 21, 2018
1 parent add4f01 commit 1336830
Showing 1 changed file with 38 additions and 60 deletions.
98 changes: 38 additions & 60 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ using v8::DontDelete;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::External;
using v8::FunctionCallback;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
Expand Down Expand Up @@ -3923,67 +3924,44 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {


void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);

const PropertyAttribute attributes =
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);

t->InstanceTemplate()->SetInternalFieldCount(1);

env->SetProtoMethod(t, "generateKeys", GenerateKeys);
env->SetProtoMethod(t, "computeSecret", ComputeSecret);
env->SetProtoMethodNoSideEffect(t, "getPrime", GetPrime);
env->SetProtoMethodNoSideEffect(t, "getGenerator", GetGenerator);
env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey);
env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey);
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);

Local<FunctionTemplate> verify_error_getter_templ =
FunctionTemplate::New(env->isolate(),
DiffieHellman::VerifyErrorGetter,
env->as_external(),
Signature::New(env->isolate(), t),
/* length */ 0,
ConstructorBehavior::kThrow,
SideEffectType::kHasNoSideEffect);

t->InstanceTemplate()->SetAccessorProperty(
env->verify_error_string(),
verify_error_getter_templ,
Local<FunctionTemplate>(),
attributes);

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"),
t->GetFunction(env->context()).ToLocalChecked());

Local<FunctionTemplate> t2 = env->NewFunctionTemplate(DiffieHellmanGroup);
t2->InstanceTemplate()->SetInternalFieldCount(1);

env->SetProtoMethod(t2, "generateKeys", GenerateKeys);
env->SetProtoMethod(t2, "computeSecret", ComputeSecret);
env->SetProtoMethodNoSideEffect(t2, "getPrime", GetPrime);
env->SetProtoMethodNoSideEffect(t2, "getGenerator", GetGenerator);
env->SetProtoMethodNoSideEffect(t2, "getPublicKey", GetPublicKey);
env->SetProtoMethodNoSideEffect(t2, "getPrivateKey", GetPrivateKey);

Local<FunctionTemplate> verify_error_getter_templ2 =
FunctionTemplate::New(env->isolate(),
DiffieHellman::VerifyErrorGetter,
env->as_external(),
Signature::New(env->isolate(), t2),
/* length */ 0,
ConstructorBehavior::kThrow,
SideEffectType::kHasNoSideEffect);

t2->InstanceTemplate()->SetAccessorProperty(
env->verify_error_string(),
verify_error_getter_templ2,
Local<FunctionTemplate>(),
attributes);
auto make = [&] (Local<String> name, FunctionCallback callback) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(callback);

const PropertyAttribute attributes =
static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);

t->InstanceTemplate()->SetInternalFieldCount(1);

env->SetProtoMethod(t, "generateKeys", GenerateKeys);
env->SetProtoMethod(t, "computeSecret", ComputeSecret);
env->SetProtoMethodNoSideEffect(t, "getPrime", GetPrime);
env->SetProtoMethodNoSideEffect(t, "getGenerator", GetGenerator);
env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey);
env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey);
env->SetProtoMethod(t, "setPublicKey", SetPublicKey);
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey);

Local<FunctionTemplate> verify_error_getter_templ =
FunctionTemplate::New(env->isolate(),
DiffieHellman::VerifyErrorGetter,
env->as_external(),
Signature::New(env->isolate(), t),
/* length */ 0,
ConstructorBehavior::kThrow,
SideEffectType::kHasNoSideEffect);

t->InstanceTemplate()->SetAccessorProperty(
env->verify_error_string(),
verify_error_getter_templ,
Local<FunctionTemplate>(),
attributes);

target->Set(name, t->GetFunction(env->context()).ToLocalChecked());
};

target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
t2->GetFunction(env->context()).ToLocalChecked());
make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New);
make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"),
DiffieHellmanGroup);
}


Expand Down

0 comments on commit 1336830

Please sign in to comment.