Fixec crypt errors

This commit is contained in:
WolverinDEV 2019-07-07 16:57:21 +02:00
parent 65b3ffb370
commit ee8e5b9dd5
4 changed files with 39 additions and 28 deletions

4
.build_win32_amd64.txt Normal file
View File

@ -0,0 +1,4 @@
1
success
65b3ffb37074b38c05022c28bd335338e236cf66
07 Jul 2019 16:54:42

View File

@ -5,9 +5,9 @@
#include <cstring> #include <cstring>
#ifdef NO_OPEN_SSL #ifdef NO_OPEN_SSL
#define SHA_DIGEST_LENGTH 20 #define SHA_DIGEST_LENGTH (20)
#define SHA256_DIGEST_LENGTH 32 #define SHA256_DIGEST_LENGTH (32)
#define SHA512_DIGEST_LENGTH 64 #define SHA512_DIGEST_LENGTH (64)
#define DECLARE_DIGEST(name, _unused_, digestLength) \ #define DECLARE_DIGEST(name, _unused_, digestLength) \
namespace tomcrypt { \ namespace tomcrypt { \
@ -16,14 +16,14 @@
inline std::string name(const std::string& input) { \ inline std::string name(const std::string& input) { \
uint8_t result[digestLength]; \ uint8_t result[digestLength]; \
tomcrypt::name(input.data(), input.length(), result); \ 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); \ if(length == -1) length = strlen(input); \
uint8_t result[digestLength]; \ uint8_t result[digestLength]; \
tomcrypt::name(input, length, result); \ 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]) { \ 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) { \ inline std::string name(const std::string& input) { \
u_char buffer[digestLength]; \ u_char buffer[digestLength]; \
method((u_char*) input.data(), input.length(), buffer); \ 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) { \ inline std::string name(const char* input, ssize_t length = -1) { \
if(length == -1) length = strlen(input); \ 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]) { \ inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \

View File

@ -5,7 +5,7 @@
#include <ed25519/ed25519.h> #include <ed25519/ed25519.h>
#include <ed25519/ge.h> #include <ed25519/ge.h>
#include <log/LogUtils.h> #include <log/LogUtils.h>
#include <src/misc/base64.h> #include "../misc/base64.h"
#include "misc/memtracker.h" #include "misc/memtracker.h"
#include "misc/digest.h" #include "misc/digest.h"
#include "CryptionHandler.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)); error = "Could not calculate shared secret. Message: " + string(error_to_string(err));
return false; 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); auto result = this->setupSharedSecret(alpha, beta, string((const char*) buffer, buffer_length), error);
return result; 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) { bool CryptionHandler::setupSharedSecret(const std::string& alpha, const std::string& beta, const std::string& sharedKey, std::string &error) {
auto secret_hash = digest::sha1(sharedKey); auto secret_hash = digest::sha1(sharedKey);
assert(secret_hash.length() == SHA_DIGEST_LENGTH);
char ivStruct[SHA_DIGEST_LENGTH]; uint8_t iv_buffer[SHA_DIGEST_LENGTH];
memcpy(ivStruct, alpha.data(), 10); memcpy(iv_buffer, alpha.data(), 10);
memcpy(&ivStruct[10], beta.data(), 10); memcpy(&iv_buffer[10], beta.data(), 10);
for (int index = 0; index < SHA_DIGEST_LENGTH; index++) { 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); 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; 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); uint8_t mac_buffer[SHA_DIGEST_LENGTH];
memcpy(this->current_mac, iv_hash.data(), 8); 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; this->useDefaultChipherKeyNonce = false;
} }
@ -140,15 +139,16 @@ bool CryptionHandler::setupSharedSecretNew(const std::string &alpha, const std::
auto xor_key = alpha + beta; auto xor_key = alpha + beta;
for(int i = 0; i < 64; i++) 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); lock_guard lock(this->cache_key_lock);
memcpy(this->iv_struct, shared_iv, 64); memcpy(this->iv_struct, shared_iv, 64);
this->iv_struct_length = 64; this->iv_struct_length = 64;
auto digest_buffer = digest::sha1((char*) this->iv_struct, 64); uint8_t mac_buffer[SHA_DIGEST_LENGTH];
memcpy(this->current_mac, digest_buffer.data(), 8); digest::sha1((char*) this->iv_struct, 64, mac_buffer);
memcpy(this->current_mac, mac_buffer, 8);
this->useDefaultChipherKeyNonce = false; this->useDefaultChipherKeyNonce = false;
} }
@ -206,10 +206,12 @@ bool CryptionHandler::generate_key_nonce(
le2be32(generation, buffer, 2); le2be32(generation, buffer, 2);
memcpy(&buffer[6], this->iv_struct, this->iv_struct_length); 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.key, key_nonce.data(), 16);
memcpy(key_cache.nonce, key_nonce.data() + 16, 16); memcpy(key_cache.nonce, key_nonce.data() + 16, 16);
*/
key_cache.generation = generation; key_cache.generation = generation;
} }

View File

@ -15,8 +15,13 @@ namespace ts {
}; };
struct KeyCache { struct KeyCache {
uint16_t generation = 0xFFEF; uint16_t generation = 0xFFEF;
uint8_t key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; union {
uint8_t nonce[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; struct {
uint8_t key[16];
uint8_t nonce[16];
};
uint8_t key_nonce[32];
};
}; };
public: public:
CryptionHandler(); CryptionHandler();