AES.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of AES.
  5. *
  6. * Uses mcrypt, if available, and an internal implementation, otherwise.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
  11. * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
  12. * it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}
  13. * is called, again, at which point, it'll be recalculated.
  14. *
  15. * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
  16. * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
  17. * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
  18. *
  19. * Here's a short example of how to use this library:
  20. * <code>
  21. * <?php
  22. * include('Crypt/AES.php');
  23. *
  24. * $aes = new Crypt_AES();
  25. *
  26. * $aes->setKey('abcdefghijklmnop');
  27. *
  28. * $size = 10 * 1024;
  29. * $plaintext = '';
  30. * for ($i = 0; $i < $size; $i++) {
  31. * $plaintext.= 'a';
  32. * }
  33. *
  34. * echo $aes->decrypt($aes->encrypt($plaintext));
  35. * ?>
  36. * </code>
  37. *
  38. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  39. * of this software and associated documentation files (the "Software"), to deal
  40. * in the Software without restriction, including without limitation the rights
  41. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  42. * copies of the Software, and to permit persons to whom the Software is
  43. * furnished to do so, subject to the following conditions:
  44. *
  45. * The above copyright notice and this permission notice shall be included in
  46. * all copies or substantial portions of the Software.
  47. *
  48. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  49. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  50. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  51. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  52. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  53. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  54. * THE SOFTWARE.
  55. *
  56. * @category Crypt
  57. * @package Crypt_AES
  58. * @author Jim Wigginton <terrafrost@php.net>
  59. * @copyright MMVIII Jim Wigginton
  60. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  61. * @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $
  62. * @link http://phpseclib.sourceforge.net
  63. */
  64. /**
  65. * Include Crypt_Rijndael
  66. */
  67. if (!class_exists('Crypt_Rijndael')) {
  68. require_once 'Rijndael.php';
  69. }
  70. /**#@+
  71. * @access public
  72. * @see Crypt_AES::encrypt()
  73. * @see Crypt_AES::decrypt()
  74. */
  75. /**
  76. * Encrypt / decrypt using the Counter mode.
  77. *
  78. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  79. *
  80. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  81. */
  82. define('CRYPT_AES_MODE_CTR', -1);
  83. /**
  84. * Encrypt / decrypt using the Electronic Code Book mode.
  85. *
  86. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  87. */
  88. define('CRYPT_AES_MODE_ECB', 1);
  89. /**
  90. * Encrypt / decrypt using the Code Book Chaining mode.
  91. *
  92. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  93. */
  94. define('CRYPT_AES_MODE_CBC', 2);
  95. /**
  96. * Encrypt / decrypt using the Cipher Feedback mode.
  97. *
  98. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  99. */
  100. define('CRYPT_AES_MODE_CFB', 3);
  101. /**
  102. * Encrypt / decrypt using the Cipher Feedback mode.
  103. *
  104. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  105. */
  106. define('CRYPT_AES_MODE_OFB', 4);
  107. /**#@-*/
  108. /**#@+
  109. * @access private
  110. * @see Crypt_AES::Crypt_AES()
  111. */
  112. /**
  113. * Toggles the internal implementation
  114. */
  115. define('CRYPT_AES_MODE_INTERNAL', 1);
  116. /**
  117. * Toggles the mcrypt implementation
  118. */
  119. define('CRYPT_AES_MODE_MCRYPT', 2);
  120. /**#@-*/
  121. /**
  122. * Pure-PHP implementation of AES.
  123. *
  124. * @author Jim Wigginton <terrafrost@php.net>
  125. * @version 0.1.0
  126. * @access public
  127. * @package Crypt_AES
  128. */
  129. class Crypt_AES extends Crypt_Rijndael {
  130. /**
  131. * mcrypt resource for encryption
  132. *
  133. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  134. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  135. *
  136. * @see Crypt_AES::encrypt()
  137. * @var String
  138. * @access private
  139. */
  140. var $enmcrypt;
  141. /**
  142. * mcrypt resource for decryption
  143. *
  144. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  145. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  146. *
  147. * @see Crypt_AES::decrypt()
  148. * @var String
  149. * @access private
  150. */
  151. var $demcrypt;
  152. /**
  153. * mcrypt resource for CFB mode
  154. *
  155. * @see Crypt_AES::encrypt()
  156. * @see Crypt_AES::decrypt()
  157. * @var String
  158. * @access private
  159. */
  160. var $ecb;
  161. /**
  162. * Default Constructor.
  163. *
  164. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  165. * CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
  166. *
  167. * @param optional Integer $mode
  168. * @return Crypt_AES
  169. * @access public
  170. */
  171. function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
  172. {
  173. if ( !defined('CRYPT_AES_MODE') ) {
  174. switch (true) {
  175. case extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()):
  176. define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
  177. break;
  178. default:
  179. define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
  180. }
  181. }
  182. switch ( CRYPT_AES_MODE ) {
  183. case CRYPT_AES_MODE_MCRYPT:
  184. switch ($mode) {
  185. case CRYPT_AES_MODE_ECB:
  186. $this->paddable = true;
  187. $this->mode = MCRYPT_MODE_ECB;
  188. break;
  189. case CRYPT_AES_MODE_CTR:
  190. // ctr doesn't have a constant associated with it even though it appears to be fairly widely
  191. // supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to
  192. // include a compatibility layer. the layer has been implemented but, for now, is commented out.
  193. $this->mode = 'ctr';
  194. //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;
  195. break;
  196. case CRYPT_AES_MODE_CFB:
  197. $this->mode = 'ncfb';
  198. break;
  199. case CRYPT_AES_MODE_OFB:
  200. $this->mode = MCRYPT_MODE_NOFB;
  201. break;
  202. case CRYPT_AES_MODE_CBC:
  203. default:
  204. $this->paddable = true;
  205. $this->mode = MCRYPT_MODE_CBC;
  206. }
  207. $this->debuffer = $this->enbuffer = '';
  208. break;
  209. default:
  210. switch ($mode) {
  211. case CRYPT_AES_MODE_ECB:
  212. $this->paddable = true;
  213. $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
  214. break;
  215. case CRYPT_AES_MODE_CTR:
  216. $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
  217. break;
  218. case CRYPT_AES_MODE_CFB:
  219. $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
  220. break;
  221. case CRYPT_AES_MODE_OFB:
  222. $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
  223. break;
  224. case CRYPT_AES_MODE_CBC:
  225. default:
  226. $this->paddable = true;
  227. $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
  228. }
  229. }
  230. if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
  231. parent::Crypt_Rijndael($this->mode);
  232. }
  233. }
  234. /**
  235. * Dummy function
  236. *
  237. * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
  238. *
  239. * @access public
  240. * @param Integer $length
  241. */
  242. function setBlockLength($length)
  243. {
  244. return;
  245. }
  246. /**
  247. * Sets the initialization vector. (optional)
  248. *
  249. * SetIV is not required when CRYPT_RIJNDAEL_MODE_ECB is being used. If not explictly set, it'll be assumed
  250. * to be all zero's.
  251. *
  252. * @access public
  253. * @param String $iv
  254. */
  255. function setIV($iv)
  256. {
  257. parent::setIV($iv);
  258. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  259. $this->changed = true;
  260. }
  261. }
  262. /**
  263. * Encrypts a message.
  264. *
  265. * $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
  266. * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
  267. * URL:
  268. *
  269. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  270. *
  271. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  272. * strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
  273. * length.
  274. *
  275. * @see Crypt_AES::decrypt()
  276. * @access public
  277. * @param String $plaintext
  278. */
  279. function encrypt($plaintext)
  280. {
  281. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  282. $changed = $this->changed;
  283. $this->_mcryptSetup();
  284. /*
  285. if ($this->mode == CRYPT_AES_MODE_CTR) {
  286. $iv = $this->encryptIV;
  287. $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));
  288. $ciphertext = $plaintext ^ $xor;
  289. if ($this->continuousBuffer) {
  290. $this->encryptIV = $iv;
  291. }
  292. return $ciphertext;
  293. }
  294. */
  295. // re: http://phpseclib.sourceforge.net/cfb-demo.phps
  296. // using mcrypt's default handing of CFB the above would output two different things. using phpseclib's
  297. // rewritten CFB implementation the above outputs the same thing twice.
  298. if ($this->mode == 'ncfb') {
  299. if ($changed) {
  300. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  301. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  302. }
  303. if (strlen($this->enbuffer)) {
  304. $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer));
  305. $this->enbuffer.= $ciphertext;
  306. if (strlen($this->enbuffer) == 16) {
  307. $this->encryptIV = $this->enbuffer;
  308. $this->enbuffer = '';
  309. mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
  310. }
  311. $plaintext = substr($plaintext, strlen($ciphertext));
  312. } else {
  313. $ciphertext = '';
  314. }
  315. $last_pos = strlen($plaintext) & 0xFFFFFFF0;
  316. $ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : '';
  317. if (strlen($plaintext) & 0xF) {
  318. if (strlen($ciphertext)) {
  319. $this->encryptIV = substr($ciphertext, -16);
  320. }
  321. $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
  322. $this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
  323. $ciphertext.= $this->enbuffer;
  324. }
  325. return $ciphertext;
  326. }
  327. if ($this->paddable) {
  328. $plaintext = $this->_pad($plaintext);
  329. }
  330. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  331. if (!$this->continuousBuffer) {
  332. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  333. }
  334. return $ciphertext;
  335. }
  336. return parent::encrypt($plaintext);
  337. }
  338. /**
  339. * Decrypts a message.
  340. *
  341. * If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
  342. *
  343. * @see Crypt_AES::encrypt()
  344. * @access public
  345. * @param String $ciphertext
  346. */
  347. function decrypt($ciphertext)
  348. {
  349. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  350. $changed = $this->changed;
  351. $this->_mcryptSetup();
  352. /*
  353. if ($this->mode == CRYPT_AES_MODE_CTR) {
  354. $iv = $this->decryptIV;
  355. $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));
  356. $plaintext = $ciphertext ^ $xor;
  357. if ($this->continuousBuffer) {
  358. $this->decryptIV = $iv;
  359. }
  360. return $plaintext;
  361. }
  362. */
  363. if ($this->mode == 'ncfb') {
  364. if ($changed) {
  365. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  366. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  367. }
  368. if (strlen($this->debuffer)) {
  369. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer));
  370. $this->debuffer.= substr($ciphertext, 0, strlen($plaintext));
  371. if (strlen($this->debuffer) == 16) {
  372. $this->decryptIV = $this->debuffer;
  373. $this->debuffer = '';
  374. mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
  375. }
  376. $ciphertext = substr($ciphertext, strlen($plaintext));
  377. } else {
  378. $plaintext = '';
  379. }
  380. $last_pos = strlen($ciphertext) & 0xFFFFFFF0;
  381. $plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : '';
  382. if (strlen($ciphertext) & 0xF) {
  383. if (strlen($plaintext)) {
  384. $this->decryptIV = substr($ciphertext, $last_pos - 16, 16);
  385. }
  386. $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
  387. $this->debuffer = substr($ciphertext, $last_pos);
  388. $plaintext.= $this->debuffer ^ $this->decryptIV;
  389. }
  390. return $plaintext;
  391. }
  392. if ($this->paddable) {
  393. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  394. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  395. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
  396. }
  397. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  398. if (!$this->continuousBuffer) {
  399. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  400. }
  401. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  402. }
  403. return parent::decrypt($ciphertext);
  404. }
  405. /**
  406. * Setup mcrypt
  407. *
  408. * Validates all the variables.
  409. *
  410. * @access private
  411. */
  412. function _mcryptSetup()
  413. {
  414. if (!$this->changed) {
  415. return;
  416. }
  417. if (!$this->explicit_key_length) {
  418. // this just copied from Crypt_Rijndael::_setup()
  419. $length = strlen($this->key) >> 2;
  420. if ($length > 8) {
  421. $length = 8;
  422. } else if ($length < 4) {
  423. $length = 4;
  424. }
  425. $this->Nk = $length;
  426. $this->key_size = $length << 2;
  427. }
  428. switch ($this->Nk) {
  429. case 4: // 128
  430. $this->key_size = 16;
  431. break;
  432. case 5: // 160
  433. case 6: // 192
  434. $this->key_size = 24;
  435. break;
  436. case 7: // 224
  437. case 8: // 256
  438. $this->key_size = 32;
  439. }
  440. $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
  441. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
  442. if (!isset($this->enmcrypt)) {
  443. $mode = $this->mode;
  444. //$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;
  445. $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  446. $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  447. } // else should mcrypt_generic_deinit be called?
  448. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  449. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  450. $this->changed = false;
  451. }
  452. /**
  453. * Encrypts a block
  454. *
  455. * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
  456. *
  457. * @see Crypt_Rijndael::_encryptBlock()
  458. * @access private
  459. * @param String $in
  460. * @return String
  461. */
  462. function _encryptBlock($in)
  463. {
  464. $state = unpack('N*word', $in);
  465. $Nr = $this->Nr;
  466. $w = $this->w;
  467. $t0 = $this->t0;
  468. $t1 = $this->t1;
  469. $t2 = $this->t2;
  470. $t3 = $this->t3;
  471. // addRoundKey and reindex $state
  472. $state = array(
  473. $state['word1'] ^ $w[0][0],
  474. $state['word2'] ^ $w[0][1],
  475. $state['word3'] ^ $w[0][2],
  476. $state['word4'] ^ $w[0][3]
  477. );
  478. // shiftRows + subWord + mixColumns + addRoundKey
  479. // we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
  480. // only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
  481. for ($round = 1; $round < $this->Nr; $round++) {
  482. $state = array(
  483. $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
  484. $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
  485. $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
  486. $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
  487. );
  488. }
  489. // subWord
  490. $state = array(
  491. $this->_subWord($state[0]),
  492. $this->_subWord($state[1]),
  493. $this->_subWord($state[2]),
  494. $this->_subWord($state[3])
  495. );
  496. // shiftRows + addRoundKey
  497. $state = array(
  498. ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
  499. ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
  500. ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
  501. ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
  502. );
  503. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  504. }
  505. /**
  506. * Decrypts a block
  507. *
  508. * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
  509. *
  510. * @see Crypt_Rijndael::_decryptBlock()
  511. * @access private
  512. * @param String $in
  513. * @return String
  514. */
  515. function _decryptBlock($in)
  516. {
  517. $state = unpack('N*word', $in);
  518. $Nr = $this->Nr;
  519. $dw = $this->dw;
  520. $dt0 = $this->dt0;
  521. $dt1 = $this->dt1;
  522. $dt2 = $this->dt2;
  523. $dt3 = $this->dt3;
  524. // addRoundKey and reindex $state
  525. $state = array(
  526. $state['word1'] ^ $dw[$this->Nr][0],
  527. $state['word2'] ^ $dw[$this->Nr][1],
  528. $state['word3'] ^ $dw[$this->Nr][2],
  529. $state['word4'] ^ $dw[$this->Nr][3]
  530. );
  531. // invShiftRows + invSubBytes + invMixColumns + addRoundKey
  532. for ($round = $this->Nr - 1; $round > 0; $round--) {
  533. $state = array(
  534. $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
  535. $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
  536. $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
  537. $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
  538. );
  539. }
  540. // invShiftRows + invSubWord + addRoundKey
  541. $state = array(
  542. $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
  543. $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
  544. $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
  545. $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
  546. );
  547. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  548. }
  549. }
  550. // vim: ts=4:sw=4:et:
  551. // vim6: fdl=1:
  552. ?>