Refactor Kdf class, remove fields concept

This commit is contained in:
Janek Bevendorff 2017-12-16 18:36:42 +01:00 committed by Jonathan White
parent d00ccd2eb5
commit 15648991fc
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
17 changed files with 282 additions and 474 deletions

View File

@ -33,7 +33,6 @@
#include "format/KeePass2Writer.h" #include "format/KeePass2Writer.h"
#include "keys/PasswordKey.h" #include "keys/PasswordKey.h"
#include "keys/FileKey.h" #include "keys/FileKey.h"
#include "keys/CompositeKey.h"
QHash<Uuid, Database*> Database::m_uuidMap; QHash<Uuid, Database*> Database::m_uuidMap;
@ -45,7 +44,7 @@ Database::Database()
{ {
m_data.cipher = KeePass2::CIPHER_AES; m_data.cipher = KeePass2::CIPHER_AES;
m_data.compressionAlgo = CompressionGZip; m_data.compressionAlgo = CompressionGZip;
m_data.kdf = new AesKdf(); m_data.kdf = QSharedPointer<AesKdf>::create();
m_data.kdf->randomizeTransformSalt(); m_data.kdf->randomizeTransformSalt();
m_data.hasKey = false; m_data.hasKey = false;
@ -485,23 +484,18 @@ QString Database::saveToFile(QString filePath)
} }
} }
Kdf* Database::kdf() const { QSharedPointer<Kdf> Database::kdf() const
{
return m_data.kdf; return m_data.kdf;
} }
void Database::setKdf(Kdf* kdf) { void Database::setKdf(QSharedPointer<Kdf> kdf)
if (m_data.kdf == kdf) { {
return; m_data.kdf = std::move(kdf);
}
if (m_data.kdf != nullptr) {
delete m_data.kdf;
}
m_data.kdf = kdf;
} }
bool Database::changeKdf(Kdf* kdf) { bool Database::changeKdf(QSharedPointer<Kdf> kdf)
{
kdf->randomizeTransformSalt(); kdf->randomizeTransformSalt();
QByteArray transformedMasterKey; QByteArray transformedMasterKey;
if (!m_data.key.transform(*kdf, transformedMasterKey)) { if (!m_data.key.transform(*kdf, transformedMasterKey)) {
@ -514,8 +508,3 @@ bool Database::changeKdf(Kdf* kdf) {
return true; return true;
} }
Database::DatabaseData::~DatabaseData()
{
delete kdf;
}

View File

@ -58,17 +58,15 @@ public:
Uuid cipher; Uuid cipher;
CompressionAlgorithm compressionAlgo; CompressionAlgorithm compressionAlgo;
QByteArray transformedMasterKey; QByteArray transformedMasterKey;
Kdf* kdf; QSharedPointer<Kdf> kdf;
CompositeKey key; CompositeKey key;
bool hasKey; bool hasKey;
QByteArray masterSeed; QByteArray masterSeed;
QByteArray challengeResponseKey; QByteArray challengeResponseKey;
~DatabaseData();
}; };
Database(); Database();
~Database(); ~Database() override;
Group* rootGroup(); Group* rootGroup();
const Group* rootGroup() const; const Group* rootGroup() const;
@ -92,7 +90,7 @@ public:
Uuid cipher() const; Uuid cipher() const;
Database::CompressionAlgorithm compressionAlgo() const; Database::CompressionAlgorithm compressionAlgo() const;
Kdf* kdf() const; QSharedPointer<Kdf> kdf() const;
QByteArray transformedMasterKey() const; QByteArray transformedMasterKey() const;
const CompositeKey& key() const; const CompositeKey& key() const;
QByteArray challengeResponseKey() const; QByteArray challengeResponseKey() const;
@ -100,7 +98,7 @@ public:
void setCipher(const Uuid& cipher); void setCipher(const Uuid& cipher);
void setCompressionAlgo(Database::CompressionAlgorithm algo); void setCompressionAlgo(Database::CompressionAlgorithm algo);
void setKdf(Kdf* kdf); void setKdf(QSharedPointer<Kdf> kdf);
bool setKey(const CompositeKey& key, bool updateChangedTime = true, bool setKey(const CompositeKey& key, bool updateChangedTime = true,
bool updateTransformSalt = false); bool updateTransformSalt = false);
bool hasKey() const; bool hasKey() const;
@ -117,7 +115,7 @@ public:
* Returns a unique id that is only valid as long as the Database exists. * Returns a unique id that is only valid as long as the Database exists.
*/ */
Uuid uuid(); Uuid uuid();
bool changeKdf(Kdf* kdf); bool changeKdf(QSharedPointer<Kdf> kdf);
static Database* databaseByUuid(const Uuid& uuid); static Database* databaseByUuid(const Uuid& uuid);
static Database* openDatabaseFile(QString fileName, CompositeKey key); static Database* openDatabaseFile(QString fileName, CompositeKey key);

View File

@ -18,20 +18,10 @@
#include <QtConcurrent> #include <QtConcurrent>
#include "format/KeePass2.h" #include "format/KeePass2.h"
#include "crypto/SymmetricCipher.h"
#include "crypto/CryptoHash.h" #include "crypto/CryptoHash.h"
#include "crypto/Random.h" #include "crypto/Random.h"
#include "AesKdf.h" #include "AesKdf.h"
const QList<Kdf::Field> AesKdf::FIELDS = AesKdf::initFields();
QList<Kdf::Field> AesKdf::initFields()
{
return QList<Kdf::Field> {
Kdf::Field(static_cast<quint32>(Fields::ROUNDS), "Transform rounds", 1, UINT64_MAX, true),
};
}
bool AesKdf::transform(const QByteArray& raw, QByteArray& result) const bool AesKdf::transform(const QByteArray& raw, QByteArray& result) const
{ {
QByteArray resultLeft; QByteArray resultLeft;
@ -76,7 +66,8 @@ bool AesKdf::transformKeyRaw(const QByteArray& key, const QByteArray& seed, quin
} }
AesKdf::AesKdf() AesKdf::AesKdf()
: m_rounds(100000ull) : Kdf::Kdf(KeePass2::KDF_AES)
, m_rounds(100000ull)
, m_seed(QByteArray(32, 0)) , m_seed(QByteArray(32, 0))
{ {
} }
@ -112,39 +103,9 @@ void AesKdf::randomizeTransformSalt()
setSeed(randomGen()->randomArray(32)); setSeed(randomGen()->randomArray(32));
} }
Kdf::Type AesKdf::type() const QSharedPointer<Kdf> AesKdf::clone() const
{ {
return Kdf::Type::AES; return QSharedPointer<AesKdf>::create(*this);
}
Kdf* AesKdf::clone() const
{
return static_cast<Kdf*>(new AesKdf(*this));
}
const QList<Kdf::Field> AesKdf::fields() const
{
return FIELDS;
}
quint64 AesKdf::field(quint32 id) const
{
switch (static_cast<Fields>(id)) {
case Fields::ROUNDS:
return m_rounds;
default:
return 0;
}
}
bool AesKdf::setField(quint32 id, quint64 val)
{
switch (static_cast<Fields>(id)) {
case Fields::ROUNDS:
return setRounds(val);
default:
return false;
}
} }
int AesKdf::benchmarkImpl(int msec) const int AesKdf::benchmarkImpl(int msec) const

View File

@ -27,26 +27,13 @@ public:
bool transform(const QByteArray& raw, QByteArray& result) const override; bool transform(const QByteArray& raw, QByteArray& result) const override;
void randomizeTransformSalt() override; void randomizeTransformSalt() override;
Kdf::Type type() const override; QSharedPointer<Kdf> clone() const override;
Kdf* clone() const override;
const QList<Field> fields() const override; quint64 rounds() const override;
quint64 field(quint32 id) const override; QByteArray seed() const override;
bool setField(quint32 id, quint64 val) override;
quint64 rounds() const; bool setRounds(quint64 rounds) override;
QByteArray seed() const; bool setSeed(const QByteArray& seed) override;
bool setRounds(quint64 rounds);
bool setSeed(const QByteArray& seed);
enum class Fields: quint32
{
ROUNDS,
SEED
};
static const QList<Kdf::Field> FIELDS;
protected: protected:
int benchmarkImpl(int msec) const override; int benchmarkImpl(int msec) const override;
@ -59,7 +46,6 @@ private:
const QByteArray& seed, const QByteArray& seed,
quint64 rounds, quint64 rounds,
QByteArray* result) Q_REQUIRED_RESULT; QByteArray* result) Q_REQUIRED_RESULT;
static QList<Kdf::Field> initFields();
}; };
#endif // KEEPASSX_AESKDF_H #endif // KEEPASSX_AESKDF_H

View File

@ -18,42 +18,16 @@
#include "Kdf.h" #include "Kdf.h"
#include "Kdf_p.h" #include "Kdf_p.h"
#include <QThread>
#include <QElapsedTimer>
#include <QtConcurrent> #include <QtConcurrent>
Kdf::Field::Field(quint32 id, const QString& name, quint64 min, quint64 max, bool benchmark) Kdf::Kdf(Uuid uuid)
: m_id(id) : m_uuid(uuid)
, m_name(name)
, m_min(min)
, m_max(max)
, m_benchmark(benchmark)
{ {
} }
quint32 Kdf::Field::id() const Uuid Kdf::uuid() const
{ {
return m_id; return m_uuid;
}
QString Kdf::Field::name() const
{
return m_name;
}
quint64 Kdf::Field::min() const
{
return m_min;
}
quint64 Kdf::Field::max() const
{
return m_max;
}
bool Kdf::Field::benchmarked() const
{
return m_benchmark;
} }
int Kdf::benchmark(int msec) const int Kdf::benchmark(int msec) const

View File

@ -25,41 +25,19 @@
class Kdf class Kdf
{ {
public: public:
enum class Type explicit Kdf(Uuid uuid);
{ virtual ~Kdf() = default;
AES
};
class Field Uuid uuid() const;
{
public:
Field(quint32 id, const QString& name, quint64 min, quint64 max, bool benchmark = false);
quint32 id() const;
QString name() const;
quint64 min() const;
quint64 max() const;
bool benchmarked() const;
private:
quint32 m_id;
QString m_name;
quint64 m_min;
quint64 m_max;
bool m_benchmark;
};
virtual ~Kdf() {}
virtual quint64 rounds() const = 0;
virtual bool setRounds(quint64 rounds) = 0;
virtual QByteArray seed() const = 0; virtual QByteArray seed() const = 0;
virtual Type type() const = 0; virtual bool setSeed(const QByteArray& seed) = 0;
virtual bool transform(const QByteArray& raw, QByteArray& result) const = 0; virtual bool transform(const QByteArray& raw, QByteArray& result) const = 0;
virtual void randomizeTransformSalt() = 0; virtual void randomizeTransformSalt() = 0;
virtual Kdf* clone() const = 0; virtual QSharedPointer<Kdf> clone() const = 0;
virtual const QList<Field> fields() const = 0;
virtual quint64 field(quint32 id) const = 0;
virtual bool setField(quint32 id, quint64 val) = 0;
virtual int benchmark(int msec) const; virtual int benchmark(int msec) const;
protected: protected:
@ -67,6 +45,7 @@ protected:
private: private:
class BenchmarkThread; class BenchmarkThread;
const Uuid m_uuid;
}; };

View File

@ -318,36 +318,33 @@ void Kdbx3Reader::setTransformSeed(const QByteArray& data)
{ {
if (data.size() != 32) { if (data.size() != 32) {
raiseError("Invalid transform seed size"); raiseError("Invalid transform seed size");
} else { return;
AesKdf* aesKdf;
if (m_db->kdf()->type() == Kdf::Type::AES) {
aesKdf = static_cast<AesKdf*>(m_db->kdf());
} else {
aesKdf = new AesKdf();
m_db->setKdf(aesKdf);
}
aesKdf->setSeed(data);
} }
auto kdf = m_db->kdf();
if (!kdf) {
kdf = QSharedPointer<AesKdf>::create();
m_db->setKdf(kdf);
}
kdf->setSeed(data);
} }
void Kdbx3Reader::setTransformRounds(const QByteArray& data) void Kdbx3Reader::setTransformRounds(const QByteArray& data)
{ {
if (data.size() != 8) { if (data.size() != 8) {
raiseError("Invalid transform rounds size"); raiseError("Invalid transform rounds size");
} else { return;
quint64 rounds = Endian::bytesToSizedInt<quint64>(data, KeePass2::BYTEORDER);
AesKdf* aesKdf;
if (m_db->kdf()->type() == Kdf::Type::AES) {
aesKdf = static_cast<AesKdf*>(m_db->kdf());
} else {
aesKdf = new AesKdf();
m_db->setKdf(aesKdf);
}
aesKdf->setRounds(rounds);
} }
auto rounds = Endian::bytesToSizedInt<quint64>(data, KeePass2::BYTEORDER);
auto kdf = m_db->kdf();
if (!kdf) {
kdf = QSharedPointer<AesKdf>::create();
m_db->setKdf(kdf);
}
kdf->setRounds(rounds);
} }
void Kdbx3Reader::setEncryptionIV(const QByteArray& data) void Kdbx3Reader::setEncryptionIV(const QByteArray& data)

View File

@ -80,7 +80,7 @@ bool Kdbx3Writer::writeDatabase(QIODevice* device, Database* db)
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::CompressionFlags, CHECK_RETURN_FALSE(writeHeaderField(KeePass2::CompressionFlags,
Endian::sizedIntToBytes<qint32>(db->compressionAlgo(), Endian::sizedIntToBytes<qint32>(db->compressionAlgo(),
KeePass2::BYTEORDER))); KeePass2::BYTEORDER)));
AesKdf* kdf = static_cast<AesKdf*>(db->kdf()); auto kdf = db->kdf();
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::MasterSeed, masterSeed)); CHECK_RETURN_FALSE(writeHeaderField(KeePass2::MasterSeed, masterSeed));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformSeed, kdf->seed())); CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformSeed, kdf->seed()));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformRounds, CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformRounds,

View File

@ -160,7 +160,7 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
raiseError("Invalid number of transform rounds"); raiseError("Invalid number of transform rounds");
return nullptr; return nullptr;
} }
AesKdf* kdf = new AesKdf(); auto kdf = QSharedPointer<AesKdf>::create();
kdf->setRounds(m_transformRounds); kdf->setRounds(m_transformRounds);
kdf->setSeed(m_transformSeed); kdf->setSeed(m_transformSeed);
db->setKdf(kdf); db->setKdf(kdf);

View File

@ -16,9 +16,9 @@
*/ */
#include "KeePass2.h" #include "KeePass2.h"
#include "crypto/CryptoHash.h"
#include "crypto/kdf/AesKdf.h" #include "crypto/kdf/AesKdf.h"
#include "core/Uuid.h" #include <QSharedPointer>
const Uuid KeePass2::CIPHER_AES = Uuid(QByteArray::fromHex("31c1f2e6bf714350be5805216afc5aff")); const Uuid KeePass2::CIPHER_AES = Uuid(QByteArray::fromHex("31c1f2e6bf714350be5805216afc5aff"));
const Uuid KeePass2::CIPHER_TWOFISH = Uuid(QByteArray::fromHex("ad68f29f576f4bb9a36ad47af965346c")); const Uuid KeePass2::CIPHER_TWOFISH = Uuid(QByteArray::fromHex("ad68f29f576f4bb9a36ad47af965346c"));
@ -28,31 +28,23 @@ const Uuid KeePass2::KDF_AES = Uuid(QByteArray::fromHex("C9D9F39A628A4460BF740D0
const QByteArray KeePass2::INNER_STREAM_SALSA20_IV("\xE8\x30\x09\x4B\x97\x20\x5D\x2A"); const QByteArray KeePass2::INNER_STREAM_SALSA20_IV("\xE8\x30\x09\x4B\x97\x20\x5D\x2A");
const QList<KeePass2::UuidNamePair> KeePass2::CIPHERS { const QList<QPair<Uuid, QString>> KeePass2::CIPHERS {
KeePass2::UuidNamePair(KeePass2::CIPHER_AES, "AES: 256-bit"), qMakePair(KeePass2::CIPHER_AES, QObject::tr("AES: 256-bit")),
KeePass2::UuidNamePair(KeePass2::CIPHER_TWOFISH, "Twofish: 256-bit"), qMakePair(KeePass2::CIPHER_TWOFISH, QObject::tr("Twofish: 256-bit")),
KeePass2::UuidNamePair(KeePass2::CIPHER_CHACHA20, "ChaCha20: 256-bit") qMakePair(KeePass2::CIPHER_CHACHA20, QObject::tr("ChaCha20: 256-bit"))
}; };
const QList<KeePass2::UuidNamePair> KeePass2::KDFS { const QList<QPair<Uuid, QString>> KeePass2::KDFS {
KeePass2::UuidNamePair(KeePass2::KDF_AES, "AES-KDF"), qMakePair(KeePass2::KDF_AES, QObject::tr("AES-KDF")),
}; };
Kdf* KeePass2::uuidToKdf(const Uuid& uuid) { QSharedPointer<Kdf> KeePass2::uuidToKdf(const Uuid& uuid)
if (uuid == KDF_AES) {
return static_cast<Kdf*>(new AesKdf());
}
return nullptr;
}
Uuid KeePass2::kdfToUuid(const Kdf& kdf)
{ {
switch (kdf.type()) { if (uuid == KDF_AES) {
case Kdf::Type::AES: return QSharedPointer<AesKdf>::create();
return KDF_AES;
default:
return Uuid();
} }
Q_ASSERT_X(false, "uuidToKdf", "Invalid UUID");
return nullptr;
} }
KeePass2::ProtectedStreamAlgo KeePass2::idToProtectedStreamAlgo(quint32 id) KeePass2::ProtectedStreamAlgo KeePass2::idToProtectedStreamAlgo(quint32 id)
@ -68,19 +60,3 @@ KeePass2::ProtectedStreamAlgo KeePass2::idToProtectedStreamAlgo(quint32 id)
return KeePass2::InvalidProtectedStreamAlgo; return KeePass2::InvalidProtectedStreamAlgo;
} }
} }
KeePass2::UuidNamePair::UuidNamePair(const Uuid& uuid, const QString& name)
: m_uuid(uuid)
, m_name(name)
{
}
Uuid KeePass2::UuidNamePair::uuid() const
{
return m_uuid;
}
QString KeePass2::UuidNamePair::name() const
{
return m_name;
}

View File

@ -43,20 +43,8 @@ namespace KeePass2
extern const QByteArray INNER_STREAM_SALSA20_IV; extern const QByteArray INNER_STREAM_SALSA20_IV;
class UuidNamePair extern const QList<QPair<Uuid, QString>> CIPHERS;
{ extern const QList<QPair<Uuid, QString>> KDFS;
public:
UuidNamePair(const Uuid& uuid, const QString& name);
Uuid uuid() const;
QString name() const;
private:
Uuid m_uuid;
QString m_name;
};
extern const QList<UuidNamePair> CIPHERS;
extern const QList<UuidNamePair> KDFS;
enum HeaderFieldID enum HeaderFieldID
{ {
@ -81,8 +69,7 @@ namespace KeePass2
InvalidProtectedStreamAlgo = -1 InvalidProtectedStreamAlgo = -1
}; };
Kdf* uuidToKdf(const Uuid& uuid); QSharedPointer<Kdf> uuidToKdf(const Uuid& uuid);
Uuid kdfToUuid(const Kdf& kdf);
ProtectedStreamAlgo idToProtectedStreamAlgo(quint32 id); ProtectedStreamAlgo idToProtectedStreamAlgo(quint32 id);
} }

View File

@ -20,6 +20,7 @@
#include <QMessageBox> #include <QMessageBox>
#include "core/Global.h"
#include "core/AsyncTask.h" #include "core/AsyncTask.h"
#include "core/Database.h" #include "core/Database.h"
#include "core/Group.h" #include "core/Group.h"
@ -40,7 +41,6 @@ DatabaseSettingsWidget::DatabaseSettingsWidget(QWidget* parent)
m_ui->historyMaxItemsSpinBox, SLOT(setEnabled(bool))); m_ui->historyMaxItemsSpinBox, SLOT(setEnabled(bool)));
connect(m_ui->historyMaxSizeCheckBox, SIGNAL(toggled(bool)), connect(m_ui->historyMaxSizeCheckBox, SIGNAL(toggled(bool)),
m_ui->historyMaxSizeSpinBox, SLOT(setEnabled(bool))); m_ui->historyMaxSizeSpinBox, SLOT(setEnabled(bool)));
connect(m_ui->kdfComboBox, SIGNAL(currentIndexChanged(int)), SLOT(changeKdf(int)));
connect(m_ui->transformBenchmarkButton, SIGNAL(clicked()), SLOT(transformRoundsBenchmark())); connect(m_ui->transformBenchmarkButton, SIGNAL(clicked()), SLOT(transformRoundsBenchmark()));
} }
@ -61,8 +61,7 @@ void DatabaseSettingsWidget::load(Database* db)
if (meta->historyMaxItems() > -1) { if (meta->historyMaxItems() > -1) {
m_ui->historyMaxItemsSpinBox->setValue(meta->historyMaxItems()); m_ui->historyMaxItemsSpinBox->setValue(meta->historyMaxItems());
m_ui->historyMaxItemsCheckBox->setChecked(true); m_ui->historyMaxItemsCheckBox->setChecked(true);
} } else {
else {
m_ui->historyMaxItemsSpinBox->setValue(Metadata::DefaultHistoryMaxItems); m_ui->historyMaxItemsSpinBox->setValue(Metadata::DefaultHistoryMaxItems);
m_ui->historyMaxItemsCheckBox->setChecked(false); m_ui->historyMaxItemsCheckBox->setChecked(false);
} }
@ -70,17 +69,14 @@ void DatabaseSettingsWidget::load(Database* db)
if (historyMaxSizeMiB > 0) { if (historyMaxSizeMiB > 0) {
m_ui->historyMaxSizeSpinBox->setValue(historyMaxSizeMiB); m_ui->historyMaxSizeSpinBox->setValue(historyMaxSizeMiB);
m_ui->historyMaxSizeCheckBox->setChecked(true); m_ui->historyMaxSizeCheckBox->setChecked(true);
} } else {
else {
m_ui->historyMaxSizeSpinBox->setValue(Metadata::DefaultHistoryMaxSize); m_ui->historyMaxSizeSpinBox->setValue(Metadata::DefaultHistoryMaxSize);
m_ui->historyMaxSizeCheckBox->setChecked(false); m_ui->historyMaxSizeCheckBox->setChecked(false);
} }
m_ui->algorithmComboBox->clear(); m_ui->algorithmComboBox->clear();
for (QList<KeePass2::UuidNamePair>::const_iterator ciphers = KeePass2::CIPHERS.constBegin(); for (auto& cipher: asConst(KeePass2::CIPHERS)) {
ciphers != KeePass2::CIPHERS.constEnd(); ++ciphers) { m_ui->algorithmComboBox->addItem(cipher.second, cipher.first.toByteArray());
KeePass2::UuidNamePair cipher = *ciphers;
m_ui->algorithmComboBox->addItem(cipher.name(), cipher.uuid().toByteArray());
} }
int cipherIndex = m_ui->algorithmComboBox->findData(m_db->cipher().toByteArray()); int cipherIndex = m_ui->algorithmComboBox->findData(m_db->cipher().toByteArray());
if (cipherIndex > -1) { if (cipherIndex > -1) {
@ -89,20 +85,17 @@ void DatabaseSettingsWidget::load(Database* db)
bool blockSignals = m_ui->kdfComboBox->signalsBlocked(); bool blockSignals = m_ui->kdfComboBox->signalsBlocked();
m_ui->kdfComboBox->blockSignals(true); m_ui->kdfComboBox->blockSignals(true);
m_kdf.reset(m_db->kdf()->clone());
m_ui->kdfComboBox->clear(); m_ui->kdfComboBox->clear();
for (QList<KeePass2::UuidNamePair>::const_iterator kdfs = KeePass2::KDFS.constBegin(); for (auto& kdf: asConst(KeePass2::KDFS)) {
kdfs != KeePass2::KDFS.constEnd(); ++kdfs) { m_ui->kdfComboBox->addItem(kdf.second, kdf.first.toByteArray());
KeePass2::UuidNamePair kdf = *kdfs;
m_ui->kdfComboBox->addItem(kdf.name(), kdf.uuid().toByteArray());
} }
int kdfIndex = m_ui->kdfComboBox->findData(KeePass2::kdfToUuid(*m_kdf).toByteArray()); int kdfIndex = m_ui->kdfComboBox->findData(m_db->kdf()->uuid().toByteArray());
if (kdfIndex > -1) { if (kdfIndex > -1) {
m_ui->kdfComboBox->setCurrentIndex(kdfIndex); m_ui->kdfComboBox->setCurrentIndex(kdfIndex);
} }
displayKdf(*m_kdf);
m_ui->kdfComboBox->blockSignals(blockSignals); m_ui->kdfComboBox->blockSignals(blockSignals);
m_ui->transformRoundsSpinBox->setValue(static_cast<unsigned>(m_kdf->rounds())); m_ui->transformRoundsSpinBox->setValue(static_cast<unsigned>(m_db->kdf()->rounds()));
m_ui->dbNameEdit->setFocus(); m_ui->dbNameEdit->setFocus();
} }
@ -121,8 +114,7 @@ void DatabaseSettingsWidget::save()
int historyMaxItems; int historyMaxItems;
if (m_ui->historyMaxItemsCheckBox->isChecked()) { if (m_ui->historyMaxItemsCheckBox->isChecked()) {
historyMaxItems = m_ui->historyMaxItemsSpinBox->value(); historyMaxItems = m_ui->historyMaxItemsSpinBox->value();
} } else {
else {
historyMaxItems = -1; historyMaxItems = -1;
} }
if (historyMaxItems != meta->historyMaxItems()) { if (historyMaxItems != meta->historyMaxItems()) {
@ -133,8 +125,7 @@ void DatabaseSettingsWidget::save()
int historyMaxSize; int historyMaxSize;
if (m_ui->historyMaxSizeCheckBox->isChecked()) { if (m_ui->historyMaxSizeCheckBox->isChecked()) {
historyMaxSize = m_ui->historyMaxSizeSpinBox->value() * 1048576; historyMaxSize = m_ui->historyMaxSizeSpinBox->value() * 1048576;
} } else {
else {
historyMaxSize = -1; historyMaxSize = -1;
} }
if (historyMaxSize != meta->historyMaxSize()) { if (historyMaxSize != meta->historyMaxSize()) {
@ -148,31 +139,20 @@ void DatabaseSettingsWidget::save()
m_db->setCipher(Uuid(m_ui->algorithmComboBox->currentData().toByteArray())); m_db->setCipher(Uuid(m_ui->algorithmComboBox->currentData().toByteArray()));
bool kdfValid = true; auto kdf = KeePass2::uuidToKdf(Uuid(m_ui->kdfComboBox->currentData().toByteArray()));
for (int i = 0; i < m_kdfFields.size(); ++i) { kdf->setRounds(static_cast<quint64>(qMax(0, m_ui->transformRoundsSpinBox->value())));
QPair<quint32, QSpinBox*> field = m_kdfFields.at(i);
kdfValid &= m_kdf->setField(field.first, static_cast<quint64>(qMax(0, field.second->value())));
if (!kdfValid) {
break;
}
}
if (kdfValid) { QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
Kdf* kdf = m_kdf.take(); // TODO: we should probably use AsyncTask::runAndWaitForFuture() here,
bool ok = m_db->changeKdf(kdf); // but not without making Database thread-safe
if (!ok) { bool ok = m_db->changeKdf(kdf);
MessageBox::warning(this, tr("KDF unchanged"), QApplication::restoreOverrideCursor();
tr("Failed to transform key with new KDF parameters; KDF unchanged."),
QMessageBox::Ok); if (!ok) {
delete kdf; // m_db has not taken ownership
}
} else {
MessageBox::warning(this, tr("KDF unchanged"), MessageBox::warning(this, tr("KDF unchanged"),
tr("Invalid KDF parameters; KDF unchanged."), tr("Failed to transform key with new KDF parameters; KDF unchanged."),
QMessageBox::Ok); QMessageBox::Ok);
} }
clearKdfWidgets();
emit editFinished(true); emit editFinished(true);
} }
@ -185,7 +165,7 @@ void DatabaseSettingsWidget::transformRoundsBenchmark()
{ {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
m_ui->transformRoundsSpinBox->setValue(AsyncTask::runAndWaitForFuture([this]() { m_ui->transformRoundsSpinBox->setValue(AsyncTask::runAndWaitForFuture([this]() {
int rounds = m_kdf->benchmark(1000); int rounds = m_db->kdf()->benchmark(1000);
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
return rounds; return rounds;
})); }));
@ -198,66 +178,3 @@ void DatabaseSettingsWidget::truncateHistories()
entry->truncateHistory(); entry->truncateHistory();
} }
} }
void DatabaseSettingsWidget::changeKdf(int index) {
QByteArray uuidBytes = m_ui->kdfComboBox->itemData(index).toByteArray();
if (uuidBytes.size() != Uuid::Length) {
return;
}
Kdf* newKdf = KeePass2::uuidToKdf(Uuid(uuidBytes));
if (newKdf != nullptr) {
m_kdf.reset(newKdf);
displayKdf(*m_kdf);
}
}
void DatabaseSettingsWidget::displayKdf(const Kdf& kdf)
{
clearKdfWidgets();
QWidget* lastWidget = nullptr;
int columnStart = m_ui->gridLayout->columnCount();
int rowStart = m_ui->gridLayout->rowCount();
QList<Kdf::Field> fields = kdf.fields();
for (int i = 0; i < fields.size(); i++) {
const Kdf::Field& field = fields.at(i);
QLabel* label = new QLabel(QString("%1:").arg(field.name()));
QSpinBox* spinBox = new QSpinBox();
m_kdfWidgets.append(label);
m_kdfWidgets.append(spinBox);
m_kdfFields.append(QPair<quint32, QSpinBox*>(field.id(), spinBox));
spinBox->setMinimum(static_cast<qint32>(qMin(qMax(0ull, field.min()), 0x7FFFFFFFull)));
spinBox->setMaximum(static_cast<qint32>(qMin(qMax(0ull, field.max()), 0x7FFFFFFFull)));
spinBox->setValue(static_cast<qint32>(qMin(qMax(0ull, kdf.field(field.id())), 0x7FFFFFFFull)));
spinBox->setObjectName(QString("kdfParams%1").arg(i));
m_ui->gridLayout->addWidget(label, rowStart + i, columnStart - 3, Qt::AlignRight);
if (field.benchmarked()) {
Q_ASSERT(m_benchmarkField == nullptr);
QPushButton* benchBtn = new QPushButton("Benchmark");
connect(benchBtn, &QPushButton::clicked, this, &DatabaseSettingsWidget::transformRoundsBenchmark);
m_kdfWidgets.append(benchBtn);
m_ui->gridLayout->addWidget(spinBox, rowStart + i, columnStart - 2);
m_ui->gridLayout->addWidget(benchBtn, rowStart + i, columnStart - 1);
m_benchmarkField = spinBox;
lastWidget = benchBtn;
} else {
m_ui->gridLayout->addWidget(spinBox, rowStart + i, columnStart - 2, 1, 2);
lastWidget = spinBox;
}
}
if (lastWidget != nullptr) {
QWidget::setTabOrder(lastWidget, m_ui->buttonBox->button(QDialogButtonBox::StandardButton::Cancel));
QWidget::setTabOrder(m_ui->buttonBox->button(QDialogButtonBox::StandardButton::Cancel), m_ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok));
}
}
void DatabaseSettingsWidget::clearKdfWidgets()
{
m_benchmarkField = nullptr;
for (int i = 0; i < m_kdfWidgets.size(); ++i) {
m_ui->gridLayout->removeWidget(m_kdfWidgets.at(i));
m_kdfWidgets.at(i)->deleteLater();
}
m_kdfWidgets.clear();
m_kdfFields.clear();
}

View File

@ -40,6 +40,7 @@ Q_OBJECT
public: public:
explicit DatabaseSettingsWidget(QWidget* parent = nullptr); explicit DatabaseSettingsWidget(QWidget* parent = nullptr);
~DatabaseSettingsWidget(); ~DatabaseSettingsWidget();
Q_DISABLE_COPY(DatabaseSettingsWidget)
void load(Database* db); void load(Database* db);
@ -50,19 +51,12 @@ private slots:
void save(); void save();
void reject(); void reject();
void transformRoundsBenchmark(); void transformRoundsBenchmark();
void changeKdf(int index);
private: private:
void truncateHistories(); void truncateHistories();
void displayKdf(const Kdf& kdf);
void clearKdfWidgets();
const QScopedPointer<Ui::DatabaseSettingsWidget> m_ui; const QScopedPointer<Ui::DatabaseSettingsWidget> m_ui;
QList<QWidget*> m_kdfWidgets;
QScopedPointer<Kdf> m_kdf;
Database* m_db; Database* m_db;
Q_DISABLE_COPY(DatabaseSettingsWidget)
}; };
#endif // KEEPASSX_DATABASESETTINGSWIDGET_H #endif // KEEPASSX_DATABASESETTINGSWIDGET_H

View File

@ -2,17 +2,9 @@
<ui version="4.0"> <ui version="4.0">
<class>DatabaseSettingsWidget</class> <class>DatabaseSettingsWidget</class>
<widget class="QWidget" name="DatabaseSettingsWidget"> <widget class="QWidget" name="DatabaseSettingsWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>340</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,2,5,1"> <layout class="QVBoxLayout" name="verticalLayout" stretch="1,2,5,1">
<item> <item>
<spacer name="topSpacer"> <spacer name="verticalSpacer_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
@ -25,9 +17,9 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0"> <layout class="QHBoxLayout" name="horizontalLayout_5" stretch="0,1,0">
<item> <item>
<spacer name="leftSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@ -40,118 +32,185 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <widget class="QWidget" name="widget" native="true">
<item row="7" column="2" colspan="2"> <property name="maximumSize">
<widget class="QComboBox" name="kdfComboBox"/> <size>
</item> <width>800</width>
<item row="0" column="1" alignment="Qt::AlignRight"> <height>16777215</height>
<widget class="QLabel" name="dbNameLabel"> </size>
<property name="text"> </property>
<string>Database name:</string> <layout class="QGridLayout" name="gridLayout">
</property> <item row="4" column="2">
</widget> <layout class="QHBoxLayout" name="horizontalLayout_3">
</item> <item>
<item row="5" column="1"> <widget class="QSpinBox" name="transformRoundsSpinBox">
<widget class="QCheckBox" name="historyMaxSizeCheckBox"> <property name="sizePolicy">
<property name="text"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<string>Max. history size:</string> <horstretch>0</horstretch>
</property> <verstretch>0</verstretch>
</widget> </sizepolicy>
</item> </property>
<item row="7" column="1" alignment="Qt::AlignRight"> <property name="minimum">
<widget class="QLabel" name="kdfLabel"> <number>1</number>
<property name="text"> </property>
<string>Key derivation function:</string> <property name="maximum">
</property> <number>1000000000</number>
</widget> </property>
</item> </widget>
<item row="4" column="1"> </item>
<widget class="QCheckBox" name="historyMaxItemsCheckBox"> <item>
<property name="text"> <widget class="QToolButton" name="transformBenchmarkButton">
<string>Max. history items:</string> <property name="sizePolicy">
</property> <sizepolicy hsizetype="Fixed" vsizetype="MinimumExpanding">
</widget> <horstretch>0</horstretch>
</item> <verstretch>0</verstretch>
<item row="0" column="2" colspan="2"> </sizepolicy>
<widget class="QLineEdit" name="dbNameEdit"/> </property>
</item> <property name="text">
<item row="4" column="2" colspan="2"> <string>Benchmark</string>
<widget class="QSpinBox" name="historyMaxItemsSpinBox"> </property>
<property name="sizePolicy"> </widget>
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> </item>
<horstretch>0</horstretch> </layout>
<verstretch>0</verstretch> </item>
</sizepolicy> <item row="0" column="1" alignment="Qt::AlignRight">
</property> <widget class="QLabel" name="dbNameLabel">
<property name="maximum"> <property name="text">
<number>2000000000</number> <string>Database name:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1" alignment="Qt::AlignRight"> <item row="8" column="1">
<widget class="QLabel" name="defaultUsernameLabel"> <widget class="QCheckBox" name="historyMaxSizeCheckBox">
<property name="text"> <property name="text">
<string>Default username:</string> <string>Max. history size:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="2" colspan="2"> <item row="4" column="1" alignment="Qt::AlignRight">
<widget class="QLineEdit" name="dbDescriptionEdit"/> <widget class="QLabel" name="transformRoundsLabel">
</item> <property name="text">
<item row="5" column="2" colspan="2"> <string>Transform rounds:</string>
<widget class="QSpinBox" name="historyMaxSizeSpinBox"> </property>
<property name="sizePolicy"> </widget>
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> </item>
<horstretch>0</horstretch> <item row="7" column="1">
<verstretch>0</verstretch> <widget class="QCheckBox" name="historyMaxItemsCheckBox">
</sizepolicy> <property name="text">
</property> <string>Max. history items:</string>
<property name="suffix"> </property>
<string> MiB</string> </widget>
</property> </item>
<property name="minimum"> <item row="0" column="2">
<number>1</number> <widget class="QLineEdit" name="dbNameEdit"/>
</property> </item>
<property name="maximum"> <item row="7" column="2">
<number>2000000000</number> <layout class="QHBoxLayout" name="horizontalLayout_2">
</property> <item>
</widget> <widget class="QSpinBox" name="historyMaxItemsSpinBox">
</item> <property name="sizePolicy">
<item row="3" column="2" colspan="2"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<widget class="QCheckBox" name="recycleBinEnabledCheckBox"> <horstretch>0</horstretch>
<property name="text"> <verstretch>0</verstretch>
<string>Use recycle bin</string> </sizepolicy>
</property> </property>
</widget> <property name="maximum">
</item> <number>2000000000</number>
<item row="2" column="2" colspan="2"> </property>
<widget class="QLineEdit" name="defaultUsernameEdit"> </widget>
<property name="enabled"> </item>
<bool>true</bool> </layout>
</property> </item>
</widget> <item row="5" column="1" alignment="Qt::AlignRight">
</item> <widget class="QLabel" name="defaultUsernameLabel">
<item row="1" column="1" alignment="Qt::AlignRight"> <property name="text">
<widget class="QLabel" name="dbDescriptionLabel"> <string>Default username:</string>
<property name="text"> </property>
<string>Database description:</string> </widget>
</property> </item>
</widget> <item row="1" column="2">
</item> <widget class="QLineEdit" name="dbDescriptionEdit"/>
<item row="6" column="2" colspan="2"> </item>
<widget class="QComboBox" name="algorithmComboBox"/> <item row="8" column="2">
</item> <layout class="QHBoxLayout" name="horizontalLayout">
<item row="6" column="1" alignment="Qt::AlignRight"> <item>
<widget class="QLabel" name="algorithmLabel"> <widget class="QSpinBox" name="historyMaxSizeSpinBox">
<property name="text"> <property name="sizePolicy">
<string>Algorithm:</string> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
</property> <horstretch>0</horstretch>
</widget> <verstretch>0</verstretch>
</item> </sizepolicy>
</layout> </property>
<property name="suffix">
<string> MiB</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>2000000000</number>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="2">
<widget class="QCheckBox" name="recycleBinEnabledCheckBox">
<property name="text">
<string>Use recycle bin</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QLineEdit" name="defaultUsernameEdit">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1" alignment="Qt::AlignRight">
<widget class="QLabel" name="dbDescriptionLabel">
<property name="text">
<string>Database description:</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QComboBox" name="algorithmComboBox">
<item>
<property name="text">
<string>AES: 256 Bit (default)</string>
</property>
</item>
<item>
<property name="text">
<string>Twofish: 256 Bit</string>
</property>
</item>
</widget>
</item>
<item row="2" column="1" alignment="Qt::AlignRight">
<widget class="QLabel" name="algorithmLabel">
<property name="text">
<string>Encryption Algorithm:</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QComboBox" name="kdfComboBox"/>
</item>
<item row="3" column="1">
<widget class="QLabel" name="kdfLabel">
<property name="text">
<string>Key Derivation Function</string>
</property>
</widget>
</item>
</layout>
</widget>
</item> </item>
<item> <item>
<spacer name="rightSpacer"> <spacer name="horizontalSpacer_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@ -166,7 +225,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<spacer name="bottomSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
@ -190,14 +249,14 @@
<tabstops> <tabstops>
<tabstop>dbNameEdit</tabstop> <tabstop>dbNameEdit</tabstop>
<tabstop>dbDescriptionEdit</tabstop> <tabstop>dbDescriptionEdit</tabstop>
<tabstop>transformRoundsSpinBox</tabstop>
<tabstop>transformBenchmarkButton</tabstop>
<tabstop>defaultUsernameEdit</tabstop> <tabstop>defaultUsernameEdit</tabstop>
<tabstop>recycleBinEnabledCheckBox</tabstop> <tabstop>recycleBinEnabledCheckBox</tabstop>
<tabstop>historyMaxItemsCheckBox</tabstop> <tabstop>historyMaxItemsCheckBox</tabstop>
<tabstop>historyMaxItemsSpinBox</tabstop> <tabstop>historyMaxItemsSpinBox</tabstop>
<tabstop>historyMaxSizeCheckBox</tabstop> <tabstop>historyMaxSizeCheckBox</tabstop>
<tabstop>historyMaxSizeSpinBox</tabstop> <tabstop>historyMaxSizeSpinBox</tabstop>
<tabstop>algorithmComboBox</tabstop>
<tabstop>kdfComboBox</tabstop>
<tabstop>buttonBox</tabstop> <tabstop>buttonBox</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>

View File

@ -17,19 +17,11 @@
*/ */
#include "CompositeKey.h" #include "CompositeKey.h"
#include "ChallengeResponseKey.h"
#include <QElapsedTimer>
#include <QFile> #include <QFile>
#include <QtConcurrent> #include <QtConcurrent>
#include "crypto/kdf/Kdf.h"
#include "format/KeePass2.h"
#include "core/Global.h" #include "core/Global.h"
#include "crypto/CryptoHash.h" #include "crypto/CryptoHash.h"
#include "crypto/SymmetricCipher.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
CompositeKey::CompositeKey() CompositeKey::CompositeKey()
{ {

View File

@ -26,7 +26,6 @@
#include "core/Group.h" #include "core/Group.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "crypto/Crypto.h" #include "crypto/Crypto.h"
#include "crypto/kdf/AesKdf.h"
#include "format/KeePass1Reader.h" #include "format/KeePass1Reader.h"
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h" #include "format/KeePass2Writer.h"
@ -111,7 +110,7 @@ void TestKeePass1Reader::testBasic()
void TestKeePass1Reader::testMasterKey() void TestKeePass1Reader::testMasterKey()
{ {
QVERIFY(m_db->hasKey()); QVERIFY(m_db->hasKey());
QCOMPARE(static_cast<AesKdf*>(m_db->kdf())->rounds(), static_cast<quint64>(713)); QCOMPARE(m_db->kdf()->rounds(), static_cast<quint64>(713));
} }
void TestKeePass1Reader::testCustomIcons() void TestKeePass1Reader::testCustomIcons()

View File

@ -903,7 +903,7 @@ void TestGui::testDatabaseSettings()
QTest::keyClick(transformRoundsSpinBox, Qt::Key_Enter); QTest::keyClick(transformRoundsSpinBox, Qt::Key_Enter);
// wait for modified timer // wait for modified timer
QTRY_COMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save*")); QTRY_COMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save*"));
QCOMPARE(static_cast<AesKdf*>(m_db->kdf())->rounds(), Q_UINT64_C(100)); QCOMPARE(m_db->kdf()->rounds(), Q_UINT64_C(100));
triggerAction("actionDatabaseSave"); triggerAction("actionDatabaseSave");
QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save")); QCOMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("Save"));