From e839fa80a30f451ab16054034c24191f2e4c0f38 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 13 Apr 2014 00:35:24 +0200 Subject: [PATCH 1/4] Add Diffie-Hellman Test to BigInteger Testcase. --- tests/Math/BigInteger/TestCase.php | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index ea9d4e2b..78c72b5f 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -265,4 +265,42 @@ abstract class Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('18446744073709551616', (string) $x); $this->assertSame('18446744073709551616', (string) $y); } + + public function testDiffieHellmanKeyAgreement() + { + // "Oakley Group 14" 2048-bit modular exponentiation group as used in + // SSH2 diffie-hellman-group14-sha1 + $prime = $this->getInstance( + 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' . + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' . + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' . + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' . + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' . + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' . + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' . + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' . + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' . + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' . + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', + 16 + ); + $generator = $this->getInstance(2); + + $one = $this->getInstance(1); + $max = $one->bitwise_leftShift(512)->subtract($one); + + $alicePrivate = $one->random($one, $max); + $alicePublic = $generator->modPow($alicePrivate, $prime); + + $bobPrivate = $one->random($one, $max); + $bobPublic = $generator->modPow($bobPrivate, $prime); + + $aliceShared = $bobPublic->modPow($alicePrivate, $prime); + $bobShared = $alicePublic->modPow($bobPrivate, $prime); + + $this->assertTrue( + $aliceShared->equals($bobShared), + 'Failed asserting that Alice and Bob share the same BigInteger.' + ); + } } From 7e007ad8795513a0c592b878fb02129db98ee414 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 13 Apr 2014 01:21:19 +0200 Subject: [PATCH 2/4] Link testDiffieHellmanKeyAgreement to corresponding bug ticket. --- tests/Math/BigInteger/TestCase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 78c72b5f..26566d86 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -266,6 +266,9 @@ abstract class Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('18446744073709551616', (string) $y); } + /** + * @group github279 + */ public function testDiffieHellmanKeyAgreement() { // "Oakley Group 14" 2048-bit modular exponentiation group as used in From 9e79fc2407afcee1a5956a69d4776888ccc120d1 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 24 Apr 2014 01:19:07 +0200 Subject: [PATCH 3/4] Remove randomness from testDiffieHellmanKeyAgreement. --- tests/Math/BigInteger/TestCase.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 26566d86..63939f87 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -289,13 +289,29 @@ abstract class Math_BigInteger_TestCase extends PhpseclibTestCase ); $generator = $this->getInstance(2); + /* + Code for generation of $alicePrivate and $bobPrivate. $one = $this->getInstance(1); $max = $one->bitwise_leftShift(512)->subtract($one); - $alicePrivate = $one->random($one, $max); - $alicePublic = $generator->modPow($alicePrivate, $prime); - $bobPrivate = $one->random($one, $max); + var_dump($alicePrivate->toHex(), $bobPrivate->toHex()); + */ + + $alicePrivate = $this->getInstance( + '22606EDA7960458BC9D65F46DD96F114F9A004F0493C1F26' . + '2139D2C8063B733162E876182CA3BF063AB1A167ABDB7F03' . + 'E0A225A6205660439F6CE46D252069FF', + 16 + ); + $bobPrivate = $this->getInstance( + '6E3EFA13A96025D63E4B0D88A09B3A46DDFE9DD3BC9D1655' . + '4898C02B4AC181F0CEB4E818664B12F02C71A07215C400F9' . + '88352A4779F3E88836F7C3D3B3C739DE', + 16 + ); + + $alicePublic = $generator->modPow($alicePrivate, $prime); $bobPublic = $generator->modPow($bobPrivate, $prime); $aliceShared = $bobPublic->modPow($alicePrivate, $prime); From 602fb55195e891487c9dfefde853f38013c911ec Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 29 Apr 2014 16:58:55 +0200 Subject: [PATCH 4/4] Skip testDiffieHellmanKeyAgreement on TravisCI+5.3.3+Internal in the meantime. --- tests/Math/BigInteger/TestCase.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Math/BigInteger/TestCase.php b/tests/Math/BigInteger/TestCase.php index 63939f87..d62dcec5 100644 --- a/tests/Math/BigInteger/TestCase.php +++ b/tests/Math/BigInteger/TestCase.php @@ -271,6 +271,14 @@ abstract class Math_BigInteger_TestCase extends PhpseclibTestCase */ public function testDiffieHellmanKeyAgreement() { + if (getenv('TRAVIS') && PHP_VERSION === '5.3.3' + && MATH_BIGINTEGER_MODE === MATH_BIGINTEGER_MODE_INTERNAL + ) { + $this->markTestIncomplete( + 'This test hangs on PHP 5.3.3 using internal mode.' + ); + } + // "Oakley Group 14" 2048-bit modular exponentiation group as used in // SSH2 diffie-hellman-group14-sha1 $prime = $this->getInstance(