Skip to content

Commit 6505669

Browse files
committed
updates for phpseclib 3.0
1 parent 5f7baba commit 6505669

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ language: php
22

33
matrix:
44
include:
5-
- php: 5.6
6-
dist: xenial
75
- php: 7.0
86
dist: xenial
97
- php: 7.1

lib/mcrypt.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use phpseclib3\Crypt\Random;
4242
use phpseclib3\Crypt\Common\SymmetricKey as Base;
4343
use phpseclib3\Common\Functions\Strings;
44+
use phpseclib3\Exception\InsufficientSetupException;
4445

4546
if (!defined('MCRYPT_MODE_ECB')) {
4647
/**#@+
@@ -338,8 +339,6 @@ function phpseclib_mcrypt_module_open($algorithm, $algorithm_directory, $mode, $
338339

339340
$cipher->disablePadding();
340341

341-
$cipher->key = null;
342-
343342
return $cipher;
344343
}
345344

@@ -696,15 +695,6 @@ function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
696695
*/
697696
function phpseclib_mcrypt_generic_helper(Base $td, &$data, $op)
698697
{
699-
// in the orig mcrypt, if mcrypt_generic_init() was called and an empty key was provided you'd get the following error:
700-
// Warning: mcrypt_generic(): supplied resource is not a valid MCrypt resource
701-
// that error doesn't really make a lot of sense in this context since $td is not a resource nor should it be one.
702-
// in light of that we'll just display the same error that you get when you don't call mcrypt_generic_init() at all
703-
if (!isset($td->key)) {
704-
trigger_error('m' . $op . '_generic(): Operation disallowed prior to mcrypt_generic_init().', E_USER_WARNING);
705-
return false;
706-
}
707-
708698
// phpseclib does not currently provide a way to retrieve the mode once it has been set via "public" methods
709699
if (phpseclib_mcrypt_enc_is_block_mode($td)) {
710700
$block_length = phpseclib_mcrypt_enc_get_iv_size($td);
@@ -714,7 +704,16 @@ function phpseclib_mcrypt_generic_helper(Base $td, &$data, $op)
714704
}
715705
}
716706

717-
return $op == 'crypt' ? $td->encrypt($data) : $td->decrypt($data);
707+
try {
708+
return $op == 'crypt' ? $td->encrypt($data) : $td->decrypt($data);
709+
} catch (InsufficientSetupException $e) {
710+
// in the orig mcrypt, if mcrypt_generic_init() was called and an empty key was provided you'd get the following error:
711+
// Warning: mcrypt_generic(): supplied resource is not a valid MCrypt resource
712+
// that error doesn't really make a lot of sense in this context since $td is not a resource nor should it be one.
713+
// in light of that we'll just display the same error that you get when you don't call mcrypt_generic_init() at all
714+
trigger_error('m' . $op . '_generic(): Operation disallowed prior to mcrypt_generic_init().', E_USER_WARNING);
715+
return false;
716+
}
718717
}
719718

720719
/**
@@ -774,15 +773,18 @@ function phpseclib_mdecrypt_generic(Base $td, $data)
774773
* @return bool
775774
* @access public
776775
*/
777-
function phpseclib_mcrypt_generic_deinit(Base $td)
776+
function phpseclib_mcrypt_generic_deinit(Base &$td)
778777
{
779-
if (!isset($td->key)) {
778+
$reflectionObject = new \ReflectionObject($td);
779+
$reflectionProperty = $reflectionObject->getProperty('key');
780+
$reflectionProperty->setAccessible(true); // can be dropped in PHP 8.1.0+
781+
if (!strlen($reflectionProperty->getValue($td))) {
780782
trigger_error('mcrypt_generic_deinit(): Could not terminate encryption specifier', E_USER_WARNING);
781783
return false;
782784
}
783785

784-
$td->disableContinuousBuffer();
785-
$td->key = null;
786+
$class = get_class($td);
787+
$td = new $class($td->getMode());
786788
return true;
787789
}
788790

@@ -797,7 +799,7 @@ function phpseclib_mcrypt_generic_deinit(Base $td)
797799
*/
798800
function phpseclib_mcrypt_module_close(Base $td)
799801
{
800-
$td->key = null;
802+
//$td->key = null;
801803
return true;
802804
}
803805

@@ -1300,7 +1302,7 @@ function mcrypt_generic(Base $td, $data)
13001302
return phpseclib_mcrypt_generic($td, $data);
13011303
}
13021304

1303-
function mcrypt_generic_deinit(Base $td)
1305+
function mcrypt_generic_deinit(Base &$td)
13041306
{
13051307
return phpseclib_mcrypt_generic_deinit($td);
13061308
}

tests/MCryptCompatTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ public function testMcryptGenericHelperWithException()
240240
$data = 'data';
241241
$op = 'operation';
242242
$td = phpseclib_mcrypt_module_open('blowfish', '', 'cbc', '');
243-
unset($td->mcrypt_polyfill_init);
244243
$result = phpseclib_mcrypt_generic_helper($td, $data, $op);
245244
}
246245

@@ -249,7 +248,6 @@ public function testMcryptGenericDeinitWithException()
249248
$this->setExpectedException('PHPUnit_Framework_Error_Warning');
250249

251250
$td = phpseclib_mcrypt_module_open('blowfish', '', 'cbc', '');
252-
unset($td->mcrypt_polyfill_init);
253251
$result = phpseclib_mcrypt_generic_deinit($td);
254252
}
255253

@@ -483,7 +481,7 @@ public function ncfbHelper($prefix)
483481
$v1.= call_user_func($prefix . 'mdecrypt_generic', $td, str_repeat('c', $block));
484482
$v2.= str_repeat('c', $block);
485483
}
486-
call_user_func($prefix . 'mcrypt_generic_deinit', $td);
484+
($prefix . 'mcrypt_generic_deinit')($td);
487485
call_user_func($prefix . 'mcrypt_generic_init', $td, str_repeat('a', 16), str_repeat('a', 16));
488486
$v2 = call_user_func($prefix . 'mdecrypt_generic', $td, $v2);
489487

@@ -1029,6 +1027,19 @@ public function testModuleOpenWithoutInit()
10291027
$result = phpseclib_mcrypt_generic($td, 'zzz');
10301028
}
10311029

1030+
public function testDeInit()
1031+
{
1032+
$key = str_repeat('x', 16);
1033+
$iv = str_repeat('x', 16);
1034+
1035+
$this->setExpectedException('PHPUnit_Framework_Error_Warning');
1036+
1037+
$td = phpseclib_mcrypt_module_open('rijndael-128', '', 'cbc', '');
1038+
phpseclib_mcrypt_generic_init($td, $key, $iv);
1039+
phpseclib_mcrypt_generic_deinit($td);
1040+
$result = phpseclib_mcrypt_generic($td, 'zzz');
1041+
}
1042+
10321043
public function mcryptModuleNameProvider()
10331044
{
10341045
return array(

0 commit comments

Comments
 (0)