1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-11 08:39:43 +01:00
tgseclib/phpseclib/Crypt/RC4.php

305 lines
7.7 KiB
PHP
Raw Normal View History

<?php
/**
* Pure-PHP implementation of RC4.
*
* Uses mcrypt, if available, and an internal implementation, otherwise.
*
2015-04-02 12:57:52 +02:00
* PHP version 5
*
* Useful resources are as follows:
*
* - {@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}
*
* RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
2013-05-08 16:34:07 +02:00
* ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
*
* Here's a short example of how to use this library:
* <code>
* <?php
2014-12-17 01:16:54 +01:00
* include 'vendor/autoload.php';
*
2014-12-17 01:16:54 +01:00
* $rc4 = new \phpseclib\Crypt\RC4();
*
* $rc4->setKey('abcdefgh');
*
* $size = 10 * 1024;
* $plaintext = '';
* for ($i = 0; $i < $size; $i++) {
* $plaintext.= 'a';
* }
*
* echo $rc4->decrypt($rc4->encrypt($plaintext));
* ?>
* </code>
*
* @category Crypt
2014-12-17 01:16:54 +01:00
* @package RC4
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2007 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
2014-12-17 01:16:54 +01:00
namespace phpseclib\Crypt;
use phpseclib\Crypt\Base;
2013-05-20 08:19:38 +02:00
/**
* Pure-PHP implementation of RC4.
*
2014-12-17 01:16:54 +01:00
* @package RC4
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2014-12-17 01:16:54 +01:00
class RC4 extends Base
{
/**#@+
* @access private
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\RC4::_crypt()
*/
const ENCRYPT = 0;
const DECRYPT = 1;
/**#@-*/
/**
2013-05-20 08:19:38 +02:00
* Block Length of the cipher
*
2014-01-18 18:29:25 +01:00
* RC4 is a stream cipher
2013-05-20 08:19:38 +02:00
* so we the block_size to 0
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::block_size
2014-01-18 18:29:25 +01:00
* @var Integer
* @access private
*/
2013-05-20 08:19:38 +02:00
var $block_size = 0;
/**
2013-05-20 08:19:38 +02:00
* The default password key_size used by setPassword()
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::password_key_size
* @see \phpseclib\Crypt\Base::setPassword()
2013-05-20 08:19:38 +02:00
* @var Integer
* @access private
*/
2013-05-20 08:19:38 +02:00
var $password_key_size = 128; // = 1024 bits
/**
2013-05-20 08:19:38 +02:00
* The mcrypt specific name of the cipher
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::cipher_name_mcrypt
2013-05-20 08:19:38 +02:00
* @var String
* @access private
*/
2013-05-20 08:19:38 +02:00
var $cipher_name_mcrypt = 'arcfour';
/**
2013-05-20 08:19:38 +02:00
* Holds whether performance-optimized $inline_crypt() can/should be used.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::inline_crypt
2013-05-20 08:19:38 +02:00
* @var mixed
* @access private
*/
2013-05-20 08:19:38 +02:00
var $use_inline_crypt = false; // currently not available
/**
2013-05-20 08:19:38 +02:00
* The Key
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\RC4::setKey()
2013-05-20 08:19:38 +02:00
* @var String
* @access private
*/
2013-05-20 08:19:38 +02:00
var $key = "\0";
/**
2013-05-20 08:19:38 +02:00
* The Key Stream for decryption and encryption
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\RC4::setKey()
2013-05-20 08:19:38 +02:00
* @var Array
* @access private
*/
2013-05-20 08:19:38 +02:00
var $stream;
/**
* Default Constructor.
*
* Determines whether or not the mcrypt extension should be used.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::__construct()
* @return \phpseclib\Crypt\RC4
* @access public
*/
function __construct()
{
2014-12-17 01:16:54 +01:00
parent::__construct(Base::MODE_STREAM);
}
/**
* Dummy function.
*
* 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
* calling setKey().
*
* [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
* {@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:
*
* {@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}
*
* @param String $iv
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\RC4::setKey()
* @access public
*/
function setIV($iv)
{
}
2013-05-25 04:05:24 +02:00
/**
* Sets the key.
*
* 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.
*
* @access public
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::setKey()
2013-05-25 04:05:24 +02:00
* @param String $key
*/
function setKey($key)
{
parent::setKey(substr($key, 0, 256));
}
/**
* Encrypts a message.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::decrypt()
* @see \phpseclib\Crypt\RC4::_crypt()
* @access public
* @param String $plaintext
2013-05-20 08:19:38 +02:00
* @return String $ciphertext
*/
function encrypt($plaintext)
{
if ($this->engine != Base::ENGINE_INTERNAL) {
2013-05-20 08:19:38 +02:00
return parent::encrypt($plaintext);
}
return $this->_crypt($plaintext, self::ENCRYPT);
}
/**
* Decrypts a message.
*
* $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
* At least if the continuous buffer is disabled.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::encrypt()
* @see \phpseclib\Crypt\RC4::_crypt()
* @access public
* @param String $ciphertext
2013-05-20 08:19:38 +02:00
* @return String $plaintext
*/
function decrypt($ciphertext)
{
if ($this->engine != Base::ENGINE_INTERNAL) {
2013-05-20 08:19:38 +02:00
return parent::decrypt($ciphertext);
}
return $this->_crypt($ciphertext, self::DECRYPT);
}
2014-12-29 21:43:56 +01:00
/**
* Encrypts a block
*
* @access private
* @param String $in
*/
function _encryptBlock($in)
{
// RC4 does not utilize this method
}
/**
* Decrypts a block
*
* @access private
* @param String $in
*/
function _decryptBlock($in)
{
// RC4 does not utilize this method
}
2013-05-25 04:05:24 +02:00
/**
* Setup the key (expansion)
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Base::_setupKey()
2013-05-25 04:05:24 +02:00
* @access private
*/
function _setupKey()
{
$key = $this->key;
$keyLength = strlen($key);
2014-06-16 16:19:14 +02:00
$keyStream = range(0, 255);
2013-05-25 04:05:24 +02:00
$j = 0;
for ($i = 0; $i < 256; $i++) {
$j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
$temp = $keyStream[$i];
$keyStream[$i] = $keyStream[$j];
$keyStream[$j] = $temp;
}
$this->stream = array();
$this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array(
2013-05-25 04:05:24 +02:00
0, // index $i
0, // index $j
$keyStream
);
}
/**
* Encrypts or decrypts a message.
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\RC4::encrypt()
* @see \phpseclib\Crypt\RC4::decrypt()
* @access private
* @param String $text
* @param Integer $mode
2013-05-20 08:19:38 +02:00
* @return String $text
*/
function _crypt($text, $mode)
{
2013-05-20 08:19:38 +02:00
if ($this->changed) {
$this->_setup();
$this->changed = false;
}
2013-05-20 08:19:38 +02:00
$stream = &$this->stream[$mode];
if ($this->continuousBuffer) {
2013-05-20 08:19:38 +02:00
$i = &$stream[0];
$j = &$stream[1];
$keyStream = &$stream[2];
} else {
$i = $stream[0];
$j = $stream[1];
$keyStream = $stream[2];
}
2013-05-20 08:19:38 +02:00
$len = strlen($text);
for ($k = 0; $k < $len; ++$k) {
$i = ($i + 1) & 255;
$ksi = $keyStream[$i];
$j = ($j + $ksi) & 255;
$ksj = $keyStream[$j];
2013-05-20 08:19:38 +02:00
$keyStream[$i] = $ksj;
$keyStream[$j] = $ksi;
2014-06-16 16:19:14 +02:00
$text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
}
2013-05-20 08:19:38 +02:00
return $text;
}
}