|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace Threema\MsgApi\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use Threema\Console\Common; |
|
12
|
|
|
use Threema\MsgApi\Tools\CryptTool; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Tests only valid for Sodium as they cannot be implemented correctly in the PHP-only version. |
|
16
|
|
|
*/ |
|
17
|
|
|
class CryptToolSodiumTests extends \PHPUnit_Framework_TestCase { |
|
18
|
|
|
/** @var Threema\MsgApi\Tools\CryptTool */ |
|
19
|
|
|
private $cryptTool; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize crypt tool. |
|
23
|
|
|
*/ |
|
24
|
|
|
function __construct() { |
|
|
|
|
|
|
25
|
|
|
$this->cryptTool = CryptTool::createInstance(CryptTool::TYPE_SODIUM); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* test hex2bin and bin2hex functions to make sure they are resistant to timing attacks |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testHexBin() { |
|
32
|
|
|
// // make strings large enough |
|
33
|
|
|
// $string1 = str_repeat(Constants::myPrivateKey, 100000); |
|
|
|
|
|
|
34
|
|
|
// $string2 = str_repeat(Constants::otherPrivateKey, 100000); |
|
|
|
|
|
|
35
|
|
|
// echo PHP_EOL; |
|
36
|
|
|
// |
|
37
|
|
|
// $humanDescr = [ |
|
38
|
|
|
// 'length' => 'different length', |
|
|
|
|
|
|
39
|
|
|
// 'diff' => 'same length, different content', |
|
|
|
|
|
|
40
|
|
|
// 'same' => 'same length, same content' |
|
|
|
|
|
|
41
|
|
|
// ]; |
|
42
|
|
|
// |
|
43
|
|
|
// // test different strings when comparing and get time needed |
|
44
|
|
|
// $result = []; |
|
|
|
|
|
|
45
|
|
|
// foreach(array( |
|
46
|
|
|
// 'length' => [$string1, $string1 . 'a'], |
|
|
|
|
|
|
47
|
|
|
// 'diff' => [$string1, $string2], |
|
|
|
|
|
|
48
|
|
|
// 'same' => [$string1, $string1] |
|
|
|
|
|
|
49
|
|
|
// ) as $testName => $strings) { |
|
|
|
|
|
|
50
|
|
|
// $timeStart = microtime(true); |
|
|
|
|
|
|
51
|
|
|
// $comparisonResult = $this->cryptTool->stringCompare($strings[0], $strings[1]); |
|
|
|
|
|
|
52
|
|
|
// $timeEnd = microtime(true); |
|
|
|
|
|
|
53
|
|
|
// $timeElapsed = $timeEnd - $timeStart; |
|
|
|
|
|
|
54
|
|
|
// |
|
55
|
|
|
// // echo $prefix.': '.$humanDescr[$testName].': '.$timeElapsed.'; result: '.$comparisonResult.PHP_EOL; |
|
56
|
|
|
// $result[$testName] = [$timeElapsed, $comparisonResult]; |
|
|
|
|
|
|
57
|
|
|
// |
|
58
|
|
|
// // check result |
|
59
|
|
|
// if ($testName == 'length' || $testName == 'diff') { |
|
|
|
|
|
|
60
|
|
|
// $this->assertEquals($comparisonResult, false, $prefix.': comparison of "'.$humanDescr[$testName].'" is wrong: expected: false, got '.$comparisonResult); |
|
|
|
|
|
|
61
|
|
|
// } else { |
|
|
|
|
|
|
62
|
|
|
// $this->assertEquals($comparisonResult, true, $prefix.': comparison of "'.$humanDescr[$testName].'" is wrong: expected: true, got '.$comparisonResult); |
|
|
|
|
|
|
63
|
|
|
// } |
|
64
|
|
|
// } |
|
65
|
|
|
// |
|
66
|
|
|
// // check timings |
|
67
|
|
|
// echo 'Timing test results with '.$prefix.':'.PHP_EOL; |
|
|
|
|
|
|
68
|
|
|
// $timingRatio = 2 - ($result['diff'][0] / $result['same'][0]); |
|
|
|
|
|
|
69
|
|
|
// $absoluteDifference = abs($result['diff'][0] - $result['same'][0]); |
|
|
|
|
|
|
70
|
|
|
// echo 'timing ratio: '.$timingRatio.PHP_EOL; |
|
|
|
|
|
|
71
|
|
|
// echo 'absolute difference: '.$absoluteDifference.PHP_EOL; |
|
|
|
|
|
|
72
|
|
|
// |
|
73
|
|
|
// // only allow 10% relative difference of two values |
|
74
|
|
|
// $allowedDifference = 0.10; |
|
|
|
|
|
|
75
|
|
|
// $this->assertLessThan(1+$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Ration: '.$timingRatio); |
|
|
|
|
|
|
76
|
|
|
// $this->assertGreaterThan(1-$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Ration: '.$timingRatio); |
|
|
|
|
|
|
77
|
|
|
// |
|
78
|
|
|
// // make sure the absolute difference is smaller than 0.05 microseconds |
|
79
|
|
|
// $this->assertLessThan(0.05, $absoluteDifference, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Value is: '.$absoluteDifference.' micro seconds'); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.