@@ -38,619 +38,619 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_CAST_256 extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 16; // 128 bits; |
|
43 | - |
|
44 | - //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | - |
|
46 | - /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | - const BYTES_KEY_MAX = 32; |
|
48 | - |
|
49 | - /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | - private static $_s1 = array(); |
|
51 | - |
|
52 | - /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | - private static $_s2 = array(); |
|
54 | - |
|
55 | - /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | - private static $_s3 = array(); |
|
57 | - |
|
58 | - /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | - private static $_s4 = array(); |
|
60 | - |
|
61 | - private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | - |
|
63 | - /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | - private $_mkey = array(); |
|
65 | - |
|
66 | - /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | - private $_rkey = array(); |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Constructor |
|
72 | - * |
|
73 | - * @param string $key The key used for Encryption/Decryption |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function __construct($key) |
|
77 | - { |
|
78 | - $keylen = strlen($key); |
|
79 | - |
|
80 | - if ($keylen > self::BYTES_KEY_MAX) |
|
81 | - { |
|
82 | - $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | - $keylen = self::BYTES_KEY_MAX; |
|
84 | - } |
|
85 | - else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | - { |
|
87 | - $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | - $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | - trigger_error($msg, E_USER_WARNING); |
|
90 | - } |
|
91 | - |
|
92 | - // set the key, make sure the required length is set in bytes |
|
93 | - parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | - |
|
95 | - // set the block size |
|
96 | - $this->blockSize(self::BYTES_BLOCK); |
|
97 | - |
|
98 | - // initialize the sboxes constants |
|
99 | - $this->initTables(); |
|
100 | - |
|
101 | - // create the sub keys using the sboxes |
|
102 | - $this->createSubKeys(); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Destructor |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function __destruct() |
|
112 | - { |
|
113 | - parent::__destruct(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Encrypt plain text data |
|
119 | - * |
|
120 | - * @param string $data A 128 bit block of plain data |
|
121 | - * @return boolean Returns true |
|
122 | - */ |
|
123 | - public function encrypt(&$data) |
|
124 | - { |
|
125 | - $this->operation(parent::ENCRYPT); |
|
126 | - |
|
127 | - // first split the data into four 32 bit blocks, reverse |
|
128 | - // the string order of each block, convert the blocks of data to integers |
|
129 | - $data = str_split($data, 4); |
|
130 | - $data = array_map("strrev", $data); |
|
131 | - $data = array_map("parent::str2Dec", $data); |
|
132 | - |
|
133 | - // do the first 6 loops |
|
134 | - for ($i = 0; $i < 6; ++$i) |
|
135 | - { |
|
136 | - |
|
137 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | - } |
|
142 | - |
|
143 | - // the second 6 loops are done in a different order |
|
144 | - for ($i = 6; $i < 12; ++$i) |
|
145 | - { |
|
146 | - |
|
147 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | - } |
|
152 | - |
|
153 | - // convert the decimals back to a string, reverse the string so it's |
|
154 | - // in the correct order |
|
155 | - $data = array_map(function($v) { |
|
156 | - $v = Core::dec2Str($v, 4); |
|
157 | - return strrev($v); |
|
158 | - }, $data); |
|
159 | - |
|
160 | - // glue the string back together |
|
161 | - $data = implode("", $data); |
|
162 | - |
|
163 | - return true; |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | - * but in reverse. |
|
170 | - * |
|
171 | - * @param string $data A 128 bit block of encrypted data |
|
172 | - * @return boolean Returns true |
|
173 | - */ |
|
174 | - public function decrypt(&$data) |
|
175 | - { |
|
176 | - $this->operation(parent::DECRYPT); |
|
177 | - |
|
178 | - // first split the data into four 32 bit blocks, reverse |
|
179 | - // the string order of each block, convert the blocks of data to integers |
|
180 | - $data = str_split($data, 4); |
|
181 | - $data = array_map("strrev", $data); |
|
182 | - $data = array_map("parent::str2Dec", $data); |
|
183 | - |
|
184 | - // do the first 6 loops |
|
185 | - for ($i = 11; $i >= 6; --$i) |
|
186 | - { |
|
187 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | - } |
|
192 | - |
|
193 | - // the second 6 loops are done in a different order |
|
194 | - for ($i = 5; $i >= 0; --$i) |
|
195 | - { |
|
196 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | - } |
|
201 | - |
|
202 | - // convert the decimals back to a string, reverse the string so it's |
|
203 | - // in the correct order |
|
204 | - $data = array_map(function($v) { |
|
205 | - $v = Core::dec2Str($v, 4); |
|
206 | - return strrev($v); |
|
207 | - }, $data); |
|
208 | - |
|
209 | - // glue the string back together |
|
210 | - $data = implode("", $data); |
|
211 | - |
|
212 | - return true; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * CAST-256 F1 function |
|
218 | - * |
|
219 | - * @param $d integer The the data input |
|
220 | - * @param $m integer The 32 bit masking key |
|
221 | - * @param $r integer The round number |
|
222 | - * @return integer The value after the F1 calculation |
|
223 | - */ |
|
224 | - private function f1($d, $m, $r) |
|
225 | - { |
|
226 | - $n = parent::uInt32($m + $d); |
|
227 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | - $n = parent::dec2Str($n, 4); |
|
229 | - |
|
230 | - return parent::uInt32( |
|
231 | - ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | - self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * CAST-256 F2 function |
|
239 | - * |
|
240 | - * @param integer $d integer The the data input |
|
241 | - * @param $m integer The 32 bit masking key |
|
242 | - * @param $r integer The round number |
|
243 | - * @return integer The value after the F2 calculation |
|
244 | - */ |
|
245 | - private function f2($d, $m, $r) |
|
246 | - { |
|
247 | - $n = parent::uInt32($m ^ $d); |
|
248 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | - $n = parent::dec2Str($n, 4); |
|
250 | - |
|
251 | - return parent::uInt32( |
|
252 | - ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | - self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | - ); |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * CAST-256 F3 function |
|
260 | - * |
|
261 | - * @param integer $d integer The the data input |
|
262 | - * @param $m integer The 32 bit masking key |
|
263 | - * @param $r integer The round number |
|
264 | - * @return integer The value after the F3 calculation |
|
265 | - */ |
|
266 | - private function f3($d, $m, $r) |
|
267 | - { |
|
268 | - $n = parent::uInt32($m - $d); |
|
269 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | - $n = parent::dec2Str($n, 4); |
|
271 | - |
|
272 | - return parent::uInt32( |
|
273 | - ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | - self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | - ); |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * Creates the subkeys $_mkey (the masking key) and |
|
281 | - * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | - * created from the original key. The original key is null |
|
283 | - * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | - * split in half to create $_mkey and $_rkey |
|
285 | - * |
|
286 | - * @return void |
|
287 | - */ |
|
288 | - private function createSubKeys() |
|
289 | - { |
|
290 | - $cm = 0x5A827999; |
|
291 | - $mm = 0x6ED9EBA1; |
|
292 | - $cr = 19; |
|
293 | - $mr = 17; |
|
294 | - $tm = array(); |
|
295 | - $tr = array(); |
|
296 | - $xkey = $this->key(); |
|
297 | - $tmpkey = array(); |
|
298 | - |
|
299 | - // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | - // for the key expansion |
|
301 | - if ($this->keySize() < 32) |
|
302 | - $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | - |
|
304 | - // split the key up into 4 byte parts, reverse the string, |
|
305 | - // then convert each part into a 32 bit integer |
|
306 | - $xkey = str_split($xkey, 4); |
|
307 | - $xkey = array_map("strrev", $xkey); |
|
308 | - $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | - |
|
310 | - // set up the values need for creating round and masking keys |
|
311 | - for ($i = 0; $i < 24; ++$i) |
|
312 | - { |
|
313 | - $tm[$i] = array(); |
|
314 | - $tr[$i] = array(); |
|
315 | - |
|
316 | - for ($j = 0; $j < 8; ++$j) |
|
317 | - { |
|
318 | - $tm[$i][$j] = $cm; |
|
319 | - $cm = parent::uInt32($cm + $mm); |
|
320 | - $tr[$i][$j] = $cr; |
|
321 | - $cr = parent::uInt32($cr + $mr); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - // now create the round and masking keys |
|
326 | - for ($i = 0; $i < 12; ++$i) |
|
327 | - { |
|
328 | - $j = 2 * $i; |
|
329 | - |
|
330 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | - |
|
339 | - $j = (2 * $i) + 1; |
|
340 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | - |
|
349 | - // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | - // to the round key |
|
351 | - $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | - $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | - $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | - $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | - |
|
356 | - // now create 32 byte masking keys |
|
357 | - $this->_mkey[$i][0] = $xkey[7]; |
|
358 | - $this->_mkey[$i][1] = $xkey[5]; |
|
359 | - $this->_mkey[$i][2] = $xkey[3]; |
|
360 | - $this->_mkey[$i][3] = $xkey[1]; |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Initialize the tables |
|
367 | - * |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - private function initTables() |
|
371 | - { |
|
372 | - // 256 unsigned 32 bit integers |
|
373 | - self::$_s1 = array( |
|
374 | - 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | - 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | - 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | - 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | - 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | - 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | - 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | - 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | - 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | - 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | - 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | - 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | - 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | - 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | - 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | - 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | - 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | - 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | - 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | - 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | - 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | - 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | - 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | - 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | - 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | - 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | - 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | - 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | - 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | - 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | - 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | - 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | - 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | - 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | - 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | - 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | - 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | - 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | - 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | - 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | - 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | - 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | - 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | - 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | - 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | - 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | - 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | - 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | - 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | - 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | - 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | - 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | - 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | - 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | - 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | - 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | - 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | - 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | - 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | - 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | - 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | - 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | - 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | - 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | - ); |
|
439 | - |
|
440 | - // 256 unsigned 32 bit integers |
|
441 | - self::$_s2 = array( |
|
442 | - 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | - 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | - 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | - 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | - 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | - 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | - 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | - 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | - 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | - 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | - 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | - 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | - 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | - 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | - 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | - 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | - 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | - 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | - 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | - 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | - 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | - 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | - 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | - 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | - 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | - 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | - 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | - 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | - 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | - 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | - 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | - 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | - 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | - 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | - 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | - 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | - 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | - 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | - 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | - 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | - 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | - 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | - 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | - 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | - 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | - 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | - 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | - 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | - 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | - 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | - 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | - 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | - 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | - 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | - 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | - 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | - 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | - 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | - 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | - 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | - 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | - 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | - 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | - 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | - ); |
|
507 | - |
|
508 | - // 256 unsigned 32 bit integers |
|
509 | - self::$_s3 = array( |
|
510 | - 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | - 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | - 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | - 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | - 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | - 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | - 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | - 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | - 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | - 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | - 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | - 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | - 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | - 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | - 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | - 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | - 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | - 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | - 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | - 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | - 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | - 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | - 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | - 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | - 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | - 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | - 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | - 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | - 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | - 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | - 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | - 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | - 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | - 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | - 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | - 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | - 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | - 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | - 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | - 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | - 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | - 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | - 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | - 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | - 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | - 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | - 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | - 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | - 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | - 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | - 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | - 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | - 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | - 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | - 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | - 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | - 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | - 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | - 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | - 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | - 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | - 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | - 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | - 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | - ); |
|
575 | - |
|
576 | - // 256 unsigned 32 bit integers |
|
577 | - self::$_s4 = array( |
|
578 | - 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | - 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | - 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | - 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | - 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | - 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | - 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | - 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | - 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | - 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | - 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | - 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | - 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | - 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | - 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | - 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | - 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | - 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | - 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | - 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | - 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | - 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | - 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | - 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | - 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | - 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | - 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | - 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | - 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | - 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | - 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | - 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | - 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | - 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | - 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | - 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | - 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | - 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | - 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | - 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | - 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | - 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | - 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | - 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | - 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | - 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | - 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | - 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | - 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | - 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | - 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | - 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | - 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | - 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | - 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | - 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | - 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | - 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | - 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | - 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | - 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | - 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | - 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | - 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * Indicates this is a block cipher |
|
648 | - * |
|
649 | - * @return integer Returns Cipher::BLOCK |
|
650 | - */ |
|
651 | - public function type() |
|
652 | - { |
|
653 | - return parent::BLOCK; |
|
654 | - } |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 16; // 128 bits; |
|
43 | + |
|
44 | + //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | + |
|
46 | + /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | + const BYTES_KEY_MAX = 32; |
|
48 | + |
|
49 | + /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | + private static $_s1 = array(); |
|
51 | + |
|
52 | + /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | + private static $_s2 = array(); |
|
54 | + |
|
55 | + /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | + private static $_s3 = array(); |
|
57 | + |
|
58 | + /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | + private static $_s4 = array(); |
|
60 | + |
|
61 | + private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | + |
|
63 | + /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | + private $_mkey = array(); |
|
65 | + |
|
66 | + /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | + private $_rkey = array(); |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Constructor |
|
72 | + * |
|
73 | + * @param string $key The key used for Encryption/Decryption |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function __construct($key) |
|
77 | + { |
|
78 | + $keylen = strlen($key); |
|
79 | + |
|
80 | + if ($keylen > self::BYTES_KEY_MAX) |
|
81 | + { |
|
82 | + $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | + $keylen = self::BYTES_KEY_MAX; |
|
84 | + } |
|
85 | + else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | + { |
|
87 | + $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | + $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | + trigger_error($msg, E_USER_WARNING); |
|
90 | + } |
|
91 | + |
|
92 | + // set the key, make sure the required length is set in bytes |
|
93 | + parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | + |
|
95 | + // set the block size |
|
96 | + $this->blockSize(self::BYTES_BLOCK); |
|
97 | + |
|
98 | + // initialize the sboxes constants |
|
99 | + $this->initTables(); |
|
100 | + |
|
101 | + // create the sub keys using the sboxes |
|
102 | + $this->createSubKeys(); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Destructor |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function __destruct() |
|
112 | + { |
|
113 | + parent::__destruct(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Encrypt plain text data |
|
119 | + * |
|
120 | + * @param string $data A 128 bit block of plain data |
|
121 | + * @return boolean Returns true |
|
122 | + */ |
|
123 | + public function encrypt(&$data) |
|
124 | + { |
|
125 | + $this->operation(parent::ENCRYPT); |
|
126 | + |
|
127 | + // first split the data into four 32 bit blocks, reverse |
|
128 | + // the string order of each block, convert the blocks of data to integers |
|
129 | + $data = str_split($data, 4); |
|
130 | + $data = array_map("strrev", $data); |
|
131 | + $data = array_map("parent::str2Dec", $data); |
|
132 | + |
|
133 | + // do the first 6 loops |
|
134 | + for ($i = 0; $i < 6; ++$i) |
|
135 | + { |
|
136 | + |
|
137 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | + } |
|
142 | + |
|
143 | + // the second 6 loops are done in a different order |
|
144 | + for ($i = 6; $i < 12; ++$i) |
|
145 | + { |
|
146 | + |
|
147 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | + } |
|
152 | + |
|
153 | + // convert the decimals back to a string, reverse the string so it's |
|
154 | + // in the correct order |
|
155 | + $data = array_map(function($v) { |
|
156 | + $v = Core::dec2Str($v, 4); |
|
157 | + return strrev($v); |
|
158 | + }, $data); |
|
159 | + |
|
160 | + // glue the string back together |
|
161 | + $data = implode("", $data); |
|
162 | + |
|
163 | + return true; |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | + * but in reverse. |
|
170 | + * |
|
171 | + * @param string $data A 128 bit block of encrypted data |
|
172 | + * @return boolean Returns true |
|
173 | + */ |
|
174 | + public function decrypt(&$data) |
|
175 | + { |
|
176 | + $this->operation(parent::DECRYPT); |
|
177 | + |
|
178 | + // first split the data into four 32 bit blocks, reverse |
|
179 | + // the string order of each block, convert the blocks of data to integers |
|
180 | + $data = str_split($data, 4); |
|
181 | + $data = array_map("strrev", $data); |
|
182 | + $data = array_map("parent::str2Dec", $data); |
|
183 | + |
|
184 | + // do the first 6 loops |
|
185 | + for ($i = 11; $i >= 6; --$i) |
|
186 | + { |
|
187 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | + } |
|
192 | + |
|
193 | + // the second 6 loops are done in a different order |
|
194 | + for ($i = 5; $i >= 0; --$i) |
|
195 | + { |
|
196 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | + } |
|
201 | + |
|
202 | + // convert the decimals back to a string, reverse the string so it's |
|
203 | + // in the correct order |
|
204 | + $data = array_map(function($v) { |
|
205 | + $v = Core::dec2Str($v, 4); |
|
206 | + return strrev($v); |
|
207 | + }, $data); |
|
208 | + |
|
209 | + // glue the string back together |
|
210 | + $data = implode("", $data); |
|
211 | + |
|
212 | + return true; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * CAST-256 F1 function |
|
218 | + * |
|
219 | + * @param $d integer The the data input |
|
220 | + * @param $m integer The 32 bit masking key |
|
221 | + * @param $r integer The round number |
|
222 | + * @return integer The value after the F1 calculation |
|
223 | + */ |
|
224 | + private function f1($d, $m, $r) |
|
225 | + { |
|
226 | + $n = parent::uInt32($m + $d); |
|
227 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | + $n = parent::dec2Str($n, 4); |
|
229 | + |
|
230 | + return parent::uInt32( |
|
231 | + ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | + self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * CAST-256 F2 function |
|
239 | + * |
|
240 | + * @param integer $d integer The the data input |
|
241 | + * @param $m integer The 32 bit masking key |
|
242 | + * @param $r integer The round number |
|
243 | + * @return integer The value after the F2 calculation |
|
244 | + */ |
|
245 | + private function f2($d, $m, $r) |
|
246 | + { |
|
247 | + $n = parent::uInt32($m ^ $d); |
|
248 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | + $n = parent::dec2Str($n, 4); |
|
250 | + |
|
251 | + return parent::uInt32( |
|
252 | + ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | + self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | + ); |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * CAST-256 F3 function |
|
260 | + * |
|
261 | + * @param integer $d integer The the data input |
|
262 | + * @param $m integer The 32 bit masking key |
|
263 | + * @param $r integer The round number |
|
264 | + * @return integer The value after the F3 calculation |
|
265 | + */ |
|
266 | + private function f3($d, $m, $r) |
|
267 | + { |
|
268 | + $n = parent::uInt32($m - $d); |
|
269 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | + $n = parent::dec2Str($n, 4); |
|
271 | + |
|
272 | + return parent::uInt32( |
|
273 | + ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | + self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | + ); |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * Creates the subkeys $_mkey (the masking key) and |
|
281 | + * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | + * created from the original key. The original key is null |
|
283 | + * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | + * split in half to create $_mkey and $_rkey |
|
285 | + * |
|
286 | + * @return void |
|
287 | + */ |
|
288 | + private function createSubKeys() |
|
289 | + { |
|
290 | + $cm = 0x5A827999; |
|
291 | + $mm = 0x6ED9EBA1; |
|
292 | + $cr = 19; |
|
293 | + $mr = 17; |
|
294 | + $tm = array(); |
|
295 | + $tr = array(); |
|
296 | + $xkey = $this->key(); |
|
297 | + $tmpkey = array(); |
|
298 | + |
|
299 | + // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | + // for the key expansion |
|
301 | + if ($this->keySize() < 32) |
|
302 | + $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | + |
|
304 | + // split the key up into 4 byte parts, reverse the string, |
|
305 | + // then convert each part into a 32 bit integer |
|
306 | + $xkey = str_split($xkey, 4); |
|
307 | + $xkey = array_map("strrev", $xkey); |
|
308 | + $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | + |
|
310 | + // set up the values need for creating round and masking keys |
|
311 | + for ($i = 0; $i < 24; ++$i) |
|
312 | + { |
|
313 | + $tm[$i] = array(); |
|
314 | + $tr[$i] = array(); |
|
315 | + |
|
316 | + for ($j = 0; $j < 8; ++$j) |
|
317 | + { |
|
318 | + $tm[$i][$j] = $cm; |
|
319 | + $cm = parent::uInt32($cm + $mm); |
|
320 | + $tr[$i][$j] = $cr; |
|
321 | + $cr = parent::uInt32($cr + $mr); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + // now create the round and masking keys |
|
326 | + for ($i = 0; $i < 12; ++$i) |
|
327 | + { |
|
328 | + $j = 2 * $i; |
|
329 | + |
|
330 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | + |
|
339 | + $j = (2 * $i) + 1; |
|
340 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | + |
|
349 | + // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | + // to the round key |
|
351 | + $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | + $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | + $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | + $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | + |
|
356 | + // now create 32 byte masking keys |
|
357 | + $this->_mkey[$i][0] = $xkey[7]; |
|
358 | + $this->_mkey[$i][1] = $xkey[5]; |
|
359 | + $this->_mkey[$i][2] = $xkey[3]; |
|
360 | + $this->_mkey[$i][3] = $xkey[1]; |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Initialize the tables |
|
367 | + * |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + private function initTables() |
|
371 | + { |
|
372 | + // 256 unsigned 32 bit integers |
|
373 | + self::$_s1 = array( |
|
374 | + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | + ); |
|
439 | + |
|
440 | + // 256 unsigned 32 bit integers |
|
441 | + self::$_s2 = array( |
|
442 | + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | + ); |
|
507 | + |
|
508 | + // 256 unsigned 32 bit integers |
|
509 | + self::$_s3 = array( |
|
510 | + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | + ); |
|
575 | + |
|
576 | + // 256 unsigned 32 bit integers |
|
577 | + self::$_s4 = array( |
|
578 | + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | + ); |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + /** |
|
647 | + * Indicates this is a block cipher |
|
648 | + * |
|
649 | + * @return integer Returns Cipher::BLOCK |
|
650 | + */ |
|
651 | + public function type() |
|
652 | + { |
|
653 | + return parent::BLOCK; |
|
654 | + } |
|
655 | 655 | } |
656 | 656 | ?> |
@@ -38,556 +38,556 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_DES extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
42 | - const BYTES_BLOCK = 8; // 64 bits |
|
41 | + /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
42 | + const BYTES_BLOCK = 8; // 64 bits |
|
43 | 43 | |
44 | - /** @type integer BYTES_KEY The key size, in bytes */ |
|
45 | - const BYTES_KEY = 8; // 64 bits |
|
44 | + /** @type integer BYTES_KEY The key size, in bytes */ |
|
45 | + const BYTES_KEY = 8; // 64 bits |
|
46 | 46 | |
47 | - /** @type array $sub_keys The permutated subkeys */ |
|
48 | - protected $sub_keys = array(); |
|
47 | + /** @type array $sub_keys The permutated subkeys */ |
|
48 | + protected $sub_keys = array(); |
|
49 | 49 | |
50 | - /* |
|
50 | + /* |
|
51 | 51 | * Tables initialized in the initTables() |
52 | 52 | */ |
53 | 53 | |
54 | - /** |
|
55 | - * @type array $_pc1 Permutated choice 1 (PC1), |
|
56 | - * This should be considered a constant |
|
57 | - */ |
|
58 | - protected static $_pc1 = array(); |
|
59 | - |
|
60 | - /** |
|
61 | - * @type array $_pc2 Permutated choice 2 (PC2), |
|
62 | - * This should be considered a constant |
|
63 | - */ |
|
64 | - protected static $_pc2 = array(); |
|
65 | - |
|
66 | - /** |
|
67 | - * @type array $_key_sched The key schedule, |
|
68 | - * This should be considered a constant |
|
69 | - */ |
|
70 | - protected static $_key_sched = array(); |
|
71 | - |
|
72 | - /** |
|
73 | - * @type array $_ip The Initial Permutation (IP), |
|
74 | - * This should be considered a constant |
|
75 | - */ |
|
76 | - private static $_ip = array(); |
|
77 | - |
|
78 | - /** |
|
79 | - * @type array $_e The Expansion table (E), |
|
80 | - * This should be considered a constant |
|
81 | - */ |
|
82 | - private static $_e = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * @type array $_s The Substitution Box (S), |
|
86 | - * This should be considered a constant |
|
87 | - */ |
|
88 | - private static $_s = array(); |
|
89 | - |
|
90 | - /** |
|
91 | - * @type array $_p The Permutation table (P), |
|
92 | - * This should be considered a constant |
|
93 | - */ |
|
94 | - private static $_p = array(); |
|
95 | - |
|
96 | - /** |
|
97 | - * @type array $_ip The The Final Permutation table (FP), |
|
98 | - * This should be considered a constant |
|
99 | - */ |
|
100 | - private static $_fp = array(); |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Constructor, used only when calling this class directly |
|
105 | - * for classes that extend this class, call __construct1() |
|
106 | - * |
|
107 | - * @param string $key The key used for Encryption/Decryption |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - public function __construct($key) |
|
111 | - { |
|
112 | - // set the DES key |
|
113 | - parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
114 | - |
|
115 | - // initialize variables |
|
116 | - $this->initTables(); |
|
117 | - |
|
118 | - // DES requires that data is 64 bits |
|
119 | - $this->blockSize(self::BYTES_BLOCK); |
|
120 | - |
|
121 | - // create the 16 rounds of 56 bit keys |
|
122 | - $this->keyPermutation(); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Second Constructor, used only by child classes that extend this class |
|
128 | - * |
|
129 | - * @param string $cipher The name of the cipher extending this class |
|
130 | - * @param string $key The key used for Encryption/Decryption |
|
131 | - * @param integer $key_byte_sz The required byte size of the extending cipher |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - protected function __construct1($cipher, $key, $key_byte_sz) |
|
135 | - { |
|
136 | - // set the key and key size |
|
137 | - parent::__construct($cipher, $key, $key_byte_sz); |
|
138 | - |
|
139 | - // initialize variables |
|
140 | - $this->initTables(); |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * Destructor |
|
146 | - * |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function __destruct() |
|
150 | - { |
|
151 | - parent::__destruct(); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Encrypt plain text data using DES |
|
157 | - * |
|
158 | - * @return boolean Returns true |
|
159 | - */ |
|
160 | - public function encrypt(&$text) |
|
161 | - { |
|
162 | - $this->operation(parent::ENCRYPT); |
|
163 | - return $this->des($text); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Decrypt a DES encrypted string |
|
169 | - * |
|
170 | - * @return boolean Returns true |
|
171 | - */ |
|
172 | - public function decrypt(&$text) |
|
173 | - { |
|
174 | - $this->operation(parent::DECRYPT); |
|
175 | - return $this->des($text); |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * This is where the actual encrypt/decryption takes place. Since |
|
181 | - * encryption and decryption are the same algorithm in DES, we only |
|
182 | - * need one function to do both. |
|
183 | - * |
|
184 | - * @param string $data The string to be encrypted or decrypted |
|
185 | - * @return boolean Returns true |
|
186 | - */ |
|
187 | - protected function des(&$data) |
|
188 | - { |
|
189 | - $l = array(); |
|
190 | - $r = array(); |
|
191 | - |
|
192 | - // step two: Initial Permutation (IP) of plaintext |
|
193 | - $data = $this->ip($data); |
|
194 | - |
|
195 | - // divide the permuted block IP into a left half L0 of 32 bits, |
|
196 | - // and a right half R0 of 32 bits |
|
197 | - $l[0] = substr($data, 0, 32); |
|
198 | - $r[0] = substr($data, 32, 32); |
|
199 | - |
|
200 | - for ($n = 1; $n <= 16; ++$n) |
|
201 | - { |
|
202 | - $l[$n] = $r[$n - 1]; |
|
203 | - |
|
204 | - if ($this->operation() == parent::DECRYPT) |
|
205 | - $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
206 | - else |
|
207 | - $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
208 | - |
|
209 | - // XOR F with Ln |
|
210 | - $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
211 | - } |
|
212 | - |
|
213 | - // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
214 | - // L[16] and R[16] so that it becomes R[16]L[16] |
|
215 | - $data = $r[16].$l[16]; |
|
216 | - |
|
217 | - // now do the final permutation |
|
218 | - $data = $this->fp($data); |
|
219 | - $data = parent::bin2Str($data); |
|
220 | - |
|
221 | - return true; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * The Key permutation, based on tables $_pc1 and $_pc2 |
|
227 | - * Create 16 subkeys, each of which is 48-bits long. |
|
228 | - * |
|
229 | - * @return void |
|
230 | - */ |
|
231 | - private function keyPermutation() |
|
232 | - { |
|
233 | - $this->sub_keys = array(); |
|
234 | - $pc1m = array(); |
|
235 | - $pcr = array(); |
|
236 | - $c = array(); |
|
237 | - $d = array(); |
|
238 | - |
|
239 | - // convert the key to binary |
|
240 | - $binkey = parent::str2Bin($this->key()); |
|
241 | - |
|
242 | - // reduce the key down to 56bits based on table $_pc1 |
|
243 | - for ($i = 0; $i < 56; ++$i) |
|
54 | + /** |
|
55 | + * @type array $_pc1 Permutated choice 1 (PC1), |
|
56 | + * This should be considered a constant |
|
57 | + */ |
|
58 | + protected static $_pc1 = array(); |
|
59 | + |
|
60 | + /** |
|
61 | + * @type array $_pc2 Permutated choice 2 (PC2), |
|
62 | + * This should be considered a constant |
|
63 | + */ |
|
64 | + protected static $_pc2 = array(); |
|
65 | + |
|
66 | + /** |
|
67 | + * @type array $_key_sched The key schedule, |
|
68 | + * This should be considered a constant |
|
69 | + */ |
|
70 | + protected static $_key_sched = array(); |
|
71 | + |
|
72 | + /** |
|
73 | + * @type array $_ip The Initial Permutation (IP), |
|
74 | + * This should be considered a constant |
|
75 | + */ |
|
76 | + private static $_ip = array(); |
|
77 | + |
|
78 | + /** |
|
79 | + * @type array $_e The Expansion table (E), |
|
80 | + * This should be considered a constant |
|
81 | + */ |
|
82 | + private static $_e = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * @type array $_s The Substitution Box (S), |
|
86 | + * This should be considered a constant |
|
87 | + */ |
|
88 | + private static $_s = array(); |
|
89 | + |
|
90 | + /** |
|
91 | + * @type array $_p The Permutation table (P), |
|
92 | + * This should be considered a constant |
|
93 | + */ |
|
94 | + private static $_p = array(); |
|
95 | + |
|
96 | + /** |
|
97 | + * @type array $_ip The The Final Permutation table (FP), |
|
98 | + * This should be considered a constant |
|
99 | + */ |
|
100 | + private static $_fp = array(); |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Constructor, used only when calling this class directly |
|
105 | + * for classes that extend this class, call __construct1() |
|
106 | + * |
|
107 | + * @param string $key The key used for Encryption/Decryption |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + public function __construct($key) |
|
111 | + { |
|
112 | + // set the DES key |
|
113 | + parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
114 | + |
|
115 | + // initialize variables |
|
116 | + $this->initTables(); |
|
117 | + |
|
118 | + // DES requires that data is 64 bits |
|
119 | + $this->blockSize(self::BYTES_BLOCK); |
|
120 | + |
|
121 | + // create the 16 rounds of 56 bit keys |
|
122 | + $this->keyPermutation(); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Second Constructor, used only by child classes that extend this class |
|
128 | + * |
|
129 | + * @param string $cipher The name of the cipher extending this class |
|
130 | + * @param string $key The key used for Encryption/Decryption |
|
131 | + * @param integer $key_byte_sz The required byte size of the extending cipher |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + protected function __construct1($cipher, $key, $key_byte_sz) |
|
135 | + { |
|
136 | + // set the key and key size |
|
137 | + parent::__construct($cipher, $key, $key_byte_sz); |
|
138 | + |
|
139 | + // initialize variables |
|
140 | + $this->initTables(); |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * Destructor |
|
146 | + * |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function __destruct() |
|
150 | + { |
|
151 | + parent::__destruct(); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Encrypt plain text data using DES |
|
157 | + * |
|
158 | + * @return boolean Returns true |
|
159 | + */ |
|
160 | + public function encrypt(&$text) |
|
161 | + { |
|
162 | + $this->operation(parent::ENCRYPT); |
|
163 | + return $this->des($text); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Decrypt a DES encrypted string |
|
169 | + * |
|
170 | + * @return boolean Returns true |
|
171 | + */ |
|
172 | + public function decrypt(&$text) |
|
173 | + { |
|
174 | + $this->operation(parent::DECRYPT); |
|
175 | + return $this->des($text); |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * This is where the actual encrypt/decryption takes place. Since |
|
181 | + * encryption and decryption are the same algorithm in DES, we only |
|
182 | + * need one function to do both. |
|
183 | + * |
|
184 | + * @param string $data The string to be encrypted or decrypted |
|
185 | + * @return boolean Returns true |
|
186 | + */ |
|
187 | + protected function des(&$data) |
|
188 | + { |
|
189 | + $l = array(); |
|
190 | + $r = array(); |
|
191 | + |
|
192 | + // step two: Initial Permutation (IP) of plaintext |
|
193 | + $data = $this->ip($data); |
|
194 | + |
|
195 | + // divide the permuted block IP into a left half L0 of 32 bits, |
|
196 | + // and a right half R0 of 32 bits |
|
197 | + $l[0] = substr($data, 0, 32); |
|
198 | + $r[0] = substr($data, 32, 32); |
|
199 | + |
|
200 | + for ($n = 1; $n <= 16; ++$n) |
|
201 | + { |
|
202 | + $l[$n] = $r[$n - 1]; |
|
203 | + |
|
204 | + if ($this->operation() == parent::DECRYPT) |
|
205 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
206 | + else |
|
207 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
208 | + |
|
209 | + // XOR F with Ln |
|
210 | + $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
211 | + } |
|
212 | + |
|
213 | + // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
214 | + // L[16] and R[16] so that it becomes R[16]L[16] |
|
215 | + $data = $r[16].$l[16]; |
|
216 | + |
|
217 | + // now do the final permutation |
|
218 | + $data = $this->fp($data); |
|
219 | + $data = parent::bin2Str($data); |
|
220 | + |
|
221 | + return true; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * The Key permutation, based on tables $_pc1 and $_pc2 |
|
227 | + * Create 16 subkeys, each of which is 48-bits long. |
|
228 | + * |
|
229 | + * @return void |
|
230 | + */ |
|
231 | + private function keyPermutation() |
|
232 | + { |
|
233 | + $this->sub_keys = array(); |
|
234 | + $pc1m = array(); |
|
235 | + $pcr = array(); |
|
236 | + $c = array(); |
|
237 | + $d = array(); |
|
238 | + |
|
239 | + // convert the key to binary |
|
240 | + $binkey = parent::str2Bin($this->key()); |
|
241 | + |
|
242 | + // reduce the key down to 56bits based on table $_pc1 |
|
243 | + for ($i = 0; $i < 56; ++$i) |
|
244 | 244 | $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
245 | 245 | |
246 | - // split $pc1m in half (C0 and D0) |
|
247 | - $c[0] = array_slice($pc1m, 0, 28); |
|
248 | - $d[0] = array_slice($pc1m, 28, 28); |
|
249 | - |
|
250 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
251 | - // where 1 <= n <= 16 |
|
252 | - for ($i = 1; $i <= 16; ++$i) |
|
253 | - { |
|
254 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
255 | - $c[$i] = $c[$i - 1]; |
|
256 | - $d[$i] = $d[$i - 1]; |
|
257 | - |
|
258 | - for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
259 | - { |
|
260 | - // do a left shift, move each bit one place to the left, |
|
261 | - // except for the first bit, which is cycled to the end |
|
262 | - // of the block. |
|
263 | - $c[$i][] = array_shift($c[$i]); |
|
264 | - $d[$i][] = array_shift($d[$i]); |
|
265 | - } |
|
266 | - |
|
267 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
268 | - // following permutation table to each of the concatenated |
|
269 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
270 | - // of these. |
|
271 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
272 | - $this->sub_keys[$i - 1] = ""; |
|
273 | - for ($j = 0; $j < 48; ++$j) |
|
274 | - $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
275 | - } |
|
276 | - |
|
277 | - // the sub_keys are created, we are done with the key permutation |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Initial Permutation (IP) |
|
283 | - * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
284 | - * the 64 bits of the message data M. This rearranges the bits according to the |
|
285 | - * following table, where the entries in the table show the new arrangement of the |
|
286 | - * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
287 | - * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
288 | - * bit of IP. |
|
289 | - * |
|
290 | - * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
291 | - * The initial permution was used to make it easier to load plain text and cipher text |
|
292 | - * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
293 | - * are not necessary in software implementations and do not affect the security. However, |
|
294 | - * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
295 | - * the standard, so we will do it here in phpCrypt. |
|
296 | - * |
|
297 | - * @param string $text |
|
298 | - * @return string the Initial Permutation (IP) |
|
299 | - */ |
|
300 | - private function ip($text) |
|
301 | - { |
|
302 | - $text = parent::str2Bin($text); |
|
303 | - $ip = ""; |
|
304 | - |
|
305 | - // loop through the 64 bit block, ordering it occording to $_ip |
|
306 | - for ($i = 0; $i < 64; ++$i) |
|
307 | - $ip .= $text[self::$_ip[$i] - 1]; |
|
308 | - |
|
309 | - return $ip; |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
315 | - * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
316 | - * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
317 | - * block, and a 48 bit output block. |
|
318 | - * |
|
319 | - * @param string $r 32 bit binary, each bit in an array element |
|
320 | - * @param string $k 48 bit binary string |
|
321 | - * @return string 48 bit binary string |
|
322 | - */ |
|
323 | - private function f($r, $k) |
|
324 | - { |
|
325 | - $bin = parent::xorBin($k, $this->E($r)); |
|
326 | - |
|
327 | - // create a 32-bit string from $bits by passing it through the S-Boxes |
|
328 | - $bin = $this->s($bin); |
|
329 | - |
|
330 | - // now send permute $bin as defined by table self::$_p |
|
331 | - $bin = $this->p($bin); |
|
332 | - |
|
333 | - return $bin; |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
339 | - * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
340 | - * to the self::$_e[] table. |
|
341 | - * This is only used in the F() function |
|
342 | - * |
|
343 | - * @param array $r 32 bit binary, each bit in an array element |
|
344 | - * @return string 48 bit binary string |
|
345 | - */ |
|
346 | - private function e($r) |
|
347 | - { |
|
348 | - $e = ""; |
|
349 | - for ($i = 0; $i < 48; ++$i) |
|
350 | - $e .= $r[self::$_e[$i] - 1]; |
|
351 | - |
|
352 | - return $e; |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * S-Box |
|
358 | - * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
359 | - * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
360 | - * through the S-Boxes |
|
361 | - * |
|
362 | - * @param string $bits The 48-bit string from F() to be processed |
|
363 | - * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
364 | - */ |
|
365 | - private function s($bits) |
|
366 | - { |
|
367 | - $s = ""; |
|
368 | - |
|
369 | - for ($i = 0; $i <= 42; $i += 6) |
|
370 | - { |
|
371 | - $sbits = substr($bits, $i, 6); |
|
372 | - |
|
373 | - // we need to determine the S-Box column number and row number |
|
374 | - // from the 6 bit string passed in, this is done using the following method: |
|
375 | - // The First & Last bits represent a number between 0-3, used to determine which row |
|
376 | - // The middle 4 bits represent a number between 0-15, used to determine the column |
|
377 | - $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
378 | - $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
379 | - |
|
380 | - // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
381 | - $pos = ($row * 16) + $col; |
|
382 | - |
|
383 | - // get the integer from the S-Box and convert it to binary |
|
384 | - $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
385 | - $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
386 | - } |
|
387 | - |
|
388 | - return $s; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Permutation P |
|
394 | - * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
395 | - * from a 32-bit input from a binary string from the S-BOX by permuting |
|
396 | - * the bits of the input block. |
|
397 | - * This is only used inside of F() function |
|
398 | - * |
|
399 | - * @param string $s A 32-bit string originating from being passed through S-Box |
|
400 | - * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
401 | - */ |
|
402 | - private function p($s) |
|
403 | - { |
|
404 | - $p = ""; |
|
405 | - for ($i = 0; $i < 32; ++$i) |
|
406 | - $p .= $s[self::$_p[$i] - 1]; |
|
407 | - |
|
408 | - return $p; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * Final Permutation (FP) |
|
414 | - * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
415 | - * we will do it to follow the DES standard). |
|
416 | - * |
|
417 | - * @param string $bin A 64-bit binary string |
|
418 | - * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
419 | - */ |
|
420 | - private function fp($bin) |
|
421 | - { |
|
422 | - $fp = ""; |
|
423 | - for ($i = 0; $i < 64; ++$i) |
|
424 | - $fp .= $bin[self::$_fp[$i] - 1]; |
|
425 | - |
|
426 | - return $fp; |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * Initialize all the tables, this function is called inside the constructor |
|
432 | - * |
|
433 | - * @return void |
|
434 | - */ |
|
435 | - private function initTables() |
|
436 | - { |
|
437 | - // permuted choice 1 (PC1) |
|
438 | - // these values are chars and should be run through chr() when used |
|
439 | - self::$_pc1 = array( |
|
440 | - 57, 49, 41, 33, 25, 17, 9, |
|
441 | - 1, 58, 50, 42, 34, 26, 18, |
|
442 | - 10, 2, 59, 51, 43, 35, 27, |
|
443 | - 19, 11, 3, 60, 52, 44, 36, |
|
444 | - 63, 55, 47, 39, 31, 23, 15, |
|
445 | - 7, 62, 54, 46, 38, 30, 22, |
|
446 | - 14, 6, 61, 53, 45, 37, 29, |
|
447 | - 21, 13, 5, 28, 20, 12, 4 |
|
448 | - ); |
|
449 | - |
|
450 | - // permuted choice 2 (PC2) |
|
451 | - // these values are chars and should be run through chr() when used |
|
452 | - self::$_pc2 = array( |
|
453 | - 14, 17, 11, 24, 1, 5, |
|
454 | - 3, 28, 15, 6, 21, 10, |
|
455 | - 23, 19, 12, 4, 26, 8, |
|
456 | - 16, 7, 27, 20, 13, 2, |
|
457 | - 41, 52, 31, 37, 47, 55, |
|
458 | - 30, 40, 51, 45, 33, 48, |
|
459 | - 44, 49, 39, 56, 34, 53, |
|
460 | - 46, 42, 50, 36, 29, 32 |
|
461 | - ); |
|
462 | - |
|
463 | - // initial permutation (IP) |
|
464 | - self::$_ip = array( |
|
465 | - 58, 50, 42, 34, 26, 18, 10, 2, |
|
466 | - 60, 52, 44, 36, 28, 20, 12, 4, |
|
467 | - 62, 54, 46, 38, 30, 22, 14, 6, |
|
468 | - 64, 56, 48, 40, 32, 24, 16, 8, |
|
469 | - 57, 49, 41, 33, 25, 17, 9, 1, |
|
470 | - 59, 51, 43, 35, 27, 19, 11, 3, |
|
471 | - 61, 53, 45, 37, 29, 21, 13, 5, |
|
472 | - 63, 55, 47, 39, 31, 23, 15, 7 |
|
473 | - ); |
|
474 | - |
|
475 | - // expansion (E) |
|
476 | - self::$_e = array( |
|
477 | - 32, 1, 2, 3, 4, 5, |
|
478 | - 4, 5, 6, 7, 8, 9, |
|
479 | - 8, 9, 10, 11, 12, 13, |
|
480 | - 12, 13, 14, 15, 16, 17, |
|
481 | - 16, 17, 18, 19, 20, 21, |
|
482 | - 20, 21, 22, 23, 24, 25, |
|
483 | - 24, 25, 26, 27, 28, 29, |
|
484 | - 28, 29, 30, 31, 32, 1 |
|
485 | - ); |
|
486 | - |
|
487 | - // substition box (S) |
|
488 | - self::$_s = array( |
|
489 | - /* S1 */ |
|
490 | - array( |
|
491 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
492 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
493 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
494 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
495 | - ), |
|
496 | - |
|
497 | - /* S2 */ |
|
498 | - array( |
|
499 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
500 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
501 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
502 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
503 | - ), |
|
504 | - |
|
505 | - /* S3 */ |
|
506 | - array( |
|
507 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
508 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
509 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
510 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
511 | - ), |
|
512 | - |
|
513 | - /* S4 */ |
|
514 | - array( |
|
515 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
516 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
517 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
518 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
519 | - ), |
|
520 | - |
|
521 | - /* S5 */ |
|
522 | - array( |
|
523 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
524 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
525 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
526 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
527 | - ), |
|
528 | - |
|
529 | - /* S6 */ |
|
530 | - array( |
|
531 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
532 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
533 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
534 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
535 | - ), |
|
536 | - |
|
537 | - /* S7 */ |
|
538 | - array( |
|
539 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
540 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
541 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
542 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
543 | - ), |
|
544 | - |
|
545 | - /* S8 */ |
|
546 | - array( |
|
547 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
548 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
549 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
550 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
551 | - ) |
|
552 | - ); |
|
553 | - |
|
554 | - // permutation (P) |
|
555 | - self::$_p = array( |
|
556 | - 16, 7, 20, 21, |
|
557 | - 29, 12, 28, 17, |
|
558 | - 1, 15, 23, 26, |
|
559 | - 5, 18, 31, 10, |
|
560 | - 2, 8, 24, 14, |
|
561 | - 32, 27, 3, 9, |
|
562 | - 19, 13, 30, 6, |
|
563 | - 22, 11, 4, 25 |
|
564 | - ); |
|
565 | - |
|
566 | - // final permutation (FP) |
|
567 | - self::$_fp = array( |
|
568 | - 40, 8, 48, 16, 56, 24, 64, 32, |
|
569 | - 39, 7, 47, 15, 55, 23, 63, 31, |
|
570 | - 38, 6, 46, 14, 54, 22, 62, 30, |
|
571 | - 37, 5, 45, 13, 53, 21, 61, 29, |
|
572 | - 36, 4, 44, 12, 52, 20, 60, 28, |
|
573 | - 35, 3, 43, 11, 51, 19, 59, 27, |
|
574 | - 34, 2, 42, 10, 50, 18, 58, 26, |
|
575 | - 33, 1, 41, 9, 49, 17, 57, 25 |
|
576 | - ); |
|
577 | - |
|
578 | - // key schedule used in KeyPermutation() |
|
579 | - self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
580 | - } |
|
581 | - |
|
582 | - |
|
583 | - /** |
|
584 | - * Indicates this is a block cipher |
|
585 | - * |
|
586 | - * @return integer Returns Cipher::BLOCK |
|
587 | - */ |
|
588 | - public function type() |
|
589 | - { |
|
590 | - return parent::BLOCK; |
|
591 | - } |
|
246 | + // split $pc1m in half (C0 and D0) |
|
247 | + $c[0] = array_slice($pc1m, 0, 28); |
|
248 | + $d[0] = array_slice($pc1m, 28, 28); |
|
249 | + |
|
250 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
251 | + // where 1 <= n <= 16 |
|
252 | + for ($i = 1; $i <= 16; ++$i) |
|
253 | + { |
|
254 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
255 | + $c[$i] = $c[$i - 1]; |
|
256 | + $d[$i] = $d[$i - 1]; |
|
257 | + |
|
258 | + for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
259 | + { |
|
260 | + // do a left shift, move each bit one place to the left, |
|
261 | + // except for the first bit, which is cycled to the end |
|
262 | + // of the block. |
|
263 | + $c[$i][] = array_shift($c[$i]); |
|
264 | + $d[$i][] = array_shift($d[$i]); |
|
265 | + } |
|
266 | + |
|
267 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
268 | + // following permutation table to each of the concatenated |
|
269 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
270 | + // of these. |
|
271 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
272 | + $this->sub_keys[$i - 1] = ""; |
|
273 | + for ($j = 0; $j < 48; ++$j) |
|
274 | + $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
275 | + } |
|
276 | + |
|
277 | + // the sub_keys are created, we are done with the key permutation |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Initial Permutation (IP) |
|
283 | + * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
284 | + * the 64 bits of the message data M. This rearranges the bits according to the |
|
285 | + * following table, where the entries in the table show the new arrangement of the |
|
286 | + * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
287 | + * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
288 | + * bit of IP. |
|
289 | + * |
|
290 | + * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
291 | + * The initial permution was used to make it easier to load plain text and cipher text |
|
292 | + * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
293 | + * are not necessary in software implementations and do not affect the security. However, |
|
294 | + * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
295 | + * the standard, so we will do it here in phpCrypt. |
|
296 | + * |
|
297 | + * @param string $text |
|
298 | + * @return string the Initial Permutation (IP) |
|
299 | + */ |
|
300 | + private function ip($text) |
|
301 | + { |
|
302 | + $text = parent::str2Bin($text); |
|
303 | + $ip = ""; |
|
304 | + |
|
305 | + // loop through the 64 bit block, ordering it occording to $_ip |
|
306 | + for ($i = 0; $i < 64; ++$i) |
|
307 | + $ip .= $text[self::$_ip[$i] - 1]; |
|
308 | + |
|
309 | + return $ip; |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
315 | + * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
316 | + * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
317 | + * block, and a 48 bit output block. |
|
318 | + * |
|
319 | + * @param string $r 32 bit binary, each bit in an array element |
|
320 | + * @param string $k 48 bit binary string |
|
321 | + * @return string 48 bit binary string |
|
322 | + */ |
|
323 | + private function f($r, $k) |
|
324 | + { |
|
325 | + $bin = parent::xorBin($k, $this->E($r)); |
|
326 | + |
|
327 | + // create a 32-bit string from $bits by passing it through the S-Boxes |
|
328 | + $bin = $this->s($bin); |
|
329 | + |
|
330 | + // now send permute $bin as defined by table self::$_p |
|
331 | + $bin = $this->p($bin); |
|
332 | + |
|
333 | + return $bin; |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
339 | + * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
340 | + * to the self::$_e[] table. |
|
341 | + * This is only used in the F() function |
|
342 | + * |
|
343 | + * @param array $r 32 bit binary, each bit in an array element |
|
344 | + * @return string 48 bit binary string |
|
345 | + */ |
|
346 | + private function e($r) |
|
347 | + { |
|
348 | + $e = ""; |
|
349 | + for ($i = 0; $i < 48; ++$i) |
|
350 | + $e .= $r[self::$_e[$i] - 1]; |
|
351 | + |
|
352 | + return $e; |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * S-Box |
|
358 | + * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
359 | + * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
360 | + * through the S-Boxes |
|
361 | + * |
|
362 | + * @param string $bits The 48-bit string from F() to be processed |
|
363 | + * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
364 | + */ |
|
365 | + private function s($bits) |
|
366 | + { |
|
367 | + $s = ""; |
|
368 | + |
|
369 | + for ($i = 0; $i <= 42; $i += 6) |
|
370 | + { |
|
371 | + $sbits = substr($bits, $i, 6); |
|
372 | + |
|
373 | + // we need to determine the S-Box column number and row number |
|
374 | + // from the 6 bit string passed in, this is done using the following method: |
|
375 | + // The First & Last bits represent a number between 0-3, used to determine which row |
|
376 | + // The middle 4 bits represent a number between 0-15, used to determine the column |
|
377 | + $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
378 | + $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
379 | + |
|
380 | + // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
381 | + $pos = ($row * 16) + $col; |
|
382 | + |
|
383 | + // get the integer from the S-Box and convert it to binary |
|
384 | + $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
385 | + $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
386 | + } |
|
387 | + |
|
388 | + return $s; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Permutation P |
|
394 | + * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
395 | + * from a 32-bit input from a binary string from the S-BOX by permuting |
|
396 | + * the bits of the input block. |
|
397 | + * This is only used inside of F() function |
|
398 | + * |
|
399 | + * @param string $s A 32-bit string originating from being passed through S-Box |
|
400 | + * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
401 | + */ |
|
402 | + private function p($s) |
|
403 | + { |
|
404 | + $p = ""; |
|
405 | + for ($i = 0; $i < 32; ++$i) |
|
406 | + $p .= $s[self::$_p[$i] - 1]; |
|
407 | + |
|
408 | + return $p; |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + /** |
|
413 | + * Final Permutation (FP) |
|
414 | + * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
415 | + * we will do it to follow the DES standard). |
|
416 | + * |
|
417 | + * @param string $bin A 64-bit binary string |
|
418 | + * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
419 | + */ |
|
420 | + private function fp($bin) |
|
421 | + { |
|
422 | + $fp = ""; |
|
423 | + for ($i = 0; $i < 64; ++$i) |
|
424 | + $fp .= $bin[self::$_fp[$i] - 1]; |
|
425 | + |
|
426 | + return $fp; |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * Initialize all the tables, this function is called inside the constructor |
|
432 | + * |
|
433 | + * @return void |
|
434 | + */ |
|
435 | + private function initTables() |
|
436 | + { |
|
437 | + // permuted choice 1 (PC1) |
|
438 | + // these values are chars and should be run through chr() when used |
|
439 | + self::$_pc1 = array( |
|
440 | + 57, 49, 41, 33, 25, 17, 9, |
|
441 | + 1, 58, 50, 42, 34, 26, 18, |
|
442 | + 10, 2, 59, 51, 43, 35, 27, |
|
443 | + 19, 11, 3, 60, 52, 44, 36, |
|
444 | + 63, 55, 47, 39, 31, 23, 15, |
|
445 | + 7, 62, 54, 46, 38, 30, 22, |
|
446 | + 14, 6, 61, 53, 45, 37, 29, |
|
447 | + 21, 13, 5, 28, 20, 12, 4 |
|
448 | + ); |
|
449 | + |
|
450 | + // permuted choice 2 (PC2) |
|
451 | + // these values are chars and should be run through chr() when used |
|
452 | + self::$_pc2 = array( |
|
453 | + 14, 17, 11, 24, 1, 5, |
|
454 | + 3, 28, 15, 6, 21, 10, |
|
455 | + 23, 19, 12, 4, 26, 8, |
|
456 | + 16, 7, 27, 20, 13, 2, |
|
457 | + 41, 52, 31, 37, 47, 55, |
|
458 | + 30, 40, 51, 45, 33, 48, |
|
459 | + 44, 49, 39, 56, 34, 53, |
|
460 | + 46, 42, 50, 36, 29, 32 |
|
461 | + ); |
|
462 | + |
|
463 | + // initial permutation (IP) |
|
464 | + self::$_ip = array( |
|
465 | + 58, 50, 42, 34, 26, 18, 10, 2, |
|
466 | + 60, 52, 44, 36, 28, 20, 12, 4, |
|
467 | + 62, 54, 46, 38, 30, 22, 14, 6, |
|
468 | + 64, 56, 48, 40, 32, 24, 16, 8, |
|
469 | + 57, 49, 41, 33, 25, 17, 9, 1, |
|
470 | + 59, 51, 43, 35, 27, 19, 11, 3, |
|
471 | + 61, 53, 45, 37, 29, 21, 13, 5, |
|
472 | + 63, 55, 47, 39, 31, 23, 15, 7 |
|
473 | + ); |
|
474 | + |
|
475 | + // expansion (E) |
|
476 | + self::$_e = array( |
|
477 | + 32, 1, 2, 3, 4, 5, |
|
478 | + 4, 5, 6, 7, 8, 9, |
|
479 | + 8, 9, 10, 11, 12, 13, |
|
480 | + 12, 13, 14, 15, 16, 17, |
|
481 | + 16, 17, 18, 19, 20, 21, |
|
482 | + 20, 21, 22, 23, 24, 25, |
|
483 | + 24, 25, 26, 27, 28, 29, |
|
484 | + 28, 29, 30, 31, 32, 1 |
|
485 | + ); |
|
486 | + |
|
487 | + // substition box (S) |
|
488 | + self::$_s = array( |
|
489 | + /* S1 */ |
|
490 | + array( |
|
491 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
492 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
493 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
494 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
495 | + ), |
|
496 | + |
|
497 | + /* S2 */ |
|
498 | + array( |
|
499 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
500 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
501 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
502 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
503 | + ), |
|
504 | + |
|
505 | + /* S3 */ |
|
506 | + array( |
|
507 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
508 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
509 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
510 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
511 | + ), |
|
512 | + |
|
513 | + /* S4 */ |
|
514 | + array( |
|
515 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
516 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
517 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
518 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
519 | + ), |
|
520 | + |
|
521 | + /* S5 */ |
|
522 | + array( |
|
523 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
524 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
525 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
526 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
527 | + ), |
|
528 | + |
|
529 | + /* S6 */ |
|
530 | + array( |
|
531 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
532 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
533 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
534 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
535 | + ), |
|
536 | + |
|
537 | + /* S7 */ |
|
538 | + array( |
|
539 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
540 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
541 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
542 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
543 | + ), |
|
544 | + |
|
545 | + /* S8 */ |
|
546 | + array( |
|
547 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
548 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
549 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
550 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
551 | + ) |
|
552 | + ); |
|
553 | + |
|
554 | + // permutation (P) |
|
555 | + self::$_p = array( |
|
556 | + 16, 7, 20, 21, |
|
557 | + 29, 12, 28, 17, |
|
558 | + 1, 15, 23, 26, |
|
559 | + 5, 18, 31, 10, |
|
560 | + 2, 8, 24, 14, |
|
561 | + 32, 27, 3, 9, |
|
562 | + 19, 13, 30, 6, |
|
563 | + 22, 11, 4, 25 |
|
564 | + ); |
|
565 | + |
|
566 | + // final permutation (FP) |
|
567 | + self::$_fp = array( |
|
568 | + 40, 8, 48, 16, 56, 24, 64, 32, |
|
569 | + 39, 7, 47, 15, 55, 23, 63, 31, |
|
570 | + 38, 6, 46, 14, 54, 22, 62, 30, |
|
571 | + 37, 5, 45, 13, 53, 21, 61, 29, |
|
572 | + 36, 4, 44, 12, 52, 20, 60, 28, |
|
573 | + 35, 3, 43, 11, 51, 19, 59, 27, |
|
574 | + 34, 2, 42, 10, 50, 18, 58, 26, |
|
575 | + 33, 1, 41, 9, 49, 17, 57, 25 |
|
576 | + ); |
|
577 | + |
|
578 | + // key schedule used in KeyPermutation() |
|
579 | + self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
580 | + } |
|
581 | + |
|
582 | + |
|
583 | + /** |
|
584 | + * Indicates this is a block cipher |
|
585 | + * |
|
586 | + * @return integer Returns Cipher::BLOCK |
|
587 | + */ |
|
588 | + public function type() |
|
589 | + { |
|
590 | + return parent::BLOCK; |
|
591 | + } |
|
592 | 592 | } |
593 | 593 | ?> |
@@ -239,102 +239,102 @@ |
||
239 | 239 | |
240 | 240 | $d = $tmp; |
241 | 241 | */ |
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Applies several of 3Way's functions used for encryption and decryption |
|
247 | - * NOTE: Please read the comments in the $this->gamma() function. This |
|
248 | - * function calls the $this->gamma() function which does not do anything. |
|
249 | - * |
|
250 | - * @param integer[] $d A 3 element 32 bit integer array |
|
251 | - * @return void |
|
252 | - */ |
|
253 | - private function rho(&$d) |
|
254 | - { |
|
255 | - $this->theta($d); |
|
256 | - $this->pi1($d); |
|
257 | - $this->gamma($d); |
|
258 | - $this->pi2($d); |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * 3Way's PI_1 function |
|
264 | - * This was taken from mcrypt's 3-way pi_1() function |
|
265 | - * |
|
266 | - * @param array $d A 3 element 32 bit integer array |
|
267 | - * @return void |
|
268 | - */ |
|
269 | - private function pi1(&$d) |
|
270 | - { |
|
271 | - $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
272 | - $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * 3Way's PI_2 function |
|
278 | - * This was taken from mcrypt's 3-way pi_2() function |
|
279 | - * |
|
280 | - * @param array $d A 3 element 32 bit integer array |
|
281 | - * @return void |
|
282 | - */ |
|
283 | - private function pi2(&$d) |
|
284 | - { |
|
285 | - $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
286 | - $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Reverse the bits of each element of array $d, and |
|
292 | - * reverses the order of array $d, used only during |
|
293 | - * decryption |
|
294 | - * |
|
295 | - * @param array $d A 3 element array of a 32 bit integers |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - private function invertBits(&$d) |
|
299 | - { |
|
300 | - $d = array_map("parent::dec2Bin", $d); |
|
301 | - $d = array_map("strrev", $d); |
|
302 | - $d = array_map("parent::bin2Dec", $d); |
|
303 | - $d = array_reverse($d); |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Initialize the tables used in 3Way Encryption. |
|
309 | - * |
|
310 | - * @return void |
|
311 | - */ |
|
312 | - private function initTables() |
|
313 | - { |
|
314 | - // round constants for encryption |
|
315 | - self::$_rcon_enc = array( |
|
316 | - 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
317 | - 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
318 | - 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
319 | - ); |
|
320 | - |
|
321 | - // round constants for decryption |
|
322 | - self::$_rcon_dec = array( |
|
323 | - 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
324 | - 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
325 | - 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
326 | - ); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Indicates this is a block cipher |
|
332 | - * |
|
333 | - * @return integer Returns Cipher::BLOCK |
|
334 | - */ |
|
335 | - public function type() |
|
336 | - { |
|
337 | - return parent::BLOCK; |
|
338 | - } |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Applies several of 3Way's functions used for encryption and decryption |
|
247 | + * NOTE: Please read the comments in the $this->gamma() function. This |
|
248 | + * function calls the $this->gamma() function which does not do anything. |
|
249 | + * |
|
250 | + * @param integer[] $d A 3 element 32 bit integer array |
|
251 | + * @return void |
|
252 | + */ |
|
253 | + private function rho(&$d) |
|
254 | + { |
|
255 | + $this->theta($d); |
|
256 | + $this->pi1($d); |
|
257 | + $this->gamma($d); |
|
258 | + $this->pi2($d); |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * 3Way's PI_1 function |
|
264 | + * This was taken from mcrypt's 3-way pi_1() function |
|
265 | + * |
|
266 | + * @param array $d A 3 element 32 bit integer array |
|
267 | + * @return void |
|
268 | + */ |
|
269 | + private function pi1(&$d) |
|
270 | + { |
|
271 | + $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
272 | + $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * 3Way's PI_2 function |
|
278 | + * This was taken from mcrypt's 3-way pi_2() function |
|
279 | + * |
|
280 | + * @param array $d A 3 element 32 bit integer array |
|
281 | + * @return void |
|
282 | + */ |
|
283 | + private function pi2(&$d) |
|
284 | + { |
|
285 | + $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
286 | + $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Reverse the bits of each element of array $d, and |
|
292 | + * reverses the order of array $d, used only during |
|
293 | + * decryption |
|
294 | + * |
|
295 | + * @param array $d A 3 element array of a 32 bit integers |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + private function invertBits(&$d) |
|
299 | + { |
|
300 | + $d = array_map("parent::dec2Bin", $d); |
|
301 | + $d = array_map("strrev", $d); |
|
302 | + $d = array_map("parent::bin2Dec", $d); |
|
303 | + $d = array_reverse($d); |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Initialize the tables used in 3Way Encryption. |
|
309 | + * |
|
310 | + * @return void |
|
311 | + */ |
|
312 | + private function initTables() |
|
313 | + { |
|
314 | + // round constants for encryption |
|
315 | + self::$_rcon_enc = array( |
|
316 | + 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
317 | + 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
318 | + 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
319 | + ); |
|
320 | + |
|
321 | + // round constants for decryption |
|
322 | + self::$_rcon_dec = array( |
|
323 | + 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
324 | + 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
325 | + 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
326 | + ); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Indicates this is a block cipher |
|
332 | + * |
|
333 | + * @return integer Returns Cipher::BLOCK |
|
334 | + */ |
|
335 | + public function type() |
|
336 | + { |
|
337 | + return parent::BLOCK; |
|
338 | + } |
|
339 | 339 | } |
340 | 340 | ?> |
@@ -39,161 +39,161 @@ |
||
39 | 39 | */ |
40 | 40 | class Cipher_ARC4 extends Cipher |
41 | 41 | { |
42 | - /** @type string $_s The S List */ |
|
43 | - private $_s = ""; |
|
44 | - |
|
45 | - /** @type string $_key_stream The Key Stream */ |
|
46 | - private $_key_stream = ""; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Constructor |
|
51 | - * |
|
52 | - * @param string $key The key used for Encryption/Decryption |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function __construct($key) |
|
56 | - { |
|
57 | - // set the ARC4 key |
|
58 | - parent::__construct(PHP_Crypt::CIPHER_ARC4, $key); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * Destructor |
|
64 | - * |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function __destruct() |
|
68 | - { |
|
69 | - parent::__destruct(); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * Encrypt plain text data using ARC4 |
|
75 | - * |
|
76 | - * @return boolean Returns true |
|
77 | - */ |
|
78 | - public function encrypt(&$text) |
|
79 | - { |
|
80 | - $this->operation(parent::ENCRYPT); |
|
81 | - return $this->arc4($text); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * Decrypt a ARC4 encrypted string |
|
87 | - * |
|
88 | - * @return boolean Returns true |
|
89 | - */ |
|
90 | - public function decrypt(&$text) |
|
91 | - { |
|
92 | - $this->operation(parent::DECRYPT); |
|
93 | - return $this->arc4($text); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * Performs the ARC4 algorithm, since encryption and decryption |
|
99 | - * are the same, all the work is done here |
|
100 | - * |
|
101 | - * @param string $text The string to encrypt/decrypt |
|
102 | - * @return boolean Returns true |
|
103 | - */ |
|
104 | - private function arc4(&$text) |
|
105 | - { |
|
106 | - $len = strlen($text); |
|
107 | - $this->prga($len); |
|
108 | - |
|
109 | - for ($i = 0; $i < $len; ++$i) |
|
110 | - $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
111 | - |
|
112 | - return true; |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * The key scheduling algorithm (KSA) |
|
118 | - * |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - private function ksa() |
|
122 | - { |
|
123 | - $j = 0; |
|
124 | - $this->_s = array(); |
|
125 | - $keylen = $this->keySize(); |
|
126 | - $key = $this->key(); |
|
127 | - |
|
128 | - // fill $this->_s with all the values from 0-255 |
|
129 | - for ($i = 0; $i < 256; ++$i) |
|
130 | - $this->_s[$i] = $i; |
|
131 | - |
|
132 | - // the changing S List |
|
133 | - for ($i = 0; $i < 256; ++$i) |
|
134 | - { |
|
135 | - $k = $key[$i % $keylen]; |
|
136 | - $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
137 | - |
|
138 | - // swap bytes |
|
139 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * The Pseudo-Random Generation Algorithm (PGRA) |
|
146 | - * Creates the key stream used for encryption / decryption |
|
147 | - * |
|
148 | - * @param integer $data_len The length of the data we are encrypting/decrypting |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - private function prga($data_len) |
|
152 | - { |
|
153 | - $i = 0; |
|
154 | - $j = 0; |
|
155 | - $this->_key_stream = ""; |
|
156 | - |
|
157 | - // set up the key schedule |
|
158 | - $this->ksa(); |
|
159 | - |
|
160 | - for ($c = 0; $c < $data_len; ++$c) |
|
161 | - { |
|
162 | - $i = ($i + 1) % 256; |
|
163 | - $j = ($j + $this->_s[$i]) % 256; |
|
164 | - |
|
165 | - // swap bytes |
|
166 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
167 | - |
|
168 | - $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
169 | - $this->_key_stream .= chr($this->_s[$pos]); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Swap two individual bytes |
|
176 | - * |
|
177 | - * @param string $a A single byte |
|
178 | - * @param string $b A single byte |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - private static function swapBytes(&$a, &$b) |
|
182 | - { |
|
183 | - $tmp = $a; |
|
184 | - $a = $b; |
|
185 | - $b = $tmp; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * Indicates that this is a stream cipher |
|
191 | - * |
|
192 | - * @return integer Returns Cipher::STREAM |
|
193 | - */ |
|
194 | - public function type() |
|
195 | - { |
|
196 | - return parent::STREAM; |
|
197 | - } |
|
42 | + /** @type string $_s The S List */ |
|
43 | + private $_s = ""; |
|
44 | + |
|
45 | + /** @type string $_key_stream The Key Stream */ |
|
46 | + private $_key_stream = ""; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Constructor |
|
51 | + * |
|
52 | + * @param string $key The key used for Encryption/Decryption |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function __construct($key) |
|
56 | + { |
|
57 | + // set the ARC4 key |
|
58 | + parent::__construct(PHP_Crypt::CIPHER_ARC4, $key); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * Destructor |
|
64 | + * |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function __destruct() |
|
68 | + { |
|
69 | + parent::__destruct(); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * Encrypt plain text data using ARC4 |
|
75 | + * |
|
76 | + * @return boolean Returns true |
|
77 | + */ |
|
78 | + public function encrypt(&$text) |
|
79 | + { |
|
80 | + $this->operation(parent::ENCRYPT); |
|
81 | + return $this->arc4($text); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * Decrypt a ARC4 encrypted string |
|
87 | + * |
|
88 | + * @return boolean Returns true |
|
89 | + */ |
|
90 | + public function decrypt(&$text) |
|
91 | + { |
|
92 | + $this->operation(parent::DECRYPT); |
|
93 | + return $this->arc4($text); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * Performs the ARC4 algorithm, since encryption and decryption |
|
99 | + * are the same, all the work is done here |
|
100 | + * |
|
101 | + * @param string $text The string to encrypt/decrypt |
|
102 | + * @return boolean Returns true |
|
103 | + */ |
|
104 | + private function arc4(&$text) |
|
105 | + { |
|
106 | + $len = strlen($text); |
|
107 | + $this->prga($len); |
|
108 | + |
|
109 | + for ($i = 0; $i < $len; ++$i) |
|
110 | + $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
111 | + |
|
112 | + return true; |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * The key scheduling algorithm (KSA) |
|
118 | + * |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + private function ksa() |
|
122 | + { |
|
123 | + $j = 0; |
|
124 | + $this->_s = array(); |
|
125 | + $keylen = $this->keySize(); |
|
126 | + $key = $this->key(); |
|
127 | + |
|
128 | + // fill $this->_s with all the values from 0-255 |
|
129 | + for ($i = 0; $i < 256; ++$i) |
|
130 | + $this->_s[$i] = $i; |
|
131 | + |
|
132 | + // the changing S List |
|
133 | + for ($i = 0; $i < 256; ++$i) |
|
134 | + { |
|
135 | + $k = $key[$i % $keylen]; |
|
136 | + $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
137 | + |
|
138 | + // swap bytes |
|
139 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * The Pseudo-Random Generation Algorithm (PGRA) |
|
146 | + * Creates the key stream used for encryption / decryption |
|
147 | + * |
|
148 | + * @param integer $data_len The length of the data we are encrypting/decrypting |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + private function prga($data_len) |
|
152 | + { |
|
153 | + $i = 0; |
|
154 | + $j = 0; |
|
155 | + $this->_key_stream = ""; |
|
156 | + |
|
157 | + // set up the key schedule |
|
158 | + $this->ksa(); |
|
159 | + |
|
160 | + for ($c = 0; $c < $data_len; ++$c) |
|
161 | + { |
|
162 | + $i = ($i + 1) % 256; |
|
163 | + $j = ($j + $this->_s[$i]) % 256; |
|
164 | + |
|
165 | + // swap bytes |
|
166 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
167 | + |
|
168 | + $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
169 | + $this->_key_stream .= chr($this->_s[$pos]); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Swap two individual bytes |
|
176 | + * |
|
177 | + * @param string $a A single byte |
|
178 | + * @param string $b A single byte |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + private static function swapBytes(&$a, &$b) |
|
182 | + { |
|
183 | + $tmp = $a; |
|
184 | + $a = $b; |
|
185 | + $b = $tmp; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * Indicates that this is a stream cipher |
|
191 | + * |
|
192 | + * @return integer Returns Cipher::STREAM |
|
193 | + */ |
|
194 | + public function type() |
|
195 | + { |
|
196 | + return parent::STREAM; |
|
197 | + } |
|
198 | 198 | } |
199 | 199 | ?> |
@@ -39,199 +39,199 @@ |
||
39 | 39 | */ |
40 | 40 | class Cipher_Skipjack extends Cipher |
41 | 41 | { |
42 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | - const BYTES_BLOCK = 8; // 64 bits |
|
42 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | + const BYTES_BLOCK = 8; // 64 bits |
|
44 | 44 | |
45 | - /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | - const BYTES_KEY = 10; // 80 bits |
|
45 | + /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | + const BYTES_KEY = 10; // 80 bits |
|
47 | 47 | |
48 | - /** @type string $expanded_key The expanded key */ |
|
49 | - private $expanded_key = ""; |
|
48 | + /** @type string $expanded_key The expanded key */ |
|
49 | + private $expanded_key = ""; |
|
50 | 50 | |
51 | - /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | - private static $_f = array(); |
|
51 | + /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | + private static $_f = array(); |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Constructor |
|
57 | - * |
|
58 | - * @param string $key The key used for Encryption/Decryption |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __construct($key) |
|
62 | - { |
|
63 | - // set the Skipjack key |
|
64 | - parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | - |
|
66 | - // initialize variables |
|
67 | - $this->initTables(); |
|
68 | - |
|
69 | - // set the block size used |
|
70 | - $this->blockSize(self::BYTES_BLOCK); |
|
71 | - |
|
72 | - // expand the key from 10 bytes to 128 bytes |
|
73 | - $this->expandKey(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Destructor |
|
79 | - * |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function __destruct() |
|
83 | - { |
|
84 | - parent::__destruct(); |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Encrypt plain text data using Skipjack |
|
90 | - * |
|
91 | - * @return boolean Returns true |
|
92 | - */ |
|
93 | - public function encrypt(&$text) |
|
94 | - { |
|
95 | - $this->operation(parent::ENCRYPT); |
|
96 | - |
|
97 | - for ($i = 1; $i <= 32; ++$i) |
|
98 | - { |
|
99 | - $pos = (4 * $i) - 4; |
|
100 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
101 | - |
|
102 | - if ($i >= 1 && $i <= 8) |
|
103 | - $this->ruleA($text, $subkey, $i); |
|
104 | - |
|
105 | - if ($i >= 9 && $i <= 16) |
|
106 | - $this->ruleB($text, $subkey, $i); |
|
107 | - |
|
108 | - if ($i >= 17 && $i <= 24) |
|
109 | - $this->ruleA($text, $subkey, $i); |
|
110 | - |
|
111 | - if ($i >= 25 && $i <= 32) |
|
112 | - $this->ruleB($text, $subkey, $i); |
|
113 | - } |
|
114 | - |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Decrypt a Skipjack encrypted string |
|
121 | - * |
|
122 | - * @return boolean Returns true |
|
123 | - */ |
|
124 | - public function decrypt(&$text) |
|
125 | - { |
|
126 | - $this->operation(parent::DECRYPT); |
|
127 | - |
|
128 | - for ($i = 32; $i >= 1; --$i) |
|
129 | - { |
|
130 | - $pos = ($i - 1) * 4; |
|
131 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
132 | - |
|
133 | - if ($i <= 32 && $i >= 25) |
|
134 | - $this->ruleB($text, $subkey, $i); |
|
135 | - |
|
136 | - if ($i <= 24 && $i >= 17) |
|
137 | - $this->ruleA($text, $subkey, $i); |
|
138 | - |
|
139 | - if ($i <= 16 && $i >= 9) |
|
140 | - $this->ruleB($text, $subkey, $i); |
|
141 | - |
|
142 | - if ($i <= 8 && $i >= 1) |
|
143 | - $this->ruleA($text, $subkey, $i); |
|
144 | - } |
|
145 | - |
|
146 | - return true; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * For the G Permutations, the input data is 2 Bytes The first byte is |
|
152 | - * the left side and the second is the right side.The round key is 4 bytes |
|
153 | - * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
154 | - * |
|
155 | - * @param string $bytes A 2 byte string |
|
156 | - * @param string $key 4 bytes of $this->expanded_key |
|
157 | - * @return string A 2 byte string, the G Permutation of $bytes |
|
158 | - */ |
|
159 | - private function gPermutation($bytes, $key) |
|
160 | - { |
|
161 | - $left = ord($bytes[0]); |
|
162 | - $right = ord($bytes[1]); |
|
163 | - |
|
164 | - if ($this->operation() == parent::ENCRYPT) |
|
165 | - { |
|
166 | - for ($i = 0; $i < 4; ++$i) |
|
167 | - { |
|
168 | - if ($i == 0 || $i == 2) |
|
169 | - { |
|
170 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
171 | - $left = $left ^ self::$_f[$pos]; |
|
172 | - } else |
|
173 | - { |
|
174 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
175 | - $right = $right ^ self::$_f[$pos]; |
|
176 | - } |
|
177 | - } |
|
178 | - } else // parent::DECRYPT |
|
179 | - { |
|
180 | - // we do the same as in encryption, but apply the key backwards, |
|
181 | - // from key[3] to key[0] |
|
182 | - for ($i = 3; $i >= 0; --$i) |
|
183 | - { |
|
184 | - if ($i == 0 || $i == 2) |
|
185 | - { |
|
186 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
187 | - $left = $left ^ self::$_f[$pos]; |
|
188 | - } else |
|
189 | - { |
|
190 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
191 | - $right = $right ^ self::$_f[$pos]; |
|
192 | - } |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - return $this->dec2Str($left).$this->dec2Str($right); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
202 | - * 2 bytes each: W0, W1, W2, W3. |
|
203 | - * |
|
204 | - * @param string $bytes An 8 byte string |
|
205 | - * @param string $key 4 bytes of $this->expanded_key |
|
206 | - * @param integer $i The round number |
|
207 | - * @return void |
|
208 | - */ |
|
209 | - private function ruleA(&$bytes, $key, $i) |
|
210 | - { |
|
211 | - $w = str_split($bytes, 2); |
|
212 | - |
|
213 | - if ($this->operation() == parent::ENCRYPT) |
|
214 | - { |
|
215 | - /* |
|
55 | + /** |
|
56 | + * Constructor |
|
57 | + * |
|
58 | + * @param string $key The key used for Encryption/Decryption |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __construct($key) |
|
62 | + { |
|
63 | + // set the Skipjack key |
|
64 | + parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | + |
|
66 | + // initialize variables |
|
67 | + $this->initTables(); |
|
68 | + |
|
69 | + // set the block size used |
|
70 | + $this->blockSize(self::BYTES_BLOCK); |
|
71 | + |
|
72 | + // expand the key from 10 bytes to 128 bytes |
|
73 | + $this->expandKey(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Destructor |
|
79 | + * |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function __destruct() |
|
83 | + { |
|
84 | + parent::__destruct(); |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Encrypt plain text data using Skipjack |
|
90 | + * |
|
91 | + * @return boolean Returns true |
|
92 | + */ |
|
93 | + public function encrypt(&$text) |
|
94 | + { |
|
95 | + $this->operation(parent::ENCRYPT); |
|
96 | + |
|
97 | + for ($i = 1; $i <= 32; ++$i) |
|
98 | + { |
|
99 | + $pos = (4 * $i) - 4; |
|
100 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
101 | + |
|
102 | + if ($i >= 1 && $i <= 8) |
|
103 | + $this->ruleA($text, $subkey, $i); |
|
104 | + |
|
105 | + if ($i >= 9 && $i <= 16) |
|
106 | + $this->ruleB($text, $subkey, $i); |
|
107 | + |
|
108 | + if ($i >= 17 && $i <= 24) |
|
109 | + $this->ruleA($text, $subkey, $i); |
|
110 | + |
|
111 | + if ($i >= 25 && $i <= 32) |
|
112 | + $this->ruleB($text, $subkey, $i); |
|
113 | + } |
|
114 | + |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Decrypt a Skipjack encrypted string |
|
121 | + * |
|
122 | + * @return boolean Returns true |
|
123 | + */ |
|
124 | + public function decrypt(&$text) |
|
125 | + { |
|
126 | + $this->operation(parent::DECRYPT); |
|
127 | + |
|
128 | + for ($i = 32; $i >= 1; --$i) |
|
129 | + { |
|
130 | + $pos = ($i - 1) * 4; |
|
131 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
132 | + |
|
133 | + if ($i <= 32 && $i >= 25) |
|
134 | + $this->ruleB($text, $subkey, $i); |
|
135 | + |
|
136 | + if ($i <= 24 && $i >= 17) |
|
137 | + $this->ruleA($text, $subkey, $i); |
|
138 | + |
|
139 | + if ($i <= 16 && $i >= 9) |
|
140 | + $this->ruleB($text, $subkey, $i); |
|
141 | + |
|
142 | + if ($i <= 8 && $i >= 1) |
|
143 | + $this->ruleA($text, $subkey, $i); |
|
144 | + } |
|
145 | + |
|
146 | + return true; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * For the G Permutations, the input data is 2 Bytes The first byte is |
|
152 | + * the left side and the second is the right side.The round key is 4 bytes |
|
153 | + * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
154 | + * |
|
155 | + * @param string $bytes A 2 byte string |
|
156 | + * @param string $key 4 bytes of $this->expanded_key |
|
157 | + * @return string A 2 byte string, the G Permutation of $bytes |
|
158 | + */ |
|
159 | + private function gPermutation($bytes, $key) |
|
160 | + { |
|
161 | + $left = ord($bytes[0]); |
|
162 | + $right = ord($bytes[1]); |
|
163 | + |
|
164 | + if ($this->operation() == parent::ENCRYPT) |
|
165 | + { |
|
166 | + for ($i = 0; $i < 4; ++$i) |
|
167 | + { |
|
168 | + if ($i == 0 || $i == 2) |
|
169 | + { |
|
170 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
171 | + $left = $left ^ self::$_f[$pos]; |
|
172 | + } else |
|
173 | + { |
|
174 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
175 | + $right = $right ^ self::$_f[$pos]; |
|
176 | + } |
|
177 | + } |
|
178 | + } else // parent::DECRYPT |
|
179 | + { |
|
180 | + // we do the same as in encryption, but apply the key backwards, |
|
181 | + // from key[3] to key[0] |
|
182 | + for ($i = 3; $i >= 0; --$i) |
|
183 | + { |
|
184 | + if ($i == 0 || $i == 2) |
|
185 | + { |
|
186 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
187 | + $left = $left ^ self::$_f[$pos]; |
|
188 | + } else |
|
189 | + { |
|
190 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
191 | + $right = $right ^ self::$_f[$pos]; |
|
192 | + } |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + return $this->dec2Str($left).$this->dec2Str($right); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
202 | + * 2 bytes each: W0, W1, W2, W3. |
|
203 | + * |
|
204 | + * @param string $bytes An 8 byte string |
|
205 | + * @param string $key 4 bytes of $this->expanded_key |
|
206 | + * @param integer $i The round number |
|
207 | + * @return void |
|
208 | + */ |
|
209 | + private function ruleA(&$bytes, $key, $i) |
|
210 | + { |
|
211 | + $w = str_split($bytes, 2); |
|
212 | + |
|
213 | + if ($this->operation() == parent::ENCRYPT) |
|
214 | + { |
|
215 | + /* |
|
216 | 216 | * Set the W3 as the old W2 |
217 | 217 | * Set the W2 as the old W1 |
218 | 218 | * Set the W1 as the G(W0) |
219 | 219 | * Set the W0 as the W1 xor W4 xor i |
220 | 220 | */ |
221 | 221 | |
222 | - $w[4] = $w[3]; |
|
223 | - $w[3] = $w[2]; |
|
224 | - $w[2] = $w[1]; |
|
225 | - $w[1] = $this->gPermutation($w[0], $key); |
|
226 | - |
|
227 | - $hex1 = $this->str2Hex($w[1]); |
|
228 | - $hex4 = $this->str2Hex($w[4]); |
|
229 | - $hexi = $this->dec2Hex($i); |
|
230 | - $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
231 | - $w[0] = $this->hex2Str($w[0]); |
|
232 | - } else // parent::DECRYPT |
|
233 | - { |
|
234 | - /* |
|
222 | + $w[4] = $w[3]; |
|
223 | + $w[3] = $w[2]; |
|
224 | + $w[2] = $w[1]; |
|
225 | + $w[1] = $this->gPermutation($w[0], $key); |
|
226 | + |
|
227 | + $hex1 = $this->str2Hex($w[1]); |
|
228 | + $hex4 = $this->str2Hex($w[4]); |
|
229 | + $hexi = $this->dec2Hex($i); |
|
230 | + $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
231 | + $w[0] = $this->hex2Str($w[0]); |
|
232 | + } else // parent::DECRYPT |
|
233 | + { |
|
234 | + /* |
|
235 | 235 | * Set W4 as W0 xor W1 xor i |
236 | 236 | * Set W0 as Inverse G(W1) |
237 | 237 | * Set W1 as the old W2 |
@@ -44,204 +44,204 @@ |
||
44 | 44 | */ |
45 | 45 | class Cipher_Vigenere extends Cipher |
46 | 46 | { |
47 | - /** @type array $_vtable The Vigenere table */ |
|
48 | - private static $_vtable = null; |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Constructor |
|
53 | - * |
|
54 | - * @param string $key The key used for Encryption/Decryption |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function __construct($key) |
|
58 | - { |
|
59 | - // set the key |
|
60 | - parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | - |
|
62 | - $this->initTables(); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Destructor |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function __destruct() |
|
72 | - { |
|
73 | - parent::__destruct(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Encrypt plain text data using Vigenere cipher |
|
79 | - * |
|
80 | - * @return boolean Returns true |
|
81 | - */ |
|
82 | - public function encrypt(&$text) |
|
83 | - { |
|
84 | - $this->operation(parent::ENCRYPT); |
|
85 | - |
|
86 | - // convert to uppercase, and remove any non alphabetic characters |
|
87 | - $text = strtoupper($text); |
|
88 | - $text = preg_replace("/[^A-Z]/", "", $text); |
|
89 | - $len = strlen($text); |
|
90 | - |
|
91 | - // prepare the key for the cipher |
|
92 | - $this->keyPrep($len); |
|
93 | - |
|
94 | - // loop through each letter of the message |
|
95 | - for ($i = 0; $i < $len; ++$i) |
|
96 | - { |
|
97 | - // get the Cipher letter from the Vigenere table, using the |
|
98 | - // current letter from the key as the row, and the current letter |
|
99 | - // from the text, as the column, subtract 65 because ascii upper case |
|
100 | - // letters start at 65 |
|
101 | - $row = ord($this->expanded_key[$i]) - 65; |
|
102 | - $col = ord($text[$i]) - 65; |
|
103 | - $pos = ($row * 26) + $col; |
|
104 | - |
|
105 | - // convert the plain text to cipher text |
|
106 | - $text[$i] = self::$_vtable[$pos]; |
|
107 | - } |
|
108 | - |
|
109 | - return true; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * Decrypt a Vigenere encrypted string |
|
115 | - * |
|
116 | - * @return boolean Returns true |
|
117 | - */ |
|
118 | - public function decrypt(&$text) |
|
119 | - { |
|
120 | - $this->operation(parent::DECRYPT); |
|
121 | - |
|
122 | - // ensure the cipher text is all uppercase |
|
123 | - $text = strtoupper($text); |
|
124 | - $len = strlen($text); |
|
125 | - |
|
126 | - // prepare the key for the cipher |
|
127 | - $this->keyPrep($len); |
|
128 | - |
|
129 | - // go to the row corresponding to the letter from the key |
|
130 | - for ($i = 0; $i < $len; ++$i) |
|
131 | - { |
|
132 | - // find the row from the current character of the key, we subtract 65 |
|
133 | - // because uppercase letters start at ASCII 65 |
|
134 | - $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
135 | - |
|
136 | - // loop throw the entire row in the table until we find the letter |
|
137 | - // that matches the letter from the encrypted text |
|
138 | - for ($j = 0; $j < 26; ++$j) |
|
139 | - { |
|
140 | - // save the position we are in in the row |
|
141 | - $pos = $row + $j; |
|
142 | - |
|
143 | - // compare the letter from the table to the letter in the cipher text |
|
144 | - if (self::$_vtable[$pos] == $text[$i]) |
|
145 | - { |
|
146 | - // bingo, we found it. The plain text is the letter associated with |
|
147 | - // with the column position, again add 65 because ascii capital |
|
148 | - // letters start at 65 |
|
149 | - $text[$i] = chr($j + 65); |
|
150 | - break; |
|
151 | - } |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * Prepare the key. The key can only contain uppercase letters. |
|
161 | - * All other characters are stripped out. The key length must match |
|
162 | - * The length of the message |
|
163 | - * |
|
164 | - * @param integer $len The length of message |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - private function keyPrep($len) |
|
168 | - { |
|
169 | - // we never modify the actual key, so we save it into another variable |
|
170 | - $this->expanded_key = $this->key(); |
|
171 | - $this->expanded_key = strtoupper($this->expanded_key); |
|
172 | - $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
173 | - $keylen = strlen($this->expanded_key); |
|
174 | - |
|
175 | - // The key must be prepared so that it is the same length as the |
|
176 | - // message. If it is longer or shorter we need to modify it |
|
177 | - // to make it the correct length |
|
178 | - if ($keylen > $len) |
|
179 | - $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | - else if ($len > $keylen) |
|
181 | - { |
|
182 | - // if the key is shorter than the message, then we need pad the key |
|
183 | - // by repeating it until it is the correct length |
|
184 | - $diff = $len - $keylen; |
|
185 | - $pos = 0; |
|
186 | - |
|
187 | - for ($i = 0; $i < $diff; ++$i) |
|
188 | - { |
|
189 | - if ($pos >= $keylen) |
|
190 | - $pos = 0; |
|
191 | - |
|
192 | - $this->expanded_key .= $this->expanded_key[$pos]; |
|
193 | - ++$pos; |
|
194 | - } |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - |
|
199 | - /** |
|
200 | - * Initialize the Vigenere table used for encryption & decryption |
|
201 | - * |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - private function initTables() |
|
205 | - { |
|
206 | - self::$_vtable = array( |
|
207 | - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
208 | - 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', |
|
209 | - 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', |
|
210 | - 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', |
|
211 | - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', |
|
212 | - 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', |
|
213 | - 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', |
|
214 | - 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
215 | - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
216 | - 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
|
217 | - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
218 | - 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
|
219 | - 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|
220 | - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
221 | - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
|
222 | - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
223 | - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
224 | - 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', |
|
225 | - 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
226 | - 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
227 | - 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
|
228 | - 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
|
229 | - 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
230 | - 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
|
231 | - 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
232 | - 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Indicates that this is a stream cipher |
|
239 | - * |
|
240 | - * @return integer Returns Cipher::STREAM |
|
241 | - */ |
|
242 | - public function type() |
|
243 | - { |
|
244 | - return parent::STREAM; |
|
245 | - } |
|
47 | + /** @type array $_vtable The Vigenere table */ |
|
48 | + private static $_vtable = null; |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Constructor |
|
53 | + * |
|
54 | + * @param string $key The key used for Encryption/Decryption |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function __construct($key) |
|
58 | + { |
|
59 | + // set the key |
|
60 | + parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | + |
|
62 | + $this->initTables(); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Destructor |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function __destruct() |
|
72 | + { |
|
73 | + parent::__destruct(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Encrypt plain text data using Vigenere cipher |
|
79 | + * |
|
80 | + * @return boolean Returns true |
|
81 | + */ |
|
82 | + public function encrypt(&$text) |
|
83 | + { |
|
84 | + $this->operation(parent::ENCRYPT); |
|
85 | + |
|
86 | + // convert to uppercase, and remove any non alphabetic characters |
|
87 | + $text = strtoupper($text); |
|
88 | + $text = preg_replace("/[^A-Z]/", "", $text); |
|
89 | + $len = strlen($text); |
|
90 | + |
|
91 | + // prepare the key for the cipher |
|
92 | + $this->keyPrep($len); |
|
93 | + |
|
94 | + // loop through each letter of the message |
|
95 | + for ($i = 0; $i < $len; ++$i) |
|
96 | + { |
|
97 | + // get the Cipher letter from the Vigenere table, using the |
|
98 | + // current letter from the key as the row, and the current letter |
|
99 | + // from the text, as the column, subtract 65 because ascii upper case |
|
100 | + // letters start at 65 |
|
101 | + $row = ord($this->expanded_key[$i]) - 65; |
|
102 | + $col = ord($text[$i]) - 65; |
|
103 | + $pos = ($row * 26) + $col; |
|
104 | + |
|
105 | + // convert the plain text to cipher text |
|
106 | + $text[$i] = self::$_vtable[$pos]; |
|
107 | + } |
|
108 | + |
|
109 | + return true; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * Decrypt a Vigenere encrypted string |
|
115 | + * |
|
116 | + * @return boolean Returns true |
|
117 | + */ |
|
118 | + public function decrypt(&$text) |
|
119 | + { |
|
120 | + $this->operation(parent::DECRYPT); |
|
121 | + |
|
122 | + // ensure the cipher text is all uppercase |
|
123 | + $text = strtoupper($text); |
|
124 | + $len = strlen($text); |
|
125 | + |
|
126 | + // prepare the key for the cipher |
|
127 | + $this->keyPrep($len); |
|
128 | + |
|
129 | + // go to the row corresponding to the letter from the key |
|
130 | + for ($i = 0; $i < $len; ++$i) |
|
131 | + { |
|
132 | + // find the row from the current character of the key, we subtract 65 |
|
133 | + // because uppercase letters start at ASCII 65 |
|
134 | + $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
135 | + |
|
136 | + // loop throw the entire row in the table until we find the letter |
|
137 | + // that matches the letter from the encrypted text |
|
138 | + for ($j = 0; $j < 26; ++$j) |
|
139 | + { |
|
140 | + // save the position we are in in the row |
|
141 | + $pos = $row + $j; |
|
142 | + |
|
143 | + // compare the letter from the table to the letter in the cipher text |
|
144 | + if (self::$_vtable[$pos] == $text[$i]) |
|
145 | + { |
|
146 | + // bingo, we found it. The plain text is the letter associated with |
|
147 | + // with the column position, again add 65 because ascii capital |
|
148 | + // letters start at 65 |
|
149 | + $text[$i] = chr($j + 65); |
|
150 | + break; |
|
151 | + } |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * Prepare the key. The key can only contain uppercase letters. |
|
161 | + * All other characters are stripped out. The key length must match |
|
162 | + * The length of the message |
|
163 | + * |
|
164 | + * @param integer $len The length of message |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + private function keyPrep($len) |
|
168 | + { |
|
169 | + // we never modify the actual key, so we save it into another variable |
|
170 | + $this->expanded_key = $this->key(); |
|
171 | + $this->expanded_key = strtoupper($this->expanded_key); |
|
172 | + $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
173 | + $keylen = strlen($this->expanded_key); |
|
174 | + |
|
175 | + // The key must be prepared so that it is the same length as the |
|
176 | + // message. If it is longer or shorter we need to modify it |
|
177 | + // to make it the correct length |
|
178 | + if ($keylen > $len) |
|
179 | + $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | + else if ($len > $keylen) |
|
181 | + { |
|
182 | + // if the key is shorter than the message, then we need pad the key |
|
183 | + // by repeating it until it is the correct length |
|
184 | + $diff = $len - $keylen; |
|
185 | + $pos = 0; |
|
186 | + |
|
187 | + for ($i = 0; $i < $diff; ++$i) |
|
188 | + { |
|
189 | + if ($pos >= $keylen) |
|
190 | + $pos = 0; |
|
191 | + |
|
192 | + $this->expanded_key .= $this->expanded_key[$pos]; |
|
193 | + ++$pos; |
|
194 | + } |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + |
|
199 | + /** |
|
200 | + * Initialize the Vigenere table used for encryption & decryption |
|
201 | + * |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + private function initTables() |
|
205 | + { |
|
206 | + self::$_vtable = array( |
|
207 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
208 | + 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', |
|
209 | + 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', |
|
210 | + 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', |
|
211 | + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', |
|
212 | + 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', |
|
213 | + 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', |
|
214 | + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
215 | + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
216 | + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
|
217 | + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
218 | + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
|
219 | + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|
220 | + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
221 | + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
|
222 | + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
223 | + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
224 | + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', |
|
225 | + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
226 | + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
227 | + 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
|
228 | + 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
|
229 | + 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
230 | + 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
|
231 | + 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
232 | + 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Indicates that this is a stream cipher |
|
239 | + * |
|
240 | + * @return integer Returns Cipher::STREAM |
|
241 | + */ |
|
242 | + public function type() |
|
243 | + { |
|
244 | + return parent::STREAM; |
|
245 | + } |
|
246 | 246 | } |
247 | 247 | ?> |
@@ -43,208 +43,208 @@ |
||
43 | 43 | */ |
44 | 44 | class Cipher_3DES extends Cipher_DES |
45 | 45 | { |
46 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
47 | - const BYTES_BLOCK = 8; // 64 bits |
|
46 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
47 | + const BYTES_BLOCK = 8; // 64 bits |
|
48 | 48 | |
49 | - /** @type integer BYTES_KEY The size of the key. The key can be |
|
49 | + /** @type integer BYTES_KEY The size of the key. The key can be |
|
50 | 50 | 8, 16, or 24 bytes but gets expanded to 24 bytes before used */ |
51 | - const BYTES_KEY = 24; |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Constructor |
|
56 | - * |
|
57 | - * @param string $key The key used for Encryption/Decryption |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function __construct($key) |
|
61 | - { |
|
62 | - $key_len = strlen($key); |
|
63 | - |
|
64 | - if ($key_len == 8 || $key_len == 16) |
|
65 | - $key = self::expandKey($key, $key_len); |
|
66 | - else if ($key_len < self::BYTES_KEY) |
|
67 | - { |
|
68 | - $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
69 | - $msg .= "$key_len bytes received."; |
|
70 | - trigger_error($msg, E_USER_WARNING); |
|
71 | - } |
|
72 | - // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
73 | - |
|
74 | - // set the 3DES key, note that we call __construct1() not __construct() |
|
75 | - // this is a second contructor we created for classes that extend the |
|
76 | - // DES class |
|
77 | - parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
78 | - |
|
79 | - // 3DES requires that data is 64 bits |
|
80 | - $this->blockSize(self::BYTES_BLOCK); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Destructor |
|
86 | - * |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function __destruct() |
|
90 | - { |
|
91 | - parent::__destruct(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Encrypt plain text data using TripleDES |
|
97 | - * For encryption 3DES takes a 192 bit key, splits |
|
98 | - * it into 3 64 bit parts, and then does the following |
|
99 | - * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
100 | - * |
|
101 | - * @return boolean Returns true |
|
102 | - */ |
|
103 | - public function encrypt(&$text) |
|
104 | - { |
|
105 | - $blocksz = $this->blockSize(); |
|
106 | - |
|
107 | - for ($i = 0; $i < 3; ++$i) |
|
108 | - { |
|
109 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
110 | - $this->keyPermutation($key); |
|
111 | - |
|
112 | - if ($i % 2) // round 1 |
|
113 | - $this->operation(parent::DECRYPT); |
|
114 | - else // rounds 0 and 2 |
|
115 | - $this->operation(parent::ENCRYPT); |
|
116 | - |
|
117 | - $this->des($text); |
|
118 | - } |
|
119 | - |
|
120 | - return true; |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * Decrypt a TripleDES encrypted string |
|
126 | - * For decryption 3DES takes a 192 bit key, splits |
|
127 | - * it into 3 64 bit parts, and then does the following |
|
128 | - * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
129 | - * |
|
130 | - * @return boolean Returns true |
|
131 | - */ |
|
132 | - public function decrypt(&$text) |
|
133 | - { |
|
134 | - $blocksz = $this->blockSize(); |
|
135 | - |
|
136 | - for ($i = 2; $i >= 0; --$i) |
|
137 | - { |
|
138 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
139 | - $this->keyPermutation($key); |
|
140 | - |
|
141 | - if ($i % 2) // round 1 |
|
142 | - $this->operation(parent::ENCRYPT); |
|
143 | - else // round 0 and 2 |
|
144 | - $this->operation(parent::DECRYPT); |
|
145 | - |
|
146 | - $this->des($text); |
|
147 | - } |
|
148 | - |
|
149 | - return true; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * We overload the DES::keyPermutation() function so it takes |
|
155 | - * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
156 | - * key which is split into 3 64 bit parts, each of which must be |
|
157 | - * passed through the keyPermutation() function |
|
158 | - * Key permutation, based on tables $_pc1 and $_pc2 |
|
159 | - * Create 16 subkeys, each of which is 48-bits long. |
|
160 | - * NOTE: The $key parameter is required, however because PHP expects |
|
161 | - * an overloaded method to contain the same number of parameters |
|
162 | - * (when E_STRICT is set) as the parent class, we make it optional |
|
163 | - * to shut the 'error' up |
|
164 | - * |
|
165 | - * @param string $key A 64 bit key |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - private function keyPermutation($key = "") |
|
169 | - { |
|
170 | - $this->sub_keys = array(); |
|
171 | - $pc1m = array(); |
|
172 | - $pcr = array(); |
|
173 | - $c = array(); |
|
174 | - $d = array(); |
|
175 | - |
|
176 | - // convert the key to binary |
|
177 | - $binkey = parent::str2Bin($key); |
|
178 | - |
|
179 | - // reduce the key down to 56bits based on table $_pc1 |
|
180 | - for ($i = 0; $i < 56; ++$i) |
|
181 | - { |
|
182 | - $pos = parent::$_pc1[$i] - 1; |
|
51 | + const BYTES_KEY = 24; |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Constructor |
|
56 | + * |
|
57 | + * @param string $key The key used for Encryption/Decryption |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function __construct($key) |
|
61 | + { |
|
62 | + $key_len = strlen($key); |
|
63 | + |
|
64 | + if ($key_len == 8 || $key_len == 16) |
|
65 | + $key = self::expandKey($key, $key_len); |
|
66 | + else if ($key_len < self::BYTES_KEY) |
|
67 | + { |
|
68 | + $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
69 | + $msg .= "$key_len bytes received."; |
|
70 | + trigger_error($msg, E_USER_WARNING); |
|
71 | + } |
|
72 | + // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
73 | + |
|
74 | + // set the 3DES key, note that we call __construct1() not __construct() |
|
75 | + // this is a second contructor we created for classes that extend the |
|
76 | + // DES class |
|
77 | + parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
78 | + |
|
79 | + // 3DES requires that data is 64 bits |
|
80 | + $this->blockSize(self::BYTES_BLOCK); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Destructor |
|
86 | + * |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function __destruct() |
|
90 | + { |
|
91 | + parent::__destruct(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Encrypt plain text data using TripleDES |
|
97 | + * For encryption 3DES takes a 192 bit key, splits |
|
98 | + * it into 3 64 bit parts, and then does the following |
|
99 | + * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
100 | + * |
|
101 | + * @return boolean Returns true |
|
102 | + */ |
|
103 | + public function encrypt(&$text) |
|
104 | + { |
|
105 | + $blocksz = $this->blockSize(); |
|
106 | + |
|
107 | + for ($i = 0; $i < 3; ++$i) |
|
108 | + { |
|
109 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
110 | + $this->keyPermutation($key); |
|
111 | + |
|
112 | + if ($i % 2) // round 1 |
|
113 | + $this->operation(parent::DECRYPT); |
|
114 | + else // rounds 0 and 2 |
|
115 | + $this->operation(parent::ENCRYPT); |
|
116 | + |
|
117 | + $this->des($text); |
|
118 | + } |
|
119 | + |
|
120 | + return true; |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * Decrypt a TripleDES encrypted string |
|
126 | + * For decryption 3DES takes a 192 bit key, splits |
|
127 | + * it into 3 64 bit parts, and then does the following |
|
128 | + * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
129 | + * |
|
130 | + * @return boolean Returns true |
|
131 | + */ |
|
132 | + public function decrypt(&$text) |
|
133 | + { |
|
134 | + $blocksz = $this->blockSize(); |
|
135 | + |
|
136 | + for ($i = 2; $i >= 0; --$i) |
|
137 | + { |
|
138 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
139 | + $this->keyPermutation($key); |
|
140 | + |
|
141 | + if ($i % 2) // round 1 |
|
142 | + $this->operation(parent::ENCRYPT); |
|
143 | + else // round 0 and 2 |
|
144 | + $this->operation(parent::DECRYPT); |
|
145 | + |
|
146 | + $this->des($text); |
|
147 | + } |
|
148 | + |
|
149 | + return true; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * We overload the DES::keyPermutation() function so it takes |
|
155 | + * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
156 | + * key which is split into 3 64 bit parts, each of which must be |
|
157 | + * passed through the keyPermutation() function |
|
158 | + * Key permutation, based on tables $_pc1 and $_pc2 |
|
159 | + * Create 16 subkeys, each of which is 48-bits long. |
|
160 | + * NOTE: The $key parameter is required, however because PHP expects |
|
161 | + * an overloaded method to contain the same number of parameters |
|
162 | + * (when E_STRICT is set) as the parent class, we make it optional |
|
163 | + * to shut the 'error' up |
|
164 | + * |
|
165 | + * @param string $key A 64 bit key |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + private function keyPermutation($key = "") |
|
169 | + { |
|
170 | + $this->sub_keys = array(); |
|
171 | + $pc1m = array(); |
|
172 | + $pcr = array(); |
|
173 | + $c = array(); |
|
174 | + $d = array(); |
|
175 | + |
|
176 | + // convert the key to binary |
|
177 | + $binkey = parent::str2Bin($key); |
|
178 | + |
|
179 | + // reduce the key down to 56bits based on table $_pc1 |
|
180 | + for ($i = 0; $i < 56; ++$i) |
|
181 | + { |
|
182 | + $pos = parent::$_pc1[$i] - 1; |
|
183 | 183 | $pc1m[$i] = $binkey[$pos]; |
184 | - } |
|
185 | - |
|
186 | - // split $pc1m in half (C0 and D0) |
|
187 | - $c[0] = array_slice($pc1m, 0, 28); |
|
188 | - $d[0] = array_slice($pc1m, 28, 28); |
|
189 | - |
|
190 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
191 | - // where 1 <= n <= 16 |
|
192 | - for ($i = 1; $i <= 16; ++$i) |
|
193 | - { |
|
194 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
195 | - $c[$i] = $c[$i - 1]; |
|
196 | - $d[$i] = $d[$i - 1]; |
|
197 | - |
|
198 | - for ($j = 0; $j < parent::$_key_sched[$i - 1]; ++$j) |
|
199 | - { |
|
200 | - // do a left shift, move each bit one place to the left, |
|
201 | - // except for the first bit, which is cycled to the end |
|
202 | - // of the block. |
|
203 | - $c[$i][] = array_shift($c[$i]); |
|
204 | - $d[$i][] = array_shift($d[$i]); |
|
205 | - } |
|
206 | - |
|
207 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
208 | - // following permutation table to each of the concatenated |
|
209 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
210 | - // of these. |
|
211 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
212 | - $this->sub_keys[$i - 1] = ""; |
|
213 | - for ($j = 0; $j < 48; ++$j) |
|
214 | - $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
215 | - } |
|
216 | - |
|
217 | - // the sub_keys are created, we are done with the key permutation |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
223 | - * 8 or 16 bytes, it is expanded to 24 bytes |
|
224 | - * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
225 | - * byte key |
|
226 | - * A 16 byte key add the first 8 bytes to the end of the string |
|
227 | - * to make a 24 byte key |
|
228 | - * |
|
229 | - * @param string $key The 8 or 16 byte key to expand |
|
230 | - * @param integer $len |
|
231 | - * @return string If the key given is 8 or 16 bytes it returns the |
|
232 | - * expanded 24 byte key, else it returns the original key unexpanded |
|
233 | - */ |
|
234 | - private static function expandKey($key, $len) |
|
235 | - { |
|
236 | - // if we were given an 8 byte key, repeat it |
|
237 | - // 3 times to produce a 24 byte key |
|
238 | - if ($len == 8) |
|
239 | - $key = str_repeat($key, 3); |
|
240 | - |
|
241 | - // if we were given a 16 byte key, add the first |
|
242 | - // 8 bytes to the end of the key to produce 24 bytes |
|
243 | - if ($len == 16) |
|
244 | - $key .= substr($key, 0, 8); |
|
245 | - |
|
246 | - // return the key |
|
247 | - return $key; |
|
248 | - } |
|
184 | + } |
|
185 | + |
|
186 | + // split $pc1m in half (C0 and D0) |
|
187 | + $c[0] = array_slice($pc1m, 0, 28); |
|
188 | + $d[0] = array_slice($pc1m, 28, 28); |
|
189 | + |
|
190 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
191 | + // where 1 <= n <= 16 |
|
192 | + for ($i = 1; $i <= 16; ++$i) |
|
193 | + { |
|
194 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
195 | + $c[$i] = $c[$i - 1]; |
|
196 | + $d[$i] = $d[$i - 1]; |
|
197 | + |
|
198 | + for ($j = 0; $j < parent::$_key_sched[$i - 1]; ++$j) |
|
199 | + { |
|
200 | + // do a left shift, move each bit one place to the left, |
|
201 | + // except for the first bit, which is cycled to the end |
|
202 | + // of the block. |
|
203 | + $c[$i][] = array_shift($c[$i]); |
|
204 | + $d[$i][] = array_shift($d[$i]); |
|
205 | + } |
|
206 | + |
|
207 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
208 | + // following permutation table to each of the concatenated |
|
209 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
210 | + // of these. |
|
211 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
212 | + $this->sub_keys[$i - 1] = ""; |
|
213 | + for ($j = 0; $j < 48; ++$j) |
|
214 | + $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
215 | + } |
|
216 | + |
|
217 | + // the sub_keys are created, we are done with the key permutation |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
223 | + * 8 or 16 bytes, it is expanded to 24 bytes |
|
224 | + * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
225 | + * byte key |
|
226 | + * A 16 byte key add the first 8 bytes to the end of the string |
|
227 | + * to make a 24 byte key |
|
228 | + * |
|
229 | + * @param string $key The 8 or 16 byte key to expand |
|
230 | + * @param integer $len |
|
231 | + * @return string If the key given is 8 or 16 bytes it returns the |
|
232 | + * expanded 24 byte key, else it returns the original key unexpanded |
|
233 | + */ |
|
234 | + private static function expandKey($key, $len) |
|
235 | + { |
|
236 | + // if we were given an 8 byte key, repeat it |
|
237 | + // 3 times to produce a 24 byte key |
|
238 | + if ($len == 8) |
|
239 | + $key = str_repeat($key, 3); |
|
240 | + |
|
241 | + // if we were given a 16 byte key, add the first |
|
242 | + // 8 bytes to the end of the key to produce 24 bytes |
|
243 | + if ($len == 16) |
|
244 | + $key .= substr($key, 0, 8); |
|
245 | + |
|
246 | + // return the key |
|
247 | + return $key; |
|
248 | + } |
|
249 | 249 | } |
250 | 250 | ?> |
@@ -42,994 +42,994 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class Cipher_Rijndael extends Cipher |
44 | 44 | { |
45 | - /** @type string $xkey The expanded key */ |
|
46 | - private $xkey = ""; |
|
47 | - |
|
48 | - /** |
|
49 | - * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | - * this should be considered a constant |
|
51 | - */ |
|
52 | - private static $_key_sizes = array(16, 24, 32); |
|
53 | - |
|
54 | - |
|
55 | - // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | - |
|
57 | - /** |
|
58 | - * @type array $_s The sbox, |
|
59 | - * this should be considered a constant |
|
60 | - */ |
|
61 | - private static $_s = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * @type array $_s_inv The inverse sbox |
|
65 | - * this should be considered a constant |
|
66 | - */ |
|
67 | - private static $_s_inv = array(); |
|
68 | - |
|
69 | - /** |
|
70 | - * @type array $_rcon The round constant, |
|
71 | - * this should be considered a constant |
|
72 | - */ |
|
73 | - private static $_rcon = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | - * this should be considered a constant |
|
78 | - */ |
|
79 | - private static $_matrix_mult = array(); |
|
80 | - |
|
81 | - /** |
|
82 | - * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | - * inverse table, this should be considered a constant |
|
84 | - */ |
|
85 | - private static $_matrix_mult_inv = array(); |
|
86 | - |
|
87 | - /* |
|
45 | + /** @type string $xkey The expanded key */ |
|
46 | + private $xkey = ""; |
|
47 | + |
|
48 | + /** |
|
49 | + * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | + * this should be considered a constant |
|
51 | + */ |
|
52 | + private static $_key_sizes = array(16, 24, 32); |
|
53 | + |
|
54 | + |
|
55 | + // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | + |
|
57 | + /** |
|
58 | + * @type array $_s The sbox, |
|
59 | + * this should be considered a constant |
|
60 | + */ |
|
61 | + private static $_s = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * @type array $_s_inv The inverse sbox |
|
65 | + * this should be considered a constant |
|
66 | + */ |
|
67 | + private static $_s_inv = array(); |
|
68 | + |
|
69 | + /** |
|
70 | + * @type array $_rcon The round constant, |
|
71 | + * this should be considered a constant |
|
72 | + */ |
|
73 | + private static $_rcon = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | + * this should be considered a constant |
|
78 | + */ |
|
79 | + private static $_matrix_mult = array(); |
|
80 | + |
|
81 | + /** |
|
82 | + * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | + * inverse table, this should be considered a constant |
|
84 | + */ |
|
85 | + private static $_matrix_mult_inv = array(); |
|
86 | + |
|
87 | + /* |
|
88 | 88 | * Galois Multiplication lookup tables, |
89 | 89 | * initialized in initTables() |
90 | 90 | */ |
91 | - /** |
|
92 | - * @type array $_gm2 The Galois Multiplication table |
|
93 | - * for muliplying by 2, this should be considered a constant |
|
94 | - */ |
|
95 | - private static $_gm2 = array(); |
|
96 | - |
|
97 | - /** |
|
98 | - * @type array $_gm3 The Galois Multiplication table |
|
99 | - * for muliplying by 3, this should be considered a constant |
|
100 | - */ |
|
101 | - private static $_gm3 = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * @type array $_gm9 The Galois Multiplication table |
|
105 | - * for muliplying by 9, this should be considered a constant |
|
106 | - */ |
|
107 | - private static $_gm9 = array(); |
|
108 | - |
|
109 | - /** |
|
110 | - * @type array $_gm11 The Galois Multiplication table |
|
111 | - * for muliplying by 11, this should be considered a constant |
|
112 | - */ |
|
113 | - private static $_gm11 = array(); |
|
114 | - |
|
115 | - /** |
|
116 | - * @type array $_gm13 The Galois Multiplication table |
|
117 | - * for muliplying by 13, this should be considered a constant |
|
118 | - */ |
|
119 | - private static $_gm13 = array(); |
|
120 | - |
|
121 | - /** |
|
122 | - * @type array $_gm14 The Galois Multiplication table |
|
123 | - * for muliplying by 14, this should be considered a constant |
|
124 | - */ |
|
125 | - private static $_gm14 = array(); |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Constructor |
|
130 | - * Sets the key used for encryption. Also sets the requied block size |
|
131 | - * |
|
132 | - * @param string The cipher name as set in a constant in the child class |
|
133 | - * @param string $key string containing the user supplied encryption key |
|
134 | - * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function __construct($cipher_name, $key, $len = 0) |
|
138 | - { |
|
139 | - // AES will pass in a $len, since it has a fixed key size, other |
|
140 | - // rijndael implementations can use variable key sizes, supported |
|
141 | - // sizes are stored in self::$_key_sizes |
|
142 | - if ($len == 0) |
|
143 | - { |
|
144 | - // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | - $len = strlen($key); |
|
146 | - if (!in_array($len, self::$_key_sizes)) |
|
147 | - { |
|
148 | - $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | - $msg .= "Received $len bytes."; |
|
150 | - trigger_error($msg, E_USER_WARNING); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - // Setup the key |
|
155 | - parent::__construct($cipher_name, $key, $len); |
|
156 | - |
|
157 | - // initialize the tables used for rijndael/aes |
|
158 | - $this->initTables(); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Destructor |
|
164 | - * |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public function __destruct() |
|
168 | - { |
|
169 | - parent::__destruct(); |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Performs Rijndael encryption |
|
175 | - * |
|
176 | - * @param string @text The string to encrypt |
|
177 | - * @return boolean Returns true |
|
178 | - */ |
|
179 | - public function encrypt(&$text) |
|
180 | - { |
|
181 | - // set the operation to decryption |
|
182 | - $this->operation(parent::ENCRYPT); |
|
183 | - |
|
184 | - $loops = 0; |
|
185 | - $key_sz = $this->keySize(); |
|
186 | - $blk_sz = $this->blockSize(); |
|
187 | - |
|
188 | - // if the key and block size is 16, do 10 rounds |
|
189 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
190 | - // if either key or block size is 32, do 14 rounds |
|
191 | - if ($key_sz == 16 && $blk_sz == 16) |
|
192 | - $loops = 10; |
|
193 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | - $loops = 12; |
|
195 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | - $loops = 14; |
|
197 | - |
|
198 | - // now begin the encryption |
|
199 | - $this->addRoundKey($text, 0); |
|
200 | - |
|
201 | - for ($i = 1; $i <= $loops; ++$i) |
|
202 | - { |
|
203 | - $this->byteSub($text); |
|
204 | - $this->shiftRow($text); |
|
205 | - |
|
206 | - // the last iteration does not use mixColumn |
|
207 | - if ($i < $loops) |
|
208 | - $this->mixColumn($text); |
|
209 | - |
|
210 | - $this->addRoundKey($text, $i); |
|
211 | - } |
|
212 | - |
|
213 | - return true; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * Performs Rijndael decryption |
|
219 | - * |
|
220 | - * @param string @text The string to decrypt |
|
221 | - * @return boolean Returns true |
|
222 | - */ |
|
223 | - public function decrypt(&$text) |
|
224 | - { |
|
225 | - // set the operation to decryption |
|
226 | - $this->operation(parent::DECRYPT); |
|
227 | - |
|
228 | - $loops = 0; |
|
229 | - $key_sz = $this->keySize(); |
|
230 | - $blk_sz = $this->blockSize(); |
|
231 | - |
|
232 | - // if the key and block size is 16, do 10 rounds |
|
233 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | - // if either key or block size is 32, do 14 rounds |
|
235 | - if ($key_sz == 16 && $blk_sz == 16) |
|
236 | - $loops = 10; |
|
237 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | - $loops = 12; |
|
239 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | - $loops = 14; |
|
241 | - |
|
242 | - // now begin the decryption |
|
243 | - $this->addRoundKey($text, 0); |
|
244 | - |
|
245 | - for ($i = 1; $i <= $loops; ++$i) |
|
246 | - { |
|
247 | - $this->shiftRow($text); |
|
248 | - $this->byteSub($text); |
|
249 | - $this->addRoundKey($text, $i); |
|
250 | - |
|
251 | - // the last iteration does not use mixColumn |
|
252 | - if ($i < $loops) |
|
253 | - $this->mixColumn($text); |
|
254 | - } |
|
255 | - |
|
256 | - return true; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * Indicates that this is a block cipher |
|
262 | - * |
|
263 | - * @return integer Returns Cipher::BLOCK |
|
264 | - */ |
|
265 | - public function type() |
|
266 | - { |
|
267 | - return parent::BLOCK; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * Do the multiplication required in mixColumn() |
|
273 | - * We follow the description the multiplication from Wikipedia: |
|
274 | - * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | - * |
|
276 | - * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | - * @param integer $byte The value to multipy by $m |
|
278 | - * @return integer The result of the multiplication |
|
279 | - */ |
|
280 | - protected function mixColumnMultiply($m, $byte) |
|
281 | - { |
|
282 | - // if multiplying by 1, then we just return the same number |
|
283 | - if ($m == 0x01) |
|
284 | - return $byte; |
|
285 | - |
|
286 | - $hex = parent::dec2Hex($byte); |
|
287 | - $row = parent::hex2Dec($hex[0]); |
|
288 | - $col = parent::hex2Dec($hex[1]); |
|
289 | - $pos = ($row * 16) + $col; |
|
290 | - |
|
291 | - // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | - if ($m == 0x02) |
|
293 | - return self::$_gm2[$pos]; |
|
294 | - |
|
295 | - // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | - if ($m == 0x03) |
|
297 | - return self::$_gm3[$pos]; |
|
298 | - |
|
299 | - // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | - if ($m == 0x09) |
|
301 | - return self::$_gm9[$pos]; |
|
302 | - |
|
303 | - // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | - if ($m == 0x0b) |
|
305 | - return self::$_gm11[$pos]; |
|
306 | - |
|
307 | - // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | - if ($m == 0x0d) |
|
309 | - return self::$_gm13[$pos]; |
|
310 | - |
|
311 | - // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | - if ($m == 0x0e) |
|
313 | - return self::$_gm14[$pos]; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | - * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | - * never has the same bytes used twice. Note that the first time |
|
321 | - * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | - * $round is given. Every call after that will be inside the rounds. |
|
323 | - * |
|
324 | - * @param string $text The text to encrypt/decrypt |
|
325 | - * @param integer $round The round we are on inside of rijndael() |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function addRoundKey(&$text, $round) |
|
329 | - { |
|
330 | - // length of the xkey |
|
331 | - $ek_len = strlen($this->xkey); |
|
332 | - $len = $this->blockSize(); |
|
333 | - |
|
334 | - if ($this->operation() == parent::ENCRYPT) |
|
335 | - $offset = $round * $len; |
|
336 | - else |
|
337 | - $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | - |
|
339 | - for ($i = 0; $i < $len; ++$i) |
|
340 | - $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Applies the Sbox to each byte of the string passed in |
|
346 | - * This is used in key expansione |
|
347 | - * |
|
348 | - * @param string $text The string to peform the byte subsitution |
|
349 | - * @return void |
|
350 | - */ |
|
351 | - private function byteSub(&$text) |
|
352 | - { |
|
353 | - $max = strlen($text); |
|
354 | - for ($i = 0; $i < $max; ++$i) |
|
355 | - { |
|
356 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | - // and column is numbered in hex (from 0 - f) |
|
358 | - $hex = parent::str2Hex($text[$i]); |
|
359 | - $row = parent::hex2Dec($hex[0]); |
|
360 | - $col = parent::hex2Dec($hex[1]); |
|
361 | - $pos = ($row * 16) + $col; |
|
362 | - |
|
363 | - // return the corresponding value from the sbox |
|
364 | - if ($this->operation() == parent::ENCRYPT) |
|
365 | - $text[$i] = chr(self::$_s[$pos]); |
|
366 | - else // parent::DECRYPT uses the inverse sbox |
|
367 | - $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | - } |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * This function is hard to explain, the easiest way to understand it is to read |
|
374 | - * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | - * |
|
376 | - * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | - * @return void |
|
378 | - */ |
|
379 | - private function mixColumn(&$t) |
|
380 | - { |
|
381 | - $tmp = $t; |
|
382 | - |
|
383 | - // the matrix we use depends on if we are encrypting or decrypting |
|
384 | - if ($this->operation() == parent::ENCRYPT) |
|
385 | - $m = self::$_matrix_mult; |
|
386 | - else // parent::DECRYPT |
|
387 | - $m = self::$_matrix_mult_inv; |
|
388 | - |
|
389 | - // the number of rounds we make depends on the block size of the text |
|
390 | - // used during encryption/decryption |
|
391 | - // 128 has a 4x4 matrix, loop 4 times |
|
392 | - // 192 has a 4x6 matrix, loop 6 times |
|
393 | - // 256 has a 4x8 matrix, loop 8 times |
|
394 | - $max_col = ($this->blockSize() * 8) / 32; |
|
395 | - |
|
396 | - // loop through each column of the matrix |
|
397 | - for ($col = 0; $col < $max_col; ++$col) |
|
398 | - { |
|
399 | - $pos = $col * 4; |
|
400 | - |
|
401 | - $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | - $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | - $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | - $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | - $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | - |
|
407 | - $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | - $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | - $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | - $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | - $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | - |
|
413 | - $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | - $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | - $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | - $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | - $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | - |
|
419 | - $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | - $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | - $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | - $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | - $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | - * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | - * Row shifts depend on the bit size of the block $text. |
|
432 | - * Rijndael-128/AES: 4x4 matrix |
|
433 | - * Rijndael-192: 6x4 matrix |
|
434 | - * Rijndael-256: 8x4 matrix |
|
435 | - * |
|
436 | - * @param string $text A 16, 24, or 32 byte string |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - private function shiftRow(&$text) |
|
440 | - { |
|
441 | - /* |
|
91 | + /** |
|
92 | + * @type array $_gm2 The Galois Multiplication table |
|
93 | + * for muliplying by 2, this should be considered a constant |
|
94 | + */ |
|
95 | + private static $_gm2 = array(); |
|
96 | + |
|
97 | + /** |
|
98 | + * @type array $_gm3 The Galois Multiplication table |
|
99 | + * for muliplying by 3, this should be considered a constant |
|
100 | + */ |
|
101 | + private static $_gm3 = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * @type array $_gm9 The Galois Multiplication table |
|
105 | + * for muliplying by 9, this should be considered a constant |
|
106 | + */ |
|
107 | + private static $_gm9 = array(); |
|
108 | + |
|
109 | + /** |
|
110 | + * @type array $_gm11 The Galois Multiplication table |
|
111 | + * for muliplying by 11, this should be considered a constant |
|
112 | + */ |
|
113 | + private static $_gm11 = array(); |
|
114 | + |
|
115 | + /** |
|
116 | + * @type array $_gm13 The Galois Multiplication table |
|
117 | + * for muliplying by 13, this should be considered a constant |
|
118 | + */ |
|
119 | + private static $_gm13 = array(); |
|
120 | + |
|
121 | + /** |
|
122 | + * @type array $_gm14 The Galois Multiplication table |
|
123 | + * for muliplying by 14, this should be considered a constant |
|
124 | + */ |
|
125 | + private static $_gm14 = array(); |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Constructor |
|
130 | + * Sets the key used for encryption. Also sets the requied block size |
|
131 | + * |
|
132 | + * @param string The cipher name as set in a constant in the child class |
|
133 | + * @param string $key string containing the user supplied encryption key |
|
134 | + * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function __construct($cipher_name, $key, $len = 0) |
|
138 | + { |
|
139 | + // AES will pass in a $len, since it has a fixed key size, other |
|
140 | + // rijndael implementations can use variable key sizes, supported |
|
141 | + // sizes are stored in self::$_key_sizes |
|
142 | + if ($len == 0) |
|
143 | + { |
|
144 | + // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | + $len = strlen($key); |
|
146 | + if (!in_array($len, self::$_key_sizes)) |
|
147 | + { |
|
148 | + $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | + $msg .= "Received $len bytes."; |
|
150 | + trigger_error($msg, E_USER_WARNING); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + // Setup the key |
|
155 | + parent::__construct($cipher_name, $key, $len); |
|
156 | + |
|
157 | + // initialize the tables used for rijndael/aes |
|
158 | + $this->initTables(); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Destructor |
|
164 | + * |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public function __destruct() |
|
168 | + { |
|
169 | + parent::__destruct(); |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Performs Rijndael encryption |
|
175 | + * |
|
176 | + * @param string @text The string to encrypt |
|
177 | + * @return boolean Returns true |
|
178 | + */ |
|
179 | + public function encrypt(&$text) |
|
180 | + { |
|
181 | + // set the operation to decryption |
|
182 | + $this->operation(parent::ENCRYPT); |
|
183 | + |
|
184 | + $loops = 0; |
|
185 | + $key_sz = $this->keySize(); |
|
186 | + $blk_sz = $this->blockSize(); |
|
187 | + |
|
188 | + // if the key and block size is 16, do 10 rounds |
|
189 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
190 | + // if either key or block size is 32, do 14 rounds |
|
191 | + if ($key_sz == 16 && $blk_sz == 16) |
|
192 | + $loops = 10; |
|
193 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | + $loops = 12; |
|
195 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | + $loops = 14; |
|
197 | + |
|
198 | + // now begin the encryption |
|
199 | + $this->addRoundKey($text, 0); |
|
200 | + |
|
201 | + for ($i = 1; $i <= $loops; ++$i) |
|
202 | + { |
|
203 | + $this->byteSub($text); |
|
204 | + $this->shiftRow($text); |
|
205 | + |
|
206 | + // the last iteration does not use mixColumn |
|
207 | + if ($i < $loops) |
|
208 | + $this->mixColumn($text); |
|
209 | + |
|
210 | + $this->addRoundKey($text, $i); |
|
211 | + } |
|
212 | + |
|
213 | + return true; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * Performs Rijndael decryption |
|
219 | + * |
|
220 | + * @param string @text The string to decrypt |
|
221 | + * @return boolean Returns true |
|
222 | + */ |
|
223 | + public function decrypt(&$text) |
|
224 | + { |
|
225 | + // set the operation to decryption |
|
226 | + $this->operation(parent::DECRYPT); |
|
227 | + |
|
228 | + $loops = 0; |
|
229 | + $key_sz = $this->keySize(); |
|
230 | + $blk_sz = $this->blockSize(); |
|
231 | + |
|
232 | + // if the key and block size is 16, do 10 rounds |
|
233 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | + // if either key or block size is 32, do 14 rounds |
|
235 | + if ($key_sz == 16 && $blk_sz == 16) |
|
236 | + $loops = 10; |
|
237 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | + $loops = 12; |
|
239 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | + $loops = 14; |
|
241 | + |
|
242 | + // now begin the decryption |
|
243 | + $this->addRoundKey($text, 0); |
|
244 | + |
|
245 | + for ($i = 1; $i <= $loops; ++$i) |
|
246 | + { |
|
247 | + $this->shiftRow($text); |
|
248 | + $this->byteSub($text); |
|
249 | + $this->addRoundKey($text, $i); |
|
250 | + |
|
251 | + // the last iteration does not use mixColumn |
|
252 | + if ($i < $loops) |
|
253 | + $this->mixColumn($text); |
|
254 | + } |
|
255 | + |
|
256 | + return true; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * Indicates that this is a block cipher |
|
262 | + * |
|
263 | + * @return integer Returns Cipher::BLOCK |
|
264 | + */ |
|
265 | + public function type() |
|
266 | + { |
|
267 | + return parent::BLOCK; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * Do the multiplication required in mixColumn() |
|
273 | + * We follow the description the multiplication from Wikipedia: |
|
274 | + * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | + * |
|
276 | + * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | + * @param integer $byte The value to multipy by $m |
|
278 | + * @return integer The result of the multiplication |
|
279 | + */ |
|
280 | + protected function mixColumnMultiply($m, $byte) |
|
281 | + { |
|
282 | + // if multiplying by 1, then we just return the same number |
|
283 | + if ($m == 0x01) |
|
284 | + return $byte; |
|
285 | + |
|
286 | + $hex = parent::dec2Hex($byte); |
|
287 | + $row = parent::hex2Dec($hex[0]); |
|
288 | + $col = parent::hex2Dec($hex[1]); |
|
289 | + $pos = ($row * 16) + $col; |
|
290 | + |
|
291 | + // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | + if ($m == 0x02) |
|
293 | + return self::$_gm2[$pos]; |
|
294 | + |
|
295 | + // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | + if ($m == 0x03) |
|
297 | + return self::$_gm3[$pos]; |
|
298 | + |
|
299 | + // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | + if ($m == 0x09) |
|
301 | + return self::$_gm9[$pos]; |
|
302 | + |
|
303 | + // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | + if ($m == 0x0b) |
|
305 | + return self::$_gm11[$pos]; |
|
306 | + |
|
307 | + // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | + if ($m == 0x0d) |
|
309 | + return self::$_gm13[$pos]; |
|
310 | + |
|
311 | + // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | + if ($m == 0x0e) |
|
313 | + return self::$_gm14[$pos]; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | + * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | + * never has the same bytes used twice. Note that the first time |
|
321 | + * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | + * $round is given. Every call after that will be inside the rounds. |
|
323 | + * |
|
324 | + * @param string $text The text to encrypt/decrypt |
|
325 | + * @param integer $round The round we are on inside of rijndael() |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function addRoundKey(&$text, $round) |
|
329 | + { |
|
330 | + // length of the xkey |
|
331 | + $ek_len = strlen($this->xkey); |
|
332 | + $len = $this->blockSize(); |
|
333 | + |
|
334 | + if ($this->operation() == parent::ENCRYPT) |
|
335 | + $offset = $round * $len; |
|
336 | + else |
|
337 | + $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | + |
|
339 | + for ($i = 0; $i < $len; ++$i) |
|
340 | + $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Applies the Sbox to each byte of the string passed in |
|
346 | + * This is used in key expansione |
|
347 | + * |
|
348 | + * @param string $text The string to peform the byte subsitution |
|
349 | + * @return void |
|
350 | + */ |
|
351 | + private function byteSub(&$text) |
|
352 | + { |
|
353 | + $max = strlen($text); |
|
354 | + for ($i = 0; $i < $max; ++$i) |
|
355 | + { |
|
356 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | + // and column is numbered in hex (from 0 - f) |
|
358 | + $hex = parent::str2Hex($text[$i]); |
|
359 | + $row = parent::hex2Dec($hex[0]); |
|
360 | + $col = parent::hex2Dec($hex[1]); |
|
361 | + $pos = ($row * 16) + $col; |
|
362 | + |
|
363 | + // return the corresponding value from the sbox |
|
364 | + if ($this->operation() == parent::ENCRYPT) |
|
365 | + $text[$i] = chr(self::$_s[$pos]); |
|
366 | + else // parent::DECRYPT uses the inverse sbox |
|
367 | + $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | + } |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * This function is hard to explain, the easiest way to understand it is to read |
|
374 | + * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | + * |
|
376 | + * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | + * @return void |
|
378 | + */ |
|
379 | + private function mixColumn(&$t) |
|
380 | + { |
|
381 | + $tmp = $t; |
|
382 | + |
|
383 | + // the matrix we use depends on if we are encrypting or decrypting |
|
384 | + if ($this->operation() == parent::ENCRYPT) |
|
385 | + $m = self::$_matrix_mult; |
|
386 | + else // parent::DECRYPT |
|
387 | + $m = self::$_matrix_mult_inv; |
|
388 | + |
|
389 | + // the number of rounds we make depends on the block size of the text |
|
390 | + // used during encryption/decryption |
|
391 | + // 128 has a 4x4 matrix, loop 4 times |
|
392 | + // 192 has a 4x6 matrix, loop 6 times |
|
393 | + // 256 has a 4x8 matrix, loop 8 times |
|
394 | + $max_col = ($this->blockSize() * 8) / 32; |
|
395 | + |
|
396 | + // loop through each column of the matrix |
|
397 | + for ($col = 0; $col < $max_col; ++$col) |
|
398 | + { |
|
399 | + $pos = $col * 4; |
|
400 | + |
|
401 | + $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | + $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | + $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | + $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | + $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | + |
|
407 | + $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | + $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | + $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | + $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | + $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | + |
|
413 | + $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | + $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | + $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | + $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | + $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | + |
|
419 | + $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | + $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | + $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | + $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | + $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | + * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | + * Row shifts depend on the bit size of the block $text. |
|
432 | + * Rijndael-128/AES: 4x4 matrix |
|
433 | + * Rijndael-192: 6x4 matrix |
|
434 | + * Rijndael-256: 8x4 matrix |
|
435 | + * |
|
436 | + * @param string $text A 16, 24, or 32 byte string |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + private function shiftRow(&$text) |
|
440 | + { |
|
441 | + /* |
|
442 | 442 | * Rijndael-128 / AES |
443 | 443 | */ |
444 | - if ($this->blockSize() == 16) |
|
445 | - { |
|
446 | - if ($this->operation() == parent::ENCRYPT) |
|
447 | - { |
|
448 | - // create a 4x4 matrix |
|
449 | - // row 0 is unchanged, |
|
450 | - // shift row 1 left 1 byte |
|
451 | - // shift row 2 left 2 bytes |
|
452 | - // shift row 3 left 3 bytes |
|
453 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | - $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | - $text[12].$text[1].$text[6].$text[11]; |
|
456 | - } else // parent::DECRYPT |
|
457 | - { |
|
458 | - // create a 4x4 matrix |
|
459 | - // row 0 is unchanged, |
|
460 | - // shift row 1 right 1 byte |
|
461 | - // shift row 2 right 2 bytes |
|
462 | - // shift row 3 right 3 bytes |
|
463 | - $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
464 | - $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
465 | - $text[12].$text[9].$text[6].$text[3]; |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - /* |
|
444 | + if ($this->blockSize() == 16) |
|
445 | + { |
|
446 | + if ($this->operation() == parent::ENCRYPT) |
|
447 | + { |
|
448 | + // create a 4x4 matrix |
|
449 | + // row 0 is unchanged, |
|
450 | + // shift row 1 left 1 byte |
|
451 | + // shift row 2 left 2 bytes |
|
452 | + // shift row 3 left 3 bytes |
|
453 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | + $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | + $text[12].$text[1].$text[6].$text[11]; |
|
456 | + } else // parent::DECRYPT |
|
457 | + { |
|
458 | + // create a 4x4 matrix |
|
459 | + // row 0 is unchanged, |
|
460 | + // shift row 1 right 1 byte |
|
461 | + // shift row 2 right 2 bytes |
|
462 | + // shift row 3 right 3 bytes |
|
463 | + $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
464 | + $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
465 | + $text[12].$text[9].$text[6].$text[3]; |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + /* |
|
470 | 470 | * Rijndael-192 |
471 | 471 | */ |
472 | - if ($this->blockSize() == 24) |
|
473 | - { |
|
474 | - if ($this->operation() == parent::ENCRYPT) |
|
475 | - { |
|
476 | - // create a 6x4 matrix |
|
477 | - // row 0 is unchanged |
|
478 | - // shift row 1 left 1 byte |
|
479 | - // shift row 2 left 2 bytes |
|
480 | - // shift row 3 left 3 bytes |
|
481 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
482 | - $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
483 | - $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
484 | - $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
485 | - |
|
486 | - } else // parent::DECRYPT |
|
487 | - { |
|
488 | - // create a 6x4 matrix |
|
489 | - // row 0 is unchanged |
|
490 | - // shift row 1 right 1 byte |
|
491 | - // shift row 2 right 2 bytes |
|
492 | - // shift row 3 right 3 bytes |
|
493 | - $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
494 | - $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
495 | - $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
496 | - $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
497 | - } |
|
498 | - } |
|
499 | - |
|
500 | - /* |
|
472 | + if ($this->blockSize() == 24) |
|
473 | + { |
|
474 | + if ($this->operation() == parent::ENCRYPT) |
|
475 | + { |
|
476 | + // create a 6x4 matrix |
|
477 | + // row 0 is unchanged |
|
478 | + // shift row 1 left 1 byte |
|
479 | + // shift row 2 left 2 bytes |
|
480 | + // shift row 3 left 3 bytes |
|
481 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
482 | + $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
483 | + $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
484 | + $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
485 | + |
|
486 | + } else // parent::DECRYPT |
|
487 | + { |
|
488 | + // create a 6x4 matrix |
|
489 | + // row 0 is unchanged |
|
490 | + // shift row 1 right 1 byte |
|
491 | + // shift row 2 right 2 bytes |
|
492 | + // shift row 3 right 3 bytes |
|
493 | + $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
494 | + $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
495 | + $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
496 | + $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
497 | + } |
|
498 | + } |
|
499 | + |
|
500 | + /* |
|
501 | 501 | * Rijndael-256 |
502 | 502 | */ |
503 | - if ($this->blockSize() == 32) |
|
504 | - { |
|
505 | - if ($this->operation() == parent::ENCRYPT) |
|
506 | - { |
|
507 | - // create an 8x4 matrix |
|
508 | - // row 0 is unchanged |
|
509 | - // shift row 1 left 1 byte |
|
510 | - // shift row 2 left 3 bytes |
|
511 | - // shift row 3 left 4 bytes |
|
512 | - $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
513 | - $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
514 | - $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
515 | - $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
516 | - $text[28].$text[1].$text[10].$text[15]; |
|
517 | - } else // parent::DECRYPT |
|
518 | - { |
|
519 | - // create an 8x4 matrix |
|
520 | - // row 0 is unchanged |
|
521 | - // shift row 1 right 1 byte |
|
522 | - // shift row 2 right 3 bytes |
|
523 | - // shift row 3 right 4 bytes |
|
524 | - $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
525 | - $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
526 | - $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
527 | - $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
528 | - $text[28].$text[25].$text[18].$text[15]; |
|
529 | - } |
|
530 | - } |
|
531 | - } |
|
532 | - |
|
533 | - |
|
534 | - /** |
|
535 | - * Applies the Sbox to each byte of the string passed in. |
|
536 | - * This is similar to subByte(), but Unlike subByte() we do not use |
|
537 | - * the _s_inv[] table. This function is only used in expandKey(), |
|
538 | - * which is implemented by the class that inherits this class |
|
539 | - * |
|
540 | - * @param string $text The string to peform the byte subsitution |
|
541 | - * @return string The string with the subsituted bytes |
|
542 | - */ |
|
543 | - private function subWord(&$text) |
|
544 | - { |
|
545 | - $max = strlen($text); |
|
546 | - for ($i = 0; $i < $max; ++$i) |
|
547 | - { |
|
548 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
549 | - // and column is numbered in hex (from 0 - f) |
|
550 | - $hex = parent::str2Hex($text[$i]); |
|
551 | - $row = parent::hex2Dec($hex[0]); |
|
552 | - $col = parent::hex2Dec($hex[1]); |
|
553 | - $pos = ($row * 16) + $col; |
|
554 | - |
|
555 | - $text[$i] = chr(self::$_s[$pos]); |
|
556 | - } |
|
557 | - } |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * Rotate a 4 byte block of the key, moving the first byte to |
|
562 | - * to the end, and shifting everything left |
|
563 | - * Used in key Expandsion |
|
564 | - * |
|
565 | - * @param string $key_block A 4 byte string |
|
566 | - * @return string The shifted 4 byte string |
|
567 | - */ |
|
568 | - private function rotWord($key_block) |
|
569 | - { |
|
570 | - return substr($key_block, 1, 3).$key_block[0]; |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Returns 4 bytes from the expanded key starting at the given offset |
|
576 | - * Used during expandKey() |
|
577 | - * |
|
578 | - * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
579 | - * @return string A 4 byte string from the key |
|
580 | - */ |
|
581 | - private function ek($offset) |
|
582 | - { |
|
583 | - return substr($this->xkey, $offset, 4); |
|
584 | - } |
|
585 | - |
|
586 | - |
|
587 | - /** |
|
588 | - * Returns 4 bytes of the original key from the given offset |
|
589 | - * Used during expandKey() |
|
590 | - * |
|
591 | - * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
592 | - * @return string A 4 byte string from the key |
|
593 | - */ |
|
594 | - private function k($offset) |
|
595 | - { |
|
596 | - return substr($this->key(), $offset, 4); |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - /** |
|
601 | - * Return the 4 byte round constant used during expandKey(). |
|
602 | - * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
603 | - * 0x01000000 to create a 4 byte value |
|
604 | - * |
|
605 | - * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
606 | - * @return integer A 4 byte value |
|
607 | - */ |
|
608 | - private function rcon($pos) |
|
609 | - { |
|
610 | - return (self::$_rcon[$pos] * 0x01000000); |
|
611 | - } |
|
612 | - |
|
613 | - |
|
614 | - /** |
|
615 | - * Expands the key |
|
616 | - * The key expands based on the block size as well as the key size |
|
617 | - * |
|
618 | - * @return boolean|null |
|
619 | - */ |
|
620 | - protected function expandKey() |
|
621 | - { |
|
622 | - if ($this->keySize() == 16) |
|
623 | - return $this->expandKey128(); |
|
624 | - else if ($this->keySize() == 24) |
|
625 | - return $this->expandKey192(); |
|
626 | - else if ($this->keySize() == 32) |
|
627 | - return $this->expandKey256(); |
|
628 | - } |
|
629 | - |
|
630 | - |
|
631 | - /** |
|
632 | - * Expand a 16 byte key, the size it is expanded to varies |
|
633 | - * based on the block size of the Rijndael implementation chosen |
|
634 | - * |
|
635 | - * @return boolean |
|
636 | - */ |
|
637 | - private function expandKey128() |
|
638 | - { |
|
639 | - // clear the xkey, we're creating a new one |
|
640 | - $this->xkey = ""; |
|
641 | - $max = 0; |
|
642 | - |
|
643 | - // the number of rounds we make depends on the block size of the text |
|
644 | - // used during encryption/decryption |
|
645 | - if ($this->blockSize() == 16) |
|
646 | - $max = 44; |
|
647 | - if ($this->blockSize() == 24) |
|
648 | - $max = 78; |
|
649 | - if ($this->blockSize() == 32) |
|
650 | - $max = 120; |
|
651 | - |
|
652 | - // 16 byte key expands to 176 bytes |
|
653 | - for ($i = 0; $i < $max; ++$i) |
|
654 | - { |
|
655 | - if ($i >= 0 && $i <= 3) |
|
656 | - $this->xkey .= $this->k($i * 4); |
|
657 | - else if (($i % 4) == 0) |
|
658 | - { |
|
659 | - // rotate the 4 bytes |
|
660 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
661 | - |
|
662 | - // apply the sbox |
|
663 | - $this->subWord($subword); |
|
664 | - |
|
665 | - // return 4 byte value based on self::$_rcon table |
|
666 | - //$rcon = $this->rcon(($i / 4) - 1); |
|
667 | - $rcon = $this->rcon(($i / 4)); |
|
668 | - |
|
669 | - // grab 4 bytes from $this->extended_key |
|
670 | - $ek = $this->ek(($i - 4) * 4); |
|
671 | - |
|
672 | - $h1 = parent::str2Hex($subword); |
|
673 | - $h2 = parent::dec2Hex($rcon); |
|
674 | - $h3 = parent::str2Hex($ek); |
|
675 | - $res = parent::xorHex($h1, $h2, $h3); |
|
676 | - $this->xkey .= parent::hex2Str($res); |
|
677 | - } else |
|
678 | - { |
|
679 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
680 | - $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
681 | - $res = parent::xorHex($h1, $h2); |
|
682 | - $this->xkey .= parent::hex2Str($res); |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - return true; |
|
687 | - } |
|
688 | - |
|
689 | - |
|
690 | - /** |
|
691 | - * Expand a 24 byte key, the size it is expanded to varies |
|
692 | - * based on the block size of the Rijndael implementation chosen |
|
693 | - * |
|
694 | - * @return boolean |
|
695 | - */ |
|
696 | - private function expandKey192() |
|
697 | - { |
|
698 | - // clear the xkey, we're creating a new one |
|
699 | - $this->xkey = ""; |
|
700 | - $max = 0; |
|
701 | - |
|
702 | - // the number of rounds we make depends on the block size of the text |
|
703 | - // used during encryption/decryption |
|
704 | - if ($this->blockSize() == 16) |
|
705 | - $max = 52; |
|
706 | - if ($this->blockSize() == 24) |
|
707 | - $max = 78; |
|
708 | - if ($this->blockSize() == 32) |
|
709 | - $max = 120; |
|
710 | - |
|
711 | - // 24 byte key expands to 208 bytes |
|
712 | - for ($i = 0; $i < $max; ++$i) |
|
713 | - { |
|
714 | - if ($i >= 0 && $i <= 5) |
|
715 | - $this->xkey .= $this->k($i * 4); |
|
716 | - else if (($i % 6) == 0) |
|
717 | - { |
|
718 | - // rotate the 4 bytes |
|
719 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
720 | - |
|
721 | - // apply the sbox |
|
722 | - $this->subWord($subword); |
|
723 | - |
|
724 | - // return 4 byte value based on self::$_rcon table |
|
725 | - //$rcon = $this->rcon(($i / 6) - 1); |
|
726 | - $rcon = $this->rcon(($i / 6)); |
|
727 | - |
|
728 | - // grab 4 bytes from $this->extended_key |
|
729 | - $ek = $this->ek(($i - 6) * 4); |
|
730 | - |
|
731 | - $h1 = parent::str2Hex($subword); |
|
732 | - $h2 = parent::dec2Hex($rcon); |
|
733 | - $h3 = parent::str2Hex($ek); |
|
734 | - $res = parent::xorHex($h1, $h2, $h3); |
|
735 | - $this->xkey .= parent::hex2Str($res); |
|
736 | - } else |
|
737 | - { |
|
738 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
739 | - $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
740 | - $res = parent::xorHex($h1, $h2); |
|
741 | - $this->xkey .= parent::hex2Str($res); |
|
742 | - } |
|
743 | - } |
|
744 | - |
|
745 | - return true; |
|
746 | - } |
|
747 | - |
|
748 | - |
|
749 | - /** |
|
750 | - * Expand a 32 byte key, the size it is expanded to varies |
|
751 | - * based on the block size of the Rijndael implementation chosen |
|
752 | - * |
|
753 | - * @return boolean |
|
754 | - */ |
|
755 | - private function expandKey256() |
|
756 | - { |
|
757 | - // clear the xkey, we're creating a new one |
|
758 | - $this->xkey = ""; |
|
759 | - $max = 0; |
|
760 | - |
|
761 | - // the number of rounds we make depends on the block size of the text |
|
762 | - // used during encryption/decryption |
|
763 | - if ($this->blockSize() == 16) |
|
764 | - $max = 60; |
|
765 | - if ($this->blockSize() == 24) |
|
766 | - $max = 90; |
|
767 | - if ($this->blockSize() == 32) |
|
768 | - $max = 120; |
|
769 | - |
|
770 | - // 32 byte key expands to 240 bytes |
|
771 | - for ($i = 0; $i < $max; ++$i) |
|
772 | - { |
|
773 | - if ($i >= 0 && $i <= 7) |
|
774 | - $this->xkey .= $this->k($i * 4); |
|
775 | - else if ($i % 8 == 0) |
|
776 | - { |
|
777 | - // rotate the 4 bytes |
|
778 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
779 | - |
|
780 | - // apply the sbox |
|
781 | - $this->subWord($subword); |
|
782 | - |
|
783 | - // return 4 byte value based on self::$_rcon table |
|
784 | - $rcon = $this->rcon(($i / 8)); |
|
785 | - |
|
786 | - // grab 4 bytes from $this->extended_key |
|
787 | - $ek = $this->ek(($i - 8) * 4); |
|
788 | - |
|
789 | - $h1 = parent::str2Hex($subword); |
|
790 | - $h2 = parent::dec2Hex($rcon); |
|
791 | - $h3 = parent::str2Hex($ek); |
|
792 | - $res = parent::xorHex($h1, $h2, $h3); |
|
793 | - $this->xkey .= parent::hex2Str($res); |
|
794 | - } |
|
795 | - else if ($i % 4 == 0) |
|
796 | - { |
|
797 | - // get the subsitution from the s-box |
|
798 | - $subword = $this->ek(($i - 1) * 4); |
|
799 | - $this->subWord($subword); |
|
800 | - |
|
801 | - // get the extended key part |
|
802 | - $ek = $this->ek(($i - 8) * 4); |
|
803 | - |
|
804 | - // xor the two parts |
|
805 | - $h1 = parent::str2Hex($subword); |
|
806 | - $h2 = parent::str2Hex($ek); |
|
807 | - $res = parent::xorHex($h1, $h2); |
|
808 | - $this->xkey .= parent::hex2Str($res); |
|
809 | - } else |
|
810 | - { |
|
811 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
812 | - $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
813 | - $res = parent::xorHex($h1, $h2); |
|
814 | - $this->xkey .= parent::hex2Str($res); |
|
815 | - } |
|
816 | - } |
|
817 | - |
|
818 | - return true; |
|
819 | - } |
|
820 | - |
|
821 | - |
|
822 | - /** |
|
823 | - * Initalizes the tables used for Rijndael/AES encryption |
|
824 | - * |
|
825 | - * @return void |
|
826 | - */ |
|
827 | - private function initTables() |
|
828 | - { |
|
829 | - // the sbox used for encryption |
|
830 | - self::$_s = array( |
|
831 | - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
832 | - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
833 | - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
834 | - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
835 | - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
836 | - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
837 | - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
838 | - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
839 | - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
840 | - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
841 | - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
842 | - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
843 | - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
844 | - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
845 | - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
846 | - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
847 | - ); |
|
848 | - |
|
849 | - // the inverse sbox used for decryption |
|
850 | - self::$_s_inv = array( |
|
851 | - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
852 | - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
853 | - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
854 | - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
855 | - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
856 | - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
857 | - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
858 | - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
859 | - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
860 | - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
861 | - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
862 | - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
863 | - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
864 | - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
865 | - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
866 | - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
867 | - ); |
|
868 | - |
|
869 | - // used in mixColumn() during encryption |
|
870 | - self::$_matrix_mult = array( |
|
871 | - 0x02, 0x01, 0x01, 0x03, |
|
872 | - 0x03, 0x02, 0x01, 0x01, |
|
873 | - 0x01, 0x03, 0x02, 0x01, |
|
874 | - 0x01, 0x01, 0x03, 0x02 |
|
875 | - ); |
|
876 | - |
|
877 | - // used in mixColumn() during decryption |
|
878 | - self::$_matrix_mult_inv = array( |
|
879 | - 0x0e, 0x09, 0x0d, 0x0b, |
|
880 | - 0x0b, 0x0e, 0x09, 0x0d, |
|
881 | - 0x0d, 0x0b, 0x0e, 0x09, |
|
882 | - 0x09, 0x0d, 0x0b, 0x0e |
|
883 | - ); |
|
884 | - |
|
885 | - // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
886 | - // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
887 | - // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
888 | - // the other values are used for larger block/key combinations supported by Rijndael |
|
889 | - // NOTE: self::$_rcon[0] is never used |
|
890 | - self::$_rcon = array( |
|
891 | - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
892 | - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
893 | - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
894 | - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
895 | - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
896 | - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
897 | - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
898 | - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
899 | - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
900 | - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
901 | - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
902 | - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
903 | - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
904 | - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
905 | - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
906 | - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
907 | - ); |
|
908 | - |
|
909 | - /* |
|
503 | + if ($this->blockSize() == 32) |
|
504 | + { |
|
505 | + if ($this->operation() == parent::ENCRYPT) |
|
506 | + { |
|
507 | + // create an 8x4 matrix |
|
508 | + // row 0 is unchanged |
|
509 | + // shift row 1 left 1 byte |
|
510 | + // shift row 2 left 3 bytes |
|
511 | + // shift row 3 left 4 bytes |
|
512 | + $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
513 | + $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
514 | + $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
515 | + $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
516 | + $text[28].$text[1].$text[10].$text[15]; |
|
517 | + } else // parent::DECRYPT |
|
518 | + { |
|
519 | + // create an 8x4 matrix |
|
520 | + // row 0 is unchanged |
|
521 | + // shift row 1 right 1 byte |
|
522 | + // shift row 2 right 3 bytes |
|
523 | + // shift row 3 right 4 bytes |
|
524 | + $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
525 | + $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
526 | + $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
527 | + $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
528 | + $text[28].$text[25].$text[18].$text[15]; |
|
529 | + } |
|
530 | + } |
|
531 | + } |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * Applies the Sbox to each byte of the string passed in. |
|
536 | + * This is similar to subByte(), but Unlike subByte() we do not use |
|
537 | + * the _s_inv[] table. This function is only used in expandKey(), |
|
538 | + * which is implemented by the class that inherits this class |
|
539 | + * |
|
540 | + * @param string $text The string to peform the byte subsitution |
|
541 | + * @return string The string with the subsituted bytes |
|
542 | + */ |
|
543 | + private function subWord(&$text) |
|
544 | + { |
|
545 | + $max = strlen($text); |
|
546 | + for ($i = 0; $i < $max; ++$i) |
|
547 | + { |
|
548 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
549 | + // and column is numbered in hex (from 0 - f) |
|
550 | + $hex = parent::str2Hex($text[$i]); |
|
551 | + $row = parent::hex2Dec($hex[0]); |
|
552 | + $col = parent::hex2Dec($hex[1]); |
|
553 | + $pos = ($row * 16) + $col; |
|
554 | + |
|
555 | + $text[$i] = chr(self::$_s[$pos]); |
|
556 | + } |
|
557 | + } |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * Rotate a 4 byte block of the key, moving the first byte to |
|
562 | + * to the end, and shifting everything left |
|
563 | + * Used in key Expandsion |
|
564 | + * |
|
565 | + * @param string $key_block A 4 byte string |
|
566 | + * @return string The shifted 4 byte string |
|
567 | + */ |
|
568 | + private function rotWord($key_block) |
|
569 | + { |
|
570 | + return substr($key_block, 1, 3).$key_block[0]; |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Returns 4 bytes from the expanded key starting at the given offset |
|
576 | + * Used during expandKey() |
|
577 | + * |
|
578 | + * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
579 | + * @return string A 4 byte string from the key |
|
580 | + */ |
|
581 | + private function ek($offset) |
|
582 | + { |
|
583 | + return substr($this->xkey, $offset, 4); |
|
584 | + } |
|
585 | + |
|
586 | + |
|
587 | + /** |
|
588 | + * Returns 4 bytes of the original key from the given offset |
|
589 | + * Used during expandKey() |
|
590 | + * |
|
591 | + * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
592 | + * @return string A 4 byte string from the key |
|
593 | + */ |
|
594 | + private function k($offset) |
|
595 | + { |
|
596 | + return substr($this->key(), $offset, 4); |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + /** |
|
601 | + * Return the 4 byte round constant used during expandKey(). |
|
602 | + * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
603 | + * 0x01000000 to create a 4 byte value |
|
604 | + * |
|
605 | + * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
606 | + * @return integer A 4 byte value |
|
607 | + */ |
|
608 | + private function rcon($pos) |
|
609 | + { |
|
610 | + return (self::$_rcon[$pos] * 0x01000000); |
|
611 | + } |
|
612 | + |
|
613 | + |
|
614 | + /** |
|
615 | + * Expands the key |
|
616 | + * The key expands based on the block size as well as the key size |
|
617 | + * |
|
618 | + * @return boolean|null |
|
619 | + */ |
|
620 | + protected function expandKey() |
|
621 | + { |
|
622 | + if ($this->keySize() == 16) |
|
623 | + return $this->expandKey128(); |
|
624 | + else if ($this->keySize() == 24) |
|
625 | + return $this->expandKey192(); |
|
626 | + else if ($this->keySize() == 32) |
|
627 | + return $this->expandKey256(); |
|
628 | + } |
|
629 | + |
|
630 | + |
|
631 | + /** |
|
632 | + * Expand a 16 byte key, the size it is expanded to varies |
|
633 | + * based on the block size of the Rijndael implementation chosen |
|
634 | + * |
|
635 | + * @return boolean |
|
636 | + */ |
|
637 | + private function expandKey128() |
|
638 | + { |
|
639 | + // clear the xkey, we're creating a new one |
|
640 | + $this->xkey = ""; |
|
641 | + $max = 0; |
|
642 | + |
|
643 | + // the number of rounds we make depends on the block size of the text |
|
644 | + // used during encryption/decryption |
|
645 | + if ($this->blockSize() == 16) |
|
646 | + $max = 44; |
|
647 | + if ($this->blockSize() == 24) |
|
648 | + $max = 78; |
|
649 | + if ($this->blockSize() == 32) |
|
650 | + $max = 120; |
|
651 | + |
|
652 | + // 16 byte key expands to 176 bytes |
|
653 | + for ($i = 0; $i < $max; ++$i) |
|
654 | + { |
|
655 | + if ($i >= 0 && $i <= 3) |
|
656 | + $this->xkey .= $this->k($i * 4); |
|
657 | + else if (($i % 4) == 0) |
|
658 | + { |
|
659 | + // rotate the 4 bytes |
|
660 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
661 | + |
|
662 | + // apply the sbox |
|
663 | + $this->subWord($subword); |
|
664 | + |
|
665 | + // return 4 byte value based on self::$_rcon table |
|
666 | + //$rcon = $this->rcon(($i / 4) - 1); |
|
667 | + $rcon = $this->rcon(($i / 4)); |
|
668 | + |
|
669 | + // grab 4 bytes from $this->extended_key |
|
670 | + $ek = $this->ek(($i - 4) * 4); |
|
671 | + |
|
672 | + $h1 = parent::str2Hex($subword); |
|
673 | + $h2 = parent::dec2Hex($rcon); |
|
674 | + $h3 = parent::str2Hex($ek); |
|
675 | + $res = parent::xorHex($h1, $h2, $h3); |
|
676 | + $this->xkey .= parent::hex2Str($res); |
|
677 | + } else |
|
678 | + { |
|
679 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
680 | + $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
681 | + $res = parent::xorHex($h1, $h2); |
|
682 | + $this->xkey .= parent::hex2Str($res); |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + return true; |
|
687 | + } |
|
688 | + |
|
689 | + |
|
690 | + /** |
|
691 | + * Expand a 24 byte key, the size it is expanded to varies |
|
692 | + * based on the block size of the Rijndael implementation chosen |
|
693 | + * |
|
694 | + * @return boolean |
|
695 | + */ |
|
696 | + private function expandKey192() |
|
697 | + { |
|
698 | + // clear the xkey, we're creating a new one |
|
699 | + $this->xkey = ""; |
|
700 | + $max = 0; |
|
701 | + |
|
702 | + // the number of rounds we make depends on the block size of the text |
|
703 | + // used during encryption/decryption |
|
704 | + if ($this->blockSize() == 16) |
|
705 | + $max = 52; |
|
706 | + if ($this->blockSize() == 24) |
|
707 | + $max = 78; |
|
708 | + if ($this->blockSize() == 32) |
|
709 | + $max = 120; |
|
710 | + |
|
711 | + // 24 byte key expands to 208 bytes |
|
712 | + for ($i = 0; $i < $max; ++$i) |
|
713 | + { |
|
714 | + if ($i >= 0 && $i <= 5) |
|
715 | + $this->xkey .= $this->k($i * 4); |
|
716 | + else if (($i % 6) == 0) |
|
717 | + { |
|
718 | + // rotate the 4 bytes |
|
719 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
720 | + |
|
721 | + // apply the sbox |
|
722 | + $this->subWord($subword); |
|
723 | + |
|
724 | + // return 4 byte value based on self::$_rcon table |
|
725 | + //$rcon = $this->rcon(($i / 6) - 1); |
|
726 | + $rcon = $this->rcon(($i / 6)); |
|
727 | + |
|
728 | + // grab 4 bytes from $this->extended_key |
|
729 | + $ek = $this->ek(($i - 6) * 4); |
|
730 | + |
|
731 | + $h1 = parent::str2Hex($subword); |
|
732 | + $h2 = parent::dec2Hex($rcon); |
|
733 | + $h3 = parent::str2Hex($ek); |
|
734 | + $res = parent::xorHex($h1, $h2, $h3); |
|
735 | + $this->xkey .= parent::hex2Str($res); |
|
736 | + } else |
|
737 | + { |
|
738 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
739 | + $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
740 | + $res = parent::xorHex($h1, $h2); |
|
741 | + $this->xkey .= parent::hex2Str($res); |
|
742 | + } |
|
743 | + } |
|
744 | + |
|
745 | + return true; |
|
746 | + } |
|
747 | + |
|
748 | + |
|
749 | + /** |
|
750 | + * Expand a 32 byte key, the size it is expanded to varies |
|
751 | + * based on the block size of the Rijndael implementation chosen |
|
752 | + * |
|
753 | + * @return boolean |
|
754 | + */ |
|
755 | + private function expandKey256() |
|
756 | + { |
|
757 | + // clear the xkey, we're creating a new one |
|
758 | + $this->xkey = ""; |
|
759 | + $max = 0; |
|
760 | + |
|
761 | + // the number of rounds we make depends on the block size of the text |
|
762 | + // used during encryption/decryption |
|
763 | + if ($this->blockSize() == 16) |
|
764 | + $max = 60; |
|
765 | + if ($this->blockSize() == 24) |
|
766 | + $max = 90; |
|
767 | + if ($this->blockSize() == 32) |
|
768 | + $max = 120; |
|
769 | + |
|
770 | + // 32 byte key expands to 240 bytes |
|
771 | + for ($i = 0; $i < $max; ++$i) |
|
772 | + { |
|
773 | + if ($i >= 0 && $i <= 7) |
|
774 | + $this->xkey .= $this->k($i * 4); |
|
775 | + else if ($i % 8 == 0) |
|
776 | + { |
|
777 | + // rotate the 4 bytes |
|
778 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
779 | + |
|
780 | + // apply the sbox |
|
781 | + $this->subWord($subword); |
|
782 | + |
|
783 | + // return 4 byte value based on self::$_rcon table |
|
784 | + $rcon = $this->rcon(($i / 8)); |
|
785 | + |
|
786 | + // grab 4 bytes from $this->extended_key |
|
787 | + $ek = $this->ek(($i - 8) * 4); |
|
788 | + |
|
789 | + $h1 = parent::str2Hex($subword); |
|
790 | + $h2 = parent::dec2Hex($rcon); |
|
791 | + $h3 = parent::str2Hex($ek); |
|
792 | + $res = parent::xorHex($h1, $h2, $h3); |
|
793 | + $this->xkey .= parent::hex2Str($res); |
|
794 | + } |
|
795 | + else if ($i % 4 == 0) |
|
796 | + { |
|
797 | + // get the subsitution from the s-box |
|
798 | + $subword = $this->ek(($i - 1) * 4); |
|
799 | + $this->subWord($subword); |
|
800 | + |
|
801 | + // get the extended key part |
|
802 | + $ek = $this->ek(($i - 8) * 4); |
|
803 | + |
|
804 | + // xor the two parts |
|
805 | + $h1 = parent::str2Hex($subword); |
|
806 | + $h2 = parent::str2Hex($ek); |
|
807 | + $res = parent::xorHex($h1, $h2); |
|
808 | + $this->xkey .= parent::hex2Str($res); |
|
809 | + } else |
|
810 | + { |
|
811 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
812 | + $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
813 | + $res = parent::xorHex($h1, $h2); |
|
814 | + $this->xkey .= parent::hex2Str($res); |
|
815 | + } |
|
816 | + } |
|
817 | + |
|
818 | + return true; |
|
819 | + } |
|
820 | + |
|
821 | + |
|
822 | + /** |
|
823 | + * Initalizes the tables used for Rijndael/AES encryption |
|
824 | + * |
|
825 | + * @return void |
|
826 | + */ |
|
827 | + private function initTables() |
|
828 | + { |
|
829 | + // the sbox used for encryption |
|
830 | + self::$_s = array( |
|
831 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
832 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
833 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
834 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
835 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
836 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
837 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
838 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
839 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
840 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
841 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
842 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
843 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
844 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
845 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
846 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
847 | + ); |
|
848 | + |
|
849 | + // the inverse sbox used for decryption |
|
850 | + self::$_s_inv = array( |
|
851 | + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
852 | + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
853 | + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
854 | + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
855 | + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
856 | + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
857 | + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
858 | + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
859 | + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
860 | + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
861 | + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
862 | + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
863 | + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
864 | + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
865 | + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
866 | + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
867 | + ); |
|
868 | + |
|
869 | + // used in mixColumn() during encryption |
|
870 | + self::$_matrix_mult = array( |
|
871 | + 0x02, 0x01, 0x01, 0x03, |
|
872 | + 0x03, 0x02, 0x01, 0x01, |
|
873 | + 0x01, 0x03, 0x02, 0x01, |
|
874 | + 0x01, 0x01, 0x03, 0x02 |
|
875 | + ); |
|
876 | + |
|
877 | + // used in mixColumn() during decryption |
|
878 | + self::$_matrix_mult_inv = array( |
|
879 | + 0x0e, 0x09, 0x0d, 0x0b, |
|
880 | + 0x0b, 0x0e, 0x09, 0x0d, |
|
881 | + 0x0d, 0x0b, 0x0e, 0x09, |
|
882 | + 0x09, 0x0d, 0x0b, 0x0e |
|
883 | + ); |
|
884 | + |
|
885 | + // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
886 | + // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
887 | + // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
888 | + // the other values are used for larger block/key combinations supported by Rijndael |
|
889 | + // NOTE: self::$_rcon[0] is never used |
|
890 | + self::$_rcon = array( |
|
891 | + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
892 | + 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
893 | + 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
894 | + 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
895 | + 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
896 | + 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
897 | + 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
898 | + 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
899 | + 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
900 | + 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
901 | + 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
902 | + 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
903 | + 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
904 | + 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
905 | + 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
906 | + 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
907 | + ); |
|
908 | + |
|
909 | + /* |
|
910 | 910 | * Galois Multiplication lookup tables |
911 | 911 | * See http://en.wikipedia.org/wiki/Rijndael_mix_columns#InverseMixColumns |
912 | 912 | */ |
913 | 913 | |
914 | - // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
915 | - self::$_gm2 = array( |
|
916 | - 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
917 | - 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
918 | - 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
919 | - 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
920 | - 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
921 | - 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
922 | - 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
923 | - 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
924 | - 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
925 | - 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
926 | - 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
927 | - 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
928 | - 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
929 | - 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
930 | - 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
931 | - 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
932 | - ); |
|
933 | - |
|
934 | - // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
935 | - self::$_gm3 = array( |
|
936 | - 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
937 | - 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
938 | - 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
939 | - 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
940 | - 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
941 | - 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
942 | - 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
943 | - 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
944 | - 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
945 | - 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
946 | - 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
947 | - 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
948 | - 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
949 | - 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
950 | - 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
951 | - 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
952 | - ); |
|
953 | - |
|
954 | - // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
955 | - self::$_gm9 = array( |
|
956 | - 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
957 | - 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
958 | - 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
959 | - 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
960 | - 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
961 | - 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
962 | - 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
963 | - 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
964 | - 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
965 | - 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
966 | - 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
967 | - 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
968 | - 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
969 | - 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
970 | - 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
971 | - 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
972 | - ); |
|
973 | - |
|
974 | - // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
975 | - self::$_gm11 = array( |
|
976 | - 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
977 | - 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
978 | - 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
979 | - 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
980 | - 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
981 | - 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
982 | - 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
983 | - 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
984 | - 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
985 | - 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
986 | - 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
987 | - 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
988 | - 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
989 | - 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
990 | - 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
991 | - 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
992 | - ); |
|
993 | - |
|
994 | - // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
995 | - self::$_gm13 = array( |
|
996 | - 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
997 | - 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
998 | - 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
999 | - 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1000 | - 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1001 | - 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1002 | - 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1003 | - 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1004 | - 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1005 | - 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1006 | - 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1007 | - 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1008 | - 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1009 | - 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1010 | - 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1011 | - 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1012 | - ); |
|
1013 | - |
|
1014 | - // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1015 | - self::$_gm14 = array( |
|
1016 | - 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1017 | - 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1018 | - 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1019 | - 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1020 | - 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1021 | - 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1022 | - 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1023 | - 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1024 | - 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1025 | - 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1026 | - 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1027 | - 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1028 | - 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1029 | - 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1030 | - 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1031 | - 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1032 | - ); |
|
1033 | - } |
|
914 | + // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
915 | + self::$_gm2 = array( |
|
916 | + 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
917 | + 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
918 | + 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
919 | + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
920 | + 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
921 | + 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
922 | + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
923 | + 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
924 | + 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
925 | + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
926 | + 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
927 | + 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
928 | + 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
929 | + 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
930 | + 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
931 | + 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
932 | + ); |
|
933 | + |
|
934 | + // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
935 | + self::$_gm3 = array( |
|
936 | + 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
937 | + 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
938 | + 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
939 | + 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
940 | + 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
941 | + 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
942 | + 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
943 | + 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
944 | + 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
945 | + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
946 | + 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
947 | + 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
948 | + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
949 | + 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
950 | + 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
951 | + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
952 | + ); |
|
953 | + |
|
954 | + // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
955 | + self::$_gm9 = array( |
|
956 | + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
957 | + 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
958 | + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
959 | + 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
960 | + 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
961 | + 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
962 | + 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
963 | + 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
964 | + 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
965 | + 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
966 | + 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
967 | + 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
968 | + 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
969 | + 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
970 | + 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
971 | + 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
972 | + ); |
|
973 | + |
|
974 | + // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
975 | + self::$_gm11 = array( |
|
976 | + 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
977 | + 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
978 | + 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
979 | + 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
980 | + 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
981 | + 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
982 | + 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
983 | + 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
984 | + 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
985 | + 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
986 | + 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
987 | + 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
988 | + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
989 | + 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
990 | + 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
991 | + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
992 | + ); |
|
993 | + |
|
994 | + // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
995 | + self::$_gm13 = array( |
|
996 | + 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
997 | + 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
998 | + 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
999 | + 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1000 | + 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1001 | + 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1002 | + 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1003 | + 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1004 | + 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1005 | + 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1006 | + 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1007 | + 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1008 | + 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1009 | + 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1010 | + 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1011 | + 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1012 | + ); |
|
1013 | + |
|
1014 | + // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1015 | + self::$_gm14 = array( |
|
1016 | + 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1017 | + 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1018 | + 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1019 | + 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1020 | + 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1021 | + 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1022 | + 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1023 | + 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1024 | + 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1025 | + 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1026 | + 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1027 | + 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1028 | + 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1029 | + 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1030 | + 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1031 | + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1032 | + ); |
|
1033 | + } |
|
1034 | 1034 | } |
1035 | 1035 | ?> |
@@ -38,1173 +38,1173 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_CAST_128 extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 8; // 64 bits; |
|
43 | - |
|
44 | - //const BYTES_KEY = 0; // between 40 - 128 bits |
|
45 | - |
|
46 | - const BYTES_KEY_SMALL = 10; |
|
47 | - const BYTES_KEY_MAX = 16; |
|
48 | - const BYTES_KEY_MIN = 5; |
|
49 | - |
|
50 | - /** @type array $_s1 An array of 256 unsigned integers */ |
|
51 | - private static $_s1 = array(); |
|
52 | - |
|
53 | - /** @type array $_s2 An array of 256 unsigned integers */ |
|
54 | - private static $_s2 = array(); |
|
55 | - |
|
56 | - /** @type array $_s3 An array of 256 unsigned integers */ |
|
57 | - private static $_s3 = array(); |
|
58 | - |
|
59 | - /** @type array $_s4 An array of 256 unsigned integers */ |
|
60 | - private static $_s4 = array(); |
|
61 | - |
|
62 | - /** @type array $_s5 An array of 256 unsigned integers */ |
|
63 | - private static $_s5 = array(); |
|
64 | - |
|
65 | - /** @type array $_s6 An array of 256 unsigned integers */ |
|
66 | - private static $_s6 = array(); |
|
67 | - |
|
68 | - /** @type array $_s7 An array of 256 unsigned integers */ |
|
69 | - private static $_s7 = array(); |
|
70 | - |
|
71 | - /** @type array $_s8 An array of 256 unsigned integers */ |
|
72 | - private static $_s8 = array(); |
|
73 | - |
|
74 | - /** @type string $_mkey The 16 byte masking subkey */ |
|
75 | - private $_mkey = ""; |
|
76 | - |
|
77 | - /** @type string $_rkey The 16 byte rotate subkey */ |
|
78 | - private $_rkey = ""; |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * Constructor |
|
83 | - * |
|
84 | - * @param string $key The key used for Encryption/Decryption |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function __construct($key) |
|
88 | - { |
|
89 | - // the length of the key is is between 5 - 16 bytes (40 - 128 bits) |
|
90 | - $keylen = strlen($key); |
|
91 | - if ($keylen > self::BYTES_KEY_MAX) |
|
92 | - { |
|
93 | - $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
94 | - $keylen = self::BYTES_KEY_MAX; |
|
95 | - } |
|
96 | - else if ($keylen < self::BYTES_KEY_MIN) |
|
97 | - { |
|
98 | - $msg = PHP_Crypt::CIPHER_CAST_128." requires a key size between "; |
|
99 | - $msg .= "5 - 16 bytes."; |
|
100 | - trigger_error($msg, E_USER_WARNING); |
|
101 | - } |
|
102 | - |
|
103 | - // set the key, make sure the required length is set in bytes |
|
104 | - parent::__construct(PHP_Crypt::CIPHER_CAST_128, $key, $keylen); |
|
105 | - |
|
106 | - // set the block size |
|
107 | - $this->blockSize(self::BYTES_BLOCK); |
|
108 | - |
|
109 | - // initialize the sboxes constants |
|
110 | - $this->initTables(); |
|
111 | - |
|
112 | - // create the sub keys using the sboxes |
|
113 | - $this->createSubKeys(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Destructor |
|
119 | - * |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function __destruct() |
|
123 | - { |
|
124 | - parent::__destruct(); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Encrypt plain text data |
|
130 | - * |
|
131 | - * @param string $data A block of plain data |
|
132 | - * @return boolean Returns true |
|
133 | - */ |
|
134 | - public function encrypt(&$data) |
|
135 | - { |
|
136 | - $this->operation(parent::ENCRYPT); |
|
137 | - |
|
138 | - // split the block in half, left and right |
|
139 | - $l = parent::str2Dec(substr($data, 0, 4)); |
|
140 | - $r = parent::str2Dec(substr($data, 4, 4)); |
|
141 | - |
|
142 | - // We do only 12 rounds if we have a key 10 bytes or less. |
|
143 | - // If we have a key greater than 10 bytes, we do the 12 rounds |
|
144 | - // then proceed to the additional 4 rounds for a total of 16 |
|
145 | - for ($i = 0; $i < 12; $i += 3) |
|
146 | - { |
|
147 | - // f1 |
|
148 | - $tmp = $r; |
|
149 | - $r = $l ^ $this->f1($r, $i); |
|
150 | - $l = $tmp; |
|
151 | - |
|
152 | - // f2 |
|
153 | - $tmp = $r; |
|
154 | - $r = $l ^ $this->f2($r, $i + 1); |
|
155 | - $l = $tmp; |
|
156 | - |
|
157 | - // f3 |
|
158 | - $tmp = $r; |
|
159 | - $r = $l ^ $this->f3($r, $i + 2); |
|
160 | - $l = $tmp; |
|
161 | - } |
|
162 | - |
|
163 | - // only do the full 16 rounds if the key is longer than |
|
164 | - // 10 bytes (80 bits) |
|
165 | - if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
166 | - { |
|
167 | - // f1 |
|
168 | - $tmp = $r; |
|
169 | - $r = $l ^ $this->f1($r, 12); |
|
170 | - $l = $tmp; |
|
171 | - |
|
172 | - // f2 |
|
173 | - $tmp = $r; |
|
174 | - $r = $l ^ $this->f2($r, 13); |
|
175 | - $l = $tmp; |
|
176 | - |
|
177 | - // f3 |
|
178 | - $tmp = $r; |
|
179 | - $r = $l ^ $this->f3($r, 14); |
|
180 | - $l = $tmp; |
|
181 | - |
|
182 | - // f1 |
|
183 | - $tmp = $r; |
|
184 | - $r = $l ^ $this->f1($r, 15); |
|
185 | - $l = $tmp; |
|
186 | - } |
|
187 | - |
|
188 | - // swap the two halfs |
|
189 | - $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
190 | - |
|
191 | - return true; |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * Decrypt an encrypted string, it does all the steps of encryption, |
|
197 | - * but in reverse. |
|
198 | - * |
|
199 | - * @param string $data A block of encrypted data |
|
200 | - * @return boolean Returns true |
|
201 | - */ |
|
202 | - public function decrypt(&$data) |
|
203 | - { |
|
204 | - $this->operation(parent::DECRYPT); |
|
205 | - |
|
206 | - // split the block in half, left and right |
|
207 | - $l = parent::str2Dec(substr($data, 0, 4)); |
|
208 | - $r = parent::str2Dec(substr($data, 4, 4)); |
|
209 | - |
|
210 | - // only do the full 16 rounds if the key is longer than |
|
211 | - // 10 bytes (80 bits) |
|
212 | - if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
213 | - { |
|
214 | - // f1 |
|
215 | - $tmp = $r; |
|
216 | - $r = $l ^ $this->f1($r, 15); |
|
217 | - $l = $tmp; |
|
218 | - |
|
219 | - // f3 |
|
220 | - $tmp = $r; |
|
221 | - $r = $l ^ $this->f3($r, 14); |
|
222 | - $l = $tmp; |
|
223 | - |
|
224 | - // f2 |
|
225 | - $tmp = $r; |
|
226 | - $r = $l ^ $this->f2($r, 13); |
|
227 | - $l = $tmp; |
|
228 | - |
|
229 | - // f1 |
|
230 | - $tmp = $r; |
|
231 | - $r = $l ^ $this->f1($r, 12); |
|
232 | - $l = $tmp; |
|
233 | - } |
|
234 | - |
|
235 | - // We do only 12 rounds if we have a key 10 bytes or less. |
|
236 | - for ($i = 11; $i >= 2; $i -= 3) |
|
237 | - { |
|
238 | - // f3 |
|
239 | - $tmp = $r; |
|
240 | - $r = $l ^ $this->f3($r, $i); |
|
241 | - $l = $tmp; |
|
242 | - |
|
243 | - // f2 |
|
244 | - $tmp = $r; |
|
245 | - $r = $l ^ $this->f2($r, $i - 1); |
|
246 | - $l = $tmp; |
|
247 | - |
|
248 | - // f1 |
|
249 | - $tmp = $r; |
|
250 | - $r = $l ^ $this->f1($r, $i - 2); |
|
251 | - $l = $tmp; |
|
252 | - } |
|
253 | - |
|
254 | - // swap the two halfs |
|
255 | - $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
256 | - |
|
257 | - return true; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * Cast 128 F1 function |
|
263 | - * |
|
264 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
265 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
266 | - * @return integer The value after the F1 transformation |
|
267 | - */ |
|
268 | - private function f1($r, $i) |
|
269 | - { |
|
270 | - $n = $this->_mkey[$i] + $r; |
|
271 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
272 | - $n = parent::dec2Str($n, 4); |
|
273 | - |
|
274 | - $f = parent::uInt32( |
|
275 | - ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
276 | - self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
277 | - ); |
|
278 | - |
|
279 | - return $f; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * Cast 128 F2 function |
|
285 | - * |
|
286 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
287 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
288 | - * @return integer The value after the F2 transformation |
|
289 | - */ |
|
290 | - private function f2($r, $i) |
|
291 | - { |
|
292 | - $n = $this->_mkey[$i] ^ $r; |
|
293 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
294 | - $n = parent::dec2Str($n, 4); |
|
295 | - |
|
296 | - $f = parent::uInt32( |
|
297 | - ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
298 | - self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
299 | - ); |
|
300 | - |
|
301 | - return $f; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Cast 128 F3 function |
|
307 | - * |
|
308 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
309 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
310 | - * @return integer The value after the F3 transformation |
|
311 | - */ |
|
312 | - private function f3($r, $i) |
|
313 | - { |
|
314 | - $n = $this->_mkey[$i] - $r; |
|
315 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
316 | - $n = parent::dec2Str($n, 4); |
|
317 | - |
|
318 | - $f = parent::uInt32( |
|
319 | - ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
320 | - self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
321 | - ); |
|
322 | - |
|
323 | - return $f; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Creates the subkeys $_mkey (the masking key) and |
|
329 | - * $_rkey (the rotate key) which are 16 bytes each. These are |
|
330 | - * created from the original key. The original key is null |
|
331 | - * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
332 | - * split in half to create $_mkey and $_rkey |
|
333 | - * |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - private function createSubKeys() |
|
337 | - { |
|
338 | - $x = $this->key(); |
|
339 | - $z = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // init to 16 bytes |
|
340 | - $skey = array(); |
|
341 | - |
|
342 | - // the max length of the key is 16 bytes, however if it is |
|
343 | - // less, pad it with null to get ito to 16 bytes |
|
344 | - if ($this->keySize() < self::BYTES_KEY_MAX) |
|
345 | - $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
346 | - |
|
347 | - /* |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 8; // 64 bits; |
|
43 | + |
|
44 | + //const BYTES_KEY = 0; // between 40 - 128 bits |
|
45 | + |
|
46 | + const BYTES_KEY_SMALL = 10; |
|
47 | + const BYTES_KEY_MAX = 16; |
|
48 | + const BYTES_KEY_MIN = 5; |
|
49 | + |
|
50 | + /** @type array $_s1 An array of 256 unsigned integers */ |
|
51 | + private static $_s1 = array(); |
|
52 | + |
|
53 | + /** @type array $_s2 An array of 256 unsigned integers */ |
|
54 | + private static $_s2 = array(); |
|
55 | + |
|
56 | + /** @type array $_s3 An array of 256 unsigned integers */ |
|
57 | + private static $_s3 = array(); |
|
58 | + |
|
59 | + /** @type array $_s4 An array of 256 unsigned integers */ |
|
60 | + private static $_s4 = array(); |
|
61 | + |
|
62 | + /** @type array $_s5 An array of 256 unsigned integers */ |
|
63 | + private static $_s5 = array(); |
|
64 | + |
|
65 | + /** @type array $_s6 An array of 256 unsigned integers */ |
|
66 | + private static $_s6 = array(); |
|
67 | + |
|
68 | + /** @type array $_s7 An array of 256 unsigned integers */ |
|
69 | + private static $_s7 = array(); |
|
70 | + |
|
71 | + /** @type array $_s8 An array of 256 unsigned integers */ |
|
72 | + private static $_s8 = array(); |
|
73 | + |
|
74 | + /** @type string $_mkey The 16 byte masking subkey */ |
|
75 | + private $_mkey = ""; |
|
76 | + |
|
77 | + /** @type string $_rkey The 16 byte rotate subkey */ |
|
78 | + private $_rkey = ""; |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * Constructor |
|
83 | + * |
|
84 | + * @param string $key The key used for Encryption/Decryption |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function __construct($key) |
|
88 | + { |
|
89 | + // the length of the key is is between 5 - 16 bytes (40 - 128 bits) |
|
90 | + $keylen = strlen($key); |
|
91 | + if ($keylen > self::BYTES_KEY_MAX) |
|
92 | + { |
|
93 | + $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
94 | + $keylen = self::BYTES_KEY_MAX; |
|
95 | + } |
|
96 | + else if ($keylen < self::BYTES_KEY_MIN) |
|
97 | + { |
|
98 | + $msg = PHP_Crypt::CIPHER_CAST_128." requires a key size between "; |
|
99 | + $msg .= "5 - 16 bytes."; |
|
100 | + trigger_error($msg, E_USER_WARNING); |
|
101 | + } |
|
102 | + |
|
103 | + // set the key, make sure the required length is set in bytes |
|
104 | + parent::__construct(PHP_Crypt::CIPHER_CAST_128, $key, $keylen); |
|
105 | + |
|
106 | + // set the block size |
|
107 | + $this->blockSize(self::BYTES_BLOCK); |
|
108 | + |
|
109 | + // initialize the sboxes constants |
|
110 | + $this->initTables(); |
|
111 | + |
|
112 | + // create the sub keys using the sboxes |
|
113 | + $this->createSubKeys(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Destructor |
|
119 | + * |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function __destruct() |
|
123 | + { |
|
124 | + parent::__destruct(); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Encrypt plain text data |
|
130 | + * |
|
131 | + * @param string $data A block of plain data |
|
132 | + * @return boolean Returns true |
|
133 | + */ |
|
134 | + public function encrypt(&$data) |
|
135 | + { |
|
136 | + $this->operation(parent::ENCRYPT); |
|
137 | + |
|
138 | + // split the block in half, left and right |
|
139 | + $l = parent::str2Dec(substr($data, 0, 4)); |
|
140 | + $r = parent::str2Dec(substr($data, 4, 4)); |
|
141 | + |
|
142 | + // We do only 12 rounds if we have a key 10 bytes or less. |
|
143 | + // If we have a key greater than 10 bytes, we do the 12 rounds |
|
144 | + // then proceed to the additional 4 rounds for a total of 16 |
|
145 | + for ($i = 0; $i < 12; $i += 3) |
|
146 | + { |
|
147 | + // f1 |
|
148 | + $tmp = $r; |
|
149 | + $r = $l ^ $this->f1($r, $i); |
|
150 | + $l = $tmp; |
|
151 | + |
|
152 | + // f2 |
|
153 | + $tmp = $r; |
|
154 | + $r = $l ^ $this->f2($r, $i + 1); |
|
155 | + $l = $tmp; |
|
156 | + |
|
157 | + // f3 |
|
158 | + $tmp = $r; |
|
159 | + $r = $l ^ $this->f3($r, $i + 2); |
|
160 | + $l = $tmp; |
|
161 | + } |
|
162 | + |
|
163 | + // only do the full 16 rounds if the key is longer than |
|
164 | + // 10 bytes (80 bits) |
|
165 | + if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
166 | + { |
|
167 | + // f1 |
|
168 | + $tmp = $r; |
|
169 | + $r = $l ^ $this->f1($r, 12); |
|
170 | + $l = $tmp; |
|
171 | + |
|
172 | + // f2 |
|
173 | + $tmp = $r; |
|
174 | + $r = $l ^ $this->f2($r, 13); |
|
175 | + $l = $tmp; |
|
176 | + |
|
177 | + // f3 |
|
178 | + $tmp = $r; |
|
179 | + $r = $l ^ $this->f3($r, 14); |
|
180 | + $l = $tmp; |
|
181 | + |
|
182 | + // f1 |
|
183 | + $tmp = $r; |
|
184 | + $r = $l ^ $this->f1($r, 15); |
|
185 | + $l = $tmp; |
|
186 | + } |
|
187 | + |
|
188 | + // swap the two halfs |
|
189 | + $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
190 | + |
|
191 | + return true; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * Decrypt an encrypted string, it does all the steps of encryption, |
|
197 | + * but in reverse. |
|
198 | + * |
|
199 | + * @param string $data A block of encrypted data |
|
200 | + * @return boolean Returns true |
|
201 | + */ |
|
202 | + public function decrypt(&$data) |
|
203 | + { |
|
204 | + $this->operation(parent::DECRYPT); |
|
205 | + |
|
206 | + // split the block in half, left and right |
|
207 | + $l = parent::str2Dec(substr($data, 0, 4)); |
|
208 | + $r = parent::str2Dec(substr($data, 4, 4)); |
|
209 | + |
|
210 | + // only do the full 16 rounds if the key is longer than |
|
211 | + // 10 bytes (80 bits) |
|
212 | + if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
213 | + { |
|
214 | + // f1 |
|
215 | + $tmp = $r; |
|
216 | + $r = $l ^ $this->f1($r, 15); |
|
217 | + $l = $tmp; |
|
218 | + |
|
219 | + // f3 |
|
220 | + $tmp = $r; |
|
221 | + $r = $l ^ $this->f3($r, 14); |
|
222 | + $l = $tmp; |
|
223 | + |
|
224 | + // f2 |
|
225 | + $tmp = $r; |
|
226 | + $r = $l ^ $this->f2($r, 13); |
|
227 | + $l = $tmp; |
|
228 | + |
|
229 | + // f1 |
|
230 | + $tmp = $r; |
|
231 | + $r = $l ^ $this->f1($r, 12); |
|
232 | + $l = $tmp; |
|
233 | + } |
|
234 | + |
|
235 | + // We do only 12 rounds if we have a key 10 bytes or less. |
|
236 | + for ($i = 11; $i >= 2; $i -= 3) |
|
237 | + { |
|
238 | + // f3 |
|
239 | + $tmp = $r; |
|
240 | + $r = $l ^ $this->f3($r, $i); |
|
241 | + $l = $tmp; |
|
242 | + |
|
243 | + // f2 |
|
244 | + $tmp = $r; |
|
245 | + $r = $l ^ $this->f2($r, $i - 1); |
|
246 | + $l = $tmp; |
|
247 | + |
|
248 | + // f1 |
|
249 | + $tmp = $r; |
|
250 | + $r = $l ^ $this->f1($r, $i - 2); |
|
251 | + $l = $tmp; |
|
252 | + } |
|
253 | + |
|
254 | + // swap the two halfs |
|
255 | + $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
256 | + |
|
257 | + return true; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * Cast 128 F1 function |
|
263 | + * |
|
264 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
265 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
266 | + * @return integer The value after the F1 transformation |
|
267 | + */ |
|
268 | + private function f1($r, $i) |
|
269 | + { |
|
270 | + $n = $this->_mkey[$i] + $r; |
|
271 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
272 | + $n = parent::dec2Str($n, 4); |
|
273 | + |
|
274 | + $f = parent::uInt32( |
|
275 | + ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
276 | + self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
277 | + ); |
|
278 | + |
|
279 | + return $f; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * Cast 128 F2 function |
|
285 | + * |
|
286 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
287 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
288 | + * @return integer The value after the F2 transformation |
|
289 | + */ |
|
290 | + private function f2($r, $i) |
|
291 | + { |
|
292 | + $n = $this->_mkey[$i] ^ $r; |
|
293 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
294 | + $n = parent::dec2Str($n, 4); |
|
295 | + |
|
296 | + $f = parent::uInt32( |
|
297 | + ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
298 | + self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
299 | + ); |
|
300 | + |
|
301 | + return $f; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Cast 128 F3 function |
|
307 | + * |
|
308 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
309 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
310 | + * @return integer The value after the F3 transformation |
|
311 | + */ |
|
312 | + private function f3($r, $i) |
|
313 | + { |
|
314 | + $n = $this->_mkey[$i] - $r; |
|
315 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
316 | + $n = parent::dec2Str($n, 4); |
|
317 | + |
|
318 | + $f = parent::uInt32( |
|
319 | + ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
320 | + self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
321 | + ); |
|
322 | + |
|
323 | + return $f; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Creates the subkeys $_mkey (the masking key) and |
|
329 | + * $_rkey (the rotate key) which are 16 bytes each. These are |
|
330 | + * created from the original key. The original key is null |
|
331 | + * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
332 | + * split in half to create $_mkey and $_rkey |
|
333 | + * |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + private function createSubKeys() |
|
337 | + { |
|
338 | + $x = $this->key(); |
|
339 | + $z = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // init to 16 bytes |
|
340 | + $skey = array(); |
|
341 | + |
|
342 | + // the max length of the key is 16 bytes, however if it is |
|
343 | + // less, pad it with null to get ito to 16 bytes |
|
344 | + if ($this->keySize() < self::BYTES_KEY_MAX) |
|
345 | + $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
346 | + |
|
347 | + /* |
|
348 | 348 | * NOW FOR THE UGLY PART, THIS IS TAKEN FROM PAGE 3-4 OF |
349 | 349 | * http://tools.ietf.org/html/rfc2144 |
350 | 350 | */ |
351 | 351 | |
352 | - // two loops, each loop does 16 bytes for a total of 32 bytes |
|
353 | - for ($i = 0; $i < 2; ++$i) |
|
354 | - { |
|
355 | - // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
356 | - $tmp = substr($x, 0x00, 4); |
|
357 | - $tmp = parent::dec2Str( |
|
358 | - parent::uInt32( |
|
359 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
360 | - self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
361 | - self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
362 | - ), 4); |
|
363 | - $z = substr_replace($z, $tmp, 0x00, 4); |
|
364 | - |
|
365 | - //print "Z0: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
366 | - |
|
367 | - // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
368 | - $tmp = substr($x, 0x08, 4); |
|
369 | - $tmp = parent::dec2Str( |
|
370 | - parent::uInt32( |
|
371 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
372 | - self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
373 | - self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
374 | - ), 4); |
|
375 | - $z = substr_replace($z, $tmp, 0x04, 4); |
|
376 | - |
|
377 | - // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
378 | - $tmp = substr($x, 0x0c, 4); |
|
379 | - $tmp = parent::dec2Str( |
|
380 | - parent::uInt32( |
|
381 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
382 | - self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
383 | - self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
384 | - ), 4); |
|
385 | - $z = substr_replace($z, $tmp, 0x08, 4); |
|
386 | - |
|
387 | - // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
388 | - $tmp = substr($x, 0x04, 4); |
|
389 | - $tmp = parent::dec2Str( |
|
390 | - parent::uInt32( |
|
391 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
392 | - self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
393 | - self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
394 | - ), 4); |
|
395 | - $z = substr_replace($z, $tmp, 0x0c, 4); |
|
396 | - |
|
397 | - //print "Z: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
398 | - |
|
399 | - // K1 = S5[z8] ^ S6[z9] ^ S7[z7] ^ S8[z6] ^ S5[z2] |
|
400 | - $skey[] = parent::uInt32(self::$_s5[ord($z[0x08])] ^ self::$_s6[ord($z[0x09])] ^ |
|
401 | - self::$_s7[ord($z[0x07])] ^ self::$_s8[ord($z[0x06])] ^ |
|
402 | - self::$_s5[ord($z[0x02])] |
|
403 | - ); |
|
404 | - |
|
405 | - // K2 = S5[zA] ^ S6[zB] ^ S7[z5] ^ S8[z4] ^ S6[z6] |
|
406 | - $skey[] = parent::uInt32( |
|
407 | - self::$_s5[ord($z[0x0a])] ^ self::$_s6[ord($z[0x0b])] ^ |
|
408 | - self::$_s7[ord($z[0x05])] ^ self::$_s8[ord($z[0x04])] ^ |
|
409 | - self::$_s6[ord($z[0x06])] |
|
410 | - ); |
|
411 | - |
|
412 | - // K3 = S5[zC] ^ S6[zD] ^ S7[z3] ^ S8[z2] ^ S7[z9] |
|
413 | - $skey[] = parent::uInt32( |
|
414 | - self::$_s5[ord($z[0x0c])] ^ self::$_s6[ord($z[0x0d])] ^ |
|
415 | - self::$_s7[ord($z[0x03])] ^ self::$_s8[ord($z[0x02])] ^ |
|
416 | - self::$_s7[ord($z[0x09])] |
|
417 | - ); |
|
418 | - |
|
419 | - // K4 = S5[zE] ^ S6[zF] ^ S7[z1] ^ S8[z0] ^ S8[zC] |
|
420 | - $skey[] = parent::uInt32( |
|
421 | - self::$_s5[ord($z[0x0e])] ^ self::$_s6[ord($z[0x0f])] ^ |
|
422 | - self::$_s7[ord($z[0x01])] ^ self::$_s8[ord($z[0x00])] ^ |
|
423 | - self::$_s8[ord($z[0x0c])] |
|
424 | - ); |
|
425 | - |
|
426 | - // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
427 | - $tmp = substr($z, 0x08, 4); |
|
428 | - $tmp = parent::dec2Str( |
|
429 | - parent::uInt32( |
|
430 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
431 | - self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
432 | - self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
433 | - ), 4); |
|
434 | - $x = substr_replace($x, $tmp, 0x00, 4); |
|
435 | - |
|
436 | - // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
437 | - $tmp = substr($z, 0x00, 4); |
|
438 | - $tmp = parent::dec2Str( |
|
439 | - parent::uInt32( |
|
440 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
441 | - self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
442 | - self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
443 | - ), 4); |
|
444 | - $x = substr_replace($x, $tmp, 0x04, 4); |
|
445 | - |
|
446 | - // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
447 | - $tmp = substr($z, 0x04, 4); |
|
448 | - $tmp = parent::dec2Str( |
|
449 | - parent::uInt32( |
|
450 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
451 | - self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
452 | - self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
453 | - ), 4); |
|
454 | - $x = substr_replace($x, $tmp, 0x08, 4); |
|
455 | - |
|
456 | - // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
457 | - $tmp = substr($z, 0x0c, 4); |
|
458 | - $tmp = parent::dec2Str( |
|
459 | - parent::uInt32( |
|
460 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
461 | - self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
462 | - self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
463 | - ), 4); |
|
464 | - $x = substr_replace($x, $tmp, 0x0c, 4); |
|
465 | - |
|
466 | - // K5 = S5[x3] ^ S6[x2] ^ S7[xC] ^ S8[xD] ^ S5[x8] |
|
467 | - $skey[] = parent::uInt32( |
|
468 | - self::$_s5[ord($x[0x03])] ^ self::$_s6[ord($x[0x02])] ^ |
|
469 | - self::$_s7[ord($x[0x0c])] ^ self::$_s8[ord($x[0x0d])] ^ |
|
470 | - self::$_s5[ord($x[0x08])] |
|
471 | - ); |
|
472 | - |
|
473 | - // K6 = S5[x1] ^ S6[x0] ^ S7[xE] ^ S8[xF] ^ S6[xD] |
|
474 | - $skey[] = parent::uInt32( |
|
475 | - self::$_s5[ord($x[0x01])] ^ self::$_s6[ord($x[0x00])] ^ |
|
476 | - self::$_s7[ord($x[0x0e])] ^ self::$_s8[ord($x[0x0f])] ^ |
|
477 | - self::$_s6[ord($x[0x0d])] |
|
478 | - ); |
|
479 | - |
|
480 | - // K7 = S5[x7] ^ S6[x6] ^ S7[x8] ^ S8[x9] ^ S7[x3] |
|
481 | - $skey[] = parent::uInt32( |
|
482 | - self::$_s5[ord($x[0x07])] ^ self::$_s6[ord($x[0x06])] ^ |
|
483 | - self::$_s7[ord($x[0x08])] ^ self::$_s8[ord($x[0x09])] ^ |
|
484 | - self::$_s7[ord($x[0x03])] |
|
485 | - ); |
|
486 | - |
|
487 | - // K8 = S5[x5] ^ S6[x4] ^ S7[xA] ^ S8[xB] ^ S8[x7] |
|
488 | - $skey[] = parent::uInt32( |
|
489 | - self::$_s5[ord($x[0x05])] ^ self::$_s6[ord($x[0x04])] ^ |
|
490 | - self::$_s7[ord($x[0x0a])] ^ self::$_s8[ord($x[0x0b])] ^ |
|
491 | - self::$_s8[ord($x[0x07])] |
|
492 | - ); |
|
493 | - |
|
494 | - // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
495 | - $tmp = substr($x, 0x00, 4); |
|
496 | - $tmp = parent::dec2Str( |
|
497 | - parent::uInt32( |
|
498 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
499 | - self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
500 | - self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
501 | - ), 4); |
|
502 | - $z = substr_replace($z, $tmp, 0x00, 4); |
|
503 | - |
|
504 | - // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
505 | - $tmp = substr($x, 0x08, 4); |
|
506 | - $tmp = parent::dec2Str( |
|
507 | - parent::uInt32( |
|
508 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
509 | - self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
510 | - self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
511 | - ), 4); |
|
512 | - $z = substr_replace($z, $tmp, 0x04, 4); |
|
513 | - |
|
514 | - // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
515 | - $tmp = substr($x, 0x0c, 4); |
|
516 | - $tmp = parent::dec2Str( |
|
517 | - parent::uInt32( |
|
518 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
519 | - self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
520 | - self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
521 | - ), 4); |
|
522 | - $z = substr_replace($z, $tmp, 0x08, 4); |
|
523 | - |
|
524 | - // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
525 | - $tmp = substr($x, 0x04, 4); |
|
526 | - $tmp = parent::dec2Str( |
|
527 | - parent::uInt32( |
|
528 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
529 | - self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
530 | - self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
531 | - ), 4); |
|
532 | - $z = substr_replace($z, $tmp, 0x0c, 4); |
|
533 | - |
|
534 | - // K9 = S5[z3] ^ S6[z2] ^ S7[zC] ^ S8[zD] ^ S5[z9] |
|
535 | - $skey[] = parent::uInt32( |
|
536 | - self::$_s5[ord($z[0x03])] ^ self::$_s6[ord($z[0x02])] ^ |
|
537 | - self::$_s7[ord($z[0x0c])] ^ self::$_s8[ord($z[0x0d])] ^ |
|
538 | - self::$_s5[ord($z[0x09])] |
|
539 | - ); |
|
540 | - |
|
541 | - // K10 = S5[z1] ^ S6[z0] ^ S7[zE] ^ S8[zF] ^ S6[zC] |
|
542 | - $skey[] = parent::uInt32( |
|
543 | - self::$_s5[ord($z[0x01])] ^ self::$_s6[ord($z[0x00])] ^ |
|
544 | - self::$_s7[ord($z[0x0e])] ^ self::$_s8[ord($z[0x0f])] ^ |
|
545 | - self::$_s6[ord($z[0x0c])] |
|
546 | - ); |
|
547 | - |
|
548 | - // K11 = S5[z7] ^ S6[z6] ^ S7[z8] ^ S8[z9] ^ S7[z2] |
|
549 | - $skey[] = parent::uInt32( |
|
550 | - self::$_s5[ord($z[0x07])] ^ self::$_s6[ord($z[0x06])] ^ |
|
551 | - self::$_s7[ord($z[0x08])] ^ self::$_s8[ord($z[0x09])] ^ |
|
552 | - self::$_s7[ord($z[0x02])] |
|
553 | - ); |
|
554 | - |
|
555 | - // K12 = S5[z5] ^ S6[z4] ^ S7[zA] ^ S8[zB] ^ S8[z6] |
|
556 | - $skey[] = parent::uInt32( |
|
557 | - self::$_s5[ord($z[0x05])] ^ self::$_s6[ord($z[0x04])] ^ |
|
558 | - self::$_s7[ord($z[0x0a])] ^ self::$_s8[ord($z[0x0b])] ^ |
|
559 | - self::$_s8[ord($z[0x06])] |
|
560 | - ); |
|
561 | - |
|
562 | - // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
563 | - $tmp = substr($z, 0x08, 4); |
|
564 | - $tmp = parent::dec2Str( |
|
565 | - parent::uInt32( |
|
566 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
567 | - self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
568 | - self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
569 | - ), 4); |
|
570 | - $x = substr_replace($x, $tmp, 0x00, 4); |
|
571 | - |
|
572 | - // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
573 | - $tmp = substr($z, 0x00, 4); |
|
574 | - $tmp = parent::dec2Str( |
|
575 | - parent::uInt32( |
|
576 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
577 | - self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
578 | - self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
579 | - ), 4); |
|
580 | - $x = substr_replace($x, $tmp, 0x04, 4); |
|
581 | - |
|
582 | - // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
583 | - $tmp = substr($z, 0x04, 4); |
|
584 | - $tmp = parent::dec2Str( |
|
585 | - parent::uInt32( |
|
586 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
587 | - self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
588 | - self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
589 | - ), 4); |
|
590 | - $x = substr_replace($x, $tmp, 0x08, 4); |
|
591 | - |
|
592 | - // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
593 | - $tmp = substr($z, 0x0c, 4); |
|
594 | - $tmp = parent::dec2Str( |
|
595 | - parent::uInt32( |
|
596 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
597 | - self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
598 | - self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
599 | - ), 4); |
|
600 | - $x = substr_replace($x, $tmp, 0x0c, 4); |
|
601 | - |
|
602 | - // K13 = S5[x8] ^ S6[x9] ^ S7[x7] ^ S8[x6] ^ S5[x3] |
|
603 | - $skey[] = parent::uInt32( |
|
604 | - self::$_s5[ord($x[0x08])] ^ self::$_s6[ord($x[0x09])] ^ |
|
605 | - self::$_s7[ord($x[0x07])] ^ self::$_s8[ord($x[0x06])] ^ |
|
606 | - self::$_s5[ord($x[0x03])] |
|
607 | - ); |
|
608 | - |
|
609 | - // K14 = S5[xA] ^ S6[xB] ^ S7[x5] ^ S8[x4] ^ S6[x7] |
|
610 | - $skey[] = parent::uInt32( |
|
611 | - self::$_s5[ord($x[0x0a])] ^ self::$_s6[ord($x[0x0b])] ^ |
|
612 | - self::$_s7[ord($x[0x05])] ^ self::$_s8[ord($x[0x04])] ^ |
|
613 | - self::$_s6[ord($x[0x07])] |
|
614 | - ); |
|
615 | - |
|
616 | - // K15 = S5[xC] ^ S6[xD] ^ S7[x3] ^ S8[x2] ^ S7[x8] |
|
617 | - $skey[] = parent::uInt32( |
|
618 | - self::$_s5[ord($x[0x0c])] ^ self::$_s6[ord($x[0x0d])] ^ |
|
619 | - self::$_s7[ord($x[0x03])] ^ self::$_s8[ord($x[0x02])] ^ |
|
620 | - self::$_s7[ord($x[0x08])] |
|
621 | - ); |
|
622 | - |
|
623 | - // K16 = S5[xE] ^ S6[xF] ^ S7[x1] ^ S8[x0] ^ S8[xD] |
|
624 | - $skey[] = parent::uInt32( |
|
625 | - self::$_s5[ord($x[0x0e])] ^ self::$_s6[ord($x[0x0f])] ^ |
|
626 | - self::$_s7[ord($x[0x01])] ^ self::$_s8[ord($x[0x00])] ^ |
|
627 | - self::$_s8[ord($x[0x0d])] |
|
628 | - ); |
|
629 | - } |
|
630 | - |
|
631 | - // create the 16 byte masking and rotate subkeys |
|
632 | - $this->_mkey = array_slice($skey, 0, 16); |
|
633 | - $this->_rkey = array_slice($skey, 16, 16); |
|
634 | - |
|
635 | - // $_rkey only uses the least significant 5 bits |
|
636 | - $this->_rkey = array_map(function($v) { |
|
637 | - return $v &= 31; |
|
638 | - }, $this->_rkey); |
|
639 | - |
|
640 | - // there is 4kb in the s5 - s8 sboxes, which are not needed after we |
|
641 | - // create the subkeys, so free up the memory. unset() doesn't work here |
|
642 | - for ($i = 5; $i <= 8; ++$i) |
|
643 | - self::${"_s$i"} = null; |
|
644 | - } |
|
645 | - |
|
646 | - |
|
647 | - /** |
|
648 | - * Initialize the tables. |
|
649 | - * |
|
650 | - * @return void |
|
651 | - */ |
|
652 | - private function initTables() |
|
653 | - { |
|
654 | - // 256 unsigned 32 bit integers |
|
655 | - self::$_s1 = array( |
|
656 | - 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
657 | - 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
658 | - 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
659 | - 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
660 | - 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
661 | - 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
662 | - 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
663 | - 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
664 | - 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
665 | - 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
666 | - 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
667 | - 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
668 | - 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
669 | - 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
670 | - 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
671 | - 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
672 | - 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
673 | - 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
674 | - 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
675 | - 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
676 | - 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
677 | - 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
678 | - 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
679 | - 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
680 | - 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
681 | - 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
682 | - 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
683 | - 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
684 | - 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
685 | - 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
686 | - 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
687 | - 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
688 | - 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
689 | - 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
690 | - 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
691 | - 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
692 | - 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
693 | - 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
694 | - 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
695 | - 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
696 | - 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
697 | - 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
698 | - 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
699 | - 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
700 | - 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
701 | - 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
702 | - 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
703 | - 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
704 | - 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
705 | - 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
706 | - 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
707 | - 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
708 | - 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
709 | - 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
710 | - 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
711 | - 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
712 | - 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
713 | - 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
714 | - 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
715 | - 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
716 | - 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
717 | - 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
718 | - 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
719 | - 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
720 | - ); |
|
721 | - |
|
722 | - // 256 unsigned 32 bit integers |
|
723 | - self::$_s2 = array( |
|
724 | - 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
725 | - 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
726 | - 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
727 | - 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
728 | - 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
729 | - 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
730 | - 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
731 | - 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
732 | - 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
733 | - 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
734 | - 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
735 | - 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
736 | - 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
737 | - 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
738 | - 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
739 | - 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
740 | - 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
741 | - 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
742 | - 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
743 | - 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
744 | - 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
745 | - 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
746 | - 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
747 | - 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
748 | - 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
749 | - 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
750 | - 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
751 | - 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
752 | - 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
753 | - 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
754 | - 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
755 | - 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
756 | - 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
757 | - 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
758 | - 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
759 | - 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
760 | - 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
761 | - 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
762 | - 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
763 | - 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
764 | - 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
765 | - 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
766 | - 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
767 | - 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
768 | - 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
769 | - 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
770 | - 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
771 | - 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
772 | - 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
773 | - 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
774 | - 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
775 | - 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
776 | - 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
777 | - 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
778 | - 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
779 | - 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
780 | - 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
781 | - 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
782 | - 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
783 | - 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
784 | - 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
785 | - 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
786 | - 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
787 | - 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
788 | - ); |
|
789 | - |
|
790 | - // 256 unsigned 32 bit integers |
|
791 | - self::$_s3 = array( |
|
792 | - 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
793 | - 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
794 | - 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
795 | - 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
796 | - 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
797 | - 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
798 | - 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
799 | - 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
800 | - 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
801 | - 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
802 | - 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
803 | - 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
804 | - 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
805 | - 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
806 | - 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
807 | - 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
808 | - 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
809 | - 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
810 | - 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
811 | - 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
812 | - 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
813 | - 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
814 | - 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
815 | - 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
816 | - 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
817 | - 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
818 | - 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
819 | - 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
820 | - 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
821 | - 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
822 | - 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
823 | - 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
824 | - 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
825 | - 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
826 | - 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
827 | - 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
828 | - 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
829 | - 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
830 | - 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
831 | - 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
832 | - 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
833 | - 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
834 | - 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
835 | - 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
836 | - 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
837 | - 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
838 | - 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
839 | - 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
840 | - 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
841 | - 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
842 | - 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
843 | - 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
844 | - 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
845 | - 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
846 | - 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
847 | - 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
848 | - 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
849 | - 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
850 | - 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
851 | - 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
852 | - 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
853 | - 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
854 | - 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
855 | - 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
856 | - ); |
|
857 | - |
|
858 | - // 256 unsigned 32 bit integers |
|
859 | - self::$_s4 = array( |
|
860 | - 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
861 | - 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
862 | - 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
863 | - 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
864 | - 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
865 | - 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
866 | - 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
867 | - 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
868 | - 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
869 | - 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
870 | - 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
871 | - 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
872 | - 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
873 | - 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
874 | - 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
875 | - 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
876 | - 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
877 | - 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
878 | - 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
879 | - 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
880 | - 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
881 | - 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
882 | - 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
883 | - 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
884 | - 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
885 | - 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
886 | - 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
887 | - 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
888 | - 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
889 | - 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
890 | - 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
891 | - 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
892 | - 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
893 | - 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
894 | - 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
895 | - 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
896 | - 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
897 | - 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
898 | - 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
899 | - 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
900 | - 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
901 | - 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
902 | - 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
903 | - 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
904 | - 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
905 | - 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
906 | - 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
907 | - 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
908 | - 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
909 | - 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
910 | - 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
911 | - 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
912 | - 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
913 | - 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
914 | - 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
915 | - 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
916 | - 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
917 | - 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
918 | - 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
919 | - 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
920 | - 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
921 | - 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
922 | - 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
923 | - 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
924 | - ); |
|
925 | - |
|
926 | - // 256 unsigned 32 bit integers |
|
927 | - self::$_s5 = array( |
|
928 | - 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, |
|
929 | - 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, |
|
930 | - 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, |
|
931 | - 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, |
|
932 | - 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, |
|
933 | - 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, |
|
934 | - 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, |
|
935 | - 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, |
|
936 | - 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, |
|
937 | - 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, |
|
938 | - 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, |
|
939 | - 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, |
|
940 | - 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, |
|
941 | - 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, |
|
942 | - 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, |
|
943 | - 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, |
|
944 | - 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, |
|
945 | - 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, |
|
946 | - 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, |
|
947 | - 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, |
|
948 | - 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, |
|
949 | - 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, |
|
950 | - 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, |
|
951 | - 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, |
|
952 | - 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, |
|
953 | - 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, |
|
954 | - 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, |
|
955 | - 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, |
|
956 | - 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, |
|
957 | - 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, |
|
958 | - 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, |
|
959 | - 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, |
|
960 | - 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, |
|
961 | - 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, |
|
962 | - 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, |
|
963 | - 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, |
|
964 | - 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, |
|
965 | - 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, |
|
966 | - 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, |
|
967 | - 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, |
|
968 | - 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, |
|
969 | - 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, |
|
970 | - 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, |
|
971 | - 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, |
|
972 | - 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, |
|
973 | - 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, |
|
974 | - 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, |
|
975 | - 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, |
|
976 | - 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, |
|
977 | - 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, |
|
978 | - 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, |
|
979 | - 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, |
|
980 | - 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, |
|
981 | - 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, |
|
982 | - 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, |
|
983 | - 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, |
|
984 | - 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, |
|
985 | - 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, |
|
986 | - 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, |
|
987 | - 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, |
|
988 | - 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, |
|
989 | - 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, |
|
990 | - 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, |
|
991 | - 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 |
|
992 | - ); |
|
993 | - |
|
994 | - // 256 unsigned 32 bit integers |
|
995 | - self::$_s6 = array( |
|
996 | - 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, |
|
997 | - 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, |
|
998 | - 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, |
|
999 | - 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, |
|
1000 | - 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, |
|
1001 | - 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, |
|
1002 | - 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, |
|
1003 | - 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, |
|
1004 | - 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, |
|
1005 | - 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, |
|
1006 | - 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, |
|
1007 | - 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, |
|
1008 | - 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, |
|
1009 | - 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, |
|
1010 | - 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, |
|
1011 | - 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, |
|
1012 | - 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, |
|
1013 | - 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, |
|
1014 | - 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, |
|
1015 | - 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, |
|
1016 | - 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, |
|
1017 | - 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, |
|
1018 | - 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, |
|
1019 | - 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, |
|
1020 | - 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, |
|
1021 | - 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, |
|
1022 | - 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, |
|
1023 | - 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, |
|
1024 | - 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, |
|
1025 | - 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, |
|
1026 | - 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, |
|
1027 | - 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, |
|
1028 | - 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, |
|
1029 | - 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, |
|
1030 | - 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, |
|
1031 | - 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, |
|
1032 | - 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, |
|
1033 | - 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, |
|
1034 | - 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, |
|
1035 | - 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, |
|
1036 | - 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, |
|
1037 | - 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, |
|
1038 | - 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, |
|
1039 | - 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, |
|
1040 | - 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, |
|
1041 | - 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, |
|
1042 | - 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, |
|
1043 | - 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, |
|
1044 | - 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, |
|
1045 | - 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, |
|
1046 | - 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, |
|
1047 | - 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, |
|
1048 | - 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, |
|
1049 | - 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, |
|
1050 | - 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, |
|
1051 | - 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, |
|
1052 | - 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, |
|
1053 | - 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, |
|
1054 | - 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, |
|
1055 | - 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, |
|
1056 | - 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, |
|
1057 | - 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, |
|
1058 | - 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, |
|
1059 | - 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F |
|
1060 | - ); |
|
1061 | - |
|
1062 | - // 256 unsigned 32 bit integers |
|
1063 | - self::$_s7 = array( |
|
1064 | - 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, |
|
1065 | - 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, |
|
1066 | - 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, |
|
1067 | - 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, |
|
1068 | - 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, |
|
1069 | - 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, |
|
1070 | - 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, |
|
1071 | - 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, |
|
1072 | - 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, |
|
1073 | - 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, |
|
1074 | - 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, |
|
1075 | - 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, |
|
1076 | - 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, |
|
1077 | - 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, |
|
1078 | - 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, |
|
1079 | - 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, |
|
1080 | - 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, |
|
1081 | - 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, |
|
1082 | - 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, |
|
1083 | - 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, |
|
1084 | - 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, |
|
1085 | - 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, |
|
1086 | - 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, |
|
1087 | - 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, |
|
1088 | - 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, |
|
1089 | - 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, |
|
1090 | - 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, |
|
1091 | - 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, |
|
1092 | - 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, |
|
1093 | - 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, |
|
1094 | - 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, |
|
1095 | - 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, |
|
1096 | - 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, |
|
1097 | - 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, |
|
1098 | - 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, |
|
1099 | - 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, |
|
1100 | - 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, |
|
1101 | - 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, |
|
1102 | - 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, |
|
1103 | - 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, |
|
1104 | - 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, |
|
1105 | - 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, |
|
1106 | - 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, |
|
1107 | - 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, |
|
1108 | - 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, |
|
1109 | - 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, |
|
1110 | - 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, |
|
1111 | - 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, |
|
1112 | - 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, |
|
1113 | - 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, |
|
1114 | - 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, |
|
1115 | - 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, |
|
1116 | - 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, |
|
1117 | - 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, |
|
1118 | - 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, |
|
1119 | - 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, |
|
1120 | - 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, |
|
1121 | - 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, |
|
1122 | - 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, |
|
1123 | - 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, |
|
1124 | - 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, |
|
1125 | - 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, |
|
1126 | - 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, |
|
1127 | - 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 |
|
1128 | - ); |
|
1129 | - |
|
1130 | - // 256 unsigned 32 bit integers |
|
1131 | - self::$_s8 = array( |
|
1132 | - 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, |
|
1133 | - 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, |
|
1134 | - 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, |
|
1135 | - 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, |
|
1136 | - 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, |
|
1137 | - 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, |
|
1138 | - 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, |
|
1139 | - 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, |
|
1140 | - 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, |
|
1141 | - 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, |
|
1142 | - 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, |
|
1143 | - 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, |
|
1144 | - 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, |
|
1145 | - 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, |
|
1146 | - 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, |
|
1147 | - 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, |
|
1148 | - 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, |
|
1149 | - 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, |
|
1150 | - 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, |
|
1151 | - 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, |
|
1152 | - 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, |
|
1153 | - 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, |
|
1154 | - 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, |
|
1155 | - 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, |
|
1156 | - 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, |
|
1157 | - 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, |
|
1158 | - 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, |
|
1159 | - 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, |
|
1160 | - 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, |
|
1161 | - 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, |
|
1162 | - 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, |
|
1163 | - 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, |
|
1164 | - 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, |
|
1165 | - 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, |
|
1166 | - 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, |
|
1167 | - 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, |
|
1168 | - 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, |
|
1169 | - 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, |
|
1170 | - 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, |
|
1171 | - 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, |
|
1172 | - 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, |
|
1173 | - 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, |
|
1174 | - 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, |
|
1175 | - 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, |
|
1176 | - 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, |
|
1177 | - 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, |
|
1178 | - 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, |
|
1179 | - 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, |
|
1180 | - 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, |
|
1181 | - 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, |
|
1182 | - 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, |
|
1183 | - 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, |
|
1184 | - 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, |
|
1185 | - 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, |
|
1186 | - 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, |
|
1187 | - 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, |
|
1188 | - 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, |
|
1189 | - 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, |
|
1190 | - 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, |
|
1191 | - 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, |
|
1192 | - 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, |
|
1193 | - 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, |
|
1194 | - 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, |
|
1195 | - 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E |
|
1196 | - ); |
|
1197 | - } |
|
1198 | - |
|
1199 | - |
|
1200 | - /** |
|
1201 | - * Indicates this is a block cipher |
|
1202 | - * |
|
1203 | - * @return integer Returns Cipher::BLOCK |
|
1204 | - */ |
|
1205 | - public function type() |
|
1206 | - { |
|
1207 | - return parent::BLOCK; |
|
1208 | - } |
|
352 | + // two loops, each loop does 16 bytes for a total of 32 bytes |
|
353 | + for ($i = 0; $i < 2; ++$i) |
|
354 | + { |
|
355 | + // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
356 | + $tmp = substr($x, 0x00, 4); |
|
357 | + $tmp = parent::dec2Str( |
|
358 | + parent::uInt32( |
|
359 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
360 | + self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
361 | + self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
362 | + ), 4); |
|
363 | + $z = substr_replace($z, $tmp, 0x00, 4); |
|
364 | + |
|
365 | + //print "Z0: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
366 | + |
|
367 | + // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
368 | + $tmp = substr($x, 0x08, 4); |
|
369 | + $tmp = parent::dec2Str( |
|
370 | + parent::uInt32( |
|
371 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
372 | + self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
373 | + self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
374 | + ), 4); |
|
375 | + $z = substr_replace($z, $tmp, 0x04, 4); |
|
376 | + |
|
377 | + // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
378 | + $tmp = substr($x, 0x0c, 4); |
|
379 | + $tmp = parent::dec2Str( |
|
380 | + parent::uInt32( |
|
381 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
382 | + self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
383 | + self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
384 | + ), 4); |
|
385 | + $z = substr_replace($z, $tmp, 0x08, 4); |
|
386 | + |
|
387 | + // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
388 | + $tmp = substr($x, 0x04, 4); |
|
389 | + $tmp = parent::dec2Str( |
|
390 | + parent::uInt32( |
|
391 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
392 | + self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
393 | + self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
394 | + ), 4); |
|
395 | + $z = substr_replace($z, $tmp, 0x0c, 4); |
|
396 | + |
|
397 | + //print "Z: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
398 | + |
|
399 | + // K1 = S5[z8] ^ S6[z9] ^ S7[z7] ^ S8[z6] ^ S5[z2] |
|
400 | + $skey[] = parent::uInt32(self::$_s5[ord($z[0x08])] ^ self::$_s6[ord($z[0x09])] ^ |
|
401 | + self::$_s7[ord($z[0x07])] ^ self::$_s8[ord($z[0x06])] ^ |
|
402 | + self::$_s5[ord($z[0x02])] |
|
403 | + ); |
|
404 | + |
|
405 | + // K2 = S5[zA] ^ S6[zB] ^ S7[z5] ^ S8[z4] ^ S6[z6] |
|
406 | + $skey[] = parent::uInt32( |
|
407 | + self::$_s5[ord($z[0x0a])] ^ self::$_s6[ord($z[0x0b])] ^ |
|
408 | + self::$_s7[ord($z[0x05])] ^ self::$_s8[ord($z[0x04])] ^ |
|
409 | + self::$_s6[ord($z[0x06])] |
|
410 | + ); |
|
411 | + |
|
412 | + // K3 = S5[zC] ^ S6[zD] ^ S7[z3] ^ S8[z2] ^ S7[z9] |
|
413 | + $skey[] = parent::uInt32( |
|
414 | + self::$_s5[ord($z[0x0c])] ^ self::$_s6[ord($z[0x0d])] ^ |
|
415 | + self::$_s7[ord($z[0x03])] ^ self::$_s8[ord($z[0x02])] ^ |
|
416 | + self::$_s7[ord($z[0x09])] |
|
417 | + ); |
|
418 | + |
|
419 | + // K4 = S5[zE] ^ S6[zF] ^ S7[z1] ^ S8[z0] ^ S8[zC] |
|
420 | + $skey[] = parent::uInt32( |
|
421 | + self::$_s5[ord($z[0x0e])] ^ self::$_s6[ord($z[0x0f])] ^ |
|
422 | + self::$_s7[ord($z[0x01])] ^ self::$_s8[ord($z[0x00])] ^ |
|
423 | + self::$_s8[ord($z[0x0c])] |
|
424 | + ); |
|
425 | + |
|
426 | + // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
427 | + $tmp = substr($z, 0x08, 4); |
|
428 | + $tmp = parent::dec2Str( |
|
429 | + parent::uInt32( |
|
430 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
431 | + self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
432 | + self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
433 | + ), 4); |
|
434 | + $x = substr_replace($x, $tmp, 0x00, 4); |
|
435 | + |
|
436 | + // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
437 | + $tmp = substr($z, 0x00, 4); |
|
438 | + $tmp = parent::dec2Str( |
|
439 | + parent::uInt32( |
|
440 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
441 | + self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
442 | + self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
443 | + ), 4); |
|
444 | + $x = substr_replace($x, $tmp, 0x04, 4); |
|
445 | + |
|
446 | + // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
447 | + $tmp = substr($z, 0x04, 4); |
|
448 | + $tmp = parent::dec2Str( |
|
449 | + parent::uInt32( |
|
450 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
451 | + self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
452 | + self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
453 | + ), 4); |
|
454 | + $x = substr_replace($x, $tmp, 0x08, 4); |
|
455 | + |
|
456 | + // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
457 | + $tmp = substr($z, 0x0c, 4); |
|
458 | + $tmp = parent::dec2Str( |
|
459 | + parent::uInt32( |
|
460 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
461 | + self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
462 | + self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
463 | + ), 4); |
|
464 | + $x = substr_replace($x, $tmp, 0x0c, 4); |
|
465 | + |
|
466 | + // K5 = S5[x3] ^ S6[x2] ^ S7[xC] ^ S8[xD] ^ S5[x8] |
|
467 | + $skey[] = parent::uInt32( |
|
468 | + self::$_s5[ord($x[0x03])] ^ self::$_s6[ord($x[0x02])] ^ |
|
469 | + self::$_s7[ord($x[0x0c])] ^ self::$_s8[ord($x[0x0d])] ^ |
|
470 | + self::$_s5[ord($x[0x08])] |
|
471 | + ); |
|
472 | + |
|
473 | + // K6 = S5[x1] ^ S6[x0] ^ S7[xE] ^ S8[xF] ^ S6[xD] |
|
474 | + $skey[] = parent::uInt32( |
|
475 | + self::$_s5[ord($x[0x01])] ^ self::$_s6[ord($x[0x00])] ^ |
|
476 | + self::$_s7[ord($x[0x0e])] ^ self::$_s8[ord($x[0x0f])] ^ |
|
477 | + self::$_s6[ord($x[0x0d])] |
|
478 | + ); |
|
479 | + |
|
480 | + // K7 = S5[x7] ^ S6[x6] ^ S7[x8] ^ S8[x9] ^ S7[x3] |
|
481 | + $skey[] = parent::uInt32( |
|
482 | + self::$_s5[ord($x[0x07])] ^ self::$_s6[ord($x[0x06])] ^ |
|
483 | + self::$_s7[ord($x[0x08])] ^ self::$_s8[ord($x[0x09])] ^ |
|
484 | + self::$_s7[ord($x[0x03])] |
|
485 | + ); |
|
486 | + |
|
487 | + // K8 = S5[x5] ^ S6[x4] ^ S7[xA] ^ S8[xB] ^ S8[x7] |
|
488 | + $skey[] = parent::uInt32( |
|
489 | + self::$_s5[ord($x[0x05])] ^ self::$_s6[ord($x[0x04])] ^ |
|
490 | + self::$_s7[ord($x[0x0a])] ^ self::$_s8[ord($x[0x0b])] ^ |
|
491 | + self::$_s8[ord($x[0x07])] |
|
492 | + ); |
|
493 | + |
|
494 | + // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
495 | + $tmp = substr($x, 0x00, 4); |
|
496 | + $tmp = parent::dec2Str( |
|
497 | + parent::uInt32( |
|
498 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
499 | + self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
500 | + self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
501 | + ), 4); |
|
502 | + $z = substr_replace($z, $tmp, 0x00, 4); |
|
503 | + |
|
504 | + // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
505 | + $tmp = substr($x, 0x08, 4); |
|
506 | + $tmp = parent::dec2Str( |
|
507 | + parent::uInt32( |
|
508 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
509 | + self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
510 | + self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
511 | + ), 4); |
|
512 | + $z = substr_replace($z, $tmp, 0x04, 4); |
|
513 | + |
|
514 | + // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
515 | + $tmp = substr($x, 0x0c, 4); |
|
516 | + $tmp = parent::dec2Str( |
|
517 | + parent::uInt32( |
|
518 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
519 | + self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
520 | + self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
521 | + ), 4); |
|
522 | + $z = substr_replace($z, $tmp, 0x08, 4); |
|
523 | + |
|
524 | + // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
525 | + $tmp = substr($x, 0x04, 4); |
|
526 | + $tmp = parent::dec2Str( |
|
527 | + parent::uInt32( |
|
528 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
529 | + self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
530 | + self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
531 | + ), 4); |
|
532 | + $z = substr_replace($z, $tmp, 0x0c, 4); |
|
533 | + |
|
534 | + // K9 = S5[z3] ^ S6[z2] ^ S7[zC] ^ S8[zD] ^ S5[z9] |
|
535 | + $skey[] = parent::uInt32( |
|
536 | + self::$_s5[ord($z[0x03])] ^ self::$_s6[ord($z[0x02])] ^ |
|
537 | + self::$_s7[ord($z[0x0c])] ^ self::$_s8[ord($z[0x0d])] ^ |
|
538 | + self::$_s5[ord($z[0x09])] |
|
539 | + ); |
|
540 | + |
|
541 | + // K10 = S5[z1] ^ S6[z0] ^ S7[zE] ^ S8[zF] ^ S6[zC] |
|
542 | + $skey[] = parent::uInt32( |
|
543 | + self::$_s5[ord($z[0x01])] ^ self::$_s6[ord($z[0x00])] ^ |
|
544 | + self::$_s7[ord($z[0x0e])] ^ self::$_s8[ord($z[0x0f])] ^ |
|
545 | + self::$_s6[ord($z[0x0c])] |
|
546 | + ); |
|
547 | + |
|
548 | + // K11 = S5[z7] ^ S6[z6] ^ S7[z8] ^ S8[z9] ^ S7[z2] |
|
549 | + $skey[] = parent::uInt32( |
|
550 | + self::$_s5[ord($z[0x07])] ^ self::$_s6[ord($z[0x06])] ^ |
|
551 | + self::$_s7[ord($z[0x08])] ^ self::$_s8[ord($z[0x09])] ^ |
|
552 | + self::$_s7[ord($z[0x02])] |
|
553 | + ); |
|
554 | + |
|
555 | + // K12 = S5[z5] ^ S6[z4] ^ S7[zA] ^ S8[zB] ^ S8[z6] |
|
556 | + $skey[] = parent::uInt32( |
|
557 | + self::$_s5[ord($z[0x05])] ^ self::$_s6[ord($z[0x04])] ^ |
|
558 | + self::$_s7[ord($z[0x0a])] ^ self::$_s8[ord($z[0x0b])] ^ |
|
559 | + self::$_s8[ord($z[0x06])] |
|
560 | + ); |
|
561 | + |
|
562 | + // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
563 | + $tmp = substr($z, 0x08, 4); |
|
564 | + $tmp = parent::dec2Str( |
|
565 | + parent::uInt32( |
|
566 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
567 | + self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
568 | + self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
569 | + ), 4); |
|
570 | + $x = substr_replace($x, $tmp, 0x00, 4); |
|
571 | + |
|
572 | + // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
573 | + $tmp = substr($z, 0x00, 4); |
|
574 | + $tmp = parent::dec2Str( |
|
575 | + parent::uInt32( |
|
576 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
577 | + self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
578 | + self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
579 | + ), 4); |
|
580 | + $x = substr_replace($x, $tmp, 0x04, 4); |
|
581 | + |
|
582 | + // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
583 | + $tmp = substr($z, 0x04, 4); |
|
584 | + $tmp = parent::dec2Str( |
|
585 | + parent::uInt32( |
|
586 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
587 | + self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
588 | + self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
589 | + ), 4); |
|
590 | + $x = substr_replace($x, $tmp, 0x08, 4); |
|
591 | + |
|
592 | + // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
593 | + $tmp = substr($z, 0x0c, 4); |
|
594 | + $tmp = parent::dec2Str( |
|
595 | + parent::uInt32( |
|
596 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
597 | + self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
598 | + self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
599 | + ), 4); |
|
600 | + $x = substr_replace($x, $tmp, 0x0c, 4); |
|
601 | + |
|
602 | + // K13 = S5[x8] ^ S6[x9] ^ S7[x7] ^ S8[x6] ^ S5[x3] |
|
603 | + $skey[] = parent::uInt32( |
|
604 | + self::$_s5[ord($x[0x08])] ^ self::$_s6[ord($x[0x09])] ^ |
|
605 | + self::$_s7[ord($x[0x07])] ^ self::$_s8[ord($x[0x06])] ^ |
|
606 | + self::$_s5[ord($x[0x03])] |
|
607 | + ); |
|
608 | + |
|
609 | + // K14 = S5[xA] ^ S6[xB] ^ S7[x5] ^ S8[x4] ^ S6[x7] |
|
610 | + $skey[] = parent::uInt32( |
|
611 | + self::$_s5[ord($x[0x0a])] ^ self::$_s6[ord($x[0x0b])] ^ |
|
612 | + self::$_s7[ord($x[0x05])] ^ self::$_s8[ord($x[0x04])] ^ |
|
613 | + self::$_s6[ord($x[0x07])] |
|
614 | + ); |
|
615 | + |
|
616 | + // K15 = S5[xC] ^ S6[xD] ^ S7[x3] ^ S8[x2] ^ S7[x8] |
|
617 | + $skey[] = parent::uInt32( |
|
618 | + self::$_s5[ord($x[0x0c])] ^ self::$_s6[ord($x[0x0d])] ^ |
|
619 | + self::$_s7[ord($x[0x03])] ^ self::$_s8[ord($x[0x02])] ^ |
|
620 | + self::$_s7[ord($x[0x08])] |
|
621 | + ); |
|
622 | + |
|
623 | + // K16 = S5[xE] ^ S6[xF] ^ S7[x1] ^ S8[x0] ^ S8[xD] |
|
624 | + $skey[] = parent::uInt32( |
|
625 | + self::$_s5[ord($x[0x0e])] ^ self::$_s6[ord($x[0x0f])] ^ |
|
626 | + self::$_s7[ord($x[0x01])] ^ self::$_s8[ord($x[0x00])] ^ |
|
627 | + self::$_s8[ord($x[0x0d])] |
|
628 | + ); |
|
629 | + } |
|
630 | + |
|
631 | + // create the 16 byte masking and rotate subkeys |
|
632 | + $this->_mkey = array_slice($skey, 0, 16); |
|
633 | + $this->_rkey = array_slice($skey, 16, 16); |
|
634 | + |
|
635 | + // $_rkey only uses the least significant 5 bits |
|
636 | + $this->_rkey = array_map(function($v) { |
|
637 | + return $v &= 31; |
|
638 | + }, $this->_rkey); |
|
639 | + |
|
640 | + // there is 4kb in the s5 - s8 sboxes, which are not needed after we |
|
641 | + // create the subkeys, so free up the memory. unset() doesn't work here |
|
642 | + for ($i = 5; $i <= 8; ++$i) |
|
643 | + self::${"_s$i"} = null; |
|
644 | + } |
|
645 | + |
|
646 | + |
|
647 | + /** |
|
648 | + * Initialize the tables. |
|
649 | + * |
|
650 | + * @return void |
|
651 | + */ |
|
652 | + private function initTables() |
|
653 | + { |
|
654 | + // 256 unsigned 32 bit integers |
|
655 | + self::$_s1 = array( |
|
656 | + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
657 | + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
658 | + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
659 | + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
660 | + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
661 | + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
662 | + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
663 | + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
664 | + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
665 | + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
666 | + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
667 | + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
668 | + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
669 | + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
670 | + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
671 | + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
672 | + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
673 | + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
674 | + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
675 | + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
676 | + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
677 | + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
678 | + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
679 | + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
680 | + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
681 | + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
682 | + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
683 | + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
684 | + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
685 | + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
686 | + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
687 | + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
688 | + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
689 | + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
690 | + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
691 | + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
692 | + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
693 | + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
694 | + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
695 | + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
696 | + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
697 | + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
698 | + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
699 | + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
700 | + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
701 | + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
702 | + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
703 | + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
704 | + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
705 | + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
706 | + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
707 | + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
708 | + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
709 | + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
710 | + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
711 | + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
712 | + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
713 | + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
714 | + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
715 | + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
716 | + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
717 | + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
718 | + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
719 | + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
720 | + ); |
|
721 | + |
|
722 | + // 256 unsigned 32 bit integers |
|
723 | + self::$_s2 = array( |
|
724 | + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
725 | + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
726 | + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
727 | + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
728 | + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
729 | + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
730 | + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
731 | + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
732 | + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
733 | + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
734 | + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
735 | + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
736 | + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
737 | + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
738 | + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
739 | + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
740 | + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
741 | + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
742 | + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
743 | + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
744 | + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
745 | + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
746 | + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
747 | + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
748 | + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
749 | + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
750 | + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
751 | + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
752 | + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
753 | + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
754 | + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
755 | + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
756 | + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
757 | + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
758 | + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
759 | + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
760 | + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
761 | + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
762 | + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
763 | + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
764 | + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
765 | + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
766 | + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
767 | + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
768 | + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
769 | + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
770 | + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
771 | + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
772 | + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
773 | + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
774 | + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
775 | + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
776 | + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
777 | + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
778 | + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
779 | + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
780 | + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
781 | + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
782 | + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
783 | + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
784 | + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
785 | + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
786 | + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
787 | + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
788 | + ); |
|
789 | + |
|
790 | + // 256 unsigned 32 bit integers |
|
791 | + self::$_s3 = array( |
|
792 | + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
793 | + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
794 | + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
795 | + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
796 | + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
797 | + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
798 | + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
799 | + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
800 | + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
801 | + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
802 | + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
803 | + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
804 | + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
805 | + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
806 | + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
807 | + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
808 | + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
809 | + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
810 | + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
811 | + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
812 | + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
813 | + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
814 | + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
815 | + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
816 | + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
817 | + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
818 | + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
819 | + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
820 | + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
821 | + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
822 | + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
823 | + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
824 | + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
825 | + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
826 | + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
827 | + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
828 | + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
829 | + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
830 | + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
831 | + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
832 | + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
833 | + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
834 | + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
835 | + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
836 | + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
837 | + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
838 | + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
839 | + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
840 | + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
841 | + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
842 | + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
843 | + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
844 | + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
845 | + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
846 | + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
847 | + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
848 | + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
849 | + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
850 | + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
851 | + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
852 | + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
853 | + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
854 | + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
855 | + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
856 | + ); |
|
857 | + |
|
858 | + // 256 unsigned 32 bit integers |
|
859 | + self::$_s4 = array( |
|
860 | + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
861 | + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
862 | + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
863 | + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
864 | + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
865 | + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
866 | + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
867 | + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
868 | + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
869 | + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
870 | + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
871 | + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
872 | + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
873 | + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
874 | + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
875 | + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
876 | + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
877 | + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
878 | + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
879 | + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
880 | + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
881 | + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
882 | + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
883 | + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
884 | + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
885 | + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
886 | + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
887 | + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
888 | + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
889 | + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
890 | + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
891 | + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
892 | + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
893 | + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
894 | + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
895 | + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
896 | + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
897 | + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
898 | + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
899 | + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
900 | + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
901 | + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
902 | + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
903 | + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
904 | + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
905 | + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
906 | + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
907 | + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
908 | + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
909 | + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
910 | + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
911 | + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
912 | + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
913 | + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
914 | + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
915 | + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
916 | + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
917 | + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
918 | + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
919 | + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
920 | + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
921 | + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
922 | + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
923 | + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
924 | + ); |
|
925 | + |
|
926 | + // 256 unsigned 32 bit integers |
|
927 | + self::$_s5 = array( |
|
928 | + 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, |
|
929 | + 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, |
|
930 | + 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, |
|
931 | + 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, |
|
932 | + 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, |
|
933 | + 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, |
|
934 | + 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, |
|
935 | + 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, |
|
936 | + 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, |
|
937 | + 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, |
|
938 | + 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, |
|
939 | + 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, |
|
940 | + 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, |
|
941 | + 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, |
|
942 | + 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, |
|
943 | + 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, |
|
944 | + 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, |
|
945 | + 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, |
|
946 | + 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, |
|
947 | + 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, |
|
948 | + 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, |
|
949 | + 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, |
|
950 | + 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, |
|
951 | + 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, |
|
952 | + 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, |
|
953 | + 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, |
|
954 | + 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, |
|
955 | + 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, |
|
956 | + 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, |
|
957 | + 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, |
|
958 | + 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, |
|
959 | + 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, |
|
960 | + 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, |
|
961 | + 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, |
|
962 | + 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, |
|
963 | + 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, |
|
964 | + 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, |
|
965 | + 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, |
|
966 | + 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, |
|
967 | + 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, |
|
968 | + 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, |
|
969 | + 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, |
|
970 | + 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, |
|
971 | + 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, |
|
972 | + 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, |
|
973 | + 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, |
|
974 | + 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, |
|
975 | + 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, |
|
976 | + 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, |
|
977 | + 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, |
|
978 | + 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, |
|
979 | + 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, |
|
980 | + 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, |
|
981 | + 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, |
|
982 | + 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, |
|
983 | + 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, |
|
984 | + 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, |
|
985 | + 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, |
|
986 | + 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, |
|
987 | + 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, |
|
988 | + 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, |
|
989 | + 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, |
|
990 | + 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, |
|
991 | + 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 |
|
992 | + ); |
|
993 | + |
|
994 | + // 256 unsigned 32 bit integers |
|
995 | + self::$_s6 = array( |
|
996 | + 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, |
|
997 | + 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, |
|
998 | + 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, |
|
999 | + 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, |
|
1000 | + 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, |
|
1001 | + 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, |
|
1002 | + 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, |
|
1003 | + 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, |
|
1004 | + 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, |
|
1005 | + 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, |
|
1006 | + 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, |
|
1007 | + 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, |
|
1008 | + 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, |
|
1009 | + 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, |
|
1010 | + 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, |
|
1011 | + 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, |
|
1012 | + 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, |
|
1013 | + 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, |
|
1014 | + 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, |
|
1015 | + 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, |
|
1016 | + 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, |
|
1017 | + 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, |
|
1018 | + 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, |
|
1019 | + 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, |
|
1020 | + 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, |
|
1021 | + 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, |
|
1022 | + 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, |
|
1023 | + 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, |
|
1024 | + 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, |
|
1025 | + 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, |
|
1026 | + 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, |
|
1027 | + 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, |
|
1028 | + 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, |
|
1029 | + 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, |
|
1030 | + 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, |
|
1031 | + 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, |
|
1032 | + 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, |
|
1033 | + 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, |
|
1034 | + 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, |
|
1035 | + 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, |
|
1036 | + 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, |
|
1037 | + 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, |
|
1038 | + 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, |
|
1039 | + 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, |
|
1040 | + 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, |
|
1041 | + 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, |
|
1042 | + 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, |
|
1043 | + 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, |
|
1044 | + 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, |
|
1045 | + 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, |
|
1046 | + 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, |
|
1047 | + 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, |
|
1048 | + 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, |
|
1049 | + 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, |
|
1050 | + 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, |
|
1051 | + 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, |
|
1052 | + 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, |
|
1053 | + 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, |
|
1054 | + 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, |
|
1055 | + 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, |
|
1056 | + 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, |
|
1057 | + 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, |
|
1058 | + 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, |
|
1059 | + 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F |
|
1060 | + ); |
|
1061 | + |
|
1062 | + // 256 unsigned 32 bit integers |
|
1063 | + self::$_s7 = array( |
|
1064 | + 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, |
|
1065 | + 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, |
|
1066 | + 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, |
|
1067 | + 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, |
|
1068 | + 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, |
|
1069 | + 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, |
|
1070 | + 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, |
|
1071 | + 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, |
|
1072 | + 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, |
|
1073 | + 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, |
|
1074 | + 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, |
|
1075 | + 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, |
|
1076 | + 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, |
|
1077 | + 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, |
|
1078 | + 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, |
|
1079 | + 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, |
|
1080 | + 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, |
|
1081 | + 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, |
|
1082 | + 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, |
|
1083 | + 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, |
|
1084 | + 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, |
|
1085 | + 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, |
|
1086 | + 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, |
|
1087 | + 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, |
|
1088 | + 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, |
|
1089 | + 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, |
|
1090 | + 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, |
|
1091 | + 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, |
|
1092 | + 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, |
|
1093 | + 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, |
|
1094 | + 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, |
|
1095 | + 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, |
|
1096 | + 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, |
|
1097 | + 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, |
|
1098 | + 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, |
|
1099 | + 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, |
|
1100 | + 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, |
|
1101 | + 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, |
|
1102 | + 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, |
|
1103 | + 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, |
|
1104 | + 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, |
|
1105 | + 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, |
|
1106 | + 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, |
|
1107 | + 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, |
|
1108 | + 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, |
|
1109 | + 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, |
|
1110 | + 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, |
|
1111 | + 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, |
|
1112 | + 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, |
|
1113 | + 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, |
|
1114 | + 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, |
|
1115 | + 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, |
|
1116 | + 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, |
|
1117 | + 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, |
|
1118 | + 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, |
|
1119 | + 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, |
|
1120 | + 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, |
|
1121 | + 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, |
|
1122 | + 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, |
|
1123 | + 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, |
|
1124 | + 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, |
|
1125 | + 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, |
|
1126 | + 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, |
|
1127 | + 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 |
|
1128 | + ); |
|
1129 | + |
|
1130 | + // 256 unsigned 32 bit integers |
|
1131 | + self::$_s8 = array( |
|
1132 | + 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, |
|
1133 | + 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, |
|
1134 | + 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, |
|
1135 | + 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, |
|
1136 | + 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, |
|
1137 | + 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, |
|
1138 | + 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, |
|
1139 | + 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, |
|
1140 | + 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, |
|
1141 | + 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, |
|
1142 | + 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, |
|
1143 | + 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, |
|
1144 | + 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, |
|
1145 | + 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, |
|
1146 | + 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, |
|
1147 | + 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, |
|
1148 | + 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, |
|
1149 | + 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, |
|
1150 | + 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, |
|
1151 | + 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, |
|
1152 | + 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, |
|
1153 | + 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, |
|
1154 | + 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, |
|
1155 | + 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, |
|
1156 | + 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, |
|
1157 | + 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, |
|
1158 | + 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, |
|
1159 | + 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, |
|
1160 | + 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, |
|
1161 | + 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, |
|
1162 | + 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, |
|
1163 | + 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, |
|
1164 | + 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, |
|
1165 | + 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, |
|
1166 | + 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, |
|
1167 | + 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, |
|
1168 | + 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, |
|
1169 | + 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, |
|
1170 | + 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, |
|
1171 | + 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, |
|
1172 | + 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, |
|
1173 | + 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, |
|
1174 | + 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, |
|
1175 | + 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, |
|
1176 | + 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, |
|
1177 | + 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, |
|
1178 | + 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, |
|
1179 | + 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, |
|
1180 | + 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, |
|
1181 | + 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, |
|
1182 | + 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, |
|
1183 | + 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, |
|
1184 | + 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, |
|
1185 | + 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, |
|
1186 | + 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, |
|
1187 | + 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, |
|
1188 | + 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, |
|
1189 | + 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, |
|
1190 | + 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, |
|
1191 | + 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, |
|
1192 | + 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, |
|
1193 | + 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, |
|
1194 | + 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, |
|
1195 | + 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E |
|
1196 | + ); |
|
1197 | + } |
|
1198 | + |
|
1199 | + |
|
1200 | + /** |
|
1201 | + * Indicates this is a block cipher |
|
1202 | + * |
|
1203 | + * @return integer Returns Cipher::BLOCK |
|
1204 | + */ |
|
1205 | + public function type() |
|
1206 | + { |
|
1207 | + return parent::BLOCK; |
|
1208 | + } |
|
1209 | 1209 | } |
1210 | 1210 | ?> |