2 * ---------------------------------------------------------------------------
3 * Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.
7 * The free distribution and use of this software is allowed (with or without
8 * changes) provided that:
10 * 1. source code distributions include the above copyright notice, this
11 * list of conditions and the following disclaimer;
13 * 2. binary distributions include the above copyright notice, this list
14 * of conditions and the following disclaimer in their documentation;
16 * 3. the name of the copyright holder is not used to endorse products
17 * built using this software without specific written permission.
21 * This software is provided 'as is' with no explicit or implied warranties
22 * in respect of its properties, including, but not limited to, correctness
23 * and/or fitness for purpose.
24 * ---------------------------------------------------------------------------
25 * Issue Date: 20/12/2007
34 * Initialise the key schedule from the user supplied key. The key
35 * length can be specified in bytes, with legal values of 16, 24
36 * and 32, or in bits, with legal values of 128, 192 and 256. These
37 * values correspond with Nk values of 4, 6 and 8 respectively.
39 * The following macros implement a single cycle in the key
40 * schedule generation process. The number of cycles needed
41 * for each cx->n_col and nk value is:
44 * ------------------------------
45 * cx->n_col = 4 10 9 8 7 7
46 * cx->n_col = 5 14 11 10 9 9
47 * cx->n_col = 6 19 15 12 11 11
48 * cx->n_col = 7 21 19 16 13 14
49 * cx->n_col = 8 29 23 19 17 14
54 * 1. Added header files aes_impl.h and aestab2.h
55 * 2. Changed uint_8t and uint_32t to uint8_t and uint32_t
56 * 3. Remove code under ifdef USE_VIA_ACE_IF_PRESENT (always undefined)
57 * 4. Removed always-defined ifdefs FUNCS_IN_C, ENC_KEYING_IN_C,
58 * AES_128, AES_192, AES_256, AES_VAR defines
59 * 5. Changed aes_encrypt_key* aes_decrypt_key* functions to "static void"
60 * 6. Changed N_COLS to MAX_AES_NB
61 * 7. Replaced functions aes_encrypt_key and aes_decrypt_key with
62 * OpenSolaris-compatible functions rijndael_key_setup_enc_amd64 and
63 * rijndael_key_setup_dec_amd64
64 * 8. cstyled code and removed lint warnings
67 #if defined(REDUCE_CODE_SIZE)
69 uint32_t ls_sub(const uint32_t t
, const uint32_t n
);
70 #define inv_mcol im_sub
71 uint32_t im_sub(const uint32_t x
);
78 #endif /* REDUCE_CODE_SIZE */
82 { k[4 * (i) + 4] = ss[0] ^= ls_box(ss[3], 3) ^ t_use(r, c)[i]; \
83 k[4 * (i) + 5] = ss[1] ^= ss[0]; \
84 k[4 * (i) + 6] = ss[2] ^= ss[1]; \
85 k[4 * (i) + 7] = ss[3] ^= ss[2]; \
89 aes_encrypt_key128(const unsigned char *key
, uint32_t rk
[])
93 rk
[0] = ss
[0] = word_in(key
, 0);
94 rk
[1] = ss
[1] = word_in(key
, 1);
95 rk
[2] = ss
[2] = word_in(key
, 2);
96 rk
[3] = ss
[3] = word_in(key
, 3);
99 ke4(rk
, 0); ke4(rk
, 1);
100 ke4(rk
, 2); ke4(rk
, 3);
101 ke4(rk
, 4); ke4(rk
, 5);
102 ke4(rk
, 6); ke4(rk
, 7);
107 for (i
= 0; i
< 9; ++i
)
110 #endif /* ENC_KS_UNROLL */
116 { k[6 * (i) + 6] = ss[0] ^= ls_box(ss[5], 3) ^ t_use(r, c)[i]; \
117 k[6 * (i) + 7] = ss[1] ^= ss[0]; \
118 k[6 * (i) + 8] = ss[2] ^= ss[1]; \
119 k[6 * (i) + 9] = ss[3] ^= ss[2]; \
124 k[6 * (i) + 10] = ss[4] ^= ss[3]; \
125 k[6 * (i) + 11] = ss[5] ^= ss[4]; \
129 aes_encrypt_key192(const unsigned char *key
, uint32_t rk
[])
133 rk
[0] = ss
[0] = word_in(key
, 0);
134 rk
[1] = ss
[1] = word_in(key
, 1);
135 rk
[2] = ss
[2] = word_in(key
, 2);
136 rk
[3] = ss
[3] = word_in(key
, 3);
137 rk
[4] = ss
[4] = word_in(key
, 4);
138 rk
[5] = ss
[5] = word_in(key
, 5);
141 ke6(rk
, 0); ke6(rk
, 1);
142 ke6(rk
, 2); ke6(rk
, 3);
143 ke6(rk
, 4); ke6(rk
, 5);
148 for (i
= 0; i
< 7; ++i
)
151 #endif /* ENC_KS_UNROLL */
158 { k[8 * (i) + 8] = ss[0] ^= ls_box(ss[7], 3) ^ t_use(r, c)[i]; \
159 k[8 * (i) + 9] = ss[1] ^= ss[0]; \
160 k[8 * (i) + 10] = ss[2] ^= ss[1]; \
161 k[8 * (i) + 11] = ss[3] ^= ss[2]; \
166 k[8 * (i) + 12] = ss[4] ^= ls_box(ss[3], 0); \
167 k[8 * (i) + 13] = ss[5] ^= ss[4]; \
168 k[8 * (i) + 14] = ss[6] ^= ss[5]; \
169 k[8 * (i) + 15] = ss[7] ^= ss[6]; \
173 aes_encrypt_key256(const unsigned char *key
, uint32_t rk
[])
177 rk
[0] = ss
[0] = word_in(key
, 0);
178 rk
[1] = ss
[1] = word_in(key
, 1);
179 rk
[2] = ss
[2] = word_in(key
, 2);
180 rk
[3] = ss
[3] = word_in(key
, 3);
181 rk
[4] = ss
[4] = word_in(key
, 4);
182 rk
[5] = ss
[5] = word_in(key
, 5);
183 rk
[6] = ss
[6] = word_in(key
, 6);
184 rk
[7] = ss
[7] = word_in(key
, 7);
187 ke8(rk
, 0); ke8(rk
, 1);
188 ke8(rk
, 2); ke8(rk
, 3);
189 ke8(rk
, 4); ke8(rk
, 5);
193 for (i
= 0; i
< 6; ++i
)
196 #endif /* ENC_KS_UNROLL */
202 * Expand the cipher key into the encryption key schedule.
204 * Return the number of rounds for the given cipher key size.
205 * The size of the key schedule depends on the number of rounds
206 * (which can be computed from the size of the key), i.e. 4 * (Nr + 1).
209 * rk AES key schedule 32-bit array to be initialized
211 * keyBits AES key size (128, 192, or 256 bits)
214 rijndael_key_setup_enc_amd64(uint32_t rk
[], const uint32_t cipherKey
[],
219 aes_encrypt_key128((unsigned char *)&cipherKey
[0], rk
);
222 aes_encrypt_key192((unsigned char *)&cipherKey
[0], rk
);
225 aes_encrypt_key256((unsigned char *)&cipherKey
[0], rk
);
227 default: /* should never get here */
235 /* this is used to store the decryption round keys */
236 /* in forward or reverse order */
239 #define v(n, i) ((n) - (i) + 2 * ((i) & 3))
244 #if DEC_ROUND == NO_TABLES
247 #define ff(x) inv_mcol(x)
248 #if defined(dec_imvars)
249 #define d_vars dec_imvars
251 #endif /* FUNCS_IN_C & DEC_KEYING_IN_C */
255 { k[v(40, (4 * (i)) + 4)] = ss[0] ^= ls_box(ss[3], 3) ^ t_use(r, c)[i]; \
256 k[v(40, (4 * (i)) + 5)] = ss[1] ^= ss[0]; \
257 k[v(40, (4 * (i)) + 6)] = ss[2] ^= ss[1]; \
258 k[v(40, (4 * (i)) + 7)] = ss[3] ^= ss[2]; \
264 { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
265 ss[1] = ss[1] ^ ss[3]; \
266 ss[2] = ss[2] ^ ss[3]; \
267 ss[4] = ls_box(ss[(i + 3) % 4], 3) ^ t_use(r, c)[i]; \
268 ss[i % 4] ^= ss[4]; \
269 ss[4] ^= k[v(40, (4 * (i)))]; k[v(40, (4 * (i)) + 4)] = ff(ss[4]); \
270 ss[4] ^= k[v(40, (4 * (i)) + 1)]; k[v(40, (4 * (i)) + 5)] = ff(ss[4]); \
271 ss[4] ^= k[v(40, (4 * (i)) + 2)]; k[v(40, (4 * (i)) + 6)] = ff(ss[4]); \
272 ss[4] ^= k[v(40, (4 * (i)) + 3)]; k[v(40, (4 * (i)) + 7)] = ff(ss[4]); \
276 { ss[4] = ls_box(ss[(i + 3) % 4], 3) ^ t_use(r, c)[i]; \
277 ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \
278 k[v(40, (4 * (i)) + 4)] = ss[4] ^= k[v(40, (4 * (i)))]; \
279 k[v(40, (4 * (i)) + 5)] = ss[4] ^= k[v(40, (4 * (i)) + 1)]; \
280 k[v(40, (4 * (i)) + 6)] = ss[4] ^= k[v(40, (4 * (i)) + 2)]; \
281 k[v(40, (4 * (i)) + 7)] = ss[4] ^= k[v(40, (4 * (i)) + 3)]; \
285 { ss[4] = ls_box(ss[(i + 3) % 4], 3) ^ t_use(r, c)[i]; \
286 ss[i % 4] ^= ss[4]; \
287 k[v(40, (4 * (i)) + 4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
288 k[v(40, (4 * (i)) + 5)] = ss[1] ^ ss[3]; \
289 k[v(40, (4 * (i)) + 6)] = ss[0]; \
290 k[v(40, (4 * (i)) + 7)] = ss[1]; \
296 { ss[0] ^= ls_box(ss[3], 3) ^ t_use(r, c)[i]; \
297 k[v(40, (4 * (i)) + 4)] = ff(ss[0]); \
298 ss[1] ^= ss[0]; k[v(40, (4 * (i)) + 5)] = ff(ss[1]); \
299 ss[2] ^= ss[1]; k[v(40, (4 * (i)) + 6)] = ff(ss[2]); \
300 ss[3] ^= ss[2]; k[v(40, (4 * (i)) + 7)] = ff(ss[3]); \
304 { ss[4] = ls_box(ss[3], 3) ^ t_use(r, c)[i]; \
307 k[v(40, (4 * (i)) + 4)] = ss[4] ^= k[v(40, (4 * (i)))]; \
309 k[v(40, (4 * (i)) + 5)] = ss[4] ^= k[v(40, (4 * (i)) + 1)]; \
311 k[v(40, (4 * (i)) + 6)] = ss[4] ^= k[v(40, (4 * (i)) + 2)]; \
313 k[v(40, (4 * (i)) + 7)] = ss[4] ^= k[v(40, (4 * (i)) + 3)]; \
317 { ss[0] ^= ls_box(ss[3], 3) ^ t_use(r, c)[i]; \
318 k[v(40, (4 * (i)) + 4)] = ss[0]; \
319 ss[1] ^= ss[0]; k[v(40, (4 * (i)) + 5)] = ss[1]; \
320 ss[2] ^= ss[1]; k[v(40, (4 * (i)) + 6)] = ss[2]; \
321 ss[3] ^= ss[2]; k[v(40, (4 * (i)) + 7)] = ss[3]; \
327 aes_decrypt_key128(const unsigned char *key
, uint32_t rk
[])
333 rk
[v(40, (0))] = ss
[0] = word_in(key
, 0);
334 rk
[v(40, (1))] = ss
[1] = word_in(key
, 1);
335 rk
[v(40, (2))] = ss
[2] = word_in(key
, 2);
336 rk
[v(40, (3))] = ss
[3] = word_in(key
, 3);
339 kdf4(rk
, 0); kd4(rk
, 1);
340 kd4(rk
, 2); kd4(rk
, 3);
341 kd4(rk
, 4); kd4(rk
, 5);
342 kd4(rk
, 6); kd4(rk
, 7);
343 kd4(rk
, 8); kdl4(rk
, 9);
347 for (i
= 0; i
< 10; ++i
)
349 #if !(DEC_ROUND == NO_TABLES)
350 for (i
= MAX_AES_NB
; i
< 10 * MAX_AES_NB
; ++i
)
351 rk
[i
] = inv_mcol(rk
[i
]);
354 #endif /* DEC_KS_UNROLL */
360 { k[v(48, (6 * (i)) + 6)] = ss[0] ^= ls_box(ss[5], 3) ^ t_use(r, c)[i]; \
361 k[v(48, (6 * (i)) + 7)] = ss[1] ^= ss[0]; \
362 k[v(48, (6 * (i)) + 8)] = ss[2] ^= ss[1]; \
363 k[v(48, (6 * (i)) + 9)] = ss[3] ^= ss[2]; \
368 k[v(48, (6 * (i)) + 10)] = ss[4] ^= ss[3]; \
369 k[v(48, (6 * (i)) + 11)] = ss[5] ^= ss[4]; \
373 { ss[0] ^= ls_box(ss[5], 3) ^ t_use(r, c)[i]; \
374 k[v(48, (6 * (i)) + 6)] = ff(ss[0]); \
375 ss[1] ^= ss[0]; k[v(48, (6 * (i)) + 7)] = ff(ss[1]); \
376 ss[2] ^= ss[1]; k[v(48, (6 * (i)) + 8)] = ff(ss[2]); \
377 ss[3] ^= ss[2]; k[v(48, (6 * (i)) + 9)] = ff(ss[3]); \
378 ss[4] ^= ss[3]; k[v(48, (6 * (i)) + 10)] = ff(ss[4]); \
379 ss[5] ^= ss[4]; k[v(48, (6 * (i)) + 11)] = ff(ss[5]); \
383 { ss[6] = ls_box(ss[5], 3) ^ t_use(r, c)[i]; \
384 ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
385 k[v(48, (6 * (i)) + 6)] = ss[6] ^= k[v(48, (6 * (i)))]; \
387 k[v(48, (6 * (i)) + 7)] = ss[6] ^= k[v(48, (6 * (i)) + 1)]; \
389 k[v(48, (6 * (i)) + 8)] = ss[6] ^= k[v(48, (6 * (i)) + 2)]; \
391 k[v(48, (6 * (i)) + 9)] = ss[6] ^= k[v(48, (6 * (i)) + 3)]; \
393 k[v(48, (6 * (i)) + 10)] = ss[6] ^= k[v(48, (6 * (i)) + 4)]; \
395 k[v(48, (6 * (i)) + 11)] = ss[6] ^= k[v(48, (6 * (i)) + 5)]; \
399 { ss[0] ^= ls_box(ss[5], 3) ^ t_use(r, c)[i]; \
400 k[v(48, (6 * (i)) + 6)] = ss[0]; \
401 ss[1] ^= ss[0]; k[v(48, (6 * (i)) + 7)] = ss[1]; \
402 ss[2] ^= ss[1]; k[v(48, (6 * (i)) + 8)] = ss[2]; \
403 ss[3] ^= ss[2]; k[v(48, (6 * (i)) + 9)] = ss[3]; \
407 aes_decrypt_key192(const unsigned char *key
, uint32_t rk
[])
413 rk
[v(48, (0))] = ss
[0] = word_in(key
, 0);
414 rk
[v(48, (1))] = ss
[1] = word_in(key
, 1);
415 rk
[v(48, (2))] = ss
[2] = word_in(key
, 2);
416 rk
[v(48, (3))] = ss
[3] = word_in(key
, 3);
419 ss
[4] = word_in(key
, 4);
420 rk
[v(48, (4))] = ff(ss
[4]);
421 ss
[5] = word_in(key
, 5);
422 rk
[v(48, (5))] = ff(ss
[5]);
423 kdf6(rk
, 0); kd6(rk
, 1);
424 kd6(rk
, 2); kd6(rk
, 3);
425 kd6(rk
, 4); kd6(rk
, 5);
426 kd6(rk
, 6); kdl6(rk
, 7);
428 rk
[v(48, (4))] = ss
[4] = word_in(key
, 4);
429 rk
[v(48, (5))] = ss
[5] = word_in(key
, 5);
433 for (i
= 0; i
< 7; ++i
)
436 #if !(DEC_ROUND == NO_TABLES)
437 for (i
= MAX_AES_NB
; i
< 12 * MAX_AES_NB
; ++i
)
438 rk
[i
] = inv_mcol(rk
[i
]);
447 { k[v(56, (8 * (i)) + 8)] = ss[0] ^= ls_box(ss[7], 3) ^ t_use(r, c)[i]; \
448 k[v(56, (8 * (i)) + 9)] = ss[1] ^= ss[0]; \
449 k[v(56, (8 * (i)) + 10)] = ss[2] ^= ss[1]; \
450 k[v(56, (8 * (i)) + 11)] = ss[3] ^= ss[2]; \
455 k[v(56, (8 * (i)) + 12)] = ss[4] ^= ls_box(ss[3], 0); \
456 k[v(56, (8 * (i)) + 13)] = ss[5] ^= ss[4]; \
457 k[v(56, (8 * (i)) + 14)] = ss[6] ^= ss[5]; \
458 k[v(56, (8 * (i)) + 15)] = ss[7] ^= ss[6]; \
462 { ss[0] ^= ls_box(ss[7], 3) ^ t_use(r, c)[i]; \
463 k[v(56, (8 * (i)) + 8)] = ff(ss[0]); \
464 ss[1] ^= ss[0]; k[v(56, (8 * (i)) + 9)] = ff(ss[1]); \
465 ss[2] ^= ss[1]; k[v(56, (8 * (i)) + 10)] = ff(ss[2]); \
466 ss[3] ^= ss[2]; k[v(56, (8 * (i)) + 11)] = ff(ss[3]); \
467 ss[4] ^= ls_box(ss[3], 0); k[v(56, (8 * (i)) + 12)] = ff(ss[4]); \
468 ss[5] ^= ss[4]; k[v(56, (8 * (i)) + 13)] = ff(ss[5]); \
469 ss[6] ^= ss[5]; k[v(56, (8 * (i)) + 14)] = ff(ss[6]); \
470 ss[7] ^= ss[6]; k[v(56, (8 * (i)) + 15)] = ff(ss[7]); \
474 { ss[8] = ls_box(ss[7], 3) ^ t_use(r, c)[i]; \
477 k[v(56, (8 * (i)) + 8)] = ss[8] ^= k[v(56, (8 * (i)))]; \
479 k[v(56, (8 * (i)) + 9)] = ss[8] ^= k[v(56, (8 * (i)) + 1)]; \
481 k[v(56, (8 * (i)) + 10)] = ss[8] ^= k[v(56, (8 * (i)) + 2)]; \
483 k[v(56, (8 * (i)) + 11)] = ss[8] ^= k[v(56, (8 * (i)) + 3)]; \
484 ss[8] = ls_box(ss[3], 0); \
487 k[v(56, (8 * (i)) + 12)] = ss[8] ^= k[v(56, (8 * (i)) + 4)]; \
489 k[v(56, (8 * (i)) + 13)] = ss[8] ^= k[v(56, (8 * (i)) + 5)]; \
491 k[v(56, (8 * (i)) + 14)] = ss[8] ^= k[v(56, (8 * (i)) + 6)]; \
493 k[v(56, (8 * (i)) + 15)] = ss[8] ^= k[v(56, (8 * (i)) + 7)]; \
497 { ss[0] ^= ls_box(ss[7], 3) ^ t_use(r, c)[i]; \
498 k[v(56, (8 * (i)) + 8)] = ss[0]; \
499 ss[1] ^= ss[0]; k[v(56, (8 * (i)) + 9)] = ss[1]; \
500 ss[2] ^= ss[1]; k[v(56, (8 * (i)) + 10)] = ss[2]; \
501 ss[3] ^= ss[2]; k[v(56, (8 * (i)) + 11)] = ss[3]; \
505 aes_decrypt_key256(const unsigned char *key
, uint32_t rk
[])
511 rk
[v(56, (0))] = ss
[0] = word_in(key
, 0);
512 rk
[v(56, (1))] = ss
[1] = word_in(key
, 1);
513 rk
[v(56, (2))] = ss
[2] = word_in(key
, 2);
514 rk
[v(56, (3))] = ss
[3] = word_in(key
, 3);
517 ss
[4] = word_in(key
, 4);
518 rk
[v(56, (4))] = ff(ss
[4]);
519 ss
[5] = word_in(key
, 5);
520 rk
[v(56, (5))] = ff(ss
[5]);
521 ss
[6] = word_in(key
, 6);
522 rk
[v(56, (6))] = ff(ss
[6]);
523 ss
[7] = word_in(key
, 7);
524 rk
[v(56, (7))] = ff(ss
[7]);
525 kdf8(rk
, 0); kd8(rk
, 1);
526 kd8(rk
, 2); kd8(rk
, 3);
527 kd8(rk
, 4); kd8(rk
, 5);
530 rk
[v(56, (4))] = ss
[4] = word_in(key
, 4);
531 rk
[v(56, (5))] = ss
[5] = word_in(key
, 5);
532 rk
[v(56, (6))] = ss
[6] = word_in(key
, 6);
533 rk
[v(56, (7))] = ss
[7] = word_in(key
, 7);
537 for (i
= 0; i
< 6; ++i
)
540 #if !(DEC_ROUND == NO_TABLES)
541 for (i
= MAX_AES_NB
; i
< 14 * MAX_AES_NB
; ++i
)
542 rk
[i
] = inv_mcol(rk
[i
]);
545 #endif /* DEC_KS_UNROLL */
550 * Expand the cipher key into the decryption key schedule.
552 * Return the number of rounds for the given cipher key size.
553 * The size of the key schedule depends on the number of rounds
554 * (which can be computed from the size of the key), i.e. 4 * (Nr + 1).
557 * rk AES key schedule 32-bit array to be initialized
559 * keyBits AES key size (128, 192, or 256 bits)
562 rijndael_key_setup_dec_amd64(uint32_t rk
[], const uint32_t cipherKey
[],
567 aes_decrypt_key128((unsigned char *)&cipherKey
[0], rk
);
570 aes_decrypt_key192((unsigned char *)&cipherKey
[0], rk
);
573 aes_decrypt_key256((unsigned char *)&cipherKey
[0], rk
);
575 default: /* should never get here */