Annotation of truecrypt/crypto/aesopt.h, revision 1.1.1.3

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.