DH: add Curve25519 unit test based off of RFC7748 test vectors

This commit is contained in:
terrafrost 2019-08-08 08:00:37 -05:00
parent 66efabda53
commit 7cf5facae7
2 changed files with 31 additions and 0 deletions

View File

@ -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;

View File

@ -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));
}
}