Skip to content

Commit 4cc57b4

Browse files
committed
crypto: simplify DH modp group name matcher
* Use ARRAY_SIZE() rather than scanning until we hit a NULL entry. * Fix `-fsigned-char -Wnarrowing` compiler warnings. Harmless but numerous and annoying. * Static-ify the modp_group and mod_groups arrays. * Const-ify the modp_groups array.
1 parent 8111ca2 commit 4cc57b4

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src/node_crypto.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,25 +2972,21 @@ void DiffieHellman::DiffieHellmanGroup(
29722972
}
29732973

29742974
const String::Utf8Value group_name(args[0]);
2975+
for (unsigned int i = 0; i < ARRAY_SIZE(modp_groups); ++i) {
2976+
const modp_group* it = modp_groups + i;
29752977

2976-
modp_group* it = modp_groups;
2978+
if (strcasecmp(*group_name, it->name) != 0)
2979+
continue;
29772980

2978-
while (it->name != NULL) {
2979-
if (!strcasecmp(*group_name, it->name))
2980-
break;
2981-
it++;
2982-
}
2983-
2984-
if (it->name != NULL) {
29852981
diffieHellman->Init(it->prime,
29862982
it->prime_size,
29872983
it->gen,
29882984
it->gen_size);
2989-
} else {
2990-
return ThrowError("Unknown group");
2985+
diffieHellman->Wrap(args.This());
2986+
return;
29912987
}
29922988

2993-
diffieHellman->Wrap(args.This());
2989+
ThrowError("Unknown group");
29942990
}
29952991

29962992

src/node_crypto_groups.h

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99

10-
const char two_generator[] = { 2 };
10+
static const unsigned char two_generator[] = { 2 };
1111

12-
const char group_modp1[] = {
12+
static const unsigned char group_modp1[] = {
1313
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
1414
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
1515
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -21,7 +21,7 @@ const char group_modp1[] = {
2121
0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
2222
0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2323

24-
const char group_modp2[] = {
24+
static const unsigned char group_modp2[] = {
2525
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
2626
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
2727
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -36,7 +36,7 @@ const char group_modp2[] = {
3636
0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe6, 0x53, 0x81,
3737
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3838

39-
const char group_modp5[] = {
39+
static const unsigned char group_modp5[] = {
4040
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
4141
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
4242
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -58,7 +58,7 @@ const char group_modp5[] = {
5858
0xca, 0x23, 0x73, 0x27, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5959
0xff, 0xff };
6060

61-
const char group_modp14[] = {
61+
static const unsigned char group_modp14[] = {
6262
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
6363
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
6464
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -86,7 +86,7 @@ const char group_modp14[] = {
8686
0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68, 0xff, 0xff,
8787
0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8888

89-
const char group_modp15[] = {
89+
static const unsigned char group_modp15[] = {
9090
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
9191
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
9292
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -127,7 +127,7 @@ const char group_modp15[] = {
127127
0xd1, 0x20, 0xa9, 0x3a, 0xd2, 0xca, 0xff, 0xff, 0xff, 0xff,
128128
0xff, 0xff, 0xff, 0xff };
129129

130-
const char group_modp16[] = {
130+
static const unsigned char group_modp16[] = {
131131
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
132132
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
133133
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -181,7 +181,7 @@ const char group_modp16[] = {
181181
0x34, 0x06, 0x31, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
182182
0xff, 0xff };
183183

184-
const char group_modp17[] = {
184+
static const unsigned char group_modp17[] = {
185185
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
186186
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
187187
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -260,7 +260,7 @@ const char group_modp17[] = {
260260
0x74, 0xd6, 0xe6, 0x94, 0xf9, 0x1e, 0x6d, 0xcc, 0x40, 0x24,
261261
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
262262

263-
const char group_modp18[] = {
263+
static const unsigned char group_modp18[] = {
264264
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f,
265265
0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
266266
0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67,
@@ -368,19 +368,20 @@ const char group_modp18[] = {
368368
typedef struct {
369369
const char* name;
370370
const char* prime;
371-
int prime_size;
371+
unsigned int prime_size;
372372
const char* gen;
373-
int gen_size;
373+
unsigned int gen_size;
374374
} modp_group;
375375

376-
modp_group modp_groups[] = {
377-
{ "modp1", group_modp1, sizeof(group_modp1) / sizeof(group_modp1[0]), two_generator, 1 },
378-
{ "modp2", group_modp2, sizeof(group_modp2) / sizeof(group_modp2[0]), two_generator, 1 },
379-
{ "modp5", group_modp5, sizeof(group_modp5) / sizeof(group_modp5[0]), two_generator, 1 },
380-
{ "modp14", group_modp14, sizeof(group_modp14) / sizeof(group_modp14[0]), two_generator, 1 },
381-
{ "modp15", group_modp15, sizeof(group_modp15) / sizeof(group_modp15[0]), two_generator, 1 },
382-
{ "modp16", group_modp16, sizeof(group_modp16) / sizeof(group_modp16[0]), two_generator, 1 },
383-
{ "modp17", group_modp17, sizeof(group_modp17) / sizeof(group_modp17[0]), two_generator, 1 },
384-
{ "modp18", group_modp18, sizeof(group_modp18) / sizeof(group_modp18[0]), two_generator, 1 },
385-
{ NULL, NULL, 0, NULL, 0 }
376+
static const modp_group modp_groups[] = {
377+
#define V(var) reinterpret_cast<const char*>(var)
378+
{ "modp1", V(group_modp1), sizeof(group_modp1), V(two_generator), 1 },
379+
{ "modp2", V(group_modp2), sizeof(group_modp2), V(two_generator), 1 },
380+
{ "modp5", V(group_modp5), sizeof(group_modp5), V(two_generator), 1 },
381+
{ "modp14", V(group_modp14), sizeof(group_modp14), V(two_generator), 1 },
382+
{ "modp15", V(group_modp15), sizeof(group_modp15), V(two_generator), 1 },
383+
{ "modp16", V(group_modp16), sizeof(group_modp16), V(two_generator), 1 },
384+
{ "modp17", V(group_modp17), sizeof(group_modp17), V(two_generator), 1 },
385+
{ "modp18", V(group_modp18), sizeof(group_modp18), V(two_generator), 1 }
386+
#undef V
386387
};

0 commit comments

Comments
 (0)