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

Public Member Functions

 setKeyLength ($length)
 Sets the key length. More...
 
 _setupKey ()
 Setup the key (expansion) More...
 
 _mdsrem ($A, $B)
 _mdsrem function using by the twofish cipher algorithm 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

 $cipher_name_mcrypt = 'twofish'
 
 $cfb_init_len = 800
 
 $q0
 
 $q1
 
 $m0
 
 $m1
 
 $m2
 
 $m3
 
 $K = array()
 
 $S0 = array()
 
 $S1 = array()
 
 $S2 = array()
 
 $S3 = array()
 
 $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 Twofish.php.

Member Function Documentation

◆ _decryptBlock()

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

Decrypts a block.

private

Parameters
string$in
Returns
string

Definition at line 615 of file Twofish.php.

References $in, phpseclib\Crypt\Twofish\$K, phpseclib\Crypt\Twofish\$S0, phpseclib\Crypt\Twofish\$S1, phpseclib\Crypt\Twofish\$S2, and phpseclib\Crypt\Twofish\$S3.

616  {
617  $S0 = $this->S0;
618  $S1 = $this->S1;
619  $S2 = $this->S2;
620  $S3 = $this->S3;
621  $K = $this->K;
622 
623  $in = unpack("V4", $in);
624  $R0 = $K[4] ^ $in[1];
625  $R1 = $K[5] ^ $in[2];
626  $R2 = $K[6] ^ $in[3];
627  $R3 = $K[7] ^ $in[4];
628 
629  $ki = 40;
630  while ($ki > 8) {
631  $t0 = $S0[$R0 & 0xff] ^
632  $S1[$R0 >> 8 & 0xff] ^
633  $S2[$R0 >> 16 & 0xff] ^
634  $S3[$R0 >> 24 & 0xff];
635  $t1 = $S0[$R1 >> 24 & 0xff] ^
636  $S1[$R1 & 0xff] ^
637  $S2[$R1 >> 8 & 0xff] ^
638  $S3[$R1 >> 16 & 0xff];
639  $R3^= $t0 + ($t1 << 1) + $K[--$ki];
640  $R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31;
641  $R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ($t0 + $t1 + $K[--$ki]);
642 
643  $t0 = $S0[$R2 & 0xff] ^
644  $S1[$R2 >> 8 & 0xff] ^
645  $S2[$R2 >> 16 & 0xff] ^
646  $S3[$R2 >> 24 & 0xff];
647  $t1 = $S0[$R3 >> 24 & 0xff] ^
648  $S1[$R3 & 0xff] ^
649  $S2[$R3 >> 8 & 0xff] ^
650  $S3[$R3 >> 16 & 0xff];
651  $R1^= $t0 + ($t1 << 1) + $K[--$ki];
652  $R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31;
653  $R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ($t0 + $t1 + $K[--$ki]);
654  }
655 
656  // @codingStandardsIgnoreStart
657  return pack("V4", $K[0] ^ $R2,
658  $K[1] ^ $R3,
659  $K[2] ^ $R0,
660  $K[3] ^ $R1);
661  // @codingStandardsIgnoreEnd
662  }
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37

◆ _encryptBlock()

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

Encrypts a block.

private

Parameters
string$in
Returns
string

Definition at line 559 of file Twofish.php.

References $in, phpseclib\Crypt\Twofish\$K, phpseclib\Crypt\Twofish\$S0, phpseclib\Crypt\Twofish\$S1, phpseclib\Crypt\Twofish\$S2, and phpseclib\Crypt\Twofish\$S3.

560  {
561  $S0 = $this->S0;
562  $S1 = $this->S1;
563  $S2 = $this->S2;
564  $S3 = $this->S3;
565  $K = $this->K;
566 
567  $in = unpack("V4", $in);
568  $R0 = $K[0] ^ $in[1];
569  $R1 = $K[1] ^ $in[2];
570  $R2 = $K[2] ^ $in[3];
571  $R3 = $K[3] ^ $in[4];
572 
573  $ki = 7;
574  while ($ki < 39) {
575  $t0 = $S0[ $R0 & 0xff] ^
576  $S1[($R0 >> 8) & 0xff] ^
577  $S2[($R0 >> 16) & 0xff] ^
578  $S3[($R0 >> 24) & 0xff];
579  $t1 = $S0[($R1 >> 24) & 0xff] ^
580  $S1[ $R1 & 0xff] ^
581  $S2[($R1 >> 8) & 0xff] ^
582  $S3[($R1 >> 16) & 0xff];
583  $R2^= $t0 + $t1 + $K[++$ki];
584  $R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31);
585  $R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ($t0 + ($t1 << 1) + $K[++$ki]);
586 
587  $t0 = $S0[ $R2 & 0xff] ^
588  $S1[($R2 >> 8) & 0xff] ^
589  $S2[($R2 >> 16) & 0xff] ^
590  $S3[($R2 >> 24) & 0xff];
591  $t1 = $S0[($R3 >> 24) & 0xff] ^
592  $S1[ $R3 & 0xff] ^
593  $S2[($R3 >> 8) & 0xff] ^
594  $S3[($R3 >> 16) & 0xff];
595  $R0^= ($t0 + $t1 + $K[++$ki]);
596  $R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31);
597  $R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ($t0 + ($t1 << 1) + $K[++$ki]);
598  }
599 
600  // @codingStandardsIgnoreStart
601  return pack("V4", $K[4] ^ $R2,
602  $K[5] ^ $R3,
603  $K[6] ^ $R0,
604  $K[7] ^ $R1);
605  // @codingStandardsIgnoreEnd
606  }
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37

◆ _mdsrem()

phpseclib\Crypt\Twofish::_mdsrem (   $A,
  $B 
)

_mdsrem function using by the twofish cipher algorithm

private

Parameters
string$A
string$B
Returns
array

Definition at line 512 of file Twofish.php.

References $i, and $t.

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

513  {
514  // No gain by unrolling this loop.
515  for ($i = 0; $i < 8; ++$i) {
516  // Get most significant coefficient.
517  $t = 0xff & ($B >> 24);
518 
519  // Shift the others up.
520  $B = ($B << 8) | (0xff & ($A >> 24));
521  $A<<= 8;
522 
523  $u = $t << 1;
524 
525  // Subtract the modular polynomial on overflow.
526  if ($t & 0x80) {
527  $u^= 0x14d;
528  }
529 
530  // Remove t * (a * x^2 + 1).
531  $B ^= $t ^ ($u << 16);
532 
533  // Form u = a*t + t/a = t*(a + 1/a).
534  $u^= 0x7fffffff & ($t >> 1);
535 
536  // Add the modular polynomial on underflow.
537  if ($t & 0x01) {
538  $u^= 0xa6 ;
539  }
540 
541  // Remove t * (a + 1/a) * (x^3 + x).
542  $B^= ($u << 24) | ($u << 8);
543  }
544 
545  return array(
546  0xff & $B >> 24,
547  0xff & $B >> 16,
548  0xff & $B >> 8,
549  0xff & $B);
550  }
$i
Definition: disco.tpl.php:19
+ Here is the caller graph for this function:

◆ _setupInlineCrypt()

phpseclib\Crypt\Twofish::_setupInlineCrypt ( )

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

See also
::_setupInlineCrypt() private

Definition at line 670 of file Twofish.php.

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

671  {
672  $lambda_functions =& self::_getLambdaFunctions();
673 
674  // Max. 10 Ultra-Hi-optimized inline-crypt functions. After that, we'll (still) create very fast code, but not the ultimate fast one.
675  // (Currently, for Crypt_Twofish, one generated $lambda_function cost on php5.5@32bit ~140kb unfreeable mem and ~240kb on php5.5@64bit)
676  $gen_hi_opt_code = (bool)(count($lambda_functions) < 10);
677 
678  // Generation of a uniqe hash for our generated code
679  $code_hash = "Crypt_Twofish, {$this->mode}";
680  if ($gen_hi_opt_code) {
681  $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
682  }
683 
684  if (!isset($lambda_functions[$code_hash])) {
685  switch (true) {
686  case $gen_hi_opt_code:
687  $K = $this->K;
688  $init_crypt = '
689  static $S0, $S1, $S2, $S3;
690  if (!$S0) {
691  for ($i = 0; $i < 256; ++$i) {
692  $S0[] = (int)$self->S0[$i];
693  $S1[] = (int)$self->S1[$i];
694  $S2[] = (int)$self->S2[$i];
695  $S3[] = (int)$self->S3[$i];
696  }
697  }
698  ';
699  break;
700  default:
701  $K = array();
702  for ($i = 0; $i < 40; ++$i) {
703  $K[] = '$K_' . $i;
704  }
705  $init_crypt = '
706  $S0 = $self->S0;
707  $S1 = $self->S1;
708  $S2 = $self->S2;
709  $S3 = $self->S3;
710  list(' . implode(',', $K) . ') = $self->K;
711  ';
712  }
713 
714  // Generating encrypt code:
715  $encrypt_block = '
716  $in = unpack("V4", $in);
717  $R0 = '.$K[0].' ^ $in[1];
718  $R1 = '.$K[1].' ^ $in[2];
719  $R2 = '.$K[2].' ^ $in[3];
720  $R3 = '.$K[3].' ^ $in[4];
721  ';
722  for ($ki = 7, $i = 0; $i < 8; ++$i) {
723  $encrypt_block.= '
724  $t0 = $S0[ $R0 & 0xff] ^
725  $S1[($R0 >> 8) & 0xff] ^
726  $S2[($R0 >> 16) & 0xff] ^
727  $S3[($R0 >> 24) & 0xff];
728  $t1 = $S0[($R1 >> 24) & 0xff] ^
729  $S1[ $R1 & 0xff] ^
730  $S2[($R1 >> 8) & 0xff] ^
731  $S3[($R1 >> 16) & 0xff];
732  $R2^= ($t0 + $t1 + '.$K[++$ki].');
733  $R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31);
734  $R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ($t0 + ($t1 << 1) + '.$K[++$ki].');
735 
736  $t0 = $S0[ $R2 & 0xff] ^
737  $S1[($R2 >> 8) & 0xff] ^
738  $S2[($R2 >> 16) & 0xff] ^
739  $S3[($R2 >> 24) & 0xff];
740  $t1 = $S0[($R3 >> 24) & 0xff] ^
741  $S1[ $R3 & 0xff] ^
742  $S2[($R3 >> 8) & 0xff] ^
743  $S3[($R3 >> 16) & 0xff];
744  $R0^= ($t0 + $t1 + '.$K[++$ki].');
745  $R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31);
746  $R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ($t0 + ($t1 << 1) + '.$K[++$ki].');
747  ';
748  }
749  $encrypt_block.= '
750  $in = pack("V4", '.$K[4].' ^ $R2,
751  '.$K[5].' ^ $R3,
752  '.$K[6].' ^ $R0,
753  '.$K[7].' ^ $R1);
754  ';
755 
756  // Generating decrypt code:
757  $decrypt_block = '
758  $in = unpack("V4", $in);
759  $R0 = '.$K[4].' ^ $in[1];
760  $R1 = '.$K[5].' ^ $in[2];
761  $R2 = '.$K[6].' ^ $in[3];
762  $R3 = '.$K[7].' ^ $in[4];
763  ';
764  for ($ki = 40, $i = 0; $i < 8; ++$i) {
765  $decrypt_block.= '
766  $t0 = $S0[$R0 & 0xff] ^
767  $S1[$R0 >> 8 & 0xff] ^
768  $S2[$R0 >> 16 & 0xff] ^
769  $S3[$R0 >> 24 & 0xff];
770  $t1 = $S0[$R1 >> 24 & 0xff] ^
771  $S1[$R1 & 0xff] ^
772  $S2[$R1 >> 8 & 0xff] ^
773  $S3[$R1 >> 16 & 0xff];
774  $R3^= $t0 + ($t1 << 1) + '.$K[--$ki].';
775  $R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31;
776  $R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ($t0 + $t1 + '.$K[--$ki].');
777 
778  $t0 = $S0[$R2 & 0xff] ^
779  $S1[$R2 >> 8 & 0xff] ^
780  $S2[$R2 >> 16 & 0xff] ^
781  $S3[$R2 >> 24 & 0xff];
782  $t1 = $S0[$R3 >> 24 & 0xff] ^
783  $S1[$R3 & 0xff] ^
784  $S2[$R3 >> 8 & 0xff] ^
785  $S3[$R3 >> 16 & 0xff];
786  $R1^= $t0 + ($t1 << 1) + '.$K[--$ki].';
787  $R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31;
788  $R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ($t0 + $t1 + '.$K[--$ki].');
789  ';
790  }
791  $decrypt_block.= '
792  $in = pack("V4", '.$K[0].' ^ $R2,
793  '.$K[1].' ^ $R3,
794  '.$K[2].' ^ $R0,
795  '.$K[3].' ^ $R1);
796  ';
797 
798  $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
799  array(
800  'init_crypt' => $init_crypt,
801  'init_encrypt' => '',
802  'init_decrypt' => '',
803  'encrypt_block' => $encrypt_block,
804  'decrypt_block' => $decrypt_block
805  )
806  );
807  }
808  $this->inline_crypt = $lambda_functions[$code_hash];
809  }
_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\Twofish::_setupKey ( )

Setup the key (expansion)

See also
::_setupKey() private

Definition at line 403 of file Twofish.php.

References $i, phpseclib\Crypt\Twofish\$K, phpseclib\Crypt\Base\$key, phpseclib\Crypt\Twofish\$m0, phpseclib\Crypt\Twofish\$m1, phpseclib\Crypt\Twofish\$m2, phpseclib\Crypt\Twofish\$m3, phpseclib\Crypt\Twofish\$q0, phpseclib\Crypt\Twofish\$q1, phpseclib\Crypt\Twofish\$S0, phpseclib\Crypt\Twofish\$S1, phpseclib\Crypt\Twofish\$S2, phpseclib\Crypt\Twofish\$S3, $sc, and phpseclib\Crypt\Twofish\_mdsrem().

404  {
405  if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
406  // already expanded
407  return;
408  }
409  $this->kl = array('key' => $this->key);
410 
411  /* Key expanding and generating the key-depended s-boxes */
412  $le_longs = unpack('V*', $this->key);
413  $key = unpack('C*', $this->key);
414  $m0 = $this->m0;
415  $m1 = $this->m1;
416  $m2 = $this->m2;
417  $m3 = $this->m3;
418  $q0 = $this->q0;
419  $q1 = $this->q1;
420 
421  $K = $S0 = $S1 = $S2 = $S3 = array();
422 
423  switch (strlen($this->key)) {
424  case 16:
425  list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[1], $le_longs[2]);
426  list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[3], $le_longs[4]);
427  for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
428  $A = $m0[$q0[$q0[$i] ^ $key[ 9]] ^ $key[1]] ^
429  $m1[$q0[$q1[$i] ^ $key[10]] ^ $key[2]] ^
430  $m2[$q1[$q0[$i] ^ $key[11]] ^ $key[3]] ^
431  $m3[$q1[$q1[$i] ^ $key[12]] ^ $key[4]];
432  $B = $m0[$q0[$q0[$j] ^ $key[13]] ^ $key[5]] ^
433  $m1[$q0[$q1[$j] ^ $key[14]] ^ $key[6]] ^
434  $m2[$q1[$q0[$j] ^ $key[15]] ^ $key[7]] ^
435  $m3[$q1[$q1[$j] ^ $key[16]] ^ $key[8]];
436  $B = ($B << 8) | ($B >> 24 & 0xff);
437  $K[] = $A+= $B;
438  $K[] = (($A+= $B) << 9 | $A >> 23 & 0x1ff);
439  }
440  for ($i = 0; $i < 256; ++$i) {
441  $S0[$i] = $m0[$q0[$q0[$i] ^ $s4] ^ $s0];
442  $S1[$i] = $m1[$q0[$q1[$i] ^ $s5] ^ $s1];
443  $S2[$i] = $m2[$q1[$q0[$i] ^ $s6] ^ $s2];
444  $S3[$i] = $m3[$q1[$q1[$i] ^ $s7] ^ $s3];
445  }
446  break;
447  case 24:
448  list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[1], $le_longs[2]);
449  list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[3], $le_longs[4]);
450  list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[5], $le_longs[6]);
451  for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
452  $A = $m0[$q0[$q0[$q1[$i] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^
453  $m1[$q0[$q1[$q1[$i] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^
454  $m2[$q1[$q0[$q0[$i] ^ $key[19]] ^ $key[11]] ^ $key[3]] ^
455  $m3[$q1[$q1[$q0[$i] ^ $key[20]] ^ $key[12]] ^ $key[4]];
456  $B = $m0[$q0[$q0[$q1[$j] ^ $key[21]] ^ $key[13]] ^ $key[5]] ^
457  $m1[$q0[$q1[$q1[$j] ^ $key[22]] ^ $key[14]] ^ $key[6]] ^
458  $m2[$q1[$q0[$q0[$j] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^
459  $m3[$q1[$q1[$q0[$j] ^ $key[24]] ^ $key[16]] ^ $key[8]];
460  $B = ($B << 8) | ($B >> 24 & 0xff);
461  $K[] = $A+= $B;
462  $K[] = (($A+= $B) << 9 | $A >> 23 & 0x1ff);
463  }
464  for ($i = 0; $i < 256; ++$i) {
465  $S0[$i] = $m0[$q0[$q0[$q1[$i] ^ $s8] ^ $s4] ^ $s0];
466  $S1[$i] = $m1[$q0[$q1[$q1[$i] ^ $s9] ^ $s5] ^ $s1];
467  $S2[$i] = $m2[$q1[$q0[$q0[$i] ^ $sa] ^ $s6] ^ $s2];
468  $S3[$i] = $m3[$q1[$q1[$q0[$i] ^ $sb] ^ $s7] ^ $s3];
469  }
470  break;
471  default: // 32
472  list($sf, $se, $sd, $sc) = $this->_mdsrem($le_longs[1], $le_longs[2]);
473  list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[3], $le_longs[4]);
474  list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[5], $le_longs[6]);
475  list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[7], $le_longs[8]);
476  for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
477  $A = $m0[$q0[$q0[$q1[$q1[$i] ^ $key[25]] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^
478  $m1[$q0[$q1[$q1[$q0[$i] ^ $key[26]] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^
479  $m2[$q1[$q0[$q0[$q0[$i] ^ $key[27]] ^ $key[19]] ^ $key[11]] ^ $key[3]] ^
480  $m3[$q1[$q1[$q0[$q1[$i] ^ $key[28]] ^ $key[20]] ^ $key[12]] ^ $key[4]];
481  $B = $m0[$q0[$q0[$q1[$q1[$j] ^ $key[29]] ^ $key[21]] ^ $key[13]] ^ $key[5]] ^
482  $m1[$q0[$q1[$q1[$q0[$j] ^ $key[30]] ^ $key[22]] ^ $key[14]] ^ $key[6]] ^
483  $m2[$q1[$q0[$q0[$q0[$j] ^ $key[31]] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^
484  $m3[$q1[$q1[$q0[$q1[$j] ^ $key[32]] ^ $key[24]] ^ $key[16]] ^ $key[8]];
485  $B = ($B << 8) | ($B >> 24 & 0xff);
486  $K[] = $A+= $B;
487  $K[] = (($A+= $B) << 9 | $A >> 23 & 0x1ff);
488  }
489  for ($i = 0; $i < 256; ++$i) {
490  $S0[$i] = $m0[$q0[$q0[$q1[$q1[$i] ^ $sc] ^ $s8] ^ $s4] ^ $s0];
491  $S1[$i] = $m1[$q0[$q1[$q1[$q0[$i] ^ $sd] ^ $s9] ^ $s5] ^ $s1];
492  $S2[$i] = $m2[$q1[$q0[$q0[$q0[$i] ^ $se] ^ $sa] ^ $s6] ^ $s2];
493  $S3[$i] = $m3[$q1[$q1[$q0[$q1[$i] ^ $sf] ^ $sb] ^ $s7] ^ $s3];
494  }
495  }
496 
497  $this->K = $K;
498  $this->S0 = $S0;
499  $this->S1 = $S1;
500  $this->S2 = $S2;
501  $this->S3 = $S3;
502  }
_mdsrem($A, $B)
_mdsrem function using by the twofish cipher algorithm
Definition: Twofish.php:512
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ setKeyLength()

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

Sets the key length.

Valid key lengths are 128, 192 or 256 bits

public

Parameters
int$length

Definition at line 381 of file Twofish.php.

382  {
383  switch (true) {
384  case $length <= 128:
385  $this->key_length = 16;
386  break;
387  case $length <= 192:
388  $this->key_length = 24;
389  break;
390  default:
391  $this->key_length = 32;
392  }
393 
394  parent::setKeyLength($length);
395  }

Field Documentation

◆ $cfb_init_len

phpseclib\Crypt\Twofish::$cfb_init_len = 800

Definition at line 68 of file Twofish.php.

◆ $cipher_name_mcrypt

phpseclib\Crypt\Twofish::$cipher_name_mcrypt = 'twofish'

Definition at line 59 of file Twofish.php.

◆ $K

◆ $key_length

phpseclib\Crypt\Twofish::$key_length = 16

Definition at line 371 of file Twofish.php.

◆ $kl

phpseclib\Crypt\Twofish::$kl

Definition at line 362 of file Twofish.php.

◆ $m0

phpseclib\Crypt\Twofish::$m0

Definition at line 158 of file Twofish.php.

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

◆ $m1

phpseclib\Crypt\Twofish::$m1

Definition at line 199 of file Twofish.php.

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

◆ $m2

phpseclib\Crypt\Twofish::$m2

Definition at line 240 of file Twofish.php.

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

◆ $m3

phpseclib\Crypt\Twofish::$m3

Definition at line 281 of file Twofish.php.

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

◆ $q0

phpseclib\Crypt\Twofish::$q0

Definition at line 76 of file Twofish.php.

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

◆ $q1

phpseclib\Crypt\Twofish::$q1

Definition at line 117 of file Twofish.php.

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

◆ $S0

phpseclib\Crypt\Twofish::$S0 = array()

◆ $S1

phpseclib\Crypt\Twofish::$S1 = array()

◆ $S2

phpseclib\Crypt\Twofish::$S2 = array()

◆ $S3

phpseclib\Crypt\Twofish::$S3 = array()

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