mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-12-16 00:44:17 -05:00
Refactor Kdf class, remove fields concept
This commit is contained in:
parent
d00ccd2eb5
commit
15648991fc
17 changed files with 282 additions and 474 deletions
|
|
@ -318,36 +318,33 @@ void Kdbx3Reader::setTransformSeed(const QByteArray& data)
|
|||
{
|
||||
if (data.size() != 32) {
|
||||
raiseError("Invalid transform seed size");
|
||||
} else {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
auto kdf = m_db->kdf();
|
||||
if (!kdf) {
|
||||
kdf = QSharedPointer<AesKdf>::create();
|
||||
m_db->setKdf(kdf);
|
||||
}
|
||||
|
||||
kdf->setSeed(data);
|
||||
}
|
||||
|
||||
void Kdbx3Reader::setTransformRounds(const QByteArray& data)
|
||||
{
|
||||
if (data.size() != 8) {
|
||||
raiseError("Invalid transform rounds size");
|
||||
} else {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ bool Kdbx3Writer::writeDatabase(QIODevice* device, Database* db)
|
|||
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::CompressionFlags,
|
||||
Endian::sizedIntToBytes<qint32>(db->compressionAlgo(),
|
||||
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::TransformSeed, kdf->seed()));
|
||||
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformRounds,
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
|
|||
raiseError("Invalid number of transform rounds");
|
||||
return nullptr;
|
||||
}
|
||||
AesKdf* kdf = new AesKdf();
|
||||
auto kdf = QSharedPointer<AesKdf>::create();
|
||||
kdf->setRounds(m_transformRounds);
|
||||
kdf->setSeed(m_transformSeed);
|
||||
db->setKdf(kdf);
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@
|
|||
*/
|
||||
|
||||
#include "KeePass2.h"
|
||||
#include "crypto/CryptoHash.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_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 QList<KeePass2::UuidNamePair> KeePass2::CIPHERS {
|
||||
KeePass2::UuidNamePair(KeePass2::CIPHER_AES, "AES: 256-bit"),
|
||||
KeePass2::UuidNamePair(KeePass2::CIPHER_TWOFISH, "Twofish: 256-bit"),
|
||||
KeePass2::UuidNamePair(KeePass2::CIPHER_CHACHA20, "ChaCha20: 256-bit")
|
||||
const QList<QPair<Uuid, QString>> KeePass2::CIPHERS {
|
||||
qMakePair(KeePass2::CIPHER_AES, QObject::tr("AES: 256-bit")),
|
||||
qMakePair(KeePass2::CIPHER_TWOFISH, QObject::tr("Twofish: 256-bit")),
|
||||
qMakePair(KeePass2::CIPHER_CHACHA20, QObject::tr("ChaCha20: 256-bit"))
|
||||
};
|
||||
const QList<KeePass2::UuidNamePair> KeePass2::KDFS {
|
||||
KeePass2::UuidNamePair(KeePass2::KDF_AES, "AES-KDF"),
|
||||
const QList<QPair<Uuid, QString>> KeePass2::KDFS {
|
||||
qMakePair(KeePass2::KDF_AES, QObject::tr("AES-KDF")),
|
||||
};
|
||||
|
||||
Kdf* KeePass2::uuidToKdf(const Uuid& uuid) {
|
||||
if (uuid == KDF_AES) {
|
||||
return static_cast<Kdf*>(new AesKdf());
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Uuid KeePass2::kdfToUuid(const Kdf& kdf)
|
||||
QSharedPointer<Kdf> KeePass2::uuidToKdf(const Uuid& uuid)
|
||||
{
|
||||
switch (kdf.type()) {
|
||||
case Kdf::Type::AES:
|
||||
return KDF_AES;
|
||||
default:
|
||||
return Uuid();
|
||||
if (uuid == KDF_AES) {
|
||||
return QSharedPointer<AesKdf>::create();
|
||||
}
|
||||
|
||||
Q_ASSERT_X(false, "uuidToKdf", "Invalid UUID");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
KeePass2::ProtectedStreamAlgo KeePass2::idToProtectedStreamAlgo(quint32 id)
|
||||
|
|
@ -68,19 +60,3 @@ KeePass2::ProtectedStreamAlgo KeePass2::idToProtectedStreamAlgo(quint32 id)
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,20 +43,8 @@ namespace KeePass2
|
|||
|
||||
extern const QByteArray INNER_STREAM_SALSA20_IV;
|
||||
|
||||
class UuidNamePair
|
||||
{
|
||||
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;
|
||||
extern const QList<QPair<Uuid, QString>> CIPHERS;
|
||||
extern const QList<QPair<Uuid, QString>> KDFS;
|
||||
|
||||
enum HeaderFieldID
|
||||
{
|
||||
|
|
@ -81,8 +69,7 @@ namespace KeePass2
|
|||
InvalidProtectedStreamAlgo = -1
|
||||
};
|
||||
|
||||
Kdf* uuidToKdf(const Uuid& uuid);
|
||||
Uuid kdfToUuid(const Kdf& kdf);
|
||||
QSharedPointer<Kdf> uuidToKdf(const Uuid& uuid);
|
||||
ProtectedStreamAlgo idToProtectedStreamAlgo(quint32 id);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue