1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-15 02:17:05 +01:00
tgseclib/phpseclib/Crypt/RC4.php

342 lines
8.3 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\Common\StreamCipher;
/**
* Pure-PHP implementation of RC4.
*
2014-12-17 01:16:54 +01:00
* @package RC4
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
class RC4 extends StreamCipher
{
/**#@+
* @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
*
* @see \phpseclib\Crypt\Common\SymmetricKey::block_size
* @var int
* @access private
*/
protected $block_size = 0;
/**
* Key Length (in bytes)
*
* @see \phpseclib\Crypt\RC4::setKeyLength()
* @var int
* @access private
*/
protected $key_length = 128; // = 1024 bits
/**
2013-05-20 08:19:38 +02:00
* The mcrypt specific name of the cipher
*
* @see \phpseclib\Crypt\Common\SymmetricKey::cipher_name_mcrypt
* @var string
* @access private
*/
protected $cipher_name_mcrypt = 'arcfour';
/**
2013-05-20 08:19:38 +02:00
* The Key
*
* @see self::setKey()
* @var string
* @access private
*/
protected $key;
/**
2013-05-20 08:19:38 +02:00
* The Key Stream for decryption and encryption
*
* @see self::setKey()
* @var array
* @access private
*/
private $stream;
/**
* Default Constructor.
*
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
2014-12-17 01:16:54 +01:00
* @return \phpseclib\Crypt\RC4
* @access public
*/
public function __construct()
{
parent::__construct('stream');
}
/**
* Test for engine validity
*
* This is mainly just a wrapper to set things up for \phpseclib\Crypt\Common\SymmetricKey::isValidEngine()
*
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
* @param int $engine
* @access protected
* @return bool
*/
protected function isValidEngineHelper($engine)
{
2017-01-27 19:40:23 +01:00
if ($engine == self::ENGINE_OPENSSL) {
2019-03-18 12:59:00 +01:00
if ($this->continuousBuffer) {
return false;
}
if (version_compare(PHP_VERSION, '5.3.7') >= 0) {
$this->cipher_name_openssl = 'rc4-40';
} else {
switch (strlen($this->key)) {
case 5:
$this->cipher_name_openssl = 'rc4-40';
break;
case 8:
$this->cipher_name_openssl = 'rc4-64';
break;
case 16:
$this->cipher_name_openssl = 'rc4';
break;
default:
return false;
}
}
}
return parent::isValidEngineHelper($engine);
}
/**
* RC4 does not use an IV
2013-05-25 04:05:24 +02:00
*
* @access public
* @return bool
2013-05-25 04:05:24 +02:00
*/
public function usesIV()
2013-05-25 04:05:24 +02:00
{
return false;
2013-05-25 04:05:24 +02:00
}
/**
* Sets the key length
*
* Keys can be between 1 and 256 bytes long.
*
* @access public
* @param int $length
* @throws \LengthException if the key length is invalid
*/
public function setKeyLength($length)
{
if ($length < 8 || $length > 2048) {
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported');
}
$this->key_length = $length >> 3;
2016-04-28 22:34:10 +02:00
parent::setKeyLength($length);
}
/**
* Sets the key length
*
* Keys can be between 1 and 256 bytes long.
*
* @access public
2017-10-25 11:44:14 +02:00
* @param string $key
*/
public function setKey($key)
{
$length = strlen($key);
if ($length < 1 || $length > 256) {
throw new \LengthException('Key size of ' . $length . ' bytes is not supported by RC4. Keys must be between 1 and 256 bytes long');
}
parent::setKey($key);
}
/**
* Encrypts a message.
*
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
* @see self::crypt()
* @access public
* @param string $plaintext
* @return string $ciphertext
*/
public function encrypt($plaintext)
{
if ($this->engine != self::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.
*
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
* @see self::crypt()
* @access public
* @param string $ciphertext
* @return string $plaintext
*/
public function decrypt($ciphertext)
{
if ($this->engine != self::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
2017-11-05 21:35:27 +01:00
*
2014-12-29 21:43:56 +01:00
* @access private
* @param string $in
2014-12-29 21:43:56 +01:00
*/
protected function encryptBlock($in)
2014-12-29 21:43:56 +01:00
{
// RC4 does not utilize this method
}
/**
* Decrypts a block
2017-11-05 21:35:27 +01:00
*
2014-12-29 21:43:56 +01:00
* @access private
* @param string $in
2014-12-29 21:43:56 +01:00
*/
protected function decryptBlock($in)
2014-12-29 21:43:56 +01:00
{
// RC4 does not utilize this method
}
2013-05-25 04:05:24 +02:00
/**
* Setup the key (expansion)
*
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey()
2013-05-25 04:05:24 +02:00
* @access private
*/
protected function setupKey()
2013-05-25 04:05:24 +02:00
{
$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;
}
2016-11-30 22:19:23 +01:00
$this->stream = [];
$this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = [
2013-05-25 04:05:24 +02:00
0, // index $i
0, // index $j
$keyStream
2016-11-30 22:19:23 +01:00
];
2013-05-25 04:05:24 +02:00
}
/**
* Encrypts or decrypts a message.
*
* @see self::encrypt()
* @see self::decrypt()
* @access private
* @param string $text
* @param int $mode
* @return string $text
*/
private function crypt($text, $mode)
{
2013-05-20 08:19:38 +02:00
if ($this->changed) {
$this->setup();
2013-05-20 08:19:38 +02:00
$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;
}
}