1 | <?php |
||
14 | use Assert\Assertion; |
||
15 | |||
16 | final class AESGCM |
||
17 | { |
||
18 | /** |
||
19 | * @param string $K Key encryption key |
||
20 | * @param string $IV Initialization vector |
||
21 | * @param string $P Data to encrypt |
||
22 | * @param string $A Additional Authentication Data |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | public static function encrypt($K, $IV, $P, $A) |
||
39 | |||
40 | /** |
||
41 | * @param string $K Key encryption key |
||
42 | * @param string $IV Initialization vector |
||
43 | * @param string $C Data to encrypt |
||
44 | * @param string $A Additional Authentication Data |
||
45 | * @param string $T Tag |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public static function decrypt($K, $IV, $C, $A, $T) |
||
65 | |||
66 | /** |
||
67 | * @param $K |
||
68 | * @param $IV |
||
69 | * @param $A |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | private static function common($K, $IV, $A) |
||
97 | |||
98 | /** |
||
99 | * @param string $value |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | private static function calcVector($value) |
||
107 | |||
108 | /** |
||
109 | * @param string $value |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | private static function addPadding($value) |
||
117 | |||
118 | /** |
||
119 | * @param string $x |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | private static function getLength($x) |
||
127 | |||
128 | /** |
||
129 | * @param int $num_bits |
||
130 | * @param int $x |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | private static function getMSB($num_bits, $x) |
||
140 | |||
141 | /** |
||
142 | * @param int $num_bits |
||
143 | * @param int $x |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | private static function getLSB($num_bits, $x) |
||
153 | |||
154 | /** |
||
155 | * @param int $s_bits |
||
156 | * @param int $x |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | private static function getInc($s_bits, $x) |
||
168 | |||
169 | /** |
||
170 | * @param string $bin |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | private static function toUInt32Bits($bin) |
||
188 | |||
189 | /** |
||
190 | * @param $X |
||
191 | * @param $Y |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | private static function getProduct($X, $Y) |
||
218 | |||
219 | /** |
||
220 | * @param string $input |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | private static function shiftStringToRight($input) |
||
246 | |||
247 | /** |
||
248 | * @param string $H |
||
249 | * @param string $X |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | private static function getHash($H, $X) |
||
264 | |||
265 | /** |
||
266 | * @param string $K |
||
267 | * @param string $ICB |
||
268 | * @param string $X |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | private static function getGCTR($K, $ICB, $X) |
||
298 | |||
299 | /** |
||
300 | * @param string $o1 |
||
301 | * @param string $o2 |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | private static function getBitXor($o1, $o2) |
||
306 | { |
||
307 | $xorWidth = PHP_INT_SIZE; |
||
308 | $o1 = str_split($o1, $xorWidth); |
||
309 | $o2 = str_split($o2, $xorWidth); |
||
310 | $res = ''; |
||
311 | $runs = count($o1); |
||
312 | for ($i = 0; $i < $runs; $i++) { |
||
313 | $res .= $o1[$i] ^ $o2[$i]; |
||
314 | } |
||
315 | |||
316 | return $res; |
||
317 | } |
||
319 |