Skip to content

Add an option for no zero padding of the key in openssl_encrypt and openssl_decrypt #2498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

bukka
Copy link
Member

@bukka bukka commented Apr 25, 2017

This fixes a problem with filling key with \0 if it's shorter than default length of the cipher and instead prioritise setting a variable key length. That applies only on ciphers with variable key length (Blowfish, RC4 and some others). Please note that this has no influence on ciphers with defined fixed key length like AES. It fixes bug 72362 and bug 71917

The reason for a new flag is to prevent BC issues that would happen if it was done by default because the used key would be different (it means that the key would no longer filled with \0).

I targeted 7.1 which I think should be fine. It's a new option (constant) which fixes 2 bugs. @krakjoe Are you ok with such addition to 7.1?

@nikic
Copy link
Member

nikic commented Apr 25, 2017

To clarify, what is the current behavior when a too-short key is passed to fixed-length primitive like AES? I assume it is rejected and not padded, right?

@bukka
Copy link
Member Author

bukka commented Apr 25, 2017

Nope such key is padded with \0. Yeah I know, it's not really nice but I guess lots of code is dependent on it so we can't really change default and adding warning / notice for that is sort of out of scope for this PR. I think that it should be at least documented though...

This is meant just for variable length cipher to allow providing shorter keys where it is possible.

@nikic
Copy link
Member

nikic commented Apr 25, 2017

In that case I'd prefer leaving 7.1 alone and just fix this completely in next minor, without introducing an option. If people want to pad, they can go pad themselves.

Btw, we started rejecting incorrectly sized keys in mcrypt in PHP 5.6.

@bukka
Copy link
Member Author

bukka commented Apr 25, 2017

I think that it doesn't make sense for variable length cipher as the result just changes without any warning. Please consider following:

openssl_encrypt($data, 'bf-ecb', '12345678');

Then it will be different between two PHP versions and without searching changelog you won't know why. That's a big problem if you have got some old code that hasn't been changed for ages and then you just don't know why it doesn't work. So what most people probably do is that they won't update. That's just not right IMHO.

@bukka
Copy link
Member Author

bukka commented Apr 25, 2017

@nikic so are you ok with this or do we need RFC? I don't agree that this should be changed in the next minor without any migration path (e.g. first emitting notice or something like that). Especially for ciphers with variable length where would be BC break really ugly as noted above.

@nikic
Copy link
Member

nikic commented Apr 25, 2017

I'll trust your judgement here, no need to RFC.

@bukka
Copy link
Member Author

bukka commented Apr 26, 2017

@nikic Thanks

@krakjoe When you have time, please could you let me know if it's ok for 7.1 or you prefer master only (7.2). Cheers. ;)

@nikic nikic changed the title Add variable length priority option for openssl_enrypt and openssl_decrypt Add variable length priority option for openssl_encrypt and openssl_decrypt May 1, 2017
@nikic
Copy link
Member

nikic commented May 1, 2017

It would be nice to have a nicer name for this option. VARIABLE_LENGTH_PRIORITY doesn't really tell me what this does. Maybe something like DONT_ZERO_PAD_KEY? (Though I guess this is confusing in that it only applies to variable-length keys, hm...)

@bukka
Copy link
Member Author

bukka commented May 1, 2017

Well it's not even completely correct for variable length cipher that the key won't be zero padded. It will be still padded if the key len is 0 (empty string). The only thing it does is trying to set key len first.

Thinking about that, it's still kind of weird. So how about something a little bit different. We could introduce the flag that you suggest - DONT_ZERO_PAD_KEY. If it's set, then it would try to set the key len (when shorter than default key len) and if that fails (e.g. fixed len cipher), then it would fail with warning and the function would return false. What do you think?

@nikic
Copy link
Member

nikic commented May 1, 2017

@bukka I like that, sounds like a cleaner and more comprehensive solution.

@krakjoe krakjoe added the Bug label May 2, 2017
@bukka bukka changed the title Add variable length priority option for openssl_encrypt and openssl_decrypt Add an option for no zero padding of the key in openssl_encrypt and openssl_decrypt May 5, 2017
@bukka
Copy link
Member Author

bukka commented May 5, 2017

@nikic I modified the patch so all should be as discussed. ;)

@krakjoe I'm taking that Bugfix label as an ACK for 7.1. :) Please let know in case I'm wrong and you don't think that it's good for 7.1 or have any other objections.

@krakjoe
Copy link
Member

krakjoe commented Jun 22, 2017

@bukka fine for 7.1, sorry about the delay there ...

@bukka
Copy link
Member Author

bukka commented Jun 25, 2017

Merged via 0c707fc

@bukka bukka closed this Jun 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants