| 1 |
<?php |
| 2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
| 3 |
|
| 4 |
/** |
| 5 |
* Pure-PHP implementation of Rijndael. |
| 6 |
* |
| 7 |
* Does not use mcrypt, even when available, for reasons that are explained below. |
| 8 |
* |
| 9 |
* PHP versions 4 and 5 |
| 10 |
* |
| 11 |
* If {@link Crypt_Rijndael::setBlockLength() setBlockLength()} isn't called, it'll be assumed to be 128 bits. If |
| 12 |
* {@link Crypt_Rijndael::setKeyLength() setKeyLength()} isn't called, it'll be calculated from |
| 13 |
* {@link Crypt_Rijndael::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's |
| 14 |
* 136-bits it'll be null-padded to 160-bits and 160 bits will be the key length until |
| 15 |
* {@link Crypt_Rijndael::setKey() setKey()} is called, again, at which point, it'll be recalculated. |
| 16 |
* |
| 17 |
* Not all Rijndael implementations may support 160-bits or 224-bits as the block length / key length. mcrypt, for example, |
| 18 |
* does not. AES, itself, only supports block lengths of 128 and key lengths of 128, 192, and 256. |
| 19 |
* {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=10 Rijndael-ammended.pdf#page=10} defines the |
| 20 |
* algorithm for block lengths of 192 and 256 but not for block lengths / key lengths of 160 and 224. Indeed, 160 and 224 |
| 21 |
* are first defined as valid key / block lengths in |
| 22 |
* {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=44 Rijndael-ammended.pdf#page=44}: |
| 23 |
* Extensions: Other block and Cipher Key lengths. |
| 24 |
* |
| 25 |
* {@internal The variable names are the same as those in |
| 26 |
* {@link http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf#page=10 fips-197.pdf#page=10}.}} |
| 27 |
* |
| 28 |
* Here's a short example of how to use this library: |
| 29 |
* <code> |
| 30 |
* <?php |
| 31 |
* include('Crypt/Rijndael.php'); |
| 32 |
* |
| 33 |
* $rijndael = new Crypt_Rijndael(); |
| 34 |
* |
| 35 |
* $rijndael->setKey('abcdefghijklmnop'); |
| 36 |
* |
| 37 |
* $size = 10 * 1024; |
| 38 |
* $plaintext = ''; |
| 39 |
* for ($i = 0; $i < $size; $i++) { |
| 40 |
* $plaintext.= 'a'; |
| 41 |
* } |
| 42 |
* |
| 43 |
* echo $rijndael->decrypt($rijndael->encrypt($plaintext)); |
| 44 |
* ?> |
| 45 |
* </code> |
| 46 |
* |
| 47 |
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy |
| 48 |
* of this software and associated documentation files (the "Software"), to deal |
| 49 |
* in the Software without restriction, including without limitation the rights |
| 50 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 51 |
* copies of the Software, and to permit persons to whom the Software is |
| 52 |
* furnished to do so, subject to the following conditions: |
| 53 |
* |
| 54 |
* The above copyright notice and this permission notice shall be included in |
| 55 |
* all copies or substantial portions of the Software. |
| 56 |
* |
| 57 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 58 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 59 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 60 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 61 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 62 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 63 |
* THE SOFTWARE. |
| 64 |
* |
| 65 |
* @category Crypt |
| 66 |
* @package Crypt_Rijndael |
| 67 |
* @author Jim Wigginton <terrafrost@php.net> |
| 68 |
* @copyright MMVIII Jim Wigginton |
| 69 |
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 70 |
* @link http://phpseclib.sourceforge.net |
| 71 |
*/ |
| 72 |
|
| 73 |
/**#@+ |
| 74 |
* @access public |
| 75 |
* @see Crypt_Rijndael::encrypt() |
| 76 |
* @see Crypt_Rijndael::decrypt() |
| 77 |
*/ |
| 78 |
/** |
| 79 |
* Encrypt / decrypt using the Counter mode. |
| 80 |
* |
| 81 |
* Set to -1 since that's what Crypt/Random.php uses to index the CTR mode. |
| 82 |
* |
| 83 |
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29 |
| 84 |
*/ |
| 85 |
define('CRYPT_RIJNDAEL_MODE_CTR', -1); |
| 86 |
/** |
| 87 |
* Encrypt / decrypt using the Electronic Code Book mode. |
| 88 |
* |
| 89 |
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29 |
| 90 |
*/ |
| 91 |
define('CRYPT_RIJNDAEL_MODE_ECB', 1); |
| 92 |
/** |
| 93 |
* Encrypt / decrypt using the Code Book Chaining mode. |
| 94 |
* |
| 95 |
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29 |
| 96 |
*/ |
| 97 |
define('CRYPT_RIJNDAEL_MODE_CBC', 2); |
| 98 |
/** |
| 99 |
* Encrypt / decrypt using the Cipher Feedback mode. |
| 100 |
* |
| 101 |
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29 |
| 102 |
*/ |
| 103 |
define('CRYPT_RIJNDAEL_MODE_CFB', 3); |
| 104 |
/** |
| 105 |
* Encrypt / decrypt using the Cipher Feedback mode. |
| 106 |
* |
| 107 |
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 |
| 108 |
*/ |
| 109 |
define('CRYPT_RIJNDAEL_MODE_OFB', 4); |
| 110 |
/**#@-*/ |
| 111 |
|
| 112 |
/**#@+ |
| 113 |
* @access private |
| 114 |
* @see Crypt_Rijndael::Crypt_Rijndael() |
| 115 |
*/ |
| 116 |
/** |
| 117 |
* Toggles the internal implementation |
| 118 |
*/ |
| 119 |
define('CRYPT_RIJNDAEL_MODE_INTERNAL', 1); |
| 120 |
/** |
| 121 |
* Toggles the mcrypt implementation |
| 122 |
*/ |
| 123 |
define('CRYPT_RIJNDAEL_MODE_MCRYPT', 2); |
| 124 |
/**#@-*/ |
| 125 |
|
| 126 |
/** |
| 127 |
* Pure-PHP implementation of Rijndael. |
| 128 |
* |
| 129 |
* @author Jim Wigginton <terrafrost@php.net> |
| 130 |
* @version 0.1.0 |
| 131 |
* @access public |
| 132 |
* @package Crypt_Rijndael |
| 133 |
*/ |
| 134 |
class Crypt_Rijndael { |
| 135 |
/** |
| 136 |
* The Encryption Mode |
| 137 |
* |
| 138 |
* @see Crypt_Rijndael::Crypt_Rijndael() |
| 139 |
* @var Integer |
| 140 |
* @access private |
| 141 |
*/ |
| 142 |
var $mode; |
| 143 |
|
| 144 |
/** |
| 145 |
* The Key |
| 146 |
* |
| 147 |
* @see Crypt_Rijndael::setKey() |
| 148 |
* @var String |
| 149 |
* @access private |
| 150 |
*/ |
| 151 |
var $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
| 152 |
|
| 153 |
/** |
| 154 |
* The Initialization Vector |
| 155 |
* |
| 156 |
* @see Crypt_Rijndael::setIV() |
| 157 |
* @var String |
| 158 |
* @access private |
| 159 |
*/ |
| 160 |
var $iv = ''; |
| 161 |
|
| 162 |
/** |
| 163 |
* A "sliding" Initialization Vector |
| 164 |
* |
| 165 |
* @see Crypt_Rijndael::enableContinuousBuffer() |
| 166 |
* @var String |
| 167 |
* @access private |
| 168 |
*/ |
| 169 |
var $encryptIV = ''; |
| 170 |
|
| 171 |
/** |
| 172 |
* A "sliding" Initialization Vector |
| 173 |
* |
| 174 |
* @see Crypt_Rijndael::enableContinuousBuffer() |
| 175 |
* @var String |
| 176 |
* @access private |
| 177 |
*/ |
| 178 |
var $decryptIV = ''; |
| 179 |
|
| 180 |
/** |
| 181 |
* Continuous Buffer status |
| 182 |
* |
| 183 |
* @see Crypt_Rijndael::enableContinuousBuffer() |
| 184 |
* @var Boolean |
| 185 |
* @access private |
| 186 |
*/ |
| 187 |
var $continuousBuffer = false; |
| 188 |
|
| 189 |
/** |
| 190 |
* Padding status |
| 191 |
* |
| 192 |
* @see Crypt_Rijndael::enablePadding() |
| 193 |
* @var Boolean |
| 194 |
* @access private |
| 195 |
*/ |
| 196 |
var $padding = true; |
| 197 |
|
| 198 |
/** |
| 199 |
* Does the key schedule need to be (re)calculated? |
| 200 |
* |
| 201 |
* @see setKey() |
| 202 |
* @see setBlockLength() |
| 203 |
* @see setKeyLength() |
| 204 |
* @var Boolean |
| 205 |
* @access private |
| 206 |
*/ |
| 207 |
var $changed = true; |
| 208 |
|
| 209 |
/** |
| 210 |
* Has the key length explicitly been set or should it be derived from the key, itself? |
| 211 |
* |
| 212 |
* @see setKeyLength() |
| 213 |
* @var Boolean |
| 214 |
* @access private |
| 215 |
*/ |
| 216 |
var $explicit_key_length = false; |
| 217 |
|
| 218 |
/** |
| 219 |
* The Key Schedule |
| 220 |
* |
| 221 |
* @see _setup() |
| 222 |
* @var Array |
| 223 |
* @access private |
| 224 |
*/ |
| 225 |
var $w; |
| 226 |
|
| 227 |
/** |
| 228 |
* The Inverse Key Schedule |
| 229 |
* |
| 230 |
* @see _setup() |
| 231 |
* @var Array |
| 232 |
* @access private |
| 233 |
*/ |
| 234 |
var $dw; |
| 235 |
|
| 236 |
/** |
| 237 |
* The Block Length |
| 238 |
* |
| 239 |
* @see setBlockLength() |
| 240 |
* @var Integer |
| 241 |
* @access private |
| 242 |
* @internal The max value is 32, the min value is 16. All valid values are multiples of 4. Exists in conjunction with |
| 243 |
* $Nb because we need this value and not $Nb to pad strings appropriately. |
| 244 |
*/ |
| 245 |
var $block_size = 16; |
| 246 |
|
| 247 |
/** |
| 248 |
* The Block Length divided by 32 |
| 249 |
* |
| 250 |
* @see setBlockLength() |
| 251 |
* @var Integer |
| 252 |
* @access private |
| 253 |
* @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4. Exists in conjunction with $block_size |
| 254 |
* because the encryption / decryption / key schedule creation requires this number and not $block_size. We could |
| 255 |
* derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
| 256 |
* of that, we'll just precompute it once. |
| 257 |
* |
| 258 |
*/ |
| 259 |
var $Nb = 4; |
| 260 |
|
| 261 |
/** |
| 262 |
* The Key Length |
| 263 |
* |
| 264 |
* @see setKeyLength() |
| 265 |
* @var Integer |
| 266 |
* @access private |
| 267 |
* @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $key_size |
| 268 |
* because the encryption / decryption / key schedule creation requires this number and not $key_size. We could |
| 269 |
* derive this from $key_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu |
| 270 |
* of that, we'll just precompute it once. |
| 271 |
*/ |
| 272 |
var $key_size = 16; |
| 273 |
|
| 274 |
/** |
| 275 |
* The Key Length divided by 32 |
| 276 |
* |
| 277 |
* @see setKeyLength() |
| 278 |
* @var Integer |
| 279 |
* @access private |
| 280 |
* @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4 |
| 281 |
*/ |
| 282 |
var $Nk = 4; |
| 283 |
|
| 284 |
/** |
| 285 |
* The Number of Rounds |
| 286 |
* |
| 287 |
* @var Integer |
| 288 |
* @access private |
| 289 |
* @internal The max value is 14, the min value is 10. |
| 290 |
*/ |
| 291 |
var $Nr; |
| 292 |
|
| 293 |
/** |
| 294 |
* Shift offsets |
| 295 |
* |
| 296 |
* @var Array |
| 297 |
* @access private |
| 298 |
*/ |
| 299 |
var $c; |
| 300 |
|
| 301 |
/** |
| 302 |
* Precomputed mixColumns table |
| 303 |
* |
| 304 |
* @see Crypt_Rijndael() |
| 305 |
* @var Array |
| 306 |
* @access private |
| 307 |
*/ |
| 308 |
var $t0; |
| 309 |
|
| 310 |
/** |
| 311 |
* Precomputed mixColumns table |
| 312 |
* |
| 313 |
* @see Crypt_Rijndael() |
| 314 |
* @var Array |
| 315 |
* @access private |
| 316 |
*/ |
| 317 |
var $t1; |
| 318 |
|
| 319 |
/** |
| 320 |
* Precomputed mixColumns table |
| 321 |
* |
| 322 |
* @see Crypt_Rijndael() |
| 323 |
* @var Array |
| 324 |
* @access private |
| 325 |
*/ |
| 326 |
var $t2; |
| 327 |
|
| 328 |
/** |
| 329 |
* Precomputed mixColumns table |
| 330 |
* |
| 331 |
* @see Crypt_Rijndael() |
| 332 |
* @var Array |
| 333 |
* @access private |
| 334 |
*/ |
| 335 |
var $t3; |
| 336 |
|
| 337 |
/** |
| 338 |
* Precomputed invMixColumns table |
| 339 |
* |
| 340 |
* @see Crypt_Rijndael() |
| 341 |
* @var Array |
| 342 |
* @access private |
| 343 |
*/ |
| 344 |
var $dt0; |
| 345 |
|
| 346 |
/** |
| 347 |
* Precomputed invMixColumns table |
| 348 |
* |
| 349 |
* @see Crypt_Rijndael() |
| 350 |
* @var Array |
| 351 |
* @access private |
| 352 |
*/ |
| 353 |
var $dt1; |
| 354 |
|
| 355 |
/** |
| 356 |
* Precomputed invMixColumns table |
| 357 |
* |
| 358 |
* @see Crypt_Rijndael() |
| 359 |
* @var Array |
| 360 |
* @access private |
| 361 |
*/ |
| 362 |
var $dt2; |
| 363 |
|
| 364 |
/** |
| 365 |
* Precomputed invMixColumns table |
| 366 |
* |
| 367 |
* @see Crypt_Rijndael() |
| 368 |
* @var Array |
| 369 |
* @access private |
| 370 |
*/ |
| 371 |
var $dt3; |
| 372 |
|
| 373 |
/** |
| 374 |
* The SubByte S-Box |
| 375 |
* |
| 376 |
* @see Crypt_Rijndael::_encryptBlock() |
| 377 |
* @var Array |
| 378 |
* @access private |
| 379 |
*/ |
| 380 |
var $sbox; |
| 381 |
|
| 382 |
/** |
| 383 |
* The inverse SubByte S-Box |
| 384 |
* |
| 385 |
* @see Crypt_Rijndael::_decryptBlock() |
| 386 |
* @var Array |
| 387 |
* @access private |
| 388 |
*/ |
| 389 |
var $isbox; |
| 390 |
|
| 391 |
/** |
| 392 |
* Performance-optimized callback function for en/decrypt() |
| 393 |
* |
| 394 |
* @see Crypt_Rijndael::encrypt() |
| 395 |
* @see Crypt_Rijndael::decrypt() |
| 396 |
* @see Crypt_Rijndael::inline_crypt_setup() |
| 397 |
* @see Crypt_Rijndael::$use_inline_crypt |
| 398 |
* @var Callback |
| 399 |
* @access private |
| 400 |
*/ |
| 401 |
var $inline_crypt; |
| 402 |
|
| 403 |
/** |
| 404 |
* Holds whether performance-optimized $inline_crypt should be used or not. |
| 405 |
* |
| 406 |
* @see Crypt_Rijndael::Crypt_Rijndael() |
| 407 |
* @see Crypt_Rijndael::inline_crypt_setup() |
| 408 |
* @see Crypt_Rijndael::$inline_crypt |
| 409 |
* @var Boolean |
| 410 |
* @access private |
| 411 |
*/ |
| 412 |
var $use_inline_crypt = true; |
| 413 |
|
| 414 |
/** |
| 415 |
* Is the mode one that is paddable? |
| 416 |
* |
| 417 |
* @see Crypt_Rijndael::Crypt_Rijndael() |
| 418 |
* @var Boolean |
| 419 |
* @access private |
| 420 |
*/ |
| 421 |
var $paddable = false; |
| 422 |
|
| 423 |
/** |
| 424 |
* Encryption buffer for CTR, OFB and CFB modes |
| 425 |
* |
| 426 |
* @see Crypt_Rijndael::encrypt() |
| 427 |
* @var String |
| 428 |
* @access private |
| 429 |
*/ |
| 430 |
var $enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0); |
| 431 |
|
| 432 |
/** |
| 433 |
* Decryption buffer for CTR, OFB and CFB modes |
| 434 |
* |
| 435 |
* @see Crypt_Rijndael::decrypt() |
| 436 |
* @var String |
| 437 |
* @access private |
| 438 |
*/ |
| 439 |
var $debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0); |
| 440 |
|
| 441 |
/** |
| 442 |
* Default Constructor. |
| 443 |
* |
| 444 |
* Determines whether or not the mcrypt extension should be used. $mode should only, at present, be |
| 445 |
* CRYPT_RIJNDAEL_MODE_ECB or CRYPT_RIJNDAEL_MODE_CBC. If not explictly set, CRYPT_RIJNDAEL_MODE_CBC will be used. |
| 446 |
* |
| 447 |
* @param optional Integer $mode |
| 448 |
* @return Crypt_Rijndael |
| 449 |
* @access public |
| 450 |
*/ |
| 451 |
function Crypt_Rijndael($mode = CRYPT_RIJNDAEL_MODE_CBC) |
| 452 |
{ |
| 453 |
switch ($mode) { |
| 454 |
case CRYPT_RIJNDAEL_MODE_ECB: |
| 455 |
case CRYPT_RIJNDAEL_MODE_CBC: |
| 456 |
$this->paddable = true; |
| 457 |
$this->mode = $mode; |
| 458 |
break; |
| 459 |
case CRYPT_RIJNDAEL_MODE_CTR: |
| 460 |
case CRYPT_RIJNDAEL_MODE_CFB: |
| 461 |
case CRYPT_RIJNDAEL_MODE_OFB: |
| 462 |
$this->mode = $mode; |
| 463 |
break; |
| 464 |
default: |
| 465 |
$this->paddable = true; |
| 466 |
$this->mode = CRYPT_RIJNDAEL_MODE_CBC; |
| 467 |
} |
| 468 |
|
| 469 |
$t3 = &$this->t3; |
| 470 |
$t2 = &$this->t2; |
| 471 |
$t1 = &$this->t1; |
| 472 |
$t0 = &$this->t0; |
| 473 |
|
| 474 |
$dt3 = &$this->dt3; |
| 475 |
$dt2 = &$this->dt2; |
| 476 |
$dt1 = &$this->dt1; |
| 477 |
$dt0 = &$this->dt0; |
| 478 |
|
| 479 |
// according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=19> (section 5.2.1), |
| 480 |
// precomputed tables can be used in the mixColumns phase. in that example, they're assigned t0...t3, so |
| 481 |
// those are the names we'll use. |
| 482 |
$t3 = array( |
| 483 |
0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, 0x6F6FB1DE, 0xC5C55491, |
| 484 |
0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC, |
| 485 |
0xCACA458F, 0x82829D1F, 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB, |
| 486 |
0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, 0x727296E4, 0xC0C05B9B, |
| 487 |
0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83, |
| 488 |
0x34345C68, 0xA5A5F451, 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A, |
| 489 |
0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, 0x05050F0A, 0x9A9AB52F, |
| 490 |
0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA, |
| 491 |
0x09091B12, 0x83839E1D, 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B, |
| 492 |
0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, 0x2F2F715E, 0x84849713, |
| 493 |
0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6, |
| 494 |
0x6A6ABED4, 0xCBCB468D, 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85, |
| 495 |
0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, 0x33335566, 0x85859411, |
| 496 |
0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B, |
| 497 |
0x5151F3A2, 0xA3A3FE5D, 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1, |
| 498 |
0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, 0xF3F30EFD, 0xD2D26DBF, |
| 499 |
0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E, |
| 500 |
0xC4C45793, 0xA7A7F255, 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6, |
| 501 |
0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, 0x9090AB3B, 0x8888830B, |
| 502 |
0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD, |
| 503 |
0xE0E03BDB, 0x32325664, 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8, |
| 504 |
0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, 0xE4E437D3, 0x79798BF2, |
| 505 |
0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049, |
| 506 |
0x6C6CB4D8, 0x5656FAAC, 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810, |
| 507 |
0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, 0xB4B4C773, 0xC6C65197, |
| 508 |
0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F, |
| 509 |
0x707090E0, 0x3E3E427C, 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C, |
| 510 |
0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, 0x1D1D273A, 0x9E9EB927, |
| 511 |
0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733, |
| 512 |
0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5, |
| 513 |
0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0, |
| 514 |
0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C |
| 515 |
); |
| 516 |
|
| 517 |
$dt3 = array( |
| 518 |
0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, 0xFA58ABAC, 0xE303934B, |
| 519 |
0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5, |
| 520 |
0xB15A49DE, 0xBA1B6725, 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B, |
| 521 |
0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, 0xE0692949, 0xC9C8448E, |
| 522 |
0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D, |
| 523 |
0xDF4A1863, 0x1A3182E5, 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9, |
| 524 |
0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, 0x1F8F57E3, 0x55AB2A66, |
| 525 |
0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED, |
| 526 |
0xCF1C2B8A, 0x79B492A7, 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4, |
| 527 |
0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, 0x719F065E, 0x6E1051BD, |
| 528 |
0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60, |
| 529 |
0x98FB2419, 0xBDE997D6, 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79, |
| 530 |
0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, 0x1170AC1E, 0x5A724E6C, |
| 531 |
0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24, |
| 532 |
0x0A67B10C, 0x57E70F93, 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C, |
| 533 |
0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, 0xB6A8B92D, 0x1EA9C814, |
| 534 |
0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B, |
| 535 |
0x4329768B, 0x23C6DCCB, 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084, |
| 536 |
0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, 0x8652EC0D, 0xC1E3D077, |
| 537 |
0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22, |
| 538 |
0x494EC787, 0x38D1C1D9, 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F, |
| 539 |
0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, 0x39F75E2E, 0xC3AFF582, |
| 540 |
0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB, |
| 541 |
0x267809CD, 0x5918F46E, 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF, |
| 542 |
0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, 0xA59430C6, 0xA266C035, |
| 543 |
0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17, |
| 544 |
0x4DD68D76, 0xEFB04D43, 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46, |
| 545 |
0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, 0x105633E9, 0xD647136D, |
| 546 |
0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A, |
| 547 |
0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678, |
| 548 |
0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF, |
| 549 |
0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0 |
| 550 |
); |
| 551 |
|
| 552 |
for ($i = 0; $i < 256; $i++) { |
| 553 |
$t2[] = (($t3[$i] << 8) & 0xFFFFFF00) | (($t3[$i] >> 24) & 0x000000FF); |
| 554 |
$t1[] = (($t3[$i] << 16) & 0xFFFF0000) | (($t3[$i] >> 16) & 0x0000FFFF); |
| 555 |
$t0[] = (($t3[$i] << 24) & 0xFF000000) | (($t3[$i] >> 8) & 0x00FFFFFF); |
| 556 |
|
| 557 |
$dt2[] = (($dt3[$i] << 8) & 0xFFFFFF00) | (($dt3[$i] >> 24) & 0x000000FF); |
| 558 |
$dt1[] = (($dt3[$i] << 16) & 0xFFFF0000) | (($dt3[$i] >> 16) & 0x0000FFFF); |
| 559 |
$dt0[] = (($dt3[$i] << 24) & 0xFF000000) | (($dt3[$i] >> 8) & 0x00FFFFFF); |
| 560 |
} |
| 561 |
|
| 562 |
// sbox for the S-Box substitution |
| 563 |
$this->sbox = array( |
| 564 |
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, |
| 565 |
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, |
| 566 |
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, |
| 567 |
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, |
| 568 |
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, |
| 569 |
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, |
| 570 |
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, |
| 571 |
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, |
| 572 |
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, |
| 573 |
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, |
| 574 |
0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, |
| 575 |
0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, |
| 576 |
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, |
| 577 |
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, |
| 578 |
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, |
| 579 |
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 |
| 580 |
); |
| 581 |
|
| 582 |
// sbox for the inverse S-Box substitution |
| 583 |
$this->isbox = array( |
| 584 |
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, |
| 585 |
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, |
| 586 |
0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, |
| 587 |
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, |
| 588 |
0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, |
| 589 |
0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, |
| 590 |
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, |
| 591 |
0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, |
| 592 |
0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, |
| 593 |
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, |
| 594 |
0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, |
| 595 |
0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, |
| 596 |
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, |
| 597 |
0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, |
| 598 |
0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, |
| 599 |
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D |
| 600 |
); |
| 601 |
|
| 602 |
if (!function_exists('create_function') || !is_callable('create_function')) { |
| 603 |
$this->use_inline_crypt = false; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Sets the key. |
| 609 |
* |
| 610 |
* Keys can be of any length. Rijndael, itself, requires the use of a key that's between 128-bits and 256-bits long and |
| 611 |
* whose length is a multiple of 32. If the key is less than 256-bits and the key length isn't set, we round the length |
| 612 |
* up to the closest valid key length, padding $key with null bytes. If the key is more than 256-bits, we trim the |
| 613 |
* excess bits. |
| 614 |
* |
| 615 |
* If the key is not explicitly set, it'll be assumed to be all null bytes. |
| 616 |
* |
| 617 |
* @access public |
| 618 |
* @param String $key |
| 619 |
*/ |
| 620 |
function setKey($key) |
| 621 |
{ |
| 622 |
$this->key = $key; |
| 623 |
$this->changed = true; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Sets the initialization vector. (optional) |
| 628 |
* |
| 629 |
* SetIV is not required when CRYPT_RIJNDAEL_MODE_ECB is being used. If not explictly set, it'll be assumed |
| 630 |
* to be all zero's. |
| 631 |
* |
| 632 |
* @access public |
| 633 |
* @param String $iv |
| 634 |
*/ |
| 635 |
function setIV($iv) |
| 636 |
{ |
| 637 |
$this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, $this->block_size), $this->block_size, chr(0)); |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Sets the key length |
| 642 |
* |
| 643 |
* Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to |
| 644 |
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. |
| 645 |
* |
| 646 |
* @access public |
| 647 |
* @param Integer $length |
| 648 |
*/ |
| 649 |
function setKeyLength($length) |
| 650 |
{ |
| 651 |
$length >>= 5; |
| 652 |
if ($length > 8) { |
| 653 |
$length = 8; |
| 654 |
} else if ($length < 4) { |
| 655 |
$length = 4; |
| 656 |
} |
| 657 |
$this->Nk = $length; |
| 658 |
$this->key_size = $length << 2; |
| 659 |
|
| 660 |
$this->explicit_key_length = true; |
| 661 |
$this->changed = true; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Sets the password. |
| 666 |
* |
| 667 |
* Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: |
| 668 |
* {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}: |
| 669 |
* $hash, $salt, $method |
| 670 |
* Set $dkLen by calling setKeyLength() |
| 671 |
* |
| 672 |
* @param String $password |
| 673 |
* @param optional String $method |
| 674 |
* @access public |
| 675 |
*/ |
| 676 |
function setPassword($password, $method = 'pbkdf2') |
| 677 |
{ |
| 678 |
$key = ''; |
| 679 |
|
| 680 |
switch ($method) { |
| 681 |
default: // 'pbkdf2' |
| 682 |
list(, , $hash, $salt, $count) = func_get_args(); |
| 683 |
if (!isset($hash)) { |
| 684 |
$hash = 'sha1'; |
| 685 |
} |
| 686 |
// WPA and WPA2 use the SSID as the salt |
| 687 |
if (!isset($salt)) { |
| 688 |
$salt = 'phpseclib'; |
| 689 |
} |
| 690 |
// RFC2898#section-4.2 uses 1,000 iterations by default |
| 691 |
// WPA and WPA2 use 4,096. |
| 692 |
if (!isset($count)) { |
| 693 |
$count = 1000; |
| 694 |
} |
| 695 |
|
| 696 |
if (!class_exists('Crypt_Hash')) { |
| 697 |
require_once('Crypt/Hash.php'); |
| 698 |
} |
| 699 |
|
| 700 |
$i = 1; |
| 701 |
while (strlen($key) < $this->key_size) { // $dkLen == $this->key_size |
| 702 |
//$dk.= $this->_pbkdf($password, $salt, $count, $i++); |
| 703 |
$hmac = new Crypt_Hash(); |
| 704 |
$hmac->setHash($hash); |
| 705 |
$hmac->setKey($password); |
| 706 |
$f = $u = $hmac->hash($salt . pack('N', $i++)); |
| 707 |
for ($j = 2; $j <= $count; $j++) { |
| 708 |
$u = $hmac->hash($u); |
| 709 |
$f^= $u; |
| 710 |
} |
| 711 |
$key.= $f; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
$this->setKey(substr($key, 0, $this->key_size)); |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Sets the block length |
| 720 |
* |
| 721 |
* Valid block lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to |
| 722 |
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. |
| 723 |
* |
| 724 |
* @access public |
| 725 |
* @param Integer $length |
| 726 |
*/ |
| 727 |
function setBlockLength($length) |
| 728 |
{ |
| 729 |
$length >>= 5; |
| 730 |
if ($length > 8) { |
| 731 |
$length = 8; |
| 732 |
} else if ($length < 4) { |
| 733 |
$length = 4; |
| 734 |
} |
| 735 |
$this->Nb = $length; |
| 736 |
$this->block_size = $length << 2; |
| 737 |
$this->changed = true; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Generate CTR XOR encryption key |
| 742 |
* |
| 743 |
* Encrypt the output of this and XOR it against the ciphertext / plaintext to get the |
| 744 |
* plaintext / ciphertext in CTR mode. |
| 745 |
* |
| 746 |
* @see Crypt_Rijndael::decrypt() |
| 747 |
* @see Crypt_Rijndael::encrypt() |
| 748 |
* @access public |
| 749 |
* @param Integer $length |
| 750 |
* @param String $iv |
| 751 |
*/ |
| 752 |
function _generate_xor($length, &$iv) |
| 753 |
{ |
| 754 |
$xor = ''; |
| 755 |
$block_size = $this->block_size; |
| 756 |
$num_blocks = floor(($length + ($block_size - 1)) / $block_size); |
| 757 |
for ($i = 0; $i < $num_blocks; $i++) { |
| 758 |
$xor.= $iv; |
| 759 |
for ($j = 4; $j <= $block_size; $j+=4) { |
| 760 |
$temp = substr($iv, -$j, 4); |
| 761 |
switch ($temp) { |
| 762 |
case "\xFF\xFF\xFF\xFF": |
| 763 |
$iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4); |
| 764 |
break; |
| 765 |
case "\x7F\xFF\xFF\xFF": |
| 766 |
$iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4); |
| 767 |
break 2; |
| 768 |
default: |
| 769 |
extract(unpack('Ncount', $temp)); |
| 770 |
$iv = substr_replace($iv, pack('N', $count + 1), -$j, 4); |
| 771 |
break 2; |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
return $xor; |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Encrypts a message. |
| 781 |
* |
| 782 |
* $plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other Rjindael |
| 783 |
* implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's |
| 784 |
* necessary are discussed in the following |
| 785 |
* URL: |
| 786 |
* |
| 787 |
* {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html} |
| 788 |
* |
| 789 |
* An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does. |
| 790 |
* strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that |
| 791 |
* length. |
| 792 |
* |
| 793 |
* @see Crypt_Rijndael::decrypt() |
| 794 |
* @access public |
| 795 |
* @param String $plaintext |
| 796 |
*/ |
| 797 |
function encrypt($plaintext) |
| 798 |
{ |
| 799 |
if ($this->changed) { |
| 800 |
$this->_setup(); |
| 801 |
} |
| 802 |
if ($this->use_inline_crypt) { |
| 803 |
$inline = $this->inline_crypt; |
| 804 |
return $inline('encrypt', $this, $plaintext); |
| 805 |
} |
| 806 |
if ($this->paddable) { |
| 807 |
$plaintext = $this->_pad($plaintext); |
| 808 |
} |
| 809 |
|
| 810 |
$block_size = $this->block_size; |
| 811 |
$buffer = &$this->enbuffer; |
| 812 |
$ciphertext = ''; |
| 813 |
switch ($this->mode) { |
| 814 |
case CRYPT_RIJNDAEL_MODE_ECB: |
| 815 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 816 |
$ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size)); |
| 817 |
} |
| 818 |
break; |
| 819 |
case CRYPT_RIJNDAEL_MODE_CBC: |
| 820 |
$xor = $this->encryptIV; |
| 821 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 822 |
$block = substr($plaintext, $i, $block_size); |
| 823 |
$block = $this->_encryptBlock($block ^ $xor); |
| 824 |
$xor = $block; |
| 825 |
$ciphertext.= $block; |
| 826 |
} |
| 827 |
if ($this->continuousBuffer) { |
| 828 |
$this->encryptIV = $xor; |
| 829 |
} |
| 830 |
break; |
| 831 |
case CRYPT_RIJNDAEL_MODE_CTR: |
| 832 |
$xor = $this->encryptIV; |
| 833 |
if (strlen($buffer['encrypted'])) { |
| 834 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 835 |
$block = substr($plaintext, $i, $block_size); |
| 836 |
if (strlen($block) > strlen($buffer['encrypted'])) { |
| 837 |
$buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); |
| 838 |
} |
| 839 |
$key = $this->_string_shift($buffer['encrypted'], $block_size); |
| 840 |
$ciphertext.= $block ^ $key; |
| 841 |
} |
| 842 |
} else { |
| 843 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 844 |
$block = substr($plaintext, $i, $block_size); |
| 845 |
$key = $this->_encryptBlock($this->_generate_xor($block_size, $xor)); |
| 846 |
$ciphertext.= $block ^ $key; |
| 847 |
} |
| 848 |
} |
| 849 |
if ($this->continuousBuffer) { |
| 850 |
$this->encryptIV = $xor; |
| 851 |
if ($start = strlen($plaintext) % $block_size) { |
| 852 |
$buffer['encrypted'] = substr($key, $start) . $buffer['encrypted']; |
| 853 |
} |
| 854 |
} |
| 855 |
break; |
| 856 |
case CRYPT_RIJNDAEL_MODE_CFB: |
| 857 |
// cfb loosely routines inspired by openssl's: |
| 858 |
// http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1 |
| 859 |
if ($this->continuousBuffer) { |
| 860 |
$iv = &$this->encryptIV; |
| 861 |
$pos = &$buffer['pos']; |
| 862 |
} else { |
| 863 |
$iv = $this->encryptIV; |
| 864 |
$pos = 0; |
| 865 |
} |
| 866 |
$len = strlen($plaintext); |
| 867 |
$i = 0; |
| 868 |
if ($pos) { |
| 869 |
$orig_pos = $pos; |
| 870 |
$max = $block_size - $pos; |
| 871 |
if ($len >= $max) { |
| 872 |
$i = $max; |
| 873 |
$len-= $max; |
| 874 |
$pos = 0; |
| 875 |
} else { |
| 876 |
$i = $len; |
| 877 |
$pos+= $len; |
| 878 |
$len = 0; |
| 879 |
} |
| 880 |
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize |
| 881 |
$ciphertext = substr($iv, $orig_pos) ^ $plaintext; |
| 882 |
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i); |
| 883 |
} |
| 884 |
while ($len >= $block_size) { |
| 885 |
$iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size); |
| 886 |
$ciphertext.= $iv; |
| 887 |
$len-= $block_size; |
| 888 |
$i+= $block_size; |
| 889 |
} |
| 890 |
if ($len) { |
| 891 |
$iv = $this->_encryptBlock($iv); |
| 892 |
$block = $iv ^ substr($plaintext, $i); |
| 893 |
$iv = substr_replace($iv, $block, 0, $len); |
| 894 |
$ciphertext.= $block; |
| 895 |
$pos = $len; |
| 896 |
} |
| 897 |
break; |
| 898 |
case CRYPT_RIJNDAEL_MODE_OFB: |
| 899 |
$xor = $this->encryptIV; |
| 900 |
if (strlen($buffer['xor'])) { |
| 901 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 902 |
$block = substr($plaintext, $i, $block_size); |
| 903 |
if (strlen($block) > strlen($buffer['xor'])) { |
| 904 |
$xor = $this->_encryptBlock($xor); |
| 905 |
$buffer['xor'].= $xor; |
| 906 |
} |
| 907 |
$key = $this->_string_shift($buffer['xor'], $block_size); |
| 908 |
$ciphertext.= $block ^ $key; |
| 909 |
} |
| 910 |
} else { |
| 911 |
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { |
| 912 |
$xor = $this->_encryptBlock($xor); |
| 913 |
$ciphertext.= substr($plaintext, $i, $block_size) ^ $xor; |
| 914 |
} |
| 915 |
$key = $xor; |
| 916 |
} |
| 917 |
if ($this->continuousBuffer) { |
| 918 |
$this->encryptIV = $xor; |
| 919 |
if ($start = strlen($plaintext) % $block_size) { |
| 920 |
$buffer['xor'] = substr($key, $start) . $buffer['xor']; |
| 921 |
} |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
return $ciphertext; |
| 926 |
} |
| 927 |
|
| 928 |
/** |
| 929 |
* Decrypts a message. |
| 930 |
* |
| 931 |
* If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until |
| 932 |
* it is. |
| 933 |
* |
| 934 |
* @see Crypt_Rijndael::encrypt() |
| 935 |
* @access public |
| 936 |
* @param String $ciphertext |
| 937 |
*/ |
| 938 |
function decrypt($ciphertext) |
| 939 |
{ |
| 940 |
if ($this->changed) { |
| 941 |
$this->_setup(); |
| 942 |
} |
| 943 |
if ($this->use_inline_crypt) { |
| 944 |
$inline = $this->inline_crypt; |
| 945 |
return $inline('decrypt', $this, $ciphertext); |
| 946 |
} |
| 947 |
if ($this->paddable) { |
| 948 |
// we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic : |
| 949 |
// "The data is padded with "\0" to make sure the length of the data is n * blocksize." |
| 950 |
$ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0)); |
| 951 |
} |
| 952 |
|
| 953 |
$block_size = $this->block_size; |
| 954 |
$buffer = &$this->debuffer; |
| 955 |
$plaintext = ''; |
| 956 |
switch ($this->mode) { |
| 957 |
case CRYPT_RIJNDAEL_MODE_ECB: |
| 958 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 959 |
$plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size)); |
| 960 |
} |
| 961 |
break; |
| 962 |
case CRYPT_RIJNDAEL_MODE_CBC: |
| 963 |
$xor = $this->decryptIV; |
| 964 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 965 |
$block = substr($ciphertext, $i, $block_size); |
| 966 |
$plaintext.= $this->_decryptBlock($block) ^ $xor; |
| 967 |
$xor = $block; |
| 968 |
} |
| 969 |
if ($this->continuousBuffer) { |
| 970 |
$this->decryptIV = $xor; |
| 971 |
} |
| 972 |
break; |
| 973 |
case CRYPT_RIJNDAEL_MODE_CTR: |
| 974 |
$xor = $this->decryptIV; |
| 975 |
if (strlen($buffer['ciphertext'])) { |
| 976 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 977 |
$block = substr($ciphertext, $i, $block_size); |
| 978 |
if (strlen($block) > strlen($buffer['ciphertext'])) { |
| 979 |
$buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); |
| 980 |
} |
| 981 |
$key = $this->_string_shift($buffer['ciphertext'], $block_size); |
| 982 |
$plaintext.= $block ^ $key; |
| 983 |
} |
| 984 |
} else { |
| 985 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 986 |
$block = substr($ciphertext, $i, $block_size); |
| 987 |
$key = $this->_encryptBlock($this->_generate_xor($block_size, $xor)); |
| 988 |
$plaintext.= $block ^ $key; |
| 989 |
} |
| 990 |
} |
| 991 |
if ($this->continuousBuffer) { |
| 992 |
$this->decryptIV = $xor; |
| 993 |
if ($start = strlen($ciphertext) % $block_size) { |
| 994 |
$buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; |
| 995 |
} |
| 996 |
} |
| 997 |
break; |
| 998 |
case CRYPT_RIJNDAEL_MODE_CFB: |
| 999 |
if ($this->continuousBuffer) { |
| 1000 |
$iv = &$this->decryptIV; |
| 1001 |
$pos = &$buffer['pos']; |
| 1002 |
} else { |
| 1003 |
$iv = $this->decryptIV; |
| 1004 |
$pos = 0; |
| 1005 |
} |
| 1006 |
$len = strlen($ciphertext); |
| 1007 |
$i = 0; |
| 1008 |
if ($pos) { |
| 1009 |
$orig_pos = $pos; |
| 1010 |
$max = $block_size - $pos; |
| 1011 |
if ($len >= $max) { |
| 1012 |
$i = $max; |
| 1013 |
$len-= $max; |
| 1014 |
$pos = 0; |
| 1015 |
} else { |
| 1016 |
$i = $len; |
| 1017 |
$pos+= $len; |
| 1018 |
$len = 0; |
| 1019 |
} |
| 1020 |
// ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize |
| 1021 |
$plaintext = substr($iv, $orig_pos) ^ $ciphertext; |
| 1022 |
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i); |
| 1023 |
} |
| 1024 |
while ($len >= $block_size) { |
| 1025 |
$iv = $this->_encryptBlock($iv); |
| 1026 |
$cb = substr($ciphertext, $i, $block_size); |
| 1027 |
$plaintext.= $iv ^ $cb; |
| 1028 |
$iv = $cb; |
| 1029 |
$len-= $block_size; |
| 1030 |
$i+= $block_size; |
| 1031 |
} |
| 1032 |
if ($len) { |
| 1033 |
$iv = $this->_encryptBlock($iv); |
| 1034 |
$plaintext.= $iv ^ substr($ciphertext, $i); |
| 1035 |
$iv = substr_replace($iv, substr($ciphertext, $i), 0, $len); |
| 1036 |
$pos = $len; |
| 1037 |
} |
| 1038 |
break; |
| 1039 |
case CRYPT_RIJNDAEL_MODE_OFB: |
| 1040 |
$xor = $this->decryptIV; |
| 1041 |
if (strlen($buffer['xor'])) { |
| 1042 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 1043 |
$block = substr($ciphertext, $i, $block_size); |
| 1044 |
if (strlen($block) > strlen($buffer['xor'])) { |
| 1045 |
$xor = $this->_encryptBlock($xor); |
| 1046 |
$buffer['xor'].= $xor; |
| 1047 |
} |
| 1048 |
$key = $this->_string_shift($buffer['xor'], $block_size); |
| 1049 |
$plaintext.= $block ^ $key; |
| 1050 |
} |
| 1051 |
} else { |
| 1052 |
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { |
| 1053 |
$xor = $this->_encryptBlock($xor); |
| 1054 |
$plaintext.= substr($ciphertext, $i, $block_size) ^ $xor; |
| 1055 |
} |
| 1056 |
$key = $xor; |
| 1057 |
} |
| 1058 |
if ($this->continuousBuffer) { |
| 1059 |
$this->decryptIV = $xor; |
| 1060 |
if ($start = strlen($ciphertext) % $block_size) { |
| 1061 |
$buffer['xor'] = substr($key, $start) . $buffer['xor']; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
return $this->paddable ? $this->_unpad($plaintext) : $plaintext; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/** |
| 1070 |
* Encrypts a block |
| 1071 |
* |
| 1072 |
* @access private |
| 1073 |
* @param String $in |
| 1074 |
* @return String |
| 1075 |
*/ |
| 1076 |
function _encryptBlock($in) |
| 1077 |
{ |
| 1078 |
$state = array(); |
| 1079 |
$words = unpack('N*word', $in); |
| 1080 |
|
| 1081 |
$w = $this->w; |
| 1082 |
$t0 = $this->t0; |
| 1083 |
$t1 = $this->t1; |
| 1084 |
$t2 = $this->t2; |
| 1085 |
$t3 = $this->t3; |
| 1086 |
$Nb = $this->Nb; |
| 1087 |
$Nr = $this->Nr; |
| 1088 |
$c = $this->c; |
| 1089 |
|
| 1090 |
// addRoundKey |
| 1091 |
$i = -1; |
| 1092 |
foreach ($words as $word) { |
| 1093 |
$state[] = $word ^ $w[0][++$i]; |
| 1094 |
} |
| 1095 |
|
| 1096 |
// fips-197.pdf#page=19, "Figure 5. Pseudo Code for the Cipher", states that this loop has four components - |
| 1097 |
// subBytes, shiftRows, mixColumns, and addRoundKey. fips-197.pdf#page=30, "Implementation Suggestions Regarding |
| 1098 |
// Various Platforms" suggests that performs enhanced implementations are described in Rijndael-ammended.pdf. |
| 1099 |
// Rijndael-ammended.pdf#page=20, "Implementation aspects / 32-bit processor", discusses such an optimization. |
| 1100 |
// Unfortunately, the description given there is not quite correct. Per aes.spec.v316.pdf#page=19 [1], |
| 1101 |
// equation (7.4.7) is supposed to use addition instead of subtraction, so we'll do that here, as well. |
| 1102 |
|
| 1103 |
// [1] http://fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.v316.pdf |
| 1104 |
$temp = array(); |
| 1105 |
for ($round = 1; $round < $Nr; ++$round) { |
| 1106 |
$i = 0; // $c[0] == 0 |
| 1107 |
$j = $c[1]; |
| 1108 |
$k = $c[2]; |
| 1109 |
$l = $c[3]; |
| 1110 |
|
| 1111 |
while ($i < $Nb) { |
| 1112 |
$temp[$i] = $t0[$state[$i] >> 24 & 0x000000FF] ^ |
| 1113 |
$t1[$state[$j] >> 16 & 0x000000FF] ^ |
| 1114 |
$t2[$state[$k] >> 8 & 0x000000FF] ^ |
| 1115 |
$t3[$state[$l] & 0x000000FF] ^ |
| 1116 |
$w[$round][$i]; |
| 1117 |
++$i; |
| 1118 |
$j = ($j + 1) % $Nb; |
| 1119 |
$k = ($k + 1) % $Nb; |
| 1120 |
$l = ($l + 1) % $Nb; |
| 1121 |
} |
| 1122 |
$state = $temp; |
| 1123 |
} |
| 1124 |
|
| 1125 |
// subWord |
| 1126 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1127 |
$state[$i] = $this->_subWord($state[$i]); |
| 1128 |
} |
| 1129 |
|
| 1130 |
// shiftRows + addRoundKey |
| 1131 |
$i = 0; // $c[0] == 0 |
| 1132 |
$j = $c[1]; |
| 1133 |
$k = $c[2]; |
| 1134 |
$l = $c[3]; |
| 1135 |
while ($i < $Nb) { |
| 1136 |
$temp[$i] = ($state[$i] & 0xFF000000) ^ |
| 1137 |
($state[$j] & 0x00FF0000) ^ |
| 1138 |
($state[$k] & 0x0000FF00) ^ |
| 1139 |
($state[$l] & 0x000000FF) ^ |
| 1140 |
$w[$Nr][$i]; |
| 1141 |
++$i; |
| 1142 |
$j = ($j + 1) % $Nb; |
| 1143 |
$k = ($k + 1) % $Nb; |
| 1144 |
$l = ($l + 1) % $Nb; |
| 1145 |
} |
| 1146 |
|
| 1147 |
// 100% ugly switch/case code... but ~5% faster ("smart code" below commented out) |
| 1148 |
switch ($Nb) { |
| 1149 |
case 8: |
| 1150 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]); |
| 1151 |
case 7: |
| 1152 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]); |
| 1153 |
case 6: |
| 1154 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]); |
| 1155 |
case 5: |
| 1156 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]); |
| 1157 |
default: |
| 1158 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]); |
| 1159 |
} |
| 1160 |
/* |
| 1161 |
$state = $temp; |
| 1162 |
|
| 1163 |
array_unshift($state, 'N*'); |
| 1164 |
|
| 1165 |
return call_user_func_array('pack', $state); |
| 1166 |
*/ |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* Decrypts a block |
| 1171 |
* |
| 1172 |
* @access private |
| 1173 |
* @param String $in |
| 1174 |
* @return String |
| 1175 |
*/ |
| 1176 |
function _decryptBlock($in) |
| 1177 |
{ |
| 1178 |
$state = array(); |
| 1179 |
$words = unpack('N*word', $in); |
| 1180 |
|
| 1181 |
$dw = $this->dw; |
| 1182 |
$dt0 = $this->dt0; |
| 1183 |
$dt1 = $this->dt1; |
| 1184 |
$dt2 = $this->dt2; |
| 1185 |
$dt3 = $this->dt3; |
| 1186 |
$Nb = $this->Nb; |
| 1187 |
$Nr = $this->Nr; |
| 1188 |
$c = $this->c; |
| 1189 |
|
| 1190 |
// addRoundKey |
| 1191 |
$i = -1; |
| 1192 |
foreach ($words as $word) { |
| 1193 |
$state[] = $word ^ $dw[$Nr][++$i]; |
| 1194 |
} |
| 1195 |
|
| 1196 |
$temp = array(); |
| 1197 |
for ($round = $Nr - 1; $round > 0; --$round) { |
| 1198 |
$i = 0; // $c[0] == 0 |
| 1199 |
$j = $Nb - $c[1]; |
| 1200 |
$k = $Nb - $c[2]; |
| 1201 |
$l = $Nb - $c[3]; |
| 1202 |
|
| 1203 |
while ($i < $Nb) { |
| 1204 |
$temp[$i] = $dt0[$state[$i] >> 24 & 0x000000FF] ^ |
| 1205 |
$dt1[$state[$j] >> 16 & 0x000000FF] ^ |
| 1206 |
$dt2[$state[$k] >> 8 & 0x000000FF] ^ |
| 1207 |
$dt3[$state[$l] & 0x000000FF] ^ |
| 1208 |
$dw[$round][$i]; |
| 1209 |
++$i; |
| 1210 |
$j = ($j + 1) % $Nb; |
| 1211 |
$k = ($k + 1) % $Nb; |
| 1212 |
$l = ($l + 1) % $Nb; |
| 1213 |
} |
| 1214 |
$state = $temp; |
| 1215 |
} |
| 1216 |
|
| 1217 |
// invShiftRows + invSubWord + addRoundKey |
| 1218 |
$i = 0; // $c[0] == 0 |
| 1219 |
$j = $Nb - $c[1]; |
| 1220 |
$k = $Nb - $c[2]; |
| 1221 |
$l = $Nb - $c[3]; |
| 1222 |
|
| 1223 |
while ($i < $Nb) { |
| 1224 |
$temp[$i] = $dw[0][$i] ^ |
| 1225 |
$this->_invSubWord(($state[$i] & 0xFF000000) | |
| 1226 |
($state[$j] & 0x00FF0000) | |
| 1227 |
($state[$k] & 0x0000FF00) | |
| 1228 |
($state[$l] & 0x000000FF)); |
| 1229 |
++$i; |
| 1230 |
$j = ($j + 1) % $Nb; |
| 1231 |
$k = ($k + 1) % $Nb; |
| 1232 |
$l = ($l + 1) % $Nb; |
| 1233 |
} |
| 1234 |
|
| 1235 |
switch ($Nb) { |
| 1236 |
case 8: |
| 1237 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]); |
| 1238 |
case 7: |
| 1239 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]); |
| 1240 |
case 6: |
| 1241 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]); |
| 1242 |
case 5: |
| 1243 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]); |
| 1244 |
default: |
| 1245 |
return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]); |
| 1246 |
} |
| 1247 |
/* |
| 1248 |
$state = $temp; |
| 1249 |
|
| 1250 |
array_unshift($state, 'N*'); |
| 1251 |
|
| 1252 |
return call_user_func_array('pack', $state); |
| 1253 |
*/ |
| 1254 |
} |
| 1255 |
|
| 1256 |
/** |
| 1257 |
* Setup Rijndael |
| 1258 |
* |
| 1259 |
* Validates all the variables and calculates $Nr - the number of rounds that need to be performed - and $w - the key |
| 1260 |
* key schedule. |
| 1261 |
* |
| 1262 |
* @access private |
| 1263 |
*/ |
| 1264 |
function _setup() |
| 1265 |
{ |
| 1266 |
// Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. |
| 1267 |
// See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse |
| 1268 |
static $rcon = array(0, |
| 1269 |
0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, |
| 1270 |
0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, |
| 1271 |
0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000, |
| 1272 |
0x2F000000, 0x5E000000, 0xBC000000, 0x63000000, 0xC6000000, |
| 1273 |
0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000, |
| 1274 |
0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000 |
| 1275 |
); |
| 1276 |
|
| 1277 |
if (!$this->explicit_key_length) { |
| 1278 |
// we do >> 2, here, and not >> 5, as we do above, since strlen($this->key) tells us the number of bytes - not bits |
| 1279 |
$length = strlen($this->key) >> 2; |
| 1280 |
if ($length > 8) { |
| 1281 |
$length = 8; |
| 1282 |
} else if ($length < 4) { |
| 1283 |
$length = 4; |
| 1284 |
} |
| 1285 |
$this->Nk = $length; |
| 1286 |
$this->key_size = $length << 2; |
| 1287 |
} |
| 1288 |
|
| 1289 |
$this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0)); |
| 1290 |
$this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, chr(0)); |
| 1291 |
|
| 1292 |
// see Rijndael-ammended.pdf#page=44 |
| 1293 |
$this->Nr = max($this->Nk, $this->Nb) + 6; |
| 1294 |
|
| 1295 |
// shift offsets for Nb = 5, 7 are defined in Rijndael-ammended.pdf#page=44, |
| 1296 |
// "Table 8: Shift offsets in Shiftrow for the alternative block lengths" |
| 1297 |
// shift offsets for Nb = 4, 6, 8 are defined in Rijndael-ammended.pdf#page=14, |
| 1298 |
// "Table 2: Shift offsets for different block lengths" |
| 1299 |
switch ($this->Nb) { |
| 1300 |
case 4: |
| 1301 |
case 5: |
| 1302 |
case 6: |
| 1303 |
$this->c = array(0, 1, 2, 3); |
| 1304 |
break; |
| 1305 |
case 7: |
| 1306 |
$this->c = array(0, 1, 2, 4); |
| 1307 |
break; |
| 1308 |
case 8: |
| 1309 |
$this->c = array(0, 1, 3, 4); |
| 1310 |
} |
| 1311 |
|
| 1312 |
$key = $this->key; |
| 1313 |
|
| 1314 |
$w = array_values(unpack('N*words', $key)); |
| 1315 |
|
| 1316 |
$length = $this->Nb * ($this->Nr + 1); |
| 1317 |
for ($i = $this->Nk; $i < $length; $i++) { |
| 1318 |
$temp = $w[$i - 1]; |
| 1319 |
if ($i % $this->Nk == 0) { |
| 1320 |
// according to <http://php.net/language.types.integer>, "the size of an integer is platform-dependent". |
| 1321 |
// on a 32-bit machine, it's 32-bits, and on a 64-bit machine, it's 64-bits. on a 32-bit machine, |
| 1322 |
// 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and' |
| 1323 |
// with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is. |
| 1324 |
$temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord |
| 1325 |
$temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk]; |
| 1326 |
} else if ($this->Nk > 6 && $i % $this->Nk == 4) { |
| 1327 |
$temp = $this->_subWord($temp); |
| 1328 |
} |
| 1329 |
$w[$i] = $w[$i - $this->Nk] ^ $temp; |
| 1330 |
} |
| 1331 |
|
| 1332 |
// convert the key schedule from a vector of $Nb * ($Nr + 1) length to a matrix with $Nr + 1 rows and $Nb columns |
| 1333 |
// and generate the inverse key schedule. more specifically, |
| 1334 |
// according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=23> (section 5.3.3), |
| 1335 |
// "The key expansion for the Inverse Cipher is defined as follows: |
| 1336 |
// 1. Apply the Key Expansion. |
| 1337 |
// 2. Apply InvMixColumn to all Round Keys except the first and the last one." |
| 1338 |
// also, see fips-197.pdf#page=27, "5.3.5 Equivalent Inverse Cipher" |
| 1339 |
$temp = $this->w = $this->dw = array(); |
| 1340 |
for ($i = $row = $col = 0; $i < $length; $i++, $col++) { |
| 1341 |
if ($col == $this->Nb) { |
| 1342 |
if ($row == 0) { |
| 1343 |
$this->dw[0] = $this->w[0]; |
| 1344 |
} else { |
| 1345 |
// subWord + invMixColumn + invSubWord = invMixColumn |
| 1346 |
$j = 0; |
| 1347 |
while ($j < $this->Nb) { |
| 1348 |
$dw = $this->_subWord($this->w[$row][$j]); |
| 1349 |
$temp[$j] = $this->dt0[$dw >> 24 & 0x000000FF] ^ |
| 1350 |
$this->dt1[$dw >> 16 & 0x000000FF] ^ |
| 1351 |
$this->dt2[$dw >> 8 & 0x000000FF] ^ |
| 1352 |
$this->dt3[$dw & 0x000000FF]; |
| 1353 |
$j++; |
| 1354 |
} |
| 1355 |
$this->dw[$row] = $temp; |
| 1356 |
} |
| 1357 |
|
| 1358 |
$col = 0; |
| 1359 |
$row++; |
| 1360 |
} |
| 1361 |
$this->w[$row][$col] = $w[$i]; |
| 1362 |
} |
| 1363 |
|
| 1364 |
$this->dw[$row] = $this->w[$row]; |
| 1365 |
|
| 1366 |
// In case of $this->use_inline_crypt === true we have to use 1-dim key arrays (both ascending) |
| 1367 |
if ($this->use_inline_crypt) { |
| 1368 |
$this->dw = array_reverse($this->dw); |
| 1369 |
$w = array_pop($this->w); |
| 1370 |
$dw = array_pop($this->dw); |
| 1371 |
foreach ($this->w as $r => $wr) { |
| 1372 |
foreach ($wr as $c => $wc) { |
| 1373 |
$w[] = $wc; |
| 1374 |
$dw[] = $this->dw[$r][$c]; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
$this->w = $w; |
| 1378 |
$this->dw = $dw; |
| 1379 |
|
| 1380 |
$this->inline_crypt_setup(); |
| 1381 |
} |
| 1382 |
|
| 1383 |
$this->changed = false; |
| 1384 |
} |
| 1385 |
|
| 1386 |
/** |
| 1387 |
* Performs S-Box substitutions |
| 1388 |
* |
| 1389 |
* @access private |
| 1390 |
*/ |
| 1391 |
function _subWord($word) |
| 1392 |
{ |
| 1393 |
$sbox = $this->sbox; |
| 1394 |
|
| 1395 |
return $sbox[$word & 0x000000FF] | |
| 1396 |
($sbox[$word >> 8 & 0x000000FF] << 8) | |
| 1397 |
($sbox[$word >> 16 & 0x000000FF] << 16) | |
| 1398 |
($sbox[$word >> 24 & 0x000000FF] << 24); |
| 1399 |
} |
| 1400 |
|
| 1401 |
/** |
| 1402 |
* Performs inverse S-Box substitutions |
| 1403 |
* |
| 1404 |
* @access private |
| 1405 |
*/ |
| 1406 |
function _invSubWord($word) |
| 1407 |
{ |
| 1408 |
$isbox = $this->isbox; |
| 1409 |
|
| 1410 |
return $isbox[$word & 0x000000FF] | |
| 1411 |
($isbox[$word >> 8 & 0x000000FF] << 8) | |
| 1412 |
($isbox[$word >> 16 & 0x000000FF] << 16) | |
| 1413 |
($isbox[$word >> 24 & 0x000000FF] << 24); |
| 1414 |
} |
| 1415 |
|
| 1416 |
/** |
| 1417 |
* Pad "packets". |
| 1418 |
* |
| 1419 |
* Rijndael works by encrypting between sixteen and thirty-two bytes at a time, provided that number is also a multiple |
| 1420 |
* of four. If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to |
| 1421 |
* pad the input so that it is of the proper length. |
| 1422 |
* |
| 1423 |
* Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, |
| 1424 |
* where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping |
| 1425 |
* away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is |
| 1426 |
* transmitted separately) |
| 1427 |
* |
| 1428 |
* @see Crypt_Rijndael::disablePadding() |
| 1429 |
* @access public |
| 1430 |
*/ |
| 1431 |
function enablePadding() |
| 1432 |
{ |
| 1433 |
$this->padding = true; |
| 1434 |
} |
| 1435 |
|
| 1436 |
/** |
| 1437 |
* Do not pad packets. |
| 1438 |
* |
| 1439 |
* @see Crypt_Rijndael::enablePadding() |
| 1440 |
* @access public |
| 1441 |
*/ |
| 1442 |
function disablePadding() |
| 1443 |
{ |
| 1444 |
$this->padding = false; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/** |
| 1448 |
* Pads a string |
| 1449 |
* |
| 1450 |
* Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. |
| 1451 |
* $block_size - (strlen($text) % $block_size) bytes are added, each of which is equal to |
| 1452 |
* chr($block_size - (strlen($text) % $block_size) |
| 1453 |
* |
| 1454 |
* If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless |
| 1455 |
* and padding will, hence forth, be enabled. |
| 1456 |
* |
| 1457 |
* @see Crypt_Rijndael::_unpad() |
| 1458 |
* @access private |
| 1459 |
*/ |
| 1460 |
function _pad($text) |
| 1461 |
{ |
| 1462 |
$length = strlen($text); |
| 1463 |
|
| 1464 |
if (!$this->padding) { |
| 1465 |
if ($length % $this->block_size == 0) { |
| 1466 |
return $text; |
| 1467 |
} else { |
| 1468 |
user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})"); |
| 1469 |
$this->padding = true; |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
$pad = $this->block_size - ($length % $this->block_size); |
| 1474 |
|
| 1475 |
return str_pad($text, $length + $pad, chr($pad)); |
| 1476 |
} |
| 1477 |
|
| 1478 |
/** |
| 1479 |
* Unpads a string. |
| 1480 |
* |
| 1481 |
* If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong |
| 1482 |
* and false will be returned. |
| 1483 |
* |
| 1484 |
* @see Crypt_Rijndael::_pad() |
| 1485 |
* @access private |
| 1486 |
*/ |
| 1487 |
function _unpad($text) |
| 1488 |
{ |
| 1489 |
if (!$this->padding) { |
| 1490 |
return $text; |
| 1491 |
} |
| 1492 |
|
| 1493 |
$length = ord($text[strlen($text) - 1]); |
| 1494 |
|
| 1495 |
if (!$length || $length > $this->block_size) { |
| 1496 |
return false; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return substr($text, 0, -$length); |
| 1500 |
} |
| 1501 |
|
| 1502 |
/** |
| 1503 |
* Treat consecutive "packets" as if they are a continuous buffer. |
| 1504 |
* |
| 1505 |
* Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets |
| 1506 |
* will yield different outputs: |
| 1507 |
* |
| 1508 |
* <code> |
| 1509 |
* echo $rijndael->encrypt(substr($plaintext, 0, 16)); |
| 1510 |
* echo $rijndael->encrypt(substr($plaintext, 16, 16)); |
| 1511 |
* </code> |
| 1512 |
* <code> |
| 1513 |
* echo $rijndael->encrypt($plaintext); |
| 1514 |
* </code> |
| 1515 |
* |
| 1516 |
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates |
| 1517 |
* another, as demonstrated with the following: |
| 1518 |
* |
| 1519 |
* <code> |
| 1520 |
* $rijndael->encrypt(substr($plaintext, 0, 16)); |
| 1521 |
* echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16))); |
| 1522 |
* </code> |
| 1523 |
* <code> |
| 1524 |
* echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16))); |
| 1525 |
* </code> |
| 1526 |
* |
| 1527 |
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different |
| 1528 |
* outputs. The reason is due to the fact that the initialization vector's change after every encryption / |
| 1529 |
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. |
| 1530 |
* |
| 1531 |
* Put another way, when the continuous buffer is enabled, the state of the Crypt_Rijndael() object changes after each |
| 1532 |
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that |
| 1533 |
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), |
| 1534 |
* however, they are also less intuitive and more likely to cause you problems. |
| 1535 |
* |
| 1536 |
* @see Crypt_Rijndael::disableContinuousBuffer() |
| 1537 |
* @access public |
| 1538 |
*/ |
| 1539 |
function enableContinuousBuffer() |
| 1540 |
{ |
| 1541 |
$this->continuousBuffer = true; |
| 1542 |
} |
| 1543 |
|
| 1544 |
/** |
| 1545 |
* Treat consecutive packets as if they are a discontinuous buffer. |
| 1546 |
* |
| 1547 |
* The default behavior. |
| 1548 |
* |
| 1549 |
* @see Crypt_Rijndael::enableContinuousBuffer() |
| 1550 |
* @access public |
| 1551 |
*/ |
| 1552 |
function disableContinuousBuffer() |
| 1553 |
{ |
| 1554 |
$this->continuousBuffer = false; |
| 1555 |
$this->encryptIV = $this->iv; |
| 1556 |
$this->decryptIV = $this->iv; |
| 1557 |
$this->enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0); |
| 1558 |
$this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0); |
| 1559 |
} |
| 1560 |
|
| 1561 |
/** |
| 1562 |
* String Shift |
| 1563 |
* |
| 1564 |
* Inspired by array_shift |
| 1565 |
* |
| 1566 |
* @param String $string |
| 1567 |
* @param optional Integer $index |
| 1568 |
* @return String |
| 1569 |
* @access private |
| 1570 |
*/ |
| 1571 |
function _string_shift(&$string, $index = 1) |
| 1572 |
{ |
| 1573 |
$substr = substr($string, 0, $index); |
| 1574 |
$string = substr($string, $index); |
| 1575 |
return $substr; |
| 1576 |
} |
| 1577 |
|
| 1578 |
/** |
| 1579 |
* Creates performance-optimized function for de/encrypt(), storing it in $this->inline_crypt |
| 1580 |
* |
| 1581 |
* @see Crypt_Rijndael::encrypt() |
| 1582 |
* @see Crypt_Rijndael::decrypt() |
| 1583 |
* @access private |
| 1584 |
*/ |
| 1585 |
function inline_crypt_setup() |
| 1586 |
{ |
| 1587 |
// Note: inline_crypt_setup() will be called only if $this->changed === true |
| 1588 |
// So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt(). |
| 1589 |
// However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible. |
| 1590 |
|
| 1591 |
$lambda_functions =& Crypt_Rijndael::get_lambda_functions(); |
| 1592 |
$block_size = $this->block_size; |
| 1593 |
$mode = $this->mode; |
| 1594 |
|
| 1595 |
// The first 5 generated $lambda_functions will use the key-words hardcoded for better performance. |
| 1596 |
// For memory reason we limit those ultra-optimized function code to 5. |
| 1597 |
// After that, we use pure (extracted) integer vars for the key-words which is faster than accessing them via array. |
| 1598 |
if (count($lambda_functions) < 5) { |
| 1599 |
$w = $this->w; |
| 1600 |
$dw = $this->dw; |
| 1601 |
$init_encryptBlock = ''; |
| 1602 |
$init_decryptBlock = ''; |
| 1603 |
} else { |
| 1604 |
for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) { |
| 1605 |
$w[] = '$w_'.$i; |
| 1606 |
$dw[] = '$dw_'.$i; |
| 1607 |
} |
| 1608 |
$init_encryptBlock = 'extract($self->w, EXTR_PREFIX_ALL, "w");'; |
| 1609 |
$init_decryptBlock = 'extract($self->dw, EXTR_PREFIX_ALL, "dw");'; |
| 1610 |
} |
| 1611 |
|
| 1612 |
$code_hash = md5("$mode, $block_size, " . implode(',', $w)); |
| 1613 |
|
| 1614 |
if (!isset($lambda_functions[$code_hash])) { |
| 1615 |
$Nr = $this->Nr; |
| 1616 |
$Nb = $this->Nb; |
| 1617 |
$c = $this->c; |
| 1618 |
|
| 1619 |
// Generating encrypt code: |
| 1620 |
$init_encryptBlock.= ' |
| 1621 |
$t0 = $self->t0; |
| 1622 |
$t1 = $self->t1; |
| 1623 |
$t2 = $self->t2; |
| 1624 |
$t3 = $self->t3; |
| 1625 |
$sbox = $self->sbox;'; |
| 1626 |
|
| 1627 |
$s = 'e'; |
| 1628 |
$e = 's'; |
| 1629 |
$wc = $Nb - 1; |
| 1630 |
|
| 1631 |
// Preround: addRoundKey |
| 1632 |
$_encryptBlock = '$in = unpack("N*", $in);'."\n"; |
| 1633 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1634 |
$_encryptBlock .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$w[++$wc].";\n"; |
| 1635 |
} |
| 1636 |
|
| 1637 |
// Mainrounds: shiftRows + subWord + mixColumns + addRoundKey |
| 1638 |
for ($round = 1; $round < $Nr; ++$round) { |
| 1639 |
list($s, $e) = array($e, $s); |
| 1640 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1641 |
$_encryptBlock.= |
| 1642 |
'$'.$e.$i.' = |
| 1643 |
$t0[($'.$s.$i .' >> 24) & 0xff] ^ |
| 1644 |
$t1[($'.$s.(($i + $c[1]) % $Nb).' >> 16) & 0xff] ^ |
| 1645 |
$t2[($'.$s.(($i + $c[2]) % $Nb).' >> 8) & 0xff] ^ |
| 1646 |
$t3[ $'.$s.(($i + $c[3]) % $Nb).' & 0xff] ^ |
| 1647 |
'.$w[++$wc].";\n"; |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
// Finalround: subWord + shiftRows + addRoundKey |
| 1652 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1653 |
$_encryptBlock.= |
| 1654 |
'$'.$e.$i.' = |
| 1655 |
$sbox[ $'.$e.$i.' & 0xff] | |
| 1656 |
($sbox[($'.$e.$i.' >> 8) & 0xff] << 8) | |
| 1657 |
($sbox[($'.$e.$i.' >> 16) & 0xff] << 16) | |
| 1658 |
($sbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n"; |
| 1659 |
} |
| 1660 |
$_encryptBlock .= '$in = pack("N*"'."\n"; |
| 1661 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1662 |
$_encryptBlock.= ', |
| 1663 |
($'.$e.$i .' & 0xFF000000) ^ |
| 1664 |
($'.$e.(($i + $c[1]) % $Nb).' & 0x00FF0000) ^ |
| 1665 |
($'.$e.(($i + $c[2]) % $Nb).' & 0x0000FF00) ^ |
| 1666 |
($'.$e.(($i + $c[3]) % $Nb).' & 0x000000FF) ^ |
| 1667 |
'.$w[$i]."\n"; |
| 1668 |
} |
| 1669 |
$_encryptBlock .= ');'; |
| 1670 |
|
| 1671 |
// Generating decrypt code: |
| 1672 |
$init_decryptBlock.= ' |
| 1673 |
$dt0 = $self->dt0; |
| 1674 |
$dt1 = $self->dt1; |
| 1675 |
$dt2 = $self->dt2; |
| 1676 |
$dt3 = $self->dt3; |
| 1677 |
$isbox = $self->isbox;'; |
| 1678 |
|
| 1679 |
$s = 'e'; |
| 1680 |
$e = 's'; |
| 1681 |
$wc = $Nb - 1; |
| 1682 |
|
| 1683 |
// Preround: addRoundKey |
| 1684 |
$_decryptBlock = '$in = unpack("N*", $in);'."\n"; |
| 1685 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1686 |
$_decryptBlock .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$dw[++$wc].';'."\n"; |
| 1687 |
} |
| 1688 |
|
| 1689 |
// Mainrounds: shiftRows + subWord + mixColumns + addRoundKey |
| 1690 |
for ($round = 1; $round < $Nr; ++$round) { |
| 1691 |
list($s, $e) = array($e, $s); |
| 1692 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1693 |
$_decryptBlock.= |
| 1694 |
'$'.$e.$i.' = |
| 1695 |
$dt0[($'.$s.$i .' >> 24) & 0xff] ^ |
| 1696 |
$dt1[($'.$s.(($Nb + $i - $c[1]) % $Nb).' >> 16) & 0xff] ^ |
| 1697 |
$dt2[($'.$s.(($Nb + $i - $c[2]) % $Nb).' >> 8) & 0xff] ^ |
| 1698 |
$dt3[ $'.$s.(($Nb + $i - $c[3]) % $Nb).' & 0xff] ^ |
| 1699 |
'.$dw[++$wc].";\n"; |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
// Finalround: subWord + shiftRows + addRoundKey |
| 1704 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1705 |
$_decryptBlock.= |
| 1706 |
'$'.$e.$i.' = |
| 1707 |
$isbox[ $'.$e.$i.' & 0xff] | |
| 1708 |
($isbox[($'.$e.$i.' >> 8) & 0xff] << 8) | |
| 1709 |
($isbox[($'.$e.$i.' >> 16) & 0xff] << 16) | |
| 1710 |
($isbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n"; |
| 1711 |
} |
| 1712 |
$_decryptBlock .= '$in = pack("N*"'."\n"; |
| 1713 |
for ($i = 0; $i < $Nb; ++$i) { |
| 1714 |
$_decryptBlock.= ', |
| 1715 |
($'.$e.$i. ' & 0xFF000000) ^ |
| 1716 |
($'.$e.(($Nb + $i - $c[1]) % $Nb).' & 0x00FF0000) ^ |
| 1717 |
($'.$e.(($Nb + $i - $c[2]) % $Nb).' & 0x0000FF00) ^ |
| 1718 |
($'.$e.(($Nb + $i - $c[3]) % $Nb).' & 0x000000FF) ^ |
| 1719 |
'.$dw[$i]."\n"; |
| 1720 |
} |
| 1721 |
$_decryptBlock .= ');'; |
| 1722 |
|
| 1723 |
// Generating mode of operation code: |
| 1724 |
switch ($mode) { |
| 1725 |
case CRYPT_RIJNDAEL_MODE_ECB: |
| 1726 |
$encrypt = $init_encryptBlock . ' |
| 1727 |
$ciphertext = ""; |
| 1728 |
$text = $self->_pad($text); |
| 1729 |
$plaintext_len = strlen($text); |
| 1730 |
|
| 1731 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1732 |
$in = substr($text, $i, '.$block_size.'); |
| 1733 |
'.$_encryptBlock.' |
| 1734 |
$ciphertext.= $in; |
| 1735 |
} |
| 1736 |
|
| 1737 |
return $ciphertext; |
| 1738 |
'; |
| 1739 |
|
| 1740 |
$decrypt = $init_decryptBlock . ' |
| 1741 |
$plaintext = ""; |
| 1742 |
$text = str_pad($text, strlen($text) + ('.$block_size.' - strlen($text) % '.$block_size.') % '.$block_size.', chr(0)); |
| 1743 |
$ciphertext_len = strlen($text); |
| 1744 |
|
| 1745 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 1746 |
$in = substr($text, $i, '.$block_size.'); |
| 1747 |
'.$_decryptBlock.' |
| 1748 |
$plaintext.= $in; |
| 1749 |
} |
| 1750 |
|
| 1751 |
return $self->_unpad($plaintext); |
| 1752 |
'; |
| 1753 |
break; |
| 1754 |
case CRYPT_RIJNDAEL_MODE_CBC: |
| 1755 |
$encrypt = $init_encryptBlock . ' |
| 1756 |
$ciphertext = ""; |
| 1757 |
$text = $self->_pad($text); |
| 1758 |
$plaintext_len = strlen($text); |
| 1759 |
|
| 1760 |
$in = $self->encryptIV; |
| 1761 |
|
| 1762 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1763 |
$in = substr($text, $i, '.$block_size.') ^ $in; |
| 1764 |
'.$_encryptBlock.' |
| 1765 |
$ciphertext.= $in; |
| 1766 |
} |
| 1767 |
|
| 1768 |
if ($self->continuousBuffer) { |
| 1769 |
$self->encryptIV = $in; |
| 1770 |
} |
| 1771 |
|
| 1772 |
return $ciphertext; |
| 1773 |
'; |
| 1774 |
|
| 1775 |
$decrypt = $init_decryptBlock . ' |
| 1776 |
$plaintext = ""; |
| 1777 |
$text = str_pad($text, strlen($text) + ('.$block_size.' - strlen($text) % '.$block_size.') % '.$block_size.', chr(0)); |
| 1778 |
$ciphertext_len = strlen($text); |
| 1779 |
|
| 1780 |
$iv = $self->decryptIV; |
| 1781 |
|
| 1782 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 1783 |
$in = $block = substr($text, $i, '.$block_size.'); |
| 1784 |
'.$_decryptBlock.' |
| 1785 |
$plaintext.= $in ^ $iv; |
| 1786 |
$iv = $block; |
| 1787 |
} |
| 1788 |
|
| 1789 |
if ($self->continuousBuffer) { |
| 1790 |
$self->decryptIV = $iv; |
| 1791 |
} |
| 1792 |
|
| 1793 |
return $self->_unpad($plaintext); |
| 1794 |
'; |
| 1795 |
break; |
| 1796 |
case CRYPT_RIJNDAEL_MODE_CTR: |
| 1797 |
$encrypt = $init_encryptBlock . ' |
| 1798 |
$ciphertext = ""; |
| 1799 |
$plaintext_len = strlen($text); |
| 1800 |
$xor = $self->encryptIV; |
| 1801 |
$buffer = &$self->enbuffer; |
| 1802 |
|
| 1803 |
if (strlen($buffer["encrypted"])) { |
| 1804 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1805 |
$block = substr($text, $i, '.$block_size.'); |
| 1806 |
if (strlen($block) > strlen($buffer["encrypted"])) { |
| 1807 |
$in = $self->_generate_xor('.$block_size.', $xor); |
| 1808 |
'.$_encryptBlock.' |
| 1809 |
$buffer["encrypted"].= $in; |
| 1810 |
} |
| 1811 |
$key = $self->_string_shift($buffer["encrypted"], '.$block_size.'); |
| 1812 |
$ciphertext.= $block ^ $key; |
| 1813 |
} |
| 1814 |
} else { |
| 1815 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1816 |
$block = substr($text, $i, '.$block_size.'); |
| 1817 |
$in = $self->_generate_xor('.$block_size.', $xor); |
| 1818 |
'.$_encryptBlock.' |
| 1819 |
$key = $in; |
| 1820 |
$ciphertext.= $block ^ $key; |
| 1821 |
} |
| 1822 |
} |
| 1823 |
if ($self->continuousBuffer) { |
| 1824 |
$self->encryptIV = $xor; |
| 1825 |
if ($start = $plaintext_len % '.$block_size.') { |
| 1826 |
$buffer["encrypted"] = substr($key, $start) . $buffer["encrypted"]; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
return $ciphertext; |
| 1831 |
'; |
| 1832 |
|
| 1833 |
$decrypt = $init_encryptBlock . ' |
| 1834 |
$plaintext = ""; |
| 1835 |
$ciphertext_len = strlen($text); |
| 1836 |
$xor = $self->decryptIV; |
| 1837 |
$buffer = &$self->debuffer; |
| 1838 |
|
| 1839 |
if (strlen($buffer["ciphertext"])) { |
| 1840 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 1841 |
$block = substr($text, $i, '.$block_size.'); |
| 1842 |
if (strlen($block) > strlen($buffer["ciphertext"])) { |
| 1843 |
$in = $self->_generate_xor('.$block_size.', $xor); |
| 1844 |
'.$_encryptBlock.' |
| 1845 |
$buffer["ciphertext"].= $in; |
| 1846 |
} |
| 1847 |
$key = $self->_string_shift($buffer["ciphertext"], '.$block_size.'); |
| 1848 |
$plaintext.= $block ^ $key; |
| 1849 |
} |
| 1850 |
} else { |
| 1851 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 1852 |
$block = substr($text, $i, '.$block_size.'); |
| 1853 |
$in = $self->_generate_xor('.$block_size.', $xor); |
| 1854 |
'.$_encryptBlock.' |
| 1855 |
$key = $in; |
| 1856 |
$plaintext.= $block ^ $key; |
| 1857 |
} |
| 1858 |
} |
| 1859 |
if ($self->continuousBuffer) { |
| 1860 |
$self->decryptIV = $xor; |
| 1861 |
if ($start = $ciphertext_len % '.$block_size.') { |
| 1862 |
$buffer["ciphertext"] = substr($key, $start) . $buffer["ciphertext"]; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
return $plaintext; |
| 1867 |
'; |
| 1868 |
break; |
| 1869 |
case CRYPT_RIJNDAEL_MODE_CFB: |
| 1870 |
$encrypt = $init_encryptBlock . ' |
| 1871 |
$ciphertext = ""; |
| 1872 |
$buffer = &$self->enbuffer; |
| 1873 |
|
| 1874 |
if ($self->continuousBuffer) { |
| 1875 |
$iv = &$self->encryptIV; |
| 1876 |
$pos = &$buffer["pos"]; |
| 1877 |
} else { |
| 1878 |
$iv = $self->encryptIV; |
| 1879 |
$pos = 0; |
| 1880 |
} |
| 1881 |
$len = strlen($text); |
| 1882 |
$i = 0; |
| 1883 |
if ($pos) { |
| 1884 |
$orig_pos = $pos; |
| 1885 |
$max = '.$block_size.' - $pos; |
| 1886 |
if ($len >= $max) { |
| 1887 |
$i = $max; |
| 1888 |
$len-= $max; |
| 1889 |
$pos = 0; |
| 1890 |
} else { |
| 1891 |
$i = $len; |
| 1892 |
$pos+= $len; |
| 1893 |
$len = 0; |
| 1894 |
} |
| 1895 |
$ciphertext = substr($iv, $orig_pos) ^ $text; |
| 1896 |
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i); |
| 1897 |
} |
| 1898 |
while ($len >= '.$block_size.') { |
| 1899 |
$in = $iv; |
| 1900 |
'.$_encryptBlock.'; |
| 1901 |
$iv = $in ^ substr($text, $i, '.$block_size.'); |
| 1902 |
$ciphertext.= $iv; |
| 1903 |
$len-= '.$block_size.'; |
| 1904 |
$i+= '.$block_size.'; |
| 1905 |
} |
| 1906 |
if ($len) { |
| 1907 |
$in = $iv; |
| 1908 |
'.$_encryptBlock.' |
| 1909 |
$iv = $in; |
| 1910 |
$block = $iv ^ substr($text, $i); |
| 1911 |
$iv = substr_replace($iv, $block, 0, $len); |
| 1912 |
$ciphertext.= $block; |
| 1913 |
$pos = $len; |
| 1914 |
} |
| 1915 |
return $ciphertext; |
| 1916 |
'; |
| 1917 |
|
| 1918 |
$decrypt = $init_encryptBlock . ' |
| 1919 |
$plaintext = ""; |
| 1920 |
$buffer = &$self->debuffer; |
| 1921 |
|
| 1922 |
if ($self->continuousBuffer) { |
| 1923 |
$iv = &$self->decryptIV; |
| 1924 |
$pos = &$buffer["pos"]; |
| 1925 |
} else { |
| 1926 |
$iv = $self->decryptIV; |
| 1927 |
$pos = 0; |
| 1928 |
} |
| 1929 |
$len = strlen($text); |
| 1930 |
$i = 0; |
| 1931 |
if ($pos) { |
| 1932 |
$orig_pos = $pos; |
| 1933 |
$max = '.$block_size.' - $pos; |
| 1934 |
if ($len >= $max) { |
| 1935 |
$i = $max; |
| 1936 |
$len-= $max; |
| 1937 |
$pos = 0; |
| 1938 |
} else { |
| 1939 |
$i = $len; |
| 1940 |
$pos+= $len; |
| 1941 |
$len = 0; |
| 1942 |
} |
| 1943 |
$plaintext = substr($iv, $orig_pos) ^ $text; |
| 1944 |
$iv = substr_replace($iv, substr($text, 0, $i), $orig_pos, $i); |
| 1945 |
} |
| 1946 |
while ($len >= '.$block_size.') { |
| 1947 |
$in = $iv; |
| 1948 |
'.$_encryptBlock.' |
| 1949 |
$iv = $in; |
| 1950 |
$cb = substr($text, $i, '.$block_size.'); |
| 1951 |
$plaintext.= $iv ^ $cb; |
| 1952 |
$iv = $cb; |
| 1953 |
$len-= '.$block_size.'; |
| 1954 |
$i+= '.$block_size.'; |
| 1955 |
} |
| 1956 |
if ($len) { |
| 1957 |
$in = $iv; |
| 1958 |
'.$_encryptBlock.' |
| 1959 |
$iv = $in; |
| 1960 |
$plaintext.= $iv ^ substr($text, $i); |
| 1961 |
$iv = substr_replace($iv, substr($text, $i), 0, $len); |
| 1962 |
$pos = $len; |
| 1963 |
} |
| 1964 |
|
| 1965 |
return $plaintext; |
| 1966 |
'; |
| 1967 |
break; |
| 1968 |
case CRYPT_RIJNDAEL_MODE_OFB: |
| 1969 |
$encrypt = $init_encryptBlock . ' |
| 1970 |
$ciphertext = ""; |
| 1971 |
$plaintext_len = strlen($text); |
| 1972 |
$xor = $self->encryptIV; |
| 1973 |
$buffer = &$self->enbuffer; |
| 1974 |
|
| 1975 |
if (strlen($buffer["xor"])) { |
| 1976 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1977 |
$block = substr($text, $i, '.$block_size.'); |
| 1978 |
if (strlen($block) > strlen($buffer["xor"])) { |
| 1979 |
$in = $xor; |
| 1980 |
'.$_encryptBlock.' |
| 1981 |
$xor = $in; |
| 1982 |
$buffer["xor"].= $xor; |
| 1983 |
} |
| 1984 |
$key = $self->_string_shift($buffer["xor"], '.$block_size.'); |
| 1985 |
$ciphertext.= $block ^ $key; |
| 1986 |
} |
| 1987 |
} else { |
| 1988 |
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') { |
| 1989 |
$in = $xor; |
| 1990 |
'.$_encryptBlock.' |
| 1991 |
$xor = $in; |
| 1992 |
$ciphertext.= substr($text, $i, '.$block_size.') ^ $xor; |
| 1993 |
} |
| 1994 |
$key = $xor; |
| 1995 |
} |
| 1996 |
if ($self->continuousBuffer) { |
| 1997 |
$self->encryptIV = $xor; |
| 1998 |
if ($start = $plaintext_len % '.$block_size.') { |
| 1999 |
$buffer["xor"] = substr($key, $start) . $buffer["xor"]; |
| 2000 |
} |
| 2001 |
} |
| 2002 |
return $ciphertext; |
| 2003 |
'; |
| 2004 |
|
| 2005 |
$decrypt = $init_encryptBlock . ' |
| 2006 |
$plaintext = ""; |
| 2007 |
$ciphertext_len = strlen($text); |
| 2008 |
$xor = $self->decryptIV; |
| 2009 |
$buffer = &$self->debuffer; |
| 2010 |
|
| 2011 |
if (strlen($buffer["xor"])) { |
| 2012 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 2013 |
$block = substr($text, $i, '.$block_size.'); |
| 2014 |
if (strlen($block) > strlen($buffer["xor"])) { |
| 2015 |
$in = $xor; |
| 2016 |
'.$_encryptBlock.' |
| 2017 |
$xor = $in; |
| 2018 |
$buffer["xor"].= $xor; |
| 2019 |
} |
| 2020 |
$key = $self->_string_shift($buffer["xor"], '.$block_size.'); |
| 2021 |
$plaintext.= $block ^ $key; |
| 2022 |
} |
| 2023 |
} else { |
| 2024 |
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') { |
| 2025 |
$in = $xor; |
| 2026 |
'.$_encryptBlock.' |
| 2027 |
$xor = $in; |
| 2028 |
$plaintext.= substr($text, $i, '.$block_size.') ^ $xor; |
| 2029 |
} |
| 2030 |
$key = $xor; |
| 2031 |
} |
| 2032 |
if ($self->continuousBuffer) { |
| 2033 |
$self->decryptIV = $xor; |
| 2034 |
if ($start = $ciphertext_len % '.$block_size.') { |
| 2035 |
$buffer["xor"] = substr($key, $start) . $buffer["xor"]; |
| 2036 |
} |
| 2037 |
} |
| 2038 |
return $plaintext; |
| 2039 |
'; |
| 2040 |
break; |
| 2041 |
} |
| 2042 |
$lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { '.$encrypt.' } else { '.$decrypt.' }'); |
| 2043 |
} |
| 2044 |
$this->inline_crypt = $lambda_functions[$code_hash]; |
| 2045 |
} |
| 2046 |
|
| 2047 |
/** |
| 2048 |
* Holds the lambda_functions table (classwide) |
| 2049 |
* |
| 2050 |
* @see Crypt_Rijndael::inline_crypt_setup() |
| 2051 |
* @return Array |
| 2052 |
* @access private |
| 2053 |
*/ |
| 2054 |
function &get_lambda_functions() |
| 2055 |
{ |
| 2056 |
static $functions = array(); |
| 2057 |
return $functions; |
| 2058 |
} |
| 2059 |
} |
| 2060 |
|
| 2061 |
// vim: ts=4:sw=4:et: |
| 2062 |
// vim6: fdl=1: |
| 2063 |
|