Ok, 2nd attempt. This time I remember arrays == pointers. Code makes alittle more sense to me atleast but still wrong.
Any suggestions anychance? Back to being stumped already.
Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned char gf_mult(unsigned char a, unsigned char b) {
int i;
int retval;
/* If there isnt a "overflow", normal multiplication can be used */
retval = a * b;
if(retval < 128)
return retval;
else
retval = 0;
/* GF multiplication with insane bit shifting */
for(i = 0; i < 8; i++) {
if((b & 1) == 1)
retval ^= a;
if((a & 0x80) == 0x80) {
a <<= 1;
a ^= 0x1b;
} else {
a <<= 1;
}
b >>= 1;
}
return (unsigned char)retval;
}
void print_state(unsigned char state[4][4]) {
int i;
int j;
printf("+----+----+----+----+\n");
for(i = 0; i < 4; ++i) {
printf("|");
for(j = 0; j < 4; ++j)
printf("0x%02X|", state[i][j]);
printf("\n");
printf("+----+----+----+----+\n");
}
}
void state123(unsigned char state[4][4]) {
int x = 1;
int i;
int j;
for(i = 0; i < 4; ++i) {
for(j = 0; j < 4; ++j) {
state[i][j] = x;
++x;
}
}
}
void rand_state(unsigned char state[4][4]) {
int i;
int j;
srand ( time(NULL) );
for(i = 0; i < 4; ++i) {
for(j = 0; j < 4; ++j)
state[i][j] = (rand() % 256);
}
}
void copy_state(unsigned char src[4][4], unsigned char dst[4][4]) {
int i;
int j;
for(i = 0; i < 4; ++i) {
for(j = 0; j < 4; ++j)
dst[i][j] = src[i][j];
}
}
int state_equal(unsigned char a[4][4], unsigned char b[4][4]) {
int i;
int j;
for(i = 0; i < 4; ++i) {
for(j = 0; j < 4; ++j) {
if(a[i][j] != b[i][j])
return 1;
}
}
return 0;
}
void MixColumns(unsigned char in[4][4], unsigned char out[4][4]) {
int c;
for(c = 0; c < 4; ++c) {
out[0][c] = gf_mult(0x02, in[0][c]) ^ gf_mult(0x03, in[1][c])
^ in[2][c] ^ in[3][c];
out[1][c] = in[0][c] ^ gf_mult(0x02, in[1][c])
^ gf_mult(0x03, in[2][c]) ^ in[3][c];
out[2][c] = in[0][c] ^ in[1][c] ^ gf_mult(0x02, in[2][c])
^ gf_mult(0x03, in[3][c]);
out[3][c] = gf_mult(0x03, in[0][c]) ^ in[1][c] ^ in[2][c]
^ gf_mult(0x02, in[3][c]);
}
}
void InvMixColumns(unsigned char in[4][4], unsigned char out[4][4]) {
int c;
for(c = 0; c < 4; ++c) {
out[0][c] = gf_mult(0x0E, in[0][c]) ^ gf_mult(0x0B, in[1][c])
^ gf_mult(0x0D, in[2][c]) ^ gf_mult(0x09, in[3][c]);
out[1][c] = gf_mult(0x09, in[0][c]) ^ gf_mult(0x0E, in[1][c])
^ gf_mult(0x0B, in[2][c]) ^ gf_mult(0x0D, in[3][c]);
out[2][c] = gf_mult(0x0D, in[0][c]) ^ gf_mult(0x09, in[1][c])
^ gf_mult(0x0E, in[2][c]) ^ gf_mult(0x0B, in[3][c]);
out[3][c] = gf_mult(0x0B, in[0][c]) ^ gf_mult(0x0D, in[1][c])
^ gf_mult(0x09, in[2][c]) ^ gf_mult(0x0E, in[3][c]);
}
}
int TestMixColumns(unsigned char state[4][4]) {
unsigned char mc[4][4];
unsigned char inv_mc[4][4];
MixColumns(state, mc);
InvMixColumns(mc, inv_mc);
return state_equal(state, inv_mc);
}
int RandomMixColumnsTests(int tests) {
int i;
int wrong = 0;
unsigned char in[4][4];
for(i = 0; i < tests; ++i) {
rand_state(in);
if(TestMixColumns(in) != 0)
++wrong;
}
printf("%i/%i wrong\n", wrong, tests);
return wrong;
}
int main (int argc, const char * argv[]) {
unsigned char in[4][4];
state123(in);
printf("123 state:\n");
print_state(in);
printf("\nMC Tests: ");
RandomMixColumnsTests(100);
return 0;
}
output:
123 state:
+----+----+----+----+
|0x01|0x02|0x03|0x04|
+----+----+----+----+
|0x05|0x06|0x07|0x08|
+----+----+----+----+
|0x09|0x0A|0x0B|0x0C|
+----+----+----+----+
|0x0D|0x0E|0x0F|0x10|
+----+----+----+----+
MC Tests: 100/100 wrong