aesopt.h 27 KB

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