@@ -42,1000 +42,1000 @@ |
||
| 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 | - } |
|
| 457 | - else // parent::DECRYPT |
|
| 458 | - { |
|
| 459 | - // create a 4x4 matrix |
|
| 460 | - // row 0 is unchanged, |
|
| 461 | - // shift row 1 right 1 byte |
|
| 462 | - // shift row 2 right 2 bytes |
|
| 463 | - // shift row 3 right 3 bytes |
|
| 464 | - $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
| 465 | - $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
| 466 | - $text[12].$text[9].$text[6].$text[3]; |
|
| 467 | - } |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /* |
|
| 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 | + } |
|
| 457 | + else // parent::DECRYPT |
|
| 458 | + { |
|
| 459 | + // create a 4x4 matrix |
|
| 460 | + // row 0 is unchanged, |
|
| 461 | + // shift row 1 right 1 byte |
|
| 462 | + // shift row 2 right 2 bytes |
|
| 463 | + // shift row 3 right 3 bytes |
|
| 464 | + $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
| 465 | + $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
| 466 | + $text[12].$text[9].$text[6].$text[3]; |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /* |
|
| 471 | 471 | * Rijndael-192 |
| 472 | 472 | */ |
| 473 | - if($this->blockSize() == 24) |
|
| 474 | - { |
|
| 475 | - if($this->operation() == parent::ENCRYPT) |
|
| 476 | - { |
|
| 477 | - // create a 6x4 matrix |
|
| 478 | - // row 0 is unchanged |
|
| 479 | - // shift row 1 left 1 byte |
|
| 480 | - // shift row 2 left 2 bytes |
|
| 481 | - // shift row 3 left 3 bytes |
|
| 482 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
| 483 | - $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
| 484 | - $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
| 485 | - $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
| 486 | - |
|
| 487 | - } |
|
| 488 | - else // parent::DECRYPT |
|
| 489 | - { |
|
| 490 | - // create a 6x4 matrix |
|
| 491 | - // row 0 is unchanged |
|
| 492 | - // shift row 1 right 1 byte |
|
| 493 | - // shift row 2 right 2 bytes |
|
| 494 | - // shift row 3 right 3 bytes |
|
| 495 | - $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
| 496 | - $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
| 497 | - $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
| 498 | - $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /* |
|
| 473 | + if($this->blockSize() == 24) |
|
| 474 | + { |
|
| 475 | + if($this->operation() == parent::ENCRYPT) |
|
| 476 | + { |
|
| 477 | + // create a 6x4 matrix |
|
| 478 | + // row 0 is unchanged |
|
| 479 | + // shift row 1 left 1 byte |
|
| 480 | + // shift row 2 left 2 bytes |
|
| 481 | + // shift row 3 left 3 bytes |
|
| 482 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
| 483 | + $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
| 484 | + $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
| 485 | + $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
| 486 | + |
|
| 487 | + } |
|
| 488 | + else // parent::DECRYPT |
|
| 489 | + { |
|
| 490 | + // create a 6x4 matrix |
|
| 491 | + // row 0 is unchanged |
|
| 492 | + // shift row 1 right 1 byte |
|
| 493 | + // shift row 2 right 2 bytes |
|
| 494 | + // shift row 3 right 3 bytes |
|
| 495 | + $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
| 496 | + $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
| 497 | + $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
| 498 | + $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /* |
|
| 503 | 503 | * Rijndael-256 |
| 504 | 504 | */ |
| 505 | - if($this->blockSize() == 32) |
|
| 506 | - { |
|
| 507 | - if($this->operation() == parent::ENCRYPT) |
|
| 508 | - { |
|
| 509 | - // create an 8x4 matrix |
|
| 510 | - // row 0 is unchanged |
|
| 511 | - // shift row 1 left 1 byte |
|
| 512 | - // shift row 2 left 3 bytes |
|
| 513 | - // shift row 3 left 4 bytes |
|
| 514 | - $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
| 515 | - $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
| 516 | - $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
| 517 | - $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
| 518 | - $text[28].$text[1].$text[10].$text[15]; |
|
| 519 | - } |
|
| 520 | - else // parent::DECRYPT |
|
| 521 | - { |
|
| 522 | - // create an 8x4 matrix |
|
| 523 | - // row 0 is unchanged |
|
| 524 | - // shift row 1 right 1 byte |
|
| 525 | - // shift row 2 right 3 bytes |
|
| 526 | - // shift row 3 right 4 bytes |
|
| 527 | - $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
| 528 | - $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
| 529 | - $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
| 530 | - $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
| 531 | - $text[28].$text[25].$text[18].$text[15]; |
|
| 532 | - } |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Applies the Sbox to each byte of the string passed in. |
|
| 539 | - * This is similar to subByte(), but Unlike subByte() we do not use |
|
| 540 | - * the _s_inv[] table. This function is only used in expandKey(), |
|
| 541 | - * which is implemented by the class that inherits this class |
|
| 542 | - * |
|
| 543 | - * @param string $text The string to peform the byte subsitution |
|
| 544 | - * @return string The string with the subsituted bytes |
|
| 545 | - */ |
|
| 546 | - private function subWord(&$text) |
|
| 547 | - { |
|
| 548 | - $max = strlen($text); |
|
| 549 | - for($i = 0; $i < $max; ++$i) |
|
| 550 | - { |
|
| 551 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
| 552 | - // and column is numbered in hex (from 0 - f) |
|
| 553 | - $hex = parent::str2Hex($text[$i]); |
|
| 554 | - $row = parent::hex2Dec($hex[0]); |
|
| 555 | - $col = parent::hex2Dec($hex[1]); |
|
| 556 | - $pos = ($row * 16) + $col; |
|
| 557 | - |
|
| 558 | - $text[$i] = chr(self::$_s[$pos]); |
|
| 559 | - } |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Rotate a 4 byte block of the key, moving the first byte to |
|
| 565 | - * to the end, and shifting everything left |
|
| 566 | - * Used in key Expandsion |
|
| 567 | - * |
|
| 568 | - * @param string $key_block A 4 byte string |
|
| 569 | - * @return string The shifted 4 byte string |
|
| 570 | - */ |
|
| 571 | - private function rotWord($key_block) |
|
| 572 | - { |
|
| 573 | - return substr($key_block, 1, 3).$key_block[0]; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Returns 4 bytes from the expanded key starting at the given offset |
|
| 579 | - * Used during expandKey() |
|
| 580 | - * |
|
| 581 | - * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
| 582 | - * @return string A 4 byte string from the key |
|
| 583 | - */ |
|
| 584 | - private function ek($offset) |
|
| 585 | - { |
|
| 586 | - return substr($this->xkey, $offset, 4); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Returns 4 bytes of the original key from the given offset |
|
| 592 | - * Used during expandKey() |
|
| 593 | - * |
|
| 594 | - * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
| 595 | - * @return string A 4 byte string from the key |
|
| 596 | - */ |
|
| 597 | - private function k($offset) |
|
| 598 | - { |
|
| 599 | - return substr($this->key(), $offset, 4); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Return the 4 byte round constant used during expandKey(). |
|
| 605 | - * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
| 606 | - * 0x01000000 to create a 4 byte value |
|
| 607 | - * |
|
| 608 | - * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
| 609 | - * @return integer A 4 byte value |
|
| 610 | - */ |
|
| 611 | - private function rcon($pos) |
|
| 612 | - { |
|
| 613 | - return (self::$_rcon[$pos] * 0x01000000); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Expands the key |
|
| 619 | - * The key expands based on the block size as well as the key size |
|
| 620 | - * |
|
| 621 | - * @return void |
|
| 622 | - */ |
|
| 623 | - protected function expandKey() |
|
| 624 | - { |
|
| 625 | - if($this->keySize() == 16) |
|
| 626 | - return $this->expandKey128(); |
|
| 627 | - else if($this->keySize() == 24) |
|
| 628 | - return $this->expandKey192(); |
|
| 629 | - else if($this->keySize() == 32) |
|
| 630 | - return $this->expandKey256(); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Expand a 16 byte key, the size it is expanded to varies |
|
| 636 | - * based on the block size of the Rijndael implementation chosen |
|
| 637 | - * |
|
| 638 | - * @return void |
|
| 639 | - */ |
|
| 640 | - private function expandKey128() |
|
| 641 | - { |
|
| 642 | - // clear the xkey, we're creating a new one |
|
| 643 | - $this->xkey = ""; |
|
| 644 | - $max = 0; |
|
| 645 | - |
|
| 646 | - // the number of rounds we make depends on the block size of the text |
|
| 647 | - // used during encryption/decryption |
|
| 648 | - if($this->blockSize() == 16) |
|
| 649 | - $max = 44; |
|
| 650 | - if($this->blockSize() == 24) |
|
| 651 | - $max = 78; |
|
| 652 | - if($this->blockSize() == 32) |
|
| 653 | - $max = 120; |
|
| 654 | - |
|
| 655 | - // 16 byte key expands to 176 bytes |
|
| 656 | - for($i = 0; $i < $max; ++$i) |
|
| 657 | - { |
|
| 658 | - if($i >= 0 && $i <= 3) |
|
| 659 | - $this->xkey .= $this->k($i * 4); |
|
| 660 | - else if(($i % 4) == 0) |
|
| 661 | - { |
|
| 662 | - // rotate the 4 bytes |
|
| 663 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 664 | - |
|
| 665 | - // apply the sbox |
|
| 666 | - $this->subWord($subword); |
|
| 667 | - |
|
| 668 | - // return 4 byte value based on self::$_rcon table |
|
| 669 | - //$rcon = $this->rcon(($i / 4) - 1); |
|
| 670 | - $rcon = $this->rcon(($i / 4)); |
|
| 671 | - |
|
| 672 | - // grab 4 bytes from $this->extended_key |
|
| 673 | - $ek = $this->ek(($i - 4) * 4); |
|
| 674 | - |
|
| 675 | - $h1 = parent::str2Hex($subword); |
|
| 676 | - $h2 = parent::dec2Hex($rcon); |
|
| 677 | - $h3 = parent::str2Hex($ek); |
|
| 678 | - $res = parent::xorHex($h1, $h2, $h3); |
|
| 679 | - $this->xkey .= parent::hex2Str($res); |
|
| 680 | - } |
|
| 681 | - else |
|
| 682 | - { |
|
| 683 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 684 | - $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
| 685 | - $res = parent::xorHex($h1, $h2); |
|
| 686 | - $this->xkey .= parent::hex2Str($res); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - return true; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * Expand a 24 byte key, the size it is expanded to varies |
|
| 696 | - * based on the block size of the Rijndael implementation chosen |
|
| 697 | - * |
|
| 698 | - * @return void |
|
| 699 | - */ |
|
| 700 | - private function expandKey192() |
|
| 701 | - { |
|
| 702 | - // clear the xkey, we're creating a new one |
|
| 703 | - $this->xkey = ""; |
|
| 704 | - $max = 0; |
|
| 705 | - |
|
| 706 | - // the number of rounds we make depends on the block size of the text |
|
| 707 | - // used during encryption/decryption |
|
| 708 | - if($this->blockSize() == 16) |
|
| 709 | - $max = 52; |
|
| 710 | - if($this->blockSize() == 24) |
|
| 711 | - $max = 78; |
|
| 712 | - if($this->blockSize() == 32) |
|
| 713 | - $max = 120; |
|
| 714 | - |
|
| 715 | - // 24 byte key expands to 208 bytes |
|
| 716 | - for($i = 0; $i < $max; ++$i) |
|
| 717 | - { |
|
| 718 | - if($i >= 0 && $i <= 5) |
|
| 719 | - $this->xkey .= $this->k($i * 4); |
|
| 720 | - else if(($i % 6) == 0) |
|
| 721 | - { |
|
| 722 | - // rotate the 4 bytes |
|
| 723 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 724 | - |
|
| 725 | - // apply the sbox |
|
| 726 | - $this->subWord($subword); |
|
| 727 | - |
|
| 728 | - // return 4 byte value based on self::$_rcon table |
|
| 729 | - //$rcon = $this->rcon(($i / 6) - 1); |
|
| 730 | - $rcon = $this->rcon(($i / 6)); |
|
| 731 | - |
|
| 732 | - // grab 4 bytes from $this->extended_key |
|
| 733 | - $ek = $this->ek(($i - 6) * 4); |
|
| 734 | - |
|
| 735 | - $h1 = parent::str2Hex($subword); |
|
| 736 | - $h2 = parent::dec2Hex($rcon); |
|
| 737 | - $h3 = parent::str2Hex($ek); |
|
| 738 | - $res = parent::xorHex($h1, $h2, $h3); |
|
| 739 | - $this->xkey .= parent::hex2Str($res); |
|
| 740 | - } |
|
| 741 | - else |
|
| 742 | - { |
|
| 743 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 744 | - $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
| 745 | - $res = parent::xorHex($h1, $h2); |
|
| 746 | - $this->xkey .= parent::hex2Str($res); |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - return true; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - |
|
| 754 | - /** |
|
| 755 | - * Expand a 32 byte key, the size it is expanded to varies |
|
| 756 | - * based on the block size of the Rijndael implementation chosen |
|
| 757 | - * |
|
| 758 | - * @return void |
|
| 759 | - */ |
|
| 760 | - private function expandKey256() |
|
| 761 | - { |
|
| 762 | - // clear the xkey, we're creating a new one |
|
| 763 | - $this->xkey = ""; |
|
| 764 | - $max = 0; |
|
| 765 | - |
|
| 766 | - // the number of rounds we make depends on the block size of the text |
|
| 767 | - // used during encryption/decryption |
|
| 768 | - if($this->blockSize() == 16) |
|
| 769 | - $max = 60; |
|
| 770 | - if($this->blockSize() == 24) |
|
| 771 | - $max = 90; |
|
| 772 | - if($this->blockSize() == 32) |
|
| 773 | - $max = 120; |
|
| 774 | - |
|
| 775 | - // 32 byte key expands to 240 bytes |
|
| 776 | - for($i = 0; $i < $max; ++$i) |
|
| 777 | - { |
|
| 778 | - if($i >= 0 && $i <= 7) |
|
| 779 | - $this->xkey .= $this->k($i * 4); |
|
| 780 | - else if($i % 8 == 0) |
|
| 781 | - { |
|
| 782 | - // rotate the 4 bytes |
|
| 783 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 784 | - |
|
| 785 | - // apply the sbox |
|
| 786 | - $this->subWord($subword); |
|
| 787 | - |
|
| 788 | - // return 4 byte value based on self::$_rcon table |
|
| 789 | - $rcon = $this->rcon(($i / 8)); |
|
| 790 | - |
|
| 791 | - // grab 4 bytes from $this->extended_key |
|
| 792 | - $ek = $this->ek(($i - 8) * 4); |
|
| 793 | - |
|
| 794 | - $h1 = parent::str2Hex($subword); |
|
| 795 | - $h2 = parent::dec2Hex($rcon); |
|
| 796 | - $h3 = parent::str2Hex($ek); |
|
| 797 | - $res = parent::xorHex($h1, $h2, $h3); |
|
| 798 | - $this->xkey .= parent::hex2Str($res); |
|
| 799 | - } |
|
| 800 | - else if($i % 4 == 0) |
|
| 801 | - { |
|
| 802 | - // get the subsitution from the s-box |
|
| 803 | - $subword = $this->ek(($i - 1) * 4); |
|
| 804 | - $this->subWord($subword); |
|
| 805 | - |
|
| 806 | - // get the extended key part |
|
| 807 | - $ek = $this->ek(($i - 8) * 4); |
|
| 808 | - |
|
| 809 | - // xor the two parts |
|
| 810 | - $h1 = parent::str2Hex($subword); |
|
| 811 | - $h2 = parent::str2Hex($ek); |
|
| 812 | - $res = parent::xorHex($h1, $h2); |
|
| 813 | - $this->xkey .= parent::hex2Str($res); |
|
| 814 | - } |
|
| 815 | - else |
|
| 816 | - { |
|
| 817 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 818 | - $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
| 819 | - $res = parent::xorHex($h1, $h2); |
|
| 820 | - $this->xkey .= parent::hex2Str($res); |
|
| 821 | - } |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - return true; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * Initalizes the tables used for Rijndael/AES encryption |
|
| 830 | - * |
|
| 831 | - * @return void |
|
| 832 | - */ |
|
| 833 | - private function initTables() |
|
| 834 | - { |
|
| 835 | - // the sbox used for encryption |
|
| 836 | - self::$_s = array( |
|
| 837 | - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
| 838 | - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
| 839 | - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
| 840 | - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
| 841 | - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
| 842 | - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
| 843 | - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
| 844 | - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
| 845 | - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
| 846 | - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
| 847 | - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
| 848 | - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
| 849 | - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
| 850 | - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
| 851 | - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
| 852 | - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
| 853 | - ); |
|
| 854 | - |
|
| 855 | - // the inverse sbox used for decryption |
|
| 856 | - self::$_s_inv = array( |
|
| 857 | - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
| 858 | - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
| 859 | - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
| 860 | - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
| 861 | - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
| 862 | - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
| 863 | - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
| 864 | - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
| 865 | - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
| 866 | - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
| 867 | - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
| 868 | - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
| 869 | - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
| 870 | - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
| 871 | - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
| 872 | - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
| 873 | - ); |
|
| 874 | - |
|
| 875 | - // used in mixColumn() during encryption |
|
| 876 | - self::$_matrix_mult = array( |
|
| 877 | - 0x02, 0x01, 0x01, 0x03, |
|
| 878 | - 0x03, 0x02, 0x01, 0x01, |
|
| 879 | - 0x01, 0x03, 0x02, 0x01, |
|
| 880 | - 0x01, 0x01, 0x03, 0x02 |
|
| 881 | - ); |
|
| 882 | - |
|
| 883 | - // used in mixColumn() during decryption |
|
| 884 | - self::$_matrix_mult_inv = array( |
|
| 885 | - 0x0e, 0x09, 0x0d, 0x0b, |
|
| 886 | - 0x0b, 0x0e, 0x09, 0x0d, |
|
| 887 | - 0x0d, 0x0b, 0x0e, 0x09, |
|
| 888 | - 0x09, 0x0d, 0x0b, 0x0e |
|
| 889 | - ); |
|
| 890 | - |
|
| 891 | - // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
| 892 | - // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
| 893 | - // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
| 894 | - // the other values are used for larger block/key combinations supported by Rijndael |
|
| 895 | - // NOTE: self::$_rcon[0] is never used |
|
| 896 | - self::$_rcon = array( |
|
| 897 | - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
| 898 | - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
| 899 | - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
| 900 | - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
| 901 | - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
| 902 | - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
| 903 | - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
| 904 | - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
| 905 | - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
| 906 | - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
| 907 | - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
| 908 | - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
| 909 | - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
| 910 | - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
| 911 | - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
| 912 | - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
| 913 | - ); |
|
| 914 | - |
|
| 915 | - /* |
|
| 505 | + if($this->blockSize() == 32) |
|
| 506 | + { |
|
| 507 | + if($this->operation() == parent::ENCRYPT) |
|
| 508 | + { |
|
| 509 | + // create an 8x4 matrix |
|
| 510 | + // row 0 is unchanged |
|
| 511 | + // shift row 1 left 1 byte |
|
| 512 | + // shift row 2 left 3 bytes |
|
| 513 | + // shift row 3 left 4 bytes |
|
| 514 | + $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
| 515 | + $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
| 516 | + $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
| 517 | + $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
| 518 | + $text[28].$text[1].$text[10].$text[15]; |
|
| 519 | + } |
|
| 520 | + else // parent::DECRYPT |
|
| 521 | + { |
|
| 522 | + // create an 8x4 matrix |
|
| 523 | + // row 0 is unchanged |
|
| 524 | + // shift row 1 right 1 byte |
|
| 525 | + // shift row 2 right 3 bytes |
|
| 526 | + // shift row 3 right 4 bytes |
|
| 527 | + $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
| 528 | + $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
| 529 | + $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
| 530 | + $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
| 531 | + $text[28].$text[25].$text[18].$text[15]; |
|
| 532 | + } |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Applies the Sbox to each byte of the string passed in. |
|
| 539 | + * This is similar to subByte(), but Unlike subByte() we do not use |
|
| 540 | + * the _s_inv[] table. This function is only used in expandKey(), |
|
| 541 | + * which is implemented by the class that inherits this class |
|
| 542 | + * |
|
| 543 | + * @param string $text The string to peform the byte subsitution |
|
| 544 | + * @return string The string with the subsituted bytes |
|
| 545 | + */ |
|
| 546 | + private function subWord(&$text) |
|
| 547 | + { |
|
| 548 | + $max = strlen($text); |
|
| 549 | + for($i = 0; $i < $max; ++$i) |
|
| 550 | + { |
|
| 551 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
| 552 | + // and column is numbered in hex (from 0 - f) |
|
| 553 | + $hex = parent::str2Hex($text[$i]); |
|
| 554 | + $row = parent::hex2Dec($hex[0]); |
|
| 555 | + $col = parent::hex2Dec($hex[1]); |
|
| 556 | + $pos = ($row * 16) + $col; |
|
| 557 | + |
|
| 558 | + $text[$i] = chr(self::$_s[$pos]); |
|
| 559 | + } |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Rotate a 4 byte block of the key, moving the first byte to |
|
| 565 | + * to the end, and shifting everything left |
|
| 566 | + * Used in key Expandsion |
|
| 567 | + * |
|
| 568 | + * @param string $key_block A 4 byte string |
|
| 569 | + * @return string The shifted 4 byte string |
|
| 570 | + */ |
|
| 571 | + private function rotWord($key_block) |
|
| 572 | + { |
|
| 573 | + return substr($key_block, 1, 3).$key_block[0]; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Returns 4 bytes from the expanded key starting at the given offset |
|
| 579 | + * Used during expandKey() |
|
| 580 | + * |
|
| 581 | + * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
| 582 | + * @return string A 4 byte string from the key |
|
| 583 | + */ |
|
| 584 | + private function ek($offset) |
|
| 585 | + { |
|
| 586 | + return substr($this->xkey, $offset, 4); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Returns 4 bytes of the original key from the given offset |
|
| 592 | + * Used during expandKey() |
|
| 593 | + * |
|
| 594 | + * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
| 595 | + * @return string A 4 byte string from the key |
|
| 596 | + */ |
|
| 597 | + private function k($offset) |
|
| 598 | + { |
|
| 599 | + return substr($this->key(), $offset, 4); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Return the 4 byte round constant used during expandKey(). |
|
| 605 | + * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
| 606 | + * 0x01000000 to create a 4 byte value |
|
| 607 | + * |
|
| 608 | + * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
| 609 | + * @return integer A 4 byte value |
|
| 610 | + */ |
|
| 611 | + private function rcon($pos) |
|
| 612 | + { |
|
| 613 | + return (self::$_rcon[$pos] * 0x01000000); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Expands the key |
|
| 619 | + * The key expands based on the block size as well as the key size |
|
| 620 | + * |
|
| 621 | + * @return void |
|
| 622 | + */ |
|
| 623 | + protected function expandKey() |
|
| 624 | + { |
|
| 625 | + if($this->keySize() == 16) |
|
| 626 | + return $this->expandKey128(); |
|
| 627 | + else if($this->keySize() == 24) |
|
| 628 | + return $this->expandKey192(); |
|
| 629 | + else if($this->keySize() == 32) |
|
| 630 | + return $this->expandKey256(); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Expand a 16 byte key, the size it is expanded to varies |
|
| 636 | + * based on the block size of the Rijndael implementation chosen |
|
| 637 | + * |
|
| 638 | + * @return void |
|
| 639 | + */ |
|
| 640 | + private function expandKey128() |
|
| 641 | + { |
|
| 642 | + // clear the xkey, we're creating a new one |
|
| 643 | + $this->xkey = ""; |
|
| 644 | + $max = 0; |
|
| 645 | + |
|
| 646 | + // the number of rounds we make depends on the block size of the text |
|
| 647 | + // used during encryption/decryption |
|
| 648 | + if($this->blockSize() == 16) |
|
| 649 | + $max = 44; |
|
| 650 | + if($this->blockSize() == 24) |
|
| 651 | + $max = 78; |
|
| 652 | + if($this->blockSize() == 32) |
|
| 653 | + $max = 120; |
|
| 654 | + |
|
| 655 | + // 16 byte key expands to 176 bytes |
|
| 656 | + for($i = 0; $i < $max; ++$i) |
|
| 657 | + { |
|
| 658 | + if($i >= 0 && $i <= 3) |
|
| 659 | + $this->xkey .= $this->k($i * 4); |
|
| 660 | + else if(($i % 4) == 0) |
|
| 661 | + { |
|
| 662 | + // rotate the 4 bytes |
|
| 663 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 664 | + |
|
| 665 | + // apply the sbox |
|
| 666 | + $this->subWord($subword); |
|
| 667 | + |
|
| 668 | + // return 4 byte value based on self::$_rcon table |
|
| 669 | + //$rcon = $this->rcon(($i / 4) - 1); |
|
| 670 | + $rcon = $this->rcon(($i / 4)); |
|
| 671 | + |
|
| 672 | + // grab 4 bytes from $this->extended_key |
|
| 673 | + $ek = $this->ek(($i - 4) * 4); |
|
| 674 | + |
|
| 675 | + $h1 = parent::str2Hex($subword); |
|
| 676 | + $h2 = parent::dec2Hex($rcon); |
|
| 677 | + $h3 = parent::str2Hex($ek); |
|
| 678 | + $res = parent::xorHex($h1, $h2, $h3); |
|
| 679 | + $this->xkey .= parent::hex2Str($res); |
|
| 680 | + } |
|
| 681 | + else |
|
| 682 | + { |
|
| 683 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 684 | + $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
| 685 | + $res = parent::xorHex($h1, $h2); |
|
| 686 | + $this->xkey .= parent::hex2Str($res); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + return true; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * Expand a 24 byte key, the size it is expanded to varies |
|
| 696 | + * based on the block size of the Rijndael implementation chosen |
|
| 697 | + * |
|
| 698 | + * @return void |
|
| 699 | + */ |
|
| 700 | + private function expandKey192() |
|
| 701 | + { |
|
| 702 | + // clear the xkey, we're creating a new one |
|
| 703 | + $this->xkey = ""; |
|
| 704 | + $max = 0; |
|
| 705 | + |
|
| 706 | + // the number of rounds we make depends on the block size of the text |
|
| 707 | + // used during encryption/decryption |
|
| 708 | + if($this->blockSize() == 16) |
|
| 709 | + $max = 52; |
|
| 710 | + if($this->blockSize() == 24) |
|
| 711 | + $max = 78; |
|
| 712 | + if($this->blockSize() == 32) |
|
| 713 | + $max = 120; |
|
| 714 | + |
|
| 715 | + // 24 byte key expands to 208 bytes |
|
| 716 | + for($i = 0; $i < $max; ++$i) |
|
| 717 | + { |
|
| 718 | + if($i >= 0 && $i <= 5) |
|
| 719 | + $this->xkey .= $this->k($i * 4); |
|
| 720 | + else if(($i % 6) == 0) |
|
| 721 | + { |
|
| 722 | + // rotate the 4 bytes |
|
| 723 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 724 | + |
|
| 725 | + // apply the sbox |
|
| 726 | + $this->subWord($subword); |
|
| 727 | + |
|
| 728 | + // return 4 byte value based on self::$_rcon table |
|
| 729 | + //$rcon = $this->rcon(($i / 6) - 1); |
|
| 730 | + $rcon = $this->rcon(($i / 6)); |
|
| 731 | + |
|
| 732 | + // grab 4 bytes from $this->extended_key |
|
| 733 | + $ek = $this->ek(($i - 6) * 4); |
|
| 734 | + |
|
| 735 | + $h1 = parent::str2Hex($subword); |
|
| 736 | + $h2 = parent::dec2Hex($rcon); |
|
| 737 | + $h3 = parent::str2Hex($ek); |
|
| 738 | + $res = parent::xorHex($h1, $h2, $h3); |
|
| 739 | + $this->xkey .= parent::hex2Str($res); |
|
| 740 | + } |
|
| 741 | + else |
|
| 742 | + { |
|
| 743 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 744 | + $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
| 745 | + $res = parent::xorHex($h1, $h2); |
|
| 746 | + $this->xkey .= parent::hex2Str($res); |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + return true; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + |
|
| 754 | + /** |
|
| 755 | + * Expand a 32 byte key, the size it is expanded to varies |
|
| 756 | + * based on the block size of the Rijndael implementation chosen |
|
| 757 | + * |
|
| 758 | + * @return void |
|
| 759 | + */ |
|
| 760 | + private function expandKey256() |
|
| 761 | + { |
|
| 762 | + // clear the xkey, we're creating a new one |
|
| 763 | + $this->xkey = ""; |
|
| 764 | + $max = 0; |
|
| 765 | + |
|
| 766 | + // the number of rounds we make depends on the block size of the text |
|
| 767 | + // used during encryption/decryption |
|
| 768 | + if($this->blockSize() == 16) |
|
| 769 | + $max = 60; |
|
| 770 | + if($this->blockSize() == 24) |
|
| 771 | + $max = 90; |
|
| 772 | + if($this->blockSize() == 32) |
|
| 773 | + $max = 120; |
|
| 774 | + |
|
| 775 | + // 32 byte key expands to 240 bytes |
|
| 776 | + for($i = 0; $i < $max; ++$i) |
|
| 777 | + { |
|
| 778 | + if($i >= 0 && $i <= 7) |
|
| 779 | + $this->xkey .= $this->k($i * 4); |
|
| 780 | + else if($i % 8 == 0) |
|
| 781 | + { |
|
| 782 | + // rotate the 4 bytes |
|
| 783 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
| 784 | + |
|
| 785 | + // apply the sbox |
|
| 786 | + $this->subWord($subword); |
|
| 787 | + |
|
| 788 | + // return 4 byte value based on self::$_rcon table |
|
| 789 | + $rcon = $this->rcon(($i / 8)); |
|
| 790 | + |
|
| 791 | + // grab 4 bytes from $this->extended_key |
|
| 792 | + $ek = $this->ek(($i - 8) * 4); |
|
| 793 | + |
|
| 794 | + $h1 = parent::str2Hex($subword); |
|
| 795 | + $h2 = parent::dec2Hex($rcon); |
|
| 796 | + $h3 = parent::str2Hex($ek); |
|
| 797 | + $res = parent::xorHex($h1, $h2, $h3); |
|
| 798 | + $this->xkey .= parent::hex2Str($res); |
|
| 799 | + } |
|
| 800 | + else if($i % 4 == 0) |
|
| 801 | + { |
|
| 802 | + // get the subsitution from the s-box |
|
| 803 | + $subword = $this->ek(($i - 1) * 4); |
|
| 804 | + $this->subWord($subword); |
|
| 805 | + |
|
| 806 | + // get the extended key part |
|
| 807 | + $ek = $this->ek(($i - 8) * 4); |
|
| 808 | + |
|
| 809 | + // xor the two parts |
|
| 810 | + $h1 = parent::str2Hex($subword); |
|
| 811 | + $h2 = parent::str2Hex($ek); |
|
| 812 | + $res = parent::xorHex($h1, $h2); |
|
| 813 | + $this->xkey .= parent::hex2Str($res); |
|
| 814 | + } |
|
| 815 | + else |
|
| 816 | + { |
|
| 817 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
| 818 | + $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
| 819 | + $res = parent::xorHex($h1, $h2); |
|
| 820 | + $this->xkey .= parent::hex2Str($res); |
|
| 821 | + } |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + return true; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * Initalizes the tables used for Rijndael/AES encryption |
|
| 830 | + * |
|
| 831 | + * @return void |
|
| 832 | + */ |
|
| 833 | + private function initTables() |
|
| 834 | + { |
|
| 835 | + // the sbox used for encryption |
|
| 836 | + self::$_s = array( |
|
| 837 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
| 838 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
| 839 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
| 840 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
| 841 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
| 842 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
| 843 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
| 844 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
| 845 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
| 846 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
| 847 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
| 848 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
| 849 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
| 850 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
| 851 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
| 852 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
| 853 | + ); |
|
| 854 | + |
|
| 855 | + // the inverse sbox used for decryption |
|
| 856 | + self::$_s_inv = array( |
|
| 857 | + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
| 858 | + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
| 859 | + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
| 860 | + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
| 861 | + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
| 862 | + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
| 863 | + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
| 864 | + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
| 865 | + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
| 866 | + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
| 867 | + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
| 868 | + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
| 869 | + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
| 870 | + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
| 871 | + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
| 872 | + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
| 873 | + ); |
|
| 874 | + |
|
| 875 | + // used in mixColumn() during encryption |
|
| 876 | + self::$_matrix_mult = array( |
|
| 877 | + 0x02, 0x01, 0x01, 0x03, |
|
| 878 | + 0x03, 0x02, 0x01, 0x01, |
|
| 879 | + 0x01, 0x03, 0x02, 0x01, |
|
| 880 | + 0x01, 0x01, 0x03, 0x02 |
|
| 881 | + ); |
|
| 882 | + |
|
| 883 | + // used in mixColumn() during decryption |
|
| 884 | + self::$_matrix_mult_inv = array( |
|
| 885 | + 0x0e, 0x09, 0x0d, 0x0b, |
|
| 886 | + 0x0b, 0x0e, 0x09, 0x0d, |
|
| 887 | + 0x0d, 0x0b, 0x0e, 0x09, |
|
| 888 | + 0x09, 0x0d, 0x0b, 0x0e |
|
| 889 | + ); |
|
| 890 | + |
|
| 891 | + // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
| 892 | + // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
| 893 | + // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
| 894 | + // the other values are used for larger block/key combinations supported by Rijndael |
|
| 895 | + // NOTE: self::$_rcon[0] is never used |
|
| 896 | + self::$_rcon = array( |
|
| 897 | + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
| 898 | + 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
| 899 | + 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
| 900 | + 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
| 901 | + 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
| 902 | + 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
| 903 | + 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
| 904 | + 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
| 905 | + 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
| 906 | + 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
| 907 | + 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
| 908 | + 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
| 909 | + 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
| 910 | + 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
| 911 | + 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
| 912 | + 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
| 913 | + ); |
|
| 914 | + |
|
| 915 | + /* |
|
| 916 | 916 | * Galois Multiplication lookup tables |
| 917 | 917 | * See http://en.wikipedia.org/wiki/Rijndael_mix_columns#InverseMixColumns |
| 918 | 918 | */ |
| 919 | 919 | |
| 920 | - // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
| 921 | - self::$_gm2 = array( |
|
| 922 | - 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
| 923 | - 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
| 924 | - 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
| 925 | - 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
| 926 | - 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
| 927 | - 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
| 928 | - 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
| 929 | - 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
| 930 | - 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
| 931 | - 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
| 932 | - 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
| 933 | - 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
| 934 | - 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
| 935 | - 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
| 936 | - 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
| 937 | - 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
| 938 | - ); |
|
| 939 | - |
|
| 940 | - // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
| 941 | - self::$_gm3 = array( |
|
| 942 | - 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
| 943 | - 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
| 944 | - 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
| 945 | - 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
| 946 | - 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
| 947 | - 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
| 948 | - 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
| 949 | - 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
| 950 | - 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
| 951 | - 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
| 952 | - 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
| 953 | - 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
| 954 | - 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
| 955 | - 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
| 956 | - 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
| 957 | - 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
| 958 | - ); |
|
| 959 | - |
|
| 960 | - // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
| 961 | - self::$_gm9 = array( |
|
| 962 | - 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
| 963 | - 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
| 964 | - 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
| 965 | - 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
| 966 | - 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
| 967 | - 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
| 968 | - 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
| 969 | - 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
| 970 | - 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
| 971 | - 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
| 972 | - 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
| 973 | - 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
| 974 | - 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
| 975 | - 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
| 976 | - 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
| 977 | - 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
| 978 | - ); |
|
| 979 | - |
|
| 980 | - // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
| 981 | - self::$_gm11 = array( |
|
| 982 | - 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
| 983 | - 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
| 984 | - 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
| 985 | - 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
| 986 | - 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
| 987 | - 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
| 988 | - 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
| 989 | - 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
| 990 | - 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
| 991 | - 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
| 992 | - 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
| 993 | - 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
| 994 | - 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
| 995 | - 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
| 996 | - 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
| 997 | - 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
| 998 | - ); |
|
| 999 | - |
|
| 1000 | - // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
| 1001 | - self::$_gm13 = array( |
|
| 1002 | - 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
| 1003 | - 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
| 1004 | - 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
| 1005 | - 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
| 1006 | - 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
| 1007 | - 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
| 1008 | - 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
| 1009 | - 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
| 1010 | - 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
| 1011 | - 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
| 1012 | - 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
| 1013 | - 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
| 1014 | - 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
| 1015 | - 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
| 1016 | - 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
| 1017 | - 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
| 1018 | - ); |
|
| 1019 | - |
|
| 1020 | - // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
| 1021 | - self::$_gm14 = array( |
|
| 1022 | - 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
| 1023 | - 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
| 1024 | - 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
| 1025 | - 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
| 1026 | - 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
| 1027 | - 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
| 1028 | - 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
| 1029 | - 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
| 1030 | - 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
| 1031 | - 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
| 1032 | - 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
| 1033 | - 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
| 1034 | - 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
| 1035 | - 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
| 1036 | - 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
| 1037 | - 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
| 1038 | - ); |
|
| 1039 | - } |
|
| 920 | + // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
| 921 | + self::$_gm2 = array( |
|
| 922 | + 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
| 923 | + 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
| 924 | + 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
| 925 | + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
| 926 | + 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
| 927 | + 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
| 928 | + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
| 929 | + 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
| 930 | + 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
| 931 | + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
| 932 | + 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
| 933 | + 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
| 934 | + 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
| 935 | + 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
| 936 | + 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
| 937 | + 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
| 938 | + ); |
|
| 939 | + |
|
| 940 | + // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
| 941 | + self::$_gm3 = array( |
|
| 942 | + 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
| 943 | + 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
| 944 | + 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
| 945 | + 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
| 946 | + 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
| 947 | + 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
| 948 | + 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
| 949 | + 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
| 950 | + 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
| 951 | + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
| 952 | + 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
| 953 | + 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
| 954 | + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
| 955 | + 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
| 956 | + 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
| 957 | + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
| 958 | + ); |
|
| 959 | + |
|
| 960 | + // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
| 961 | + self::$_gm9 = array( |
|
| 962 | + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
| 963 | + 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
| 964 | + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
| 965 | + 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
| 966 | + 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
| 967 | + 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
| 968 | + 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
| 969 | + 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
| 970 | + 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
| 971 | + 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
| 972 | + 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
| 973 | + 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
| 974 | + 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
| 975 | + 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
| 976 | + 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
| 977 | + 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
| 978 | + ); |
|
| 979 | + |
|
| 980 | + // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
| 981 | + self::$_gm11 = array( |
|
| 982 | + 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
| 983 | + 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
| 984 | + 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
| 985 | + 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
| 986 | + 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
| 987 | + 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
| 988 | + 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
| 989 | + 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
| 990 | + 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
| 991 | + 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
| 992 | + 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
| 993 | + 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
| 994 | + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
| 995 | + 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
| 996 | + 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
| 997 | + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
| 998 | + ); |
|
| 999 | + |
|
| 1000 | + // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
| 1001 | + self::$_gm13 = array( |
|
| 1002 | + 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
| 1003 | + 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
| 1004 | + 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
| 1005 | + 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
| 1006 | + 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
| 1007 | + 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
| 1008 | + 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
| 1009 | + 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
| 1010 | + 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
| 1011 | + 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
| 1012 | + 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
| 1013 | + 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
| 1014 | + 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
| 1015 | + 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
| 1016 | + 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
| 1017 | + 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
| 1018 | + ); |
|
| 1019 | + |
|
| 1020 | + // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
| 1021 | + self::$_gm14 = array( |
|
| 1022 | + 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
| 1023 | + 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
| 1024 | + 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
| 1025 | + 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
| 1026 | + 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
| 1027 | + 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
| 1028 | + 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
| 1029 | + 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
| 1030 | + 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
| 1031 | + 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
| 1032 | + 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
| 1033 | + 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
| 1034 | + 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
| 1035 | + 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
| 1036 | + 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
| 1037 | + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
| 1038 | + ); |
|
| 1039 | + } |
|
| 1040 | 1040 | } |
| 1041 | 1041 | ?> |
@@ -39,163 +39,163 @@ |
||
| 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 | - * @param string $data A plain text string, 8 bytes long |
|
| 77 | - * @return boolean Returns true |
|
| 78 | - */ |
|
| 79 | - public function encrypt(&$text) |
|
| 80 | - { |
|
| 81 | - $this->operation(parent::ENCRYPT); |
|
| 82 | - return $this->arc4($text); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Decrypt a ARC4 encrypted string |
|
| 88 | - * |
|
| 89 | - * @param string $encrypted A ARC4 encrypted string, 8 bytes long |
|
| 90 | - * @return boolean Returns true |
|
| 91 | - */ |
|
| 92 | - public function decrypt(&$text) |
|
| 93 | - { |
|
| 94 | - $this->operation(parent::DECRYPT); |
|
| 95 | - return $this->arc4($text); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Performs the ARC4 algorithm, since encryption and decryption |
|
| 101 | - * are the same, all the work is done here |
|
| 102 | - * |
|
| 103 | - * @param string $text The string to encrypt/decrypt |
|
| 104 | - * @return boolean Returns true |
|
| 105 | - */ |
|
| 106 | - private function arc4(&$text) |
|
| 107 | - { |
|
| 108 | - $len = strlen($text); |
|
| 109 | - $this->prga($len); |
|
| 110 | - |
|
| 111 | - for($i = 0; $i < $len; ++$i) |
|
| 112 | - $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 113 | - |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * The key scheduling algorithm (KSA) |
|
| 120 | - * |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - private function ksa() |
|
| 124 | - { |
|
| 125 | - $j = 0; |
|
| 126 | - $this->_s = array(); |
|
| 127 | - $keylen = $this->keySize(); |
|
| 128 | - $key = $this->key(); |
|
| 129 | - |
|
| 130 | - // fill $this->_s with all the values from 0-255 |
|
| 131 | - for($i = 0; $i < 256; ++$i) |
|
| 132 | - $this->_s[$i] = $i; |
|
| 133 | - |
|
| 134 | - // the changing S List |
|
| 135 | - for($i = 0; $i < 256; ++$i) |
|
| 136 | - { |
|
| 137 | - $k = $key[$i % $keylen]; |
|
| 138 | - $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
| 139 | - |
|
| 140 | - // swap bytes |
|
| 141 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * The Pseudo-Random Generation Algorithm (PGRA) |
|
| 148 | - * Creates the key stream used for encryption / decryption |
|
| 149 | - * |
|
| 150 | - * @param integer $data_len The length of the data we are encrypting/decrypting |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - private function prga($data_len) |
|
| 154 | - { |
|
| 155 | - $i = 0; |
|
| 156 | - $j = 0; |
|
| 157 | - $this->_key_stream = ""; |
|
| 158 | - |
|
| 159 | - // set up the key schedule |
|
| 160 | - $this->ksa(); |
|
| 161 | - |
|
| 162 | - for($c = 0; $c < $data_len; ++$c) |
|
| 163 | - { |
|
| 164 | - $i = ($i + 1) % 256; |
|
| 165 | - $j = ($j + $this->_s[$i]) % 256; |
|
| 166 | - |
|
| 167 | - // swap bytes |
|
| 168 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 169 | - |
|
| 170 | - $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
| 171 | - $this->_key_stream .= chr($this->_s[$pos]); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Swap two individual bytes |
|
| 178 | - * |
|
| 179 | - * @param string $a A single byte |
|
| 180 | - * @param string $b A single byte |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - private static function swapBytes(&$a, &$b) |
|
| 184 | - { |
|
| 185 | - $tmp = $a; |
|
| 186 | - $a = $b; |
|
| 187 | - $b = $tmp; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Indicates that this is a stream cipher |
|
| 193 | - * |
|
| 194 | - * @return integer Returns Cipher::STREAM |
|
| 195 | - */ |
|
| 196 | - public function type() |
|
| 197 | - { |
|
| 198 | - return parent::STREAM; |
|
| 199 | - } |
|
| 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 | + * @param string $data A plain text string, 8 bytes long |
|
| 77 | + * @return boolean Returns true |
|
| 78 | + */ |
|
| 79 | + public function encrypt(&$text) |
|
| 80 | + { |
|
| 81 | + $this->operation(parent::ENCRYPT); |
|
| 82 | + return $this->arc4($text); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Decrypt a ARC4 encrypted string |
|
| 88 | + * |
|
| 89 | + * @param string $encrypted A ARC4 encrypted string, 8 bytes long |
|
| 90 | + * @return boolean Returns true |
|
| 91 | + */ |
|
| 92 | + public function decrypt(&$text) |
|
| 93 | + { |
|
| 94 | + $this->operation(parent::DECRYPT); |
|
| 95 | + return $this->arc4($text); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Performs the ARC4 algorithm, since encryption and decryption |
|
| 101 | + * are the same, all the work is done here |
|
| 102 | + * |
|
| 103 | + * @param string $text The string to encrypt/decrypt |
|
| 104 | + * @return boolean Returns true |
|
| 105 | + */ |
|
| 106 | + private function arc4(&$text) |
|
| 107 | + { |
|
| 108 | + $len = strlen($text); |
|
| 109 | + $this->prga($len); |
|
| 110 | + |
|
| 111 | + for($i = 0; $i < $len; ++$i) |
|
| 112 | + $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 113 | + |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * The key scheduling algorithm (KSA) |
|
| 120 | + * |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + private function ksa() |
|
| 124 | + { |
|
| 125 | + $j = 0; |
|
| 126 | + $this->_s = array(); |
|
| 127 | + $keylen = $this->keySize(); |
|
| 128 | + $key = $this->key(); |
|
| 129 | + |
|
| 130 | + // fill $this->_s with all the values from 0-255 |
|
| 131 | + for($i = 0; $i < 256; ++$i) |
|
| 132 | + $this->_s[$i] = $i; |
|
| 133 | + |
|
| 134 | + // the changing S List |
|
| 135 | + for($i = 0; $i < 256; ++$i) |
|
| 136 | + { |
|
| 137 | + $k = $key[$i % $keylen]; |
|
| 138 | + $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
| 139 | + |
|
| 140 | + // swap bytes |
|
| 141 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * The Pseudo-Random Generation Algorithm (PGRA) |
|
| 148 | + * Creates the key stream used for encryption / decryption |
|
| 149 | + * |
|
| 150 | + * @param integer $data_len The length of the data we are encrypting/decrypting |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + private function prga($data_len) |
|
| 154 | + { |
|
| 155 | + $i = 0; |
|
| 156 | + $j = 0; |
|
| 157 | + $this->_key_stream = ""; |
|
| 158 | + |
|
| 159 | + // set up the key schedule |
|
| 160 | + $this->ksa(); |
|
| 161 | + |
|
| 162 | + for($c = 0; $c < $data_len; ++$c) |
|
| 163 | + { |
|
| 164 | + $i = ($i + 1) % 256; |
|
| 165 | + $j = ($j + $this->_s[$i]) % 256; |
|
| 166 | + |
|
| 167 | + // swap bytes |
|
| 168 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 169 | + |
|
| 170 | + $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
| 171 | + $this->_key_stream .= chr($this->_s[$pos]); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Swap two individual bytes |
|
| 178 | + * |
|
| 179 | + * @param string $a A single byte |
|
| 180 | + * @param string $b A single byte |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + private static function swapBytes(&$a, &$b) |
|
| 184 | + { |
|
| 185 | + $tmp = $a; |
|
| 186 | + $a = $b; |
|
| 187 | + $b = $tmp; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Indicates that this is a stream cipher |
|
| 193 | + * |
|
| 194 | + * @return integer Returns Cipher::STREAM |
|
| 195 | + */ |
|
| 196 | + public function type() |
|
| 197 | + { |
|
| 198 | + return parent::STREAM; |
|
| 199 | + } |
|
| 200 | 200 | } |
| 201 | 201 | ?> |
@@ -38,39 +38,39 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Cipher_Rijndael_192 extends Cipher_Rijndael |
| 40 | 40 | { |
| 41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | - const BYTES_BLOCK = 24; // 192 bits |
|
| 41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 42 | + const BYTES_BLOCK = 24; // 192 bits |
|
| 43 | 43 | |
| 44 | - //const BITS_KEY = 0; |
|
| 44 | + //const BITS_KEY = 0; |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor |
|
| 49 | - * Sets the key used for encryption. Also sets the requied block size |
|
| 50 | - * |
|
| 51 | - * @param string $key string containing the user supplied encryption key |
|
| 52 | - * @return void |
|
| 53 | - */ |
|
| 54 | - public function __construct($key) |
|
| 55 | - { |
|
| 56 | - // Set up the key |
|
| 57 | - parent::__construct(PHP_CRYPT::CIPHER_RIJNDAEL_192, $key); |
|
| 47 | + /** |
|
| 48 | + * Constructor |
|
| 49 | + * Sets the key used for encryption. Also sets the requied block size |
|
| 50 | + * |
|
| 51 | + * @param string $key string containing the user supplied encryption key |
|
| 52 | + * @return void |
|
| 53 | + */ |
|
| 54 | + public function __construct($key) |
|
| 55 | + { |
|
| 56 | + // Set up the key |
|
| 57 | + parent::__construct(PHP_CRYPT::CIPHER_RIJNDAEL_192, $key); |
|
| 58 | 58 | |
| 59 | - // required block size in bits |
|
| 60 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 59 | + // required block size in bits |
|
| 60 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 61 | 61 | |
| 62 | - // expand the key now that we know the key size, and the bit size |
|
| 63 | - $this->expandKey(); |
|
| 64 | - } |
|
| 62 | + // expand the key now that we know the key size, and the bit size |
|
| 63 | + $this->expandKey(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Destructor |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function __destruct() |
|
| 73 | - { |
|
| 74 | - parent::__destruct(); |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Destructor |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function __destruct() |
|
| 73 | + { |
|
| 74 | + parent::__destruct(); |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -36,222 +36,222 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | abstract class Cipher extends Core |
| 38 | 38 | { |
| 39 | - /** @type integer ENCRYPT Indicates when we are in encryption mode */ |
|
| 40 | - const ENCRYPT = 1; |
|
| 39 | + /** @type integer ENCRYPT Indicates when we are in encryption mode */ |
|
| 40 | + const ENCRYPT = 1; |
|
| 41 | 41 | |
| 42 | - /** @type integer DECRYPT Indicates when we are in decryption mode */ |
|
| 43 | - const DECRYPT = 2; |
|
| 42 | + /** @type integer DECRYPT Indicates when we are in decryption mode */ |
|
| 43 | + const DECRYPT = 2; |
|
| 44 | 44 | |
| 45 | - /** @type integer BLOCK Indicates that a cipher is a block cipher */ |
|
| 46 | - const BLOCK = 1; |
|
| 45 | + /** @type integer BLOCK Indicates that a cipher is a block cipher */ |
|
| 46 | + const BLOCK = 1; |
|
| 47 | 47 | |
| 48 | - /** @type integer STREAM Indicates that a cipher is a stream cipher */ |
|
| 49 | - const STREAM = 2; |
|
| 48 | + /** @type integer STREAM Indicates that a cipher is a stream cipher */ |
|
| 49 | + const STREAM = 2; |
|
| 50 | 50 | |
| 51 | - /** @type string $cipher_name Stores the name of the cipher */ |
|
| 52 | - protected $cipher_name = ""; |
|
| 51 | + /** @type string $cipher_name Stores the name of the cipher */ |
|
| 52 | + protected $cipher_name = ""; |
|
| 53 | 53 | |
| 54 | - /** @type integer $block_size The block size of the cipher in bytes */ |
|
| 55 | - protected $block_size = 0; |
|
| 54 | + /** @type integer $block_size The block size of the cipher in bytes */ |
|
| 55 | + protected $block_size = 0; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @type integer $operation Indicates if a cipher is Encrypting or Decrypting |
|
| 59 | - * this can be set to either Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 60 | - */ |
|
| 61 | - protected $operation = self::ENCRYPT; // can be either Cipher::ENCRYPT | Cipher::DECRYPT; |
|
| 57 | + /** |
|
| 58 | + * @type integer $operation Indicates if a cipher is Encrypting or Decrypting |
|
| 59 | + * this can be set to either Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 60 | + */ |
|
| 61 | + protected $operation = self::ENCRYPT; // can be either Cipher::ENCRYPT | Cipher::DECRYPT; |
|
| 62 | 62 | |
| 63 | - /** @type string $key Stores the key for the Cipher */ |
|
| 64 | - private $key = ""; |
|
| 63 | + /** @type string $key Stores the key for the Cipher */ |
|
| 64 | + private $key = ""; |
|
| 65 | 65 | |
| 66 | - /** @type integer $key_len Keep track of the key length, so we don't |
|
| 66 | + /** @type integer $key_len Keep track of the key length, so we don't |
|
| 67 | 67 | have to make repeated calls to strlen() to find the length */ |
| 68 | - private $key_len = 0; |
|
| 68 | + private $key_len = 0; |
|
| 69 | 69 | |
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Constructor |
|
| 73 | - * |
|
| 74 | - * @param string $name one of the predefined ciphers |
|
| 75 | - * @param string $key The key used for encryption |
|
| 76 | - * @param int Optional, the required size of a key for the cipher |
|
| 77 | - * @return void |
|
| 78 | - */ |
|
| 79 | - protected function __construct($name, $key = "", $required_key_sz = 0) |
|
| 80 | - { |
|
| 81 | - $this->name($name); |
|
| 82 | - $this->key($key, $required_key_sz); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Destructor |
|
| 88 | - * |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - protected function __destruct() |
|
| 92 | - { |
|
| 71 | + /** |
|
| 72 | + * Constructor |
|
| 73 | + * |
|
| 74 | + * @param string $name one of the predefined ciphers |
|
| 75 | + * @param string $key The key used for encryption |
|
| 76 | + * @param int Optional, the required size of a key for the cipher |
|
| 77 | + * @return void |
|
| 78 | + */ |
|
| 79 | + protected function __construct($name, $key = "", $required_key_sz = 0) |
|
| 80 | + { |
|
| 81 | + $this->name($name); |
|
| 82 | + $this->key($key, $required_key_sz); |
|
| 83 | + } |
|
| 84 | + |
|
| 93 | 85 | |
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Destructor |
|
| 88 | + * |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + protected function __destruct() |
|
| 92 | + { |
|
| 95 | 93 | |
| 94 | + } |
|
| 96 | 95 | |
| 97 | 96 | |
| 98 | - /********************************************************************** |
|
| 97 | + |
|
| 98 | + /********************************************************************** |
|
| 99 | 99 | * ABSTRACT METHODS |
| 100 | 100 | * |
| 101 | 101 | * The abstract methods required by inheriting classes to implement |
| 102 | 102 | **********************************************************************/ |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * The cipher's encryption function. Must be defined |
|
| 106 | - * by the class inheriting this Cipher object. This function |
|
| 107 | - * will most often be called from within the Mode object |
|
| 108 | - * |
|
| 109 | - * @param string $text The text to be encrypted |
|
| 110 | - * @return boolean Always returns false |
|
| 111 | - */ |
|
| 112 | - abstract public function encrypt(&$text); |
|
| 104 | + /** |
|
| 105 | + * The cipher's encryption function. Must be defined |
|
| 106 | + * by the class inheriting this Cipher object. This function |
|
| 107 | + * will most often be called from within the Mode object |
|
| 108 | + * |
|
| 109 | + * @param string $text The text to be encrypted |
|
| 110 | + * @return boolean Always returns false |
|
| 111 | + */ |
|
| 112 | + abstract public function encrypt(&$text); |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * The cipher's decryption function. Must be defined |
|
| 117 | - * by the class inheriting this Cipher object. This function |
|
| 118 | - * will most often be called from within the Mode object |
|
| 119 | - * |
|
| 120 | - * @param string $text The text to decrypt |
|
| 121 | - * @return boolean Always returns false |
|
| 122 | - */ |
|
| 123 | - abstract public function decrypt(&$text); |
|
| 115 | + /** |
|
| 116 | + * The cipher's decryption function. Must be defined |
|
| 117 | + * by the class inheriting this Cipher object. This function |
|
| 118 | + * will most often be called from within the Mode object |
|
| 119 | + * |
|
| 120 | + * @param string $text The text to decrypt |
|
| 121 | + * @return boolean Always returns false |
|
| 122 | + */ |
|
| 123 | + abstract public function decrypt(&$text); |
|
| 124 | 124 | |
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Indiciates whether the cipher is a block or stream cipher |
|
| 128 | - * |
|
| 129 | - * @return integer Returns either Cipher::BLOCK or Cipher::STREAM |
|
| 130 | - */ |
|
| 131 | - abstract public function type(); |
|
| 126 | + /** |
|
| 127 | + * Indiciates whether the cipher is a block or stream cipher |
|
| 128 | + * |
|
| 129 | + * @return integer Returns either Cipher::BLOCK or Cipher::STREAM |
|
| 130 | + */ |
|
| 131 | + abstract public function type(); |
|
| 132 | 132 | |
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | |
| 136 | - /********************************************************************** |
|
| 136 | + /********************************************************************** |
|
| 137 | 137 | * PUBLIC METHODS |
| 138 | 138 | * |
| 139 | 139 | **********************************************************************/ |
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Determine if we are Encrypting or Decrypting |
|
| 143 | - * Since some ciphers use the same algorithm to Encrypt or Decrypt but with only |
|
| 144 | - * slight differences, we need a way to check if we are Encrypting or Decrypting |
|
| 145 | - * An example is DES, which uses the same algorithm except that when Decrypting |
|
| 146 | - * the sub_keys are reversed |
|
| 147 | - * |
|
| 148 | - * @param integer $op Sets the operation to Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 149 | - * @return integer The current operation, either Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 150 | - */ |
|
| 151 | - public function operation($op = 0) |
|
| 152 | - { |
|
| 153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
| 154 | - $this->operation = $op; |
|
| 155 | - |
|
| 156 | - return $this->operation; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Return the name of cipher that is currently being used |
|
| 162 | - * |
|
| 163 | - * @return string The cipher name |
|
| 164 | - */ |
|
| 165 | - public function name($name = "") |
|
| 166 | - { |
|
| 167 | - if($name != "") |
|
| 168 | - $this->cipher_name = $name; |
|
| 169 | - |
|
| 170 | - return $this->cipher_name; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Size of the data in Bits that get used during encryption |
|
| 176 | - * |
|
| 177 | - * @param integer $bytes Number of bytes each block of data is required by the cipher |
|
| 178 | - * @return integer The number of bytes each block of data required by the cipher |
|
| 179 | - */ |
|
| 180 | - public function blockSize($bytes = 0) |
|
| 181 | - { |
|
| 182 | - if($bytes > 0) |
|
| 183 | - $this->block_size = $bytes; |
|
| 184 | - |
|
| 185 | - // in some cases a blockSize is not set, such as stream ciphers. |
|
| 186 | - // so just return 0 for the block size |
|
| 187 | - if(!isset($this->block_size)) |
|
| 188 | - return 0; |
|
| 189 | - |
|
| 190 | - return $this->block_size; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Returns the size (in bytes) required by the cipher. |
|
| 196 | - * |
|
| 197 | - * @return integer The number of bytes the cipher requires the key to be |
|
| 198 | - */ |
|
| 199 | - public function keySize() |
|
| 200 | - { |
|
| 201 | - return $this->key_len; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Set the cipher key used for encryption/decryption. This function |
|
| 207 | - * may lengthen or shorten the key to meet the size requirements of |
|
| 208 | - * the cipher. |
|
| 209 | - * |
|
| 210 | - * If the $key parameter is not given, this function simply returns the |
|
| 211 | - * current key being used. |
|
| 212 | - * |
|
| 213 | - * @param string $key Optional, A key for the cipher |
|
| 214 | - * @param integer $req_sz The byte size required for the key |
|
| 215 | - * @return string They key, which may have been modified to fit size |
|
| 216 | - * requirements |
|
| 217 | - */ |
|
| 218 | - public function key($key = "", $req_sz = 0) |
|
| 219 | - { |
|
| 220 | - if($key != "" && $key != null) |
|
| 221 | - { |
|
| 222 | - // in the case where the key is changed changed after |
|
| 223 | - // creating a new Cipher object and the $req_sz was not |
|
| 224 | - // given, we need to make sure the new key meets the size |
|
| 225 | - // requirements. This can be determined from the $this->key_len |
|
| 226 | - // member set from the previous key |
|
| 227 | - if($this->key_len > 0 && $req_sz == 0) |
|
| 228 | - $req_sz = $this->key_len; |
|
| 229 | - else |
|
| 230 | - $this->key_len = strlen($key); |
|
| 231 | - |
|
| 232 | - if($req_sz > 0) |
|
| 233 | - { |
|
| 234 | - if($this->key_len > $req_sz) |
|
| 235 | - { |
|
| 236 | - // shorten the key length |
|
| 237 | - $key = substr($key, 0, $req_sz); |
|
| 238 | - $this->key_len = $req_sz; |
|
| 239 | - } |
|
| 240 | - else if($this->key_len < $req_sz) |
|
| 241 | - { |
|
| 242 | - // send a notice that the key was too small |
|
| 243 | - // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
|
| 244 | - $msg = strtoupper($this->name())." requires a $req_sz byte key, {$this->key_len} bytes received"; |
|
| 245 | - trigger_error($msg, E_USER_WARNING); |
|
| 141 | + /** |
|
| 142 | + * Determine if we are Encrypting or Decrypting |
|
| 143 | + * Since some ciphers use the same algorithm to Encrypt or Decrypt but with only |
|
| 144 | + * slight differences, we need a way to check if we are Encrypting or Decrypting |
|
| 145 | + * An example is DES, which uses the same algorithm except that when Decrypting |
|
| 146 | + * the sub_keys are reversed |
|
| 147 | + * |
|
| 148 | + * @param integer $op Sets the operation to Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 149 | + * @return integer The current operation, either Cipher::ENCRYPT or Cipher::DECRYPT |
|
| 150 | + */ |
|
| 151 | + public function operation($op = 0) |
|
| 152 | + { |
|
| 153 | + if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
| 154 | + $this->operation = $op; |
|
| 155 | + |
|
| 156 | + return $this->operation; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Return the name of cipher that is currently being used |
|
| 162 | + * |
|
| 163 | + * @return string The cipher name |
|
| 164 | + */ |
|
| 165 | + public function name($name = "") |
|
| 166 | + { |
|
| 167 | + if($name != "") |
|
| 168 | + $this->cipher_name = $name; |
|
| 169 | + |
|
| 170 | + return $this->cipher_name; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Size of the data in Bits that get used during encryption |
|
| 176 | + * |
|
| 177 | + * @param integer $bytes Number of bytes each block of data is required by the cipher |
|
| 178 | + * @return integer The number of bytes each block of data required by the cipher |
|
| 179 | + */ |
|
| 180 | + public function blockSize($bytes = 0) |
|
| 181 | + { |
|
| 182 | + if($bytes > 0) |
|
| 183 | + $this->block_size = $bytes; |
|
| 184 | + |
|
| 185 | + // in some cases a blockSize is not set, such as stream ciphers. |
|
| 186 | + // so just return 0 for the block size |
|
| 187 | + if(!isset($this->block_size)) |
|
| 188 | + return 0; |
|
| 189 | + |
|
| 190 | + return $this->block_size; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Returns the size (in bytes) required by the cipher. |
|
| 196 | + * |
|
| 197 | + * @return integer The number of bytes the cipher requires the key to be |
|
| 198 | + */ |
|
| 199 | + public function keySize() |
|
| 200 | + { |
|
| 201 | + return $this->key_len; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Set the cipher key used for encryption/decryption. This function |
|
| 207 | + * may lengthen or shorten the key to meet the size requirements of |
|
| 208 | + * the cipher. |
|
| 209 | + * |
|
| 210 | + * If the $key parameter is not given, this function simply returns the |
|
| 211 | + * current key being used. |
|
| 212 | + * |
|
| 213 | + * @param string $key Optional, A key for the cipher |
|
| 214 | + * @param integer $req_sz The byte size required for the key |
|
| 215 | + * @return string They key, which may have been modified to fit size |
|
| 216 | + * requirements |
|
| 217 | + */ |
|
| 218 | + public function key($key = "", $req_sz = 0) |
|
| 219 | + { |
|
| 220 | + if($key != "" && $key != null) |
|
| 221 | + { |
|
| 222 | + // in the case where the key is changed changed after |
|
| 223 | + // creating a new Cipher object and the $req_sz was not |
|
| 224 | + // given, we need to make sure the new key meets the size |
|
| 225 | + // requirements. This can be determined from the $this->key_len |
|
| 226 | + // member set from the previous key |
|
| 227 | + if($this->key_len > 0 && $req_sz == 0) |
|
| 228 | + $req_sz = $this->key_len; |
|
| 229 | + else |
|
| 230 | + $this->key_len = strlen($key); |
|
| 231 | + |
|
| 232 | + if($req_sz > 0) |
|
| 233 | + { |
|
| 234 | + if($this->key_len > $req_sz) |
|
| 235 | + { |
|
| 236 | + // shorten the key length |
|
| 237 | + $key = substr($key, 0, $req_sz); |
|
| 238 | + $this->key_len = $req_sz; |
|
| 239 | + } |
|
| 240 | + else if($this->key_len < $req_sz) |
|
| 241 | + { |
|
| 242 | + // send a notice that the key was too small |
|
| 243 | + // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
|
| 244 | + $msg = strtoupper($this->name())." requires a $req_sz byte key, {$this->key_len} bytes received"; |
|
| 245 | + trigger_error($msg, E_USER_WARNING); |
|
| 246 | 246 | |
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - } |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | - $this->key = $key; |
|
| 252 | - } |
|
| 251 | + $this->key = $key; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - return $this->key; |
|
| 255 | - } |
|
| 254 | + return $this->key; |
|
| 255 | + } |
|
| 256 | 256 | } |
| 257 | 257 | ?> |
@@ -36,167 +36,167 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Mode_CTR extends Mode |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * Constructor |
|
| 41 | - * Sets the cipher object that will be used for encryption |
|
| 42 | - * |
|
| 43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | - * @return void |
|
| 45 | - */ |
|
| 46 | - function __construct($cipher) |
|
| 47 | - { |
|
| 48 | - parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
|
| 49 | - |
|
| 50 | - // this works with only block Ciphers |
|
| 51 | - if($cipher->type() != Cipher::BLOCK) |
|
| 52 | - trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Destructor |
|
| 58 | - * |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public function __destruct() |
|
| 62 | - { |
|
| 63 | - parent::__destruct(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Encrypts an encrypted string |
|
| 69 | - * |
|
| 70 | - * @param string $text the string to be encrypted |
|
| 71 | - * @return boolean Returns true |
|
| 72 | - */ |
|
| 73 | - public function encrypt(&$text) |
|
| 74 | - { |
|
| 75 | - $len = strlen($text); |
|
| 76 | - $blocksz = $this->cipher->blockSize(); |
|
| 77 | - |
|
| 78 | - $max = $len / $blocksz; |
|
| 79 | - for($i = 0; $i < $max; ++$i) |
|
| 80 | - { |
|
| 81 | - // get the current position in $text |
|
| 82 | - $pos = $i * $blocksz; |
|
| 83 | - |
|
| 84 | - // make sure we don't extend past the length of $text |
|
| 85 | - $byte_len = $blocksz; |
|
| 86 | - if(($pos + $byte_len) > $len) |
|
| 87 | - $byte_len -= ($pos + $byte_len) - $len; |
|
| 88 | - |
|
| 89 | - // encrypt the register |
|
| 90 | - $this->enc_register = $this->register; |
|
| 91 | - $this->cipher->encrypt($this->enc_register); |
|
| 92 | - |
|
| 93 | - // grab a block of plain text |
|
| 94 | - $block = substr($text, $pos, $byte_len); |
|
| 95 | - |
|
| 96 | - // xor the block |
|
| 97 | - for($j = 0; $j < $byte_len; ++$j) |
|
| 98 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 99 | - |
|
| 100 | - // replace the plain text block with the encrypted block |
|
| 101 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 102 | - |
|
| 103 | - // increment the counter |
|
| 104 | - $this->counter(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return true; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Decrypt an encrypted string |
|
| 113 | - * |
|
| 114 | - * @param string $text The string to be decrypted |
|
| 115 | - * @return boolean Returns true |
|
| 116 | - */ |
|
| 117 | - public function decrypt(&$text) |
|
| 118 | - { |
|
| 119 | - $len = strlen($text); |
|
| 120 | - $blocksz = $this->cipher->blockSize(); |
|
| 121 | - |
|
| 122 | - $max = $len / $blocksz; |
|
| 123 | - for($i = 0; $i < $max; ++$i) |
|
| 124 | - { |
|
| 125 | - // get the current position in $text |
|
| 126 | - $pos = $i * $blocksz; |
|
| 127 | - |
|
| 128 | - // make sure we don't extend past the length of $text |
|
| 129 | - $byte_len = $blocksz; |
|
| 130 | - if(($pos + $byte_len) > $len) |
|
| 131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
| 132 | - |
|
| 133 | - // encrypt the register |
|
| 134 | - $this->enc_register = $this->register; |
|
| 135 | - $this->cipher->encrypt($this->enc_register); |
|
| 136 | - |
|
| 137 | - // grab a block of plain text |
|
| 138 | - $block = substr($text, $pos, $byte_len); |
|
| 139 | - |
|
| 140 | - // xor the block with the register (which contains the IV) |
|
| 141 | - for($j = 0; $j < $byte_len; ++$j) |
|
| 142 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 143 | - |
|
| 144 | - // replace the encrypted block with the plain text |
|
| 145 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 146 | - |
|
| 147 | - // increment the counter |
|
| 148 | - $this->counter(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return true; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * This mode requires an IV |
|
| 157 | - * |
|
| 158 | - * @return boolean True |
|
| 159 | - */ |
|
| 160 | - public function requiresIV() |
|
| 161 | - { |
|
| 162 | - return true; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Increments the counter (which is initially the IV) by one byte starting at the |
|
| 168 | - * last byte. Once that byte has reached 0xff, it is then set to 0x00, and the |
|
| 169 | - * next byte is then incremented. On the following incrementation, the last byte |
|
| 170 | - * is incremented again. An example: |
|
| 171 | - * PASS 1: 2037e9ae63f73dfe |
|
| 172 | - * PASS 2: 2037e9ae63f73dff |
|
| 173 | - * PASS 3: 2037e9ae63f73e00 |
|
| 174 | - * PASS 4: 2037e9ae63f73e01 |
|
| 175 | - * ... |
|
| 176 | - * PASS N: 2100000000000001 |
|
| 177 | - * PASS N+1: 2100000000000002 |
|
| 178 | - * |
|
| 179 | - * @return void |
|
| 180 | - */ |
|
| 181 | - private function counter() |
|
| 182 | - { |
|
| 183 | - $pos = $this->cipher->blockSize() - 1; |
|
| 184 | - |
|
| 185 | - // starting at the last byte, loop through each byte until |
|
| 186 | - // we find one that can be incremented |
|
| 187 | - for($i = $pos; $i >= 0; --$i) |
|
| 188 | - { |
|
| 189 | - // if we reached the last byte, set it to 0x00, then |
|
| 190 | - // loop one more time to increment the next byte |
|
| 191 | - if(ord($this->register[$i]) == 0xff) |
|
| 192 | - $this->register[$i] = chr(0x00); |
|
| 193 | - else |
|
| 194 | - { |
|
| 195 | - // now increment the byte by 1 |
|
| 196 | - $this->register[$i] = chr(ord($this->register[$i]) + 1); |
|
| 197 | - break; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 39 | + /** |
|
| 40 | + * Constructor |
|
| 41 | + * Sets the cipher object that will be used for encryption |
|
| 42 | + * |
|
| 43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | + * @return void |
|
| 45 | + */ |
|
| 46 | + function __construct($cipher) |
|
| 47 | + { |
|
| 48 | + parent::__construct(PHP_CRYPT::MODE_CTR, $cipher); |
|
| 49 | + |
|
| 50 | + // this works with only block Ciphers |
|
| 51 | + if($cipher->type() != Cipher::BLOCK) |
|
| 52 | + trigger_error("CTR mode requires a block cipher", E_USER_WARNING); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Destructor |
|
| 58 | + * |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public function __destruct() |
|
| 62 | + { |
|
| 63 | + parent::__destruct(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Encrypts an encrypted string |
|
| 69 | + * |
|
| 70 | + * @param string $text the string to be encrypted |
|
| 71 | + * @return boolean Returns true |
|
| 72 | + */ |
|
| 73 | + public function encrypt(&$text) |
|
| 74 | + { |
|
| 75 | + $len = strlen($text); |
|
| 76 | + $blocksz = $this->cipher->blockSize(); |
|
| 77 | + |
|
| 78 | + $max = $len / $blocksz; |
|
| 79 | + for($i = 0; $i < $max; ++$i) |
|
| 80 | + { |
|
| 81 | + // get the current position in $text |
|
| 82 | + $pos = $i * $blocksz; |
|
| 83 | + |
|
| 84 | + // make sure we don't extend past the length of $text |
|
| 85 | + $byte_len = $blocksz; |
|
| 86 | + if(($pos + $byte_len) > $len) |
|
| 87 | + $byte_len -= ($pos + $byte_len) - $len; |
|
| 88 | + |
|
| 89 | + // encrypt the register |
|
| 90 | + $this->enc_register = $this->register; |
|
| 91 | + $this->cipher->encrypt($this->enc_register); |
|
| 92 | + |
|
| 93 | + // grab a block of plain text |
|
| 94 | + $block = substr($text, $pos, $byte_len); |
|
| 95 | + |
|
| 96 | + // xor the block |
|
| 97 | + for($j = 0; $j < $byte_len; ++$j) |
|
| 98 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 99 | + |
|
| 100 | + // replace the plain text block with the encrypted block |
|
| 101 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 102 | + |
|
| 103 | + // increment the counter |
|
| 104 | + $this->counter(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return true; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Decrypt an encrypted string |
|
| 113 | + * |
|
| 114 | + * @param string $text The string to be decrypted |
|
| 115 | + * @return boolean Returns true |
|
| 116 | + */ |
|
| 117 | + public function decrypt(&$text) |
|
| 118 | + { |
|
| 119 | + $len = strlen($text); |
|
| 120 | + $blocksz = $this->cipher->blockSize(); |
|
| 121 | + |
|
| 122 | + $max = $len / $blocksz; |
|
| 123 | + for($i = 0; $i < $max; ++$i) |
|
| 124 | + { |
|
| 125 | + // get the current position in $text |
|
| 126 | + $pos = $i * $blocksz; |
|
| 127 | + |
|
| 128 | + // make sure we don't extend past the length of $text |
|
| 129 | + $byte_len = $blocksz; |
|
| 130 | + if(($pos + $byte_len) > $len) |
|
| 131 | + $byte_len -= ($pos + $byte_len) - $len; |
|
| 132 | + |
|
| 133 | + // encrypt the register |
|
| 134 | + $this->enc_register = $this->register; |
|
| 135 | + $this->cipher->encrypt($this->enc_register); |
|
| 136 | + |
|
| 137 | + // grab a block of plain text |
|
| 138 | + $block = substr($text, $pos, $byte_len); |
|
| 139 | + |
|
| 140 | + // xor the block with the register (which contains the IV) |
|
| 141 | + for($j = 0; $j < $byte_len; ++$j) |
|
| 142 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 143 | + |
|
| 144 | + // replace the encrypted block with the plain text |
|
| 145 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 146 | + |
|
| 147 | + // increment the counter |
|
| 148 | + $this->counter(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return true; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * This mode requires an IV |
|
| 157 | + * |
|
| 158 | + * @return boolean True |
|
| 159 | + */ |
|
| 160 | + public function requiresIV() |
|
| 161 | + { |
|
| 162 | + return true; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Increments the counter (which is initially the IV) by one byte starting at the |
|
| 168 | + * last byte. Once that byte has reached 0xff, it is then set to 0x00, and the |
|
| 169 | + * next byte is then incremented. On the following incrementation, the last byte |
|
| 170 | + * is incremented again. An example: |
|
| 171 | + * PASS 1: 2037e9ae63f73dfe |
|
| 172 | + * PASS 2: 2037e9ae63f73dff |
|
| 173 | + * PASS 3: 2037e9ae63f73e00 |
|
| 174 | + * PASS 4: 2037e9ae63f73e01 |
|
| 175 | + * ... |
|
| 176 | + * PASS N: 2100000000000001 |
|
| 177 | + * PASS N+1: 2100000000000002 |
|
| 178 | + * |
|
| 179 | + * @return void |
|
| 180 | + */ |
|
| 181 | + private function counter() |
|
| 182 | + { |
|
| 183 | + $pos = $this->cipher->blockSize() - 1; |
|
| 184 | + |
|
| 185 | + // starting at the last byte, loop through each byte until |
|
| 186 | + // we find one that can be incremented |
|
| 187 | + for($i = $pos; $i >= 0; --$i) |
|
| 188 | + { |
|
| 189 | + // if we reached the last byte, set it to 0x00, then |
|
| 190 | + // loop one more time to increment the next byte |
|
| 191 | + if(ord($this->register[$i]) == 0xff) |
|
| 192 | + $this->register[$i] = chr(0x00); |
|
| 193 | + else |
|
| 194 | + { |
|
| 195 | + // now increment the byte by 1 |
|
| 196 | + $this->register[$i] = chr(ord($this->register[$i]) + 1); |
|
| 197 | + break; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | 201 | } |
| 202 | 202 | ?> |
@@ -36,107 +36,107 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Mode_ECB extends Mode |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * Constructor |
|
| 41 | - * Sets the cipher object that will be used for encryption |
|
| 42 | - * |
|
| 43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | - * @param int $block_size The size of blocks (in bits) used for encryption |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function __construct($cipher) |
|
| 48 | - { |
|
| 49 | - parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
| 50 | - |
|
| 51 | - // this works with only block Ciphers |
|
| 52 | - if($cipher->type() != Cipher::BLOCK) |
|
| 53 | - trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Destructor |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function __destruct() |
|
| 63 | - { |
|
| 64 | - parent::__destruct(); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
| 70 | - * to the constructor in ECB mode |
|
| 71 | - * |
|
| 72 | - * @param string $text The string to be encrypted in ECB mode |
|
| 73 | - * @return boolean Returns true |
|
| 74 | - */ |
|
| 75 | - public function encrypt(&$text) |
|
| 76 | - { |
|
| 77 | - $this->pad($text); |
|
| 78 | - $blocksz = $this->cipher->blockSize(); |
|
| 79 | - |
|
| 80 | - $max = strlen($text) / $blocksz; |
|
| 81 | - for($i = 0; $i < $max; ++$i) |
|
| 82 | - { |
|
| 83 | - // get the current position within $text |
|
| 84 | - $pos = $i * $blocksz; |
|
| 85 | - |
|
| 86 | - // grab a block of text |
|
| 87 | - $block = substr($text, $pos, $blocksz); |
|
| 88 | - |
|
| 89 | - // encrypt the block |
|
| 90 | - $this->cipher->encrypt($block); |
|
| 91 | - |
|
| 92 | - // replace the plain text with the cipher text |
|
| 93 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return true; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
| 102 | - * to the constructor in ECB mode |
|
| 103 | - * |
|
| 104 | - * @param string $text The string to be decrypted in ECB mode |
|
| 105 | - * @return boolean Returns true |
|
| 106 | - */ |
|
| 107 | - public function decrypt(&$text) |
|
| 108 | - { |
|
| 109 | - $blocksz = $this->cipher->blockSize(); |
|
| 110 | - |
|
| 111 | - $max = strlen($text) / $blocksz; |
|
| 112 | - for($i = 0; $i < $max; ++$i) |
|
| 113 | - { |
|
| 114 | - // get the current position within $text |
|
| 115 | - $pos = $i * $blocksz; |
|
| 116 | - |
|
| 117 | - // get a block of cipher text |
|
| 118 | - $block = substr($text, $pos, $blocksz); |
|
| 119 | - |
|
| 120 | - // decrypt the block |
|
| 121 | - $this->cipher->decrypt($block); |
|
| 122 | - |
|
| 123 | - // replace the block of cipher text with plain text |
|
| 124 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $this->strip($text); |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * This mode does not require an IV |
|
| 134 | - * |
|
| 135 | - * @return boolean Returns false |
|
| 136 | - */ |
|
| 137 | - public function requiresIV() |
|
| 138 | - { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 39 | + /** |
|
| 40 | + * Constructor |
|
| 41 | + * Sets the cipher object that will be used for encryption |
|
| 42 | + * |
|
| 43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | + * @param int $block_size The size of blocks (in bits) used for encryption |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function __construct($cipher) |
|
| 48 | + { |
|
| 49 | + parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
| 50 | + |
|
| 51 | + // this works with only block Ciphers |
|
| 52 | + if($cipher->type() != Cipher::BLOCK) |
|
| 53 | + trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Destructor |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function __destruct() |
|
| 63 | + { |
|
| 64 | + parent::__destruct(); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
| 70 | + * to the constructor in ECB mode |
|
| 71 | + * |
|
| 72 | + * @param string $text The string to be encrypted in ECB mode |
|
| 73 | + * @return boolean Returns true |
|
| 74 | + */ |
|
| 75 | + public function encrypt(&$text) |
|
| 76 | + { |
|
| 77 | + $this->pad($text); |
|
| 78 | + $blocksz = $this->cipher->blockSize(); |
|
| 79 | + |
|
| 80 | + $max = strlen($text) / $blocksz; |
|
| 81 | + for($i = 0; $i < $max; ++$i) |
|
| 82 | + { |
|
| 83 | + // get the current position within $text |
|
| 84 | + $pos = $i * $blocksz; |
|
| 85 | + |
|
| 86 | + // grab a block of text |
|
| 87 | + $block = substr($text, $pos, $blocksz); |
|
| 88 | + |
|
| 89 | + // encrypt the block |
|
| 90 | + $this->cipher->encrypt($block); |
|
| 91 | + |
|
| 92 | + // replace the plain text with the cipher text |
|
| 93 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return true; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
| 102 | + * to the constructor in ECB mode |
|
| 103 | + * |
|
| 104 | + * @param string $text The string to be decrypted in ECB mode |
|
| 105 | + * @return boolean Returns true |
|
| 106 | + */ |
|
| 107 | + public function decrypt(&$text) |
|
| 108 | + { |
|
| 109 | + $blocksz = $this->cipher->blockSize(); |
|
| 110 | + |
|
| 111 | + $max = strlen($text) / $blocksz; |
|
| 112 | + for($i = 0; $i < $max; ++$i) |
|
| 113 | + { |
|
| 114 | + // get the current position within $text |
|
| 115 | + $pos = $i * $blocksz; |
|
| 116 | + |
|
| 117 | + // get a block of cipher text |
|
| 118 | + $block = substr($text, $pos, $blocksz); |
|
| 119 | + |
|
| 120 | + // decrypt the block |
|
| 121 | + $this->cipher->decrypt($block); |
|
| 122 | + |
|
| 123 | + // replace the block of cipher text with plain text |
|
| 124 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $this->strip($text); |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * This mode does not require an IV |
|
| 134 | + * |
|
| 135 | + * @return boolean Returns false |
|
| 136 | + */ |
|
| 137 | + public function requiresIV() |
|
| 138 | + { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | } |
| 142 | 142 | ?> |
@@ -37,128 +37,128 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class Mode_NOFB extends Mode |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * Constructor |
|
| 42 | - * Sets the cipher object that will be used for encryption |
|
| 43 | - * |
|
| 44 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function __construct($cipher) |
|
| 48 | - { |
|
| 49 | - parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
|
| 50 | - |
|
| 51 | - // this works with only block Ciphers |
|
| 52 | - if($cipher->type() != Cipher::BLOCK) |
|
| 53 | - trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Destructor |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function __destruct() |
|
| 63 | - { |
|
| 64 | - parent::__destruct(); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
| 70 | - * |
|
| 71 | - * @param string $text the string to be encrypted |
|
| 72 | - * @return boolean Returns true |
|
| 73 | - */ |
|
| 74 | - public function encrypt(&$text) |
|
| 75 | - { |
|
| 76 | - $len = strlen($text); |
|
| 77 | - $blocksz = $this->cipher->blockSize(); |
|
| 78 | - |
|
| 79 | - $max = $len / $blocksz; |
|
| 80 | - for($i = 0; $i < $max; ++$i) |
|
| 81 | - { |
|
| 82 | - // current position in the text |
|
| 83 | - $pos = $i * $blocksz; |
|
| 84 | - |
|
| 85 | - // make sure we don't extend past the length of $text |
|
| 86 | - $byte_len = $blocksz; |
|
| 87 | - if(($pos + $blocksz) > $len) |
|
| 88 | - $byte_len -= ($pos + $blocksz) - $len; |
|
| 89 | - |
|
| 90 | - // encrypt the register |
|
| 91 | - $this->enc_register = $this->register; |
|
| 92 | - $this->cipher->encrypt($this->enc_register); |
|
| 93 | - |
|
| 94 | - // now grab a block of text and a block of from the register, and XOR them |
|
| 95 | - $block = substr($text, $pos, $byte_len); |
|
| 96 | - for($j = 0; $j < $byte_len; ++$j) |
|
| 97 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 98 | - |
|
| 99 | - // replace the plain text block with encrypted text |
|
| 100 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 101 | - |
|
| 102 | - // shift the register left n-bytes, append n-bytes of encrypted register |
|
| 103 | - $this->register = substr($this->register, $byte_len); |
|
| 104 | - $this->register .= substr($this->enc_register, 0, $byte_len); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return true; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
| 113 | - * |
|
| 114 | - * @param string $text the string to be decrypted |
|
| 115 | - * @return boolean Returns true |
|
| 116 | - */ |
|
| 117 | - public function decrypt(&$text) |
|
| 118 | - { |
|
| 119 | - $len = strlen($text); |
|
| 120 | - $blocksz = $this->cipher->blockSize(); |
|
| 121 | - |
|
| 122 | - $max = $len / $blocksz; |
|
| 123 | - for($i = 0; $i < $max; ++$i) |
|
| 124 | - { |
|
| 125 | - // current position within $text |
|
| 126 | - $pos = $i * $blocksz; |
|
| 127 | - |
|
| 128 | - // make sure we don't extend past the length of $text |
|
| 129 | - $byte_len = $blocksz; |
|
| 130 | - if(($pos + $byte_len) > $len) |
|
| 131 | - $byte_len -= ($pos + $byte_len) - $len; |
|
| 132 | - |
|
| 133 | - // encrypt the register |
|
| 134 | - $this->enc_register = $this->register; |
|
| 135 | - $this->cipher->encrypt($this->enc_register); |
|
| 136 | - |
|
| 137 | - // shift the register left n-bytes, append n-bytes of encrypted register |
|
| 138 | - $this->register = substr($this->register, $byte_len); |
|
| 139 | - $this->register .= substr($this->enc_register, 0, $byte_len); |
|
| 140 | - |
|
| 141 | - // now grab a block of text and xor with the register |
|
| 142 | - $block = substr($text, $pos, $byte_len); |
|
| 143 | - for($j = 0; $j < $byte_len; ++$j) |
|
| 144 | - $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 145 | - |
|
| 146 | - // replace the encrypted block with plain text |
|
| 147 | - $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * This mode requires an IV |
|
| 156 | - * |
|
| 157 | - * @return boolean true |
|
| 158 | - */ |
|
| 159 | - public function requiresIV() |
|
| 160 | - { |
|
| 161 | - return true; |
|
| 162 | - } |
|
| 40 | + /** |
|
| 41 | + * Constructor |
|
| 42 | + * Sets the cipher object that will be used for encryption |
|
| 43 | + * |
|
| 44 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function __construct($cipher) |
|
| 48 | + { |
|
| 49 | + parent::__construct(PHP_Crypt::MODE_NOFB, $cipher); |
|
| 50 | + |
|
| 51 | + // this works with only block Ciphers |
|
| 52 | + if($cipher->type() != Cipher::BLOCK) |
|
| 53 | + trigger_error("NOFB mode requires a block cipher", E_USER_WARNING); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Destructor |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function __destruct() |
|
| 63 | + { |
|
| 64 | + parent::__destruct(); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
| 70 | + * |
|
| 71 | + * @param string $text the string to be encrypted |
|
| 72 | + * @return boolean Returns true |
|
| 73 | + */ |
|
| 74 | + public function encrypt(&$text) |
|
| 75 | + { |
|
| 76 | + $len = strlen($text); |
|
| 77 | + $blocksz = $this->cipher->blockSize(); |
|
| 78 | + |
|
| 79 | + $max = $len / $blocksz; |
|
| 80 | + for($i = 0; $i < $max; ++$i) |
|
| 81 | + { |
|
| 82 | + // current position in the text |
|
| 83 | + $pos = $i * $blocksz; |
|
| 84 | + |
|
| 85 | + // make sure we don't extend past the length of $text |
|
| 86 | + $byte_len = $blocksz; |
|
| 87 | + if(($pos + $blocksz) > $len) |
|
| 88 | + $byte_len -= ($pos + $blocksz) - $len; |
|
| 89 | + |
|
| 90 | + // encrypt the register |
|
| 91 | + $this->enc_register = $this->register; |
|
| 92 | + $this->cipher->encrypt($this->enc_register); |
|
| 93 | + |
|
| 94 | + // now grab a block of text and a block of from the register, and XOR them |
|
| 95 | + $block = substr($text, $pos, $byte_len); |
|
| 96 | + for($j = 0; $j < $byte_len; ++$j) |
|
| 97 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 98 | + |
|
| 99 | + // replace the plain text block with encrypted text |
|
| 100 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 101 | + |
|
| 102 | + // shift the register left n-bytes, append n-bytes of encrypted register |
|
| 103 | + $this->register = substr($this->register, $byte_len); |
|
| 104 | + $this->register .= substr($this->enc_register, 0, $byte_len); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return true; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
| 113 | + * |
|
| 114 | + * @param string $text the string to be decrypted |
|
| 115 | + * @return boolean Returns true |
|
| 116 | + */ |
|
| 117 | + public function decrypt(&$text) |
|
| 118 | + { |
|
| 119 | + $len = strlen($text); |
|
| 120 | + $blocksz = $this->cipher->blockSize(); |
|
| 121 | + |
|
| 122 | + $max = $len / $blocksz; |
|
| 123 | + for($i = 0; $i < $max; ++$i) |
|
| 124 | + { |
|
| 125 | + // current position within $text |
|
| 126 | + $pos = $i * $blocksz; |
|
| 127 | + |
|
| 128 | + // make sure we don't extend past the length of $text |
|
| 129 | + $byte_len = $blocksz; |
|
| 130 | + if(($pos + $byte_len) > $len) |
|
| 131 | + $byte_len -= ($pos + $byte_len) - $len; |
|
| 132 | + |
|
| 133 | + // encrypt the register |
|
| 134 | + $this->enc_register = $this->register; |
|
| 135 | + $this->cipher->encrypt($this->enc_register); |
|
| 136 | + |
|
| 137 | + // shift the register left n-bytes, append n-bytes of encrypted register |
|
| 138 | + $this->register = substr($this->register, $byte_len); |
|
| 139 | + $this->register .= substr($this->enc_register, 0, $byte_len); |
|
| 140 | + |
|
| 141 | + // now grab a block of text and xor with the register |
|
| 142 | + $block = substr($text, $pos, $byte_len); |
|
| 143 | + for($j = 0; $j < $byte_len; ++$j) |
|
| 144 | + $block[$j] = $block[$j] ^ $this->enc_register[$j]; |
|
| 145 | + |
|
| 146 | + // replace the encrypted block with plain text |
|
| 147 | + $text = substr_replace($text, $block, $pos, $byte_len); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * This mode requires an IV |
|
| 156 | + * |
|
| 157 | + * @return boolean true |
|
| 158 | + */ |
|
| 159 | + public function requiresIV() |
|
| 160 | + { |
|
| 161 | + return true; |
|
| 162 | + } |
|
| 163 | 163 | } |
| 164 | 164 | ?> |
@@ -36,132 +36,132 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Mode_CBC extends Mode |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * Constructor |
|
| 41 | - * Sets the cipher object that will be used for encryption |
|
| 42 | - * |
|
| 43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | - * @return void |
|
| 45 | - */ |
|
| 46 | - function __construct($cipher) |
|
| 47 | - { |
|
| 48 | - parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
|
| 49 | - |
|
| 50 | - // this works with only block Ciphers |
|
| 51 | - if($cipher->type() != Cipher::BLOCK) |
|
| 52 | - trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Destructor |
|
| 58 | - * |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public function __destruct() |
|
| 62 | - { |
|
| 63 | - parent::__destruct(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
| 69 | - * to the constructor in CBC mode |
|
| 70 | - * The steps to encrypt using CBC are as follows |
|
| 71 | - * 1) Get a block of plain text data to use in the Cipher |
|
| 72 | - * 2) XOR the block with IV (if it's the first round) or the previous |
|
| 73 | - * round's encrypted result |
|
| 74 | - * 3) Encrypt the block using the cipher |
|
| 75 | - * 4) Save the encrypted block to use in the next round |
|
| 76 | - * |
|
| 77 | - * @param string $text the string to be encrypted in CBC mode |
|
| 78 | - * @return boolean Returns true |
|
| 79 | - */ |
|
| 80 | - public function encrypt(&$text) |
|
| 81 | - { |
|
| 82 | - $this->pad($text); |
|
| 83 | - $blocksz = $this->cipher->blockSize(); |
|
| 84 | - |
|
| 85 | - $max = strlen($text) / $blocksz; |
|
| 86 | - for($i = 0; $i < $max; ++$i) |
|
| 87 | - { |
|
| 88 | - // get the current position in $text |
|
| 89 | - $pos = $i * $blocksz; |
|
| 90 | - |
|
| 91 | - // grab a block of plain text |
|
| 92 | - $block = substr($text, $pos, $blocksz); |
|
| 93 | - |
|
| 94 | - // xor the block with the register |
|
| 95 | - for($j = 0; $j < $blocksz; ++$j) |
|
| 96 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
| 97 | - |
|
| 98 | - // encrypt the block, and save it back to the register |
|
| 99 | - $this->cipher->encrypt($block); |
|
| 100 | - $this->register = $block; |
|
| 101 | - |
|
| 102 | - // replace the plain text block with the cipher text |
|
| 103 | - $text = substr_replace($text, $this->register, $pos, $blocksz); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return true; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
| 112 | - * to the constructor in CBC mode |
|
| 113 | - * The decryption algorithm requires the following steps |
|
| 114 | - * 1) Get the first block of encrypted text, save it |
|
| 115 | - * 2) Decrypt the block |
|
| 116 | - * 3) XOR the decrypted block (if it's the first pass use the IV, |
|
| 117 | - * else use the encrypted block from step 1 |
|
| 118 | - * 4) the result from the XOR will be a plain text block, save it |
|
| 119 | - * 5) assign the encrypted block from step 1 to use in the next round |
|
| 120 | - * |
|
| 121 | - * @param string $text the string to be decrypted in CBC mode |
|
| 122 | - * @return boolean Returns true |
|
| 123 | - */ |
|
| 124 | - public function decrypt(&$text) |
|
| 125 | - { |
|
| 126 | - $blocksz = $this->cipher->blockSize(); |
|
| 127 | - |
|
| 128 | - $max = strlen($text) / $blocksz; |
|
| 129 | - for($i = 0; $i < $max; ++$i) |
|
| 130 | - { |
|
| 131 | - // get the current position in $text |
|
| 132 | - $pos = $i * $blocksz; |
|
| 133 | - |
|
| 134 | - // grab a block of cipher text, and save it for use later |
|
| 135 | - $block = substr($text, $pos, $blocksz); |
|
| 136 | - $tmp_block = $block; |
|
| 137 | - |
|
| 138 | - // decrypt the block of cipher text |
|
| 139 | - $this->cipher->decrypt($block); |
|
| 140 | - |
|
| 141 | - // xor the block with the register |
|
| 142 | - for($j = 0; $j < $blocksz; ++$j) |
|
| 143 | - $block[$j] = $block[$j] ^ $this->register[$j]; |
|
| 144 | - |
|
| 145 | - // replace the block of cipher text with plain text |
|
| 146 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 147 | - |
|
| 148 | - // save the cipher text block to the register |
|
| 149 | - $this->register = $tmp_block; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $this->strip($text); |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * This mode requires an IV |
|
| 159 | - * |
|
| 160 | - * @return boolean Returns True |
|
| 161 | - */ |
|
| 162 | - public function requiresIV() |
|
| 163 | - { |
|
| 164 | - return true; |
|
| 165 | - } |
|
| 39 | + /** |
|
| 40 | + * Constructor |
|
| 41 | + * Sets the cipher object that will be used for encryption |
|
| 42 | + * |
|
| 43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 44 | + * @return void |
|
| 45 | + */ |
|
| 46 | + function __construct($cipher) |
|
| 47 | + { |
|
| 48 | + parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
|
| 49 | + |
|
| 50 | + // this works with only block Ciphers |
|
| 51 | + if($cipher->type() != Cipher::BLOCK) |
|
| 52 | + trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Destructor |
|
| 58 | + * |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public function __destruct() |
|
| 62 | + { |
|
| 63 | + parent::__destruct(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
| 69 | + * to the constructor in CBC mode |
|
| 70 | + * The steps to encrypt using CBC are as follows |
|
| 71 | + * 1) Get a block of plain text data to use in the Cipher |
|
| 72 | + * 2) XOR the block with IV (if it's the first round) or the previous |
|
| 73 | + * round's encrypted result |
|
| 74 | + * 3) Encrypt the block using the cipher |
|
| 75 | + * 4) Save the encrypted block to use in the next round |
|
| 76 | + * |
|
| 77 | + * @param string $text the string to be encrypted in CBC mode |
|
| 78 | + * @return boolean Returns true |
|
| 79 | + */ |
|
| 80 | + public function encrypt(&$text) |
|
| 81 | + { |
|
| 82 | + $this->pad($text); |
|
| 83 | + $blocksz = $this->cipher->blockSize(); |
|
| 84 | + |
|
| 85 | + $max = strlen($text) / $blocksz; |
|
| 86 | + for($i = 0; $i < $max; ++$i) |
|
| 87 | + { |
|
| 88 | + // get the current position in $text |
|
| 89 | + $pos = $i * $blocksz; |
|
| 90 | + |
|
| 91 | + // grab a block of plain text |
|
| 92 | + $block = substr($text, $pos, $blocksz); |
|
| 93 | + |
|
| 94 | + // xor the block with the register |
|
| 95 | + for($j = 0; $j < $blocksz; ++$j) |
|
| 96 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
| 97 | + |
|
| 98 | + // encrypt the block, and save it back to the register |
|
| 99 | + $this->cipher->encrypt($block); |
|
| 100 | + $this->register = $block; |
|
| 101 | + |
|
| 102 | + // replace the plain text block with the cipher text |
|
| 103 | + $text = substr_replace($text, $this->register, $pos, $blocksz); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return true; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
| 112 | + * to the constructor in CBC mode |
|
| 113 | + * The decryption algorithm requires the following steps |
|
| 114 | + * 1) Get the first block of encrypted text, save it |
|
| 115 | + * 2) Decrypt the block |
|
| 116 | + * 3) XOR the decrypted block (if it's the first pass use the IV, |
|
| 117 | + * else use the encrypted block from step 1 |
|
| 118 | + * 4) the result from the XOR will be a plain text block, save it |
|
| 119 | + * 5) assign the encrypted block from step 1 to use in the next round |
|
| 120 | + * |
|
| 121 | + * @param string $text the string to be decrypted in CBC mode |
|
| 122 | + * @return boolean Returns true |
|
| 123 | + */ |
|
| 124 | + public function decrypt(&$text) |
|
| 125 | + { |
|
| 126 | + $blocksz = $this->cipher->blockSize(); |
|
| 127 | + |
|
| 128 | + $max = strlen($text) / $blocksz; |
|
| 129 | + for($i = 0; $i < $max; ++$i) |
|
| 130 | + { |
|
| 131 | + // get the current position in $text |
|
| 132 | + $pos = $i * $blocksz; |
|
| 133 | + |
|
| 134 | + // grab a block of cipher text, and save it for use later |
|
| 135 | + $block = substr($text, $pos, $blocksz); |
|
| 136 | + $tmp_block = $block; |
|
| 137 | + |
|
| 138 | + // decrypt the block of cipher text |
|
| 139 | + $this->cipher->decrypt($block); |
|
| 140 | + |
|
| 141 | + // xor the block with the register |
|
| 142 | + for($j = 0; $j < $blocksz; ++$j) |
|
| 143 | + $block[$j] = $block[$j] ^ $this->register[$j]; |
|
| 144 | + |
|
| 145 | + // replace the block of cipher text with plain text |
|
| 146 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
| 147 | + |
|
| 148 | + // save the cipher text block to the register |
|
| 149 | + $this->register = $tmp_block; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $this->strip($text); |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * This mode requires an IV |
|
| 159 | + * |
|
| 160 | + * @return boolean Returns True |
|
| 161 | + */ |
|
| 162 | + public function requiresIV() |
|
| 163 | + { |
|
| 164 | + return true; |
|
| 165 | + } |
|
| 166 | 166 | } |
| 167 | 167 | ?> |
@@ -38,32 +38,32 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Mode_Stream extends Mode_Raw |
| 40 | 40 | { |
| 41 | - /** |
|
| 42 | - * Constructor |
|
| 43 | - * Sets the cipher object that will be used for encryption |
|
| 44 | - * |
|
| 45 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function __construct($cipher) |
|
| 49 | - { |
|
| 50 | - // call the secondary 'constructor' from the parent |
|
| 51 | - parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
|
| 41 | + /** |
|
| 42 | + * Constructor |
|
| 43 | + * Sets the cipher object that will be used for encryption |
|
| 44 | + * |
|
| 45 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function __construct($cipher) |
|
| 49 | + { |
|
| 50 | + // call the secondary 'constructor' from the parent |
|
| 51 | + parent::__construct1(PHP_Crypt::MODE_STREAM, $cipher); |
|
| 52 | 52 | |
| 53 | - // this works with only stream Ciphers |
|
| 54 | - if($cipher->type() != Cipher::STREAM) |
|
| 55 | - trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
| 56 | - } |
|
| 53 | + // this works with only stream Ciphers |
|
| 54 | + if($cipher->type() != Cipher::STREAM) |
|
| 55 | + trigger_error("Stream mode requires a stream cipher", E_USER_WARNING); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Destructor |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function __destruct() |
|
| 65 | - { |
|
| 66 | - parent::__destruct(); |
|
| 67 | - } |
|
| 59 | + /** |
|
| 60 | + * Destructor |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function __destruct() |
|
| 65 | + { |
|
| 66 | + parent::__destruct(); |
|
| 67 | + } |
|
| 68 | 68 | } |
| 69 | 69 | ?> |