2017-07-03 09:30:10 +02:00
|
|
|
/*!
|
2018-03-28 15:10:51 +02:00
|
|
|
* \copyright Copyright (c) 2014-2018 Governikus GmbH & Co. KG, Germany
|
2017-07-03 09:30:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asn1/KnownOIDs.h"
|
|
|
|
#include "pace/KeyDerivationFunction.h"
|
|
|
|
#include "pace/SymmetricCipher.h"
|
|
|
|
|
2017-07-03 09:33:28 +02:00
|
|
|
#include <openssl/evp.h>
|
2017-09-15 10:23:30 +02:00
|
|
|
#include <QtTest>
|
2017-07-03 09:30:10 +02:00
|
|
|
|
|
|
|
using namespace governikus;
|
|
|
|
|
|
|
|
class test_SymmetricCipher
|
|
|
|
: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QByteArray DATA = QByteArray::fromRawData("Moin Moin Anton!", 16);
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void unknownAlgorithm()
|
|
|
|
{
|
|
|
|
QByteArray paceAlgo("unknown-algorithm");
|
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
|
|
|
|
QVERIFY(!sc.isInitialized());
|
|
|
|
QVERIFY(sc.encrypt(QByteArray()).isEmpty());
|
|
|
|
QVERIFY(sc.decrypt(QByteArray()).isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tripleDes()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_3DES_CBC_CBC);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QVERIFY(!sc.isInitialized());
|
|
|
|
QVERIFY(sc.encrypt(QByteArray()).isEmpty());
|
|
|
|
QVERIFY(sc.decrypt(QByteArray()).isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wrongKeySize()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_128);
|
2017-07-03 09:30:10 +02:00
|
|
|
QByteArray key("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QVERIFY(!sc.isInitialized());
|
|
|
|
QVERIFY(sc.encrypt(QByteArray()).isEmpty());
|
|
|
|
QVERIFY(sc.decrypt(QByteArray()).isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void noData()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_128);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QVERIFY(sc.encrypt(QByteArray()).isEmpty());
|
|
|
|
QVERIFY(sc.decrypt(QByteArray()).isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void aes128()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_128);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QByteArray encryptedData = sc.encrypt(DATA);
|
|
|
|
QByteArray decryptedData = sc.decrypt(encryptedData);
|
|
|
|
|
|
|
|
QVERIFY(sc.isInitialized());
|
|
|
|
QCOMPARE(decryptedData, DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void aes196()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_192);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QByteArray encryptedData = sc.encrypt(DATA);
|
|
|
|
QByteArray decryptedData = sc.decrypt(encryptedData);
|
|
|
|
|
|
|
|
QVERIFY(sc.isInitialized());
|
|
|
|
QCOMPARE(decryptedData, DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void aes256()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_256);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QByteArray encryptedData = sc.encrypt(DATA);
|
|
|
|
QByteArray decryptedData = sc.decrypt(encryptedData);
|
|
|
|
|
|
|
|
QVERIFY(sc.isInitialized());
|
|
|
|
QCOMPARE(decryptedData, DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void multipleuse()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_256);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QCOMPARE(sc.decrypt(sc.encrypt(DATA)), DATA);
|
|
|
|
QCOMPARE(sc.decrypt(sc.encrypt(DATA)), DATA);
|
|
|
|
QCOMPARE(sc.decrypt(sc.encrypt(DATA)), DATA);
|
|
|
|
QCOMPARE(sc.decrypt(sc.encrypt(DATA)), DATA);
|
|
|
|
QCOMPARE(sc.decrypt(sc.encrypt(DATA)), DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setIv()
|
|
|
|
{
|
2017-12-20 14:54:05 +01:00
|
|
|
QByteArray paceAlgo = toByteArray(KnownOIDs::id_PACE::ECDH::GM_AES_CBC_CMAC_256);
|
2017-07-03 09:30:10 +02:00
|
|
|
KeyDerivationFunction kdf(paceAlgo);
|
|
|
|
QByteArray key = kdf.pi("123456");
|
|
|
|
SymmetricCipher sc(paceAlgo, key);
|
|
|
|
|
|
|
|
QByteArray encryptedData = sc.encrypt(DATA);
|
|
|
|
|
|
|
|
sc.setIv(QByteArray().fill(1, 16));
|
|
|
|
QByteArray decryptedData_withIv1 = sc.decrypt(encryptedData);
|
|
|
|
|
|
|
|
sc.setIv(QByteArray().fill(0, 16));
|
|
|
|
QByteArray decryptedData_withIv0 = sc.decrypt(encryptedData);
|
|
|
|
|
|
|
|
QCOMPARE(decryptedData_withIv0, DATA);
|
|
|
|
QVERIFY(decryptedData_withIv1 != DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(test_SymmetricCipher)
|
|
|
|
#include "test_SymmetricCipher.moc"
|