From d4497789ca7671ce6b5cf7e0e5111a5483cce7af Mon Sep 17 00:00:00 2001 From: Jaroslav Pekara Date: Mon, 9 Jan 2017 14:52:48 +0100 Subject: [PATCH 1/3] Fixes for 64b Linux. --- development/include/GXBytebuffer.h | 3 +- development/include/GXCipher.h | 13 +++--- development/src/GXBytebuffer.cpp | 2 +- development/src/GXCipher.cpp | 67 ++++++++++++++------------- development/src/GXDLMS.cpp | 4 +- development/src/GXDLMSContextType.cpp | 2 +- development/src/GXDLMSPppSetup.cpp | 4 +- 7 files changed, 49 insertions(+), 46 deletions(-) diff --git a/development/include/GXBytebuffer.h b/development/include/GXBytebuffer.h index 6632eda8..4e4f2deb 100644 --- a/development/include/GXBytebuffer.h +++ b/development/include/GXBytebuffer.h @@ -37,6 +37,7 @@ #include "errorcodes.h" #include "enums.h" +#include const unsigned char VECTOR_CAPACITY = 50; class CGXByteBuffer @@ -108,7 +109,7 @@ class CGXByteBuffer void Set(const void* pSource, unsigned long count); - void Set(CGXByteBuffer* data, unsigned long index = 0, unsigned long count = -1); + void Set(CGXByteBuffer* data, unsigned long index = 0, unsigned long count = ULONG_MAX); void AddString(const char* value); diff --git a/development/include/GXCipher.h b/development/include/GXCipher.h index c4680cd0..1f1e038c 100644 --- a/development/include/GXCipher.h +++ b/development/include/GXCipher.h @@ -36,15 +36,16 @@ #define GXCIPHER_H #include "GXBytebuffer.h" +#include //This is reserved for internal use to save block info. class CGXGMacBlock { public: - unsigned long c0; - unsigned long c1; - unsigned long c2; - unsigned long c3; + uint32_t c0; + uint32_t c1; + uint32_t c2; + uint32_t c3; // How many bytes are not crypted/encrypted. int bytesRemaining; long totalLength; @@ -84,12 +85,12 @@ class CGXCipher /** * Working key is counted only once from block cipher key. */ - unsigned long* m_WorkingKey; + uint32_t* m_WorkingKey; CGXByteBuffer m_H; CGXByteBuffer m_J0; CGXByteBuffer m_S; CGXByteBuffer m_Counter; - unsigned long* m_mArray; + uint32_t* m_mArray; static int GetRounds( CGXCipher* settings); diff --git a/development/src/GXBytebuffer.cpp b/development/src/GXBytebuffer.cpp index a4bdae64..6aa13be2 100644 --- a/development/src/GXBytebuffer.cpp +++ b/development/src/GXBytebuffer.cpp @@ -719,7 +719,7 @@ CGXByteBuffer& CGXByteBuffer::operator=(CGXByteBuffer& value) m_Size = 0; if (value.GetSize() != 0) { - Set(&value, 0, 0xFFFFFFFF); + Set(&value, 0, ULONG_MAX); } return *this; } diff --git a/development/src/GXCipher.cpp b/development/src/GXCipher.cpp index d918f0d2..3d5fc628 100644 --- a/development/src/GXCipher.cpp +++ b/development/src/GXCipher.cpp @@ -35,6 +35,7 @@ #include "../include/GXCipher.h" #include "../include/chipperingenums.h" #include "../include/GXHelpers.h" +#include // Consts. const unsigned char BLOCK_SIZE = 16; @@ -120,8 +121,8 @@ static void PackBlock( int GetUInt32ByIndexLE( CGXByteBuffer *arr, - unsigned long index, - unsigned long* value) + uint32_t index, + uint32_t* value) { if (index + 4 > arr->GetSize()) { @@ -193,7 +194,7 @@ int CGXCipher::ProcessBlock( return 0; } -static unsigned long* GetCell(unsigned long *start, int row, int col) +static uint32_t* GetCell(uint32_t *start, int row, int col) { return start + 4 * row + col; } @@ -208,13 +209,13 @@ static unsigned long* GetCell(unsigned long *start, int row, int col) * Blocks to shift. */ static void ShiftRight( - unsigned long* block, + uint32_t* block, int count) { int i, bit = 0; for (i = 0; i < 4; ++i) { - unsigned long b = block[i]; + uint32_t b = block[i]; block[i] = (b >> count) | bit; bit = b << (32 - count); } @@ -228,8 +229,8 @@ static void ShiftRight( * @param val */ static void Xor128( - unsigned long* block, - unsigned long* value) + uint32_t* block, + uint32_t* value) { int pos; for (pos = 0; pos != 4; ++pos) @@ -239,7 +240,7 @@ static void Xor128( } static void MultiplyP( - unsigned long* x) + uint32_t* x) { unsigned char lsb = (x[3] & 1) != 0; ShiftRight(x, 1); @@ -250,10 +251,10 @@ static void MultiplyP( } static void MultiplyP8( - unsigned long* x) + uint32_t* x) { char pos; - unsigned long lsw = x[3]; + uint32_t lsw = x[3]; ShiftRight(x, 8); for (pos = 0; pos != 8; ++pos) { @@ -267,13 +268,13 @@ static void MultiplyP8( int CGXCipher::Init2( CGXCipher* settings) { - unsigned long *tmp; + uint32_t *tmp; int pos, pos1, pos2, k, ret; //If array is not created yet. if (settings->m_mArray == 0) { //Array must set to zero. - settings->m_mArray = (unsigned long*)calloc(1, sizeof(unsigned long) * 32 * 16 * 4); + settings->m_mArray = (uint32_t*)calloc(1, sizeof(uint32_t) * 32 * 16 * 4); if ((ret = settings->m_H.GetUInt128(0, (unsigned char*)GetCell(settings->m_mArray, 1 * 16, 8 * 4))) != 0) { return ret; @@ -282,18 +283,18 @@ int CGXCipher::Init2( for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); } tmp = GetCell(settings->m_mArray, 0 * 16, 8 * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 0 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); } @@ -304,7 +305,7 @@ int CGXCipher::Init2( for (k = 1; k < pos2; ++k) { tmp = GetCell(settings->m_mArray, pos1 * 16, (pos2 + k) * 4); - memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(uint32_t)); Xor128(tmp, GetCell(settings->m_mArray, pos1 * 16, k * 4)); } } @@ -319,7 +320,7 @@ int CGXCipher::Init2( for (pos = 8; pos > 0; pos >>= 1) { tmp = GetCell(settings->m_mArray, pos1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(uint32_t)); MultiplyP8(tmp); } } @@ -338,18 +339,18 @@ int CGXCipher::GetAuthenticatedData( if (security == DLMS_SECURITY_AUTHENTICATION) { result.SetUInt8(security); - result.Set(&authenticationKey, 0, -1); + result.Set(&authenticationKey, 0, ULONG_MAX); result.Set(plainText.GetData() + plainText.GetPosition(), plainText.GetSize() - plainText.GetPosition()); } else if (security == DLMS_SECURITY_ENCRYPTION) { - result.Set(&authenticationKey, 0, -1); + result.Set(&authenticationKey, 0, ULONG_MAX); } else if (security == DLMS_SECURITY_AUTHENTICATION_ENCRYPTION) { result.SetUInt8(security); - result.Set(&authenticationKey, 0, -1); + result.Set(&authenticationKey, 0, ULONG_MAX); } else { @@ -376,8 +377,8 @@ static unsigned int SubWord(unsigned int value) * @return */ static unsigned int Shift( - unsigned long value, - unsigned long shift) + uint32_t value, + uint32_t shift) { return (value >> shift) | (value << (32 - shift)); } @@ -428,7 +429,7 @@ void CGXCipher::MultiplyH( { int pos; long tmp[4] = { 0 }; - unsigned long* m; + uint32_t* m; for (pos = 0; pos != 16; ++pos) { m = GetCell(settings->m_mArray, 16 * (pos + pos), 4 * (value[pos] & 0x0f)); @@ -606,7 +607,7 @@ int CGXCipher::GetRounds(CGXCipher* settings) int GetUInt32LE( CGXByteBuffer* arr, - unsigned long* value) + uint32_t* value) { if (arr->GetPosition() + 4 > arr->GetSize()) { @@ -630,7 +631,7 @@ int CGXCipher::GenerateKey(CGXCipher* settings) int rounds, ret, j, t = 0; rounds = GetRounds(settings); // 4 words make one block. - settings->m_WorkingKey = (unsigned long*)malloc(4 * sizeof(unsigned long) * (rounds + 1)); + settings->m_WorkingKey = (uint32_t*)malloc(4 * sizeof(uint32_t) * (rounds + 1)); // Copy the key into the round key array. settings->m_BlockCipherKey.SetPosition(0); for (i = 0; i < settings->m_BlockCipherKey.GetSize(); t++) @@ -746,12 +747,12 @@ int CGXCipher::Init( settings->m_J0.Zero(0, 16); settings->m_J0.SetSize(0); - settings->m_J0.Set(&iv, 0, -1); + settings->m_J0.Set(&iv, 0, ULONG_MAX); settings->m_J0.SetSize(16); settings->m_J0.GetData()[15] = 0x01; GetGHash(settings, &aad); settings->m_Counter.Clear(); - settings->m_Counter.Set(&settings->m_J0, 0, -1); + settings->m_Counter.Set(&settings->m_J0, 0, ULONG_MAX); block.bytesRemaining = 0; block.totalLength = 0; return 0; @@ -843,7 +844,7 @@ void CGXCipher::Reset( { GetGHash(settings, aad); settings->m_Counter.Capacity(0); - settings->m_Counter.Set(&settings->m_J0, 0, -1); + settings->m_Counter.Set(&settings->m_J0, 0, ULONG_MAX); } /** @@ -955,7 +956,7 @@ int CGXCipher::Encrypt( } if ((type & DLMS_COUNT_TYPE_TAG) != 0) { - data.Set(&block.tag, 0, -1); + data.Set(&block.tag, 0, ULONG_MAX); } } else if (security == DLMS_SECURITY_ENCRYPTION) @@ -964,7 +965,7 @@ int CGXCipher::Encrypt( { data.SetUInt32(frameCounter); } - data.Set(&encrypted, 0, -1); + data.Set(&encrypted, 0, ULONG_MAX); } else if (security == DLMS_SECURITY_AUTHENTICATION_ENCRYPTION) { @@ -974,12 +975,12 @@ int CGXCipher::Encrypt( } if ((type & DLMS_COUNT_TYPE_DATA) != 0) { - data.Set(&encrypted, 0, -1); + data.Set(&encrypted, 0, ULONG_MAX); } if ((type & DLMS_COUNT_TYPE_DATA) != 0 || (type & DLMS_COUNT_TYPE_TAG) != 0) { - data.Set(&block.tag, 0, -1); + data.Set(&block.tag, 0, ULONG_MAX); } } else @@ -997,7 +998,7 @@ int CGXCipher::Encrypt( { encrypted.Capacity(data.GetSize()); } - encrypted.Set(&data, 0, -1); + encrypted.Set(&data, 0, ULONG_MAX); return 0; } diff --git a/development/src/GXDLMS.cpp b/development/src/GXDLMS.cpp index 41b13318..3ca75a03 100644 --- a/development/src/GXDLMS.cpp +++ b/development/src/GXDLMS.cpp @@ -217,7 +217,7 @@ int CGXDLMS::ReceiverReady( &bb, NULL); ret = GetSnMessages(p, tmp); } - reply.Set(&tmp.at(0), 0, -1); + reply.Set(&tmp.at(0), 0, ULONG_MAX); return ret; } @@ -251,7 +251,7 @@ int CGXDLMS::GetWrapperFrame( // Data length. reply.SetUInt16((unsigned short)data.GetSize()); // Data - reply.Set(&data, data.GetPosition(), -1); + reply.Set(&data, data.GetPosition(), ULONG_MAX); // Remove sent data in server side. if (settings.IsServer()) diff --git a/development/src/GXDLMSContextType.cpp b/development/src/GXDLMSContextType.cpp index 6cf0dad1..d41403e9 100644 --- a/development/src/GXDLMSContextType.cpp +++ b/development/src/GXDLMSContextType.cpp @@ -103,6 +103,6 @@ std::string CGXDLMSContextType::ToString() bb.SetInt8(' '); bb.AddIntAsString(m_QualityOfService); bb.SetInt8(' '); - bb.Set(&m_CypheringInfo, 0, -1); + bb.Set(&m_CypheringInfo, 0, ULONG_MAX); return bb.ToString(); } \ No newline at end of file diff --git a/development/src/GXDLMSPppSetup.cpp b/development/src/GXDLMSPppSetup.cpp index f3edc77c..f124897e 100644 --- a/development/src/GXDLMSPppSetup.cpp +++ b/development/src/GXDLMSPppSetup.cpp @@ -292,11 +292,11 @@ int CGXDLMSPppSetup::GetValue(CGXDLMSSettings& settings, CGXDLMSValueEventArg& e //Add username. data.SetUInt8(DLMS_DATA_TYPE_OCTET_STRING); data.SetUInt8((unsigned char)m_UserName.GetSize()); - data.Set(&m_UserName, 0, -1); + data.Set(&m_UserName, 0, ULONG_MAX); //Add password. data.SetUInt8(DLMS_DATA_TYPE_OCTET_STRING); data.SetUInt8((unsigned char)m_Password.GetSize()); - data.Set(&m_Password, 0, -1); + data.Set(&m_Password, 0, ULONG_MAX); e.SetValue(data); return DLMS_ERROR_CODE_OK; } From cd33d37625e01240a56922b1f13bfbe384edc511 Mon Sep 17 00:00:00 2001 From: Jaroslav Pekara Date: Mon, 9 Jan 2017 15:17:38 +0100 Subject: [PATCH 2/3] Revert "Fixes for 64b Linux." This reverts commit d4497789ca7671ce6b5cf7e0e5111a5483cce7af. --- development/include/GXBytebuffer.h | 3 +- development/include/GXCipher.h | 13 +++--- development/src/GXBytebuffer.cpp | 2 +- development/src/GXCipher.cpp | 67 +++++++++++++-------------- development/src/GXDLMS.cpp | 4 +- development/src/GXDLMSContextType.cpp | 2 +- development/src/GXDLMSPppSetup.cpp | 4 +- 7 files changed, 46 insertions(+), 49 deletions(-) diff --git a/development/include/GXBytebuffer.h b/development/include/GXBytebuffer.h index 4e4f2deb..6632eda8 100644 --- a/development/include/GXBytebuffer.h +++ b/development/include/GXBytebuffer.h @@ -37,7 +37,6 @@ #include "errorcodes.h" #include "enums.h" -#include const unsigned char VECTOR_CAPACITY = 50; class CGXByteBuffer @@ -109,7 +108,7 @@ class CGXByteBuffer void Set(const void* pSource, unsigned long count); - void Set(CGXByteBuffer* data, unsigned long index = 0, unsigned long count = ULONG_MAX); + void Set(CGXByteBuffer* data, unsigned long index = 0, unsigned long count = -1); void AddString(const char* value); diff --git a/development/include/GXCipher.h b/development/include/GXCipher.h index 1f1e038c..c4680cd0 100644 --- a/development/include/GXCipher.h +++ b/development/include/GXCipher.h @@ -36,16 +36,15 @@ #define GXCIPHER_H #include "GXBytebuffer.h" -#include //This is reserved for internal use to save block info. class CGXGMacBlock { public: - uint32_t c0; - uint32_t c1; - uint32_t c2; - uint32_t c3; + unsigned long c0; + unsigned long c1; + unsigned long c2; + unsigned long c3; // How many bytes are not crypted/encrypted. int bytesRemaining; long totalLength; @@ -85,12 +84,12 @@ class CGXCipher /** * Working key is counted only once from block cipher key. */ - uint32_t* m_WorkingKey; + unsigned long* m_WorkingKey; CGXByteBuffer m_H; CGXByteBuffer m_J0; CGXByteBuffer m_S; CGXByteBuffer m_Counter; - uint32_t* m_mArray; + unsigned long* m_mArray; static int GetRounds( CGXCipher* settings); diff --git a/development/src/GXBytebuffer.cpp b/development/src/GXBytebuffer.cpp index 6aa13be2..a4bdae64 100644 --- a/development/src/GXBytebuffer.cpp +++ b/development/src/GXBytebuffer.cpp @@ -719,7 +719,7 @@ CGXByteBuffer& CGXByteBuffer::operator=(CGXByteBuffer& value) m_Size = 0; if (value.GetSize() != 0) { - Set(&value, 0, ULONG_MAX); + Set(&value, 0, 0xFFFFFFFF); } return *this; } diff --git a/development/src/GXCipher.cpp b/development/src/GXCipher.cpp index 3d5fc628..d918f0d2 100644 --- a/development/src/GXCipher.cpp +++ b/development/src/GXCipher.cpp @@ -35,7 +35,6 @@ #include "../include/GXCipher.h" #include "../include/chipperingenums.h" #include "../include/GXHelpers.h" -#include // Consts. const unsigned char BLOCK_SIZE = 16; @@ -121,8 +120,8 @@ static void PackBlock( int GetUInt32ByIndexLE( CGXByteBuffer *arr, - uint32_t index, - uint32_t* value) + unsigned long index, + unsigned long* value) { if (index + 4 > arr->GetSize()) { @@ -194,7 +193,7 @@ int CGXCipher::ProcessBlock( return 0; } -static uint32_t* GetCell(uint32_t *start, int row, int col) +static unsigned long* GetCell(unsigned long *start, int row, int col) { return start + 4 * row + col; } @@ -209,13 +208,13 @@ static uint32_t* GetCell(uint32_t *start, int row, int col) * Blocks to shift. */ static void ShiftRight( - uint32_t* block, + unsigned long* block, int count) { int i, bit = 0; for (i = 0; i < 4; ++i) { - uint32_t b = block[i]; + unsigned long b = block[i]; block[i] = (b >> count) | bit; bit = b << (32 - count); } @@ -229,8 +228,8 @@ static void ShiftRight( * @param val */ static void Xor128( - uint32_t* block, - uint32_t* value) + unsigned long* block, + unsigned long* value) { int pos; for (pos = 0; pos != 4; ++pos) @@ -240,7 +239,7 @@ static void Xor128( } static void MultiplyP( - uint32_t* x) + unsigned long* x) { unsigned char lsb = (x[3] & 1) != 0; ShiftRight(x, 1); @@ -251,10 +250,10 @@ static void MultiplyP( } static void MultiplyP8( - uint32_t* x) + unsigned long* x) { char pos; - uint32_t lsw = x[3]; + unsigned long lsw = x[3]; ShiftRight(x, 8); for (pos = 0; pos != 8; ++pos) { @@ -268,13 +267,13 @@ static void MultiplyP8( int CGXCipher::Init2( CGXCipher* settings) { - uint32_t *tmp; + unsigned long *tmp; int pos, pos1, pos2, k, ret; //If array is not created yet. if (settings->m_mArray == 0) { //Array must set to zero. - settings->m_mArray = (uint32_t*)calloc(1, sizeof(uint32_t) * 32 * 16 * 4); + settings->m_mArray = (unsigned long*)calloc(1, sizeof(unsigned long) * 32 * 16 * 4); if ((ret = settings->m_H.GetUInt128(0, (unsigned char*)GetCell(settings->m_mArray, 1 * 16, 8 * 4))) != 0) { return ret; @@ -283,18 +282,18 @@ int CGXCipher::Init2( for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(long)); MultiplyP(tmp); } tmp = GetCell(settings->m_mArray, 0 * 16, 8 * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(uint32_t)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(long)); MultiplyP(tmp); for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 0 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); + memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(long)); MultiplyP(tmp); } @@ -305,7 +304,7 @@ int CGXCipher::Init2( for (k = 1; k < pos2; ++k) { tmp = GetCell(settings->m_mArray, pos1 * 16, (pos2 + k) * 4); - memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(uint32_t)); + memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(long)); Xor128(tmp, GetCell(settings->m_mArray, pos1 * 16, k * 4)); } } @@ -320,7 +319,7 @@ int CGXCipher::Init2( for (pos = 8; pos > 0; pos >>= 1) { tmp = GetCell(settings->m_mArray, pos1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(uint32_t)); + memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(long)); MultiplyP8(tmp); } } @@ -339,18 +338,18 @@ int CGXCipher::GetAuthenticatedData( if (security == DLMS_SECURITY_AUTHENTICATION) { result.SetUInt8(security); - result.Set(&authenticationKey, 0, ULONG_MAX); + result.Set(&authenticationKey, 0, -1); result.Set(plainText.GetData() + plainText.GetPosition(), plainText.GetSize() - plainText.GetPosition()); } else if (security == DLMS_SECURITY_ENCRYPTION) { - result.Set(&authenticationKey, 0, ULONG_MAX); + result.Set(&authenticationKey, 0, -1); } else if (security == DLMS_SECURITY_AUTHENTICATION_ENCRYPTION) { result.SetUInt8(security); - result.Set(&authenticationKey, 0, ULONG_MAX); + result.Set(&authenticationKey, 0, -1); } else { @@ -377,8 +376,8 @@ static unsigned int SubWord(unsigned int value) * @return */ static unsigned int Shift( - uint32_t value, - uint32_t shift) + unsigned long value, + unsigned long shift) { return (value >> shift) | (value << (32 - shift)); } @@ -429,7 +428,7 @@ void CGXCipher::MultiplyH( { int pos; long tmp[4] = { 0 }; - uint32_t* m; + unsigned long* m; for (pos = 0; pos != 16; ++pos) { m = GetCell(settings->m_mArray, 16 * (pos + pos), 4 * (value[pos] & 0x0f)); @@ -607,7 +606,7 @@ int CGXCipher::GetRounds(CGXCipher* settings) int GetUInt32LE( CGXByteBuffer* arr, - uint32_t* value) + unsigned long* value) { if (arr->GetPosition() + 4 > arr->GetSize()) { @@ -631,7 +630,7 @@ int CGXCipher::GenerateKey(CGXCipher* settings) int rounds, ret, j, t = 0; rounds = GetRounds(settings); // 4 words make one block. - settings->m_WorkingKey = (uint32_t*)malloc(4 * sizeof(uint32_t) * (rounds + 1)); + settings->m_WorkingKey = (unsigned long*)malloc(4 * sizeof(unsigned long) * (rounds + 1)); // Copy the key into the round key array. settings->m_BlockCipherKey.SetPosition(0); for (i = 0; i < settings->m_BlockCipherKey.GetSize(); t++) @@ -747,12 +746,12 @@ int CGXCipher::Init( settings->m_J0.Zero(0, 16); settings->m_J0.SetSize(0); - settings->m_J0.Set(&iv, 0, ULONG_MAX); + settings->m_J0.Set(&iv, 0, -1); settings->m_J0.SetSize(16); settings->m_J0.GetData()[15] = 0x01; GetGHash(settings, &aad); settings->m_Counter.Clear(); - settings->m_Counter.Set(&settings->m_J0, 0, ULONG_MAX); + settings->m_Counter.Set(&settings->m_J0, 0, -1); block.bytesRemaining = 0; block.totalLength = 0; return 0; @@ -844,7 +843,7 @@ void CGXCipher::Reset( { GetGHash(settings, aad); settings->m_Counter.Capacity(0); - settings->m_Counter.Set(&settings->m_J0, 0, ULONG_MAX); + settings->m_Counter.Set(&settings->m_J0, 0, -1); } /** @@ -956,7 +955,7 @@ int CGXCipher::Encrypt( } if ((type & DLMS_COUNT_TYPE_TAG) != 0) { - data.Set(&block.tag, 0, ULONG_MAX); + data.Set(&block.tag, 0, -1); } } else if (security == DLMS_SECURITY_ENCRYPTION) @@ -965,7 +964,7 @@ int CGXCipher::Encrypt( { data.SetUInt32(frameCounter); } - data.Set(&encrypted, 0, ULONG_MAX); + data.Set(&encrypted, 0, -1); } else if (security == DLMS_SECURITY_AUTHENTICATION_ENCRYPTION) { @@ -975,12 +974,12 @@ int CGXCipher::Encrypt( } if ((type & DLMS_COUNT_TYPE_DATA) != 0) { - data.Set(&encrypted, 0, ULONG_MAX); + data.Set(&encrypted, 0, -1); } if ((type & DLMS_COUNT_TYPE_DATA) != 0 || (type & DLMS_COUNT_TYPE_TAG) != 0) { - data.Set(&block.tag, 0, ULONG_MAX); + data.Set(&block.tag, 0, -1); } } else @@ -998,7 +997,7 @@ int CGXCipher::Encrypt( { encrypted.Capacity(data.GetSize()); } - encrypted.Set(&data, 0, ULONG_MAX); + encrypted.Set(&data, 0, -1); return 0; } diff --git a/development/src/GXDLMS.cpp b/development/src/GXDLMS.cpp index 3ca75a03..41b13318 100644 --- a/development/src/GXDLMS.cpp +++ b/development/src/GXDLMS.cpp @@ -217,7 +217,7 @@ int CGXDLMS::ReceiverReady( &bb, NULL); ret = GetSnMessages(p, tmp); } - reply.Set(&tmp.at(0), 0, ULONG_MAX); + reply.Set(&tmp.at(0), 0, -1); return ret; } @@ -251,7 +251,7 @@ int CGXDLMS::GetWrapperFrame( // Data length. reply.SetUInt16((unsigned short)data.GetSize()); // Data - reply.Set(&data, data.GetPosition(), ULONG_MAX); + reply.Set(&data, data.GetPosition(), -1); // Remove sent data in server side. if (settings.IsServer()) diff --git a/development/src/GXDLMSContextType.cpp b/development/src/GXDLMSContextType.cpp index d41403e9..6cf0dad1 100644 --- a/development/src/GXDLMSContextType.cpp +++ b/development/src/GXDLMSContextType.cpp @@ -103,6 +103,6 @@ std::string CGXDLMSContextType::ToString() bb.SetInt8(' '); bb.AddIntAsString(m_QualityOfService); bb.SetInt8(' '); - bb.Set(&m_CypheringInfo, 0, ULONG_MAX); + bb.Set(&m_CypheringInfo, 0, -1); return bb.ToString(); } \ No newline at end of file diff --git a/development/src/GXDLMSPppSetup.cpp b/development/src/GXDLMSPppSetup.cpp index f124897e..f3edc77c 100644 --- a/development/src/GXDLMSPppSetup.cpp +++ b/development/src/GXDLMSPppSetup.cpp @@ -292,11 +292,11 @@ int CGXDLMSPppSetup::GetValue(CGXDLMSSettings& settings, CGXDLMSValueEventArg& e //Add username. data.SetUInt8(DLMS_DATA_TYPE_OCTET_STRING); data.SetUInt8((unsigned char)m_UserName.GetSize()); - data.Set(&m_UserName, 0, ULONG_MAX); + data.Set(&m_UserName, 0, -1); //Add password. data.SetUInt8(DLMS_DATA_TYPE_OCTET_STRING); data.SetUInt8((unsigned char)m_Password.GetSize()); - data.Set(&m_Password, 0, ULONG_MAX); + data.Set(&m_Password, 0, -1); e.SetValue(data); return DLMS_ERROR_CODE_OK; } From ffb7c599677f8b91a18369d8433d5c0f6f56a9c5 Mon Sep 17 00:00:00 2001 From: Jaroslav Pekara Date: Mon, 9 Jan 2017 15:23:51 +0100 Subject: [PATCH 3/3] 64 bit issue fixed from ciphering. --- development/include/GXCipher.h | 13 +++++----- development/src/GXCipher.cpp | 45 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/development/include/GXCipher.h b/development/include/GXCipher.h index c4680cd0..1f1e038c 100644 --- a/development/include/GXCipher.h +++ b/development/include/GXCipher.h @@ -36,15 +36,16 @@ #define GXCIPHER_H #include "GXBytebuffer.h" +#include //This is reserved for internal use to save block info. class CGXGMacBlock { public: - unsigned long c0; - unsigned long c1; - unsigned long c2; - unsigned long c3; + uint32_t c0; + uint32_t c1; + uint32_t c2; + uint32_t c3; // How many bytes are not crypted/encrypted. int bytesRemaining; long totalLength; @@ -84,12 +85,12 @@ class CGXCipher /** * Working key is counted only once from block cipher key. */ - unsigned long* m_WorkingKey; + uint32_t* m_WorkingKey; CGXByteBuffer m_H; CGXByteBuffer m_J0; CGXByteBuffer m_S; CGXByteBuffer m_Counter; - unsigned long* m_mArray; + uint32_t* m_mArray; static int GetRounds( CGXCipher* settings); diff --git a/development/src/GXCipher.cpp b/development/src/GXCipher.cpp index d918f0d2..b8cb8ceb 100644 --- a/development/src/GXCipher.cpp +++ b/development/src/GXCipher.cpp @@ -35,6 +35,7 @@ #include "../include/GXCipher.h" #include "../include/chipperingenums.h" #include "../include/GXHelpers.h" +#include // Consts. const unsigned char BLOCK_SIZE = 16; @@ -120,8 +121,8 @@ static void PackBlock( int GetUInt32ByIndexLE( CGXByteBuffer *arr, - unsigned long index, - unsigned long* value) + uint32_t index, + uint32_t* value) { if (index + 4 > arr->GetSize()) { @@ -193,7 +194,7 @@ int CGXCipher::ProcessBlock( return 0; } -static unsigned long* GetCell(unsigned long *start, int row, int col) +static uint32_t* GetCell(uint32_t *start, int row, int col) { return start + 4 * row + col; } @@ -208,13 +209,13 @@ static unsigned long* GetCell(unsigned long *start, int row, int col) * Blocks to shift. */ static void ShiftRight( - unsigned long* block, + uint32_t* block, int count) { int i, bit = 0; for (i = 0; i < 4; ++i) { - unsigned long b = block[i]; + uint32_t b = block[i]; block[i] = (b >> count) | bit; bit = b << (32 - count); } @@ -228,8 +229,8 @@ static void ShiftRight( * @param val */ static void Xor128( - unsigned long* block, - unsigned long* value) + uint32_t* block, + uint32_t* value) { int pos; for (pos = 0; pos != 4; ++pos) @@ -239,7 +240,7 @@ static void Xor128( } static void MultiplyP( - unsigned long* x) + uint32_t* x) { unsigned char lsb = (x[3] & 1) != 0; ShiftRight(x, 1); @@ -250,10 +251,10 @@ static void MultiplyP( } static void MultiplyP8( - unsigned long* x) + uint32_t* x) { char pos; - unsigned long lsw = x[3]; + uint32_t lsw = x[3]; ShiftRight(x, 8); for (pos = 0; pos != 8; ++pos) { @@ -267,13 +268,13 @@ static void MultiplyP8( int CGXCipher::Init2( CGXCipher* settings) { - unsigned long *tmp; + uint32_t *tmp; int pos, pos1, pos2, k, ret; //If array is not created yet. if (settings->m_mArray == 0) { //Array must set to zero. - settings->m_mArray = (unsigned long*)calloc(1, sizeof(unsigned long) * 32 * 16 * 4); + settings->m_mArray = (uint32_t*)calloc(1, sizeof(uint32_t) * 32 * 16 * 4); if ((ret = settings->m_H.GetUInt128(0, (unsigned char*)GetCell(settings->m_mArray, 1 * 16, 8 * 4))) != 0) { return ret; @@ -282,18 +283,18 @@ int CGXCipher::Init2( for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); } tmp = GetCell(settings->m_mArray, 0 * 16, 8 * 4); - memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 1 * 16, 1 * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); for (pos = 4; pos >= 1; pos >>= 1) { tmp = GetCell(settings->m_mArray, 0 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, 0 * 16, (pos + pos) * 4), 4 * sizeof(uint32_t)); MultiplyP(tmp); } @@ -304,7 +305,7 @@ int CGXCipher::Init2( for (k = 1; k < pos2; ++k) { tmp = GetCell(settings->m_mArray, pos1 * 16, (pos2 + k) * 4); - memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, pos1 * 16, pos2 * 4), 4 * sizeof(uint32_t)); Xor128(tmp, GetCell(settings->m_mArray, pos1 * 16, k * 4)); } } @@ -319,7 +320,7 @@ int CGXCipher::Init2( for (pos = 8; pos > 0; pos >>= 1) { tmp = GetCell(settings->m_mArray, pos1 * 16, pos * 4); - memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(long)); + memcpy(tmp, GetCell(settings->m_mArray, (pos1 - 2) * 16, pos * 4), 4 * sizeof(uint32_t)); MultiplyP8(tmp); } } @@ -376,8 +377,8 @@ static unsigned int SubWord(unsigned int value) * @return */ static unsigned int Shift( - unsigned long value, - unsigned long shift) + uint32_t value, + uint32_t shift) { return (value >> shift) | (value << (32 - shift)); } @@ -428,7 +429,7 @@ void CGXCipher::MultiplyH( { int pos; long tmp[4] = { 0 }; - unsigned long* m; + uint32_t* m; for (pos = 0; pos != 16; ++pos) { m = GetCell(settings->m_mArray, 16 * (pos + pos), 4 * (value[pos] & 0x0f)); @@ -606,7 +607,7 @@ int CGXCipher::GetRounds(CGXCipher* settings) int GetUInt32LE( CGXByteBuffer* arr, - unsigned long* value) + uint32_t* value) { if (arr->GetPosition() + 4 > arr->GetSize()) { @@ -630,7 +631,7 @@ int CGXCipher::GenerateKey(CGXCipher* settings) int rounds, ret, j, t = 0; rounds = GetRounds(settings); // 4 words make one block. - settings->m_WorkingKey = (unsigned long*)malloc(4 * sizeof(unsigned long) * (rounds + 1)); + settings->m_WorkingKey = (uint32_t*)malloc(4 * sizeof(uint32_t) * (rounds + 1)); // Copy the key into the round key array. settings->m_BlockCipherKey.SetPosition(0); for (i = 0; i < settings->m_BlockCipherKey.GetSize(); t++)