aesopt.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  4. All rights reserved.
  5. LICENSE TERMS
  6. The free distribution and use of this software in both source and binary
  7. form is allowed (with or without changes) provided that:
  8. 1. distributions of this source code include the above copyright
  9. notice, this list of conditions and the following disclaimer;
  10. 2. distributions in binary form include the above copyright
  11. notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other associated materials;
  13. 3. the copyright holder's name is not used to endorse products
  14. built using this software without specific written permission.
  15. ALTERNATIVELY, provided that this notice is retained in full, this product
  16. may be distributed under the terms of the GNU General Public License (GPL),
  17. in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. DISCLAIMER
  19. This software is provided 'as is' with no explicit or implied warranties
  20. in respect of its properties, including, but not limited to, correctness
  21. and/or fitness for purpose.
  22. ---------------------------------------------------------------------------
  23. Issue Date: 26/08/2003
  24. My thanks go to Dag Arne Osvik for devising the schemes used here for key
  25. length derivation from the form of the key schedule
  26. This file contains the compilation options for AES (Rijndael) and code
  27. that is common across encryption, key scheduling and table generation.
  28. OPERATION
  29. These source code files implement the AES algorithm Rijndael designed by
  30. Joan Daemen and Vincent Rijmen. This version is designed for the standard
  31. block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
  32. and 32 bytes).
  33. This version is designed for flexibility and speed using operations on
  34. 32-bit words rather than operations on bytes. It can be compiled with
  35. either big or little endian internal byte order but is faster when the
  36. native byte order for the processor is used.
  37. THE CIPHER INTERFACE
  38. The cipher interface is implemented as an array of bytes in which lower
  39. AES bit sequence indexes map to higher numeric significance within bytes.
  40. aes_08t (an unsigned 8-bit type)
  41. aes_32t (an unsigned 32-bit type)
  42. struct aes_encrypt_ctx (structure for the cipher encryption context)
  43. struct aes_decrypt_ctx (structure for the cipher decryption context)
  44. aes_rval the function return type
  45. C subroutine calls:
  46. aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1]);
  47. aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1]);
  48. aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1]);
  49. aes_rval aes_encrypt(const void *in_blk,
  50. void *out_blk, const aes_encrypt_ctx cx[1]);
  51. aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1]);
  52. aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1]);
  53. aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1]);
  54. aes_rval aes_decrypt(const void *in_blk,
  55. void *out_blk, const aes_decrypt_ctx cx[1]);
  56. IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that
  57. you call genTabs() before AES is used so that the tables are initialised.
  58. C++ aes class subroutines:
  59. Class AESencrypt for encryption
  60. Construtors:
  61. AESencrypt(void)
  62. AESencrypt(const void *in_key) - 128 bit key
  63. Members:
  64. void key128(const void *in_key)
  65. void key192(const void *in_key)
  66. void key256(const void *in_key)
  67. void encrypt(const void *in_blk, void *out_blk) const
  68. Class AESdecrypt for encryption
  69. Construtors:
  70. AESdecrypt(void)
  71. AESdecrypt(const void *in_key) - 128 bit key
  72. Members:
  73. void key128(const void *in_key)
  74. void key192(const void *in_key)
  75. void key256(const void *in_key)
  76. void decrypt(const void *in_blk, void *out_blk) const
  77. COMPILATION
  78. The files used to provide AES (Rijndael) are
  79. a. aes.h for the definitions needed for use in C.
  80. b. aescpp.h for the definitions needed for use in C++.
  81. c. aesopt.h for setting compilation options (also includes common code).
  82. d. aescrypt.c for encryption and decrytpion, or
  83. e. aeskey.c for key scheduling.
  84. f. aestab.c for table loading or generation.
  85. g. aescrypt.asm for encryption and decryption using assembler code.
  86. h. aescrypt.mmx.asm for encryption and decryption using MMX assembler.
  87. To compile AES (Rijndael) for use in C code use aes.h and set the
  88. defines here for the facilities you need (key lengths, encryption
  89. and/or decryption). Do not define AES_DLL or AES_CPP. Set the options
  90. for optimisations and table sizes here.
  91. To compile AES (Rijndael) for use in in C++ code use aescpp.h but do
  92. not define AES_DLL
  93. To compile AES (Rijndael) in C as a Dynamic Link Library DLL) use
  94. aes.h and include the AES_DLL define.
  95. CONFIGURATION OPTIONS (here and in aes.h)
  96. a. set AES_DLL in aes.h if AES (Rijndael) is to be compiled as a DLL
  97. b. You may need to set PLATFORM_BYTE_ORDER to define the byte order.
  98. c. If you want the code to run in a specific internal byte order, then
  99. ALGORITHM_BYTE_ORDER must be set accordingly.
  100. d. set other configuration options decribed below.
  101. */
  102. #ifndef _AESOPT_H
  103. #define _AESOPT_H
  104. #include <asterisk/aes.h>
  105. /* CONFIGURATION - USE OF DEFINES
  106. Later in this section there are a number of defines that control the
  107. operation of the code. In each section, the purpose of each define is
  108. explained so that the relevant form can be included or excluded by
  109. setting either 1's or 0's respectively on the branches of the related
  110. #if clauses.
  111. */
  112. /* PLATFORM SPECIFIC INCLUDES */
  113. #if defined( __OpenBSD__ )
  114. # include <machine/types.h>
  115. # include <sys/endian.h>
  116. #elif defined( __FreeBSD__ ) || defined( __NetBSD__ )
  117. # include <sys/types.h>
  118. # include <sys/endian.h>
  119. #elif defined( BSD ) && ( BSD >= 199103 ) || defined(__APPLE__)
  120. # include <machine/endian.h>
  121. #elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
  122. # include <endian.h>
  123. #if !defined(__APPLE__)
  124. # include <byteswap.h>
  125. #endif
  126. #elif defined( linux )
  127. # include <endian.h>
  128. #endif
  129. /* BYTE ORDER IN 32-BIT WORDS
  130. To obtain the highest speed on processors with 32-bit words, this code
  131. needs to determine the byte order of the target machine. The following
  132. block of code is an attempt to capture the most obvious ways in which
  133. various environemnts define byte order. It may well fail, in which case
  134. the definitions will need to be set by editing at the points marked
  135. **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for
  136. some of these defines (from cryptlib).
  137. */
  138. #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
  139. #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
  140. #if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
  141. defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
  142. defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
  143. defined( vax ) || defined( vms ) || defined( VMS ) || \
  144. defined( __VMS )
  145. #define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  146. #endif
  147. #if defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
  148. defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
  149. defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
  150. defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
  151. defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
  152. defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )
  153. #define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  154. #endif
  155. /* if the platform is still not known, try to find its byte order */
  156. /* from commonly used definitions in the headers included earlier */
  157. #if !defined(PLATFORM_BYTE_ORDER)
  158. #if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
  159. # if defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
  160. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  161. # elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
  162. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  163. # elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
  164. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  165. # elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
  166. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  167. # endif
  168. #elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
  169. # if defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
  170. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  171. # elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
  172. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  173. # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
  174. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  175. # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
  176. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  177. # endif
  178. #elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)
  179. # if defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  180. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  181. # elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
  182. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  183. # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
  184. # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  185. # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
  186. # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  187. # endif
  188. #elif 0 /* **** EDIT HERE IF NECESSARY **** */
  189. #define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
  190. #elif 0 /* **** EDIT HERE IF NECESSARY **** */
  191. #define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
  192. #else
  193. #error Please edit aesopt.h (line 235 or 238) to set the platform byte order
  194. #endif
  195. #endif
  196. /* SOME LOCAL DEFINITIONS */
  197. #define NO_TABLES 0
  198. #define ONE_TABLE 1
  199. #define FOUR_TABLES 4
  200. #define NONE 0
  201. #define PARTIAL 1
  202. #define FULL 2
  203. #if defined(bswap32)
  204. #define aes_sw32 bswap32
  205. #elif defined(bswap_32)
  206. #define aes_sw32 bswap_32
  207. #else
  208. #define brot(x,n) (((aes_32t)(x) << n) | ((aes_32t)(x) >> (32 - n)))
  209. #define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
  210. #endif
  211. /* 1. FUNCTIONS REQUIRED
  212. This implementation provides subroutines for encryption, decryption
  213. and for setting the three key lengths (separately) for encryption
  214. and decryption. When the assembler code is not being used the following
  215. definition blocks allow the selection of the routines that are to be
  216. included in the compilation.
  217. */
  218. #ifdef AES_ENCRYPT
  219. #define ENCRYPTION
  220. #define ENCRYPTION_KEY_SCHEDULE
  221. #endif
  222. #ifdef AES_DECRYPT
  223. #define DECRYPTION
  224. #define DECRYPTION_KEY_SCHEDULE
  225. #endif
  226. /* 2. ASSEMBLER SUPPORT
  227. This define (which can be on the command line) enables the use of the
  228. assembler code routines for encryption and decryption with the C code
  229. only providing key scheduling
  230. */
  231. #if 0
  232. #define AES_ASM
  233. #endif
  234. /* 3. BYTE ORDER WITHIN 32 BIT WORDS
  235. The fundamental data processing units in Rijndael are 8-bit bytes. The
  236. input, output and key input are all enumerated arrays of bytes in which
  237. bytes are numbered starting at zero and increasing to one less than the
  238. number of bytes in the array in question. This enumeration is only used
  239. for naming bytes and does not imply any adjacency or order relationship
  240. from one byte to another. When these inputs and outputs are considered
  241. as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
  242. byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
  243. In this implementation bits are numbered from 0 to 7 starting at the
  244. numerically least significant end of each byte (bit n represents 2^n).
  245. However, Rijndael can be implemented more efficiently using 32-bit
  246. words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
  247. into word[n]. While in principle these bytes can be assembled into words
  248. in any positions, this implementation only supports the two formats in
  249. which bytes in adjacent positions within words also have adjacent byte
  250. numbers. This order is called big-endian if the lowest numbered bytes
  251. in words have the highest numeric significance and little-endian if the
  252. opposite applies.
  253. This code can work in either order irrespective of the order used by the
  254. machine on which it runs. Normally the internal byte order will be set
  255. to the order of the processor on which the code is to be run but this
  256. define can be used to reverse this in special situations
  257. NOTE: Assembler code versions rely on PLATFORM_BYTE_ORDER being set
  258. */
  259. #if 1 || defined(AES_ASM)
  260. #define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
  261. #elif 0
  262. #define ALGORITHM_BYTE_ORDER BRG_LITTLE_ENDIAN
  263. #elif 0
  264. #define ALGORITHM_BYTE_ORDER BRG_BIG_ENDIAN
  265. #else
  266. #error The algorithm byte order is not defined
  267. #endif
  268. /* 4. FAST INPUT/OUTPUT OPERATIONS.
  269. On some machines it is possible to improve speed by transferring the
  270. bytes in the input and output arrays to and from the internal 32-bit
  271. variables by addressing these arrays as if they are arrays of 32-bit
  272. words. On some machines this will always be possible but there may
  273. be a large performance penalty if the byte arrays are not aligned on
  274. the normal word boundaries. On other machines this technique will
  275. lead to memory access errors when such 32-bit word accesses are not
  276. properly aligned. The option SAFE_IO avoids such problems but will
  277. often be slower on those machines that support misaligned access
  278. (especially so if care is taken to align the input and output byte
  279. arrays on 32-bit word boundaries). If SAFE_IO is not defined it is
  280. assumed that access to byte arrays as if they are arrays of 32-bit
  281. words will not cause problems when such accesses are misaligned.
  282. */
  283. #if 1 && !defined(_MSC_VER)
  284. #define SAFE_IO
  285. #endif
  286. /* 5. LOOP UNROLLING
  287. The code for encryption and decrytpion cycles through a number of rounds
  288. that can be implemented either in a loop or by expanding the code into a
  289. long sequence of instructions, the latter producing a larger program but
  290. one that will often be much faster. The latter is called loop unrolling.
  291. There are also potential speed advantages in expanding two iterations in
  292. a loop with half the number of iterations, which is called partial loop
  293. unrolling. The following options allow partial or full loop unrolling
  294. to be set independently for encryption and decryption
  295. */
  296. #if 1
  297. #define ENC_UNROLL FULL
  298. #elif 0
  299. #define ENC_UNROLL PARTIAL
  300. #else
  301. #define ENC_UNROLL NONE
  302. #endif
  303. #if 1
  304. #define DEC_UNROLL FULL
  305. #elif 0
  306. #define DEC_UNROLL PARTIAL
  307. #else
  308. #define DEC_UNROLL NONE
  309. #endif
  310. /* 6. FAST FINITE FIELD OPERATIONS
  311. If this section is included, tables are used to provide faster finite
  312. field arithmetic (this has no effect if FIXED_TABLES is defined).
  313. */
  314. #if 1
  315. #define FF_TABLES
  316. #endif
  317. /* 7. INTERNAL STATE VARIABLE FORMAT
  318. The internal state of Rijndael is stored in a number of local 32-bit
  319. word varaibles which can be defined either as an array or as individual
  320. names variables. Include this section if you want to store these local
  321. varaibles in arrays. Otherwise individual local variables will be used.
  322. */
  323. #if 1
  324. #define ARRAYS
  325. #endif
  326. /* In this implementation the columns of the state array are each held in
  327. 32-bit words. The state array can be held in various ways: in an array
  328. of words, in a number of individual word variables or in a number of
  329. processor registers. The following define maps a variable name x and
  330. a column number c to the way the state array variable is to be held.
  331. The first define below maps the state into an array x[c] whereas the
  332. second form maps the state into a number of individual variables x0,
  333. x1, etc. Another form could map individual state colums to machine
  334. register names.
  335. */
  336. #if defined(ARRAYS)
  337. #define s(x,c) x[c]
  338. #else
  339. #define s(x,c) x##c
  340. #endif
  341. /* 8. FIXED OR DYNAMIC TABLES
  342. When this section is included the tables used by the code are compiled
  343. statically into the binary file. Otherwise the subroutine gen_tabs()
  344. must be called to compute them before the code is first used.
  345. */
  346. #if 1
  347. #define FIXED_TABLES
  348. #endif
  349. /* 9. TABLE ALIGNMENT
  350. On some sytsems speed will be improved by aligning the AES large lookup
  351. tables on particular boundaries. This define should be set to a power of
  352. two giving the desired alignment. It can be left undefined if alignment
  353. is not needed. This option is specific to the Microsft VC++ compiler -
  354. it seems to sometimes cause trouble for the VC++ version 6 compiler.
  355. */
  356. #if 0 && defined(_MSC_VER) && (_MSC_VER >= 1300)
  357. #define TABLE_ALIGN 64
  358. #endif
  359. /* 10. INTERNAL TABLE CONFIGURATION
  360. This cipher proceeds by repeating in a number of cycles known as 'rounds'
  361. which are implemented by a round function which can optionally be speeded
  362. up using tables. The basic tables are each 256 32-bit words, with either
  363. one or four tables being required for each round function depending on
  364. how much speed is required. The encryption and decryption round functions
  365. are different and the last encryption and decrytpion round functions are
  366. different again making four different round functions in all.
  367. This means that:
  368. 1. Normal encryption and decryption rounds can each use either 0, 1
  369. or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
  370. 2. The last encryption and decryption rounds can also use either 0, 1
  371. or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
  372. Include or exclude the appropriate definitions below to set the number
  373. of tables used by this implementation.
  374. */
  375. #if 1 /* set tables for the normal encryption round */
  376. #define ENC_ROUND FOUR_TABLES
  377. #elif 0
  378. #define ENC_ROUND ONE_TABLE
  379. #else
  380. #define ENC_ROUND NO_TABLES
  381. #endif
  382. #if 1 /* set tables for the last encryption round */
  383. #define LAST_ENC_ROUND FOUR_TABLES
  384. #elif 0
  385. #define LAST_ENC_ROUND ONE_TABLE
  386. #else
  387. #define LAST_ENC_ROUND NO_TABLES
  388. #endif
  389. #if 1 /* set tables for the normal decryption round */
  390. #define DEC_ROUND FOUR_TABLES
  391. #elif 0
  392. #define DEC_ROUND ONE_TABLE
  393. #else
  394. #define DEC_ROUND NO_TABLES
  395. #endif
  396. #if 1 /* set tables for the last decryption round */
  397. #define LAST_DEC_ROUND FOUR_TABLES
  398. #elif 0
  399. #define LAST_DEC_ROUND ONE_TABLE
  400. #else
  401. #define LAST_DEC_ROUND NO_TABLES
  402. #endif
  403. /* The decryption key schedule can be speeded up with tables in the same
  404. way that the round functions can. Include or exclude the following
  405. defines to set this requirement.
  406. */
  407. #if 1
  408. #define KEY_SCHED FOUR_TABLES
  409. #elif 0
  410. #define KEY_SCHED ONE_TABLE
  411. #else
  412. #define KEY_SCHED NO_TABLES
  413. #endif
  414. /* END OF CONFIGURATION OPTIONS */
  415. #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
  416. /* Disable or report errors on some combinations of options */
  417. #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
  418. #undef LAST_ENC_ROUND
  419. #define LAST_ENC_ROUND NO_TABLES
  420. #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
  421. #undef LAST_ENC_ROUND
  422. #define LAST_ENC_ROUND ONE_TABLE
  423. #endif
  424. #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
  425. #undef ENC_UNROLL
  426. #define ENC_UNROLL NONE
  427. #endif
  428. #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
  429. #undef LAST_DEC_ROUND
  430. #define LAST_DEC_ROUND NO_TABLES
  431. #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
  432. #undef LAST_DEC_ROUND
  433. #define LAST_DEC_ROUND ONE_TABLE
  434. #endif
  435. #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
  436. #undef DEC_UNROLL
  437. #define DEC_UNROLL NONE
  438. #endif
  439. /* upr(x,n): rotates bytes within words by n positions, moving bytes to
  440. higher index positions with wrap around into low positions
  441. ups(x,n): moves bytes by n positions to higher index positions in
  442. words but without wrap around
  443. bval(x,n): extracts a byte from a word
  444. NOTE: The definitions given here are intended only for use with
  445. unsigned variables and with shift counts that are compile
  446. time constants
  447. */
  448. #if (ALGORITHM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
  449. #define upr(x,n) (((aes_32t)(x) << (8 * (n))) | ((aes_32t)(x) >> (32 - 8 * (n))))
  450. #define ups(x,n) ((aes_32t) (x) << (8 * (n)))
  451. #define bval(x,n) ((aes_08t)((x) >> (8 * (n))))
  452. #define bytes2word(b0, b1, b2, b3) \
  453. (((aes_32t)(b3) << 24) | ((aes_32t)(b2) << 16) | ((aes_32t)(b1) << 8) | (b0))
  454. #endif
  455. #if (ALGORITHM_BYTE_ORDER == BRG_BIG_ENDIAN)
  456. #define upr(x,n) (((aes_32t)(x) >> (8 * (n))) | ((aes_32t)(x) << (32 - 8 * (n))))
  457. #define ups(x,n) ((aes_32t) (x) >> (8 * (n))))
  458. #define bval(x,n) ((aes_08t)((x) >> (24 - 8 * (n))))
  459. #define bytes2word(b0, b1, b2, b3) \
  460. (((aes_32t)(b0) << 24) | ((aes_32t)(b1) << 16) | ((aes_32t)(b2) << 8) | (b3))
  461. #endif
  462. #if defined(SAFE_IO)
  463. #define word_in(x,c) bytes2word(((aes_08t*)(x)+4*c)[0], ((aes_08t*)(x)+4*c)[1], \
  464. ((aes_08t*)(x)+4*c)[2], ((aes_08t*)(x)+4*c)[3])
  465. #define word_out(x,c,v) { ((aes_08t*)(x)+4*c)[0] = bval(v,0); ((aes_08t*)(x)+4*c)[1] = bval(v,1); \
  466. ((aes_08t*)(x)+4*c)[2] = bval(v,2); ((aes_08t*)(x)+4*c)[3] = bval(v,3); }
  467. #elif (ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER)
  468. #define word_in(x,c) (*((aes_32t*)(x)+(c)))
  469. #define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = (v))
  470. #else
  471. #define word_in(x,c) aes_sw32(*((aes_32t*)(x)+(c)))
  472. #define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = aes_sw32(v))
  473. #endif
  474. /* the finite field modular polynomial and elements */
  475. #define WPOLY 0x011b
  476. #define BPOLY 0x1b
  477. /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
  478. #define m1 0x80808080
  479. #define m2 0x7f7f7f7f
  480. #define gf_mulx(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
  481. /* The following defines provide alternative definitions of gf_mulx that might
  482. give improved performance if a fast 32-bit multiply is not available. Note
  483. that a temporary variable u needs to be defined where gf_mulx is used.
  484. #define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
  485. #define m4 (0x01010101 * BPOLY)
  486. #define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
  487. */
  488. /* Work out which tables are needed for the different options */
  489. #ifdef AES_ASM
  490. #ifdef ENC_ROUND
  491. #undef ENC_ROUND
  492. #endif
  493. #define ENC_ROUND FOUR_TABLES
  494. #ifdef LAST_ENC_ROUND
  495. #undef LAST_ENC_ROUND
  496. #endif
  497. #define LAST_ENC_ROUND FOUR_TABLES
  498. #ifdef DEC_ROUND
  499. #undef DEC_ROUND
  500. #endif
  501. #define DEC_ROUND FOUR_TABLES
  502. #ifdef LAST_DEC_ROUND
  503. #undef LAST_DEC_ROUND
  504. #endif
  505. #define LAST_DEC_ROUND FOUR_TABLES
  506. #ifdef KEY_SCHED
  507. #undef KEY_SCHED
  508. #define KEY_SCHED FOUR_TABLES
  509. #endif
  510. #endif
  511. #if defined(ENCRYPTION) || defined(AES_ASM)
  512. #if ENC_ROUND == ONE_TABLE
  513. #define FT1_SET
  514. #elif ENC_ROUND == FOUR_TABLES
  515. #define FT4_SET
  516. #else
  517. #define SBX_SET
  518. #endif
  519. #if LAST_ENC_ROUND == ONE_TABLE
  520. #define FL1_SET
  521. #elif LAST_ENC_ROUND == FOUR_TABLES
  522. #define FL4_SET
  523. #elif !defined(SBX_SET)
  524. #define SBX_SET
  525. #endif
  526. #endif
  527. #if defined(DECRYPTION) || defined(AES_ASM)
  528. #if DEC_ROUND == ONE_TABLE
  529. #define IT1_SET
  530. #elif DEC_ROUND == FOUR_TABLES
  531. #define IT4_SET
  532. #else
  533. #define ISB_SET
  534. #endif
  535. #if LAST_DEC_ROUND == ONE_TABLE
  536. #define IL1_SET
  537. #elif LAST_DEC_ROUND == FOUR_TABLES
  538. #define IL4_SET
  539. #elif !defined(ISB_SET)
  540. #define ISB_SET
  541. #endif
  542. #endif
  543. #if defined(ENCRYPTION_KEY_SCHEDULE) || defined(DECRYPTION_KEY_SCHEDULE)
  544. #if KEY_SCHED == ONE_TABLE
  545. #define LS1_SET
  546. #define IM1_SET
  547. #elif KEY_SCHED == FOUR_TABLES
  548. #define LS4_SET
  549. #define IM4_SET
  550. #elif !defined(SBX_SET)
  551. #define SBX_SET
  552. #endif
  553. #endif
  554. /* generic definitions of Rijndael macros that use tables */
  555. #define no_table(x,box,vf,rf,c) bytes2word( \
  556. box[bval(vf(x,0,c),rf(0,c))], \
  557. box[bval(vf(x,1,c),rf(1,c))], \
  558. box[bval(vf(x,2,c),rf(2,c))], \
  559. box[bval(vf(x,3,c),rf(3,c))])
  560. #define one_table(x,op,tab,vf,rf,c) \
  561. ( tab[bval(vf(x,0,c),rf(0,c))] \
  562. ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
  563. ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
  564. ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
  565. #define four_tables(x,tab,vf,rf,c) \
  566. ( tab[0][bval(vf(x,0,c),rf(0,c))] \
  567. ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
  568. ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
  569. ^ tab[3][bval(vf(x,3,c),rf(3,c))])
  570. #define vf1(x,r,c) (x)
  571. #define rf1(r,c) (r)
  572. #define rf2(r,c) ((8+r-c)&3)
  573. /* perform forward and inverse column mix operation on four bytes in long word x in */
  574. /* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */
  575. #if defined(FM4_SET) /* not currently used */
  576. #define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
  577. #elif defined(FM1_SET) /* not currently used */
  578. #define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
  579. #else
  580. #define dec_fmvars aes_32t g2
  581. #define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
  582. #endif
  583. #if defined(IM4_SET)
  584. #define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
  585. #elif defined(IM1_SET)
  586. #define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
  587. #else
  588. #define dec_imvars aes_32t g2, g4, g9
  589. #define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
  590. (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
  591. #endif
  592. #if defined(FL4_SET)
  593. #define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
  594. #elif defined(LS4_SET)
  595. #define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
  596. #elif defined(FL1_SET)
  597. #define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
  598. #elif defined(LS1_SET)
  599. #define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
  600. #else
  601. #define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
  602. #endif
  603. #if defined(__cplusplus)
  604. extern "C"
  605. {
  606. #endif
  607. /* If there are no global variables, the definitions here can be
  608. used to put the AES tables in a structure so that a pointer
  609. can then be added to the AES context to pass them to the AES
  610. routines that need them. If this facility is used, the calling
  611. program has to ensure that this pointer is managed appropriately.
  612. In particular, the value of the t_dec(in,it) item in the table
  613. structure must be set to zero in order to ensure that the tables
  614. are initialised. In practice the three code sequences in aeskey.c
  615. that control the calls to gen_tabs() and the gen_tabs() routine
  616. itself will have to be changed for a specific implementation. If
  617. global variables are available it will generally be preferable to
  618. use them with the precomputed FIXED_TABLES option that uses static
  619. global tables.
  620. The following defines can be used to control the way the tables
  621. are defined, initialised and used in embedded environments that
  622. require special features for these purposes
  623. the 't_dec' construction is used to declare fixed table arrays
  624. the 't_set' construction is used to set fixed table values
  625. the 't_use' construction is used to access fixed table values
  626. 256 byte tables:
  627. t_xxx(s,box) => forward S box
  628. t_xxx(i,box) => inverse S box
  629. 256 32-bit word OR 4 x 256 32-bit word tables:
  630. t_xxx(f,n) => forward normal round
  631. t_xxx(f,l) => forward last round
  632. t_xxx(i,n) => inverse normal round
  633. t_xxx(i,l) => inverse last round
  634. t_xxx(l,s) => key schedule table
  635. t_xxx(i,m) => key schedule table
  636. Other variables and tables:
  637. t_xxx(r,c) => the rcon table
  638. */
  639. #define t_dec(m,n) t_##m##n
  640. #define t_set(m,n) t_##m##n
  641. #define t_use(m,n) t_##m##n
  642. #if defined(DO_TABLES) /* declare and instantiate tables */
  643. /* finite field arithmetic operations for table generation */
  644. #if defined(FIXED_TABLES) || !defined(FF_TABLES)
  645. #define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY))
  646. #define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY))
  647. #define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \
  648. ^ (((x>>5) & 4) * WPOLY))
  649. #define f3(x) (f2(x) ^ x)
  650. #define f9(x) (f8(x) ^ x)
  651. #define fb(x) (f8(x) ^ f2(x) ^ x)
  652. #define fd(x) (f8(x) ^ f4(x) ^ x)
  653. #define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  654. #else
  655. #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
  656. #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
  657. #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
  658. #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
  659. #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
  660. #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
  661. #define fi(x) ((x) ? pow[ 255 - log[x]] : 0)
  662. #endif
  663. #if defined(FIXED_TABLES) /* declare and set values for static tables */
  664. #define sb_data(w) \
  665. w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\
  666. w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\
  667. w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\
  668. w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\
  669. w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\
  670. w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\
  671. w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\
  672. w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\
  673. w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\
  674. w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\
  675. w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\
  676. w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\
  677. w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\
  678. w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\
  679. w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\
  680. w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\
  681. w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\
  682. w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\
  683. w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\
  684. w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\
  685. w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\
  686. w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\
  687. w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\
  688. w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\
  689. w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\
  690. w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\
  691. w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\
  692. w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\
  693. w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\
  694. w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\
  695. w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\
  696. w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16)
  697. #define isb_data(w) \
  698. w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\
  699. w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\
  700. w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\
  701. w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\
  702. w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\
  703. w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\
  704. w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\
  705. w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\
  706. w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\
  707. w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\
  708. w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\
  709. w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\
  710. w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\
  711. w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\
  712. w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\
  713. w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\
  714. w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\
  715. w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\
  716. w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\
  717. w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\
  718. w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\
  719. w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\
  720. w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\
  721. w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\
  722. w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\
  723. w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\
  724. w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\
  725. w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\
  726. w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\
  727. w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\
  728. w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\
  729. w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d),
  730. #define mm_data(w) \
  731. w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\
  732. w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\
  733. w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\
  734. w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\
  735. w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\
  736. w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\
  737. w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\
  738. w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\
  739. w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\
  740. w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\
  741. w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\
  742. w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\
  743. w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\
  744. w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\
  745. w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\
  746. w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\
  747. w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\
  748. w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\
  749. w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\
  750. w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\
  751. w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\
  752. w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\
  753. w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\
  754. w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\
  755. w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\
  756. w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\
  757. w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\
  758. w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\
  759. w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\
  760. w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\
  761. w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\
  762. w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff)
  763. #define h0(x) (x)
  764. /* These defines are used to ensure tables are generated in the
  765. right format depending on the internal byte order required
  766. */
  767. #define w0(p) bytes2word(p, 0, 0, 0)
  768. #define w1(p) bytes2word(0, p, 0, 0)
  769. #define w2(p) bytes2word(0, 0, p, 0)
  770. #define w3(p) bytes2word(0, 0, 0, p)
  771. #define u0(p) bytes2word(f2(p), p, p, f3(p))
  772. #define u1(p) bytes2word(f3(p), f2(p), p, p)
  773. #define u2(p) bytes2word(p, f3(p), f2(p), p)
  774. #define u3(p) bytes2word(p, p, f3(p), f2(p))
  775. #define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p))
  776. #define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p))
  777. #define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p))
  778. #define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p))
  779. const aes_32t t_dec(r,c)[RC_LENGTH] =
  780. {
  781. w0(0x01), w0(0x02), w0(0x04), w0(0x08), w0(0x10),
  782. w0(0x20), w0(0x40), w0(0x80), w0(0x1b), w0(0x36)
  783. };
  784. #define d_1(t,n,b,v) const t n[256] = { b(v##0) }
  785. #define d_4(t,n,b,v) const t n[4][256] = { { b(v##0) }, { b(v##1) }, { b(v##2) }, { b(v##3) } }
  786. #else /* declare and instantiate tables for dynamic value generation in in tab.c */
  787. aes_32t t_dec(r,c)[RC_LENGTH];
  788. #define d_1(t,n,b,v) t n[256]
  789. #define d_4(t,n,b,v) t n[4][256]
  790. #endif
  791. #else /* declare tables without instantiation */
  792. #if defined(FIXED_TABLES)
  793. extern const aes_32t t_dec(r,c)[RC_LENGTH];
  794. #if defined(_MSC_VER) && defined(TABLE_ALIGN)
  795. #define d_1(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) const t n[256]
  796. #define d_4(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) const t n[4][256]
  797. #else
  798. #define d_1(t,n,b,v) extern const t n[256]
  799. #define d_4(t,n,b,v) extern const t n[4][256]
  800. #endif
  801. #else
  802. extern aes_32t t_dec(r,c)[RC_LENGTH];
  803. #if defined(_MSC_VER) && defined(TABLE_ALIGN)
  804. #define d_1(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) t n[256]
  805. #define d_4(t,n,b,v) extern __declspec(align(TABLE_ALIGN)) t n[4][256]
  806. #else
  807. #define d_1(t,n,b,v) extern t n[256]
  808. #define d_4(t,n,b,v) extern t n[4][256]
  809. #endif
  810. #endif
  811. #endif
  812. #ifdef SBX_SET
  813. d_1(aes_08t, t_dec(s,box), sb_data, h);
  814. #endif
  815. #ifdef ISB_SET
  816. d_1(aes_08t, t_dec(i,box), isb_data, h);
  817. #endif
  818. #ifdef FT1_SET
  819. d_1(aes_32t, t_dec(f,n), sb_data, u);
  820. #endif
  821. #ifdef FT4_SET
  822. d_4(aes_32t, t_dec(f,n), sb_data, u);
  823. #endif
  824. #ifdef FL1_SET
  825. d_1(aes_32t, t_dec(f,l), sb_data, w);
  826. #endif
  827. #ifdef FL4_SET
  828. d_4(aes_32t, t_dec(f,l), sb_data, w);
  829. #endif
  830. #ifdef IT1_SET
  831. d_1(aes_32t, t_dec(i,n), isb_data, v);
  832. #endif
  833. #ifdef IT4_SET
  834. d_4(aes_32t, t_dec(i,n), isb_data, v);
  835. #endif
  836. #ifdef IL1_SET
  837. d_1(aes_32t, t_dec(i,l), isb_data, w);
  838. #endif
  839. #ifdef IL4_SET
  840. d_4(aes_32t, t_dec(i,l), isb_data, w);
  841. #endif
  842. #ifdef LS1_SET
  843. #ifdef FL1_SET
  844. #undef LS1_SET
  845. #else
  846. d_1(aes_32t, t_dec(l,s), sb_data, w);
  847. #endif
  848. #endif
  849. #ifdef LS4_SET
  850. #ifdef FL4_SET
  851. #undef LS4_SET
  852. #else
  853. d_4(aes_32t, t_dec(l,s), sb_data, w);
  854. #endif
  855. #endif
  856. #ifdef IM1_SET
  857. d_1(aes_32t, t_dec(i,m), mm_data, v);
  858. #endif
  859. #ifdef IM4_SET
  860. d_4(aes_32t, t_dec(i,m), mm_data, v);
  861. #endif
  862. #if defined(__cplusplus)
  863. }
  864. #endif
  865. #endif