Merge pull request #39433 from nextcloud/backport/39024/stable20

[stable20] [stable21] Increase from 100000 to 600000 iterations for hash_pbkdf2
pull/39498/head
Côme Chilliet 11 months ago committed by GitHub
commit b1f198ac34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -105,7 +105,7 @@ class Crypt {
$this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : '"no user given"'; $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : '"no user given"';
$this->config = $config; $this->config = $config;
$this->l = $l; $this->l = $l;
$this->supportedKeyFormats = ['hash', 'password']; $this->supportedKeyFormats = ['hash2', 'hash', 'password'];
$this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false); $this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false);
} }
@ -207,11 +207,11 @@ class Crypt {
/** /**
* generate header for encrypted file * generate header for encrypted file
* *
* @param string $keyFormat (can be 'hash' or 'password') * @param string $keyFormat (can be 'hash2', 'hash' or 'password')
* @return string * @return string
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function generateHeader($keyFormat = 'hash') { public function generateHeader($keyFormat = 'hash2') {
if (in_array($keyFormat, $this->supportedKeyFormats, true) === false) { if (in_array($keyFormat, $this->supportedKeyFormats, true) === false) {
throw new \InvalidArgumentException('key format "' . $keyFormat . '" is not supported'); throw new \InvalidArgumentException('key format "' . $keyFormat . '" is not supported');
} }
@ -351,22 +351,20 @@ class Crypt {
* @param string $uid only used for user keys * @param string $uid only used for user keys
* @return string * @return string
*/ */
protected function generatePasswordHash($password, $cipher, $uid = '') { protected function generatePasswordHash(string $password, string $cipher, string $uid = '', int $iterations = 600000): string {
$instanceId = $this->config->getSystemValue('instanceid'); $instanceId = $this->config->getSystemValue('instanceid');
$instanceSecret = $this->config->getSystemValue('secret'); $instanceSecret = $this->config->getSystemValue('secret');
$salt = hash('sha256', $uid . $instanceId . $instanceSecret, true); $salt = hash('sha256', $uid . $instanceId . $instanceSecret, true);
$keySize = $this->getKeySize($cipher); $keySize = $this->getKeySize($cipher);
$hash = hash_pbkdf2( return hash_pbkdf2(
'sha256', 'sha256',
$password, $password,
$salt, $salt,
100000, $iterations,
$keySize, $keySize,
true true
); );
return $hash;
} }
/** /**
@ -412,7 +410,9 @@ class Crypt {
} }
if ($keyFormat === 'hash') { if ($keyFormat === 'hash') {
$password = $this->generatePasswordHash($password, $cipher, $uid); $password = $this->generatePasswordHash($password, $cipher, $uid, 100000);
} elseif ($keyFormat === 'hash2') {
$password = $this->generatePasswordHash($password, $cipher, $uid, 600000);
} }
// If we found a header we need to remove it from the key we want to decrypt // If we found a header we need to remove it from the key we want to decrypt

@ -140,7 +140,7 @@ class CryptTest extends TestCase {
*/ */
public function dataTestGenerateHeader() { public function dataTestGenerateHeader() {
return [ return [
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND'], [null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash2:HEND'],
['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:HEND'], ['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:HEND'],
['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND'] ['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND']
]; ];

Loading…
Cancel
Save