camellia-aesni-avx-asm_64.S 34 KB

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