From 7cf5facae75138f30b91fc54a4b342a9c8efa5d8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 8 Aug 2019 08:00:37 -0500 Subject: [PATCH] DH: add Curve25519 unit test based off of RFC7748 test vectors --- phpseclib/Crypt/EC.php | 5 +++++ tests/Unit/Crypt/DHTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/phpseclib/Crypt/EC.php b/phpseclib/Crypt/EC.php index 3366824a..f9e9b59d 100644 --- a/phpseclib/Crypt/EC.php +++ b/phpseclib/Crypt/EC.php @@ -264,6 +264,11 @@ abstract class EC extends AsymmetricKey return $this->curveName; } + if ($this->curve instanceof MontgomeryCurve) { + $this->curveName = $this->curve instanceof Curve25519 ? 'Curve25519' : 'Curve448'; + return $this->curveName; + } + if ($this->curve instanceof TwistedEdwardsCurve) { $this->curveName = $this->curve instanceof Ed25519 ? 'Ed25519' : 'Ed448'; return $this->curveName; diff --git a/tests/Unit/Crypt/DHTest.php b/tests/Unit/Crypt/DHTest.php index 6c8fee0c..2c90185f 100644 --- a/tests/Unit/Crypt/DHTest.php +++ b/tests/Unit/Crypt/DHTest.php @@ -200,4 +200,30 @@ Q3ADAIcv9LEmTBnSAOsCs1K9ExAmSv/T2/4+9dW28UYb+p/uV477d1wf+nCWS6VU $this->assertSame($secrets[0], $secrets[$i]); } } + + public function testCurve25519() + { + // utilizing test vector from https://tools.ietf.org/html/rfc7748#section-6.1 + + $alicePrivate = EC::loadFormat('Curve25519Private', pack('H*', '77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a')); + $bobPrivate = EC::loadFormat('Curve25519Private', pack('H*', '5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb')); + + $alicePublic = $alicePrivate->getPublicKey(); + $bobPublic = $bobPrivate->getPublicKey(); + + $this->assertSame( + '8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a', + bin2hex($alicePublic->toString('Curve25519Public')) + ); + + $this->assertSame( + 'de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f', + bin2hex($bobPublic->toString('Curve25519Public')) + ); + + $expected = pack('H*', '4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742'); + + $this->assertSame($expected, DH::computeSecret($alicePrivate, $bobPublic)); + $this->assertSame($expected, DH::computeSecret($bobPrivate, $alicePublic)); + } }