xref: /titanic_51/usr/src/common/crypto/aes/amd64/aesopt.h (revision 4b56a00321e0ce508e55cc5e43e3ad7b00005a39)
190bcde94Sda73024 /*
290bcde94Sda73024  * ---------------------------------------------------------------------------
390bcde94Sda73024  * Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.
490bcde94Sda73024  *
590bcde94Sda73024  * LICENSE TERMS
690bcde94Sda73024  *
790bcde94Sda73024  * The free distribution and use of this software is allowed (with or without
890bcde94Sda73024  * changes) provided that:
990bcde94Sda73024  *
1090bcde94Sda73024  *  1. source code distributions include the above copyright notice, this
1190bcde94Sda73024  *	list of conditions and the following disclaimer;
1290bcde94Sda73024  *
1390bcde94Sda73024  *  2. binary distributions include the above copyright notice, this list
1490bcde94Sda73024  *	of conditions and the following disclaimer in their documentation;
1590bcde94Sda73024  *
1690bcde94Sda73024  *  3. the name of the copyright holder is not used to endorse products
1790bcde94Sda73024  *	built using this software without specific written permission.
1890bcde94Sda73024  *
1990bcde94Sda73024  * DISCLAIMER
2090bcde94Sda73024  *
2190bcde94Sda73024  * This software is provided 'as is' with no explicit or implied warranties
2290bcde94Sda73024  * in respect of its properties, including, but not limited to, correctness
2390bcde94Sda73024  * and/or fitness for purpose.
2490bcde94Sda73024  * ---------------------------------------------------------------------------
2590bcde94Sda73024  * Issue Date: 20/12/2007
2690bcde94Sda73024  *
2790bcde94Sda73024  * This file contains the compilation options for AES (Rijndael) and code
2890bcde94Sda73024  * that is common across encryption, key scheduling and table generation.
2990bcde94Sda73024  *
3090bcde94Sda73024  * OPERATION
3190bcde94Sda73024  *
3290bcde94Sda73024  * These source code files implement the AES algorithm Rijndael designed by
3390bcde94Sda73024  * Joan Daemen and Vincent Rijmen. This version is designed for the standard
3490bcde94Sda73024  * block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
3590bcde94Sda73024  * and 32 bytes).
3690bcde94Sda73024  *
3790bcde94Sda73024  * This version is designed for flexibility and speed using operations on
3890bcde94Sda73024  * 32-bit words rather than operations on bytes.  It can be compiled with
3990bcde94Sda73024  * either big or little endian internal byte order but is faster when the
4090bcde94Sda73024  * native byte order for the processor is used.
4190bcde94Sda73024  *
4290bcde94Sda73024  * THE CIPHER INTERFACE
4390bcde94Sda73024  *
4490bcde94Sda73024  * The cipher interface is implemented as an array of bytes in which lower
4590bcde94Sda73024  * AES bit sequence indexes map to higher numeric significance within bytes.
4690bcde94Sda73024  */
4790bcde94Sda73024 
4890bcde94Sda73024 /*
4990bcde94Sda73024  * OpenSolaris changes
5090bcde94Sda73024  * 1. Added __cplusplus and _AESTAB_H header guards
5190bcde94Sda73024  * 2. Added header files sys/types.h and aes_impl.h
5290bcde94Sda73024  * 3. Added defines for AES_ENCRYPT, AES_DECRYPT, AES_REV_DKS, and ASM_AMD64_C
5390bcde94Sda73024  * 4. Moved defines for IS_BIG_ENDIAN, IS_LITTLE_ENDIAN, PLATFORM_BYTE_ORDER
5490bcde94Sda73024  *    from brg_endian.h
5590bcde94Sda73024  * 5. Undefined VIA_ACE_POSSIBLE and ASSUME_VIA_ACE_PRESENT
5690bcde94Sda73024  * 6. Changed uint_8t and uint_32t to uint8_t and uint32_t
57*4b56a003SDaniel Anderson  * 7. Defined aes_sw32 as htonl() for byte swapping
58*4b56a003SDaniel Anderson  * 8. Cstyled and hdrchk code
5990bcde94Sda73024  *
6090bcde94Sda73024  */
6190bcde94Sda73024 
6290bcde94Sda73024 #ifndef _AESOPT_H
6390bcde94Sda73024 #define	_AESOPT_H
6490bcde94Sda73024 
6590bcde94Sda73024 #ifdef	__cplusplus
6690bcde94Sda73024 extern "C" {
6790bcde94Sda73024 #endif
6890bcde94Sda73024 
6990bcde94Sda73024 #include <sys/types.h>
70*4b56a003SDaniel Anderson #include <sys/byteorder.h>
7190bcde94Sda73024 #include <aes_impl.h>
7290bcde94Sda73024 
7390bcde94Sda73024 /*  SUPPORT FEATURES */
7490bcde94Sda73024 #define	AES_ENCRYPT /* if support for encryption is needed */
7590bcde94Sda73024 #define	AES_DECRYPT /* if support for decryption is needed */
7690bcde94Sda73024 
7790bcde94Sda73024 /*  PLATFORM-SPECIFIC FEATURES */
7890bcde94Sda73024 #define	IS_BIG_ENDIAN		4321 /* byte 0 is most significant (mc68k) */
7990bcde94Sda73024 #define	IS_LITTLE_ENDIAN	1234 /* byte 0 is least significant (i386) */
8090bcde94Sda73024 #define	PLATFORM_BYTE_ORDER	IS_LITTLE_ENDIAN
8190bcde94Sda73024 #define	AES_REV_DKS /* define to reverse decryption key schedule */
8290bcde94Sda73024 
8390bcde94Sda73024 
8490bcde94Sda73024 /*
8590bcde94Sda73024  *  CONFIGURATION - THE USE OF DEFINES
8690bcde94Sda73024  *	Later in this section there are a number of defines that control the
8790bcde94Sda73024  *	operation of the code.  In each section, the purpose of each define is
8890bcde94Sda73024  *	explained so that the relevant form can be included or excluded by
8990bcde94Sda73024  *	setting either 1's or 0's respectively on the branches of the related
9090bcde94Sda73024  *	#if clauses.  The following local defines should not be changed.
9190bcde94Sda73024  */
9290bcde94Sda73024 
9390bcde94Sda73024 #define	ENCRYPTION_IN_C	1
9490bcde94Sda73024 #define	DECRYPTION_IN_C	2
9590bcde94Sda73024 #define	ENC_KEYING_IN_C	4
9690bcde94Sda73024 #define	DEC_KEYING_IN_C	8
9790bcde94Sda73024 
9890bcde94Sda73024 #define	NO_TABLES	0
9990bcde94Sda73024 #define	ONE_TABLE	1
10090bcde94Sda73024 #define	FOUR_TABLES	4
10190bcde94Sda73024 #define	NONE		0
10290bcde94Sda73024 #define	PARTIAL		1
10390bcde94Sda73024 #define	FULL		2
10490bcde94Sda73024 
10590bcde94Sda73024 /*  --- START OF USER CONFIGURED OPTIONS --- */
10690bcde94Sda73024 
10790bcde94Sda73024 /*
10890bcde94Sda73024  *  1. BYTE ORDER WITHIN 32 BIT WORDS
10990bcde94Sda73024  *
11090bcde94Sda73024  *	The fundamental data processing units in Rijndael are 8-bit bytes. The
11190bcde94Sda73024  *	input, output and key input are all enumerated arrays of bytes in which
11290bcde94Sda73024  *	bytes are numbered starting at zero and increasing to one less than the
11390bcde94Sda73024  *	number of bytes in the array in question. This enumeration is only used
11490bcde94Sda73024  *	for naming bytes and does not imply any adjacency or order relationship
11590bcde94Sda73024  *	from one byte to another. When these inputs and outputs are considered
11690bcde94Sda73024  *	as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
11790bcde94Sda73024  *	byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
11890bcde94Sda73024  *	In this implementation bits are numbered from 0 to 7 starting at the
11990bcde94Sda73024  *	numerically least significant end of each byte.  Bit n represents 2^n.
12090bcde94Sda73024  *
12190bcde94Sda73024  *	However, Rijndael can be implemented more efficiently using 32-bit
12290bcde94Sda73024  *	words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
12390bcde94Sda73024  *	into word[n]. While in principle these bytes can be assembled into words
12490bcde94Sda73024  *	in any positions, this implementation only supports the two formats in
12590bcde94Sda73024  *	which bytes in adjacent positions within words also have adjacent byte
12690bcde94Sda73024  *	numbers. This order is called big-endian if the lowest numbered bytes
12790bcde94Sda73024  *	in words have the highest numeric significance and little-endian if the
12890bcde94Sda73024  *	opposite applies.
12990bcde94Sda73024  *
13090bcde94Sda73024  *	This code can work in either order irrespective of the order used by the
13190bcde94Sda73024  *	machine on which it runs. Normally the internal byte order will be set
13290bcde94Sda73024  *	to the order of the processor on which the code is to be run but this
13390bcde94Sda73024  *	define	can be used to reverse this in special situations
13490bcde94Sda73024  *
13590bcde94Sda73024  *	WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set.
13690bcde94Sda73024  *	This define will hence be redefined later (in section 4) if necessary
13790bcde94Sda73024  */
13890bcde94Sda73024 
13990bcde94Sda73024 #if 1
14090bcde94Sda73024 #define	ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
14190bcde94Sda73024 #elif 0
14290bcde94Sda73024 #define	ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN
14390bcde94Sda73024 #elif 0
14490bcde94Sda73024 #define	ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN
14590bcde94Sda73024 #else
14690bcde94Sda73024 #error The algorithm byte order is not defined
14790bcde94Sda73024 #endif
14890bcde94Sda73024 
14990bcde94Sda73024 /*  2. VIA ACE SUPPORT */
15090bcde94Sda73024 
15190bcde94Sda73024 #if defined(__GNUC__) && defined(__i386__) || \
15290bcde94Sda73024 	defined(_WIN32) && defined(_M_IX86) && \
15390bcde94Sda73024 	!(defined(_WIN64) || defined(_WIN32_WCE) || \
15490bcde94Sda73024 	defined(_MSC_VER) && (_MSC_VER <= 800))
15590bcde94Sda73024 #define	VIA_ACE_POSSIBLE
15690bcde94Sda73024 #endif
15790bcde94Sda73024 
15890bcde94Sda73024 /*
15990bcde94Sda73024  *  Define this option if support for the VIA ACE is required. This uses
16090bcde94Sda73024  *  inline assembler instructions and is only implemented for the Microsoft,
16190bcde94Sda73024  *  Intel and GCC compilers.  If VIA ACE is known to be present, then defining
16290bcde94Sda73024  *  ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption
16390bcde94Sda73024  *  code.  If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if
16490bcde94Sda73024  *  it is detected (both present and enabled) but the normal AES code will
16590bcde94Sda73024  *  also be present.
16690bcde94Sda73024  *
16790bcde94Sda73024  *  When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte
16890bcde94Sda73024  *  aligned; other input/output buffers do not need to be 16 byte aligned
16990bcde94Sda73024  *  but there are very large performance gains if this can be arranged.
17090bcde94Sda73024  *  VIA ACE also requires the decryption key schedule to be in reverse
17190bcde94Sda73024  *  order (which later checks below ensure).
17290bcde94Sda73024  */
17390bcde94Sda73024 
17490bcde94Sda73024 /*  VIA ACE is not used here for OpenSolaris: */
17590bcde94Sda73024 #undef	VIA_ACE_POSSIBLE
17690bcde94Sda73024 #undef	ASSUME_VIA_ACE_PRESENT
17790bcde94Sda73024 
17890bcde94Sda73024 #if 0 && defined(VIA_ACE_POSSIBLE) && !defined(USE_VIA_ACE_IF_PRESENT)
17990bcde94Sda73024 #define	USE_VIA_ACE_IF_PRESENT
18090bcde94Sda73024 #endif
18190bcde94Sda73024 
18290bcde94Sda73024 #if 0 && defined(VIA_ACE_POSSIBLE) && !defined(ASSUME_VIA_ACE_PRESENT)
18390bcde94Sda73024 #define	ASSUME_VIA_ACE_PRESENT
18490bcde94Sda73024 #endif
18590bcde94Sda73024 
18690bcde94Sda73024 
18790bcde94Sda73024 /*
18890bcde94Sda73024  *  3. ASSEMBLER SUPPORT
18990bcde94Sda73024  *
19090bcde94Sda73024  *	This define (which can be on the command line) enables the use of the
19190bcde94Sda73024  *	assembler code routines for encryption, decryption and key scheduling
19290bcde94Sda73024  *	as follows:
19390bcde94Sda73024  *
19490bcde94Sda73024  *	ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for
19590bcde94Sda73024  *		encryption and decryption and but with key scheduling in C
19690bcde94Sda73024  *	ASM_X86_V2  uses assembler (aes_x86_v2.asm) with compressed tables for
19790bcde94Sda73024  *		encryption, decryption and key scheduling
19890bcde94Sda73024  *	ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for
19990bcde94Sda73024  *		encryption and decryption and but with key scheduling in C
20090bcde94Sda73024  *	ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for
20190bcde94Sda73024  *		encryption and decryption and but with key scheduling in C
20290bcde94Sda73024  *
20390bcde94Sda73024  *	Change one 'if 0' below to 'if 1' to select the version or define
20490bcde94Sda73024  *	as a compilation option.
20590bcde94Sda73024  */
20690bcde94Sda73024 
20790bcde94Sda73024 #if 0 && !defined(ASM_X86_V1C)
20890bcde94Sda73024 #define	ASM_X86_V1C
20990bcde94Sda73024 #elif 0 && !defined(ASM_X86_V2)
21090bcde94Sda73024 #define	ASM_X86_V2
21190bcde94Sda73024 #elif 0 && !defined(ASM_X86_V2C)
21290bcde94Sda73024 #define	ASM_X86_V2C
21390bcde94Sda73024 #elif 1 && !defined(ASM_AMD64_C)
21490bcde94Sda73024 #define	ASM_AMD64_C
21590bcde94Sda73024 #endif
21690bcde94Sda73024 
21790bcde94Sda73024 #if (defined(ASM_X86_V1C) || defined(ASM_X86_V2) || defined(ASM_X86_V2C)) && \
21890bcde94Sda73024 	!defined(_M_IX86) || defined(ASM_AMD64_C) && !defined(_M_X64) && \
21990bcde94Sda73024 	!defined(__amd64)
22090bcde94Sda73024 #error Assembler code is only available for x86 and AMD64 systems
22190bcde94Sda73024 #endif
22290bcde94Sda73024 
22390bcde94Sda73024 /*
22490bcde94Sda73024  *  4. FAST INPUT/OUTPUT OPERATIONS.
22590bcde94Sda73024  *
22690bcde94Sda73024  *	On some machines it is possible to improve speed by transferring the
22790bcde94Sda73024  *	bytes in the input and output arrays to and from the internal 32-bit
22890bcde94Sda73024  *	variables by addressing these arrays as if they are arrays of 32-bit
22990bcde94Sda73024  *	words.  On some machines this will always be possible but there may
23090bcde94Sda73024  *	be a large performance penalty if the byte arrays are not aligned on
23190bcde94Sda73024  *	the normal word boundaries. On other machines this technique will
23290bcde94Sda73024  *	lead to memory access errors when such 32-bit word accesses are not
23390bcde94Sda73024  *	properly aligned. The option SAFE_IO avoids such problems but will
23490bcde94Sda73024  *	often be slower on those machines that support misaligned access
23590bcde94Sda73024  *	(especially so if care is taken to align the input  and output byte
23690bcde94Sda73024  *	arrays on 32-bit word boundaries). If SAFE_IO is not defined it is
23790bcde94Sda73024  *	assumed that access to byte arrays as if they are arrays of 32-bit
23890bcde94Sda73024  *	words will not cause problems when such accesses are misaligned.
23990bcde94Sda73024  */
24090bcde94Sda73024 #if 1 && !defined(_MSC_VER)
24190bcde94Sda73024 #define	SAFE_IO
24290bcde94Sda73024 #endif
24390bcde94Sda73024 
24490bcde94Sda73024 /*
24590bcde94Sda73024  *  5. LOOP UNROLLING
24690bcde94Sda73024  *
24790bcde94Sda73024  *	The code for encryption and decryption cycles through a number of rounds
24890bcde94Sda73024  *	that can be implemented either in a loop or by expanding the code into a
24990bcde94Sda73024  *	long sequence of instructions, the latter producing a larger program but
25090bcde94Sda73024  *	one that will often be much faster. The latter is called loop unrolling.
25190bcde94Sda73024  *	There are also potential speed advantages in expanding two iterations in
25290bcde94Sda73024  *	a loop with half the number of iterations, which is called partial loop
25390bcde94Sda73024  *	unrolling.  The following options allow partial or full loop unrolling
25490bcde94Sda73024  *	to be set independently for encryption and decryption
25590bcde94Sda73024  */
25690bcde94Sda73024 #if 1
25790bcde94Sda73024 #define	ENC_UNROLL  FULL
25890bcde94Sda73024 #elif 0
25990bcde94Sda73024 #define	ENC_UNROLL  PARTIAL
26090bcde94Sda73024 #else
26190bcde94Sda73024 #define	ENC_UNROLL  NONE
26290bcde94Sda73024 #endif
26390bcde94Sda73024 
26490bcde94Sda73024 #if 1
26590bcde94Sda73024 #define	DEC_UNROLL  FULL
26690bcde94Sda73024 #elif 0
26790bcde94Sda73024 #define	DEC_UNROLL  PARTIAL
26890bcde94Sda73024 #else
26990bcde94Sda73024 #define	DEC_UNROLL  NONE
27090bcde94Sda73024 #endif
27190bcde94Sda73024 
27290bcde94Sda73024 #if 1
27390bcde94Sda73024 #define	ENC_KS_UNROLL
27490bcde94Sda73024 #endif
27590bcde94Sda73024 
27690bcde94Sda73024 #if 1
27790bcde94Sda73024 #define	DEC_KS_UNROLL
27890bcde94Sda73024 #endif
27990bcde94Sda73024 
28090bcde94Sda73024 /*
28190bcde94Sda73024  *  6. FAST FINITE FIELD OPERATIONS
28290bcde94Sda73024  *
28390bcde94Sda73024  *	If this section is included, tables are used to provide faster finite
28490bcde94Sda73024  *	field arithmetic.  This has no effect if FIXED_TABLES is defined.
28590bcde94Sda73024  */
28690bcde94Sda73024 #if 1
28790bcde94Sda73024 #define	FF_TABLES
28890bcde94Sda73024 #endif
28990bcde94Sda73024 
29090bcde94Sda73024 /*
29190bcde94Sda73024  *  7. INTERNAL STATE VARIABLE FORMAT
29290bcde94Sda73024  *
29390bcde94Sda73024  *	The internal state of Rijndael is stored in a number of local 32-bit
29490bcde94Sda73024  *	word variables which can be defined either as an array or as individual
29590bcde94Sda73024  *	names variables. Include this section if you want to store these local
29690bcde94Sda73024  *	variables in arrays. Otherwise individual local variables will be used.
29790bcde94Sda73024  */
29890bcde94Sda73024 #if 1
29990bcde94Sda73024 #define	ARRAYS
30090bcde94Sda73024 #endif
30190bcde94Sda73024 
30290bcde94Sda73024 /*
30390bcde94Sda73024  *  8. FIXED OR DYNAMIC TABLES
30490bcde94Sda73024  *
30590bcde94Sda73024  *	When this section is included the tables used by the code are compiled
30690bcde94Sda73024  *	statically into the binary file.  Otherwise the subroutine aes_init()
30790bcde94Sda73024  *	must be called to compute them before the code is first used.
30890bcde94Sda73024  */
30990bcde94Sda73024 #if 1 && !(defined(_MSC_VER) && (_MSC_VER <= 800))
31090bcde94Sda73024 #define	FIXED_TABLES
31190bcde94Sda73024 #endif
31290bcde94Sda73024 
31390bcde94Sda73024 /*
31490bcde94Sda73024  *  9. MASKING OR CASTING FROM LONGER VALUES TO BYTES
31590bcde94Sda73024  *
31690bcde94Sda73024  *	In some systems it is better to mask longer values to extract bytes
31790bcde94Sda73024  *	rather than using a cast. This option allows this choice.
31890bcde94Sda73024  */
31990bcde94Sda73024 #if 0
32090bcde94Sda73024 #define	to_byte(x)  ((uint8_t)(x))
32190bcde94Sda73024 #else
32290bcde94Sda73024 #define	to_byte(x)  ((x) & 0xff)
32390bcde94Sda73024 #endif
32490bcde94Sda73024 
32590bcde94Sda73024 /*
32690bcde94Sda73024  *  10. TABLE ALIGNMENT
32790bcde94Sda73024  *
32890bcde94Sda73024  *	On some systems speed will be improved by aligning the AES large lookup
32990bcde94Sda73024  *	tables on particular boundaries. This define should be set to a power of
33090bcde94Sda73024  *	two giving the desired alignment. It can be left undefined if alignment
33190bcde94Sda73024  *	is not needed.  This option is specific to the Micrsoft VC++ compiler -
33290bcde94Sda73024  *	it seems to sometimes cause trouble for the VC++ version 6 compiler.
33390bcde94Sda73024  */
33490bcde94Sda73024 
33590bcde94Sda73024 #if 1 && defined(_MSC_VER) && (_MSC_VER >= 1300)
33690bcde94Sda73024 #define	TABLE_ALIGN 32
33790bcde94Sda73024 #endif
33890bcde94Sda73024 
33990bcde94Sda73024 /*
34090bcde94Sda73024  *  11.  REDUCE CODE AND TABLE SIZE
34190bcde94Sda73024  *
34290bcde94Sda73024  *	This replaces some expanded macros with function calls if AES_ASM_V2 or
34390bcde94Sda73024  *	AES_ASM_V2C are defined
34490bcde94Sda73024  */
34590bcde94Sda73024 
34690bcde94Sda73024 #if 1 && (defined(ASM_X86_V2) || defined(ASM_X86_V2C))
34790bcde94Sda73024 #define	REDUCE_CODE_SIZE
34890bcde94Sda73024 #endif
34990bcde94Sda73024 
35090bcde94Sda73024 /*
35190bcde94Sda73024  *  12. TABLE OPTIONS
35290bcde94Sda73024  *
35390bcde94Sda73024  *	This cipher proceeds by repeating in a number of cycles known as rounds
35490bcde94Sda73024  *	which are implemented by a round function which is optionally be speeded
35590bcde94Sda73024  *	up using tables.  The basic tables are 256 32-bit words, with either
35690bcde94Sda73024  *	one or four tables being required for each round function depending on
35790bcde94Sda73024  *	how much speed is required. Encryption and decryption round functions
35890bcde94Sda73024  *	are different and the last encryption and decryption round functions are
35990bcde94Sda73024  *	different again making four different round functions in all.
36090bcde94Sda73024  *
36190bcde94Sda73024  *	This means that:
36290bcde94Sda73024  *	1. Normal encryption and decryption rounds can each use either 0, 1
36390bcde94Sda73024  *		or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
36490bcde94Sda73024  *	2. The last encryption and decryption rounds can also use either 0, 1
36590bcde94Sda73024  *		or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
36690bcde94Sda73024  *
36790bcde94Sda73024  *	Include or exclude the appropriate definitions below to set the number
36890bcde94Sda73024  *	of tables used by this implementation.
36990bcde94Sda73024  */
37090bcde94Sda73024 
37190bcde94Sda73024 #if 1   /* set tables for the normal encryption round */
37290bcde94Sda73024 #define	ENC_ROUND   FOUR_TABLES
37390bcde94Sda73024 #elif 0
37490bcde94Sda73024 #define	ENC_ROUND   ONE_TABLE
37590bcde94Sda73024 #else
37690bcde94Sda73024 #define	ENC_ROUND   NO_TABLES
37790bcde94Sda73024 #endif
37890bcde94Sda73024 
37990bcde94Sda73024 #if 1   /* set tables for the last encryption round */
38090bcde94Sda73024 #define	LAST_ENC_ROUND  FOUR_TABLES
38190bcde94Sda73024 #elif 0
38290bcde94Sda73024 #define	LAST_ENC_ROUND  ONE_TABLE
38390bcde94Sda73024 #else
38490bcde94Sda73024 #define	LAST_ENC_ROUND  NO_TABLES
38590bcde94Sda73024 #endif
38690bcde94Sda73024 
38790bcde94Sda73024 #if 1   /* set tables for the normal decryption round */
38890bcde94Sda73024 #define	DEC_ROUND   FOUR_TABLES
38990bcde94Sda73024 #elif 0
39090bcde94Sda73024 #define	DEC_ROUND   ONE_TABLE
39190bcde94Sda73024 #else
39290bcde94Sda73024 #define	DEC_ROUND   NO_TABLES
39390bcde94Sda73024 #endif
39490bcde94Sda73024 
39590bcde94Sda73024 #if 1   /* set tables for the last decryption round */
39690bcde94Sda73024 #define	LAST_DEC_ROUND  FOUR_TABLES
39790bcde94Sda73024 #elif 0
39890bcde94Sda73024 #define	LAST_DEC_ROUND  ONE_TABLE
39990bcde94Sda73024 #else
40090bcde94Sda73024 #define	LAST_DEC_ROUND  NO_TABLES
40190bcde94Sda73024 #endif
40290bcde94Sda73024 
40390bcde94Sda73024 /*
40490bcde94Sda73024  *  The decryption key schedule can be speeded up with tables in the same
40590bcde94Sda73024  *	way that the round functions can.  Include or exclude the following
40690bcde94Sda73024  *	defines to set this requirement.
40790bcde94Sda73024  */
40890bcde94Sda73024 #if 1
40990bcde94Sda73024 #define	KEY_SCHED   FOUR_TABLES
41090bcde94Sda73024 #elif 0
41190bcde94Sda73024 #define	KEY_SCHED   ONE_TABLE
41290bcde94Sda73024 #else
41390bcde94Sda73024 #define	KEY_SCHED   NO_TABLES
41490bcde94Sda73024 #endif
41590bcde94Sda73024 
41690bcde94Sda73024 /*  ---- END OF USER CONFIGURED OPTIONS ---- */
41790bcde94Sda73024 
41890bcde94Sda73024 /* VIA ACE support is only available for VC++ and GCC */
41990bcde94Sda73024 
42090bcde94Sda73024 #if !defined(_MSC_VER) && !defined(__GNUC__)
42190bcde94Sda73024 #if defined(ASSUME_VIA_ACE_PRESENT)
42290bcde94Sda73024 #undef ASSUME_VIA_ACE_PRESENT
42390bcde94Sda73024 #endif
42490bcde94Sda73024 #if defined(USE_VIA_ACE_IF_PRESENT)
42590bcde94Sda73024 #undef USE_VIA_ACE_IF_PRESENT
42690bcde94Sda73024 #endif
42790bcde94Sda73024 #endif
42890bcde94Sda73024 
42990bcde94Sda73024 #if defined(ASSUME_VIA_ACE_PRESENT) && !defined(USE_VIA_ACE_IF_PRESENT)
43090bcde94Sda73024 #define	USE_VIA_ACE_IF_PRESENT
43190bcde94Sda73024 #endif
43290bcde94Sda73024 
43390bcde94Sda73024 #if defined(USE_VIA_ACE_IF_PRESENT) && !defined(AES_REV_DKS)
43490bcde94Sda73024 #define	AES_REV_DKS
43590bcde94Sda73024 #endif
43690bcde94Sda73024 
43790bcde94Sda73024 /* Assembler support requires the use of platform byte order */
43890bcde94Sda73024 
43990bcde94Sda73024 #if (defined(ASM_X86_V1C) || defined(ASM_X86_V2C) || defined(ASM_AMD64_C)) && \
44090bcde94Sda73024 	(ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)
44190bcde94Sda73024 #undef  ALGORITHM_BYTE_ORDER
44290bcde94Sda73024 #define	ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
44390bcde94Sda73024 #endif
44490bcde94Sda73024 
44590bcde94Sda73024 /*
44690bcde94Sda73024  * In this implementation the columns of the state array are each held in
44790bcde94Sda73024  *	32-bit words. The state array can be held in various ways: in an array
44890bcde94Sda73024  *	of words, in a number of individual word variables or in a number of
44990bcde94Sda73024  *	processor registers. The following define maps a variable name x and
45090bcde94Sda73024  *	a column number c to the way the state array variable is to be held.
45190bcde94Sda73024  *	The first define below maps the state into an array x[c] whereas the
45290bcde94Sda73024  *	second form maps the state into a number of individual variables x0,
45390bcde94Sda73024  *	x1, etc.  Another form could map individual state columns to machine
45490bcde94Sda73024  *	register names.
45590bcde94Sda73024  */
45690bcde94Sda73024 
45790bcde94Sda73024 #if defined(ARRAYS)
45890bcde94Sda73024 #define	s(x, c) x[c]
45990bcde94Sda73024 #else
46090bcde94Sda73024 #define	s(x, c) x##c
46190bcde94Sda73024 #endif
46290bcde94Sda73024 
46390bcde94Sda73024 /*
46490bcde94Sda73024  *  This implementation provides subroutines for encryption, decryption
46590bcde94Sda73024  *	and for setting the three key lengths (separately) for encryption
46690bcde94Sda73024  *	and decryption. Since not all functions are needed, masks are set
46790bcde94Sda73024  *	up here to determine which will be implemented in C
46890bcde94Sda73024  */
46990bcde94Sda73024 
47090bcde94Sda73024 #if !defined(AES_ENCRYPT)
47190bcde94Sda73024 #define	EFUNCS_IN_C   0
47290bcde94Sda73024 #elif defined(ASSUME_VIA_ACE_PRESENT) || defined(ASM_X86_V1C) || \
47390bcde94Sda73024 	defined(ASM_X86_V2C) || defined(ASM_AMD64_C)
47490bcde94Sda73024 #define	EFUNCS_IN_C   ENC_KEYING_IN_C
47590bcde94Sda73024 #elif !defined(ASM_X86_V2)
47690bcde94Sda73024 #define	EFUNCS_IN_C   (ENCRYPTION_IN_C | ENC_KEYING_IN_C)
47790bcde94Sda73024 #else
47890bcde94Sda73024 #define	EFUNCS_IN_C   0
47990bcde94Sda73024 #endif
48090bcde94Sda73024 
48190bcde94Sda73024 #if !defined(AES_DECRYPT)
48290bcde94Sda73024 #define	DFUNCS_IN_C   0
48390bcde94Sda73024 #elif defined(ASSUME_VIA_ACE_PRESENT) || defined(ASM_X86_V1C) || \
48490bcde94Sda73024 	defined(ASM_X86_V2C) || defined(ASM_AMD64_C)
48590bcde94Sda73024 #define	DFUNCS_IN_C   DEC_KEYING_IN_C
48690bcde94Sda73024 #elif !defined(ASM_X86_V2)
48790bcde94Sda73024 #define	DFUNCS_IN_C   (DECRYPTION_IN_C | DEC_KEYING_IN_C)
48890bcde94Sda73024 #else
48990bcde94Sda73024 #define	DFUNCS_IN_C   0
49090bcde94Sda73024 #endif
49190bcde94Sda73024 
49290bcde94Sda73024 #define	FUNCS_IN_C  (EFUNCS_IN_C | DFUNCS_IN_C)
49390bcde94Sda73024 
49490bcde94Sda73024 /* END OF CONFIGURATION OPTIONS */
49590bcde94Sda73024 
49690bcde94Sda73024 /* Disable or report errors on some combinations of options */
49790bcde94Sda73024 
49890bcde94Sda73024 #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
49990bcde94Sda73024 #undef  LAST_ENC_ROUND
50090bcde94Sda73024 #define	LAST_ENC_ROUND  NO_TABLES
50190bcde94Sda73024 #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
50290bcde94Sda73024 #undef  LAST_ENC_ROUND
50390bcde94Sda73024 #define	LAST_ENC_ROUND  ONE_TABLE
50490bcde94Sda73024 #endif
50590bcde94Sda73024 
50690bcde94Sda73024 #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
50790bcde94Sda73024 #undef  ENC_UNROLL
50890bcde94Sda73024 #define	ENC_UNROLL  NONE
50990bcde94Sda73024 #endif
51090bcde94Sda73024 
51190bcde94Sda73024 #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
51290bcde94Sda73024 #undef  LAST_DEC_ROUND
51390bcde94Sda73024 #define	LAST_DEC_ROUND  NO_TABLES
51490bcde94Sda73024 #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
51590bcde94Sda73024 #undef  LAST_DEC_ROUND
51690bcde94Sda73024 #define	LAST_DEC_ROUND  ONE_TABLE
51790bcde94Sda73024 #endif
51890bcde94Sda73024 
51990bcde94Sda73024 #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
52090bcde94Sda73024 #undef  DEC_UNROLL
52190bcde94Sda73024 #define	DEC_UNROLL  NONE
52290bcde94Sda73024 #endif
52390bcde94Sda73024 
524*4b56a003SDaniel Anderson #if (ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN)
525*4b56a003SDaniel Anderson #define	aes_sw32	htonl
526*4b56a003SDaniel Anderson #elif defined(bswap32)
52790bcde94Sda73024 #define	aes_sw32	bswap32
52890bcde94Sda73024 #elif defined(bswap_32)
52990bcde94Sda73024 #define	aes_sw32	bswap_32
53090bcde94Sda73024 #else
531*4b56a003SDaniel Anderson #define	brot(x, n)  (((uint32_t)(x) << (n)) | ((uint32_t)(x) >> (32 - (n))))
53290bcde94Sda73024 #define	aes_sw32(x) ((brot((x), 8) & 0x00ff00ff) | (brot((x), 24) & 0xff00ff00))
53390bcde94Sda73024 #endif
53490bcde94Sda73024 
535*4b56a003SDaniel Anderson 
53690bcde94Sda73024 /*
53790bcde94Sda73024  *	upr(x, n):  rotates bytes within words by n positions, moving bytes to
53890bcde94Sda73024  *		higher index positions with wrap around into low positions
53990bcde94Sda73024  *	ups(x, n):  moves bytes by n positions to higher index positions in
54090bcde94Sda73024  *		words but without wrap around
54190bcde94Sda73024  *	bval(x, n): extracts a byte from a word
54290bcde94Sda73024  *
54390bcde94Sda73024  *	WARNING:   The definitions given here are intended only for use with
54490bcde94Sda73024  *		unsigned variables and with shift counts that are compile
54590bcde94Sda73024  *		time constants
54690bcde94Sda73024  */
54790bcde94Sda73024 
54890bcde94Sda73024 #if (ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN)
54990bcde94Sda73024 #define	upr(x, n)	(((uint32_t)(x) << (8 * (n))) | \
55090bcde94Sda73024 			((uint32_t)(x) >> (32 - 8 * (n))))
55190bcde94Sda73024 #define	ups(x, n)	((uint32_t)(x) << (8 * (n)))
55290bcde94Sda73024 #define	bval(x, n)	to_byte((x) >> (8 * (n)))
55390bcde94Sda73024 #define	bytes2word(b0, b1, b2, b3)  \
55490bcde94Sda73024 		(((uint32_t)(b3) << 24) | ((uint32_t)(b2) << 16) | \
55590bcde94Sda73024 		((uint32_t)(b1) << 8) | (b0))
55690bcde94Sda73024 #endif
55790bcde94Sda73024 
55890bcde94Sda73024 #if (ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN)
55990bcde94Sda73024 #define	upr(x, n)	(((uint32_t)(x) >> (8 * (n))) | \
56090bcde94Sda73024 			((uint32_t)(x) << (32 - 8 * (n))))
56190bcde94Sda73024 #define	ups(x, n)	((uint32_t)(x) >> (8 * (n)))
56290bcde94Sda73024 #define	bval(x, n)	to_byte((x) >> (24 - 8 * (n)))
56390bcde94Sda73024 #define	bytes2word(b0, b1, b2, b3)  \
56490bcde94Sda73024 		(((uint32_t)(b0) << 24) | ((uint32_t)(b1) << 16) | \
56590bcde94Sda73024 		((uint32_t)(b2) << 8) | (b3))
56690bcde94Sda73024 #endif
56790bcde94Sda73024 
56890bcde94Sda73024 #if defined(SAFE_IO)
56990bcde94Sda73024 #define	word_in(x, c)	bytes2word(((const uint8_t *)(x) + 4 * c)[0], \
57090bcde94Sda73024 				((const uint8_t *)(x) + 4 * c)[1], \
57190bcde94Sda73024 				((const uint8_t *)(x) + 4 * c)[2], \
57290bcde94Sda73024 				((const uint8_t *)(x) + 4 * c)[3])
57390bcde94Sda73024 #define	word_out(x, c, v) { ((uint8_t *)(x) + 4 * c)[0] = bval(v, 0); \
57490bcde94Sda73024 			((uint8_t *)(x) + 4 * c)[1] = bval(v, 1); \
57590bcde94Sda73024 			((uint8_t *)(x) + 4 * c)[2] = bval(v, 2); \
57690bcde94Sda73024 			((uint8_t *)(x) + 4 * c)[3] = bval(v, 3); }
57790bcde94Sda73024 #elif (ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER)
57890bcde94Sda73024 #define	word_in(x, c)	(*((uint32_t *)(x) + (c)))
57990bcde94Sda73024 #define	word_out(x, c, v) (*((uint32_t *)(x) + (c)) = (v))
58090bcde94Sda73024 #else
58190bcde94Sda73024 #define	word_in(x, c)	aes_sw32(*((uint32_t *)(x) + (c)))
58290bcde94Sda73024 #define	word_out(x, c, v) (*((uint32_t *)(x) + (c)) = aes_sw32(v))
58390bcde94Sda73024 #endif
58490bcde94Sda73024 
58590bcde94Sda73024 /* the finite field modular polynomial and elements */
58690bcde94Sda73024 
58790bcde94Sda73024 #define	WPOLY   0x011b
58890bcde94Sda73024 #define	BPOLY	0x1b
58990bcde94Sda73024 
59090bcde94Sda73024 /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
59190bcde94Sda73024 
59290bcde94Sda73024 #define	m1  0x80808080
59390bcde94Sda73024 #define	m2  0x7f7f7f7f
59490bcde94Sda73024 #define	gf_mulx(x)  ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
59590bcde94Sda73024 
59690bcde94Sda73024 /*
59790bcde94Sda73024  * The following defines provide alternative definitions of gf_mulx that might
59890bcde94Sda73024  * give improved performance if a fast 32-bit multiply is not available. Note
59990bcde94Sda73024  * that a temporary variable u needs to be defined where gf_mulx is used.
60090bcde94Sda73024  *
60190bcde94Sda73024  * #define	gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ \
60290bcde94Sda73024  *			((u >> 3) | (u >> 6))
60390bcde94Sda73024  * #define	m4  (0x01010101 * BPOLY)
60490bcde94Sda73024  * #define	gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) \
60590bcde94Sda73024  *			& m4)
60690bcde94Sda73024  */
60790bcde94Sda73024 
60890bcde94Sda73024 /* Work out which tables are needed for the different options   */
60990bcde94Sda73024 
61090bcde94Sda73024 #if defined(ASM_X86_V1C)
61190bcde94Sda73024 #if defined(ENC_ROUND)
61290bcde94Sda73024 #undef  ENC_ROUND
61390bcde94Sda73024 #endif
61490bcde94Sda73024 #define	ENC_ROUND   FOUR_TABLES
61590bcde94Sda73024 #if defined(LAST_ENC_ROUND)
61690bcde94Sda73024 #undef  LAST_ENC_ROUND
61790bcde94Sda73024 #endif
61890bcde94Sda73024 #define	LAST_ENC_ROUND  FOUR_TABLES
61990bcde94Sda73024 #if defined(DEC_ROUND)
62090bcde94Sda73024 #undef  DEC_ROUND
62190bcde94Sda73024 #endif
62290bcde94Sda73024 #define	DEC_ROUND   FOUR_TABLES
62390bcde94Sda73024 #if defined(LAST_DEC_ROUND)
62490bcde94Sda73024 #undef  LAST_DEC_ROUND
62590bcde94Sda73024 #endif
62690bcde94Sda73024 #define	LAST_DEC_ROUND  FOUR_TABLES
62790bcde94Sda73024 #if defined(KEY_SCHED)
62890bcde94Sda73024 #undef  KEY_SCHED
62990bcde94Sda73024 #define	KEY_SCHED   FOUR_TABLES
63090bcde94Sda73024 #endif
63190bcde94Sda73024 #endif
63290bcde94Sda73024 
63390bcde94Sda73024 #if (FUNCS_IN_C & ENCRYPTION_IN_C) || defined(ASM_X86_V1C)
63490bcde94Sda73024 #if ENC_ROUND == ONE_TABLE
63590bcde94Sda73024 #define	FT1_SET
63690bcde94Sda73024 #elif ENC_ROUND == FOUR_TABLES
63790bcde94Sda73024 #define	FT4_SET
63890bcde94Sda73024 #else
63990bcde94Sda73024 #define	SBX_SET
64090bcde94Sda73024 #endif
64190bcde94Sda73024 #if LAST_ENC_ROUND == ONE_TABLE
64290bcde94Sda73024 #define	FL1_SET
64390bcde94Sda73024 #elif LAST_ENC_ROUND == FOUR_TABLES
64490bcde94Sda73024 #define	FL4_SET
64590bcde94Sda73024 #elif !defined(SBX_SET)
64690bcde94Sda73024 #define	SBX_SET
64790bcde94Sda73024 #endif
64890bcde94Sda73024 #endif
64990bcde94Sda73024 
65090bcde94Sda73024 #if (FUNCS_IN_C & DECRYPTION_IN_C) || defined(ASM_X86_V1C)
65190bcde94Sda73024 #if DEC_ROUND == ONE_TABLE
65290bcde94Sda73024 #define	IT1_SET
65390bcde94Sda73024 #elif DEC_ROUND == FOUR_TABLES
65490bcde94Sda73024 #define	IT4_SET
65590bcde94Sda73024 #else
65690bcde94Sda73024 #define	ISB_SET
65790bcde94Sda73024 #endif
65890bcde94Sda73024 #if LAST_DEC_ROUND == ONE_TABLE
65990bcde94Sda73024 #define	IL1_SET
66090bcde94Sda73024 #elif LAST_DEC_ROUND == FOUR_TABLES
66190bcde94Sda73024 #define	IL4_SET
66290bcde94Sda73024 #elif !defined(ISB_SET)
66390bcde94Sda73024 #define	ISB_SET
66490bcde94Sda73024 #endif
66590bcde94Sda73024 #endif
66690bcde94Sda73024 
66790bcde94Sda73024 
66890bcde94Sda73024 #if !(defined(REDUCE_CODE_SIZE) && (defined(ASM_X86_V2) || \
66990bcde94Sda73024 	defined(ASM_X86_V2C)))
67090bcde94Sda73024 #if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))
67190bcde94Sda73024 #if KEY_SCHED == ONE_TABLE
67290bcde94Sda73024 #if !defined(FL1_SET) && !defined(FL4_SET)
67390bcde94Sda73024 #define	LS1_SET
67490bcde94Sda73024 #endif
67590bcde94Sda73024 #elif KEY_SCHED == FOUR_TABLES
67690bcde94Sda73024 #if !defined(FL4_SET)
67790bcde94Sda73024 #define	LS4_SET
67890bcde94Sda73024 #endif
67990bcde94Sda73024 #elif !defined(SBX_SET)
68090bcde94Sda73024 #define	SBX_SET
68190bcde94Sda73024 #endif
68290bcde94Sda73024 #endif
68390bcde94Sda73024 #if (FUNCS_IN_C & DEC_KEYING_IN_C)
68490bcde94Sda73024 #if KEY_SCHED == ONE_TABLE
68590bcde94Sda73024 #define	IM1_SET
68690bcde94Sda73024 #elif KEY_SCHED == FOUR_TABLES
68790bcde94Sda73024 #define	IM4_SET
68890bcde94Sda73024 #elif !defined(SBX_SET)
68990bcde94Sda73024 #define	SBX_SET
69090bcde94Sda73024 #endif
69190bcde94Sda73024 #endif
69290bcde94Sda73024 #endif
69390bcde94Sda73024 
69490bcde94Sda73024 /* generic definitions of Rijndael macros that use tables */
69590bcde94Sda73024 
69690bcde94Sda73024 #define	no_table(x, box, vf, rf, c) bytes2word(\
69790bcde94Sda73024 	box[bval(vf(x, 0, c), rf(0, c))], \
69890bcde94Sda73024 	box[bval(vf(x, 1, c), rf(1, c))], \
69990bcde94Sda73024 	box[bval(vf(x, 2, c), rf(2, c))], \
70090bcde94Sda73024 	box[bval(vf(x, 3, c), rf(3, c))])
70190bcde94Sda73024 
70290bcde94Sda73024 #define	one_table(x, op, tab, vf, rf, c) \
70390bcde94Sda73024 	(tab[bval(vf(x, 0, c), rf(0, c))] \
70490bcde94Sda73024 	^ op(tab[bval(vf(x, 1, c), rf(1, c))], 1) \
70590bcde94Sda73024 	^ op(tab[bval(vf(x, 2, c), rf(2, c))], 2) \
70690bcde94Sda73024 	^ op(tab[bval(vf(x, 3, c), rf(3, c))], 3))
70790bcde94Sda73024 
70890bcde94Sda73024 #define	four_tables(x, tab, vf, rf, c) \
70990bcde94Sda73024 	(tab[0][bval(vf(x, 0, c), rf(0, c))] \
71090bcde94Sda73024 	^ tab[1][bval(vf(x, 1, c), rf(1, c))] \
71190bcde94Sda73024 	^ tab[2][bval(vf(x, 2, c), rf(2, c))] \
71290bcde94Sda73024 	^ tab[3][bval(vf(x, 3, c), rf(3, c))])
71390bcde94Sda73024 
71490bcde94Sda73024 #define	vf1(x, r, c)	(x)
71590bcde94Sda73024 #define	rf1(r, c)	(r)
71690bcde94Sda73024 #define	rf2(r, c)	((8+r-c)&3)
71790bcde94Sda73024 
71890bcde94Sda73024 /*
71990bcde94Sda73024  * Perform forward and inverse column mix operation on four bytes in long word
72090bcde94Sda73024  * x in parallel. NOTE: x must be a simple variable, NOT an expression in
72190bcde94Sda73024  * these macros.
72290bcde94Sda73024  */
72390bcde94Sda73024 
72490bcde94Sda73024 #if !(defined(REDUCE_CODE_SIZE) && (defined(ASM_X86_V2) || \
72590bcde94Sda73024 	defined(ASM_X86_V2C)))
72690bcde94Sda73024 
72790bcde94Sda73024 #if defined(FM4_SET)	/* not currently used */
72890bcde94Sda73024 #define	fwd_mcol(x)	four_tables(x, t_use(f, m), vf1, rf1, 0)
72990bcde94Sda73024 #elif defined(FM1_SET)	/* not currently used */
73090bcde94Sda73024 #define	fwd_mcol(x)	one_table(x, upr, t_use(f, m), vf1, rf1, 0)
73190bcde94Sda73024 #else
73290bcde94Sda73024 #define	dec_fmvars	uint32_t g2
73390bcde94Sda73024 #define	fwd_mcol(x)	(g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ \
73490bcde94Sda73024 				upr((x), 2) ^ upr((x), 1))
73590bcde94Sda73024 #endif
73690bcde94Sda73024 
73790bcde94Sda73024 #if defined(IM4_SET)
73890bcde94Sda73024 #define	inv_mcol(x)	four_tables(x, t_use(i, m), vf1, rf1, 0)
73990bcde94Sda73024 #elif defined(IM1_SET)
74090bcde94Sda73024 #define	inv_mcol(x)	one_table(x, upr, t_use(i, m), vf1, rf1, 0)
74190bcde94Sda73024 #else
74290bcde94Sda73024 #define	dec_imvars	uint32_t g2, g4, g9
74390bcde94Sda73024 #define	inv_mcol(x)	(g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = \
74490bcde94Sda73024 				(x) ^ gf_mulx(g4), g4 ^= g9, \
74590bcde94Sda73024 				(x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ \
74690bcde94Sda73024 				upr(g4, 2) ^ upr(g9, 1))
74790bcde94Sda73024 #endif
74890bcde94Sda73024 
74990bcde94Sda73024 #if defined(FL4_SET)
75090bcde94Sda73024 #define	ls_box(x, c)	four_tables(x, t_use(f, l), vf1, rf2, c)
75190bcde94Sda73024 #elif defined(LS4_SET)
75290bcde94Sda73024 #define	ls_box(x, c)	four_tables(x, t_use(l, s), vf1, rf2, c)
75390bcde94Sda73024 #elif defined(FL1_SET)
75490bcde94Sda73024 #define	ls_box(x, c)	one_table(x, upr, t_use(f, l), vf1, rf2, c)
75590bcde94Sda73024 #elif defined(LS1_SET)
75690bcde94Sda73024 #define	ls_box(x, c)	one_table(x, upr, t_use(l, s), vf1, rf2, c)
75790bcde94Sda73024 #else
75890bcde94Sda73024 #define	ls_box(x, c)	no_table(x, t_use(s, box), vf1, rf2, c)
75990bcde94Sda73024 #endif
76090bcde94Sda73024 
76190bcde94Sda73024 #endif
76290bcde94Sda73024 
76390bcde94Sda73024 #if defined(ASM_X86_V1C) && defined(AES_DECRYPT) && !defined(ISB_SET)
76490bcde94Sda73024 #define	ISB_SET
76590bcde94Sda73024 #endif
76690bcde94Sda73024 
76790bcde94Sda73024 #ifdef	__cplusplus
76890bcde94Sda73024 }
76990bcde94Sda73024 #endif
77090bcde94Sda73024 
77190bcde94Sda73024 #endif	/* _AESOPT_H */
772