Skip to content
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

Add HashDWORD function #3

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
103 changes: 103 additions & 0 deletions EncDecSim.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,106 @@ void GetCode (uint16_t seed, uint32_t *bufPtr, uint8_t *secTable) {
}
}
}

#define TR_DIV 0x7E0
#define SUBTRANSV(a,b) (a / 18 + b + 112 * (a % 18)) % TR_DIV
#define TRANSV(a,b,c) (c + SUBTRANSV(a, b)) % TR_DIV

uint32_t TransformDWORD(uint32_t in1, uint32_t in2)
{

uint16_t lw = in1;
uint16_t hw = in1 >> 16;

return SUBTRANSV(TRANSV(TRANSV((in2 + lw) % TR_DIV, hw, lw), hw, lw), hw);

}

#define GET_FROM_EDS(a, edStruct) ((edStruct[a >> 3] >> (7 - (a & 7))) & 1)
#define GET_FROM_EDS_XOR(a, b, edStruct) ((edStruct[a >> 3] >> (7 - (a & 7))) ^ (edStruct[b >> 3] >> (7 - (b & 7)))) & 1

uint8_t TransformEdStruct(uint8_t in5Bit, uint8_t *edStruct, uint8_t index)
{
uint32_t dw1;
uint32_t dw2;

in5Bit &= 0x1F;
index += 6;

uint8_t tr5Bit = (245 * in5Bit ^ 5) & 0x1F;
uint32_t tr5Bit56 = 56 * in5Bit + 144;
uint32_t endEdStruct = ((uint32_t*)edStruct)[63];

for (int i = 4; i >= 0; i-- ) {
dw1 = TransformDWORD(endEdStruct, tr5Bit56 + 5 - i);
tr5Bit ^= GET_FROM_EDS(dw1, edStruct) << i;
}

dw1 = TransformDWORD(endEdStruct, tr5Bit56);
uint8_t f1 = (edStruct[dw1 >> 3] >> (7 - (dw1 & 7))) & 1;

if ( tr5Bit != 0x1F ) {
dw1 = TransformDWORD(endEdStruct, tr5Bit56 + index + tr5Bit);
dw2 = TransformDWORD(endEdStruct, tr5Bit56 + ((tr5Bit + index + in5Bit) & 7) + 48);
f1 ^= GET_FROM_EDS_XOR(dw2, dw1, edStruct);
}

if (index < 48) {

for (int count = 0; count + index < 48; count++ ) {

uint8_t f2 = 0;

if ( in5Bit & 1 ) {
dw1 = TransformDWORD(endEdStruct, 2015 - count % 38);
dw2 = TransformDWORD(endEdStruct, 143 - count);
f2 = GET_FROM_EDS_XOR(dw2, dw1, edStruct);
}

if ( f1 ) {
dw1 = TransformDWORD(endEdStruct, count % 38 + 64);
dw2 = TransformDWORD(endEdStruct, count + 1936);
f2 ^= GET_FROM_EDS_XOR(dw2, dw1, edStruct);
}

if ( f2 ) {
for (int i = 0; i < 32; i++ ) {
dw1 = TransformDWORD(endEdStruct, 56 * i + count + index + 144);
edStruct[dw1 >> 3] = edStruct[dw1 >> 3] & ~(1 << (7 - (dw1 & 7))) | (((f2 ^ GET_FROM_EDS(dw1, edStruct)) & 1) << (7 - (dw1 & 7)));
}
}
}
}

dw1 = TransformDWORD(endEdStruct, tr5Bit56 + index);
dw2 = TransformDWORD(endEdStruct, tr5Bit56 + (in5Bit + index) % 8 + 48);
return GET_FROM_EDS_XOR(dw2, dw1, edStruct);

}



void HashDWORD(uint32_t *Data, uint8_t *edStruct)
{
uint8_t fodd;
uint8_t buf[256];

uint8_t *arrayData8 = (uint8_t *) Data;
uint8_t index = 0;

memcpy(buf, edStruct, 256);

for ( uint8_t i = 0; i < 39; i++ ) {
while ( 1 )
{
fodd = TransformEdStruct(arrayData8[index], buf, i);
index = (fodd | 2 * arrayData8[0]) & 3;
if ( (arrayData8[0] & 1) != fodd ) break;
*Data = *Data >> 1;
i++;
if ( i >= 39 ) return;
}
*Data = (*Data >> 1) ^ ROL((uint32_t) 0x14028003, 5);
}
}

1 change: 1 addition & 0 deletions EncDecSim.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void Transform (uint32_t *Data, KEY_INFO *keyInfo);
void Encode (uint32_t *bufPtr, uint32_t *nextBufPtr, KEY_INFO *keyInfo);
void Decode (uint32_t *bufPtr, uint32_t *nextBufPtr, KEY_INFO *keyInfo);
void GetCode (uint16_t seed, uint32_t *bufPtr, uint8_t *secTable);
void HashDWORD(uint32_t *Data, uint8_t *edStruct);

#endif

2 changes: 1 addition & 1 deletion USBKeyEmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void EmulateKey(PKEYDATA pKeyData, PKEY_REQUEST request, uint32_t *outBufLen, PK
if ( pKeyData->isKeyOpened ) {
keyResponse.status = KEY_OPERATION_STATUS_OK;
memcpy (keyResponse.data, &request->param1, 4);
Transform ((uint32_t *)keyResponse.data, (KEY_INFO *)pKeyData->edStruct);
HashDWORD ((uint32_t *)keyResponse.data, pKeyData->edStruct);
outDataLen = sizeof(uint32_t);
encodeOutData = 1;
}
Expand Down