Skip to content

Commit 5d76cbc

Browse files
committed
Added testing of more ciphers and kdf's
1 parent 180c1af commit 5d76cbc

File tree

2 files changed

+178
-37
lines changed

2 files changed

+178
-37
lines changed

src/encryptionengine_crypto.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,18 @@ bool EncryptionEngine::performStandardDecryption(EVP_CIPHER_CTX* ctx, const EVP_
170170
}
171171

172172
bool EncryptionEngine::performAuthenticatedEncryption(EVP_CIPHER_CTX* ctx, const EVP_CIPHER* cipher, const QByteArray& key, const QByteArray& iv, QFile& inputFile, QFile& outputFile) {
173-
QByteArray tag(16, 0); // GCM mode requires a 16-byte tag
173+
int cipherMode = EVP_CIPHER_mode(cipher);
174+
QByteArray tag;
175+
bool isAuthenticatedMode = false;
176+
177+
if (cipherMode == EVP_CIPH_GCM_MODE || cipherMode == EVP_CIPH_CCM_MODE ||
178+
EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305) {
179+
tag.resize(16);
180+
isAuthenticatedMode = true;
181+
qDebug() << "Authenticated mode detected:" << EVP_CIPHER_name(cipher);
182+
} else {
183+
qDebug() << "Non-authenticated mode detected:" << EVP_CIPHER_name(cipher);
184+
}
174185

175186
// Initialize encryption operation
176187
if (!EVP_EncryptInit_ex(ctx, cipher, nullptr, reinterpret_cast<const unsigned char*>(key.data()), reinterpret_cast<const unsigned char*>(iv.data()))) {
@@ -219,11 +230,29 @@ bool EncryptionEngine::performAuthenticatedEncryption(EVP_CIPHER_CTX* ctx, const
219230
// Append the tag to the end of the file
220231
outputFile.write(tag);
221232

233+
// At the end of the function
234+
qDebug() << "Encryption completed successfully";
235+
qDebug() << "Encrypted file size:" << outputFile.size() << "bytes";
236+
if (isAuthenticatedMode) {
237+
qDebug() << "Authentication tag:" << tag.toHex();
238+
}
239+
222240
return true;
223241
}
224242

225243
bool EncryptionEngine::performAuthenticatedDecryption(EVP_CIPHER_CTX* ctx, const EVP_CIPHER* cipher, const QByteArray& key, const QByteArray& iv, QFile& inputFile, QFile& outputFile) {
226-
QByteArray tag(16, 0); // GCM mode requires a 16-byte tag
244+
int cipherMode = EVP_CIPHER_mode(cipher);
245+
QByteArray tag;
246+
bool isAuthenticatedMode = false;
247+
248+
if (cipherMode == EVP_CIPH_GCM_MODE || cipherMode == EVP_CIPH_CCM_MODE ||
249+
EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305) {
250+
tag.resize(16);
251+
isAuthenticatedMode = true;
252+
qDebug() << "Authenticated mode detected:" << EVP_CIPHER_name(cipher);
253+
} else {
254+
qDebug() << "Non-authenticated mode detected:" << EVP_CIPHER_name(cipher);
255+
}
227256

228257
// Read the entire encrypted content
229258
QByteArray encryptedContent = inputFile.readAll();

tests/test_encryption_app.cpp

Lines changed: 147 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QFileInfo>
77
#include <QMessageBox>
88
#include <QListWidget>
9+
#include <QComboBox>
910
#include "mainwindow.h"
1011
#include <QTimer>
1112
#include <QWindow>
@@ -15,16 +16,37 @@ class TestOpenCryptUI : public QObject
1516
Q_OBJECT
1617

1718
private slots:
19+
void initTestCase();
1820
void testEncryptDecrypt();
1921
void testEncryptDecryptWithKeyfile();
22+
void testAllCiphersAndKDFs();
23+
void cleanupTestCase();
2024
void closeMessageBoxes();
2125

2226
private:
2327
QTimer *messageBoxTimer;
28+
MainWindow *mainWindow;
2429
QString createTestFile(const QString &content);
2530
QString createKeyfile(const QString &content);
31+
bool encryptAndDecrypt(const QString &cipher, const QString &kdf, bool useKeyfile);
2632
};
2733

34+
void TestOpenCryptUI::initTestCase()
35+
{
36+
mainWindow = new MainWindow();
37+
mainWindow->show();
38+
39+
messageBoxTimer = new QTimer(this);
40+
connect(messageBoxTimer, &QTimer::timeout, this, &TestOpenCryptUI::closeMessageBoxes);
41+
messageBoxTimer->start(1000);
42+
}
43+
44+
void TestOpenCryptUI::cleanupTestCase()
45+
{
46+
messageBoxTimer->stop();
47+
delete mainWindow;
48+
}
49+
2850
QString TestOpenCryptUI::createTestFile(const QString &content)
2951
{
3052
QString testFilePath = QDir::currentPath() + "/test.txt";
@@ -57,13 +79,10 @@ QString TestOpenCryptUI::createKeyfile(const QString &content)
5779

5880
void TestOpenCryptUI::testEncryptDecrypt()
5981
{
60-
MainWindow mainWindow;
61-
mainWindow.show();
62-
63-
QLineEdit *filePathInput = mainWindow.findChild<QLineEdit*>("filePathLineEdit");
64-
QLineEdit *passwordInput = mainWindow.findChild<QLineEdit*>("filePasswordLineEdit");
65-
QPushButton *encryptButton = mainWindow.findChild<QPushButton*>("fileEncryptButton");
66-
QPushButton *decryptButton = mainWindow.findChild<QPushButton*>("fileDecryptButton");
82+
QLineEdit *filePathInput = mainWindow->findChild<QLineEdit*>("filePathLineEdit");
83+
QLineEdit *passwordInput = mainWindow->findChild<QLineEdit*>("filePasswordLineEdit");
84+
QPushButton *encryptButton = mainWindow->findChild<QPushButton*>("fileEncryptButton");
85+
QPushButton *decryptButton = mainWindow->findChild<QPushButton*>("fileDecryptButton");
6786

6887
QVERIFY(filePathInput);
6988
QVERIFY(passwordInput);
@@ -82,14 +101,8 @@ void TestOpenCryptUI::testEncryptDecrypt()
82101
out << "test";
83102
testFile.close();
84103

85-
// Log the file creation
86104
qDebug() << "Test file created with content 'test' at" << testFilePath;
87105

88-
// Set up for message box handling
89-
messageBoxTimer = new QTimer(this);
90-
connect(messageBoxTimer, &QTimer::timeout, this, &TestOpenCryptUI::closeMessageBoxes);
91-
messageBoxTimer->start(1000);
92-
93106
filePathInput->setText(testFilePath);
94107
passwordInput->setText("testpassword");
95108
QTest::mouseClick(encryptButton, Qt::LeftButton);
@@ -98,7 +111,6 @@ void TestOpenCryptUI::testEncryptDecrypt()
98111

99112
QVERIFY(QFileInfo::exists(encryptedFilePath));
100113

101-
// Log encrypted file content
102114
QFile encryptedFile(encryptedFilePath);
103115
QVERIFY(encryptedFile.open(QIODevice::ReadOnly));
104116
QByteArray encryptedContent = encryptedFile.readAll();
@@ -113,7 +125,6 @@ void TestOpenCryptUI::testEncryptDecrypt()
113125

114126
QVERIFY(QFileInfo::exists(testFilePath));
115127

116-
// Log decrypted file content
117128
QFile decryptedFile(testFilePath);
118129
QVERIFY(decryptedFile.open(QIODevice::ReadOnly | QIODevice::Text));
119130
QTextStream in(&decryptedFile);
@@ -123,20 +134,15 @@ void TestOpenCryptUI::testEncryptDecrypt()
123134

124135
QFile::remove(testFilePath);
125136
QFile::remove(encryptedFilePath);
126-
127-
messageBoxTimer->stop();
128137
}
129138

130139
void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
131140
{
132-
MainWindow mainWindow;
133-
mainWindow.show();
134-
135-
QLineEdit *filePathInput = mainWindow.findChild<QLineEdit*>("filePathLineEdit");
136-
QLineEdit *passwordInput = mainWindow.findChild<QLineEdit*>("filePasswordLineEdit");
137-
QPushButton *encryptButton = mainWindow.findChild<QPushButton*>("fileEncryptButton");
138-
QPushButton *decryptButton = mainWindow.findChild<QPushButton*>("fileDecryptButton");
139-
QListWidget *keyfileListWidget = mainWindow.findChild<QListWidget*>("fileKeyfileListWidget");
141+
QLineEdit *filePathInput = mainWindow->findChild<QLineEdit*>("filePathLineEdit");
142+
QLineEdit *passwordInput = mainWindow->findChild<QLineEdit*>("filePasswordLineEdit");
143+
QPushButton *encryptButton = mainWindow->findChild<QPushButton*>("fileEncryptButton");
144+
QPushButton *decryptButton = mainWindow->findChild<QPushButton*>("fileDecryptButton");
145+
QListWidget *keyfileListWidget = mainWindow->findChild<QListWidget*>("fileKeyfileListWidget");
140146

141147
QVERIFY(filePathInput);
142148
QVERIFY(passwordInput);
@@ -148,11 +154,6 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
148154
QString encryptedFilePath = testFilePath + ".enc";
149155
QString keyfilePath = createKeyfile("secret key content");
150156

151-
// Set up for message box handling
152-
messageBoxTimer = new QTimer(this);
153-
connect(messageBoxTimer, &QTimer::timeout, this, &TestOpenCryptUI::closeMessageBoxes);
154-
messageBoxTimer->start(1000);
155-
156157
// Encryption with keyfile
157158
filePathInput->setText(testFilePath);
158159
passwordInput->setText("testpassword");
@@ -161,7 +162,6 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
161162

162163
QTRY_VERIFY_WITH_TIMEOUT(QFileInfo::exists(encryptedFilePath), 15000);
163164

164-
// Log encrypted file content
165165
QFile encryptedFile(encryptedFilePath);
166166
QVERIFY(encryptedFile.open(QIODevice::ReadOnly));
167167
QByteArray encryptedContent = encryptedFile.readAll();
@@ -171,25 +171,137 @@ void TestOpenCryptUI::testEncryptDecryptWithKeyfile()
171171
// Decryption with keyfile
172172
filePathInput->setText(encryptedFilePath);
173173
passwordInput->setText("testpassword");
174-
// Keyfile should still be in the list
175174
QTest::mouseClick(decryptButton, Qt::LeftButton);
176175

177176
QTRY_VERIFY_WITH_TIMEOUT(QFileInfo::exists(testFilePath), 15000);
178177

179-
// Verify decrypted content
180178
QFile decryptedFile(testFilePath);
181179
QVERIFY(decryptedFile.open(QIODevice::ReadOnly | QIODevice::Text));
182180
QTextStream in(&decryptedFile);
183181
QString content = in.readAll().trimmed();
184182
qDebug() << "Decrypted file content:" << content;
185183
QCOMPARE(content, QString("test with keyfile"));
186184

187-
// Clean up
188185
QFile::remove(testFilePath);
189186
QFile::remove(encryptedFilePath);
190187
QFile::remove(keyfilePath);
188+
}
191189

192-
messageBoxTimer->stop();
190+
bool TestOpenCryptUI::encryptAndDecrypt(const QString &cipher, const QString &kdf, bool useKeyfile)
191+
{
192+
qDebug() << "Starting encryptAndDecrypt test for" << cipher << "with" << kdf << (useKeyfile ? "and keyfile" : "");
193+
194+
QLineEdit *filePathInput = mainWindow->findChild<QLineEdit*>("filePathLineEdit");
195+
QLineEdit *passwordInput = mainWindow->findChild<QLineEdit*>("filePasswordLineEdit");
196+
QPushButton *encryptButton = mainWindow->findChild<QPushButton*>("fileEncryptButton");
197+
QPushButton *decryptButton = mainWindow->findChild<QPushButton*>("fileDecryptButton");
198+
QComboBox *algorithmComboBox = mainWindow->findChild<QComboBox*>("fileAlgorithmComboBox");
199+
QComboBox *kdfComboBox = mainWindow->findChild<QComboBox*>("kdfComboBox");
200+
QListWidget *keyfileListWidget = mainWindow->findChild<QListWidget*>("fileKeyfileListWidget");
201+
202+
if (!filePathInput || !passwordInput || !encryptButton || !decryptButton ||
203+
!algorithmComboBox || !kdfComboBox || !keyfileListWidget) {
204+
qDebug() << "Failed to find all required UI elements";
205+
return false;
206+
}
207+
208+
QString testContent = "Test content for " + cipher + " with " + kdf;
209+
QString testFilePath = createTestFile(testContent);
210+
if (testFilePath.isEmpty()) {
211+
qDebug() << "Failed to create test file";
212+
return false;
213+
}
214+
215+
QString encryptedFilePath = testFilePath + ".enc";
216+
QString keyfilePath;
217+
218+
if (useKeyfile) {
219+
keyfilePath = createKeyfile("Secret key for " + cipher);
220+
if (keyfilePath.isEmpty()) {
221+
qDebug() << "Failed to create keyfile";
222+
return false;
223+
}
224+
keyfileListWidget->addItem(keyfilePath);
225+
qDebug() << "Keyfile created and added to list widget";
226+
} else {
227+
keyfileListWidget->clear();
228+
qDebug() << "Keyfile list widget cleared";
229+
}
230+
231+
// Set up encryption parameters
232+
filePathInput->setText(testFilePath);
233+
passwordInput->setText("testpassword");
234+
algorithmComboBox->setCurrentText(cipher);
235+
kdfComboBox->setCurrentText(kdf);
236+
qDebug() << "Encryption parameters set up";
237+
238+
// Encrypt
239+
qDebug() << "Clicking encrypt button";
240+
QTest::mouseClick(encryptButton, Qt::LeftButton);
241+
242+
if (!QTest::qWaitFor([&]() { return QFileInfo::exists(encryptedFilePath); }, 15000)) {
243+
qDebug() << "Encryption failed or timed out for" << cipher << "with" << kdf;
244+
return false;
245+
}
246+
qDebug() << "Encrypted file created:" << encryptedFilePath;
247+
248+
// Set up decryption parameters
249+
filePathInput->setText(encryptedFilePath);
250+
passwordInput->setText("testpassword");
251+
qDebug() << "Decryption parameters set up";
252+
253+
// Decrypt
254+
qDebug() << "Clicking decrypt button";
255+
QTest::mouseClick(decryptButton, Qt::LeftButton);
256+
257+
if (!QTest::qWaitFor([&]() { return QFileInfo::exists(testFilePath); }, 15000)) {
258+
qDebug() << "Decryption failed or timed out for" << cipher << "with" << kdf;
259+
return false;
260+
}
261+
qDebug() << "Decrypted file created:" << testFilePath;
262+
263+
// Verify decrypted content
264+
QFile decryptedFile(testFilePath);
265+
if (!decryptedFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
266+
qDebug() << "Failed to open decrypted file";
267+
return false;
268+
}
269+
QTextStream in(&decryptedFile);
270+
QString decryptedContent = in.readAll().trimmed();
271+
decryptedFile.close();
272+
273+
if (decryptedContent != testContent) {
274+
qDebug() << "Decrypted content does not match original for" << cipher << "with" << kdf;
275+
qDebug() << "Expected:" << testContent;
276+
qDebug() << "Actual:" << decryptedContent;
277+
return false;
278+
}
279+
qDebug() << "Decrypted content matches original";
280+
281+
qDebug() << "Test for" << cipher << "with" << kdf << (useKeyfile ? "and keyfile" : "") << "PASSED";
282+
283+
// Clean up
284+
QFile::remove(testFilePath);
285+
QFile::remove(encryptedFilePath);
286+
if (useKeyfile) {
287+
QFile::remove(keyfilePath);
288+
}
289+
qDebug() << "Test files cleaned up";
290+
291+
return true;
292+
}
293+
294+
void TestOpenCryptUI::testAllCiphersAndKDFs()
295+
{
296+
QStringList ciphers = {"AES-256-GCM", "ChaCha20-Poly1305", "AES-256-CTR", "AES-256-CBC"};
297+
QStringList kdfs = {"Argon2", "Scrypt", "PBKDF2"};
298+
299+
for (const QString &cipher : ciphers) {
300+
for (const QString &kdf : kdfs) {
301+
QVERIFY2(encryptAndDecrypt(cipher, kdf, false), qPrintable(QString("Failed for %1 with %2 without keyfile").arg(cipher, kdf)));
302+
QVERIFY2(encryptAndDecrypt(cipher, kdf, true), qPrintable(QString("Failed for %1 with %2 with keyfile").arg(cipher, kdf)));
303+
}
304+
}
193305
}
194306

195307
void TestOpenCryptUI::closeMessageBoxes()

0 commit comments

Comments
 (0)