2 ---------------------------------------------------------------------------
3 Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 ---------------------------------------------------------------------------
31 Issue Date: 26/08/2003
37 * \brief This file contains the code for implementing the key schedule for AES
38 * (Rijndael) for block and key sizes of 16, 24, and 32 bytes. See aesopt.h
39 * for further details including optimisation.
41 * \author Dr Brian Gladman <brg@gladman.me.uk>
46 #if defined(__cplusplus)
51 /* Initialise the key schedule from the user supplied key. The key
52 length can be specified in bytes, with legal values of 16, 24
53 and 32, or in bits, with legal values of 128, 192 and 256. These
54 values correspond with Nk values of 4, 6 and 8 respectively.
56 The following macros implement a single cycle in the key
57 schedule generation process. The number of cycles needed
58 for each cx->n_col and nk value is:
61 ------------------------------
62 cx->n_col = 4 10 9 8 7 7
63 cx->n_col = 5 14 11 10 9 9
64 cx->n_col = 6 19 15 12 11 11
65 cx->n_col = 7 21 19 16 13 14
66 cx->n_col = 8 29 23 19 17 14
70 { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \
71 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
74 { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \
75 k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
79 { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \
80 k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \
81 k[6*(i)+10] = ss[4] ^= ss[3]; k[6*(i)+11] = ss[5] ^= ss[4]; \
84 { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \
85 k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \
89 { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \
90 k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \
91 k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); k[8*(i)+13] = ss[5] ^= ss[4]; \
92 k[8*(i)+14] = ss[6] ^= ss[5]; k[8*(i)+15] = ss[7] ^= ss[6]; \
95 { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \
96 k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \
99 #if defined(ENCRYPTION_KEY_SCHEDULE)
101 #if defined(AES_128) || defined(AES_VAR)
103 aes_rval
aes_encrypt_key128(const void *in_key
, aes_encrypt_ctx cx
[1])
106 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
107 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
108 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
109 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
111 #if ENC_UNROLL == NONE
114 for(i
= 0; i
< ((11 * N_COLS
- 1) / 4); ++i
)
118 ke4(cx
->ks
, 0); ke4(cx
->ks
, 1);
119 ke4(cx
->ks
, 2); ke4(cx
->ks
, 3);
120 ke4(cx
->ks
, 4); ke4(cx
->ks
, 5);
121 ke4(cx
->ks
, 6); ke4(cx
->ks
, 7);
122 ke4(cx
->ks
, 8); kel4(cx
->ks
, 9);
125 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
126 /* key and must be non-zero for 128 and 192 bits keys */
127 cx
->ks
[53] = cx
->ks
[45] = 0;
136 #if defined(AES_192) || defined(AES_VAR)
138 aes_rval
aes_encrypt_key192(const void *in_key
, aes_encrypt_ctx cx
[1])
141 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
142 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
143 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
144 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
145 cx
->ks
[4] = ss
[4] = word_in(in_key
, 4);
146 cx
->ks
[5] = ss
[5] = word_in(in_key
, 5);
148 #if ENC_UNROLL == NONE
151 for(i
= 0; i
< (13 * N_COLS
- 1) / 6; ++i
)
155 ke6(cx
->ks
, 0); ke6(cx
->ks
, 1);
156 ke6(cx
->ks
, 2); ke6(cx
->ks
, 3);
157 ke6(cx
->ks
, 4); ke6(cx
->ks
, 5);
158 ke6(cx
->ks
, 6); kel6(cx
->ks
, 7);
161 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
162 /* key and must be non-zero for 128 and 192 bits keys */
163 cx
->ks
[53] = cx
->ks
[45];
172 #if defined(AES_256) || defined(AES_VAR)
174 aes_rval
aes_encrypt_key256(const void *in_key
, aes_encrypt_ctx cx
[1])
177 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
178 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
179 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
180 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
181 cx
->ks
[4] = ss
[4] = word_in(in_key
, 4);
182 cx
->ks
[5] = ss
[5] = word_in(in_key
, 5);
183 cx
->ks
[6] = ss
[6] = word_in(in_key
, 6);
184 cx
->ks
[7] = ss
[7] = word_in(in_key
, 7);
186 #if ENC_UNROLL == NONE
189 for(i
= 0; i
< (15 * N_COLS
- 1) / 8; ++i
)
193 ke8(cx
->ks
, 0); ke8(cx
->ks
, 1);
194 ke8(cx
->ks
, 2); ke8(cx
->ks
, 3);
195 ke8(cx
->ks
, 4); ke8(cx
->ks
, 5);
207 aes_rval
aes_encrypt_key(const void *in_key
, int key_len
, aes_encrypt_ctx cx
[1])
212 case 16: case 128: return aes_encrypt_key128(in_key
, cx
);
213 case 24: case 192: return aes_encrypt_key192(in_key
, cx
);
214 case 32: case 256: return aes_encrypt_key256(in_key
, cx
);
215 default: return aes_error
;
217 case 16: case 128: aes_encrypt_key128(in_key
, cx
); return;
218 case 24: case 192: aes_encrypt_key192(in_key
, cx
); return;
219 case 32: case 256: aes_encrypt_key256(in_key
, cx
); return;
228 #if defined(DECRYPTION_KEY_SCHEDULE)
230 #if DEC_ROUND == NO_TABLES
233 #define ff(x) inv_mcol(x)
235 #define d_vars dec_imvars
241 { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; ss[1] = ss[1] ^ ss[3]; ss[2] = ss[2] ^ ss[3]; ss[3] = ss[3]; \
242 ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
243 ss[4] ^= k[4*(i)]; k[4*(i)+4] = ff(ss[4]); ss[4] ^= k[4*(i)+1]; k[4*(i)+5] = ff(ss[4]); \
244 ss[4] ^= k[4*(i)+2]; k[4*(i)+6] = ff(ss[4]); ss[4] ^= k[4*(i)+3]; k[4*(i)+7] = ff(ss[4]); \
247 { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \
248 k[4*(i)+4] = ss[4] ^= k[4*(i)]; k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
249 k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
252 { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
253 k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; k[4*(i)+5] = ss[1] ^ ss[3]; \
254 k[4*(i)+6] = ss[0]; k[4*(i)+7] = ss[1]; \
258 { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ff(ss[0]); ss[1] ^= ss[0]; k[4*(i)+ 5] = ff(ss[1]); \
259 ss[2] ^= ss[1]; k[4*(i)+ 6] = ff(ss[2]); ss[3] ^= ss[2]; k[4*(i)+ 7] = ff(ss[3]); \
262 { ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \
263 ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[4*(i)+ 4] = ss[4] ^= k[4*(i)]; \
264 ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[4] ^= k[4*(i)+ 1]; \
265 ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[4] ^= k[4*(i)+ 2]; \
266 ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[4] ^= k[4*(i)+ 3]; \
269 { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ss[0]; ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[1]; \
270 ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[2]; ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[3]; \
275 { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ff(ss[0]); ss[1] ^= ss[0]; k[6*(i)+ 7] = ff(ss[1]); \
276 ss[2] ^= ss[1]; k[6*(i)+ 8] = ff(ss[2]); ss[3] ^= ss[2]; k[6*(i)+ 9] = ff(ss[3]); \
277 ss[4] ^= ss[3]; k[6*(i)+10] = ff(ss[4]); ss[5] ^= ss[4]; k[6*(i)+11] = ff(ss[5]); \
280 { ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \
281 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
282 ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
283 ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
284 ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
285 ss[4] ^= ss[3]; k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
286 ss[5] ^= ss[4]; k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
289 { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ss[0]; ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[1]; \
290 ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[2]; ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[3]; \
294 { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ff(ss[0]); ss[1] ^= ss[0]; k[8*(i)+ 9] = ff(ss[1]); \
295 ss[2] ^= ss[1]; k[8*(i)+10] = ff(ss[2]); ss[3] ^= ss[2]; k[8*(i)+11] = ff(ss[3]); \
296 ss[4] ^= ls_box(ss[3],0); k[8*(i)+12] = ff(ss[4]); ss[5] ^= ss[4]; k[8*(i)+13] = ff(ss[5]); \
297 ss[6] ^= ss[5]; k[8*(i)+14] = ff(ss[6]); ss[7] ^= ss[6]; k[8*(i)+15] = ff(ss[7]); \
300 { aes_32t g = ls_box(ss[7],3) ^ t_use(r,c)[i]; \
301 ss[0] ^= g; g = ff(g); k[8*(i)+ 8] = g ^= k[8*(i)]; \
302 ss[1] ^= ss[0]; k[8*(i)+ 9] = g ^= k[8*(i)+ 1]; \
303 ss[2] ^= ss[1]; k[8*(i)+10] = g ^= k[8*(i)+ 2]; \
304 ss[3] ^= ss[2]; k[8*(i)+11] = g ^= k[8*(i)+ 3]; \
305 g = ls_box(ss[3],0); \
306 ss[4] ^= g; g = ff(g); k[8*(i)+12] = g ^= k[8*(i)+ 4]; \
307 ss[5] ^= ss[4]; k[8*(i)+13] = g ^= k[8*(i)+ 5]; \
308 ss[6] ^= ss[5]; k[8*(i)+14] = g ^= k[8*(i)+ 6]; \
309 ss[7] ^= ss[6]; k[8*(i)+15] = g ^= k[8*(i)+ 7]; \
312 { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ss[0]; ss[1] ^= ss[0]; k[8*(i)+ 9] = ss[1]; \
313 ss[2] ^= ss[1]; k[8*(i)+10] = ss[2]; ss[3] ^= ss[2]; k[8*(i)+11] = ss[3]; \
316 #if defined(AES_128) || defined(AES_VAR)
318 aes_rval
aes_decrypt_key128(const void *in_key
, aes_decrypt_ctx cx
[1])
323 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
324 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
325 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
326 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
328 #if DEC_UNROLL == NONE
331 for(i
= 0; i
< (11 * N_COLS
- 1) / 4; ++i
)
333 #if !(DEC_ROUND == NO_TABLES)
334 for(i
= N_COLS
; i
< 10 * N_COLS
; ++i
)
335 cx
->ks
[i
] = inv_mcol(cx
->ks
[i
]);
339 kdf4(cx
->ks
, 0); kd4(cx
->ks
, 1);
340 kd4(cx
->ks
, 2); kd4(cx
->ks
, 3);
341 kd4(cx
->ks
, 4); kd4(cx
->ks
, 5);
342 kd4(cx
->ks
, 6); kd4(cx
->ks
, 7);
343 kd4(cx
->ks
, 8); kdl4(cx
->ks
, 9);
346 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
347 /* key and must be non-zero for 128 and 192 bits keys */
348 cx
->ks
[53] = cx
->ks
[45] = 0;
357 #if defined(AES_192) || defined(AES_VAR)
359 aes_rval
aes_decrypt_key192(const void *in_key
, aes_decrypt_ctx cx
[1])
364 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
365 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
366 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
367 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
369 #if DEC_UNROLL == NONE
370 cx
->ks
[4] = ss
[4] = word_in(in_key
, 4);
371 cx
->ks
[5] = ss
[5] = word_in(in_key
, 5);
374 for(i
= 0; i
< (13 * N_COLS
- 1) / 6; ++i
)
376 #if !(DEC_ROUND == NO_TABLES)
377 for(i
= N_COLS
; i
< 12 * N_COLS
; ++i
)
378 cx
->ks
[i
] = inv_mcol(cx
->ks
[i
]);
382 cx
->ks
[4] = ff(ss
[4] = word_in(in_key
, 4));
383 cx
->ks
[5] = ff(ss
[5] = word_in(in_key
, 5));
384 kdf6(cx
->ks
, 0); kd6(cx
->ks
, 1);
385 kd6(cx
->ks
, 2); kd6(cx
->ks
, 3);
386 kd6(cx
->ks
, 4); kd6(cx
->ks
, 5);
387 kd6(cx
->ks
, 6); kdl6(cx
->ks
, 7);
390 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
391 /* key and must be non-zero for 128 and 192 bits keys */
392 cx
->ks
[53] = cx
->ks
[45];
401 #if defined(AES_256) || defined(AES_VAR)
403 aes_rval
aes_decrypt_key256(const void *in_key
, aes_decrypt_ctx cx
[1])
408 cx
->ks
[0] = ss
[0] = word_in(in_key
, 0);
409 cx
->ks
[1] = ss
[1] = word_in(in_key
, 1);
410 cx
->ks
[2] = ss
[2] = word_in(in_key
, 2);
411 cx
->ks
[3] = ss
[3] = word_in(in_key
, 3);
413 #if DEC_UNROLL == NONE
414 cx
->ks
[4] = ss
[4] = word_in(in_key
, 4);
415 cx
->ks
[5] = ss
[5] = word_in(in_key
, 5);
416 cx
->ks
[6] = ss
[6] = word_in(in_key
, 6);
417 cx
->ks
[7] = ss
[7] = word_in(in_key
, 7);
420 for(i
= 0; i
< (15 * N_COLS
- 1) / 8; ++i
)
422 #if !(DEC_ROUND == NO_TABLES)
423 for(i
= N_COLS
; i
< 14 * N_COLS
; ++i
)
424 cx
->ks
[i
] = inv_mcol(cx
->ks
[i
]);
428 cx
->ks
[4] = ff(ss
[4] = word_in(in_key
, 4));
429 cx
->ks
[5] = ff(ss
[5] = word_in(in_key
, 5));
430 cx
->ks
[6] = ff(ss
[6] = word_in(in_key
, 6));
431 cx
->ks
[7] = ff(ss
[7] = word_in(in_key
, 7));
432 kdf8(cx
->ks
, 0); kd8(cx
->ks
, 1);
433 kd8(cx
->ks
, 2); kd8(cx
->ks
, 3);
434 kd8(cx
->ks
, 4); kd8(cx
->ks
, 5);
446 aes_rval
aes_decrypt_key(const void *in_key
, int key_len
, aes_decrypt_ctx cx
[1])
451 case 16: case 128: return aes_decrypt_key128(in_key
, cx
);
452 case 24: case 192: return aes_decrypt_key192(in_key
, cx
);
453 case 32: case 256: return aes_decrypt_key256(in_key
, cx
);
454 default: return aes_error
;
456 case 16: case 128: aes_decrypt_key128(in_key
, cx
); return;
457 case 24: case 192: aes_decrypt_key192(in_key
, cx
); return;
458 case 32: case 256: aes_decrypt_key256(in_key
, cx
); return;
467 #if defined(__cplusplus)