Skip to content

Commit b2d6fe2

Browse files
committed
Correct Argon2 settings when creating new database
* Argon2 default parallelism settings were set to the number of threads on the computer. That is excessive on high cpu count computers.
1 parent 7432661 commit b2d6fe2

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/crypto/kdf/Argon2Kdf.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
*/
3434
Argon2Kdf::Argon2Kdf(Type type)
3535
: Kdf::Kdf(type == Type::Argon2d ? KeePass2::KDF_ARGON2D : KeePass2::KDF_ARGON2ID)
36-
, m_version(0x13)
37-
, m_memory(1 << 16)
38-
, m_parallelism(static_cast<quint32>(QThread::idealThreadCount()))
36+
, m_version(ARGON2_DEFAULT_VERSION)
37+
, m_memory(ARGON2_DEFAULT_MEMORY)
38+
, m_parallelism(qMin<quint32>(QThread::idealThreadCount(), ARGON2_DEFAULT_PARALLELISM))
3939
{
40-
m_rounds = 10;
40+
m_rounds = ARGON2_DEFAULT_ROUNDS;
4141
}
4242

4343
quint32 Argon2Kdf::version() const
@@ -52,7 +52,7 @@ bool Argon2Kdf::setVersion(quint32 version)
5252
m_version = version;
5353
return true;
5454
}
55-
m_version = 0x13;
55+
m_version = ARGON2_DEFAULT_VERSION;
5656
return false;
5757
}
5858

@@ -73,7 +73,7 @@ bool Argon2Kdf::setMemory(quint64 kibibytes)
7373
m_memory = kibibytes;
7474
return true;
7575
}
76-
m_memory = 16;
76+
m_memory = ARGON2_DEFAULT_MEMORY;
7777
return false;
7878
}
7979

@@ -89,7 +89,7 @@ bool Argon2Kdf::setParallelism(quint32 threads)
8989
m_parallelism = threads;
9090
return true;
9191
}
92-
m_parallelism = 1;
92+
m_parallelism = ARGON2_DEFAULT_PARALLELISM;
9393
return false;
9494
}
9595

src/crypto/kdf/Argon2Kdf.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
#include "Kdf.h"
2222

23+
constexpr auto ARGON2_DEFAULT_VERSION = 0x13;
24+
constexpr auto ARGON2_DEFAULT_ROUNDS = 10;
25+
constexpr auto ARGON2_DEFAULT_MEMORY = 1 << 32;
26+
constexpr auto ARGON2_DEFAULT_PARALLELISM = 4;
27+
2328
class Argon2Kdf : public Kdf
2429
{
2530
public:
@@ -47,6 +52,15 @@ class Argon2Kdf : public Kdf
4752

4853
int benchmark(int msec) const override;
4954

55+
static quint64 toMebibytes(quint64 kibibytes)
56+
{
57+
return kibibytes >> 10;
58+
}
59+
static quint64 toKibibytes(quint64 mebibits)
60+
{
61+
return mebibits << 10;
62+
}
63+
5064
quint32 m_version;
5165
quint64 m_memory;
5266
quint32 m_parallelism;

src/gui/dbsettings/DatabaseSettingsWidgetEncryption.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ void DatabaseSettingsWidgetEncryption::initialize()
159159
// Set up KDF algorithms
160160
loadKdfAlgorithms();
161161

162-
// Perform Benchmark if requested
163162
if (isNewDatabase) {
164-
if (IS_ARGON2(m_ui->kdfComboBox->currentData())) {
165-
m_ui->memorySpinBox->setValue(16);
166-
m_ui->parallelismSpinBox->setValue(2);
167-
}
168163
benchmarkTransformRounds();
169164
}
170165

@@ -225,16 +220,16 @@ void DatabaseSettingsWidgetEncryption::loadKdfParameters()
225220
// Set Argon2 parameters
226221
auto argon2Kdf = kdf.staticCast<Argon2Kdf>();
227222
m_ui->transformRoundsSpinBox->setValue(argon2Kdf->rounds());
228-
m_ui->memorySpinBox->setValue(static_cast<int>(argon2Kdf->memory()) / (1 << 10));
223+
m_ui->memorySpinBox->setValue(Argon2Kdf::toMebibytes(argon2Kdf->memory()));
229224
m_ui->parallelismSpinBox->setValue(argon2Kdf->parallelism());
230225
} else if (!dbIsArgon2 && !kdfIsArgon2) {
231226
// Set AES KDF parameters
232227
m_ui->transformRoundsSpinBox->setValue(kdf->rounds());
233228
} else {
234229
// Set reasonable defaults and then benchmark
235230
if (kdfIsArgon2) {
236-
m_ui->memorySpinBox->setValue(16);
237-
m_ui->parallelismSpinBox->setValue(2);
231+
m_ui->memorySpinBox->setValue(Argon2Kdf::toMebibytes(ARGON2_DEFAULT_MEMORY));
232+
m_ui->parallelismSpinBox->setValue(ARGON2_DEFAULT_PARALLELISM);
238233
}
239234
benchmarkTransformRounds();
240235
}
@@ -343,7 +338,7 @@ bool DatabaseSettingsWidgetEncryption::saveSettings()
343338
kdf->setRounds(m_ui->transformRoundsSpinBox->value());
344339
if (IS_ARGON2(kdf->uuid())) {
345340
auto argon2Kdf = kdf.staticCast<Argon2Kdf>();
346-
argon2Kdf->setMemory(static_cast<quint64>(m_ui->memorySpinBox->value()) * (1 << 10));
341+
argon2Kdf->setMemory(Argon2Kdf::toKibibytes(m_ui->memorySpinBox->value()));
347342
argon2Kdf->setParallelism(static_cast<quint32>(m_ui->parallelismSpinBox->value()));
348343
}
349344

@@ -377,8 +372,8 @@ void DatabaseSettingsWidgetEncryption::benchmarkTransformRounds(int millisecs)
377372
auto argon2Kdf = kdf.staticCast<Argon2Kdf>();
378373
// Set a small static number of rounds for the benchmark
379374
argon2Kdf->setRounds(4);
380-
if (!argon2Kdf->setMemory(static_cast<quint64>(m_ui->memorySpinBox->value()) * (1 << 10))) {
381-
m_ui->memorySpinBox->setValue(static_cast<int>(argon2Kdf->memory() / (1 << 10)));
375+
if (!argon2Kdf->setMemory(Argon2Kdf::toKibibytes(m_ui->memorySpinBox->value()))) {
376+
m_ui->memorySpinBox->setValue(Argon2Kdf::toMebibytes(argon2Kdf->memory()));
382377
}
383378
if (!argon2Kdf->setParallelism(static_cast<quint32>(m_ui->parallelismSpinBox->value()))) {
384379
m_ui->parallelismSpinBox->setValue(argon2Kdf->parallelism());

0 commit comments

Comments
 (0)