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

Public Member Functions

 __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_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
 

Static Public Attributes

static $WHIRLPOOL_AVAILABLE
 

Detailed Description

Definition at line 48 of file Base.php.

Constructor & Destructor Documentation

◆ __construct()

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

Default Constructor.

Determines whether or not the mcrypt extension should be used.

$mode could be:

  • self::MODE_ECB
  • self::MODE_CBC
  • self::MODE_CTR
  • self::MODE_CFB
  • self::MODE_OFB

If not explicitly set, self::MODE_CBC will be used.

Parameters
int$modepublic

Definition at line 474 of file Base.php.

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

475  {
476  // $mode dependent settings
477  switch ($mode) {
478  case self::MODE_ECB:
479  $this->paddable = true;
480  $this->mode = self::MODE_ECB;
481  break;
482  case self::MODE_CTR:
483  case self::MODE_CFB:
484  case self::MODE_OFB:
485  case self::MODE_STREAM:
486  $this->mode = $mode;
487  break;
488  case self::MODE_CBC:
489  default:
490  $this->paddable = true;
491  $this->mode = self::MODE_CBC;
492  }
493 
494  $this->_setEngine();
495 
496  // Determining whether inline crypting can be used by the cipher
497  if ($this->use_inline_crypt !== false && function_exists('create_function')) {
498  $this->use_inline_crypt = true;
499  }
500  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
+ Here is the call graph for this function:

Member Function Documentation

◆ _clearBuffers()

phpseclib\Crypt\Base::_clearBuffers ( )

Clears internal buffers.

Clearing/resetting the internal buffers is done everytime after disableContinuousBuffer() or on cipher $engine (re)init ie after setKey() or setIV()

public

Definition at line 1876 of file Base.php.

Referenced by phpseclib\Crypt\Base\_setup(), phpseclib\Crypt\Base\_setupMcrypt(), phpseclib\Crypt\Base\decrypt(), and phpseclib\Crypt\Base\encrypt().

1877  {
1878  $this->enbuffer = $this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
1879 
1880  // mcrypt's handling of invalid's $iv:
1881  // $this->encryptIV = $this->decryptIV = strlen($this->iv) == $this->block_size ? $this->iv : str_repeat("\0", $this->block_size);
1882  $this->encryptIV = $this->decryptIV = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, "\0");
1883 
1884  if (!$this->skip_key_adjustment) {
1885  $this->key = str_pad(substr($this->key, 0, $this->key_length), $this->key_length, "\0");
1886  }
1887  }
+ Here is the caller graph for this function:

◆ _createInlineCryptFunction()

phpseclib\Crypt\Base::_createInlineCryptFunction (   $cipher_code)

Creates the performance-optimized function for en/decrypt()

Internally for phpseclib developers:

_createInlineCryptFunction():

  • merge the $cipher_code [setup'ed by _setupInlineCrypt()] with the current [$this->]mode of operation code
  • create the $inline function, which called by encrypt() / decrypt() as its replacement to speed up the en/decryption operations.
  • return the name of the created $inline callback function
  • used to speed up en/decryption

The main reason why can speed up things [up to 50%] this way are:

  • using variables more effective then regular. (ie no use of expensive arrays but integers $k_0, $k_1 ... or even, for example, the pure $key[] values hardcoded)
  • avoiding 1000's of function calls of ie _encryptBlock() but inlining the crypt operations. in the mode of operation for() loop.
  • full loop unroll the (sometimes key-dependent) rounds avoiding this way ++$i counters and runtime-if's etc...

The basic code architectur of the generated $inline en/decrypt() lambda function, in pseudo php, is:

+-------------------------------------------------------------------------------------------—+ | callback $inline = create_function: | | lambda_function_0001_crypt_ECB($action, $text) | | { | | INSERT PHP CODE OF: | | $cipher_code['init_crypt']; // general init code. | | // ie: $sbox'es declarations used for |

// encrypt and decrypt'ing.
switch ($action) {
case 'encrypt':
INSERT PHP CODE OF:
$cipher_code['init_encrypt']; // encrypt sepcific init code.
ie: specified $key or $box
declarations for encrypt'ing.
foreach ($ciphertext) {
$in = $block_size of $ciphertext;
INSERT PHP CODE OF:
$cipher_code['encrypt_block']; // encrypt's (string) $in, which is always:
// strlen($in) == $this->block_size
// here comes the cipher algorithm in action
// for encryption.
// $cipher_code['encrypt_block'] has to
// encrypt the content of the $in variable
$plaintext .= $in;
}
return $plaintext;
case 'decrypt':
INSERT PHP CODE OF:
$cipher_code['init_decrypt']; // decrypt sepcific init code
ie: specified $key or $box
declarations for decrypt'ing.
foreach ($plaintext) {
$in = $block_size of $plaintext;
INSERT PHP CODE OF:
$cipher_code['decrypt_block']; // decrypt's (string) $in, which is always
// strlen($in) == $this->block_size
// here comes the cipher algorithm in action
// for decryption.
// $cipher_code['decrypt_block'] has to
// decrypt the content of the $in variable
$ciphertext .= $in;
}
return $ciphertext;
}
}

+-------------------------------------------------------------------------------------------—+

See also the *::_setupInlineCrypt()'s for productive inline $cipher_code's how they works.

Structure of: $cipher_code = array( 'init_crypt' => (string) '', // optional 'init_encrypt' => (string) '', // optional 'init_decrypt' => (string) '', // optional 'encrypt_block' => (string) '', // required 'decrypt_block' => (string) '' // required );

See also
self::_setupInlineCrypt()
self::encrypt()
self::decrypt()
Parameters
array$cipher_codeprivate
Returns
string (the name of the created callback function)

Definition at line 2142 of file Base.php.

References phpseclib\Crypt\Base\$block_size.

Referenced by phpseclib\Crypt\Blowfish\_setupInlineCrypt(), phpseclib\Crypt\RC2\_setupInlineCrypt(), phpseclib\Crypt\Twofish\_setupInlineCrypt(), phpseclib\Crypt\Rijndael\_setupInlineCrypt(), and phpseclib\Crypt\DES\_setupInlineCrypt().

2143  {
2145 
2146  // optional
2147  $init_crypt = isset($cipher_code['init_crypt']) ? $cipher_code['init_crypt'] : '';
2148  $init_encrypt = isset($cipher_code['init_encrypt']) ? $cipher_code['init_encrypt'] : '';
2149  $init_decrypt = isset($cipher_code['init_decrypt']) ? $cipher_code['init_decrypt'] : '';
2150  // required
2151  $encrypt_block = $cipher_code['encrypt_block'];
2152  $decrypt_block = $cipher_code['decrypt_block'];
2153 
2154  // Generating mode of operation inline code,
2155  // merged with the $cipher_code algorithm
2156  // for encrypt- and decryption.
2157  switch ($this->mode) {
2158  case self::MODE_ECB:
2159  $encrypt = $init_encrypt . '
2160  $_ciphertext = "";
2161  $_plaintext_len = strlen($_text);
2162 
2163  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2164  $in = substr($_text, $_i, '.$block_size.');
2165  '.$encrypt_block.'
2166  $_ciphertext.= $in;
2167  }
2168 
2169  return $_ciphertext;
2170  ';
2171 
2172  $decrypt = $init_decrypt . '
2173  $_plaintext = "";
2174  $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
2175  $_ciphertext_len = strlen($_text);
2176 
2177  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2178  $in = substr($_text, $_i, '.$block_size.');
2179  '.$decrypt_block.'
2180  $_plaintext.= $in;
2181  }
2182 
2183  return $self->_unpad($_plaintext);
2184  ';
2185  break;
2186  case self::MODE_CTR:
2187  $encrypt = $init_encrypt . '
2188  $_ciphertext = "";
2189  $_plaintext_len = strlen($_text);
2190  $_xor = $self->encryptIV;
2191  $_buffer = &$self->enbuffer;
2192  if (strlen($_buffer["ciphertext"])) {
2193  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2194  $_block = substr($_text, $_i, '.$block_size.');
2195  if (strlen($_block) > strlen($_buffer["ciphertext"])) {
2196  $in = $_xor;
2197  '.$encrypt_block.'
2198  $self->_increment_str($_xor);
2199  $_buffer["ciphertext"].= $in;
2200  }
2201  $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.');
2202  $_ciphertext.= $_block ^ $_key;
2203  }
2204  } else {
2205  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2206  $_block = substr($_text, $_i, '.$block_size.');
2207  $in = $_xor;
2208  '.$encrypt_block.'
2209  $self->_increment_str($_xor);
2210  $_key = $in;
2211  $_ciphertext.= $_block ^ $_key;
2212  }
2213  }
2214  if ($self->continuousBuffer) {
2215  $self->encryptIV = $_xor;
2216  if ($_start = $_plaintext_len % '.$block_size.') {
2217  $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
2218  }
2219  }
2220 
2221  return $_ciphertext;
2222  ';
2223 
2224  $decrypt = $init_encrypt . '
2225  $_plaintext = "";
2226  $_ciphertext_len = strlen($_text);
2227  $_xor = $self->decryptIV;
2228  $_buffer = &$self->debuffer;
2229 
2230  if (strlen($_buffer["ciphertext"])) {
2231  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2232  $_block = substr($_text, $_i, '.$block_size.');
2233  if (strlen($_block) > strlen($_buffer["ciphertext"])) {
2234  $in = $_xor;
2235  '.$encrypt_block.'
2236  $self->_increment_str($_xor);
2237  $_buffer["ciphertext"].= $in;
2238  }
2239  $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.');
2240  $_plaintext.= $_block ^ $_key;
2241  }
2242  } else {
2243  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2244  $_block = substr($_text, $_i, '.$block_size.');
2245  $in = $_xor;
2246  '.$encrypt_block.'
2247  $self->_increment_str($_xor);
2248  $_key = $in;
2249  $_plaintext.= $_block ^ $_key;
2250  }
2251  }
2252  if ($self->continuousBuffer) {
2253  $self->decryptIV = $_xor;
2254  if ($_start = $_ciphertext_len % '.$block_size.') {
2255  $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
2256  }
2257  }
2258 
2259  return $_plaintext;
2260  ';
2261  break;
2262  case self::MODE_CFB:
2263  $encrypt = $init_encrypt . '
2264  $_ciphertext = "";
2265  $_buffer = &$self->enbuffer;
2266 
2267  if ($self->continuousBuffer) {
2268  $_iv = &$self->encryptIV;
2269  $_pos = &$_buffer["pos"];
2270  } else {
2271  $_iv = $self->encryptIV;
2272  $_pos = 0;
2273  }
2274  $_len = strlen($_text);
2275  $_i = 0;
2276  if ($_pos) {
2277  $_orig_pos = $_pos;
2278  $_max = '.$block_size.' - $_pos;
2279  if ($_len >= $_max) {
2280  $_i = $_max;
2281  $_len-= $_max;
2282  $_pos = 0;
2283  } else {
2284  $_i = $_len;
2285  $_pos+= $_len;
2286  $_len = 0;
2287  }
2288  $_ciphertext = substr($_iv, $_orig_pos) ^ $_text;
2289  $_iv = substr_replace($_iv, $_ciphertext, $_orig_pos, $_i);
2290  }
2291  while ($_len >= '.$block_size.') {
2292  $in = $_iv;
2293  '.$encrypt_block.';
2294  $_iv = $in ^ substr($_text, $_i, '.$block_size.');
2295  $_ciphertext.= $_iv;
2296  $_len-= '.$block_size.';
2297  $_i+= '.$block_size.';
2298  }
2299  if ($_len) {
2300  $in = $_iv;
2301  '.$encrypt_block.'
2302  $_iv = $in;
2303  $_block = $_iv ^ substr($_text, $_i);
2304  $_iv = substr_replace($_iv, $_block, 0, $_len);
2305  $_ciphertext.= $_block;
2306  $_pos = $_len;
2307  }
2308  return $_ciphertext;
2309  ';
2310 
2311  $decrypt = $init_encrypt . '
2312  $_plaintext = "";
2313  $_buffer = &$self->debuffer;
2314 
2315  if ($self->continuousBuffer) {
2316  $_iv = &$self->decryptIV;
2317  $_pos = &$_buffer["pos"];
2318  } else {
2319  $_iv = $self->decryptIV;
2320  $_pos = 0;
2321  }
2322  $_len = strlen($_text);
2323  $_i = 0;
2324  if ($_pos) {
2325  $_orig_pos = $_pos;
2326  $_max = '.$block_size.' - $_pos;
2327  if ($_len >= $_max) {
2328  $_i = $_max;
2329  $_len-= $_max;
2330  $_pos = 0;
2331  } else {
2332  $_i = $_len;
2333  $_pos+= $_len;
2334  $_len = 0;
2335  }
2336  $_plaintext = substr($_iv, $_orig_pos) ^ $_text;
2337  $_iv = substr_replace($_iv, substr($_text, 0, $_i), $_orig_pos, $_i);
2338  }
2339  while ($_len >= '.$block_size.') {
2340  $in = $_iv;
2341  '.$encrypt_block.'
2342  $_iv = $in;
2343  $cb = substr($_text, $_i, '.$block_size.');
2344  $_plaintext.= $_iv ^ $cb;
2345  $_iv = $cb;
2346  $_len-= '.$block_size.';
2347  $_i+= '.$block_size.';
2348  }
2349  if ($_len) {
2350  $in = $_iv;
2351  '.$encrypt_block.'
2352  $_iv = $in;
2353  $_plaintext.= $_iv ^ substr($_text, $_i);
2354  $_iv = substr_replace($_iv, substr($_text, $_i), 0, $_len);
2355  $_pos = $_len;
2356  }
2357 
2358  return $_plaintext;
2359  ';
2360  break;
2361  case self::MODE_OFB:
2362  $encrypt = $init_encrypt . '
2363  $_ciphertext = "";
2364  $_plaintext_len = strlen($_text);
2365  $_xor = $self->encryptIV;
2366  $_buffer = &$self->enbuffer;
2367 
2368  if (strlen($_buffer["xor"])) {
2369  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2370  $_block = substr($_text, $_i, '.$block_size.');
2371  if (strlen($_block) > strlen($_buffer["xor"])) {
2372  $in = $_xor;
2373  '.$encrypt_block.'
2374  $_xor = $in;
2375  $_buffer["xor"].= $_xor;
2376  }
2377  $_key = $self->_string_shift($_buffer["xor"], '.$block_size.');
2378  $_ciphertext.= $_block ^ $_key;
2379  }
2380  } else {
2381  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2382  $in = $_xor;
2383  '.$encrypt_block.'
2384  $_xor = $in;
2385  $_ciphertext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
2386  }
2387  $_key = $_xor;
2388  }
2389  if ($self->continuousBuffer) {
2390  $self->encryptIV = $_xor;
2391  if ($_start = $_plaintext_len % '.$block_size.') {
2392  $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
2393  }
2394  }
2395  return $_ciphertext;
2396  ';
2397 
2398  $decrypt = $init_encrypt . '
2399  $_plaintext = "";
2400  $_ciphertext_len = strlen($_text);
2401  $_xor = $self->decryptIV;
2402  $_buffer = &$self->debuffer;
2403 
2404  if (strlen($_buffer["xor"])) {
2405  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2406  $_block = substr($_text, $_i, '.$block_size.');
2407  if (strlen($_block) > strlen($_buffer["xor"])) {
2408  $in = $_xor;
2409  '.$encrypt_block.'
2410  $_xor = $in;
2411  $_buffer["xor"].= $_xor;
2412  }
2413  $_key = $self->_string_shift($_buffer["xor"], '.$block_size.');
2414  $_plaintext.= $_block ^ $_key;
2415  }
2416  } else {
2417  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2418  $in = $_xor;
2419  '.$encrypt_block.'
2420  $_xor = $in;
2421  $_plaintext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
2422  }
2423  $_key = $_xor;
2424  }
2425  if ($self->continuousBuffer) {
2426  $self->decryptIV = $_xor;
2427  if ($_start = $_ciphertext_len % '.$block_size.') {
2428  $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
2429  }
2430  }
2431  return $_plaintext;
2432  ';
2433  break;
2434  case self::MODE_STREAM:
2435  $encrypt = $init_encrypt . '
2436  $_ciphertext = "";
2437  '.$encrypt_block.'
2438  return $_ciphertext;
2439  ';
2440  $decrypt = $init_decrypt . '
2441  $_plaintext = "";
2442  '.$decrypt_block.'
2443  return $_plaintext;
2444  ';
2445  break;
2446  // case self::MODE_CBC:
2447  default:
2448  $encrypt = $init_encrypt . '
2449  $_ciphertext = "";
2450  $_plaintext_len = strlen($_text);
2451 
2452  $in = $self->encryptIV;
2453 
2454  for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
2455  $in = substr($_text, $_i, '.$block_size.') ^ $in;
2456  '.$encrypt_block.'
2457  $_ciphertext.= $in;
2458  }
2459 
2460  if ($self->continuousBuffer) {
2461  $self->encryptIV = $in;
2462  }
2463 
2464  return $_ciphertext;
2465  ';
2466 
2467  $decrypt = $init_decrypt . '
2468  $_plaintext = "";
2469  $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
2470  $_ciphertext_len = strlen($_text);
2471 
2472  $_iv = $self->decryptIV;
2473 
2474  for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
2475  $in = $_block = substr($_text, $_i, '.$block_size.');
2476  '.$decrypt_block.'
2477  $_plaintext.= $in ^ $_iv;
2478  $_iv = $_block;
2479  }
2480 
2481  if ($self->continuousBuffer) {
2482  $self->decryptIV = $_iv;
2483  }
2484 
2485  return $self->_unpad($_plaintext);
2486  ';
2487  break;
2488  }
2489 
2490  // Create the $inline function and return its name as string. Ready to run!
2491  return create_function('$_action, &$self, $_text', $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' }');
2492  }
+ Here is the caller graph for this function:

◆ _decryptBlock()

phpseclib\Crypt\Base::_decryptBlock (   $in)
abstract

Decrypts a block.

Note: Must be extended by the child * class

private

Parameters
string$in
Returns
string

Referenced by phpseclib\Crypt\Base\_setEngine(), and phpseclib\Crypt\Base\decrypt().

+ Here is the caller graph for this function:

◆ _encryptBlock()

phpseclib\Crypt\Base::_encryptBlock (   $in)
abstract

Encrypts a block.

Note: Must be extended by the child * class

private

Parameters
string$in
Returns
string

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

+ Here is the caller graph for this function:

◆ _getLambdaFunctions()

& phpseclib\Crypt\Base::_getLambdaFunctions ( )

Holds the lambda_functions table (classwide)

Each name of the lambda function, created from _setupInlineCrypt() && _createInlineCryptFunction() is stored, classwide (!), here for reusing.

The string-based index of $function is a classwide uniqe value representing, at least, the $mode of operation (or more... depends of the optimizing level) for which $mode the lambda function was created.

private

Returns
array &$functions

Definition at line 2509 of file Base.php.

2510  {
2511  static $functions = array();
2512  return $functions;
2513  }

◆ _hashInlineCryptFunction()

phpseclib\Crypt\Base::_hashInlineCryptFunction (   $bytes)

Generates a digest from $bytes.

See also
self::_setupInlineCrypt() private
Parameters
$bytes
Returns
string

Definition at line 2523 of file Base.php.

References $i, $result, $t, and GuzzleHttp\Psr7\hash().

Referenced by phpseclib\Crypt\Blowfish\_setupInlineCrypt(), phpseclib\Crypt\RC2\_setupInlineCrypt(), phpseclib\Crypt\Twofish\_setupInlineCrypt(), phpseclib\Crypt\Rijndael\_setupInlineCrypt(), and phpseclib\Crypt\DES\_setupInlineCrypt().

2524  {
2525  if (!isset(self::$WHIRLPOOL_AVAILABLE)) {
2526  self::$WHIRLPOOL_AVAILABLE = extension_loaded('hash') && in_array('whirlpool', hash_algos());
2527  }
2528 
2529  $result = '';
2530  $hash = $bytes;
2531 
2532  switch (true) {
2533  case self::$WHIRLPOOL_AVAILABLE:
2534  foreach (str_split($bytes, 64) as $t) {
2535  $hash = hash('whirlpool', $hash, true);
2536  $result .= $t ^ $hash;
2537  }
2538  return $result . hash('whirlpool', $hash, true);
2539  default:
2540  $len = strlen($bytes);
2541  for ($i = 0; $i < $len; $i+=20) {
2542  $t = substr($bytes, $i, 20);
2543  $hash = pack('H*', sha1($hash));
2544  $result .= $t ^ $hash;
2545  }
2546  return $result . pack('H*', sha1($hash));
2547  }
2548  }
$result
$i
Definition: disco.tpl.php:19
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _increment_str()

phpseclib\Crypt\Base::_increment_str ( $var)

Increment the current string.

See also
self::decrypt()
self::encrypt()
Parameters
string$varprivate

Definition at line 1931 of file Base.php.

References $i.

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

1932  {
1933  for ($i = 4; $i <= strlen($var); $i+= 4) {
1934  $temp = substr($var, -$i, 4);
1935  switch ($temp) {
1936  case "\xFF\xFF\xFF\xFF":
1937  $var = substr_replace($var, "\x00\x00\x00\x00", -$i, 4);
1938  break;
1939  case "\x7F\xFF\xFF\xFF":
1940  $var = substr_replace($var, "\x80\x00\x00\x00", -$i, 4);
1941  return;
1942  default:
1943  $temp = unpack('Nnum', $temp);
1944  $var = substr_replace($var, pack('N', $temp['num'] + 1), -$i, 4);
1945  return;
1946  }
1947  }
1948 
1949  $remainder = strlen($var) % 4;
1950 
1951  if ($remainder == 0) {
1952  return;
1953  }
1954 
1955  $temp = unpack('Nnum', str_pad(substr($var, 0, $remainder), 4, "\0", STR_PAD_LEFT));
1956  $temp = substr(pack('N', $temp['num'] + 1), -$remainder);
1957  $var = substr_replace($var, $temp, 0, $remainder);
1958  }
$i
Definition: disco.tpl.php:19
+ Here is the caller graph for this function:

◆ _openssl_ctr_process()

phpseclib\Crypt\Base::_openssl_ctr_process (   $plaintext,
$encryptIV,
$buffer 
)

OpenSSL CTR Processor.

PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream for CTR is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() and Base::decrypt(). Also, OpenSSL doesn't implement CTR for all of it's symmetric ciphers so this function will emulate CTR with ECB when necesary.

See also
self::encrypt()
self::decrypt()
Parameters
string$plaintext
string$encryptIV
array$buffer
Returns
string private

Definition at line 1283 of file Base.php.

References phpseclib\Crypt\Base\$block_size, phpseclib\Crypt\Base\$encryptIV, $i, phpseclib\Crypt\Base\$key, $result, $start, phpseclib\Crypt\Base\_increment_str(), phpseclib\Crypt\Base\_string_pop(), and phpseclib\Crypt\Base\_string_shift().

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

1284  {
1285  $ciphertext = '';
1286 
1288  $key = $this->key;
1289 
1290  if ($this->openssl_emulate_ctr) {
1291  $xor = $encryptIV;
1292  if (strlen($buffer['ciphertext'])) {
1293  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
1294  $block = substr($plaintext, $i, $block_size);
1295  if (strlen($block) > strlen($buffer['ciphertext'])) {
1296  $result = openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
1297  $result = !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
1298  $buffer['ciphertext'].= $result;
1299  }
1300  $this->_increment_str($xor);
1301  $otp = $this->_string_shift($buffer['ciphertext'], $block_size);
1302  $ciphertext.= $block ^ $otp;
1303  }
1304  } else {
1305  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
1306  $block = substr($plaintext, $i, $block_size);
1307  $otp = openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
1308  $otp = !defined('OPENSSL_RAW_DATA') ? substr($otp, 0, -$this->block_size) : $otp;
1309  $this->_increment_str($xor);
1310  $ciphertext.= $block ^ $otp;
1311  }
1312  }
1313  if ($this->continuousBuffer) {
1314  $encryptIV = $xor;
1315  if ($start = strlen($plaintext) % $block_size) {
1316  $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
1317  }
1318  }
1319 
1320  return $ciphertext;
1321  }
1322 
1323  if (strlen($buffer['ciphertext'])) {
1324  $ciphertext = $plaintext ^ $this->_string_shift($buffer['ciphertext'], strlen($plaintext));
1325  $plaintext = substr($plaintext, strlen($ciphertext));
1326 
1327  if (!strlen($plaintext)) {
1328  return $ciphertext;
1329  }
1330  }
1331 
1332  $overflow = strlen($plaintext) % $block_size;
1333  if ($overflow) {
1334  $plaintext2 = $this->_string_pop($plaintext, $overflow); // ie. trim $plaintext to a multiple of $block_size and put rest of $plaintext in $plaintext2
1335  $encrypted = openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
1336  $temp = $this->_string_pop($encrypted, $block_size);
1337  $ciphertext.= $encrypted . ($plaintext2 ^ $temp);
1338  if ($this->continuousBuffer) {
1339  $buffer['ciphertext'] = substr($temp, $overflow);
1340  $encryptIV = $temp;
1341  }
1342  } elseif (!strlen($buffer['ciphertext'])) {
1343  $ciphertext.= openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
1344  $temp = $this->_string_pop($ciphertext, $block_size);
1345  if ($this->continuousBuffer) {
1346  $encryptIV = $temp;
1347  }
1348  }
1349  if ($this->continuousBuffer) {
1350  if (!defined('OPENSSL_RAW_DATA')) {
1351  $encryptIV.= openssl_encrypt('', $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
1352  }
1353  $encryptIV = openssl_decrypt($encryptIV, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
1354  if ($overflow) {
1355  $this->_increment_str($encryptIV);
1356  }
1357  }
1358 
1359  return $ciphertext;
1360  }
_string_shift(&$string, $index=1)
String Shift.
Definition: Base.php:1899
$result
_increment_str(&$var)
Increment the current string.
Definition: Base.php:1931
$start
Definition: bench.php:8
_string_pop(&$string, $index=1)
String Pop.
Definition: Base.php:1916
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _openssl_ofb_process()

phpseclib\Crypt\Base::_openssl_ofb_process (   $plaintext,
$encryptIV,
$buffer 
)

OpenSSL OFB Processor.

PHP's OpenSSL bindings do not operate in continuous mode so we'll wrap around it. Since the keystream for OFB is the same for both encrypting and decrypting this function is re-used by both Base::encrypt() and Base::decrypt().

See also
self::encrypt()
self::decrypt()
Parameters
string$plaintext
string$encryptIV
array$buffer
Returns
string private

Definition at line 1377 of file Base.php.

References phpseclib\Crypt\Base\$block_size, phpseclib\Crypt\Base\$key, phpseclib\Crypt\Base\_string_pop(), and phpseclib\Crypt\Base\_string_shift().

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

1378  {
1379  if (strlen($buffer['xor'])) {
1380  $ciphertext = $plaintext ^ $buffer['xor'];
1381  $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
1382  $plaintext = substr($plaintext, strlen($ciphertext));
1383  } else {
1384  $ciphertext = '';
1385  }
1386 
1388 
1389  $len = strlen($plaintext);
1390  $key = $this->key;
1391  $overflow = $len % $block_size;
1392 
1393  if (strlen($plaintext)) {
1394  if ($overflow) {
1395  $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
1396  $xor = $this->_string_pop($ciphertext, $block_size);
1397  if ($this->continuousBuffer) {
1398  $encryptIV = $xor;
1399  }
1400  $ciphertext.= $this->_string_shift($xor, $overflow) ^ substr($plaintext, -$overflow);
1401  if ($this->continuousBuffer) {
1402  $buffer['xor'] = $xor;
1403  }
1404  } else {
1405  $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
1406  if ($this->continuousBuffer) {
1407  $encryptIV = substr($ciphertext, -$block_size) ^ substr($plaintext, -$block_size);
1408  }
1409  }
1410  }
1411 
1412  return $ciphertext;
1413  }
_string_shift(&$string, $index=1)
String Shift.
Definition: Base.php:1899
_string_pop(&$string, $index=1)
String Pop.
Definition: Base.php:1916
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _openssl_translate_mode()

phpseclib\Crypt\Base::_openssl_translate_mode ( )

phpseclib <-> OpenSSL Mode Mapper

May need to be overwritten by classes extending this one in some cases

Returns
int private

Definition at line 1423 of file Base.php.

Referenced by phpseclib\Crypt\TripleDES\isValidEngine(), phpseclib\Crypt\Rijndael\isValidEngine(), phpseclib\Crypt\RC2\isValidEngine(), phpseclib\Crypt\Blowfish\isValidEngine(), and phpseclib\Crypt\DES\isValidEngine().

1424  {
1425  switch ($this->mode) {
1426  case self::MODE_ECB:
1427  return 'ecb';
1428  case self::MODE_CBC:
1429  return 'cbc';
1430  case self::MODE_CTR:
1431  return 'ctr';
1432  case self::MODE_CFB:
1433  return 'cfb';
1434  case self::MODE_OFB:
1435  return 'ofb';
1436  }
1437  }
+ Here is the caller graph for this function:

◆ _pad()

phpseclib\Crypt\Base::_pad (   $text)

Pads a string.

Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize. $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to chr($this->block_size - (strlen($text) % $this->block_size)

If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless and padding will, hence forth, be enabled.

See also
self::_unpad()
Parameters
string$textprivate
Returns
string

Definition at line 1822 of file Base.php.

References phpseclib\Crypt\Base\$block_size, and $text.

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

1823  {
1824  $length = strlen($text);
1825 
1826  if (!$this->padding) {
1827  if ($length % $this->block_size == 0) {
1828  return $text;
1829  } else {
1830  user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})");
1831  $this->padding = true;
1832  }
1833  }
1834 
1835  $pad = $this->block_size - ($length % $this->block_size);
1836 
1837  return str_pad($text, $length + $pad, chr($pad));
1838  }
$text
Definition: errorreport.php:18
+ Here is the caller graph for this function:

◆ _setEngine()

phpseclib\Crypt\Base::_setEngine ( )

Sets the engine as appropriate.

See also
self::__construct() private

Definition at line 1649 of file Base.php.

References phpseclib\Crypt\Base\$engine, $in, phpseclib\Crypt\Base\_decryptBlock(), phpseclib\Crypt\Base\_encryptBlock(), phpseclib\Crypt\Base\_setupKey(), and phpseclib\Crypt\Base\isValidEngine().

Referenced by phpseclib\Crypt\Base\__construct(), phpseclib\Crypt\Base\disableContinuousBuffer(), phpseclib\Crypt\Base\enableContinuousBuffer(), phpseclib\Crypt\Rijndael\setBlockLength(), phpseclib\Crypt\AES\setKey(), phpseclib\Crypt\Base\setKey(), phpseclib\Crypt\Base\setKeyLength(), and phpseclib\Crypt\Base\setPreferredEngine().

1650  {
1651  $this->engine = null;
1652 
1653  $candidateEngines = array(
1654  $this->preferredEngine,
1655  self::ENGINE_OPENSSL,
1656  self::ENGINE_MCRYPT
1657  );
1658  foreach ($candidateEngines as $engine) {
1659  if ($this->isValidEngine($engine)) {
1660  $this->engine = $engine;
1661  break;
1662  }
1663  }
1664  if (!$this->engine) {
1665  $this->engine = self::ENGINE_INTERNAL;
1666  }
1667 
1668  if ($this->engine != self::ENGINE_MCRYPT && $this->enmcrypt) {
1669  // Closing the current mcrypt resource(s). _mcryptSetup() will, if needed,
1670  // (re)open them with the module named in $this->cipher_name_mcrypt
1671  mcrypt_module_close($this->enmcrypt);
1672  mcrypt_module_close($this->demcrypt);
1673  $this->enmcrypt = null;
1674  $this->demcrypt = null;
1675 
1676  if ($this->ecb) {
1677  mcrypt_module_close($this->ecb);
1678  $this->ecb = null;
1679  }
1680  }
1681 
1682  $this->changed = true;
1683  }
isValidEngine($engine)
Test for engine validity.
Definition: Base.php:1551
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _setup()

phpseclib\Crypt\Base::_setup ( )

Setup the self::ENGINE_INTERNAL $engine.

(re)init, if necessary, the internal cipher $engine and flush all $buffers Used (only) if $engine == self::ENGINE_INTERNAL

_setup() will be called each time if $changed === true typically this happens when using one or more of following public methods:

See also
self::setKey()
self::setIV()
self::disableContinuousBuffer() private

Definition at line 1743 of file Base.php.

References phpseclib\Crypt\Base\_clearBuffers(), phpseclib\Crypt\Base\_setupInlineCrypt(), and phpseclib\Crypt\Base\_setupKey().

Referenced by phpseclib\Crypt\RC4\_crypt(), phpseclib\Crypt\Base\decrypt(), and phpseclib\Crypt\Base\encrypt().

1744  {
1745  $this->_clearBuffers();
1746  $this->_setupKey();
1747 
1748  if ($this->use_inline_crypt) {
1749  $this->_setupInlineCrypt();
1750  }
1751  }
_setupKey()
Setup the key (expansion)
_clearBuffers()
Clears internal buffers.
Definition: Base.php:1876
_setupInlineCrypt()
Setup the performance-optimized function for de/encrypt()
Definition: Base.php:2020
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _setupInlineCrypt()

phpseclib\Crypt\Base::_setupInlineCrypt ( )

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

Stores the created (or existing) callback function-name in $this->inline_crypt

Internally for phpseclib developers:

_setupInlineCrypt() would be called only if:

- $engine == self::ENGINE_INTERNAL and

- $use_inline_crypt === true

- each time on _setup(), after(!) _setupKey()


This ensures that _setupInlineCrypt() has always a
full ready2go initializated internal cipher $engine state
where, for example, the keys allready expanded,
keys/block_size calculated and such.

It is, each time if called, the responsibility of _setupInlineCrypt():

- to set $this->inline_crypt to a valid and fully working callback function
  as a (faster) replacement for encrypt() / decrypt()

- NOT to create unlimited callback functions (for memory reasons!)
  no matter how often _setupInlineCrypt() would be called. At some
  point of amount they must be generic re-useable.

- the code of _setupInlineCrypt() it self,
  and the generated callback code,
  must be, in following order:
  - 100% safe
  - 100% compatible to encrypt()/decrypt()
  - using only php5+ features/lang-constructs/php-extensions if
    compatibility (down to php4) or fallback is provided
  - readable/maintainable/understandable/commented and... not-cryptic-styled-code :-)
  - >= 10% faster than encrypt()/decrypt() [which is, by the way,
    the reason for the existence of _setupInlineCrypt() :-)]
  - memory-nice
  - short (as good as possible)

Note: - _setupInlineCrypt() is using _createInlineCryptFunction() to create the full callback function code.

  • In case of using inline crypting, _setupInlineCrypt() must extend by the child * class.
  • The following variable names are reserved:
    • $_* (all variable names prefixed with an underscore)
    • $self (object reference to it self. Do not use $this, but $self instead)
    • $in (the content of $in has to en/decrypt by the generated code)
  • The callback function should not use the 'return' statement, but en/decrypt'ing the content of $in only
See also
self::_setup()
self::_createInlineCryptFunction()
self::encrypt()
self::decrypt() private

Definition at line 2020 of file Base.php.

Referenced by phpseclib\Crypt\Base\_setup().

2021  {
2022  // If, for any reason, an extending \phpseclib\Crypt\Base() \phpseclib\Crypt\* class
2023  // not using inline crypting then it must be ensured that: $this->use_inline_crypt = false
2024  // ie in the class var declaration of $use_inline_crypt in general for the \phpseclib\Crypt\* class,
2025  // in the constructor at object instance-time
2026  // or, if it's runtime-specific, at runtime
2027 
2028  $this->use_inline_crypt = false;
2029  }
+ Here is the caller graph for this function:

◆ _setupKey()

phpseclib\Crypt\Base::_setupKey ( )
abstract

Setup the key (expansion)

Only used if $engine == self::ENGINE_INTERNAL

Note: Must extend by the child * class

See also
self::_setup() private

Referenced by phpseclib\Crypt\Base\_setEngine(), and phpseclib\Crypt\Base\_setup().

+ Here is the caller graph for this function:

◆ _setupMcrypt()

phpseclib\Crypt\Base::_setupMcrypt ( )

Setup the self::ENGINE_MCRYPT $engine.

(re)init, if necessary, the (ext)mcrypt resources and flush all $buffers Used (only) if $engine = self::ENGINE_MCRYPT

_setupMcrypt() will be called each time if $changed === true typically this happens when using one or more of following public methods:

See also
self::setKey()
self::setIV()
self::disableContinuousBuffer() private

Definition at line 1776 of file Base.php.

References phpseclib\Crypt\Base\_clearBuffers().

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

1777  {
1778  $this->_clearBuffers();
1779  $this->enchanged = $this->dechanged = true;
1780 
1781  if (!isset($this->enmcrypt)) {
1782  static $mcrypt_modes = array(
1783  self::MODE_CTR => 'ctr',
1784  self::MODE_ECB => MCRYPT_MODE_ECB,
1785  self::MODE_CBC => MCRYPT_MODE_CBC,
1786  self::MODE_CFB => 'ncfb',
1787  self::MODE_OFB => MCRYPT_MODE_NOFB,
1788  self::MODE_STREAM => MCRYPT_MODE_STREAM,
1789  );
1790 
1791  $this->demcrypt = mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
1792  $this->enmcrypt = mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
1793 
1794  // we need the $ecb mcrypt resource (only) in MODE_CFB with enableContinuousBuffer()
1795  // to workaround mcrypt's broken ncfb implementation in buffered mode
1796  // see: {@link http://phpseclib.sourceforge.net/cfb-demo.phps}
1797  if ($this->mode == self::MODE_CFB) {
1798  $this->ecb = mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, '');
1799  }
1800  } // else should mcrypt_generic_deinit be called?
1801 
1802  if ($this->mode == self::MODE_CFB) {
1803  mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size));
1804  }
1805  }
_clearBuffers()
Clears internal buffers.
Definition: Base.php:1876
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _string_pop()

phpseclib\Crypt\Base::_string_pop ( $string,
  $index = 1 
)

String Pop.

Inspired by array_pop

Parameters
string$string
int$indexprivate
Returns
string

Definition at line 1916 of file Base.php.

References $index.

Referenced by phpseclib\Crypt\Base\_openssl_ctr_process(), phpseclib\Crypt\Base\_openssl_ofb_process(), and phpseclib\Crypt\Base\encrypt().

1917  {
1918  $substr = substr($string, -$index);
1919  $string = substr($string, 0, -$index);
1920  return $substr;
1921  }
$index
Definition: metadata.php:60
+ Here is the caller graph for this function:

◆ _string_shift()

phpseclib\Crypt\Base::_string_shift ( $string,
  $index = 1 
)

String Shift.

Inspired by array_shift

Parameters
string$string
int$indexprivate
Returns
string

Definition at line 1899 of file Base.php.

References $index.

Referenced by phpseclib\Crypt\Base\_openssl_ctr_process(), phpseclib\Crypt\Base\_openssl_ofb_process(), phpseclib\Crypt\Base\decrypt(), and phpseclib\Crypt\Base\encrypt().

1900  {
1901  $substr = substr($string, 0, $index);
1902  $string = substr($string, $index);
1903  return $substr;
1904  }
$index
Definition: metadata.php:60
+ Here is the caller graph for this function:

◆ _unpad()

phpseclib\Crypt\Base::_unpad (   $text)

Unpads a string.

If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong and false will be returned.

See also
self::_pad()
Parameters
string$textprivate
Returns
string

Definition at line 1851 of file Base.php.

References $text.

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

1852  {
1853  if (!$this->padding) {
1854  return $text;
1855  }
1856 
1857  $length = ord($text[strlen($text) - 1]);
1858 
1859  if (!$length || $length > $this->block_size) {
1860  return false;
1861  }
1862 
1863  return substr($text, 0, -$length);
1864  }
$text
Definition: errorreport.php:18
+ Here is the caller graph for this function:

◆ decrypt()

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

Decrypts a message.

If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until it is.

See also
self::encrypt() User interface
Parameters
string$ciphertext
Returns
string $plaintext

Definition at line 990 of file Base.php.

References phpseclib\Crypt\Base\$block_size, phpseclib\Crypt\Base\$debuffer, phpseclib\Crypt\Base\$decryptIV, $i, phpseclib\Crypt\Base\$inline_crypt, phpseclib\Crypt\Base\$key, $start, phpseclib\Crypt\Base\_clearBuffers(), phpseclib\Crypt\Base\_decryptBlock(), phpseclib\Crypt\Base\_encryptBlock(), phpseclib\Crypt\Base\_increment_str(), phpseclib\Crypt\Base\_openssl_ctr_process(), phpseclib\Crypt\Base\_openssl_ofb_process(), phpseclib\Crypt\Base\_setup(), phpseclib\Crypt\Base\_setupMcrypt(), phpseclib\Crypt\Base\_string_shift(), and phpseclib\Crypt\Base\_unpad().

991  {
992  if ($this->paddable) {
993  // we pad with chr(0) since that's what mcrypt_generic does. to quote from {@link http://www.php.net/function.mcrypt-generic}:
994  // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
995  $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0));
996  }
997 
998  if ($this->engine === self::ENGINE_OPENSSL) {
999  if ($this->changed) {
1000  $this->_clearBuffers();
1001  $this->changed = false;
1002  }
1003  switch ($this->mode) {
1004  case self::MODE_STREAM:
1005  $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
1006  break;
1007  case self::MODE_ECB:
1008  if (!defined('OPENSSL_RAW_DATA')) {
1009  $ciphertext.= openssl_encrypt('', $this->cipher_name_openssl_ecb, $this->key, true);
1010  }
1011  $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
1012  break;
1013  case self::MODE_CBC:
1014  if (!defined('OPENSSL_RAW_DATA')) {
1015  $padding = str_repeat(chr($this->block_size), $this->block_size) ^ substr($ciphertext, -$this->block_size);
1016  $ciphertext.= substr(openssl_encrypt($padding, $this->cipher_name_openssl_ecb, $this->key, true), 0, $this->block_size);
1017  }
1018  $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV);
1019  if ($this->continuousBuffer) {
1020  $this->decryptIV = substr($ciphertext, -$this->block_size);
1021  }
1022  break;
1023  case self::MODE_CTR:
1024  $plaintext = $this->_openssl_ctr_process($ciphertext, $this->decryptIV, $this->debuffer);
1025  break;
1026  case self::MODE_CFB:
1027  // cfb loosely routines inspired by openssl's:
1028  // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
1029  $plaintext = '';
1030  if ($this->continuousBuffer) {
1031  $iv = &$this->decryptIV;
1032  $pos = &$this->buffer['pos'];
1033  } else {
1035  $pos = 0;
1036  }
1037  $len = strlen($ciphertext);
1038  $i = 0;
1039  if ($pos) {
1040  $orig_pos = $pos;
1041  $max = $this->block_size - $pos;
1042  if ($len >= $max) {
1043  $i = $max;
1044  $len-= $max;
1045  $pos = 0;
1046  } else {
1047  $i = $len;
1048  $pos+= $len;
1049  $len = 0;
1050  }
1051  // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $this->blocksize
1052  $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
1053  $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
1054  $ciphertext = substr($ciphertext, $i);
1055  }
1056  $overflow = $len % $this->block_size;
1057  if ($overflow) {
1058  $plaintext.= openssl_decrypt(substr($ciphertext, 0, -$overflow), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
1059  if ($len - $overflow) {
1060  $iv = substr($ciphertext, -$overflow - $this->block_size, -$overflow);
1061  }
1062  $iv = openssl_encrypt(str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
1063  $plaintext.= $iv ^ substr($ciphertext, -$overflow);
1064  $iv = substr_replace($iv, substr($ciphertext, -$overflow), 0, $overflow);
1065  $pos = $overflow;
1066  } elseif ($len) {
1067  $plaintext.= openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
1068  $iv = substr($ciphertext, -$this->block_size);
1069  }
1070  break;
1071  case self::MODE_OFB:
1072  $plaintext = $this->_openssl_ofb_process($ciphertext, $this->decryptIV, $this->debuffer);
1073  }
1074 
1075  return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
1076  }
1077 
1078  if ($this->engine === self::ENGINE_MCRYPT) {
1080  if ($this->changed) {
1081  $this->_setupMcrypt();
1082  $this->changed = false;
1083  }
1084  if ($this->dechanged) {
1085  mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
1086  $this->dechanged = false;
1087  }
1088 
1089  if ($this->mode == self::MODE_CFB && $this->continuousBuffer) {
1090  $iv = &$this->decryptIV;
1091  $pos = &$this->debuffer['pos'];
1092  $len = strlen($ciphertext);
1093  $plaintext = '';
1094  $i = 0;
1095  if ($pos) {
1096  $orig_pos = $pos;
1097  $max = $block_size - $pos;
1098  if ($len >= $max) {
1099  $i = $max;
1100  $len-= $max;
1101  $pos = 0;
1102  } else {
1103  $i = $len;
1104  $pos+= $len;
1105  $len = 0;
1106  }
1107  // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
1108  $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
1109  $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
1110  }
1111  if ($len >= $block_size) {
1112  $cb = substr($ciphertext, $i, $len - $len % $block_size);
1113  $plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
1114  $iv = substr($cb, -$block_size);
1115  $len%= $block_size;
1116  }
1117  if ($len) {
1118  $iv = mcrypt_generic($this->ecb, $iv);
1119  $plaintext.= $iv ^ substr($ciphertext, -$len);
1120  $iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
1121  $pos = $len;
1122  }
1123 
1124  return $plaintext;
1125  }
1126 
1127  $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
1128 
1129  if (!$this->continuousBuffer) {
1130  mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
1131  }
1132 
1133  return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
1134  }
1135 
1136  if ($this->changed) {
1137  $this->_setup();
1138  $this->changed = false;
1139  }
1140  if ($this->use_inline_crypt) {
1141  $inline = $this->inline_crypt;
1142  return $inline('decrypt', $this, $ciphertext);
1143  }
1144 
1146 
1147  $buffer = &$this->debuffer;
1148  $plaintext = '';
1149  switch ($this->mode) {
1150  case self::MODE_ECB:
1151  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1152  $plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size));
1153  }
1154  break;
1155  case self::MODE_CBC:
1156  $xor = $this->decryptIV;
1157  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1158  $block = substr($ciphertext, $i, $block_size);
1159  $plaintext.= $this->_decryptBlock($block) ^ $xor;
1160  $xor = $block;
1161  }
1162  if ($this->continuousBuffer) {
1163  $this->decryptIV = $xor;
1164  }
1165  break;
1166  case self::MODE_CTR:
1167  $xor = $this->decryptIV;
1168  if (strlen($buffer['ciphertext'])) {
1169  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1170  $block = substr($ciphertext, $i, $block_size);
1171  if (strlen($block) > strlen($buffer['ciphertext'])) {
1172  $buffer['ciphertext'].= $this->_encryptBlock($xor);
1173  $this->_increment_str($xor);
1174  }
1175  $key = $this->_string_shift($buffer['ciphertext'], $block_size);
1176  $plaintext.= $block ^ $key;
1177  }
1178  } else {
1179  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1180  $block = substr($ciphertext, $i, $block_size);
1181  $key = $this->_encryptBlock($xor);
1182  $this->_increment_str($xor);
1183  $plaintext.= $block ^ $key;
1184  }
1185  }
1186  if ($this->continuousBuffer) {
1187  $this->decryptIV = $xor;
1188  if ($start = strlen($ciphertext) % $block_size) {
1189  $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
1190  }
1191  }
1192  break;
1193  case self::MODE_CFB:
1194  if ($this->continuousBuffer) {
1195  $iv = &$this->decryptIV;
1196  $pos = &$buffer['pos'];
1197  } else {
1199  $pos = 0;
1200  }
1201  $len = strlen($ciphertext);
1202  $i = 0;
1203  if ($pos) {
1204  $orig_pos = $pos;
1205  $max = $block_size - $pos;
1206  if ($len >= $max) {
1207  $i = $max;
1208  $len-= $max;
1209  $pos = 0;
1210  } else {
1211  $i = $len;
1212  $pos+= $len;
1213  $len = 0;
1214  }
1215  // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
1216  $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
1217  $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
1218  }
1219  while ($len >= $block_size) {
1220  $iv = $this->_encryptBlock($iv);
1221  $cb = substr($ciphertext, $i, $block_size);
1222  $plaintext.= $iv ^ $cb;
1223  $iv = $cb;
1224  $len-= $block_size;
1225  $i+= $block_size;
1226  }
1227  if ($len) {
1228  $iv = $this->_encryptBlock($iv);
1229  $plaintext.= $iv ^ substr($ciphertext, $i);
1230  $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
1231  $pos = $len;
1232  }
1233  break;
1234  case self::MODE_OFB:
1235  $xor = $this->decryptIV;
1236  if (strlen($buffer['xor'])) {
1237  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1238  $block = substr($ciphertext, $i, $block_size);
1239  if (strlen($block) > strlen($buffer['xor'])) {
1240  $xor = $this->_encryptBlock($xor);
1241  $buffer['xor'].= $xor;
1242  }
1243  $key = $this->_string_shift($buffer['xor'], $block_size);
1244  $plaintext.= $block ^ $key;
1245  }
1246  } else {
1247  for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
1248  $xor = $this->_encryptBlock($xor);
1249  $plaintext.= substr($ciphertext, $i, $block_size) ^ $xor;
1250  }
1251  $key = $xor;
1252  }
1253  if ($this->continuousBuffer) {
1254  $this->decryptIV = $xor;
1255  if ($start = strlen($ciphertext) % $block_size) {
1256  $buffer['xor'] = substr($key, $start) . $buffer['xor'];
1257  }
1258  }
1259  break;
1260  case self::MODE_STREAM:
1261  $plaintext = $this->_decryptBlock($ciphertext);
1262  break;
1263  }
1264  return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
1265  }
_string_shift(&$string, $index=1)
String Shift.
Definition: Base.php:1899
_setupMcrypt()
Setup the self::ENGINE_MCRYPT $engine.
Definition: Base.php:1776
_decryptBlock($in)
Decrypts a block.
_increment_str(&$var)
Increment the current string.
Definition: Base.php:1931
_unpad($text)
Unpads a string.
Definition: Base.php:1851
$start
Definition: bench.php:8
_setup()
Setup the self::ENGINE_INTERNAL $engine.
Definition: Base.php:1743
_clearBuffers()
Clears internal buffers.
Definition: Base.php:1876
_openssl_ofb_process($plaintext, &$encryptIV, &$buffer)
OpenSSL OFB Processor.
Definition: Base.php:1377
$i
Definition: disco.tpl.php:19
_openssl_ctr_process($plaintext, &$encryptIV, &$buffer)
OpenSSL CTR Processor.
Definition: Base.php:1283
_encryptBlock($in)
Encrypts a block.
+ Here is the call graph for this function:

◆ disableContinuousBuffer()

phpseclib\Crypt\Base::disableContinuousBuffer ( )

Treat consecutive packets as if they are a discontinuous buffer.

The default behavior.

See also
self::enableContinuousBuffer() User interface

Definition at line 1528 of file Base.php.

References phpseclib\Crypt\Base\_setEngine().

1529  {
1530  if ($this->mode == self::MODE_ECB) {
1531  return;
1532  }
1533  if (!$this->continuousBuffer) {
1534  return;
1535  }
1536 
1537  $this->continuousBuffer = false;
1538  $this->changed = true;
1539 
1540  $this->_setEngine();
1541  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
+ Here is the call graph for this function:

◆ disablePadding()

phpseclib\Crypt\Base::disablePadding ( )

Do not pad packets.

See also
self::enablePadding() User interface

Definition at line 1465 of file Base.php.

1466  {
1467  $this->padding = false;
1468  }

◆ enableContinuousBuffer()

phpseclib\Crypt\Base::enableContinuousBuffer ( )

Treat consecutive "packets" as if they are a continuous buffer.

Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets will yield different outputs:

echo $rijndael->encrypt(substr($plaintext, 0, 16)); echo $rijndael->encrypt(substr($plaintext, 16, 16)); echo $rijndael->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:

$rijndael->encrypt(substr($plaintext, 0, 16)); echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16))); echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16)));

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 *() 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
self::disableContinuousBuffer() User interface

Definition at line 1508 of file Base.php.

References phpseclib\Crypt\Base\_setEngine().

1509  {
1510  if ($this->mode == self::MODE_ECB) {
1511  return;
1512  }
1513 
1514  $this->continuousBuffer = true;
1515 
1516  $this->_setEngine();
1517  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
+ Here is the call graph for this function:

◆ enablePadding()

phpseclib\Crypt\Base::enablePadding ( )

Pad "packets".

Block ciphers working by encrypting between their specified [$this->]block_size at a time If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to pad the input so that it is of the proper length.

Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is transmitted separately)

See also
self::disablePadding() User interface

Definition at line 1454 of file Base.php.

1455  {
1456  $this->padding = true;
1457  }

◆ encrypt()

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

Encrypts a message.

$plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other cipher implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following URL:

http://www.di-mgt.com.au/cryptopad.html

An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does. strlen($plaintext) will still need to be a multiple of the block size, however, arbitrary values can be added to make it that length.

See also
self::decrypt() User interface
Parameters
string$plaintext
Returns
string $ciphertext

Definition at line 692 of file Base.php.

References phpseclib\Crypt\Base\$block_size, phpseclib\Crypt\Base\$enbuffer, phpseclib\Crypt\Base\$encryptIV, $i, phpseclib\Crypt\Base\$inline_crypt, phpseclib\Crypt\Base\$iv, phpseclib\Crypt\Base\$key, $result, $size, $start, phpseclib\Crypt\Base\_clearBuffers(), phpseclib\Crypt\Base\_encryptBlock(), phpseclib\Crypt\Base\_increment_str(), phpseclib\Crypt\Base\_openssl_ctr_process(), phpseclib\Crypt\Base\_openssl_ofb_process(), phpseclib\Crypt\Base\_pad(), phpseclib\Crypt\Base\_setup(), phpseclib\Crypt\Base\_setupMcrypt(), phpseclib\Crypt\Base\_string_pop(), and phpseclib\Crypt\Base\_string_shift().

693  {
694  if ($this->paddable) {
695  $plaintext = $this->_pad($plaintext);
696  }
697 
698  if ($this->engine === self::ENGINE_OPENSSL) {
699  if ($this->changed) {
700  $this->_clearBuffers();
701  $this->changed = false;
702  }
703  switch ($this->mode) {
704  case self::MODE_STREAM:
705  return openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
706  case self::MODE_ECB:
707  $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
708  return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
709  case self::MODE_CBC:
710  $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV);
711  if ($this->continuousBuffer) {
712  $this->encryptIV = substr($result, -$this->block_size);
713  }
714  return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
715  case self::MODE_CTR:
716  return $this->_openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer);
717  case self::MODE_CFB:
718  // cfb loosely routines inspired by openssl's:
719  // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
720  $ciphertext = '';
721  if ($this->continuousBuffer) {
723  $pos = &$this->enbuffer['pos'];
724  } else {
726  $pos = 0;
727  }
728  $len = strlen($plaintext);
729  $i = 0;
730  if ($pos) {
731  $orig_pos = $pos;
732  $max = $this->block_size - $pos;
733  if ($len >= $max) {
734  $i = $max;
735  $len-= $max;
736  $pos = 0;
737  } else {
738  $i = $len;
739  $pos+= $len;
740  $len = 0;
741  }
742  // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
743  $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
744  $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
745  $plaintext = substr($plaintext, $i);
746  }
747 
748  $overflow = $len % $this->block_size;
749 
750  if ($overflow) {
751  $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
752  $iv = $this->_string_pop($ciphertext, $this->block_size);
753 
754  $size = $len - $overflow;
755  $block = $iv ^ substr($plaintext, -$overflow);
756  $iv = substr_replace($iv, $block, 0, $overflow);
757  $ciphertext.= $block;
758  $pos = $overflow;
759  } elseif ($len) {
760  $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
761  $iv = substr($ciphertext, -$this->block_size);
762  }
763 
764  return $ciphertext;
765  case self::MODE_OFB:
766  return $this->_openssl_ofb_process($plaintext, $this->encryptIV, $this->enbuffer);
767  }
768  }
769 
770  if ($this->engine === self::ENGINE_MCRYPT) {
771  if ($this->changed) {
772  $this->_setupMcrypt();
773  $this->changed = false;
774  }
775  if ($this->enchanged) {
776  mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
777  $this->enchanged = false;
778  }
779 
780  // re: {@link http://phpseclib.sourceforge.net/cfb-demo.phps}
781  // using mcrypt's default handing of CFB the above would output two different things. using phpseclib's
782  // rewritten CFB implementation the above outputs the same thing twice.
783  if ($this->mode == self::MODE_CFB && $this->continuousBuffer) {
786  $pos = &$this->enbuffer['pos'];
787  $len = strlen($plaintext);
788  $ciphertext = '';
789  $i = 0;
790  if ($pos) {
791  $orig_pos = $pos;
792  $max = $block_size - $pos;
793  if ($len >= $max) {
794  $i = $max;
795  $len-= $max;
796  $pos = 0;
797  } else {
798  $i = $len;
799  $pos+= $len;
800  $len = 0;
801  }
802  $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
803  $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
804  $this->enbuffer['enmcrypt_init'] = true;
805  }
806  if ($len >= $block_size) {
807  if ($this->enbuffer['enmcrypt_init'] === false || $len > $this->cfb_init_len) {
808  if ($this->enbuffer['enmcrypt_init'] === true) {
809  mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
810  $this->enbuffer['enmcrypt_init'] = false;
811  }
812  $ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size));
813  $iv = substr($ciphertext, -$block_size);
814  $len%= $block_size;
815  } else {
816  while ($len >= $block_size) {
817  $iv = mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size);
818  $ciphertext.= $iv;
819  $len-= $block_size;
820  $i+= $block_size;
821  }
822  }
823  }
824 
825  if ($len) {
826  $iv = mcrypt_generic($this->ecb, $iv);
827  $block = $iv ^ substr($plaintext, -$len);
828  $iv = substr_replace($iv, $block, 0, $len);
829  $ciphertext.= $block;
830  $pos = $len;
831  }
832 
833  return $ciphertext;
834  }
835 
836  $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
837 
838  if (!$this->continuousBuffer) {
839  mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
840  }
841 
842  return $ciphertext;
843  }
844 
845  if ($this->changed) {
846  $this->_setup();
847  $this->changed = false;
848  }
849  if ($this->use_inline_crypt) {
850  $inline = $this->inline_crypt;
851  return $inline('encrypt', $this, $plaintext);
852  }
853 
854  $buffer = &$this->enbuffer;
856  $ciphertext = '';
857  switch ($this->mode) {
858  case self::MODE_ECB:
859  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
860  $ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size));
861  }
862  break;
863  case self::MODE_CBC:
864  $xor = $this->encryptIV;
865  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
866  $block = substr($plaintext, $i, $block_size);
867  $block = $this->_encryptBlock($block ^ $xor);
868  $xor = $block;
869  $ciphertext.= $block;
870  }
871  if ($this->continuousBuffer) {
872  $this->encryptIV = $xor;
873  }
874  break;
875  case self::MODE_CTR:
876  $xor = $this->encryptIV;
877  if (strlen($buffer['ciphertext'])) {
878  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
879  $block = substr($plaintext, $i, $block_size);
880  if (strlen($block) > strlen($buffer['ciphertext'])) {
881  $buffer['ciphertext'].= $this->_encryptBlock($xor);
882  }
883  $this->_increment_str($xor);
884  $key = $this->_string_shift($buffer['ciphertext'], $block_size);
885  $ciphertext.= $block ^ $key;
886  }
887  } else {
888  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
889  $block = substr($plaintext, $i, $block_size);
890  $key = $this->_encryptBlock($xor);
891  $this->_increment_str($xor);
892  $ciphertext.= $block ^ $key;
893  }
894  }
895  if ($this->continuousBuffer) {
896  $this->encryptIV = $xor;
897  if ($start = strlen($plaintext) % $block_size) {
898  $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
899  }
900  }
901  break;
902  case self::MODE_CFB:
903  // cfb loosely routines inspired by openssl's:
904  // {@link http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1}
905  if ($this->continuousBuffer) {
907  $pos = &$buffer['pos'];
908  } else {
910  $pos = 0;
911  }
912  $len = strlen($plaintext);
913  $i = 0;
914  if ($pos) {
915  $orig_pos = $pos;
916  $max = $block_size - $pos;
917  if ($len >= $max) {
918  $i = $max;
919  $len-= $max;
920  $pos = 0;
921  } else {
922  $i = $len;
923  $pos+= $len;
924  $len = 0;
925  }
926  // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
927  $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
928  $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
929  }
930  while ($len >= $block_size) {
931  $iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
932  $ciphertext.= $iv;
933  $len-= $block_size;
934  $i+= $block_size;
935  }
936  if ($len) {
937  $iv = $this->_encryptBlock($iv);
938  $block = $iv ^ substr($plaintext, $i);
939  $iv = substr_replace($iv, $block, 0, $len);
940  $ciphertext.= $block;
941  $pos = $len;
942  }
943  break;
944  case self::MODE_OFB:
945  $xor = $this->encryptIV;
946  if (strlen($buffer['xor'])) {
947  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
948  $block = substr($plaintext, $i, $block_size);
949  if (strlen($block) > strlen($buffer['xor'])) {
950  $xor = $this->_encryptBlock($xor);
951  $buffer['xor'].= $xor;
952  }
953  $key = $this->_string_shift($buffer['xor'], $block_size);
954  $ciphertext.= $block ^ $key;
955  }
956  } else {
957  for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
958  $xor = $this->_encryptBlock($xor);
959  $ciphertext.= substr($plaintext, $i, $block_size) ^ $xor;
960  }
961  $key = $xor;
962  }
963  if ($this->continuousBuffer) {
964  $this->encryptIV = $xor;
965  if ($start = strlen($plaintext) % $block_size) {
966  $buffer['xor'] = substr($key, $start) . $buffer['xor'];
967  }
968  }
969  break;
970  case self::MODE_STREAM:
971  $ciphertext = $this->_encryptBlock($plaintext);
972  break;
973  }
974 
975  return $ciphertext;
976  }
_string_shift(&$string, $index=1)
String Shift.
Definition: Base.php:1899
$size
Definition: RandomTest.php:84
_setupMcrypt()
Setup the self::ENGINE_MCRYPT $engine.
Definition: Base.php:1776
$result
_increment_str(&$var)
Increment the current string.
Definition: Base.php:1931
$start
Definition: bench.php:8
_setup()
Setup the self::ENGINE_INTERNAL $engine.
Definition: Base.php:1743
_pad($text)
Pads a string.
Definition: Base.php:1822
_clearBuffers()
Clears internal buffers.
Definition: Base.php:1876
_string_pop(&$string, $index=1)
String Pop.
Definition: Base.php:1916
_openssl_ofb_process($plaintext, &$encryptIV, &$buffer)
OpenSSL OFB Processor.
Definition: Base.php:1377
$i
Definition: disco.tpl.php:19
_openssl_ctr_process($plaintext, &$encryptIV, &$buffer)
OpenSSL CTR Processor.
Definition: Base.php:1283
_encryptBlock($in)
Encrypts a block.
+ Here is the call graph for this function:

◆ getBlockLength()

phpseclib\Crypt\Base::getBlockLength ( )

Returns the current block length in bits.

public

Returns
int

Definition at line 554 of file Base.php.

555  {
556  return $this->block_size << 3;
557  }

◆ getEngine()

phpseclib\Crypt\Base::getEngine ( )

Returns the engine currently being utilized.

See also
self::_setEngine() User interface

Definition at line 1638 of file Base.php.

References phpseclib\Crypt\Base\$engine.

1639  {
1640  return $this->engine;
1641  }

◆ getKeyLength()

phpseclib\Crypt\Base::getKeyLength ( )

Returns the current key length in bits.

public

Returns
int

Definition at line 543 of file Base.php.

544  {
545  return $this->key_length << 3;
546  }

◆ isValidEngine()

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

Test for engine validity.

See also
self::__construct()
Parameters
int$enginepublic
Returns
bool

Definition at line 1551 of file Base.php.

References $result.

Referenced by phpseclib\Crypt\Base\_setEngine().

1552  {
1553  switch ($engine) {
1554  case self::ENGINE_OPENSSL:
1555  if ($this->mode == self::MODE_STREAM && $this->continuousBuffer) {
1556  return false;
1557  }
1558  $this->openssl_emulate_ctr = false;
1559  $result = $this->cipher_name_openssl &&
1560  extension_loaded('openssl') &&
1561  // PHP 5.3.0 - 5.3.2 did not let you set IV's
1562  version_compare(PHP_VERSION, '5.3.3', '>=');
1563  if (!$result) {
1564  return false;
1565  }
1566 
1567  // prior to PHP 5.4.0 OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING were not defined. instead of expecting an integer
1568  // $options openssl_encrypt expected a boolean $raw_data.
1569  if (!defined('OPENSSL_RAW_DATA')) {
1570  $this->openssl_options = true;
1571  } else {
1572  $this->openssl_options = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
1573  }
1574 
1575  $methods = openssl_get_cipher_methods();
1576  if (in_array($this->cipher_name_openssl, $methods)) {
1577  return true;
1578  }
1579  // not all of openssl's symmetric cipher's support ctr. for those
1580  // that don't we'll emulate it
1581  switch ($this->mode) {
1582  case self::MODE_CTR:
1583  if (in_array($this->cipher_name_openssl_ecb, $methods)) {
1584  $this->openssl_emulate_ctr = true;
1585  return true;
1586  }
1587  }
1588  return false;
1589  case self::ENGINE_MCRYPT:
1590  return $this->cipher_name_mcrypt &&
1591  extension_loaded('mcrypt') &&
1592  in_array($this->cipher_name_mcrypt, mcrypt_list_algorithms());
1593  case self::ENGINE_INTERNAL:
1594  return true;
1595  }
1596 
1597  return false;
1598  }
$result
+ Here is the caller graph for this function:

◆ setIV()

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

Sets the initialization vector.

(optional)

SetIV is not required when self::MODE_ECB (or ie for AES: ::MODE_ECB) is being used. If not explicitly set, it'll be assumed to be all zero's.

public

Parameters
string$iv

Definition at line 512 of file Base.php.

References phpseclib\Crypt\Base\$iv.

Referenced by phpseclib\Crypt\Base\setPassword().

513  {
514  if ($this->mode == self::MODE_ECB) {
515  return;
516  }
517 
518  $this->iv = $iv;
519  $this->changed = true;
520  }
+ Here is the caller graph for this function:

◆ setKey()

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

Sets the key.

The min/max length(s) of the key depends on the cipher which is used. If the key not fits the length(s) of the cipher it will paded with null bytes up to the closest valid key length. If the key is more than max length, we trim the excess bits.

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

public

Parameters
string$key

Definition at line 573 of file Base.php.

References phpseclib\Crypt\Base\$key, phpseclib\Crypt\Base\_setEngine(), and phpseclib\Crypt\Base\setKeyLength().

Referenced by phpseclib\Crypt\Base\setPassword().

574  {
575  if (!$this->explicit_key_length) {
576  $this->setKeyLength(strlen($key) << 3);
577  $this->explicit_key_length = false;
578  }
579 
580  $this->key = $key;
581  $this->changed = true;
582  $this->_setEngine();
583  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
setKeyLength($length)
Sets the key length.
Definition: Base.php:530
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setKeyLength()

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

Sets the key length.

Keys with explicitly set lengths need to be treated accordingly

public

Parameters
int$length

Definition at line 530 of file Base.php.

References phpseclib\Crypt\Base\_setEngine().

Referenced by phpseclib\Crypt\Base\setKey().

531  {
532  $this->explicit_key_length = true;
533  $this->changed = true;
534  $this->_setEngine();
535  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setPassword()

phpseclib\Crypt\Base::setPassword (   $password,
  $method = 'pbkdf2' 
)

Sets the password.

Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: pbkdf2 or pbkdf1: $hash, $salt, $count, $dkLen

Where $hash (default = sha1) currently supports the following hashes: see: Crypt/Hash.php

See also
Crypt/Hash.php
Parameters
string$password
string$method
Returns
bool public

Definition at line 601 of file Base.php.

References $f, $i, $password, phpseclib\Crypt\Base\$password_default_salt, $t, phpseclib\Crypt\Base\setIV(), and phpseclib\Crypt\Base\setKey().

602  {
603  $key = '';
604 
605  switch ($method) {
606  default: // 'pbkdf2' or 'pbkdf1'
607  $func_args = func_get_args();
608 
609  // Hash function
610  $hash = isset($func_args[2]) ? $func_args[2] : 'sha1';
611 
612  // WPA and WPA2 use the SSID as the salt
613  $salt = isset($func_args[3]) ? $func_args[3] : $this->password_default_salt;
614 
615  // RFC2898#section-4.2 uses 1,000 iterations by default
616  // WPA and WPA2 use 4,096.
617  $count = isset($func_args[4]) ? $func_args[4] : 1000;
618 
619  // Keylength
620  if (isset($func_args[5])) {
621  $dkLen = $func_args[5];
622  } else {
623  $dkLen = $method == 'pbkdf1' ? 2 * $this->key_length : $this->key_length;
624  }
625 
626  switch (true) {
627  case $method == 'pbkdf1':
628  $hashObj = new Hash();
629  $hashObj->setHash($hash);
630  if ($dkLen > $hashObj->getLength()) {
631  user_error('Derived key too long');
632  return false;
633  }
634  $t = $password . $salt;
635  for ($i = 0; $i < $count; ++$i) {
636  $t = $hashObj->hash($t);
637  }
638  $key = substr($t, 0, $dkLen);
639 
640  $this->setKey(substr($key, 0, $dkLen >> 1));
641  $this->setIV(substr($key, $dkLen >> 1));
642 
643  return true;
644  // Determining if php[>=5.5.0]'s hash_pbkdf2() function avail- and useable
645  case !function_exists('hash_pbkdf2'):
646  case !function_exists('hash_algos'):
647  case !in_array($hash, hash_algos()):
648  $i = 1;
649  while (strlen($key) < $dkLen) {
650  $hmac = new Hash();
651  $hmac->setHash($hash);
652  $hmac->setKey($password);
653  $f = $u = $hmac->hash($salt . pack('N', $i++));
654  for ($j = 2; $j <= $count; ++$j) {
655  $u = $hmac->hash($u);
656  $f^= $u;
657  }
658  $key.= $f;
659  }
660  $key = substr($key, 0, $dkLen);
661  break;
662  default:
663  $key = hash_pbkdf2($hash, $password, $salt, $count, $dkLen, true);
664  }
665  }
666 
667  $this->setKey($key);
668 
669  return true;
670  }
setIV($iv)
Sets the initialization vector.
Definition: Base.php:512
setKey($key)
Sets the key.
Definition: Base.php:573
Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic...
$password
Definition: cron.php:14
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ setPreferredEngine()

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

Sets the preferred crypt engine.

Currently, $engine could be:

  • ::ENGINE_OPENSSL [very fast]
  • ::ENGINE_MCRYPT [fast]
  • ::ENGINE_INTERNAL [slow]

If the preferred crypt engine is not available the fastest available one will be used

See also
self::__construct()
Parameters
int$enginepublic

Definition at line 1617 of file Base.php.

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

1618  {
1619  switch ($engine) {
1620  //case self::ENGINE_OPENSSL;
1621  case self::ENGINE_MCRYPT:
1622  case self::ENGINE_INTERNAL:
1623  $this->preferredEngine = $engine;
1624  break;
1625  default:
1626  $this->preferredEngine = self::ENGINE_OPENSSL;
1627  }
1628 
1629  $this->_setEngine();
1630  }
_setEngine()
Sets the engine as appropriate.
Definition: Base.php:1649
+ Here is the call graph for this function:

Field Documentation

◆ $block_size

◆ $cfb_init_len

phpseclib\Crypt\Base::$cfb_init_len = 600

Definition at line 287 of file Base.php.

◆ $changed

phpseclib\Crypt\Base::$changed = true

Definition at line 298 of file Base.php.

◆ $cipher_name_mcrypt

phpseclib\Crypt\Base::$cipher_name_mcrypt

Definition at line 356 of file Base.php.

◆ $cipher_name_openssl

phpseclib\Crypt\Base::$cipher_name_openssl

Definition at line 367 of file Base.php.

◆ $cipher_name_openssl_ecb

phpseclib\Crypt\Base::$cipher_name_openssl_ecb

Definition at line 379 of file Base.php.

◆ $continuousBuffer

phpseclib\Crypt\Base::$continuousBuffer = false

Definition at line 182 of file Base.php.

◆ $debuffer

phpseclib\Crypt\Base::$debuffer

Definition at line 202 of file Base.php.

Referenced by phpseclib\Crypt\Base\decrypt().

◆ $dechanged

phpseclib\Crypt\Base::$dechanged = true

Definition at line 246 of file Base.php.

◆ $decryptIV

phpseclib\Crypt\Base::$decryptIV

Definition at line 173 of file Base.php.

Referenced by phpseclib\Crypt\Base\decrypt().

◆ $demcrypt

phpseclib\Crypt\Base::$demcrypt

Definition at line 226 of file Base.php.

◆ $ecb

phpseclib\Crypt\Base::$ecb

Definition at line 265 of file Base.php.

◆ $enbuffer

phpseclib\Crypt\Base::$enbuffer

Definition at line 192 of file Base.php.

Referenced by phpseclib\Crypt\Base\encrypt().

◆ $enchanged

phpseclib\Crypt\Base::$enchanged = true

Definition at line 236 of file Base.php.

◆ $encryptIV

phpseclib\Crypt\Base::$encryptIV

◆ $engine

◆ $enmcrypt

phpseclib\Crypt\Base::$enmcrypt

Definition at line 214 of file Base.php.

◆ $explicit_key_length

phpseclib\Crypt\Base::$explicit_key_length = false

Definition at line 441 of file Base.php.

◆ $inline_crypt

phpseclib\Crypt\Base::$inline_crypt

Definition at line 403 of file Base.php.

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

◆ $iv

phpseclib\Crypt\Base::$iv

◆ $key

◆ $mode

◆ $openssl_emulate_ctr

phpseclib\Crypt\Base::$openssl_emulate_ctr = false

Definition at line 423 of file Base.php.

◆ $openssl_options

phpseclib\Crypt\Base::$openssl_options

Definition at line 432 of file Base.php.

◆ $paddable

phpseclib\Crypt\Base::$paddable = false

Definition at line 316 of file Base.php.

◆ $padding

phpseclib\Crypt\Base::$padding = true

Definition at line 307 of file Base.php.

◆ $password_default_salt

phpseclib\Crypt\Base::$password_default_salt = 'phpseclib/salt'

Definition at line 388 of file Base.php.

Referenced by phpseclib\Crypt\Base\setPassword().

◆ $preferredEngine

phpseclib\Crypt\Base::$preferredEngine

Definition at line 343 of file Base.php.

◆ $skip_key_adjustment

phpseclib\Crypt\Base::$skip_key_adjustment = false

Definition at line 450 of file Base.php.

◆ $use_inline_crypt

phpseclib\Crypt\Base::$use_inline_crypt

Definition at line 414 of file Base.php.

◆ $WHIRLPOOL_AVAILABLE

phpseclib\Crypt\Base::$WHIRLPOOL_AVAILABLE
static

Definition at line 100 of file Base.php.

◆ ENGINE_INTERNAL

const phpseclib\Crypt\Base::ENGINE_INTERNAL = 1

#+ private

See also
::__construct() Base value for the Implementation implementation $engine switch

Definition at line 109 of file Base.php.

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

◆ ENGINE_MCRYPT

const phpseclib\Crypt\Base::ENGINE_MCRYPT = 2

Base value for the mcrypt implementation $engine switch.

Definition at line 113 of file Base.php.

◆ ENGINE_OPENSSL

const phpseclib\Crypt\Base::ENGINE_OPENSSL = 3

Base value for the mcrypt implementation $engine switch.

Definition at line 117 of file Base.php.

Referenced by phpseclib\Crypt\RC4\isValidEngine().

◆ MODE_CBC

const phpseclib\Crypt\Base::MODE_CBC = 2

Encrypt / decrypt using the Code Book Chaining mode.

http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29

Definition at line 74 of file Base.php.

Referenced by phpseclib\Crypt\TripleDES\__construct().

◆ MODE_CFB

const phpseclib\Crypt\Base::MODE_CFB = 3

Encrypt / decrypt using the Cipher Feedback mode.

http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29

Definition at line 80 of file Base.php.

Referenced by phpseclib\Crypt\RSA\_parseKey().

◆ MODE_CTR

const phpseclib\Crypt\Base::MODE_CTR = -1

#+ public

See also
::encrypt()
::decrypt() Encrypt / decrypt using the Counter mode.

Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.

http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29

Definition at line 62 of file Base.php.

Referenced by phpseclib\Net\SSH2\_encryption_algorithm_to_crypt_instance(), and phpseclib\Crypt\Random\string().

◆ MODE_ECB

const phpseclib\Crypt\Base::MODE_ECB = 1

Encrypt / decrypt using the Electronic Code Book mode.

http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29

Definition at line 68 of file Base.php.

◆ MODE_OFB

const phpseclib\Crypt\Base::MODE_OFB = 4

Encrypt / decrypt using the Output Feedback mode.

http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29

Definition at line 86 of file Base.php.

◆ MODE_STREAM

const phpseclib\Crypt\Base::MODE_STREAM = 5

Encrypt / decrypt using streaming mode.

Definition at line 90 of file Base.php.

Referenced by phpseclib\Crypt\RC4\__construct().


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