From 4cc57b4aca65ea86f4e42ddaeadfa796db239719 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 29 Jul 2013 20:36:11 +0200 Subject: [PATCH] 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. --- src/node_crypto.cc | 18 +++++++---------- src/node_crypto_groups.h | 43 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 6247334535..ab3b6cc5aa 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2972,25 +2972,21 @@ void DiffieHellman::DiffieHellmanGroup( } const String::Utf8Value group_name(args[0]); + for (unsigned int i = 0; i < ARRAY_SIZE(modp_groups); ++i) { + const modp_group* it = modp_groups + i; - modp_group* it = modp_groups; + if (strcasecmp(*group_name, it->name) != 0) + continue; - while (it->name != NULL) { - if (!strcasecmp(*group_name, it->name)) - break; - it++; - } - - if (it->name != NULL) { diffieHellman->Init(it->prime, it->prime_size, it->gen, it->gen_size); - } else { - return ThrowError("Unknown group"); + diffieHellman->Wrap(args.This()); + return; } - diffieHellman->Wrap(args.This()); + ThrowError("Unknown group"); } diff --git a/src/node_crypto_groups.h b/src/node_crypto_groups.h index 5e8d0d516f..86ddd2ddaf 100644 --- a/src/node_crypto_groups.h +++ b/src/node_crypto_groups.h @@ -7,9 +7,9 @@ */ -const char two_generator[] = { 2 }; +static const unsigned char two_generator[] = { 2 }; -const char group_modp1[] = { +static const unsigned char group_modp1[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -21,7 +21,7 @@ const char group_modp1[] = { 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp2[] = { +static const unsigned char group_modp2[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -36,7 +36,7 @@ const char group_modp2[] = { 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe6, 0x53, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp5[] = { +static const unsigned char group_modp5[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -58,7 +58,7 @@ const char group_modp5[] = { 0xca, 0x23, 0x73, 0x27, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp14[] = { +static const unsigned char group_modp14[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -86,7 +86,7 @@ const char group_modp14[] = { 0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp15[] = { +static const unsigned char group_modp15[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -127,7 +127,7 @@ const char group_modp15[] = { 0xd1, 0x20, 0xa9, 0x3a, 0xd2, 0xca, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp16[] = { +static const unsigned char group_modp16[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -181,7 +181,7 @@ const char group_modp16[] = { 0x34, 0x06, 0x31, 0x99, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp17[] = { +static const unsigned char group_modp17[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -260,7 +260,7 @@ const char group_modp17[] = { 0x74, 0xd6, 0xe6, 0x94, 0xf9, 0x1e, 0x6d, 0xcc, 0x40, 0x24, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -const char group_modp18[] = { +static const unsigned char group_modp18[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, @@ -368,19 +368,20 @@ const char group_modp18[] = { typedef struct { const char* name; const char* prime; - int prime_size; + unsigned int prime_size; const char* gen; - int gen_size; + unsigned int gen_size; } modp_group; -modp_group modp_groups[] = { - { "modp1", group_modp1, sizeof(group_modp1) / sizeof(group_modp1[0]), two_generator, 1 }, - { "modp2", group_modp2, sizeof(group_modp2) / sizeof(group_modp2[0]), two_generator, 1 }, - { "modp5", group_modp5, sizeof(group_modp5) / sizeof(group_modp5[0]), two_generator, 1 }, - { "modp14", group_modp14, sizeof(group_modp14) / sizeof(group_modp14[0]), two_generator, 1 }, - { "modp15", group_modp15, sizeof(group_modp15) / sizeof(group_modp15[0]), two_generator, 1 }, - { "modp16", group_modp16, sizeof(group_modp16) / sizeof(group_modp16[0]), two_generator, 1 }, - { "modp17", group_modp17, sizeof(group_modp17) / sizeof(group_modp17[0]), two_generator, 1 }, - { "modp18", group_modp18, sizeof(group_modp18) / sizeof(group_modp18[0]), two_generator, 1 }, - { NULL, NULL, 0, NULL, 0 } +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 } +#undef V };