Skip to content

Commit 86d851b

Browse files
committed
add support for setting target version
1 parent 077eccc commit 86d851b

File tree

1 file changed

+169
-2
lines changed

1 file changed

+169
-2
lines changed

lib/mcrypt.php

Lines changed: 169 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,22 @@ function phpseclib_mdecrypt_generic(Base $td, $data)
773773
return phpseclib_mcrypt_generic_helper($td, $data, 'decrypt');
774774
}
775775

776+
/**
777+
* This function terminates encryption
778+
*
779+
* Alias of mcrypt_generic_deinit()
780+
*
781+
* @param Base $td
782+
* @return bool
783+
* @access public
784+
*/
785+
function phpseclib_mcrypt_generic_end(Base $td)
786+
{
787+
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-generic-end.php
788+
789+
return phpseclib_mcrypt_generic_deinit($td);
790+
}
791+
776792
/**
777793
* This function deinitializes an encryption module
778794
*
@@ -962,6 +978,47 @@ function phpseclib_mcrypt_module_self_test($algorithm, $lib_dir = '')
962978
return in_array($algorithm, phpseclib_mcrypt_list_algorithms());
963979
}
964980

981+
/**
982+
* Encrypt / decrypt data using pre PHP 5.6.0 behavior
983+
*
984+
* Performs checks common to both mcrypt_encrypt and mcrypt_decrypt
985+
*
986+
* @param string $cipher
987+
* @param string $key
988+
* @param string $data
989+
* @param string $mode
990+
* @param string $iv
991+
* @param string $op
992+
* @return string|bool
993+
* @access private
994+
*/
995+
function phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, $op)
996+
{
997+
$td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
998+
phpseclib_set_key($td, $key);
999+
1000+
$iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
1001+
if ($iv_size && phpseclib_mcrypt_module_is_iv_mode($mode)) {
1002+
if (!isset($iv)) {
1003+
trigger_error(
1004+
'mcrypt_' . $op . '(): Attempt to use an empty IV, which is NOT recommended',
1005+
E_USER_WARNING
1006+
);
1007+
$iv = str_repeat("\0", $iv_size);
1008+
} elseif (strlen($iv) != $iv_size) {
1009+
trigger_error(
1010+
'mcrypt_' . $op . '(): The IV parameter must be as long as the blocksize',
1011+
E_USER_WARNING
1012+
);
1013+
$iv = str_repeat("\0", $iv_size);
1014+
}
1015+
} else {
1016+
$iv = null;
1017+
}
1018+
phpseclib_mcrypt_generic_init($td, $key, $iv);
1019+
return $op == 'encrypt' ? phpseclib_mcrypt_generic($td, $data) : phpseclib_mdecrypt_generic($td, $data);
1020+
}
1021+
9651022
/**
9661023
* Encrypt / decrypt data
9671024
*
@@ -1027,6 +1084,85 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
10271084
return $op == 'encrypt' ? phpseclib_mcrypt_generic($td, $data) : phpseclib_mdecrypt_generic($td, $data);
10281085
}
10291086

1087+
/**
1088+
* Encrypts/decrypts data in CFB mode
1089+
*
1090+
* @param string $cipher
1091+
* @param string $key
1092+
* @param string $data
1093+
* @param int $mode
1094+
* @param string $iv optional
1095+
* @return string|bool
1096+
* @access public
1097+
*/
1098+
function phpseclib_mcrypt_cfb($cipher, $key, $data, $mode, $iv = null)
1099+
{
1100+
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cfb.php
1101+
return $mode == MCRYPT_ENCRYPT ?
1102+
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CFB, $iv) :
1103+
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_CFB, $iv);
1104+
}
1105+
1106+
/**
1107+
* Encrypts/decrypts data in OFB mode
1108+
*
1109+
* @param string $cipher
1110+
* @param string $key
1111+
* @param string $data
1112+
* @param int $mode
1113+
* @param string $iv optional
1114+
* @return string|bool
1115+
* @access public
1116+
*/
1117+
function phpseclib_mcrypt_ofb($cipher, $key, $data, $mode, $iv = null)
1118+
{
1119+
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ofb.php
1120+
return $mode == MCRYPT_ENCRYPT ?
1121+
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_OFB, $iv) :
1122+
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_OFB, $iv);
1123+
}
1124+
1125+
/**
1126+
* Encrypts/decrypts data in CBC mode
1127+
*
1128+
* @param string $cipher
1129+
* @param string $key
1130+
* @param string $data
1131+
* @param int $mode
1132+
* @param string $iv optional
1133+
* @return string|bool
1134+
* @access public
1135+
*/
1136+
function phpseclib_mcrypt_cbc($cipher, $key, $data, $mode, $iv = null)
1137+
{
1138+
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cbc.php
1139+
return $mode == MCRYPT_ENCRYPT ?
1140+
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv) :
1141+
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv);
1142+
}
1143+
1144+
/**
1145+
* Encrypts/decrypts data in ECB mode
1146+
*
1147+
* @param string $cipher
1148+
* @param string $key
1149+
* @param string $data
1150+
* @param int $mode
1151+
* @param string $iv optional
1152+
* @return string|bool
1153+
* @access public
1154+
*/
1155+
function phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv = null)
1156+
{
1157+
// idk why mcrypt_ecb had an $iv parameter when ECB mode doesn't use an IV
1158+
// but whatever
1159+
1160+
// https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ecb.php
1161+
return $mode == MCRYPT_ENCRYPT ?
1162+
phpseclib_mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_ECB, $iv) :
1163+
phpseclib_mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_ECB, $iv);
1164+
}
1165+
10301166
/**
10311167
* Encrypts plaintext with given parameters
10321168
*
@@ -1042,7 +1178,9 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
10421178
*/
10431179
function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
10441180
{
1045-
return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt');
1181+
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
1182+
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt') :
1183+
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'encrypt');
10461184
}
10471185

10481186
/**
@@ -1060,7 +1198,9 @@ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
10601198
*/
10611199
function phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null)
10621200
{
1063-
return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt');
1201+
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
1202+
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt') :
1203+
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'decrypt');
10641204
}
10651205

10661206
/**
@@ -1266,6 +1406,33 @@ public function onClose()
12661406

12671407
// define
12681408
if (!function_exists('mcrypt_list_algorithms')) {
1409+
if (defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '7.0.0', '<')) {
1410+
function mcrypt_generic_end($td)
1411+
{
1412+
return phpseclib_mcrypt_generic_end($td);
1413+
}
1414+
1415+
function mcrypt_ecb($cipher, $key, $data, $mode, $iv = null)
1416+
{
1417+
return phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv);
1418+
}
1419+
1420+
function mcrypt_cbc($cipher, $key, $data, $mode, $iv = null)
1421+
{
1422+
return phpseclib_mcrypt_cbc($cipher, $key, $data, $mode, $iv);
1423+
}
1424+
1425+
function mcrypt_cfb($cipher, $key, $data, $mode, $iv = null)
1426+
{
1427+
return phpseclib_mcrypt_cfb($cipher, $key, $data, $mode, $iv);
1428+
}
1429+
1430+
function mcrypt_ofb($cipher, $key, $data, $mode, $iv = null)
1431+
{
1432+
return phpseclib_mcrypt_ofb($cipher, $key, $data, $mode, $iv);
1433+
}
1434+
}
1435+
12691436
function mcrypt_list_algorithms($lib_dir = '')
12701437
{
12711438
return phpseclib_mcrypt_list_algorithms($lib_dir);

0 commit comments

Comments
 (0)