Code Monkey home page Code Monkey logo

mcrypt_compat's Introduction

phpseclib - PHP Secure Communications Library

CI Status

Supporting phpseclib

Introduction

MIT-licensed pure-PHP implementations of the following:

SSH-2, SFTP, X.509, an arbitrary-precision integer arithmetic library, Ed25519 / Ed449 / Curve25519 / Curve449, ECDSA / ECDH (with support for 66 curves), RSA (PKCS#1 v2.2 compliant), DSA / DH, DES / 3DES / RC4 / Rijndael / AES / Blowfish / Twofish / Salsa20 / ChaCha20, GCM / Poly1305

Documentation

Branches

master

  • Development Branch
  • Unstable API
  • Do not use in production

3.0

  • Long term support (LTS) release
  • Major expansion of cryptographic primitives
  • Minimum PHP version: 5.6.1
  • PSR-4 autoloading with namespace rooted at \phpseclib3
  • Install via Composer: composer require phpseclib/phpseclib:~3.0

2.0

  • Long term support (LTS) release
  • Modernized version of 1.0
  • Minimum PHP version: 5.3.3
  • PSR-4 autoloading with namespace rooted at \phpseclib
  • Install via Composer: composer require phpseclib/phpseclib:~2.0

1.0

  • Long term support (LTS) release
  • PHP4 compatible
  • Composer compatible (PSR-0 autoloading)
  • Install using Composer: composer require phpseclib/phpseclib:~1.0
  • Download 1.0.23 as ZIP

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Support

Need Support?

Special Thanks

Special Thanks to our $50+ sponsors!:

Contributing

  1. Fork the Project

  2. Ensure you have Composer installed (see Composer Download Instructions)

  3. Install Development Dependencies

    composer install
  4. Create a Feature Branch

  5. Run continuous integration checks:

    composer run-script all-quality-tools
  6. Send us a Pull Request

mcrypt_compat's People

Contributors

drpayyne avatar kingmar avatar peter279k avatar terrafrost avatar theofidry avatar umitakkaya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mcrypt_compat's Issues

Dependency on phpseclib/phpseclib:dev-master?

Hi, thanks for this project. Due to these security advisories:

... we can't use v2.0.4 because it has a dependency on "phpseclib/phpseclib": ">=3.0.13 <4.0.0" (it needs to be "phpseclib/phpseclib": ">=3.0.34 <4.0.0".

However, this project's master has a dependency on phpseclib/phpseclib:dev-master. And phpseclib/phpseclib's README.MD says:

master

Development Branch
Unstable API
Do not use in production

... so I can't use that either. Is there a recommendation what to do in this situation?

Unexplained incompatibility

I've been trying to replace some legacy mcrypt functions with openssl equivalents, but even though the cipher and mode (MCRYPT_TRIPLEDES and MCRYPT_MODE_ECB) are apparently supported, I haven't been able to get it to work. So, I tried installing your shim/polyfill instead, but this also doesn't seem to work for this particular use case.

Here is some code to reproduce the problem. When run under PHP 5.6.30 with mcrypt enabled, it works fine, but if I disable mcrypt and use your polyfill it doesn't.

$string = 'Password:abc123';
$expected = base64_decode('UEFjUYBUmsVnm7WU1fBfGQ==');

$td = mcrypt_module_open(MCRYPT_TRIPLEDES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, '123456789abcdefg', $iv);
$result = mcrypt_generic($td, $string);
mcrypt_generic_deinit($td);

echo $result == $expected ? "PASS\n" : "FAIL\n";

Fatal Error in Version 1.07

In version 1.07, there is a typo in the source code that produces a fatal error: Undefined class constant 'MODE_CFB8' in vendor/phpseclib/mcrypt_compat/lib/mcrypt.php on line 275.

php 8.1 compatibility issues

Hi, we noticed a compatibility issue with 8.1

Return type of phpseclib_mcrypt_filter::filter($in, $out, &$consumed, $closing) should either be compatible with php_user_filter::filter($in, $out, &$consumed, bool $closing): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Return type of phpseclib_mcrypt_filter::onCreate() should either be compatible with php_user_filter::onCreate(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Serpent Support

// MCRYPT_SERPENT_128?
// MCRYPT_SERPENT_192?
// MCRYPT_SERPENT_256?

If I were to write a PHP implementation of Serpent, would there be any interest in using it to complete the Serpent polyfill?

stream_filter_append output is different than original mcrypt when writing

Hi,

I ran into some trouble on both PHP 5.6 and PHP 7.2 while trying to use the mcrypt_compat filters for stream_filter_append.

When writing, the end of encrypted data is missing from the file and then of course from the decrypted data as well.
Meanwhile reading a file encrypted with original mcrypt work correctly.

Here is an example:

<?php
require_once __DIR__ . '/vendor/autoload.php';

$data = "Hello, this a sample file content to be encrypted and decrypted.... END of file.";
$key = "ae6fa3da6ae39b05ce17e69d5e18c236a7341c80592626d81d7c70013b7d436d";
$iv = "5dc7a17ebe32b6e62f0b5f9519d57afb";
$opts = array('iv' => $iv, 'key' => $key, 'mode' => 'cbc');

$tmpPath = __DIR__ . '/tmp/';
if(!is_dir($tmpPath)) mkdir($tmpPath);
$srcFile = $tmpPath . 'data.txt';
$encryptedFile = $tmpPath . 'encrypted';
$decryptedFile = $tmpPath . 'decrypted.txt';

// Store data file
file_put_contents($srcFile, $data);

// encrypt the file
$in_enc = fopen($srcFile, 'rb');
$out_enc = fopen($encryptedFile, 'wb');
stream_filter_append($out_enc, 'convert.base64-encode');
stream_filter_append($out_enc, 'phpseclib.mcrypt.rijndael-256', STREAM_FILTER_WRITE, $opts);
while (!feof($in_enc)) {
    fwrite($out_enc, fread($in_enc, 1024));
}
fclose($in_enc);
fclose($out_enc);

// decrypt the file
$in_dec = fopen($encryptedFile, 'rb');
stream_filter_append($in_dec, 'phpseclib.mdecrypt.rijndael-256', STREAM_FILTER_READ, $opts);
stream_filter_append($in_dec, 'convert.base64-decode');
$out_dec = fopen($decryptedFile, 'wb');
while (!feof($in_dec)) {
    fwrite($out_dec, fread($in_dec, 1024));
}
fclose($in_dec);
fclose($out_dec);

// retrieve the decrypted file content
$out = "";
$fp = fopen($decryptedFile, 'r');
while (!feof($fp)) {
    $out .= fread($fp, 1024);
}
fclose($fp);

echo "Expected: \"$data\"" . PHP_EOL;
echo "Output  : \"$out\"" . PHP_EOL;

unlink($srcFile);
unlink($encryptedFile);
unlink($decryptedFile);

Output with rijndael-256 cipher:

Expected: "Hello, this a sample file content to be encrypted and decrypted.... END of file."
Output : "Hello, this a sample file content to be encrypted and decrypted.... END "

Output with tripledes cipher:

Expected: "Hello, this a sample file content to be encrypted and decrypted.... END of file."
Output : "Hello, this a sample file content to be encrypted and decrypted.... END of fil"

Output with shorter data string:

Expected: "Hello, this a sample file content.... END of file."
Output : "Hello, this a sample file content.... END of fil"

Changing the length of the data string or using another cipher doesn't seem to make the issue go away except if you accidentally match the length of the output (I think that's why it doesn't show up in the project unit tests).

If you replace the filtername to use mcrypt the same example seem to work properly. And if you compare both file it's really like a chunk is missing.

Edit: Tried to improve the example
After doing some additional search on this last night it I tried to add the "mode" option, look like it only happen in cbc mode.
I don't have enough knowledge to figure out if it's a "padding" issue but this code certainly doesn't seem to work like the regular mcrypt.

test-related questions, understand whether you need to change the test in the testMcryptGenericMode

Well I noticed that in the tests has a "Risky", and I went to look at the code:

    /**
     * @dataProvider mcryptBlockModuleNameProvider
     */
    public function testMcryptGenericMode($modeName, $validMode)
    {
        if (!$validMode) {
            return;
        }
        $key = str_repeat('a', 16);
        $iv = str_repeat('b', 16);
        $plaintext = str_repeat('c', 16);
        $mcrypt = mcrypt_encrypt('rijndael-128', $key, $plaintext, $modeName, $iv);
        $compat = phpseclib_mcrypt_encrypt('rijndael-128', $key, $plaintext, $modeName, $iv);
        $this->assertEquals(bin2hex($mcrypt), bin2hex($compat));
    }

And you wanted to know if it's for you this way, or do you accept that you make changes to this method?

I ran the tests on my machine, and this was the displayed log of script execution:

$ ./vendor/bin/phpunit
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.

.....................S.S.S.....................................  63 / 132 ( 47%)
.............SSS.S.SSS.SSS.SSS.S.S.S.S.S.S.S.S.S.S.S.S.S...SS.. 126 / 132 ( 95%)
.....R                                                          132 / 132 (100%)

Time: 15.28 minutes, Memory: 6.00MB

There was 1 risky test:

1) MCryptCompatTest::testMcryptGenericMode with data set #6 ('invalid-mode', false)
This test did not perform any assertions

OK, but incomplete, skipped, or risky tests!
Tests: 132, Assertions: 186, Skipped: 31, Risky: 1.

THANKYOU!!!!!!

Not sure where else to put this. No issue. Just wanted to say THANK YOU SO MUCH for this... you literally saved my ass. Upgraded the server to php7.4 and found out weeks later that one part of one site that used mcrypt had been broken. This library saved me hours or pain porting the code my self. Is there a link where I can send yall a beer or something?

Module initialization failed... PHP7.2.1

Got error from this code:

public static function cryptToCodeCompact($data)
{
    $iv_size    = phpseclib_mcrypt_get_iv_size("cast-128", "ecb");
    var_dump("cryptToCode:iv_size:".$iv_size);

    $iv         = phpseclib_mcrypt_create_iv($iv_size, MCRYPT_RAND );
    var_dump("cryptToCode:iv:".$iv);

    $code       = phpseclib_mcrypt_encrypt("cast-128", self::KEY_TRANSLATE_ID, $data, "ecb", $iv);
    var_dump("cryptToCode:code:".$code);

    $code = bin2hex($code);

    return $code;
}



Warning: mcrypt_get_iv_size(): Module initialization failed in C:\phpStormProjects\gamezhero.com\vendor\phpseclib\mcrypt_compat\lib\mcrypt.php on line 449
string(20) "cryptToCode:iv_size:"
Warning: mcrypt_create_iv(): Cannot create an IV with a size of less than 1 or greater than 2147483647 in C:\phpStormProjects\gamezhero.com\vendor\phpseclib\mcrypt_compat\lib\mcrypt.php on line 249
string(15) "cryptToCode:iv:"
Warning: mcrypt_encrypt(): Module initialization failed in C:\phpStormProjects\gamezhero.com\vendor\phpseclib\mcrypt_compat\lib\mcrypt.php on line 967
string(17) "cryptToCode:code:" string(0) ""

Oops, sorry, cast-128 is not supported

Thank you thank you thank you

Hoping that google catches when I say "you don't need to install mcrypt extension in php 8 you can use the excellent mcrypt polyfill mcrypt_compat"

Lifesaver. Great work team!

RIJNDAEL-128 ECB Mode doesn't use IV but this polyfill requires a 16-bit IV

Using original mcrypt extension, this works:

>>> $key = str_repeat('a', 32);
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> $data = 'Hello world! ';
=> "Hello world! "
>>> $encrypted = @mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);
PHP Deprecated:  Function mcrypt_encrypt() is deprecated on line 1
=> (binary data)
>>> echo bin2hex($encrypted) . PHP_EOL;
15095a3abbc6ecff1bed490d1396a0c7

However when running the code with this polyfill (after removing mcrypt extension) a warning is raised and false is returned:

PHP Warning:  mcrypt_encrypt(): Received initialization vector of size  0, but size 16 is required for this encryption mode in /Users/username/tmp/mcrypt_compat/lib/mcrypt.php on line 970

Here's my test script:

<?php

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/lib/mcrypt.php';

$key = str_repeat('a', 32);
$data = 'Hello world! ฦ’';
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB, '');
echo bin2hex($encrypted) . PHP_EOL;

I've tried using '' as well as null for the IV and got similar result.

Can we also have old PHP behavior?

I know it's a long shot, but doesn't hurt to ask. Can we have multiple versions of this package so that we can have PHP 5 behavior?

For example, in PHP 5, one could pass an incorrect key size to mcrypt_encrypt and it triggered a warning. In PHP 7, it triggered an error, which is what this package does too. This makes patching legacy a much bigger project than it needs to be. Thanks!

And before anyone schools me, yes, I do want to refactor the code to not do dumb things like that. However, the codebase is huge and it's urgent to get it off PHP 5.3 as a first step.

P.S.: This package rocks and saved me headaches on quite a few legacy upgrades.

[mcrypt_encrypt] PHP5.4 Despite producing warning error, mcrypt still produce a result for mcrypt_encrypt, phpseclib_mcrypt_encrypt does not.

Despite producing warning error, mcrypt still produce a result for mcrypt_encrypt on php5.4, phpseclib_mcrypt_encrypt does not.

In short the following code does not produce the same result :

<?php
$input = ""; // empty string 
$iv = str_repeat('a', mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
$key = "someKeyThatDoesNot"; // empty 
mcrypt_encrypt(MCRYPT_3DES, $key, $input, MCRYPT_MODE_CBC, $iv);
phpseclib_mcrypt_encrypt(MCRYPT_3DES, $key, $input, MCRYPT_MODE_CBC, $iv);

Results here : http://sandbox.onlinephpfunctions.com/code/931a527873bfcaca5a1837993cb180c7b6843716

Use the actual PHP test suite for validating mcrypt_compat behavior

I started writing a polyfill a couple years ago, and while I didn't make much progress on the actual implementation of the mcrypt functions, the test suite was pretty robust. Maybe you'd like to take some inspiration from it: https://github.com/cweagans/mcrypt-polyfill/tree/d261945011d06273fa3221e68cb2b7e75a40b3ee

Travis CI's PHP has mcrypt compiled in (rather than loaded as an extension - bug report here: travis-ci/travis-ci#4701), so I chose to run everything in Docker containers. I'm happy to provide details if it's something that you'd be interested in. The basic idea is that all of the tests from PHP itself that were used to validate ext-mcrypt can also be run against a polyfill, which would guarantee full compatibility.

Uncaught Error: Class 'phpseclib3\Crypt\Blowfish' not found in lib/mcryptcompat/mcrypt.php:307

I can see use phpseclib3\Crypt\Blowfish; in lib/mcryptcompat/mcrypt.php

and I can find the missing class in the file lib/phpseclib/Crypt/Blowfish.php

so my question is: what am I missing here to get this error?

Help appreciated ;)

PHP Fatal error: Uncaught Error: Class 'phpseclib3\Crypt\Blowfish' not found in lib/mcryptcompat/mcrypt.php:307
Stack trace:
#0 lib/mcryptcompat/mcrypt.php(1251): phpseclib_mcrypt_module_open('blowfish', '', 'ecb', '')
#1 lib/Varien/Crypt/Mcrypt.php(77): mcrypt_module_open('blowfish', '', 'ecb', '')
#2 app/code/core/Mage/Core/Model/Encryption.php(174): Varien_Crypt_Mcrypt->init('0d5a5169c64edaa...')
#3 app/code/core/Mage/Core/Model/Encryption.php(198): Mage_Core_Model_Encryption->_getCrypt()
#4 app/code/core/Mage/Core/Helper/Data.php(242): Mage_Core_Model_Encryption->decrypt('FltGPeVKbgKAPZo...')
#5 app/code/community/Ebizmarts/MailChimp/Helper/Data.php(378): Mage_Core_Helper_Data->decrypt('FltGPeVKbgKAPZo...')
#6 app/code/community/Ebizmarts/MailChimp/Helper/Data.ph in lib/mcryptcompat/mcrypt.php on line 307

image

image

create_function deprecated in PHP 7.2

Deprecated functionality: Function create_function() is deprecated in /var/www/vendor/phpseclib/phpseclib/phpseclib/Crypt/Base.php on line 2495

We're trying to use this library to move to 7.2 but the code uses a deprecated method and throws a warning.

PHP 8.1 deprecation warnings

Hello, when using this package I'm seeing the following deprecation warning:

8192: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in vendor/phpseclib/mcrypt_compat/lib/mcrypt.php line 650

I've seen that similar issues have been resolved on the underlying library (referenced here) and was just wondering if/when they would be resolved for this additional package.

Thanks.

Enhance project description

as php 7.2 removed mcrypt support entirely it would be good to see if this lib is a drop-in replacement to get old code run again. i first only read the github project description and thought this lib is only for backporting mcrypt to php5.

github desc:

PHP 5.x polyfill for mcrypt extension.

readme:

PHP 5.x/7.x polyfill for mcrypt extension.

packagist:

PHP 7.1 polyfill for the mcrypt extension from PHP <= 7.0

RC2 max key size incompatibility

There is a compatibility issue with the maximum key size of RC2 cipher.

mcrypt says key size must be between 1 and 128 but in the polyfill it's 56, also if key size is out of bounds, it's being truncated silently.

OFB mode - Uncaught TypeError: Argument 1 passed to mcrypt_enc_get_iv_size() must be an instance of phpseclib3\Crypt\Common\SymmetricKey, bool given

This code produces an error with mcrypt_compat, but works with PHP mcrypt:


$crypt = mcrypt_module_open('rijndael-256', '', 'ofb', '');
$iv_size = mcrypt_enc_get_iv_size($crypt);
$ks = mcrypt_enc_get_key_size($crypt);

mcrypt_generic_init($crypt, $key, $iv);
$decrypted = trim(mdecrypt_generic($crypt, $encrypted));

print('DECRYPT=' . $decrypted);

The error produced is:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to mcrypt_enc_get_iv_size() must be an instance of phpseclib3\Crypt\Common\SymmetricKey, bool given, called in /var/www/docker/sandbox/phpseclib2/test.php on line 48 and defined in /var/www/docker/sandbox/phpseclib2/vendor/phpseclib/mcrypt_compat/lib/mcrypt.php:1264
Stack trace:
#0 /var/www/docker/sandbox/phpseclib2/test.php(48): mcrypt_enc_get_iv_size(false)
#1 {main}
  thrown in /var/www/docker/sandbox/phpseclib2/vendor/phpseclib/mcrypt_compat/lib/mcrypt.php on line 1264

Fatal error: Uncaught TypeError: Argument 1 passed to mcrypt_enc_get_iv_size() must be an instance of phpseclib3\Crypt\Common\SymmetricKey, bool given, called in /var/www/docker/sandbox/phpseclib2/test.php on line 48 and defined in /var/www/docker/sandbox/phpseclib2/vendor/phpseclib/mcrypt_compat/lib/mcrypt.php on line 1264

TypeError: Argument 1 passed to mcrypt_enc_get_iv_size() must be an instance of phpseclib3\Crypt\Common\SymmetricKey, bool given, called in /var/www/docker/sandbox/phpseclib2/test.php on line 48 in /var/www/docker/sandbox/phpseclib2/vendor/phpseclib/mcrypt_compat/lib/mcrypt.php on line 1264

Call Stack:
    0.0005     390408   1. {main}() /var/www/docker/sandbox/phpseclib2/test.php:0
    0.0176     624728   2. mcrypt_enc_get_iv_size($td = FALSE) /var/www/docker/sandbox/phpseclib2/test.php:48

Is there a way to make this work?

Php 8.1 deprecation warning

1x: Method "php_user_filter::onClose()" might add "void" as a native return type declaration in the future. Do the same in child class "phpseclib_mcrypt_filter" now to avoid errors or add an explicit @return annotation to suppress this message.

The package is not full

Hello there,

When I install it through composer I don't get the full mcrypt.php as it is in your published folder. For example, I don't see:
function mcrypt_get_iv_size($cipher, $mode) { return phpseclib_mcrypt_get_iv_size($cipher, $mode); }
in the mcrypt.php when I installed through composer however see it in the zip archive.
So I had to manually upload the zip package and replace mcrypt.php with that which in the zip package.

Would it be possible to fix that please?

Thank you

Buffer bug in filter function

When size of bucket data plus buffer divided by block length does not leave a remainder and buffer is not empty it needs to be cleared to avoid buffer content being reinserted next time filter function is called. Please consider this fix:
if ($extra) {
$this->buffer = substr($bucket->data, -$extra);
$bucket->data = substr($bucket->data, 0, -$extra);
} else if (strlen($this->buffer)) {
$this->buffer = "";
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.