Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit b83fa3b

Browse files
committed
add code to react to changed key sizes
1 parent e333abf commit b83fa3b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/datasync/datasync.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
TARGET = QtDataSync
22

33
QT = core jsonserializer sql websockets
4-
QTAES_KEYSIZE = 128 #TODO make 256 with next major release
4+
# uncomment the following line to keep data compability to previous versions
5+
#QTAES_KEYSIZE = 128
56

67
include(../3rdparty/vendor/vendor.pri)
78

src/datasync/qtinyaesencryptor.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "qtinyaesencryptor_p.h"
2+
#include "setup_p.h"
23

34
#include <QtCore/QJsonDocument>
45
#include <QtCore/QJsonObject>
@@ -19,11 +20,23 @@ void QTinyAesEncryptor::initialize(Defaults *defaults)
1920
{
2021
_defaults = defaults;
2122
_key = _defaults->settings()->value(QStringLiteral("encryption/key")).toByteArray();
23+
2224
if(_key.isEmpty()) {
2325
QRng secureRng;
2426
secureRng.setSecurityLevel(QRng::HighSecurity);
2527
_key = secureRng.generateRandom(QTinyAes::KEYSIZE);
2628
_defaults->settings()->setValue(QStringLiteral("encryption/key"), _key);
29+
} else if((quint32)_key.size() != QTinyAes::KEYSIZE) { //key size changed -> derive new key from old one
30+
QCryptographicHash hash(QCryptographicHash::Sha3_256);
31+
for(quint32 i = 0; i < QTinyAes::KEYSIZE; i += _key.size())
32+
hash.addData(_key);
33+
_key = hash.result();
34+
_key.resize(QTinyAes::KEYSIZE);
35+
_defaults->settings()->setValue(QStringLiteral("encryption/key"), _key);
36+
37+
//trigger a resync to get rid of all datasets with the old key
38+
auto engine = SetupPrivate::engine(defaults->setupName());
39+
QMetaObject::invokeMethod(engine, "triggerResync", Qt::QueuedConnection);
2740
}
2841
}
2942

@@ -44,13 +57,12 @@ void QTinyAesEncryptor::setKey(const QByteArray &key)
4457

4558
QJsonValue QTinyAesEncryptor::encrypt(const ObjectKey &key, const QJsonObject &object, const QByteArray &keyProperty) const
4659
{
47-
//TODO adjust to AES256
4860
auto salt = QRng().generateRandom(28);//224 bits
4961
auto iv = QCryptographicHash::hash(salt + key.first + key.second.toUtf8() + keyProperty, QCryptographicHash::Sha3_224);
5062
iv.resize(QTinyAes::BLOCKSIZE);
5163

5264
auto data = QJsonDocument(object).toBinaryData();
53-
auto cipher = QTinyAes::cbcEncrypt( _key, iv, data);
65+
auto cipher = QTinyAes::cbcEncrypt(_key, iv, data);
5466

5567
QJsonObject result;
5668
result[QStringLiteral("salt")] = QString::fromUtf8(salt.toBase64());
@@ -68,9 +80,6 @@ QJsonObject QTinyAesEncryptor::decrypt(const ObjectKey &key, const QJsonValue &d
6880
iv.resize(QTinyAes::BLOCKSIZE);
6981

7082
auto cipher = QByteArray::fromBase64(obj[QStringLiteral("data")].toString().toUtf8());
71-
if(cipher.size() % QTinyAes::KEYSIZE != 0)
72-
throw DecryptionFailedException();
73-
7483
auto plain = QTinyAes::cbcDecrypt(_key, iv, cipher);
7584
auto json = QJsonDocument::fromBinaryData(plain);
7685
if(json.isObject())
@@ -83,7 +92,7 @@ QJsonObject QTinyAesEncryptor::decrypt(const ObjectKey &key, const QJsonValue &d
8392

8493
const char *InvalidKeyException::what() const noexcept
8594
{
86-
return "The given key does not have the valid length of 128 bit!";
95+
return "The given key does not have the valid length of 256 bit!";
8796
}
8897

8998
void InvalidKeyException::raise() const
@@ -98,7 +107,7 @@ QException *InvalidKeyException::clone() const
98107

99108
const char *DecryptionFailedException::what() const noexcept
100109
{
101-
return "Failed to decrypt data returned from server. Maybe it's not encrypted?";
110+
return "Failed to decrypt data returned from server. Try a resync.";
102111
}
103112

104113
void DecryptionFailedException::raise() const

0 commit comments

Comments
 (0)