I am working on AES encryption program using c, while doing the galois field multiplication in mix column block,
ex. [https://crypto.stackexchange.com/questions/2402/how-to-solve-mixcolumns][1]
code
for galois field multiplication
int galois_multiply( int a,int b){
int flag,res;
switch (a){
case 1:
return b;
break;
case 2: flag= b * 0x80;
b= b << 1; //left shift
if (flag)
res= b ^ 0x1b;
else
res= b ^0x00;
printf("\nnumber %d returned by galois_multiply function\n",res);
return res;
case 3: res= b ^ galois_multiply(2,b);
printf("\nnumber %d returned by galois_multiply function\n",res);
return res;
default:
printf("Invalid number %d passed to galois_multiply function\n",a);
exit(EXIT_FAILURE);
}
return 0;
}
suppose for
even though the above problem is solved by masking the msb, when used in mixcolumn in following code, the answer seems to be incorrect,its general matrix operation only where multiplication is replaced by galois multilication and addition by XOR operation
void mixColumn(unsigned char **state){
int mc[4][4]={{2,3,1,1},{1,2,3,1},{1,1,2,3},{3,1,1,2}};
int res[4][4]={{0}};
int i,j,k;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
res[i][j]=0;
for(k=0;k<4;k++)
res[i][j]= res[i][j] ^ galois_multiply(mc[i][k],state[k][j]);
state[i][j]=res[i][j];
}
}
}
can u locate any mistakes which might be causing the error...
finally caught the mistake i did in case 2, I have used
flag= b * 0x80;
but What I should have used is
flag= b & 0x80;
thinking in biary made me think both these are same operators, but at byte level the story is quite different, * will multiply the content by 80h whereas & will bitwise AND(multiply) the two operands, which is what I needed.