Skip to content

Commit

Permalink
add support for setting target version
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Feb 21, 2024
1 parent 077eccc commit 86d851b
Showing 1 changed file with 169 additions and 2 deletions.
171 changes: 169 additions & 2 deletions lib/mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,22 @@ function phpseclib_mdecrypt_generic(Base $td, $data)
return phpseclib_mcrypt_generic_helper($td, $data, 'decrypt');
}

/**
* This function terminates encryption
*
* Alias of mcrypt_generic_deinit()
*
* @param Base $td
* @return bool
* @access public
*/
function phpseclib_mcrypt_generic_end(Base $td)
{
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-generic-end.php

return phpseclib_mcrypt_generic_deinit($td);
}

/**
* This function deinitializes an encryption module
*
Expand Down Expand Up @@ -962,6 +978,47 @@ function phpseclib_mcrypt_module_self_test($algorithm, $lib_dir = '')
return in_array($algorithm, phpseclib_mcrypt_list_algorithms());
}

/**
* Encrypt / decrypt data using pre PHP 5.6.0 behavior
*
* Performs checks common to both mcrypt_encrypt and mcrypt_decrypt
*
* @param string $cipher
* @param string $key
* @param string $data
* @param string $mode
* @param string $iv
* @param string $op
* @return string|bool
* @access private
*/
function phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, $op)
{
$td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
phpseclib_set_key($td, $key);

$iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
if ($iv_size && phpseclib_mcrypt_module_is_iv_mode($mode)) {
if (!isset($iv)) {
trigger_error(
'mcrypt_' . $op . '(): Attempt to use an empty IV, which is NOT recommended',
E_USER_WARNING
);
$iv = str_repeat("\0", $iv_size);
} elseif (strlen($iv) != $iv_size) {
trigger_error(
'mcrypt_' . $op . '(): The IV parameter must be as long as the blocksize',
E_USER_WARNING
);
$iv = str_repeat("\0", $iv_size);
}
} else {
$iv = null;
}
phpseclib_mcrypt_generic_init($td, $key, $iv);
return $op == 'encrypt' ? phpseclib_mcrypt_generic($td, $data) : phpseclib_mdecrypt_generic($td, $data);
}

/**
* Encrypt / decrypt data
*
Expand Down Expand Up @@ -1027,6 +1084,85 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
return $op == 'encrypt' ? phpseclib_mcrypt_generic($td, $data) : phpseclib_mdecrypt_generic($td, $data);
}

/**
* Encrypts/decrypts data in CFB mode
*
* @param string $cipher
* @param string $key
* @param string $data
* @param int $mode
* @param string $iv optional
* @return string|bool
* @access public
*/
function phpseclib_mcrypt_cfb($cipher, $key, $data, $mode, $iv = null)
{
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cfb.php
return $mode == MCRYPT_ENCRYPT ?
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CFB, $iv) :
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_CFB, $iv);
}

/**
* Encrypts/decrypts data in OFB mode
*
* @param string $cipher
* @param string $key
* @param string $data
* @param int $mode
* @param string $iv optional
* @return string|bool
* @access public
*/
function phpseclib_mcrypt_ofb($cipher, $key, $data, $mode, $iv = null)
{
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ofb.php
return $mode == MCRYPT_ENCRYPT ?
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_OFB, $iv) :
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_OFB, $iv);
}

/**
* Encrypts/decrypts data in CBC mode
*
* @param string $cipher
* @param string $key
* @param string $data
* @param int $mode
* @param string $iv optional
* @return string|bool
* @access public
*/
function phpseclib_mcrypt_cbc($cipher, $key, $data, $mode, $iv = null)
{
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cbc.php
return $mode == MCRYPT_ENCRYPT ?
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv) :
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv);
}

/**
* Encrypts/decrypts data in ECB mode
*
* @param string $cipher
* @param string $key
* @param string $data
* @param int $mode
* @param string $iv optional
* @return string|bool
* @access public
*/
function phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv = null)
{
// idk why mcrypt_ecb had an $iv parameter when ECB mode doesn't use an IV
// but whatever

// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ecb.php
return $mode == MCRYPT_ENCRYPT ?
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_ECB, $iv) :
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_ECB, $iv);
}

/**
* Encrypts plaintext with given parameters
*
Expand All @@ -1042,7 +1178,9 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
*/
function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt');
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt') :
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'encrypt');
}

/**
Expand All @@ -1060,7 +1198,9 @@ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
*/
function phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt');
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt') :
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'decrypt');
}

/**
Expand Down Expand Up @@ -1266,6 +1406,33 @@ public function onClose()

// define
if (!function_exists('mcrypt_list_algorithms')) {
if (defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '7.0.0', '<')) {
function mcrypt_generic_end($td)
{
return phpseclib_mcrypt_generic_end($td);
}

function mcrypt_ecb($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv);
}

function mcrypt_cbc($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_cbc($cipher, $key, $data, $mode, $iv);
}

function mcrypt_cfb($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_cfb($cipher, $key, $data, $mode, $iv);
}

function mcrypt_ofb($cipher, $key, $data, $mode, $iv = null)
{
return phpseclib_mcrypt_ofb($cipher, $key, $data, $mode, $iv);
}
}

function mcrypt_list_algorithms($lib_dir = '')
{
return phpseclib_mcrypt_list_algorithms($lib_dir);
Expand Down

1 comment on commit 86d851b

@terrafrost
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #38

Please # to comment.