Merge pull request #45 from strider-paff-shell/encryption

Add some encryption stuff
This commit is contained in:
Denio
2020-05-23 19:54:23 +02:00
committed by GitHub
9 changed files with 127 additions and 51 deletions

View File

@@ -48,3 +48,9 @@ b FileSystem::writeContacts
r r
n n
q q
r
b FileEncryption::encrypt
r
s
n
q

View File

@@ -1,2 +1,2 @@
break FileSystem::writeContacts break FileEncryption::encrypt

View File

@@ -83,7 +83,8 @@ SOURCES += \
src/Chat/Helper/ChatIDGenerator.cpp \ src/Chat/Helper/ChatIDGenerator.cpp \
src/Chat/Chat.cpp \ src/Chat/Chat.cpp \
src/FileSystem/FileSystem.cpp \ src/FileSystem/FileSystem.cpp \
src/Crypto/FileEncryption.cpp src/Crypto/FileEncryption.cpp \
src/Crypto/passwd.cpp
HEADERS += \ HEADERS += \
src/firsttimewizard.h \ src/firsttimewizard.h \

View File

@@ -7,42 +7,48 @@ void FileEncryption::showConfig()
int FileEncryption::encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) int FileEncryption::encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES])
{ {
unsigned char buf_in[FILEENCRYPTION_CHUNK_SIZE]; unsigned char plain_data[FILEENCRYPTION_CHUNK_SIZE];
unsigned char buf_out[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; unsigned char cipher_data[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
crypto_secretstream_xchacha20poly1305_state st; crypto_secretstream_xchacha20poly1305_state state;
FILE *fp_t, *fp_s; FILE *target, *source;
unsigned long long out_len; unsigned long long cipher_len;
size_t rlen; size_t rlen;
int eof; int eof;
unsigned char tag; unsigned char tag;
fp_s = fopen(source_file.toStdString().c_str(), "rb"); if(!FileEncryption::exists(source_file.toStdString()))
fp_t = fopen(target_file.toStdString().c_str(), "wb"); {
crypto_secretstream_xchacha20poly1305_init_push(&st, header, key); qDebug() << "File not exits" << source_file;
fwrite(header, 1, sizeof header, fp_t); return -1;
}
source = fopen(source_file.toStdString().c_str(), "rb");
target = fopen(target_file.toStdString().c_str(), "wb");
crypto_secretstream_xchacha20poly1305_init_push(&state, header, key);
fwrite(header, 1, sizeof header, target);
do do
{ {
rlen = fread(buf_in, 1, sizeof buf_in, fp_s); rlen = fread(plain_data, 1, sizeof plain_data, source);
eof = feof(fp_s); eof = feof(source);
tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
crypto_secretstream_xchacha20poly1305_push( crypto_secretstream_xchacha20poly1305_push(
&st, &state,
buf_out, cipher_data,
&out_len, &cipher_len,
buf_in, plain_data,
rlen, rlen,
NULL, NULL,
0, 0,
tag tag
); );
fwrite(buf_out, 1, (size_t) out_len, fp_t); fwrite(cipher_data, 1, (size_t) cipher_len, target);
} }
while (! eof); while (! eof);
fclose(fp_t); fclose(target);
fclose(fp_s); fclose(source);
return 0; return 0;
} }
@@ -59,6 +65,12 @@ int FileEncryption::decrypt(QString target_file, QString source_file, const unsi
int ret = -1; int ret = -1;
unsigned char tag; unsigned char tag;
if(!FileEncryption::exists(source_file.toStdString()))
{
qDebug() << "File not exits" << source_file;
return -1;
}
fp_s = fopen(source_file.toStdString().c_str(), "rb"); fp_s = fopen(source_file.toStdString().c_str(), "rb");
fp_t = fopen(target_file.toStdString().c_str(), "wb"); fp_t = fopen(target_file.toStdString().c_str(), "wb");
fread(header, 1, sizeof header, fp_s); fread(header, 1, sizeof header, fp_s);

View File

@@ -3,11 +3,17 @@
#include <stdio.h> #include <stdio.h>
#include <sodium.h> #include <sodium.h>
#include <QString> #include <QString>
#include <fstream>
#define FILEENCRYPTION_CHUNK_SIZE 4096 #define FILEENCRYPTION_CHUNK_SIZE 4096
class FileEncryption class FileEncryption
{ {
private:
inline static bool exists (const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
public: public:
static void showConfig(); static void showConfig();
static int encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); static int encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);

52
src/Crypto/passwd.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include "passwd.h"
void PASSWD::show_hex_buff(unsigned char buf[])
{
int i;
for (uint8_t i=0; i < crypto_secretstream_xchacha20poly1305_KEYBYTES; i++)
printf("%02X ", buf[i]);
printf("\n");
}
const unsigned char* PASSWD::hash(QString password)
{
/*std::string data = password.toStdString();
unsigned char hash[crypto_generichash_BYTES];
crypto_generichash(hash, sizeof hash,
(const unsigned char*)data.c_str(), data.size(),
NULL, 0);
//qDebug() << PASSWD::convertToHexString(hash);
return (const unsigned char*)hash;*/
int length = password.length();
char *sequence = NULL;
sequence = new char[length+1];
strncpy(sequence, password.toLocal8Bit(), length +1);
#define MESSAGE ((const unsigned char *) sequence)
#define MESSAGE_LEN length
qDebug()<<"Generating cryptographic key from password: " <<sequence;
unsigned char * sha256hash = new unsigned char[crypto_hash_sha256_BYTES];
unsigned char * blacke2hash = new unsigned char[crypto_generichash_KEYBYTES];
unsigned char * hash = new unsigned char[crypto_secretstream_xchacha20poly1305_KEYBYTES];
crypto_hash_sha256(sha256hash, MESSAGE, MESSAGE_LEN);
crypto_generichash(blacke2hash, sizeof hash, MESSAGE, MESSAGE_LEN, NULL, 0);
for(uint8_t i = 0; i < crypto_secretstream_xchacha20poly1305_KEYBYTES/2; i++)
hash[i] = blacke2hash[i];
for(uint8_t i = crypto_secretstream_xchacha20poly1305_KEYBYTES/2; i < crypto_secretstream_xchacha20poly1305_KEYBYTES; i++)
hash[i] = sha256hash[i];
delete[] sha256hash;
delete[] blacke2hash;
qDebug()<<"secret key generated:\n";
PASSWD::show_hex_buff(hash);
return hash;
}

14
src/Crypto/passwd.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef PASSWD_H
#define PASSWD_H
#include <stdio.h>
#include <sodium.h>
#include <QString>
class PASSWD
{
public:
static void show_hex_buff(unsigned char buf[]);
static const unsigned char* hash(QString);
};
#endif

View File

@@ -2,6 +2,7 @@
#include <QString> #include <QString>
#include <QList> #include <QList>
#include "../Crypto/passwd.h"
FileSystem::FileSystem() FileSystem::FileSystem()
{ {

View File

@@ -25,6 +25,9 @@
#include "sodium.h" #include "sodium.h"
#include "sodium/crypto_generichash_blake2b.h" #include "sodium/crypto_generichash_blake2b.h"
#include <QRegularExpression> #include <QRegularExpression>
#include "FileSystem/FileSystem.h"
#include "Crypto/passwd.h"
#include "Crypto/FileEncryption.h"
using json = nlohmann::json; using json = nlohmann::json;
@@ -283,39 +286,20 @@ void MainWindow::encryptWallet() {
QObject::connect(ed.txtConfirmPassword, &QLineEdit::textChanged, fnPasswordEdited); QObject::connect(ed.txtConfirmPassword, &QLineEdit::textChanged, fnPasswordEdited);
QObject::connect(ed.txtPassword, &QLineEdit::textChanged, fnPasswordEdited); QObject::connect(ed.txtPassword, &QLineEdit::textChanged, fnPasswordEdited);
if (d.exec() == QDialog::Accepted) { if (d.exec() == QDialog::Accepted)
{
QString str = ed.txtPassword->text(); // data comes from user inputs const unsigned char* key=PASSWD::hash(ed.txtPassword->text());
int length = str.length(); PASSWD::show_hex_buff((unsigned char*) key);
auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
char *sequence = NULL; QString source_file = dir.filePath("addresslabels.dat");
sequence = new char[length+1]; QString target_enc_file = dir.filePath("addresslabels.dat.enc");
strncpy(sequence, str.toLocal8Bit(), length +1); QString target_dec_file = dir.filePath("addresslabels.dat.dec");
FileEncryption::encrypt(target_enc_file, source_file, key);
FileEncryption::decrypt(target_dec_file, target_enc_file, key);
#define MESSAGE ((const unsigned char *) sequence) d.exec();
#define MESSAGE_LEN length
qDebug()<<"Generating cryptographic key from password: " <<sequence; }
unsigned char hash[crypto_secretstream_xchacha20poly1305_KEYBYTES];
crypto_generichash(hash, sizeof hash,
MESSAGE, MESSAGE_LEN,
NULL, 0);
qDebug()<<"secret key generated:\n";
dump_hex_buff(hash,crypto_secretstream_xchacha20poly1305_KEYBYTES);
QString source_file = "/home/denio/.local/share/Hush/SilentDragonLite/addresslabel.dat";
QString target_file = "/home/denio/.local/share/Hush/SilentDragonLite/addresslabel-encrypt.dat";
FileEncryption::encrypt(target_file, source_file, hash);
d.exec();
}
} }