serpent-sse2-x86_64-asm_64.S 19 KB

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