twofish-avx-x86_64-asm_64.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Twofish Cipher 8-way parallel algorithm (AVX/x86_64)
  3. *
  4. * Copyright (C) 2012 Johannes Goetzfried
  5. * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
  6. *
  7. * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/linkage.h>
  26. #include "glue_helper-asm-avx.S"
  27. .file "twofish-avx-x86_64-asm_64.S"
  28. .data
  29. .align 16
  30. .Lbswap128_mask:
  31. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  32. .Lxts_gf128mul_and_shl1_mask:
  33. .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  34. .text
  35. /* structure of crypto context */
  36. #define s0 0
  37. #define s1 1024
  38. #define s2 2048
  39. #define s3 3072
  40. #define w 4096
  41. #define k 4128
  42. /**********************************************************************
  43. 8-way AVX twofish
  44. **********************************************************************/
  45. #define CTX %rdi
  46. #define RA1 %xmm0
  47. #define RB1 %xmm1
  48. #define RC1 %xmm2
  49. #define RD1 %xmm3
  50. #define RA2 %xmm4
  51. #define RB2 %xmm5
  52. #define RC2 %xmm6
  53. #define RD2 %xmm7
  54. #define RX0 %xmm8
  55. #define RY0 %xmm9
  56. #define RX1 %xmm10
  57. #define RY1 %xmm11
  58. #define RK1 %xmm12
  59. #define RK2 %xmm13
  60. #define RT %xmm14
  61. #define RR %xmm15
  62. #define RID1 %rbp
  63. #define RID1d %ebp
  64. #define RID2 %rsi
  65. #define RID2d %esi
  66. #define RGI1 %rdx
  67. #define RGI1bl %dl
  68. #define RGI1bh %dh
  69. #define RGI2 %rcx
  70. #define RGI2bl %cl
  71. #define RGI2bh %ch
  72. #define RGI3 %rax
  73. #define RGI3bl %al
  74. #define RGI3bh %ah
  75. #define RGI4 %rbx
  76. #define RGI4bl %bl
  77. #define RGI4bh %bh
  78. #define RGS1 %r8
  79. #define RGS1d %r8d
  80. #define RGS2 %r9
  81. #define RGS2d %r9d
  82. #define RGS3 %r10
  83. #define RGS3d %r10d
  84. #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
  85. movzbl src ## bl, RID1d; \
  86. movzbl src ## bh, RID2d; \
  87. shrq $16, src; \
  88. movl t0(CTX, RID1, 4), dst ## d; \
  89. movl t1(CTX, RID2, 4), RID2d; \
  90. movzbl src ## bl, RID1d; \
  91. xorl RID2d, dst ## d; \
  92. movzbl src ## bh, RID2d; \
  93. interleave_op(il_reg); \
  94. xorl t2(CTX, RID1, 4), dst ## d; \
  95. xorl t3(CTX, RID2, 4), dst ## d;
  96. #define dummy(d) /* do nothing */
  97. #define shr_next(reg) \
  98. shrq $16, reg;
  99. #define G(gi1, gi2, x, t0, t1, t2, t3) \
  100. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
  101. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
  102. \
  103. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
  104. shlq $32, RGS2; \
  105. orq RGS1, RGS2; \
  106. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
  107. shlq $32, RGS1; \
  108. orq RGS1, RGS3;
  109. #define round_head_2(a, b, x1, y1, x2, y2) \
  110. vmovq b ## 1, RGI3; \
  111. vpextrq $1, b ## 1, RGI4; \
  112. \
  113. G(RGI1, RGI2, x1, s0, s1, s2, s3); \
  114. vmovq a ## 2, RGI1; \
  115. vpextrq $1, a ## 2, RGI2; \
  116. vmovq RGS2, x1; \
  117. vpinsrq $1, RGS3, x1, x1; \
  118. \
  119. G(RGI3, RGI4, y1, s1, s2, s3, s0); \
  120. vmovq b ## 2, RGI3; \
  121. vpextrq $1, b ## 2, RGI4; \
  122. vmovq RGS2, y1; \
  123. vpinsrq $1, RGS3, y1, y1; \
  124. \
  125. G(RGI1, RGI2, x2, s0, s1, s2, s3); \
  126. vmovq RGS2, x2; \
  127. vpinsrq $1, RGS3, x2, x2; \
  128. \
  129. G(RGI3, RGI4, y2, s1, s2, s3, s0); \
  130. vmovq RGS2, y2; \
  131. vpinsrq $1, RGS3, y2, y2;
  132. #define encround_tail(a, b, c, d, x, y, prerotate) \
  133. vpaddd x, y, x; \
  134. vpaddd x, RK1, RT;\
  135. prerotate(b); \
  136. vpxor RT, c, c; \
  137. vpaddd y, x, y; \
  138. vpaddd y, RK2, y; \
  139. vpsrld $1, c, RT; \
  140. vpslld $(32 - 1), c, c; \
  141. vpor c, RT, c; \
  142. vpxor d, y, d; \
  143. #define decround_tail(a, b, c, d, x, y, prerotate) \
  144. vpaddd x, y, x; \
  145. vpaddd x, RK1, RT;\
  146. prerotate(a); \
  147. vpxor RT, c, c; \
  148. vpaddd y, x, y; \
  149. vpaddd y, RK2, y; \
  150. vpxor d, y, d; \
  151. vpsrld $1, d, y; \
  152. vpslld $(32 - 1), d, d; \
  153. vpor d, y, d; \
  154. #define rotate_1l(x) \
  155. vpslld $1, x, RR; \
  156. vpsrld $(32 - 1), x, x; \
  157. vpor x, RR, x;
  158. #define preload_rgi(c) \
  159. vmovq c, RGI1; \
  160. vpextrq $1, c, RGI2;
  161. #define encrypt_round(n, a, b, c, d, preload, prerotate) \
  162. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  163. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  164. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  165. encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  166. preload(c ## 1); \
  167. encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  168. #define decrypt_round(n, a, b, c, d, preload, prerotate) \
  169. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  170. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  171. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  172. decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  173. preload(c ## 1); \
  174. decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  175. #define encrypt_cycle(n) \
  176. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  177. encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
  178. #define encrypt_cycle_last(n) \
  179. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  180. encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
  181. #define decrypt_cycle(n) \
  182. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  183. decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
  184. #define decrypt_cycle_last(n) \
  185. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  186. decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
  187. #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  188. vpunpckldq x1, x0, t0; \
  189. vpunpckhdq x1, x0, t2; \
  190. vpunpckldq x3, x2, t1; \
  191. vpunpckhdq x3, x2, x3; \
  192. \
  193. vpunpcklqdq t1, t0, x0; \
  194. vpunpckhqdq t1, t0, x1; \
  195. vpunpcklqdq x3, t2, x2; \
  196. vpunpckhqdq x3, t2, x3;
  197. #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  198. vpxor x0, wkey, x0; \
  199. vpxor x1, wkey, x1; \
  200. vpxor x2, wkey, x2; \
  201. vpxor x3, wkey, x3; \
  202. \
  203. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  204. #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  205. transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  206. \
  207. vpxor x0, wkey, x0; \
  208. vpxor x1, wkey, x1; \
  209. vpxor x2, wkey, x2; \
  210. vpxor x3, wkey, x3;
  211. .align 8
  212. __twofish_enc_blk8:
  213. /* input:
  214. * %rdi: ctx, CTX
  215. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
  216. * output:
  217. * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
  218. */
  219. vmovdqu w(CTX), RK1;
  220. pushq %rbp;
  221. pushq %rbx;
  222. pushq %rcx;
  223. inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
  224. preload_rgi(RA1);
  225. rotate_1l(RD1);
  226. inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
  227. rotate_1l(RD2);
  228. encrypt_cycle(0);
  229. encrypt_cycle(1);
  230. encrypt_cycle(2);
  231. encrypt_cycle(3);
  232. encrypt_cycle(4);
  233. encrypt_cycle(5);
  234. encrypt_cycle(6);
  235. encrypt_cycle_last(7);
  236. vmovdqu (w+4*4)(CTX), RK1;
  237. popq %rcx;
  238. popq %rbx;
  239. popq %rbp;
  240. outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
  241. outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
  242. ret;
  243. ENDPROC(__twofish_enc_blk8)
  244. .align 8
  245. __twofish_dec_blk8:
  246. /* input:
  247. * %rdi: ctx, CTX
  248. * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
  249. * output:
  250. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
  251. */
  252. vmovdqu (w+4*4)(CTX), RK1;
  253. pushq %rbp;
  254. pushq %rbx;
  255. inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
  256. preload_rgi(RC1);
  257. rotate_1l(RA1);
  258. inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
  259. rotate_1l(RA2);
  260. decrypt_cycle(7);
  261. decrypt_cycle(6);
  262. decrypt_cycle(5);
  263. decrypt_cycle(4);
  264. decrypt_cycle(3);
  265. decrypt_cycle(2);
  266. decrypt_cycle(1);
  267. decrypt_cycle_last(0);
  268. vmovdqu (w)(CTX), RK1;
  269. popq %rbx;
  270. popq %rbp;
  271. outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
  272. outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
  273. ret;
  274. ENDPROC(__twofish_dec_blk8)
  275. ENTRY(twofish_ecb_enc_8way)
  276. /* input:
  277. * %rdi: ctx, CTX
  278. * %rsi: dst
  279. * %rdx: src
  280. */
  281. movq %rsi, %r11;
  282. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  283. call __twofish_enc_blk8;
  284. store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  285. ret;
  286. ENDPROC(twofish_ecb_enc_8way)
  287. ENTRY(twofish_ecb_dec_8way)
  288. /* input:
  289. * %rdi: ctx, CTX
  290. * %rsi: dst
  291. * %rdx: src
  292. */
  293. movq %rsi, %r11;
  294. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  295. call __twofish_dec_blk8;
  296. store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  297. ret;
  298. ENDPROC(twofish_ecb_dec_8way)
  299. ENTRY(twofish_cbc_dec_8way)
  300. /* input:
  301. * %rdi: ctx, CTX
  302. * %rsi: dst
  303. * %rdx: src
  304. */
  305. pushq %r12;
  306. movq %rsi, %r11;
  307. movq %rdx, %r12;
  308. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  309. call __twofish_dec_blk8;
  310. store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  311. popq %r12;
  312. ret;
  313. ENDPROC(twofish_cbc_dec_8way)
  314. ENTRY(twofish_ctr_8way)
  315. /* input:
  316. * %rdi: ctx, CTX
  317. * %rsi: dst
  318. * %rdx: src
  319. * %rcx: iv (little endian, 128bit)
  320. */
  321. pushq %r12;
  322. movq %rsi, %r11;
  323. movq %rdx, %r12;
  324. load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
  325. RD2, RX0, RX1, RY0);
  326. call __twofish_enc_blk8;
  327. store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  328. popq %r12;
  329. ret;
  330. ENDPROC(twofish_ctr_8way)
  331. ENTRY(twofish_xts_enc_8way)
  332. /* input:
  333. * %rdi: ctx, CTX
  334. * %rsi: dst
  335. * %rdx: src
  336. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  337. */
  338. movq %rsi, %r11;
  339. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  340. load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
  341. RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
  342. call __twofish_enc_blk8;
  343. /* dst <= regs xor IVs(in dst) */
  344. store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  345. ret;
  346. ENDPROC(twofish_xts_enc_8way)
  347. ENTRY(twofish_xts_dec_8way)
  348. /* input:
  349. * %rdi: ctx, CTX
  350. * %rsi: dst
  351. * %rdx: src
  352. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  353. */
  354. movq %rsi, %r11;
  355. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  356. load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2,
  357. RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
  358. call __twofish_dec_blk8;
  359. /* dst <= regs xor IVs(in dst) */
  360. store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  361. ret;
  362. ENDPROC(twofish_xts_dec_8way)