--- /dev/null
+<?php\r
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
+\r
+/**\r
+ * Pure-PHP implementation of AES.\r
+ *\r
+ * Uses mcrypt, if available, and an internal implementation, otherwise.\r
+ *\r
+ * PHP versions 4 and 5\r
+ *\r
+ * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from\r
+ * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits\r
+ * it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}\r
+ * is called, again, at which point, it'll be recalculated.\r
+ *\r
+ * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't\r
+ * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,\r
+ * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).\r
+ *\r
+ * Here's a short example of how to use this library:\r
+ * <code>\r
+ * <?php\r
+ * include('Crypt/AES.php');\r
+ *\r
+ * $aes = new Crypt_AES();\r
+ *\r
+ * $aes->setKey('abcdefghijklmnop');\r
+ *\r
+ * $size = 10 * 1024;\r
+ * $plaintext = '';\r
+ * for ($i = 0; $i < $size; $i++) {\r
+ * $plaintext.= 'a';\r
+ * }\r
+ *\r
+ * echo $aes->decrypt($aes->encrypt($plaintext));\r
+ * ?>\r
+ * </code>\r
+ *\r
+ * LICENSE: This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
+ * MA 02111-1307 USA\r
+ *\r
+ * @category Crypt\r
+ * @package Crypt_AES\r
+ * @author Jim Wigginton <terrafrost@php.net>\r
+ * @copyright MMVIII Jim Wigginton\r
+ * @license http://www.gnu.org/licenses/lgpl.txt\r
+ * @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $\r
+ * @link http://phpseclib.sourceforge.net\r
+ */\r
+\r
+/**\r
+ * Include Crypt_Rijndael\r
+ */\r
+require_once 'Rijndael.php';\r
+\r
+/**#@+\r
+ * @access public\r
+ * @see Crypt_AES::encrypt()\r
+ * @see Crypt_AES::decrypt()\r
+ */\r
+/**\r
+ * Encrypt / decrypt using the Counter mode.\r
+ *\r
+ * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.\r
+ *\r
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29\r
+ */\r
+define('CRYPT_AES_MODE_CTR', -1);\r
+/**\r
+ * Encrypt / decrypt using the Electronic Code Book mode.\r
+ *\r
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29\r
+ */\r
+define('CRYPT_AES_MODE_ECB', 1);\r
+/**\r
+ * Encrypt / decrypt using the Code Book Chaining mode.\r
+ *\r
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29\r
+ */\r
+define('CRYPT_AES_MODE_CBC', 2);\r
+/**#@-*/\r
+\r
+/**#@+\r
+ * @access private\r
+ * @see Crypt_AES::Crypt_AES()\r
+ */\r
+/**\r
+ * Toggles the internal implementation\r
+ */\r
+define('CRYPT_AES_MODE_INTERNAL', 1);\r
+/**\r
+ * Toggles the mcrypt implementation\r
+ */\r
+define('CRYPT_AES_MODE_MCRYPT', 2);\r
+/**#@-*/\r
+\r
+/**\r
+ * Pure-PHP implementation of AES.\r
+ *\r
+ * @author Jim Wigginton <terrafrost@php.net>\r
+ * @version 0.1.0\r
+ * @access public\r
+ * @package Crypt_AES\r
+ */\r
+class Crypt_AES extends Crypt_Rijndael {\r
+ /**\r
+ * mcrypt resource for encryption\r
+ *\r
+ * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
+ * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
+ *\r
+ * @see Crypt_AES::encrypt()\r
+ * @var String\r
+ * @access private\r
+ */\r
+ var $enmcrypt;\r
+\r
+ /**\r
+ * mcrypt resource for decryption\r
+ *\r
+ * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
+ * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
+ *\r
+ * @see Crypt_AES::decrypt()\r
+ * @var String\r
+ * @access private\r
+ */\r
+ var $demcrypt;\r
+\r
+ /**\r
+ * Default Constructor.\r
+ *\r
+ * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be\r
+ * CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.\r
+ *\r
+ * @param optional Integer $mode\r
+ * @return Crypt_AES\r
+ * @access public\r
+ */\r
+ function Crypt_AES($mode = CRYPT_AES_MODE_CBC)\r
+ {\r
+ if ( !defined('CRYPT_AES_MODE') ) {\r
+ switch (true) {\r
+ case extension_loaded('mcrypt'):\r
+ // i'd check to see if aes was supported, by doing in_array('des', mcrypt_list_algorithms('')),\r
+ // but since that can be changed after the object has been created, there doesn't seem to be\r
+ // a lot of point...\r
+ define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);\r
+ break;\r
+ default:\r
+ define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);\r
+ }\r
+ }\r
+\r
+ switch ( CRYPT_AES_MODE ) {\r
+ case CRYPT_AES_MODE_MCRYPT:\r
+ switch ($mode) {\r
+ case CRYPT_AES_MODE_ECB:\r
+ $this->mode = MCRYPT_MODE_ECB;\r
+ break;\r
+ case CRYPT_AES_MODE_CTR:\r
+ // ctr doesn't have a constant associated with it even though it appears to be fairly widely\r
+ // supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to\r
+ // include a compatibility layer. the layer has been implemented but, for now, is commented out.\r
+ $this->mode = 'ctr';\r
+ //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;\r
+ break;\r
+ case CRYPT_AES_MODE_CBC:\r
+ default:\r
+ $this->mode = MCRYPT_MODE_CBC;\r
+ }\r
+\r
+ break;\r
+ default:\r
+ switch ($mode) {\r
+ case CRYPT_AES_MODE_ECB:\r
+ $this->mode = CRYPT_RIJNDAEL_MODE_ECB;\r
+ break;\r
+ case CRYPT_AES_MODE_CTR:\r
+ $this->mode = CRYPT_RIJNDAEL_MODE_CTR;\r
+ break;\r
+ case CRYPT_AES_MODE_CBC:\r
+ default:\r
+ $this->mode = CRYPT_RIJNDAEL_MODE_CBC;\r
+ }\r
+ }\r
+\r
+ if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {\r
+ parent::Crypt_Rijndael($this->mode);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Dummy function\r
+ *\r
+ * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.\r
+ *\r
+ * @access public\r
+ * @param Integer $length\r
+ */\r
+ function setBlockLength($length)\r
+ {\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * Encrypts a message.\r
+ *\r
+ * $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the\r
+ * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following\r
+ * URL:\r
+ *\r
+ * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}\r
+ *\r
+ * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.\r
+ * strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that\r
+ * length.\r
+ *\r
+ * @see Crypt_AES::decrypt()\r
+ * @access public\r
+ * @param String $plaintext\r
+ */\r
+ function encrypt($plaintext)\r
+ {\r
+ if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {\r
+ $this->_mcryptSetup();\r
+ /*\r
+ if ($this->mode == CRYPT_AES_MODE_CTR) {\r
+ $iv = $this->encryptIV;\r
+ $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));\r
+ $ciphertext = $plaintext ^ $xor;\r
+ if ($this->continuousBuffer) {\r
+ $this->encryptIV = $iv;\r
+ }\r
+ return $ciphertext;\r
+ }\r
+ */\r
+\r
+ if ($this->mode != 'ctr') {\r
+ $plaintext = $this->_pad($plaintext);\r
+ }\r
+\r
+ $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);\r
+\r
+ if (!$this->continuousBuffer) {\r
+ mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);\r
+ }\r
+\r
+ return $ciphertext;\r
+ }\r
+\r
+ return parent::encrypt($plaintext);\r
+ }\r
+\r
+ /**\r
+ * Decrypts a message.\r
+ *\r
+ * If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.\r
+ *\r
+ * @see Crypt_AES::encrypt()\r
+ * @access public\r
+ * @param String $ciphertext\r
+ */\r
+ function decrypt($ciphertext)\r
+ {\r
+ if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {\r
+ $this->_mcryptSetup();\r
+ /*\r
+ if ($this->mode == CRYPT_AES_MODE_CTR) {\r
+ $iv = $this->decryptIV;\r
+ $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));\r
+ $plaintext = $ciphertext ^ $xor;\r
+ if ($this->continuousBuffer) {\r
+ $this->decryptIV = $iv;\r
+ }\r
+ return $plaintext;\r
+ }\r
+ */\r
+\r
+ if ($this->mode != 'ctr') {\r
+ // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :\r
+ // "The data is padded with "\0" to make sure the length of the data is n * blocksize."\r
+ $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));\r
+ }\r
+\r
+ $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);\r
+\r
+ if (!$this->continuousBuffer) {\r
+ mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);\r
+ }\r
+\r
+ return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;\r
+ }\r
+\r
+ return parent::decrypt($ciphertext);\r
+ }\r
+\r
+ /**\r
+ * Setup mcrypt\r
+ *\r
+ * Validates all the variables.\r
+ *\r
+ * @access private\r
+ */\r
+ function _mcryptSetup()\r
+ {\r
+ if (!$this->changed) {\r
+ return;\r
+ }\r
+\r
+ if (!$this->explicit_key_length) {\r
+ // this just copied from Crypt_Rijndael::_setup()\r
+ $length = strlen($this->key) >> 2;\r
+ if ($length > 8) {\r
+ $length = 8;\r
+ } else if ($length < 4) {\r
+ $length = 4;\r
+ }\r
+ $this->Nk = $length;\r
+ $this->key_size = $length << 2;\r
+ }\r
+\r
+ switch ($this->Nk) {\r
+ case 4: // 128\r
+ $this->key_size = 16;\r
+ break;\r
+ case 5: // 160\r
+ case 6: // 192\r
+ $this->key_size = 24;\r
+ break;\r
+ case 7: // 224\r
+ case 8: // 256\r
+ $this->key_size = 32;\r
+ }\r
+\r
+ $this->key = substr($this->key, 0, $this->key_size);\r
+ $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));\r
+\r
+ if (!isset($this->enmcrypt)) {\r
+ $mode = $this->mode;\r
+ //$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;\r
+\r
+ $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');\r
+ $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');\r
+ } // else should mcrypt_generic_deinit be called?\r
+\r
+ mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);\r
+ mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);\r
+\r
+ $this->changed = false;\r
+ }\r
+\r
+ /**\r
+ * Encrypts a block\r
+ *\r
+ * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.\r
+ *\r
+ * @see Crypt_Rijndael::_encryptBlock()\r
+ * @access private\r
+ * @param String $in\r
+ * @return String\r
+ */\r
+ function _encryptBlock($in)\r
+ {\r
+ $state = unpack('N*word', $in);\r
+\r
+ $Nr = $this->Nr;\r
+ $w = $this->w;\r
+ $t0 = $this->t0;\r
+ $t1 = $this->t1;\r
+ $t2 = $this->t2;\r
+ $t3 = $this->t3;\r
+\r
+ // addRoundKey and reindex $state\r
+ $state = array(\r
+ $state['word1'] ^ $w[0][0],\r
+ $state['word2'] ^ $w[0][1],\r
+ $state['word3'] ^ $w[0][2],\r
+ $state['word4'] ^ $w[0][3]\r
+ );\r
+\r
+ // shiftRows + subWord + mixColumns + addRoundKey\r
+ // we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields\r
+ // only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.\r
+ for ($round = 1; $round < $this->Nr; $round++) {\r
+ $state = array(\r
+ $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],\r
+ $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],\r
+ $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],\r
+ $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]\r
+ );\r
+\r
+ }\r
+\r
+ // subWord\r
+ $state = array(\r
+ $this->_subWord($state[0]),\r
+ $this->_subWord($state[1]),\r
+ $this->_subWord($state[2]),\r
+ $this->_subWord($state[3])\r
+ );\r
+\r
+ // shiftRows + addRoundKey\r
+ $state = array(\r
+ ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],\r
+ ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],\r
+ ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],\r
+ ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]\r
+ );\r
+\r
+ return pack('N*', $state[0], $state[1], $state[2], $state[3]);\r
+ }\r
+\r
+ /**\r
+ * Decrypts a block\r
+ *\r
+ * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.\r
+ *\r
+ * @see Crypt_Rijndael::_decryptBlock()\r
+ * @access private\r
+ * @param String $in\r
+ * @return String\r
+ */\r
+ function _decryptBlock($in)\r
+ {\r
+ $state = unpack('N*word', $in);\r
+\r
+ $Nr = $this->Nr;\r
+ $dw = $this->dw;\r
+ $dt0 = $this->dt0;\r
+ $dt1 = $this->dt1;\r
+ $dt2 = $this->dt2;\r
+ $dt3 = $this->dt3;\r
+\r
+ // addRoundKey and reindex $state\r
+ $state = array(\r
+ $state['word1'] ^ $dw[$this->Nr][0],\r
+ $state['word2'] ^ $dw[$this->Nr][1],\r
+ $state['word3'] ^ $dw[$this->Nr][2],\r
+ $state['word4'] ^ $dw[$this->Nr][3]\r
+ );\r
+\r
+\r
+ // invShiftRows + invSubBytes + invMixColumns + addRoundKey\r
+ for ($round = $this->Nr - 1; $round > 0; $round--) {\r
+ $state = array(\r
+ $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],\r
+ $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],\r
+ $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],\r
+ $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]\r
+ );\r
+ }\r
+\r
+ // invShiftRows + invSubWord + addRoundKey\r
+ $state = array(\r
+ $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],\r
+ $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],\r
+ $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],\r
+ $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]\r
+ );\r
+\r
+ return pack('N*', $state[0], $state[1], $state[2], $state[3]);\r
+ }\r
+}\r
+\r
+// vim: ts=4:sw=4:et:\r
+// vim6: fdl=1:
\ No newline at end of file