aes_x86core.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /* crypto/aes/aes_core.c */
  2. /**
  3. * rijndael-alg-fst.c
  4. *
  5. * @version 3.0 (December 2000)
  6. *
  7. * Optimised ANSI C code for the Rijndael cipher (now AES)
  8. *
  9. * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
  10. * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
  11. * @author Paulo Barreto <paulo.barreto@terra.com.br>
  12. *
  13. * This code is hereby placed in the public domain.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
  16. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  22. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  24. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * This is experimental x86[_64] derivative. It assumes little-endian
  29. * byte order and expects CPU to sustain unaligned memory references.
  30. * It is used as playground for cache-time attack mitigations and
  31. * serves as reference C implementation for x86[_64] assembler.
  32. *
  33. * <appro@fy.chalmers.se>
  34. */
  35. #ifndef AES_DEBUG
  36. # ifndef NDEBUG
  37. # define NDEBUG
  38. # endif
  39. #endif
  40. #include <assert.h>
  41. #include <stdlib.h>
  42. #include <openssl/aes.h>
  43. #include "aes_locl.h"
  44. /*
  45. * These two parameters control which table, 256-byte or 2KB, is
  46. * referenced in outer and respectively inner rounds.
  47. */
  48. #define AES_COMPACT_IN_OUTER_ROUNDS
  49. #ifdef AES_COMPACT_IN_OUTER_ROUNDS
  50. /* AES_COMPACT_IN_OUTER_ROUNDS costs ~30% in performance, while
  51. * adding AES_COMPACT_IN_INNER_ROUNDS reduces benchmark *further*
  52. * by factor of ~2. */
  53. # undef AES_COMPACT_IN_INNER_ROUNDS
  54. #endif
  55. #if 1
  56. static void prefetch256(const void *table)
  57. {
  58. volatile unsigned long *t=(void *)table,ret;
  59. unsigned long sum;
  60. int i;
  61. /* 32 is common least cache-line size */
  62. for (sum=0,i=0;i<256/sizeof(t[0]);i+=32/sizeof(t[0])) sum ^= t[i];
  63. ret = sum;
  64. }
  65. #else
  66. # define prefetch256(t)
  67. #endif
  68. #undef GETU32
  69. #define GETU32(p) (*((u32*)(p)))
  70. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  71. typedef unsigned __int64 u64;
  72. #define U64(C) C##UI64
  73. #elif defined(__arch64__)
  74. typedef unsigned long u64;
  75. #define U64(C) C##UL
  76. #else
  77. typedef unsigned long long u64;
  78. #define U64(C) C##ULL
  79. #endif
  80. #undef ROTATE
  81. #if defined(_MSC_VER)
  82. # define ROTATE(a,n) _lrotl(a,n)
  83. #elif defined(__ICC)
  84. # define ROTATE(a,n) _rotl(a,n)
  85. #elif defined(__GNUC__) && __GNUC__>=2
  86. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  87. # define ROTATE(a,n) ({ register unsigned int ret; \
  88. asm ( \
  89. "roll %1,%0" \
  90. : "=r"(ret) \
  91. : "I"(n), "0"(a) \
  92. : "cc"); \
  93. ret; \
  94. })
  95. # endif
  96. #endif
  97. /*-
  98. Te [x] = S [x].[02, 01, 01, 03, 02, 01, 01, 03];
  99. Te0[x] = S [x].[02, 01, 01, 03];
  100. Te1[x] = S [x].[03, 02, 01, 01];
  101. Te2[x] = S [x].[01, 03, 02, 01];
  102. Te3[x] = S [x].[01, 01, 03, 02];
  103. */
  104. #define Te0 (u32)((u64*)((u8*)Te+0))
  105. #define Te1 (u32)((u64*)((u8*)Te+3))
  106. #define Te2 (u32)((u64*)((u8*)Te+2))
  107. #define Te3 (u32)((u64*)((u8*)Te+1))
  108. /*-
  109. Td [x] = Si[x].[0e, 09, 0d, 0b, 0e, 09, 0d, 0b];
  110. Td0[x] = Si[x].[0e, 09, 0d, 0b];
  111. Td1[x] = Si[x].[0b, 0e, 09, 0d];
  112. Td2[x] = Si[x].[0d, 0b, 0e, 09];
  113. Td3[x] = Si[x].[09, 0d, 0b, 0e];
  114. Td4[x] = Si[x].[01];
  115. */
  116. #define Td0 (u32)((u64*)((u8*)Td+0))
  117. #define Td1 (u32)((u64*)((u8*)Td+3))
  118. #define Td2 (u32)((u64*)((u8*)Td+2))
  119. #define Td3 (u32)((u64*)((u8*)Td+1))
  120. static const u64 Te[256] = {
  121. U64(0xa56363c6a56363c6), U64(0x847c7cf8847c7cf8),
  122. U64(0x997777ee997777ee), U64(0x8d7b7bf68d7b7bf6),
  123. U64(0x0df2f2ff0df2f2ff), U64(0xbd6b6bd6bd6b6bd6),
  124. U64(0xb16f6fdeb16f6fde), U64(0x54c5c59154c5c591),
  125. U64(0x5030306050303060), U64(0x0301010203010102),
  126. U64(0xa96767cea96767ce), U64(0x7d2b2b567d2b2b56),
  127. U64(0x19fefee719fefee7), U64(0x62d7d7b562d7d7b5),
  128. U64(0xe6abab4de6abab4d), U64(0x9a7676ec9a7676ec),
  129. U64(0x45caca8f45caca8f), U64(0x9d82821f9d82821f),
  130. U64(0x40c9c98940c9c989), U64(0x877d7dfa877d7dfa),
  131. U64(0x15fafaef15fafaef), U64(0xeb5959b2eb5959b2),
  132. U64(0xc947478ec947478e), U64(0x0bf0f0fb0bf0f0fb),
  133. U64(0xecadad41ecadad41), U64(0x67d4d4b367d4d4b3),
  134. U64(0xfda2a25ffda2a25f), U64(0xeaafaf45eaafaf45),
  135. U64(0xbf9c9c23bf9c9c23), U64(0xf7a4a453f7a4a453),
  136. U64(0x967272e4967272e4), U64(0x5bc0c09b5bc0c09b),
  137. U64(0xc2b7b775c2b7b775), U64(0x1cfdfde11cfdfde1),
  138. U64(0xae93933dae93933d), U64(0x6a26264c6a26264c),
  139. U64(0x5a36366c5a36366c), U64(0x413f3f7e413f3f7e),
  140. U64(0x02f7f7f502f7f7f5), U64(0x4fcccc834fcccc83),
  141. U64(0x5c3434685c343468), U64(0xf4a5a551f4a5a551),
  142. U64(0x34e5e5d134e5e5d1), U64(0x08f1f1f908f1f1f9),
  143. U64(0x937171e2937171e2), U64(0x73d8d8ab73d8d8ab),
  144. U64(0x5331316253313162), U64(0x3f15152a3f15152a),
  145. U64(0x0c0404080c040408), U64(0x52c7c79552c7c795),
  146. U64(0x6523234665232346), U64(0x5ec3c39d5ec3c39d),
  147. U64(0x2818183028181830), U64(0xa1969637a1969637),
  148. U64(0x0f05050a0f05050a), U64(0xb59a9a2fb59a9a2f),
  149. U64(0x0907070e0907070e), U64(0x3612122436121224),
  150. U64(0x9b80801b9b80801b), U64(0x3de2e2df3de2e2df),
  151. U64(0x26ebebcd26ebebcd), U64(0x6927274e6927274e),
  152. U64(0xcdb2b27fcdb2b27f), U64(0x9f7575ea9f7575ea),
  153. U64(0x1b0909121b090912), U64(0x9e83831d9e83831d),
  154. U64(0x742c2c58742c2c58), U64(0x2e1a1a342e1a1a34),
  155. U64(0x2d1b1b362d1b1b36), U64(0xb26e6edcb26e6edc),
  156. U64(0xee5a5ab4ee5a5ab4), U64(0xfba0a05bfba0a05b),
  157. U64(0xf65252a4f65252a4), U64(0x4d3b3b764d3b3b76),
  158. U64(0x61d6d6b761d6d6b7), U64(0xceb3b37dceb3b37d),
  159. U64(0x7b2929527b292952), U64(0x3ee3e3dd3ee3e3dd),
  160. U64(0x712f2f5e712f2f5e), U64(0x9784841397848413),
  161. U64(0xf55353a6f55353a6), U64(0x68d1d1b968d1d1b9),
  162. U64(0x0000000000000000), U64(0x2cededc12cededc1),
  163. U64(0x6020204060202040), U64(0x1ffcfce31ffcfce3),
  164. U64(0xc8b1b179c8b1b179), U64(0xed5b5bb6ed5b5bb6),
  165. U64(0xbe6a6ad4be6a6ad4), U64(0x46cbcb8d46cbcb8d),
  166. U64(0xd9bebe67d9bebe67), U64(0x4b3939724b393972),
  167. U64(0xde4a4a94de4a4a94), U64(0xd44c4c98d44c4c98),
  168. U64(0xe85858b0e85858b0), U64(0x4acfcf854acfcf85),
  169. U64(0x6bd0d0bb6bd0d0bb), U64(0x2aefefc52aefefc5),
  170. U64(0xe5aaaa4fe5aaaa4f), U64(0x16fbfbed16fbfbed),
  171. U64(0xc5434386c5434386), U64(0xd74d4d9ad74d4d9a),
  172. U64(0x5533336655333366), U64(0x9485851194858511),
  173. U64(0xcf45458acf45458a), U64(0x10f9f9e910f9f9e9),
  174. U64(0x0602020406020204), U64(0x817f7ffe817f7ffe),
  175. U64(0xf05050a0f05050a0), U64(0x443c3c78443c3c78),
  176. U64(0xba9f9f25ba9f9f25), U64(0xe3a8a84be3a8a84b),
  177. U64(0xf35151a2f35151a2), U64(0xfea3a35dfea3a35d),
  178. U64(0xc0404080c0404080), U64(0x8a8f8f058a8f8f05),
  179. U64(0xad92923fad92923f), U64(0xbc9d9d21bc9d9d21),
  180. U64(0x4838387048383870), U64(0x04f5f5f104f5f5f1),
  181. U64(0xdfbcbc63dfbcbc63), U64(0xc1b6b677c1b6b677),
  182. U64(0x75dadaaf75dadaaf), U64(0x6321214263212142),
  183. U64(0x3010102030101020), U64(0x1affffe51affffe5),
  184. U64(0x0ef3f3fd0ef3f3fd), U64(0x6dd2d2bf6dd2d2bf),
  185. U64(0x4ccdcd814ccdcd81), U64(0x140c0c18140c0c18),
  186. U64(0x3513132635131326), U64(0x2fececc32fececc3),
  187. U64(0xe15f5fbee15f5fbe), U64(0xa2979735a2979735),
  188. U64(0xcc444488cc444488), U64(0x3917172e3917172e),
  189. U64(0x57c4c49357c4c493), U64(0xf2a7a755f2a7a755),
  190. U64(0x827e7efc827e7efc), U64(0x473d3d7a473d3d7a),
  191. U64(0xac6464c8ac6464c8), U64(0xe75d5dbae75d5dba),
  192. U64(0x2b1919322b191932), U64(0x957373e6957373e6),
  193. U64(0xa06060c0a06060c0), U64(0x9881811998818119),
  194. U64(0xd14f4f9ed14f4f9e), U64(0x7fdcdca37fdcdca3),
  195. U64(0x6622224466222244), U64(0x7e2a2a547e2a2a54),
  196. U64(0xab90903bab90903b), U64(0x8388880b8388880b),
  197. U64(0xca46468cca46468c), U64(0x29eeeec729eeeec7),
  198. U64(0xd3b8b86bd3b8b86b), U64(0x3c1414283c141428),
  199. U64(0x79dedea779dedea7), U64(0xe25e5ebce25e5ebc),
  200. U64(0x1d0b0b161d0b0b16), U64(0x76dbdbad76dbdbad),
  201. U64(0x3be0e0db3be0e0db), U64(0x5632326456323264),
  202. U64(0x4e3a3a744e3a3a74), U64(0x1e0a0a141e0a0a14),
  203. U64(0xdb494992db494992), U64(0x0a06060c0a06060c),
  204. U64(0x6c2424486c242448), U64(0xe45c5cb8e45c5cb8),
  205. U64(0x5dc2c29f5dc2c29f), U64(0x6ed3d3bd6ed3d3bd),
  206. U64(0xefacac43efacac43), U64(0xa66262c4a66262c4),
  207. U64(0xa8919139a8919139), U64(0xa4959531a4959531),
  208. U64(0x37e4e4d337e4e4d3), U64(0x8b7979f28b7979f2),
  209. U64(0x32e7e7d532e7e7d5), U64(0x43c8c88b43c8c88b),
  210. U64(0x5937376e5937376e), U64(0xb76d6ddab76d6dda),
  211. U64(0x8c8d8d018c8d8d01), U64(0x64d5d5b164d5d5b1),
  212. U64(0xd24e4e9cd24e4e9c), U64(0xe0a9a949e0a9a949),
  213. U64(0xb46c6cd8b46c6cd8), U64(0xfa5656acfa5656ac),
  214. U64(0x07f4f4f307f4f4f3), U64(0x25eaeacf25eaeacf),
  215. U64(0xaf6565caaf6565ca), U64(0x8e7a7af48e7a7af4),
  216. U64(0xe9aeae47e9aeae47), U64(0x1808081018080810),
  217. U64(0xd5baba6fd5baba6f), U64(0x887878f0887878f0),
  218. U64(0x6f25254a6f25254a), U64(0x722e2e5c722e2e5c),
  219. U64(0x241c1c38241c1c38), U64(0xf1a6a657f1a6a657),
  220. U64(0xc7b4b473c7b4b473), U64(0x51c6c69751c6c697),
  221. U64(0x23e8e8cb23e8e8cb), U64(0x7cdddda17cdddda1),
  222. U64(0x9c7474e89c7474e8), U64(0x211f1f3e211f1f3e),
  223. U64(0xdd4b4b96dd4b4b96), U64(0xdcbdbd61dcbdbd61),
  224. U64(0x868b8b0d868b8b0d), U64(0x858a8a0f858a8a0f),
  225. U64(0x907070e0907070e0), U64(0x423e3e7c423e3e7c),
  226. U64(0xc4b5b571c4b5b571), U64(0xaa6666ccaa6666cc),
  227. U64(0xd8484890d8484890), U64(0x0503030605030306),
  228. U64(0x01f6f6f701f6f6f7), U64(0x120e0e1c120e0e1c),
  229. U64(0xa36161c2a36161c2), U64(0x5f35356a5f35356a),
  230. U64(0xf95757aef95757ae), U64(0xd0b9b969d0b9b969),
  231. U64(0x9186861791868617), U64(0x58c1c19958c1c199),
  232. U64(0x271d1d3a271d1d3a), U64(0xb99e9e27b99e9e27),
  233. U64(0x38e1e1d938e1e1d9), U64(0x13f8f8eb13f8f8eb),
  234. U64(0xb398982bb398982b), U64(0x3311112233111122),
  235. U64(0xbb6969d2bb6969d2), U64(0x70d9d9a970d9d9a9),
  236. U64(0x898e8e07898e8e07), U64(0xa7949433a7949433),
  237. U64(0xb69b9b2db69b9b2d), U64(0x221e1e3c221e1e3c),
  238. U64(0x9287871592878715), U64(0x20e9e9c920e9e9c9),
  239. U64(0x49cece8749cece87), U64(0xff5555aaff5555aa),
  240. U64(0x7828285078282850), U64(0x7adfdfa57adfdfa5),
  241. U64(0x8f8c8c038f8c8c03), U64(0xf8a1a159f8a1a159),
  242. U64(0x8089890980898909), U64(0x170d0d1a170d0d1a),
  243. U64(0xdabfbf65dabfbf65), U64(0x31e6e6d731e6e6d7),
  244. U64(0xc6424284c6424284), U64(0xb86868d0b86868d0),
  245. U64(0xc3414182c3414182), U64(0xb0999929b0999929),
  246. U64(0x772d2d5a772d2d5a), U64(0x110f0f1e110f0f1e),
  247. U64(0xcbb0b07bcbb0b07b), U64(0xfc5454a8fc5454a8),
  248. U64(0xd6bbbb6dd6bbbb6d), U64(0x3a16162c3a16162c)
  249. };
  250. static const u8 Te4[256] = {
  251. 0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U,
  252. 0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U,
  253. 0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U,
  254. 0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U,
  255. 0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU,
  256. 0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U,
  257. 0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU,
  258. 0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U,
  259. 0x09U, 0x83U, 0x2cU, 0x1aU, 0x1bU, 0x6eU, 0x5aU, 0xa0U,
  260. 0x52U, 0x3bU, 0xd6U, 0xb3U, 0x29U, 0xe3U, 0x2fU, 0x84U,
  261. 0x53U, 0xd1U, 0x00U, 0xedU, 0x20U, 0xfcU, 0xb1U, 0x5bU,
  262. 0x6aU, 0xcbU, 0xbeU, 0x39U, 0x4aU, 0x4cU, 0x58U, 0xcfU,
  263. 0xd0U, 0xefU, 0xaaU, 0xfbU, 0x43U, 0x4dU, 0x33U, 0x85U,
  264. 0x45U, 0xf9U, 0x02U, 0x7fU, 0x50U, 0x3cU, 0x9fU, 0xa8U,
  265. 0x51U, 0xa3U, 0x40U, 0x8fU, 0x92U, 0x9dU, 0x38U, 0xf5U,
  266. 0xbcU, 0xb6U, 0xdaU, 0x21U, 0x10U, 0xffU, 0xf3U, 0xd2U,
  267. 0xcdU, 0x0cU, 0x13U, 0xecU, 0x5fU, 0x97U, 0x44U, 0x17U,
  268. 0xc4U, 0xa7U, 0x7eU, 0x3dU, 0x64U, 0x5dU, 0x19U, 0x73U,
  269. 0x60U, 0x81U, 0x4fU, 0xdcU, 0x22U, 0x2aU, 0x90U, 0x88U,
  270. 0x46U, 0xeeU, 0xb8U, 0x14U, 0xdeU, 0x5eU, 0x0bU, 0xdbU,
  271. 0xe0U, 0x32U, 0x3aU, 0x0aU, 0x49U, 0x06U, 0x24U, 0x5cU,
  272. 0xc2U, 0xd3U, 0xacU, 0x62U, 0x91U, 0x95U, 0xe4U, 0x79U,
  273. 0xe7U, 0xc8U, 0x37U, 0x6dU, 0x8dU, 0xd5U, 0x4eU, 0xa9U,
  274. 0x6cU, 0x56U, 0xf4U, 0xeaU, 0x65U, 0x7aU, 0xaeU, 0x08U,
  275. 0xbaU, 0x78U, 0x25U, 0x2eU, 0x1cU, 0xa6U, 0xb4U, 0xc6U,
  276. 0xe8U, 0xddU, 0x74U, 0x1fU, 0x4bU, 0xbdU, 0x8bU, 0x8aU,
  277. 0x70U, 0x3eU, 0xb5U, 0x66U, 0x48U, 0x03U, 0xf6U, 0x0eU,
  278. 0x61U, 0x35U, 0x57U, 0xb9U, 0x86U, 0xc1U, 0x1dU, 0x9eU,
  279. 0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U,
  280. 0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU,
  281. 0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U,
  282. 0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U
  283. };
  284. static const u64 Td[256] = {
  285. U64(0x50a7f45150a7f451), U64(0x5365417e5365417e),
  286. U64(0xc3a4171ac3a4171a), U64(0x965e273a965e273a),
  287. U64(0xcb6bab3bcb6bab3b), U64(0xf1459d1ff1459d1f),
  288. U64(0xab58faacab58faac), U64(0x9303e34b9303e34b),
  289. U64(0x55fa302055fa3020), U64(0xf66d76adf66d76ad),
  290. U64(0x9176cc889176cc88), U64(0x254c02f5254c02f5),
  291. U64(0xfcd7e54ffcd7e54f), U64(0xd7cb2ac5d7cb2ac5),
  292. U64(0x8044352680443526), U64(0x8fa362b58fa362b5),
  293. U64(0x495ab1de495ab1de), U64(0x671bba25671bba25),
  294. U64(0x980eea45980eea45), U64(0xe1c0fe5de1c0fe5d),
  295. U64(0x02752fc302752fc3), U64(0x12f04c8112f04c81),
  296. U64(0xa397468da397468d), U64(0xc6f9d36bc6f9d36b),
  297. U64(0xe75f8f03e75f8f03), U64(0x959c9215959c9215),
  298. U64(0xeb7a6dbfeb7a6dbf), U64(0xda595295da595295),
  299. U64(0x2d83bed42d83bed4), U64(0xd3217458d3217458),
  300. U64(0x2969e0492969e049), U64(0x44c8c98e44c8c98e),
  301. U64(0x6a89c2756a89c275), U64(0x78798ef478798ef4),
  302. U64(0x6b3e58996b3e5899), U64(0xdd71b927dd71b927),
  303. U64(0xb64fe1beb64fe1be), U64(0x17ad88f017ad88f0),
  304. U64(0x66ac20c966ac20c9), U64(0xb43ace7db43ace7d),
  305. U64(0x184adf63184adf63), U64(0x82311ae582311ae5),
  306. U64(0x6033519760335197), U64(0x457f5362457f5362),
  307. U64(0xe07764b1e07764b1), U64(0x84ae6bbb84ae6bbb),
  308. U64(0x1ca081fe1ca081fe), U64(0x942b08f9942b08f9),
  309. U64(0x5868487058684870), U64(0x19fd458f19fd458f),
  310. U64(0x876cde94876cde94), U64(0xb7f87b52b7f87b52),
  311. U64(0x23d373ab23d373ab), U64(0xe2024b72e2024b72),
  312. U64(0x578f1fe3578f1fe3), U64(0x2aab55662aab5566),
  313. U64(0x0728ebb20728ebb2), U64(0x03c2b52f03c2b52f),
  314. U64(0x9a7bc5869a7bc586), U64(0xa50837d3a50837d3),
  315. U64(0xf2872830f2872830), U64(0xb2a5bf23b2a5bf23),
  316. U64(0xba6a0302ba6a0302), U64(0x5c8216ed5c8216ed),
  317. U64(0x2b1ccf8a2b1ccf8a), U64(0x92b479a792b479a7),
  318. U64(0xf0f207f3f0f207f3), U64(0xa1e2694ea1e2694e),
  319. U64(0xcdf4da65cdf4da65), U64(0xd5be0506d5be0506),
  320. U64(0x1f6234d11f6234d1), U64(0x8afea6c48afea6c4),
  321. U64(0x9d532e349d532e34), U64(0xa055f3a2a055f3a2),
  322. U64(0x32e18a0532e18a05), U64(0x75ebf6a475ebf6a4),
  323. U64(0x39ec830b39ec830b), U64(0xaaef6040aaef6040),
  324. U64(0x069f715e069f715e), U64(0x51106ebd51106ebd),
  325. U64(0xf98a213ef98a213e), U64(0x3d06dd963d06dd96),
  326. U64(0xae053eddae053edd), U64(0x46bde64d46bde64d),
  327. U64(0xb58d5491b58d5491), U64(0x055dc471055dc471),
  328. U64(0x6fd406046fd40604), U64(0xff155060ff155060),
  329. U64(0x24fb981924fb9819), U64(0x97e9bdd697e9bdd6),
  330. U64(0xcc434089cc434089), U64(0x779ed967779ed967),
  331. U64(0xbd42e8b0bd42e8b0), U64(0x888b8907888b8907),
  332. U64(0x385b19e7385b19e7), U64(0xdbeec879dbeec879),
  333. U64(0x470a7ca1470a7ca1), U64(0xe90f427ce90f427c),
  334. U64(0xc91e84f8c91e84f8), U64(0x0000000000000000),
  335. U64(0x8386800983868009), U64(0x48ed2b3248ed2b32),
  336. U64(0xac70111eac70111e), U64(0x4e725a6c4e725a6c),
  337. U64(0xfbff0efdfbff0efd), U64(0x5638850f5638850f),
  338. U64(0x1ed5ae3d1ed5ae3d), U64(0x27392d3627392d36),
  339. U64(0x64d90f0a64d90f0a), U64(0x21a65c6821a65c68),
  340. U64(0xd1545b9bd1545b9b), U64(0x3a2e36243a2e3624),
  341. U64(0xb1670a0cb1670a0c), U64(0x0fe757930fe75793),
  342. U64(0xd296eeb4d296eeb4), U64(0x9e919b1b9e919b1b),
  343. U64(0x4fc5c0804fc5c080), U64(0xa220dc61a220dc61),
  344. U64(0x694b775a694b775a), U64(0x161a121c161a121c),
  345. U64(0x0aba93e20aba93e2), U64(0xe52aa0c0e52aa0c0),
  346. U64(0x43e0223c43e0223c), U64(0x1d171b121d171b12),
  347. U64(0x0b0d090e0b0d090e), U64(0xadc78bf2adc78bf2),
  348. U64(0xb9a8b62db9a8b62d), U64(0xc8a91e14c8a91e14),
  349. U64(0x8519f1578519f157), U64(0x4c0775af4c0775af),
  350. U64(0xbbdd99eebbdd99ee), U64(0xfd607fa3fd607fa3),
  351. U64(0x9f2601f79f2601f7), U64(0xbcf5725cbcf5725c),
  352. U64(0xc53b6644c53b6644), U64(0x347efb5b347efb5b),
  353. U64(0x7629438b7629438b), U64(0xdcc623cbdcc623cb),
  354. U64(0x68fcedb668fcedb6), U64(0x63f1e4b863f1e4b8),
  355. U64(0xcadc31d7cadc31d7), U64(0x1085634210856342),
  356. U64(0x4022971340229713), U64(0x2011c6842011c684),
  357. U64(0x7d244a857d244a85), U64(0xf83dbbd2f83dbbd2),
  358. U64(0x1132f9ae1132f9ae), U64(0x6da129c76da129c7),
  359. U64(0x4b2f9e1d4b2f9e1d), U64(0xf330b2dcf330b2dc),
  360. U64(0xec52860dec52860d), U64(0xd0e3c177d0e3c177),
  361. U64(0x6c16b32b6c16b32b), U64(0x99b970a999b970a9),
  362. U64(0xfa489411fa489411), U64(0x2264e9472264e947),
  363. U64(0xc48cfca8c48cfca8), U64(0x1a3ff0a01a3ff0a0),
  364. U64(0xd82c7d56d82c7d56), U64(0xef903322ef903322),
  365. U64(0xc74e4987c74e4987), U64(0xc1d138d9c1d138d9),
  366. U64(0xfea2ca8cfea2ca8c), U64(0x360bd498360bd498),
  367. U64(0xcf81f5a6cf81f5a6), U64(0x28de7aa528de7aa5),
  368. U64(0x268eb7da268eb7da), U64(0xa4bfad3fa4bfad3f),
  369. U64(0xe49d3a2ce49d3a2c), U64(0x0d9278500d927850),
  370. U64(0x9bcc5f6a9bcc5f6a), U64(0x62467e5462467e54),
  371. U64(0xc2138df6c2138df6), U64(0xe8b8d890e8b8d890),
  372. U64(0x5ef7392e5ef7392e), U64(0xf5afc382f5afc382),
  373. U64(0xbe805d9fbe805d9f), U64(0x7c93d0697c93d069),
  374. U64(0xa92dd56fa92dd56f), U64(0xb31225cfb31225cf),
  375. U64(0x3b99acc83b99acc8), U64(0xa77d1810a77d1810),
  376. U64(0x6e639ce86e639ce8), U64(0x7bbb3bdb7bbb3bdb),
  377. U64(0x097826cd097826cd), U64(0xf418596ef418596e),
  378. U64(0x01b79aec01b79aec), U64(0xa89a4f83a89a4f83),
  379. U64(0x656e95e6656e95e6), U64(0x7ee6ffaa7ee6ffaa),
  380. U64(0x08cfbc2108cfbc21), U64(0xe6e815efe6e815ef),
  381. U64(0xd99be7bad99be7ba), U64(0xce366f4ace366f4a),
  382. U64(0xd4099fead4099fea), U64(0xd67cb029d67cb029),
  383. U64(0xafb2a431afb2a431), U64(0x31233f2a31233f2a),
  384. U64(0x3094a5c63094a5c6), U64(0xc066a235c066a235),
  385. U64(0x37bc4e7437bc4e74), U64(0xa6ca82fca6ca82fc),
  386. U64(0xb0d090e0b0d090e0), U64(0x15d8a73315d8a733),
  387. U64(0x4a9804f14a9804f1), U64(0xf7daec41f7daec41),
  388. U64(0x0e50cd7f0e50cd7f), U64(0x2ff691172ff69117),
  389. U64(0x8dd64d768dd64d76), U64(0x4db0ef434db0ef43),
  390. U64(0x544daacc544daacc), U64(0xdf0496e4df0496e4),
  391. U64(0xe3b5d19ee3b5d19e), U64(0x1b886a4c1b886a4c),
  392. U64(0xb81f2cc1b81f2cc1), U64(0x7f5165467f516546),
  393. U64(0x04ea5e9d04ea5e9d), U64(0x5d358c015d358c01),
  394. U64(0x737487fa737487fa), U64(0x2e410bfb2e410bfb),
  395. U64(0x5a1d67b35a1d67b3), U64(0x52d2db9252d2db92),
  396. U64(0x335610e9335610e9), U64(0x1347d66d1347d66d),
  397. U64(0x8c61d79a8c61d79a), U64(0x7a0ca1377a0ca137),
  398. U64(0x8e14f8598e14f859), U64(0x893c13eb893c13eb),
  399. U64(0xee27a9ceee27a9ce), U64(0x35c961b735c961b7),
  400. U64(0xede51ce1ede51ce1), U64(0x3cb1477a3cb1477a),
  401. U64(0x59dfd29c59dfd29c), U64(0x3f73f2553f73f255),
  402. U64(0x79ce141879ce1418), U64(0xbf37c773bf37c773),
  403. U64(0xeacdf753eacdf753), U64(0x5baafd5f5baafd5f),
  404. U64(0x146f3ddf146f3ddf), U64(0x86db447886db4478),
  405. U64(0x81f3afca81f3afca), U64(0x3ec468b93ec468b9),
  406. U64(0x2c3424382c342438), U64(0x5f40a3c25f40a3c2),
  407. U64(0x72c31d1672c31d16), U64(0x0c25e2bc0c25e2bc),
  408. U64(0x8b493c288b493c28), U64(0x41950dff41950dff),
  409. U64(0x7101a8397101a839), U64(0xdeb30c08deb30c08),
  410. U64(0x9ce4b4d89ce4b4d8), U64(0x90c1566490c15664),
  411. U64(0x6184cb7b6184cb7b), U64(0x70b632d570b632d5),
  412. U64(0x745c6c48745c6c48), U64(0x4257b8d04257b8d0)
  413. };
  414. static const u8 Td4[256] = {
  415. 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
  416. 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
  417. 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
  418. 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
  419. 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
  420. 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
  421. 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
  422. 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
  423. 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
  424. 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
  425. 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
  426. 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
  427. 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
  428. 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
  429. 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
  430. 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
  431. 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
  432. 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
  433. 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
  434. 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
  435. 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
  436. 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
  437. 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
  438. 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
  439. 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
  440. 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
  441. 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
  442. 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
  443. 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
  444. 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
  445. 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
  446. 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU
  447. };
  448. static const u32 rcon[] = {
  449. 0x00000001U, 0x00000002U, 0x00000004U, 0x00000008U,
  450. 0x00000010U, 0x00000020U, 0x00000040U, 0x00000080U,
  451. 0x0000001bU, 0x00000036U, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
  452. };
  453. /**
  454. * Expand the cipher key into the encryption key schedule.
  455. */
  456. int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
  457. AES_KEY *key)
  458. {
  459. u32 *rk;
  460. int i = 0;
  461. u32 temp;
  462. if (!userKey || !key)
  463. return -1;
  464. if (bits != 128 && bits != 192 && bits != 256)
  465. return -2;
  466. rk = key->rd_key;
  467. if (bits==128)
  468. key->rounds = 10;
  469. else if (bits==192)
  470. key->rounds = 12;
  471. else
  472. key->rounds = 14;
  473. rk[0] = GETU32(userKey );
  474. rk[1] = GETU32(userKey + 4);
  475. rk[2] = GETU32(userKey + 8);
  476. rk[3] = GETU32(userKey + 12);
  477. if (bits == 128) {
  478. while (1) {
  479. temp = rk[3];
  480. rk[4] = rk[0] ^
  481. ((u32)Te4[(temp >> 8) & 0xff] ) ^
  482. ((u32)Te4[(temp >> 16) & 0xff] << 8) ^
  483. ((u32)Te4[(temp >> 24) ] << 16) ^
  484. ((u32)Te4[(temp ) & 0xff] << 24) ^
  485. rcon[i];
  486. rk[5] = rk[1] ^ rk[4];
  487. rk[6] = rk[2] ^ rk[5];
  488. rk[7] = rk[3] ^ rk[6];
  489. if (++i == 10) {
  490. return 0;
  491. }
  492. rk += 4;
  493. }
  494. }
  495. rk[4] = GETU32(userKey + 16);
  496. rk[5] = GETU32(userKey + 20);
  497. if (bits == 192) {
  498. while (1) {
  499. temp = rk[ 5];
  500. rk[ 6] = rk[ 0] ^
  501. ((u32)Te4[(temp >> 8) & 0xff] ) ^
  502. ((u32)Te4[(temp >> 16) & 0xff] << 8) ^
  503. ((u32)Te4[(temp >> 24) ] << 16) ^
  504. ((u32)Te4[(temp ) & 0xff] << 24) ^
  505. rcon[i];
  506. rk[ 7] = rk[ 1] ^ rk[ 6];
  507. rk[ 8] = rk[ 2] ^ rk[ 7];
  508. rk[ 9] = rk[ 3] ^ rk[ 8];
  509. if (++i == 8) {
  510. return 0;
  511. }
  512. rk[10] = rk[ 4] ^ rk[ 9];
  513. rk[11] = rk[ 5] ^ rk[10];
  514. rk += 6;
  515. }
  516. }
  517. rk[6] = GETU32(userKey + 24);
  518. rk[7] = GETU32(userKey + 28);
  519. if (bits == 256) {
  520. while (1) {
  521. temp = rk[ 7];
  522. rk[ 8] = rk[ 0] ^
  523. ((u32)Te4[(temp >> 8) & 0xff] ) ^
  524. ((u32)Te4[(temp >> 16) & 0xff] << 8) ^
  525. ((u32)Te4[(temp >> 24) ] << 16) ^
  526. ((u32)Te4[(temp ) & 0xff] << 24) ^
  527. rcon[i];
  528. rk[ 9] = rk[ 1] ^ rk[ 8];
  529. rk[10] = rk[ 2] ^ rk[ 9];
  530. rk[11] = rk[ 3] ^ rk[10];
  531. if (++i == 7) {
  532. return 0;
  533. }
  534. temp = rk[11];
  535. rk[12] = rk[ 4] ^
  536. ((u32)Te4[(temp ) & 0xff] ) ^
  537. ((u32)Te4[(temp >> 8) & 0xff] << 8) ^
  538. ((u32)Te4[(temp >> 16) & 0xff] << 16) ^
  539. ((u32)Te4[(temp >> 24) ] << 24);
  540. rk[13] = rk[ 5] ^ rk[12];
  541. rk[14] = rk[ 6] ^ rk[13];
  542. rk[15] = rk[ 7] ^ rk[14];
  543. rk += 8;
  544. }
  545. }
  546. return 0;
  547. }
  548. /**
  549. * Expand the cipher key into the decryption key schedule.
  550. */
  551. int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
  552. AES_KEY *key)
  553. {
  554. u32 *rk;
  555. int i, j, status;
  556. u32 temp;
  557. /* first, start with an encryption schedule */
  558. status = AES_set_encrypt_key(userKey, bits, key);
  559. if (status < 0)
  560. return status;
  561. rk = key->rd_key;
  562. /* invert the order of the round keys: */
  563. for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
  564. temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
  565. temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
  566. temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
  567. temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
  568. }
  569. /* apply the inverse MixColumn transform to all round keys but the first and the last: */
  570. for (i = 1; i < (key->rounds); i++) {
  571. rk += 4;
  572. #if 1
  573. for (j = 0; j < 4; j++) {
  574. u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
  575. tp1 = rk[j];
  576. m = tp1 & 0x80808080;
  577. tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^
  578. ((m - (m >> 7)) & 0x1b1b1b1b);
  579. m = tp2 & 0x80808080;
  580. tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^
  581. ((m - (m >> 7)) & 0x1b1b1b1b);
  582. m = tp4 & 0x80808080;
  583. tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^
  584. ((m - (m >> 7)) & 0x1b1b1b1b);
  585. tp9 = tp8 ^ tp1;
  586. tpb = tp9 ^ tp2;
  587. tpd = tp9 ^ tp4;
  588. tpe = tp8 ^ tp4 ^ tp2;
  589. #if defined(ROTATE)
  590. rk[j] = tpe ^ ROTATE(tpd,16) ^
  591. ROTATE(tp9,8) ^ ROTATE(tpb,24);
  592. #else
  593. rk[j] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^
  594. (tp9 >> 24) ^ (tp9 << 8) ^
  595. (tpb >> 8) ^ (tpb << 24);
  596. #endif
  597. }
  598. #else
  599. rk[0] =
  600. Td0[Te2[(rk[0] ) & 0xff] & 0xff] ^
  601. Td1[Te2[(rk[0] >> 8) & 0xff] & 0xff] ^
  602. Td2[Te2[(rk[0] >> 16) & 0xff] & 0xff] ^
  603. Td3[Te2[(rk[0] >> 24) ] & 0xff];
  604. rk[1] =
  605. Td0[Te2[(rk[1] ) & 0xff] & 0xff] ^
  606. Td1[Te2[(rk[1] >> 8) & 0xff] & 0xff] ^
  607. Td2[Te2[(rk[1] >> 16) & 0xff] & 0xff] ^
  608. Td3[Te2[(rk[1] >> 24) ] & 0xff];
  609. rk[2] =
  610. Td0[Te2[(rk[2] ) & 0xff] & 0xff] ^
  611. Td1[Te2[(rk[2] >> 8) & 0xff] & 0xff] ^
  612. Td2[Te2[(rk[2] >> 16) & 0xff] & 0xff] ^
  613. Td3[Te2[(rk[2] >> 24) ] & 0xff];
  614. rk[3] =
  615. Td0[Te2[(rk[3] ) & 0xff] & 0xff] ^
  616. Td1[Te2[(rk[3] >> 8) & 0xff] & 0xff] ^
  617. Td2[Te2[(rk[3] >> 16) & 0xff] & 0xff] ^
  618. Td3[Te2[(rk[3] >> 24) ] & 0xff];
  619. #endif
  620. }
  621. return 0;
  622. }
  623. /*
  624. * Encrypt a single block
  625. * in and out can overlap
  626. */
  627. void AES_encrypt(const unsigned char *in, unsigned char *out,
  628. const AES_KEY *key)
  629. {
  630. const u32 *rk;
  631. u32 s0, s1, s2, s3, t[4];
  632. int r;
  633. assert(in && out && key);
  634. rk = key->rd_key;
  635. /*
  636. * map byte array block to cipher state
  637. * and add initial round key:
  638. */
  639. s0 = GETU32(in ) ^ rk[0];
  640. s1 = GETU32(in + 4) ^ rk[1];
  641. s2 = GETU32(in + 8) ^ rk[2];
  642. s3 = GETU32(in + 12) ^ rk[3];
  643. #if defined(AES_COMPACT_IN_OUTER_ROUNDS)
  644. prefetch256(Te4);
  645. t[0] = (u32)Te4[(s0 ) & 0xff] ^
  646. (u32)Te4[(s1 >> 8) & 0xff] << 8 ^
  647. (u32)Te4[(s2 >> 16) & 0xff] << 16 ^
  648. (u32)Te4[(s3 >> 24) ] << 24;
  649. t[1] = (u32)Te4[(s1 ) & 0xff] ^
  650. (u32)Te4[(s2 >> 8) & 0xff] << 8 ^
  651. (u32)Te4[(s3 >> 16) & 0xff] << 16 ^
  652. (u32)Te4[(s0 >> 24) ] << 24;
  653. t[2] = (u32)Te4[(s2 ) & 0xff] ^
  654. (u32)Te4[(s3 >> 8) & 0xff] << 8 ^
  655. (u32)Te4[(s0 >> 16) & 0xff] << 16 ^
  656. (u32)Te4[(s1 >> 24) ] << 24;
  657. t[3] = (u32)Te4[(s3 ) & 0xff] ^
  658. (u32)Te4[(s0 >> 8) & 0xff] << 8 ^
  659. (u32)Te4[(s1 >> 16) & 0xff] << 16 ^
  660. (u32)Te4[(s2 >> 24) ] << 24;
  661. /* now do the linear transform using words */
  662. { int i;
  663. u32 r0, r1, r2;
  664. for (i = 0; i < 4; i++) {
  665. r0 = t[i];
  666. r1 = r0 & 0x80808080;
  667. r2 = ((r0 & 0x7f7f7f7f) << 1) ^
  668. ((r1 - (r1 >> 7)) & 0x1b1b1b1b);
  669. #if defined(ROTATE)
  670. t[i] = r2 ^ ROTATE(r2,24) ^ ROTATE(r0,24) ^
  671. ROTATE(r0,16) ^ ROTATE(r0,8);
  672. #else
  673. t[i] = r2 ^ ((r2 ^ r0) << 24) ^ ((r2 ^ r0) >> 8) ^
  674. (r0 << 16) ^ (r0 >> 16) ^
  675. (r0 << 8) ^ (r0 >> 24);
  676. #endif
  677. t[i] ^= rk[4+i];
  678. }
  679. }
  680. #else
  681. t[0] = Te0[(s0 ) & 0xff] ^
  682. Te1[(s1 >> 8) & 0xff] ^
  683. Te2[(s2 >> 16) & 0xff] ^
  684. Te3[(s3 >> 24) ] ^
  685. rk[4];
  686. t[1] = Te0[(s1 ) & 0xff] ^
  687. Te1[(s2 >> 8) & 0xff] ^
  688. Te2[(s3 >> 16) & 0xff] ^
  689. Te3[(s0 >> 24) ] ^
  690. rk[5];
  691. t[2] = Te0[(s2 ) & 0xff] ^
  692. Te1[(s3 >> 8) & 0xff] ^
  693. Te2[(s0 >> 16) & 0xff] ^
  694. Te3[(s1 >> 24) ] ^
  695. rk[6];
  696. t[3] = Te0[(s3 ) & 0xff] ^
  697. Te1[(s0 >> 8) & 0xff] ^
  698. Te2[(s1 >> 16) & 0xff] ^
  699. Te3[(s2 >> 24) ] ^
  700. rk[7];
  701. #endif
  702. s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3];
  703. /*
  704. * Nr - 2 full rounds:
  705. */
  706. for (rk+=8,r=key->rounds-2; r>0; rk+=4,r--) {
  707. #if defined(AES_COMPACT_IN_INNER_ROUNDS)
  708. t[0] = (u32)Te4[(s0 ) & 0xff] ^
  709. (u32)Te4[(s1 >> 8) & 0xff] << 8 ^
  710. (u32)Te4[(s2 >> 16) & 0xff] << 16 ^
  711. (u32)Te4[(s3 >> 24) ] << 24;
  712. t[1] = (u32)Te4[(s1 ) & 0xff] ^
  713. (u32)Te4[(s2 >> 8) & 0xff] << 8 ^
  714. (u32)Te4[(s3 >> 16) & 0xff] << 16 ^
  715. (u32)Te4[(s0 >> 24) ] << 24;
  716. t[2] = (u32)Te4[(s2 ) & 0xff] ^
  717. (u32)Te4[(s3 >> 8) & 0xff] << 8 ^
  718. (u32)Te4[(s0 >> 16) & 0xff] << 16 ^
  719. (u32)Te4[(s1 >> 24) ] << 24;
  720. t[3] = (u32)Te4[(s3 ) & 0xff] ^
  721. (u32)Te4[(s0 >> 8) & 0xff] << 8 ^
  722. (u32)Te4[(s1 >> 16) & 0xff] << 16 ^
  723. (u32)Te4[(s2 >> 24) ] << 24;
  724. /* now do the linear transform using words */
  725. {
  726. int i;
  727. u32 r0, r1, r2;
  728. for (i = 0; i < 4; i++) {
  729. r0 = t[i];
  730. r1 = r0 & 0x80808080;
  731. r2 = ((r0 & 0x7f7f7f7f) << 1) ^
  732. ((r1 - (r1 >> 7)) & 0x1b1b1b1b);
  733. #if defined(ROTATE)
  734. t[i] = r2 ^ ROTATE(r2,24) ^ ROTATE(r0,24) ^
  735. ROTATE(r0,16) ^ ROTATE(r0,8);
  736. #else
  737. t[i] = r2 ^ ((r2 ^ r0) << 24) ^ ((r2 ^ r0) >> 8) ^
  738. (r0 << 16) ^ (r0 >> 16) ^
  739. (r0 << 8) ^ (r0 >> 24);
  740. #endif
  741. t[i] ^= rk[i];
  742. }
  743. }
  744. #else
  745. t[0] = Te0[(s0 ) & 0xff] ^
  746. Te1[(s1 >> 8) & 0xff] ^
  747. Te2[(s2 >> 16) & 0xff] ^
  748. Te3[(s3 >> 24) ] ^
  749. rk[0];
  750. t[1] = Te0[(s1 ) & 0xff] ^
  751. Te1[(s2 >> 8) & 0xff] ^
  752. Te2[(s3 >> 16) & 0xff] ^
  753. Te3[(s0 >> 24) ] ^
  754. rk[1];
  755. t[2] = Te0[(s2 ) & 0xff] ^
  756. Te1[(s3 >> 8) & 0xff] ^
  757. Te2[(s0 >> 16) & 0xff] ^
  758. Te3[(s1 >> 24) ] ^
  759. rk[2];
  760. t[3] = Te0[(s3 ) & 0xff] ^
  761. Te1[(s0 >> 8) & 0xff] ^
  762. Te2[(s1 >> 16) & 0xff] ^
  763. Te3[(s2 >> 24) ] ^
  764. rk[3];
  765. #endif
  766. s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3];
  767. }
  768. /*
  769. * apply last round and
  770. * map cipher state to byte array block:
  771. */
  772. #if defined(AES_COMPACT_IN_OUTER_ROUNDS)
  773. prefetch256(Te4);
  774. *(u32*)(out+0) =
  775. (u32)Te4[(s0 ) & 0xff] ^
  776. (u32)Te4[(s1 >> 8) & 0xff] << 8 ^
  777. (u32)Te4[(s2 >> 16) & 0xff] << 16 ^
  778. (u32)Te4[(s3 >> 24) ] << 24 ^
  779. rk[0];
  780. *(u32*)(out+4) =
  781. (u32)Te4[(s1 ) & 0xff] ^
  782. (u32)Te4[(s2 >> 8) & 0xff] << 8 ^
  783. (u32)Te4[(s3 >> 16) & 0xff] << 16 ^
  784. (u32)Te4[(s0 >> 24) ] << 24 ^
  785. rk[1];
  786. *(u32*)(out+8) =
  787. (u32)Te4[(s2 ) & 0xff] ^
  788. (u32)Te4[(s3 >> 8) & 0xff] << 8 ^
  789. (u32)Te4[(s0 >> 16) & 0xff] << 16 ^
  790. (u32)Te4[(s1 >> 24) ] << 24 ^
  791. rk[2];
  792. *(u32*)(out+12) =
  793. (u32)Te4[(s3 ) & 0xff] ^
  794. (u32)Te4[(s0 >> 8) & 0xff] << 8 ^
  795. (u32)Te4[(s1 >> 16) & 0xff] << 16 ^
  796. (u32)Te4[(s2 >> 24) ] << 24 ^
  797. rk[3];
  798. #else
  799. *(u32*)(out+0) =
  800. (Te2[(s0 ) & 0xff] & 0x000000ffU) ^
  801. (Te3[(s1 >> 8) & 0xff] & 0x0000ff00U) ^
  802. (Te0[(s2 >> 16) & 0xff] & 0x00ff0000U) ^
  803. (Te1[(s3 >> 24) ] & 0xff000000U) ^
  804. rk[0];
  805. *(u32*)(out+4) =
  806. (Te2[(s1 ) & 0xff] & 0x000000ffU) ^
  807. (Te3[(s2 >> 8) & 0xff] & 0x0000ff00U) ^
  808. (Te0[(s3 >> 16) & 0xff] & 0x00ff0000U) ^
  809. (Te1[(s0 >> 24) ] & 0xff000000U) ^
  810. rk[1];
  811. *(u32*)(out+8) =
  812. (Te2[(s2 ) & 0xff] & 0x000000ffU) ^
  813. (Te3[(s3 >> 8) & 0xff] & 0x0000ff00U) ^
  814. (Te0[(s0 >> 16) & 0xff] & 0x00ff0000U) ^
  815. (Te1[(s1 >> 24) ] & 0xff000000U) ^
  816. rk[2];
  817. *(u32*)(out+12) =
  818. (Te2[(s3 ) & 0xff] & 0x000000ffU) ^
  819. (Te3[(s0 >> 8) & 0xff] & 0x0000ff00U) ^
  820. (Te0[(s1 >> 16) & 0xff] & 0x00ff0000U) ^
  821. (Te1[(s2 >> 24) ] & 0xff000000U) ^
  822. rk[3];
  823. #endif
  824. }
  825. /*
  826. * Decrypt a single block
  827. * in and out can overlap
  828. */
  829. void AES_decrypt(const unsigned char *in, unsigned char *out,
  830. const AES_KEY *key)
  831. {
  832. const u32 *rk;
  833. u32 s0, s1, s2, s3, t[4];
  834. int r;
  835. assert(in && out && key);
  836. rk = key->rd_key;
  837. /*
  838. * map byte array block to cipher state
  839. * and add initial round key:
  840. */
  841. s0 = GETU32(in ) ^ rk[0];
  842. s1 = GETU32(in + 4) ^ rk[1];
  843. s2 = GETU32(in + 8) ^ rk[2];
  844. s3 = GETU32(in + 12) ^ rk[3];
  845. #if defined(AES_COMPACT_IN_OUTER_ROUNDS)
  846. prefetch256(Td4);
  847. t[0] = (u32)Td4[(s0 ) & 0xff] ^
  848. (u32)Td4[(s3 >> 8) & 0xff] << 8 ^
  849. (u32)Td4[(s2 >> 16) & 0xff] << 16 ^
  850. (u32)Td4[(s1 >> 24) ] << 24;
  851. t[1] = (u32)Td4[(s1 ) & 0xff] ^
  852. (u32)Td4[(s0 >> 8) & 0xff] << 8 ^
  853. (u32)Td4[(s3 >> 16) & 0xff] << 16 ^
  854. (u32)Td4[(s2 >> 24) ] << 24;
  855. t[2] = (u32)Td4[(s2 ) & 0xff] ^
  856. (u32)Td4[(s1 >> 8) & 0xff] << 8 ^
  857. (u32)Td4[(s0 >> 16) & 0xff] << 16 ^
  858. (u32)Td4[(s3 >> 24) ] << 24;
  859. t[3] = (u32)Td4[(s3 ) & 0xff] ^
  860. (u32)Td4[(s2 >> 8) & 0xff] << 8 ^
  861. (u32)Td4[(s1 >> 16) & 0xff] << 16 ^
  862. (u32)Td4[(s0 >> 24) ] << 24;
  863. /* now do the linear transform using words */
  864. {
  865. int i;
  866. u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
  867. for (i = 0; i < 4; i++) {
  868. tp1 = t[i];
  869. m = tp1 & 0x80808080;
  870. tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^
  871. ((m - (m >> 7)) & 0x1b1b1b1b);
  872. m = tp2 & 0x80808080;
  873. tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^
  874. ((m - (m >> 7)) & 0x1b1b1b1b);
  875. m = tp4 & 0x80808080;
  876. tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^
  877. ((m - (m >> 7)) & 0x1b1b1b1b);
  878. tp9 = tp8 ^ tp1;
  879. tpb = tp9 ^ tp2;
  880. tpd = tp9 ^ tp4;
  881. tpe = tp8 ^ tp4 ^ tp2;
  882. #if defined(ROTATE)
  883. t[i] = tpe ^ ROTATE(tpd,16) ^
  884. ROTATE(tp9,8) ^ ROTATE(tpb,24);
  885. #else
  886. t[i] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^
  887. (tp9 >> 24) ^ (tp9 << 8) ^
  888. (tpb >> 8) ^ (tpb << 24);
  889. #endif
  890. t[i] ^= rk[4+i];
  891. }
  892. }
  893. #else
  894. t[0] = Td0[(s0 ) & 0xff] ^
  895. Td1[(s3 >> 8) & 0xff] ^
  896. Td2[(s2 >> 16) & 0xff] ^
  897. Td3[(s1 >> 24) ] ^
  898. rk[4];
  899. t[1] = Td0[(s1 ) & 0xff] ^
  900. Td1[(s0 >> 8) & 0xff] ^
  901. Td2[(s3 >> 16) & 0xff] ^
  902. Td3[(s2 >> 24) ] ^
  903. rk[5];
  904. t[2] = Td0[(s2 ) & 0xff] ^
  905. Td1[(s1 >> 8) & 0xff] ^
  906. Td2[(s0 >> 16) & 0xff] ^
  907. Td3[(s3 >> 24) ] ^
  908. rk[6];
  909. t[3] = Td0[(s3 ) & 0xff] ^
  910. Td1[(s2 >> 8) & 0xff] ^
  911. Td2[(s1 >> 16) & 0xff] ^
  912. Td3[(s0 >> 24) ] ^
  913. rk[7];
  914. #endif
  915. s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3];
  916. /*
  917. * Nr - 2 full rounds:
  918. */
  919. for (rk+=8,r=key->rounds-2; r>0; rk+=4,r--) {
  920. #if defined(AES_COMPACT_IN_INNER_ROUNDS)
  921. t[0] = (u32)Td4[(s0 ) & 0xff] ^
  922. (u32)Td4[(s3 >> 8) & 0xff] << 8 ^
  923. (u32)Td4[(s2 >> 16) & 0xff] << 16 ^
  924. (u32)Td4[(s1 >> 24) ] << 24;
  925. t[1] = (u32)Td4[(s1 ) & 0xff] ^
  926. (u32)Td4[(s0 >> 8) & 0xff] << 8 ^
  927. (u32)Td4[(s3 >> 16) & 0xff] << 16 ^
  928. (u32)Td4[(s2 >> 24) ] << 24;
  929. t[2] = (u32)Td4[(s2 ) & 0xff] ^
  930. (u32)Td4[(s1 >> 8) & 0xff] << 8 ^
  931. (u32)Td4[(s0 >> 16) & 0xff] << 16 ^
  932. (u32)Td4[(s3 >> 24) ] << 24;
  933. t[3] = (u32)Td4[(s3 ) & 0xff] ^
  934. (u32)Td4[(s2 >> 8) & 0xff] << 8 ^
  935. (u32)Td4[(s1 >> 16) & 0xff] << 16 ^
  936. (u32)Td4[(s0 >> 24) ] << 24;
  937. /* now do the linear transform using words */
  938. {
  939. int i;
  940. u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
  941. for (i = 0; i < 4; i++) {
  942. tp1 = t[i];
  943. m = tp1 & 0x80808080;
  944. tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^
  945. ((m - (m >> 7)) & 0x1b1b1b1b);
  946. m = tp2 & 0x80808080;
  947. tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^
  948. ((m - (m >> 7)) & 0x1b1b1b1b);
  949. m = tp4 & 0x80808080;
  950. tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^
  951. ((m - (m >> 7)) & 0x1b1b1b1b);
  952. tp9 = tp8 ^ tp1;
  953. tpb = tp9 ^ tp2;
  954. tpd = tp9 ^ tp4;
  955. tpe = tp8 ^ tp4 ^ tp2;
  956. #if defined(ROTATE)
  957. t[i] = tpe ^ ROTATE(tpd,16) ^
  958. ROTATE(tp9,8) ^ ROTATE(tpb,24);
  959. #else
  960. t[i] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^
  961. (tp9 >> 24) ^ (tp9 << 8) ^
  962. (tpb >> 8) ^ (tpb << 24);
  963. #endif
  964. t[i] ^= rk[i];
  965. }
  966. }
  967. #else
  968. t[0] = Td0[(s0 ) & 0xff] ^
  969. Td1[(s3 >> 8) & 0xff] ^
  970. Td2[(s2 >> 16) & 0xff] ^
  971. Td3[(s1 >> 24) ] ^
  972. rk[0];
  973. t[1] = Td0[(s1 ) & 0xff] ^
  974. Td1[(s0 >> 8) & 0xff] ^
  975. Td2[(s3 >> 16) & 0xff] ^
  976. Td3[(s2 >> 24) ] ^
  977. rk[1];
  978. t[2] = Td0[(s2 ) & 0xff] ^
  979. Td1[(s1 >> 8) & 0xff] ^
  980. Td2[(s0 >> 16) & 0xff] ^
  981. Td3[(s3 >> 24) ] ^
  982. rk[2];
  983. t[3] = Td0[(s3 ) & 0xff] ^
  984. Td1[(s2 >> 8) & 0xff] ^
  985. Td2[(s1 >> 16) & 0xff] ^
  986. Td3[(s0 >> 24) ] ^
  987. rk[3];
  988. #endif
  989. s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3];
  990. }
  991. /*
  992. * apply last round and
  993. * map cipher state to byte array block:
  994. */
  995. prefetch256(Td4);
  996. *(u32*)(out+0) =
  997. ((u32)Td4[(s0 ) & 0xff]) ^
  998. ((u32)Td4[(s3 >> 8) & 0xff] << 8) ^
  999. ((u32)Td4[(s2 >> 16) & 0xff] << 16) ^
  1000. ((u32)Td4[(s1 >> 24) ] << 24) ^
  1001. rk[0];
  1002. *(u32*)(out+4) =
  1003. ((u32)Td4[(s1 ) & 0xff]) ^
  1004. ((u32)Td4[(s0 >> 8) & 0xff] << 8) ^
  1005. ((u32)Td4[(s3 >> 16) & 0xff] << 16) ^
  1006. ((u32)Td4[(s2 >> 24) ] << 24) ^
  1007. rk[1];
  1008. *(u32*)(out+8) =
  1009. ((u32)Td4[(s2 ) & 0xff]) ^
  1010. ((u32)Td4[(s1 >> 8) & 0xff] << 8) ^
  1011. ((u32)Td4[(s0 >> 16) & 0xff] << 16) ^
  1012. ((u32)Td4[(s3 >> 24) ] << 24) ^
  1013. rk[2];
  1014. *(u32*)(out+12) =
  1015. ((u32)Td4[(s3 ) & 0xff]) ^
  1016. ((u32)Td4[(s2 >> 8) & 0xff] << 8) ^
  1017. ((u32)Td4[(s1 >> 16) & 0xff] << 16) ^
  1018. ((u32)Td4[(s0 >> 24) ] << 24) ^
  1019. rk[3];
  1020. }