diffie-hellman: Explicitly initialize DH exponent sizes during initialization
authorMartin Willi <martin@revosec.ch>
Wed, 16 Jul 2014 14:44:24 +0000 (16:44 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 25 Aug 2014 07:43:29 +0000 (09:43 +0200)
To avoid any race conditions when multiple threads call and initialize
diffie_hellman_get_params(), explicitly examine the optimum DH exponent size
during library initialization.

Fixes #655.

src/libstrongswan/crypto/diffie_hellman.c
src/libstrongswan/crypto/diffie_hellman.h
src/libstrongswan/library.c

index 5c1d08d..87c9b21 100644 (file)
@@ -66,6 +66,7 @@ static struct {
 } dh_params[] = {
        {
                .group = MODP_768_BIT, .opt_exp = 32, .public = {
+                       .exp_len = 32,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -74,10 +75,10 @@ static struct {
                                0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
                                0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
                                0xF4,0x4C,0x42,0xE9,0xA6,0x3A,0x36,0x20,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_1024_BIT, .opt_exp = 32, .public = {
+                       .exp_len = 32,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -88,10 +89,10 @@ static struct {
                                0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
                                0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
                                0x49,0x28,0x66,0x51,0xEC,0xE6,0x53,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_1536_BIT, .opt_exp = 32, .public = {
+                       .exp_len = 32,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -106,10 +107,10 @@ static struct {
                                0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
                                0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
                                0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_2048_BIT, .opt_exp = 48, .public = {
+                       .exp_len = 48,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -128,10 +129,10 @@ static struct {
                                0xB5,0xC5,0x5D,0xF0,0x6F,0x4C,0x52,0xC9,0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,
                                0x39,0x95,0x49,0x7C,0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,0x98,0xFA,0x05,0x10,
                                0x15,0x72,0x8E,0x5A,0x8A,0xAC,0xAA,0x68,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_3072_BIT, .opt_exp = 48, .public = {
+                       .exp_len = 48,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -158,10 +159,10 @@ static struct {
                                0xBB,0xE1,0x17,0x57,0x7A,0x61,0x5D,0x6C,0x77,0x09,0x88,0xC0,0xBA,0xD9,0x46,0xE2,
                                0x08,0xE2,0x4F,0xA0,0x74,0xE5,0xAB,0x31,0x43,0xDB,0x5B,0xFC,0xE0,0xFD,0x10,0x8E,
                                0x4B,0x82,0xD1,0x20,0xA9,0x3A,0xD2,0xCA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_4096_BIT, .opt_exp = 64, .public = {
+                       .exp_len = 64,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -196,10 +197,10 @@ static struct {
                                0xB8,0x1B,0xDD,0x76,0x21,0x70,0x48,0x1C,0xD0,0x06,0x91,0x27,0xD5,0xB0,0x5A,0xA9,
                                0x93,0xB4,0xEA,0x98,0x8D,0x8F,0xDD,0xC1,0x86,0xFF,0xB7,0xDC,0x90,0xA6,0xC0,0x8F,
                                0x4D,0xF4,0x35,0xC9,0x34,0x06,0x31,0x99,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_6144_BIT, .opt_exp = 64, .public = {
+                       .exp_len = 64,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -250,10 +251,10 @@ static struct {
                                0xDA,0x56,0xC9,0xEC,0x2E,0xF2,0x96,0x32,0x38,0x7F,0xE8,0xD7,0x6E,0x3C,0x04,0x68,
                                0x04,0x3E,0x8F,0x66,0x3F,0x48,0x60,0xEE,0x12,0xBF,0x2D,0x5B,0x0B,0x74,0x74,0xD6,
                                0xE6,0x94,0xF9,0x1E,0x6D,0xCC,0x40,0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_8192_BIT, .opt_exp = 64, .public = {
+                       .exp_len = 64,
                        .generator = chunk_from_chars(0x02),
                        .prime = chunk_from_chars(
                                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
@@ -320,10 +321,10 @@ static struct {
                                0xC9,0x19,0x0D,0xA6,0xFC,0x02,0x6E,0x47,0x95,0x58,0xE4,0x47,0x56,0x77,0xE9,0xAA,
                                0x9E,0x30,0x50,0xE2,0x76,0x56,0x94,0xDF,0xC8,0x1F,0x56,0xE8,0x80,0xB9,0x6E,0x71,
                                0x60,0xC9,0x80,0xDD,0x98,0xED,0xD3,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF),
-                       .exp_len = 0,
                },
        },{
                .group = MODP_1024_160, .opt_exp = 20, .public = {
+                       .exp_len = 20,
                        .subgroup = chunk_from_chars(
                                0xF5,0x18,0xAA,0x87,0x81,0xA8,0xDF,0x27,0x8A,0xBA,0x4E,0x7D,0x64,0xB7,0xCB,0x9D,
                                0x49,0x46,0x23,0x53),
@@ -348,6 +349,7 @@ static struct {
                },
        }, {
                .group = MODP_2048_224, .opt_exp = 28, .public = {
+                       .exp_len = 28,
                        .subgroup = chunk_from_chars(
                                0x80,0x1C,0x0D,0x34,0xC5,0x8D,0x93,0xFE,0x99,0x71,0x77,0x10,0x1F,0x80,0x53,0x5A,
                                0x47,0x38,0xCE,0xBC,0xBF,0x38,0x9A,0x99,0xB3,0x63,0x71,0xEB),
@@ -388,6 +390,7 @@ static struct {
                },
        },{
                .group = MODP_2048_256, .opt_exp = 32, .public = {
+                       .exp_len = 32,
                        .subgroup = chunk_from_chars(
                                0x8C,0xF8,0x36,0x42,0xA7,0x09,0xA0,0x97,0xB4,0x47,0x99,0x76,0x40,0x12,0x9D,0xA2,
                                0x99,0xB1,0xA4,0x7D,0x1E,0xB3,0x75,0x0B,0xA3,0x08,0xB0,0xFE,0x64,0xF5,0xFB,0xD3),
@@ -430,6 +433,23 @@ static struct {
 };
 
 /**
+ * See header.
+ */
+void diffie_hellman_init()
+{
+       int i;
+
+       if (lib->settings->get_int(lib->settings,
+                                       "%s.dh_exponent_ansi_x9_42", TRUE, lib->ns))
+       {
+               for (i = 0; i < countof(dh_params); i++)
+               {
+                       dh_params[i].public.exp_len = dh_params[i].public.prime.len;
+               }
+       }
+}
+
+/**
  * Described in header.
  */
 diffie_hellman_params_t *diffie_hellman_get_params(diffie_hellman_group_t group)
index 00d7003..105db22 100644 (file)
@@ -149,8 +149,16 @@ struct diffie_hellman_params_t {
 };
 
 /**
+ * Initialize diffie hellman parameters during startup.
+ */
+void diffie_hellman_init();
+
+/**
  * Get the parameters associated with the specified diffie hellman group.
  *
+ * Before calling this method, use diffie_hellman_init() to initialize the
+ * DH group table. This is usually done by library_init().
+ *
  * @param group                        DH group
  * @return                             The parameters or NULL, if the group is not supported
  */
index e3ad164..4d2b3eb 100644 (file)
@@ -351,5 +351,7 @@ bool library_init(char *settings, const char *namespace)
 #endif /* INTEGRITY_TEST */
        }
 
+       diffie_hellman_init();
+
        return !this->integrity_failed;
 }