Passed
Pull Request — master (#14)
by Florent
06:01 queued 02:47
created

Performance::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AESKW\Tests;
6
7
use AESKW\A128KW;
8
use AESKW\A192KW;
9
use AESKW\A256KW;
10
use function call_user_func_array;
11
use InvalidArgumentException;
12
use const PHP_EOL;
13
14
/**
15
 * This class is used to check the performance of the library on the current platform. You just have to call
16
 * Performance::run(); By default, tests are performed 1000 times. You can modify it by passing a positive integer  as
17
 * first argument: Performance::run(10000);.
18
 */
19
final class Performance
20
{
21
    public static function run(int $nb = 1000): void
22
    {
23
        if ($nb < 1) {
24
            throw new InvalidArgumentException('You must perform at least 1 test.');
25
        }
26
        $cases = self::getData();
27
        foreach ($cases as $case) {
28
            self::wrap($nb, $case);
29
            self::unwrap($nb, $case);
30
        }
31
    }
32
33
    private static function wrap(int $nb, array $case): void
34
    {
35
        $class = $case['class'];
36
        $kek = $case['kek'];
37
        $data = $case['data'];
38
        $padding = $case['padding'];
39
        $time = self::do($class, 'wrap', $nb, $kek, $data, $padding);
40
41
        printf('%s: %f milliseconds/wrap' . PHP_EOL, $case['name'], $time);
42
    }
43
44
    private static function unwrap(int $nb, array $case): void
45
    {
46
        $class = $case['class'];
47
        $kek = $case['kek'];
48
        $result = $case['result'];
49
        $padding = $case['padding'];
50
        $time = self::do($class, 'unwrap', $nb, $kek, $result, $padding);
51
52
        printf('%s: %f milliseconds/unwrap' . PHP_EOL, $case['name'], $time);
53
    }
54
55
    /**
56
     * @param array ...$args
57
     */
58
    private static function do(string $class, string $method, int $nb, ...$args): float
59
    {
60
        $time_start = microtime(true);
61
        for ($i = 0; $i < $nb; ++$i) {
62
            call_user_func_array([$class, $method], $args);
63
        }
64
        $time_end = microtime(true);
65
66
        return ($time_end - $time_start) / $nb * 1000;
67
    }
68
69
    private static function getData(): array
70
    {
71
        return [
72
            [
73
                'class' => A128KW::class,
74
                'name' => 'RFC3394: 128 bits data with 128 bits KEK',
75
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F'),
76
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF'),
77
                'result' => hex2bin('1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5'),
78
                'padding' => false,
79
            ],
80
            [
81
                'class' => A192KW::class,
82
                'name' => 'RFC3394: 128 bits data with 192 bits KEK',
83
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617'),
84
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF'),
85
                'result' => hex2bin('96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D'),
86
                'padding' => false,
87
            ],
88
            [
89
                'class' => A256KW::class,
90
                'name' => 'RFC3394: 128 bits data with 256 bits KEK',
91
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'),
92
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF'),
93
                'result' => hex2bin('64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7'),
94
                'padding' => false,
95
            ],
96
            [
97
                'class' => A192KW::class,
98
                'name' => 'RFC3394: 192 bits data with 192 bits KEK',
99
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617'),
100
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF0001020304050607'),
101
                'result' => hex2bin('031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2'),
102
                'padding' => false,
103
            ],
104
            [
105
                'class' => A256KW::class,
106
                'name' => 'RFC3394: 192 bits data with 256 bits KEK',
107
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'),
108
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF0001020304050607'),
109
                'result' => hex2bin('A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1'),
110
                'padding' => false,
111
            ],
112
            [
113
                'class' => A256KW::class,
114
                'name' => 'RFC3394: 256 bits data with 256 bits KEK',
115
                'kek' => hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'),
116
                'data' => hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'),
117
                'result' => hex2bin('28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21'),
118
                'padding' => false,
119
            ],
120
            [
121
                'class' => A192KW::class,
122
                'name' => 'RFC5649 160 bits data with 192 bits KEK',
123
                'kek' => hex2bin('5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8'),
124
                'data' => hex2bin('c37b7e6492584340bed12207808941155068f738'),
125
                'result' => hex2bin('138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a'),
126
                'padding' => true,
127
            ],
128
            [
129
                'class' => A192KW::class,
130
                'name' => 'RFC5649 56 bits data with 192 bits KEK',
131
                'kek' => hex2bin('5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8'),
132
                'data' => hex2bin('466f7250617369'),
133
                'result' => hex2bin('afbeb0f07dfbf5419200f2ccb50bb24f'),
134
                'padding' => true,
135
            ],
136
        ];
137
    }
138
}
139