camellia-aesni-avx-asm_64.S 33 KB

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