document usage of 'transfer' configuration option for ISDN PRI switch-side transfers
[asterisk-bristuff.git] / main / aeskey.c
blobd34badc6be35b07265502da13a38eb930a6f9834
1 /*
2 ---------------------------------------------------------------------------
3 Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
4 All rights reserved.
6 LICENSE TERMS
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.
25 DISCLAIMER
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
35 /*! \file
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>
44 #include "aesopt.h"
46 #if defined(__cplusplus)
47 extern "C"
49 #endif
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:
60 nk = 4 5 6 7 8
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
69 #define ke4(k,i) \
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]; \
73 #define kel4(k,i) \
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]; \
78 #define ke6(k,i) \
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]; \
83 #define kel6(k,i) \
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]; \
88 #define ke8(k,i) \
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]; \
94 #define kel8(k,i) \
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])
104 { aes_32t ss[4];
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
112 { aes_32t i;
114 for(i = 0; i < ((11 * N_COLS - 1) / 4); ++i)
115 ke4(cx->ks, i);
117 #else
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);
123 #endif
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;
128 cx->ks[52] = 10;
129 #ifdef AES_ERR_CHK
130 return aes_good;
131 #endif
134 #endif
136 #if defined(AES_192) || defined(AES_VAR)
138 aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1])
139 { aes_32t ss[6];
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
149 { aes_32t i;
151 for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)
152 ke6(cx->ks, i);
154 #else
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);
159 #endif
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];
164 cx->ks[52] = 12;
165 #ifdef AES_ERR_CHK
166 return aes_good;
167 #endif
170 #endif
172 #if defined(AES_256) || defined(AES_VAR)
174 aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1])
175 { aes_32t ss[8];
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
187 { aes_32t i;
189 for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)
190 ke8(cx->ks, i);
192 #else
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);
196 kel8(cx->ks, 6);
197 #endif
198 #ifdef AES_ERR_CHK
199 return aes_good;
200 #endif
203 #endif
205 #if defined(AES_VAR)
207 aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1])
209 switch(key_len)
211 #ifdef AES_ERR_CHK
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;
216 #else
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;
220 #endif
224 #endif
226 #endif
228 #if defined(DECRYPTION_KEY_SCHEDULE)
230 #if DEC_ROUND == NO_TABLES
231 #define ff(x) (x)
232 #else
233 #define ff(x) inv_mcol(x)
234 #ifdef dec_imvars
235 #define d_vars dec_imvars
236 #endif
237 #endif
239 #if 1
240 #define kdf4(k,i) \
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]); \
246 #define kd4(k,i) \
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]; \
251 #define kdl4(k,i) \
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]; \
256 #else
257 #define kdf4(k,i) \
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]); \
261 #define kd4(k,i) \
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]; \
268 #define kdl4(k,i) \
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]; \
272 #endif
274 #define kdf6(k,i) \
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]); \
279 #define kd6(k,i) \
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]; \
288 #define kdl6(k,i) \
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]; \
293 #define kdf8(k,i) \
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]); \
299 #define kd8(k,i) \
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]; \
311 #define kdl8(k,i) \
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])
319 { aes_32t ss[5];
320 #ifdef d_vars
321 d_vars;
322 #endif
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
329 { aes_32t i;
331 for(i = 0; i < (11 * N_COLS - 1) / 4; ++i)
332 ke4(cx->ks, 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]);
336 #endif
338 #else
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);
344 #endif
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;
349 cx->ks[52] = 10;
350 #ifdef AES_ERR_CHK
351 return aes_good;
352 #endif
355 #endif
357 #if defined(AES_192) || defined(AES_VAR)
359 aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1])
360 { aes_32t ss[7];
361 #ifdef d_vars
362 d_vars;
363 #endif
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);
372 { aes_32t i;
374 for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)
375 ke6(cx->ks, 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]);
379 #endif
381 #else
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);
388 #endif
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];
393 cx->ks[52] = 12;
394 #ifdef AES_ERR_CHK
395 return aes_good;
396 #endif
399 #endif
401 #if defined(AES_256) || defined(AES_VAR)
403 aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1])
404 { aes_32t ss[8];
405 #ifdef d_vars
406 d_vars;
407 #endif
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);
418 { aes_32t i;
420 for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)
421 ke8(cx->ks, 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]);
425 #endif
427 #else
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);
435 kdl8(cx->ks, 6);
436 #endif
437 #ifdef AES_ERR_CHK
438 return aes_good;
439 #endif
442 #endif
444 #if defined(AES_VAR)
446 aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1])
448 switch(key_len)
450 #ifdef AES_ERR_CHK
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;
455 #else
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;
459 #endif
463 #endif
465 #endif
467 #if defined(__cplusplus)
469 #endif