Skip to content

Commit 5dab489

Browse files
tniessentargos
authored andcommitted
crypto: simplify DH groups
PR-URL: #31178 Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
1 parent 9f85585 commit 5dab489

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/node_crypto.cc

+15-15
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,13 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
51455145
return VerifyContext();
51465146
}
51475147

5148+
inline const modp_group* FindDiffieHellmanGroup(const char* name) {
5149+
for (const modp_group& group : modp_groups) {
5150+
if (StringEqualNoCase(name, group.name))
5151+
return &group;
5152+
}
5153+
return nullptr;
5154+
}
51485155

51495156
void DiffieHellman::DiffieHellmanGroup(
51505157
const FunctionCallbackInfo<Value>& args) {
@@ -5160,22 +5167,15 @@ void DiffieHellman::DiffieHellmanGroup(
51605167
bool initialized = false;
51615168

51625169
const node::Utf8Value group_name(env->isolate(), args[0]);
5163-
for (size_t i = 0; i < arraysize(modp_groups); ++i) {
5164-
const modp_group* it = modp_groups + i;
5165-
5166-
if (!StringEqualNoCase(*group_name, it->name))
5167-
continue;
5168-
5169-
initialized = diffieHellman->Init(it->prime,
5170-
it->prime_size,
5171-
it->gen,
5172-
it->gen_size);
5173-
if (!initialized)
5174-
env->ThrowError("Initialization failed");
5175-
return;
5176-
}
5170+
const modp_group* group = FindDiffieHellmanGroup(*group_name);
5171+
if (group == nullptr)
5172+
return env->ThrowError("Unknown group");
51775173

5178-
env->ThrowError("Unknown group");
5174+
initialized = diffieHellman->Init(group->prime,
5175+
group->prime_size,
5176+
group->gen);
5177+
if (!initialized)
5178+
env->ThrowError("Initialization failed");
51795179
}
51805180

51815181

src/node_crypto_groups.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333

3434

35-
static const unsigned char two_generator[] = { 2 };
35+
static const unsigned int two_generator = 2;
3636

3737
static const unsigned char group_modp1[] = {
3838
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
@@ -394,20 +394,19 @@ typedef struct {
394394
const char* name;
395395
const char* prime;
396396
unsigned int prime_size;
397-
const char* gen;
398-
unsigned int gen_size;
397+
unsigned int gen;
399398
} modp_group;
400399

401400
static const modp_group modp_groups[] = {
402401
#define V(var) reinterpret_cast<const char*>(var)
403-
{ "modp1", V(group_modp1), sizeof(group_modp1), V(two_generator), 1 },
404-
{ "modp2", V(group_modp2), sizeof(group_modp2), V(two_generator), 1 },
405-
{ "modp5", V(group_modp5), sizeof(group_modp5), V(two_generator), 1 },
406-
{ "modp14", V(group_modp14), sizeof(group_modp14), V(two_generator), 1 },
407-
{ "modp15", V(group_modp15), sizeof(group_modp15), V(two_generator), 1 },
408-
{ "modp16", V(group_modp16), sizeof(group_modp16), V(two_generator), 1 },
409-
{ "modp17", V(group_modp17), sizeof(group_modp17), V(two_generator), 1 },
410-
{ "modp18", V(group_modp18), sizeof(group_modp18), V(two_generator), 1 }
402+
{ "modp1", V(group_modp1), sizeof(group_modp1), two_generator },
403+
{ "modp2", V(group_modp2), sizeof(group_modp2), two_generator },
404+
{ "modp5", V(group_modp5), sizeof(group_modp5), two_generator },
405+
{ "modp14", V(group_modp14), sizeof(group_modp14), two_generator },
406+
{ "modp15", V(group_modp15), sizeof(group_modp15), two_generator },
407+
{ "modp16", V(group_modp16), sizeof(group_modp16), two_generator },
408+
{ "modp17", V(group_modp17), sizeof(group_modp17), two_generator },
409+
{ "modp18", V(group_modp18), sizeof(group_modp18), two_generator }
411410
#undef V
412411
};
413412

0 commit comments

Comments
 (0)