Manually cleaned up MakeKeyFromBlock

I don't completely understand the algorithm that's being employed here, but at least the code is no longer hideous machine-generated unreadable nonsense. Should be much easier to translate to other languages now.
This commit is contained in:
2019-02-21 14:25:19 -06:00
parent b1cb846420
commit edc7050279

View File

@ -90,19 +90,16 @@
MakeKeyFromBlock(MakeBlock(sChallengeStr, 0)); MakeKeyFromBlock(MakeBlock(sChallengeStr, 0));
} }
private void MakeKeyFromBlock(byte[] pass) private void MakeKeyFromBlock(byte[] block)
{ {
if (pass == null) if (block == null)
{ {
return; return;
} }
if (pass.Length == 16 || pass.Length == 24 || pass.Length == 32)
if (block.Length == 16 || block.Length == 24 || block.Length == 32)
{ {
int i; switch (block.Length)
int j;
int[] iArr;
int tt;
switch (pass.Length)
{ {
case 16: case 16:
iROUNDS = 10; iROUNDS = 10;
@ -114,110 +111,76 @@
iROUNDS = MAX_ROUNDS; iROUNDS = MAX_ROUNDS;
break; break;
} }
for (i = 0; i <= iROUNDS; i++)
for (int round = 0; round <= iROUNDS; round++)
{ {
for (j = 0; j < 4; j++) for (int i = 0; i < 4; i++)
{ {
Ke[i, j] = 0; Ke[round, i] = 0;
Kd[i, j] = 0; Kd[round, i] = 0;
} }
} }
int ROUND_KEY_COUNT = (iROUNDS + 1) * 4; int ROUND_KEY_COUNT = (iROUNDS + 1) * 4;
int KC = pass.Length / 4; int numBlockChunks = block.Length / 4;
int j2 = 0; for (int blockChunk = 0; blockChunk < numBlockChunks; blockChunk++)
for (i = 0; i < KC; i++)
{ {
j = j2 + 1; tk[blockChunk] = (block[(blockChunk * 4) + 0] << 24) | (block[(blockChunk * 4) + 1] << 16) | (block[(blockChunk * 4) + 2] << 8) | block[(blockChunk * 4) + 3];
tk[i] = pass[j2] << 24;
iArr = tk;
j2 = j + 1;
iArr[i] = iArr[i] | (pass[j] << 16);
iArr = tk;
j = j2 + 1;
iArr[i] = iArr[i] | (pass[j2] << 8);
iArr = tk;
j2 = j + 1;
iArr[i] = iArr[i] | pass[j];
} }
int t = 0;
i = 0; int roundKey = 0;
while (i < KC && t < ROUND_KEY_COUNT) for (int i = 0; i < numBlockChunks && roundKey < ROUND_KEY_COUNT; i++, roundKey++)
{ {
Ke[t / 4, t % 4] = tk[i]; Ke[roundKey / 4, roundKey % 4] = tk[i];
Kd[iROUNDS - (t / 4), t % 4] = tk[i]; Kd[iROUNDS - (roundKey / 4), roundKey % 4] = tk[i];
i++;
t++;
} }
int rconpointer = 0;
while (t < ROUND_KEY_COUNT) for (int rconPointer = 0; roundKey < ROUND_KEY_COUNT; rconPointer++)
{ {
tt = tk[KC - 1];
iArr = tk;
int rconpointer2 = rconpointer + 1;
iArr[0] = iArr[0] ^ ((((((sm_S[(tt >> 16) & 255] & 255) << 24) ^ ((sm_S[(tt >> 8) & 255] & 255) << 16)) ^ ((sm_S[tt & 255] & 255) << 8)) ^ (sm_S[(tt >> 24) & 255] & 255)) ^ ((sm_rcon[rconpointer] & 255) << 24));
int i2;
if (KC != MAX_KC)
{ {
j2 = 0; var tkVal = tk[numBlockChunks - 1];
i2 = 1; tk[0] ^= ((((sm_S[(tkVal >> 16) & 0xFF] << 24) ^ (sm_S[(tkVal >> 8) & 0xFF] << 16)) ^ (sm_S[tkVal & 0xFF] << 8)) ^ sm_S[(tkVal >> 24) & 0xFF]) ^ (sm_rcon[rconPointer] << 24);
while (i2 < KC) }
if (numBlockChunks != MAX_KC)
{
for (int prevIdx = 0, tkIdx = 1; tkIdx < numBlockChunks; prevIdx++, tkIdx++)
{ {
iArr = tk; tk[tkIdx] ^= tk[prevIdx];
i = i2 + 1;
j = j2 + 1;
iArr[i2] = iArr[i2] ^ tk[j2];
j2 = j;
i2 = i;
} }
} }
else else
{ {
i = 1; var blockChunkMidpoint = numBlockChunks / 2;
j = 0; for (int prevIdx = 0, tkIdx = 1; tkIdx < blockChunkMidpoint; prevIdx++, tkIdx++)
while (i < KC / 2)
{ {
iArr = tk; tk[tkIdx] ^= tk[prevIdx];
i2 = i + 1;
j2 = j + 1;
iArr[i] = iArr[i] ^ tk[j];
j = j2;
i = i2;
} }
tt = tk[(KC / 2) - 1];
iArr = tk; var tkVal = tk[blockChunkMidpoint - 1];
int i3 = KC / 2; tk[blockChunkMidpoint] ^= ((sm_S[tkVal & 0xFF] ^ (sm_S[(tkVal >> 8) & 0xFF] << 8)) ^ (sm_S[(tkVal >> 16) & 0xFF] << 16)) ^ (sm_S[(tkVal >> 24) & 0xFF] << 24);
iArr[i3] = iArr[i3] ^ ((((sm_S[tt & 255] & 255) ^ ((sm_S[(tt >> 8) & 255] & 255) << 8)) ^ ((sm_S[(tt >> 16) & 255] & 255) << 16)) ^ ((sm_S[(tt >> 24) & 255] & 255) << 24)); for (int prevIdx = blockChunkMidpoint, tkIdx = blockChunkMidpoint + 1; tkIdx < numBlockChunks; prevIdx++, tkIdx++)
j = KC / 2;
i2 = j + 1;
j2 = j;
while (i2 < KC)
{ {
iArr = tk; tk[tkIdx] ^= tk[prevIdx];
i = i2 + 1;
j = j2 + 1;
iArr[i2] = iArr[i2] ^ tk[j2];
i2 = i;
j2 = j;
} }
} }
j = 0;
while (j < KC && t < ROUND_KEY_COUNT) for (int tkIdx = 0; tkIdx < numBlockChunks && roundKey < ROUND_KEY_COUNT; tkIdx++, roundKey++)
{ {
Ke[t / 4, t % 4] = tk[j]; Ke[roundKey / 4, roundKey % 4] = tk[tkIdx];
Kd[iROUNDS - (t / 4), t % 4] = tk[j]; Kd[iROUNDS - (roundKey / 4), roundKey % 4] = tk[tkIdx];
j++;
t++;
} }
rconpointer = rconpointer2;
} }
for (int r = 1; r < iROUNDS; r++)
for (int round = 1; round < iROUNDS; round++)
{ {
for (j = 0; j < 4; j++) for (int i = 0; i < 4; i++)
{ {
tt = Kd[r, j]; var tt = Kd[round, i];
Kd[r, j] = ((sm_U1[(tt >> 24) & 255] ^ sm_U2[(tt >> 16) & 255]) ^ sm_U3[(tt >> 8) & 255]) ^ sm_U4[tt & 255]; Kd[round, i] = ((sm_U1[(tt >> 24) & 0xFF] ^ sm_U2[(tt >> 16) & 0xFF]) ^ sm_U3[(tt >> 8) & 0xFF]) ^ sm_U4[tt & 0xFF];
} }
} }
bKeyInit = true; bKeyInit = true;
} }
} }