Skip to content

Commit

Permalink
crypto: simplify DH groups
Browse files Browse the repository at this point in the history
PR-URL: #31178
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
  • Loading branch information
tniessen authored and codebytere committed Feb 17, 2020
1 parent 572322f commit 15bd2c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
30 changes: 15 additions & 15 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5687,6 +5687,13 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
return VerifyContext();
}

inline const modp_group* FindDiffieHellmanGroup(const char* name) {
for (const modp_group& group : modp_groups) {
if (StringEqualNoCase(name, group.name))
return &group;
}
return nullptr;
}

void DiffieHellman::DiffieHellmanGroup(
const FunctionCallbackInfo<Value>& args) {
Expand All @@ -5702,22 +5709,15 @@ void DiffieHellman::DiffieHellmanGroup(
bool initialized = false;

const node::Utf8Value group_name(env->isolate(), args[0]);
for (size_t i = 0; i < arraysize(modp_groups); ++i) {
const modp_group* it = modp_groups + i;

if (!StringEqualNoCase(*group_name, it->name))
continue;

initialized = diffieHellman->Init(it->prime,
it->prime_size,
it->gen,
it->gen_size);
if (!initialized)
env->ThrowError("Initialization failed");
return;
}
const modp_group* group = FindDiffieHellmanGroup(*group_name);
if (group == nullptr)
return env->ThrowError("Unknown group");

env->ThrowError("Unknown group");
initialized = diffieHellman->Init(group->prime,
group->prime_size,
group->gen);
if (!initialized)
env->ThrowError("Initialization failed");
}


Expand Down
21 changes: 10 additions & 11 deletions src/node_crypto_groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/


static const unsigned char two_generator[] = { 2 };
static const unsigned int two_generator = 2;

static const unsigned char group_modp1[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
Expand Down Expand Up @@ -394,20 +394,19 @@ typedef struct {
const char* name;
const char* prime;
unsigned int prime_size;
const char* gen;
unsigned int gen_size;
unsigned int gen;
} modp_group;

static const modp_group modp_groups[] = {
#define V(var) reinterpret_cast<const char*>(var)
{ "modp1", V(group_modp1), sizeof(group_modp1), V(two_generator), 1 },
{ "modp2", V(group_modp2), sizeof(group_modp2), V(two_generator), 1 },
{ "modp5", V(group_modp5), sizeof(group_modp5), V(two_generator), 1 },
{ "modp14", V(group_modp14), sizeof(group_modp14), V(two_generator), 1 },
{ "modp15", V(group_modp15), sizeof(group_modp15), V(two_generator), 1 },
{ "modp16", V(group_modp16), sizeof(group_modp16), V(two_generator), 1 },
{ "modp17", V(group_modp17), sizeof(group_modp17), V(two_generator), 1 },
{ "modp18", V(group_modp18), sizeof(group_modp18), V(two_generator), 1 }
{ "modp1", V(group_modp1), sizeof(group_modp1), two_generator },
{ "modp2", V(group_modp2), sizeof(group_modp2), two_generator },
{ "modp5", V(group_modp5), sizeof(group_modp5), two_generator },
{ "modp14", V(group_modp14), sizeof(group_modp14), two_generator },
{ "modp15", V(group_modp15), sizeof(group_modp15), two_generator },
{ "modp16", V(group_modp16), sizeof(group_modp16), two_generator },
{ "modp17", V(group_modp17), sizeof(group_modp17), two_generator },
{ "modp18", V(group_modp18), sizeof(group_modp18), two_generator }
#undef V
};

Expand Down

0 comments on commit 15bd2c9

Please sign in to comment.