From 6afda908bcfd278e49abad3584677516b3a158d6 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 29 Jun 2011 16:39:39 +0200 Subject: [PATCH] Move key transform() call into Database. --- src/core/Database.cpp | 20 +++++++++----------- src/core/Database.h | 6 ++++-- src/format/KeePass2Reader.cpp | 7 +++---- src/format/KeePass2Reader.h | 1 + 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 88c12e0ed..09a8eb088 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -21,6 +21,7 @@ #include #include "Metadata.h" +#include "crypto/Random.h" Database::Database() { @@ -142,21 +143,18 @@ void Database::setCompressionAlgo(Database::CompressionAlgorithm algo) m_compressionAlgo = algo; } -void Database::setTransformSeed(const QByteArray& seed) -{ - Q_ASSERT(seed.size() == 32); - - m_transformSeed = seed; -} - void Database::setTransformRounds(quint64 rounds) { m_transformRounds = rounds; } -void Database::setTransformedMasterKey(QByteArray& key) +void Database::setKey(const CompositeKey& key, const QByteArray& transformSeed) { - Q_ASSERT(key.size() == 32); - - m_transformedMasterKey = key; + m_transformSeed = transformSeed; + m_transformedMasterKey = key.transform(transformSeed, transformRounds()); +} + +void Database::setKey(const CompositeKey& key) +{ + setKey(key, Random::randomArray(32)); } diff --git a/src/core/Database.h b/src/core/Database.h index 5ec296e20..78c132ee8 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -23,6 +23,8 @@ #include #include +#include "keys/CompositeKey.h" + class Metadata; struct DeletedObject @@ -62,9 +64,9 @@ public: void setCipher(const Uuid& cipher); void setCompressionAlgo(Database::CompressionAlgorithm algo); - void setTransformSeed(const QByteArray& seed); void setTransformRounds(quint64 rounds); - void setTransformedMasterKey(QByteArray& key); + void setKey(const CompositeKey& key, const QByteArray& transformSeed); + void setKey(const CompositeKey& key); Q_SIGNALS: void groupDataChanged(Group* group); diff --git a/src/format/KeePass2Reader.cpp b/src/format/KeePass2Reader.cpp index 2f827dfe8..1a499d1ce 100644 --- a/src/format/KeePass2Reader.cpp +++ b/src/format/KeePass2Reader.cpp @@ -64,12 +64,11 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke // TODO check if all header fields have been parsed - QByteArray transformedMasterKey = key.transform(m_db->transformSeed(), m_db->transformRounds()); - m_db->setTransformedMasterKey(transformedMasterKey); + m_db->setKey(key, m_transformSeed); CryptoHash hash(CryptoHash::Sha256); hash.addData(m_masterSeed); - hash.addData(transformedMasterKey); + hash.addData(m_db->transformedMasterKey()); QByteArray finalKey = hash.result(); SymmetricCipherStream cipherStream(device, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt, finalKey, m_encryptionIV); @@ -261,7 +260,7 @@ void KeePass2Reader::setTransformSeed(const QByteArray& data) raiseError(""); } else { - m_db->setTransformSeed(data); + m_transformSeed = data; } } diff --git a/src/format/KeePass2Reader.h b/src/format/KeePass2Reader.h index 154097e53..c2e3c31b7 100644 --- a/src/format/KeePass2Reader.h +++ b/src/format/KeePass2Reader.h @@ -58,6 +58,7 @@ private: Database* m_db; QByteArray m_masterSeed; + QByteArray m_transformSeed; QByteArray m_encryptionIV; QByteArray m_streamStartBytes; QByteArray m_protectedStreamKey;