2010-05-14 14:06:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of AES.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* Uses mcrypt, if available/possible, and an internal implementation, otherwise.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
|
|
|
|
* {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
|
2014-03-30 06:55:33 +00:00
|
|
|
* it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
|
2010-05-14 14:06:17 +00:00
|
|
|
* is called, again, at which point, it'll be recalculated.
|
|
|
|
*
|
|
|
|
* Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
|
|
|
|
* make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
|
|
|
|
* however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
|
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2015-07-06 19:44:12 +00:00
|
|
|
* include 'Crypt/AES.php';
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
|
|
|
* $aes = new Crypt_AES();
|
|
|
|
*
|
|
|
|
* $aes->setKey('abcdefghijklmnop');
|
|
|
|
*
|
|
|
|
* $size = 10 * 1024;
|
|
|
|
* $plaintext = '';
|
|
|
|
* for ($i = 0; $i < $size; $i++) {
|
|
|
|
* $plaintext.= 'a';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $aes->decrypt($aes->encrypt($plaintext));
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2011-05-19 18:02:11 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @category Crypt
|
|
|
|
* @package Crypt_AES
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2015-07-06 19:44:12 +00:00
|
|
|
* @copyright 2008 Jim Wigginton
|
2014-03-30 06:55:33 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Crypt_Rijndael
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
if (!class_exists('Crypt_Rijndael')) {
|
|
|
|
include_once 'Rijndael.php';
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
|
|
|
* @see Crypt_AES::encrypt()
|
|
|
|
* @see Crypt_AES::decrypt()
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Counter mode.
|
|
|
|
*
|
|
|
|
* Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Electronic Code Book mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Code Book Chaining mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
|
2011-05-19 18:02:11 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
|
2011-05-19 18:02:11 +00:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access private
|
2015-07-06 19:44:12 +00:00
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Toggles the internal implementation
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
|
|
|
* Toggles the mcrypt implementation
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
|
2010-05-14 14:06:17 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of AES.
|
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @package Crypt_AES
|
2010-05-14 14:06:17 +00:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
class Crypt_AES extends Crypt_Rijndael
|
|
|
|
{
|
2010-05-14 14:06:17 +00:00
|
|
|
/**
|
2014-03-30 06:55:33 +00:00
|
|
|
* The namespace used by the cipher for its constants.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2014-03-30 06:55:33 +00:00
|
|
|
* @see Crypt_Base::const_namespace
|
2010-05-14 14:06:17 +00:00
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
2014-03-30 06:55:33 +00:00
|
|
|
var $const_namespace = 'AES';
|
2010-05-14 14:06:17 +00:00
|
|
|
|
|
|
|
/**
|
2015-07-06 19:44:12 +00:00
|
|
|
* Dummy function
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* @see Crypt_Rijndael::setBlockLength()
|
|
|
|
* @access public
|
|
|
|
* @param Integer $length
|
|
|
|
*/
|
|
|
|
function setBlockLength($length)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the key length
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
|
|
|
|
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
|
2014-03-30 06:55:33 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* @see Crypt_Rijndael:setKeyLength()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
2015-07-06 19:44:12 +00:00
|
|
|
* @param Integer $length
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
2015-07-06 19:44:12 +00:00
|
|
|
function setKeyLength($length)
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2015-07-06 19:44:12 +00:00
|
|
|
switch ($length) {
|
|
|
|
case 160:
|
|
|
|
$length = 192;
|
|
|
|
break;
|
|
|
|
case 224:
|
|
|
|
$length = 256;
|
|
|
|
}
|
|
|
|
parent::setKeyLength($length);
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-06 19:44:12 +00:00
|
|
|
* Sets the key.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* Rijndael supports five different key lengths, AES only supports three.
|
2010-05-14 14:06:17 +00:00
|
|
|
*
|
2015-07-06 19:44:12 +00:00
|
|
|
* @see Crypt_Rijndael:setKey()
|
|
|
|
* @see setKeyLength()
|
2010-05-14 14:06:17 +00:00
|
|
|
* @access public
|
2015-07-06 19:44:12 +00:00
|
|
|
* @param String $key
|
2010-05-14 14:06:17 +00:00
|
|
|
*/
|
2015-07-06 19:44:12 +00:00
|
|
|
function setKey($key)
|
2010-05-14 14:06:17 +00:00
|
|
|
{
|
2015-07-06 19:44:12 +00:00
|
|
|
parent::setKey($key);
|
|
|
|
|
|
|
|
if (!$this->explicit_key_length) {
|
|
|
|
$length = strlen($key);
|
|
|
|
switch (true) {
|
|
|
|
case $length <= 16:
|
|
|
|
$this->key_size = 16;
|
|
|
|
break;
|
|
|
|
case $length <= 24:
|
|
|
|
$this->key_size = 24;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->key_size = 32;
|
|
|
|
}
|
|
|
|
$this->_setupEngine();
|
|
|
|
}
|
2010-05-14 14:06:17 +00:00
|
|
|
}
|
|
|
|
}
|