2009-02-17 00:11:01 +01:00
|
|
|
<?php
|
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of AES.
|
|
|
|
*
|
|
|
|
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}
|
|
|
|
* 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
|
|
|
|
* include('Crypt/AES.php');
|
|
|
|
*
|
|
|
|
* $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-04-18 14:17:40 +02: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:
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2011-04-18 14:17:40 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2011-04-18 14:17:40 +02: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.
|
2009-02-17 00:11:01 +01:00
|
|
|
*
|
|
|
|
* @category Crypt
|
|
|
|
* @package Crypt_AES
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright MMVIII Jim Wigginton
|
2010-11-13 20:28:20 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $
|
2009-02-17 00:11:01 +01:00
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Crypt_Rijndael
|
|
|
|
*/
|
2012-06-08 21:38:27 +02:00
|
|
|
if (!class_exists('Crypt_Rijndael')) {
|
|
|
|
require_once 'Rijndael.php';
|
|
|
|
}
|
2009-02-17 00:11:01 +01:00
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access public
|
2009-05-27 18:15:23 +02:00
|
|
|
* @see Crypt_AES::encrypt()
|
|
|
|
* @see Crypt_AES::decrypt()
|
2009-02-17 00:11:01 +01:00
|
|
|
*/
|
2010-02-09 07:10:26 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_CTR', CRYPT_MODE_CTR);
|
2009-02-17 00:11:01 +01:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Electronic Code Book mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_ECB', CRYPT_MODE_ECB);
|
2009-02-17 00:11:01 +01: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
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_CBC', CRYPT_MODE_CBC);
|
2010-09-12 23:58:54 +02:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_CFB', CRYPT_MODE_CFB);
|
2010-09-12 23:58:54 +02:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using the Cipher Feedback mode.
|
|
|
|
*
|
|
|
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_OFB', CRYPT_MODE_OFB);
|
2009-02-17 00:11:01 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @access private
|
|
|
|
* @see Crypt_AES::Crypt_AES()
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Toggles the internal implementation
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
|
2009-02-17 00:11:01 +01:00
|
|
|
/**
|
|
|
|
* Toggles the mcrypt implementation
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
|
2009-02-17 00:11:01 +01:00
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of AES.
|
|
|
|
*
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @version 0.1.0
|
|
|
|
* @access public
|
|
|
|
* @package Crypt_AES
|
|
|
|
*/
|
|
|
|
class Crypt_AES extends Crypt_Rijndael {
|
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The namespace used by the cipher for its constants.
|
2010-02-09 07:10:26 +01:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::const_namespace
|
2010-02-09 07:10:26 +01:00
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $const_namespace = 'AES';
|
2010-02-09 07:10:26 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* The mcrypt specific name of the cipher
|
2009-02-17 00:11:01 +01:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::cipher_name_mcrypt
|
2010-02-09 07:10:26 +01:00
|
|
|
* @var String
|
2009-02-17 00:11:01 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-20 08:19:38 +02:00
|
|
|
var $cipher_name_mcrypt = 'rijndael-128';
|
2010-09-26 07:24:52 +02:00
|
|
|
|
2009-02-17 00:11:01 +01:00
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* Determines whether or not the mcrypt extension should be used.
|
2009-02-17 00:11:01 +01:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* $mode could be:
|
|
|
|
* - CRYPT_AES_MODE_ECB
|
|
|
|
* - CRYPT_AES_MODE_CBC
|
|
|
|
* - CRYPT_AES_MODE_CTR
|
|
|
|
* - CRYPT_AES_MODE_CFB
|
|
|
|
* - CRYPT_AES_MODE_OFB
|
|
|
|
*
|
|
|
|
* If not explictly set, CRYPT_AES_MODE_CBC will be used.
|
|
|
|
*
|
|
|
|
* @see Crypt_Rijndael::Crypt_Rijndael()
|
|
|
|
* @see Crypt_Base::Crypt_Base()
|
2009-02-17 00:11:01 +01:00
|
|
|
* @param optional Integer $mode
|
|
|
|
* @access public
|
|
|
|
*/
|
2009-02-26 18:25:03 +01:00
|
|
|
function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
|
2009-02-17 00:11:01 +01:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::Crypt_Rijndael($mode);
|
2013-01-30 18:59:53 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 00:11:01 +01:00
|
|
|
/**
|
|
|
|
* Dummy function
|
|
|
|
*
|
|
|
|
* Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Rijndael::setBlockLength()
|
2009-02-17 00:11:01 +01:00
|
|
|
* @access public
|
|
|
|
* @param Integer $length
|
|
|
|
*/
|
|
|
|
function setBlockLength($length)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-16 07:53:36 +01:00
|
|
|
/**
|
2013-05-20 08:19:38 +02:00
|
|
|
* Setup the CRYPT_MODE_MCRYPT $engine
|
2009-02-17 00:11:01 +01:00
|
|
|
*
|
|
|
|
* Validates all the variables.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::_mcryptSetup()
|
2009-02-17 00:11:01 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _mcryptSetup()
|
|
|
|
{
|
|
|
|
if (!$this->explicit_key_length) {
|
|
|
|
// this just copied from Crypt_Rijndael::_setup()
|
|
|
|
$length = strlen($this->key) >> 2;
|
|
|
|
if ($length > 8) {
|
|
|
|
$length = 8;
|
|
|
|
} else if ($length < 4) {
|
|
|
|
$length = 4;
|
|
|
|
}
|
|
|
|
$this->Nk = $length;
|
|
|
|
$this->key_size = $length << 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->Nk) {
|
|
|
|
case 4: // 128
|
|
|
|
$this->key_size = 16;
|
|
|
|
break;
|
|
|
|
case 5: // 160
|
|
|
|
case 6: // 192
|
|
|
|
$this->key_size = 24;
|
|
|
|
break;
|
|
|
|
case 7: // 224
|
|
|
|
case 8: // 256
|
|
|
|
$this->key_size = 32;
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->password_key_size = $this->key_size;
|
2010-12-02 04:38:22 +01:00
|
|
|
$this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
|
2009-02-17 00:11:01 +01:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::_mcryptSetup();
|
2013-01-10 00:39:57 +01:00
|
|
|
}
|
2009-02-17 00:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: ts=4:sw=4:et:
|
2013-01-21 04:08:49 +01:00
|
|
|
// vim6: fdl=1:
|