1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Encryption: it works! Code cleanup is still required

This commit is contained in:
apprb 2014-10-17 00:38:15 +09:00
parent 3cf224a34e
commit 1b9eb3d239
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
3 changed files with 116 additions and 12 deletions

View File

@ -55,27 +55,27 @@ contains(JENKINS,YES) {
# Rules for Windows, Mac OSX, and Linux
win32 {
LIBS += -liphlpapi -L$$PWD/libs/lib -ltoxav -ltoxcore -lvpx -lpthread
LIBS += -liphlpapi -L$$PWD/libs/lib -ltoxav -ltoxcore -ltoxencryptsave -lvpx -lpthread
LIBS += -L$$PWD/libs/lib -lopencv_core248 -lopencv_highgui248 -lopencv_imgproc248 -lOpenAL32 -lopus
LIBS += -lz -lopengl32 -lole32 -loleaut32 -luuid -lvfw32 -ljpeg -ltiff -lpng -ljasper -lIlmImf -lHalf -lws2_32
} else {
macx {
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -framework OpenAL -lopencv_core -lopencv_highgui
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -ltoxencryptsave -lsodium -lvpx -framework OpenAL -lopencv_core -lopencv_highgui
} else {
# If we're building a package, static link libtox[core,av] and libsodium, since they are not provided by any package
contains(STATICPKG, YES) {
target.path = /usr/bin
INSTALLS += target
LIBS += -L$$PWD/libs/lib/ -lopus -lvpx -lopenal -Wl,-Bstatic -ltoxcore -ltoxav -lsodium -lopencv_highgui -lopencv_imgproc -lopencv_core -lz -Wl,-Bdynamic
LIBS += -L$$PWD/libs/lib/ -lopus -lvpx -lopenal -Wl,-Bstatic -ltoxcore -ltoxav -ltoxencryptsave -lsodium -lopencv_highgui -lopencv_imgproc -lopencv_core -lz -Wl,-Bdynamic
LIBS += -Wl,-Bstatic -ljpeg -ltiff -lpng -ljasper -lIlmImf -lIlmThread -lIex -ldc1394 -lraw1394 -lHalf -lz -llzma -ljbig
LIBS += -Wl,-Bdynamic -ltbb -lv4l1 -lv4l2 -lgnutls -lrtmp -lgnutls -lavformat -lavcodec -lavutil -lavfilter -lswscale -lusb-1.0
} else {
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lvpx -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxencryptsave -ltoxav -lvpx -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc
}
contains(JENKINS, YES) {
LIBS = ./libs/lib/libtoxav.a ./libs/lib/libvpx.a ./libs/lib/libopus.a ./libs/lib/libtoxcore.a ./libs/lib/libsodium.a -lopencv_core -lopencv_highgui -lopenal
LIBS = ./libs/lib/libtoxav.a ./libs/lib/libtoxencryptsave.a ./libs/lib/libvpx.a ./libs/lib/libopus.a ./libs/lib/libtoxcore.a ./libs/lib/libsodium.a -lopencv_core -lopencv_highgui -lopenal
}
}
}

View File

@ -17,28 +17,57 @@
#include "encrypteddb.h"
#include "src/misc/settings.h"
#include <tox/toxencryptsave.h>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
EncryptedDb::EncryptedDb(const QString &fname, const QString &key) :
PlainDb(":memory:"), key(key), encrFile(fname)
PlainDb(":memory:"), encrFile(fname)
{
encrkey = new u_int8_t[tox_pass_key_length()];
QByteArray key_ba;
key_ba.append(key);
// tox_derive_key_from_pass(reinterpret_cast<uint8_t*>(key_ba.data()), key_ba.size(), encrkey);
passwd = "test";
qDebug() << QByteArray::fromRawData(reinterpret_cast<char *>(encrkey), tox_pass_key_length()).toBase64();
plainChunkSize = 1024;
encryptedChunkSize = plainChunkSize + tox_pass_encryption_extra_length();
encrFile.open(QIODevice::ReadOnly);
QList<QString> sqlCommands = decryptFile();
for (const QString &cmd : sqlCommands)
{
PlainDb::exec(cmd);
// check line here
QSqlQuery r = PlainDb::exec(cmd);
qDebug() << r.lastError();
}
chunkPosition = encrFile.size() / encryptedChunkSize;
// encrFile.seek(chunkPosition * encryptedChunkSize);
// buffer = encrFile.read(encrFile.size() % encryptedChunkSize);
encrFile.seek(0);
QByteArray fileContent = encrFile.readAll();
encrFile.close();
encrFile.open(QIODevice::WriteOnly);
encrFile.write(fileContent);
}
EncryptedDb::~EncryptedDb()
{
// save to file if necessary
encrFile.close();
delete encrkey;
}
QSqlQuery EncryptedDb::exec(const QString &query)
{
QSqlQuery retQSqlQuery = PlainDb::exec(query);
if (query.startsWith("INSERT", Qt::CaseInsensitive))
if (query.startsWith("INSERT", Qt::CaseInsensitive) || query.startsWith("CREATE", Qt::CaseInsensitive))
appendToEncrypted(query);
return retQSqlQuery;
@ -51,10 +80,78 @@ bool EncryptedDb::save()
QList<QString> EncryptedDb::decryptFile()
{
return QList<QString>();
QByteArray fileContent;
while (!encrFile.atEnd())
{
QByteArray encrChunk = encrFile.read(encryptedChunkSize);
buffer = decrypt(encrChunk);
fileContent += buffer;
}
QList<QByteArray> splittedBA = fileContent.split('\n');
QList<QString> res;
for (auto ba_line : splittedBA)
{
QString line = QByteArray::fromBase64(ba_line);
//check line correctness here
res.append(line);
// res.append(ba_line);
}
return res;
}
void EncryptedDb::appendToEncrypted(const QString &sql)
{
QByteArray b64Str;
b64Str.append(sql);
b64Str = b64Str.toBase64();
buffer += b64Str + "\n";
while (buffer.size() > plainChunkSize)
{
QByteArray filledChunk = buffer.left(plainChunkSize);
encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(encrypt(filledChunk));
buffer = buffer.right(buffer.size() - plainChunkSize);
chunkPosition++;
}
encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(encrypt(buffer));
encrFile.flush();
qDebug() << sql;
}
QByteArray EncryptedDb::encrypt(QByteArray data)
{
int encrSize = data.size() + tox_pass_encryption_extra_length();
int plainSize = data.size();
uint8_t *out = new u_int8_t[encrSize];
// int state = tox_pass_key_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize, encrkey, out);
int state = tox_pass_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize,
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
qDebug() << state;
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), encrSize);
return ret;
}
QByteArray EncryptedDb::decrypt(QByteArray data)
{
int encrSize = data.size();
int plainSize = data.size() - tox_pass_encryption_extra_length();
uint8_t *out = new u_int8_t[plainSize];
// int state = tox_pass_key_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize, encrkey, out);
int state = tox_pass_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize,
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
qDebug() << state << encrSize << plainSize;
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), plainSize);
return ret;
}

View File

@ -32,13 +32,20 @@ public:
virtual bool save();
private:
QString getKey(){return key;}
QByteArray encrypt(QByteArray data);
QByteArray decrypt(QByteArray data);
QList<QString> decryptFile();
void appendToEncrypted(const QString &sql);
QString key;
u_int8_t *encrkey;
QFile encrFile;
QByteArray passwd;
qint64 plainChunkSize;
qint64 encryptedChunkSize;
qint64 chunkPosition;
QByteArray buffer;
};
#endif // ENCRYPTEDDB_H