AES.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. require_once 'Rijndael.php';
  68. /**#@+
  69. * @access public
  70. * @see Crypt_AES::encrypt()
  71. * @see Crypt_AES::decrypt()
  72. */
  73. /**
  74. * Encrypt / decrypt using the Counter mode.
  75. *
  76. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  77. *
  78. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  79. */
  80. define('CRYPT_AES_MODE_CTR', -1);
  81. /**
  82. * Encrypt / decrypt using the Electronic Code Book mode.
  83. *
  84. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  85. */
  86. define('CRYPT_AES_MODE_ECB', 1);
  87. /**
  88. * Encrypt / decrypt using the Code Book Chaining mode.
  89. *
  90. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  91. */
  92. define('CRYPT_AES_MODE_CBC', 2);
  93. /**
  94. * Encrypt / decrypt using the Cipher Feedback mode.
  95. *
  96. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  97. */
  98. define('CRYPT_AES_MODE_CFB', 3);
  99. /**
  100. * Encrypt / decrypt using the Cipher Feedback mode.
  101. *
  102. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  103. */
  104. define('CRYPT_AES_MODE_OFB', 4);
  105. /**#@-*/
  106. /**#@+
  107. * @access private
  108. * @see Crypt_AES::Crypt_AES()
  109. */
  110. /**
  111. * Toggles the internal implementation
  112. */
  113. define('CRYPT_AES_MODE_INTERNAL', 1);
  114. /**
  115. * Toggles the mcrypt implementation
  116. */
  117. define('CRYPT_AES_MODE_MCRYPT', 2);
  118. /**#@-*/
  119. /**
  120. * Pure-PHP implementation of AES.
  121. *
  122. * @author Jim Wigginton <terrafrost@php.net>
  123. * @version 0.1.0
  124. * @access public
  125. * @package Crypt_AES
  126. */
  127. class Crypt_AES extends Crypt_Rijndael {
  128. /**
  129. * mcrypt resource for encryption
  130. *
  131. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  132. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  133. *
  134. * @see Crypt_AES::encrypt()
  135. * @var String
  136. * @access private
  137. */
  138. var $enmcrypt;
  139. /**
  140. * mcrypt resource for decryption
  141. *
  142. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  143. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  144. *
  145. * @see Crypt_AES::decrypt()
  146. * @var String
  147. * @access private
  148. */
  149. var $demcrypt;
  150. /**
  151. * mcrypt resource for CFB mode
  152. *
  153. * @see Crypt_AES::encrypt()
  154. * @see Crypt_AES::decrypt()
  155. * @var String
  156. * @access private
  157. */
  158. var $ecb;
  159. /**
  160. * Default Constructor.
  161. *
  162. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  163. * CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
  164. *
  165. * @param optional Integer $mode
  166. * @return Crypt_AES
  167. * @access public
  168. */
  169. function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
  170. {
  171. if ( !defined('CRYPT_AES_MODE') ) {
  172. switch (true) {
  173. case extension_loaded('mcrypt'):
  174. // i'd check to see if aes was supported, by doing in_array('des', mcrypt_list_algorithms('')),
  175. // but since that can be changed after the object has been created, there doesn't seem to be
  176. // a lot of point...
  177. define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
  178. break;
  179. default:
  180. define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
  181. }
  182. }
  183. switch ( CRYPT_AES_MODE ) {
  184. case CRYPT_AES_MODE_MCRYPT:
  185. switch ($mode) {
  186. case CRYPT_AES_MODE_ECB:
  187. $this->paddable = true;
  188. $this->mode = MCRYPT_MODE_ECB;
  189. break;
  190. case CRYPT_AES_MODE_CTR:
  191. // ctr doesn't have a constant associated with it even though it appears to be fairly widely
  192. // supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to
  193. // include a compatibility layer. the layer has been implemented but, for now, is commented out.
  194. $this->mode = 'ctr';
  195. //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;
  196. break;
  197. case CRYPT_AES_MODE_CFB:
  198. $this->mode = 'ncfb';
  199. break;
  200. case CRYPT_AES_MODE_OFB:
  201. $this->mode = MCRYPT_MODE_NOFB;
  202. break;
  203. case CRYPT_AES_MODE_CBC:
  204. default:
  205. $this->paddable = true;
  206. $this->mode = MCRYPT_MODE_CBC;
  207. }
  208. $this->debuffer = $this->enbuffer = '';
  209. break;
  210. default:
  211. switch ($mode) {
  212. case CRYPT_AES_MODE_ECB:
  213. $this->paddable = true;
  214. $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
  215. break;
  216. case CRYPT_AES_MODE_CTR:
  217. $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
  218. break;
  219. case CRYPT_AES_MODE_CFB:
  220. $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
  221. break;
  222. case CRYPT_AES_MODE_OFB:
  223. $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
  224. break;
  225. case CRYPT_AES_MODE_CBC:
  226. default:
  227. $this->paddable = true;
  228. $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
  229. }
  230. }
  231. if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
  232. parent::Crypt_Rijndael($this->mode);
  233. }
  234. }
  235. /**
  236. * Dummy function
  237. *
  238. * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
  239. *
  240. * @access public
  241. * @param Integer $length
  242. */
  243. function setBlockLength($length)
  244. {
  245. return;
  246. }
  247. /**
  248. * Encrypts a message.
  249. *
  250. * $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
  251. * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
  252. * URL:
  253. *
  254. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  255. *
  256. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  257. * strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
  258. * length.
  259. *
  260. * @see Crypt_AES::decrypt()
  261. * @access public
  262. * @param String $plaintext
  263. */
  264. function encrypt($plaintext)
  265. {
  266. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  267. $changed = $this->changed;
  268. $this->_mcryptSetup();
  269. /*
  270. if ($this->mode == CRYPT_AES_MODE_CTR) {
  271. $iv = $this->encryptIV;
  272. $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));
  273. $ciphertext = $plaintext ^ $xor;
  274. if ($this->continuousBuffer) {
  275. $this->encryptIV = $iv;
  276. }
  277. return $ciphertext;
  278. }
  279. */
  280. // re: http://phpseclib.sourceforge.net/cfb-demo.phps
  281. // using mcrypt's default handing of CFB the above would output two different things. using phpseclib's
  282. // rewritten CFB implementation the above outputs the same thing twice.
  283. if ($this->mode == 'ncfb') {
  284. if ($changed) {
  285. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  286. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  287. }
  288. if (strlen($this->enbuffer)) {
  289. $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer));
  290. $this->enbuffer.= $ciphertext;
  291. if (strlen($this->enbuffer) == 16) {
  292. $this->encryptIV = $this->enbuffer;
  293. $this->enbuffer = '';
  294. mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
  295. }
  296. $plaintext = substr($plaintext, strlen($ciphertext));
  297. } else {
  298. $ciphertext = '';
  299. }
  300. $last_pos = strlen($plaintext) & 0xFFFFFFF0;
  301. $ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : '';
  302. if (strlen($plaintext) & 0xF) {
  303. if (strlen($ciphertext)) {
  304. $this->encryptIV = substr($ciphertext, -16);
  305. }
  306. $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
  307. $this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
  308. $ciphertext.= $this->enbuffer;
  309. }
  310. return $ciphertext;
  311. }
  312. if ($this->paddable) {
  313. $plaintext = $this->_pad($plaintext);
  314. }
  315. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  316. if (!$this->continuousBuffer) {
  317. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  318. }
  319. return $ciphertext;
  320. }
  321. return parent::encrypt($plaintext);
  322. }
  323. /**
  324. * Decrypts a message.
  325. *
  326. * If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
  327. *
  328. * @see Crypt_AES::encrypt()
  329. * @access public
  330. * @param String $ciphertext
  331. */
  332. function decrypt($ciphertext)
  333. {
  334. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  335. $changed = $this->changed;
  336. $this->_mcryptSetup();
  337. /*
  338. if ($this->mode == CRYPT_AES_MODE_CTR) {
  339. $iv = $this->decryptIV;
  340. $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));
  341. $plaintext = $ciphertext ^ $xor;
  342. if ($this->continuousBuffer) {
  343. $this->decryptIV = $iv;
  344. }
  345. return $plaintext;
  346. }
  347. */
  348. if ($this->mode == 'ncfb') {
  349. if ($changed) {
  350. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  351. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  352. }
  353. if (strlen($this->debuffer)) {
  354. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer));
  355. $this->debuffer.= substr($ciphertext, 0, strlen($plaintext));
  356. if (strlen($this->debuffer) == 16) {
  357. $this->decryptIV = $this->debuffer;
  358. $this->debuffer = '';
  359. mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
  360. }
  361. $ciphertext = substr($ciphertext, strlen($plaintext));
  362. } else {
  363. $plaintext = '';
  364. }
  365. $last_pos = strlen($ciphertext) & 0xFFFFFFF0;
  366. $plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : '';
  367. if (strlen($ciphertext) & 0xF) {
  368. if (strlen($plaintext)) {
  369. $this->decryptIV = substr($ciphertext, $last_pos - 16, 16);
  370. }
  371. $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
  372. $this->debuffer = substr($ciphertext, $last_pos);
  373. $plaintext.= $this->debuffer ^ $this->decryptIV;
  374. }
  375. return $plaintext;
  376. }
  377. if ($this->paddable) {
  378. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  379. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  380. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
  381. }
  382. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  383. if (!$this->continuousBuffer) {
  384. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  385. }
  386. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  387. }
  388. return parent::decrypt($ciphertext);
  389. }
  390. /**
  391. * Setup mcrypt
  392. *
  393. * Validates all the variables.
  394. *
  395. * @access private
  396. */
  397. function _mcryptSetup()
  398. {
  399. if (!$this->changed) {
  400. return;
  401. }
  402. if (!$this->explicit_key_length) {
  403. // this just copied from Crypt_Rijndael::_setup()
  404. $length = strlen($this->key) >> 2;
  405. if ($length > 8) {
  406. $length = 8;
  407. } else if ($length < 4) {
  408. $length = 4;
  409. }
  410. $this->Nk = $length;
  411. $this->key_size = $length << 2;
  412. }
  413. switch ($this->Nk) {
  414. case 4: // 128
  415. $this->key_size = 16;
  416. break;
  417. case 5: // 160
  418. case 6: // 192
  419. $this->key_size = 24;
  420. break;
  421. case 7: // 224
  422. case 8: // 256
  423. $this->key_size = 32;
  424. }
  425. $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
  426. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
  427. if (!isset($this->enmcrypt)) {
  428. $mode = $this->mode;
  429. //$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;
  430. $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  431. $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  432. } // else should mcrypt_generic_deinit be called?
  433. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  434. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  435. $this->changed = false;
  436. }
  437. /**
  438. * Encrypts a block
  439. *
  440. * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
  441. *
  442. * @see Crypt_Rijndael::_encryptBlock()
  443. * @access private
  444. * @param String $in
  445. * @return String
  446. */
  447. function _encryptBlock($in)
  448. {
  449. $state = unpack('N*word', $in);
  450. $Nr = $this->Nr;
  451. $w = $this->w;
  452. $t0 = $this->t0;
  453. $t1 = $this->t1;
  454. $t2 = $this->t2;
  455. $t3 = $this->t3;
  456. // addRoundKey and reindex $state
  457. $state = array(
  458. $state['word1'] ^ $w[0][0],
  459. $state['word2'] ^ $w[0][1],
  460. $state['word3'] ^ $w[0][2],
  461. $state['word4'] ^ $w[0][3]
  462. );
  463. // shiftRows + subWord + mixColumns + addRoundKey
  464. // we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
  465. // only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
  466. for ($round = 1; $round < $this->Nr; $round++) {
  467. $state = array(
  468. $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
  469. $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
  470. $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
  471. $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
  472. );
  473. }
  474. // subWord
  475. $state = array(
  476. $this->_subWord($state[0]),
  477. $this->_subWord($state[1]),
  478. $this->_subWord($state[2]),
  479. $this->_subWord($state[3])
  480. );
  481. // shiftRows + addRoundKey
  482. $state = array(
  483. ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
  484. ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
  485. ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
  486. ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
  487. );
  488. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  489. }
  490. /**
  491. * Decrypts a block
  492. *
  493. * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
  494. *
  495. * @see Crypt_Rijndael::_decryptBlock()
  496. * @access private
  497. * @param String $in
  498. * @return String
  499. */
  500. function _decryptBlock($in)
  501. {
  502. $state = unpack('N*word', $in);
  503. $Nr = $this->Nr;
  504. $dw = $this->dw;
  505. $dt0 = $this->dt0;
  506. $dt1 = $this->dt1;
  507. $dt2 = $this->dt2;
  508. $dt3 = $this->dt3;
  509. // addRoundKey and reindex $state
  510. $state = array(
  511. $state['word1'] ^ $dw[$this->Nr][0],
  512. $state['word2'] ^ $dw[$this->Nr][1],
  513. $state['word3'] ^ $dw[$this->Nr][2],
  514. $state['word4'] ^ $dw[$this->Nr][3]
  515. );
  516. // invShiftRows + invSubBytes + invMixColumns + addRoundKey
  517. for ($round = $this->Nr - 1; $round > 0; $round--) {
  518. $state = array(
  519. $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
  520. $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
  521. $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
  522. $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
  523. );
  524. }
  525. // invShiftRows + invSubWord + addRoundKey
  526. $state = array(
  527. $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
  528. $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
  529. $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
  530. $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
  531. );
  532. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  533. }
  534. }
  535. // vim: ts=4:sw=4:et:
  536. // vim6: fdl=1: