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 <QtConcurrent>
22
#include "crypto/CryptoHash.h"
23
#include "crypto/SymmetricCipher.h"
24
#include "format/KeePass2.h"
27
: Kdf::Kdf(KeePass2::KDF_AES_KDBX4)
32
* @param legacyKdbx3 initialize as legacy KDBX3 KDF
34
AesKdf::AesKdf(bool legacyKdbx3)
35
: Kdf::Kdf(legacyKdbx3 ? KeePass2::KDF_AES_KDBX3 : KeePass2::KDF_AES_KDBX4)
39
bool AesKdf::processParameters(const QVariantMap& p)
42
int rounds = p.value(KeePass2::KDFPARAM_AES_ROUNDS).toInt(&ok);
43
if (!ok || !setRounds(rounds)) {
47
QByteArray seed = p.value(KeePass2::KDFPARAM_AES_SEED).toByteArray();
51
QVariantMap AesKdf::writeParameters()
55
// always write old KDBX3 AES-KDF UUID for compatibility with other applications
56
p.insert(KeePass2::KDFPARAM_UUID, KeePass2::KDF_AES_KDBX3.toRfc4122());
58
p.insert(KeePass2::KDFPARAM_AES_ROUNDS, static_cast<quint64>(rounds()));
59
p.insert(KeePass2::KDFPARAM_AES_SEED, seed());
63
bool AesKdf::transform(const QByteArray& raw, QByteArray& result) const
65
return transformKeyRaw(raw, m_seed, m_rounds, &result);
68
bool AesKdf::transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds, QByteArray* result)
75
SymmetricCipher::aesKdf(seed, rounds, out);
76
*result = CryptoHash::hash(out, CryptoHash::Sha256);
80
QSharedPointer<Kdf> AesKdf::clone() const
82
return QSharedPointer<AesKdf>::create(*this);
85
int AesKdf::benchmark(int msec) const
87
QByteArray key(16, '\x7E');
88
QByteArray seed(32, '\x4B');
95
for (int i = 0; i < trials; ++i) {
97
if (!transformKeyRaw(key, seed, rounds, &result)) {
102
return static_cast<int>(rounds * trials * static_cast<float>(msec) / timer.elapsed());
105
QString AesKdf::toString() const
107
return QObject::tr("AES (%1 rounds)").arg(QString::number(rounds()));