1<?php
2/**
3 * Copyright 2015-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category   Horde
9 * @copyright  2015-2016 Horde LLC
10 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package    Crypt_Blowfish
12 * @subpackage UnitTests
13 */
14
15/**
16 * Tests for PBKDF2.
17 *
18 * @author     Michael Slusarz <slusarz@horde.org>
19 * @category   Horde
20 * @copyright  2015-2016 Horde LLC
21 * @ignore
22 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
23 * @package    Crypt_Blowfish
24 * @subpackage UnitTests
25 */
26class Horde_Crypt_Blowfish_Pbkdf2Test extends Horde_Test_Case
27{
28    /**
29     * Test vectors.
30     *
31     * @dataProvider vectorsProvider
32     */
33    public function testVectors($expected, $algo, $pass, $salt, $iter, $klen)
34    {
35        $pbkdf2 = new Horde_Crypt_Blowfish_Pbkdf2($pass, $klen, array(
36            'algo' => $algo,
37            'i_count' => $iter,
38            'salt' => $salt
39        ));
40
41        $this->assertEquals(
42            $expected,
43            bin2hex($pbkdf2)
44        );
45
46        $this->assertEquals(
47            $algo,
48            $pbkdf2->hashAlgo
49        );
50
51        $this->assertEquals(
52            $iter,
53            $pbkdf2->iterations
54        );
55
56        $this->assertEquals(
57            $salt,
58            $pbkdf2->salt
59        );
60    }
61
62    public function vectorsProvider()
63    {
64        return array(
65            /* Begin: RFC 6070 Vectors */
66            array(
67                // Expected
68                '0c60c80f961f0e71f3a9b524af6012062fe037a6',
69                // Hash
70                'SHA1',
71                // Password
72                'password',
73                // Salt
74                'salt',
75                // Iterations
76                1,
77                // Key length
78                20
79            ),
80            array(
81                'ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957',
82                'SHA1',
83                'password',
84                'salt',
85                2,
86                20
87            ),
88            array(
89                '4b007901b765489abead49d926f721d065a429c1',
90                'SHA1',
91                'password',
92                'salt',
93                4096,
94                20
95            ),
96            /* Disable - 16 million iterations takes about 30 seconds on
97             * my dev machine so don't want to cause that kind of CPU load
98             * when doing automated testing.
99            array(
100                'eefe3d61cd4da4e4e9945b3d6ba2158c2634e984',
101                'SHA1',
102                'password',
103                'salt',
104                16777216,
105                20
106            ),
107            */
108            array(
109                '3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038',
110                'SHA1',
111                'passwordPASSWORDpassword',
112                'saltSALTsaltSALTsaltSALTsaltSALTsalt',
113                4096,
114                25
115            ),
116            array(
117                '56fa6aa75548099dcc37d7f03425e0c3',
118                'SHA1',
119                "pass\0word",
120                "sa\0lt",
121                4096,
122                16
123            ),
124            /* End: RFC 6070 Vectors */
125            array(
126                '3144c39857011a14e27d2b83e6c814f8f3dab70208aa1b4ffab1b7978599ffc3',
127                'SHA256',
128                'Password Password',
129                '123SaLt456',
130                16384,
131                32
132            ),
133            array(
134                '0a866b26a368f733d2bd95dc0acea6b544c38ba31bc357f527cf85e9f6a937bb',
135                'SHA512',
136                'Password Password',
137                '123SaLt456',
138                16384,
139                32
140            ),
141        );
142    }
143
144    public function testAutoSaltGeneration()
145    {
146        $pbkdf2 = new Horde_Crypt_Blowfish_Pbkdf2('password', 20);
147
148        $this->assertEquals(
149            20,
150            strlen($pbkdf2->salt)
151        );
152    }
153
154}
155