keepassxc/tests/TestKeys.cpp
angelsl 2e19af5032
Pull out KDFs into their own class hierarchy
In preparation for multiple KDFs in KDBX 4
2018-01-13 14:23:20 -05:00

223 lines
6.1 KiB
C++

/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestKeys.h"
#include <QBuffer>
#include <QTest>
#include "config-keepassx-tests.h"
#include "core/Database.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/Crypto.h"
#include "crypto/kdf/AesKdf.h"
#include "crypto/CryptoHash.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
QTEST_GUILESS_MAIN(TestKeys)
void TestKeys::initTestCase()
{
QVERIFY(Crypto::init());
}
void TestKeys::testComposite()
{
QScopedPointer<CompositeKey> compositeKey1(new CompositeKey());
QScopedPointer<PasswordKey> passwordKey1(new PasswordKey());
QScopedPointer<PasswordKey> passwordKey2(new PasswordKey("test"));
// make sure that addKey() creates a copy of the keys
compositeKey1->addKey(*passwordKey1);
compositeKey1->addKey(*passwordKey2);
AesKdf kdf;
kdf.setRounds(1);
QByteArray transformed1;
QVERIFY(compositeKey1->transform(kdf, transformed1));
QCOMPARE(transformed1.size(), 32);
// make sure the subkeys are copied
QScopedPointer<CompositeKey> compositeKey2(compositeKey1->clone());
QByteArray transformed2;
QVERIFY(compositeKey2->transform(kdf, transformed2));
QCOMPARE(transformed2.size(), 32);
QCOMPARE(transformed1, transformed2);
QScopedPointer<CompositeKey> compositeKey3(new CompositeKey());
QScopedPointer<CompositeKey> compositeKey4(new CompositeKey());
// test clear()
compositeKey3->addKey(PasswordKey("test"));
compositeKey3->clear();
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
// test assignment operator
compositeKey3->addKey(PasswordKey("123"));
*compositeKey4 = *compositeKey3;
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
// test self-assignment
*compositeKey3 = *compositeKey3;
QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey());
}
void TestKeys::testFileKey()
{
QFETCH(QString, type);
QString name = QString("FileKey").append(type);
KeePass2Reader reader;
QString dbFilename = QString("%1/%2.kdbx").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
QString keyFilename = QString("%1/%2.key").arg(QString(KEEPASSX_TEST_DATA_DIR), name);
CompositeKey compositeKey;
FileKey fileKey;
QVERIFY(fileKey.load(keyFilename));
QCOMPARE(fileKey.rawKey().size(), 32);
compositeKey.addKey(fileKey);
QScopedPointer<Database> db(reader.readDatabase(dbFilename, compositeKey));
QVERIFY(db);
QVERIFY(!reader.hasError());
QCOMPARE(db->metadata()->name(), QString("%1 Database").arg(name));
}
void TestKeys::testFileKey_data()
{
QTest::addColumn<QString>("type");
QTest::newRow("Xml") << QString("Xml");
QTest::newRow("XmlBrokenBase64") << QString("XmlBrokenBase64");
QTest::newRow("Binary") << QString("Binary");
QTest::newRow("Hex") << QString("Hex");
QTest::newRow("Hashed") << QString("Hashed");
}
void TestKeys::testCreateFileKey()
{
QBuffer keyBuffer1;
keyBuffer1.open(QBuffer::ReadWrite);
FileKey::create(&keyBuffer1, 128);
QCOMPARE(keyBuffer1.size(), 128);
QBuffer keyBuffer2;
keyBuffer2.open(QBuffer::ReadWrite);
FileKey::create(&keyBuffer2, 64);
QCOMPARE(keyBuffer2.size(), 64);
}
void TestKeys::testCreateAndOpenFileKey()
{
const QString dbName("testCreateFileKey database");
QBuffer keyBuffer;
keyBuffer.open(QBuffer::ReadWrite);
FileKey::create(&keyBuffer);
keyBuffer.reset();
FileKey fileKey;
QVERIFY(fileKey.load(&keyBuffer));
CompositeKey compositeKey;
compositeKey.addKey(fileKey);
QScopedPointer<Database> dbOrg(new Database());
QVERIFY(dbOrg->setKey(compositeKey));
dbOrg->metadata()->setName(dbName);
QBuffer dbBuffer;
dbBuffer.open(QBuffer::ReadWrite);
KeePass2Writer writer;
writer.writeDatabase(&dbBuffer, dbOrg.data());
dbBuffer.reset();
KeePass2Reader reader;
QScopedPointer<Database> dbRead(reader.readDatabase(&dbBuffer, compositeKey));
QVERIFY(dbRead);
QVERIFY(!reader.hasError());
QCOMPARE(dbRead->metadata()->name(), dbName);
}
void TestKeys::testFileKeyHash()
{
QBuffer keyBuffer;
keyBuffer.open(QBuffer::ReadWrite);
FileKey::create(&keyBuffer);
CryptoHash cryptoHash(CryptoHash::Sha256);
cryptoHash.addData(keyBuffer.data());
FileKey fileKey;
fileKey.load(&keyBuffer);
QCOMPARE(fileKey.rawKey(), cryptoHash.result());
}
void TestKeys::testFileKeyError()
{
bool result;
QString errorMsg;
const QString fileName(QString(KEEPASSX_TEST_DATA_DIR).append("/does/not/exist"));
FileKey fileKey;
result = fileKey.load(fileName, &errorMsg);
QVERIFY(!result);
QVERIFY(!errorMsg.isEmpty());
errorMsg = "";
result = FileKey::create(fileName, &errorMsg);
QVERIFY(!result);
QVERIFY(!errorMsg.isEmpty());
errorMsg = "";
}
void TestKeys::benchmarkTransformKey()
{
QByteArray env = qgetenv("BENCHMARK");
if (env.isEmpty() || env == "0" || env == "no") {
QSKIP("Benchmark skipped. Set env variable BENCHMARK=1 to enable.");
}
PasswordKey pwKey;
pwKey.setPassword("password");
CompositeKey compositeKey;
compositeKey.addKey(pwKey);
QByteArray seed(32, '\x4B');
QByteArray result;
AesKdf kdf;
kdf.setSeed(seed);
kdf.setRounds(1e6);
QBENCHMARK {
Q_UNUSED(compositeKey.transform(kdf, result));
};
}