Skip to content

Commit

Permalink
Fix Argon2 options checks
Browse files Browse the repository at this point in the history
The minimum for memory cost is 8 KiB per thread. Threads must be checked and set first to allow checking against the correct memory cost mimimum.
Options are now applied the following way:
- If config.php contains the setting with an integer higher or equal to the minimum, it is applied.
- If config.php contains the setting with an integer lower than the minimum, the minimum is applied.
- If config.php does not contain the setting or with no integer value, the PHP default is applied.

Signed-off-by: MichaIng <micha@dietpi.com>
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
MichaIng authored and rullzer committed Apr 30, 2020
1 parent a1c1b35 commit ad60619
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/private/Security/Hasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,11 @@ public function __construct(IConfig $config) {

if (\defined('PASSWORD_ARGON2I')) {
// password_hash fails, when the minimum values are undershot.
// In this case, ignore and revert to default
if ($this->config->getSystemValueInt('hashingMemoryCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST) >= 8) {
$this->options['memory_cost'] = $this->config->getSystemValueInt('hashingMemoryCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST);
}
if ($this->config->getSystemValueInt('hashingTimeCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST) >= 1) {
$this->options['time_cost'] = $this->config->getSystemValueInt('hashingTimeCost', PASSWORD_ARGON2_DEFAULT_TIME_COST);
}
if ($this->config->getSystemValueInt('hashingThreads', PASSWORD_ARGON2_DEFAULT_MEMORY_COST) >= 1) {
$this->options['threads'] = $this->config->getSystemValueInt('hashingThreads', PASSWORD_ARGON2_DEFAULT_THREADS);
}
// In this case, apply minimum.
$this->options['threads'] = max($this->config->getSystemValueInt('hashingThreads', PASSWORD_ARGON2_DEFAULT_THREADS), 1);
// The minimum memory cost is 8 KiB per thread.
$this->options['memory_cost'] = max($this->config->getSystemValueInt('hashingMemoryCost', PASSWORD_ARGON2_DEFAULT_MEMORY_COST), $this->options['threads'] * 8);
$this->options['time_cost'] = max($this->config->getSystemValueInt('hashingTimeCost', PASSWORD_ARGON2_DEFAULT_TIME_COST), 1);
}

$hashingCost = $this->config->getSystemValue('hashingCost', null);
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/Security/HasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ protected function setUp(): void {

$this->config = $this->createMock(IConfig::class);

$this->config->method('getSystemValueInt')
->willReturnCallback(function ($name, $default) {
return $default;
});

$this->hasher = new Hasher($this->config);
}

Expand Down

0 comments on commit ad60619

Please sign in to comment.