diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 485e53ad851e40..c9065278120541 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -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& args) { @@ -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"); } diff --git a/src/node_crypto_groups.h b/src/node_crypto_groups.h index d22fdc7f966f9a..108edda01bb4eb 100644 --- a/src/node_crypto_groups.h +++ b/src/node_crypto_groups.h @@ -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, @@ -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(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 };