camellia-aesni-avx2-asm_64.S 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*
  2. * x86_64/AVX2/AES-NI assembler implementation of Camellia
  3. *
  4. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #include <linux/linkage.h>
  13. #include <asm/frame.h>
  14. #include <asm/nospec-branch.h>
  15. #define CAMELLIA_TABLE_BYTE_LEN 272
  16. /* struct camellia_ctx: */
  17. #define key_table 0
  18. #define key_length CAMELLIA_TABLE_BYTE_LEN
  19. /* register macros */
  20. #define CTX %rdi
  21. #define RIO %r8
  22. /**********************************************************************
  23. helper macros
  24. **********************************************************************/
  25. #define filter_8bit(x, lo_t, hi_t, mask4bit, tmp0) \
  26. vpand x, mask4bit, tmp0; \
  27. vpandn x, mask4bit, x; \
  28. vpsrld $4, x, x; \
  29. \
  30. vpshufb tmp0, lo_t, tmp0; \
  31. vpshufb x, hi_t, x; \
  32. vpxor tmp0, x, x;
  33. #define ymm0_x xmm0
  34. #define ymm1_x xmm1
  35. #define ymm2_x xmm2
  36. #define ymm3_x xmm3
  37. #define ymm4_x xmm4
  38. #define ymm5_x xmm5
  39. #define ymm6_x xmm6
  40. #define ymm7_x xmm7
  41. #define ymm8_x xmm8
  42. #define ymm9_x xmm9
  43. #define ymm10_x xmm10
  44. #define ymm11_x xmm11
  45. #define ymm12_x xmm12
  46. #define ymm13_x xmm13
  47. #define ymm14_x xmm14
  48. #define ymm15_x xmm15
  49. /**********************************************************************
  50. 32-way camellia
  51. **********************************************************************/
  52. /*
  53. * IN:
  54. * x0..x7: byte-sliced AB state
  55. * mem_cd: register pointer storing CD state
  56. * key: index for key material
  57. * OUT:
  58. * x0..x7: new byte-sliced CD state
  59. */
  60. #define roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, t0, t1, t2, t3, t4, t5, t6, \
  61. t7, mem_cd, key) \
  62. /* \
  63. * S-function with AES subbytes \
  64. */ \
  65. vbroadcasti128 .Linv_shift_row, t4; \
  66. vpbroadcastd .L0f0f0f0f, t7; \
  67. vbroadcasti128 .Lpre_tf_lo_s1, t5; \
  68. vbroadcasti128 .Lpre_tf_hi_s1, t6; \
  69. vbroadcasti128 .Lpre_tf_lo_s4, t2; \
  70. vbroadcasti128 .Lpre_tf_hi_s4, t3; \
  71. \
  72. /* AES inverse shift rows */ \
  73. vpshufb t4, x0, x0; \
  74. vpshufb t4, x7, x7; \
  75. vpshufb t4, x3, x3; \
  76. vpshufb t4, x6, x6; \
  77. vpshufb t4, x2, x2; \
  78. vpshufb t4, x5, x5; \
  79. vpshufb t4, x1, x1; \
  80. vpshufb t4, x4, x4; \
  81. \
  82. /* prefilter sboxes 1, 2 and 3 */ \
  83. /* prefilter sbox 4 */ \
  84. filter_8bit(x0, t5, t6, t7, t4); \
  85. filter_8bit(x7, t5, t6, t7, t4); \
  86. vextracti128 $1, x0, t0##_x; \
  87. vextracti128 $1, x7, t1##_x; \
  88. filter_8bit(x3, t2, t3, t7, t4); \
  89. filter_8bit(x6, t2, t3, t7, t4); \
  90. vextracti128 $1, x3, t3##_x; \
  91. vextracti128 $1, x6, t2##_x; \
  92. filter_8bit(x2, t5, t6, t7, t4); \
  93. filter_8bit(x5, t5, t6, t7, t4); \
  94. filter_8bit(x1, t5, t6, t7, t4); \
  95. filter_8bit(x4, t5, t6, t7, t4); \
  96. \
  97. vpxor t4##_x, t4##_x, t4##_x; \
  98. \
  99. /* AES subbytes + AES shift rows */ \
  100. vextracti128 $1, x2, t6##_x; \
  101. vextracti128 $1, x5, t5##_x; \
  102. vaesenclast t4##_x, x0##_x, x0##_x; \
  103. vaesenclast t4##_x, t0##_x, t0##_x; \
  104. vinserti128 $1, t0##_x, x0, x0; \
  105. vaesenclast t4##_x, x7##_x, x7##_x; \
  106. vaesenclast t4##_x, t1##_x, t1##_x; \
  107. vinserti128 $1, t1##_x, x7, x7; \
  108. vaesenclast t4##_x, x3##_x, x3##_x; \
  109. vaesenclast t4##_x, t3##_x, t3##_x; \
  110. vinserti128 $1, t3##_x, x3, x3; \
  111. vaesenclast t4##_x, x6##_x, x6##_x; \
  112. vaesenclast t4##_x, t2##_x, t2##_x; \
  113. vinserti128 $1, t2##_x, x6, x6; \
  114. vextracti128 $1, x1, t3##_x; \
  115. vextracti128 $1, x4, t2##_x; \
  116. vbroadcasti128 .Lpost_tf_lo_s1, t0; \
  117. vbroadcasti128 .Lpost_tf_hi_s1, t1; \
  118. vaesenclast t4##_x, x2##_x, x2##_x; \
  119. vaesenclast t4##_x, t6##_x, t6##_x; \
  120. vinserti128 $1, t6##_x, x2, x2; \
  121. vaesenclast t4##_x, x5##_x, x5##_x; \
  122. vaesenclast t4##_x, t5##_x, t5##_x; \
  123. vinserti128 $1, t5##_x, x5, x5; \
  124. vaesenclast t4##_x, x1##_x, x1##_x; \
  125. vaesenclast t4##_x, t3##_x, t3##_x; \
  126. vinserti128 $1, t3##_x, x1, x1; \
  127. vaesenclast t4##_x, x4##_x, x4##_x; \
  128. vaesenclast t4##_x, t2##_x, t2##_x; \
  129. vinserti128 $1, t2##_x, x4, x4; \
  130. \
  131. /* postfilter sboxes 1 and 4 */ \
  132. vbroadcasti128 .Lpost_tf_lo_s3, t2; \
  133. vbroadcasti128 .Lpost_tf_hi_s3, t3; \
  134. filter_8bit(x0, t0, t1, t7, t6); \
  135. filter_8bit(x7, t0, t1, t7, t6); \
  136. filter_8bit(x3, t0, t1, t7, t6); \
  137. filter_8bit(x6, t0, t1, t7, t6); \
  138. \
  139. /* postfilter sbox 3 */ \
  140. vbroadcasti128 .Lpost_tf_lo_s2, t4; \
  141. vbroadcasti128 .Lpost_tf_hi_s2, t5; \
  142. filter_8bit(x2, t2, t3, t7, t6); \
  143. filter_8bit(x5, t2, t3, t7, t6); \
  144. \
  145. vpbroadcastq key, t0; /* higher 64-bit duplicate ignored */ \
  146. \
  147. /* postfilter sbox 2 */ \
  148. filter_8bit(x1, t4, t5, t7, t2); \
  149. filter_8bit(x4, t4, t5, t7, t2); \
  150. vpxor t7, t7, t7; \
  151. \
  152. vpsrldq $1, t0, t1; \
  153. vpsrldq $2, t0, t2; \
  154. vpshufb t7, t1, t1; \
  155. vpsrldq $3, t0, t3; \
  156. \
  157. /* P-function */ \
  158. vpxor x5, x0, x0; \
  159. vpxor x6, x1, x1; \
  160. vpxor x7, x2, x2; \
  161. vpxor x4, x3, x3; \
  162. \
  163. vpshufb t7, t2, t2; \
  164. vpsrldq $4, t0, t4; \
  165. vpshufb t7, t3, t3; \
  166. vpsrldq $5, t0, t5; \
  167. vpshufb t7, t4, t4; \
  168. \
  169. vpxor x2, x4, x4; \
  170. vpxor x3, x5, x5; \
  171. vpxor x0, x6, x6; \
  172. vpxor x1, x7, x7; \
  173. \
  174. vpsrldq $6, t0, t6; \
  175. vpshufb t7, t5, t5; \
  176. vpshufb t7, t6, t6; \
  177. \
  178. vpxor x7, x0, x0; \
  179. vpxor x4, x1, x1; \
  180. vpxor x5, x2, x2; \
  181. vpxor x6, x3, x3; \
  182. \
  183. vpxor x3, x4, x4; \
  184. vpxor x0, x5, x5; \
  185. vpxor x1, x6, x6; \
  186. vpxor x2, x7, x7; /* note: high and low parts swapped */ \
  187. \
  188. /* Add key material and result to CD (x becomes new CD) */ \
  189. \
  190. vpxor t6, x1, x1; \
  191. vpxor 5 * 32(mem_cd), x1, x1; \
  192. \
  193. vpsrldq $7, t0, t6; \
  194. vpshufb t7, t0, t0; \
  195. vpshufb t7, t6, t7; \
  196. \
  197. vpxor t7, x0, x0; \
  198. vpxor 4 * 32(mem_cd), x0, x0; \
  199. \
  200. vpxor t5, x2, x2; \
  201. vpxor 6 * 32(mem_cd), x2, x2; \
  202. \
  203. vpxor t4, x3, x3; \
  204. vpxor 7 * 32(mem_cd), x3, x3; \
  205. \
  206. vpxor t3, x4, x4; \
  207. vpxor 0 * 32(mem_cd), x4, x4; \
  208. \
  209. vpxor t2, x5, x5; \
  210. vpxor 1 * 32(mem_cd), x5, x5; \
  211. \
  212. vpxor t1, x6, x6; \
  213. vpxor 2 * 32(mem_cd), x6, x6; \
  214. \
  215. vpxor t0, x7, x7; \
  216. vpxor 3 * 32(mem_cd), x7, x7;
  217. /*
  218. * Size optimization... with inlined roundsm32 binary would be over 5 times
  219. * larger and would only marginally faster.
  220. */
  221. .align 8
  222. roundsm32_x0_x1_x2_x3_x4_x5_x6_x7_y0_y1_y2_y3_y4_y5_y6_y7_cd:
  223. roundsm32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  224. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14, %ymm15,
  225. %rcx, (%r9));
  226. ret;
  227. ENDPROC(roundsm32_x0_x1_x2_x3_x4_x5_x6_x7_y0_y1_y2_y3_y4_y5_y6_y7_cd)
  228. .align 8
  229. roundsm32_x4_x5_x6_x7_x0_x1_x2_x3_y4_y5_y6_y7_y0_y1_y2_y3_ab:
  230. roundsm32(%ymm4, %ymm5, %ymm6, %ymm7, %ymm0, %ymm1, %ymm2, %ymm3,
  231. %ymm12, %ymm13, %ymm14, %ymm15, %ymm8, %ymm9, %ymm10, %ymm11,
  232. %rax, (%r9));
  233. ret;
  234. ENDPROC(roundsm32_x4_x5_x6_x7_x0_x1_x2_x3_y4_y5_y6_y7_y0_y1_y2_y3_ab)
  235. /*
  236. * IN/OUT:
  237. * x0..x7: byte-sliced AB state preloaded
  238. * mem_ab: byte-sliced AB state in memory
  239. * mem_cb: byte-sliced CD state in memory
  240. */
  241. #define two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  242. y6, y7, mem_ab, mem_cd, i, dir, store_ab) \
  243. leaq (key_table + (i) * 8)(CTX), %r9; \
  244. call roundsm32_x0_x1_x2_x3_x4_x5_x6_x7_y0_y1_y2_y3_y4_y5_y6_y7_cd; \
  245. \
  246. vmovdqu x0, 4 * 32(mem_cd); \
  247. vmovdqu x1, 5 * 32(mem_cd); \
  248. vmovdqu x2, 6 * 32(mem_cd); \
  249. vmovdqu x3, 7 * 32(mem_cd); \
  250. vmovdqu x4, 0 * 32(mem_cd); \
  251. vmovdqu x5, 1 * 32(mem_cd); \
  252. vmovdqu x6, 2 * 32(mem_cd); \
  253. vmovdqu x7, 3 * 32(mem_cd); \
  254. \
  255. leaq (key_table + ((i) + (dir)) * 8)(CTX), %r9; \
  256. call roundsm32_x4_x5_x6_x7_x0_x1_x2_x3_y4_y5_y6_y7_y0_y1_y2_y3_ab; \
  257. \
  258. store_ab(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab);
  259. #define dummy_store(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab) /* do nothing */
  260. #define store_ab_state(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab) \
  261. /* Store new AB state */ \
  262. vmovdqu x4, 4 * 32(mem_ab); \
  263. vmovdqu x5, 5 * 32(mem_ab); \
  264. vmovdqu x6, 6 * 32(mem_ab); \
  265. vmovdqu x7, 7 * 32(mem_ab); \
  266. vmovdqu x0, 0 * 32(mem_ab); \
  267. vmovdqu x1, 1 * 32(mem_ab); \
  268. vmovdqu x2, 2 * 32(mem_ab); \
  269. vmovdqu x3, 3 * 32(mem_ab);
  270. #define enc_rounds32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  271. y6, y7, mem_ab, mem_cd, i) \
  272. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  273. y6, y7, mem_ab, mem_cd, (i) + 2, 1, store_ab_state); \
  274. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  275. y6, y7, mem_ab, mem_cd, (i) + 4, 1, store_ab_state); \
  276. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  277. y6, y7, mem_ab, mem_cd, (i) + 6, 1, dummy_store);
  278. #define dec_rounds32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  279. y6, y7, mem_ab, mem_cd, i) \
  280. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  281. y6, y7, mem_ab, mem_cd, (i) + 7, -1, store_ab_state); \
  282. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  283. y6, y7, mem_ab, mem_cd, (i) + 5, -1, store_ab_state); \
  284. two_roundsm32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  285. y6, y7, mem_ab, mem_cd, (i) + 3, -1, dummy_store);
  286. /*
  287. * IN:
  288. * v0..3: byte-sliced 32-bit integers
  289. * OUT:
  290. * v0..3: (IN <<< 1)
  291. */
  292. #define rol32_1_32(v0, v1, v2, v3, t0, t1, t2, zero) \
  293. vpcmpgtb v0, zero, t0; \
  294. vpaddb v0, v0, v0; \
  295. vpabsb t0, t0; \
  296. \
  297. vpcmpgtb v1, zero, t1; \
  298. vpaddb v1, v1, v1; \
  299. vpabsb t1, t1; \
  300. \
  301. vpcmpgtb v2, zero, t2; \
  302. vpaddb v2, v2, v2; \
  303. vpabsb t2, t2; \
  304. \
  305. vpor t0, v1, v1; \
  306. \
  307. vpcmpgtb v3, zero, t0; \
  308. vpaddb v3, v3, v3; \
  309. vpabsb t0, t0; \
  310. \
  311. vpor t1, v2, v2; \
  312. vpor t2, v3, v3; \
  313. vpor t0, v0, v0;
  314. /*
  315. * IN:
  316. * r: byte-sliced AB state in memory
  317. * l: byte-sliced CD state in memory
  318. * OUT:
  319. * x0..x7: new byte-sliced CD state
  320. */
  321. #define fls32(l, l0, l1, l2, l3, l4, l5, l6, l7, r, t0, t1, t2, t3, tt0, \
  322. tt1, tt2, tt3, kll, klr, krl, krr) \
  323. /* \
  324. * t0 = kll; \
  325. * t0 &= ll; \
  326. * lr ^= rol32(t0, 1); \
  327. */ \
  328. vpbroadcastd kll, t0; /* only lowest 32-bit used */ \
  329. vpxor tt0, tt0, tt0; \
  330. vpshufb tt0, t0, t3; \
  331. vpsrldq $1, t0, t0; \
  332. vpshufb tt0, t0, t2; \
  333. vpsrldq $1, t0, t0; \
  334. vpshufb tt0, t0, t1; \
  335. vpsrldq $1, t0, t0; \
  336. vpshufb tt0, t0, t0; \
  337. \
  338. vpand l0, t0, t0; \
  339. vpand l1, t1, t1; \
  340. vpand l2, t2, t2; \
  341. vpand l3, t3, t3; \
  342. \
  343. rol32_1_32(t3, t2, t1, t0, tt1, tt2, tt3, tt0); \
  344. \
  345. vpxor l4, t0, l4; \
  346. vpbroadcastd krr, t0; /* only lowest 32-bit used */ \
  347. vmovdqu l4, 4 * 32(l); \
  348. vpxor l5, t1, l5; \
  349. vmovdqu l5, 5 * 32(l); \
  350. vpxor l6, t2, l6; \
  351. vmovdqu l6, 6 * 32(l); \
  352. vpxor l7, t3, l7; \
  353. vmovdqu l7, 7 * 32(l); \
  354. \
  355. /* \
  356. * t2 = krr; \
  357. * t2 |= rr; \
  358. * rl ^= t2; \
  359. */ \
  360. \
  361. vpshufb tt0, t0, t3; \
  362. vpsrldq $1, t0, t0; \
  363. vpshufb tt0, t0, t2; \
  364. vpsrldq $1, t0, t0; \
  365. vpshufb tt0, t0, t1; \
  366. vpsrldq $1, t0, t0; \
  367. vpshufb tt0, t0, t0; \
  368. \
  369. vpor 4 * 32(r), t0, t0; \
  370. vpor 5 * 32(r), t1, t1; \
  371. vpor 6 * 32(r), t2, t2; \
  372. vpor 7 * 32(r), t3, t3; \
  373. \
  374. vpxor 0 * 32(r), t0, t0; \
  375. vpxor 1 * 32(r), t1, t1; \
  376. vpxor 2 * 32(r), t2, t2; \
  377. vpxor 3 * 32(r), t3, t3; \
  378. vmovdqu t0, 0 * 32(r); \
  379. vpbroadcastd krl, t0; /* only lowest 32-bit used */ \
  380. vmovdqu t1, 1 * 32(r); \
  381. vmovdqu t2, 2 * 32(r); \
  382. vmovdqu t3, 3 * 32(r); \
  383. \
  384. /* \
  385. * t2 = krl; \
  386. * t2 &= rl; \
  387. * rr ^= rol32(t2, 1); \
  388. */ \
  389. vpshufb tt0, t0, t3; \
  390. vpsrldq $1, t0, t0; \
  391. vpshufb tt0, t0, t2; \
  392. vpsrldq $1, t0, t0; \
  393. vpshufb tt0, t0, t1; \
  394. vpsrldq $1, t0, t0; \
  395. vpshufb tt0, t0, t0; \
  396. \
  397. vpand 0 * 32(r), t0, t0; \
  398. vpand 1 * 32(r), t1, t1; \
  399. vpand 2 * 32(r), t2, t2; \
  400. vpand 3 * 32(r), t3, t3; \
  401. \
  402. rol32_1_32(t3, t2, t1, t0, tt1, tt2, tt3, tt0); \
  403. \
  404. vpxor 4 * 32(r), t0, t0; \
  405. vpxor 5 * 32(r), t1, t1; \
  406. vpxor 6 * 32(r), t2, t2; \
  407. vpxor 7 * 32(r), t3, t3; \
  408. vmovdqu t0, 4 * 32(r); \
  409. vpbroadcastd klr, t0; /* only lowest 32-bit used */ \
  410. vmovdqu t1, 5 * 32(r); \
  411. vmovdqu t2, 6 * 32(r); \
  412. vmovdqu t3, 7 * 32(r); \
  413. \
  414. /* \
  415. * t0 = klr; \
  416. * t0 |= lr; \
  417. * ll ^= t0; \
  418. */ \
  419. \
  420. vpshufb tt0, t0, t3; \
  421. vpsrldq $1, t0, t0; \
  422. vpshufb tt0, t0, t2; \
  423. vpsrldq $1, t0, t0; \
  424. vpshufb tt0, t0, t1; \
  425. vpsrldq $1, t0, t0; \
  426. vpshufb tt0, t0, t0; \
  427. \
  428. vpor l4, t0, t0; \
  429. vpor l5, t1, t1; \
  430. vpor l6, t2, t2; \
  431. vpor l7, t3, t3; \
  432. \
  433. vpxor l0, t0, l0; \
  434. vmovdqu l0, 0 * 32(l); \
  435. vpxor l1, t1, l1; \
  436. vmovdqu l1, 1 * 32(l); \
  437. vpxor l2, t2, l2; \
  438. vmovdqu l2, 2 * 32(l); \
  439. vpxor l3, t3, l3; \
  440. vmovdqu l3, 3 * 32(l);
  441. #define transpose_4x4(x0, x1, x2, x3, t1, t2) \
  442. vpunpckhdq x1, x0, t2; \
  443. vpunpckldq x1, x0, x0; \
  444. \
  445. vpunpckldq x3, x2, t1; \
  446. vpunpckhdq x3, x2, x2; \
  447. \
  448. vpunpckhqdq t1, x0, x1; \
  449. vpunpcklqdq t1, x0, x0; \
  450. \
  451. vpunpckhqdq x2, t2, x3; \
  452. vpunpcklqdq x2, t2, x2;
  453. #define byteslice_16x16b_fast(a0, b0, c0, d0, a1, b1, c1, d1, a2, b2, c2, d2, \
  454. a3, b3, c3, d3, st0, st1) \
  455. vmovdqu d2, st0; \
  456. vmovdqu d3, st1; \
  457. transpose_4x4(a0, a1, a2, a3, d2, d3); \
  458. transpose_4x4(b0, b1, b2, b3, d2, d3); \
  459. vmovdqu st0, d2; \
  460. vmovdqu st1, d3; \
  461. \
  462. vmovdqu a0, st0; \
  463. vmovdqu a1, st1; \
  464. transpose_4x4(c0, c1, c2, c3, a0, a1); \
  465. transpose_4x4(d0, d1, d2, d3, a0, a1); \
  466. \
  467. vbroadcasti128 .Lshufb_16x16b, a0; \
  468. vmovdqu st1, a1; \
  469. vpshufb a0, a2, a2; \
  470. vpshufb a0, a3, a3; \
  471. vpshufb a0, b0, b0; \
  472. vpshufb a0, b1, b1; \
  473. vpshufb a0, b2, b2; \
  474. vpshufb a0, b3, b3; \
  475. vpshufb a0, a1, a1; \
  476. vpshufb a0, c0, c0; \
  477. vpshufb a0, c1, c1; \
  478. vpshufb a0, c2, c2; \
  479. vpshufb a0, c3, c3; \
  480. vpshufb a0, d0, d0; \
  481. vpshufb a0, d1, d1; \
  482. vpshufb a0, d2, d2; \
  483. vpshufb a0, d3, d3; \
  484. vmovdqu d3, st1; \
  485. vmovdqu st0, d3; \
  486. vpshufb a0, d3, a0; \
  487. vmovdqu d2, st0; \
  488. \
  489. transpose_4x4(a0, b0, c0, d0, d2, d3); \
  490. transpose_4x4(a1, b1, c1, d1, d2, d3); \
  491. vmovdqu st0, d2; \
  492. vmovdqu st1, d3; \
  493. \
  494. vmovdqu b0, st0; \
  495. vmovdqu b1, st1; \
  496. transpose_4x4(a2, b2, c2, d2, b0, b1); \
  497. transpose_4x4(a3, b3, c3, d3, b0, b1); \
  498. vmovdqu st0, b0; \
  499. vmovdqu st1, b1; \
  500. /* does not adjust output bytes inside vectors */
  501. /* load blocks to registers and apply pre-whitening */
  502. #define inpack32_pre(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  503. y6, y7, rio, key) \
  504. vpbroadcastq key, x0; \
  505. vpshufb .Lpack_bswap, x0, x0; \
  506. \
  507. vpxor 0 * 32(rio), x0, y7; \
  508. vpxor 1 * 32(rio), x0, y6; \
  509. vpxor 2 * 32(rio), x0, y5; \
  510. vpxor 3 * 32(rio), x0, y4; \
  511. vpxor 4 * 32(rio), x0, y3; \
  512. vpxor 5 * 32(rio), x0, y2; \
  513. vpxor 6 * 32(rio), x0, y1; \
  514. vpxor 7 * 32(rio), x0, y0; \
  515. vpxor 8 * 32(rio), x0, x7; \
  516. vpxor 9 * 32(rio), x0, x6; \
  517. vpxor 10 * 32(rio), x0, x5; \
  518. vpxor 11 * 32(rio), x0, x4; \
  519. vpxor 12 * 32(rio), x0, x3; \
  520. vpxor 13 * 32(rio), x0, x2; \
  521. vpxor 14 * 32(rio), x0, x1; \
  522. vpxor 15 * 32(rio), x0, x0;
  523. /* byteslice pre-whitened blocks and store to temporary memory */
  524. #define inpack32_post(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  525. y6, y7, mem_ab, mem_cd) \
  526. byteslice_16x16b_fast(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, \
  527. y4, y5, y6, y7, (mem_ab), (mem_cd)); \
  528. \
  529. vmovdqu x0, 0 * 32(mem_ab); \
  530. vmovdqu x1, 1 * 32(mem_ab); \
  531. vmovdqu x2, 2 * 32(mem_ab); \
  532. vmovdqu x3, 3 * 32(mem_ab); \
  533. vmovdqu x4, 4 * 32(mem_ab); \
  534. vmovdqu x5, 5 * 32(mem_ab); \
  535. vmovdqu x6, 6 * 32(mem_ab); \
  536. vmovdqu x7, 7 * 32(mem_ab); \
  537. vmovdqu y0, 0 * 32(mem_cd); \
  538. vmovdqu y1, 1 * 32(mem_cd); \
  539. vmovdqu y2, 2 * 32(mem_cd); \
  540. vmovdqu y3, 3 * 32(mem_cd); \
  541. vmovdqu y4, 4 * 32(mem_cd); \
  542. vmovdqu y5, 5 * 32(mem_cd); \
  543. vmovdqu y6, 6 * 32(mem_cd); \
  544. vmovdqu y7, 7 * 32(mem_cd);
  545. /* de-byteslice, apply post-whitening and store blocks */
  546. #define outunpack32(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, \
  547. y5, y6, y7, key, stack_tmp0, stack_tmp1) \
  548. byteslice_16x16b_fast(y0, y4, x0, x4, y1, y5, x1, x5, y2, y6, x2, x6, \
  549. y3, y7, x3, x7, stack_tmp0, stack_tmp1); \
  550. \
  551. vmovdqu x0, stack_tmp0; \
  552. \
  553. vpbroadcastq key, x0; \
  554. vpshufb .Lpack_bswap, x0, x0; \
  555. \
  556. vpxor x0, y7, y7; \
  557. vpxor x0, y6, y6; \
  558. vpxor x0, y5, y5; \
  559. vpxor x0, y4, y4; \
  560. vpxor x0, y3, y3; \
  561. vpxor x0, y2, y2; \
  562. vpxor x0, y1, y1; \
  563. vpxor x0, y0, y0; \
  564. vpxor x0, x7, x7; \
  565. vpxor x0, x6, x6; \
  566. vpxor x0, x5, x5; \
  567. vpxor x0, x4, x4; \
  568. vpxor x0, x3, x3; \
  569. vpxor x0, x2, x2; \
  570. vpxor x0, x1, x1; \
  571. vpxor stack_tmp0, x0, x0;
  572. #define write_output(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
  573. y6, y7, rio) \
  574. vmovdqu x0, 0 * 32(rio); \
  575. vmovdqu x1, 1 * 32(rio); \
  576. vmovdqu x2, 2 * 32(rio); \
  577. vmovdqu x3, 3 * 32(rio); \
  578. vmovdqu x4, 4 * 32(rio); \
  579. vmovdqu x5, 5 * 32(rio); \
  580. vmovdqu x6, 6 * 32(rio); \
  581. vmovdqu x7, 7 * 32(rio); \
  582. vmovdqu y0, 8 * 32(rio); \
  583. vmovdqu y1, 9 * 32(rio); \
  584. vmovdqu y2, 10 * 32(rio); \
  585. vmovdqu y3, 11 * 32(rio); \
  586. vmovdqu y4, 12 * 32(rio); \
  587. vmovdqu y5, 13 * 32(rio); \
  588. vmovdqu y6, 14 * 32(rio); \
  589. vmovdqu y7, 15 * 32(rio);
  590. .data
  591. .align 32
  592. #define SHUFB_BYTES(idx) \
  593. 0 + (idx), 4 + (idx), 8 + (idx), 12 + (idx)
  594. .Lshufb_16x16b:
  595. .byte SHUFB_BYTES(0), SHUFB_BYTES(1), SHUFB_BYTES(2), SHUFB_BYTES(3)
  596. .byte SHUFB_BYTES(0), SHUFB_BYTES(1), SHUFB_BYTES(2), SHUFB_BYTES(3)
  597. .Lpack_bswap:
  598. .long 0x00010203, 0x04050607, 0x80808080, 0x80808080
  599. .long 0x00010203, 0x04050607, 0x80808080, 0x80808080
  600. /* For CTR-mode IV byteswap */
  601. .Lbswap128_mask:
  602. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  603. /* For XTS mode */
  604. .Lxts_gf128mul_and_shl1_mask_0:
  605. .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  606. .Lxts_gf128mul_and_shl1_mask_1:
  607. .byte 0x0e, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0
  608. /*
  609. * pre-SubByte transform
  610. *
  611. * pre-lookup for sbox1, sbox2, sbox3:
  612. * swap_bitendianness(
  613. * isom_map_camellia_to_aes(
  614. * camellia_f(
  615. * swap_bitendianess(in)
  616. * )
  617. * )
  618. * )
  619. *
  620. * (note: '⊕ 0xc5' inside camellia_f())
  621. */
  622. .Lpre_tf_lo_s1:
  623. .byte 0x45, 0xe8, 0x40, 0xed, 0x2e, 0x83, 0x2b, 0x86
  624. .byte 0x4b, 0xe6, 0x4e, 0xe3, 0x20, 0x8d, 0x25, 0x88
  625. .Lpre_tf_hi_s1:
  626. .byte 0x00, 0x51, 0xf1, 0xa0, 0x8a, 0xdb, 0x7b, 0x2a
  627. .byte 0x09, 0x58, 0xf8, 0xa9, 0x83, 0xd2, 0x72, 0x23
  628. /*
  629. * pre-SubByte transform
  630. *
  631. * pre-lookup for sbox4:
  632. * swap_bitendianness(
  633. * isom_map_camellia_to_aes(
  634. * camellia_f(
  635. * swap_bitendianess(in <<< 1)
  636. * )
  637. * )
  638. * )
  639. *
  640. * (note: '⊕ 0xc5' inside camellia_f())
  641. */
  642. .Lpre_tf_lo_s4:
  643. .byte 0x45, 0x40, 0x2e, 0x2b, 0x4b, 0x4e, 0x20, 0x25
  644. .byte 0x14, 0x11, 0x7f, 0x7a, 0x1a, 0x1f, 0x71, 0x74
  645. .Lpre_tf_hi_s4:
  646. .byte 0x00, 0xf1, 0x8a, 0x7b, 0x09, 0xf8, 0x83, 0x72
  647. .byte 0xad, 0x5c, 0x27, 0xd6, 0xa4, 0x55, 0x2e, 0xdf
  648. /*
  649. * post-SubByte transform
  650. *
  651. * post-lookup for sbox1, sbox4:
  652. * swap_bitendianness(
  653. * camellia_h(
  654. * isom_map_aes_to_camellia(
  655. * swap_bitendianness(
  656. * aes_inverse_affine_transform(in)
  657. * )
  658. * )
  659. * )
  660. * )
  661. *
  662. * (note: '⊕ 0x6e' inside camellia_h())
  663. */
  664. .Lpost_tf_lo_s1:
  665. .byte 0x3c, 0xcc, 0xcf, 0x3f, 0x32, 0xc2, 0xc1, 0x31
  666. .byte 0xdc, 0x2c, 0x2f, 0xdf, 0xd2, 0x22, 0x21, 0xd1
  667. .Lpost_tf_hi_s1:
  668. .byte 0x00, 0xf9, 0x86, 0x7f, 0xd7, 0x2e, 0x51, 0xa8
  669. .byte 0xa4, 0x5d, 0x22, 0xdb, 0x73, 0x8a, 0xf5, 0x0c
  670. /*
  671. * post-SubByte transform
  672. *
  673. * post-lookup for sbox2:
  674. * swap_bitendianness(
  675. * camellia_h(
  676. * isom_map_aes_to_camellia(
  677. * swap_bitendianness(
  678. * aes_inverse_affine_transform(in)
  679. * )
  680. * )
  681. * )
  682. * ) <<< 1
  683. *
  684. * (note: '⊕ 0x6e' inside camellia_h())
  685. */
  686. .Lpost_tf_lo_s2:
  687. .byte 0x78, 0x99, 0x9f, 0x7e, 0x64, 0x85, 0x83, 0x62
  688. .byte 0xb9, 0x58, 0x5e, 0xbf, 0xa5, 0x44, 0x42, 0xa3
  689. .Lpost_tf_hi_s2:
  690. .byte 0x00, 0xf3, 0x0d, 0xfe, 0xaf, 0x5c, 0xa2, 0x51
  691. .byte 0x49, 0xba, 0x44, 0xb7, 0xe6, 0x15, 0xeb, 0x18
  692. /*
  693. * post-SubByte transform
  694. *
  695. * post-lookup for sbox3:
  696. * swap_bitendianness(
  697. * camellia_h(
  698. * isom_map_aes_to_camellia(
  699. * swap_bitendianness(
  700. * aes_inverse_affine_transform(in)
  701. * )
  702. * )
  703. * )
  704. * ) >>> 1
  705. *
  706. * (note: '⊕ 0x6e' inside camellia_h())
  707. */
  708. .Lpost_tf_lo_s3:
  709. .byte 0x1e, 0x66, 0xe7, 0x9f, 0x19, 0x61, 0xe0, 0x98
  710. .byte 0x6e, 0x16, 0x97, 0xef, 0x69, 0x11, 0x90, 0xe8
  711. .Lpost_tf_hi_s3:
  712. .byte 0x00, 0xfc, 0x43, 0xbf, 0xeb, 0x17, 0xa8, 0x54
  713. .byte 0x52, 0xae, 0x11, 0xed, 0xb9, 0x45, 0xfa, 0x06
  714. /* For isolating SubBytes from AESENCLAST, inverse shift row */
  715. .Linv_shift_row:
  716. .byte 0x00, 0x0d, 0x0a, 0x07, 0x04, 0x01, 0x0e, 0x0b
  717. .byte 0x08, 0x05, 0x02, 0x0f, 0x0c, 0x09, 0x06, 0x03
  718. .align 4
  719. /* 4-bit mask */
  720. .L0f0f0f0f:
  721. .long 0x0f0f0f0f
  722. .text
  723. .align 8
  724. __camellia_enc_blk32:
  725. /* input:
  726. * %rdi: ctx, CTX
  727. * %rax: temporary storage, 512 bytes
  728. * %ymm0..%ymm15: 32 plaintext blocks
  729. * output:
  730. * %ymm0..%ymm15: 32 encrypted blocks, order swapped:
  731. * 7, 8, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8
  732. */
  733. FRAME_BEGIN
  734. leaq 8 * 32(%rax), %rcx;
  735. inpack32_post(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  736. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  737. %ymm15, %rax, %rcx);
  738. enc_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  739. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  740. %ymm15, %rax, %rcx, 0);
  741. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  742. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  743. %ymm15,
  744. ((key_table + (8) * 8) + 0)(CTX),
  745. ((key_table + (8) * 8) + 4)(CTX),
  746. ((key_table + (8) * 8) + 8)(CTX),
  747. ((key_table + (8) * 8) + 12)(CTX));
  748. enc_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  749. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  750. %ymm15, %rax, %rcx, 8);
  751. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  752. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  753. %ymm15,
  754. ((key_table + (16) * 8) + 0)(CTX),
  755. ((key_table + (16) * 8) + 4)(CTX),
  756. ((key_table + (16) * 8) + 8)(CTX),
  757. ((key_table + (16) * 8) + 12)(CTX));
  758. enc_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  759. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  760. %ymm15, %rax, %rcx, 16);
  761. movl $24, %r8d;
  762. cmpl $16, key_length(CTX);
  763. jne .Lenc_max32;
  764. .Lenc_done:
  765. /* load CD for output */
  766. vmovdqu 0 * 32(%rcx), %ymm8;
  767. vmovdqu 1 * 32(%rcx), %ymm9;
  768. vmovdqu 2 * 32(%rcx), %ymm10;
  769. vmovdqu 3 * 32(%rcx), %ymm11;
  770. vmovdqu 4 * 32(%rcx), %ymm12;
  771. vmovdqu 5 * 32(%rcx), %ymm13;
  772. vmovdqu 6 * 32(%rcx), %ymm14;
  773. vmovdqu 7 * 32(%rcx), %ymm15;
  774. outunpack32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  775. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  776. %ymm15, (key_table)(CTX, %r8, 8), (%rax), 1 * 32(%rax));
  777. FRAME_END
  778. ret;
  779. .align 8
  780. .Lenc_max32:
  781. movl $32, %r8d;
  782. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  783. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  784. %ymm15,
  785. ((key_table + (24) * 8) + 0)(CTX),
  786. ((key_table + (24) * 8) + 4)(CTX),
  787. ((key_table + (24) * 8) + 8)(CTX),
  788. ((key_table + (24) * 8) + 12)(CTX));
  789. enc_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  790. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  791. %ymm15, %rax, %rcx, 24);
  792. jmp .Lenc_done;
  793. ENDPROC(__camellia_enc_blk32)
  794. .align 8
  795. __camellia_dec_blk32:
  796. /* input:
  797. * %rdi: ctx, CTX
  798. * %rax: temporary storage, 512 bytes
  799. * %r8d: 24 for 16 byte key, 32 for larger
  800. * %ymm0..%ymm15: 16 encrypted blocks
  801. * output:
  802. * %ymm0..%ymm15: 16 plaintext blocks, order swapped:
  803. * 7, 8, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8
  804. */
  805. FRAME_BEGIN
  806. leaq 8 * 32(%rax), %rcx;
  807. inpack32_post(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  808. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  809. %ymm15, %rax, %rcx);
  810. cmpl $32, %r8d;
  811. je .Ldec_max32;
  812. .Ldec_max24:
  813. dec_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  814. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  815. %ymm15, %rax, %rcx, 16);
  816. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  817. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  818. %ymm15,
  819. ((key_table + (16) * 8) + 8)(CTX),
  820. ((key_table + (16) * 8) + 12)(CTX),
  821. ((key_table + (16) * 8) + 0)(CTX),
  822. ((key_table + (16) * 8) + 4)(CTX));
  823. dec_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  824. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  825. %ymm15, %rax, %rcx, 8);
  826. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  827. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  828. %ymm15,
  829. ((key_table + (8) * 8) + 8)(CTX),
  830. ((key_table + (8) * 8) + 12)(CTX),
  831. ((key_table + (8) * 8) + 0)(CTX),
  832. ((key_table + (8) * 8) + 4)(CTX));
  833. dec_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  834. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  835. %ymm15, %rax, %rcx, 0);
  836. /* load CD for output */
  837. vmovdqu 0 * 32(%rcx), %ymm8;
  838. vmovdqu 1 * 32(%rcx), %ymm9;
  839. vmovdqu 2 * 32(%rcx), %ymm10;
  840. vmovdqu 3 * 32(%rcx), %ymm11;
  841. vmovdqu 4 * 32(%rcx), %ymm12;
  842. vmovdqu 5 * 32(%rcx), %ymm13;
  843. vmovdqu 6 * 32(%rcx), %ymm14;
  844. vmovdqu 7 * 32(%rcx), %ymm15;
  845. outunpack32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  846. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  847. %ymm15, (key_table)(CTX), (%rax), 1 * 32(%rax));
  848. FRAME_END
  849. ret;
  850. .align 8
  851. .Ldec_max32:
  852. dec_rounds32(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  853. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  854. %ymm15, %rax, %rcx, 24);
  855. fls32(%rax, %ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  856. %rcx, %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  857. %ymm15,
  858. ((key_table + (24) * 8) + 8)(CTX),
  859. ((key_table + (24) * 8) + 12)(CTX),
  860. ((key_table + (24) * 8) + 0)(CTX),
  861. ((key_table + (24) * 8) + 4)(CTX));
  862. jmp .Ldec_max24;
  863. ENDPROC(__camellia_dec_blk32)
  864. ENTRY(camellia_ecb_enc_32way)
  865. /* input:
  866. * %rdi: ctx, CTX
  867. * %rsi: dst (32 blocks)
  868. * %rdx: src (32 blocks)
  869. */
  870. FRAME_BEGIN
  871. vzeroupper;
  872. inpack32_pre(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  873. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  874. %ymm15, %rdx, (key_table)(CTX));
  875. /* now dst can be used as temporary buffer (even in src == dst case) */
  876. movq %rsi, %rax;
  877. call __camellia_enc_blk32;
  878. write_output(%ymm7, %ymm6, %ymm5, %ymm4, %ymm3, %ymm2, %ymm1, %ymm0,
  879. %ymm15, %ymm14, %ymm13, %ymm12, %ymm11, %ymm10, %ymm9,
  880. %ymm8, %rsi);
  881. vzeroupper;
  882. FRAME_END
  883. ret;
  884. ENDPROC(camellia_ecb_enc_32way)
  885. ENTRY(camellia_ecb_dec_32way)
  886. /* input:
  887. * %rdi: ctx, CTX
  888. * %rsi: dst (32 blocks)
  889. * %rdx: src (32 blocks)
  890. */
  891. FRAME_BEGIN
  892. vzeroupper;
  893. cmpl $16, key_length(CTX);
  894. movl $32, %r8d;
  895. movl $24, %eax;
  896. cmovel %eax, %r8d; /* max */
  897. inpack32_pre(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  898. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  899. %ymm15, %rdx, (key_table)(CTX, %r8, 8));
  900. /* now dst can be used as temporary buffer (even in src == dst case) */
  901. movq %rsi, %rax;
  902. call __camellia_dec_blk32;
  903. write_output(%ymm7, %ymm6, %ymm5, %ymm4, %ymm3, %ymm2, %ymm1, %ymm0,
  904. %ymm15, %ymm14, %ymm13, %ymm12, %ymm11, %ymm10, %ymm9,
  905. %ymm8, %rsi);
  906. vzeroupper;
  907. FRAME_END
  908. ret;
  909. ENDPROC(camellia_ecb_dec_32way)
  910. ENTRY(camellia_cbc_dec_32way)
  911. /* input:
  912. * %rdi: ctx, CTX
  913. * %rsi: dst (32 blocks)
  914. * %rdx: src (32 blocks)
  915. */
  916. FRAME_BEGIN
  917. vzeroupper;
  918. cmpl $16, key_length(CTX);
  919. movl $32, %r8d;
  920. movl $24, %eax;
  921. cmovel %eax, %r8d; /* max */
  922. inpack32_pre(%ymm0, %ymm1, %ymm2, %ymm3, %ymm4, %ymm5, %ymm6, %ymm7,
  923. %ymm8, %ymm9, %ymm10, %ymm11, %ymm12, %ymm13, %ymm14,
  924. %ymm15, %rdx, (key_table)(CTX, %r8, 8));
  925. movq %rsp, %r10;
  926. cmpq %rsi, %rdx;
  927. je .Lcbc_dec_use_stack;
  928. /* dst can be used as temporary storage, src is not overwritten. */
  929. movq %rsi, %rax;
  930. jmp .Lcbc_dec_continue;
  931. .Lcbc_dec_use_stack:
  932. /*
  933. * dst still in-use (because dst == src), so use stack for temporary
  934. * storage.
  935. */
  936. subq $(16 * 32), %rsp;
  937. movq %rsp, %rax;
  938. .Lcbc_dec_continue:
  939. call __camellia_dec_blk32;
  940. vmovdqu %ymm7, (%rax);
  941. vpxor %ymm7, %ymm7, %ymm7;
  942. vinserti128 $1, (%rdx), %ymm7, %ymm7;
  943. vpxor (%rax), %ymm7, %ymm7;
  944. movq %r10, %rsp;
  945. vpxor (0 * 32 + 16)(%rdx), %ymm6, %ymm6;
  946. vpxor (1 * 32 + 16)(%rdx), %ymm5, %ymm5;
  947. vpxor (2 * 32 + 16)(%rdx), %ymm4, %ymm4;
  948. vpxor (3 * 32 + 16)(%rdx), %ymm3, %ymm3;
  949. vpxor (4 * 32 + 16)(%rdx), %ymm2, %ymm2;
  950. vpxor (5 * 32 + 16)(%rdx), %ymm1, %ymm1;
  951. vpxor (6 * 32 + 16)(%rdx), %ymm0, %ymm0;
  952. vpxor (7 * 32 + 16)(%rdx), %ymm15, %ymm15;
  953. vpxor (8 * 32 + 16)(%rdx), %ymm14, %ymm14;
  954. vpxor (9 * 32 + 16)(%rdx), %ymm13, %ymm13;
  955. vpxor (10 * 32 + 16)(%rdx), %ymm12, %ymm12;
  956. vpxor (11 * 32 + 16)(%rdx), %ymm11, %ymm11;
  957. vpxor (12 * 32 + 16)(%rdx), %ymm10, %ymm10;
  958. vpxor (13 * 32 + 16)(%rdx), %ymm9, %ymm9;
  959. vpxor (14 * 32 + 16)(%rdx), %ymm8, %ymm8;
  960. write_output(%ymm7, %ymm6, %ymm5, %ymm4, %ymm3, %ymm2, %ymm1, %ymm0,
  961. %ymm15, %ymm14, %ymm13, %ymm12, %ymm11, %ymm10, %ymm9,
  962. %ymm8, %rsi);
  963. vzeroupper;
  964. FRAME_END
  965. ret;
  966. ENDPROC(camellia_cbc_dec_32way)
  967. #define inc_le128(x, minus_one, tmp) \
  968. vpcmpeqq minus_one, x, tmp; \
  969. vpsubq minus_one, x, x; \
  970. vpslldq $8, tmp, tmp; \
  971. vpsubq tmp, x, x;
  972. #define add2_le128(x, minus_one, minus_two, tmp1, tmp2) \
  973. vpcmpeqq minus_one, x, tmp1; \
  974. vpcmpeqq minus_two, x, tmp2; \
  975. vpsubq minus_two, x, x; \
  976. vpor tmp2, tmp1, tmp1; \
  977. vpslldq $8, tmp1, tmp1; \
  978. vpsubq tmp1, x, x;
  979. ENTRY(camellia_ctr_32way)
  980. /* input:
  981. * %rdi: ctx, CTX
  982. * %rsi: dst (32 blocks)
  983. * %rdx: src (32 blocks)
  984. * %rcx: iv (little endian, 128bit)
  985. */
  986. FRAME_BEGIN
  987. vzeroupper;
  988. movq %rsp, %r10;
  989. cmpq %rsi, %rdx;
  990. je .Lctr_use_stack;
  991. /* dst can be used as temporary storage, src is not overwritten. */
  992. movq %rsi, %rax;
  993. jmp .Lctr_continue;
  994. .Lctr_use_stack:
  995. subq $(16 * 32), %rsp;
  996. movq %rsp, %rax;
  997. .Lctr_continue:
  998. vpcmpeqd %ymm15, %ymm15, %ymm15;
  999. vpsrldq $8, %ymm15, %ymm15; /* ab: -1:0 ; cd: -1:0 */
  1000. vpaddq %ymm15, %ymm15, %ymm12; /* ab: -2:0 ; cd: -2:0 */
  1001. /* load IV and byteswap */
  1002. vmovdqu (%rcx), %xmm0;
  1003. vmovdqa %xmm0, %xmm1;
  1004. inc_le128(%xmm0, %xmm15, %xmm14);
  1005. vbroadcasti128 .Lbswap128_mask, %ymm14;
  1006. vinserti128 $1, %xmm0, %ymm1, %ymm0;
  1007. vpshufb %ymm14, %ymm0, %ymm13;
  1008. vmovdqu %ymm13, 15 * 32(%rax);
  1009. /* construct IVs */
  1010. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13); /* ab:le2 ; cd:le3 */
  1011. vpshufb %ymm14, %ymm0, %ymm13;
  1012. vmovdqu %ymm13, 14 * 32(%rax);
  1013. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1014. vpshufb %ymm14, %ymm0, %ymm13;
  1015. vmovdqu %ymm13, 13 * 32(%rax);
  1016. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1017. vpshufb %ymm14, %ymm0, %ymm13;
  1018. vmovdqu %ymm13, 12 * 32(%rax);
  1019. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1020. vpshufb %ymm14, %ymm0, %ymm13;
  1021. vmovdqu %ymm13, 11 * 32(%rax);
  1022. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1023. vpshufb %ymm14, %ymm0, %ymm10;
  1024. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1025. vpshufb %ymm14, %ymm0, %ymm9;
  1026. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1027. vpshufb %ymm14, %ymm0, %ymm8;
  1028. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1029. vpshufb %ymm14, %ymm0, %ymm7;
  1030. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1031. vpshufb %ymm14, %ymm0, %ymm6;
  1032. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1033. vpshufb %ymm14, %ymm0, %ymm5;
  1034. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1035. vpshufb %ymm14, %ymm0, %ymm4;
  1036. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1037. vpshufb %ymm14, %ymm0, %ymm3;
  1038. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1039. vpshufb %ymm14, %ymm0, %ymm2;
  1040. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1041. vpshufb %ymm14, %ymm0, %ymm1;
  1042. add2_le128(%ymm0, %ymm15, %ymm12, %ymm11, %ymm13);
  1043. vextracti128 $1, %ymm0, %xmm13;
  1044. vpshufb %ymm14, %ymm0, %ymm0;
  1045. inc_le128(%xmm13, %xmm15, %xmm14);
  1046. vmovdqu %xmm13, (%rcx);
  1047. /* inpack32_pre: */
  1048. vpbroadcastq (key_table)(CTX), %ymm15;
  1049. vpshufb .Lpack_bswap, %ymm15, %ymm15;
  1050. vpxor %ymm0, %ymm15, %ymm0;
  1051. vpxor %ymm1, %ymm15, %ymm1;
  1052. vpxor %ymm2, %ymm15, %ymm2;
  1053. vpxor %ymm3, %ymm15, %ymm3;
  1054. vpxor %ymm4, %ymm15, %ymm4;
  1055. vpxor %ymm5, %ymm15, %ymm5;
  1056. vpxor %ymm6, %ymm15, %ymm6;
  1057. vpxor %ymm7, %ymm15, %ymm7;
  1058. vpxor %ymm8, %ymm15, %ymm8;
  1059. vpxor %ymm9, %ymm15, %ymm9;
  1060. vpxor %ymm10, %ymm15, %ymm10;
  1061. vpxor 11 * 32(%rax), %ymm15, %ymm11;
  1062. vpxor 12 * 32(%rax), %ymm15, %ymm12;
  1063. vpxor 13 * 32(%rax), %ymm15, %ymm13;
  1064. vpxor 14 * 32(%rax), %ymm15, %ymm14;
  1065. vpxor 15 * 32(%rax), %ymm15, %ymm15;
  1066. call __camellia_enc_blk32;
  1067. movq %r10, %rsp;
  1068. vpxor 0 * 32(%rdx), %ymm7, %ymm7;
  1069. vpxor 1 * 32(%rdx), %ymm6, %ymm6;
  1070. vpxor 2 * 32(%rdx), %ymm5, %ymm5;
  1071. vpxor 3 * 32(%rdx), %ymm4, %ymm4;
  1072. vpxor 4 * 32(%rdx), %ymm3, %ymm3;
  1073. vpxor 5 * 32(%rdx), %ymm2, %ymm2;
  1074. vpxor 6 * 32(%rdx), %ymm1, %ymm1;
  1075. vpxor 7 * 32(%rdx), %ymm0, %ymm0;
  1076. vpxor 8 * 32(%rdx), %ymm15, %ymm15;
  1077. vpxor 9 * 32(%rdx), %ymm14, %ymm14;
  1078. vpxor 10 * 32(%rdx), %ymm13, %ymm13;
  1079. vpxor 11 * 32(%rdx), %ymm12, %ymm12;
  1080. vpxor 12 * 32(%rdx), %ymm11, %ymm11;
  1081. vpxor 13 * 32(%rdx), %ymm10, %ymm10;
  1082. vpxor 14 * 32(%rdx), %ymm9, %ymm9;
  1083. vpxor 15 * 32(%rdx), %ymm8, %ymm8;
  1084. write_output(%ymm7, %ymm6, %ymm5, %ymm4, %ymm3, %ymm2, %ymm1, %ymm0,
  1085. %ymm15, %ymm14, %ymm13, %ymm12, %ymm11, %ymm10, %ymm9,
  1086. %ymm8, %rsi);
  1087. vzeroupper;
  1088. FRAME_END
  1089. ret;
  1090. ENDPROC(camellia_ctr_32way)
  1091. #define gf128mul_x_ble(iv, mask, tmp) \
  1092. vpsrad $31, iv, tmp; \
  1093. vpaddq iv, iv, iv; \
  1094. vpshufd $0x13, tmp, tmp; \
  1095. vpand mask, tmp, tmp; \
  1096. vpxor tmp, iv, iv;
  1097. #define gf128mul_x2_ble(iv, mask1, mask2, tmp0, tmp1) \
  1098. vpsrad $31, iv, tmp0; \
  1099. vpaddq iv, iv, tmp1; \
  1100. vpsllq $2, iv, iv; \
  1101. vpshufd $0x13, tmp0, tmp0; \
  1102. vpsrad $31, tmp1, tmp1; \
  1103. vpand mask2, tmp0, tmp0; \
  1104. vpshufd $0x13, tmp1, tmp1; \
  1105. vpxor tmp0, iv, iv; \
  1106. vpand mask1, tmp1, tmp1; \
  1107. vpxor tmp1, iv, iv;
  1108. .align 8
  1109. camellia_xts_crypt_32way:
  1110. /* input:
  1111. * %rdi: ctx, CTX
  1112. * %rsi: dst (32 blocks)
  1113. * %rdx: src (32 blocks)
  1114. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  1115. * %r8: index for input whitening key
  1116. * %r9: pointer to __camellia_enc_blk32 or __camellia_dec_blk32
  1117. */
  1118. FRAME_BEGIN
  1119. vzeroupper;
  1120. subq $(16 * 32), %rsp;
  1121. movq %rsp, %rax;
  1122. vbroadcasti128 .Lxts_gf128mul_and_shl1_mask_0, %ymm12;
  1123. /* load IV and construct second IV */
  1124. vmovdqu (%rcx), %xmm0;
  1125. vmovdqa %xmm0, %xmm15;
  1126. gf128mul_x_ble(%xmm0, %xmm12, %xmm13);
  1127. vbroadcasti128 .Lxts_gf128mul_and_shl1_mask_1, %ymm13;
  1128. vinserti128 $1, %xmm0, %ymm15, %ymm0;
  1129. vpxor 0 * 32(%rdx), %ymm0, %ymm15;
  1130. vmovdqu %ymm15, 15 * 32(%rax);
  1131. vmovdqu %ymm0, 0 * 32(%rsi);
  1132. /* construct IVs */
  1133. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1134. vpxor 1 * 32(%rdx), %ymm0, %ymm15;
  1135. vmovdqu %ymm15, 14 * 32(%rax);
  1136. vmovdqu %ymm0, 1 * 32(%rsi);
  1137. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1138. vpxor 2 * 32(%rdx), %ymm0, %ymm15;
  1139. vmovdqu %ymm15, 13 * 32(%rax);
  1140. vmovdqu %ymm0, 2 * 32(%rsi);
  1141. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1142. vpxor 3 * 32(%rdx), %ymm0, %ymm15;
  1143. vmovdqu %ymm15, 12 * 32(%rax);
  1144. vmovdqu %ymm0, 3 * 32(%rsi);
  1145. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1146. vpxor 4 * 32(%rdx), %ymm0, %ymm11;
  1147. vmovdqu %ymm0, 4 * 32(%rsi);
  1148. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1149. vpxor 5 * 32(%rdx), %ymm0, %ymm10;
  1150. vmovdqu %ymm0, 5 * 32(%rsi);
  1151. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1152. vpxor 6 * 32(%rdx), %ymm0, %ymm9;
  1153. vmovdqu %ymm0, 6 * 32(%rsi);
  1154. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1155. vpxor 7 * 32(%rdx), %ymm0, %ymm8;
  1156. vmovdqu %ymm0, 7 * 32(%rsi);
  1157. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1158. vpxor 8 * 32(%rdx), %ymm0, %ymm7;
  1159. vmovdqu %ymm0, 8 * 32(%rsi);
  1160. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1161. vpxor 9 * 32(%rdx), %ymm0, %ymm6;
  1162. vmovdqu %ymm0, 9 * 32(%rsi);
  1163. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1164. vpxor 10 * 32(%rdx), %ymm0, %ymm5;
  1165. vmovdqu %ymm0, 10 * 32(%rsi);
  1166. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1167. vpxor 11 * 32(%rdx), %ymm0, %ymm4;
  1168. vmovdqu %ymm0, 11 * 32(%rsi);
  1169. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1170. vpxor 12 * 32(%rdx), %ymm0, %ymm3;
  1171. vmovdqu %ymm0, 12 * 32(%rsi);
  1172. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1173. vpxor 13 * 32(%rdx), %ymm0, %ymm2;
  1174. vmovdqu %ymm0, 13 * 32(%rsi);
  1175. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1176. vpxor 14 * 32(%rdx), %ymm0, %ymm1;
  1177. vmovdqu %ymm0, 14 * 32(%rsi);
  1178. gf128mul_x2_ble(%ymm0, %ymm12, %ymm13, %ymm14, %ymm15);
  1179. vpxor 15 * 32(%rdx), %ymm0, %ymm15;
  1180. vmovdqu %ymm15, 0 * 32(%rax);
  1181. vmovdqu %ymm0, 15 * 32(%rsi);
  1182. vextracti128 $1, %ymm0, %xmm0;
  1183. gf128mul_x_ble(%xmm0, %xmm12, %xmm15);
  1184. vmovdqu %xmm0, (%rcx);
  1185. /* inpack32_pre: */
  1186. vpbroadcastq (key_table)(CTX, %r8, 8), %ymm15;
  1187. vpshufb .Lpack_bswap, %ymm15, %ymm15;
  1188. vpxor 0 * 32(%rax), %ymm15, %ymm0;
  1189. vpxor %ymm1, %ymm15, %ymm1;
  1190. vpxor %ymm2, %ymm15, %ymm2;
  1191. vpxor %ymm3, %ymm15, %ymm3;
  1192. vpxor %ymm4, %ymm15, %ymm4;
  1193. vpxor %ymm5, %ymm15, %ymm5;
  1194. vpxor %ymm6, %ymm15, %ymm6;
  1195. vpxor %ymm7, %ymm15, %ymm7;
  1196. vpxor %ymm8, %ymm15, %ymm8;
  1197. vpxor %ymm9, %ymm15, %ymm9;
  1198. vpxor %ymm10, %ymm15, %ymm10;
  1199. vpxor %ymm11, %ymm15, %ymm11;
  1200. vpxor 12 * 32(%rax), %ymm15, %ymm12;
  1201. vpxor 13 * 32(%rax), %ymm15, %ymm13;
  1202. vpxor 14 * 32(%rax), %ymm15, %ymm14;
  1203. vpxor 15 * 32(%rax), %ymm15, %ymm15;
  1204. CALL_NOSPEC %r9;
  1205. addq $(16 * 32), %rsp;
  1206. vpxor 0 * 32(%rsi), %ymm7, %ymm7;
  1207. vpxor 1 * 32(%rsi), %ymm6, %ymm6;
  1208. vpxor 2 * 32(%rsi), %ymm5, %ymm5;
  1209. vpxor 3 * 32(%rsi), %ymm4, %ymm4;
  1210. vpxor 4 * 32(%rsi), %ymm3, %ymm3;
  1211. vpxor 5 * 32(%rsi), %ymm2, %ymm2;
  1212. vpxor 6 * 32(%rsi), %ymm1, %ymm1;
  1213. vpxor 7 * 32(%rsi), %ymm0, %ymm0;
  1214. vpxor 8 * 32(%rsi), %ymm15, %ymm15;
  1215. vpxor 9 * 32(%rsi), %ymm14, %ymm14;
  1216. vpxor 10 * 32(%rsi), %ymm13, %ymm13;
  1217. vpxor 11 * 32(%rsi), %ymm12, %ymm12;
  1218. vpxor 12 * 32(%rsi), %ymm11, %ymm11;
  1219. vpxor 13 * 32(%rsi), %ymm10, %ymm10;
  1220. vpxor 14 * 32(%rsi), %ymm9, %ymm9;
  1221. vpxor 15 * 32(%rsi), %ymm8, %ymm8;
  1222. write_output(%ymm7, %ymm6, %ymm5, %ymm4, %ymm3, %ymm2, %ymm1, %ymm0,
  1223. %ymm15, %ymm14, %ymm13, %ymm12, %ymm11, %ymm10, %ymm9,
  1224. %ymm8, %rsi);
  1225. vzeroupper;
  1226. FRAME_END
  1227. ret;
  1228. ENDPROC(camellia_xts_crypt_32way)
  1229. ENTRY(camellia_xts_enc_32way)
  1230. /* input:
  1231. * %rdi: ctx, CTX
  1232. * %rsi: dst (32 blocks)
  1233. * %rdx: src (32 blocks)
  1234. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  1235. */
  1236. xorl %r8d, %r8d; /* input whitening key, 0 for enc */
  1237. leaq __camellia_enc_blk32, %r9;
  1238. jmp camellia_xts_crypt_32way;
  1239. ENDPROC(camellia_xts_enc_32way)
  1240. ENTRY(camellia_xts_dec_32way)
  1241. /* input:
  1242. * %rdi: ctx, CTX
  1243. * %rsi: dst (32 blocks)
  1244. * %rdx: src (32 blocks)
  1245. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  1246. */
  1247. cmpl $16, key_length(CTX);
  1248. movl $32, %r8d;
  1249. movl $24, %eax;
  1250. cmovel %eax, %r8d; /* input whitening key, last for dec */
  1251. leaq __camellia_dec_blk32, %r9;
  1252. jmp camellia_xts_crypt_32way;
  1253. ENDPROC(camellia_xts_dec_32way)