mcrypt.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. <?php
  2. /**
  3. * mcrypt polyfill
  4. *
  5. * PHP 7.1 removed the mcrypt extension. This provides a compatibility layer for legacy applications.
  6. *
  7. * PHP versions 5 and 7
  8. *
  9. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * @author Jim Wigginton <terrafrost@php.net>
  28. * @copyright 2016 Jim Wigginton
  29. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  30. * @link http://phpseclib.sourceforge.net
  31. */
  32. use phpseclib\Crypt\Rijndael;
  33. use phpseclib\Crypt\Twofish;
  34. use phpseclib\Crypt\Blowfish;
  35. use phpseclib\Crypt\TripleDES;
  36. use phpseclib\Crypt\DES;
  37. use phpseclib\Crypt\RC2;
  38. use phpseclib\Crypt\RC4;
  39. use phpseclib\Crypt\Random;
  40. use phpseclib\Crypt\Base;
  41. if (!defined('MCRYPT_MODE_ECB')) {
  42. /**#@+
  43. * mcrypt constants
  44. *
  45. * @access public
  46. */
  47. // http://php.net/manual/en/mcrypt.constants.php
  48. define('MCRYPT_MODE_ECB', 'ecb');
  49. define('MCRYPT_MODE_CBC', 'cbc');
  50. define('MCRYPT_MODE_CFB', 'cfb');
  51. define('MCRYPT_MODE_OFB', 'ofb');
  52. define('MCRYPT_MODE_NOFB', 'nofb');
  53. define('MCRYPT_MODE_STREAM', 'stream');
  54. define('MCRYPT_ENCRYPT', 0);
  55. define('MCRYPT_DECRYPT', 1);
  56. define('MCRYPT_DEV_RANDOM', 0);
  57. define('MCRYPT_DEV_URANDOM', 1);
  58. define('MCRYPT_RAND', 2);
  59. // http://php.net/manual/en/mcrypt.ciphers.php
  60. define('MCRYPT_3DES', 'tripledes');
  61. define('MCRYPT_ARCFOUR_IV', 'arcfour-iv');
  62. define('MCRYPT_ARCFOUR', 'arcfour');
  63. define('MCRYPT_BLOWFISH', 'blowfish');
  64. define('MCRYPT_CAST_128', 'cast-128');
  65. define('MCRYPT_CAST_256', 'cast-256');
  66. define('MCRYPT_CRYPT', 'crypt');
  67. define('MCRYPT_DES', 'des');
  68. // MCRYPT_DES_COMPAT?
  69. // MCRYPT_ENIGMA?
  70. define('MCRYPT_GOST', 'gost');
  71. define('MCRYPT_IDEA', 'idea');
  72. define('MCRYPT_LOKI97', 'loki97');
  73. define('MCRYPT_MARS', 'mars');
  74. define('MCRYPT_PANAMA', 'panama');
  75. define('MCRYPT_RIJNDAEL_128', 'rijndael-128');
  76. define('MCRYPT_RIJNDAEL_192', 'rijndael-192');
  77. define('MCRYPT_RIJNDAEL_256', 'rijndael-256');
  78. define('MCRYPT_RC2', 'rc2');
  79. // MCRYPT_RC4?
  80. define('MCRYPT_RC6', 'rc6');
  81. // MCRYPT_RC6_128
  82. // MCRYPT_RC6_192
  83. // MCRYPT_RC6_256
  84. define('MCRYPT_SAFER64', 'safer-sk64');
  85. define('MCRYPT_SAFER128', 'safer-sk128');
  86. define('MCRYPT_SAFERPLUS', 'saferplus');
  87. define('MCRYPT_SERPENT', 'serpent');
  88. // MCRYPT_SERPENT_128?
  89. // MCRYPT_SERPENT_192?
  90. // MCRYPT_SERPENT_256?
  91. define('MCRYPT_SKIPJACK', 'skipjack');
  92. // MCRYPT_TEAN?
  93. define('MCRYPT_THREEWAY', 'threeway');
  94. define('MCRYPT_TRIPLEDES', 'tripledes');
  95. define('MCRYPT_TWOFISH', 'twofish');
  96. // MCRYPT_TWOFISH128?
  97. // MCRYPT_TWOFISH192?
  98. // MCRYPT_TWOFISH256?
  99. define('MCRYPT_WAKE', 'wake');
  100. define('MCRYPT_XTEA', 'xtea');
  101. /**#@-*/
  102. }
  103. if (!function_exists('phpseclib_mcrypt_list_algorithms')) {
  104. /**
  105. * Sets the key
  106. *
  107. * @param \phpseclib\Crypt\Base $td
  108. * @param string $key
  109. * @access private
  110. */
  111. function phpseclib_set_key(Base $td, $key)
  112. {
  113. $length = $origLength = strlen($key);
  114. $reflection = new \ReflectionClass($td);
  115. switch ($reflection->getShortName()) {
  116. case 'TripleDES':
  117. $length = 24;
  118. break;
  119. case 'DES':
  120. $length = 8;
  121. break;
  122. case 'Twofish':
  123. case 'Rijndael':
  124. switch (true) {
  125. case $length <= 16:
  126. $length = 16;
  127. break;
  128. case $length <= 24:
  129. $length = 24;
  130. break;
  131. default:
  132. $length = 32;
  133. }
  134. break;
  135. case 'Blowfish':
  136. switch (true) {
  137. case $length <= 3:
  138. while (strlen($key) <= 5) {
  139. $key.= $key;
  140. }
  141. $key = substr($key, 0, 6);
  142. $td->setKey($key);
  143. return;
  144. case $length > 56:
  145. $length = 56;
  146. }
  147. break;
  148. case 'RC2':
  149. if ($length > 56) {
  150. $length = 56;
  151. }
  152. break;
  153. case 'RC4':
  154. if ($length > 256) {
  155. $length = 256;
  156. }
  157. }
  158. if ($length != $origLength) {
  159. $key = str_pad(substr($key, 0, $length), $length, "\0");
  160. }
  161. $td->setKey($key);
  162. }
  163. /**
  164. * Sets the IV
  165. *
  166. * @param \phpseclib\Crypt\Base $td
  167. * @param string $iv
  168. * @access private
  169. */
  170. function phpseclib_set_iv(Base $td, $iv)
  171. {
  172. if (phpseclib_mcrypt_module_is_iv_mode($td->mcrypt_mode)) {
  173. $length = $td->getBlockLength() >> 3;
  174. $iv = str_pad(substr($iv, 0, $length), $length, "\0");
  175. $td->setIV($iv);
  176. }
  177. }
  178. /**
  179. * Gets an array of all supported ciphers.
  180. *
  181. * @param string $lib_dir optional
  182. * @return array
  183. * @access public
  184. */
  185. function phpseclib_mcrypt_list_algorithms($lib_dir = '')
  186. {
  187. return array(
  188. 'rijndael-128',
  189. 'twofish',
  190. 'rijndael-192',
  191. 'blowfish-compat',
  192. 'des',
  193. 'rijndael-256',
  194. 'blowfish',
  195. 'rc2',
  196. 'tripledes',
  197. 'arcfour'
  198. );
  199. }
  200. /**
  201. * Gets an array of all supported modes
  202. *
  203. * @param string $lib_dir optional
  204. * @return array
  205. * @access public
  206. */
  207. function phpseclib_mcrypt_list_modes($lib_dir = '')
  208. {
  209. return array(
  210. 'cbc',
  211. 'cfb',
  212. 'ctr',
  213. 'ecb',
  214. 'ncfb',
  215. 'nofb',
  216. //'ofb',
  217. 'stream'
  218. );
  219. }
  220. /**
  221. * Creates an initialization vector (IV) from a random source
  222. *
  223. * The IV is only meant to give an alternative seed to the encryption routines. This IV does not need
  224. * to be secret at all, though it can be desirable. You even can send it along with your ciphertext
  225. * without losing security.
  226. *
  227. * @param int $size
  228. * @param int $source optional
  229. * @return string
  230. * @access public
  231. */
  232. function phpseclib_mcrypt_create_iv($size, $source = MCRYPT_DEV_URANDOM)
  233. {
  234. if ($size < 1 || $size > 0x7FFFFFFF) {
  235. trigger_error('mcrypt_create_iv(): Cannot create an IV with a size of less than 1 or greater than 2147483647', E_USER_WARNING);
  236. return '';
  237. }
  238. return Random::string($size);
  239. }
  240. /**
  241. * Opens the module of the algorithm and the mode to be used
  242. *
  243. * This function opens the module of the algorithm and the mode to be used. The name of the algorithm
  244. * is specified in algorithm, e.g. "twofish" or is one of the MCRYPT_ciphername constants. The module
  245. * is closed by calling mcrypt_module_close().
  246. *
  247. * @param string $algorithm
  248. * @param string $algorithm_directory
  249. * @param string $mode
  250. * @param string $mode_directory
  251. * @return object
  252. * @access public
  253. */
  254. function phpseclib_mcrypt_module_open($algorithm, $algorithm_directory, $mode, $mode_directory)
  255. {
  256. $modeMap = array(
  257. 'ctr' => Base::MODE_CTR,
  258. 'ecb' => Base::MODE_ECB,
  259. 'cbc' => Base::MODE_CBC,
  260. 'cfb' => Base::MODE_CFB8,
  261. 'ncfb'=> Base::MODE_CFB,
  262. 'nofb'=> Base::MODE_OFB,
  263. 'stream' => Base::MODE_STREAM
  264. );
  265. switch (true) {
  266. case !isset($modeMap[$mode]):
  267. case $mode == 'stream' && $algorithm != 'arcfour':
  268. case $algorithm == 'arcfour' && $mode != 'stream':
  269. trigger_error('mcrypt_module_open(): Could not open encryption module', E_USER_WARNING);
  270. return false;
  271. }
  272. switch ($algorithm) {
  273. case 'rijndael-128':
  274. $cipher = new Rijndael($modeMap[$mode]);
  275. $cipher->setBlockLength(128);
  276. break;
  277. case 'twofish':
  278. $cipher = new Twofish($modeMap[$mode]);
  279. break;
  280. case 'rijndael-192':
  281. $cipher = new Rijndael($modeMap[$mode]);
  282. $cipher->setBlockLength(192);
  283. break;
  284. case 'des':
  285. $cipher = new DES($modeMap[$mode]);
  286. break;
  287. case 'rijndael-256':
  288. $cipher = new Rijndael($modeMap[$mode]);
  289. $cipher->setBlockLength(256);
  290. break;
  291. case 'blowfish':
  292. $cipher = new Blowfish($modeMap[$mode]);
  293. break;
  294. case 'rc2':
  295. $cipher = new RC2($modeMap[$mode]);
  296. break;
  297. case'tripledes':
  298. $cipher = new TripleDES($modeMap[$mode]);
  299. break;
  300. case 'arcfour':
  301. $cipher = new RC4();
  302. break;
  303. default:
  304. trigger_error('mcrypt_module_open(): Could not open encryption module', E_USER_WARNING);
  305. return false;
  306. }
  307. $cipher->mcrypt_mode = $mode;
  308. $cipher->disablePadding();
  309. return $cipher;
  310. }
  311. /**
  312. * Returns the maximum supported keysize of the opened mode
  313. *
  314. * Gets the maximum supported key size of the algorithm in bytes.
  315. *
  316. * @param \phpseclib\Crypt\Base $td
  317. * @return int
  318. * @access public
  319. */
  320. function phpseclib_mcrypt_enc_get_key_size(Base $td)
  321. {
  322. // invalid parameters with mcrypt result in warning's. type hinting, as this function is doing,
  323. // produces a catchable fatal error.
  324. $reflection = new \ReflectionClass($td);
  325. switch ($reflection->getShortName()) {
  326. case 'Rijndael':
  327. case 'Twofish':
  328. return 32;
  329. case 'DES':
  330. return 8;
  331. case 'TripleDES':
  332. return 24;
  333. case 'RC4':
  334. return 256;
  335. case 'Blowfish':
  336. return 56;
  337. case 'RC2':
  338. return 128;
  339. }
  340. }
  341. /**
  342. * Gets the name of the specified cipher
  343. *
  344. * @param string $cipher
  345. * @return mixed
  346. * @access public
  347. */
  348. function phpseclib_mcrypt_get_cipher_name($cipher)
  349. {
  350. switch ($cipher) {
  351. case 'rijndael-128':
  352. return 'Rijndael-128';
  353. case 'twofish':
  354. return 'Twofish';
  355. case 'rijndael-192':
  356. return 'Rijndael-192';
  357. case 'des':
  358. return 'DES';
  359. case 'rijndael-256':
  360. return 'Rijndael-256';
  361. case 'blowfish':
  362. return 'Blowfish';
  363. case 'rc2':
  364. return 'RC2';
  365. case 'tripledes':
  366. return '3DES';
  367. case 'arcfour':
  368. return 'RC4';
  369. default:
  370. return false;
  371. }
  372. }
  373. /**
  374. * Gets the block size of the specified cipher
  375. *
  376. * @param string $cipher
  377. * @param string $mode optional
  378. * @return int
  379. * @access public
  380. */
  381. function phpseclib_mcrypt_get_block_size($cipher, $mode = false)
  382. {
  383. if (!$mode) {
  384. $mode = $cipher == 'rc4' ? 'stream' : 'cbc';
  385. }
  386. $td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
  387. if ($td === false) {
  388. trigger_error('mcrypt_get_block_size(): Module initialization failed', E_USER_WARNING);
  389. return false;
  390. }
  391. return phpseclib_mcrypt_enc_get_block_size($td);
  392. }
  393. /**
  394. * Gets the key size of the specified cipher
  395. *
  396. * @param string $cipher
  397. * @param string $mode optional
  398. * @return int
  399. * @access public
  400. */
  401. function phpseclib_mcrypt_get_key_size($cipher, $mode = false)
  402. {
  403. if (!$mode) {
  404. $mode = $cipher == 'rc4' ? 'stream' : 'cbc';
  405. }
  406. $td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
  407. if ($td === false) {
  408. trigger_error('mcrypt_get_key_size(): Module initialization failed', E_USER_WARNING);
  409. return false;
  410. }
  411. return phpseclib_mcrypt_enc_get_key_size($td);
  412. }
  413. /**
  414. * Returns the size of the IV belonging to a specific cipher/mode combination
  415. *
  416. * @param string $cipher
  417. * @param string $mode
  418. * @return int
  419. * @access public
  420. */
  421. function phpseclib_mcrypt_get_iv_size($cipher, $mode)
  422. {
  423. $td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
  424. if ($td === false) {
  425. trigger_error('mcrypt_get_iv_size(): Module initialization failed', E_USER_WARNING);
  426. return false;
  427. }
  428. return phpseclib_mcrypt_enc_get_iv_size($td);
  429. }
  430. /**
  431. * Returns the maximum supported keysize of the opened mode
  432. *
  433. * Gets the maximum supported keysize of the opened mode.
  434. *
  435. * @param string $algorithm
  436. * @param string $lib_dir
  437. * @return int
  438. * @access public
  439. */
  440. function phpseclib_mcrypt_module_get_algo_key_size($algorithm, $lib_dir = '')
  441. {
  442. $mode = $algorithm == 'rc4' ? 'stream' : 'cbc';
  443. $td = @phpseclib_mcrypt_module_open($algorithm, '', $mode, '');
  444. if ($td === false) {
  445. trigger_error('mcrypt_module_get_algo_key_size(): Module initialization failed', E_USER_WARNING);
  446. return false;
  447. }
  448. return phpseclib_mcrypt_enc_get_key_size($td);
  449. }
  450. /**
  451. * Returns the size of the IV of the opened algorithm
  452. *
  453. * This function returns the size of the IV of the algorithm specified by the encryption
  454. * descriptor in bytes. An IV is used in cbc, cfb and ofb modes, and in some algorithms
  455. * in stream mode.
  456. *
  457. * @param \phpseclib\Crypt\Base $td
  458. * @return int
  459. * @access public
  460. */
  461. function phpseclib_mcrypt_enc_get_iv_size(Base $td)
  462. {
  463. return $td->getBlockLength() >> 3;
  464. }
  465. /**
  466. * Returns the blocksize of the opened algorithm
  467. *
  468. * Gets the blocksize of the opened algorithm.
  469. *
  470. * @param \phpseclib\Crypt\Base $td
  471. * @return int
  472. * @access public
  473. */
  474. function phpseclib_mcrypt_enc_get_block_size(Base $td)
  475. {
  476. return $td->getBlockLength() >> 3;
  477. }
  478. /**
  479. * Returns the blocksize of the specified algorithm
  480. *
  481. * Gets the blocksize of the specified algorithm.
  482. *
  483. * @param string $algorithm
  484. * @param string $lib_dir
  485. * @return int
  486. * @access public
  487. */
  488. function phpseclib_mcrypt_module_get_algo_block_size($algorithm, $lib_dir = '')
  489. {
  490. // cbc isn't a valid mode for rc4 but that's ok: -1 will still be returned
  491. $td = @phpseclib_mcrypt_module_open($algorithm, '', 'cbc', '');
  492. if ($td === false) {
  493. return -1;
  494. }
  495. return $td->getBlockLength() >> 3;
  496. }
  497. /**
  498. * Returns the name of the opened algorithm
  499. *
  500. * This function returns the name of the algorithm.
  501. *
  502. * @param \phpseclib\Crypt\Base $td
  503. * @return string|bool
  504. * @access public
  505. */
  506. function phpseclib_mcrypt_enc_get_algorithms_name(Base $td)
  507. {
  508. $reflection = new \ReflectionObject($td);
  509. switch ($reflection->getShortName()) {
  510. case 'Rijndael':
  511. return 'RIJNDAEL-' . $td->getBlockLength();
  512. case 'Twofish':
  513. return 'TWOFISH';
  514. case 'Blowfish':
  515. return 'BLOWFISH'; // what about BLOWFISH-COMPAT?
  516. case 'DES':
  517. return 'DES';
  518. case 'RC2':
  519. return 'RC2';
  520. case 'TripleDES':
  521. return 'TRIPLEDES';
  522. case 'RC4':
  523. return 'ARCFOUR';
  524. }
  525. return false;
  526. }
  527. /**
  528. * Returns the name of the opened mode
  529. *
  530. * This function returns the name of the mode.
  531. *
  532. * @param \phpseclib\Crypt\Base $td
  533. * @return string|bool
  534. * @access public
  535. */
  536. function phpseclib_mcrypt_enc_get_modes_name(Base $td)
  537. {
  538. $modeMap = array(
  539. Base::MODE_CTR => 'CTR',
  540. Base::MODE_ECB => 'ECB',
  541. Base::MODE_CBC => 'CBC',
  542. Base::MODE_CFB => 'nCFB',
  543. Base::MODE_OFB => 'nOFB',
  544. Base::MODE_STREAM => 'STREAM'
  545. );
  546. return isset($modeMap[$td->mode]) ? $modeMap[$td->mode] : false;
  547. }
  548. /**
  549. * Checks whether the encryption of the opened mode works on blocks
  550. *
  551. * Tells whether the algorithm of the opened mode works on blocks (e.g. FALSE for stream, and TRUE for cbc, cfb, ofb)..
  552. *
  553. * @param \phpseclib\Crypt\Base $td
  554. * @return bool
  555. * @access public
  556. */
  557. function phpseclib_mcrypt_enc_is_block_algorithm_mode(Base $td)
  558. {
  559. return $td->mode != Base::MODE_STREAM;
  560. }
  561. /**
  562. * Checks whether the algorithm of the opened mode is a block algorithm
  563. *
  564. * Tells whether the algorithm of the opened mode is a block algorithm.
  565. *
  566. * @param \phpseclib\Crypt\Base $td
  567. * @return bool
  568. * @access public
  569. */
  570. function phpseclib_mcrypt_enc_is_block_algorithm(Base $td)
  571. {
  572. return phpseclib_mcrypt_enc_get_algorithms_name($td) != 'ARCFOUR';
  573. }
  574. /**
  575. * Checks whether the opened mode outputs blocks
  576. *
  577. * Tells whether the opened mode outputs blocks (e.g. TRUE for cbc and ecb, and FALSE for cfb and stream).
  578. *
  579. * @param \phpseclib\Crypt\Base $td
  580. * @return bool
  581. * @access public
  582. */
  583. function phpseclib_mcrypt_enc_is_block_mode(Base $td)
  584. {
  585. return $td->mode == Base::MODE_ECB || $td->mode == Base::MODE_CBC;
  586. }
  587. /**
  588. * Runs a self test on the opened module
  589. *
  590. * This function runs the self test on the algorithm specified by the descriptor td.
  591. *
  592. * @param \phpseclib\Crypt\Base $td
  593. * @return bool
  594. * @access public
  595. */
  596. function phpseclib_mcrypt_enc_self_test(Base $td)
  597. {
  598. return true;
  599. }
  600. /**
  601. * This function initializes all buffers needed for en/decryption.
  602. *
  603. * @param \phpseclib\Crypt\Base $td
  604. * @param string $key
  605. * @param string $iv
  606. * @return int
  607. * @access public
  608. */
  609. function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
  610. {
  611. $iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
  612. if (strlen($iv) != $iv_size && $td->mcrypt_mode != 'ecb') {
  613. trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING);
  614. }
  615. if (!strlen($key)) {
  616. trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING);
  617. return -3;
  618. }
  619. $max_key_size = phpseclib_mcrypt_enc_get_key_size($td);
  620. if (strlen($key) > $max_key_size) {
  621. trigger_error('mcrypt_generic_init(): Key size too large; supplied length: ' . strlen($key) . ', max: ' . $max_key_size, E_USER_WARNING);
  622. }
  623. phpseclib_set_key($td, $key);
  624. phpseclib_set_iv($td, $iv);
  625. $td->enableContinuousBuffer();
  626. $td->mcrypt_polyfill_init = true;
  627. return 0;
  628. }
  629. /**
  630. * Encrypt / decrypt data
  631. *
  632. * Performs checks common to both mcrypt_generic and mdecrypt_generic
  633. *
  634. * @param \phpseclib\Crypt\Base $td
  635. * @param string $data
  636. * @param string $op
  637. * @return string|bool
  638. * @access private
  639. */
  640. function phpseclib_mcrypt_generic_helper(Base $td, &$data, $op)
  641. {
  642. // in the orig mcrypt, if mcrypt_generic_init() was called and an empty key was provided you'd get the following error:
  643. // Warning: mcrypt_generic(): supplied resource is not a valid MCrypt resource
  644. // that error doesn't really make a lot of sense in this context since $td is not a resource nor should it be one.
  645. // in light of that we'll just display the same error that you get when you don't call mcrypt_generic_init() at all
  646. if (!isset($td->mcrypt_polyfill_init)) {
  647. trigger_error('m' . $op . '_generic(): Operation disallowed prior to mcrypt_generic_init().', E_USER_WARNING);
  648. return false;
  649. }
  650. // phpseclib does not currently provide a way to retrieve the mode once it has been set via "public" methods
  651. if (phpseclib_mcrypt_module_is_block_mode($td->mcrypt_mode)) {
  652. $block_length = phpseclib_mcrypt_enc_get_iv_size($td);
  653. $extra = strlen($data) % $block_length;
  654. if ($extra) {
  655. $data.= str_repeat("\0", $block_length - $extra);
  656. }
  657. }
  658. return $op == 'crypt' ? $td->encrypt($data) : $td->decrypt($data);
  659. }
  660. /**
  661. * This function encrypts data
  662. *
  663. * This function encrypts data. The data is padded with "\0" to make sure the length of the data
  664. * is n * blocksize. This function returns the encrypted data. Note that the length of the
  665. * returned string can in fact be longer than the input, due to the padding of the data.
  666. *
  667. * If you want to store the encrypted data in a database make sure to store the entire string as
  668. * returned by mcrypt_generic, or the string will not entirely decrypt properly. If your original
  669. * string is 10 characters long and the block size is 8 (use mcrypt_enc_get_block_size() to
  670. * determine the blocksize), you would need at least 16 characters in your database field. Note
  671. * the string returned by mdecrypt_generic() will be 16 characters as well...use rtrim($str, "\0")
  672. * to remove the padding.
  673. *
  674. * If you are for example storing the data in a MySQL database remember that varchar fields
  675. * automatically have trailing spaces removed during insertion. As encrypted data can end in a
  676. * space (ASCII 32), the data will be damaged by this removal. Store data in a tinyblob/tinytext
  677. * (or larger) field instead.
  678. *
  679. * @param \phpseclib\Crypt\Base $td
  680. * @param string $data
  681. * @return string|bool
  682. * @access public
  683. */
  684. function phpseclib_mcrypt_generic(Base $td, $data)
  685. {
  686. return phpseclib_mcrypt_generic_helper($td, $data, 'crypt');
  687. }
  688. /**
  689. * Decrypts data
  690. *
  691. * This function decrypts data. Note that the length of the returned string can in fact be
  692. * longer than the unencrypted string, due to the padding of the data.
  693. *
  694. * @param \phpseclib\Crypt\Base $td
  695. * @param string $data
  696. * @return string|bool
  697. * @access public
  698. */
  699. function phpseclib_mdecrypt_generic(Base $td, $data)
  700. {
  701. return phpseclib_mcrypt_generic_helper($td, $data, 'decrypt');
  702. }
  703. /**
  704. * This function deinitializes an encryption module
  705. *
  706. * This function terminates encryption specified by the encryption descriptor (td).
  707. * It clears all buffers, but does not close the module. You need to call
  708. * mcrypt_module_close() yourself. (But PHP does this for you at the end of the
  709. * script.)
  710. *
  711. * @param \phpseclib\Crypt\Base $td
  712. * @return bool
  713. * @access public
  714. */
  715. function phpseclib_mcrypt_generic_deinit(Base $td)
  716. {
  717. if (!isset($td->mcrypt_polyfill_init)) {
  718. trigger_error('mcrypt_generic_deinit(): Could not terminate encryption specifier', E_USER_WARNING);
  719. return false;
  720. }
  721. $td->disableContinuousBuffer();
  722. unset($td->mcrypt_polyfill_init);
  723. return true;
  724. }
  725. /**
  726. * Closes the mcrypt module
  727. *
  728. * Closes the specified encryption handle.
  729. *
  730. * @param \phpseclib\Crypt\Base $td
  731. * @return bool
  732. * @access public
  733. */
  734. function phpseclib_mcrypt_module_close(Base $td)
  735. {
  736. //unset($td->mcrypt_polyfill_init);
  737. return true;
  738. }
  739. /**
  740. * Returns an array with the supported keysizes of the opened algorithm
  741. *
  742. * Returns an array with the key sizes supported by the specified algorithm.
  743. * If it returns an empty array then all key sizes between 1 and mcrypt_module_get_algo_key_size()
  744. * are supported by the algorithm.
  745. *
  746. * @param string $algorithm
  747. * @param string $lib_dir optional
  748. * @return array
  749. * @access public
  750. */
  751. function phpseclib_mcrypt_module_get_supported_key_sizes($algorithm, $lib_dir = '')
  752. {
  753. switch ($algorithm) {
  754. case 'rijndael-128':
  755. case 'rijndael-192':
  756. case 'rijndael-256':
  757. case 'twofish':
  758. return array(16, 24, 32);
  759. case 'des':
  760. return array(8);
  761. case 'tripledes':
  762. return array(24);
  763. //case 'arcfour':
  764. //case 'blowfish':
  765. //case 'rc2':
  766. default:
  767. return array();
  768. }
  769. }
  770. /**
  771. * Returns an array with the supported keysizes of the opened algorithm
  772. *
  773. * Gets the supported key sizes of the opened algorithm.
  774. *
  775. * @param \phpseclib\Crypt\Base $td
  776. * @return array
  777. * @access public
  778. */
  779. function phpseclib_mcrypt_enc_get_supported_key_sizes(Base $td)
  780. {
  781. $algorithm = strtolower(phpseclib_mcrypt_enc_get_algorithms_name($td));
  782. return phpseclib_mcrypt_module_get_supported_key_sizes($algorithm);
  783. }
  784. /**
  785. * Returns if the specified module is a block algorithm or not
  786. *
  787. * This function returns TRUE if the mode is for use with block algorithms, otherwise it returns FALSE. (e.g. FALSE for stream, and TRUE for cbc, cfb, ofb).
  788. *
  789. * @param string $mode
  790. * @param string $lib_dir optional
  791. * @return bool
  792. * @access public
  793. */
  794. function phpseclib_mcrypt_module_is_block_algorithm_mode($mode, $lib_dir = '')
  795. {
  796. switch ($mode) {
  797. case 'cbc':
  798. case 'ctr':
  799. case 'ecb':
  800. case 'cfb':
  801. case 'ncfb':
  802. case 'nofb':
  803. return true;
  804. }
  805. return false;
  806. }
  807. /**
  808. * This function checks whether the specified algorithm is a block algorithm
  809. *
  810. * This function returns TRUE if the specified algorithm is a block algorithm, or FALSE if it is a stream one.
  811. *
  812. * @param string $mode
  813. * @param string $lib_dir optional
  814. * @return bool
  815. * @access public
  816. */
  817. function phpseclib_mcrypt_module_is_block_algorithm($algorithm, $lib_dir = '')
  818. {
  819. switch ($algorithm) {
  820. case 'rijndael-128':
  821. case 'twofish':
  822. case 'rijndael-192':
  823. case 'des':
  824. case 'rijndael-256':
  825. case 'blowfish':
  826. case 'rc2':
  827. case 'tripledes':
  828. return true;
  829. }
  830. return false;
  831. }
  832. /**
  833. * Returns if the specified mode outputs blocks or not
  834. *
  835. * This function returns TRUE if the mode outputs blocks of bytes or FALSE if it outputs just bytes. (e.g. TRUE for cbc and ecb, and FALSE for cfb and stream).
  836. *
  837. * @param string $mode
  838. * @param string $lib_dir optional
  839. * @return bool
  840. * @access public
  841. */
  842. function phpseclib_mcrypt_module_is_block_mode($mode, $lib_dir = '')
  843. {
  844. switch ($mode) {
  845. case 'cbc':
  846. case 'ecb':
  847. return true;
  848. }
  849. return false;
  850. }
  851. /**
  852. * Returns if the specified mode can use an IV or not
  853. *
  854. * @param string $mode
  855. * @return bool
  856. * @access private
  857. */
  858. function phpseclib_mcrypt_module_is_iv_mode($mode)
  859. {
  860. switch ($mode) {
  861. case 'ecb':
  862. case 'stream':
  863. return false;
  864. }
  865. return true;
  866. }
  867. /**
  868. * This function runs a self test on the specified module
  869. *
  870. * This function runs the self test on the algorithm specified.
  871. *
  872. * @param string $mode
  873. * @param string $lib_dir optional
  874. * @return bool
  875. * @access public
  876. */
  877. function phpseclib_mcrypt_module_self_test($algorithm, $lib_dir = '')
  878. {
  879. return in_array($algorithm, phpseclib_mcrypt_list_algorithms());
  880. }
  881. /**
  882. * Encrypt / decrypt data
  883. *
  884. * Performs checks common to both mcrypt_encrypt and mcrypt_decrypt
  885. *
  886. * @param string $cipher
  887. * @param string $key
  888. * @param string $data
  889. * @param string $mode
  890. * @param string $iv
  891. * @param string $op
  892. * @return string|bool
  893. * @access private
  894. */
  895. function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
  896. {
  897. // PHP 5.6 made mcrypt_encrypt() a lot less tolerant of bad input but it neglected to change
  898. // anything about mcrypt_generic(). and despite the changes insufficiently long plaintext
  899. // is still accepted.
  900. $keyLen = strlen($key);
  901. $sizes = phpseclib_mcrypt_module_get_supported_key_sizes($cipher);
  902. if (count($sizes) && !in_array($keyLen, $sizes)) {
  903. trigger_error(
  904. 'mcrypt_' . $op . '(): Key of size ' . $keyLen . ' not supported by this algorithm. Only keys of sizes ' .
  905. preg_replace('#, (\d+)$#', ' or $1', implode(', ', $sizes)) . ' supported',
  906. E_USER_WARNING
  907. );
  908. return false;
  909. }
  910. $td = @phpseclib_mcrypt_module_open($cipher, '', $mode, '');
  911. if ($td === false) {
  912. trigger_error('mcrypt_encrypt(): Module initialization failed', E_USER_WARNING);
  913. return false;
  914. }
  915. $maxKeySize = phpseclib_mcrypt_enc_get_key_size($td);
  916. if (!count($sizes) && $keyLen > $maxKeySize) {
  917. trigger_error(
  918. 'mcrypt_' . $op . '(): Key of size ' . $keyLen . ' not supported by this algorithm. Only keys of size 1 to ' . $maxKeySize . ' supported',
  919. E_USER_WARNING
  920. );
  921. return false;
  922. }
  923. if (phpseclib_mcrypt_module_is_iv_mode($mode)) {
  924. $iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
  925. if (!isset($iv) && $iv_size) {
  926. trigger_error(
  927. 'mcrypt_' . $op . '(): Encryption mode requires an initialization vector of size ' . $iv_size,
  928. E_USER_WARNING
  929. );
  930. return false;
  931. }
  932. if (strlen($iv) != $iv_size) {
  933. trigger_error(
  934. 'mcrypt_' . $op . '(): Received initialization vector of size ' . strlen($iv) . ', but size ' . $iv_size . ' is required for this encryption mode',
  935. E_USER_WARNING
  936. );
  937. return false;
  938. }
  939. } else {
  940. $iv = null;
  941. }
  942. phpseclib_mcrypt_generic_init($td, $key, $iv);
  943. return $op == 'encrypt' ? phpseclib_mcrypt_generic($td, $data) : phpseclib_mdecrypt_generic($td, $data);
  944. }
  945. /**
  946. * Encrypts plaintext with given parameters
  947. *
  948. * Encrypts the data and returns it.
  949. *
  950. * @param string $cipher
  951. * @param string $key
  952. * @param string $data
  953. * @param string $mode
  954. * @param string $iv optional
  955. * @return string|bool
  956. * @access public
  957. */
  958. function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
  959. {
  960. return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt');
  961. }
  962. /**
  963. * Decrypts crypttext with given parameters
  964. *
  965. * Decrypts the data and returns the unencrypted data.
  966. *
  967. * @param string $cipher
  968. * @param string $key
  969. * @param string $data
  970. * @param string $mode
  971. * @param string $iv optional
  972. * @return string|bool
  973. * @access public
  974. */
  975. function phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null)
  976. {
  977. return phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt');
  978. }
  979. /**
  980. * mcrypt_compat stream filter
  981. *
  982. * @author Jim Wigginton <terrafrost@php.net>
  983. * @access public
  984. */
  985. class phpseclib_mcrypt_filter extends php_user_filter
  986. {
  987. /**
  988. * The Cipher Object
  989. *
  990. * @var object
  991. * @access private
  992. */
  993. private $cipher;
  994. /**
  995. * To encrypt or decrypt
  996. *
  997. * @var boolean
  998. * @access private
  999. */
  1000. private $op;
  1001. /**
  1002. * Buffer for ECB / CBC
  1003. *
  1004. * @var string
  1005. * @access private
  1006. */
  1007. private $buffer = '';
  1008. /**
  1009. * Cipher block length
  1010. *
  1011. * @var int
  1012. * @access private
  1013. */
  1014. private $block_length;
  1015. /**
  1016. * Cipher block mode
  1017. *
  1018. * @var bool
  1019. * @access private
  1020. */
  1021. private $block_mode;
  1022. /**
  1023. * Buffer handle
  1024. *
  1025. * @var resource
  1026. * @access private
  1027. */
  1028. private $bh;
  1029. /**
  1030. * Called when applying the filter
  1031. *
  1032. * This method is called whenever data is read from or written to the attached stream
  1033. * (such as with fread() or fwrite()).
  1034. *
  1035. * @param resource $in
  1036. * @param resource $out
  1037. * @param int $consumed
  1038. * @param bool $closing
  1039. * @link http://php.net/manual/en/php-user-filter.filter.php
  1040. * @return int
  1041. * @access public
  1042. */
  1043. public function filter($in, $out, &$consumed, $closing)
  1044. {
  1045. $newlen = 0;
  1046. while ($bucket = stream_bucket_make_writeable($in)) {
  1047. if ($this->block_mode) {
  1048. $bucket->data = $this->buffer . $bucket->data;
  1049. $extra = strlen($bucket->data) % $this->block_length;
  1050. if ($extra) {
  1051. $this->buffer = substr($bucket->data, -$extra);
  1052. $bucket->data = substr($bucket->data, 0, -$extra);
  1053. }
  1054. if (!strlen($bucket->data)) {
  1055. continue;
  1056. }
  1057. }
  1058. $bucket->data = $this->op ?
  1059. $this->cipher->encrypt($bucket->data) :
  1060. $this->cipher->decrypt($bucket->data);
  1061. $newlen+= strlen($bucket->data);
  1062. $consumed+= $bucket->datalen;
  1063. stream_bucket_append($out, $bucket);
  1064. }
  1065. if ($closing && strlen($this->buffer)) {
  1066. $temp = $this->buffer . str_repeat("\0", $this->block_length - strlen($this->buffer));
  1067. $data = $this->op ?
  1068. $this->cipher->encrypt($temp) :
  1069. $this->cipher->decrypt($temp);
  1070. $newlen+= strlen($data);
  1071. $bucket = stream_bucket_new($this->bh, $data);
  1072. $this->buffer = '';
  1073. $newlen = 0;
  1074. stream_bucket_append($out, $bucket);
  1075. }
  1076. return $this->block_mode && $newlen && $newlen < $this->block_length ? PSFS_FEED_ME : PSFS_PASS_ON;
  1077. }
  1078. /**
  1079. * Called when creating the filter
  1080. *
  1081. * This method is called during instantiation of the filter class object.
  1082. * If your filter allocates or initializes any other resources (such as a buffer),
  1083. * this is the place to do it.
  1084. *
  1085. * @link http://php.net/manual/en/php-user-filter.oncreate.php
  1086. * @return bool
  1087. * @access public
  1088. */
  1089. public function onCreate()
  1090. {
  1091. if (!isset($this->params) || !is_array($this->params)) {
  1092. trigger_error('stream_filter_append(): Filter parameters for ' . $this->filtername . ' must be an array');
  1093. return false;
  1094. }
  1095. if (!isset($this->params['iv']) || !is_string($this->params['iv'])) {
  1096. trigger_error('stream_filter_append(): Filter parameter[iv] not provided or not of type: string');
  1097. return false;
  1098. }
  1099. if (!isset($this->params['key']) || !is_string($this->params['key'])) {
  1100. trigger_error('stream_filter_append(): key not specified or is not a string');
  1101. return false;
  1102. }
  1103. $filtername = substr($this->filtername, 0, 10) == 'phpseclib.' ?
  1104. substr($this->filtername, 10) :
  1105. $this->filtername;
  1106. $parts = explode('.', $filtername);
  1107. if (count($parts) != 2) {
  1108. trigger_error('stream_filter_append(): Could not open encryption module');
  1109. return false;
  1110. }
  1111. switch ($parts[0]) {
  1112. case 'mcrypt':
  1113. case 'mdecrypt':
  1114. break;
  1115. default:
  1116. trigger_error('stream_filter_append(): Could not open encryption module');
  1117. return false;
  1118. }
  1119. $mode = isset($this->params['mode']) ? $this->params['mode'] : 'cbc';
  1120. $cipher = @phpseclib_mcrypt_module_open($parts[1], '', $mode, '');
  1121. if ($cipher === false) {
  1122. trigger_error('stream_filter_append(): Could not open encryption module');
  1123. return false;
  1124. }
  1125. $cipher->enableContinuousBuffer();
  1126. phpseclib_set_key($cipher, $this->params['key']);
  1127. phpseclib_set_iv($cipher, $this->params['iv']);
  1128. $this->op = $parts[0] == 'mcrypt';
  1129. $this->cipher = $cipher;
  1130. $this->block_length = phpseclib_mcrypt_enc_get_iv_size($cipher);
  1131. $this->block_mode = phpseclib_mcrypt_module_is_block_mode($mode);
  1132. if ($this->block_mode) {
  1133. $this->bh = fopen('php://memory', 'w+');
  1134. }
  1135. return true;
  1136. }
  1137. /**
  1138. * Called when closing the filter
  1139. *
  1140. * This method is called upon filter shutdown (typically, this is also during stream shutdown), and is
  1141. * executed after the flush method is called. If any resources were allocated or initialized during
  1142. * onCreate() this would be the time to destroy or dispose of them.
  1143. *
  1144. * @link http://php.net/manual/en/php-user-filter.onclose.php
  1145. * @access public
  1146. */
  1147. public function onClose()
  1148. {
  1149. if ($this->bh) {
  1150. fclose($this->bh);
  1151. }
  1152. }
  1153. }
  1154. stream_filter_register('phpseclib.mcrypt.*', 'phpseclib_mcrypt_filter');
  1155. stream_filter_register('phpseclib.mdecrypt.*', 'phpseclib_mcrypt_filter');
  1156. }
  1157. // define
  1158. if (!function_exists('mcrypt_list_algorithms')) {
  1159. function mcrypt_list_algorithms($lib_dir = '')
  1160. {
  1161. return phpseclib_mcrypt_list_algorithms($lib_dir);
  1162. }
  1163. function mcrypt_list_modes($lib_dir = '')
  1164. {
  1165. return phpseclib_mcrypt_list_modes($lib_dir);
  1166. }
  1167. function mcrypt_create_iv($size, $source = MCRYPT_DEV_URANDOM)
  1168. {
  1169. return phpseclib_mcrypt_create_iv($size, $source);
  1170. }
  1171. function mcrypt_module_open($algorithm, $algorithm_directory, $mode, $mode_directory)
  1172. {
  1173. return phpseclib_mcrypt_module_open($algorithm, $algorithm_directory, $mode, $mode_directory);
  1174. }
  1175. function mcrypt_enc_get_key_size(Base $td)
  1176. {
  1177. return phpseclib_mcrypt_enc_get_key_size($td);
  1178. }
  1179. function mcrypt_enc_get_iv_size(Base $td)
  1180. {
  1181. return phpseclib_mcrypt_enc_get_iv_size($td);
  1182. }
  1183. function mcrypt_enc_get_block_size(Base $td)
  1184. {
  1185. return phpseclib_mcrypt_enc_get_block_size($td);
  1186. }
  1187. function mcrypt_generic_init(Base $td, $key, $iv)
  1188. {
  1189. return phpseclib_mcrypt_generic_init($td, $key, $iv);
  1190. }
  1191. function mcrypt_generic(Base $td, $data)
  1192. {
  1193. return phpseclib_mcrypt_generic($td, $data);
  1194. }
  1195. function mcrypt_generic_deinit(Base $td)
  1196. {
  1197. return phpseclib_mcrypt_generic_deinit($td);
  1198. }
  1199. function mcrypt_module_close(Base $td)
  1200. {
  1201. return phpseclib_mcrypt_module_close($td);
  1202. }
  1203. function mdecrypt_generic(Base $td, $data)
  1204. {
  1205. return phpseclib_mdecrypt_generic($td, $data);
  1206. }
  1207. function mcrypt_enc_get_algorithms_name(Base $td)
  1208. {
  1209. return phpseclib_mcrypt_enc_get_algorithms_name($td);
  1210. }
  1211. function mcrypt_enc_get_modes_name(Base $td)
  1212. {
  1213. return phpseclib_mcrypt_enc_get_modes_name($td);
  1214. }
  1215. function mcrypt_enc_is_block_algorithm_mode(Base $td)
  1216. {
  1217. return phpseclib_mcrypt_enc_is_block_algorithm_mode($td);
  1218. }
  1219. function mcrypt_enc_is_block_algorithm(Base $td)
  1220. {
  1221. return phpseclib_mcrypt_enc_is_block_algorithm($td);
  1222. }
  1223. function mcrypt_enc_self_test(Base $td)
  1224. {
  1225. return phpseclib_mcrypt_enc_self_test($td);
  1226. }
  1227. function mcrypt_module_get_supported_key_sizes($algorithm, $lib_dir = '')
  1228. {
  1229. return phpseclib_mcrypt_module_get_supported_key_sizes($algorithm, $lib_dir);
  1230. }
  1231. function mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
  1232. {
  1233. return phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv);
  1234. }
  1235. function mcrypt_module_get_algo_block_size($algorithm, $lib_dir = '')
  1236. {
  1237. return phpseclib_mcrypt_module_get_algo_block_size($algorithm, $lib_dir);
  1238. }
  1239. function mcrypt_get_block_size($cipher, $mode = '')
  1240. {
  1241. return phpseclib_mcrypt_get_block_size($cipher, $mode);
  1242. }
  1243. function mcrypt_get_cipher_name($cipher)
  1244. {
  1245. return phpseclib_mcrypt_get_cipher_name($cipher);
  1246. }
  1247. function mcrypt_get_key_size($cipher, $mode = false)
  1248. {
  1249. return phpseclib_mcrypt_get_key_size($cipher, $mode);
  1250. }
  1251. function mcrypt_get_iv_size($cipher, $mode)
  1252. {
  1253. return phpseclib_mcrypt_get_iv_size($cipher, $mode);
  1254. }
  1255. function mcrypt_module_get_algo_key_size($algorithm, $lib_dir = '')
  1256. {
  1257. return phpseclib_mcrypt_module_get_algo_key_size($algorithm, $lib_dir);
  1258. }
  1259. function mcrypt_enc_get_supported_key_sizes(Base $td)
  1260. {
  1261. return phpseclib_mcrypt_enc_get_supported_key_sizes($td);
  1262. }
  1263. function mcrypt_module_is_block_algorithm_mode($mode, $lib_dir = '')
  1264. {
  1265. return phpseclib_mcrypt_module_is_block_algorithm_mode($mode, $lib_dir);
  1266. }
  1267. function mcrypt_module_is_block_algorithm($algorithm, $lib_dir= '')
  1268. {
  1269. return phpseclib_mcrypt_module_is_block_algorithm($algorithm, $lib_dir);
  1270. }
  1271. function mcrypt_module_is_block_mode($mode, $lib_dir = '')
  1272. {
  1273. return phpseclib_mcrypt_module_is_block_mode($mode, $lib_dir);
  1274. }
  1275. function mcrypt_module_self_test($algorithm, $lib_dir = '')
  1276. {
  1277. return phpseclib_mcrypt_module_self_test($algorithm, $lib_dir);
  1278. }
  1279. function mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null)
  1280. {
  1281. return phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv);
  1282. }
  1283. //if (!in_array('mcrypt.*', stream_get_filters()) {
  1284. stream_filter_register('mcrypt.*', 'phpseclib_mcrypt_filter');
  1285. stream_filter_register('mdecrypt.*', 'phpseclib_mcrypt_filter');
  1286. //}
  1287. }