Failed Conditions
Push — master ( 072e31...fa6436 )
by Florent
10s
created

Performance::unwrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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