Skip to content

Commit cb31b37

Browse files
committed
add missing mcrypt_get_* functions
1 parent b07601a commit cb31b37

File tree

2 files changed

+182
-3
lines changed

2 files changed

+182
-3
lines changed

lib/mcrypt.php

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* mcrypt polyfill
45
*
@@ -311,6 +312,99 @@ function phpseclib_mcrypt_enc_get_key_size(Base $td)
311312
return $backup->getKeyLength() >> 3;
312313
}
313314

315+
/**
316+
* Gets the name of the specified cipher
317+
*
318+
* @param string $cipher
319+
* @return mixed
320+
* @access public
321+
*/
322+
function phpseclib_mcrypt_get_cipher_name($cipher)
323+
{
324+
switch ($cipher) {
325+
case 'rijndael-128':
326+
return 'Rijndael-128';
327+
case 'twofish':
328+
return 'Twofish';
329+
case 'rijndael-192':
330+
return 'Rijndael-192';
331+
case 'des':
332+
return 'DES';
333+
case 'rijndael-256':
334+
return 'Rijndael-256';
335+
case 'blowfish':
336+
return 'Blowfish';
337+
case 'rc2':
338+
return 'RC2';
339+
case 'tripledes':
340+
return '3DES';
341+
case 'arcfour':
342+
return 'RC4';
343+
default:
344+
return false;
345+
}
346+
}
347+
348+
/**
349+
* Gets the block size of the specified cipher
350+
*
351+
* @param string $cipher
352+
* @param string $mode optional
353+
* @return int
354+
* @access public
355+
*/
356+
function phpseclib_mcrypt_get_block_size($cipher, $mode = false)
357+
{
358+
if (!$mode) {
359+
$mode = $cipher == 'rc4' ? 'stream' : 'cbc';
360+
}
361+
$td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
362+
if ($td === false) {
363+
trigger_error('mcrypt_get_block_size(): Module initialization failed', E_USER_WARNING);
364+
return false;
365+
}
366+
return phpseclib_mcrypt_enc_get_block_size($td);
367+
}
368+
369+
/**
370+
* Gets the key size of the specified cipher
371+
*
372+
* @param string $cipher
373+
* @param string $mode optional
374+
* @return int
375+
* @access public
376+
*/
377+
function phpseclib_mcrypt_get_key_size($cipher, $mode = false)
378+
{
379+
if (!$mode) {
380+
$mode = $cipher == 'rc4' ? 'stream' : 'cbc';
381+
}
382+
$td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
383+
if ($td === false) {
384+
trigger_error('mcrypt_get_key_size(): Module initialization failed', E_USER_WARNING);
385+
return false;
386+
}
387+
return phpseclib_mcrypt_enc_get_key_size($td);
388+
}
389+
390+
/**
391+
* Returns the size of the IV belonging to a specific cipher/mode combination
392+
*
393+
* @param string $cipher
394+
* @param string $mode
395+
* @return int
396+
* @access public
397+
*/
398+
function phpseclib_mcrypt_get_iv_size($cipher, $mode)
399+
{
400+
$td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
401+
if ($td === false) {
402+
trigger_error('mcrypt_get_iv_size(): Module initialization failed', E_USER_WARNING);
403+
return false;
404+
}
405+
return phpseclib_mcrypt_enc_get_iv_size($td);
406+
}
407+
314408
/**
315409
* Returns the maximum supported keysize of the opened mode
316410
*
@@ -325,6 +419,10 @@ function phpseclib_mcrypt_module_get_algo_key_size($algorithm, $lib_dir = '')
325419
{
326420
$mode = $algorithm == 'rc4' ? 'stream' : 'cbc';
327421
$td = @phpseclib_mcrypt_module_open($algorithm, '', $mode, '');
422+
if ($td === false) {
423+
trigger_error('mcrypt_module_get_algo_key_size(): Module initialization failed', E_USER_WARNING);
424+
return false;
425+
}
328426
return phpseclib_mcrypt_enc_get_key_size($td);
329427
}
330428

@@ -669,7 +767,6 @@ function phpseclib_mcrypt_module_get_supported_key_sizes($algorithm, $lib_dir =
669767
return array(24);
670768
//case 'arcfour':
671769
//case 'blowfish':
672-
//case 'blowfish-compat':
673770
//case 'rc2':
674771
default:
675772
return array();
@@ -730,7 +827,6 @@ function phpseclib_mcrypt_module_is_block_algorithm($algorithm, $lib_dir = '')
730827
case 'rijndael-128':
731828
case 'twofish':
732829
case 'rijndael-192':
733-
case 'blowfish-compat':
734830
case 'des':
735831
case 'rijndael-256':
736832
case 'blowfish':
@@ -1119,6 +1215,26 @@ function mcrypt_module_get_algo_block_size($algorithm, $lib_dir = '')
11191215
return phpseclib_mcrypt_module_get_algo_block_size($algorithm, $lib_dir);
11201216
}
11211217

1218+
function mcrypt_get_block_size($cipher, $mode = '')
1219+
{
1220+
return phpseclib_mcrypt_get_block_size($cipher, $mode);
1221+
}
1222+
1223+
function mcrypt_get_cipher_name($cipher)
1224+
{
1225+
return phpseclib_mcrypt_get_cipher_name($cipher);
1226+
}
1227+
1228+
function mcrypt_get_key_size($cipher, $mode = false)
1229+
{
1230+
return phpseclib_mcrypt_get_key_size($cipher, $mode);
1231+
}
1232+
1233+
function mcrypt_get_iv_size($cipher, $mode)
1234+
{
1235+
return phpseclib_mcrypt_get_iv_size($cipher, $mode);
1236+
}
1237+
11221238
function mcrypt_module_get_algo_key_size($algorithm, $lib_dir = '')
11231239
{
11241240
return phpseclib_mcrypt_module_get_algo_key_size($algorithm, $lib_dir);

tests/MCryptCompatTest.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,70 @@ public function testMcryptEncGetBlockSize()
7272
$this->assertEquals($expectedBlockLen, $result);
7373
}
7474

75+
/**
76+
* @expectedException PHPUnit_Framework_Error_Warning
77+
*/
78+
public function testMcryptGetAlgoKeySizeBad()
79+
{
80+
phpseclib_mcrypt_module_get_algo_key_size('zzz');
81+
}
82+
83+
public function testMcryptGetAlgoKeySizeGood()
84+
{
85+
$this->assertEquals(
86+
mcrypt_module_get_algo_key_size('rijndael-128'),
87+
phpseclib_mcrypt_module_get_algo_key_size('rijndael-128')
88+
);
89+
}
90+
91+
/**
92+
* @expectedException PHPUnit_Framework_Error_Warning
93+
*/
94+
public function testMcryptGetIVSizeBad()
95+
{
96+
phpseclib_mcrypt_get_iv_size('zzz', 'cbc');
97+
}
98+
99+
public function testMcryptGetIVSizeGood()
100+
{
101+
$this->assertEquals(
102+
mcrypt_get_iv_size('rijndael-128', 'cbc'),
103+
phpseclib_mcrypt_get_iv_size('rijndael-128', 'cbc')
104+
);
105+
}
106+
107+
/**
108+
* @expectedException PHPUnit_Framework_Error_Warning
109+
*/
110+
public function testMcryptGetKeySizeBad()
111+
{
112+
phpseclib_mcrypt_get_key_size('zzz', 'cbc');
113+
}
114+
115+
public function testMcryptGetKeySizeGood()
116+
{
117+
$this->assertEquals(
118+
mcrypt_get_key_size('rijndael-128', 'cbc'),
119+
phpseclib_mcrypt_get_key_size('rijndael-128', 'cbc')
120+
);
121+
}
122+
123+
/**
124+
* @expectedException PHPUnit_Framework_Error_Warning
125+
*/
126+
public function testMcryptGetBlockSizeBad()
127+
{
128+
phpseclib_mcrypt_get_block_size('zzz', 'cbc');
129+
}
130+
131+
public function testMcryptGetBlockSizeGood()
132+
{
133+
$this->assertEquals(
134+
mcrypt_get_block_size('rijndael-128', 'cbc'),
135+
phpseclib_mcrypt_get_block_size('rijndael-128', 'cbc')
136+
);
137+
}
138+
75139
/**
76140
* @dataProvider mcryptEncGetAlgorithmsNameProvider
77141
*/
@@ -675,7 +739,6 @@ public function mcryptBlockModuleAlgoNameProvider()
675739
array('rijndael-128', true),
676740
array('twofish', true),
677741
array('rijndael-192', true),
678-
array('blowfish-compat', true),
679742
array('des', true),
680743
array('rijndael-256', true),
681744
array('blowfish', true),

0 commit comments

Comments
 (0)