6. MixColumns (25 points) This step is the same as the MixColumns step in the AE
ID: 669220 • Letter: 6
Question
6. MixColumns (25 points) This step is the same as the MixColumns step in the AES encryption. It diffuses the data. The transformation is performed by multiplying the so called circulant MDS matrix with each column from the input (i.e., the output of step 5) as shown below. This transformation is repeated for each block. 01 C2 C3 03 However, this is not a simple multiplication. It is a multiplication in the Rijndael's Galois field. No need to panic! It is not as difficult as it sounds. You only need to do multiplication with 2 and 3 because, besides 1, these are the only values in the transformation matrix. Let's assume we want to multiply the eight bit number. x = 10101111 with 2-00000010 and 3 00000011, in Rijndael's Galois field. The multiplication is done the following way: Multiplication with 2: r * 2-101011 11 « 1 (10101111 « 1) 0101 1110 where « is left shift and "I' is the number of places to be shifted. This is the easiest way to multiply a number by 2. Notice that the most significant bit (MSB) of r, which is 1, is dropped. Because the MSB of r is 1 the result has to be XORed with 00011011 01011110 00011011 01000101 Multiplication with 3 Since 3 can be represented as a sum of 2 and 1, the multiplication can be performed as z 3 as 3 (2 +1)2+r. However in Rijndael's Galois field addition is done with the XOR operation so the miu!tiplication would be x * 3 z * 2 3Explanation / Answer
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define Nb 4
#define xtime(x) ((x<<1) ^ (((x>>7) & 1) * 0x1b))
unsigned char multiply(unsigned char a, unsigned char b)
{
int i;
unsigned char c = 0;
unsigned char d = b;
for (int i=0 ; i < 8 ; i++)
{
if (a%2 == 1) c ^= d;
a /= 2;
d = xtime(d);
}
return c;
}
void MixColumns(unsigned char a, unsigned char b, unsigned char c, unsigned char d)
{
int i;
unsigned char Tmp,Tm,t,e,f,g,h;
t = a;
Tmp = a ^ b ^ c ^ d;
Tm = a ^ b ;
Tm = xtime(Tm);
e = Tm ^ Tmp ^ a ;
Tm = b ^ c;
Tm = xtime(Tm);
f = Tm ^ Tmp ^ b;
Tm = c ^ d ;
Tm = xtime(Tm);
g = Tm ^ Tmp ^ c;
Tm = d ^ t ;
Tm = xtime(Tm);
h = Tm ^ Tmp ^ d;
printf("output: a=0x%02x b=0x%02x c=0x%02x d=0x%02x ",e,f,g,h);
}
unsigned char htoi(char *x)
{
unsigned char y = 0;
if (x[1] == 'x' || x[1] == 'X')
{
unsigned char a, b;
if (x[2]-'0' < 10) a = (x[2]-'0')*16;
else if (x[2]-'A' < 6 && x[2] -'A' >= 0) a = (x[2]-'A'+10)*16;
else a = (x[2]-'a'+10)*16;
if (x[3]-'0' < 10) b = x[3]-'0';
else if (x[3]-'A' < 6 && x[3] -'A' >= 0) b = x[3]-'A'+10;
else b = x[3]-'a'+10;
y = a + b;
}
else y = atoi(x);
return y;
}
int main(int argc, char **argv)
{
int i;
if (argc != 5)
{
cerr << "Usage: " << argv[0] << " <num> <num> <num> <num> ";
cerr << " test of mixcolumn function ";
exit(0);
}
unsigned char a = htoi(argv[1]);
unsigned char b = htoi(argv[2]);
unsigned char c = htoi(argv[3]);
unsigned char d = htoi(argv[4]);
printf("input: a=0x%02x(%d) b=0x%02x(%d) c=0x%02x(%d) d=0x%02x(%d) ",a, a, b, b, c, c, d, d);
MixColumns(a, b, c, d);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.