aesopt.h 27 KB

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