diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2020-05-11 22:00:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-11 22:00:28 +0300 |
commit | 30a7c4fb6e4d0be4ba680914c30c9658f5614dd9 (patch) | |
tree | 5b22f16f2da71f325839907a12f05cc7a24c8d30 /lib | |
parent | 394ca6f7ce9332a390e3b0e1b0687d2558ab839f (diff) | |
parent | b02a79b327c34d4e9927793fe2997812f35e39ff (diff) |
Merge pull request #20924 from nextcloud/backport/20915/stable18
[stable18] Use random_bytes
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 ca17b6e2b8a..664e652bfc0 100644 --- a/lib/private/Security/Crypto.php +++ b/lib/private/Security/Crypto.php @@ -51,17 +51,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; } /** @@ -94,13 +91,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'; } /** @@ -118,7 +116,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.'); } @@ -126,6 +125,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)) { |