serpent-sse2-x86_64-asm_64.S 19 KB

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