Bez popisu

aesopt.h 26KB

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