Skip to content

Ciphering issues when running on 64b Linux #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions development/include/GXCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
#define GXCIPHER_H

#include "GXBytebuffer.h"
#include <stdint.h>

//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;
Expand Down Expand Up @@ -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);
Expand Down
45 changes: 23 additions & 22 deletions development/src/GXCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "../include/GXCipher.h"
#include "../include/chipperingenums.h"
#include "../include/GXHelpers.h"
#include <climits>

// Consts.
const unsigned char BLOCK_SIZE = 16;
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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));
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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())
{
Expand All @@ -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++)
Expand Down