ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
phpseclib\Crypt\TripleDES Class Reference
+ Inheritance diagram for phpseclib\Crypt\TripleDES:
+ Collaboration diagram for phpseclib\Crypt\TripleDES:

Public Member Functions

 __construct ($mode=Base::MODE_CBC)
 Default Constructor. More...
 
 isValidEngine ($engine)
 Test for engine validity. More...
 
 setIV ($iv)
 Sets the initialization vector. More...
 
 setKeyLength ($length)
 Sets the key length. More...
 
 setKey ($key)
 Sets the key. More...
 
 encrypt ($plaintext)
 Encrypts a message. More...
 
 decrypt ($ciphertext)
 Decrypts a message. More...
 
 enableContinuousBuffer ()
 Treat consecutive "packets" as if they are a continuous buffer. More...
 
 disableContinuousBuffer ()
 Treat consecutive packets as if they are a discontinuous buffer. More...
 
 _setupKey ()
 Creates the key schedule. More...
 
 setPreferredEngine ($engine)
 Sets the internal crypt engine. More...
 
- Public Member Functions inherited from phpseclib\Crypt\DES
 isValidEngine ($engine)
 Test for engine validity. More...
 
 setKey ($key)
 Sets the key. More...
 
 _encryptBlock ($in)
 Encrypts a block. More...
 
 _decryptBlock ($in)
 Decrypts a block. More...
 
 _processBlock ($block, $mode)
 Encrypts or decrypts a 64-bit block. More...
 
 _setupKey ()
 Creates the key schedule. More...
 
 _setupInlineCrypt ()
 Setup the performance-optimized function for de/encrypt() More...
 
- Public Member Functions inherited from phpseclib\Crypt\Base
 __construct ($mode=self::MODE_CBC)
 Default Constructor. More...
 
 setIV ($iv)
 Sets the initialization vector. More...
 
 setKeyLength ($length)
 Sets the key length. More...
 
 getKeyLength ()
 Returns the current key length in bits. More...
 
 getBlockLength ()
 Returns the current block length in bits. More...
 
 setKey ($key)
 Sets the key. More...
 
 setPassword ($password, $method='pbkdf2')
 Sets the password. More...
 
 encrypt ($plaintext)
 Encrypts a message. More...
 
 decrypt ($ciphertext)
 Decrypts a message. More...
 
 _openssl_ctr_process ($plaintext, &$encryptIV, &$buffer)
 OpenSSL CTR Processor. More...
 
 _openssl_ofb_process ($plaintext, &$encryptIV, &$buffer)
 OpenSSL OFB Processor. More...
 
 _openssl_translate_mode ()
 phpseclib <-> OpenSSL Mode Mapper More...
 
 enablePadding ()
 Pad "packets". More...
 
 disablePadding ()
 Do not pad packets. More...
 
 enableContinuousBuffer ()
 Treat consecutive "packets" as if they are a continuous buffer. More...
 
 disableContinuousBuffer ()
 Treat consecutive packets as if they are a discontinuous buffer. More...
 
 isValidEngine ($engine)
 Test for engine validity. More...
 
 setPreferredEngine ($engine)
 Sets the preferred crypt engine. More...
 
 getEngine ()
 Returns the engine currently being utilized. More...
 
 _setEngine ()
 Sets the engine as appropriate. More...
 
 _encryptBlock ($in)
 Encrypts a block. More...
 
 _decryptBlock ($in)
 Decrypts a block. More...
 
 _setupKey ()
 Setup the key (expansion) More...
 
 _setup ()
 Setup the self::ENGINE_INTERNAL $engine. More...
 
 _setupMcrypt ()
 Setup the self::ENGINE_MCRYPT $engine. More...
 
 _pad ($text)
 Pads a string. More...
 
 _unpad ($text)
 Unpads a string. More...
 
 _clearBuffers ()
 Clears internal buffers. More...
 
 _string_shift (&$string, $index=1)
 String Shift. More...
 
 _string_pop (&$string, $index=1)
 String Pop. More...
 
 _increment_str (&$var)
 Increment the current string. More...
 
 _setupInlineCrypt ()
 Setup the performance-optimized function for de/encrypt() More...
 
 _createInlineCryptFunction ($cipher_code)
 Creates the performance-optimized function for en/decrypt() More...
 
_getLambdaFunctions ()
 Holds the lambda_functions table (classwide) More...
 
 _hashInlineCryptFunction ($bytes)
 Generates a digest from $bytes. More...
 

Data Fields

const MODE_3CBC = -2
 Encrypt / decrypt using inner chaining. More...
 
const MODE_CBC3 = Base::MODE_CBC
 Encrypt / decrypt using outer chaining. More...
 
 $key_length = 24
 
 $password_default_salt = 'phpseclib'
 
 $cipher_name_mcrypt = 'tripledes'
 
 $cfb_init_len = 750
 
 $key_length_max = 24
 
 $mode_3cbc
 
 $des
 
- Data Fields inherited from phpseclib\Crypt\DES
const ENCRYPT = 0
 #+ @access private More...
 
const DECRYPT = 1
 Contains $keys[self::DECRYPT]. More...
 
 $block_size = 8
 
 $key_length = 8
 
 $cipher_name_mcrypt = 'des'
 
 $openssl_mode_names
 
 $cfb_init_len = 500
 
 $des_rounds = 1
 
 $key_length_max = 8
 
 $keys
 
 $shuffle
 
 $ipmap
 
 $invipmap
 
 $sbox1
 
 $sbox2
 
 $sbox3
 
 $sbox4
 
 $sbox5
 
 $sbox6
 
 $sbox7
 
 $sbox8
 
- Data Fields inherited from phpseclib\Crypt\Base
const MODE_CTR = -1
 #+ @access public More...
 
const MODE_ECB = 1
 Encrypt / decrypt using the Electronic Code Book mode. More...
 
const MODE_CBC = 2
 Encrypt / decrypt using the Code Book Chaining mode. More...
 
const MODE_CFB = 3
 Encrypt / decrypt using the Cipher Feedback mode. More...
 
const MODE_OFB = 4
 Encrypt / decrypt using the Output Feedback mode. More...
 
const MODE_STREAM = 5
 Encrypt / decrypt using streaming mode. More...
 
const ENGINE_INTERNAL = 1
 #+ @access private More...
 
const ENGINE_MCRYPT = 2
 Base value for the mcrypt implementation $engine switch. More...
 
const ENGINE_OPENSSL = 3
 Base value for the mcrypt implementation $engine switch. More...
 
 $mode
 
 $block_size = 16
 
 $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 
 $iv
 
 $encryptIV
 
 $decryptIV
 
 $continuousBuffer = false
 
 $enbuffer
 
 $debuffer
 
 $enmcrypt
 
 $demcrypt
 
 $enchanged = true
 
 $dechanged = true
 
 $ecb
 
 $cfb_init_len = 600
 
 $changed = true
 
 $padding = true
 
 $paddable = false
 
 $engine
 
 $preferredEngine
 
 $cipher_name_mcrypt
 
 $cipher_name_openssl
 
 $cipher_name_openssl_ecb
 
 $password_default_salt = 'phpseclib/salt'
 
 $inline_crypt
 
 $use_inline_crypt
 
 $openssl_emulate_ctr = false
 
 $openssl_options
 
 $explicit_key_length = false
 
 $skip_key_adjustment = false
 

Additional Inherited Members

- Static Public Attributes inherited from phpseclib\Crypt\Base
static $WHIRLPOOL_AVAILABLE
 

Detailed Description

Definition at line 49 of file TripleDES.php.

Constructor & Destructor Documentation

◆ __construct()

phpseclib\Crypt\TripleDES::__construct (   $mode = Base::MODE_CBC)

Default Constructor.

Determines whether or not the mcrypt extension should be used.

$mode could be:

  • \phpseclib\Crypt\Base::MODE_ECB
  • \phpseclib\Crypt\Base::MODE_CBC
  • \phpseclib\Crypt\Base::MODE_CTR
  • \phpseclib\Crypt\Base::MODE_CFB
  • \phpseclib\Crypt\Base::MODE_OFB
  • \phpseclib\Crypt\TripleDES::MODE_3CBC

If not explicitly set, \phpseclib\Crypt\Base::MODE_CBC will be used.

See also
\phpseclib\Crypt\DES::__construct()
\phpseclib\Crypt\Base::__construct()
Parameters
int$mode@access public

Reimplemented from phpseclib\Crypt\Base.

Definition at line 157 of file TripleDES.php.

158 {
159 switch ($mode) {
160 // In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC
161 // and additional flag us internally as 3CBC
162 case self::MODE_3CBC:
163 parent::__construct(Base::MODE_CBC);
164 $this->mode_3cbc = true;
165
166 // This three $des'es will do the 3CBC work (if $key > 64bits)
167 $this->des = array(
168 new DES(Base::MODE_CBC),
169 new DES(Base::MODE_CBC),
170 new DES(Base::MODE_CBC),
171 );
172
173 // we're going to be doing the padding, ourselves, so disable it in the \phpseclib\Crypt\DES objects
174 $this->des[0]->disablePadding();
175 $this->des[1]->disablePadding();
176 $this->des[2]->disablePadding();
177 break;
178 // If not 3CBC, we init as usual
179 default:
180 parent::__construct($mode);
181 }
182 }
const MODE_CBC
Encrypt / decrypt using the Code Book Chaining mode.
Definition: Base.php:74
const MODE_3CBC
Encrypt / decrypt using inner chaining.
Definition: TripleDES.php:56
Pure-PHP implementation of DES.

References phpseclib\Crypt\Base\$mode, phpseclib\Crypt\TripleDES\MODE_3CBC, and phpseclib\Crypt\Base\MODE_CBC.

Member Function Documentation

◆ _setupKey()

phpseclib\Crypt\TripleDES::_setupKey ( )

Creates the key schedule.

See also
\phpseclib\Crypt\DES::_setupKey()
\phpseclib\Crypt\Base::_setupKey() @access private

Reimplemented from phpseclib\Crypt\DES.

Definition at line 416 of file TripleDES.php.

417 {
418 switch (true) {
419 // if $key <= 64bits we configure our internal pure-php cipher engine
420 // to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
421 case strlen($this->key) <= 8:
422 $this->des_rounds = 1;
423 break;
424
425 // otherwise, if $key > 64bits, we configure our engine to work as 3DES.
426 default:
427 $this->des_rounds = 3;
428
429 // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
430 if ($this->mode_3cbc) {
431 $this->des[0]->_setupKey();
432 $this->des[1]->_setupKey();
433 $this->des[2]->_setupKey();
434
435 // because $des[0-2] will, now, do all the work we can return here
436 // not need unnecessary stress parent::_setupKey() with our, now unused, $key.
437 return;
438 }
439 }
440 // setup our key
441 parent::_setupKey();
442 }

◆ decrypt()

phpseclib\Crypt\TripleDES::decrypt (   $ciphertext)

Decrypts a message.

See also
\phpseclib\Crypt\Base::decrypt() @access User interface
Parameters
string$ciphertext
Returns
string $plaintext

Reimplemented from phpseclib\Crypt\Base.

Definition at line 325 of file TripleDES.php.

326 {
327 if ($this->mode_3cbc && strlen($this->key) > 8) {
328 return $this->_unpad(
329 $this->des[0]->decrypt(
330 $this->des[1]->encrypt(
331 $this->des[2]->decrypt(
332 str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0")
333 )
334 )
335 )
336 );
337 }
338
339 return parent::decrypt($ciphertext);
340 }
_unpad($text)
Unpads a string.
Definition: Base.php:1851
decrypt($ciphertext)
Decrypts a message.
Definition: TripleDES.php:325
encrypt($plaintext)
Encrypts a message.
Definition: TripleDES.php:298

References phpseclib\Crypt\Base\_unpad(), phpseclib\Crypt\TripleDES\decrypt(), and phpseclib\Crypt\TripleDES\encrypt().

Referenced by phpseclib\Crypt\TripleDES\decrypt(), and phpseclib\Crypt\TripleDES\encrypt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ disableContinuousBuffer()

phpseclib\Crypt\TripleDES::disableContinuousBuffer ( )

Treat consecutive packets as if they are a discontinuous buffer.

The default behavior.

See also
\phpseclib\Crypt\Base::disableContinuousBuffer()
self::enableContinuousBuffer() @access User interface

Reimplemented from phpseclib\Crypt\Base.

Definition at line 399 of file TripleDES.php.

400 {
401 parent::disableContinuousBuffer();
402 if ($this->mode_3cbc) {
403 $this->des[0]->disableContinuousBuffer();
404 $this->des[1]->disableContinuousBuffer();
405 $this->des[2]->disableContinuousBuffer();
406 }
407 }

◆ enableContinuousBuffer()

phpseclib\Crypt\TripleDES::enableContinuousBuffer ( )

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:

echo $des->encrypt(substr($plaintext, 0, 8)); echo $des->encrypt(substr($plaintext, 8, 8)); echo $des->encrypt($plaintext);

The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates another, as demonstrated with the following:

$des->encrypt(substr($plaintext, 0, 8)); echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));

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 \phpseclib\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.

See also
\phpseclib\Crypt\Base::enableContinuousBuffer()
self::disableContinuousBuffer() @access User interface

Reimplemented from phpseclib\Crypt\Base.

Definition at line 380 of file TripleDES.php.

381 {
382 parent::enableContinuousBuffer();
383 if ($this->mode_3cbc) {
384 $this->des[0]->enableContinuousBuffer();
385 $this->des[1]->enableContinuousBuffer();
386 $this->des[2]->enableContinuousBuffer();
387 }
388 }

◆ encrypt()

phpseclib\Crypt\TripleDES::encrypt (   $plaintext)

Encrypts a message.

See also
\phpseclib\Crypt\Base::encrypt() @access User interface
Parameters
string$plaintext
Returns
string $cipertext

Reimplemented from phpseclib\Crypt\Base.

Definition at line 298 of file TripleDES.php.

299 {
300 // parent::en/decrypt() is able to do all the work for all modes and keylengths,
301 // except for: self::MODE_3CBC (inner chaining CBC) with a key > 64bits
302
303 // if the key is smaller then 8, do what we'd normally do
304 if ($this->mode_3cbc && strlen($this->key) > 8) {
305 return $this->des[2]->encrypt(
306 $this->des[1]->decrypt(
307 $this->des[0]->encrypt(
308 $this->_pad($plaintext)
309 )
310 )
311 );
312 }
313
314 return parent::encrypt($plaintext);
315 }
_pad($text)
Pads a string.
Definition: Base.php:1822

References phpseclib\Crypt\Base\_pad(), phpseclib\Crypt\TripleDES\decrypt(), and phpseclib\Crypt\TripleDES\encrypt().

Referenced by phpseclib\Crypt\TripleDES\decrypt(), and phpseclib\Crypt\TripleDES\encrypt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isValidEngine()

phpseclib\Crypt\TripleDES::isValidEngine (   $engine)

Test for engine validity.

This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine()

See also
\phpseclib\Crypt\Base::__construct()
Parameters
int$engine@access public
Returns
bool

Reimplemented from phpseclib\Crypt\DES.

Definition at line 194 of file TripleDES.php.

195 {
196 if ($engine == self::ENGINE_OPENSSL) {
197 $this->cipher_name_openssl_ecb = 'des-ede3';
199 $this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode;
200 }
201
202 return parent::isValidEngine($engine);
203 }
_openssl_translate_mode()
phpseclib <-> OpenSSL Mode Mapper
Definition: Base.php:1423

References phpseclib\Crypt\Base\$engine, phpseclib\Crypt\Base\$mode, and phpseclib\Crypt\Base\_openssl_translate_mode().

+ Here is the call graph for this function:

◆ setIV()

phpseclib\Crypt\TripleDES::setIV (   $iv)

Sets the initialization vector.

(optional)

SetIV is not required when \phpseclib\Crypt\Base::MODE_ECB is being used. If not explicitly set, it'll be assumed to be all zero's.

See also
\phpseclib\Crypt\Base::setIV() @access User interface
Parameters
string$iv

Reimplemented from phpseclib\Crypt\Base.

Definition at line 215 of file TripleDES.php.

216 {
217 parent::setIV($iv);
218 if ($this->mode_3cbc) {
219 $this->des[0]->setIV($iv);
220 $this->des[1]->setIV($iv);
221 $this->des[2]->setIV($iv);
222 }
223 }

References phpseclib\Crypt\Base\$iv.

◆ setKey()

phpseclib\Crypt\TripleDES::setKey (   $key)

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.

If the key is not explicitly set, it'll be assumed to be all null bytes.

@access public

See also
\phpseclib\Crypt\DES::setKey()
\phpseclib\Crypt\Base::setKey()
Parameters
string$key

Reimplemented from phpseclib\Crypt\DES.

Definition at line 266 of file TripleDES.php.

267 {
268 $length = $this->explicit_key_length ? $this->key_length : strlen($key);
269 if ($length > 8) {
270 $key = str_pad(substr($key, 0, 24), 24, chr(0));
271 // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
272 // http://php.net/function.mcrypt-encrypt#47973
273 $key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
274 } else {
275 $key = str_pad($key, 8, chr(0));
276 }
277 parent::setKey($key);
278
279 // And in case of self::MODE_3CBC:
280 // if key <= 64bits we not need the 3 $des to work,
281 // because we will then act as regular DES-CBC with just a <= 64bit key.
282 // So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des.
283 if ($this->mode_3cbc && $length > 8) {
284 $this->des[0]->setKey(substr($key, 0, 8));
285 $this->des[1]->setKey(substr($key, 8, 8));
286 $this->des[2]->setKey(substr($key, 16, 8));
287 }
288 }

References phpseclib\Crypt\Base\$key.

◆ setKeyLength()

phpseclib\Crypt\TripleDES::setKeyLength (   $length)

Sets the key length.

Valid key lengths are 64, 128 and 192

See also
\phpseclib\Crypt\Base:setKeyLength() @access User interface
Parameters
int$length

Reimplemented from phpseclib\Crypt\Base.

Definition at line 234 of file TripleDES.php.

235 {
236 $length >>= 3;
237 switch (true) {
238 case $length <= 8:
239 $this->key_length = 8;
240 break;
241 case $length <= 16:
242 $this->key_length = 16;
243 break;
244 default:
245 $this->key_length = 24;
246 }
247
248 parent::setKeyLength($length);
249 }

◆ setPreferredEngine()

phpseclib\Crypt\TripleDES::setPreferredEngine (   $engine)

Sets the internal crypt engine.

See also
\phpseclib\Crypt\Base::__construct()
\phpseclib\Crypt\Base::setPreferredEngine()
Parameters
int$engine@access public
Returns
int

Reimplemented from phpseclib\Crypt\Base.

Definition at line 453 of file TripleDES.php.

454 {
455 if ($this->mode_3cbc) {
456 $this->des[0]->setPreferredEngine($engine);
457 $this->des[1]->setPreferredEngine($engine);
458 $this->des[2]->setPreferredEngine($engine);
459 }
460
461 return parent::setPreferredEngine($engine);
462 }

References phpseclib\Crypt\Base\$engine.

Field Documentation

◆ $cfb_init_len

phpseclib\Crypt\TripleDES::$cfb_init_len = 750

Definition at line 101 of file TripleDES.php.

◆ $cipher_name_mcrypt

phpseclib\Crypt\TripleDES::$cipher_name_mcrypt = 'tripledes'

Definition at line 92 of file TripleDES.php.

◆ $des

phpseclib\Crypt\TripleDES::$des

Definition at line 129 of file TripleDES.php.

◆ $key_length

phpseclib\Crypt\TripleDES::$key_length = 24

Definition at line 72 of file TripleDES.php.

◆ $key_length_max

phpseclib\Crypt\TripleDES::$key_length_max = 24

Definition at line 111 of file TripleDES.php.

◆ $mode_3cbc

phpseclib\Crypt\TripleDES::$mode_3cbc

Definition at line 119 of file TripleDES.php.

◆ $password_default_salt

phpseclib\Crypt\TripleDES::$password_default_salt = 'phpseclib'

Definition at line 82 of file TripleDES.php.

◆ MODE_3CBC

const phpseclib\Crypt\TripleDES::MODE_3CBC = -2

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).

Definition at line 56 of file TripleDES.php.

Referenced by phpseclib\Crypt\TripleDES\__construct(), and phpseclib\Net\SSH1\_connect().

◆ MODE_CBC3

const phpseclib\Crypt\TripleDES::MODE_CBC3 = Base::MODE_CBC

Encrypt / decrypt using outer chaining.

Outer chaining is used by SSH-2 and when the mode is set to \phpseclib\Crypt\Base::MODE_CBC.

Definition at line 63 of file TripleDES.php.


The documentation for this class was generated from the following file: