cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Add openssl options to \Phalcon\Crypt

Open kornerita opened this issue 9 years ago • 6 comments

Hi,

I need to switch using mcrypt in favor of openssl since mcrpyt is deprecated since php 7.1

So, why not use \Phalcon\Crypt?

openssl uses PKCS#7 Padding see here and mcrypt does not so, there is a -nopad option in openssl cli command (and then you can pad as you wish or as mcrypt does) and an OPENSSL_NO_PADDING option for php's openssl function.

Migrating from mcrypt to openssl

mcrypt:

$value = mcrypt_decrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_ECB);

openssl:

$value = openssl_decrypt($data, "des-ede3", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);

The issue is that \Phalcon\Crypt has coded here only OPENSSL_RAW_DATA option and does not provide a way to add extra openssl options.

So I would like to ask for a way to add those options like i.e.:

$cr = new \Phalcon\Crypt();
$cr->setOpensslOptions(OPENSSL_NO_PADDING | OPENSSL_... );

Thanks, Federico.

kornerita avatar Aug 23 '17 20:08 kornerita

The Crypt class has used OpenSSL for quite a while. No changes need to be done on your part. The Crypt class doesn't have a few odd pall padding options as you probably shouldn't be using no_padding as you need to provide data in exact length for encrypt/decrypt as there are numerous issues with these.

Ideally, the crypt classes can be specific to implementation. So inline with the rest of Phalcon, there should just be a crypt interface with a default \Crypt\Adapter\OpenSSL adapter that implements it (pretty much a straight 1-1 copy).

Then a second sodium adapter can be added as well. There is no mcrypt support in Phalcon now.

  1. So, the functionality you desire already exists for the main feature set.
  2. Specific functionality should not be designed as noted in your example; instead, future updates should refactor Crypt to have adapters for OpenSSL and LibSodium consistent with the rest of the framework.
  3. This issue can be closed.

virgofx avatar Aug 23 '17 21:08 virgofx

Hmm, I thought Crypt class already uses OpenSSL!? Or did I miss something? :) https://github.com/phalcon/cphalcon/blob/master/phalcon/crypt.zep#L358

michanismus avatar Aug 24 '17 07:08 michanismus

Hi,

The \Phalcon\Crypt class use the OpenSSL and that is just fine, no complains there.

The problem comes when you try to decrypt i.e. third party encrypted data with openssl and it does not has PKCS#7 Padding.

For instance this code show the difference with encrypted result:

<?php

$data = '12345678'; // one 8 bytes block
$key = "THIS_IS_A_LONG_KEY_12345"; // 24 bytes key for 3des.

echo "Data no encryption: " . PHP_EOL . bin2hex($data) . PHP_EOL;

// Encrypt with openssl, default PKCS#7 Padding.
$value_pkcs7 = openssl_encrypt($data, "des-ede3", $key, OPENSSL_RAW_DATA);

echo "Encrypted data openssl pkcs#7: " . PHP_EOL . bin2hex($value_pkcs7) . PHP_EOL;

// Encrypt the same thing with mcrypt
$value_mcrypt = mcrypt_encrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_ECB);

echo "Encrypted data mcrypt: " . PHP_EOL . bin2hex($value_mcrypt) . PHP_EOL;

// Now, encrypt with openssl without padding.
$value_no_padding = openssl_encrypt($data, "des-ede3", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);

echo "Encrypted data openssl no padding: " . PHP_EOL . bin2hex($value_no_padding) . PHP_EOL;

The output is:

Data no encryption: 
3132333435363738
Encrypted data openssl pkcs#7: 
4e482868ad8cea12cb174766443668f6
Encrypted data mcrypt: 
4e482868ad8cea12
Encrypted data openssl no padding: 
4e482868ad8cea12

The difference in the output is one more block with PKCS#7 Padding which forces to always end with a padded block, in the case that the data size ends with a full block, it adds one more block as you can see in the example; and if you do not have a full block at the end, and you want to use no padding with openssl you should pad that final block by yourself

\Phalcon\Crypt

This one uses openssl, and does not provide a way to use no padding and manually pad data as you would like because it's forced to PKCS#7 by default.

If I were encrypting and decrypting data I personally would NOT use 3DES with ecb, and if I were, I wouldn't have problems with padding, since I'm encrypting and decrypting the data; but when a third party software does not use PKCS#7 Padding which is the case, I must disable that padding on openssl which cannot be achieved with actual \Phalcon\Crypt behavior.

Regards, Federico.

kornerita avatar Aug 24 '17 13:08 kornerita

Federico -- You're correct.. It's one of the slight options right not included as it's a rare case. Typically, crypt is handling both Encrypt/Decrypt for most users (and not necessarily decyrpting data from 3rd party). However, the right approach in fixing the API interfaces, creating adapters for each (OpenSSL and Libsodium) and allowing the native options for both padding, schemes, and few other areas would solve this issue.

I've actually had it on my list for awhile as I've made some other changes to the adapters internally to make them faster. I'll see if I can get around to updating these.

In the meantime, I would just extend the Crypt class for your use case for the time being or use straight openssl methods as needed.

virgofx avatar Aug 24 '17 13:08 virgofx

@virgofx

I agree, the right approach is to have a \Phalcon\CryptInterface, \Phalcon\Crypt\Adapter, \Phalcon\Crypt\Adapter\OpenSSL and \Phalcon\Crypt\Adapter\Libsodium or something like that.

Thanks for looking into this, and if you can get it faster, the better.

In the meantime I am using openssl manually, I open this NFR to enhance the Phalcon framework since I like it so much :)

Regards, Federico.

kornerita avatar Aug 24 '17 13:08 kornerita

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 16 '18 17:04 stale[bot]