2
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <QElapsedTimer>
25
#include "format/KeePass2.h"
28
* KeePass' Argon2 implementation supports all parameters that are defined in the official specification,
29
* but only the number of iterations, the memory size and the degree of parallelism can be configured by
30
* the user in the database settings dialog. For the other parameters, KeePass chooses reasonable defaults:
31
* a 256-bit salt is generated each time the database is saved, the tag length is 256 bits, no secret key
32
* or associated data. KeePass uses the latest version of Argon2, v1.3.
34
Argon2Kdf::Argon2Kdf(Type type)
35
: Kdf::Kdf(type == Type::Argon2d ? KeePass2::KDF_ARGON2D : KeePass2::KDF_ARGON2ID)
38
, m_parallelism(static_cast<quint32>(QThread::idealThreadCount()))
43
quint32 Argon2Kdf::version() const
48
bool Argon2Kdf::setVersion(quint32 version)
50
// MIN=0x10; MAX=0x13)
51
if (version >= 0x10 && version <= 0x13) {
59
Argon2Kdf::Type Argon2Kdf::type() const
61
return uuid() == KeePass2::KDF_ARGON2D ? Type::Argon2d : Type::Argon2id;
64
quint64 Argon2Kdf::memory() const
69
bool Argon2Kdf::setMemory(quint64 kibibytes)
71
// MIN=8KB; MAX=2,147,483,648KB
72
if (kibibytes >= 8 && kibibytes < (1ULL << 32)) {
80
quint32 Argon2Kdf::parallelism() const
85
bool Argon2Kdf::setParallelism(quint32 threads)
87
// MIN=1; MAX=16,777,215
88
if (threads >= 1 && threads < (1 << 24)) {
89
m_parallelism = threads;
96
bool Argon2Kdf::processParameters(const QVariantMap& p)
98
QByteArray salt = p.value(KeePass2::KDFPARAM_ARGON2_SALT).toByteArray();
104
quint32 version = p.value(KeePass2::KDFPARAM_ARGON2_VERSION).toUInt(&ok);
105
if (!ok || !setVersion(version)) {
109
quint32 lanes = p.value(KeePass2::KDFPARAM_ARGON2_PARALLELISM).toUInt(&ok);
110
if (!ok || !setParallelism(lanes)) {
114
quint64 memory = p.value(KeePass2::KDFPARAM_ARGON2_MEMORY).toULongLong(&ok) / 1024ULL;
115
if (!ok || !setMemory(memory)) {
119
quint64 iterations = p.value(KeePass2::KDFPARAM_ARGON2_ITERATIONS).toULongLong(&ok);
120
if (!ok || !setRounds(iterations)) {
124
/* KeePass2 does not currently implement these parameters
126
QByteArray secret = p.value(KeePass2::KDFPARAM_ARGON2_SECRET).toByteArray();
127
if (!argon2Kdf->setSecret(secret)) {
131
QByteArray ad = p.value(KeePass2::KDFPARAM_ARGON2_ASSOCDATA).toByteArray();
132
if (!argon2Kdf->setAssocData(ad)) {
140
QVariantMap Argon2Kdf::writeParameters()
143
p.insert(KeePass2::KDFPARAM_UUID, uuid().toRfc4122());
144
p.insert(KeePass2::KDFPARAM_ARGON2_VERSION, version());
145
p.insert(KeePass2::KDFPARAM_ARGON2_PARALLELISM, parallelism());
146
p.insert(KeePass2::KDFPARAM_ARGON2_MEMORY, memory() * 1024);
147
p.insert(KeePass2::KDFPARAM_ARGON2_ITERATIONS, static_cast<quint64>(rounds()));
148
p.insert(KeePass2::KDFPARAM_ARGON2_SALT, seed());
150
/* KeePass2 does not currently implement these
152
if (!assocData().isEmpty()) {
153
p.insert(KeePass2::KDFPARAM_ARGON2_ASSOCDATA, argon2Kdf.assocData());
156
if (!secret().isEmpty()) {
157
p.insert(KeePass2::KDFPARAM_ARGON2_SECRET, argon2Kdf.secret());
164
bool Argon2Kdf::transform(const QByteArray& raw, QByteArray& result) const
168
// Time Cost, Mem Cost, Threads/Lanes, Password, length, Salt, length, out, length
170
int rc = argon2_hash(rounds(),
181
type() == Type::Argon2d ? Argon2_d : Argon2_id,
183
if (rc != ARGON2_OK) {
184
qWarning("Argon2 error: %s", argon2_error_message(rc));
191
QSharedPointer<Kdf> Argon2Kdf::clone() const
193
return QSharedPointer<Argon2Kdf>::create(*this);
196
int Argon2Kdf::benchmark(int msec) const
198
QByteArray key = QByteArray(16, '\x7E');
203
if (transform(key, key)) {
204
return static_cast<int>(rounds() * (static_cast<float>(msec) / timer.elapsed()));
210
QString Argon2Kdf::toString() const
212
return QObject::tr("Argon2%1 (%2 rounds, %3 KB)")
213
.arg(type() == Type::Argon2d ? "d" : "id", QString::number(rounds()), QString::number(memory()));