Skip to content

Commit 0ad56e5

Browse files
authored
Merge pull request #38206 from nextcloud/fix/increase-iterations-for-pbkdf2
Increase from 100000 to 600000 iterations for hash_pbkdf2
2 parents cd5bd11 + 99bdb15 commit 0ad56e5

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

apps/encryption/lib/Crypto/Crypt.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class Crypt {
7070
// default cipher from old Nextcloud versions
7171
public const LEGACY_CIPHER = 'AES-128-CFB';
7272

73-
public const SUPPORTED_KEY_FORMATS = ['hash', 'password'];
73+
public const SUPPORTED_KEY_FORMATS = ['hash2', 'hash', 'password'];
7474
// one out of SUPPORTED_KEY_FORMATS
75-
public const DEFAULT_KEY_FORMAT = 'hash';
75+
public const DEFAULT_KEY_FORMAT = 'hash2';
7676
// default key format, old Nextcloud version encrypted the private key directly
7777
// with the user password
7878
public const LEGACY_KEY_FORMAT = 'password';
@@ -371,22 +371,20 @@ private function addPadding($data) {
371371
* @param string $uid only used for user keys
372372
* @return string
373373
*/
374-
protected function generatePasswordHash($password, $cipher, $uid = '') {
374+
protected function generatePasswordHash(string $password, string $cipher, string $uid = '', int $iterations = 600000): string {
375375
$instanceId = $this->config->getSystemValue('instanceid');
376376
$instanceSecret = $this->config->getSystemValue('secret');
377377
$salt = hash('sha256', $uid . $instanceId . $instanceSecret, true);
378378
$keySize = $this->getKeySize($cipher);
379379

380-
$hash = hash_pbkdf2(
380+
return hash_pbkdf2(
381381
'sha256',
382382
$password,
383383
$salt,
384-
100000,
384+
$iterations,
385385
$keySize,
386386
true
387387
);
388-
389-
return $hash;
390388
}
391389

392390
/**
@@ -431,8 +429,10 @@ public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
431429
$keyFormat = self::LEGACY_KEY_FORMAT;
432430
}
433431

434-
if ($keyFormat === self::DEFAULT_KEY_FORMAT) {
435-
$password = $this->generatePasswordHash($password, $cipher, $uid);
432+
if ($keyFormat === 'hash') {
433+
$password = $this->generatePasswordHash($password, $cipher, $uid, 100000);
434+
} elseif ($keyFormat === 'hash2') {
435+
$password = $this->generatePasswordHash($password, $cipher, $uid, 600000);
436436
}
437437

438438
$binaryEncoding = isset($header['encoding']) && $header['encoding'] === self::BINARY_ENCODING_FORMAT;

apps/encryption/tests/Crypto/CryptTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testGenerateHeaderInvalid() {
137137
*/
138138
public function dataTestGenerateHeader() {
139139
return [
140-
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:encoding:binary:HEND'],
140+
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash2:encoding:binary:HEND'],
141141
['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:encoding:binary:HEND'],
142142
['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:encoding:binary:HEND']
143143
];

0 commit comments

Comments
 (0)