1 | <?php |
||
16 | final class AESGCM |
||
17 | { |
||
18 | /** |
||
19 | * @param string $K Key encryption key |
||
20 | * @param string $IV Initialization vector |
||
21 | * @param null|string $P Data to encrypt (null for authentication) |
||
22 | * @param null|string $A Additional Authentication Data |
||
23 | * @param int $tag_length Tag length |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | public static function encrypt($K, $IV, $P = null, $A = null, $tag_length = 128) |
||
46 | |||
47 | /** |
||
48 | * This method will append the tag at the end of the ciphertext. |
||
49 | * |
||
50 | * @param string $K Key encryption key |
||
51 | * @param string $IV Initialization vector |
||
52 | * @param null|string $P Data to encrypt (null for authentication) |
||
53 | * @param null|string $A Additional Authentication Data |
||
54 | * @param int $tag_length Tag length |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public static function encryptAndAppendTag($K, $IV, $P = null, $A = null, $tag_length = 128) |
||
62 | |||
63 | /** |
||
64 | * @param string $K Key encryption key |
||
65 | * @param string $IV Initialization vector |
||
66 | * @param string|null $C Data to encrypt (null for authentication) |
||
67 | * @param string|null $A Additional Authentication Data |
||
68 | * @param string $T Tag |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public static function decrypt($K, $IV, $C = null, $A = null, $T) |
||
96 | |||
97 | /** |
||
98 | * This method should be used if the tag is appended at the end of the ciphertext. |
||
99 | * It is used by some AES GCM implementations such as the Java one. |
||
100 | * |
||
101 | * @param string $K Key encryption key |
||
102 | * @param string $IV Initialization vector |
||
103 | * @param string|null $Ciphertext Data to encrypt (null for authentication) |
||
104 | * @param string|null $A Additional Authentication Data |
||
105 | * @param int $tag_length Tag length |
||
106 | * |
||
107 | * @return string |
||
108 | * |
||
109 | * @see self::encryptAndAppendTag |
||
110 | */ |
||
111 | public static function decryptWithAppendedTag($K, $IV, $Ciphertext = null, $A = null, $tag_length = 128) |
||
119 | |||
120 | /** |
||
121 | * @param $K |
||
122 | * @param $IV |
||
123 | * @param $A |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | private static function common($K, $IV, $A) |
||
151 | |||
152 | /** |
||
153 | * @param string $value |
||
154 | * |
||
155 | * @return int |
||
156 | */ |
||
157 | private static function calcVector($value) |
||
161 | |||
162 | /** |
||
163 | * @param string $value |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | private static function addPadding($value) |
||
171 | |||
172 | /** |
||
173 | * @param string $x |
||
174 | * |
||
175 | * @return int |
||
176 | */ |
||
177 | private static function getLength($x) |
||
181 | |||
182 | /** |
||
183 | * @param int $num_bits |
||
184 | * @param int $x |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | private static function getMSB($num_bits, $x) |
||
194 | |||
195 | /** |
||
196 | * @param int $num_bits |
||
197 | * @param int $x |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | private static function getLSB($num_bits, $x) |
||
207 | |||
208 | /** |
||
209 | * @param int $s_bits |
||
210 | * @param int $x |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | private static function getInc($s_bits, $x) |
||
222 | |||
223 | /** |
||
224 | * @param string $bin |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | private static function toUInt32Bits($bin) |
||
234 | |||
235 | /** |
||
236 | * @param $X |
||
237 | * @param $Y |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | private static function getProduct($X, $Y) |
||
264 | |||
265 | /** |
||
266 | * @param string $input |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | private static function shiftStringToRight($input) |
||
292 | |||
293 | /** |
||
294 | * @param string $H |
||
295 | * @param string $X |
||
296 | * |
||
297 | * @return mixed |
||
298 | */ |
||
299 | private static function getHash($H, $X) |
||
310 | |||
311 | /** |
||
312 | * @param string $K |
||
313 | * @param string $ICB |
||
314 | * @param string $X |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | private static function getGCTR($K, $ICB, $X) |
||
344 | |||
345 | /** |
||
346 | * @param string $o1 |
||
347 | * @param string $o2 |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | private static function getBitXor($o1, $o2) |
||
364 | } |
||
365 |