serpent-avx2-asm_64.S 23 KB

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