Add support for Twofish in KeePass2 code

This commit is contained in:
Timothy Redaelli 2015-08-04 15:18:41 +02:00 committed by Janek Bevendorff
parent a3fd3205a9
commit a01607e869
No known key found for this signature in database
GPG Key ID: CFEC2F6850BFFA53
5 changed files with 29 additions and 4 deletions

View File

@ -83,3 +83,23 @@ QString SymmetricCipher::errorString() const
{ {
return m_backend->errorString(); return m_backend->errorString();
} }
SymmetricCipher::Algorithm SymmetricCipher::cipherToAlgorithm(Uuid cipher)
{
if (cipher == KeePass2::CIPHER_AES) {
return SymmetricCipher::Aes256;
}
else {
return SymmetricCipher::Twofish;
}
}
Uuid SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm algo)
{
switch (algo) {
case SymmetricCipher::Aes256:
return KeePass2::CIPHER_AES;
default:
return KeePass2::CIPHER_TWOFISH;
}
}

View File

@ -23,6 +23,7 @@
#include <QString> #include <QString>
#include "crypto/SymmetricCipherBackend.h" #include "crypto/SymmetricCipherBackend.h"
#include "format/KeePass2.h"
class SymmetricCipher class SymmetricCipher
{ {
@ -71,6 +72,9 @@ public:
int blockSize() const; int blockSize() const;
QString errorString() const; QString errorString() const;
static SymmetricCipher::Algorithm cipherToAlgorithm(Uuid cipher);
static Uuid algorithmToCipher(SymmetricCipher::Algorithm algo);
private: private:
static SymmetricCipherBackend* createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, static SymmetricCipherBackend* createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction); SymmetricCipher::Direction direction);

View File

@ -33,6 +33,7 @@ namespace KeePass2
const QSysInfo::Endian BYTEORDER = QSysInfo::LittleEndian; const QSysInfo::Endian BYTEORDER = QSysInfo::LittleEndian;
const Uuid CIPHER_AES = Uuid(QByteArray::fromHex("31c1f2e6bf714350be5805216afc5aff")); const Uuid CIPHER_AES = Uuid(QByteArray::fromHex("31c1f2e6bf714350be5805216afc5aff"));
const Uuid CIPHER_TWOFISH = Uuid(QByteArray::fromHex("ad68f29f576f4bb9a36ad47af965346c"));
const QByteArray INNER_STREAM_SALSA20_IV("\xE8\x30\x09\x4B\x97\x20\x5D\x2A"); const QByteArray INNER_STREAM_SALSA20_IV("\xE8\x30\x09\x4B\x97\x20\x5D\x2A");

View File

@ -118,7 +118,7 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
hash.addData(m_db->transformedMasterKey()); hash.addData(m_db->transformedMasterKey());
QByteArray finalKey = hash.result(); QByteArray finalKey = hash.result();
SymmetricCipherStream cipherStream(m_device, SymmetricCipher::Aes256, SymmetricCipherStream cipherStream(m_device, SymmetricCipher::cipherToAlgorithm(m_db->cipher()),
SymmetricCipher::Cbc, SymmetricCipher::Decrypt); SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
if (!cipherStream.init(finalKey, m_encryptionIV)) { if (!cipherStream.init(finalKey, m_encryptionIV)) {
raiseError(cipherStream.errorString()); raiseError(cipherStream.errorString());
@ -330,7 +330,7 @@ void KeePass2Reader::setCipher(const QByteArray& data)
else { else {
Uuid uuid(data); Uuid uuid(data);
if (uuid != KeePass2::CIPHER_AES) { if (uuid != KeePass2::CIPHER_AES && uuid != KeePass2::CIPHER_TWOFISH) {
raiseError("Unsupported cipher"); raiseError("Unsupported cipher");
} }
else { else {

View File

@ -87,8 +87,8 @@ void KeePass2Writer::writeDatabase(QIODevice* device, Database* db)
QByteArray headerHash = CryptoHash::hash(header.data(), CryptoHash::Sha256); QByteArray headerHash = CryptoHash::hash(header.data(), CryptoHash::Sha256);
CHECK_RETURN(writeData(header.data())); CHECK_RETURN(writeData(header.data()));
SymmetricCipherStream cipherStream(device, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipherStream cipherStream(device, SymmetricCipher::cipherToAlgorithm(db->cipher()),
SymmetricCipher::Encrypt); SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
cipherStream.init(finalKey, encryptionIV); cipherStream.init(finalKey, encryptionIV);
if (!cipherStream.open(QIODevice::WriteOnly)) { if (!cipherStream.open(QIODevice::WriteOnly)) {
raiseError(cipherStream.errorString()); raiseError(cipherStream.errorString());