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
|
|
|
$testStrSmall = Constants::myPrivateKeyExtract; |
34
|
|
|
$testStrLong = Constants::myPublicKeyExtract; |
35
|
|
|
echo PHP_EOL; |
36
|
|
|
|
37
|
|
|
// test different strings when comparing and get time needed |
38
|
|
|
$result = []; |
39
|
|
|
foreach(array( |
40
|
|
|
'short' => $testStrSmall, |
41
|
|
|
'long' => $testStrLong |
42
|
|
|
) as $testName => $testString) { |
43
|
|
|
$timeStart = microtime(true); |
44
|
|
|
$conResultBin = $this->cryptTool->hex2bin($testString); |
45
|
|
|
$conResultHex = $this->cryptTool->bin2hex($conResultBin); |
46
|
|
|
$timeEnd = microtime(true); |
47
|
|
|
$timeElapsed = $timeEnd - $timeStart; |
48
|
|
|
|
49
|
|
|
echo $testName.': '.$timeElapsed.PHP_EOL; |
50
|
|
|
$result[$testName] = [$timeElapsed, $conResultBin, $conResultHex]; |
51
|
|
|
|
52
|
|
|
// check result |
53
|
|
|
$this->assertEquals(hex2bin($testString), $conResultBin, $testName.': hex2bin returns different result than PHP-only implementation'); |
54
|
|
|
$this->assertEquals($testString, $conResultHex, $testName.': hex string differs from original string after conversion'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// check timings |
58
|
|
|
$timingRatio = 2 - ($result['short'][0] / $result['long'][0]); |
59
|
|
|
$absoluteDifference = abs($result['short'][0] - $result['long'][0]); |
60
|
|
|
echo 'timing ratio: '.$timingRatio.PHP_EOL; |
61
|
|
|
echo 'absolute difference: '.$absoluteDifference.PHP_EOL; |
62
|
|
|
|
63
|
|
|
// only allow 10% relative difference of two values |
64
|
|
|
$allowedDifference = 0.10; |
|
|
|
|
65
|
|
|
// $this->assertLessThan(1+$allowedDifference, $timingRatio, 'difference of conversion ration of "short" compared to "long" is too high. Ration: '.$timingRatio); |
|
|
|
|
66
|
|
|
// $this->assertGreaterThan(1-$allowedDifference, $timingRatio, 'difference of conversion ration of "short" compared to "long" is too high. Ration: '.$timingRatio); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// make sure the absolute difference is smaller than 0.05 microseconds |
69
|
|
|
$this->assertLessThan(0.05, $absoluteDifference, 'difference of conversion ration of "short" compared to "long" is too high. Value is: '.$absoluteDifference.' micro seconds'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.