1 | <?php |
||
15 | class CryptToolTests extends \PHPUnit_Framework_TestCase { |
||
16 | |||
17 | /** |
||
18 | * test generating key pair |
||
19 | */ |
||
20 | public function testCreateKeyPair() { |
||
29 | |||
30 | /** |
||
31 | * test generating random nonce |
||
32 | */ |
||
33 | public function testRandomNonce() { |
||
39 | |||
40 | public function testDecrypt() { |
||
64 | |||
65 | public function testEncrypt() { |
||
66 | /** @noinspection PhpUnusedParameterInspection */ |
||
67 | $this->doTest(function(CryptTool $cryptTool, $prefix) { |
||
68 | $text = 'Dies ist eine Testnachricht. äöü'; |
||
69 | $nonce = '0a1ec5b67b4d61a1ef91f55e8ce0471fee96ea5d8596dfd0'; |
||
70 | |||
71 | $privateKey = Common::getPrivateKey(Constants::myPrivateKey); |
||
72 | $this->assertNotNull($privateKey); |
||
73 | |||
74 | $publicKey = Common::getPublicKey(Constants::otherPublicKey); |
||
75 | $this->assertNotNull($publicKey); |
||
76 | |||
77 | $message = $cryptTool->encryptMessageText($text, |
||
78 | $cryptTool->hex2bin($privateKey), |
||
79 | $cryptTool->hex2bin($publicKey), |
||
80 | $cryptTool->hex2bin($nonce)); |
||
81 | |||
82 | $this->assertNotNull($message); |
||
83 | |||
84 | $box = $cryptTool->decryptMessage($message, |
||
85 | $cryptTool->hex2bin(Common::getPrivateKey(Constants::otherPrivateKey)), |
||
86 | $cryptTool->hex2bin(Common::getPublicKey(Constants::myPublicKey)), |
||
87 | $cryptTool->hex2bin($nonce)); |
||
88 | |||
89 | $this->assertNotNull($box); |
||
90 | }); |
||
91 | } |
||
92 | |||
93 | |||
94 | public function testDerivePublicKey() { |
||
102 | |||
103 | public function testEncryptImage() { |
||
122 | |||
123 | /** |
||
124 | * test hex2bin and bin2hex |
||
125 | */ |
||
126 | public function testHexBin() { |
||
149 | |||
150 | /** |
||
151 | * test compare functions to make sure they are resistant to timing attacks |
||
152 | */ |
||
153 | public function testCompare() { |
||
154 | $this->doTest(function(CryptTool $cryptTool, $prefix) { |
||
155 | // make strings large enough |
||
156 | $string1 = str_repeat(Constants::myPrivateKey, 100000); |
||
157 | $string2 = str_repeat(Constants::otherPrivateKey, 100000); |
||
158 | echo PHP_EOL; |
||
159 | |||
160 | $humanDescr = [ |
||
161 | 'length' => 'different length', |
||
162 | 'diff' => 'same length, different content', |
||
163 | 'same' => 'same length, same content' |
||
164 | ]; |
||
165 | |||
166 | // test different strings when comparing and get time needed |
||
167 | $result = []; |
||
168 | foreach(array( |
||
169 | 'length' => [$string1, $string1 . 'a'], |
||
170 | 'diff' => [$string1, $string2], |
||
171 | 'same' => [$string1, $string1] |
||
172 | ) as $testName => $strings) { |
||
173 | $timeStart = microtime(true); |
||
174 | $comparisonResult = $cryptTool->stringCompare($strings[0], $strings[1]); |
||
175 | $timeEnd = microtime(true); |
||
176 | $timeElapsed = $timeEnd - $timeStart; |
||
177 | |||
178 | // echo $prefix.': '.$humanDescr[$testName].': '.$timeElapsed.'; result: '.$comparisonResult.PHP_EOL; |
||
179 | $result[$testName] = [$timeElapsed, $comparisonResult]; |
||
180 | |||
181 | // check result |
||
182 | if ($testName == 'length' || $testName == 'diff') { |
||
183 | $this->assertEquals($comparisonResult, false, $prefix.': comparison of "'.$humanDescr[$testName].'" is wrong: expected: false, got '.$comparisonResult); |
||
184 | } else { |
||
185 | $this->assertEquals($comparisonResult, true, $prefix.': comparison of "'.$humanDescr[$testName].'" is wrong: expected: true, got '.$comparisonResult); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | // check timings |
||
190 | echo 'Timing test results with '.$prefix.':'.PHP_EOL; |
||
191 | $timingRatio = 2 - ($result['diff'][0] / $result['same'][0]); |
||
192 | $absoluteDifference = abs($result['diff'][0] - $result['same'][0]); |
||
193 | echo 'timing ratio: '.$timingRatio.PHP_EOL; |
||
194 | echo 'absolute difference: '.$absoluteDifference.PHP_EOL; |
||
195 | |||
196 | // only allow 20% relative difference of two values |
||
197 | $allowedDifference = 0.20; |
||
198 | $this->assertLessThan(1+$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Ration: '.$timingRatio); |
||
199 | $this->assertGreaterThan(1-$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Ration: '.$timingRatio); |
||
200 | |||
201 | // make sure the absolute difference is smaller than 0.06 microseconds |
||
202 | $this->assertLessThan(0.6, $absoluteDifference, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Value is: '.$absoluteDifference.' micro seconds'); |
||
203 | }); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * test variable deletion |
||
208 | */ |
||
209 | public function testRemoveVar() { |
||
223 | |||
224 | private function doTest(\Closure $c) { |
||
239 | } |
||
240 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.