I am trying to implement the "Mix Column" function and its inverse in C++.

I have a piece of homework to implement AES-128. I have all the other functions (and there inverses) working appropriately. I am struggling with getting the mixcolumn function to work, however. I print the plaintext before applying the function, then I apply mix column and its inverse and print out the result. The two outputs do not match as they should, and I am at a loss as to why this is happening.

void mixColumns(array< array<uint8_t, 4>, 4> &state)
{
  //Create temp variable to store intermediate results                                                                            
  array< array<uint8_t,4>, 4> temp;
  //Perform matrix multiplication under GF
  for(int i=0;i<4;i++)
    {
      temp[0][i] = (0x02 * state[0][i]) ^ (0x03 * state[1][i]) ^ state[2][i] ^ state[3][i];
      temp[1][i] = state[0][i] ^ (0x02 * state[1][i]) ^ (0x03 * state[2][i]) ^ state[3][i];
      temp[2][i] = state[0][i] ^ state[1][i] ^ (0x02 * state[2][i]) ^ (0x03 * state[3][i]);
      temp[3][i] = (0x03 * state[0][i]) ^ state[1][i] ^ state[2][i] ^ (0x02 * state[3][i]);
    }
  //Fill state with mix column data                                                                                               
  for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
      state[j][i] = temp[j][i];
}

void invMixColumns(array< array<uint8_t, 4>, 4> &state)
{
  //Create temp variable to store intermediate results                                                                            
  array< array<uint8_t,4>, 4> temp;
  for(int i=0;i<4;i++)
    {
      temp[0][i] = (0x0E * state[0][i]) ^ (0x0B * state[1][i]) ^ (0x0D * state[2][i]) ^ (0x09 * state[3][i]);
      temp[1][i] = (0x09 * state[0][i]) ^ (0x0E * state[1][i]) ^ (0x0B * state[2][i]) ^ (0x0D * state[3][i]);
      temp[2][i] = (0x0D * state[0][i]) ^ (0x09 * state[1][i]) ^ (0x0E * state[2][i]) ^ (0x0B * state[3][i]);
      temp[3][i] = (0x0B * state[0][i]) ^ (0x0D * state[1][i]) ^ (0x09 * state[2][i]) ^ (0x0E * state[3][i]);
    }
  //Fill state with inverse column data                                                                                           
  for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
      state[j][i] = temp[j][i];

}

Input (and expected output): 11011010111011011000101101110110011001100001101101011000101101001001100100010101010101001110010111001111100110110110000110110010

Output with mixCoulmns & invMixColumns: 10111010111010010011111110010010011101101010111110001100001000000100100101101001001011001101010111111011000110110011010100110110

0 Answers