Skip to content

Commit 15bd2c9

Browse files
tniessencodebytere
authored andcommitted
crypto: simplify DH groups
PR-URL: #31178 Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
1 parent 572322f commit 15bd2c9

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
@@ -5687,6 +5687,13 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
56875687
return VerifyContext();
56885688
}
56895689

5690+
inline const modp_group* FindDiffieHellmanGroup(const char* name) {
5691+
for (const modp_group& group : modp_groups) {
5692+
if (StringEqualNoCase(name, group.name))
5693+
return &group;
5694+
}
5695+
return nullptr;
5696+
}
56905697

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

57045711
const node::Utf8Value group_name(env->isolate(), args[0]);
5705-
for (size_t i = 0; i < arraysize(modp_groups); ++i) {
5706-
const modp_group* it = modp_groups + i;
5707-
5708-
if (!StringEqualNoCase(*group_name, it->name))
5709-
continue;
5710-
5711-
initialized = diffieHellman->Init(it->prime,
5712-
it->prime_size,
5713-
it->gen,
5714-
it->gen_size);
5715-
if (!initialized)
5716-
env->ThrowError("Initialization failed");
5717-
return;
5718-
}
5712+
const modp_group* group = FindDiffieHellmanGroup(*group_name);
5713+
if (group == nullptr)
5714+
return env->ThrowError("Unknown group");
57195715

5720-
env->ThrowError("Unknown group");
5716+
initialized = diffieHellman->Init(group->prime,
5717+
group->prime_size,
5718+
group->gen);
5719+
if (!initialized)
5720+
env->ThrowError("Initialization failed");
57215721
}
57225722

57235723

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)