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