Skip to content

Commit

Permalink
Fix and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasFella committed Oct 29, 2023
1 parent 9f06d98 commit 1a86e01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 8 additions & 5 deletions Quotient/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void Database::migrateTo6()
qCDebug(DATABASE) << "Migrating database to version 6";
transaction();

execute(QStringLiteral("CREATE TABLE encrypted (name TEXT, cipher TEXT);"));
execute(QStringLiteral("CREATE TABLE encrypted (name TEXT, cipher TEXT, iv TEXT);"));
execute(QStringLiteral("PRAGMA user_version = 6"));
commit();
}
Expand Down Expand Up @@ -490,12 +490,14 @@ QString Database::edKeyForKeyId(const QString& userId, const QString& edKeyId)

void Database::storeEncrypted(const QString& name, const QByteArray& key)
{
auto cipher = aesCtr256Encrypt(key, m_picklingKey.viewAsByteArray().left(32), QByteArray(32, 0)).toBase64();
auto query = prepareQuery(QStringLiteral("INSERT INTO encrypted(name, cipher) VALUES(:name, :cipher);"));
auto iv = getRandom<16>();
auto cipher = aesCtr256Encrypt(key, m_picklingKey.viewAsByteArray().left(32), iv.viewAsByteArray()).toBase64();
auto query = prepareQuery(QStringLiteral("INSERT INTO encrypted(name, cipher, iv) VALUES(:name, :cipher, :iv);"));
auto deleteQuery = prepareQuery(QStringLiteral("DELETE FROM encrypted WHERE name=:name;"));
deleteQuery.bindValue(":name"_ls, name);
query.bindValue(":name"_ls, name);
query.bindValue(":cipher"_ls, cipher);
query.bindValue(":iv"_ls, iv.viewAsByteArray().toBase64());
transaction();
execute(deleteQuery);
execute(query);
Expand All @@ -504,12 +506,13 @@ void Database::storeEncrypted(const QString& name, const QByteArray& key)

QByteArray Database::loadEncrypted(const QString& name)
{
auto query = prepareQuery("SELECT cipher FROM encrypted WHERE name=:name;"_ls);
auto query = prepareQuery("SELECT cipher, iv FROM encrypted WHERE name=:name;"_ls);
query.bindValue(":name"_ls, name);
execute(query);
if (!query.next()) {
return {};
}
auto cipher = QByteArray::fromBase64(query.value("cipher"_ls).toString().toLatin1());
return aesCtr256Decrypt(cipher, m_picklingKey.viewAsByteArray().left(32), QByteArray(32, 0));
auto iv = QByteArray::fromBase64(query.value("iv"_ls).toString().toLatin1());
return aesCtr256Decrypt(cipher, m_picklingKey.viewAsByteArray().left(32), iv);
}
15 changes: 14 additions & 1 deletion autotests/testcryptoutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <Quotient/connection.h>
#include <Quotient/database.h>
#include <Quotient/e2ee/cryptoutils.h>
#include <Quotient/e2ee/e2ee_common.h>

Expand All @@ -22,6 +24,7 @@ private slots:
void hmac();
void curve25519AesEncryptDecrypt();
void decodeBase58();
void testEncrypted();
};

namespace {
Expand Down Expand Up @@ -91,5 +94,15 @@ void TestCryptoUtils::decodeBase58()
QCOMPARE(base58Decode(QByteArrayLiteral("ABCDEFabcdef")).toBase64(), QByteArrayLiteral("DG3GmkxFR1TQ"));
}

QTEST_APPLESS_MAIN(TestCryptoUtils)
void TestCryptoUtils::testEncrypted()
{
QByteArray key(32, '\0');
auto text = QByteArrayLiteral("This is a message");
auto connection = Connection::makeMockConnection("@foo:bar.com"_ls, true);
connection->database()->storeEncrypted("testKey"_ls, text);
auto decrypted = connection->database()->loadEncrypted("testKey"_ls);
QCOMPARE(text, decrypted);
}

QTEST_GUILESS_MAIN(TestCryptoUtils)
#include "testcryptoutils.moc"

0 comments on commit 1a86e01

Please sign in to comment.