phpseclib/phpseclib/Crypt/RC4.php

265 lines
6.9 KiB
PHP
Raw Normal View History

<?php
/**
* Pure-PHP implementation of RC4.
*
2022-07-23 00:16:04 +00:00
* Uses OpenSSL, if available/possible, and an internal implementation, otherwise
*
2015-04-02 10:57:52 +00: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 14:34:07 +00: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 00:16:54 +00:00
* include 'vendor/autoload.php';
*
* $rc4 = new \phpseclib3\Crypt\RC4();
*
* $rc4->setKey('abcdefgh');
*
* $size = 10 * 1024;
* $plaintext = '';
* for ($i = 0; $i < $size; $i++) {
* $plaintext.= 'a';
* }
*
* echo $rc4->decrypt($rc4->encrypt($plaintext));
* ?>
* </code>
*
* @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
*/
2022-06-04 15:31:21 +00:00
declare(strict_types=1);
namespace phpseclib3\Crypt;
2014-12-17 00:16:54 +00:00
use phpseclib3\Crypt\Common\StreamCipher;
use phpseclib3\Exception\LengthException;
/**
* Pure-PHP implementation of RC4.
*
* @author Jim Wigginton <terrafrost@php.net>
*/
class RC4 extends StreamCipher
{
2020-12-30 15:08:05 +00:00
/**
* @see \phpseclib3\Crypt\RC4::_crypt()
2020-12-30 15:08:05 +00:00
*/
2022-07-07 01:43:09 +00:00
public const ENCRYPT = 0;
2020-12-30 15:08:05 +00:00
/**
* @see \phpseclib3\Crypt\RC4::_crypt()
*/
2022-07-07 01:43:09 +00:00
public const DECRYPT = 1;
/**
* Key Length (in bytes)
*
* @see \phpseclib3\Crypt\RC4::setKeyLength()
* @var int
*/
protected $key_length = 128; // = 1024 bits
/**
2013-05-20 06:19:38 +00:00
* The Key
*
* @see self::setKey()
* @var string
*/
protected $key;
/**
2013-05-20 06:19:38 +00:00
* The Key Stream for decryption and encryption
*
* @see self::setKey()
* @var array
*/
private $stream;
/**
* Test for engine validity
*
* This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
*
2022-06-24 00:44:04 +00:00
* @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
*/
2022-06-04 15:31:21 +00:00
protected function isValidEngineHelper(int $engine): bool
{
2017-01-27 18:40:23 +00:00
if ($engine == self::ENGINE_OPENSSL) {
2019-03-18 11:59:00 +00:00
if ($this->continuousBuffer) {
return false;
}
// quoting https://www.openssl.org/news/openssl-3.0-notes.html, OpenSSL 3.0.1
// "Moved all variations of the EVP ciphers CAST5, BF, IDEA, SEED, RC2, RC4, RC5, and DES to the legacy provider"
// in theory openssl_get_cipher_methods() should catch this but, on GitHub Actions, at least, it does not
if (defined('OPENSSL_VERSION_TEXT') && version_compare(preg_replace('#OpenSSL (\d+\.\d+\.\d+) .*#', '$1', OPENSSL_VERSION_TEXT), '3.0.1', '>=')) {
return false;
}
2022-12-17 04:53:49 +00:00
$this->cipher_name_openssl = 'rc4-40';
}
return parent::isValidEngineHelper($engine);
}
/**
* Sets the key length
*
* Keys can be between 1 and 256 bytes long.
*
* @throws LengthException if the key length is invalid
*/
2022-06-04 15:31:21 +00:00
public function setKeyLength(int $length): void
{
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 20:34:10 +00:00
parent::setKeyLength($length);
}
/**
* Sets the key length
*
* Keys can be between 1 and 256 bytes long.
*/
2022-06-04 15:31:21 +00:00
public function setKey(string $key): void
{
$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.
*
* @return string $ciphertext
2022-06-24 00:44:04 +00:00
* @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
2022-06-04 15:31:21 +00:00
* @see self::crypt()
*/
2022-06-04 15:31:21 +00:00
public function encrypt(string $plaintext): string
{
if ($this->engine != self::ENGINE_INTERNAL) {
2013-05-20 06:19:38 +00: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.
*
* @return string $plaintext
2022-06-24 00:44:04 +00:00
* @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
2022-06-04 15:31:21 +00:00
* @see self::crypt()
*/
2022-06-04 15:31:21 +00:00
public function decrypt(string $ciphertext): string
{
if ($this->engine != self::ENGINE_INTERNAL) {
2013-05-20 06:19:38 +00:00
return parent::decrypt($ciphertext);
}
return $this->crypt($ciphertext, self::DECRYPT);
}
2014-12-29 20:43:56 +00:00
/**
* Encrypts a block
*/
2022-06-04 15:31:21 +00:00
protected function encryptBlock(string $in): string
2014-12-29 20:43:56 +00:00
{
// RC4 does not utilize this method
2022-06-04 15:31:21 +00:00
return '';
2014-12-29 20:43:56 +00:00
}
/**
* Decrypts a block
*/
2022-06-04 15:31:21 +00:00
protected function decryptBlock(string $in): string
2014-12-29 20:43:56 +00:00
{
// RC4 does not utilize this method
2022-06-04 15:31:21 +00:00
return '';
2014-12-29 20:43:56 +00:00
}
2013-05-25 02:05:24 +00:00
/**
* Setup the key (expansion)
*
* @see \phpseclib3\Crypt\Common\SymmetricKey::_setupKey()
2013-05-25 02:05:24 +00:00
*/
2022-06-04 15:31:21 +00:00
protected function setupKey(): void
2013-05-25 02:05:24 +00:00
{
$key = $this->key;
$keyLength = strlen($key);
2014-06-16 14:19:14 +00:00
$keyStream = range(0, 255);
2013-05-25 02:05:24 +00: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 21:19:23 +00:00
$this->stream = [];
$this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = [
2013-05-25 02:05:24 +00:00
0, // index $i
0, // index $j
$keyStream,
2016-11-30 21:19:23 +00:00
];
2013-05-25 02:05:24 +00:00
}
/**
* Encrypts or decrypts a message.
*
* @return string $text
2022-06-24 00:44:04 +00:00
* @see self::decrypt()
2022-06-04 15:31:21 +00:00
* @see self::encrypt()
*/
2022-06-04 15:31:21 +00:00
private function crypt(string $text, int $mode): string
{
2013-05-20 06:19:38 +00:00
if ($this->changed) {
$this->setup();
}
2013-05-20 06:19:38 +00:00
$stream = &$this->stream[$mode];
if ($this->continuousBuffer) {
2013-05-20 06:19:38 +00:00
$i = &$stream[0];
$j = &$stream[1];
$keyStream = &$stream[2];
} else {
$i = $stream[0];
$j = $stream[1];
$keyStream = $stream[2];
}
2013-05-20 06:19:38 +00: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 06:19:38 +00:00
$keyStream[$i] = $ksj;
$keyStream[$j] = $ksi;
2014-06-16 14:19:14 +00:00
$text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
}
2013-05-20 06:19:38 +00:00
return $text;
}
}