aesopt.h 38 KB

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