diff options
author | binsky <timo@binsky.org> | 2022-05-04 02:41:18 +0300 |
---|---|---|
committer | binsky <timo@binsky.org> | 2022-05-04 02:41:18 +0300 |
commit | f48fd19979f3432f91619a02d22f7a6e22d9e74c (patch) | |
tree | f6ebdb2d8e7df9eaf3f6470f3381670fb747565c | |
parent | ccb884f5ac9d14ca050f5970c0d70bfebcae1447 (diff) |
refactor hash_equals usage in the EncryptService decrypt methodfix/642/hash_equals
Signed-off-by: binsky <timo@binsky.org>
-rw-r--r-- | lib/Service/EncryptService.php | 32 |
1 files changed, 6 insertions, 26 deletions
diff --git a/lib/Service/EncryptService.php b/lib/Service/EncryptService.php index d142ca14..8f4d9d28 100644 --- a/lib/Service/EncryptService.php +++ b/lib/Service/EncryptService.php @@ -152,14 +152,14 @@ class EncryptService { list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key); - if (!$this->hash_equals(hash_hmac('sha512', $enc, $macKey, true), $mac)) { - return false; - } + if (hash_equals(hash_hmac('sha512', $enc, $macKey, true), $mac)) { + $dec = openssl_decrypt($enc, $this->cipher, $cipherKey, true, $iv); + $data = $this->unpad($dec); - $dec = openssl_decrypt($enc, $this->cipher, $cipherKey, true, $iv); - $data = $this->unpad($dec); + return $data; + } - return $data; + return false; } /** @@ -206,26 +206,6 @@ class EncryptService { return array($cipherKey, $macKey, $iv); } - /** - * Use Double HMAC Comparison with a random key to truly blind the comparison operation. - * It is not strictly required by using hash_equals (https://www.php.net/manual/en/function.hash-equals.php), - * but it is a second layer of security to prevent timing attacks. - * - * @param string $a - * @param string $b - * - * @return bool - * @throws \Exception - */ - protected function hash_equals($a, $b) { - if (function_exists('random_bytes')) { - $key = random_bytes(128); - } else { - $key = openssl_random_pseudo_bytes(128); - } - return hash_equals(hash_hmac('sha512', $a, $key), hash_hmac('sha512', $b, $key)); - } - /** * Stretch the key using the PBKDF2 algorithm * |