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
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-06-01 23:28:49 +02:00
|
|
|
* include 'Crypt/TripleDES.php';
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* $des = new Crypt_TripleDES();
|
|
|
|
*
|
|
|
|
* $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
|
|
|
|
* @package Crypt_TripleDES
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include Crypt_DES
|
|
|
|
*/
|
2012-06-08 21:38:27 +02:00
|
|
|
if (!class_exists('Crypt_DES')) {
|
2013-12-06 01:03:34 +01:00
|
|
|
include_once 'DES.php';
|
2012-06-08 21:38:27 +02:00
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of Triple DES.
|
|
|
|
*
|
2013-12-11 18:33:18 +01:00
|
|
|
* @package Crypt_TripleDES
|
2007-07-02 06:19:55 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2013-12-03 19:34:41 +01:00
|
|
|
class Crypt_TripleDES extends Crypt_DES
|
|
|
|
{
|
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
|
|
|
|
*
|
|
|
|
* Outer chaining is used by SSH-2 and when the mode is set to Crypt_Base::MODE_CBC.
|
|
|
|
*/
|
|
|
|
const MODE_CBC3 = Crypt_Base::MODE_CBC;
|
|
|
|
|
2013-05-20 08:19:38 +02:00
|
|
|
/**
|
|
|
|
* The default password key_size used by setPassword()
|
|
|
|
*
|
|
|
|
* @see Crypt_DES::password_key_size
|
|
|
|
* @see Crypt_Base::password_key_size
|
|
|
|
* @see Crypt_Base::setPassword()
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $password_key_size = 24;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default salt used by setPassword()
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::password_default_salt
|
|
|
|
* @see Crypt_Base::setPassword()
|
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $password_default_salt = 'phpseclib';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The namespace used by the cipher for its constants.
|
|
|
|
*
|
|
|
|
* @see Crypt_DES::const_namespace
|
|
|
|
* @see Crypt_Base::const_namespace
|
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $const_namespace = 'DES';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The mcrypt specific name of the cipher
|
|
|
|
*
|
|
|
|
* @see Crypt_DES::cipher_name_mcrypt
|
|
|
|
* @see Crypt_Base::cipher_name_mcrypt
|
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $cipher_name_mcrypt = 'tripledes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimizing value while CFB-encrypting
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::cfb_init_len
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $cfb_init_len = 750;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* max possible size of $key
|
|
|
|
*
|
|
|
|
* @see Crypt_TripleDES::setKey()
|
|
|
|
* @see Crypt_DES::setKey()
|
|
|
|
* @var String
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $key_size_max = 24;
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @var Boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $mode_3cbc;
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
2010-02-09 07:10:26 +01:00
|
|
|
* The 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
|
|
|
|
*
|
2007-07-02 06:19:55 +02:00
|
|
|
* @var Array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-02-09 07:10:26 +01:00
|
|
|
var $des;
|
2007-07-02 06:19:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* Determines whether or not the mcrypt extension should be used.
|
|
|
|
*
|
|
|
|
* $mode could be:
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - Crypt_Base::MODE_ECB
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - Crypt_Base::MODE_CBC
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - Crypt_Base::MODE_CTR
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - Crypt_Base::MODE_CFB
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - Crypt_Base::MODE_OFB
|
2013-05-25 06:22:25 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* - self::MODE_3CBC
|
2013-05-20 08:19:38 +02:00
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* If not explicitly set, Crypt_Base::MODE_CBC will be used.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2014-06-16 17:06:34 +02:00
|
|
|
* @see Crypt_DES::__construct()
|
|
|
|
* @see Crypt_Base::__construct()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @param optional Integer $mode
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-09 19:46:30 +01:00
|
|
|
function __construct($mode = Crypt_Base::MODE_CBC)
|
2007-07-02 06:19:55 +02:00
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
switch ($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
|
2014-12-09 19:46:30 +01:00
|
|
|
case self::MODE_3CBC:
|
|
|
|
parent::__construct(Crypt_Base::MODE_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)
|
2007-07-02 06:19:55 +02:00
|
|
|
$this->des = array(
|
2014-12-09 19:46:30 +01:00
|
|
|
new Crypt_DES(Crypt_Base::MODE_CBC),
|
|
|
|
new Crypt_DES(Crypt_Base::MODE_CBC),
|
|
|
|
new Crypt_DES(Crypt_Base::MODE_CBC),
|
2007-07-02 06:19:55 +02:00
|
|
|
);
|
2013-05-20 08:19:38 +02:00
|
|
|
|
2007-07-23 07:21:39 +02:00
|
|
|
// we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
|
|
|
|
$this->des[0]->disablePadding();
|
|
|
|
$this->des[1]->disablePadding();
|
|
|
|
$this->des[2]->disablePadding();
|
2013-05-20 08:19:38 +02:00
|
|
|
break;
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:05:24 +02:00
|
|
|
/**
|
|
|
|
* Sets the initialization vector. (optional)
|
|
|
|
*
|
2014-12-09 19:46:30 +01:00
|
|
|
* SetIV is not required when Crypt_Base::MODE_ECB is being used. If not explicitly set, it'll be assumed
|
2013-05-25 04:05:24 +02:00
|
|
|
* to be all zero's.
|
|
|
|
*
|
|
|
|
* @see Crypt_Base::setIV()
|
|
|
|
* @access public
|
|
|
|
* @param String $iv
|
|
|
|
*/
|
|
|
|
function setIV($iv)
|
|
|
|
{
|
|
|
|
parent::setIV($iv);
|
|
|
|
if ($this->mode_3cbc) {
|
|
|
|
$this->des[0]->setIV($iv);
|
|
|
|
$this->des[1]->setIV($iv);
|
|
|
|
$this->des[2]->setIV($iv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-02 06:19:55 +02:00
|
|
|
/**
|
|
|
|
* Sets the key.
|
|
|
|
*
|
|
|
|
* Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
|
|
|
|
* 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
|
|
|
|
*
|
|
|
|
* DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* If the key is not explicitly set, it'll be assumed to be all null bytes.
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_DES::setKey()
|
|
|
|
* @see Crypt_Base::setKey()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @param String $key
|
|
|
|
*/
|
|
|
|
function setKey($key)
|
|
|
|
{
|
|
|
|
$length = strlen($key);
|
|
|
|
if ($length > 8) {
|
2013-05-20 08:19:38 +02:00
|
|
|
$key = str_pad(substr($key, 0, 24), 24, chr(0));
|
2007-07-02 06:19:55 +02:00
|
|
|
// if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
|
|
|
|
// http://php.net/function.mcrypt-encrypt#47973
|
2010-02-26 04:40:26 +01:00
|
|
|
//$key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
|
2010-12-02 05:59:46 +01:00
|
|
|
} else {
|
|
|
|
$key = str_pad($key, 8, chr(0));
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
2013-05-20 08:19:38 +02:00
|
|
|
parent::setKey($key);
|
|
|
|
|
2014-12-09 19:46:30 +01:00
|
|
|
// And in case of self::MODE_3CBC:
|
2013-05-20 08:19:38 +02:00
|
|
|
// if key <= 64bits we not need the 3 $des to work,
|
|
|
|
// because we will then act as regular DES-CBC with just a <= 64bit key.
|
|
|
|
// So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des.
|
|
|
|
if ($this->mode_3cbc && $length > 8) {
|
|
|
|
$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.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::encrypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
|
|
|
* @param String $plaintext
|
2013-05-20 08:19:38 +02:00
|
|
|
* @return String $cipertext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
function encrypt($plaintext)
|
|
|
|
{
|
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(
|
|
|
|
$this->_pad($plaintext)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
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.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::decrypt()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @access public
|
|
|
|
* @param String $ciphertext
|
2013-05-20 08:19:38 +02:00
|
|
|
* @return String $plaintext
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
function decrypt($ciphertext)
|
|
|
|
{
|
2013-05-20 08:19:38 +02:00
|
|
|
if ($this->mode_3cbc && strlen($this->key) > 8) {
|
2013-12-09 22:45:07 +01:00
|
|
|
return $this->_unpad(
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
|
|
|
|
* 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.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::enableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @see Crypt_TripleDES::disableContinuousBuffer()
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function enableContinuousBuffer()
|
|
|
|
{
|
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.
|
|
|
|
*
|
2013-05-20 08:19:38 +02:00
|
|
|
* @see Crypt_Base::disableContinuousBuffer()
|
2007-07-02 06:19:55 +02:00
|
|
|
* @see Crypt_TripleDES::enableContinuousBuffer()
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function disableContinuousBuffer()
|
|
|
|
{
|
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
|
|
|
|
*
|
|
|
|
* @see Crypt_DES::_setupKey()
|
|
|
|
* @see Crypt_Base::_setupKey()
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _setupKey()
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
$this->des[0]->_setupKey();
|
|
|
|
$this->des[1]->_setupKey();
|
|
|
|
$this->des[2]->_setupKey();
|
|
|
|
|
|
|
|
// because $des[0-2] will, now, do all the work we can return here
|
|
|
|
// not need unnecessary stress parent::_setupKey() with our, now unused, $key.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// setup our key
|
|
|
|
parent::_setupKey();
|
|
|
|
}
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|