serpent-avx2-asm_64.S 23 KB

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