-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Remove unnecessary double MAC for AEAD ciphers #38475
Conversation
@@ -70,7 +82,7 @@ public static function supported($key, $cipher) | |||
*/ | |||
public static function generateKey($cipher) | |||
{ | |||
return random_bytes($cipher === 'AES-128-CBC' ? 16 : 32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was incorrect for AES-128-GCM, causing key:generate
to output a 256 bit key and the application to throw a RuntimeException
.
{ | ||
return hash_hmac('sha256', $tag.$iv.$value, $this->key); | ||
return hash_hmac('sha256', $iv.$value, $this->key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the only change here and in validMac
is reverting the unnecessary tag addition in https://github.com/laravel/framework/pull/38190/files
2bd55ef
to
e60732f
Compare
@@ -184,7 +194,6 @@ public function decryptString($payload) | |||
* | |||
* @param string $iv | |||
* @param mixed $value | |||
* @param string $tag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you re-add this?
e60732f
to
8abf9c6
Compare
8abf9c6
to
3800323
Compare
Removing the |
Could the following be a problem? |
No I don't think that's a high risk. |
This PR does two things:
generateKey
method (wrong result forAES-128-GCM
, see comment in code below).hmac
computation for AEAD ciphers, improving performance and reducing encrypted data amount.These two problems were introduced recently when support for GCM ciphers was added, in #38190
GCM is a so called Authenticated Encryption scheme (AEAD), which means that a Message Authentication Code (
mac
) is included in the algorithm and handled byopenssl_encrypt/openssl_decrypt
. This is calledtag
in the code, and serves the exact same purpose as themac
that is also present from before (and necessary in the CBC-case, which is not natively authenticated).This PR removes the HMAC computation in the GCM case, since the
tag
already ensures data integrity (before decryption is attempted). This simplifies the code (very important for cryptographic implementations), improves performance and reduces the amount of data stored/transmitted. As an example, the XSRF and session id cookies are reduced by around 25 % (but the reduction for larger data is of course much smaller).The changes are backwards compatible with #38190, and of course with any data stored before that. In fact, the only change to the decryption part is skipping the MAC validation in the case of an AEAD-cipher being used.
This was actually all done correctly in a 2017 PR by bukka, #21963, but that was never merged. I did use that PR as inspiration for this, but I tried to keep the changes to a minimum to increase chances of merging. One larger change is generalizing the list of supported ciphers, and adding the AEAD support as a property. This should simplify the process of adding additional ciphers in the future, and simplified other parts of the code.