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
4558QJsonValue 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
8493const 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
8998void InvalidKeyException::raise () const
@@ -98,7 +107,7 @@ QException *InvalidKeyException::clone() const
98107
99108const 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
104113void DecryptionFailedException::raise () const
0 commit comments