Skip to content

Commit 28a4c0b

Browse files
authored
Add files via upload
1 parent 0799a6a commit 28a4c0b

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

aes.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ const unsigned int rmCon[4][4] = {
6464
};
6565

6666
// aes functions prototypes
67-
void key_schedule(unsigned int [][4]);
67+
void key_schedule(unsigned int [], unsigned int [][4]);
6868
void add_roundkey(unsigned int [][4], unsigned int [][4], unsigned int [][4]);
6969
unsigned int sub_byte(unsigned int, unsigned int);
7070
void shift_rows(unsigned int [][4]);
7171

7272
// secondary functions
7373
int hexCharToDec(char);
7474
void get2Bytes(unsigned int, unsigned int *, unsigned int *);
75+
void getRoundKey(unsigned int [], unsigned int [][4], int);
76+
7577
void printArray(unsigned int [][4]);
7678

7779
int main(){
@@ -90,16 +92,25 @@ int main(){
9092
{ 0x74, 0x79, 0x6e, 0x75 }
9193
};
9294

95+
// we have 10 round, so we need 40 words in array plus 4 for the given key
96+
// its word have 4 bytes, so we need 44 * 4 = 176
97+
unsigned int w[176];
98+
9399
unsigned int table[4][4]; // temp array to hold results
94100

95101
unsigned int row, column; // row and column for lookup
96102

97103
int i, j; // counters for loops
98104

99105
// we calculate all round keys 1-10
100-
key_schedule(roundKey);
101-
106+
key_schedule(w, roundKey);
107+
102108
/*
109+
// get round key test
110+
printf("get round key:\n");
111+
for(i = 0; i < 11; i++)
112+
getRoundKey(w, roundKey, i);
113+
103114
// shift row test
104115
printf("shift rows:\n");
105116
printArray(state);
@@ -124,11 +135,7 @@ int main(){
124135
return 0;
125136
}
126137

127-
void key_schedule(unsigned int key[][4]){
128-
// we have 10 round, so we need 40 words in array plus 4 for the given key
129-
// its word have 4 bytes, so we need 44 * 4 = 176
130-
unsigned int w[176];
131-
138+
void key_schedule(unsigned int w[], unsigned int key[][4]){
132139
int i, j, k; // counters for loops and used as array "pointers"
133140

134141
unsigned int row, column; // row and column for lookup
@@ -149,7 +156,7 @@ void key_schedule(unsigned int key[][4]){
149156
// all other round keys are found from the previous round keys
150157
// start for 4, because we calculated the 4 words before
151158
// 4 is the block size and 10 is the number of rounds
152-
for(i = 4; i < 4 * (10 + 1); i++){
159+
for(i = 4; i < (4 * (10 + 1)); i++){
153160
// k is "pointer" to find wi-1
154161
k = (i - 1) * 4;
155162
// temp = w-1
@@ -315,6 +322,26 @@ void get2Bytes(unsigned int a, unsigned int *row, unsigned int *column){
315322
return;
316323
}
317324

325+
void getRoundKey(unsigned int w[], unsigned int roundKey[][4], int round){
326+
int i, j;
327+
328+
printf("Round key %d: ", round);
329+
for(i = (round * 4); i < ((round * 4) + 4); i++){
330+
for(j = 0; j < 4; j++){
331+
if(round == 0){
332+
roundKey[j][i] = w[(i * 4) + j];
333+
printf("%x ", roundKey[j][i]);
334+
}else{
335+
roundKey[j][i - (round * 4)] = w[(i * 4) + j];
336+
printf("%x ", roundKey[j][i - (round * 4)]);
337+
}
338+
}
339+
}
340+
printf("\n");
341+
342+
return;
343+
}
344+
318345
void printArray(unsigned int array[][4]){
319346
int i, j;
320347

0 commit comments

Comments
 (0)