From 854b14df6a744733e6052d8582887f15226d0941 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Sat, 30 May 2020 14:12:17 +0200 Subject: [PATCH 01/15] unready code for mmessage encryption --- src/chatmodel.cpp | 231 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 230 insertions(+), 1 deletion(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index ee3fe6d..445a6f1 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -374,7 +374,236 @@ Tx MainWindow::createTxFromChatPage() { QString hmemo= createHeaderMemo(type,cid,myAddr); - QString memo = ui->memoTxtChat->toPlainText().trimmed(); + QString memounencrypt = ui->memoTxtChat->toPlainText().trimmed(); + + + int length = memounencrypt.length(); + + char *sequence = NULL; + sequence = new char[length+1]; + strncpy(sequence, memounencrypt.toLocal8Bit(), length +1); + +////////////////////////////////////////////////////Important: If we can decrypt the output of QString memo, after we encrypt it, Bobs code must be in Controller.cpp + + /////////////////Alice Pubkey + #define MESSAGEAP ((const unsigned char *) "Ioesd") + #define MESSAGEAP_LEN 5 + + unsigned char alice_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + + crypto_generichash(alice_publickey, sizeof alice_publickey, + MESSAGEAP, MESSAGEAP_LEN, + NULL, 0); + QString alice = QString::fromLocal8Bit((char*)alice_publickey); + qDebug()<<"Alice Pubkey : "<(c1), CIPHERTEXT_LEN); + + qDebug()<<"Size Controller Memo :"<< encryptedMemo.length(); + + QString memo = QString::fromUtf8( encryptedMemo.data(), encryptedMemo.size()); + + + /////////////////Bob Pubkey + #define MESSAGEBAP1 ((const unsigned char *) "Hal12") + #define MESSAGEBAP1_LEN 5 + + unsigned char bob1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + + crypto_generichash(bob1_publickey, sizeof bob1_publickey, + MESSAGEBAP1, MESSAGEBAP1_LEN, + NULL, 0); + + qDebug()<<"Bobs Pubkey created"; + + /////////////////Bob Secretkey + #define MESSAGEBS ((const unsigned char *) "Hal11") + #define MESSAGEBS_LEN 5 + + unsigned char bob_secretkey[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; + + crypto_generichash(bob_secretkey, sizeof bob_secretkey, + MESSAGEBS, MESSAGEBS_LEN, + NULL, 0); + + qDebug()<<"Bobs Pubkey created"; + + /////////////////Alice Pubkey bob creates + #define MESSAGEA121 ((const unsigned char *) "Ioesd") + #define MESSAGEAP121_LEN 5 + + unsigned char alice1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + + crypto_generichash(alice1_publickey, sizeof alice1_publickey, + MESSAGEA121, MESSAGEAP121_LEN, + NULL, 0); + + QString alice1 = QString::fromLocal8Bit((char*)alice1_publickey); + qDebug()<<"Alice Pubkey Bob create: "<(m2),MESSAGE_LEN); + qDebug()<<"7: "; + QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + + + + qDebug()<<"OUT decrypt:" << memodecrypt; From 55b11480478ac77bf099d1692bee88e7723c5116 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Sun, 31 May 2020 00:29:03 +0200 Subject: [PATCH 02/15] comments for debug issues --- src/chatmodel.cpp | 213 ++++++++++++++++++++++------------------------ 1 file changed, 102 insertions(+), 111 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index f25fe79..6d6a216 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -370,21 +370,23 @@ Tx MainWindow::createTxFromChatPage() { QString type = "Memo"; QString addr = c.getPartnerAddress(); - + QString hmemo= createHeaderMemo(type,cid,myAddr); + + /////////User input for chatmemos QString memounencrypt = ui->memoTxtChat->toPlainText().trimmed(); - + /////////We convert the user input from QString to unsigned char*, so we can encrypt it later int length = memounencrypt.length(); char *sequence = NULL; sequence = new char[length+1]; strncpy(sequence, memounencrypt.toLocal8Bit(), length +1); -////////////////////////////////////////////////////Important: If we can decrypt the output of QString memo, after we encrypt it, Bobs code must be in Controller.cpp +//////////////////////////////////////////////////Lets create Alice keys for the conversation/////////////////////////////////// /////////////////Alice Pubkey - #define MESSAGEAP ((const unsigned char *) "Ioesd") + #define MESSAGEAP ((const unsigned char *) "Ioesd")///////////static atm, in future we will use the CID here #define MESSAGEAP_LEN 5 unsigned char alice_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; @@ -398,7 +400,7 @@ Tx MainWindow::createTxFromChatPage() { /////////////////Alice Secretkey - #define MESSAGEAS ((const unsigned char *) "Hallo") + #define MESSAGEAS ((const unsigned char *) "Hallo")///////////static atm, in future we will use the Passphrase here #define MESSAGEAS_LEN 5 unsigned char alice_secretkey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; @@ -408,7 +410,7 @@ Tx MainWindow::createTxFromChatPage() { NULL, 0); /////////////////Bob Pubkey that Alice creates - #define MESSAGEBAP ((const unsigned char *) "Hal11") + #define MESSAGEBAP ((const unsigned char *) "Hal11")///////////static atm, in future we will use the CID here #define MESSAGEBAP_LEN 5 unsigned char bob_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; @@ -420,55 +422,67 @@ Tx MainWindow::createTxFromChatPage() { qDebug()<<"Alice version of Bobs Pubkey created"; - ////////////Alice creates the Shared key - unsigned char server_rx[crypto_kx_SESSIONKEYBYTES], server_tx[crypto_kx_SESSIONKEYBYTES]; -/* Generate the server's key pair */ -crypto_kx_keypair(alice_publickey, alice_secretkey); + ////////////Now we create shared keys for the conversation////////////////////////////// -/* Prerequisite after this point: the client's public key must be known by the server */ + unsigned char server_rx[crypto_kx_SESSIONKEYBYTES], server_tx[crypto_kx_SESSIONKEYBYTES]; + /* Generate the server's key pair */ + crypto_kx_keypair(alice_publickey, alice_secretkey); -/* Compute two shared keys using the client's public key and the server's secret key. - server_rx will be used by the server to receive data from the client, - server_tx will by used by the server to send data to the client. */ -if (crypto_kx_server_session_keys(server_rx, server_tx, - alice_publickey, alice_secretkey, bob_publickey) != 0) { + /* Prerequisite after this point: the client's public key must be known by the server */ + + /* Compute two shared keys using the client's public key and the server's secret key. + server_rx will be used by the server to receive data from the client, + server_tx will by used by the server to send data to the client. */ + if (crypto_kx_server_session_keys(server_rx, server_tx, + alice_publickey, alice_secretkey, bob_publickey) != 0) { /* Suspicious client public key, bail out */ } - + ////////////Now lets encrypt the message Alice send to Bob////////////////////////////// #define MESSAGE (const unsigned char *) sequence #define MESSAGE_LEN length #define CIPHERTEXT_LEN (MESSAGE_LEN + crypto_secretstream_xchacha20poly1305_ABYTES) -crypto_secretstream_xchacha20poly1305_state state; -//unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; -unsigned char c1[CIPHERTEXT_LEN]; + + crypto_secretstream_xchacha20poly1305_state state; + /////The Header must be known by both, so we can use alice or bobs pubkey here + + //unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; + + unsigned char c1[CIPHERTEXT_LEN]; + + /* Shared secret key required to encrypt/decrypt the stream */ + crypto_secretstream_xchacha20poly1305_keygen(alice_publickey); + + /* Set up a new stream: initialize the state and create the header */ + ////////////Bobs pubkey will be use as header here, and alice_publickey as Key. Just for testing, we will switch to server_tx here ////////////////////////////// + crypto_secretstream_xchacha20poly1305_init_push(&state, bob_publickey, alice_publickey); + + ////Now encrypt the Message/////////////////////////////////// + crypto_secretstream_xchacha20poly1305_push + (&state, c1, NULL, MESSAGE, MESSAGE_LEN, NULL, 0, 0); + + ///////////Ciphertext is now in c1 as unsigned char*, we need it as QString, to send it as memo to Bob////////////////////// + + /////This is not working. It seems the QString has not exact the ciphertext in it. It will not decrypt with it//////////////// + QString memo( reinterpret_cast< char* >( c1 ) ); + + qDebug()<<"Size QString with encrypted data :"<< memo.length(); ////We check the length here, to compare it with the length our QString + + ///////Just for testing we convert the unsigned char* to std::string, to see if we can decrypt,and that works.//////////// + + std::string encryptedMemo(reinterpret_cast(c1), CIPHERTEXT_LEN); -/* Shared secret key required to encrypt/decrypt the stream */ -crypto_secretstream_xchacha20poly1305_keygen(alice_publickey); + qDebug()<<"Size std::string with encrypted data :"<< encryptedMemo.length(); ////We check the length here, to compare it with the length our QString -/* Set up a new stream: initialize the state and create the header */ -crypto_secretstream_xchacha20poly1305_init_push(&state, bob_publickey, alice_publickey); + -/* Now, encrypt the first chunk. `c1` will contain an encrypted, - * authenticated representation of `MESSAGE_PART1`. */ -crypto_secretstream_xchacha20poly1305_push - (&state, c1, NULL, MESSAGE, MESSAGE_LEN, NULL, 0, 0); - - -///////////get a std:string....QString will give us not good results - - std::string encryptedMemo(reinterpret_cast(c1), CIPHERTEXT_LEN); - - qDebug()<<"Size Controller Memo :"<< encryptedMemo.length(); - - QString memo = QString::fromUtf8( encryptedMemo.data(), encryptedMemo.size()); - - - /////////////////Bob Pubkey - #define MESSAGEBAP1 ((const unsigned char *) "Hal12") + /////////////////////////////////Now we create Bobs keys, just for testing at this place. If the encryption/decryption works we put it in Controller.cpp (RefreshTransactions) + + /////////////////Bob Pubkey///////////////////////////////// + #define MESSAGEBAP1 ((const unsigned char *) "Hal12")///////////static atm, in future we will use the CID here #define MESSAGEBAP1_LEN 5 unsigned char bob1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; @@ -480,7 +494,7 @@ crypto_secretstream_xchacha20poly1305_push qDebug()<<"Bobs Pubkey created"; /////////////////Bob Secretkey - #define MESSAGEBS ((const unsigned char *) "Hal11") + #define MESSAGEBS ((const unsigned char *) "Hal11")///////////static atm, in future we will use the Passphrase here #define MESSAGEBS_LEN 5 unsigned char bob_secretkey[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; @@ -492,7 +506,7 @@ crypto_secretstream_xchacha20poly1305_push qDebug()<<"Bobs Pubkey created"; /////////////////Alice Pubkey bob creates - #define MESSAGEA121 ((const unsigned char *) "Ioesd") + #define MESSAGEA121 ((const unsigned char *) "Ioesd")///////////static atm, in future we will use the CID here #define MESSAGEAP121_LEN 5 unsigned char alice1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; @@ -507,104 +521,81 @@ crypto_secretstream_xchacha20poly1305_push -////////////BOB creates the Shared key -//unsigned char bob_publickey[crypto_kx_PUBLICKEYBYTES], bob_secretkey[crypto_kx_SECRETKEYBYTES]; -unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; +////////////Now we create the shared keys for Bob/////////////////////////////////////////////// -/* Generate the client's key pair */ -crypto_kx_keypair(bob1_publickey, bob_secretkey); + unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; -/* Prerequisite after this point: the server's public key must be known by the client */ + /* Generate the client's key pair */ + crypto_kx_keypair(bob1_publickey, bob_secretkey); -/* Compute two shared keys using the server's public key and the client's secret key. - client_rx will be used by the client to receive data from the server, - client_tx will by used by the client to send data to the server. */ -if (crypto_kx_client_session_keys(client_rx, client_tx, - bob1_publickey, bob_secretkey, alice1_publickey) != 0) { - /* Suspicious server public key, bail out */ -} + /* Prerequisite after this point: the server's public key must be known by the client */ -qDebug()<<"1 : "; + /* Compute two shared keys using the server's public key and the client's secret key. + client_rx will be used by the client to receive data from the server, + client_tx will by used by the client to send data to the server. */ + if (crypto_kx_client_session_keys(client_rx, client_tx, + bob1_publickey, bob_secretkey, alice1_publickey) != 0) { + /* Suspicious server public key, bail out */ + } -qDebug()<<"Size of QString memo send as Transaction:" << memo.length(); - -QString memo1 = QString::fromUtf8( encryptedMemo.data(), encryptedMemo.size()); - -int lenght1 = encryptedMemo.length(); - -qDebug()<<"std::string Memo size : "<(m),MESSAGE1_LEN); + /////Now we can convert it to QString + QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); - - //QString memodecrypt = QString::fromLocal8Bit((char*)m2); - std::string decryptedMemo(reinterpret_cast(m2),MESSAGE_LEN); - qDebug()<<"7: "; - QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); - - - - qDebug()<<"OUT decrypt:" << memodecrypt; - - - + //////////////Give us the output of the decrypted message as debug to see if it was successfully + qDebug()<<"OUT decrypt:" << memodecrypt; tx.toAddrs.push_back(ToFields{addr, amt, hmemo}); tx.toAddrs.push_back(ToFields{addr, amt, memo}); From af4a555fb031915e97a751b5fdd0d05e763c36f0 Mon Sep 17 00:00:00 2001 From: Denio <41270280+DenioD@users.noreply.github.com> Date: Sun, 31 May 2020 11:13:56 +0200 Subject: [PATCH 03/15] Update chatmodel.cpp --- src/chatmodel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 6d6a216..8b5f97d 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -545,8 +545,8 @@ Tx MainWindow::createTxFromChatPage() { ////QString to char+ (not working, it will not decrypt) /*char *memoIncoming = NULL; - memoIncoming = new char[lenght1+1]; - strncpy(memoIncoming, memo.toLocal8Bit(), lenght1+1);*/ + memoIncoming = new char[memo.length()+1]; + strncpy(memoIncoming, memo.toLocal8Bit(), [memo.length()+1);*/ ///////////////////////////if we take the std::string (of the encryption output) instead of QString memo, it will decrypt//////// From 4d74555c171011d52f7912a7fa4c672293f42299 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 00:03:11 +0200 Subject: [PATCH 04/15] first message encryption with static keys - work in progress --- src/chatmodel.cpp | 196 ++++----------------------------------------- src/controller.cpp | 112 ++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 187 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 8b5f97d..9a2e5f5 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -386,217 +386,53 @@ Tx MainWindow::createTxFromChatPage() { //////////////////////////////////////////////////Lets create Alice keys for the conversation/////////////////////////////////// /////////////////Alice Pubkey - #define MESSAGEAP ((const unsigned char *) "Ioesd")///////////static atm, in future we will use the CID here + #define MESSAGEAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here #define MESSAGEAP_LEN 5 - unsigned char alice_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; crypto_generichash(alice_publickey, sizeof alice_publickey, MESSAGEAP, MESSAGEAP_LEN, NULL, 0); QString alice = QString::fromLocal8Bit((char*)alice_publickey); - qDebug()<<"Alice Pubkey : "<( c1 ) ); - - qDebug()<<"Size QString with encrypted data :"<< memo.length(); ////We check the length here, to compare it with the length our QString - - ///////Just for testing we convert the unsigned char* to std::string, to see if we can decrypt,and that works.//////////// - - std::string encryptedMemo(reinterpret_cast(c1), CIPHERTEXT_LEN); - - - qDebug()<<"Size std::string with encrypted data :"<< encryptedMemo.length(); ////We check the length here, to compare it with the length our QString - - - - /////////////////////////////////Now we create Bobs keys, just for testing at this place. If the encryption/decryption works we put it in Controller.cpp (RefreshTransactions) - - /////////////////Bob Pubkey///////////////////////////////// - #define MESSAGEBAP1 ((const unsigned char *) "Hal12")///////////static atm, in future we will use the CID here - #define MESSAGEBAP1_LEN 5 - - unsigned char bob1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; - - crypto_generichash(bob1_publickey, sizeof bob1_publickey, - MESSAGEBAP1, MESSAGEBAP1_LEN, - NULL, 0); - - qDebug()<<"Bobs Pubkey created"; - - /////////////////Bob Secretkey - #define MESSAGEBS ((const unsigned char *) "Hal11")///////////static atm, in future we will use the Passphrase here - #define MESSAGEBS_LEN 5 - - unsigned char bob_secretkey[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; - - crypto_generichash(bob_secretkey, sizeof bob_secretkey, - MESSAGEBS, MESSAGEBS_LEN, - NULL, 0); - - qDebug()<<"Bobs Pubkey created"; - - /////////////////Alice Pubkey bob creates - #define MESSAGEA121 ((const unsigned char *) "Ioesd")///////////static atm, in future we will use the CID here - #define MESSAGEAP121_LEN 5 - - unsigned char alice1_publickey[crypto_secretstream_xchacha20poly1305_KEYBYTES]; - - crypto_generichash(alice1_publickey, sizeof alice1_publickey, - MESSAGEA121, MESSAGEAP121_LEN, - NULL, 0); - - QString alice1 = QString::fromLocal8Bit((char*)alice1_publickey); - qDebug()<<"Alice Pubkey Bob create: "<(m),MESSAGE1_LEN); - /////Now we can convert it to QString - QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); - - //////////////Give us the output of the decrypted message as debug to see if it was successfully - qDebug()<<"OUT decrypt:" << memodecrypt; - + /////CIphertext Memo + QString memo = QByteArray(reinterpret_cast(ciphertext), CIPHERTEXT_LEN).toHex(); + + tx.toAddrs.push_back(ToFields{addr, amt, hmemo}); tx.toAddrs.push_back(ToFields{addr, amt, memo}); diff --git a/src/controller.cpp b/src/controller.cpp index 0c79240..39efa6a 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -907,10 +907,12 @@ void Controller::refreshTransactions() { this->refresh(true); } + // QString memo1; + QString memo; if (!o["memo"].is_null()) { - memo = QString::fromStdString(o["memo"]); - + QString memo = QString::fromStdString(o["memo"].get()); + QString cid; bool isNotarized; @@ -921,8 +923,106 @@ void Controller::refreshTransactions() { isNotarized = false; } +/////////////////////////////////Now we create Bobs keys, just for testing at this place. If the encryption/decryption works we put it in Controller.cpp (RefreshTransactions) + + /////////////////Alice Pubkey bob create + #define MESSAGEAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here + #define MESSAGEAP_LEN 5 - ChatItem item = ChatItem( + unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; + + crypto_generichash(alice_publickey, sizeof alice_publickey, + MESSAGEAP, MESSAGEAP_LEN, + NULL, 0); + + + /////////////////Bob Secretkey + + #define MESSAGEAS ((const unsigned char *) "Hallo")///////////static atm, in future we will use the Passphrase here + #define MESSAGEAS_LEN 5 + + unsigned char bob_secretkey[crypto_box_SECRETKEYBYTES]; + + crypto_generichash(bob_secretkey, sizeof bob_secretkey, + MESSAGEAS, MESSAGEAS_LEN, + NULL, 0); + + /////////////////Bob Pubkey + #define MESSAGEBAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here + #define MESSAGEBAP_LEN 5 + + unsigned char bob_publickey[crypto_box_PUBLICKEYBYTES]; + + crypto_generichash(bob_publickey, sizeof bob_publickey, + MESSAGEBAP, MESSAGEBAP_LEN, + NULL, 0); + + + + /////We need to filter out Memos smaller then the ciphertext size, or it will dump + + if ((memo.length() > 120) && (memo.startsWith("{") == false)) + { + + + const QByteArray ba = QByteArray::fromHex(memo.toLatin1()); + const unsigned char *encryptedMemo = reinterpret_cast(ba.constData()); + + int encryptedMemoSize1 = ba.length(); + + //////unsigned char* as message from QString + #define MESSAGE2 (const unsigned char *) encryptedMemo + + ///////// length of the encrypted message + #define CIPHERTEXT1_LEN encryptedMemoSize1 + + ///////Message length is smaller then the encrypted message + #define MESSAGE1_LEN encryptedMemoSize1 - crypto_box_MACBYTES + + //////Set the length of the decrypted message + + unsigned char decrypted[MESSAGE1_LEN]; + + ///////Decrypt the message + if (crypto_box_open_easy(decrypted, MESSAGE2, CIPHERTEXT1_LEN, alice_publickey, + alice_publickey, alice_publickey) != 0) { + /* message for Bob pretending to be from Alice has been forged! */ + } + + /////Our decrypted message is now in decrypted. We need it as QString to render it + /////Only the QString gives weird data, so convert first to std::string + + std::string decryptedMemo(reinterpret_cast(decrypted),MESSAGE1_LEN); + + /////Now we can convert it to QString + QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + + //////////////Give us the output of the decrypted message as debug to see if it was successfully + qDebug()<<"OUT decrypt:" << memodecrypt; + + + ChatItem item = ChatItem( + datetime, + address, + QString(""), + memodecrypt, + QString(""), + QString(""), + cid, + txid, + confirmations, + true, + isNotarized, + false + ); + DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); + + + + }else{ + + + ChatItem item = ChatItem( datetime, address, QString(""), @@ -938,11 +1038,9 @@ void Controller::refreshTransactions() { ); DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - + } - } - - + } items.push_back(TransactionItemDetail{address, amount, memo}); total_amount = total_amount + amount; } From e1863058da78d90f67ce166d650d385c0f253616 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 00:17:47 +0200 Subject: [PATCH 05/15] use cid as key for the moment --- src/chatmodel.cpp | 23 +++++++++++++++-------- src/controller.cpp | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 9a2e5f5..1ac2acc 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -374,19 +374,26 @@ Tx MainWindow::createTxFromChatPage() { QString hmemo= createHeaderMemo(type,cid,myAddr); /////////User input for chatmemos - QString memounencrypt = ui->memoTxtChat->toPlainText().trimmed(); + QString memoplain = ui->memoTxtChat->toPlainText().trimmed(); /////////We convert the user input from QString to unsigned char*, so we can encrypt it later - int length = memounencrypt.length(); + int lengthmemo = memoplain.length(); - char *sequence = NULL; - sequence = new char[length+1]; - strncpy(sequence, memounencrypt.toLocal8Bit(), length +1); + char *memoplainchar = NULL; + memoplainchar = new char[lengthmemo+1]; + strncpy(memoplainchar, memoplain.toLocal8Bit(), lengthmemo +1); + + /////////We convert the CID from QString to unsigned char*, so we can encrypt it later + int lengthcid = cid.length(); + + char *cidchar = NULL; + cidchar = new char[lengthcid+1]; + strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); //////////////////////////////////////////////////Lets create Alice keys for the conversation/////////////////////////////////// /////////////////Alice Pubkey - #define MESSAGEAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here + #define MESSAGEAP ((const unsigned char *) cidchar)///////////static atm, in future we will use the CID here #define MESSAGEAP_LEN 5 unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; @@ -418,8 +425,8 @@ Tx MainWindow::createTxFromChatPage() { NULL, 0); ////////////Now lets encrypt the message Alice send to Bob////////////////////////////// - #define MESSAGE (const unsigned char *) sequence - #define MESSAGE_LEN length + #define MESSAGE (const unsigned char *) memoplainchar + #define MESSAGE_LEN lengthmemo #define CIPHERTEXT_LEN (crypto_box_MACBYTES + MESSAGE_LEN) unsigned char ciphertext[CIPHERTEXT_LEN]; diff --git a/src/controller.cpp b/src/controller.cpp index 39efa6a..4069022 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -910,10 +910,24 @@ void Controller::refreshTransactions() { // QString memo1; QString memo; + QString cid; if (!o["memo"].is_null()) { QString memo = QString::fromStdString(o["memo"].get()); + + if (memo.startsWith("{")) { + + QJsonDocument headermemo = QJsonDocument::fromJson(memo.toUtf8()); + + cid = headermemo["cid"].toString(); + + + + chatModel->addCid(txid, cid); + + + } + - QString cid; bool isNotarized; if (confirmations > getLag()) @@ -923,10 +937,25 @@ void Controller::refreshTransactions() { isNotarized = false; } + + if (chatModel->getCidByTx(txid) != QString("0xdeadbeef")){ + + cid = chatModel->getCidByTx(txid); + + }else{ + cid = ""; + } + + int lengthcid = cid.length(); + + char *cidchar = NULL; + cidchar = new char[lengthcid+1]; + strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); + /////////////////////////////////Now we create Bobs keys, just for testing at this place. If the encryption/decryption works we put it in Controller.cpp (RefreshTransactions) /////////////////Alice Pubkey bob create - #define MESSAGEAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here + #define MESSAGEAP ((const unsigned char *) cidchar)///////////static atm, in future we will use the CID here #define MESSAGEAP_LEN 5 unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; From a8c9a8e2efea81db8b263342e70f231790f19bf6 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 00:47:46 +0200 Subject: [PATCH 06/15] use length of cid --- src/chatmodel.cpp | 2 +- src/controller.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 1ac2acc..b033d7e 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -394,7 +394,7 @@ Tx MainWindow::createTxFromChatPage() { /////////////////Alice Pubkey #define MESSAGEAP ((const unsigned char *) cidchar)///////////static atm, in future we will use the CID here - #define MESSAGEAP_LEN 5 + #define MESSAGEAP_LEN lengthcid unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; diff --git a/src/controller.cpp b/src/controller.cpp index 4069022..342a24a 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -956,7 +956,7 @@ void Controller::refreshTransactions() { /////////////////Alice Pubkey bob create #define MESSAGEAP ((const unsigned char *) cidchar)///////////static atm, in future we will use the CID here - #define MESSAGEAP_LEN 5 + #define MESSAGEAP_LEN lengthcid unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; From 21c727efb006ecc5b215a3c831f734a2524fb5a5 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 21:24:39 +0200 Subject: [PATCH 07/15] switch to secretstream for encryption - work in progress --- src/Model/ContactItem.cpp | 2 + src/Model/ContactItem.h | 1 + src/chatmodel.cpp | 156 ++++++++++++++++------- src/chatmodel.h | 3 + src/controller.cpp | 254 ++++++++++++++++++++++++++++---------- src/mainwindow.h | 2 +- 6 files changed, 308 insertions(+), 110 deletions(-) diff --git a/src/Model/ContactItem.cpp b/src/Model/ContactItem.cpp index 589ca9f..b542270 100644 --- a/src/Model/ContactItem.cpp +++ b/src/Model/ContactItem.cpp @@ -19,6 +19,7 @@ ContactItem::ContactItem(QString name, QString partnerAddress, QString myAddress _myAddress = myAddress; _partnerAddress = partnerAddress; _cid = cid; + } ContactItem::ContactItem(QString name, QString partnerAddress, QString myAddress, QString cid, QString avatar) @@ -28,6 +29,7 @@ ContactItem::ContactItem(QString name, QString partnerAddress, QString myAddress _partnerAddress = partnerAddress; _cid = cid; _avatar = avatar; + } QString ContactItem::getName() const diff --git a/src/Model/ContactItem.h b/src/Model/ContactItem.h index 63b4b39..e5e313d 100644 --- a/src/Model/ContactItem.h +++ b/src/Model/ContactItem.h @@ -16,6 +16,7 @@ private: QString _name; QString _cid; QString _avatar; + QString _pubkey; public: ContactItem(); diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index b033d7e..bc45d8b 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -255,6 +255,11 @@ void ChatModel::addCid(QString tx, QString cid) this->cidMap[tx] = cid; } +void ChatModel::addHeader(QString tx, QString headerbytes) +{ + this->headerMap[tx] = headerbytes; +} + void ChatModel::addrequestZaddr(QString tx, QString requestZaddr) { this->requestZaddrMap[tx] = requestZaddr; @@ -280,6 +285,21 @@ QString ChatModel::getCidByTx(QString tx) return QString("0xdeadbeef"); } +QString ChatModel::getHeaderByTx(QString tx) +{ + for(auto& pair : this->headerMap) + { + + } + + if(this->headerMap.count(tx) > 0) + { + return this->headerMap[tx]; + } + + return QString("0xdeadbeef"); +} + QString ChatModel::getConfirmationByTx(QString tx) { for(auto& pair : this->confirmationsMap) @@ -325,7 +345,7 @@ void ChatModel::killConfirmationCache() this->confirmationsMap.clear(); } -QString MainWindow::createHeaderMemo(QString type, QString cid, QString zaddr, int version=0, int headerNumber=1) +QString MainWindow::createHeaderMemo(QString type, QString cid, QString zaddr, QString headerbytes, QString publickey, int version=0, int headerNumber=1) { QString header=""; @@ -338,6 +358,9 @@ QString MainWindow::createHeaderMemo(QString type, QString cid, QString zaddr, h["z"] = zaddr; // zaddr to respond to h["cid"] = cid; // conversation id h["t"] = type; // Memo or incoming contact request + h["e"] = headerbytes; // Memo or incoming contact request + h["p"] = publickey; // Memo or incoming contact request + j.setObject(h); header = j.toJson(); @@ -346,6 +369,7 @@ QString MainWindow::createHeaderMemo(QString type, QString cid, QString zaddr, } + // Create a Tx from the current state of the Chat page. Tx MainWindow::createTxFromChatPage() { Tx tx; @@ -370,8 +394,7 @@ Tx MainWindow::createTxFromChatPage() { QString type = "Memo"; QString addr = c.getPartnerAddress(); - - QString hmemo= createHeaderMemo(type,cid,myAddr); + /////////User input for chatmemos QString memoplain = ui->memoTxtChat->toPlainText().trimmed(); @@ -390,58 +413,76 @@ Tx MainWindow::createTxFromChatPage() { cidchar = new char[lengthcid+1]; strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); -//////////////////////////////////////////////////Lets create Alice keys for the conversation/////////////////////////////////// + - /////////////////Alice Pubkey - #define MESSAGEAP ((const unsigned char *) cidchar)///////////static atm, in future we will use the CID here - #define MESSAGEAP_LEN lengthcid + QString pubkey = "test"; + QString passphrase = this->getPassword(); + QString hashEncryptionKey = passphrase; + int length = hashEncryptionKey.length(); - unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES]; + qDebug()<<"Encryption String :"<(ba2.constData()); - #define MESSAGEAS ((const unsigned char *) "Hallo")///////////static atm, in future we will use the Passphrase here - #define MESSAGEAS_LEN 5 + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) + #define MESSAGEAS1_LEN length + unsigned char hash[crypto_kx_SEEDBYTES]; - unsigned char alice_secretkey[crypto_box_SECRETKEYBYTES]; + crypto_hash_sha256(hash,MESSAGEAS1, MESSAGEAS1_LEN); - crypto_generichash(alice_secretkey, sizeof alice_secretkey, - MESSAGEAS, MESSAGEAS_LEN, - NULL, 0); - /////////////////Bob Pubkey that Alice creates - #define MESSAGEBAP ((const unsigned char *) "Hallo")///////////static atm, in future we will use the CID here - #define MESSAGEBAP_LEN 5 + unsigned char sk[crypto_kx_SECRETKEYBYTES]; + unsigned char pk[crypto_kx_PUBLICKEYBYTES]; + unsigned char server_rx[crypto_kx_SESSIONKEYBYTES], server_tx[crypto_kx_SESSIONKEYBYTES]; + + if (crypto_kx_seed_keypair(pk,sk, + hash) !=0) { + } + ////////////////Get the pubkey from Bob, so we can create the share key - unsigned char bob_publickey[crypto_box_PUBLICKEYBYTES]; + const QByteArray pubkeyBobArray = QByteArray::fromHex(pubkey.toLatin1()); + const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); + /////Create the shared key for sending the message - crypto_generichash(bob_publickey, sizeof bob_publickey, - MESSAGEBAP, MESSAGEBAP_LEN, - NULL, 0); + if (crypto_kx_server_session_keys(server_rx, server_tx, + pk, sk, pubkeyBob) != 0) { + /* Suspicious client public key, bail out */ + } - ////////////Now lets encrypt the message Alice send to Bob////////////////////////////// - #define MESSAGE (const unsigned char *) memoplainchar - #define MESSAGE_LEN lengthmemo - #define CIPHERTEXT_LEN (crypto_box_MACBYTES + MESSAGE_LEN) - unsigned char ciphertext[CIPHERTEXT_LEN]; - - //////Encrypt the message. ATM static keys, this will change! - if (crypto_box_easy(ciphertext, MESSAGE, MESSAGE_LEN, alice_publickey, - alice_publickey, alice_publickey) != 0) { - /* error */ - } - /////CIphertext Memo - QString memo = QByteArray(reinterpret_cast(ciphertext), CIPHERTEXT_LEN).toHex(); + + ////////////Now lets encrypt the message Alice send to Bob////////////////////////////// + #define MESSAGE (const unsigned char *) memoplainchar + #define MESSAGE_LEN lengthmemo + #define CIPHERTEXT_LEN (crypto_secretstream_xchacha20poly1305_ABYTES + MESSAGE_LEN) + unsigned char ciphertext[CIPHERTEXT_LEN]; + unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; + + crypto_secretstream_xchacha20poly1305_state state; + + /* Set up a new stream: initialize the state and create the header */ + crypto_secretstream_xchacha20poly1305_init_push(&state, header, server_tx); + + + /* Now, encrypt the first chunk. `c1` will contain an encrypted, + * authenticated representation of `MESSAGE_PART1`. */ + crypto_secretstream_xchacha20poly1305_push + (&state, ciphertext, NULL, MESSAGE, MESSAGE_LEN, NULL, 0, crypto_secretstream_xchacha20poly1305_TAG_FINAL); + + ////Create the HM for this message + QString headerbytes = QByteArray(reinterpret_cast(header), crypto_secretstream_xchacha20poly1305_HEADERBYTES).toHex(); + + QString hmemo= createHeaderMemo(type,cid,myAddr,"",headerbytes); + + /////Ciphertext Memo + QString memo = QByteArray(reinterpret_cast(ciphertext), CIPHERTEXT_LEN).toHex(); - tx.toAddrs.push_back(ToFields{addr, amt, hmemo}); - tx.toAddrs.push_back(ToFields{addr, amt, memo}); + tx.toAddrs.push_back(ToFields{addr, amt, hmemo}); + tx.toAddrs.push_back(ToFields{addr, amt, memo}); @@ -670,6 +711,7 @@ Tx MainWindow::createTxForSafeContactRequest() CAmount totalAmt; QString amtStr = "0"; CAmount amt; + QString headerbytes = ""; amt = CAmount::fromDecimalString("0"); totalAmt = totalAmt + amt; @@ -678,8 +720,38 @@ Tx MainWindow::createTxForSafeContactRequest() QString type = "Cont"; QString addr = contactRequest.getReceiverAddress(); - QString hmemo= createHeaderMemo(type,cid,myAddr); + QString memo = contactRequest.getMemo(); + // QString privkey = rpc->fetchPrivKey(myAddr); + QString passphrase = this->getPassword(); + QString hashEncryptionKey = passphrase; + int length = hashEncryptionKey.length(); + + qDebug()<<"Encryption String :"<(ba2.constData()); + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) + #define MESSAGEAS1_LEN length + + + unsigned char hash[crypto_kx_SEEDBYTES]; + + crypto_hash_sha256(hash,MESSAGEAS1, MESSAGEAS1_LEN); + + + unsigned char sk[crypto_kx_SECRETKEYBYTES]; + unsigned char pk[crypto_kx_PUBLICKEYBYTES]; + + if (crypto_kx_seed_keypair(pk,sk, + hash) !=0) { + } + + QString publicKey = QByteArray(reinterpret_cast(pk), crypto_kx_PUBLICKEYBYTES).toHex(); + + QString hmemo= createHeaderMemo(type,cid,myAddr,"", publicKey); tx.toAddrs.push_back(ToFields{addr, amt, hmemo}); diff --git a/src/chatmodel.h b/src/chatmodel.h index 1768b9f..b7203ad 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -35,6 +35,7 @@ class ChatModel std::map requestZaddrMap; std::map confirmationsMap; std::map> sendrequestMap; + std::map headerMap; std::map AddressbyLabelMap; public: @@ -52,10 +53,12 @@ class ChatModel void addMessage(ChatItem item); void addMessage(QString timestamp, ChatItem item); void addCid(QString tx, QString cid); + void addHeader(QString tx, QString headerbytes); void addrequestZaddr(QString tx, QString requestZaddr); void addconfirmations(QString tx, int confirmation); void addSendRequest(int i, QString myAddr, QString cid, QString addr ); QString getCidByTx(QString tx); + QString getHeaderByTx(QString tx); QString getrequestZaddrByTx(QString tx); QString getConfirmationByTx(QString tx); QString Addressbylabel(QString addr); diff --git a/src/controller.cpp b/src/controller.cpp index 342a24a..50bcdd6 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -907,27 +907,25 @@ void Controller::refreshTransactions() { this->refresh(true); } - // QString memo1; - QString memo; - QString cid; + QString cid; + QString headerbytes; + QString pubkey; if (!o["memo"].is_null()) { - QString memo = QString::fromStdString(o["memo"].get()); - + memo = QString::fromStdString(o["memo"].get()); + if (memo.startsWith("{")) { QJsonDocument headermemo = QJsonDocument::fromJson(memo.toUtf8()); cid = headermemo["cid"].toString(); - - + headerbytes = headermemo["e"].toString(); - chatModel->addCid(txid, cid); - + chatModel->addCid(txid, cid); + chatModel->addHeader(txid, headerbytes); } - - + bool isNotarized; if (confirmations > getLag()) @@ -942,62 +940,81 @@ void Controller::refreshTransactions() { cid = chatModel->getCidByTx(txid); + }else{ + cid = ""; + } + + + if (chatModel->getHeaderByTx(txid) != QString("0xdeadbeef")){ + + headerbytes = chatModel->getHeaderByTx(txid); + }else{ - cid = ""; + headerbytes = ""; } + qDebug()<<"Headerbytes :"< 120) && (memo.startsWith("{") == false)) + if ((memo.startsWith("{") == false) && (headerbytes > 0)) { + /*for (auto &p : AddressBook::getInstance()->getAllAddressLabels()) + { + + if (p.getPartnerAddress() == address) + { pubkey = p.getPubkey(); + }else {pubkey = ""; + }*/ + + //QString myAddr = p.getMyAddress(); + QString passphrase = main->getPassword(); + QString hashEncryptionKey = passphrase; + int length = hashEncryptionKey.length(); + + qDebug()<<"Encryption String :"<(ba2.constData()); + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) + #define MESSAGEAS1_LEN length + + + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw)///////////static atm, in future we will use the Passphrase here + #define MESSAGEAS1_LEN 12 + + + unsigned char hash1[crypto_kx_SECRETKEYBYTES]; + + crypto_hash_sha256(hash1,MESSAGEAS1, MESSAGEAS1_LEN); + unsigned char sk[crypto_kx_SECRETKEYBYTES]; + unsigned char pk[crypto_kx_PUBLICKEYBYTES]; + + if (crypto_kx_seed_keypair(pk,sk, + hash1) !=0) { + + + } + + unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; + - const QByteArray ba = QByteArray::fromHex(memo.toLatin1()); const unsigned char *encryptedMemo = reinterpret_cast(ba.constData()); + const QByteArray ba1 = QByteArray::fromHex(headerbytes.toLatin1()); + const unsigned char *header = reinterpret_cast(ba1.constData()); + int encryptedMemoSize1 = ba.length(); + int headersize = ba1.length(); //////unsigned char* as message from QString #define MESSAGE2 (const unsigned char *) encryptedMemo @@ -1006,20 +1023,25 @@ void Controller::refreshTransactions() { #define CIPHERTEXT1_LEN encryptedMemoSize1 ///////Message length is smaller then the encrypted message - #define MESSAGE1_LEN encryptedMemoSize1 - crypto_box_MACBYTES + #define MESSAGE1_LEN encryptedMemoSize1 - crypto_secretstream_xchacha20poly1305_ABYTES //////Set the length of the decrypted message unsigned char decrypted[MESSAGE1_LEN]; + unsigned char tag[crypto_secretstream_xchacha20poly1305_TAG_FINAL]; + crypto_secretstream_xchacha20poly1305_state state; - ///////Decrypt the message - if (crypto_box_open_easy(decrypted, MESSAGE2, CIPHERTEXT1_LEN, alice_publickey, - alice_publickey, alice_publickey) != 0) { - /* message for Bob pretending to be from Alice has been forged! */ - } - /////Our decrypted message is now in decrypted. We need it as QString to render it /////Only the QString gives weird data, so convert first to std::string + // crypto_secretstream_xchacha20poly1305_keygen(client_rx); + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, client_rx) != 0) { + /* Invalid header, no need to go any further */ + } + + if (crypto_secretstream_xchacha20poly1305_pull + (&state, decrypted, NULL, tag, MESSAGE2, CIPHERTEXT1_LEN, NULL, 0) != 0) { + /* Invalid/incomplete/corrupted ciphertext - abort */ + } std::string decryptedMemo(reinterpret_cast(decrypted),MESSAGE1_LEN); @@ -1047,7 +1069,7 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - + // } }else{ @@ -1068,8 +1090,8 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); } + } - } items.push_back(TransactionItemDetail{address, amount, memo}); total_amount = total_amount + amount; } @@ -1114,6 +1136,8 @@ void Controller::refreshTransactions() { txdata.push_back(tx); QString type; + QString publickey; + QString headerbytes; QString cid; int position; QString requestZaddr; @@ -1149,9 +1173,12 @@ void Controller::refreshTransactions() { cid = headermemo["cid"].toString(); type = headermemo["t"].toString(); requestZaddr = headermemo["z"].toString(); + headerbytes = headermemo["e"].toString(); + publickey = headermemo["p"].toString(); chatModel->addCid(txid, cid); chatModel->addrequestZaddr(txid, requestZaddr); + chatModel->addHeader(txid, headerbytes); } @@ -1169,9 +1196,18 @@ void Controller::refreshTransactions() { requestZaddr = chatModel->getrequestZaddrByTx(txid); }else{ requestZaddr = ""; - } + } - position = it["position"].get(); + + if (chatModel->getHeaderByTx(txid) != QString("0xdeadbeef")){ + + headerbytes = chatModel->getHeaderByTx(txid); + + }else{ + headerbytes = ""; + } + + //position = it["position"].get(); bool isNotarized; @@ -1183,7 +1219,90 @@ void Controller::refreshTransactions() { isNotarized = false; } - ChatItem item = ChatItem( + + int lengthcid = cid.length(); + + char *cidchar = NULL; + cidchar = new char[lengthcid+1]; + strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); + + if ((memo.startsWith("{") == false) && (headerbytes > 0)) + { + + #define MESSAGEAS ((const unsigned char *) cidchar) + #define MESSAGEAS_LEN lengthcid + + unsigned char hash[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + + crypto_hash_sha256(hash,MESSAGEAS, MESSAGEAS_LEN); + + const QByteArray ba = QByteArray::fromHex(memo.toLatin1()); + const unsigned char *encryptedMemo = reinterpret_cast(ba.constData()); + + const QByteArray ba1 = QByteArray::fromHex(headerbytes.toLatin1()); + const unsigned char *header = reinterpret_cast(ba1.constData()); + + int encryptedMemoSize1 = ba.length(); + int headersize = ba1.length(); + + //////unsigned char* as message from QString + #define MESSAGE2 (const unsigned char *) encryptedMemo + + ///////// length of the encrypted message + #define CIPHERTEXT1_LEN encryptedMemoSize1 + + ///////Message length is smaller then the encrypted message + #define MESSAGE1_LEN encryptedMemoSize1 - crypto_secretstream_xchacha20poly1305_ABYTES + + //////Set the length of the decrypted message + + unsigned char decrypted[MESSAGE1_LEN]; + unsigned char tag[crypto_secretstream_xchacha20poly1305_TAG_FINAL]; + crypto_secretstream_xchacha20poly1305_state state; + + /////Our decrypted message is now in decrypted. We need it as QString to render it + /////Only the QString gives weird data, so convert first to std::string + + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, hash) != 0) { + /* Invalid header, no need to go any further */ + } + + if (crypto_secretstream_xchacha20poly1305_pull + (&state, decrypted, NULL, tag, MESSAGE2, CIPHERTEXT1_LEN, NULL, 0) != 0) { + /* Invalid/incomplete/corrupted ciphertext - abort */ + } + + std::string decryptedMemo(reinterpret_cast(decrypted),MESSAGE1_LEN); + + /////Now we can convert it to QString + QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + + //////////////Give us the output of the decrypted message as debug to see if it was successfully + qDebug()<<"OUT decrypt:" << memodecrypt; + + + ChatItem item = ChatItem( + datetime, + address, + QString(""), + memodecrypt, + requestZaddr, + type, + cid, + txid, + confirmations, + false, + isNotarized, + isContact + ); + DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); + + + + }else{ + + + ChatItem item = ChatItem( datetime, address, QString(""), @@ -1197,9 +1316,10 @@ void Controller::refreshTransactions() { isNotarized, isContact ); - - DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - } + DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); + + } + } } } } diff --git a/src/mainwindow.h b/src/mainwindow.h index 56bfbc6..815fd84 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -90,7 +90,7 @@ public: void doClose(); void doClosePw(); - QString createHeaderMemo(QString type, QString cid, QString zaddr, int version, int headerNumber); + QString createHeaderMemo(QString type, QString cid, QString zaddr,QString headerbytes,QString publickey, int version, int headerNumber); public slots: void slot_change_theme(const QString& themeName); From 74de61d265caa7e6c21e2b739b25d8350e15c309 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 22:21:17 +0200 Subject: [PATCH 08/15] add pubkey cache --- src/chatmodel.cpp | 12 ++++++++++-- src/chatmodel.h | 1 + src/controller.cpp | 28 +++++++++++++++++++++++----- src/mainwindow.cpp | 20 ++++++++++++++++++++ src/mainwindow.h | 5 +++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index bc45d8b..f29be0f 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -260,6 +260,8 @@ void ChatModel::addHeader(QString tx, QString headerbytes) this->headerMap[tx] = headerbytes; } + + void ChatModel::addrequestZaddr(QString tx, QString requestZaddr) { this->requestZaddrMap[tx] = requestZaddr; @@ -285,6 +287,9 @@ QString ChatModel::getCidByTx(QString tx) return QString("0xdeadbeef"); } + + + QString ChatModel::getHeaderByTx(QString tx) { for(auto& pair : this->headerMap) @@ -415,7 +420,7 @@ Tx MainWindow::createTxFromChatPage() { - QString pubkey = "test"; + QString pubkey = this->getPubkeyByAddress(addr); QString passphrase = this->getPassword(); QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); @@ -474,8 +479,9 @@ Tx MainWindow::createTxFromChatPage() { ////Create the HM for this message QString headerbytes = QByteArray(reinterpret_cast(header), crypto_secretstream_xchacha20poly1305_HEADERBYTES).toHex(); + QString publickeyAlice = QByteArray(reinterpret_cast(pk), crypto_kx_PUBLICKEYBYTES).toHex(); - QString hmemo= createHeaderMemo(type,cid,myAddr,"",headerbytes); + QString hmemo= createHeaderMemo(type,cid,myAddr,publickeyAlice,headerbytes); /////Ciphertext Memo QString memo = QByteArray(reinterpret_cast(ciphertext), CIPHERTEXT_LEN).toHex(); @@ -751,6 +757,8 @@ Tx MainWindow::createTxForSafeContactRequest() QString publicKey = QByteArray(reinterpret_cast(pk), crypto_kx_PUBLICKEYBYTES).toHex(); + qDebug()<<"Publickey created Request: "<> sendrequestMap; std::map headerMap; std::map AddressbyLabelMap; + public: ChatModel() {}; diff --git a/src/controller.cpp b/src/controller.cpp index 50bcdd6..0f5b4f3 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -910,7 +910,7 @@ void Controller::refreshTransactions() { QString memo; QString cid; QString headerbytes; - QString pubkey; + QString publickey; if (!o["memo"].is_null()) { memo = QString::fromStdString(o["memo"].get()); @@ -953,7 +953,15 @@ void Controller::refreshTransactions() { headerbytes = ""; } - qDebug()<<"Headerbytes :"<getPubkeyByAddress(address) != QString("0xdeadbeef")){ + + publickey = main->getPubkeyByAddress(address); + + }else{ + publickey = ""; + } + + qDebug()<<"Pubkey :"<addCid(txid, cid); chatModel->addrequestZaddr(txid, requestZaddr); chatModel->addHeader(txid, headerbytes); + main->addPubkey(requestZaddr, publickey); - } + } if (chatModel->getCidByTx(txid) != QString("0xdeadbeef")){ @@ -1207,6 +1216,14 @@ void Controller::refreshTransactions() { headerbytes = ""; } + if (main->getPubkeyByAddress(requestZaddr) != QString("0xdeadbeef")){ + + publickey = main->getPubkeyByAddress(requestZaddr); + + }else{ + publickey = ""; + } + //position = it["position"].get(); bool isNotarized; @@ -1226,7 +1243,7 @@ void Controller::refreshTransactions() { cidchar = new char[lengthcid+1]; strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); - if ((memo.startsWith("{") == false) && (headerbytes > 0)) + if ((memo.startsWith("{") == false) && (headerbytes > 0)) { #define MESSAGEAS ((const unsigned char *) cidchar) @@ -1298,7 +1315,7 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - + }else{ @@ -1320,6 +1337,7 @@ void Controller::refreshTransactions() { } } + } } } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 27c567e..bd12d90 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1062,6 +1062,26 @@ void MainWindow::exportSeed() { }); } +void MainWindow::addPubkey(QString requestZaddr, QString pubkey) +{ + this->pubkeyMap[requestZaddr] = pubkey; +} + +QString MainWindow::getPubkeyByAddress(QString requestZaddr) +{ + for(auto& pair : this->pubkeyMap) + { + + } + + if(this->pubkeyMap.count(requestZaddr) > 0) + { + return this->pubkeyMap[requestZaddr]; + } + + return QString("0xdeadbeef"); +} + void MainWindow::exportAllKeys() { exportKeys(""); } diff --git a/src/mainwindow.h b/src/mainwindow.h index 815fd84..8c63540 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -12,6 +12,7 @@ class Controller; class Settings; class WSServer; class WormholeClient; +class ChatModel; using json = nlohmann::json; @@ -52,7 +53,11 @@ public: QString doSendRequestTxValidations(Tx tx); QString getCid(); QString getPassword(); + std::map pubkeyMap; + QString getPubkeyByAddress(QString requestZaddr); void setPassword(QString Password); + void addPubkey(QString requestZaddr, QString pubkey); + void replaceWormholeClient(WormholeClient* newClient); bool isWebsocketListening(); From 875d1a16a13bf3c744f30154f3e9191107263bea Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 22:32:38 +0200 Subject: [PATCH 09/15] add pubkey cache --- src/controller.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/controller.cpp b/src/controller.cpp index 0f5b4f3..f4475cd 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -957,6 +957,8 @@ void Controller::refreshTransactions() { publickey = main->getPubkeyByAddress(address); + qDebug()<<"Pubkey outgoing found"<addCid(txid, cid); chatModel->addrequestZaddr(txid, requestZaddr); chatModel->addHeader(txid, headerbytes); + + if (publickey.length() > 0){ main->addPubkey(requestZaddr, publickey); + } } @@ -1336,7 +1341,7 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); } - } + } } } From 173ec6ccdc7bb9f2f075a0ec3a605e6fcd1a6b21 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Mon, 1 Jun 2020 22:47:13 +0200 Subject: [PATCH 10/15] fix for incoming memos --- src/controller.cpp | 69 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index f4475cd..a4f315d 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -995,10 +995,13 @@ void Controller::refreshTransactions() { const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) - #define MESSAGEAS1_LEN length + #define MESSAGEAS1_LEN length + + const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); + const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); - #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw)///////////static atm, in future we will use the Passphrase here + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw)/////////// #define MESSAGEAS1_LEN 12 @@ -1015,6 +1018,17 @@ void Controller::refreshTransactions() { } unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; + + + ////////////////Get the pubkey from Bob, so we can create the share key + + + /////Create the shared key for sending the message + + if (crypto_kx_server_session_keys(client_rx, client_tx, + pk, sk, pubkeyBob) != 0) { + /* Suspicious client public key, bail out */ + } const QByteArray ba = QByteArray::fromHex(memo.toLatin1()); @@ -1243,6 +1257,11 @@ void Controller::refreshTransactions() { int lengthcid = cid.length(); + QString passphrase = main->getPassword(); + QString hashEncryptionKey = passphrase; + int length = hashEncryptionKey.length(); + + qDebug()<<"Encryption String :"< 0)) { - #define MESSAGEAS ((const unsigned char *) cidchar) - #define MESSAGEAS_LEN lengthcid + const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); + const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) + #define MESSAGEAS1_LEN length - unsigned char hash[crypto_secretstream_xchacha20poly1305_KEYBYTES]; + const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); + const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); - crypto_hash_sha256(hash,MESSAGEAS, MESSAGEAS_LEN); + + #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw)/////////// + #define MESSAGEAS1_LEN 12 + + + unsigned char hash1[crypto_kx_SECRETKEYBYTES]; + + crypto_hash_sha256(hash1,MESSAGEAS1, MESSAGEAS1_LEN); + unsigned char sk[crypto_kx_SECRETKEYBYTES]; + unsigned char pk[crypto_kx_PUBLICKEYBYTES]; + + if (crypto_kx_seed_keypair(pk,sk, + hash1) !=0) { + + + } + + unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; + + + ////////////////Get the pubkey from Bob, so we can create the share key + + + /////Create the shared key for sending the message + + if (crypto_kx_server_session_keys(client_rx, client_tx, + pk, sk, pubkeyBob) != 0) { + /* Suspicious client public key, bail out */ + } + const QByteArray ba = QByteArray::fromHex(memo.toLatin1()); const unsigned char *encryptedMemo = reinterpret_cast(ba.constData()); @@ -1284,8 +1335,8 @@ void Controller::refreshTransactions() { /////Our decrypted message is now in decrypted. We need it as QString to render it /////Only the QString gives weird data, so convert first to std::string - - if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, hash) != 0) { + // crypto_secretstream_xchacha20poly1305_keygen(client_rx); + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, client_rx) != 0) { /* Invalid header, no need to go any further */ } @@ -1300,7 +1351,7 @@ void Controller::refreshTransactions() { QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); //////////////Give us the output of the decrypted message as debug to see if it was successfully - qDebug()<<"OUT decrypt:" << memodecrypt; + qDebug()<<"OUT decrypt:" << memodecrypt; ChatItem item = ChatItem( From 1aed6e9ed004d11e2d8669ee462651541035ce57 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Tue, 2 Jun 2020 00:26:06 +0200 Subject: [PATCH 11/15] change server to client --- src/chatmodel.cpp | 8 +++++--- src/controller.cpp | 49 +++++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index f29be0f..69751ad 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -422,10 +422,10 @@ Tx MainWindow::createTxFromChatPage() { QString pubkey = this->getPubkeyByAddress(addr); QString passphrase = this->getPassword(); - QString hashEncryptionKey = passphrase; + QString hashEncryptionKey = "Test"; int length = hashEncryptionKey.length(); - qDebug()<<"Encryption String :"<(header), crypto_secretstream_xchacha20poly1305_HEADERBYTES).toHex(); QString publickeyAlice = QByteArray(reinterpret_cast(pk), crypto_kx_PUBLICKEYBYTES).toHex(); - QString hmemo= createHeaderMemo(type,cid,myAddr,publickeyAlice,headerbytes); + qDebug()<<"Headerbyte erstellung : "<(ciphertext), CIPHERTEXT_LEN).toHex(); diff --git a/src/controller.cpp b/src/controller.cpp index a4f315d..cd1c3e4 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -948,6 +948,7 @@ void Controller::refreshTransactions() { if (chatModel->getHeaderByTx(txid) != QString("0xdeadbeef")){ headerbytes = chatModel->getHeaderByTx(txid); + qDebug()<<"headerbytes outgoing found"<getPassword(); - QString hashEncryptionKey = passphrase; + QString hashEncryptionKey = "Test"; int length = hashEncryptionKey.length(); qDebug()<<"Encryption String :"<(ba2.constData()); - #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) - #define MESSAGEAS1_LEN length - const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); - const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); + const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); + const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); + + qDebug()<<"Pubkey benutzt incoming transaction:"<(ba1.constData()); + qDebug()<<"Header benutzt incoming transaction:"<getHeaderByTx(txid) != QString("0xdeadbeef")){ headerbytes = chatModel->getHeaderByTx(txid); + qDebug()<<"HEADERBYTE INCOMING"<getPassword(); - QString hashEncryptionKey = passphrase; + int lengthcid = cid.length(); + QString passphrase = main->getPassword(); + QString hashEncryptionKey = "Test"; int length = hashEncryptionKey.length(); qDebug()<<"Encryption String :"<(ba2.constData()); - #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) - #define MESSAGEAS1_LEN length - - const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); + + const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw)/////////// - #define MESSAGEAS1_LEN 12 + #define MESSAGEAS1_LEN length unsigned char hash1[crypto_kx_SECRETKEYBYTES]; @@ -1303,7 +1302,7 @@ void Controller::refreshTransactions() { /////Create the shared key for sending the message - if (crypto_kx_server_session_keys(client_rx, client_tx, + if (crypto_kx_client_session_keys(client_rx, client_tx, pk, sk, pubkeyBob) != 0) { /* Suspicious client public key, bail out */ } From 04575afd5a1eb64036595c22832129ac6c5aa5d3 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Tue, 2 Jun 2020 01:24:12 +0200 Subject: [PATCH 12/15] change decrypt key to shared --- src/controller.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index cd1c3e4..66b7823 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -1239,6 +1239,8 @@ void Controller::refreshTransactions() { if (main->getPubkeyByAddress(requestZaddr) != QString("0xdeadbeef")){ publickey = main->getPubkeyByAddress(requestZaddr); + qDebug()<<"Incoming Pubkey :"<(ba.constData()); @@ -1335,7 +1337,7 @@ void Controller::refreshTransactions() { /////Our decrypted message is now in decrypted. We need it as QString to render it /////Only the QString gives weird data, so convert first to std::string // crypto_secretstream_xchacha20poly1305_keygen(client_rx); - if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, client_rx) != 0) { + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, server_tx) != 0) { /* Invalid header, no need to go any further */ } From ce3120dcd1a60ef584f8f62d1274a064bbe6bf38 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Tue, 2 Jun 2020 01:56:28 +0200 Subject: [PATCH 13/15] we use seedbytes --- src/chatmodel.cpp | 13 +++++++------ src/controller.cpp | 19 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 69751ad..5df7693 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -429,8 +429,9 @@ Tx MainWindow::createTxFromChatPage() { ////////////////Generate the secretkey for our message encryption - const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); - const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); + char *hashEncryptionKeyraw = NULL; + hashEncryptionKeyraw = new char[length+1]; + strncpy(hashEncryptionKeyraw, hashEncryptionKey.toLocal8Bit(), length +1); #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) #define MESSAGEAS1_LEN length @@ -732,15 +733,15 @@ Tx MainWindow::createTxForSafeContactRequest() QString memo = contactRequest.getMemo(); // QString privkey = rpc->fetchPrivKey(myAddr); QString passphrase = this->getPassword(); - QString hashEncryptionKey = passphrase; + QString hashEncryptionKey = "Test"; int length = hashEncryptionKey.length(); qDebug()<<"Encryption String :"<(ba2.constData()); + char *hashEncryptionKeyraw = NULL; + hashEncryptionKeyraw = new char[length+1]; + strncpy(hashEncryptionKeyraw, hashEncryptionKey.toLocal8Bit(), length +1); #define MESSAGEAS1 ((const unsigned char *) hashEncryptionKeyraw) #define MESSAGEAS1_LEN length diff --git a/src/controller.cpp b/src/controller.cpp index 66b7823..6ca80cd 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -993,8 +993,9 @@ void Controller::refreshTransactions() { ////////////////Generate the secretkey for our message encryption - const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); - const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); + char *hashEncryptionKeyraw = NULL; + hashEncryptionKeyraw = new char[length+1]; + strncpy(hashEncryptionKeyraw, hashEncryptionKey.toLocal8Bit(), length +1); const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); @@ -1006,7 +1007,7 @@ void Controller::refreshTransactions() { #define MESSAGEAS1_LEN length - unsigned char hash1[crypto_kx_SECRETKEYBYTES]; + unsigned char hash1[crypto_kx_SEEDBYTES]; crypto_hash_sha256(hash1,MESSAGEAS1, MESSAGEAS1_LEN); unsigned char sk[crypto_kx_SECRETKEYBYTES]; @@ -1266,15 +1267,15 @@ void Controller::refreshTransactions() { qDebug()<<"Encryption String :"< 0)) { - const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); - const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); + //const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); + // const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); const QByteArray pubkeyBobArray = QByteArray::fromHex(publickey.toLatin1()); const unsigned char *pubkeyBob = reinterpret_cast(pubkeyBobArray.constData()); @@ -1284,7 +1285,7 @@ void Controller::refreshTransactions() { #define MESSAGEAS1_LEN length - unsigned char hash1[crypto_kx_SECRETKEYBYTES]; + unsigned char hash1[crypto_kx_SEEDBYTES]; crypto_hash_sha256(hash1,MESSAGEAS1, MESSAGEAS1_LEN); unsigned char sk[crypto_kx_SECRETKEYBYTES]; From 4b7af93e0d8a6a6b2c86aca1a96331672679f0b0 Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Tue, 2 Jun 2020 18:10:19 +0200 Subject: [PATCH 14/15] on/off switch for rendering encrypted messages --- src/addressbook.cpp | 4 +- src/chatmodel.cpp | 6 +-- src/controller.cpp | 97 +++++++++++++++++++++++++++------------------ src/mainwindow.ui | 14 +++++++ 4 files changed, 78 insertions(+), 43 deletions(-) diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 591a9c0..ae81ae2 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -392,7 +392,7 @@ void AddressBook::readFromStorage() QDataStream in(&file); // read the data serialized from the file QString version; in >> version; - qDebug() << "Detected old addressbook format"; + // qDebug() << "Detected old addressbook format"; // Convert old addressbook format v1 to v2 QList> stuff; in >> stuff; @@ -408,7 +408,7 @@ void AddressBook::readFromStorage() allLabels.push_back(contact); } - qDebug() << "Read " << version << " Hush contacts from disk..."; + // qDebug() << "Read " << version << " Hush contacts from disk..."; file.close(); } else diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 5df7693..8d5d32f 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -422,7 +422,7 @@ Tx MainWindow::createTxFromChatPage() { QString pubkey = this->getPubkeyByAddress(addr); QString passphrase = this->getPassword(); - QString hashEncryptionKey = "Test"; + QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); qDebug()<<"Pubkey Erstellung :"<refresh(true); + rpc->refresh(true); } QString MainWindow::doSendChatTxValidations(Tx tx) { @@ -733,7 +733,7 @@ Tx MainWindow::createTxForSafeContactRequest() QString memo = contactRequest.getMemo(); // QString privkey = rpc->fetchPrivKey(myAddr); QString passphrase = this->getPassword(); - QString hashEncryptionKey = "Test"; + QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); qDebug()<<"Encryption String :"<getHeaderByTx(txid) != QString("0xdeadbeef")){ headerbytes = chatModel->getHeaderByTx(txid); - qDebug()<<"headerbytes outgoing found"<getPubkeyByAddress(address); - qDebug()<<"Pubkey outgoing found"< 0)) { - /*for (auto &p : AddressBook::getInstance()->getAllAddressLabels()) - { - - if (p.getPartnerAddress() == address) - { pubkey = p.getPubkey(); - }else {pubkey = ""; - }*/ + //QString myAddr = p.getMyAddress(); QString passphrase = main->getPassword(); - QString hashEncryptionKey = "Test"; + QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); - qDebug()<<"Encryption String :"<(pubkeyBobArray.constData()); - qDebug()<<"Pubkey benutzt incoming transaction:"<(ba1.constData()); - qDebug()<<"Header benutzt incoming transaction:"<(decrypted),MESSAGE1_LEN); - + QString memodecrypt; /////Now we can convert it to QString - QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + if (ui->decryptionMessage->isChecked()){ + memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + DataStore::getChatDataStore()->clear(); + this->refresh(true); + chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); + }else{ memodecrypt = memo; + DataStore::getChatDataStore()->clear(); + this->refresh(true); + chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); + } + //////////////Give us the output of the decrypted message as debug to see if it was successfully qDebug()<<"OUT decrypt:" << memodecrypt; @@ -1094,7 +1098,7 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - // } + }else{ @@ -1143,7 +1147,6 @@ void Controller::refreshTransactions() { model->markAddressUsed(address); QString memo; - QString test; if (!it["memo"].is_null()) { memo = QString::fromStdString(it["memo"]); } @@ -1205,10 +1208,11 @@ void Controller::refreshTransactions() { chatModel->addrequestZaddr(txid, requestZaddr); chatModel->addHeader(txid, headerbytes); - if (publickey.length() > 0){ + if (publickey.length() > 10){ main->addPubkey(requestZaddr, publickey); } - + qDebug()<<"Scane HM Incoming:"; + qDebug()<<"Scane HM Incoming:"<getHeaderByTx(txid) != QString("0xdeadbeef")){ headerbytes = chatModel->getHeaderByTx(txid); - qDebug()<<"HEADERBYTE INCOMING"<getPubkeyByAddress(requestZaddr) != QString("0xdeadbeef")){ publickey = main->getPubkeyByAddress(requestZaddr); - qDebug()<<"Incoming Pubkey :"< 20)) + { + + int lengthcid = cid.length(); QString passphrase = main->getPassword(); - QString hashEncryptionKey = "Test"; + QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); - qDebug()<<"Encryption String :"< 0)) - { - //const QByteArray ba2 = QByteArray::fromHex(hashEncryptionKey.toLatin1()); // const unsigned char *hashEncryptionKeyraw = reinterpret_cast(ba2.constData()); @@ -1296,7 +1301,7 @@ void Controller::refreshTransactions() { } - unsigned char server_rx[crypto_kx_SESSIONKEYBYTES], server_tx[crypto_kx_SESSIONKEYBYTES]; + unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; ////////////////Get the pubkey from Bob, so we can create the share key @@ -1304,7 +1309,7 @@ void Controller::refreshTransactions() { /////Create the shared key for sending the message - if (crypto_kx_server_session_keys(server_rx, server_tx, + if (crypto_kx_client_session_keys(client_rx, client_tx, pk, sk, pubkeyBob) != 0) { /* Suspicious client public key, bail out */ } @@ -1338,7 +1343,7 @@ void Controller::refreshTransactions() { /////Our decrypted message is now in decrypted. We need it as QString to render it /////Only the QString gives weird data, so convert first to std::string // crypto_secretstream_xchacha20poly1305_keygen(client_rx); - if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, server_tx) != 0) { + if (crypto_secretstream_xchacha20poly1305_init_pull(&state, header, client_rx) != 0) { /* Invalid header, no need to go any further */ } @@ -1350,10 +1355,22 @@ void Controller::refreshTransactions() { std::string decryptedMemo(reinterpret_cast(decrypted),MESSAGE1_LEN); /////Now we can convert it to QString - QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + QString memodecrypt; + if (ui->decryptionMessage->isChecked()){ + memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); + DataStore::getChatDataStore()->clear(); + this->refresh(true); + chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); + }else{ memodecrypt = memo; + DataStore::getChatDataStore()->clear(); + this->refresh(true); + chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); + } + + // } //////////////Give us the output of the decrypted message as debug to see if it was successfully - qDebug()<<"OUT decrypt:" << memodecrypt; + ChatItem item = ChatItem( @@ -1372,12 +1389,13 @@ void Controller::refreshTransactions() { ); DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); + qDebug()<<"Pushe decrypte Items:"; }else{ - - ChatItem item = ChatItem( + qDebug()<<"Pushe plain Items 1:"; + ChatItem item = ChatItem( datetime, address, QString(""), @@ -1392,13 +1410,15 @@ void Controller::refreshTransactions() { isContact ); DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); + + qDebug()<<"Pushe Plain items 2:"; } } } - } } + } // Calculate the total unspent amount that's pending. This will need to be // shown in the UI so the user can keep track of pending funds @@ -1410,7 +1430,8 @@ void Controller::refreshTransactions() { } } } - getModel()->setTotalPending(totalPending); + + getModel()->setTotalPending(totalPending); // Update UI Balance updateUIBalances(); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index de58411..a715861 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -1703,6 +1703,19 @@ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + 900 + 10 + 281 + 23 + + + + Message encryption/decryption ON + + listContactWidget label_39 contactNameMemo @@ -1714,6 +1727,7 @@ listChat memoTxtChat sendChatButton + decryptionMessage From 8ffed6683c012ca87e14d5aa030f4ac2ca95607c Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Tue, 2 Jun 2020 20:19:18 +0200 Subject: [PATCH 15/15] dont refresh to often --- src/DataStore/ChatDataStore.cpp | 3 ++- src/chatmodel.cpp | 2 +- src/controller.cpp | 35 +++++++++++++-------------------- src/controller.h | 1 + src/mainwindow.cpp | 4 +++- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/DataStore/ChatDataStore.cpp b/src/DataStore/ChatDataStore.cpp index 7bb3840..7b03c0e 100644 --- a/src/DataStore/ChatDataStore.cpp +++ b/src/DataStore/ChatDataStore.cpp @@ -60,7 +60,8 @@ std::map ChatDataStore::getAllNewContactRequests() if ( (c.second.isOutgoing() == false) && (c.second.getType() == "Cont") && - (c.second.isContact() == false) + (c.second.isContact() == false) && + (c.second.getMemo().isEmpty()) ) { diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 8d5d32f..2358ae9 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -618,7 +618,7 @@ void MainWindow::sendChat() { } ); - rpc->refresh(true); + // rpc->refresh(true); } QString MainWindow::doSendChatTxValidations(Tx tx) { diff --git a/src/controller.cpp b/src/controller.cpp index 4c7edfe..66c00b8 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -963,22 +963,12 @@ void Controller::refreshTransactions() { }else{ publickey = ""; } - - - - int lengthcid = cid.length(); - - char *cidchar = NULL; - cidchar = new char[lengthcid+1]; - strncpy(cidchar, cid.toLocal8Bit(), lengthcid +1); /////We need to filter out Memos smaller then the ciphertext size, or it will dump if ((memo.startsWith("{") == false) && (headerbytes > 0)) { - - //QString myAddr = p.getMyAddress(); QString passphrase = main->getPassword(); QString hashEncryptionKey = passphrase; int length = hashEncryptionKey.length(); @@ -1068,11 +1058,11 @@ void Controller::refreshTransactions() { if (ui->decryptionMessage->isChecked()){ memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); DataStore::getChatDataStore()->clear(); - this->refresh(true); + // this->refresh(true); chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); }else{ memodecrypt = memo; DataStore::getChatDataStore()->clear(); - this->refresh(true); + // this->refresh(true); chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); } @@ -1097,7 +1087,7 @@ void Controller::refreshTransactions() { ); DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - + updateUIBalances(); }else{ @@ -1117,7 +1107,7 @@ void Controller::refreshTransactions() { false ); DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); - + updateUIBalances(); } } @@ -1181,7 +1171,7 @@ void Controller::refreshTransactions() { chatModel->addAddressbylabel(address, requestZaddr); } else{} - + } if (chatModel->Addressbylabel(address) != QString("0xdeadbeef")){ isContact = true; @@ -1359,12 +1349,15 @@ void Controller::refreshTransactions() { if (ui->decryptionMessage->isChecked()){ memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); DataStore::getChatDataStore()->clear(); - this->refresh(true); - chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); - }else{ memodecrypt = memo; - DataStore::getChatDataStore()->clear(); - this->refresh(true); + // this->refresh(true); chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); + + }else{ + + memodecrypt = memo; + DataStore::getChatDataStore()->clear(); + //this->refresh(true); + chat->renderChatBox(ui, ui->listChat,ui->memoSizeChat); } @@ -1415,7 +1408,7 @@ void Controller::refreshTransactions() { } - } + // } } } } diff --git a/src/controller.h b/src/controller.h index 9fd445b..f66596f 100644 --- a/src/controller.h +++ b/src/controller.h @@ -80,6 +80,7 @@ public: void refreshGBPCAP(); void refreshAUDCAP(); + void refreshChat(QListView *listWidget, QLabel *label); void refreshContacts(QListView *listWidget); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bd12d90..4984811 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1357,7 +1357,9 @@ void MainWindow::setupchatTab() { ui->memoTxtChat->setTextColor("Black"); } - + + ui->decryptionMessage->setChecked(true); + QObject::connect(ui->sendChatButton, &QPushButton::clicked, this, &MainWindow::sendChat); QObject::connect(ui->safeContactRequest, &QPushButton::clicked, this, &MainWindow::addContact); QObject::connect(ui->pushContact, &QPushButton::clicked, this , &MainWindow::renderContactRequest);