From d508c2dd6887bc1e59ecb7c18e41d60d9ec221d1 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 13 Sep 2010 23:25:48 +0200 Subject: [PATCH] Open a kdbx file in main() and add more SymmetricCipher tests. --- src/main.cpp | 18 ++++++++++-- tests/NewDatabase.kdbx | Bin 0 -> 8350 bytes tests/TestSymmetricCipher.cpp | 52 +++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tests/NewDatabase.kdbx diff --git a/src/main.cpp b/src/main.cpp index 6bd3a7739..9a8d161f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,8 +19,12 @@ #include #include "core/Database.h" +#include "crypto/Crypto.h" +#include "format/KeePass2Reader.h" #include "format/KeePass2XmlReader.h" #include "gui/DatabaseWidget.h" +#include "keys/CompositeKey.h" +#include "keys/PasswordKey.h" #include "../tests/config-keepassx-tests.h" @@ -28,8 +32,18 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - KeePass2XmlReader* reader = new KeePass2XmlReader(); - Database* db = reader->readDatabase(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); + Crypto::init(); + + CompositeKey key; + PasswordKey password; + password.setPassword("a"); + key.addKey(password); + + KeePass2Reader* xreader = new KeePass2Reader(); + Database* db = xreader->readDatabase(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.kdbx"), key); + + //KeePass2XmlReader* reader = new KeePass2XmlReader(); + //Database* db = reader->readDatabase(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); DatabaseWidget dbWidget(db); dbWidget.show(); diff --git a/tests/NewDatabase.kdbx b/tests/NewDatabase.kdbx new file mode 100644 index 0000000000000000000000000000000000000000..4e77724c7e8cfdf22bcd951ccfcaa4ae7f49d460 GIT binary patch literal 8350 zcmV;PAYtDF*`k_f`%AS100IC45CAd3^5(yBLr}h01tDtuTK@wC0000000bZaaDhty z76ZqhAc+=}hbj_{u2zr|*-O)p zJK=Cm$rV(v6YH`D2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Id0O1Wi3fl0o zLc{)oLQGEmmINJxh&98sLzv)lS^27sB{S6iu+|A^F;?zei2JGJR~pEe<=`aD7b%7N z8Nl1)HynlP#`~}Bxe6f1y}wPs%r*_uE%#Gn?c!@<`Zg(CKIsvALqQWH=S3N<{Xo-@ z$*+~o#H2Hjj)n=L|Ce#R9-TDonV#<6To6r7N$1AD<{+0C*RxYF6(wP+DwhE{YFB*0>VOhJ9Am=BSMN%AktkUW?|R=2kLJmrGWmU^fZ{_#O_9s z0Er-C7&F;a?efuPERASuJ|JoBF#`~C!h!PBLUF^x50>*~bg_{Ob z~a1eleCKv@Gt~!j5O`F(=2rDZ*Fc=u< zEyoMUs?FrgDcj@wDq5%^j4Q|`=^4xR!cqwoEFSZ~*_4fUWzQ~RYy>8u9khWyrZxg{ zRd2lu4QyWofTpd%d(&kHZFX*Tc8;qKd*&oD+Y-8NC(ks-j#GJZ%U}vk^1XY6>Rn21 ohWrdUDzo?Q%vq*Rhad$AXZ(jMv{h#2H_=&?N{ucinMVNj7A)%Tn*aa+ literal 0 HcmV?d00001 diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp index ed45c9dbb..17feef1ba 100644 --- a/tests/TestSymmetricCipher.cpp +++ b/tests/TestSymmetricCipher.cpp @@ -15,10 +15,12 @@ * along with this program. If not, see . */ +#include #include #include "crypto/Crypto.h" #include "crypto/SymmetricCipher.h" +#include "streams/SymmetricCipherStream.h" class TestSymmetricCipher : public QObject { @@ -27,6 +29,7 @@ class TestSymmetricCipher : public QObject private Q_SLOTS: void initTestCase(); void testAes256CbcEncryption(); + void testAes256CbcDecryption(); }; void TestSymmetricCipher::initTestCase() @@ -40,11 +43,52 @@ void TestSymmetricCipher::testAes256CbcEncryption() QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f"); - QByteArray plain = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a"); + QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a"); + plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51")); + QByteArray cipherText = QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd6"); + cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d")); - SymmetricCipher encrypt(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt, key, iv); - QCOMPARE(QString(encrypt.process(plain).toHex()), - QString("f58c4c04d6e5f1ba779eabfb5f7bfbd6")); + SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt, key, iv); + QCOMPARE(cipher.blockSize(), 16); + + QCOMPARE(QString(cipher.process(plainText).toHex()), + QString(cipherText.toHex())); +} + +void TestSymmetricCipher::testAes256CbcDecryption() +{ + QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); + QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f"); + QByteArray cipherText = QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd6"); + cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d")); + QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a"); + plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51")); + + SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt, key, iv); + QCOMPARE(cipher.blockSize(), 16); + + QCOMPARE(QString(cipher.process(cipherText).toHex()), + QString(plainText.toHex())); + + QBuffer buffer(&cipherText); + SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt, key, iv); + buffer.open(QIODevice::ReadOnly); + stream.open(QIODevice::ReadOnly); + + QCOMPARE(QString(stream.read(10).toHex()), + QString(plainText.left(10).toHex())); + buffer.reset(); + stream.reset(); + QCOMPARE(QString(stream.read(20).toHex()), + QString(plainText.left(20).toHex())); + buffer.reset(); + stream.reset(); + QCOMPARE(QString(stream.read(16).toHex()), + QString(plainText.left(16).toHex())); + buffer.reset(); + stream.reset(); + QCOMPARE(QString(stream.read(100).toHex()), + QString(plainText.toHex())); } QTEST_MAIN(TestSymmetricCipher);