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] comments for debug issues --- src/chatmodel.cpp | 201 ++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 105 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); - QString memounencrypt = ui->memoTxtChat->toPlainText().trimmed(); + /////////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////////////////////////////// + + 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); -/* Prerequisite after this point: the client's public key must be known by the server */ + /* 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) { + /* 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]; -/* Shared secret key required to encrypt/decrypt the stream */ -crypto_secretstream_xchacha20poly1305_keygen(alice_publickey); + unsigned char c1[CIPHERTEXT_LEN]; -/* Set up a new stream: initialize the state and create the header */ -crypto_secretstream_xchacha20poly1305_init_push(&state, bob_publickey, alice_publickey); + /* Shared secret key required to encrypt/decrypt the stream */ + crypto_secretstream_xchacha20poly1305_keygen(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); + /* 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); -///////////get a std:string....QString will give us not good results + ///////////Ciphertext is now in c1 as unsigned char*, we need it as QString, to send it as memo to Bob////////////////////// - std::string encryptedMemo(reinterpret_cast(c1), CIPHERTEXT_LEN); + /////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 Controller Memo :"<< encryptedMemo.length(); + qDebug()<<"Size QString with encrypted data :"<< memo.length(); ////We check the length here, to compare it with the length our QString - QString memo = QString::fromUtf8( encryptedMemo.data(), encryptedMemo.size()); + ///////Just for testing we convert the unsigned char* to std::string, to see if we can decrypt,and that works.//////////// - - /////////////////Bob Pubkey - #define MESSAGEBAP1 ((const unsigned char *) "Hal12") + 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]; @@ -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]; - -/* Generate the client's key pair */ -crypto_kx_keypair(bob1_publickey, bob_secretkey); +////////////Now we create the shared keys for Bob/////////////////////////////////////////////// -/* Prerequisite after this point: the server's public key must be known by the client */ + unsigned char client_rx[crypto_kx_SESSIONKEYBYTES], client_tx[crypto_kx_SESSIONKEYBYTES]; -/* 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 */ -} + /* Generate the client's key pair */ + crypto_kx_keypair(bob1_publickey, bob_secretkey); -qDebug()<<"1 : "; + /* Prerequisite after this point: the server's public key must be known by the client */ + /* 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 : "<(m2),MESSAGE_LEN); - qDebug()<<"7: "; - QString memodecrypt = QString::fromUtf8( decryptedMemo.data(), decryptedMemo.size()); - + /////Our decrypted message is now in m. 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(m),MESSAGE1_LEN); + /////Now we can convert it to QString + 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});