]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use random_bytes 20925/head
authorRoeland Jago Douma <roeland@famdouma.nl>
Mon, 11 May 2020 08:31:46 +0000 (10:31 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Mon, 11 May 2020 12:27:06 +0000 (12:27 +0000)
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>
lib/private/Security/Crypto.php
tests/lib/Security/CryptoTest.php

index 876f159950c9c55f35eb9921f3ff48ca033a20a2..0d6f8fa198396d9c63a5315339d0a37bcdeb36af 100644 (file)
@@ -50,17 +50,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;
        }
 
        /**
@@ -93,13 +90,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';
        }
 
        /**
@@ -117,7 +115,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.');
                }
 
@@ -125,6 +124,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)) {
index 356d504f4b37ca214e2fd769f4352ede376dab1e..521ae849efea95ecd97efcc741c3bbf0fe764cb5 100644 (file)
@@ -26,7 +26,7 @@ class CryptoTest extends \Test\TestCase {
 
        protected function setUp() {
                parent::setUp();
-               $this->crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
+               $this->crypto = new Crypto(\OC::$server->getConfig());
        }
 
        /**
@@ -70,4 +70,11 @@ class CryptoTest extends \Test\TestCase {
                $encryptedString = '1|2';
                $this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
        }
+
+       public function testLegacy() {
+               $cipherText = 'e16599188e3d212f5c7f17fdc2abca46|M1WfLAxbcAmITeD6|509457885d6ca5e6c3bfd3741852687a7f2bffce197f8d5ae97b65818b15a1b7f616b68326ff312371540f4ca8ac55f8e2de4aa13aab3474bd3431e51214e3ee';
+               $password = 'mypass';
+
+               $this->assertSame('legacy test', $this->crypto->decrypt($cipherText, $password));
+       }
 }