serpent-avx-x86_64-asm_64.S 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * Serpent Cipher 8-way parallel algorithm (x86_64/AVX)
  3. *
  4. * Copyright (C) 2012 Johannes Goetzfried
  5. * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
  6. *
  7. * Copyright © 2011-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/linkage.h>
  26. #include <asm/frame.h>
  27. #include "glue_helper-asm-avx.S"
  28. .file "serpent-avx-x86_64-asm_64.S"
  29. .data
  30. .align 16
  31. .Lbswap128_mask:
  32. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  33. .Lxts_gf128mul_and_shl1_mask:
  34. .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  35. .text
  36. #define CTX %rdi
  37. /**********************************************************************
  38. 8-way AVX serpent
  39. **********************************************************************/
  40. #define RA1 %xmm0
  41. #define RB1 %xmm1
  42. #define RC1 %xmm2
  43. #define RD1 %xmm3
  44. #define RE1 %xmm4
  45. #define tp %xmm5
  46. #define RA2 %xmm6
  47. #define RB2 %xmm7
  48. #define RC2 %xmm8
  49. #define RD2 %xmm9
  50. #define RE2 %xmm10
  51. #define RNOT %xmm11
  52. #define RK0 %xmm12
  53. #define RK1 %xmm13
  54. #define RK2 %xmm14
  55. #define RK3 %xmm15
  56. #define S0_1(x0, x1, x2, x3, x4) \
  57. vpor x0, x3, tp; \
  58. vpxor x3, x0, x0; \
  59. vpxor x2, x3, x4; \
  60. vpxor RNOT, x4, x4; \
  61. vpxor x1, tp, x3; \
  62. vpand x0, x1, x1; \
  63. vpxor x4, x1, x1; \
  64. vpxor x0, x2, x2;
  65. #define S0_2(x0, x1, x2, x3, x4) \
  66. vpxor x3, x0, x0; \
  67. vpor x0, x4, x4; \
  68. vpxor x2, x0, x0; \
  69. vpand x1, x2, x2; \
  70. vpxor x2, x3, x3; \
  71. vpxor RNOT, x1, x1; \
  72. vpxor x4, x2, x2; \
  73. vpxor x2, x1, x1;
  74. #define S1_1(x0, x1, x2, x3, x4) \
  75. vpxor x0, x1, tp; \
  76. vpxor x3, x0, x0; \
  77. vpxor RNOT, x3, x3; \
  78. vpand tp, x1, x4; \
  79. vpor tp, x0, x0; \
  80. vpxor x2, x3, x3; \
  81. vpxor x3, x0, x0; \
  82. vpxor x3, tp, x1;
  83. #define S1_2(x0, x1, x2, x3, x4) \
  84. vpxor x4, x3, x3; \
  85. vpor x4, x1, x1; \
  86. vpxor x2, x4, x4; \
  87. vpand x0, x2, x2; \
  88. vpxor x1, x2, x2; \
  89. vpor x0, x1, x1; \
  90. vpxor RNOT, x0, x0; \
  91. vpxor x2, x0, x0; \
  92. vpxor x1, x4, x4;
  93. #define S2_1(x0, x1, x2, x3, x4) \
  94. vpxor RNOT, x3, x3; \
  95. vpxor x0, x1, x1; \
  96. vpand x2, x0, tp; \
  97. vpxor x3, tp, tp; \
  98. vpor x0, x3, x3; \
  99. vpxor x1, x2, x2; \
  100. vpxor x1, x3, x3; \
  101. vpand tp, x1, x1;
  102. #define S2_2(x0, x1, x2, x3, x4) \
  103. vpxor x2, tp, tp; \
  104. vpand x3, x2, x2; \
  105. vpor x1, x3, x3; \
  106. vpxor RNOT, tp, tp; \
  107. vpxor tp, x3, x3; \
  108. vpxor tp, x0, x4; \
  109. vpxor x2, tp, x0; \
  110. vpor x2, x1, x1;
  111. #define S3_1(x0, x1, x2, x3, x4) \
  112. vpxor x3, x1, tp; \
  113. vpor x0, x3, x3; \
  114. vpand x0, x1, x4; \
  115. vpxor x2, x0, x0; \
  116. vpxor tp, x2, x2; \
  117. vpand x3, tp, x1; \
  118. vpxor x3, x2, x2; \
  119. vpor x4, x0, x0; \
  120. vpxor x3, x4, x4;
  121. #define S3_2(x0, x1, x2, x3, x4) \
  122. vpxor x0, x1, x1; \
  123. vpand x3, x0, x0; \
  124. vpand x4, x3, x3; \
  125. vpxor x2, x3, x3; \
  126. vpor x1, x4, x4; \
  127. vpand x1, x2, x2; \
  128. vpxor x3, x4, x4; \
  129. vpxor x3, x0, x0; \
  130. vpxor x2, x3, x3;
  131. #define S4_1(x0, x1, x2, x3, x4) \
  132. vpand x0, x3, tp; \
  133. vpxor x3, x0, x0; \
  134. vpxor x2, tp, tp; \
  135. vpor x3, x2, x2; \
  136. vpxor x1, x0, x0; \
  137. vpxor tp, x3, x4; \
  138. vpor x0, x2, x2; \
  139. vpxor x1, x2, x2;
  140. #define S4_2(x0, x1, x2, x3, x4) \
  141. vpand x0, x1, x1; \
  142. vpxor x4, x1, x1; \
  143. vpand x2, x4, x4; \
  144. vpxor tp, x2, x2; \
  145. vpxor x0, x4, x4; \
  146. vpor x1, tp, x3; \
  147. vpxor RNOT, x1, x1; \
  148. vpxor x0, x3, x3;
  149. #define S5_1(x0, x1, x2, x3, x4) \
  150. vpor x0, x1, tp; \
  151. vpxor tp, x2, x2; \
  152. vpxor RNOT, x3, x3; \
  153. vpxor x0, x1, x4; \
  154. vpxor x2, x0, x0; \
  155. vpand x4, tp, x1; \
  156. vpor x3, x4, x4; \
  157. vpxor x0, x4, x4;
  158. #define S5_2(x0, x1, x2, x3, x4) \
  159. vpand x3, x0, x0; \
  160. vpxor x3, x1, x1; \
  161. vpxor x2, x3, x3; \
  162. vpxor x1, x0, x0; \
  163. vpand x4, x2, x2; \
  164. vpxor x2, x1, x1; \
  165. vpand x0, x2, x2; \
  166. vpxor x2, x3, x3;
  167. #define S6_1(x0, x1, x2, x3, x4) \
  168. vpxor x0, x3, x3; \
  169. vpxor x2, x1, tp; \
  170. vpxor x0, x2, x2; \
  171. vpand x3, x0, x0; \
  172. vpor x3, tp, tp; \
  173. vpxor RNOT, x1, x4; \
  174. vpxor tp, x0, x0; \
  175. vpxor x2, tp, x1;
  176. #define S6_2(x0, x1, x2, x3, x4) \
  177. vpxor x4, x3, x3; \
  178. vpxor x0, x4, x4; \
  179. vpand x0, x2, x2; \
  180. vpxor x1, x4, x4; \
  181. vpxor x3, x2, x2; \
  182. vpand x1, x3, x3; \
  183. vpxor x0, x3, x3; \
  184. vpxor x2, x1, x1;
  185. #define S7_1(x0, x1, x2, x3, x4) \
  186. vpxor RNOT, x1, tp; \
  187. vpxor RNOT, x0, x0; \
  188. vpand x2, tp, x1; \
  189. vpxor x3, x1, x1; \
  190. vpor tp, x3, x3; \
  191. vpxor x2, tp, x4; \
  192. vpxor x3, x2, x2; \
  193. vpxor x0, x3, x3; \
  194. vpor x1, x0, x0;
  195. #define S7_2(x0, x1, x2, x3, x4) \
  196. vpand x0, x2, x2; \
  197. vpxor x4, x0, x0; \
  198. vpxor x3, x4, x4; \
  199. vpand x0, x3, x3; \
  200. vpxor x1, x4, x4; \
  201. vpxor x4, x2, x2; \
  202. vpxor x1, x3, x3; \
  203. vpor x0, x4, x4; \
  204. vpxor x1, x4, x4;
  205. #define SI0_1(x0, x1, x2, x3, x4) \
  206. vpxor x0, x1, x1; \
  207. vpor x1, x3, tp; \
  208. vpxor x1, x3, x4; \
  209. vpxor RNOT, x0, x0; \
  210. vpxor tp, x2, x2; \
  211. vpxor x0, tp, x3; \
  212. vpand x1, x0, x0; \
  213. vpxor x2, x0, x0;
  214. #define SI0_2(x0, x1, x2, x3, x4) \
  215. vpand x3, x2, x2; \
  216. vpxor x4, x3, x3; \
  217. vpxor x3, x2, x2; \
  218. vpxor x3, x1, x1; \
  219. vpand x0, x3, x3; \
  220. vpxor x0, x1, x1; \
  221. vpxor x2, x0, x0; \
  222. vpxor x3, x4, x4;
  223. #define SI1_1(x0, x1, x2, x3, x4) \
  224. vpxor x3, x1, x1; \
  225. vpxor x2, x0, tp; \
  226. vpxor RNOT, x2, x2; \
  227. vpor x1, x0, x4; \
  228. vpxor x3, x4, x4; \
  229. vpand x1, x3, x3; \
  230. vpxor x2, x1, x1; \
  231. vpand x4, x2, x2;
  232. #define SI1_2(x0, x1, x2, x3, x4) \
  233. vpxor x1, x4, x4; \
  234. vpor x3, x1, x1; \
  235. vpxor tp, x3, x3; \
  236. vpxor tp, x2, x2; \
  237. vpor x4, tp, x0; \
  238. vpxor x4, x2, x2; \
  239. vpxor x0, x1, x1; \
  240. vpxor x1, x4, x4;
  241. #define SI2_1(x0, x1, x2, x3, x4) \
  242. vpxor x1, x2, x2; \
  243. vpxor RNOT, x3, tp; \
  244. vpor x2, tp, tp; \
  245. vpxor x3, x2, x2; \
  246. vpxor x0, x3, x4; \
  247. vpxor x1, tp, x3; \
  248. vpor x2, x1, x1; \
  249. vpxor x0, x2, x2;
  250. #define SI2_2(x0, x1, x2, x3, x4) \
  251. vpxor x4, x1, x1; \
  252. vpor x3, x4, x4; \
  253. vpxor x3, x2, x2; \
  254. vpxor x2, x4, x4; \
  255. vpand x1, x2, x2; \
  256. vpxor x3, x2, x2; \
  257. vpxor x4, x3, x3; \
  258. vpxor x0, x4, x4;
  259. #define SI3_1(x0, x1, x2, x3, x4) \
  260. vpxor x1, x2, x2; \
  261. vpand x2, x1, tp; \
  262. vpxor x0, tp, tp; \
  263. vpor x1, x0, x0; \
  264. vpxor x3, x1, x4; \
  265. vpxor x3, x0, x0; \
  266. vpor tp, x3, x3; \
  267. vpxor x2, tp, x1;
  268. #define SI3_2(x0, x1, x2, x3, x4) \
  269. vpxor x3, x1, x1; \
  270. vpxor x2, x0, x0; \
  271. vpxor x3, x2, x2; \
  272. vpand x1, x3, x3; \
  273. vpxor x0, x1, x1; \
  274. vpand x2, x0, x0; \
  275. vpxor x3, x4, x4; \
  276. vpxor x0, x3, x3; \
  277. vpxor x1, x0, x0;
  278. #define SI4_1(x0, x1, x2, x3, x4) \
  279. vpxor x3, x2, x2; \
  280. vpand x1, x0, tp; \
  281. vpxor x2, tp, tp; \
  282. vpor x3, x2, x2; \
  283. vpxor RNOT, x0, x4; \
  284. vpxor tp, x1, x1; \
  285. vpxor x2, tp, x0; \
  286. vpand x4, x2, x2;
  287. #define SI4_2(x0, x1, x2, x3, x4) \
  288. vpxor x0, x2, x2; \
  289. vpor x4, x0, x0; \
  290. vpxor x3, x0, x0; \
  291. vpand x2, x3, x3; \
  292. vpxor x3, x4, x4; \
  293. vpxor x1, x3, x3; \
  294. vpand x0, x1, x1; \
  295. vpxor x1, x4, x4; \
  296. vpxor x3, x0, x0;
  297. #define SI5_1(x0, x1, x2, x3, x4) \
  298. vpor x2, x1, tp; \
  299. vpxor x1, x2, x2; \
  300. vpxor x3, tp, tp; \
  301. vpand x1, x3, x3; \
  302. vpxor x3, x2, x2; \
  303. vpor x0, x3, x3; \
  304. vpxor RNOT, x0, x0; \
  305. vpxor x2, x3, x3; \
  306. vpor x0, x2, x2;
  307. #define SI5_2(x0, x1, x2, x3, x4) \
  308. vpxor tp, x1, x4; \
  309. vpxor x4, x2, x2; \
  310. vpand x0, x4, x4; \
  311. vpxor tp, x0, x0; \
  312. vpxor x3, tp, x1; \
  313. vpand x2, x0, x0; \
  314. vpxor x3, x2, x2; \
  315. vpxor x2, x0, x0; \
  316. vpxor x4, x2, x2; \
  317. vpxor x3, x4, x4;
  318. #define SI6_1(x0, x1, x2, x3, x4) \
  319. vpxor x2, x0, x0; \
  320. vpand x3, x0, tp; \
  321. vpxor x3, x2, x2; \
  322. vpxor x2, tp, tp; \
  323. vpxor x1, x3, x3; \
  324. vpor x0, x2, x2; \
  325. vpxor x3, x2, x2; \
  326. vpand tp, x3, x3;
  327. #define SI6_2(x0, x1, x2, x3, x4) \
  328. vpxor RNOT, tp, tp; \
  329. vpxor x1, x3, x3; \
  330. vpand x2, x1, x1; \
  331. vpxor tp, x0, x4; \
  332. vpxor x4, x3, x3; \
  333. vpxor x2, x4, x4; \
  334. vpxor x1, tp, x0; \
  335. vpxor x0, x2, x2;
  336. #define SI7_1(x0, x1, x2, x3, x4) \
  337. vpand x0, x3, tp; \
  338. vpxor x2, x0, x0; \
  339. vpor x3, x2, x2; \
  340. vpxor x1, x3, x4; \
  341. vpxor RNOT, x0, x0; \
  342. vpor tp, x1, x1; \
  343. vpxor x0, x4, x4; \
  344. vpand x2, x0, x0; \
  345. vpxor x1, x0, x0;
  346. #define SI7_2(x0, x1, x2, x3, x4) \
  347. vpand x2, x1, x1; \
  348. vpxor x2, tp, x3; \
  349. vpxor x3, x4, x4; \
  350. vpand x3, x2, x2; \
  351. vpor x0, x3, x3; \
  352. vpxor x4, x1, x1; \
  353. vpxor x4, x3, x3; \
  354. vpand x0, x4, x4; \
  355. vpxor x2, x4, x4;
  356. #define get_key(i, j, t) \
  357. vbroadcastss (4*(i)+(j))*4(CTX), t;
  358. #define K2(x0, x1, x2, x3, x4, i) \
  359. get_key(i, 0, RK0); \
  360. get_key(i, 1, RK1); \
  361. get_key(i, 2, RK2); \
  362. get_key(i, 3, RK3); \
  363. vpxor RK0, x0 ## 1, x0 ## 1; \
  364. vpxor RK1, x1 ## 1, x1 ## 1; \
  365. vpxor RK2, x2 ## 1, x2 ## 1; \
  366. vpxor RK3, x3 ## 1, x3 ## 1; \
  367. vpxor RK0, x0 ## 2, x0 ## 2; \
  368. vpxor RK1, x1 ## 2, x1 ## 2; \
  369. vpxor RK2, x2 ## 2, x2 ## 2; \
  370. vpxor RK3, x3 ## 2, x3 ## 2;
  371. #define LK2(x0, x1, x2, x3, x4, i) \
  372. vpslld $13, x0 ## 1, x4 ## 1; \
  373. vpsrld $(32 - 13), x0 ## 1, x0 ## 1; \
  374. vpor x4 ## 1, x0 ## 1, x0 ## 1; \
  375. vpxor x0 ## 1, x1 ## 1, x1 ## 1; \
  376. vpslld $3, x2 ## 1, x4 ## 1; \
  377. vpsrld $(32 - 3), x2 ## 1, x2 ## 1; \
  378. vpor x4 ## 1, x2 ## 1, x2 ## 1; \
  379. vpxor x2 ## 1, x1 ## 1, x1 ## 1; \
  380. vpslld $13, x0 ## 2, x4 ## 2; \
  381. vpsrld $(32 - 13), x0 ## 2, x0 ## 2; \
  382. vpor x4 ## 2, x0 ## 2, x0 ## 2; \
  383. vpxor x0 ## 2, x1 ## 2, x1 ## 2; \
  384. vpslld $3, x2 ## 2, x4 ## 2; \
  385. vpsrld $(32 - 3), x2 ## 2, x2 ## 2; \
  386. vpor x4 ## 2, x2 ## 2, x2 ## 2; \
  387. vpxor x2 ## 2, x1 ## 2, x1 ## 2; \
  388. vpslld $1, x1 ## 1, x4 ## 1; \
  389. vpsrld $(32 - 1), x1 ## 1, x1 ## 1; \
  390. vpor x4 ## 1, x1 ## 1, x1 ## 1; \
  391. vpslld $3, x0 ## 1, x4 ## 1; \
  392. vpxor x2 ## 1, x3 ## 1, x3 ## 1; \
  393. vpxor x4 ## 1, x3 ## 1, x3 ## 1; \
  394. get_key(i, 1, RK1); \
  395. vpslld $1, x1 ## 2, x4 ## 2; \
  396. vpsrld $(32 - 1), x1 ## 2, x1 ## 2; \
  397. vpor x4 ## 2, x1 ## 2, x1 ## 2; \
  398. vpslld $3, x0 ## 2, x4 ## 2; \
  399. vpxor x2 ## 2, x3 ## 2, x3 ## 2; \
  400. vpxor x4 ## 2, x3 ## 2, x3 ## 2; \
  401. get_key(i, 3, RK3); \
  402. vpslld $7, x3 ## 1, x4 ## 1; \
  403. vpsrld $(32 - 7), x3 ## 1, x3 ## 1; \
  404. vpor x4 ## 1, x3 ## 1, x3 ## 1; \
  405. vpslld $7, x1 ## 1, x4 ## 1; \
  406. vpxor x1 ## 1, x0 ## 1, x0 ## 1; \
  407. vpxor x3 ## 1, x0 ## 1, x0 ## 1; \
  408. vpxor x3 ## 1, x2 ## 1, x2 ## 1; \
  409. vpxor x4 ## 1, x2 ## 1, x2 ## 1; \
  410. get_key(i, 0, RK0); \
  411. vpslld $7, x3 ## 2, x4 ## 2; \
  412. vpsrld $(32 - 7), x3 ## 2, x3 ## 2; \
  413. vpor x4 ## 2, x3 ## 2, x3 ## 2; \
  414. vpslld $7, x1 ## 2, x4 ## 2; \
  415. vpxor x1 ## 2, x0 ## 2, x0 ## 2; \
  416. vpxor x3 ## 2, x0 ## 2, x0 ## 2; \
  417. vpxor x3 ## 2, x2 ## 2, x2 ## 2; \
  418. vpxor x4 ## 2, x2 ## 2, x2 ## 2; \
  419. get_key(i, 2, RK2); \
  420. vpxor RK1, x1 ## 1, x1 ## 1; \
  421. vpxor RK3, x3 ## 1, x3 ## 1; \
  422. vpslld $5, x0 ## 1, x4 ## 1; \
  423. vpsrld $(32 - 5), x0 ## 1, x0 ## 1; \
  424. vpor x4 ## 1, x0 ## 1, x0 ## 1; \
  425. vpslld $22, x2 ## 1, x4 ## 1; \
  426. vpsrld $(32 - 22), x2 ## 1, x2 ## 1; \
  427. vpor x4 ## 1, x2 ## 1, x2 ## 1; \
  428. vpxor RK0, x0 ## 1, x0 ## 1; \
  429. vpxor RK2, x2 ## 1, x2 ## 1; \
  430. vpxor RK1, x1 ## 2, x1 ## 2; \
  431. vpxor RK3, x3 ## 2, x3 ## 2; \
  432. vpslld $5, x0 ## 2, x4 ## 2; \
  433. vpsrld $(32 - 5), x0 ## 2, x0 ## 2; \
  434. vpor x4 ## 2, x0 ## 2, x0 ## 2; \
  435. vpslld $22, x2 ## 2, x4 ## 2; \
  436. vpsrld $(32 - 22), x2 ## 2, x2 ## 2; \
  437. vpor x4 ## 2, x2 ## 2, x2 ## 2; \
  438. vpxor RK0, x0 ## 2, x0 ## 2; \
  439. vpxor RK2, x2 ## 2, x2 ## 2;
  440. #define KL2(x0, x1, x2, x3, x4, i) \
  441. vpxor RK0, x0 ## 1, x0 ## 1; \
  442. vpxor RK2, x2 ## 1, x2 ## 1; \
  443. vpsrld $5, x0 ## 1, x4 ## 1; \
  444. vpslld $(32 - 5), x0 ## 1, x0 ## 1; \
  445. vpor x4 ## 1, x0 ## 1, x0 ## 1; \
  446. vpxor RK3, x3 ## 1, x3 ## 1; \
  447. vpxor RK1, x1 ## 1, x1 ## 1; \
  448. vpsrld $22, x2 ## 1, x4 ## 1; \
  449. vpslld $(32 - 22), x2 ## 1, x2 ## 1; \
  450. vpor x4 ## 1, x2 ## 1, x2 ## 1; \
  451. vpxor x3 ## 1, x2 ## 1, x2 ## 1; \
  452. vpxor RK0, x0 ## 2, x0 ## 2; \
  453. vpxor RK2, x2 ## 2, x2 ## 2; \
  454. vpsrld $5, x0 ## 2, x4 ## 2; \
  455. vpslld $(32 - 5), x0 ## 2, x0 ## 2; \
  456. vpor x4 ## 2, x0 ## 2, x0 ## 2; \
  457. vpxor RK3, x3 ## 2, x3 ## 2; \
  458. vpxor RK1, x1 ## 2, x1 ## 2; \
  459. vpsrld $22, x2 ## 2, x4 ## 2; \
  460. vpslld $(32 - 22), x2 ## 2, x2 ## 2; \
  461. vpor x4 ## 2, x2 ## 2, x2 ## 2; \
  462. vpxor x3 ## 2, x2 ## 2, x2 ## 2; \
  463. vpxor x3 ## 1, x0 ## 1, x0 ## 1; \
  464. vpslld $7, x1 ## 1, x4 ## 1; \
  465. vpxor x1 ## 1, x0 ## 1, x0 ## 1; \
  466. vpxor x4 ## 1, x2 ## 1, x2 ## 1; \
  467. vpsrld $1, x1 ## 1, x4 ## 1; \
  468. vpslld $(32 - 1), x1 ## 1, x1 ## 1; \
  469. vpor x4 ## 1, x1 ## 1, x1 ## 1; \
  470. vpxor x3 ## 2, x0 ## 2, x0 ## 2; \
  471. vpslld $7, x1 ## 2, x4 ## 2; \
  472. vpxor x1 ## 2, x0 ## 2, x0 ## 2; \
  473. vpxor x4 ## 2, x2 ## 2, x2 ## 2; \
  474. vpsrld $1, x1 ## 2, x4 ## 2; \
  475. vpslld $(32 - 1), x1 ## 2, x1 ## 2; \
  476. vpor x4 ## 2, x1 ## 2, x1 ## 2; \
  477. vpsrld $7, x3 ## 1, x4 ## 1; \
  478. vpslld $(32 - 7), x3 ## 1, x3 ## 1; \
  479. vpor x4 ## 1, x3 ## 1, x3 ## 1; \
  480. vpxor x0 ## 1, x1 ## 1, x1 ## 1; \
  481. vpslld $3, x0 ## 1, x4 ## 1; \
  482. vpxor x4 ## 1, x3 ## 1, x3 ## 1; \
  483. vpsrld $7, x3 ## 2, x4 ## 2; \
  484. vpslld $(32 - 7), x3 ## 2, x3 ## 2; \
  485. vpor x4 ## 2, x3 ## 2, x3 ## 2; \
  486. vpxor x0 ## 2, x1 ## 2, x1 ## 2; \
  487. vpslld $3, x0 ## 2, x4 ## 2; \
  488. vpxor x4 ## 2, x3 ## 2, x3 ## 2; \
  489. vpsrld $13, x0 ## 1, x4 ## 1; \
  490. vpslld $(32 - 13), x0 ## 1, x0 ## 1; \
  491. vpor x4 ## 1, x0 ## 1, x0 ## 1; \
  492. vpxor x2 ## 1, x1 ## 1, x1 ## 1; \
  493. vpxor x2 ## 1, x3 ## 1, x3 ## 1; \
  494. vpsrld $3, x2 ## 1, x4 ## 1; \
  495. vpslld $(32 - 3), x2 ## 1, x2 ## 1; \
  496. vpor x4 ## 1, x2 ## 1, x2 ## 1; \
  497. vpsrld $13, x0 ## 2, x4 ## 2; \
  498. vpslld $(32 - 13), x0 ## 2, x0 ## 2; \
  499. vpor x4 ## 2, x0 ## 2, x0 ## 2; \
  500. vpxor x2 ## 2, x1 ## 2, x1 ## 2; \
  501. vpxor x2 ## 2, x3 ## 2, x3 ## 2; \
  502. vpsrld $3, x2 ## 2, x4 ## 2; \
  503. vpslld $(32 - 3), x2 ## 2, x2 ## 2; \
  504. vpor x4 ## 2, x2 ## 2, x2 ## 2;
  505. #define S(SBOX, x0, x1, x2, x3, x4) \
  506. SBOX ## _1(x0 ## 1, x1 ## 1, x2 ## 1, x3 ## 1, x4 ## 1); \
  507. SBOX ## _2(x0 ## 1, x1 ## 1, x2 ## 1, x3 ## 1, x4 ## 1); \
  508. SBOX ## _1(x0 ## 2, x1 ## 2, x2 ## 2, x3 ## 2, x4 ## 2); \
  509. SBOX ## _2(x0 ## 2, x1 ## 2, x2 ## 2, x3 ## 2, x4 ## 2);
  510. #define SP(SBOX, x0, x1, x2, x3, x4, i) \
  511. get_key(i, 0, RK0); \
  512. SBOX ## _1(x0 ## 1, x1 ## 1, x2 ## 1, x3 ## 1, x4 ## 1); \
  513. get_key(i, 2, RK2); \
  514. SBOX ## _2(x0 ## 1, x1 ## 1, x2 ## 1, x3 ## 1, x4 ## 1); \
  515. get_key(i, 3, RK3); \
  516. SBOX ## _1(x0 ## 2, x1 ## 2, x2 ## 2, x3 ## 2, x4 ## 2); \
  517. get_key(i, 1, RK1); \
  518. SBOX ## _2(x0 ## 2, x1 ## 2, x2 ## 2, x3 ## 2, x4 ## 2); \
  519. #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  520. vpunpckldq x1, x0, t0; \
  521. vpunpckhdq x1, x0, t2; \
  522. vpunpckldq x3, x2, t1; \
  523. vpunpckhdq x3, x2, x3; \
  524. \
  525. vpunpcklqdq t1, t0, x0; \
  526. vpunpckhqdq t1, t0, x1; \
  527. vpunpcklqdq x3, t2, x2; \
  528. vpunpckhqdq x3, t2, x3;
  529. #define read_blocks(x0, x1, x2, x3, t0, t1, t2) \
  530. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  531. #define write_blocks(x0, x1, x2, x3, t0, t1, t2) \
  532. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  533. .align 8
  534. __serpent_enc_blk8_avx:
  535. /* input:
  536. * %rdi: ctx, CTX
  537. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
  538. * output:
  539. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: encrypted blocks
  540. */
  541. vpcmpeqd RNOT, RNOT, RNOT;
  542. read_blocks(RA1, RB1, RC1, RD1, RK0, RK1, RK2);
  543. read_blocks(RA2, RB2, RC2, RD2, RK0, RK1, RK2);
  544. K2(RA, RB, RC, RD, RE, 0);
  545. S(S0, RA, RB, RC, RD, RE); LK2(RC, RB, RD, RA, RE, 1);
  546. S(S1, RC, RB, RD, RA, RE); LK2(RE, RD, RA, RC, RB, 2);
  547. S(S2, RE, RD, RA, RC, RB); LK2(RB, RD, RE, RC, RA, 3);
  548. S(S3, RB, RD, RE, RC, RA); LK2(RC, RA, RD, RB, RE, 4);
  549. S(S4, RC, RA, RD, RB, RE); LK2(RA, RD, RB, RE, RC, 5);
  550. S(S5, RA, RD, RB, RE, RC); LK2(RC, RA, RD, RE, RB, 6);
  551. S(S6, RC, RA, RD, RE, RB); LK2(RD, RB, RA, RE, RC, 7);
  552. S(S7, RD, RB, RA, RE, RC); LK2(RC, RA, RE, RD, RB, 8);
  553. S(S0, RC, RA, RE, RD, RB); LK2(RE, RA, RD, RC, RB, 9);
  554. S(S1, RE, RA, RD, RC, RB); LK2(RB, RD, RC, RE, RA, 10);
  555. S(S2, RB, RD, RC, RE, RA); LK2(RA, RD, RB, RE, RC, 11);
  556. S(S3, RA, RD, RB, RE, RC); LK2(RE, RC, RD, RA, RB, 12);
  557. S(S4, RE, RC, RD, RA, RB); LK2(RC, RD, RA, RB, RE, 13);
  558. S(S5, RC, RD, RA, RB, RE); LK2(RE, RC, RD, RB, RA, 14);
  559. S(S6, RE, RC, RD, RB, RA); LK2(RD, RA, RC, RB, RE, 15);
  560. S(S7, RD, RA, RC, RB, RE); LK2(RE, RC, RB, RD, RA, 16);
  561. S(S0, RE, RC, RB, RD, RA); LK2(RB, RC, RD, RE, RA, 17);
  562. S(S1, RB, RC, RD, RE, RA); LK2(RA, RD, RE, RB, RC, 18);
  563. S(S2, RA, RD, RE, RB, RC); LK2(RC, RD, RA, RB, RE, 19);
  564. S(S3, RC, RD, RA, RB, RE); LK2(RB, RE, RD, RC, RA, 20);
  565. S(S4, RB, RE, RD, RC, RA); LK2(RE, RD, RC, RA, RB, 21);
  566. S(S5, RE, RD, RC, RA, RB); LK2(RB, RE, RD, RA, RC, 22);
  567. S(S6, RB, RE, RD, RA, RC); LK2(RD, RC, RE, RA, RB, 23);
  568. S(S7, RD, RC, RE, RA, RB); LK2(RB, RE, RA, RD, RC, 24);
  569. S(S0, RB, RE, RA, RD, RC); LK2(RA, RE, RD, RB, RC, 25);
  570. S(S1, RA, RE, RD, RB, RC); LK2(RC, RD, RB, RA, RE, 26);
  571. S(S2, RC, RD, RB, RA, RE); LK2(RE, RD, RC, RA, RB, 27);
  572. S(S3, RE, RD, RC, RA, RB); LK2(RA, RB, RD, RE, RC, 28);
  573. S(S4, RA, RB, RD, RE, RC); LK2(RB, RD, RE, RC, RA, 29);
  574. S(S5, RB, RD, RE, RC, RA); LK2(RA, RB, RD, RC, RE, 30);
  575. S(S6, RA, RB, RD, RC, RE); LK2(RD, RE, RB, RC, RA, 31);
  576. S(S7, RD, RE, RB, RC, RA); K2(RA, RB, RC, RD, RE, 32);
  577. write_blocks(RA1, RB1, RC1, RD1, RK0, RK1, RK2);
  578. write_blocks(RA2, RB2, RC2, RD2, RK0, RK1, RK2);
  579. ret;
  580. ENDPROC(__serpent_enc_blk8_avx)
  581. .align 8
  582. __serpent_dec_blk8_avx:
  583. /* input:
  584. * %rdi: ctx, CTX
  585. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: encrypted blocks
  586. * output:
  587. * RC1, RD1, RB1, RE1, RC2, RD2, RB2, RE2: decrypted blocks
  588. */
  589. vpcmpeqd RNOT, RNOT, RNOT;
  590. read_blocks(RA1, RB1, RC1, RD1, RK0, RK1, RK2);
  591. read_blocks(RA2, RB2, RC2, RD2, RK0, RK1, RK2);
  592. K2(RA, RB, RC, RD, RE, 32);
  593. SP(SI7, RA, RB, RC, RD, RE, 31); KL2(RB, RD, RA, RE, RC, 31);
  594. SP(SI6, RB, RD, RA, RE, RC, 30); KL2(RA, RC, RE, RB, RD, 30);
  595. SP(SI5, RA, RC, RE, RB, RD, 29); KL2(RC, RD, RA, RE, RB, 29);
  596. SP(SI4, RC, RD, RA, RE, RB, 28); KL2(RC, RA, RB, RE, RD, 28);
  597. SP(SI3, RC, RA, RB, RE, RD, 27); KL2(RB, RC, RD, RE, RA, 27);
  598. SP(SI2, RB, RC, RD, RE, RA, 26); KL2(RC, RA, RE, RD, RB, 26);
  599. SP(SI1, RC, RA, RE, RD, RB, 25); KL2(RB, RA, RE, RD, RC, 25);
  600. SP(SI0, RB, RA, RE, RD, RC, 24); KL2(RE, RC, RA, RB, RD, 24);
  601. SP(SI7, RE, RC, RA, RB, RD, 23); KL2(RC, RB, RE, RD, RA, 23);
  602. SP(SI6, RC, RB, RE, RD, RA, 22); KL2(RE, RA, RD, RC, RB, 22);
  603. SP(SI5, RE, RA, RD, RC, RB, 21); KL2(RA, RB, RE, RD, RC, 21);
  604. SP(SI4, RA, RB, RE, RD, RC, 20); KL2(RA, RE, RC, RD, RB, 20);
  605. SP(SI3, RA, RE, RC, RD, RB, 19); KL2(RC, RA, RB, RD, RE, 19);
  606. SP(SI2, RC, RA, RB, RD, RE, 18); KL2(RA, RE, RD, RB, RC, 18);
  607. SP(SI1, RA, RE, RD, RB, RC, 17); KL2(RC, RE, RD, RB, RA, 17);
  608. SP(SI0, RC, RE, RD, RB, RA, 16); KL2(RD, RA, RE, RC, RB, 16);
  609. SP(SI7, RD, RA, RE, RC, RB, 15); KL2(RA, RC, RD, RB, RE, 15);
  610. SP(SI6, RA, RC, RD, RB, RE, 14); KL2(RD, RE, RB, RA, RC, 14);
  611. SP(SI5, RD, RE, RB, RA, RC, 13); KL2(RE, RC, RD, RB, RA, 13);
  612. SP(SI4, RE, RC, RD, RB, RA, 12); KL2(RE, RD, RA, RB, RC, 12);
  613. SP(SI3, RE, RD, RA, RB, RC, 11); KL2(RA, RE, RC, RB, RD, 11);
  614. SP(SI2, RA, RE, RC, RB, RD, 10); KL2(RE, RD, RB, RC, RA, 10);
  615. SP(SI1, RE, RD, RB, RC, RA, 9); KL2(RA, RD, RB, RC, RE, 9);
  616. SP(SI0, RA, RD, RB, RC, RE, 8); KL2(RB, RE, RD, RA, RC, 8);
  617. SP(SI7, RB, RE, RD, RA, RC, 7); KL2(RE, RA, RB, RC, RD, 7);
  618. SP(SI6, RE, RA, RB, RC, RD, 6); KL2(RB, RD, RC, RE, RA, 6);
  619. SP(SI5, RB, RD, RC, RE, RA, 5); KL2(RD, RA, RB, RC, RE, 5);
  620. SP(SI4, RD, RA, RB, RC, RE, 4); KL2(RD, RB, RE, RC, RA, 4);
  621. SP(SI3, RD, RB, RE, RC, RA, 3); KL2(RE, RD, RA, RC, RB, 3);
  622. SP(SI2, RE, RD, RA, RC, RB, 2); KL2(RD, RB, RC, RA, RE, 2);
  623. SP(SI1, RD, RB, RC, RA, RE, 1); KL2(RE, RB, RC, RA, RD, 1);
  624. S(SI0, RE, RB, RC, RA, RD); K2(RC, RD, RB, RE, RA, 0);
  625. write_blocks(RC1, RD1, RB1, RE1, RK0, RK1, RK2);
  626. write_blocks(RC2, RD2, RB2, RE2, RK0, RK1, RK2);
  627. ret;
  628. ENDPROC(__serpent_dec_blk8_avx)
  629. ENTRY(serpent_ecb_enc_8way_avx)
  630. /* input:
  631. * %rdi: ctx, CTX
  632. * %rsi: dst
  633. * %rdx: src
  634. */
  635. FRAME_BEGIN
  636. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  637. call __serpent_enc_blk8_avx;
  638. store_8way(%rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  639. FRAME_END
  640. ret;
  641. ENDPROC(serpent_ecb_enc_8way_avx)
  642. ENTRY(serpent_ecb_dec_8way_avx)
  643. /* input:
  644. * %rdi: ctx, CTX
  645. * %rsi: dst
  646. * %rdx: src
  647. */
  648. FRAME_BEGIN
  649. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  650. call __serpent_dec_blk8_avx;
  651. store_8way(%rsi, RC1, RD1, RB1, RE1, RC2, RD2, RB2, RE2);
  652. FRAME_END
  653. ret;
  654. ENDPROC(serpent_ecb_dec_8way_avx)
  655. ENTRY(serpent_cbc_dec_8way_avx)
  656. /* input:
  657. * %rdi: ctx, CTX
  658. * %rsi: dst
  659. * %rdx: src
  660. */
  661. FRAME_BEGIN
  662. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  663. call __serpent_dec_blk8_avx;
  664. store_cbc_8way(%rdx, %rsi, RC1, RD1, RB1, RE1, RC2, RD2, RB2, RE2);
  665. FRAME_END
  666. ret;
  667. ENDPROC(serpent_cbc_dec_8way_avx)
  668. ENTRY(serpent_ctr_8way_avx)
  669. /* input:
  670. * %rdi: ctx, CTX
  671. * %rsi: dst
  672. * %rdx: src
  673. * %rcx: iv (little endian, 128bit)
  674. */
  675. FRAME_BEGIN
  676. load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
  677. RD2, RK0, RK1, RK2);
  678. call __serpent_enc_blk8_avx;
  679. store_ctr_8way(%rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  680. FRAME_END
  681. ret;
  682. ENDPROC(serpent_ctr_8way_avx)
  683. ENTRY(serpent_xts_enc_8way_avx)
  684. /* input:
  685. * %rdi: ctx, CTX
  686. * %rsi: dst
  687. * %rdx: src
  688. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  689. */
  690. FRAME_BEGIN
  691. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  692. load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
  693. RK0, RK1, RK2, .Lxts_gf128mul_and_shl1_mask);
  694. call __serpent_enc_blk8_avx;
  695. /* dst <= regs xor IVs(in dst) */
  696. store_xts_8way(%rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  697. FRAME_END
  698. ret;
  699. ENDPROC(serpent_xts_enc_8way_avx)
  700. ENTRY(serpent_xts_dec_8way_avx)
  701. /* input:
  702. * %rdi: ctx, CTX
  703. * %rsi: dst
  704. * %rdx: src
  705. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  706. */
  707. FRAME_BEGIN
  708. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  709. load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
  710. RK0, RK1, RK2, .Lxts_gf128mul_and_shl1_mask);
  711. call __serpent_dec_blk8_avx;
  712. /* dst <= regs xor IVs(in dst) */
  713. store_xts_8way(%rsi, RC1, RD1, RB1, RE1, RC2, RD2, RB2, RE2);
  714. FRAME_END
  715. ret;
  716. ENDPROC(serpent_xts_dec_8way_avx)