From e839fa80a30f451ab16054034c24191f2e4c0f38 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 13 Apr 2014 00:35:24 +0200 Subject: [PATCH] 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.' + ); + } }