aesopt.h 27 KB

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