2007-07-02 06:19:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-06-09 06:00:38 +02:00
|
|
|
* Pure-PHP implementation of Triple DES.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2007-07-23 07:21:39 +02:00
|
|
|
* Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* 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';
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* $des = new \phpseclib\Crypt\TripleDES();
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* $des->setKey('abcdefghijklmnopqrstuvwx');
|
|
|
|
*
|
|
|
|
* $size = 10 * 1024;
|
|
|
|
* $plaintext = '';
|
|
|
|
* for ($i = 0; $i < $size; $i++) {
|
|
|
|
* $plaintext.= 'a';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $des->decrypt($des->encrypt($plaintext));
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package TripleDES
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
namespace phpseclib\Crypt;
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of Triple DES.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package TripleDES
|
2007-07-02 06:19:55 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-17 01:16:54 +01:00
|
|
|
class TripleDES extends DES
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2014-12-09 19:46:30 +01:00
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using inner chaining
|
|
|
|
*
|
|
|
|
* Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (self::MODE_CBC3).
|
|
|
|
*/
|
|
|
|
const MODE_3CBC = -2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt / decrypt using outer chaining
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* Outer chaining is used by SSH-2 and when the mode is set to \phpseclib\Crypt\Common\BlockCipher::MODE_CBC.
|
2016-04-10 18:30:59 +02:00
|
|
|
*/
|
2016-07-31 05:18:06 +02:00
|
|
|
const MODE_CBC3 = self::MODE_CBC;
|
2014-12-09 19:46:30 +01:00
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
2016-04-10 18:30:59 +02:00
|
|
|
* Key Length (in bytes)
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see \phpseclib\Crypt\TripleDES::setKeyLength()
|
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $key_length = 24;
|
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\DES::cipher_name_mcrypt
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::cipher_name_mcrypt
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cipher_name_mcrypt = 'tripledes';
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimizing value while CFB-encrypting
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::cfb_init_len
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var int
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $cfb_init_len = 750;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* max possible size of $key
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::setKey()
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\DES::setKey()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var string
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected $key_length_max = 24;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-09 19:46:30 +01:00
|
|
|
* Internal flag whether using self::MODE_3CBC or not
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var bool
|
2013-05-20 08:19:38 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $mode_3cbc;
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
2014-12-17 01:16:54 +01:00
|
|
|
* The \phpseclib\Crypt\DES objects
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* Used only if $mode_3cbc === true
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @var array
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
private $des;
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Determines whether or not the mcrypt or OpenSSL extensions should be used.
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
|
|
|
* $mode could be:
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ecb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - cbc
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ctr
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - cfb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - ofb
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2017-06-28 05:34:36 +02:00
|
|
|
* - 3cbc
|
|
|
|
*
|
|
|
|
* - cbc3 (same as cbc)
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\DES::__construct()
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $mode
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function __construct($mode)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2017-06-28 05:34:36 +02:00
|
|
|
switch (strtolower($mode)) {
|
2014-12-09 19:46:30 +01:00
|
|
|
// In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC
|
2013-05-20 08:19:38 +02:00
|
|
|
// and additional flag us internally as 3CBC
|
2017-06-28 05:34:36 +02:00
|
|
|
case '3cbc':
|
|
|
|
$mode = self::MODE_3CBC;
|
|
|
|
parent::__construct('cbc');
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->mode_3cbc = true;
|
|
|
|
|
|
|
|
// This three $des'es will do the 3CBC work (if $key > 64bits)
|
2016-11-30 22:19:23 +01:00
|
|
|
$this->des = [
|
2017-06-28 05:34:36 +02:00
|
|
|
new DES('cbc'),
|
|
|
|
new DES('cbc'),
|
|
|
|
new DES('cbc'),
|
2016-11-30 22:19:23 +01:00
|
|
|
];
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
// we're going to be doing the padding, ourselves, so disable it in the \phpseclib\Crypt\DES objects
|
2007-07-23 07:21:39 +02:00
|
|
|
$this->des[0]->disablePadding();
|
|
|
|
$this->des[1]->disablePadding();
|
|
|
|
$this->des[2]->disablePadding();
|
2013-05-20 08:19:38 +02:00
|
|
|
break;
|
2017-06-28 05:34:36 +02:00
|
|
|
case 'cbc3':
|
|
|
|
$mode = 'cbc';
|
2013-05-20 08:19:38 +02:00
|
|
|
// If not 3CBC, we init as usual
|
|
|
|
default:
|
2014-06-16 17:06:34 +02:00
|
|
|
parent::__construct($mode);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:39:21 +01:00
|
|
|
/**
|
|
|
|
* Test for engine validity
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* This is mainly just a wrapper to set things up for \phpseclib\Crypt\Common\SymmetricKey::isValidEngine()
|
2014-11-29 14:39:21 +01:00
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $engine
|
2017-08-08 06:20:23 +02:00
|
|
|
* @access protected
|
2016-04-10 18:30:59 +02:00
|
|
|
* @return bool
|
2014-11-29 14:39:21 +01:00
|
|
|
*/
|
2017-08-08 06:20:23 +02:00
|
|
|
protected function isValidEngineHelper($engine)
|
2014-11-29 14:39:21 +01:00
|
|
|
{
|
2015-04-02 12:32:31 +02:00
|
|
|
if ($engine == self::ENGINE_OPENSSL) {
|
2018-12-30 06:21:18 +01:00
|
|
|
self::$cipher_name_openssl_ecb = 'des-ede3';
|
2016-12-03 21:39:11 +01:00
|
|
|
$mode = $this->openssl_translate_mode();
|
2014-11-29 14:39:21 +01:00
|
|
|
$this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode;
|
|
|
|
}
|
|
|
|
|
2017-08-08 06:20:23 +02:00
|
|
|
return parent::isValidEngineHelper($engine);
|
2014-11-29 14:39:21 +01:00
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
/**
|
2016-04-30 23:23:35 +02:00
|
|
|
* Sets the initialization vector.
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* SetIV is not required when \phpseclib\Crypt\Common\SymmetricKey::MODE_ECB is being used.
|
2013-05-25 04:05:24 +02:00
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::setIV()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $iv
|
2013-05-25 04:05:24 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setIV($iv)
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
|
|
|
parent::setIV($iv);
|
|
|
|
if ($this->mode_3cbc) {
|
|
|
|
$this->des[0]->setIV($iv);
|
|
|
|
$this->des[1]->setIV($iv);
|
|
|
|
$this->des[2]->setIV($iv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
/**
|
|
|
|
* Sets the key length.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Valid key lengths are 128 and 192 bits.
|
|
|
|
*
|
|
|
|
* If you want to use a 64-bit key use DES.php
|
2016-04-10 18:30:59 +02:00
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey:setKeyLength()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @access public
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the key length is invalid
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $length
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setKeyLength($length)
|
2016-04-10 18:30:59 +02:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
switch ($length) {
|
|
|
|
case 128:
|
|
|
|
case 192:
|
2016-04-10 18:30:59 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-30 23:23:35 +02:00
|
|
|
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes 128 or 192 bits are supported');
|
2016-04-10 18:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::setKeyLength($length);
|
|
|
|
}
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Sets the key.
|
|
|
|
*
|
2016-04-30 23:23:35 +02:00
|
|
|
* Triple DES can use 128-bit (eg. strlen($key) == 16) or 192-bit (eg. strlen($key) == 24) keys.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
|
|
|
|
*
|
|
|
|
* @access public
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\DES::setKey()
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::setKey()
|
2016-04-30 23:23:35 +02:00
|
|
|
* @throws \LengthException if the key length is invalid
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $key
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setKey($key)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2016-04-30 23:23:35 +02:00
|
|
|
if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) {
|
|
|
|
throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
|
2016-01-25 20:03:02 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:23:35 +02:00
|
|
|
switch (strlen($key)) {
|
|
|
|
case 16:
|
|
|
|
$key.= substr($key, 0, 8);
|
2017-07-20 11:35:08 +02:00
|
|
|
break;
|
2016-04-30 23:23:35 +02:00
|
|
|
case 24:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16 or 24 are supported');
|
|
|
|
}
|
|
|
|
|
2016-07-31 05:18:06 +02:00
|
|
|
// copied from self::setKey()
|
2016-04-30 23:23:35 +02:00
|
|
|
$this->key = $key;
|
|
|
|
$this->key_length = strlen($key);
|
|
|
|
$this->changed = true;
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->setEngine();
|
2016-04-30 23:23:35 +02:00
|
|
|
|
|
|
|
if ($this->mode_3cbc) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$this->des[0]->setKey(substr($key, 0, 8));
|
|
|
|
$this->des[1]->setKey(substr($key, 8, 8));
|
|
|
|
$this->des[2]->setKey(substr($key, 16, 8));
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypts a message.
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $plaintext
|
|
|
|
* @return string $cipertext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function encrypt($plaintext)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
// parent::en/decrypt() is able to do all the work for all modes and keylengths,
|
2014-12-09 19:46:30 +01:00
|
|
|
// except for: self::MODE_3CBC (inner chaining CBC) with a key > 64bits
|
2007-07-23 07:21:39 +02:00
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
// if the key is smaller then 8, do what we'd normally do
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->mode_3cbc && strlen($this->key) > 8) {
|
|
|
|
return $this->des[2]->encrypt(
|
2013-12-09 22:45:07 +01:00
|
|
|
$this->des[1]->decrypt(
|
|
|
|
$this->des[0]->encrypt(
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->pad($plaintext)
|
2013-12-09 22:45:07 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
return parent::encrypt($plaintext);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypts a message.
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param string $ciphertext
|
|
|
|
* @return string $plaintext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function decrypt($ciphertext)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->mode_3cbc && strlen($this->key) > 8) {
|
2016-12-03 21:39:11 +01:00
|
|
|
return $this->unpad(
|
2013-12-09 22:45:07 +01:00
|
|
|
$this->des[0]->decrypt(
|
|
|
|
$this->des[1]->encrypt(
|
|
|
|
$this->des[2]->decrypt(
|
|
|
|
str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
return parent::decrypt($ciphertext);
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* will yield different outputs:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* echo $des->encrypt(substr($plaintext, 0, 8));
|
|
|
|
* echo $des->encrypt(substr($plaintext, 8, 8));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $des->encrypt($plaintext);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
|
|
|
|
* another, as demonstrated with the following:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $des->encrypt(substr($plaintext, 0, 8));
|
|
|
|
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
|
|
|
* </code>
|
|
|
|
* <code>
|
|
|
|
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* 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 /
|
|
|
|
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* Put another way, when the continuous buffer is enabled, the state of the \phpseclib\Crypt\DES() object changes after each
|
2007-07-02 06:19:55 +02:00
|
|
|
* 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),
|
|
|
|
* however, they are also less intuitive and more likely to cause you problems.
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::enableContinuousBuffer()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::disableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function enableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::enableContinuousBuffer();
|
|
|
|
if ($this->mode_3cbc) {
|
2007-07-23 07:21:39 +02:00
|
|
|
$this->des[0]->enableContinuousBuffer();
|
|
|
|
$this->des[1]->enableContinuousBuffer();
|
|
|
|
$this->des[2]->enableContinuousBuffer();
|
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat consecutive packets as if they are a discontinuous buffer.
|
|
|
|
*
|
|
|
|
* The default behavior.
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::disableContinuousBuffer()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @see self::enableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function disableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::disableContinuousBuffer();
|
|
|
|
if ($this->mode_3cbc) {
|
2007-07-23 07:21:39 +02:00
|
|
|
$this->des[0]->disableContinuousBuffer();
|
|
|
|
$this->des[1]->disableContinuousBuffer();
|
|
|
|
$this->des[2]->disableContinuousBuffer();
|
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
2013-05-25 04:05:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the key schedule
|
|
|
|
*
|
2016-12-03 21:39:11 +01:00
|
|
|
* @see \phpseclib\Crypt\DES::setupKey()
|
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::setupKey()
|
2013-05-25 04:05:24 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
protected function setupKey()
|
2013-05-25 04:05:24 +02:00
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
// if $key <= 64bits we configure our internal pure-php cipher engine
|
|
|
|
// to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
|
|
|
|
case strlen($this->key) <= 8:
|
|
|
|
$this->des_rounds = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// otherwise, if $key > 64bits, we configure our engine to work as 3DES.
|
|
|
|
default:
|
|
|
|
$this->des_rounds = 3;
|
|
|
|
|
|
|
|
// (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
|
|
|
|
if ($this->mode_3cbc) {
|
2016-12-03 21:39:11 +01:00
|
|
|
$this->des[0]->setupKey();
|
|
|
|
$this->des[1]->setupKey();
|
|
|
|
$this->des[2]->setupKey();
|
2013-05-25 04:05:24 +02:00
|
|
|
|
|
|
|
// because $des[0-2] will, now, do all the work we can return here
|
2016-12-03 21:39:11 +01:00
|
|
|
// not need unnecessary stress parent::setupKey() with our, now unused, $key.
|
2013-05-25 04:05:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// setup our key
|
2016-12-03 21:39:11 +01:00
|
|
|
parent::setupKey();
|
2013-05-25 04:05:24 +02:00
|
|
|
}
|
2014-01-31 00:13:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal crypt engine
|
|
|
|
*
|
2016-07-31 05:18:06 +02:00
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
|
|
|
|
* @see \phpseclib\Crypt\Common\SymmetricKey::setPreferredEngine()
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $engine
|
2014-01-31 00:13:14 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
2016-12-03 21:39:11 +01:00
|
|
|
public function setPreferredEngine($engine)
|
2014-01-31 00:13:14 +01:00
|
|
|
{
|
|
|
|
if ($this->mode_3cbc) {
|
2015-01-17 16:42:47 +01:00
|
|
|
$this->des[0]->setPreferredEngine($engine);
|
|
|
|
$this->des[1]->setPreferredEngine($engine);
|
|
|
|
$this->des[2]->setPreferredEngine($engine);
|
2014-01-31 00:13:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 11:35:08 +02:00
|
|
|
parent::setPreferredEngine($engine);
|
2014-01-31 00:13:14 +01:00
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|