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

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

unix.superglobalmegacorp.com

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