File: TwofishTest.php

package info (click to toggle)
php-phpseclib3 3.0.46-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,528 kB
  • sloc: php: 30,756; xml: 392; makefile: 21
file content (79 lines) | stat: -rw-r--r-- 3,367 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

/**
 * @author    Andreas Fischer <bantu@phpbb.com>
 * @copyright MMXIII Andreas Fischer
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 */

namespace phpseclib3\Tests\Unit\Crypt;

use phpseclib3\Crypt\Twofish;
use phpseclib3\Tests\PhpseclibTestCase;

class TwofishTest extends PhpseclibTestCase
{
    public function testVectors()
    {
        $engines = [
            'PHP',
            'Eval',
            'mcrypt',
            'OpenSSL',
        ];

        foreach ($engines as $engine) {
            $tf = new Twofish('cbc');
            $tf->setIV(str_repeat("\0", $tf->getBlockLength() >> 3));
            $tf->disablePadding();

            // tests from https://www.schneier.com/code/ecb_ival.txt

            // key size = 128
            $key = pack('H*', '00000000000000000000000000000000');
            $tf->setKey($key);
            if (!$tf->isValidEngine($engine)) {
                self::markTestSkipped("Unable to initialize $engine engine");
            }

            $plaintext = pack('H*', '00000000000000000000000000000000');
            $ciphertext = $tf->encrypt($plaintext);
            $expected = strtolower('9F589F5CF6122C32B6BFEC2F2AE8C35A');
            $this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");

            $expected = bin2hex($plaintext);
            $plaintext = bin2hex($tf->decrypt($ciphertext));
            $this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");

            // key size = 192
            $key = pack('H*', '0123456789ABCDEFFEDCBA98765432100011223344556677');
            $tf->setKey($key);
            if (!$tf->isValidEngine($engine)) {
                self::markTestSkipped("Unable to initialize $engine engine");
            }
            $plaintext = pack('H*', '00000000000000000000000000000000');
            $ciphertext = $tf->encrypt($plaintext);
            $expected = strtolower('CFD1D2E5A9BE9CDF501F13B892BD2248');
            $this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");

            $expected = bin2hex($plaintext);
            $plaintext = bin2hex($tf->decrypt($ciphertext));
            $this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");

            // key size = 256
            $key = pack('H*', '0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF');
            $tf->setKey($key);
            if (!$tf->isValidEngine($engine)) {
                self::markTestSkipped("Unable to initialize $engine engine");
            }
            $plaintext = pack('H*', '00000000000000000000000000000000');
            $ciphertext = $tf->encrypt($plaintext);
            $expected = strtolower('37527BE0052334B89F0CFCCAE87CFA20');
            $this->assertEquals(bin2hex($ciphertext), $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");

            $expected = bin2hex($plaintext);
            $plaintext = bin2hex($tf->decrypt($ciphertext));
            $this->assertEquals($plaintext, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
        }
    }
}