Failed Conditions
Push — master ( 339400...89d9e2 )
by Florent
01:13
created

tests/Performance.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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