1 |
21 |
jcastillo |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// AES C encrypt and decrypt functions for C golden model ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the SystemC AES ////
|
6 |
|
|
//// ////
|
7 |
|
|
//// Description: ////
|
8 |
|
|
//// AES C encrypt and decrypt functions for C golden model ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// To Do: ////
|
11 |
|
|
//// - done ////
|
12 |
|
|
//// ////
|
13 |
|
|
//// Author(s): ////
|
14 |
|
|
//// - Javier Castillo, jcastilo@opencores.org ////
|
15 |
|
|
//// ////
|
16 |
|
|
//////////////////////////////////////////////////////////////////////
|
17 |
|
|
//// ////
|
18 |
|
|
//// Copyright (C) 2000 Authors and OPENCORES.ORG ////
|
19 |
|
|
//// ////
|
20 |
|
|
//// This source file may be used and distributed without ////
|
21 |
|
|
//// restriction provided that this copyright statement is not ////
|
22 |
|
|
//// removed from the file and that any derivative work contains ////
|
23 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
24 |
|
|
//// ////
|
25 |
|
|
//// This source file is free software; you can redistribute it ////
|
26 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
27 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
28 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
29 |
|
|
//// later version. ////
|
30 |
|
|
//// ////
|
31 |
|
|
//// This source is distributed in the hope that it will be ////
|
32 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
33 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
34 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
35 |
|
|
//// details. ////
|
36 |
|
|
//// ////
|
37 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
38 |
|
|
//// Public License along with this source; if not, download it ////
|
39 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
40 |
|
|
//// ////
|
41 |
|
|
//////////////////////////////////////////////////////////////////////
|
42 |
|
|
//
|
43 |
|
|
// CVS Revision History
|
44 |
|
|
//
|
45 |
|
|
// $Log: not supported by cvs2svn $
|
46 |
|
|
// Revision 1.2 2004/08/30 14:47:38 jcastillo
|
47 |
|
|
// Code formated
|
48 |
|
|
//
|
49 |
|
|
// Revision 1.1.1.1 2004/07/05 09:46:22 jcastillo
|
50 |
|
|
// First import
|
51 |
|
|
//
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
static const unsigned char Alogtable[] =
|
55 |
|
|
{
|
56 |
|
|
1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
|
57 |
|
|
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
|
58 |
|
|
229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
|
59 |
|
|
83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
|
60 |
|
|
76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
|
61 |
|
|
131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
|
62 |
|
|
181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
|
63 |
|
|
254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
|
64 |
|
|
251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
|
65 |
|
|
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
|
66 |
|
|
159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
|
67 |
|
|
155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
|
68 |
|
|
252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
|
69 |
|
|
69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
|
70 |
|
|
18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
|
71 |
|
|
57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1,
|
72 |
|
|
};
|
73 |
|
|
/*----------------------------------------------------------------------------*/
|
74 |
|
|
static const unsigned char Logtable[] =
|
75 |
|
|
{
|
76 |
|
|
0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3,
|
77 |
|
|
100, 4, 224, 14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193,
|
78 |
|
|
125, 194, 29, 181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120,
|
79 |
|
|
101, 47, 138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142,
|
80 |
|
|
150, 143, 219, 189, 54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56,
|
81 |
|
|
102, 221, 253, 48, 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16,
|
82 |
|
|
126, 110, 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186,
|
83 |
|
|
43, 121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87,
|
84 |
|
|
175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230, 173, 232,
|
85 |
|
|
44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95, 176, 156, 169, 81, 160,
|
86 |
|
|
127, 12, 246, 111, 23, 196, 73, 236, 216, 67, 31, 45, 164, 118, 123, 183,
|
87 |
|
|
204, 187, 62, 90, 251, 96, 177, 134, 59, 82, 161, 108, 170, 85, 41, 157,
|
88 |
|
|
151, 178, 135, 144, 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209,
|
89 |
|
|
83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242, 211, 171,
|
90 |
|
|
68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38, 119, 153, 227, 165,
|
91 |
|
|
103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140, 128, 192, 247, 112, 7,
|
92 |
|
|
};
|
93 |
|
|
/*----------------------------------------------------------------------------*/
|
94 |
|
|
static const unsigned char Sen[] =
|
95 |
|
|
{
|
96 |
|
|
99, 124, 119, 123, 242, 107, 111, 197,
|
97 |
|
|
48, 1, 103, 43, 254, 215, 171, 118,
|
98 |
|
|
202, 130, 201, 125, 250, 89, 71, 240,
|
99 |
|
|
173, 212, 162, 175, 156, 164, 114, 192,
|
100 |
|
|
183, 253, 147, 38, 54, 63, 247, 204,
|
101 |
|
|
52, 165, 229, 241, 113, 216, 49, 21,
|
102 |
|
|
4, 199, 35, 195, 24, 150, 5, 154,
|
103 |
|
|
7, 18, 128, 226, 235, 39, 178, 117,
|
104 |
|
|
9, 131, 44, 26, 27, 110, 90, 160,
|
105 |
|
|
82, 59, 214, 179, 41, 227, 47, 132,
|
106 |
|
|
83, 209, 0, 237, 32, 252, 177, 91,
|
107 |
|
|
106, 203, 190, 57, 74, 76, 88, 207,
|
108 |
|
|
208, 239, 170, 251, 67, 77, 51, 133,
|
109 |
|
|
69, 249, 2, 127, 80, 60, 159, 168,
|
110 |
|
|
81, 163, 64, 143, 146, 157, 56, 245,
|
111 |
|
|
188, 182, 218, 33, 16, 255, 243, 210,
|
112 |
|
|
205, 12, 19, 236, 95, 151, 68, 23,
|
113 |
|
|
196, 167, 126, 61, 100, 93, 25, 115,
|
114 |
|
|
96, 129, 79, 220, 34, 42, 144, 136,
|
115 |
|
|
70, 238, 184, 20, 222, 94, 11, 219,
|
116 |
|
|
224, 50, 58, 10, 73, 6, 36, 92,
|
117 |
|
|
194, 211, 172, 98, 145, 149, 228, 121,
|
118 |
|
|
231, 200, 55, 109, 141, 213, 78, 169,
|
119 |
|
|
108, 86, 244, 234, 101, 122, 174, 8,
|
120 |
|
|
186, 120, 37, 46, 28, 166, 180, 198,
|
121 |
|
|
232, 221, 116, 31, 75, 189, 139, 138,
|
122 |
|
|
112, 62, 181, 102, 72, 3, 246, 14,
|
123 |
|
|
97, 53, 87, 185, 134, 193, 29, 158,
|
124 |
|
|
225, 248, 152, 17, 105, 217, 142, 148,
|
125 |
|
|
155, 30, 135, 233, 206, 85, 40, 223,
|
126 |
|
|
140, 161, 137, 13, 191, 230, 66, 104,
|
127 |
|
|
65, 153, 45, 15, 176, 84, 187, 22
|
128 |
|
|
};
|
129 |
|
|
/*----------------------------------------------------------------------------*/
|
130 |
|
|
static const unsigned char Sde[] =
|
131 |
|
|
{
|
132 |
|
|
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251,
|
133 |
|
|
124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203,
|
134 |
|
|
84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78,
|
135 |
|
|
8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37,
|
136 |
|
|
114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146,
|
137 |
|
|
108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132,
|
138 |
|
|
144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6,
|
139 |
|
|
208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107,
|
140 |
|
|
58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115,
|
141 |
|
|
150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110,
|
142 |
|
|
71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27,
|
143 |
|
|
252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244,
|
144 |
|
|
31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95,
|
145 |
|
|
96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239,
|
146 |
|
|
160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
|
147 |
|
|
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125,
|
148 |
|
|
};
|
149 |
|
|
|
150 |
|
|
void
|
151 |
|
|
switchblock(unsigned char *block)
|
152 |
|
|
{
|
153 |
|
|
int i;
|
154 |
|
|
unsigned char *aux;
|
155 |
|
|
aux = (unsigned char *)malloc(16 * sizeof(char));
|
156 |
|
|
|
157 |
|
|
*(aux) = *(block);
|
158 |
|
|
*(aux + 1) = *(block + 4);
|
159 |
|
|
*(aux + 2) = *(block + 8);
|
160 |
|
|
*(aux + 3) = *(block + 12);
|
161 |
|
|
*(aux + 4) = *(block + 1);
|
162 |
|
|
*(aux + 5) = *(block + 5);
|
163 |
|
|
*(aux + 6) = *(block + 9);
|
164 |
|
|
*(aux + 7) = *(block + 13);
|
165 |
|
|
*(aux + 8) = *(block + 2);
|
166 |
|
|
*(aux + 9) = *(block + 6);
|
167 |
|
|
*(aux + 10) = *(block + 10);
|
168 |
|
|
*(aux + 11) = *(block + 14);
|
169 |
|
|
*(aux + 12) = *(block + 3);
|
170 |
|
|
*(aux + 13) = *(block + 7);
|
171 |
|
|
*(aux + 14) = *(block + 11);
|
172 |
|
|
*(aux + 15) = *(block + 15);
|
173 |
|
|
|
174 |
|
|
for (i = 0; i < 16; i++)
|
175 |
|
|
*(block + i) = *(aux + i);
|
176 |
|
|
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
void
|
180 |
|
|
RotWord(unsigned char *a)
|
181 |
|
|
{
|
182 |
|
|
unsigned char aux;
|
183 |
|
|
aux = *(a + 0);
|
184 |
|
|
*(a + 0) = *(a + 1);
|
185 |
|
|
*(a + 1) = *(a + 2);
|
186 |
|
|
*(a + 2) = *(a + 3);
|
187 |
|
|
*(a + 3) = aux;
|
188 |
|
|
}
|
189 |
|
|
/*----------------------------------------------------------------------------*/
|
190 |
|
|
void
|
191 |
|
|
KeySchedule(unsigned char *key, unsigned char *rcon)
|
192 |
|
|
{
|
193 |
|
|
unsigned char aux1[4];
|
194 |
|
|
unsigned char aux2[4];
|
195 |
|
|
unsigned char aux3[4];
|
196 |
|
|
int i = 0;
|
197 |
|
|
for (i = 0; i < 4; i++)
|
198 |
|
|
{
|
199 |
|
|
aux1[i] = *(key + i * 4 + 3);
|
200 |
|
|
aux2[i] = *(key + i * 4);
|
201 |
|
|
}
|
202 |
|
|
RotWord((unsigned char *)aux1);
|
203 |
|
|
|
204 |
|
|
//SubBytes
|
205 |
|
|
for (i = 0; i < 4; i++)
|
206 |
|
|
{
|
207 |
|
|
aux1[i] = Sen[aux1[i]];
|
208 |
|
|
}
|
209 |
|
|
for (i = 0; i < 4; i++)
|
210 |
|
|
{
|
211 |
|
|
aux3[i] = aux2[i] ^ aux1[i] ^ *(rcon + i);
|
212 |
|
|
*(key + i * 4) = aux3[i];
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
for (i = 0; i < 4; i++)
|
216 |
|
|
{
|
217 |
|
|
aux3[i] = *(key + i * 4 + 1) ^ aux3[i];
|
218 |
|
|
*(key + i * 4 + 1) = aux3[i];
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
for (i = 0; i < 4; i++)
|
222 |
|
|
{
|
223 |
|
|
aux3[i] = *(key + i * 4 + 2) ^ aux3[i];
|
224 |
|
|
*(key + i * 4 + 2) = aux3[i];
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
for (i = 0; i < 4; i++)
|
228 |
|
|
{
|
229 |
|
|
aux3[i] = *(key + i * 4 + 3) ^ aux3[i];
|
230 |
|
|
*(key + i * 4 + 3) = aux3[i];
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
}
|
234 |
|
|
/*----------------------------------------------------------------------------*/
|
235 |
|
|
unsigned char
|
236 |
|
|
mul(unsigned char a, unsigned char b)
|
237 |
|
|
{
|
238 |
|
|
if (a && b)
|
239 |
|
|
return Alogtable[(Logtable[a] + Logtable[b])%255];
|
240 |
|
|
else
|
241 |
|
|
return 0;
|
242 |
|
|
}
|
243 |
|
|
/*----------------------------------------------------------------------------*/
|
244 |
|
|
void
|
245 |
|
|
MixColum(unsigned char *state)
|
246 |
|
|
{
|
247 |
|
|
int j = 0;
|
248 |
|
|
unsigned char aux = 0;
|
249 |
|
|
unsigned char aux_vector[16];
|
250 |
|
|
|
251 |
|
|
for (j = 0; j < 4; j++)
|
252 |
|
|
{
|
253 |
|
|
aux = mul(0x2, *(state + j)) ^ mul(0x3, *(state + j + 4)) ^ *(state + j + 8) ^ *(state + j + 12);
|
254 |
|
|
aux_vector[j] = aux;
|
255 |
|
|
aux = *(state + j) ^ mul(0x2, *(state + j + 4)) ^ mul(0x3, *(state + j + 8)) ^ *(state + j + 12);
|
256 |
|
|
aux_vector[j + 4] = aux;
|
257 |
|
|
aux = *(state + j) ^ *(state + j + 4) ^ mul(0x2, *(state + j + 8)) ^ mul(0x3, *(state + j + 12));
|
258 |
|
|
aux_vector[j + 8] = aux;
|
259 |
|
|
aux = mul(0x3, *(state + j)) ^ *(state + j + 4) ^ *(state + j + 8) ^ mul(0x2, *(state + j + 12));
|
260 |
|
|
aux_vector[j + 12] = aux;
|
261 |
|
|
}
|
262 |
|
|
for (j = 0; j < 16; j++)
|
263 |
|
|
*(state + j) = aux_vector[j];
|
264 |
|
|
}
|
265 |
|
|
/*----------------------------------------------------------------------------*/
|
266 |
|
|
void
|
267 |
|
|
InvMixColum(unsigned char *state)
|
268 |
|
|
{
|
269 |
|
|
int j = 0;
|
270 |
|
|
unsigned char aux = 0;
|
271 |
|
|
unsigned char aux_vector[16];
|
272 |
|
|
|
273 |
|
|
for (j = 0; j < 4; j++)
|
274 |
|
|
{
|
275 |
|
|
aux = mul(0x0e, *(state + j)) ^ mul(0x0b, *(state + j + 4)) ^ mul(0x0d, *(state + j + 8)) ^ mul(0x09, *(state + j + 12));
|
276 |
|
|
aux_vector[j] = aux;
|
277 |
|
|
aux = mul(0x09, *(state + j)) ^ mul(0x0e, *(state + j + 4)) ^ mul(0x0b, *(state + j + 8)) ^ mul(0x0d, *(state + j + 12));
|
278 |
|
|
aux_vector[j + 4] = aux;
|
279 |
|
|
aux = mul(0x0d, *(state + j)) ^ mul(0x09, *(state + j + 4)) ^ mul(0x0e, *(state + j + 8)) ^ mul(0x0b, *(state + j + 12));
|
280 |
|
|
aux_vector[j + 8] = aux;
|
281 |
|
|
aux = mul(0x0b, *(state + j)) ^ mul(0x0d, *(state + j + 4)) ^ mul(0x09, *(state + j + 8)) ^ mul(0x0e, *(state + j + 12));
|
282 |
|
|
aux_vector[j + 12] = aux;
|
283 |
|
|
}
|
284 |
|
|
for (j = 0; j < 16; j++)
|
285 |
|
|
*(state + j) = aux_vector[j];
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
/*----------------------------------------------------------------------------*/
|
289 |
|
|
void
|
290 |
|
|
AddRoundKey(unsigned char *state, unsigned char *key)
|
291 |
|
|
{
|
292 |
|
|
int i = 0;
|
293 |
|
|
for (i = 0; i < 16; i++)
|
294 |
|
|
*(state + i) ^= *(key + i);
|
295 |
|
|
}
|
296 |
|
|
/*----------------------------------------------------------------------------*/
|
297 |
|
|
/*void
|
298 |
|
|
PrintState(unsigned char *state)
|
299 |
|
|
{
|
300 |
|
|
int i = 0;
|
301 |
|
|
for(i = 0; i < 16; i++)
|
302 |
|
|
{
|
303 |
|
|
diag_printf("%x ", *(state + i));
|
304 |
|
|
if((i+1) % 4 == 0)
|
305 |
|
|
diag_printf("\n");
|
306 |
|
|
|
307 |
|
|
}
|
308 |
|
|
}*/
|
309 |
|
|
/*----------------------------------------------------------------------------*/
|
310 |
|
|
void
|
311 |
|
|
SubBytes(unsigned char *state)
|
312 |
|
|
{
|
313 |
|
|
int i = 0;
|
314 |
|
|
for (i = 0; i < 16; i++)
|
315 |
|
|
{
|
316 |
|
|
*(state + i) = Sen[*(state + i)];
|
317 |
|
|
}
|
318 |
|
|
}
|
319 |
|
|
/*----------------------------------------------------------------------------*/
|
320 |
|
|
void
|
321 |
|
|
InvSubBytes(unsigned char *state)
|
322 |
|
|
{
|
323 |
|
|
int i = 0;
|
324 |
|
|
for (i = 0; i < 16; i++)
|
325 |
|
|
{
|
326 |
|
|
*(state + i) = Sde[*(state + i)];
|
327 |
|
|
}
|
328 |
|
|
}
|
329 |
|
|
/*----------------------------------------------------------------------------*/
|
330 |
|
|
|
331 |
|
|
void
|
332 |
|
|
ShiftRows(unsigned char *state)
|
333 |
|
|
{
|
334 |
|
|
unsigned char AUX[16];
|
335 |
|
|
int i = 0;
|
336 |
|
|
for (i = 0; i < 16; i++)
|
337 |
|
|
{
|
338 |
|
|
AUX[i] = *(state + i);
|
339 |
|
|
}
|
340 |
|
|
*(state + 4) = AUX[5];
|
341 |
|
|
*(state + 5) = AUX[6];
|
342 |
|
|
*(state + 6) = AUX[7];
|
343 |
|
|
*(state + 7) = AUX[4];
|
344 |
|
|
|
345 |
|
|
*(state + 8) = AUX[10];
|
346 |
|
|
*(state + 9) = AUX[11];
|
347 |
|
|
*(state + 10) = AUX[8];
|
348 |
|
|
*(state + 11) = AUX[9];
|
349 |
|
|
|
350 |
|
|
*(state + 12) = AUX[15];
|
351 |
|
|
*(state + 13) = AUX[12];
|
352 |
|
|
*(state + 14) = AUX[13];
|
353 |
|
|
*(state + 15) = AUX[14];
|
354 |
|
|
}
|
355 |
|
|
/*----------------------------------------------------------------------------*/
|
356 |
|
|
|
357 |
|
|
void
|
358 |
|
|
InvShiftRows(unsigned char *state)
|
359 |
|
|
{
|
360 |
|
|
unsigned char AUX[16];
|
361 |
|
|
int i = 0;
|
362 |
|
|
for (i = 0; i < 16; i++)
|
363 |
|
|
{
|
364 |
|
|
AUX[i] = *(state + i);
|
365 |
|
|
}
|
366 |
|
|
*(state + 4) = AUX[7];
|
367 |
|
|
*(state + 5) = AUX[4];
|
368 |
|
|
*(state + 6) = AUX[5];
|
369 |
|
|
*(state + 7) = AUX[6];
|
370 |
|
|
|
371 |
|
|
*(state + 8) = AUX[10];
|
372 |
|
|
*(state + 9) = AUX[11];
|
373 |
|
|
*(state + 10) = AUX[8];
|
374 |
|
|
*(state + 11) = AUX[9];
|
375 |
|
|
|
376 |
|
|
*(state + 12) = AUX[13];
|
377 |
|
|
*(state + 13) = AUX[14];
|
378 |
|
|
*(state + 14) = AUX[15];
|
379 |
|
|
*(state + 15) = AUX[12];
|
380 |
|
|
}
|
381 |
|
|
/*----------------------------------------------------------------------------*/
|
382 |
|
|
|
383 |
|
|
unsigned char *
|
384 |
|
|
KeyExpand(unsigned char *key)
|
385 |
|
|
{
|
386 |
|
|
int i = 0;
|
387 |
|
|
int j = 0;
|
388 |
|
|
|
389 |
|
|
unsigned char aux[10] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
|
390 |
|
|
unsigned char rcon[4] = {0x00, 0x00, 0x00, 0x00};
|
391 |
|
|
|
392 |
|
|
unsigned char auxmalloc[1000];
|
393 |
|
|
unsigned char *expandedkey;
|
394 |
|
|
expandedkey = (unsigned char *)auxmalloc;
|
395 |
|
|
|
396 |
|
|
for (i = 0; i < 16; i++)
|
397 |
|
|
*(expandedkey + i) = *(key + i);
|
398 |
|
|
|
399 |
|
|
for (i = 0; i < 10; i++)
|
400 |
|
|
{
|
401 |
|
|
rcon[0] = aux[i];
|
402 |
|
|
KeySchedule((unsigned char *)(key), (unsigned char *)rcon);
|
403 |
|
|
for (j = 0; j < 16; j++)
|
404 |
|
|
*(expandedkey + 16 * (i + 1) + j) = *(key + j);
|
405 |
|
|
}
|
406 |
|
|
return expandedkey;
|
407 |
|
|
}
|
408 |
|
|
/*----------------------------------------------------------------------------*/
|
409 |
|
|
void
|
410 |
|
|
encrypt_aes(unsigned char *block, unsigned char *key)
|
411 |
|
|
{
|
412 |
|
|
int i = 0;
|
413 |
|
|
|
414 |
|
|
switchblock(block);
|
415 |
|
|
switchblock(key);
|
416 |
|
|
|
417 |
|
|
|
418 |
|
|
unsigned char *expandedkey;
|
419 |
|
|
expandedkey = KeyExpand((unsigned char *)key);
|
420 |
|
|
|
421 |
|
|
AddRoundKey((unsigned char *)block, (unsigned char *)expandedkey);
|
422 |
|
|
|
423 |
|
|
|
424 |
|
|
|
425 |
|
|
for (i = 0; i < 10; i++)
|
426 |
|
|
{
|
427 |
|
|
SubBytes((unsigned char *)block);
|
428 |
|
|
ShiftRows((unsigned char *)block);
|
429 |
|
|
if (i != 9)
|
430 |
|
|
MixColum((unsigned char *)block);
|
431 |
|
|
AddRoundKey((unsigned char *)block, (unsigned char *)(expandedkey + 16 * (i + 1)));
|
432 |
|
|
}
|
433 |
|
|
switchblock(block);
|
434 |
|
|
}
|
435 |
|
|
/*----------------------------------------------------------------------------*/
|
436 |
|
|
void
|
437 |
|
|
decrypt_aes(unsigned char *block, unsigned char *key)
|
438 |
|
|
{
|
439 |
|
|
int i = 0;
|
440 |
|
|
|
441 |
|
|
switchblock(block);
|
442 |
|
|
switchblock(key);
|
443 |
|
|
|
444 |
|
|
unsigned char *expandedkey;
|
445 |
|
|
expandedkey = KeyExpand((unsigned char *)key);
|
446 |
|
|
|
447 |
|
|
for (i = 10; i > 0; i--)
|
448 |
|
|
{
|
449 |
|
|
AddRoundKey((unsigned char *)block, (unsigned char *)(expandedkey + 16 * i));
|
450 |
|
|
if (i != 10)
|
451 |
|
|
InvMixColum((unsigned char *)block);
|
452 |
|
|
InvShiftRows((unsigned char *)block);
|
453 |
|
|
InvSubBytes((unsigned char *)block);
|
454 |
|
|
}
|
455 |
|
|
AddRoundKey((unsigned char *)block, (unsigned char *)expandedkey);
|
456 |
|
|
switchblock(block);
|
457 |
|
|
}
|
458 |
|
|
/*----------------------------------------------------------------------------*/
|