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

Commit b01d8ca

Browse files
committed
imlemented tiny aes enc
1 parent 167cef1 commit b01d8ca

File tree

7 files changed

+111
-17
lines changed

7 files changed

+111
-17
lines changed

src/3rdparty/qpm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "",
33
"description": "",
44
"dependencies": [
5-
5+
66
77
],
88
"license": "NONE",

src/datasync/encryptor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Encryptor : public QObject
2121
//! Called from the engine to finalize the encryptor
2222
virtual void finalize();
2323

24+
//! Returns the current encryption key
25+
virtual QByteArray key() const = 0;
26+
//! Sets the encryption key
27+
virtual void setKey(const QByteArray &key) = 0;
28+
2429
//! Encrypts the given dataset
2530
virtual QJsonValue encrypt(const ObjectKey &key, const QJsonObject &object, const QByteArray &keyProperty) const = 0;
2631
//! Encrypts the given dataset

src/datasync/qtinyaesencryptor.cpp

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,107 @@
11
#include "qtinyaesencryptor_p.h"
22

33
#include <QtCore/QJsonDocument>
4+
#include <QtCore/qcryptographichash.h>
45

56
#include <qtinyaes.h>
67
using namespace QtDataSync;
78

89
QTinyAesEncryptor::QTinyAesEncryptor(QObject *parent) :
910
Encryptor(parent),
10-
key()
11+
_defaults(nullptr),
12+
_key()
1113
{}
1214

1315
void QTinyAesEncryptor::initialize(Defaults *defaults)
1416
{
15-
key = defaults->property("encryptKey").toByteArray();
16-
if(key.isNull()) {
17-
for(auto i = 0; i < QTinyAes::KEYSIZES.first(); i++)
18-
key += (char)qrand();
17+
_defaults = defaults;
18+
_key = _defaults->settings()->value(QStringLiteral("encryption/key")).toByteArray();
19+
if(_key.isNull()) {
20+
for(quint32 i = 0; i < QTinyAes::KEYSIZE; i++)
21+
_key += (char)qrand();
22+
_defaults->settings()->setValue(QStringLiteral("encryption/key"), _key);
1923
}
2024
}
2125

26+
QByteArray QTinyAesEncryptor::key() const
27+
{
28+
return _key;
29+
}
30+
31+
void QTinyAesEncryptor::setKey(const QByteArray &key)
32+
{
33+
if((quint32)_key.size() != QTinyAes::KEYSIZE)
34+
throw InvalidKeyException();
35+
_key = key;
36+
_defaults->settings()->setValue(QStringLiteral("encryption/key"), _key);
37+
}
38+
2239
QJsonValue QTinyAesEncryptor::encrypt(const ObjectKey &key, const QJsonObject &object, const QByteArray &keyProperty) const
2340
{
41+
QByteArray salt;
42+
for(quint32 i = 0; i < 28; i++)//224 bits
43+
salt += (char)qrand();
44+
auto iv = QCryptographicHash::hash(salt + key.first + key.second.toUtf8() + keyProperty, QCryptographicHash::Sha3_224);
45+
iv.resize(QTinyAes::BLOCKSIZE);
46+
2447
auto data = QJsonDocument(object).toBinaryData();
25-
auto iv = "";
48+
auto cipher = QTinyAes::cbcEncrypt( _key, iv, data);
49+
50+
QJsonObject result;
51+
result[QStringLiteral("salt")] = QString::fromUtf8(salt.toBase64());
52+
result[QStringLiteral("data")] = QString::fromUtf8(cipher.toBase64());
53+
return result;
2654
}
2755

2856
QJsonObject QTinyAesEncryptor::decrypt(const ObjectKey &key, const QJsonValue &data, const QByteArray &keyProperty) const
2957
{
58+
auto obj = data.toObject();
59+
auto salt = QByteArray::fromBase64(obj[QStringLiteral("salt")].toString().toUtf8());
60+
if(salt.size() != 28)//224 bits
61+
throw DecryptionFailedException();
62+
auto iv = QCryptographicHash::hash(salt + key.first + key.second.toUtf8() + keyProperty, QCryptographicHash::Sha3_224);
63+
iv.resize(QTinyAes::BLOCKSIZE);
3064

65+
auto cipher = QByteArray::fromBase64(obj[QStringLiteral("data")].toString().toUtf8());
66+
if(cipher.size() % QTinyAes::KEYSIZE != 0)
67+
throw DecryptionFailedException();
68+
69+
auto plain = QTinyAes::cbcDecrypt(_key, iv, cipher);
70+
auto json = QJsonDocument::fromBinaryData(plain);
71+
if(json.isObject())
72+
return json.object();
73+
else
74+
throw DecryptionFailedException();
75+
}
76+
77+
78+
79+
const char *InvalidKeyException::what() const noexcept
80+
{
81+
return "The given key does not have the valid length of 128 bit!";
82+
}
83+
84+
void InvalidKeyException::raise() const
85+
{
86+
throw *this;
87+
}
88+
89+
QException *InvalidKeyException::clone() const
90+
{
91+
return new InvalidKeyException();
92+
}
93+
94+
const char *DecryptionFailedException::what() const noexcept
95+
{
96+
return "Failed to decrypt data returned from server. Maybe it's not encrypted?";
97+
}
98+
99+
void DecryptionFailedException::raise() const
100+
{
101+
throw *this;
102+
}
103+
104+
QException *DecryptionFailedException::clone() const
105+
{
106+
return new DecryptionFailedException();
31107
}

src/datasync/qtinyaesencryptor_p.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
#include "qtdatasync_global.h"
55
#include "encryptor.h"
66

7+
#include <QtCore/QException>
8+
79
namespace QtDataSync {
810

11+
class Q_DATASYNC_EXPORT InvalidKeyException : public QException
12+
{
13+
public:
14+
const char *what() const noexcept override;
15+
void raise() const override;
16+
QException *clone() const override;
17+
};
18+
19+
class Q_DATASYNC_EXPORT DecryptionFailedException : public QException
20+
{
21+
public:
22+
const char *what() const noexcept override;
23+
void raise() const override;
24+
QException *clone() const override;
25+
};
26+
927
class Q_DATASYNC_EXPORT QTinyAesEncryptor : public Encryptor
1028
{
1129
Q_OBJECT
@@ -15,11 +33,15 @@ class Q_DATASYNC_EXPORT QTinyAesEncryptor : public Encryptor
1533

1634
void initialize(Defaults *defaults) override;
1735

36+
QByteArray key() const override;
37+
void setKey(const QByteArray &key) override;
38+
1839
QJsonValue encrypt(const ObjectKey &key, const QJsonObject &object, const QByteArray &keyProperty) const override;
1940
QJsonObject decrypt(const ObjectKey &key, const QJsonValue &data, const QByteArray &keyProperty) const override;
2041

2142
private:
22-
QByteArray key;
43+
Defaults *_defaults;
44+
QByteArray _key;
2345
};
2446

2547
}

src/datasync/remoteconnector.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ RemoteConnector::~RemoteConnector() {}
2424

2525
void RemoteConnector::initialize(Defaults *defaults, Encryptor *)
2626
{
27-
QT_WARNING_PUSH
28-
QT_WARNING_DISABLE_DEPRECATED
29-
initialize(defaults);
30-
QT_WARNING_POP
3127
d->defaults = defaults;
3228
}
3329

34-
void RemoteConnector::initialize(Defaults *) {}
35-
3630
void RemoteConnector::finalize() {}
3731

3832
void RemoteConnector::resetUserId(QFutureInterface<QVariant> futureInterface, const QVariant &extraData, bool resetLocalStore)

src/datasync/remoteconnector.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class Q_DATASYNC_EXPORT RemoteConnector : public QObject
3737

3838
//! Called from the engine to initialize the connector
3939
virtual void initialize(Defaults *defaults, Encryptor *cryptor);
40-
//! Called from the engine to initialize the connector
41-
Q_DECL_DEPRECATED virtual void initialize(Defaults *defaults);
4240
//! Called from the engine to finalize the connector
4341
virtual void finalize();
4442

tests/auto/datasync/WsRemoteConnectorTest/tst_wsremoteconnector.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ void WsRemoteConnectorTest::testSecondDevice()
271271
QVERIFY(auth->isConnected());
272272
for(auto i = 0; i < 10 && syncSpy.count() < 4; i++)
273273
syncSpy.wait(500);
274-
qDebug() << syncSpy;
275274
QCOMPARE(syncSpy.count(), 4);
276275
QCOMPARE(syncSpy[0][0], QVariant::fromValue(SyncController::Disconnected));
277276
QCOMPARE(syncSpy[1][0], QVariant::fromValue(SyncController::Loading));

0 commit comments

Comments
 (0)