I am trying to implement an encryption function that uses the AES T-Tables implementation.
I am quite sure that my roundKeys are correct. I also know that the initial round key addition in the following code is correct.
So there must be an error in the main rounds, but i am not sure what went wrong. I have narrowed it down to the following problems:
- The logic I use to lookup in the t-tables in each round:
temp[i] = T0[(state[i] >>> 24) & 0xFF] ^
T1[(state[(i + 3) % 4] >>> 16) & 0xFF] ^
T2[(state[(i + 2) % 4] >>> 8) & 0xFF] ^
T3[state[(i + 1) % 4] & 0xFF] ^ roundKeys[round][i];
That seems correct? I followed the descripton in Rijnadel Proposel on page 18.
- Maybe I am using the wrong t-tables for looking up. I am not sure which ones are correct. I found T-Tables in the bouncy castle implementation and then i found some different tables in the Go implementation.
They are both different, but i have tried my code with both and it doesnt work. Which of these tables are the correct ones?
public byte[] blockEncryption(byte[] plaintext, int[][] roundKeys) {
// Convert plaintext bytes to state matrix
int[] state = new int[4];
for (int i = 0; i < 4; i++) {
state[i] = ((plaintext[4 * i] & 0xFF) << 24) |
((plaintext[4 * i + 1] & 0xFF) << 16) |
((plaintext[4 * i + 2] & 0xFF) << 8) |
(plaintext[4 * i + 3] & 0xFF);
}
// Initial round key addition
for (int i = 0; i < 4; i++) {
state[i] ^= roundKeys[0][i];
}
// Main rounds
for (int round = 1; round < 10; round++) {
int[] temp = new int[4];
for (int i = 0; i < 4; i++) {
temp[i] = T0[(state[i] >>> 24) & 0xFF] ^
T1[(state[(i + 3) % 4] >>> 16) & 0xFF] ^
T2[(state[(i + 2) % 4] >>> 8) & 0xFF] ^
T3[state[(i + 1) % 4] & 0xFF] ^ roundKeys[round][i];
};System.arraycopy(temp, 0, state, 0, 4);
}
// Final round (without MixColumns)
byte[] ciphertext = new byte[16];
for (int i = 0; i < 4; i++) {
int word = roundKeys[roundKeys.length - 1][i];
ciphertext[4 * i] = (byte) (S[(state[i] >>> 24) & 0xFF] ^ (word >>> 24));
ciphertext[4 * i + 1] = (byte) (S[(state[(i + 3) % 4] >>> 16) & 0xFF] ^ (word >>> 16));
ciphertext[4 * i + 2] = (byte) (S[(state[(i + 2) % 4] >>> 8) & 0xFF] ^ (word >>> 8));
ciphertext[4 * i + 3] = (byte) (S[state[(i + 1) % 4] & 0xFF] ^ word);
}
System.out.println("Endzustand: " + intArrayToHex(state));
return ciphertext;
}
```