2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
\r
5 * Pure-PHP implementation of RC4.
\r
7 * Uses mcrypt, if available, and an internal implementation, otherwise.
\r
9 * PHP versions 4 and 5
\r
11 * Useful resources are as follows:
\r
13 * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
\r
14 * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
\r
16 * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
\r
17 * ARCFOUR or ARC4 because RC4 is how it is refered to in the SSH1 specification.
\r
19 * Here's a short example of how to use this library:
\r
22 * include('Crypt/RC4.php');
\r
24 * $rc4 = new Crypt_RC4();
\r
26 * $rc4->setKey('abcdefgh');
\r
28 * $size = 10 * 1024;
\r
30 * for ($i = 0; $i < $size; $i++) {
\r
34 * echo $rc4->decrypt($rc4->encrypt($plaintext));
\r
38 * LICENSE: This library is free software; you can redistribute it and/or
\r
39 * modify it under the terms of the GNU Lesser General Public
\r
40 * License as published by the Free Software Foundation; either
\r
41 * version 2.1 of the License, or (at your option) any later version.
\r
43 * This library is distributed in the hope that it will be useful,
\r
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
46 * Lesser General Public License for more details.
\r
48 * You should have received a copy of the GNU Lesser General Public
\r
49 * License along with this library; if not, write to the Free Software
\r
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
\r
54 * @package Crypt_RC4
\r
55 * @author Jim Wigginton <terrafrost@php.net>
\r
56 * @copyright MMVII Jim Wigginton
\r
57 * @license http://www.gnu.org/licenses/lgpl.txt
\r
58 * @version $Id: RC4.php,v 1.8 2009/06/09 04:00:38 terrafrost Exp $
\r
59 * @link http://phpseclib.sourceforge.net
\r
64 * @see Crypt_RC4::Crypt_RC4()
\r
67 * Toggles the internal implementation
\r
69 define('CRYPT_RC4_MODE_INTERNAL', 1);
\r
71 * Toggles the mcrypt implementation
\r
73 define('CRYPT_RC4_MODE_MCRYPT', 2);
\r
78 * @see Crypt_RC4::_crypt()
\r
80 define('CRYPT_RC4_ENCRYPT', 0);
\r
81 define('CRYPT_RC4_DECRYPT', 1);
\r
85 * Pure-PHP implementation of RC4.
\r
87 * @author Jim Wigginton <terrafrost@php.net>
\r
90 * @package Crypt_RC4
\r
96 * @see Crypt_RC4::setKey()
\r
103 * The Key Stream for encryption
\r
105 * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
\r
107 * @see Crypt_RC4::setKey()
\r
111 var $encryptStream = false;
\r
114 * The Key Stream for decryption
\r
116 * If CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT, this will be equal to the mcrypt object
\r
118 * @see Crypt_RC4::setKey()
\r
122 var $decryptStream = false;
\r
125 * The $i and $j indexes for encryption
\r
127 * @see Crypt_RC4::_crypt()
\r
131 var $encryptIndex = 0;
\r
134 * The $i and $j indexes for decryption
\r
136 * @see Crypt_RC4::_crypt()
\r
140 var $decryptIndex = 0;
\r
143 * MCrypt parameters
\r
145 * @see Crypt_RC4::setMCrypt()
\r
149 var $mcrypt = array('', '');
\r
152 * The Encryption Algorithm
\r
154 * Only used if CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT. Only possible values are MCRYPT_RC4 or MCRYPT_ARCFOUR.
\r
156 * @see Crypt_RC4::Crypt_RC4()
\r
163 * Default Constructor.
\r
165 * Determines whether or not the mcrypt extension should be used.
\r
167 * @param optional Integer $mode
\r
168 * @return Crypt_RC4
\r
171 function Crypt_RC4()
\r
173 if ( !defined('CRYPT_RC4_MODE') ) {
\r
175 case extension_loaded('mcrypt') && (defined('MCRYPT_ARCFOUR') || defined('MCRYPT_RC4')):
\r
176 // i'd check to see if rc4 was supported, by doing in_array('arcfour', mcrypt_list_algorithms('')),
\r
177 // but since that can be changed after the object has been created, there doesn't seem to be
\r
178 // a lot of point...
\r
179 define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_MCRYPT);
\r
182 define('CRYPT_RC4_MODE', CRYPT_RC4_MODE_INTERNAL);
\r
186 switch ( CRYPT_RC4_MODE ) {
\r
187 case CRYPT_RC4_MODE_MCRYPT:
\r
189 case defined('MCRYPT_ARCFOUR'):
\r
190 $this->mode = MCRYPT_ARCFOUR;
\r
192 case defined('MCRYPT_RC4');
\r
193 $this->mode = MCRYPT_RC4;
\r
201 * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will
\r
202 * be used. If no key is explicitly set, it'll be assumed to be a single null byte.
\r
205 * @param String $key
\r
207 function setKey($key)
\r
211 if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
\r
215 $keyLength = strlen($key);
\r
216 $keyStream = array();
\r
217 for ($i = 0; $i < 256; $i++) {
\r
218 $keyStream[$i] = $i;
\r
221 for ($i = 0; $i < 256; $i++) {
\r
222 $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
\r
223 $temp = $keyStream[$i];
\r
224 $keyStream[$i] = $keyStream[$j];
\r
225 $keyStream[$j] = $temp;
\r
228 $this->encryptIndex = $this->decryptIndex = array(0, 0);
\r
229 $this->encryptStream = $this->decryptStream = $keyStream;
\r
235 * Some protocols, such as WEP, prepend an "initialization vector" to the key, effectively creating a new key [1].
\r
236 * If you need to use an initialization vector in this manner, feel free to prepend it to the key, yourself, before
\r
237 * calling setKey().
\r
239 * [1] WEP's initialization vectors (IV's) are used in a somewhat insecure way. Since, in that protocol,
\r
240 * the IV's are relatively easy to predict, an attack described by
\r
241 * {@link http://www.drizzle.com/~aboba/IEEE/rc4_ksaproc.pdf Scott Fluhrer, Itsik Mantin, and Adi Shamir}
\r
242 * can be used to quickly guess at the rest of the key. The following links elaborate:
\r
244 * {@link http://www.rsa.com/rsalabs/node.asp?id=2009 http://www.rsa.com/rsalabs/node.asp?id=2009}
\r
245 * {@link http://en.wikipedia.org/wiki/Related_key_attack http://en.wikipedia.org/wiki/Related_key_attack}
\r
247 * @param String $iv
\r
248 * @see Crypt_RC4::setKey()
\r
251 function setIV($iv)
\r
256 * Sets MCrypt parameters. (optional)
\r
258 * If MCrypt is being used, empty strings will be used, unless otherwise specified.
\r
260 * @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
\r
262 * @param optional Integer $algorithm_directory
\r
263 * @param optional Integer $mode_directory
\r
265 function setMCrypt($algorithm_directory = '', $mode_directory = '')
\r
267 if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
\r
268 $this->mcrypt = array($algorithm_directory, $mode_directory);
\r
269 $this->_closeMCrypt();
\r
274 * Encrypts a message.
\r
276 * @see Crypt_RC4::_crypt()
\r
278 * @param String $plaintext
\r
280 function encrypt($plaintext)
\r
282 return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
\r
286 * Decrypts a message.
\r
288 * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
\r
289 * Atleast if the continuous buffer is disabled.
\r
291 * @see Crypt_RC4::_crypt()
\r
293 * @param String $ciphertext
\r
295 function decrypt($ciphertext)
\r
297 return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT);
\r
301 * Encrypts or decrypts a message.
\r
303 * @see Crypt_RC4::encrypt()
\r
304 * @see Crypt_RC4::decrypt()
\r
306 * @param String $text
\r
307 * @param Integer $mode
\r
309 function _crypt($text, $mode)
\r
311 if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
\r
312 $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
\r
314 if ($this->$keyStream === false) {
\r
315 $this->$keyStream = mcrypt_module_open($this->mode, $this->mcrypt[0], MCRYPT_MODE_STREAM, $this->mcrypt[1]);
\r
316 mcrypt_generic_init($this->$keyStream, $this->key, '');
\r
317 } else if (!$this->continuousBuffer) {
\r
318 mcrypt_generic_init($this->$keyStream, $this->key, '');
\r
320 $newText = mcrypt_generic($this->$keyStream, $text);
\r
321 if (!$this->continuousBuffer) {
\r
322 mcrypt_generic_deinit($this->$keyStream);
\r
328 if ($this->encryptStream === false) {
\r
329 $this->setKey($this->key);
\r
333 case CRYPT_RC4_ENCRYPT:
\r
334 $keyStream = $this->encryptStream;
\r
335 list($i, $j) = $this->encryptIndex;
\r
337 case CRYPT_RC4_DECRYPT:
\r
338 $keyStream = $this->decryptStream;
\r
339 list($i, $j) = $this->decryptIndex;
\r
343 for ($k = 0; $k < strlen($text); $k++) {
\r
344 $i = ($i + 1) & 255;
\r
345 $j = ($j + $keyStream[$i]) & 255;
\r
346 $temp = $keyStream[$i];
\r
347 $keyStream[$i] = $keyStream[$j];
\r
348 $keyStream[$j] = $temp;
\r
349 $temp = $keyStream[($keyStream[$i] + $keyStream[$j]) & 255];
\r
350 $newText.= chr(ord($text[$k]) ^ $temp);
\r
353 if ($this->continuousBuffer) {
\r
355 case CRYPT_RC4_ENCRYPT:
\r
356 $this->encryptStream = $keyStream;
\r
357 $this->encryptIndex = array($i, $j);
\r
359 case CRYPT_RC4_DECRYPT:
\r
360 $this->decryptStream = $keyStream;
\r
361 $this->decryptIndex = array($i, $j);
\r
369 * Treat consecutive "packets" as if they are a continuous buffer.
\r
371 * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
\r
372 * will yield different outputs:
\r
375 * echo $rc4->encrypt(substr($plaintext, 0, 8));
\r
376 * echo $rc4->encrypt(substr($plaintext, 8, 8));
\r
379 * echo $rc4->encrypt($plaintext);
\r
382 * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
\r
383 * another, as demonstrated with the following:
\r
386 * $rc4->encrypt(substr($plaintext, 0, 8));
\r
387 * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
\r
390 * echo $rc4->decrypt($des->encrypt(substr($plaintext, 8, 8)));
\r
393 * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
\r
394 * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
\r
395 * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
\r
397 * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
\r
398 * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
\r
399 * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
\r
400 * however, they are also less intuitive and more likely to cause you problems.
\r
402 * @see Crypt_RC4::disableContinuousBuffer()
\r
405 function enableContinuousBuffer()
\r
407 $this->continuousBuffer = true;
\r
411 * Treat consecutive packets as if they are a discontinuous buffer.
\r
413 * The default behavior.
\r
415 * @see Crypt_RC4::enableContinuousBuffer()
\r
418 function disableContinuousBuffer()
\r
420 if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
\r
421 $this->encryptIndex = $this->decryptIndex = array(0, 0);
\r
422 $this->setKey($this->key);
\r
425 $this->continuousBuffer = false;
\r
431 * Since RC4 is a stream cipher and not a block cipher, no padding is necessary. The only reason this function is
\r
432 * included is so that you can switch between a block cipher and a stream cipher transparently.
\r
434 * @see Crypt_RC4::disablePadding()
\r
437 function enablePadding()
\r
444 * @see Crypt_RC4::enablePadding()
\r
447 function disablePadding()
\r
452 * Class destructor.
\r
454 * Will be called, automatically, if you're using PHP5. If you're using PHP4, call it yourself. Only really
\r
455 * needs to be called if mcrypt is being used.
\r
459 function __destruct()
\r
461 if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
\r
462 $this->_closeMCrypt();
\r
467 * Properly close the MCrypt objects.
\r
471 function _closeMCrypt()
\r
473 if ( $this->encryptStream !== false ) {
\r
474 if ( $this->continuousBuffer ) {
\r
475 mcrypt_generic_deinit($this->encryptStream);
\r
478 mcrypt_module_close($this->encryptStream);
\r
480 $this->encryptStream = false;
\r
483 if ( $this->decryptStream !== false ) {
\r
484 if ( $this->continuousBuffer ) {
\r
485 mcrypt_generic_deinit($this->decryptStream);
\r
488 mcrypt_module_close($this->decryptStream);
\r
490 $this->decryptStream = false;
\r