@@ -47,9 +47,24 @@ const unsigned int rsBox[16][16] = {
4747// round constants to find round keys
4848const unsigned int rCon [11 ] = { 0x8d , 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 , 0x1b , 0x36 };
4949
50+ // GF(2^8) multiplication constants to do mix columns
51+ const unsigned int mCon [16 ][16 ] = {
52+ { 0x02 , 0x02 , 0x01 , 0x01 },
53+ { 0x01 , 0x02 , 0x03 , 0x01 },
54+ { 0x01 , 0x01 , 0x02 , 0x03 },
55+ { 0x03 , 0x01 , 0x01 , 0x02 }
56+ };
57+
58+ // GF(2^8) multiplication constants to reverse mix columns
59+ const unsigned int rmCon [16 ][16 ] = {
60+ { 0x0e , 0x0b , 0x0d , 0x09 },
61+ { 0x09 , 0x0e , 0x0b , 0x0d },
62+ { 0x0d , 0x09 , 0x0e , 0x0b },
63+ { 0x0b , 0x0d , 0x09 , 0x0e }
64+ };
5065 void key_schedule (unsigned int [][4 ]);
5166 void add_roundkey (unsigned int [][4 ], unsigned int [][4 ], unsigned int [][4 ]);
52- unsigned int sub_byte (unsigned int row , unsigned int column );
67+ unsigned int sub_byte (unsigned int , unsigned int );
5368
5469 int hexCharToDec (char );
5570 void get2Bytes (unsigned int , unsigned int * , unsigned int * );
@@ -78,19 +93,24 @@ int main(){
7893
7994 int i , j ; // counters for loops
8095
96+ // we calculate all round keys 1-10
8197 key_schedule (roundKey );
82- /*
98+
99+ /*
100+ printf("add round key:\n");
83101 add_roundkey(table ,state, roundKey);
84102 printArray(table);
85103
104+ printf("sub bytes:\n");
86105 for(i = 0; i < 4; i++){
87106 for(j = 0; j < 4; j++){
88107 get2Bytes(table[i][j], &row, &column);
89108 table[i][j] = sub_byte(row, column);
90109 }
91110 }
92111 printArray(table);
93- */
112+ */
113+
94114return 0 ;
95115}
96116
@@ -156,13 +176,6 @@ void key_schedule(unsigned int key[][4]){
156176 w [j + 1 ] = w [k + 1 ] ^ temp [1 ];
157177 w [j + 2 ] = w [k + 2 ] ^ temp [2 ];
158178 w [j + 3 ] = w [k + 3 ] ^ temp [3 ];
159-
160-
161- //printf("w[%d] = ", i);
162- for (j = 0 ; j < 4 ; j ++ ){
163- // printf("%x ", w[i*4 + j]);
164- }
165- //printf("\n");
166179 }
167180
168181 // print round keys 1 - 10
@@ -174,18 +187,17 @@ void key_schedule(unsigned int key[][4]){
174187 printf ("%x " , w [(i * 4 ) + j ]);
175188 }
176189 }
190+ printf ("\n\n\n\n" );
177191
178192return ;
179193}
180194
181195void add_roundkey (unsigned int result [][4 ], unsigned int a [][4 ], unsigned int b [][4 ]){
182196 int i , j ;
183197
184- for (i = 0 ; i < 4 ; i ++ ){
185- for (j = 0 ; j < 4 ; j ++ ){
198+ for (i = 0 ; i < 4 ; i ++ )
199+ for (j = 0 ; j < 4 ; j ++ )
186200 result [i ][j ] = a [i ][j ] ^ b [i ][j ];
187- }
188- }
189201
190202return ;
191203}
@@ -265,6 +277,7 @@ void printArray(unsigned int array[][4]){
265277 printf ("\n" );
266278 }
267279 printf ("\n" );
280+
268281return ;
269282}
270283
0 commit comments