Performance   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 120
rs 10
c 0
b 0
f 0

5 Methods

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