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

Public Member Functions

 setKeyLength ($length)
 Sets the key length. More...
 
 isValidEngine ($engine)
 Test for engine validity. More...
 
 _setupKey ()
 Setup the key (expansion) More...
 
 _encryptBlock ($in)
 Encrypts a block. More...
 
 _decryptBlock ($in)
 Decrypts a block. 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

 $block_size = 8
 
 $cipher_name_mcrypt = 'blowfish'
 
 $cfb_init_len = 500
 
 $sbox0
 
 $sbox1
 
 $sbox2
 
 $sbox3
 
 $parray
 
 $bctx
 
 $kl
 
 $key_length = 16
 
- Data Fields inherited from phpseclib\Crypt\Base
const MODE_CTR = -1
 #+ 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
 #+ 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 50 of file Blowfish.php.

Member Function Documentation

◆ _decryptBlock()

phpseclib\Crypt\Blowfish::_decryptBlock (   $in)

Decrypts a block.

private

Parameters
string$in
Returns
string

Definition at line 431 of file Blowfish.php.

References $i, $in, $l, and $r.

432  {
433  $p = $this->bctx["p"];
434  $sb_0 = $this->bctx["sb"][0];
435  $sb_1 = $this->bctx["sb"][1];
436  $sb_2 = $this->bctx["sb"][2];
437  $sb_3 = $this->bctx["sb"][3];
438 
439  $in = unpack("N*", $in);
440  $l = $in[1];
441  $r = $in[2];
442 
443  for ($i = 17; $i > 2; $i-= 2) {
444  $l^= $p[$i];
445  $r^= ($sb_0[$l >> 24 & 0xff] +
446  $sb_1[$l >> 16 & 0xff] ^
447  $sb_2[$l >> 8 & 0xff]) +
448  $sb_3[$l & 0xff];
449 
450  $r^= $p[$i - 1];
451  $l^= ($sb_0[$r >> 24 & 0xff] +
452  $sb_1[$r >> 16 & 0xff] ^
453  $sb_2[$r >> 8 & 0xff]) +
454  $sb_3[$r & 0xff];
455  }
456  return pack("N*", $r ^ $p[0], $l ^ $p[1]);
457  }
$r
Definition: example_031.php:79
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
global $l
Definition: afr.php:30
$i
Definition: disco.tpl.php:19

◆ _encryptBlock()

phpseclib\Crypt\Blowfish::_encryptBlock (   $in)

Encrypts a block.

private

Parameters
string$in
Returns
string

Definition at line 395 of file Blowfish.php.

References $i, $in, $l, and $r.

Referenced by phpseclib\Crypt\Blowfish\_setupKey().

396  {
397  $p = $this->bctx["p"];
398  // extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower
399  $sb_0 = $this->bctx["sb"][0];
400  $sb_1 = $this->bctx["sb"][1];
401  $sb_2 = $this->bctx["sb"][2];
402  $sb_3 = $this->bctx["sb"][3];
403 
404  $in = unpack("N*", $in);
405  $l = $in[1];
406  $r = $in[2];
407 
408  for ($i = 0; $i < 16; $i+= 2) {
409  $l^= $p[$i];
410  $r^= ($sb_0[$l >> 24 & 0xff] +
411  $sb_1[$l >> 16 & 0xff] ^
412  $sb_2[$l >> 8 & 0xff]) +
413  $sb_3[$l & 0xff];
414 
415  $r^= $p[$i + 1];
416  $l^= ($sb_0[$r >> 24 & 0xff] +
417  $sb_1[$r >> 16 & 0xff] ^
418  $sb_2[$r >> 8 & 0xff]) +
419  $sb_3[$r & 0xff];
420  }
421  return pack("N*", $r ^ $p[17], $l ^ $p[16]);
422  }
$r
Definition: example_031.php:79
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
global $l
Definition: afr.php:30
$i
Definition: disco.tpl.php:19
+ Here is the caller graph for this function:

◆ _setupInlineCrypt()

phpseclib\Crypt\Blowfish::_setupInlineCrypt ( )

Setup the performance-optimized function for de/encrypt()

See also
::_setupInlineCrypt() private

Definition at line 465 of file Blowfish.php.

References $i, phpseclib\Crypt\Base\_createInlineCryptFunction(), and phpseclib\Crypt\Base\_hashInlineCryptFunction().

466  {
467  $lambda_functions =& self::_getLambdaFunctions();
468 
469  // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
470  // (Currently, for Blowfish, one generated $lambda_function cost on php5.5@32bit ~100kb unfreeable mem and ~180kb on php5.5@64bit)
471  // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one.
472  $gen_hi_opt_code = (bool)(count($lambda_functions) < 10);
473 
474  // Generation of a unique hash for our generated code
475  $code_hash = "Crypt_Blowfish, {$this->mode}";
476  if ($gen_hi_opt_code) {
477  $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
478  }
479 
480  if (!isset($lambda_functions[$code_hash])) {
481  switch (true) {
482  case $gen_hi_opt_code:
483  $p = $this->bctx['p'];
484  $init_crypt = '
485  static $sb_0, $sb_1, $sb_2, $sb_3;
486  if (!$sb_0) {
487  $sb_0 = $self->bctx["sb"][0];
488  $sb_1 = $self->bctx["sb"][1];
489  $sb_2 = $self->bctx["sb"][2];
490  $sb_3 = $self->bctx["sb"][3];
491  }
492  ';
493  break;
494  default:
495  $p = array();
496  for ($i = 0; $i < 18; ++$i) {
497  $p[] = '$p_' . $i;
498  }
499  $init_crypt = '
500  list($sb_0, $sb_1, $sb_2, $sb_3) = $self->bctx["sb"];
501  list(' . implode(',', $p) . ') = $self->bctx["p"];
502 
503  ';
504  }
505 
506  // Generating encrypt code:
507  $encrypt_block = '
508  $in = unpack("N*", $in);
509  $l = $in[1];
510  $r = $in[2];
511  ';
512  for ($i = 0; $i < 16; $i+= 2) {
513  $encrypt_block.= '
514  $l^= ' . $p[$i] . ';
515  $r^= ($sb_0[$l >> 24 & 0xff] +
516  $sb_1[$l >> 16 & 0xff] ^
517  $sb_2[$l >> 8 & 0xff]) +
518  $sb_3[$l & 0xff];
519 
520  $r^= ' . $p[$i + 1] . ';
521  $l^= ($sb_0[$r >> 24 & 0xff] +
522  $sb_1[$r >> 16 & 0xff] ^
523  $sb_2[$r >> 8 & 0xff]) +
524  $sb_3[$r & 0xff];
525  ';
526  }
527  $encrypt_block.= '
528  $in = pack("N*",
529  $r ^ ' . $p[17] . ',
530  $l ^ ' . $p[16] . '
531  );
532  ';
533 
534  // Generating decrypt code:
535  $decrypt_block = '
536  $in = unpack("N*", $in);
537  $l = $in[1];
538  $r = $in[2];
539  ';
540 
541  for ($i = 17; $i > 2; $i-= 2) {
542  $decrypt_block.= '
543  $l^= ' . $p[$i] . ';
544  $r^= ($sb_0[$l >> 24 & 0xff] +
545  $sb_1[$l >> 16 & 0xff] ^
546  $sb_2[$l >> 8 & 0xff]) +
547  $sb_3[$l & 0xff];
548 
549  $r^= ' . $p[$i - 1] . ';
550  $l^= ($sb_0[$r >> 24 & 0xff] +
551  $sb_1[$r >> 16 & 0xff] ^
552  $sb_2[$r >> 8 & 0xff]) +
553  $sb_3[$r & 0xff];
554  ';
555  }
556 
557  $decrypt_block.= '
558  $in = pack("N*",
559  $r ^ ' . $p[0] . ',
560  $l ^ ' . $p[1] . '
561  );
562  ';
563 
564  $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
565  array(
566  'init_crypt' => $init_crypt,
567  'init_encrypt' => '',
568  'init_decrypt' => '',
569  'encrypt_block' => $encrypt_block,
570  'decrypt_block' => $decrypt_block
571  )
572  );
573  }
574  $this->inline_crypt = $lambda_functions[$code_hash];
575  }
_hashInlineCryptFunction($bytes)
Generates a digest from $bytes.
Definition: Base.php:2523
_createInlineCryptFunction($cipher_code)
Creates the performance-optimized function for en/decrypt()
Definition: Base.php:2142
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ _setupKey()

phpseclib\Crypt\Blowfish::_setupKey ( )

Setup the key (expansion)

See also
::_setupKey() private

Definition at line 338 of file Blowfish.php.

References $data, $i, phpseclib\Crypt\Base\$key, $l, $r, and phpseclib\Crypt\Blowfish\_encryptBlock().

339  {
340  if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
341  // already expanded
342  return;
343  }
344  $this->kl = array('key' => $this->key);
345 
346  /* key-expanding p[] and S-Box building sb[] */
347  $this->bctx = array(
348  'p' => array(),
349  'sb' => array(
350  $this->sbox0,
351  $this->sbox1,
352  $this->sbox2,
353  $this->sbox3
354  )
355  );
356 
357  // unpack binary string in unsigned chars
358  $key = array_values(unpack('C*', $this->key));
359  $keyl = count($key);
360  for ($j = 0, $i = 0; $i < 18; ++$i) {
361  // xor P1 with the first 32-bits of the key, xor P2 with the second 32-bits ...
362  for ($data = 0, $k = 0; $k < 4; ++$k) {
363  $data = ($data << 8) | $key[$j];
364  if (++$j >= $keyl) {
365  $j = 0;
366  }
367  }
368  $this->bctx['p'][] = $this->parray[$i] ^ $data;
369  }
370 
371  // encrypt the zero-string, replace P1 and P2 with the encrypted data,
372  // encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys
373  $data = "\0\0\0\0\0\0\0\0";
374  for ($i = 0; $i < 18; $i += 2) {
375  list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data)));
376  $this->bctx['p'][$i ] = $l;
377  $this->bctx['p'][$i + 1] = $r;
378  }
379  for ($i = 0; $i < 4; ++$i) {
380  for ($j = 0; $j < 256; $j += 2) {
381  list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data)));
382  $this->bctx['sb'][$i][$j ] = $l;
383  $this->bctx['sb'][$i][$j + 1] = $r;
384  }
385  }
386  }
$r
Definition: example_031.php:79
_encryptBlock($in)
Encrypts a block.
Definition: Blowfish.php:395
global $l
Definition: afr.php:30
$i
Definition: disco.tpl.php:19
$data
Definition: bench.php:6
+ Here is the call graph for this function:

◆ isValidEngine()

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

Test for engine validity.

This is mainly just a wrapper to set things up for ::isValidEngine()

See also
::isValidEngine()
Parameters
int$enginepublic
Returns
bool

Definition at line 319 of file Blowfish.php.

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

320  {
321  if ($engine == self::ENGINE_OPENSSL) {
322  if ($this->key_length != 16) {
323  return false;
324  }
325  $this->cipher_name_openssl_ecb = 'bf-ecb';
326  $this->cipher_name_openssl = 'bf-' . $this->_openssl_translate_mode();
327  }
328 
329  return parent::isValidEngine($engine);
330  }
_openssl_translate_mode()
phpseclib <-> OpenSSL Mode Mapper
Definition: Base.php:1423
+ Here is the call graph for this function:

◆ setKeyLength()

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

Sets the key length.

Key lengths can be between 32 and 448 bits.

public

Parameters
int$length

Definition at line 296 of file Blowfish.php.

297  {
298  if ($length < 32) {
299  $this->key_length = 7;
300  } elseif ($length > 448) {
301  $this->key_length = 56;
302  } else {
303  $this->key_length = $length >> 3;
304  }
305 
306  parent::setKeyLength($length);
307  }

Field Documentation

◆ $bctx

phpseclib\Crypt\Blowfish::$bctx

Definition at line 265 of file Blowfish.php.

◆ $block_size

phpseclib\Crypt\Blowfish::$block_size = 8

Definition at line 59 of file Blowfish.php.

◆ $cfb_init_len

phpseclib\Crypt\Blowfish::$cfb_init_len = 500

Definition at line 77 of file Blowfish.php.

◆ $cipher_name_mcrypt

phpseclib\Crypt\Blowfish::$cipher_name_mcrypt = 'blowfish'

Definition at line 68 of file Blowfish.php.

◆ $key_length

phpseclib\Crypt\Blowfish::$key_length = 16

Definition at line 286 of file Blowfish.php.

◆ $kl

phpseclib\Crypt\Blowfish::$kl

Definition at line 273 of file Blowfish.php.

◆ $parray

phpseclib\Crypt\Blowfish::$parray
Initial value:
= array(
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b
)

Definition at line 251 of file Blowfish.php.

◆ $sbox0

phpseclib\Crypt\Blowfish::$sbox0

Definition at line 87 of file Blowfish.php.

◆ $sbox1

phpseclib\Crypt\Blowfish::$sbox1

Definition at line 128 of file Blowfish.php.

◆ $sbox2

phpseclib\Crypt\Blowfish::$sbox2

Definition at line 169 of file Blowfish.php.

◆ $sbox3

phpseclib\Crypt\Blowfish::$sbox3

Definition at line 210 of file Blowfish.php.


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