2015-01-20 03:52:25 +01:00
|
|
|
<?php
|
2022-02-17 03:25:59 +01:00
|
|
|
|
2015-01-20 03:52:25 +01:00
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
|
|
|
* @copyright MMXIII Andreas Fischer
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 03:48:51 +01:00
|
|
|
namespace phpseclib3\Tests\Unit\Crypt;
|
|
|
|
|
2019-11-07 06:41:40 +01:00
|
|
|
use phpseclib3\Crypt\Twofish;
|
2022-02-23 03:48:51 +01:00
|
|
|
use phpseclib3\Tests\PhpseclibTestCase;
|
2015-01-20 03:52:25 +01:00
|
|
|
|
2022-02-23 03:48:51 +01:00
|
|
|
class TwofishTest extends PhpseclibTestCase
|
2015-01-20 03:52:25 +01:00
|
|
|
{
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testVectors(): void
|
2015-01-20 03:52:25 +01:00
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
$engines = [
|
2017-06-28 05:34:36 +02:00
|
|
|
'PHP',
|
|
|
|
'Eval',
|
|
|
|
'OpenSSL',
|
2017-11-27 09:30:14 +01:00
|
|
|
];
|
2015-01-20 03:52:25 +01:00
|
|
|
|
2017-06-28 05:34:36 +02:00
|
|
|
foreach ($engines as $engine) {
|
|
|
|
$tf = new Twofish('cbc');
|
2016-04-30 23:23:35 +02:00
|
|
|
$tf->setIV(str_repeat("\0", $tf->getBlockLength() >> 3));
|
2015-01-20 05:00:49 +01:00
|
|
|
$tf->disablePadding();
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
// tests from https://www.schneier.com/code/ecb_ival.txt
|
|
|
|
|
|
|
|
// key size = 128
|
|
|
|
$key = pack('H*', '00000000000000000000000000000000');
|
|
|
|
$tf->setKey($key);
|
2015-01-20 04:19:12 +01:00
|
|
|
if (!$tf->isValidEngine($engine)) {
|
2017-09-14 09:42:12 +02:00
|
|
|
self::markTestSkipped("Unable to initialize $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$plaintext = pack('H*', '00000000000000000000000000000000');
|
|
|
|
$ciphertext = $tf->encrypt($plaintext);
|
|
|
|
$expected = strtolower('9F589F5CF6122C32B6BFEC2F2AE8C35A');
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
$expected = bin2hex($plaintext);
|
|
|
|
$plaintext = bin2hex($tf->decrypt($ciphertext));
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
// key size = 192
|
|
|
|
$key = pack('H*', '0123456789ABCDEFFEDCBA98765432100011223344556677');
|
|
|
|
$tf->setKey($key);
|
2015-01-20 04:19:12 +01:00
|
|
|
if (!$tf->isValidEngine($engine)) {
|
2017-06-28 05:34:36 +02:00
|
|
|
self::markTestSkipped("Unable to initialize $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
}
|
|
|
|
$plaintext = pack('H*', '00000000000000000000000000000000');
|
|
|
|
$ciphertext = $tf->encrypt($plaintext);
|
|
|
|
$expected = strtolower('CFD1D2E5A9BE9CDF501F13B892BD2248');
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
$expected = bin2hex($plaintext);
|
|
|
|
$plaintext = bin2hex($tf->decrypt($ciphertext));
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
// key size = 256
|
|
|
|
$key = pack('H*', '0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF');
|
|
|
|
$tf->setKey($key);
|
2015-01-20 04:19:12 +01:00
|
|
|
if (!$tf->isValidEngine($engine)) {
|
2017-06-28 05:34:36 +02:00
|
|
|
self::markTestSkipped("Unable to initialize $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
}
|
|
|
|
$plaintext = pack('H*', '00000000000000000000000000000000');
|
|
|
|
$ciphertext = $tf->encrypt($plaintext);
|
|
|
|
$expected = strtolower('37527BE0052334B89F0CFCCAE87CFA20');
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
|
|
|
|
$expected = bin2hex($plaintext);
|
|
|
|
$plaintext = bin2hex($tf->decrypt($ciphertext));
|
2017-06-28 05:34:36 +02:00
|
|
|
$this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
|
2015-01-20 03:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|