mirror of
https://github.com/danog/phpseclib.git
synced 2024-12-11 00:29:36 +01:00
Tests: fix issues with Salsa20 / ChaCha20 unit tests
This commit is contained in:
parent
35d8974ac1
commit
4141799c02
@ -341,7 +341,9 @@ class Salsa20 extends StreamCipher
|
|||||||
} else {
|
} else {
|
||||||
$buffer = &$this->debuffer;
|
$buffer = &$this->debuffer;
|
||||||
}
|
}
|
||||||
if (strlen($buffer['ciphertext'])) {
|
if (!strlen($buffer['ciphertext'])) {
|
||||||
|
$ciphertext = '';
|
||||||
|
} else {
|
||||||
$ciphertext = $text ^ Strings::shift($buffer['ciphertext'], strlen($text));
|
$ciphertext = $text ^ Strings::shift($buffer['ciphertext'], strlen($text));
|
||||||
$text = substr($text, strlen($ciphertext));
|
$text = substr($text, strlen($ciphertext));
|
||||||
if (!strlen($text)) {
|
if (!strlen($text)) {
|
||||||
|
@ -93,11 +93,10 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
|
|||||||
$expected = pack('H*', $expected);
|
$expected = pack('H*', $expected);
|
||||||
|
|
||||||
$c = new ChaCha20;
|
$c = new ChaCha20;
|
||||||
|
$c->setPoly1305Key($key);
|
||||||
$r = new \ReflectionClass(get_class($c));
|
$r = new \ReflectionClass(get_class($c));
|
||||||
$p = $r->getProperty('poly1305Key');
|
// this unit test is testing Poly1305 independent of ChaCha20, which phpseclib doesn't
|
||||||
$p->setAccessible(true);
|
// really support, hence this hackish approach
|
||||||
$p->setValue($c, $key);
|
|
||||||
|
|
||||||
$m = $r->getMethod('poly1305');
|
$m = $r->getMethod('poly1305');
|
||||||
$m->setAccessible(true);
|
$m->setAccessible(true);
|
||||||
$result = $m->invokeArgs($c, [$plaintext]);
|
$result = $m->invokeArgs($c, [$plaintext]);
|
||||||
@ -108,7 +107,7 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
|
|||||||
// see https://tools.ietf.org/html/rfc8439#section-2.6.2
|
// see https://tools.ietf.org/html/rfc8439#section-2.6.2
|
||||||
public function test262()
|
public function test262()
|
||||||
{
|
{
|
||||||
$key = implode('', range("\80", "\x9f"));
|
$key = implode('', range("\x80", "\x9f"));
|
||||||
|
|
||||||
$nonce = '00 00 00 00 00 01 02 03 04 05 06 07';
|
$nonce = '00 00 00 00 00 01 02 03 04 05 06 07';
|
||||||
$nonce = str_replace(' ', '', $nonce);
|
$nonce = str_replace(' ', '', $nonce);
|
||||||
@ -119,25 +118,26 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
|
|||||||
$expected = str_replace(' ', '', $expected);
|
$expected = str_replace(' ', '', $expected);
|
||||||
$expected = pack('H*', $expected);
|
$expected = pack('H*', $expected);
|
||||||
|
|
||||||
$engines = ['PHP', 'OpenSSL', 'libsodium'];
|
|
||||||
foreach ($engines as $engine) {
|
|
||||||
$c = new ChaCha20();
|
$c = new ChaCha20();
|
||||||
$c->setKey($key);
|
$c->setKey($key);
|
||||||
$c->setNonce($nonce);
|
$c->setNonce($nonce);
|
||||||
//$c->setCounter(0);
|
|
||||||
$c->setPreferredEngine($engine);
|
$r = new \ReflectionClass(get_class($c));
|
||||||
if ($c->getEngine() != $engine) {
|
$m = $r->getMethod('createPoly1305Key');
|
||||||
continue;
|
$m->setAccessible(true);
|
||||||
}
|
$result = $m->invoke($c);
|
||||||
$result = $c->encrypt($plaintext);
|
|
||||||
$this->assertSame($expected, $result, "Failed asserting that ciphertext matches expected value with $engine engine");
|
$p = $r->getProperty('poly1305Key');
|
||||||
}
|
$p->setAccessible(true);
|
||||||
|
$actual = $p->getValue($c);
|
||||||
|
|
||||||
|
$this->assertSame($expected, $actual, 'Failed asserting that the poly1305 key is what it ought to be');
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc8439#section-2.8.2
|
// https://tools.ietf.org/html/rfc8439#section-2.8.2
|
||||||
public function test282()
|
public function test282()
|
||||||
{
|
{
|
||||||
$key = implode('', range("\80", "\x9f"));
|
$key = implode('', range("\x80", "\x9f"));
|
||||||
|
|
||||||
$nonce = "\x07\0\0\0" . "\x40\x41\x42\x43\x44\x45\x46\x47";
|
$nonce = "\x07\0\0\0" . "\x40\x41\x42\x43\x44\x45\x46\x47";
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ class Unit_Crypt_ChaCha20Test extends PhpseclibTestCase
|
|||||||
$expected = pack('H*', $expected);
|
$expected = pack('H*', $expected);
|
||||||
|
|
||||||
$tag = '1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91';
|
$tag = '1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91';
|
||||||
$tag = str_replace(' ', '', $tag);
|
$tag = str_replace(':', '', $tag);
|
||||||
$tag = pack('H*', $tag);
|
$tag = pack('H*', $tag);
|
||||||
|
|
||||||
$engines = ['PHP', 'OpenSSL', 'libsodium'];
|
$engines = ['PHP', 'OpenSSL', 'libsodium'];
|
||||||
|
@ -133,9 +133,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
|
|||||||
|
|
||||||
foreach ($engines as $engine) {
|
foreach ($engines as $engine) {
|
||||||
foreach ($tests as $test) {
|
foreach ($tests as $test) {
|
||||||
foreach ($test['output'] as $output) {
|
$result[] = [$engine, $test['key'], $test['iv'], $test['result']];
|
||||||
$result[] = [$engine, $test['key'], $output['iv'], $output['result']];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +145,7 @@ class Unit_Crypt_Salsa20Test extends PhpseclibTestCase
|
|||||||
*/
|
*/
|
||||||
public function testVectors($engine, $key, $iv, $expected)
|
public function testVectors($engine, $key, $iv, $expected)
|
||||||
{
|
{
|
||||||
$cipher = new Salsa();
|
$cipher = new Salsa20();
|
||||||
$cipher->setPreferredEngine($engine);
|
$cipher->setPreferredEngine($engine);
|
||||||
$cipher->setKey(pack('H*', $key));
|
$cipher->setKey(pack('H*', $key));
|
||||||
$cipher->setNonce(pack('H*', $iv));
|
$cipher->setNonce(pack('H*', $iv));
|
||||||
|
Loading…
Reference in New Issue
Block a user