2010-09-11 13:49:30 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2010-09-11 13:49:30 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-21 17:06:30 -05:00
|
|
|
#include "TestSymmetricCipher.h"
|
2018-01-24 07:22:20 -05:00
|
|
|
#include "TestGlobal.h"
|
2010-11-21 17:06:30 -05:00
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QBuffer>
|
2010-09-11 13:49:30 -04:00
|
|
|
|
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
#include "crypto/SymmetricCipher.h"
|
2010-09-13 17:25:48 -04:00
|
|
|
#include "streams/SymmetricCipherStream.h"
|
2010-09-11 13:49:30 -04:00
|
|
|
|
2014-05-16 06:32:52 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestSymmetricCipher)
|
2018-09-29 13:00:47 -04:00
|
|
|
Q_DECLARE_METATYPE(SymmetricCipher::Algorithm);
|
|
|
|
Q_DECLARE_METATYPE(SymmetricCipher::Mode);
|
|
|
|
Q_DECLARE_METATYPE(SymmetricCipher::Direction);
|
2014-05-16 06:32:52 -04:00
|
|
|
|
2010-09-11 13:49:30 -04:00
|
|
|
void TestSymmetricCipher::initTestCase()
|
|
|
|
{
|
2014-06-15 05:17:40 -04:00
|
|
|
QVERIFY(Crypto::init());
|
2010-09-11 13:49:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testAlgorithmToCipher()
|
2018-02-06 12:20:10 -05:00
|
|
|
{
|
2018-09-29 13:00:47 -04:00
|
|
|
QCOMPARE(SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm::Aes128), KeePass2::CIPHER_AES128);
|
|
|
|
QCOMPARE(SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm::Aes256), KeePass2::CIPHER_AES256);
|
|
|
|
QCOMPARE(SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm::Twofish), KeePass2::CIPHER_TWOFISH);
|
|
|
|
QCOMPARE(SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm::ChaCha20), KeePass2::CIPHER_CHACHA20);
|
|
|
|
QCOMPARE(SymmetricCipher::algorithmToCipher(SymmetricCipher::Algorithm::InvalidAlgorithm), QUuid());
|
2018-02-06 12:20:10 -05:00
|
|
|
}
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
// clang-format off
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testEncryptionDecryption_data()
|
2018-02-06 12:20:10 -05:00
|
|
|
{
|
2018-09-29 13:00:47 -04:00
|
|
|
QTest::addColumn<SymmetricCipher::Algorithm>("algorithm");
|
|
|
|
QTest::addColumn<SymmetricCipher::Mode>("mode");
|
|
|
|
QTest::addColumn<SymmetricCipher::Direction>("direction");
|
|
|
|
QTest::addColumn<QByteArray>("key");
|
|
|
|
QTest::addColumn<QByteArray>("iv");
|
|
|
|
QTest::addColumn<QByteArray>("plainText");
|
|
|
|
QTest::addColumn<QByteArray>("cipherText");
|
2018-02-06 12:20:10 -05:00
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
|
|
|
QTest::newRow("AES128-CBC Encryption")
|
|
|
|
<< SymmetricCipher::Aes128
|
|
|
|
<< SymmetricCipher::Cbc
|
|
|
|
<< SymmetricCipher::Encrypt
|
|
|
|
<< QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
|
|
|
|
<< QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2");
|
|
|
|
|
|
|
|
QTest::newRow("AES128-CBC Decryption")
|
|
|
|
<< SymmetricCipher::Aes128
|
|
|
|
<< SymmetricCipher::Cbc
|
|
|
|
<< SymmetricCipher::Decrypt
|
|
|
|
<< QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51");
|
|
|
|
|
|
|
|
QTest::newRow("AES256-CBC Encryption")
|
|
|
|
<< SymmetricCipher::Aes256
|
|
|
|
<< SymmetricCipher::Cbc
|
|
|
|
<< SymmetricCipher::Encrypt
|
|
|
|
<< QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
|
|
|
|
<< QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d");
|
|
|
|
|
|
|
|
QTest::newRow("AES256-CBC Decryption")
|
|
|
|
<< SymmetricCipher::Aes256
|
|
|
|
<< SymmetricCipher::Cbc
|
|
|
|
<< SymmetricCipher::Decrypt
|
|
|
|
<< QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51");
|
|
|
|
|
|
|
|
QTest::newRow("AES256-CTR Encryption")
|
|
|
|
<< SymmetricCipher::Aes256
|
|
|
|
<< SymmetricCipher::Ctr
|
|
|
|
<< SymmetricCipher::Encrypt
|
|
|
|
<< QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
|
|
|
|
<< QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
|
|
|
|
<< QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5");
|
|
|
|
|
|
|
|
QTest::newRow("AES256-CTR Decryption")
|
|
|
|
<< SymmetricCipher::Aes256
|
|
|
|
<< SymmetricCipher::Ctr
|
|
|
|
<< SymmetricCipher::Decrypt
|
|
|
|
<< QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
|
|
|
|
<< QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
|
|
|
|
<< QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51");
|
2018-02-06 12:20:10 -05:00
|
|
|
}
|
2018-10-31 23:27:38 -04:00
|
|
|
// clang-format on
|
2018-02-06 12:20:10 -05:00
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testEncryptionDecryption()
|
2010-09-11 13:49:30 -04:00
|
|
|
{
|
2018-09-29 13:00:47 -04:00
|
|
|
QFETCH(SymmetricCipher::Algorithm, algorithm);
|
|
|
|
QFETCH(SymmetricCipher::Mode, mode);
|
|
|
|
QFETCH(SymmetricCipher::Direction, direction);
|
|
|
|
QFETCH(QByteArray, key);
|
|
|
|
QFETCH(QByteArray, iv);
|
|
|
|
QFETCH(QByteArray, plainText);
|
|
|
|
QFETCH(QByteArray, cipherText);
|
2010-09-11 13:49:30 -04:00
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
bool ok;
|
2018-09-29 13:00:47 -04:00
|
|
|
SymmetricCipher cipher(algorithm, mode, direction);
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(cipher.init(key, iv));
|
2010-09-13 17:25:48 -04:00
|
|
|
QCOMPARE(cipher.blockSize(), 16);
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(cipher.process(plainText, &ok), cipherText);
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(ok);
|
2010-09-16 12:20:46 -04:00
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
if (mode == SymmetricCipher::Cbc) {
|
|
|
|
QBuffer buffer;
|
|
|
|
SymmetricCipherStream stream(&buffer, algorithm, mode, direction);
|
|
|
|
QVERIFY(stream.init(key, iv));
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
QVERIFY(stream.open(QIODevice::WriteOnly));
|
|
|
|
QVERIFY(stream.reset());
|
|
|
|
|
|
|
|
buffer.reset();
|
|
|
|
buffer.buffer().clear();
|
|
|
|
QCOMPARE(stream.write(plainText.left(16)), qint64(16));
|
|
|
|
QCOMPARE(buffer.data(), cipherText.left(16));
|
|
|
|
QVERIFY(stream.reset());
|
|
|
|
// make sure padding is written
|
|
|
|
QCOMPARE(buffer.data().size(), 32);
|
|
|
|
|
|
|
|
buffer.reset();
|
|
|
|
buffer.buffer().clear();
|
|
|
|
QCOMPARE(stream.write(plainText.left(10)), qint64(10));
|
|
|
|
QVERIFY(buffer.data().isEmpty());
|
|
|
|
|
|
|
|
QVERIFY(stream.reset());
|
|
|
|
buffer.reset();
|
|
|
|
buffer.buffer().clear();
|
|
|
|
QCOMPARE(stream.write(plainText.left(10)), qint64(10));
|
|
|
|
stream.close();
|
|
|
|
QCOMPARE(buffer.data().size(), 16);
|
|
|
|
}
|
2010-09-13 17:25:48 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testAesCbcPadding_data()
|
2010-09-13 17:25:48 -04:00
|
|
|
{
|
2018-09-29 13:00:47 -04:00
|
|
|
QTest::addColumn<QByteArray>("key");
|
|
|
|
QTest::addColumn<QByteArray>("iv");
|
|
|
|
QTest::addColumn<QByteArray>("cipherText");
|
|
|
|
QTest::addColumn<QByteArray>("plainText");
|
|
|
|
QTest::addColumn<QByteArray>("padding");
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QTest::newRow("AES128") << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
|
|
|
|
<< QByteArray::fromHex("55e21d7100b988ffec32feeafaf23538");
|
|
|
|
|
|
|
|
QTest::newRow("AES256") << QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4")
|
|
|
|
<< QByteArray::fromHex("000102030405060708090a0b0c0d0e0f")
|
|
|
|
<< QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51")
|
|
|
|
<< QByteArray::fromHex("3a3aa5e0213db1a9901f9036cf5102d2");
|
2018-09-29 13:00:47 -04:00
|
|
|
}
|
2010-09-13 17:25:48 -04:00
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testAesCbcPadding()
|
|
|
|
{
|
|
|
|
QFETCH(QByteArray, key);
|
|
|
|
QFETCH(QByteArray, iv);
|
|
|
|
QFETCH(QByteArray, cipherText);
|
|
|
|
QFETCH(QByteArray, plainText);
|
|
|
|
QFETCH(QByteArray, padding);
|
2010-09-13 17:25:48 -04:00
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
// padded with 16 0x10 bytes
|
|
|
|
QByteArray cipherTextPadded = cipherText + padding;
|
2010-09-13 17:25:48 -04:00
|
|
|
|
2012-05-08 15:33:21 -04:00
|
|
|
QBuffer buffer(&cipherTextPadded);
|
2018-09-29 13:00:47 -04:00
|
|
|
SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes128, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(stream.init(key, iv));
|
2010-09-13 17:25:48 -04:00
|
|
|
buffer.open(QIODevice::ReadOnly);
|
2015-05-09 14:11:20 -04:00
|
|
|
QVERIFY(stream.open(QIODevice::ReadOnly));
|
2010-09-13 17:25:48 -04:00
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(stream.read(10), plainText.left(10));
|
2010-09-13 17:25:48 -04:00
|
|
|
buffer.reset();
|
2015-05-09 14:11:20 -04:00
|
|
|
QVERIFY(stream.reset());
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(stream.read(20), plainText.left(20));
|
2010-09-13 17:25:48 -04:00
|
|
|
buffer.reset();
|
2015-05-09 14:11:20 -04:00
|
|
|
QVERIFY(stream.reset());
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(stream.read(16), plainText.left(16));
|
2010-09-13 17:25:48 -04:00
|
|
|
buffer.reset();
|
2015-05-09 14:11:20 -04:00
|
|
|
QVERIFY(stream.reset());
|
2018-03-31 16:01:30 -04:00
|
|
|
QCOMPARE(stream.read(100), plainText);
|
2010-09-11 13:49:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testInplaceEcb_data()
|
2017-10-29 11:07:01 -04:00
|
|
|
{
|
2018-09-29 13:00:47 -04:00
|
|
|
QTest::addColumn<QByteArray>("key");
|
|
|
|
QTest::addColumn<QByteArray>("plainText");
|
|
|
|
QTest::addColumn<QByteArray>("cipherText");
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QTest::newRow("AES128") << QByteArray::fromHex("2b7e151628aed2a6abf7158809cf4f3c")
|
|
|
|
<< QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a")
|
|
|
|
<< QByteArray::fromHex("3ad77bb40d7a3660a89ecaf32466ef97");
|
2017-10-29 11:07:01 -04:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
void TestSymmetricCipher::testInplaceEcb()
|
2017-10-29 11:07:01 -04:00
|
|
|
{
|
2018-10-31 23:27:38 -04:00
|
|
|
QFETCH(QByteArray, key);
|
|
|
|
QFETCH(QByteArray, plainText);
|
|
|
|
QFETCH(QByteArray, cipherText);
|
2018-09-29 13:00:47 -04:00
|
|
|
|
|
|
|
SymmetricCipher cipherInPlaceEnc(SymmetricCipher::Aes128, SymmetricCipher::Ecb, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipherInPlaceEnc.init(key, QByteArray(16, 0)));
|
|
|
|
QCOMPARE(cipherInPlaceEnc.blockSize(), 16);
|
|
|
|
auto data = QByteArray(plainText);
|
|
|
|
QVERIFY(cipherInPlaceEnc.processInPlace(data));
|
|
|
|
QCOMPARE(data, cipherText);
|
|
|
|
|
|
|
|
SymmetricCipher cipherInPlaceDec(SymmetricCipher::Aes128, SymmetricCipher::Ecb, SymmetricCipher::Decrypt);
|
|
|
|
QVERIFY(cipherInPlaceDec.init(key, QByteArray(16, 0)));
|
|
|
|
QCOMPARE(cipherInPlaceDec.blockSize(), 16);
|
|
|
|
QVERIFY(cipherInPlaceDec.processInPlace(data));
|
|
|
|
QCOMPARE(data, plainText);
|
|
|
|
|
|
|
|
SymmetricCipher cipherInPlaceEnc2(SymmetricCipher::Aes128, SymmetricCipher::Ecb, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipherInPlaceEnc2.init(key, QByteArray(16, 0)));
|
|
|
|
QCOMPARE(cipherInPlaceEnc2.blockSize(), 16);
|
|
|
|
data = QByteArray(plainText);
|
|
|
|
QVERIFY(cipherInPlaceEnc2.processInPlace(data, 100));
|
|
|
|
|
|
|
|
SymmetricCipher cipherInPlaceDec2(SymmetricCipher::Aes128, SymmetricCipher::Ecb, SymmetricCipher::Decrypt);
|
|
|
|
QVERIFY(cipherInPlaceDec2.init(key, QByteArray(16, 0)));
|
|
|
|
QCOMPARE(cipherInPlaceDec2.blockSize(), 16);
|
|
|
|
QVERIFY(cipherInPlaceDec2.processInPlace(data, 100));
|
|
|
|
QCOMPARE(data, plainText);
|
2017-10-29 11:07:01 -04:00
|
|
|
}
|
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
void TestSymmetricCipher::testTwofish256CbcEncryption()
|
|
|
|
{
|
|
|
|
// NIST MCT Known-Answer Tests (cbc_e_m.txt)
|
|
|
|
// https://www.schneier.com/code/twofish-kat.zip
|
2018-03-31 16:01:30 -04:00
|
|
|
|
|
|
|
QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3EA7162E65490B03B1AE4871FB35EF23B"),
|
|
|
|
QByteArray::fromHex("8D55E4849A4DED08D89881E6708EDD26BEEE942073DFB3790B2798B240ACD74A"),
|
|
|
|
QByteArray::fromHex("606EFDC2066A837AF0430EBE4CF1F21071CCB236C33B4B9D82404FDB05C74621"),
|
|
|
|
QByteArray::fromHex("B119AA9485CEEEB4CC778AF21121E54DE4BDBA3498C61C8FD9004AA0C71909C3")};
|
|
|
|
QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
|
|
|
|
QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
|
|
|
|
QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
|
|
|
|
QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2")};
|
|
|
|
QVector<QByteArray> plainTexts{QByteArray::fromHex("00000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("D0A260EB41755B19374BABF259A79DB3"),
|
|
|
|
QByteArray::fromHex("5DF7846FDB38B611EFD32A1429294095"),
|
|
|
|
QByteArray::fromHex("ED3B19469C276E7228DB8F583C7F2F36"),
|
|
|
|
QByteArray::fromHex("D177575683A46DCE3C34844C5DD0175D")};
|
|
|
|
QVector<QByteArray> cipherTexts{QByteArray::fromHex("EA7162E65490B03B1AE4871FB35EF23B"),
|
|
|
|
QByteArray::fromHex("549FF6C6274F034211C31FADF3F22571"),
|
|
|
|
QByteArray::fromHex("CF222616B0E4F8E48967D769456B916B"),
|
|
|
|
QByteArray::fromHex("957108025BFD57125B40057BC2DE4FE2"),
|
|
|
|
QByteArray::fromHex("6F725C5950133F82EF021A94CADC8508")};
|
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
SymmetricCipher cipher(SymmetricCipher::Twofish, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
|
|
|
|
bool ok;
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
for (int i = 0; i < keys.size(); ++i) {
|
2017-11-27 15:41:58 -05:00
|
|
|
QVERIFY(cipher.init(keys[i], ivs[i]));
|
2017-01-15 13:12:23 -05:00
|
|
|
QByteArray ptNext = plainTexts[i];
|
|
|
|
QByteArray ctPrev = ivs[i];
|
|
|
|
QByteArray ctCur;
|
|
|
|
QCOMPARE(cipher.blockSize(), 16);
|
|
|
|
for (int j = 0; j < 5000; ++j) {
|
|
|
|
ctCur = cipher.process(ptNext, &ok);
|
2020-01-07 22:06:31 -05:00
|
|
|
if (!ok) {
|
2017-01-15 13:36:00 -05:00
|
|
|
break;
|
2020-01-07 22:06:31 -05:00
|
|
|
}
|
2017-01-15 13:12:23 -05:00
|
|
|
ptNext = ctPrev;
|
|
|
|
ctPrev = ctCur;
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
ctCur = cipher.process(ptNext, &ok);
|
2020-01-07 22:06:31 -05:00
|
|
|
if (!ok) {
|
2017-01-15 13:36:00 -05:00
|
|
|
break;
|
2020-01-07 22:06:31 -05:00
|
|
|
}
|
2017-01-15 13:12:23 -05:00
|
|
|
ptNext = ctPrev;
|
|
|
|
ctPrev = ctCur;
|
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
QCOMPARE(ctCur, cipherTexts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSymmetricCipher::testTwofish256CbcDecryption()
|
|
|
|
{
|
|
|
|
// NIST MCT Known-Answer Tests (cbc_d_m.txt)
|
|
|
|
// https://www.schneier.com/code/twofish-kat.zip
|
2018-03-31 16:01:30 -04:00
|
|
|
|
|
|
|
QVector<QByteArray> keys{QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A942CBA6271A1044F90C30BA8FE91E1C163"),
|
|
|
|
QByteArray::fromHex("EBA31FF8D2A24FDD769A937353E23257294A33394E4D17A668060AD8230811A1"),
|
|
|
|
QByteArray::fromHex("1DCF1915C389AB273F80F897BF008F058ED89F58A95C1BE523C4B11295ED2D0F"),
|
|
|
|
QByteArray::fromHex("491B9A66D3ED4EF19F02180289D5B1A1C2596AE568540A95DC5244198A9B8869")};
|
|
|
|
QVector<QByteArray> ivs{QByteArray::fromHex("00000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("1B1FE8F5A911CD4C0D800EDCE8ED0A94"),
|
|
|
|
QByteArray::fromHex("F0BCF70D7BB382917B1A9DAFBB0F38C3"),
|
|
|
|
QByteArray::fromHex("F66C06ED112BE4FA491A6BE4ECE2BD52"),
|
|
|
|
QByteArray::fromHex("54D483731064E5D6A082E09536D53EA4")};
|
|
|
|
QVector<QByteArray> plainTexts{QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
|
|
|
|
QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
|
|
|
|
QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
|
|
|
|
QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566"),
|
|
|
|
QByteArray::fromHex("BD959F5B787037631A37051EA5F369F8")};
|
|
|
|
QVector<QByteArray> cipherTexts{QByteArray::fromHex("00000000000000000000000000000000"),
|
|
|
|
QByteArray::fromHex("2CBA6271A1044F90C30BA8FE91E1C163"),
|
|
|
|
QByteArray::fromHex("05F05148EF495836AB0DA226B2E9D0C2"),
|
|
|
|
QByteArray::fromHex("A792AC61E7110C434BC2BBCAB6E53CAE"),
|
|
|
|
QByteArray::fromHex("4C81F5BDC1081170FF96F50B1F76A566")};
|
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
SymmetricCipher cipher(SymmetricCipher::Twofish, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
|
|
|
|
bool ok;
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
for (int i = 0; i < keys.size(); ++i) {
|
|
|
|
cipher.init(keys[i], ivs[i]);
|
|
|
|
QByteArray ctNext = cipherTexts[i];
|
|
|
|
QByteArray ptCur;
|
|
|
|
QCOMPARE(cipher.blockSize(), 16);
|
|
|
|
for (int j = 0; j < 5000; ++j) {
|
|
|
|
ptCur = cipher.process(ctNext, &ok);
|
2020-01-07 22:06:31 -05:00
|
|
|
if (!ok) {
|
2017-01-15 13:36:00 -05:00
|
|
|
break;
|
2020-01-07 22:06:31 -05:00
|
|
|
}
|
2017-01-15 13:12:23 -05:00
|
|
|
ctNext = ptCur;
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
ptCur = cipher.process(ctNext, &ok);
|
2020-01-07 22:06:31 -05:00
|
|
|
if (!ok) {
|
2017-01-15 13:36:00 -05:00
|
|
|
break;
|
2020-01-07 22:06:31 -05:00
|
|
|
}
|
2017-01-15 13:12:23 -05:00
|
|
|
ctNext = ptCur;
|
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-01-15 13:12:23 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
QCOMPARE(ptCur, plainTexts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-21 10:19:27 -05:00
|
|
|
void TestSymmetricCipher::testSalsa20()
|
|
|
|
{
|
|
|
|
// http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup
|
|
|
|
|
|
|
|
QByteArray key = QByteArray::fromHex("F3F4F5F6F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E0F101112");
|
|
|
|
QByteArray iv = QByteArray::fromHex("0000000000000000");
|
2015-05-09 13:47:53 -04:00
|
|
|
bool ok;
|
2010-11-21 10:19:27 -05:00
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipher.init(key, iv));
|
2010-11-21 10:19:27 -05:00
|
|
|
|
|
|
|
QByteArray cipherTextA;
|
2012-04-18 16:08:22 -04:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2015-05-09 13:47:53 -04:00
|
|
|
cipherTextA.append(cipher.process(QByteArray(64, '\0'), &ok));
|
|
|
|
QVERIFY(ok);
|
2010-11-21 10:19:27 -05:00
|
|
|
}
|
|
|
|
cipher.reset();
|
|
|
|
|
2015-05-09 13:47:53 -04:00
|
|
|
QByteArray cipherTextB = cipher.process(QByteArray(512, '\0'), &ok);
|
|
|
|
QVERIFY(ok);
|
2010-11-21 10:19:27 -05:00
|
|
|
cipher.reset();
|
|
|
|
|
|
|
|
QByteArray expectedCipherText1;
|
|
|
|
expectedCipherText1.append(QByteArray::fromHex("B4C0AFA503BE7FC29A62058166D56F8F"));
|
|
|
|
expectedCipherText1.append(QByteArray::fromHex("5D27DC246F75B9AD8760C8C39DFD8749"));
|
|
|
|
expectedCipherText1.append(QByteArray::fromHex("2D3B76D5D9637F009EADA14458A52DFB"));
|
|
|
|
expectedCipherText1.append(QByteArray::fromHex("09815337E72672681DDDC24633750D83"));
|
|
|
|
|
|
|
|
QByteArray expectedCipherText2;
|
|
|
|
expectedCipherText2.append(QByteArray::fromHex("DBBA0683DF48C335A9802EEF02522563"));
|
|
|
|
expectedCipherText2.append(QByteArray::fromHex("54C9F763C3FDE19131A6BB7B85040624"));
|
|
|
|
expectedCipherText2.append(QByteArray::fromHex("B1D6CD4BF66D16F7482236C8602A6D58"));
|
|
|
|
expectedCipherText2.append(QByteArray::fromHex("505EEDCCA0B77AED574AB583115124B9"));
|
|
|
|
|
|
|
|
QByteArray expectedCipherText3;
|
|
|
|
expectedCipherText3.append(QByteArray::fromHex("F0C5F98BAE05E019764EF6B65E0694A9"));
|
|
|
|
expectedCipherText3.append(QByteArray::fromHex("04CB9EC9C10C297B1AB1A6052365BB78"));
|
|
|
|
expectedCipherText3.append(QByteArray::fromHex("E55D3C6CB9F06184BA7D425A92E7E987"));
|
|
|
|
expectedCipherText3.append(QByteArray::fromHex("757FC5D9AFD7082418DD64125CA6F2B6"));
|
|
|
|
|
|
|
|
QByteArray expectedCipherText4;
|
|
|
|
expectedCipherText4.append(QByteArray::fromHex("5A5FB5C8F0AFEA471F0318A4A2792F7A"));
|
|
|
|
expectedCipherText4.append(QByteArray::fromHex("A5C67B6D6E0F0DDB79961C34E3A564BA"));
|
|
|
|
expectedCipherText4.append(QByteArray::fromHex("2EECE78D9AFF45E510FEAB1030B102D3"));
|
|
|
|
expectedCipherText4.append(QByteArray::fromHex("9DFCECB77F5798F7D2793C0AB09C7A04"));
|
|
|
|
|
|
|
|
QCOMPARE(cipherTextA.mid(0, 64), expectedCipherText1);
|
|
|
|
QCOMPARE(cipherTextA.mid(192, 64), expectedCipherText2);
|
|
|
|
QCOMPARE(cipherTextA.mid(256, 64), expectedCipherText3);
|
|
|
|
QCOMPARE(cipherTextA.mid(448, 64), expectedCipherText4);
|
|
|
|
|
|
|
|
QCOMPARE(cipherTextB.mid(0, 64), expectedCipherText1);
|
|
|
|
QCOMPARE(cipherTextB.mid(192, 64), expectedCipherText2);
|
|
|
|
QCOMPARE(cipherTextB.mid(256, 64), expectedCipherText3);
|
|
|
|
QCOMPARE(cipherTextB.mid(448, 64), expectedCipherText4);
|
|
|
|
}
|
|
|
|
|
2017-11-12 13:23:01 -05:00
|
|
|
void TestSymmetricCipher::testChaCha20()
|
|
|
|
{
|
|
|
|
// https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
QByteArray iv = QByteArray::fromHex("0000000000000000");
|
|
|
|
SymmetricCipher cipher(SymmetricCipher::ChaCha20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipher.init(key, iv));
|
|
|
|
QCOMPARE(cipher.process(QByteArray(64, 0), &ok),
|
2018-03-31 16:01:30 -04:00
|
|
|
QByteArray::fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7"
|
|
|
|
"724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"));
|
2017-11-12 13:23:01 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000001");
|
|
|
|
QByteArray iv = QByteArray::fromHex("0000000000000000");
|
|
|
|
SymmetricCipher cipher(SymmetricCipher::ChaCha20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipher.init(key, iv));
|
|
|
|
QCOMPARE(cipher.process(QByteArray(64, 0), &ok),
|
2018-03-31 16:01:30 -04:00
|
|
|
QByteArray::fromHex("4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a"
|
|
|
|
"5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963"));
|
2017-11-12 13:23:01 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
QByteArray iv = QByteArray::fromHex("0000000000000001");
|
|
|
|
SymmetricCipher cipher(SymmetricCipher::ChaCha20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipher.init(key, iv));
|
|
|
|
QCOMPARE(cipher.process(QByteArray(60, 0), &ok),
|
2018-03-31 16:01:30 -04:00
|
|
|
QByteArray::fromHex("de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a708452"
|
|
|
|
"7214f73efc7fa5b5277062eb7a0433e445f41e3"));
|
2017-11-12 13:23:01 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
QByteArray iv = QByteArray::fromHex("0100000000000000");
|
|
|
|
SymmetricCipher cipher(SymmetricCipher::ChaCha20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
|
|
|
|
QVERIFY(cipher.init(key, iv));
|
|
|
|
QCOMPARE(cipher.process(QByteArray(64, 0), &ok),
|
2018-03-31 16:01:30 -04:00
|
|
|
QByteArray::fromHex("ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca"
|
|
|
|
"8ad6426194a88545ddc497a0b466e7d6bbdb0041b2f586b"));
|
2017-11-12 13:23:01 -05:00
|
|
|
QVERIFY(ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:33:21 -04:00
|
|
|
void TestSymmetricCipher::testPadding()
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
|
|
|
|
QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
|
|
|
|
QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d");
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
SymmetricCipherStream streamEnc(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(streamEnc.init(key, iv));
|
2012-05-08 15:33:21 -04:00
|
|
|
streamEnc.open(QIODevice::WriteOnly);
|
|
|
|
streamEnc.write(plainText);
|
|
|
|
streamEnc.close();
|
|
|
|
buffer.reset();
|
|
|
|
// make sure padding is written
|
|
|
|
QCOMPARE(buffer.buffer().size(), 16);
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
SymmetricCipherStream streamDec(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
|
2015-05-09 13:47:53 -04:00
|
|
|
QVERIFY(streamDec.init(key, iv));
|
2012-05-08 15:33:21 -04:00
|
|
|
streamDec.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray decrypted = streamDec.readAll();
|
|
|
|
QCOMPARE(decrypted, plainText);
|
|
|
|
}
|
2015-07-20 15:50:39 -04:00
|
|
|
|
|
|
|
void TestSymmetricCipher::testStreamReset()
|
|
|
|
{
|
|
|
|
QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
|
|
|
|
QByteArray iv = QByteArray::fromHex("000102030405060708090a0b0c0d0e0f");
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
QVERIFY(buffer.open(QIODevice::WriteOnly));
|
2018-03-31 16:01:30 -04:00
|
|
|
SymmetricCipherStream writer(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
|
2015-07-20 15:50:39 -04:00
|
|
|
QVERIFY(writer.init(key, iv));
|
|
|
|
QVERIFY(writer.open(QIODevice::WriteOnly));
|
|
|
|
QCOMPARE(writer.write(QByteArray(4, 'Z')), qint64(4));
|
|
|
|
// test if reset() and close() write only one block
|
|
|
|
QVERIFY(writer.reset());
|
|
|
|
QVERIFY(writer.reset());
|
|
|
|
writer.close();
|
|
|
|
QCOMPARE(buffer.buffer().size(), 16);
|
|
|
|
}
|