OSDN Git Service

Fix bugs of initialization of FTPIS.
[ffftp/ffftp.git] / aesopt.h
1 /*\r
2  ---------------------------------------------------------------------------\r
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.\r
4 \r
5  LICENSE TERMS\r
6 \r
7  The redistribution and use of this software (with or without changes)\r
8  is allowed without the payment of fees or royalties provided that:\r
9 \r
10   1. source code distributions include the above copyright notice, this\r
11      list of conditions and the following disclaimer;\r
12 \r
13   2. binary distributions include the above copyright notice, this list\r
14      of conditions and the following disclaimer in their documentation;\r
15 \r
16   3. the name of the copyright holder is not used to endorse products\r
17      built using this software without specific written permission.\r
18 \r
19  DISCLAIMER\r
20 \r
21  This software is provided 'as is' with no explicit or implied warranties\r
22  in respect of its properties, including, but not limited to, correctness\r
23  and/or fitness for purpose.\r
24  ---------------------------------------------------------------------------\r
25  Issue Date: 20/12/2007\r
26 \r
27  This file contains the compilation options for AES (Rijndael) and code\r
28  that is common across encryption, key scheduling and table generation.\r
29 \r
30  OPERATION\r
31 \r
32  These source code files implement the AES algorithm Rijndael designed by\r
33  Joan Daemen and Vincent Rijmen. This version is designed for the standard\r
34  block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24\r
35  and 32 bytes).\r
36 \r
37  This version is designed for flexibility and speed using operations on\r
38  32-bit words rather than operations on bytes.  It can be compiled with\r
39  either big or little endian internal byte order but is faster when the\r
40  native byte order for the processor is used.\r
41 \r
42  THE CIPHER INTERFACE\r
43 \r
44  The cipher interface is implemented as an array of bytes in which lower\r
45  AES bit sequence indexes map to higher numeric significance within bytes.\r
46 \r
47   uint_8t                 (an unsigned  8-bit type)\r
48   uint_32t                (an unsigned 32-bit type)\r
49   struct aes_encrypt_ctx  (structure for the cipher encryption context)\r
50   struct aes_decrypt_ctx  (structure for the cipher decryption context)\r
51   AES_RETURN                the function return type\r
52 \r
53   C subroutine calls:\r
54 \r
55   AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
56   AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
57   AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
58   AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out,\r
59                                                   const aes_encrypt_ctx cx[1]);\r
60 \r
61   AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
62   AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
63   AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
64   AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out,\r
65                                                   const aes_decrypt_ctx cx[1]);\r
66 \r
67  IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that\r
68  you call aes_init() before AES is used so that the tables are initialised.\r
69 \r
70  C++ aes class subroutines:\r
71 \r
72      Class AESencrypt  for encryption\r
73 \r
74       Construtors:\r
75           AESencrypt(void)\r
76           AESencrypt(const unsigned char *key) - 128 bit key\r
77       Members:\r
78           AES_RETURN key128(const unsigned char *key)\r
79           AES_RETURN key192(const unsigned char *key)\r
80           AES_RETURN key256(const unsigned char *key)\r
81           AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const\r
82 \r
83       Class AESdecrypt  for encryption\r
84       Construtors:\r
85           AESdecrypt(void)\r
86           AESdecrypt(const unsigned char *key) - 128 bit key\r
87       Members:\r
88           AES_RETURN key128(const unsigned char *key)\r
89           AES_RETURN key192(const unsigned char *key)\r
90           AES_RETURN key256(const unsigned char *key)\r
91           AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const\r
92 */\r
93 \r
94 #if !defined( _AESOPT_H )\r
95 #define _AESOPT_H\r
96 \r
97 #if defined( __cplusplus )\r
98 #include "aescpp.h"\r
99 #else\r
100 #include "aes.h"\r
101 #endif\r
102 \r
103 /*  PLATFORM SPECIFIC INCLUDES */\r
104 \r
105 #include "brg_endian.h"\r
106 \r
107 /*  CONFIGURATION - THE USE OF DEFINES\r
108 \r
109     Later in this section there are a number of defines that control the\r
110     operation of the code.  In each section, the purpose of each define is\r
111     explained so that the relevant form can be included or excluded by\r
112     setting either 1's or 0's respectively on the branches of the related\r
113     #if clauses.  The following local defines should not be changed.\r
114 */\r
115 \r
116 #define ENCRYPTION_IN_C     1\r
117 #define DECRYPTION_IN_C     2\r
118 #define ENC_KEYING_IN_C     4\r
119 #define DEC_KEYING_IN_C     8\r
120 \r
121 #define NO_TABLES           0\r
122 #define ONE_TABLE           1\r
123 #define FOUR_TABLES         4\r
124 #define NONE                0\r
125 #define PARTIAL             1\r
126 #define FULL                2\r
127 \r
128 /*  --- START OF USER CONFIGURED OPTIONS --- */\r
129 \r
130 /*  1. BYTE ORDER WITHIN 32 BIT WORDS\r
131 \r
132     The fundamental data processing units in Rijndael are 8-bit bytes. The\r
133     input, output and key input are all enumerated arrays of bytes in which\r
134     bytes are numbered starting at zero and increasing to one less than the\r
135     number of bytes in the array in question. This enumeration is only used\r
136     for naming bytes and does not imply any adjacency or order relationship\r
137     from one byte to another. When these inputs and outputs are considered\r
138     as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to\r
139     byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.\r
140     In this implementation bits are numbered from 0 to 7 starting at the\r
141     numerically least significant end of each byte (bit n represents 2^n).\r
142 \r
143     However, Rijndael can be implemented more efficiently using 32-bit\r
144     words by packing bytes into words so that bytes 4*n to 4*n+3 are placed\r
145     into word[n]. While in principle these bytes can be assembled into words\r
146     in any positions, this implementation only supports the two formats in\r
147     which bytes in adjacent positions within words also have adjacent byte\r
148     numbers. This order is called big-endian if the lowest numbered bytes\r
149     in words have the highest numeric significance and little-endian if the\r
150     opposite applies.\r
151 \r
152     This code can work in either order irrespective of the order used by the\r
153     machine on which it runs. Normally the internal byte order will be set\r
154     to the order of the processor on which the code is to be run but this\r
155     define can be used to reverse this in special situations\r
156 \r
157     WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set.\r
158     This define will hence be redefined later (in section 4) if necessary\r
159 */\r
160 \r
161 #if 1\r
162 #  define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER\r
163 #elif 0\r
164 #  define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN\r
165 #elif 0\r
166 #  define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN\r
167 #else\r
168 #  error The algorithm byte order is not defined\r
169 #endif\r
170 \r
171 /*  2. VIA ACE SUPPORT */\r
172 \r
173 #if defined( __GNUC__ ) && defined( __i386__ ) \\r
174  || defined( _WIN32   ) && defined( _M_IX86  ) \\r
175  && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 ))\r
176 #  define VIA_ACE_POSSIBLE\r
177 #endif\r
178 \r
179 /*  Define this option if support for the VIA ACE is required. This uses\r
180     inline assembler instructions and is only implemented for the Microsoft,\r
181     Intel and GCC compilers.  If VIA ACE is known to be present, then defining\r
182     ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption\r
183     code.  If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if\r
184     it is detected (both present and enabled) but the normal AES code will\r
185     also be present.\r
186 \r
187     When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte\r
188     aligned; other input/output buffers do not need to be 16 byte aligned\r
189     but there are very large performance gains if this can be arranged.\r
190     VIA ACE also requires the decryption key schedule to be in reverse\r
191     order (which later checks below ensure).\r
192 */\r
193 \r
194 #if 1 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT )\r
195 #  define USE_VIA_ACE_IF_PRESENT\r
196 #endif\r
197 \r
198 #if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT )\r
199 #  define ASSUME_VIA_ACE_PRESENT\r
200 #  endif\r
201 \r
202 /*  3. ASSEMBLER SUPPORT\r
203 \r
204     This define (which can be on the command line) enables the use of the\r
205     assembler code routines for encryption, decryption and key scheduling\r
206     as follows:\r
207 \r
208     ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for\r
209                 encryption and decryption and but with key scheduling in C\r
210     ASM_X86_V2  uses assembler (aes_x86_v2.asm) with compressed tables for\r
211                 encryption, decryption and key scheduling\r
212     ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for\r
213                 encryption and decryption and but with key scheduling in C\r
214     ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for\r
215                 encryption and decryption and but with key scheduling in C\r
216 \r
217     Change one 'if 0' below to 'if 1' to select the version or define\r
218     as a compilation option.\r
219 */\r
220 \r
221 #if 0 && !defined( ASM_X86_V1C )\r
222 #  define ASM_X86_V1C\r
223 #elif 0 && !defined( ASM_X86_V2  )\r
224 #  define ASM_X86_V2\r
225 #elif 0 && !defined( ASM_X86_V2C )\r
226 #  define ASM_X86_V2C\r
227 #elif 0 && !defined( ASM_AMD64_C )\r
228 #  define ASM_AMD64_C\r
229 #endif\r
230 \r
231 #if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \\r
232       && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 )\r
233 #  error Assembler code is only available for x86 and AMD64 systems\r
234 #endif\r
235 \r
236 /*  4. FAST INPUT/OUTPUT OPERATIONS.\r
237 \r
238     On some machines it is possible to improve speed by transferring the\r
239     bytes in the input and output arrays to and from the internal 32-bit\r
240     variables by addressing these arrays as if they are arrays of 32-bit\r
241     words.  On some machines this will always be possible but there may\r
242     be a large performance penalty if the byte arrays are not aligned on\r
243     the normal word boundaries. On other machines this technique will\r
244     lead to memory access errors when such 32-bit word accesses are not\r
245     properly aligned. The option SAFE_IO avoids such problems but will\r
246     often be slower on those machines that support misaligned access\r
247     (especially so if care is taken to align the input  and output byte\r
248     arrays on 32-bit word boundaries). If SAFE_IO is not defined it is\r
249     assumed that access to byte arrays as if they are arrays of 32-bit\r
250     words will not cause problems when such accesses are misaligned.\r
251 */\r
252 #if 1 && !defined( _MSC_VER )\r
253 #  define SAFE_IO\r
254 #endif\r
255 \r
256 /*  5. LOOP UNROLLING\r
257 \r
258     The code for encryption and decrytpion cycles through a number of rounds\r
259     that can be implemented either in a loop or by expanding the code into a\r
260     long sequence of instructions, the latter producing a larger program but\r
261     one that will often be much faster. The latter is called loop unrolling.\r
262     There are also potential speed advantages in expanding two iterations in\r
263     a loop with half the number of iterations, which is called partial loop\r
264     unrolling.  The following options allow partial or full loop unrolling\r
265     to be set independently for encryption and decryption\r
266 */\r
267 #if 1\r
268 #  define ENC_UNROLL  FULL\r
269 #elif 0\r
270 #  define ENC_UNROLL  PARTIAL\r
271 #else\r
272 #  define ENC_UNROLL  NONE\r
273 #endif\r
274 \r
275 #if 1\r
276 #  define DEC_UNROLL  FULL\r
277 #elif 0\r
278 #  define DEC_UNROLL  PARTIAL\r
279 #else\r
280 #  define DEC_UNROLL  NONE\r
281 #endif\r
282 \r
283 #if 1\r
284 #  define ENC_KS_UNROLL\r
285 #endif\r
286 \r
287 #if 1\r
288 #  define DEC_KS_UNROLL\r
289 #endif\r
290 \r
291 /*  6. FAST FINITE FIELD OPERATIONS\r
292 \r
293     If this section is included, tables are used to provide faster finite\r
294     field arithmetic (this has no effect if FIXED_TABLES is defined).\r
295 */\r
296 #if 1\r
297 #  define FF_TABLES\r
298 #endif\r
299 \r
300 /*  7. INTERNAL STATE VARIABLE FORMAT\r
301 \r
302     The internal state of Rijndael is stored in a number of local 32-bit\r
303     word varaibles which can be defined either as an array or as individual\r
304     names variables. Include this section if you want to store these local\r
305     varaibles in arrays. Otherwise individual local variables will be used.\r
306 */\r
307 #if 1\r
308 #  define ARRAYS\r
309 #endif\r
310 \r
311 /*  8. FIXED OR DYNAMIC TABLES\r
312 \r
313     When this section is included the tables used by the code are compiled\r
314     statically into the binary file.  Otherwise the subroutine aes_init()\r
315     must be called to compute them before the code is first used.\r
316 */\r
317 #if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))\r
318 #  define FIXED_TABLES\r
319 #endif\r
320 \r
321 /*  9. MASKING OR CASTING FROM LONGER VALUES TO BYTES\r
322 \r
323     In some systems it is better to mask longer values to extract bytes \r
324     rather than using a cast. This option allows this choice.\r
325 */\r
326 #if 0\r
327 #  define to_byte(x)  ((uint_8t)(x))\r
328 #else\r
329 #  define to_byte(x)  ((x) & 0xff)\r
330 #endif\r
331 \r
332 /*  10. TABLE ALIGNMENT\r
333 \r
334     On some sytsems speed will be improved by aligning the AES large lookup\r
335     tables on particular boundaries. This define should be set to a power of\r
336     two giving the desired alignment. It can be left undefined if alignment\r
337     is not needed.  This option is specific to the Microsft VC++ compiler -\r
338     it seems to sometimes cause trouble for the VC++ version 6 compiler.\r
339 */\r
340 \r
341 #if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )\r
342 #  define TABLE_ALIGN 32\r
343 #endif\r
344 \r
345 /*  11.  REDUCE CODE AND TABLE SIZE\r
346 \r
347     This replaces some expanded macros with function calls if AES_ASM_V2 or\r
348     AES_ASM_V2C are defined\r
349 */\r
350 \r
351 #if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))\r
352 #  define REDUCE_CODE_SIZE\r
353 #endif\r
354 \r
355 /*  12. TABLE OPTIONS\r
356 \r
357     This cipher proceeds by repeating in a number of cycles known as 'rounds'\r
358     which are implemented by a round function which can optionally be speeded\r
359     up using tables.  The basic tables are each 256 32-bit words, with either\r
360     one or four tables being required for each round function depending on\r
361     how much speed is required. The encryption and decryption round functions\r
362     are different and the last encryption and decrytpion round functions are\r
363     different again making four different round functions in all.\r
364 \r
365     This means that:\r
366       1. Normal encryption and decryption rounds can each use either 0, 1\r
367          or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
368       2. The last encryption and decryption rounds can also use either 0, 1\r
369          or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
370 \r
371     Include or exclude the appropriate definitions below to set the number\r
372     of tables used by this implementation.\r
373 */\r
374 \r
375 #if 1   /* set tables for the normal encryption round */\r
376 #  define ENC_ROUND   FOUR_TABLES\r
377 #elif 0\r
378 #  define ENC_ROUND   ONE_TABLE\r
379 #else\r
380 #  define ENC_ROUND   NO_TABLES\r
381 #endif\r
382 \r
383 #if 1   /* set tables for the last encryption round */\r
384 #  define LAST_ENC_ROUND  FOUR_TABLES\r
385 #elif 0\r
386 #  define LAST_ENC_ROUND  ONE_TABLE\r
387 #else\r
388 #  define LAST_ENC_ROUND  NO_TABLES\r
389 #endif\r
390 \r
391 #if 1   /* set tables for the normal decryption round */\r
392 #  define DEC_ROUND   FOUR_TABLES\r
393 #elif 0\r
394 #  define DEC_ROUND   ONE_TABLE\r
395 #else\r
396 #  define DEC_ROUND   NO_TABLES\r
397 #endif\r
398 \r
399 #if 1   /* set tables for the last decryption round */\r
400 #  define LAST_DEC_ROUND  FOUR_TABLES\r
401 #elif 0\r
402 #  define LAST_DEC_ROUND  ONE_TABLE\r
403 #else\r
404 #  define LAST_DEC_ROUND  NO_TABLES\r
405 #endif\r
406 \r
407 /*  The decryption key schedule can be speeded up with tables in the same\r
408     way that the round functions can.  Include or exclude the following\r
409     defines to set this requirement.\r
410 */\r
411 #if 1\r
412 #  define KEY_SCHED   FOUR_TABLES\r
413 #elif 0\r
414 #  define KEY_SCHED   ONE_TABLE\r
415 #else\r
416 #  define KEY_SCHED   NO_TABLES\r
417 #endif\r
418 \r
419 /*  ---- END OF USER CONFIGURED OPTIONS ---- */\r
420 \r
421 /* VIA ACE support is only available for VC++ and GCC */\r
422 \r
423 #if !defined( _MSC_VER ) && !defined( __GNUC__ )\r
424 #  if defined( ASSUME_VIA_ACE_PRESENT )\r
425 #    undef ASSUME_VIA_ACE_PRESENT\r
426 #  endif\r
427 #  if defined( USE_VIA_ACE_IF_PRESENT )\r
428 #    undef USE_VIA_ACE_IF_PRESENT\r
429 #  endif\r
430 #endif\r
431 \r
432 #if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )\r
433 #  define USE_VIA_ACE_IF_PRESENT\r
434 #endif\r
435 \r
436 #if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )\r
437 #  define AES_REV_DKS\r
438 #endif\r
439 \r
440 /* Assembler support requires the use of platform byte order */\r
441 \r
442 #if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \\r
443     && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)\r
444 #  undef  ALGORITHM_BYTE_ORDER\r
445 #  define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER\r
446 #endif\r
447 \r
448 /* In this implementation the columns of the state array are each held in\r
449    32-bit words. The state array can be held in various ways: in an array\r
450    of words, in a number of individual word variables or in a number of\r
451    processor registers. The following define maps a variable name x and\r
452    a column number c to the way the state array variable is to be held.\r
453    The first define below maps the state into an array x[c] whereas the\r
454    second form maps the state into a number of individual variables x0,\r
455    x1, etc.  Another form could map individual state colums to machine\r
456    register names.\r
457 */\r
458 \r
459 #if defined( ARRAYS )\r
460 #  define s(x,c) x[c]\r
461 #else\r
462 #  define s(x,c) x##c\r
463 #endif\r
464 \r
465 /*  This implementation provides subroutines for encryption, decryption\r
466     and for setting the three key lengths (separately) for encryption\r
467     and decryption. Since not all functions are needed, masks are set\r
468     up here to determine which will be implemented in C\r
469 */\r
470 \r
471 #if !defined( AES_ENCRYPT )\r
472 #  define EFUNCS_IN_C   0\r
473 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \\r
474     || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )\r
475 #  define EFUNCS_IN_C   ENC_KEYING_IN_C\r
476 #elif !defined( ASM_X86_V2 )\r
477 #  define EFUNCS_IN_C   ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )\r
478 #else\r
479 #  define EFUNCS_IN_C   0\r
480 #endif\r
481 \r
482 #if !defined( AES_DECRYPT )\r
483 #  define DFUNCS_IN_C   0\r
484 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \\r
485     || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )\r
486 #  define DFUNCS_IN_C   DEC_KEYING_IN_C\r
487 #elif !defined( ASM_X86_V2 )\r
488 #  define DFUNCS_IN_C   ( DECRYPTION_IN_C | DEC_KEYING_IN_C )\r
489 #else\r
490 #  define DFUNCS_IN_C   0\r
491 #endif\r
492 \r
493 #define FUNCS_IN_C  ( EFUNCS_IN_C | DFUNCS_IN_C )\r
494 \r
495 /* END OF CONFIGURATION OPTIONS */\r
496 \r
497 #define RC_LENGTH   (5 * (AES_BLOCK_SIZE / 4 - 2))\r
498 \r
499 /* Disable or report errors on some combinations of options */\r
500 \r
501 #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES\r
502 #  undef  LAST_ENC_ROUND\r
503 #  define LAST_ENC_ROUND  NO_TABLES\r
504 #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES\r
505 #  undef  LAST_ENC_ROUND\r
506 #  define LAST_ENC_ROUND  ONE_TABLE\r
507 #endif\r
508 \r
509 #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE\r
510 #  undef  ENC_UNROLL\r
511 #  define ENC_UNROLL  NONE\r
512 #endif\r
513 \r
514 #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES\r
515 #  undef  LAST_DEC_ROUND\r
516 #  define LAST_DEC_ROUND  NO_TABLES\r
517 #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES\r
518 #  undef  LAST_DEC_ROUND\r
519 #  define LAST_DEC_ROUND  ONE_TABLE\r
520 #endif\r
521 \r
522 #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE\r
523 #  undef  DEC_UNROLL\r
524 #  define DEC_UNROLL  NONE\r
525 #endif\r
526 \r
527 #if defined( bswap32 )\r
528 #  define aes_sw32    bswap32\r
529 #elif defined( bswap_32 )\r
530 #  define aes_sw32    bswap_32\r
531 #else\r
532 #  define brot(x,n)   (((uint_32t)(x) <<  n) | ((uint_32t)(x) >> (32 - n)))\r
533 #  define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))\r
534 #endif\r
535 \r
536 /*  upr(x,n):  rotates bytes within words by n positions, moving bytes to\r
537                higher index positions with wrap around into low positions\r
538     ups(x,n):  moves bytes by n positions to higher index positions in\r
539                words but without wrap around\r
540     bval(x,n): extracts a byte from a word\r
541 \r
542     WARNING:   The definitions given here are intended only for use with\r
543                unsigned variables and with shift counts that are compile\r
544                time constants\r
545 */\r
546 \r
547 #if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )\r
548 #  define upr(x,n)      (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n))))\r
549 #  define ups(x,n)      ((uint_32t) (x) << (8 * (n)))\r
550 #  define bval(x,n)     to_byte((x) >> (8 * (n)))\r
551 #  define bytes2word(b0, b1, b2, b3)  \\r
552         (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0))\r
553 #endif\r
554 \r
555 #if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )\r
556 #  define upr(x,n)      (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n))))\r
557 #  define ups(x,n)      ((uint_32t) (x) >> (8 * (n)))\r
558 #  define bval(x,n)     to_byte((x) >> (24 - 8 * (n)))\r
559 #  define bytes2word(b0, b1, b2, b3)  \\r
560         (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3))\r
561 #endif\r
562 \r
563 #if defined( SAFE_IO )\r
564 #  define word_in(x,c)    bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \\r
565                                    ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3])\r
566 #  define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \\r
567                           ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); }\r
568 #elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )\r
569 #  define word_in(x,c)    (*((uint_32t*)(x)+(c)))\r
570 #  define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v))\r
571 #else\r
572 #  define word_in(x,c)    aes_sw32(*((uint_32t*)(x)+(c)))\r
573 #  define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v))\r
574 #endif\r
575 \r
576 /* the finite field modular polynomial and elements */\r
577 \r
578 #define WPOLY   0x011b\r
579 #define BPOLY     0x1b\r
580 \r
581 /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */\r
582 \r
583 #define m1  0x80808080\r
584 #define m2  0x7f7f7f7f\r
585 #define gf_mulx(x)  ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))\r
586 \r
587 /* The following defines provide alternative definitions of gf_mulx that might\r
588    give improved performance if a fast 32-bit multiply is not available. Note\r
589    that a temporary variable u needs to be defined where gf_mulx is used.\r
590 \r
591 #define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))\r
592 #define m4  (0x01010101 * BPOLY)\r
593 #define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)\r
594 */\r
595 \r
596 /* Work out which tables are needed for the different options   */\r
597 \r
598 #if defined( ASM_X86_V1C )\r
599 #  if defined( ENC_ROUND )\r
600 #    undef  ENC_ROUND\r
601 #  endif\r
602 #  define ENC_ROUND   FOUR_TABLES\r
603 #  if defined( LAST_ENC_ROUND )\r
604 #    undef  LAST_ENC_ROUND\r
605 #  endif\r
606 #  define LAST_ENC_ROUND  FOUR_TABLES\r
607 #  if defined( DEC_ROUND )\r
608 #    undef  DEC_ROUND\r
609 #  endif\r
610 #  define DEC_ROUND   FOUR_TABLES\r
611 #  if defined( LAST_DEC_ROUND )\r
612 #    undef  LAST_DEC_ROUND\r
613 #  endif\r
614 #  define LAST_DEC_ROUND  FOUR_TABLES\r
615 #  if defined( KEY_SCHED )\r
616 #    undef  KEY_SCHED\r
617 #    define KEY_SCHED   FOUR_TABLES\r
618 #  endif\r
619 #endif\r
620 \r
621 #if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )\r
622 #  if ENC_ROUND == ONE_TABLE\r
623 #    define FT1_SET\r
624 #  elif ENC_ROUND == FOUR_TABLES\r
625 #    define FT4_SET\r
626 #  else\r
627 #    define SBX_SET\r
628 #  endif\r
629 #  if LAST_ENC_ROUND == ONE_TABLE\r
630 #    define FL1_SET\r
631 #  elif LAST_ENC_ROUND == FOUR_TABLES\r
632 #    define FL4_SET\r
633 #  elif !defined( SBX_SET )\r
634 #    define SBX_SET\r
635 #  endif\r
636 #endif\r
637 \r
638 #if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )\r
639 #  if DEC_ROUND == ONE_TABLE\r
640 #    define IT1_SET\r
641 #  elif DEC_ROUND == FOUR_TABLES\r
642 #    define IT4_SET\r
643 #  else\r
644 #    define ISB_SET\r
645 #  endif\r
646 #  if LAST_DEC_ROUND == ONE_TABLE\r
647 #    define IL1_SET\r
648 #  elif LAST_DEC_ROUND == FOUR_TABLES\r
649 #    define IL4_SET\r
650 #  elif !defined(ISB_SET)\r
651 #    define ISB_SET\r
652 #  endif\r
653 #endif\r
654 \r
655 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))\r
656 #  if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))\r
657 #    if KEY_SCHED == ONE_TABLE\r
658 #      if !defined( FL1_SET )  && !defined( FL4_SET ) \r
659 #        define LS1_SET\r
660 #      endif\r
661 #    elif KEY_SCHED == FOUR_TABLES\r
662 #      if !defined( FL4_SET )\r
663 #        define LS4_SET\r
664 #      endif\r
665 #    elif !defined( SBX_SET )\r
666 #      define SBX_SET\r
667 #    endif\r
668 #  endif\r
669 #  if (FUNCS_IN_C & DEC_KEYING_IN_C)\r
670 #    if KEY_SCHED == ONE_TABLE\r
671 #      define IM1_SET\r
672 #    elif KEY_SCHED == FOUR_TABLES\r
673 #      define IM4_SET\r
674 #    elif !defined( SBX_SET )\r
675 #      define SBX_SET\r
676 #    endif\r
677 #  endif\r
678 #endif\r
679 \r
680 /* generic definitions of Rijndael macros that use tables    */\r
681 \r
682 #define no_table(x,box,vf,rf,c) bytes2word( \\r
683     box[bval(vf(x,0,c),rf(0,c))], \\r
684     box[bval(vf(x,1,c),rf(1,c))], \\r
685     box[bval(vf(x,2,c),rf(2,c))], \\r
686     box[bval(vf(x,3,c),rf(3,c))])\r
687 \r
688 #define one_table(x,op,tab,vf,rf,c) \\r
689  (     tab[bval(vf(x,0,c),rf(0,c))] \\r
690   ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \\r
691   ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \\r
692   ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))\r
693 \r
694 #define four_tables(x,tab,vf,rf,c) \\r
695  (  tab[0][bval(vf(x,0,c),rf(0,c))] \\r
696   ^ tab[1][bval(vf(x,1,c),rf(1,c))] \\r
697   ^ tab[2][bval(vf(x,2,c),rf(2,c))] \\r
698   ^ tab[3][bval(vf(x,3,c),rf(3,c))])\r
699 \r
700 #define vf1(x,r,c)  (x)\r
701 #define rf1(r,c)    (r)\r
702 #define rf2(r,c)    ((8+r-c)&3)\r
703 \r
704 /* perform forward and inverse column mix operation on four bytes in long word x in */\r
705 /* parallel. NOTE: x must be a simple variable, NOT an expression in these macros.  */\r
706 \r
707 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) \r
708 \r
709 #if defined( FM4_SET )      /* not currently used */\r
710 #  define fwd_mcol(x)       four_tables(x,t_use(f,m),vf1,rf1,0)\r
711 #elif defined( FM1_SET )    /* not currently used */\r
712 #  define fwd_mcol(x)       one_table(x,upr,t_use(f,m),vf1,rf1,0)\r
713 #else\r
714 #  define dec_fmvars        uint_32t g2\r
715 #  define fwd_mcol(x)       (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))\r
716 #endif\r
717 \r
718 #if defined( IM4_SET )\r
719 #  define inv_mcol(x)       four_tables(x,t_use(i,m),vf1,rf1,0)\r
720 #elif defined( IM1_SET )\r
721 #  define inv_mcol(x)       one_table(x,upr,t_use(i,m),vf1,rf1,0)\r
722 #else\r
723 #  define dec_imvars        uint_32t g2, g4, g9\r
724 #  define inv_mcol(x)       (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \\r
725                             (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))\r
726 #endif\r
727 \r
728 #if defined( FL4_SET )\r
729 #  define ls_box(x,c)       four_tables(x,t_use(f,l),vf1,rf2,c)\r
730 #elif defined( LS4_SET )\r
731 #  define ls_box(x,c)       four_tables(x,t_use(l,s),vf1,rf2,c)\r
732 #elif defined( FL1_SET )\r
733 #  define ls_box(x,c)       one_table(x,upr,t_use(f,l),vf1,rf2,c)\r
734 #elif defined( LS1_SET )\r
735 #  define ls_box(x,c)       one_table(x,upr,t_use(l,s),vf1,rf2,c)\r
736 #else\r
737 #  define ls_box(x,c)       no_table(x,t_use(s,box),vf1,rf2,c)\r
738 #endif\r
739 \r
740 #endif\r
741 \r
742 #if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )\r
743 #  define ISB_SET\r
744 #endif\r
745 \r
746 #endif\r