updating phpseclib to latest cvs - fixes a bunch of key generation issues
This commit is contained in:
parent
c5bb41176e
commit
520faaf67d
@ -1,421 +1,479 @@
|
|||||||
<?php
|
<?php
|
||||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pure-PHP implementation of AES.
|
* Pure-PHP implementation of AES.
|
||||||
*
|
*
|
||||||
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
||||||
*
|
*
|
||||||
* PHP versions 4 and 5
|
* PHP versions 4 and 5
|
||||||
*
|
*
|
||||||
* If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
|
* 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
|
* {@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()}
|
* 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.
|
* 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
|
* 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,
|
* 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).
|
* 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:
|
* Here's a short example of how to use this library:
|
||||||
* <code>
|
* <code>
|
||||||
* <?php
|
* <?php
|
||||||
* include('Crypt/AES.php');
|
* include('Crypt/AES.php');
|
||||||
*
|
*
|
||||||
* $aes = new Crypt_AES();
|
* $aes = new Crypt_AES();
|
||||||
*
|
*
|
||||||
* $aes->setKey('abcdefghijklmnop');
|
* $aes->setKey('abcdefghijklmnop');
|
||||||
*
|
*
|
||||||
* $size = 10 * 1024;
|
* $size = 10 * 1024;
|
||||||
* $plaintext = '';
|
* $plaintext = '';
|
||||||
* for ($i = 0; $i < $size; $i++) {
|
* for ($i = 0; $i < $size; $i++) {
|
||||||
* $plaintext.= 'a';
|
* $plaintext.= 'a';
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* echo $aes->decrypt($aes->encrypt($plaintext));
|
* echo $aes->decrypt($aes->encrypt($plaintext));
|
||||||
* ?>
|
* ?>
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* LICENSE: This library is free software; you can redistribute it and/or
|
* LICENSE: This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||||
* MA 02111-1307 USA
|
* MA 02111-1307 USA
|
||||||
*
|
*
|
||||||
* @category Crypt
|
* @category Crypt
|
||||||
* @package Crypt_AES
|
* @package Crypt_AES
|
||||||
* @author Jim Wigginton <terrafrost@php.net>
|
* @author Jim Wigginton <terrafrost@php.net>
|
||||||
* @copyright MMVIII Jim Wigginton
|
* @copyright MMVIII Jim Wigginton
|
||||||
* @license http://www.gnu.org/licenses/lgpl.txt
|
* @license http://www.gnu.org/licenses/lgpl.txt
|
||||||
* @version $Id: AES.php,v 1.5 2009/11/23 19:06:06 terrafrost Exp $
|
* @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $
|
||||||
* @link http://phpseclib.sourceforge.net
|
* @link http://phpseclib.sourceforge.net
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Include Crypt_Rijndael
|
* Include Crypt_Rijndael
|
||||||
*/
|
*/
|
||||||
require_once 'Rijndael.php';
|
require_once 'Rijndael.php';
|
||||||
|
|
||||||
/**#@+
|
/**#@+
|
||||||
* @access public
|
* @access public
|
||||||
* @see Crypt_AES::encrypt()
|
* @see Crypt_AES::encrypt()
|
||||||
* @see Crypt_AES::decrypt()
|
* @see Crypt_AES::decrypt()
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Encrypt / decrypt using the Electronic Code Book mode.
|
* Encrypt / decrypt using the Counter mode.
|
||||||
*
|
*
|
||||||
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
|
* Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
|
||||||
*/
|
*
|
||||||
define('CRYPT_AES_MODE_ECB', 1);
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
|
||||||
/**
|
*/
|
||||||
* Encrypt / decrypt using the Code Book Chaining mode.
|
define('CRYPT_AES_MODE_CTR', -1);
|
||||||
*
|
/**
|
||||||
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
|
* Encrypt / decrypt using the Electronic Code Book mode.
|
||||||
*/
|
*
|
||||||
define('CRYPT_AES_MODE_CBC', 2);
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
|
||||||
/**#@-*/
|
*/
|
||||||
|
define('CRYPT_AES_MODE_ECB', 1);
|
||||||
/**#@+
|
/**
|
||||||
* @access private
|
* Encrypt / decrypt using the Code Book Chaining mode.
|
||||||
* @see Crypt_AES::Crypt_AES()
|
*
|
||||||
*/
|
* @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
|
||||||
/**
|
*/
|
||||||
* Toggles the internal implementation
|
define('CRYPT_AES_MODE_CBC', 2);
|
||||||
*/
|
/**#@-*/
|
||||||
define('CRYPT_AES_MODE_INTERNAL', 1);
|
|
||||||
/**
|
/**#@+
|
||||||
* Toggles the mcrypt implementation
|
* @access private
|
||||||
*/
|
* @see Crypt_AES::Crypt_AES()
|
||||||
define('CRYPT_AES_MODE_MCRYPT', 2);
|
*/
|
||||||
/**#@-*/
|
/**
|
||||||
|
* Toggles the internal implementation
|
||||||
/**
|
*/
|
||||||
* Pure-PHP implementation of AES.
|
define('CRYPT_AES_MODE_INTERNAL', 1);
|
||||||
*
|
/**
|
||||||
* @author Jim Wigginton <terrafrost@php.net>
|
* Toggles the mcrypt implementation
|
||||||
* @version 0.1.0
|
*/
|
||||||
* @access public
|
define('CRYPT_AES_MODE_MCRYPT', 2);
|
||||||
* @package Crypt_AES
|
/**#@-*/
|
||||||
*/
|
|
||||||
class Crypt_AES extends Crypt_Rijndael {
|
/**
|
||||||
/**
|
* Pure-PHP implementation of AES.
|
||||||
* MCrypt parameters
|
*
|
||||||
*
|
* @author Jim Wigginton <terrafrost@php.net>
|
||||||
* @see Crypt_AES::setMCrypt()
|
* @version 0.1.0
|
||||||
* @var Array
|
* @access public
|
||||||
* @access private
|
* @package Crypt_AES
|
||||||
*/
|
*/
|
||||||
var $mcrypt = array('', '');
|
class Crypt_AES extends Crypt_Rijndael {
|
||||||
|
/**
|
||||||
/**
|
* mcrypt resource for encryption
|
||||||
* Default Constructor.
|
*
|
||||||
*
|
* The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
|
||||||
* Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
|
* Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
|
||||||
* CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
|
*
|
||||||
*
|
* @see Crypt_AES::encrypt()
|
||||||
* @param optional Integer $mode
|
* @var String
|
||||||
* @return Crypt_AES
|
* @access private
|
||||||
* @access public
|
*/
|
||||||
*/
|
var $enmcrypt;
|
||||||
function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
|
|
||||||
{
|
/**
|
||||||
if ( !defined('CRYPT_AES_MODE') ) {
|
* mcrypt resource for decryption
|
||||||
switch (true) {
|
*
|
||||||
case extension_loaded('mcrypt'):
|
* The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
|
||||||
// i'd check to see if aes was supported, by doing in_array('des', mcrypt_list_algorithms('')),
|
* Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
|
||||||
// but since that can be changed after the object has been created, there doesn't seem to be
|
*
|
||||||
// a lot of point...
|
* @see Crypt_AES::decrypt()
|
||||||
define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
|
* @var String
|
||||||
break;
|
* @access private
|
||||||
default:
|
*/
|
||||||
define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
|
var $demcrypt;
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* Default Constructor.
|
||||||
switch ( CRYPT_AES_MODE ) {
|
*
|
||||||
case CRYPT_AES_MODE_MCRYPT:
|
* Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
|
||||||
switch ($mode) {
|
* CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
|
||||||
case CRYPT_AES_MODE_ECB:
|
*
|
||||||
$this->mode = MCRYPT_MODE_ECB;
|
* @param optional Integer $mode
|
||||||
break;
|
* @return Crypt_AES
|
||||||
case CRYPT_AES_MODE_CBC:
|
* @access public
|
||||||
default:
|
*/
|
||||||
$this->mode = MCRYPT_MODE_CBC;
|
function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
|
||||||
}
|
{
|
||||||
|
if ( !defined('CRYPT_AES_MODE') ) {
|
||||||
break;
|
switch (true) {
|
||||||
default:
|
case extension_loaded('mcrypt'):
|
||||||
switch ($mode) {
|
// i'd check to see if aes was supported, by doing in_array('des', mcrypt_list_algorithms('')),
|
||||||
case CRYPT_AES_MODE_ECB:
|
// but since that can be changed after the object has been created, there doesn't seem to be
|
||||||
$this->mode = CRYPT_RIJNDAEL_MODE_ECB;
|
// a lot of point...
|
||||||
break;
|
define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
|
||||||
case CRYPT_AES_MODE_CBC:
|
break;
|
||||||
default:
|
default:
|
||||||
$this->mode = CRYPT_RIJNDAEL_MODE_CBC;
|
define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
|
switch ( CRYPT_AES_MODE ) {
|
||||||
parent::Crypt_Rijndael($this->mode);
|
case CRYPT_AES_MODE_MCRYPT:
|
||||||
}
|
switch ($mode) {
|
||||||
}
|
case CRYPT_AES_MODE_ECB:
|
||||||
|
$this->mode = MCRYPT_MODE_ECB;
|
||||||
/**
|
break;
|
||||||
* Dummy function
|
case CRYPT_AES_MODE_CTR:
|
||||||
*
|
// ctr doesn't have a constant associated with it even though it appears to be fairly widely
|
||||||
* Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
|
// supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to
|
||||||
*
|
// include a compatibility layer. the layer has been implemented but, for now, is commented out.
|
||||||
* @access public
|
$this->mode = 'ctr';
|
||||||
* @param Integer $length
|
//$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;
|
||||||
*/
|
break;
|
||||||
function setBlockLength($length)
|
case CRYPT_AES_MODE_CBC:
|
||||||
{
|
default:
|
||||||
return;
|
$this->mode = MCRYPT_MODE_CBC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
break;
|
||||||
* Encrypts a message.
|
default:
|
||||||
*
|
switch ($mode) {
|
||||||
* $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
|
case CRYPT_AES_MODE_ECB:
|
||||||
* same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
|
$this->mode = CRYPT_RIJNDAEL_MODE_ECB;
|
||||||
* URL:
|
break;
|
||||||
*
|
case CRYPT_AES_MODE_CTR:
|
||||||
* {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
|
$this->mode = CRYPT_RIJNDAEL_MODE_CTR;
|
||||||
*
|
break;
|
||||||
* An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
|
case CRYPT_AES_MODE_CBC:
|
||||||
* strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
|
default:
|
||||||
* length.
|
$this->mode = CRYPT_RIJNDAEL_MODE_CBC;
|
||||||
*
|
}
|
||||||
* @see Crypt_AES::decrypt()
|
}
|
||||||
* @access public
|
|
||||||
* @param String $plaintext
|
if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
|
||||||
*/
|
parent::Crypt_Rijndael($this->mode);
|
||||||
function encrypt($plaintext)
|
}
|
||||||
{
|
}
|
||||||
if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
|
|
||||||
$this->_mcryptSetup();
|
/**
|
||||||
$plaintext = $this->_pad($plaintext);
|
* Dummy function
|
||||||
|
*
|
||||||
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
|
* Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
|
||||||
mcrypt_generic_init($td, $this->key, $this->encryptIV);
|
*
|
||||||
|
* @access public
|
||||||
$ciphertext = mcrypt_generic($td, $plaintext);
|
* @param Integer $length
|
||||||
|
*/
|
||||||
mcrypt_generic_deinit($td);
|
function setBlockLength($length)
|
||||||
mcrypt_module_close($td);
|
{
|
||||||
|
return;
|
||||||
if ($this->continuousBuffer) {
|
}
|
||||||
$this->encryptIV = substr($ciphertext, -16);
|
|
||||||
}
|
/**
|
||||||
|
* Encrypts a message.
|
||||||
return $ciphertext;
|
*
|
||||||
}
|
* $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
|
||||||
|
* same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
|
||||||
return parent::encrypt($plaintext);
|
* URL:
|
||||||
}
|
*
|
||||||
|
* {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
|
||||||
/**
|
*
|
||||||
* Decrypts a message.
|
* An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
|
||||||
*
|
* strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
|
||||||
* If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
|
* length.
|
||||||
*
|
*
|
||||||
* @see Crypt_AES::encrypt()
|
* @see Crypt_AES::decrypt()
|
||||||
* @access public
|
* @access public
|
||||||
* @param String $ciphertext
|
* @param String $plaintext
|
||||||
*/
|
*/
|
||||||
function decrypt($ciphertext)
|
function encrypt($plaintext)
|
||||||
{
|
{
|
||||||
// we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
|
if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
|
||||||
// "The data is padded with "\0" to make sure the length of the data is n * blocksize."
|
$this->_mcryptSetup();
|
||||||
$ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
|
/*
|
||||||
|
if ($this->mode == CRYPT_AES_MODE_CTR) {
|
||||||
if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
|
$iv = $this->encryptIV;
|
||||||
$this->_mcryptSetup();
|
$xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));
|
||||||
|
$ciphertext = $plaintext ^ $xor;
|
||||||
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
|
if ($this->continuousBuffer) {
|
||||||
mcrypt_generic_init($td, $this->key, $this->decryptIV);
|
$this->encryptIV = $iv;
|
||||||
|
}
|
||||||
$plaintext = mdecrypt_generic($td, $ciphertext);
|
return $ciphertext;
|
||||||
|
}
|
||||||
mcrypt_generic_deinit($td);
|
*/
|
||||||
mcrypt_module_close($td);
|
|
||||||
|
if ($this->mode != 'ctr') {
|
||||||
if ($this->continuousBuffer) {
|
$plaintext = $this->_pad($plaintext);
|
||||||
$this->decryptIV = substr($ciphertext, -16);
|
}
|
||||||
}
|
|
||||||
|
$ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
|
||||||
return $this->_unpad($plaintext);
|
|
||||||
}
|
if (!$this->continuousBuffer) {
|
||||||
|
mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
|
||||||
return parent::decrypt($ciphertext);
|
}
|
||||||
}
|
|
||||||
|
return $ciphertext;
|
||||||
/**
|
}
|
||||||
* Sets MCrypt parameters. (optional)
|
|
||||||
*
|
return parent::encrypt($plaintext);
|
||||||
* If MCrypt is being used, empty strings will be used, unless otherwise specified.
|
}
|
||||||
*
|
|
||||||
* @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
|
/**
|
||||||
* @access public
|
* Decrypts a message.
|
||||||
* @param optional Integer $algorithm_directory
|
*
|
||||||
* @param optional Integer $mode_directory
|
* If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
|
||||||
*/
|
*
|
||||||
function setMCrypt($algorithm_directory = '', $mode_directory = '')
|
* @see Crypt_AES::encrypt()
|
||||||
{
|
* @access public
|
||||||
$this->mcrypt = array($algorithm_directory, $mode_directory);
|
* @param String $ciphertext
|
||||||
}
|
*/
|
||||||
|
function decrypt($ciphertext)
|
||||||
/**
|
{
|
||||||
* Setup mcrypt
|
if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
|
||||||
*
|
$this->_mcryptSetup();
|
||||||
* Validates all the variables.
|
/*
|
||||||
*
|
if ($this->mode == CRYPT_AES_MODE_CTR) {
|
||||||
* @access private
|
$iv = $this->decryptIV;
|
||||||
*/
|
$xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));
|
||||||
function _mcryptSetup()
|
$plaintext = $ciphertext ^ $xor;
|
||||||
{
|
if ($this->continuousBuffer) {
|
||||||
if (!$this->changed) {
|
$this->decryptIV = $iv;
|
||||||
return;
|
}
|
||||||
}
|
return $plaintext;
|
||||||
|
}
|
||||||
if (!$this->explicit_key_length) {
|
*/
|
||||||
// this just copied from Crypt_Rijndael::_setup()
|
|
||||||
$length = strlen($this->key) >> 2;
|
if ($this->mode != 'ctr') {
|
||||||
if ($length > 8) {
|
// we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
|
||||||
$length = 8;
|
// "The data is padded with "\0" to make sure the length of the data is n * blocksize."
|
||||||
} else if ($length < 4) {
|
$ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
|
||||||
$length = 4;
|
}
|
||||||
}
|
|
||||||
$this->Nk = $length;
|
$plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
|
||||||
$this->key_size = $length << 2;
|
|
||||||
}
|
if (!$this->continuousBuffer) {
|
||||||
|
mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
|
||||||
switch ($this->Nk) {
|
}
|
||||||
case 4: // 128
|
|
||||||
$this->key_size = 16;
|
return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;
|
||||||
break;
|
}
|
||||||
case 5: // 160
|
|
||||||
case 6: // 192
|
return parent::decrypt($ciphertext);
|
||||||
$this->key_size = 24;
|
}
|
||||||
break;
|
|
||||||
case 7: // 224
|
/**
|
||||||
case 8: // 256
|
* Setup mcrypt
|
||||||
$this->key_size = 32;
|
*
|
||||||
}
|
* Validates all the variables.
|
||||||
|
*
|
||||||
$this->key = substr($this->key, 0, $this->key_size);
|
* @access private
|
||||||
$this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
|
*/
|
||||||
|
function _mcryptSetup()
|
||||||
$this->changed = false;
|
{
|
||||||
}
|
if (!$this->changed) {
|
||||||
|
return;
|
||||||
/**
|
}
|
||||||
* Encrypts a block
|
|
||||||
*
|
if (!$this->explicit_key_length) {
|
||||||
* Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
|
// this just copied from Crypt_Rijndael::_setup()
|
||||||
*
|
$length = strlen($this->key) >> 2;
|
||||||
* @see Crypt_Rijndael::_encryptBlock()
|
if ($length > 8) {
|
||||||
* @access private
|
$length = 8;
|
||||||
* @param String $in
|
} else if ($length < 4) {
|
||||||
* @return String
|
$length = 4;
|
||||||
*/
|
}
|
||||||
function _encryptBlock($in)
|
$this->Nk = $length;
|
||||||
{
|
$this->key_size = $length << 2;
|
||||||
$state = unpack('N*word', $in);
|
}
|
||||||
|
|
||||||
// addRoundKey and reindex $state
|
switch ($this->Nk) {
|
||||||
$state = array(
|
case 4: // 128
|
||||||
$state['word1'] ^ $this->w[0][0],
|
$this->key_size = 16;
|
||||||
$state['word2'] ^ $this->w[0][1],
|
break;
|
||||||
$state['word3'] ^ $this->w[0][2],
|
case 5: // 160
|
||||||
$state['word4'] ^ $this->w[0][3]
|
case 6: // 192
|
||||||
);
|
$this->key_size = 24;
|
||||||
|
break;
|
||||||
// shiftRows + subWord + mixColumns + addRoundKey
|
case 7: // 224
|
||||||
// we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
|
case 8: // 256
|
||||||
// only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
|
$this->key_size = 32;
|
||||||
for ($round = 1; $round < $this->Nr; $round++) {
|
}
|
||||||
$state = array(
|
|
||||||
$this->t0[$state[0] & 0xFF000000] ^ $this->t1[$state[1] & 0x00FF0000] ^ $this->t2[$state[2] & 0x0000FF00] ^ $this->t3[$state[3] & 0x000000FF] ^ $this->w[$round][0],
|
$this->key = substr($this->key, 0, $this->key_size);
|
||||||
$this->t0[$state[1] & 0xFF000000] ^ $this->t1[$state[2] & 0x00FF0000] ^ $this->t2[$state[3] & 0x0000FF00] ^ $this->t3[$state[0] & 0x000000FF] ^ $this->w[$round][1],
|
$this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
|
||||||
$this->t0[$state[2] & 0xFF000000] ^ $this->t1[$state[3] & 0x00FF0000] ^ $this->t2[$state[0] & 0x0000FF00] ^ $this->t3[$state[1] & 0x000000FF] ^ $this->w[$round][2],
|
|
||||||
$this->t0[$state[3] & 0xFF000000] ^ $this->t1[$state[0] & 0x00FF0000] ^ $this->t2[$state[1] & 0x0000FF00] ^ $this->t3[$state[2] & 0x000000FF] ^ $this->w[$round][3]
|
if (!isset($this->enmcrypt)) {
|
||||||
);
|
$mode = $this->mode;
|
||||||
|
//$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;
|
||||||
}
|
|
||||||
|
$this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
|
||||||
// subWord
|
$this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
|
||||||
$state = array(
|
} // else should mcrypt_generic_deinit be called?
|
||||||
$this->_subWord($state[0]),
|
|
||||||
$this->_subWord($state[1]),
|
mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
|
||||||
$this->_subWord($state[2]),
|
mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
|
||||||
$this->_subWord($state[3])
|
|
||||||
);
|
$this->changed = false;
|
||||||
|
}
|
||||||
// shiftRows + addRoundKey
|
|
||||||
$state = array(
|
/**
|
||||||
($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
|
* Encrypts a block
|
||||||
($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
|
*
|
||||||
($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
|
* Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
|
||||||
($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
|
*
|
||||||
);
|
* @see Crypt_Rijndael::_encryptBlock()
|
||||||
|
* @access private
|
||||||
return pack('N*', $state[0], $state[1], $state[2], $state[3]);
|
* @param String $in
|
||||||
}
|
* @return String
|
||||||
|
*/
|
||||||
/**
|
function _encryptBlock($in)
|
||||||
* Decrypts a block
|
{
|
||||||
*
|
$state = unpack('N*word', $in);
|
||||||
* Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
|
|
||||||
*
|
$Nr = $this->Nr;
|
||||||
* @see Crypt_Rijndael::_decryptBlock()
|
$w = $this->w;
|
||||||
* @access private
|
$t0 = $this->t0;
|
||||||
* @param String $in
|
$t1 = $this->t1;
|
||||||
* @return String
|
$t2 = $this->t2;
|
||||||
*/
|
$t3 = $this->t3;
|
||||||
function _decryptBlock($in)
|
|
||||||
{
|
// addRoundKey and reindex $state
|
||||||
$state = unpack('N*word', $in);
|
$state = array(
|
||||||
|
$state['word1'] ^ $w[0][0],
|
||||||
// addRoundKey and reindex $state
|
$state['word2'] ^ $w[0][1],
|
||||||
$state = array(
|
$state['word3'] ^ $w[0][2],
|
||||||
$state['word1'] ^ $this->dw[$this->Nr][0],
|
$state['word4'] ^ $w[0][3]
|
||||||
$state['word2'] ^ $this->dw[$this->Nr][1],
|
);
|
||||||
$state['word3'] ^ $this->dw[$this->Nr][2],
|
|
||||||
$state['word4'] ^ $this->dw[$this->Nr][3]
|
// shiftRows + subWord + mixColumns + addRoundKey
|
||||||
);
|
// we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
|
||||||
|
// only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
|
||||||
|
for ($round = 1; $round < $this->Nr; $round++) {
|
||||||
// invShiftRows + invSubBytes + invMixColumns + addRoundKey
|
$state = array(
|
||||||
for ($round = $this->Nr - 1; $round > 0; $round--) {
|
$t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
|
||||||
$state = array(
|
$t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
|
||||||
$this->dt0[$state[0] & 0xFF000000] ^ $this->dt1[$state[3] & 0x00FF0000] ^ $this->dt2[$state[2] & 0x0000FF00] ^ $this->dt3[$state[1] & 0x000000FF] ^ $this->dw[$round][0],
|
$t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
|
||||||
$this->dt0[$state[1] & 0xFF000000] ^ $this->dt1[$state[0] & 0x00FF0000] ^ $this->dt2[$state[3] & 0x0000FF00] ^ $this->dt3[$state[2] & 0x000000FF] ^ $this->dw[$round][1],
|
$t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
|
||||||
$this->dt0[$state[2] & 0xFF000000] ^ $this->dt1[$state[1] & 0x00FF0000] ^ $this->dt2[$state[0] & 0x0000FF00] ^ $this->dt3[$state[3] & 0x000000FF] ^ $this->dw[$round][2],
|
);
|
||||||
$this->dt0[$state[3] & 0xFF000000] ^ $this->dt1[$state[2] & 0x00FF0000] ^ $this->dt2[$state[1] & 0x0000FF00] ^ $this->dt3[$state[0] & 0x000000FF] ^ $this->dw[$round][3]
|
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
// subWord
|
||||||
// invShiftRows + invSubWord + addRoundKey
|
$state = array(
|
||||||
$state = array(
|
$this->_subWord($state[0]),
|
||||||
$this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $this->dw[0][0],
|
$this->_subWord($state[1]),
|
||||||
$this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $this->dw[0][1],
|
$this->_subWord($state[2]),
|
||||||
$this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $this->dw[0][2],
|
$this->_subWord($state[3])
|
||||||
$this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $this->dw[0][3]
|
);
|
||||||
);
|
|
||||||
|
// shiftRows + addRoundKey
|
||||||
return pack('N*', $state[0], $state[1], $state[2], $state[3]);
|
$state = array(
|
||||||
}
|
($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
|
||||||
}
|
($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
|
||||||
|
($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
|
||||||
// vim: ts=4:sw=4:et:
|
($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
|
||||||
|
);
|
||||||
|
|
||||||
|
return pack('N*', $state[0], $state[1], $state[2], $state[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypts a block
|
||||||
|
*
|
||||||
|
* Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
|
||||||
|
*
|
||||||
|
* @see Crypt_Rijndael::_decryptBlock()
|
||||||
|
* @access private
|
||||||
|
* @param String $in
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
function _decryptBlock($in)
|
||||||
|
{
|
||||||
|
$state = unpack('N*word', $in);
|
||||||
|
|
||||||
|
$Nr = $this->Nr;
|
||||||
|
$dw = $this->dw;
|
||||||
|
$dt0 = $this->dt0;
|
||||||
|
$dt1 = $this->dt1;
|
||||||
|
$dt2 = $this->dt2;
|
||||||
|
$dt3 = $this->dt3;
|
||||||
|
|
||||||
|
// addRoundKey and reindex $state
|
||||||
|
$state = array(
|
||||||
|
$state['word1'] ^ $dw[$this->Nr][0],
|
||||||
|
$state['word2'] ^ $dw[$this->Nr][1],
|
||||||
|
$state['word3'] ^ $dw[$this->Nr][2],
|
||||||
|
$state['word4'] ^ $dw[$this->Nr][3]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// invShiftRows + invSubBytes + invMixColumns + addRoundKey
|
||||||
|
for ($round = $this->Nr - 1; $round > 0; $round--) {
|
||||||
|
$state = array(
|
||||||
|
$dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
|
||||||
|
$dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
|
||||||
|
$dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
|
||||||
|
$dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// invShiftRows + invSubWord + addRoundKey
|
||||||
|
$state = array(
|
||||||
|
$this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
|
||||||
|
$this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
|
||||||
|
$this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
|
||||||
|
$this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
|
||||||
|
);
|
||||||
|
|
||||||
|
return pack('N*', $state[0], $state[1], $state[2], $state[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: ts=4:sw=4:et:
|
||||||
// vim6: fdl=1:
|
// vim6: fdl=1:
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,493 +1,493 @@
|
|||||||
<?php
|
<?php
|
||||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pure-PHP implementation of RC4.
|
* Pure-PHP implementation of RC4.
|
||||||
*
|
*
|
||||||
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
* Uses mcrypt, if available, and an internal implementation, otherwise.
|
||||||
*
|
*
|
||||||
* PHP versions 4 and 5
|
* PHP versions 4 and 5
|
||||||
*
|
*
|
||||||
* Useful resources are as follows:
|
* Useful resources are as follows:
|
||||||
*
|
*
|
||||||
* - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
|
* - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
|
||||||
* - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
|
* - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
|
||||||
*
|
*
|
||||||
* RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
|
* RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
|
||||||
* ARCFOUR or ARC4 because RC4 is how it is refered to in the SSH1 specification.
|
* ARCFOUR or ARC4 because RC4 is how it is refered to in the SSH1 specification.
|
||||||
*
|
*
|
||||||
* Here's a short example of how to use this library:
|
* Here's a short example of how to use this library:
|
||||||
* <code>
|
* <code>
|
||||||
* <?php
|
* <?php
|
||||||
* include('Crypt/RC4.php');
|
* include('Crypt/RC4.php');
|
||||||
*
|
*
|
||||||
* $rc4 = new Crypt_RC4();
|
* $rc4 = new Crypt_RC4();
|
||||||
*
|
*
|
||||||
* $rc4->setKey('abcdefgh');
|
* $rc4->setKey('abcdefgh');
|
||||||
*
|
*
|
||||||
* $size = 10 * 1024;
|
* $size = 10 * 1024;
|
||||||
* $plaintext = '';
|
* $plaintext = '';
|
||||||
* for ($i = 0; $i < $size; $i++) {
|
* for ($i = 0; $i < $size; $i++) {
|
||||||
* $plaintext.= 'a';
|
* $plaintext.= 'a';
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* echo $rc4->decrypt($rc4->encrypt($plaintext));
|
* echo $rc4->decrypt($rc4->encrypt($plaintext));
|
||||||
* ?>
|
* ?>
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* LICENSE: This library is free software; you can redistribute it and/or
|
* LICENSE: This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||||
* MA 02111-1307 USA
|
* MA 02111-1307 USA
|
||||||
*
|
*
|
||||||
* @category Crypt
|
* @category Crypt
|
||||||
* @package Crypt_RC4
|
* @package Crypt_RC4
|
||||||
* @author Jim Wigginton <terrafrost@php.net>
|
* @author Jim Wigginton <terrafrost@php.net>
|
||||||
* @copyright MMVII Jim Wigginton
|
* @copyright MMVII Jim Wigginton
|
||||||
* @license http://www.gnu.org/licenses/lgpl.txt
|
* @license http://www.gnu.org/licenses/lgpl.txt
|
||||||
* @version $Id: RC4.php,v 1.8 2009/06/09 04:00:38 terrafrost Exp $
|
* @version $Id: RC4.php,v 1.8 2009/06/09 04:00:38 terrafrost Exp $
|
||||||
* @link http://phpseclib.sourceforge.net
|
* @link http://phpseclib.sourceforge.net
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**#@+
|
/**#@+
|
||||||
* @access private
|
* @access private
|
||||||
* @see Crypt_RC4::Crypt_RC4()
|
* @see Crypt_RC4::Crypt_RC4()
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Toggles the internal implementation
|
* Toggles the internal implementation
|
||||||
*/
|
*/
|
||||||
define('CRYPT_RC4_MODE_INTERNAL', 1);
|
define('CRYPT_RC4_MODE_INTERNAL', 1);
|
||||||
/**
|
/**
|
||||||
* Toggles the mcrypt implementation
|
* Toggles the mcrypt implementation
|
||||||
*/
|
*/
|
||||||
define('CRYPT_RC4_MODE_MCRYPT', 2);
|
define('CRYPT_RC4_MODE_MCRYPT', 2);
|
||||||
/**#@-*/
|
/**#@-*/
|
||||||
|
|
||||||
/**#@+
|
/**#@+
|
||||||
* @access private
|
* @access private
|
||||||
* @see Crypt_RC4::_crypt()
|
* @see Crypt_RC4::_crypt()
|
||||||
*/
|
*/
|
||||||
define('CRYPT_RC4_ENCRYPT', 0);
|
define('CRYPT_RC4_ENCRYPT', 0);
|
||||||
define('CRYPT_RC4_DECRYPT', 1);
|
define('CRYPT_RC4_DECRYPT', 1);
|
||||||
/**#@-*/
|
/**#@-*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pure-PHP implementation of RC4.
|
* Pure-PHP implementation of RC4.
|
||||||
*
|
*
|
||||||
* @author Jim Wigginton <terrafrost@php.net>
|
* @author Jim Wigginton <terrafrost@php.net>
|
||||||
* @version 0.1.0
|
* @version 0.1.0
|
||||||
* @access public
|
* @access public
|
||||||
* @package Crypt_RC4
|
* @package Crypt_RC4
|
||||||
*/
|
*/
|
||||||
class Crypt_RC4 {
|
class Crypt_RC4 {
|
||||||
/**
|
/**
|
||||||
* The Key
|
* The Key
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::setKey()
|
* @see Crypt_RC4::setKey()
|
||||||
* @var String
|
* @var String
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $key = "\0";
|
var $key = "\0";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Key Stream for encryption
|
* The Key Stream for encryption
|
||||||
*
|
*
|
||||||
* If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
|
* If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::setKey()
|
* @see Crypt_RC4::setKey()
|
||||||
* @var Array
|
* @var Array
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $encryptStream = false;
|
var $encryptStream = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Key Stream for decryption
|
* The Key Stream for decryption
|
||||||
*
|
*
|
||||||
* If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
|
* If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::setKey()
|
* @see Crypt_RC4::setKey()
|
||||||
* @var Array
|
* @var Array
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $decryptStream = false;
|
var $decryptStream = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The $i and $j indexes for encryption
|
* The $i and $j indexes for encryption
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::_crypt()
|
* @see Crypt_RC4::_crypt()
|
||||||
* @var Integer
|
* @var Integer
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $encryptIndex = 0;
|
var $encryptIndex = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The $i and $j indexes for decryption
|
* The $i and $j indexes for decryption
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::_crypt()
|
* @see Crypt_RC4::_crypt()
|
||||||
* @var Integer
|
* @var Integer
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $decryptIndex = 0;
|
var $decryptIndex = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MCrypt parameters
|
* MCrypt parameters
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::setMCrypt()
|
* @see Crypt_RC4::setMCrypt()
|
||||||
* @var Array
|
* @var Array
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $mcrypt = array('', '');
|
var $mcrypt = array('', '');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Encryption Algorithm
|
* The Encryption Algorithm
|
||||||
*
|
*
|
||||||
* Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.
|
* Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::Crypt_RC4()
|
* @see Crypt_RC4::Crypt_RC4()
|
||||||
* @var Integer
|
* @var Integer
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $mode;
|
var $mode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default Constructor.
|
* Default Constructor.
|
||||||
*
|
*
|
||||||
* Determines whether or not the mcrypt extension should be used.
|
* Determines whether or not the mcrypt extension should be used.
|
||||||
*
|
*
|
||||||
* @param optional Integer $mode
|
* @param optional Integer $mode
|
||||||
* @return Crypt_RC4
|
* @return Crypt_RC4
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function Crypt_RC4()
|
function Crypt_RC4()
|
||||||
{
|
{
|
||||||
if ( !defined('CRYPT_RC4_MODE') ) {
|
if ( !defined('CRYPT_RC4_MODE') ) {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case extension_loaded('mcrypt') && (defined('MCRYPT_ARCFOUR') || defined('MCRYPT_RC4')):
|
case extension_loaded('mcrypt') && (defined('MCRYPT_ARCFOUR') || defined('MCRYPT_RC4')):
|
||||||
// i'd check to see if rc4 was supported, by doing in_array('arcfour', mcrypt_list_algorithms('')),
|
// i'd check to see if rc4 was supported, by doing in_array('arcfour', mcrypt_list_algorithms('')),
|
||||||
// but since that can be changed after the object has been created, there doesn't seem to be
|
// but since that can be changed after the object has been created, there doesn't seem to be
|
||||||
// a lot of point...
|
// a lot of point...
|
||||||
define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_MCRYPT);
|
define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_MCRYPT);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_INTERNAL);
|
define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_INTERNAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ( CRYPT_RC4_MODE ) {
|
switch ( CRYPT_RC4_MODE ) {
|
||||||
case CRYPT_RC4_MODE_MCRYPT:
|
case CRYPT_RC4_MODE_MCRYPT:
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case defined('MCRYPT_ARCFOUR'):
|
case defined('MCRYPT_ARCFOUR'):
|
||||||
$this->mode = MCRYPT_ARCFOUR;
|
$this->mode = MCRYPT_ARCFOUR;
|
||||||
break;
|
break;
|
||||||
case defined('MCRYPT_RC4');
|
case defined('MCRYPT_RC4');
|
||||||
$this->mode = MCRYPT_RC4;
|
$this->mode = MCRYPT_RC4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the key.
|
* Sets the key.
|
||||||
*
|
*
|
||||||
* Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
|
* Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
|
||||||
* be used. If no key is explicitly set, it'll be assumed to be a single null byte.
|
* be used. If no key is explicitly set, it'll be assumed to be a single null byte.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param String $key
|
* @param String $key
|
||||||
*/
|
*/
|
||||||
function setKey($key)
|
function setKey($key)
|
||||||
{
|
{
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
|
|
||||||
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$keyLength = strlen($key);
|
$keyLength = strlen($key);
|
||||||
$keyStream = array();
|
$keyStream = array();
|
||||||
for ($i = 0; $i < 256; $i++) {
|
for ($i = 0; $i < 256; $i++) {
|
||||||
$keyStream[$i] = $i;
|
$keyStream[$i] = $i;
|
||||||
}
|
}
|
||||||
$j = 0;
|
$j = 0;
|
||||||
for ($i = 0; $i < 256; $i++) {
|
for ($i = 0; $i < 256; $i++) {
|
||||||
$j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
|
$j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
|
||||||
$temp = $keyStream[$i];
|
$temp = $keyStream[$i];
|
||||||
$keyStream[$i] = $keyStream[$j];
|
$keyStream[$i] = $keyStream[$j];
|
||||||
$keyStream[$j] = $temp;
|
$keyStream[$j] = $temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->encryptIndex = $this->decryptIndex = array(0, 0);
|
$this->encryptIndex = $this->decryptIndex = array(0, 0);
|
||||||
$this->encryptStream = $this->decryptStream = $keyStream;
|
$this->encryptStream = $this->decryptStream = $keyStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy function.
|
* Dummy function.
|
||||||
*
|
*
|
||||||
* Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
|
* Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
|
||||||
* If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
|
* If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
|
||||||
* calling setKey().
|
* calling setKey().
|
||||||
*
|
*
|
||||||
* [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
|
* [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
|
||||||
* the IV's are relatively easy to predict, an attack described by
|
* the IV's are relatively easy to predict, an attack described by
|
||||||
* {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
|
* {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
|
||||||
* can be used to quickly guess at the rest of the key. The following links elaborate:
|
* can be used to quickly guess at the rest of the key. The following links elaborate:
|
||||||
*
|
*
|
||||||
* {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
|
* {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
|
||||||
* {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
|
* {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
|
||||||
*
|
*
|
||||||
* @param String $iv
|
* @param String $iv
|
||||||
* @see Crypt_RC4::setKey()
|
* @see Crypt_RC4::setKey()
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function setIV($iv)
|
function setIV($iv)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets MCrypt parameters. (optional)
|
* Sets MCrypt parameters. (optional)
|
||||||
*
|
*
|
||||||
* If MCrypt is being used, empty strings will be used, unless otherwise specified.
|
* If MCrypt is being used, empty strings will be used, unless otherwise specified.
|
||||||
*
|
*
|
||||||
* @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
|
* @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
|
||||||
* @access public
|
* @access public
|
||||||
* @param optional Integer $algorithm_directory
|
* @param optional Integer $algorithm_directory
|
||||||
* @param optional Integer $mode_directory
|
* @param optional Integer $mode_directory
|
||||||
*/
|
*/
|
||||||
function setMCrypt($algorithm_directory = '', $mode_directory = '')
|
function setMCrypt($algorithm_directory = '', $mode_directory = '')
|
||||||
{
|
{
|
||||||
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
||||||
$this->mcrypt = array($algorithm_directory, $mode_directory);
|
$this->mcrypt = array($algorithm_directory, $mode_directory);
|
||||||
$this->_closeMCrypt();
|
$this->_closeMCrypt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts a message.
|
* Encrypts a message.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::_crypt()
|
* @see Crypt_RC4::_crypt()
|
||||||
* @access public
|
* @access public
|
||||||
* @param String $plaintext
|
* @param String $plaintext
|
||||||
*/
|
*/
|
||||||
function encrypt($plaintext)
|
function encrypt($plaintext)
|
||||||
{
|
{
|
||||||
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
|
return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts a message.
|
* Decrypts a message.
|
||||||
*
|
*
|
||||||
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
|
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
|
||||||
* Atleast if the continuous buffer is disabled.
|
* Atleast if the continuous buffer is disabled.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::_crypt()
|
* @see Crypt_RC4::_crypt()
|
||||||
* @access public
|
* @access public
|
||||||
* @param String $ciphertext
|
* @param String $ciphertext
|
||||||
*/
|
*/
|
||||||
function decrypt($ciphertext)
|
function decrypt($ciphertext)
|
||||||
{
|
{
|
||||||
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
|
return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts or decrypts a message.
|
* Encrypts or decrypts a message.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::encrypt()
|
* @see Crypt_RC4::encrypt()
|
||||||
* @see Crypt_RC4::decrypt()
|
* @see Crypt_RC4::decrypt()
|
||||||
* @access private
|
* @access private
|
||||||
* @param String $text
|
* @param String $text
|
||||||
* @param Integer $mode
|
* @param Integer $mode
|
||||||
*/
|
*/
|
||||||
function _crypt($text, $mode)
|
function _crypt($text, $mode)
|
||||||
{
|
{
|
||||||
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
||||||
$keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
|
$keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
|
||||||
|
|
||||||
if ($this->$keyStream === false) {
|
if ($this->$keyStream === false) {
|
||||||
$this->$keyStream = mcrypt_module_open($this->mode, $this->mcrypt[0], MCRYPT_MODE_STREAM, $this->mcrypt[1]);
|
$this->$keyStream = mcrypt_module_open($this->mode, $this->mcrypt[0], MCRYPT_MODE_STREAM, $this->mcrypt[1]);
|
||||||
mcrypt_generic_init($this->$keyStream, $this->key, '');
|
mcrypt_generic_init($this->$keyStream, $this->key, '');
|
||||||
} else if (!$this->continuousBuffer) {
|
} else if (!$this->continuousBuffer) {
|
||||||
mcrypt_generic_init($this->$keyStream, $this->key, '');
|
mcrypt_generic_init($this->$keyStream, $this->key, '');
|
||||||
}
|
}
|
||||||
$newText = mcrypt_generic($this->$keyStream, $text);
|
$newText = mcrypt_generic($this->$keyStream, $text);
|
||||||
if (!$this->continuousBuffer) {
|
if (!$this->continuousBuffer) {
|
||||||
mcrypt_generic_deinit($this->$keyStream);
|
mcrypt_generic_deinit($this->$keyStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $newText;
|
return $newText;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->encryptStream === false) {
|
if ($this->encryptStream === false) {
|
||||||
$this->setKey($this->key);
|
$this->setKey($this->key);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($mode) {
|
switch ($mode) {
|
||||||
case CRYPT_RC4_ENCRYPT:
|
case CRYPT_RC4_ENCRYPT:
|
||||||
$keyStream = $this->encryptStream;
|
$keyStream = $this->encryptStream;
|
||||||
list($i, $j) = $this->encryptIndex;
|
list($i, $j) = $this->encryptIndex;
|
||||||
break;
|
break;
|
||||||
case CRYPT_RC4_DECRYPT:
|
case CRYPT_RC4_DECRYPT:
|
||||||
$keyStream = $this->decryptStream;
|
$keyStream = $this->decryptStream;
|
||||||
list($i, $j) = $this->decryptIndex;
|
list($i, $j) = $this->decryptIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
$newText = '';
|
$newText = '';
|
||||||
for ($k = 0; $k < strlen($text); $k++) {
|
for ($k = 0; $k < strlen($text); $k++) {
|
||||||
$i = ($i + 1) & 255;
|
$i = ($i + 1) & 255;
|
||||||
$j = ($j + $keyStream[$i]) & 255;
|
$j = ($j + $keyStream[$i]) & 255;
|
||||||
$temp = $keyStream[$i];
|
$temp = $keyStream[$i];
|
||||||
$keyStream[$i] = $keyStream[$j];
|
$keyStream[$i] = $keyStream[$j];
|
||||||
$keyStream[$j] = $temp;
|
$keyStream[$j] = $temp;
|
||||||
$temp = $keyStream[($keyStream[$i] + $keyStream[$j]) & 255];
|
$temp = $keyStream[($keyStream[$i] + $keyStream[$j]) & 255];
|
||||||
$newText.= chr(ord($text[$k]) ^ $temp);
|
$newText.= chr(ord($text[$k]) ^ $temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->continuousBuffer) {
|
if ($this->continuousBuffer) {
|
||||||
switch ($mode) {
|
switch ($mode) {
|
||||||
case CRYPT_RC4_ENCRYPT:
|
case CRYPT_RC4_ENCRYPT:
|
||||||
$this->encryptStream = $keyStream;
|
$this->encryptStream = $keyStream;
|
||||||
$this->encryptIndex = array($i, $j);
|
$this->encryptIndex = array($i, $j);
|
||||||
break;
|
break;
|
||||||
case CRYPT_RC4_DECRYPT:
|
case CRYPT_RC4_DECRYPT:
|
||||||
$this->decryptStream = $keyStream;
|
$this->decryptStream = $keyStream;
|
||||||
$this->decryptIndex = array($i, $j);
|
$this->decryptIndex = array($i, $j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $newText;
|
return $newText;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Treat consecutive "packets" as if they are a continuous buffer.
|
* Treat consecutive "packets" as if they are a continuous buffer.
|
||||||
*
|
*
|
||||||
* Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
|
* Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
|
||||||
* will yield different outputs:
|
* will yield different outputs:
|
||||||
*
|
*
|
||||||
* <code>
|
* <code>
|
||||||
* echo $rc4->encrypt(substr($plaintext, 0, 8));
|
* echo $rc4->encrypt(substr($plaintext, 0, 8));
|
||||||
* echo $rc4->encrypt(substr($plaintext, 8, 8));
|
* echo $rc4->encrypt(substr($plaintext, 8, 8));
|
||||||
* </code>
|
* </code>
|
||||||
* <code>
|
* <code>
|
||||||
* echo $rc4->encrypt($plaintext);
|
* echo $rc4->encrypt($plaintext);
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
|
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
|
||||||
* another, as demonstrated with the following:
|
* another, as demonstrated with the following:
|
||||||
*
|
*
|
||||||
* <code>
|
* <code>
|
||||||
* $rc4->encrypt(substr($plaintext, 0, 8));
|
* $rc4->encrypt(substr($plaintext, 0, 8));
|
||||||
* echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
* echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
||||||
* </code>
|
* </code>
|
||||||
* <code>
|
* <code>
|
||||||
* echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
* echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
|
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
|
||||||
* outputs. The reason is due to the fact that the initialization vector's change after every encryption /
|
* outputs. The reason is due to the fact that the initialization vector's change after every encryption /
|
||||||
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
|
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
|
||||||
*
|
*
|
||||||
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
|
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
|
||||||
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
|
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
|
||||||
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
|
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
|
||||||
* however, they are also less intuitive and more likely to cause you problems.
|
* however, they are also less intuitive and more likely to cause you problems.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::disableContinuousBuffer()
|
* @see Crypt_RC4::disableContinuousBuffer()
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function enableContinuousBuffer()
|
function enableContinuousBuffer()
|
||||||
{
|
{
|
||||||
$this->continuousBuffer = true;
|
$this->continuousBuffer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Treat consecutive packets as if they are a discontinuous buffer.
|
* Treat consecutive packets as if they are a discontinuous buffer.
|
||||||
*
|
*
|
||||||
* The default behavior.
|
* The default behavior.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::enableContinuousBuffer()
|
* @see Crypt_RC4::enableContinuousBuffer()
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function disableContinuousBuffer()
|
function disableContinuousBuffer()
|
||||||
{
|
{
|
||||||
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
|
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
|
||||||
$this->encryptIndex = $this->decryptIndex = array(0, 0);
|
$this->encryptIndex = $this->decryptIndex = array(0, 0);
|
||||||
$this->setKey($this->key);
|
$this->setKey($this->key);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->continuousBuffer = false;
|
$this->continuousBuffer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy function.
|
* Dummy function.
|
||||||
*
|
*
|
||||||
* Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is
|
* Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is
|
||||||
* included is so that you can switch between a block cipher and a stream cipher transparently.
|
* included is so that you can switch between a block cipher and a stream cipher transparently.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::disablePadding()
|
* @see Crypt_RC4::disablePadding()
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function enablePadding()
|
function enablePadding()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy function.
|
* Dummy function.
|
||||||
*
|
*
|
||||||
* @see Crypt_RC4::enablePadding()
|
* @see Crypt_RC4::enablePadding()
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function disablePadding()
|
function disablePadding()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class destructor.
|
* Class destructor.
|
||||||
*
|
*
|
||||||
* Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really
|
* Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really
|
||||||
* needs to be called if mcrypt is being used.
|
* needs to be called if mcrypt is being used.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function __destruct()
|
function __destruct()
|
||||||
{
|
{
|
||||||
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
|
||||||
$this->_closeMCrypt();
|
$this->_closeMCrypt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Properly close the MCrypt objects.
|
* Properly close the MCrypt objects.
|
||||||
*
|
*
|
||||||
* @access prviate
|
* @access prviate
|
||||||
*/
|
*/
|
||||||
function _closeMCrypt()
|
function _closeMCrypt()
|
||||||
{
|
{
|
||||||
if ( $this->encryptStream !== false ) {
|
if ( $this->encryptStream !== false ) {
|
||||||
if ( $this->continuousBuffer ) {
|
if ( $this->continuousBuffer ) {
|
||||||
mcrypt_generic_deinit($this->encryptStream);
|
mcrypt_generic_deinit($this->encryptStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
mcrypt_module_close($this->encryptStream);
|
mcrypt_module_close($this->encryptStream);
|
||||||
|
|
||||||
$this->encryptStream = false;
|
$this->encryptStream = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $this->decryptStream !== false ) {
|
if ( $this->decryptStream !== false ) {
|
||||||
if ( $this->continuousBuffer ) {
|
if ( $this->continuousBuffer ) {
|
||||||
mcrypt_generic_deinit($this->decryptStream);
|
mcrypt_generic_deinit($this->decryptStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
mcrypt_module_close($this->decryptStream);
|
mcrypt_module_close($this->decryptStream);
|
||||||
|
|
||||||
$this->decryptStream = false;
|
$this->decryptStream = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
@ -1,70 +1,125 @@
|
|||||||
<?php
|
<?php
|
||||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Random Number Generator
|
* Random Number Generator
|
||||||
*
|
*
|
||||||
* PHP versions 4 and 5
|
* PHP versions 4 and 5
|
||||||
*
|
*
|
||||||
* Here's a short example of how to use this library:
|
* Here's a short example of how to use this library:
|
||||||
* <code>
|
* <code>
|
||||||
* <?php
|
* <?php
|
||||||
* include('Crypt/Random.php');
|
* include('Crypt/Random.php');
|
||||||
*
|
*
|
||||||
* echo crypt_random();
|
* echo crypt_random();
|
||||||
* ?>
|
* ?>
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* LICENSE: This library is free software; you can redistribute it and/or
|
* LICENSE: This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||||
* MA 02111-1307 USA
|
* MA 02111-1307 USA
|
||||||
*
|
*
|
||||||
* @category Crypt
|
* @category Crypt
|
||||||
* @package Crypt_Random
|
* @package Crypt_Random
|
||||||
* @author Jim Wigginton <terrafrost@php.net>
|
* @author Jim Wigginton <terrafrost@php.net>
|
||||||
* @copyright MMVII Jim Wigginton
|
* @copyright MMVII Jim Wigginton
|
||||||
* @license http://www.gnu.org/licenses/lgpl.txt
|
* @license http://www.gnu.org/licenses/lgpl.txt
|
||||||
* @version $Id: Random.php,v 1.4 2008/05/21 05:15:32 terrafrost Exp $
|
* @version $Id: Random.php,v 1.6 2010/02/28 05:28:38 terrafrost Exp $
|
||||||
* @link http://phpseclib.sourceforge.net
|
* @link http://phpseclib.sourceforge.net
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random value. Feel free to replace this function with a cryptographically secure PRNG.
|
* Generate a random value.
|
||||||
*
|
*
|
||||||
* @param optional Integer $min
|
* On 32-bit machines, the largest distance that can exist between $min and $max is 2**31.
|
||||||
* @param optional Integer $max
|
* If $min and $max are farther apart than that then the last ($max - range) numbers.
|
||||||
* @param optional String $randomness_path
|
*
|
||||||
* @return Integer
|
* Depending on how this is being used, it may be worth while to write a replacement. For example,
|
||||||
* @access public
|
* a PHP-based web app that stores its data in an SQL database can collect more entropy than this function
|
||||||
*/
|
* can.
|
||||||
function crypt_random($min = 0, $max = 0x7FFFFFFF, $randomness_path = '/dev/urandom')
|
*
|
||||||
{
|
* @param optional Integer $min
|
||||||
static $seeded = false;
|
* @param optional Integer $max
|
||||||
|
* @return Integer
|
||||||
if (!$seeded) {
|
* @access public
|
||||||
$seeded = true;
|
*/
|
||||||
if (file_exists($randomness_path)) {
|
function crypt_random($min = 0, $max = 0x7FFFFFFF)
|
||||||
$fp = fopen($randomness_path, 'r');
|
{
|
||||||
$temp = unpack('Nint', fread($fp, 4));
|
if ($min == $max) {
|
||||||
mt_srand($temp['int']);
|
return $min;
|
||||||
fclose($fp);
|
}
|
||||||
} else {
|
|
||||||
list($sec, $usec) = explode(' ', microtime());
|
// see http://en.wikipedia.org/wiki//dev/random
|
||||||
mt_srand((float) $sec + ((float) $usec * 100000));
|
if (file_exists('/dev/urandom')) {
|
||||||
}
|
$fp = fopen('/dev/urandom', 'rb');
|
||||||
}
|
extract(unpack('Nrandom', fread($fp, 4)));
|
||||||
|
fclose($fp);
|
||||||
return mt_rand($min, $max);
|
|
||||||
}
|
// say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this:
|
||||||
|
// -4 % 3 + 0 = -1, even though -1 < $min
|
||||||
|
return abs($random) % ($max - $min) + $min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called.
|
||||||
|
Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here:
|
||||||
|
|
||||||
|
http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/
|
||||||
|
|
||||||
|
The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro:
|
||||||
|
|
||||||
|
http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */
|
||||||
|
if (version_compare(PHP_VERSION, '5.2.5', '<=')) {
|
||||||
|
static $seeded;
|
||||||
|
if (!isset($seeded)) {
|
||||||
|
$seeded = true;
|
||||||
|
mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static $crypto;
|
||||||
|
|
||||||
|
// The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5
|
||||||
|
// in the browser and reloading the page.
|
||||||
|
|
||||||
|
if (!isset($crypto)) {
|
||||||
|
$key = $iv = '';
|
||||||
|
for ($i = 0; $i < 8; $i++) {
|
||||||
|
$key.= pack('n', mt_rand(0, 0xFFFF));
|
||||||
|
$iv .= pack('n', mt_rand(0, 0xFFFF));
|
||||||
|
}
|
||||||
|
switch (true) {
|
||||||
|
case class_exists('Crypt_AES'):
|
||||||
|
$crypto = new Crypt_AES(CRYPT_AES_MODE_CTR);
|
||||||
|
break;
|
||||||
|
case class_exists('Crypt_TripleDES'):
|
||||||
|
$crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
|
||||||
|
break;
|
||||||
|
case class_exists('Crypt_DES'):
|
||||||
|
$crypto = new Crypt_DES(CRYPT_DES_MODE_CTR);
|
||||||
|
break;
|
||||||
|
case class_exists('Crypt_RC4'):
|
||||||
|
$crypto = new Crypt_RC4();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF)))));
|
||||||
|
return abs($random) % ($max - $min) + $min;
|
||||||
|
}
|
||||||
|
$crypto->setKey($key);
|
||||||
|
$crypto->setIV($iv);
|
||||||
|
}
|
||||||
|
|
||||||
|
extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0")));
|
||||||
|
return abs($random) % ($max - $min) + $min;
|
||||||
|
}
|
||||||
?>
|
?>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user