@@ -47,7 +47,7 @@ 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- void keySchedule (unsigned int [][4 ]);
50+ void key_schedule (unsigned int [][4 ]);
5151 void add_roundkey (unsigned int [][4 ], unsigned int [][4 ], unsigned int [][4 ]);
5252 unsigned int sub_byte (unsigned int row , unsigned int column );
5353
@@ -78,7 +78,7 @@ int main(){
7878
7979 int i , j ; // counters for loops
8080
81- keySchedule (roundKey );
81+ key_schedule (roundKey );
8282/*
8383 add_roundkey(table ,state, roundKey);
8484 printArray(table);
@@ -94,14 +94,19 @@ int main(){
9494return 0 ;
9595}
9696
97- void keySchedule (unsigned int key [][4 ]){
97+ void key_schedule (unsigned int key [][4 ]){
9898 // we have 10 round, so we need 40 words in array plus 4 for the given key
99- // its word have 4 bytes, so we need 44 * 4
99+ // its word have 4 bytes, so we need 44 * 4 = 176
100100 unsigned int w [176 ];
101101
102- int i , j ; // counters for loops
102+ int i , j , k ; // counters for loops and used as array "pointers"
103+
104+ unsigned int row , column ; // row and column for lookup
105+ unsigned int temp [4 ]; // temporary holds the results
103106
104107 // the first round key is the given key
108+ // we store it to the first 4 words
109+ // w0 w1 w2 w3 || w[0] ... w[15]
105110 for (i = 0 ; i < 4 ; i ++ ){
106111 printf ("w[%d] = " , i );
107112 for (j = 0 ; j < 4 ; j ++ ){
@@ -110,7 +115,66 @@ void keySchedule(unsigned int key[][4]){
110115 }
111116 printf ("\t" );
112117 }
118+
119+ // all other round keys are found from the previous round keys
120+ // start for 4, because we calculated the 4 words before
121+ // 4 is the block size and 10 is the number of rounds
122+ for (i = 4 ; i < 4 * (10 + 1 ); i ++ ){
123+ // k is "pointer" to find wi-1
124+ k = (i - 1 ) * 4 ;
125+ // temp = w-1
126+ temp [0 ] = w [k + 0 ];
127+ temp [1 ] = w [k + 1 ];
128+ temp [2 ] = w [k + 2 ];
129+ temp [3 ] = w [k + 3 ];
130+
131+ if (i % 4 == 0 ){
132+ // rot_word function
133+ k = temp [0 ];
134+ temp [0 ] = temp [1 ];
135+ temp [1 ] = temp [2 ];
136+ temp [2 ] = temp [3 ];
137+ temp [3 ] = k ;
138+
139+ // sub_word function
140+ for (j = 0 ; j < 4 ; j ++ ){
141+ get2Bytes (temp [j ], & row , & column );
142+ temp [j ] = sub_byte (row , column );
143+ }
144+
145+ // temp = sub_word(rot_word(temp)) XOR RCi/4
146+ temp [0 ] = temp [0 ] ^ rCon [i /4 ];
147+ }
148+
149+ // j is "pointer" to find wi
150+ j = i * 4 ;
151+ // k is "pointer" to find wi-4
152+ k = (i - 4 ) * 4 ;
113153
154+ // wi = wi-4 XOR temp
155+ w [j + 0 ] = w [k + 0 ] ^ temp [0 ];
156+ w [j + 1 ] = w [k + 1 ] ^ temp [1 ];
157+ w [j + 2 ] = w [k + 2 ] ^ temp [2 ];
158+ 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");
166+ }
167+
168+ // print round keys 1 - 10
169+ // round key 0(given key) printed before
170+ for (i = 4 ; i < 44 ; i ++ ){
171+ printf ("%s" , (i % 4 == 0 ) ? "\n" : "\t" );
172+ printf ("w[%d] = " , i );
173+ for (j = 0 ; j < 4 ; j ++ ){
174+ printf ("%x " , w [(i * 4 ) + j ]);
175+ }
176+ }
177+
114178return ;
115179}
116180
0 commit comments