Skip to content

Commit a7eee16

Browse files
committed
add OFB support
1 parent 5ab00f1 commit a7eee16

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require": {
2424
"php": ">=5.3.3",
25-
"phpseclib/phpseclib": ">=2.0.11 <3.0.0"
25+
"phpseclib/phpseclib": ">=2.0.36 <3.0.0"
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "^4.8.35|^5.7|^6.0|^9.4"

lib/mcrypt.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function phpseclib_mcrypt_list_modes($lib_dir = '')
226226
'ecb',
227227
'ncfb',
228228
'nofb',
229-
//'ofb',
229+
'ofb',
230230
'stream'
231231
);
232232
}
@@ -273,6 +273,7 @@ function phpseclib_mcrypt_module_open($algorithm, $algorithm_directory, $mode, $
273273
'ecb' => Base::MODE_ECB,
274274
'cbc' => Base::MODE_CBC,
275275
'cfb' => Base::MODE_CFB8,
276+
'ofb' => Base::MODE_OFB8,
276277
'ncfb'=> Base::MODE_CFB,
277278
'nofb'=> Base::MODE_OFB,
278279
'stream' => Base::MODE_STREAM
@@ -571,6 +572,8 @@ function phpseclib_mcrypt_enc_get_modes_name(Base $td)
571572
Base::MODE_CBC => 'CBC',
572573
Base::MODE_CFB => 'nCFB',
573574
Base::MODE_OFB => 'nOFB',
575+
Base::MODE_CFB8 => 'CFB',
576+
Base::MODE_OFB8 => 'OFB',
574577
Base::MODE_STREAM => 'STREAM'
575578
);
576579

@@ -847,6 +850,7 @@ function phpseclib_mcrypt_module_is_block_algorithm_mode($mode, $lib_dir = '')
847850
case 'ctr':
848851
case 'ecb':
849852
case 'cfb':
853+
case 'ofb':
850854
case 'ncfb':
851855
case 'nofb':
852856
return true;

tests/MCryptCompatTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,53 @@ public function test128ByteRC2Key()
962962
}
963963
}
964964

965+
public function testOFB()
966+
{
967+
$key = str_repeat('x', 32);
968+
$iv = str_repeat('x', 32);
969+
$plaintext = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
970+
$ciphertext = '3d49c7fab4f20c3cc2a54e5c0a22a4feceffb6fc758ee3800380614042c567731a35cf';
971+
972+
$td = phpseclib_mcrypt_module_open('rijndael-256', '', 'ofb', '');
973+
phpseclib_mcrypt_generic_init($td, $key, $iv);
974+
$mcrypt = bin2hex(phpseclib_mcrypt_generic($td, $plaintext));
975+
976+
$this->assertEquals(
977+
$ciphertext,
978+
$mcrypt
979+
);
980+
981+
$td = phpseclib_mcrypt_module_open('rijndael-256', '', 'ofb', '');
982+
phpseclib_mcrypt_generic_init($td, $key, $iv);
983+
$mcrypt = bin2hex(phpseclib_mcrypt_generic($td, substr($plaintext, 0, -1)));
984+
$mcrypt.= bin2hex(phpseclib_mcrypt_generic($td, substr($plaintext, -1)));
985+
986+
$this->assertEquals(
987+
$ciphertext,
988+
$mcrypt
989+
);
990+
991+
$td = phpseclib_mcrypt_module_open('rijndael-256', '', 'ofb', '');
992+
phpseclib_mcrypt_generic_init($td, $key, $iv);
993+
$reference = phpseclib_mdecrypt_generic($td, pack('H*', $ciphertext));
994+
995+
$this->assertEquals(
996+
$plaintext,
997+
$reference
998+
);
999+
1000+
if (extension_loaded('mcrypt')) {
1001+
$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
1002+
mcrypt_generic_init($td, $key, $iv);
1003+
$mcrypt = bin2hex(mcrypt_generic($td, $plaintext));
1004+
1005+
$this->assertEquals(
1006+
$ciphertext,
1007+
$mcrypt
1008+
);
1009+
}
1010+
}
1011+
9651012
public function mcryptModuleNameProvider()
9661013
{
9671014
return array(
@@ -997,6 +1044,7 @@ public function mcryptBlockModuleNameProvider()
9971044
array('ctr', true),
9981045
array('ecb', true),
9991046
array('cfb', true),
1047+
array('ofb', true),
10001048
array('ncfb', true),
10011049
array('nofb', true),
10021050
array('invalid-mode', false)

0 commit comments

Comments
 (0)