diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2020-05-11 11:31:46 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2020-05-11 13:46:59 +0300 |
commit | 35ff4aa1c6693a7aafab4860e8d0c4b0fc077b74 (patch) | |
tree | fc885ac3c7872e49d15f4d330e96500d4e804c0c /lib | |
parent | 90e6b3105948c697fbd1956b79c8bd3775ef0c9f (diff) |
Use random_bytes
Since we don't care if it is human readbale.
The code is backwards compatible with the old format.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Security/Crypto.php | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/private/Security/Crypto.php b/lib/private/Security/Crypto.php index 10a52d9fc8f..154448281b9 100644 --- a/lib/private/Security/Crypto.php +++ b/lib/private/Security/Crypto.php @@ -52,17 +52,14 @@ class Crypto implements ICrypto { private $ivLength = 16; /** @var IConfig */ private $config; - /** @var ISecureRandom */ - private $random; /** * @param IConfig $config * @param ISecureRandom $random */ - public function __construct(IConfig $config, ISecureRandom $random) { + public function __construct(IConfig $config) { $this->cipher = new AES(); $this->config = $config; - $this->random = $random; } /** @@ -95,13 +92,14 @@ class Crypto implements ICrypto { } $this->cipher->setPassword($password); - $iv = $this->random->generate($this->ivLength); + $iv = \random_bytes($this->ivLength); $this->cipher->setIV($iv); $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); + $iv = bin2hex($iv); $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); - return $ciphertext.'|'.$iv.'|'.$hmac; + return $ciphertext.'|'.$iv.'|'.$hmac.'|2'; } /** @@ -119,7 +117,8 @@ class Crypto implements ICrypto { $this->cipher->setPassword($password); $parts = explode('|', $authenticatedCiphertext); - if (\count($parts) !== 3) { + $partCount = \count($parts); + if ($partCount < 3 || $partCount > 4) { throw new \Exception('Authenticated ciphertext could not be decoded.'); } @@ -127,6 +126,13 @@ class Crypto implements ICrypto { $iv = $parts[1]; $hmac = hex2bin($parts[2]); + if ($partCount === 4) { + $version = $parts[3]; + if ($version === '2') { + $iv = hex2bin($iv); + } + } + $this->cipher->setIV($iv); if (!hash_equals($this->calculateHMAC($parts[0] . $parts[1], $password), $hmac)) { |