2 ---------------------------------------------------------------------------
\r
3 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
\r
7 The redistribution and use of this software (with or without changes)
\r
8 is allowed without the payment of fees or royalties provided that:
\r
10 1. source code distributions include the above copyright notice, this
\r
11 list of conditions and the following disclaimer;
\r
13 2. binary distributions include the above copyright notice, this list
\r
14 of conditions and the following disclaimer in their documentation;
\r
16 3. the name of the copyright holder is not used to endorse products
\r
17 built using this software without specific written permission.
\r
21 This software is provided 'as is' with no explicit or implied warranties
\r
22 in respect of its properties, including, but not limited to, correctness
\r
23 and/or fitness for purpose.
\r
24 ---------------------------------------------------------------------------
\r
25 Issue Date: 20/12/2007
\r
27 This file contains the compilation options for AES (Rijndael) and code
\r
28 that is common across encryption, key scheduling and table generation.
\r
32 These source code files implement the AES algorithm Rijndael designed by
\r
33 Joan Daemen and Vincent Rijmen. This version is designed for the standard
\r
34 block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
\r
37 This version is designed for flexibility and speed using operations on
\r
38 32-bit words rather than operations on bytes. It can be compiled with
\r
39 either big or little endian internal byte order but is faster when the
\r
40 native byte order for the processor is used.
\r
42 THE CIPHER INTERFACE
\r
44 The cipher interface is implemented as an array of bytes in which lower
\r
45 AES bit sequence indexes map to higher numeric significance within bytes.
\r
47 uint_8t (an unsigned 8-bit type)
\r
48 uint_32t (an unsigned 32-bit type)
\r
49 struct aes_encrypt_ctx (structure for the cipher encryption context)
\r
50 struct aes_decrypt_ctx (structure for the cipher decryption context)
\r
51 AES_RETURN the function return type
\r
55 AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
\r
56 AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
\r
57 AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
\r
58 AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out,
\r
59 const aes_encrypt_ctx cx[1]);
\r
61 AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
\r
62 AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
\r
63 AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
\r
64 AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out,
\r
65 const aes_decrypt_ctx cx[1]);
\r
67 IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that
\r
68 you call aes_init() before AES is used so that the tables are initialised.
\r
70 C++ aes class subroutines:
\r
72 Class AESencrypt for encryption
\r
76 AESencrypt(const unsigned char *key) - 128 bit key
\r
78 AES_RETURN key128(const unsigned char *key)
\r
79 AES_RETURN key192(const unsigned char *key)
\r
80 AES_RETURN key256(const unsigned char *key)
\r
81 AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const
\r
83 Class AESdecrypt for encryption
\r
86 AESdecrypt(const unsigned char *key) - 128 bit key
\r
88 AES_RETURN key128(const unsigned char *key)
\r
89 AES_RETURN key192(const unsigned char *key)
\r
90 AES_RETURN key256(const unsigned char *key)
\r
91 AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const
\r
94 #if !defined( _AESOPT_H )
\r
97 #if defined( __cplusplus )
\r
103 /* PLATFORM SPECIFIC INCLUDES */
\r
105 #include "brg_endian.h"
\r
107 /* CONFIGURATION - THE USE OF DEFINES
\r
109 Later in this section there are a number of defines that control the
\r
110 operation of the code. In each section, the purpose of each define is
\r
111 explained so that the relevant form can be included or excluded by
\r
112 setting either 1's or 0's respectively on the branches of the related
\r
113 #if clauses. The following local defines should not be changed.
\r
116 #define ENCRYPTION_IN_C 1
\r
117 #define DECRYPTION_IN_C 2
\r
118 #define ENC_KEYING_IN_C 4
\r
119 #define DEC_KEYING_IN_C 8
\r
121 #define NO_TABLES 0
\r
122 #define ONE_TABLE 1
\r
123 #define FOUR_TABLES 4
\r
128 /* --- START OF USER CONFIGURED OPTIONS --- */
\r
130 /* 1. BYTE ORDER WITHIN 32 BIT WORDS
\r
132 The fundamental data processing units in Rijndael are 8-bit bytes. The
\r
133 input, output and key input are all enumerated arrays of bytes in which
\r
134 bytes are numbered starting at zero and increasing to one less than the
\r
135 number of bytes in the array in question. This enumeration is only used
\r
136 for naming bytes and does not imply any adjacency or order relationship
\r
137 from one byte to another. When these inputs and outputs are considered
\r
138 as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
\r
139 byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
\r
140 In this implementation bits are numbered from 0 to 7 starting at the
\r
141 numerically least significant end of each byte (bit n represents 2^n).
\r
143 However, Rijndael can be implemented more efficiently using 32-bit
\r
144 words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
\r
145 into word[n]. While in principle these bytes can be assembled into words
\r
146 in any positions, this implementation only supports the two formats in
\r
147 which bytes in adjacent positions within words also have adjacent byte
\r
148 numbers. This order is called big-endian if the lowest numbered bytes
\r
149 in words have the highest numeric significance and little-endian if the
\r
152 This code can work in either order irrespective of the order used by the
\r
153 machine on which it runs. Normally the internal byte order will be set
\r
154 to the order of the processor on which the code is to be run but this
\r
155 define can be used to reverse this in special situations
\r
157 WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set.
\r
158 This define will hence be redefined later (in section 4) if necessary
\r
162 # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
\r
164 # define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN
\r
166 # define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN
\r
168 # error The algorithm byte order is not defined
\r
171 /* 2. VIA ACE SUPPORT */
\r
173 #if defined( __GNUC__ ) && defined( __i386__ ) \
\r
174 || defined( _WIN32 ) && defined( _M_IX86 ) \
\r
175 && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
\r
176 # define VIA_ACE_POSSIBLE
\r
179 /* Define this option if support for the VIA ACE is required. This uses
\r
180 inline assembler instructions and is only implemented for the Microsoft,
\r
181 Intel and GCC compilers. If VIA ACE is known to be present, then defining
\r
182 ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption
\r
183 code. If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if
\r
184 it is detected (both present and enabled) but the normal AES code will
\r
187 When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte
\r
188 aligned; other input/output buffers do not need to be 16 byte aligned
\r
189 but there are very large performance gains if this can be arranged.
\r
190 VIA ACE also requires the decryption key schedule to be in reverse
\r
191 order (which later checks below ensure).
\r
194 #if 1 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT )
\r
195 # define USE_VIA_ACE_IF_PRESENT
\r
198 #if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT )
\r
199 # define ASSUME_VIA_ACE_PRESENT
\r
202 /* 3. ASSEMBLER SUPPORT
\r
204 This define (which can be on the command line) enables the use of the
\r
205 assembler code routines for encryption, decryption and key scheduling
\r
208 ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for
\r
209 encryption and decryption and but with key scheduling in C
\r
210 ASM_X86_V2 uses assembler (aes_x86_v2.asm) with compressed tables for
\r
211 encryption, decryption and key scheduling
\r
212 ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for
\r
213 encryption and decryption and but with key scheduling in C
\r
214 ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for
\r
215 encryption and decryption and but with key scheduling in C
\r
217 Change one 'if 0' below to 'if 1' to select the version or define
\r
218 as a compilation option.
\r
221 #if 0 && !defined( ASM_X86_V1C )
\r
222 # define ASM_X86_V1C
\r
223 #elif 0 && !defined( ASM_X86_V2 )
\r
224 # define ASM_X86_V2
\r
225 #elif 0 && !defined( ASM_X86_V2C )
\r
226 # define ASM_X86_V2C
\r
227 #elif 0 && !defined( ASM_AMD64_C )
\r
228 # define ASM_AMD64_C
\r
231 #if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \
\r
232 && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 )
\r
233 # error Assembler code is only available for x86 and AMD64 systems
\r
236 /* 4. FAST INPUT/OUTPUT OPERATIONS.
\r
238 On some machines it is possible to improve speed by transferring the
\r
239 bytes in the input and output arrays to and from the internal 32-bit
\r
240 variables by addressing these arrays as if they are arrays of 32-bit
\r
241 words. On some machines this will always be possible but there may
\r
242 be a large performance penalty if the byte arrays are not aligned on
\r
243 the normal word boundaries. On other machines this technique will
\r
244 lead to memory access errors when such 32-bit word accesses are not
\r
245 properly aligned. The option SAFE_IO avoids such problems but will
\r
246 often be slower on those machines that support misaligned access
\r
247 (especially so if care is taken to align the input and output byte
\r
248 arrays on 32-bit word boundaries). If SAFE_IO is not defined it is
\r
249 assumed that access to byte arrays as if they are arrays of 32-bit
\r
250 words will not cause problems when such accesses are misaligned.
\r
252 #if 1 && !defined( _MSC_VER )
\r
256 /* 5. LOOP UNROLLING
\r
258 The code for encryption and decrytpion cycles through a number of rounds
\r
259 that can be implemented either in a loop or by expanding the code into a
\r
260 long sequence of instructions, the latter producing a larger program but
\r
261 one that will often be much faster. The latter is called loop unrolling.
\r
262 There are also potential speed advantages in expanding two iterations in
\r
263 a loop with half the number of iterations, which is called partial loop
\r
264 unrolling. The following options allow partial or full loop unrolling
\r
265 to be set independently for encryption and decryption
\r
268 # define ENC_UNROLL FULL
\r
270 # define ENC_UNROLL PARTIAL
\r
272 # define ENC_UNROLL NONE
\r
276 # define DEC_UNROLL FULL
\r
278 # define DEC_UNROLL PARTIAL
\r
280 # define DEC_UNROLL NONE
\r
284 # define ENC_KS_UNROLL
\r
288 # define DEC_KS_UNROLL
\r
291 /* 6. FAST FINITE FIELD OPERATIONS
\r
293 If this section is included, tables are used to provide faster finite
\r
294 field arithmetic (this has no effect if FIXED_TABLES is defined).
\r
300 /* 7. INTERNAL STATE VARIABLE FORMAT
\r
302 The internal state of Rijndael is stored in a number of local 32-bit
\r
303 word varaibles which can be defined either as an array or as individual
\r
304 names variables. Include this section if you want to store these local
\r
305 varaibles in arrays. Otherwise individual local variables will be used.
\r
311 /* 8. FIXED OR DYNAMIC TABLES
\r
313 When this section is included the tables used by the code are compiled
\r
314 statically into the binary file. Otherwise the subroutine aes_init()
\r
315 must be called to compute them before the code is first used.
\r
317 #if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
\r
318 # define FIXED_TABLES
\r
321 /* 9. MASKING OR CASTING FROM LONGER VALUES TO BYTES
\r
323 In some systems it is better to mask longer values to extract bytes
\r
324 rather than using a cast. This option allows this choice.
\r
327 # define to_byte(x) ((uint_8t)(x))
\r
329 # define to_byte(x) ((x) & 0xff)
\r
332 /* 10. TABLE ALIGNMENT
\r
334 On some sytsems speed will be improved by aligning the AES large lookup
\r
335 tables on particular boundaries. This define should be set to a power of
\r
336 two giving the desired alignment. It can be left undefined if alignment
\r
337 is not needed. This option is specific to the Microsft VC++ compiler -
\r
338 it seems to sometimes cause trouble for the VC++ version 6 compiler.
\r
341 #if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
\r
342 # define TABLE_ALIGN 32
\r
345 /* 11. REDUCE CODE AND TABLE SIZE
\r
347 This replaces some expanded macros with function calls if AES_ASM_V2 or
\r
348 AES_ASM_V2C are defined
\r
351 #if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))
\r
352 # define REDUCE_CODE_SIZE
\r
355 /* 12. TABLE OPTIONS
\r
357 This cipher proceeds by repeating in a number of cycles known as 'rounds'
\r
358 which are implemented by a round function which can optionally be speeded
\r
359 up using tables. The basic tables are each 256 32-bit words, with either
\r
360 one or four tables being required for each round function depending on
\r
361 how much speed is required. The encryption and decryption round functions
\r
362 are different and the last encryption and decrytpion round functions are
\r
363 different again making four different round functions in all.
\r
366 1. Normal encryption and decryption rounds can each use either 0, 1
\r
367 or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
\r
368 2. The last encryption and decryption rounds can also use either 0, 1
\r
369 or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
\r
371 Include or exclude the appropriate definitions below to set the number
\r
372 of tables used by this implementation.
\r
375 #if 1 /* set tables for the normal encryption round */
\r
376 # define ENC_ROUND FOUR_TABLES
\r
378 # define ENC_ROUND ONE_TABLE
\r
380 # define ENC_ROUND NO_TABLES
\r
383 #if 1 /* set tables for the last encryption round */
\r
384 # define LAST_ENC_ROUND FOUR_TABLES
\r
386 # define LAST_ENC_ROUND ONE_TABLE
\r
388 # define LAST_ENC_ROUND NO_TABLES
\r
391 #if 1 /* set tables for the normal decryption round */
\r
392 # define DEC_ROUND FOUR_TABLES
\r
394 # define DEC_ROUND ONE_TABLE
\r
396 # define DEC_ROUND NO_TABLES
\r
399 #if 1 /* set tables for the last decryption round */
\r
400 # define LAST_DEC_ROUND FOUR_TABLES
\r
402 # define LAST_DEC_ROUND ONE_TABLE
\r
404 # define LAST_DEC_ROUND NO_TABLES
\r
407 /* The decryption key schedule can be speeded up with tables in the same
\r
408 way that the round functions can. Include or exclude the following
\r
409 defines to set this requirement.
\r
412 # define KEY_SCHED FOUR_TABLES
\r
414 # define KEY_SCHED ONE_TABLE
\r
416 # define KEY_SCHED NO_TABLES
\r
419 /* ---- END OF USER CONFIGURED OPTIONS ---- */
\r
421 /* VIA ACE support is only available for VC++ and GCC */
\r
423 #if !defined( _MSC_VER ) && !defined( __GNUC__ )
\r
424 # if defined( ASSUME_VIA_ACE_PRESENT )
\r
425 # undef ASSUME_VIA_ACE_PRESENT
\r
427 # if defined( USE_VIA_ACE_IF_PRESENT )
\r
428 # undef USE_VIA_ACE_IF_PRESENT
\r
432 #if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )
\r
433 # define USE_VIA_ACE_IF_PRESENT
\r
436 #if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )
\r
437 # define AES_REV_DKS
\r
440 /* Assembler support requires the use of platform byte order */
\r
442 #if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \
\r
443 && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)
\r
444 # undef ALGORITHM_BYTE_ORDER
\r
445 # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
\r
448 /* In this implementation the columns of the state array are each held in
\r
449 32-bit words. The state array can be held in various ways: in an array
\r
450 of words, in a number of individual word variables or in a number of
\r
451 processor registers. The following define maps a variable name x and
\r
452 a column number c to the way the state array variable is to be held.
\r
453 The first define below maps the state into an array x[c] whereas the
\r
454 second form maps the state into a number of individual variables x0,
\r
455 x1, etc. Another form could map individual state colums to machine
\r
459 #if defined( ARRAYS )
\r
460 # define s(x,c) x[c]
\r
462 # define s(x,c) x##c
\r
465 /* This implementation provides subroutines for encryption, decryption
\r
466 and for setting the three key lengths (separately) for encryption
\r
467 and decryption. Since not all functions are needed, masks are set
\r
468 up here to determine which will be implemented in C
\r
471 #if !defined( AES_ENCRYPT )
\r
472 # define EFUNCS_IN_C 0
\r
473 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
\r
474 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
\r
475 # define EFUNCS_IN_C ENC_KEYING_IN_C
\r
476 #elif !defined( ASM_X86_V2 )
\r
477 # define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )
\r
479 # define EFUNCS_IN_C 0
\r
482 #if !defined( AES_DECRYPT )
\r
483 # define DFUNCS_IN_C 0
\r
484 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
\r
485 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
\r
486 # define DFUNCS_IN_C DEC_KEYING_IN_C
\r
487 #elif !defined( ASM_X86_V2 )
\r
488 # define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C )
\r
490 # define DFUNCS_IN_C 0
\r
493 #define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C )
\r
495 /* END OF CONFIGURATION OPTIONS */
\r
497 #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
\r
499 /* Disable or report errors on some combinations of options */
\r
501 #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
\r
502 # undef LAST_ENC_ROUND
\r
503 # define LAST_ENC_ROUND NO_TABLES
\r
504 #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
\r
505 # undef LAST_ENC_ROUND
\r
506 # define LAST_ENC_ROUND ONE_TABLE
\r
509 #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
\r
511 # define ENC_UNROLL NONE
\r
514 #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
\r
515 # undef LAST_DEC_ROUND
\r
516 # define LAST_DEC_ROUND NO_TABLES
\r
517 #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
\r
518 # undef LAST_DEC_ROUND
\r
519 # define LAST_DEC_ROUND ONE_TABLE
\r
522 #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
\r
524 # define DEC_UNROLL NONE
\r
527 #if defined( bswap32 )
\r
528 # define aes_sw32 bswap32
\r
529 #elif defined( bswap_32 )
\r
530 # define aes_sw32 bswap_32
\r
532 # define brot(x,n) (((uint_32t)(x) << n) | ((uint_32t)(x) >> (32 - n)))
\r
533 # define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
\r
536 /* upr(x,n): rotates bytes within words by n positions, moving bytes to
\r
537 higher index positions with wrap around into low positions
\r
538 ups(x,n): moves bytes by n positions to higher index positions in
\r
539 words but without wrap around
\r
540 bval(x,n): extracts a byte from a word
\r
542 WARNING: The definitions given here are intended only for use with
\r
543 unsigned variables and with shift counts that are compile
\r
547 #if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )
\r
548 # define upr(x,n) (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n))))
\r
549 # define ups(x,n) ((uint_32t) (x) << (8 * (n)))
\r
550 # define bval(x,n) to_byte((x) >> (8 * (n)))
\r
551 # define bytes2word(b0, b1, b2, b3) \
\r
552 (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0))
\r
555 #if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )
\r
556 # define upr(x,n) (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n))))
\r
557 # define ups(x,n) ((uint_32t) (x) >> (8 * (n)))
\r
558 # define bval(x,n) to_byte((x) >> (24 - 8 * (n)))
\r
559 # define bytes2word(b0, b1, b2, b3) \
\r
560 (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3))
\r
563 #if defined( SAFE_IO )
\r
564 # define word_in(x,c) bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \
\r
565 ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3])
\r
566 # define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \
\r
567 ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); }
\r
568 #elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )
\r
569 # define word_in(x,c) (*((uint_32t*)(x)+(c)))
\r
570 # define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v))
\r
572 # define word_in(x,c) aes_sw32(*((uint_32t*)(x)+(c)))
\r
573 # define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v))
\r
576 /* the finite field modular polynomial and elements */
\r
578 #define WPOLY 0x011b
\r
581 /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
\r
583 #define m1 0x80808080
\r
584 #define m2 0x7f7f7f7f
\r
585 #define gf_mulx(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
\r
587 /* The following defines provide alternative definitions of gf_mulx that might
\r
588 give improved performance if a fast 32-bit multiply is not available. Note
\r
589 that a temporary variable u needs to be defined where gf_mulx is used.
\r
591 #define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
\r
592 #define m4 (0x01010101 * BPOLY)
\r
593 #define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
\r
596 /* Work out which tables are needed for the different options */
\r
598 #if defined( ASM_X86_V1C )
\r
599 # if defined( ENC_ROUND )
\r
602 # define ENC_ROUND FOUR_TABLES
\r
603 # if defined( LAST_ENC_ROUND )
\r
604 # undef LAST_ENC_ROUND
\r
606 # define LAST_ENC_ROUND FOUR_TABLES
\r
607 # if defined( DEC_ROUND )
\r
610 # define DEC_ROUND FOUR_TABLES
\r
611 # if defined( LAST_DEC_ROUND )
\r
612 # undef LAST_DEC_ROUND
\r
614 # define LAST_DEC_ROUND FOUR_TABLES
\r
615 # if defined( KEY_SCHED )
\r
617 # define KEY_SCHED FOUR_TABLES
\r
621 #if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )
\r
622 # if ENC_ROUND == ONE_TABLE
\r
624 # elif ENC_ROUND == FOUR_TABLES
\r
629 # if LAST_ENC_ROUND == ONE_TABLE
\r
631 # elif LAST_ENC_ROUND == FOUR_TABLES
\r
633 # elif !defined( SBX_SET )
\r
638 #if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )
\r
639 # if DEC_ROUND == ONE_TABLE
\r
641 # elif DEC_ROUND == FOUR_TABLES
\r
646 # if LAST_DEC_ROUND == ONE_TABLE
\r
648 # elif LAST_DEC_ROUND == FOUR_TABLES
\r
650 # elif !defined(ISB_SET)
\r
655 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
\r
656 # if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))
\r
657 # if KEY_SCHED == ONE_TABLE
\r
658 # if !defined( FL1_SET ) && !defined( FL4_SET )
\r
661 # elif KEY_SCHED == FOUR_TABLES
\r
662 # if !defined( FL4_SET )
\r
665 # elif !defined( SBX_SET )
\r
669 # if (FUNCS_IN_C & DEC_KEYING_IN_C)
\r
670 # if KEY_SCHED == ONE_TABLE
\r
672 # elif KEY_SCHED == FOUR_TABLES
\r
674 # elif !defined( SBX_SET )
\r
680 /* generic definitions of Rijndael macros that use tables */
\r
682 #define no_table(x,box,vf,rf,c) bytes2word( \
\r
683 box[bval(vf(x,0,c),rf(0,c))], \
\r
684 box[bval(vf(x,1,c),rf(1,c))], \
\r
685 box[bval(vf(x,2,c),rf(2,c))], \
\r
686 box[bval(vf(x,3,c),rf(3,c))])
\r
688 #define one_table(x,op,tab,vf,rf,c) \
\r
689 ( tab[bval(vf(x,0,c),rf(0,c))] \
\r
690 ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
\r
691 ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
\r
692 ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
\r
694 #define four_tables(x,tab,vf,rf,c) \
\r
695 ( tab[0][bval(vf(x,0,c),rf(0,c))] \
\r
696 ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
\r
697 ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
\r
698 ^ tab[3][bval(vf(x,3,c),rf(3,c))])
\r
700 #define vf1(x,r,c) (x)
\r
701 #define rf1(r,c) (r)
\r
702 #define rf2(r,c) ((8+r-c)&3)
\r
704 /* perform forward and inverse column mix operation on four bytes in long word x in */
\r
705 /* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */
\r
707 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
\r
709 #if defined( FM4_SET ) /* not currently used */
\r
710 # define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
\r
711 #elif defined( FM1_SET ) /* not currently used */
\r
712 # define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
\r
714 # define dec_fmvars uint_32t g2
\r
715 # define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
\r
718 #if defined( IM4_SET )
\r
719 # define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
\r
720 #elif defined( IM1_SET )
\r
721 # define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
\r
723 # define dec_imvars uint_32t g2, g4, g9
\r
724 # define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
\r
725 (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
\r
728 #if defined( FL4_SET )
\r
729 # define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
\r
730 #elif defined( LS4_SET )
\r
731 # define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
\r
732 #elif defined( FL1_SET )
\r
733 # define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
\r
734 #elif defined( LS1_SET )
\r
735 # define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
\r
737 # define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
\r
742 #if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )
\r