From ee8e5b9dd592c32bdcf059a241a92c1e539474be Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 7 Jul 2019 16:57:21 +0200 Subject: [PATCH] Fixec crypt errors --- .build_win32_amd64.txt | 4 ++++ src/misc/digest.h | 18 ++++++++-------- src/protocol/CryptionHandler.cpp | 36 +++++++++++++++++--------------- src/protocol/CryptionHandler.h | 9 ++++++-- 4 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 .build_win32_amd64.txt diff --git a/.build_win32_amd64.txt b/.build_win32_amd64.txt new file mode 100644 index 0000000..fe1e359 --- /dev/null +++ b/.build_win32_amd64.txt @@ -0,0 +1,4 @@ +1 +success +65b3ffb37074b38c05022c28bd335338e236cf66 +07 Jul 2019 16:54:42 diff --git a/src/misc/digest.h b/src/misc/digest.h index cf1c57a..8da08ca 100644 --- a/src/misc/digest.h +++ b/src/misc/digest.h @@ -5,9 +5,9 @@ #include #ifdef NO_OPEN_SSL - #define SHA_DIGEST_LENGTH 20 - #define SHA256_DIGEST_LENGTH 32 - #define SHA512_DIGEST_LENGTH 64 + #define SHA_DIGEST_LENGTH (20) + #define SHA256_DIGEST_LENGTH (32) + #define SHA512_DIGEST_LENGTH (64) #define DECLARE_DIGEST(name, _unused_, digestLength) \ namespace tomcrypt { \ @@ -16,14 +16,14 @@ inline std::string name(const std::string& input) { \ uint8_t result[digestLength]; \ tomcrypt::name(input.data(), input.length(), result); \ - return std::string((const char*) result, digestLength); \ + return std::string((const char*) result, (size_t) digestLength); \ } \ \ - inline std::string name(const char* input, int64_t length = -1) { \ + inline std::string __ ##name(const char* input, int64_t length = -1) { \ if(length == -1) length = strlen(input); \ uint8_t result[digestLength]; \ tomcrypt::name(input, length, result); \ - return std::string((const char*) result, digestLength); \ + return std::string((const char*) result, (size_t) digestLength); \ } \ \ inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \ @@ -37,12 +37,12 @@ inline std::string name(const std::string& input) { \ u_char buffer[digestLength]; \ method((u_char*) input.data(), input.length(), buffer); \ - return std::string((const char*) buffer, digestLength); \ + return std::string((const char*) buffer, (size_t) digestLength); \ } \ \ inline std::string name(const char* input, ssize_t length = -1) { \ if(length == -1) length = strlen(input); \ - return name(std::string(input, length)); \ + return name(std::string(input, (size_t) length)); \ } \ \ inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \ @@ -56,4 +56,4 @@ namespace digest { DECLARE_DIGEST(sha512, SHA512, SHA512_DIGEST_LENGTH) } -#undef DECLARE_DIGEST \ No newline at end of file +#undef DECLARE_DIGEST diff --git a/src/protocol/CryptionHandler.cpp b/src/protocol/CryptionHandler.cpp index 1fdb84d..cb920e6 100644 --- a/src/protocol/CryptionHandler.cpp +++ b/src/protocol/CryptionHandler.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include "../misc/base64.h" #include "misc/memtracker.h" #include "misc/digest.h" #include "CryptionHandler.h" @@ -46,7 +46,6 @@ bool CryptionHandler::setupSharedSecret(const std::string& alpha, const std::str error = "Could not calculate shared secret. Message: " + string(error_to_string(err)); return false; } - debugMessage(0, "ecc_shared_secret: {}", base64::encode((char*) buffer, buffer_length)); auto result = this->setupSharedSecret(alpha, beta, string((const char*) buffer, buffer_length), error); return result; @@ -54,25 +53,25 @@ bool CryptionHandler::setupSharedSecret(const std::string& alpha, const std::str bool CryptionHandler::setupSharedSecret(const std::string& alpha, const std::string& beta, const std::string& sharedKey, std::string &error) { auto secret_hash = digest::sha1(sharedKey); + assert(secret_hash.length() == SHA_DIGEST_LENGTH); - char ivStruct[SHA_DIGEST_LENGTH]; - memcpy(ivStruct, alpha.data(), 10); - memcpy(&ivStruct[10], beta.data(), 10); + uint8_t iv_buffer[SHA_DIGEST_LENGTH]; + memcpy(iv_buffer, alpha.data(), 10); + memcpy(&iv_buffer[10], beta.data(), 10); for (int index = 0; index < SHA_DIGEST_LENGTH; index++) { - ivStruct[index] ^= (uint8_t) secret_hash[index]; + iv_buffer[index] ^= (uint8_t) secret_hash[index]; } { lock_guard lock(this->cache_key_lock); - memcpy(this->iv_struct, ivStruct, SHA_DIGEST_LENGTH); + memcpy(this->iv_struct, iv_buffer, SHA_DIGEST_LENGTH); this->iv_struct_length = SHA_DIGEST_LENGTH; - debugMessage(0, "iv_struct: {}", base64::encode((char*) this->iv_struct, SHA_DIGEST_LENGTH)); - auto iv_hash = digest::sha1(ivStruct, SHA_DIGEST_LENGTH); - memcpy(this->current_mac, iv_hash.data(), 8); + uint8_t mac_buffer[SHA_DIGEST_LENGTH]; + digest::sha1((const char*) iv_buffer, SHA_DIGEST_LENGTH, mac_buffer); + memcpy(this->current_mac, mac_buffer, 8); - debugMessage(0, "Mac: {}", base64::encode((char*) this->current_mac, 8)); this->useDefaultChipherKeyNonce = false; } @@ -140,15 +139,16 @@ bool CryptionHandler::setupSharedSecretNew(const std::string &alpha, const std:: auto xor_key = alpha + beta; for(int i = 0; i < 64; i++) - shared_iv[i] ^= xor_key[i]; + shared_iv[i] ^= (uint8_t) xor_key[i]; { lock_guard lock(this->cache_key_lock); memcpy(this->iv_struct, shared_iv, 64); this->iv_struct_length = 64; - auto digest_buffer = digest::sha1((char*) this->iv_struct, 64); - memcpy(this->current_mac, digest_buffer.data(), 8); + uint8_t mac_buffer[SHA_DIGEST_LENGTH]; + digest::sha1((char*) this->iv_struct, 64, mac_buffer); + memcpy(this->current_mac, mac_buffer, 8); this->useDefaultChipherKeyNonce = false; } @@ -206,11 +206,13 @@ bool CryptionHandler::generate_key_nonce( le2be32(generation, buffer, 2); memcpy(&buffer[6], this->iv_struct, this->iv_struct_length); - auto key_nonce = digest::sha256(buffer, buffer_length); + digest::sha256(buffer, buffer_length, key_cache.key_nonce); + /* memcpy(key_cache.key, key_nonce.data(), 16); memcpy(key_cache.nonce, key_nonce.data() + 16, 16); - key_cache.generation = generation; + */ + key_cache.generation = generation; } memcpy(key, key_cache.key, 16); @@ -371,4 +373,4 @@ bool CryptionHandler::progressPacketOut(protocol::BasicPacket* packet, std::stri return success; } return true; -} \ No newline at end of file +} diff --git a/src/protocol/CryptionHandler.h b/src/protocol/CryptionHandler.h index 929bb09..51c8fa7 100644 --- a/src/protocol/CryptionHandler.h +++ b/src/protocol/CryptionHandler.h @@ -15,8 +15,13 @@ namespace ts { }; struct KeyCache { uint16_t generation = 0xFFEF; - uint8_t key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - uint8_t nonce[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + union { + struct { + uint8_t key[16]; + uint8_t nonce[16]; + }; + uint8_t key_nonce[32]; + }; }; public: CryptionHandler();