diff --git a/.gdb_history b/.gdb_history index 4e303b8..06f6849 100644 --- a/.gdb_history +++ b/.gdb_history @@ -48,3 +48,9 @@ b FileSystem::writeContacts r n q +r +b FileEncryption::encrypt +r +s +n +q diff --git a/peda-session-SilentDragonLite.txt b/peda-session-SilentDragonLite.txt index f57fa7c..6f620bf 100644 --- a/peda-session-SilentDragonLite.txt +++ b/peda-session-SilentDragonLite.txt @@ -1,2 +1,2 @@ -break FileSystem::writeContacts +break FileEncryption::encrypt diff --git a/silentdragon-lite.pro b/silentdragon-lite.pro index 86f38b0..7d3a281 100644 --- a/silentdragon-lite.pro +++ b/silentdragon-lite.pro @@ -83,7 +83,8 @@ SOURCES += \ src/Chat/Helper/ChatIDGenerator.cpp \ src/Chat/Chat.cpp \ src/FileSystem/FileSystem.cpp \ - src/Crypto/FileEncryption.cpp + src/Crypto/FileEncryption.cpp \ + src/Crypto/passwd.cpp HEADERS += \ src/firsttimewizard.h \ diff --git a/src/Crypto/FileEncryption.cpp b/src/Crypto/FileEncryption.cpp index 0100585..fae9bbc 100644 --- a/src/Crypto/FileEncryption.cpp +++ b/src/Crypto/FileEncryption.cpp @@ -7,42 +7,48 @@ void FileEncryption::showConfig() 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 buf_out[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; + unsigned char plain_data[FILEENCRYPTION_CHUNK_SIZE]; + unsigned char cipher_data[FILEENCRYPTION_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; - crypto_secretstream_xchacha20poly1305_state st; - FILE *fp_t, *fp_s; - unsigned long long out_len; + crypto_secretstream_xchacha20poly1305_state state; + FILE *target, *source; + unsigned long long cipher_len; size_t rlen; int eof; unsigned char tag; - fp_s = fopen(source_file.toStdString().c_str(), "rb"); - fp_t = fopen(target_file.toStdString().c_str(), "wb"); - crypto_secretstream_xchacha20poly1305_init_push(&st, header, key); - fwrite(header, 1, sizeof header, fp_t); + if(!FileEncryption::exists(source_file.toStdString())) + { + qDebug() << "File not exits" << source_file; + 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 { - rlen = fread(buf_in, 1, sizeof buf_in, fp_s); - eof = feof(fp_s); + rlen = fread(plain_data, 1, sizeof plain_data, source); + eof = feof(source); tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; crypto_secretstream_xchacha20poly1305_push( - &st, - buf_out, - &out_len, - buf_in, + &state, + cipher_data, + &cipher_len, + plain_data, rlen, NULL, 0, tag ); - fwrite(buf_out, 1, (size_t) out_len, fp_t); + fwrite(cipher_data, 1, (size_t) cipher_len, target); } while (! eof); - fclose(fp_t); - fclose(fp_s); + fclose(target); + fclose(source); return 0; } @@ -59,6 +65,12 @@ int FileEncryption::decrypt(QString target_file, QString source_file, const unsi int ret = -1; 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_t = fopen(target_file.toStdString().c_str(), "wb"); fread(header, 1, sizeof header, fp_s); diff --git a/src/Crypto/FileEncryption.h b/src/Crypto/FileEncryption.h index 6db8977..c7a09d5 100644 --- a/src/Crypto/FileEncryption.h +++ b/src/Crypto/FileEncryption.h @@ -3,11 +3,17 @@ #include #include #include +#include #define FILEENCRYPTION_CHUNK_SIZE 4096 class FileEncryption { + private: + inline static bool exists (const std::string& name) { + std::ifstream f(name.c_str()); + return f.good(); + } public: static void showConfig(); static int encrypt(QString target_file, QString source_file, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); diff --git a/src/Crypto/passwd.cpp b/src/Crypto/passwd.cpp new file mode 100644 index 0000000..160e631 --- /dev/null +++ b/src/Crypto/passwd.cpp @@ -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: " < +#include +#include + +class PASSWD +{ + public: + static void show_hex_buff(unsigned char buf[]); + static const unsigned char* hash(QString); +}; + +#endif \ No newline at end of file diff --git a/src/FileSystem/FileSystem.cpp b/src/FileSystem/FileSystem.cpp index d560d98..2a60de1 100644 --- a/src/FileSystem/FileSystem.cpp +++ b/src/FileSystem/FileSystem.cpp @@ -2,6 +2,7 @@ #include #include +#include "../Crypto/passwd.h" FileSystem::FileSystem() { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9138c7c..a2566ad 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -25,6 +25,9 @@ #include "sodium.h" #include "sodium/crypto_generichash_blake2b.h" #include +#include "FileSystem/FileSystem.h" +#include "Crypto/passwd.h" +#include "Crypto/FileEncryption.h" using json = nlohmann::json; @@ -283,39 +286,20 @@ void MainWindow::encryptWallet() { QObject::connect(ed.txtConfirmPassword, &QLineEdit::textChanged, fnPasswordEdited); QObject::connect(ed.txtPassword, &QLineEdit::textChanged, fnPasswordEdited); - if (d.exec() == QDialog::Accepted) { - - QString str = ed.txtPassword->text(); // data comes from user inputs - int length = str.length(); - - char *sequence = NULL; - sequence = new char[length+1]; - strncpy(sequence, str.toLocal8Bit(), length +1); - - #define MESSAGE ((const unsigned char *) sequence) - #define MESSAGE_LEN length - - qDebug()<<"Generating cryptographic key from password: " <text()); + PASSWD::show_hex_buff((unsigned char*) key); + auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); + QString source_file = dir.filePath("addresslabels.dat"); + QString target_enc_file = dir.filePath("addresslabels.dat.enc"); + 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); -d.exec(); + d.exec(); -} + } }