@@ -773,78 +773,121 @@ public function test2DES()
773773 $ this ->assertEquals (bin2hex ($ mcrypt ), bin2hex ($ compat ));
774774 }
775775
776- /**
777- * demonstrates how mcrypt deals with short IV's in stream mode
778- */
779- public function testIVOnStream ()
776+ public function testMcryptGenericWithTwoParamsPHPPre71 ()
780777 {
781- if (! extension_loaded ( ' mcrypt ' ) ) {
782- $ this ->markTestSkipped ('mcrypt must be demonstrate it \' s behaviors ' );
778+ if (version_compare ( PHP_VERSION , ' 7.1.0 ' ) >= 0 ) {
779+ $ this ->markTestSkipped ('PHPUnit_Framework_Error_Warning exception is thrown for legacy PHP versions only ' );
783780 }
784781
785782 $ this ->setExpectedException ('PHPUnit_Framework_Error_Warning ' );
786783
787- $ td = mcrypt_module_open (MCRYPT_ARCFOUR , '' , MCRYPT_MODE_STREAM , '' );
788- mcrypt_generic_init ($ td , 'xxx ' , ' x ' );
784+ $ td = phpseclib_mcrypt_module_open (MCRYPT_ARCFOUR , '' , MCRYPT_MODE_STREAM , '' );
785+ phpseclib_mcrypt_generic_init ($ td , 'xxx ' );
789786 }
790787
791- /**
792- * demonstrates how phpseclib deals with short IV's in stream mode
793- */
794- public function testIVOnStreamPHP ()
788+ public function testMcryptGenericWithTwoParamsPHPPost71 ()
795789 {
796- $ this ->setExpectedException ('PHPUnit_Framework_Error_Warning ' );
790+ if (version_compare (PHP_VERSION , '7.1.0 ' ) < 0 ) {
791+ $ this ->markTestSkipped ('ArgumentCountError exception is thrown for newer PHP versions only ' );
792+ }
793+
794+ $ this ->setExpectedException ('ArgumentCountError ' );
797795
798796 $ td = phpseclib_mcrypt_module_open (MCRYPT_ARCFOUR , '' , MCRYPT_MODE_STREAM , '' );
799- phpseclib_mcrypt_generic_init ($ td , 'xxx ' , ' x ' );
797+ phpseclib_mcrypt_generic_init ($ td , 'xxx ' );
800798 }
801799
802- /**
803- * demonstrates how mcrypt deals with short IV's in ECB mode (eg. no warning thrown)
804- *
805- * @requires PHP 7.0
806- */
807- public function testIVOnECB ()
808- {
809- if (!extension_loaded ('mcrypt ' )) {
810- $ this ->markTestSkipped ('mcrypt must be demonstrate it \'s behaviors ' );
800+ public function providerForIVSizeChecks ()
801+ {
802+ $ tests = [
803+ [ '' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'generic ' , 8 , '44448888 ' , 8 , false ],
804+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'generic ' , 8 , '44448888 ' , 8 , false ],
805+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'generic ' , 0 , '44448888 ' , 8 , 'Iv size incorrect; supplied length: 0, needed: 8 ' ],
806+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'generic ' , 4 , '44448888 ' , 8 , 'Iv size incorrect; supplied length: 4, needed: 8 ' ],
807+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'generic ' , 0 , '44448888 ' , 0 , false ],
808+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'generic ' , 4 , '44448888 ' , 0 , 'Iv size incorrect; supplied length: 4, needed: 0 ' ],
809+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'generic ' , 8 , '44448888 ' , 0 , 'Iv size incorrect; supplied length: 8, needed: 0 ' ],
810+ [ '' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'decrypt ' , 0 , '44448888 ' , 8 , false ],
811+ [ '' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'decrypt ' , 4 , '44448888 ' , 8 , false ],
812+ [ '' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'decrypt ' , 8 , '44448888 ' , 8 , false ],
813+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'decrypt ' , 8 , '44448888 ' , 8 , false ],
814+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'decrypt ' , 0 , '44448888 ' , 0 , false ],
815+ // here comes a known, but acceptable difference between the ext and phpseclib:
816+ [ 'compat ' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'generic ' , 0 , '44448888 ' , 8 , false ],
817+ [ 'compat ' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'generic ' , 4 , '44448888 ' , 8 , false ],
818+ [ 'ext ' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'generic ' , 0 , '44448888 ' , 8 , PHP_VERSION_ID >= 70000 ? false : 'Iv size incorrect; supplied length: 0, needed: 8 ' ],
819+ [ 'ext ' , MCRYPT_3DES , MCRYPT_MODE_ECB , 'generic ' , 4 , '44448888 ' , 8 , PHP_VERSION_ID >= 70000 ? false : 'Iv size incorrect; supplied length: 4, needed: 8 ' ],
820+ ];
821+ if (PHP_VERSION_ID >= 56000 ) {
822+ $ tests += [
823+ // the following produce errors in older versions of PHP but stopped as of PHP 5.6+
824+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'decrypt ' , 4 , '44448888 ' , 0 , false ],
825+ [ '' , MCRYPT_ARCFOUR , MCRYPT_MODE_STREAM , 'decrypt ' , 8 , '44448888 ' , 0 , false ],
826+ // the following produced an error with a different message before PHP 5.6. mcrypt_compat uses the
827+ // PHP 5.6+ error messages.
828+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'decrypt ' , 0 , '44448888 ' , 8 , 'initialization vector of size 0, but size 8 is required ' ],
829+ [ '' , MCRYPT_3DES , MCRYPT_MODE_CBC , 'decrypt ' , 4 , '44448888 ' , 8 , 'initialization vector of size 4, but size 8 is required ' ]
830+ ];
831+ }
832+
833+ $ all_tests = [];
834+ foreach ($ tests as $ test ) {
835+ if (empty ($ test [0 ])) {
836+ $ test [0 ] = 'ext ' ;
837+ $ all_tests [] = $ test ;
838+ $ test [0 ] = 'compat ' ;
839+ $ all_tests [] = $ test ;
840+ } else {
841+ $ all_tests [] = $ test ;
842+ }
811843 }
812844
813- $ td = mcrypt_module_open (MCRYPT_RIJNDAEL_128 , '' , MCRYPT_MODE_ECB , '' );
814- mcrypt_generic_init ($ td , 'x ' , 'x ' );
845+ return $ all_tests ;
815846 }
816847
817848 /**
818- * demonstrates how phpseclib deals with short IV's in ECB mode (eg. no warning thrown)
849+ * @dataProvider providerForIVSizeChecks
819850 */
820- public function testIVOnECBPHP ( )
851+ public function testCompareIVSizeChecks ( $ ext_or_compat , $ cipher , $ mode , $ api , $ input_iv_size , $ input , $ expected_iv_size , $ expected_warning )
821852 {
822- $ td = phpseclib_mcrypt_module_open ( MCRYPT_RIJNDAEL_128 , '' , MCRYPT_MODE_ECB , '' );
823- phpseclib_mcrypt_generic_init ( $ td , ' x ' , ' x ' );
824- }
853+ $ this -> assertSame ( mcrypt_get_key_size ( $ cipher , $ mode ), phpseclib_mcrypt_get_key_size ( $ cipher , $ mode ) );
854+ $ this -> assertSame ( $ expected_iv_size , mcrypt_get_iv_size ( $ cipher , $ mode ) );
855+ $ this -> assertSame ( $ expected_iv_size , phpseclib_mcrypt_get_iv_size ( $ cipher , $ mode ));
825856
826- public function testMcryptGenericWithTwoParamsPHPPre71 ()
827- {
828- if (version_compare (PHP_VERSION , '7.1.0 ' ) >= 0 ) {
829- $ this ->markTestSkipped ('PHPUnit_Framework_Error_Warning exception is thrown for legacy PHP versions only ' );
857+ $ key = str_repeat ('X ' , phpseclib_mcrypt_get_key_size ($ cipher , $ mode ));
858+ $ iv = str_repeat ('Y ' , $ input_iv_size );
859+
860+ if ($ ext_or_compat === 'ext ' && !extension_loaded ('mcrypt ' )) {
861+ $ this ->markTestSkipped ('mcrypt extension not loaded ' );
830862 }
831863
832- $ this ->setExpectedException ('PHPUnit_Framework_Error_Warning ' );
864+ if ($ expected_warning ) {
865+ $ this ->setExpectedException (PHPUnit_Framework_Error_Warning::class, $ expected_warning );
866+ }
833867
834- $ td = phpseclib_mcrypt_module_open (MCRYPT_ARCFOUR , '' , MCRYPT_MODE_STREAM , '' );
835- phpseclib_mcrypt_generic_init ($ td , 'xxx ' );
868+ if ($ api === 'generic ' && $ ext_or_compat === 'ext ' ) {
869+ $ td = mcrypt_module_open ($ cipher , '' , $ mode , '' );
870+ mcrypt_generic_init ($ td , $ key , $ iv );
871+ mcrypt_generic_deinit ($ td );
872+ mcrypt_module_close ($ td );
873+ } elseif ($ api === 'generic ' ) {
874+ $ td = phpseclib_mcrypt_module_open ($ cipher , '' , $ mode , '' );
875+ phpseclib_mcrypt_generic_init ($ td , $ key , $ iv );
876+ phpseclib_mcrypt_generic_deinit ($ td );
877+ phpseclib_mcrypt_module_close ($ td );
878+ } elseif ($ ext_or_compat === 'ext ' ) {
879+ mcrypt_encrypt ($ cipher , $ key , $ input , $ mode , $ iv );
880+ } else {
881+ phpseclib_mcrypt_encrypt ($ cipher , $ key , $ input , $ mode , $ iv );
882+ }
836883 }
837884
838- public function testMcryptGenericWithTwoParamsPHPPost71 ()
885+ public function testTripleDESECBParameters ()
839886 {
840- if (version_compare (PHP_VERSION , '7.1.0 ' ) < 0 ) {
841- $ this ->markTestSkipped ('ArgumentCountError exception is thrown for newer PHP versions only ' );
842- }
843-
844- $ this ->setExpectedException ('ArgumentCountError ' );
845-
846- $ td = phpseclib_mcrypt_module_open (MCRYPT_ARCFOUR , '' , MCRYPT_MODE_STREAM , '' );
847- phpseclib_mcrypt_generic_init ($ td , 'xxx ' );
887+ $ key_size = phpseclib_mcrypt_get_key_size (MCRYPT_3DES , MCRYPT_MODE_ECB );
888+ $ this ->assertSame (24 , $ key_size );
889+ $ iv_size = phpseclib_mcrypt_get_iv_size (MCRYPT_3DES , MCRYPT_MODE_ECB );
890+ $ this ->assertSame (8 , $ iv_size );
848891 }
849892
850893 public function mcryptModuleNameProvider ()
0 commit comments