twofish-avx-x86_64-asm_64.S 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 Jussi Kivilinna <jussi.kivilinna@mbnet.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. .text
  33. /* structure of crypto context */
  34. #define s0 0
  35. #define s1 1024
  36. #define s2 2048
  37. #define s3 3072
  38. #define w 4096
  39. #define k 4128
  40. /**********************************************************************
  41. 8-way AVX twofish
  42. **********************************************************************/
  43. #define CTX %rdi
  44. #define RA1 %xmm0
  45. #define RB1 %xmm1
  46. #define RC1 %xmm2
  47. #define RD1 %xmm3
  48. #define RA2 %xmm4
  49. #define RB2 %xmm5
  50. #define RC2 %xmm6
  51. #define RD2 %xmm7
  52. #define RX0 %xmm8
  53. #define RY0 %xmm9
  54. #define RX1 %xmm10
  55. #define RY1 %xmm11
  56. #define RK1 %xmm12
  57. #define RK2 %xmm13
  58. #define RT %xmm14
  59. #define RR %xmm15
  60. #define RID1 %rbp
  61. #define RID1d %ebp
  62. #define RID2 %rsi
  63. #define RID2d %esi
  64. #define RGI1 %rdx
  65. #define RGI1bl %dl
  66. #define RGI1bh %dh
  67. #define RGI2 %rcx
  68. #define RGI2bl %cl
  69. #define RGI2bh %ch
  70. #define RGI3 %rax
  71. #define RGI3bl %al
  72. #define RGI3bh %ah
  73. #define RGI4 %rbx
  74. #define RGI4bl %bl
  75. #define RGI4bh %bh
  76. #define RGS1 %r8
  77. #define RGS1d %r8d
  78. #define RGS2 %r9
  79. #define RGS2d %r9d
  80. #define RGS3 %r10
  81. #define RGS3d %r10d
  82. #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
  83. movzbl src ## bl, RID1d; \
  84. movzbl src ## bh, RID2d; \
  85. shrq $16, src; \
  86. movl t0(CTX, RID1, 4), dst ## d; \
  87. movl t1(CTX, RID2, 4), RID2d; \
  88. movzbl src ## bl, RID1d; \
  89. xorl RID2d, dst ## d; \
  90. movzbl src ## bh, RID2d; \
  91. interleave_op(il_reg); \
  92. xorl t2(CTX, RID1, 4), dst ## d; \
  93. xorl t3(CTX, RID2, 4), dst ## d;
  94. #define dummy(d) /* do nothing */
  95. #define shr_next(reg) \
  96. shrq $16, reg;
  97. #define G(gi1, gi2, x, t0, t1, t2, t3) \
  98. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
  99. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
  100. \
  101. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
  102. shlq $32, RGS2; \
  103. orq RGS1, RGS2; \
  104. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
  105. shlq $32, RGS1; \
  106. orq RGS1, RGS3;
  107. #define round_head_2(a, b, x1, y1, x2, y2) \
  108. vmovq b ## 1, RGI3; \
  109. vpextrq $1, b ## 1, RGI4; \
  110. \
  111. G(RGI1, RGI2, x1, s0, s1, s2, s3); \
  112. vmovq a ## 2, RGI1; \
  113. vpextrq $1, a ## 2, RGI2; \
  114. vmovq RGS2, x1; \
  115. vpinsrq $1, RGS3, x1, x1; \
  116. \
  117. G(RGI3, RGI4, y1, s1, s2, s3, s0); \
  118. vmovq b ## 2, RGI3; \
  119. vpextrq $1, b ## 2, RGI4; \
  120. vmovq RGS2, y1; \
  121. vpinsrq $1, RGS3, y1, y1; \
  122. \
  123. G(RGI1, RGI2, x2, s0, s1, s2, s3); \
  124. vmovq RGS2, x2; \
  125. vpinsrq $1, RGS3, x2, x2; \
  126. \
  127. G(RGI3, RGI4, y2, s1, s2, s3, s0); \
  128. vmovq RGS2, y2; \
  129. vpinsrq $1, RGS3, y2, y2;
  130. #define encround_tail(a, b, c, d, x, y, prerotate) \
  131. vpaddd x, y, x; \
  132. vpaddd x, RK1, RT;\
  133. prerotate(b); \
  134. vpxor RT, c, c; \
  135. vpaddd y, x, y; \
  136. vpaddd y, RK2, y; \
  137. vpsrld $1, c, RT; \
  138. vpslld $(32 - 1), c, c; \
  139. vpor c, RT, c; \
  140. vpxor d, y, d; \
  141. #define decround_tail(a, b, c, d, x, y, prerotate) \
  142. vpaddd x, y, x; \
  143. vpaddd x, RK1, RT;\
  144. prerotate(a); \
  145. vpxor RT, c, c; \
  146. vpaddd y, x, y; \
  147. vpaddd y, RK2, y; \
  148. vpxor d, y, d; \
  149. vpsrld $1, d, y; \
  150. vpslld $(32 - 1), d, d; \
  151. vpor d, y, d; \
  152. #define rotate_1l(x) \
  153. vpslld $1, x, RR; \
  154. vpsrld $(32 - 1), x, x; \
  155. vpor x, RR, x;
  156. #define preload_rgi(c) \
  157. vmovq c, RGI1; \
  158. vpextrq $1, c, RGI2;
  159. #define encrypt_round(n, a, b, c, d, preload, prerotate) \
  160. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  161. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  162. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  163. encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  164. preload(c ## 1); \
  165. encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  166. #define decrypt_round(n, a, b, c, d, preload, prerotate) \
  167. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  168. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  169. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  170. decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  171. preload(c ## 1); \
  172. decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  173. #define encrypt_cycle(n) \
  174. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  175. encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
  176. #define encrypt_cycle_last(n) \
  177. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  178. encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
  179. #define decrypt_cycle(n) \
  180. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  181. decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
  182. #define decrypt_cycle_last(n) \
  183. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  184. decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
  185. #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  186. vpunpckldq x1, x0, t0; \
  187. vpunpckhdq x1, x0, t2; \
  188. vpunpckldq x3, x2, t1; \
  189. vpunpckhdq x3, x2, x3; \
  190. \
  191. vpunpcklqdq t1, t0, x0; \
  192. vpunpckhqdq t1, t0, x1; \
  193. vpunpcklqdq x3, t2, x2; \
  194. vpunpckhqdq x3, t2, x3;
  195. #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  196. vpxor x0, wkey, x0; \
  197. vpxor x1, wkey, x1; \
  198. vpxor x2, wkey, x2; \
  199. vpxor x3, wkey, x3; \
  200. \
  201. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  202. #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  203. transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  204. \
  205. vpxor x0, wkey, x0; \
  206. vpxor x1, wkey, x1; \
  207. vpxor x2, wkey, x2; \
  208. vpxor x3, wkey, x3;
  209. .align 8
  210. __twofish_enc_blk8:
  211. /* input:
  212. * %rdi: ctx, CTX
  213. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
  214. * output:
  215. * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
  216. */
  217. vmovdqu w(CTX), RK1;
  218. pushq %rbp;
  219. pushq %rbx;
  220. pushq %rcx;
  221. inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
  222. preload_rgi(RA1);
  223. rotate_1l(RD1);
  224. inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
  225. rotate_1l(RD2);
  226. encrypt_cycle(0);
  227. encrypt_cycle(1);
  228. encrypt_cycle(2);
  229. encrypt_cycle(3);
  230. encrypt_cycle(4);
  231. encrypt_cycle(5);
  232. encrypt_cycle(6);
  233. encrypt_cycle_last(7);
  234. vmovdqu (w+4*4)(CTX), RK1;
  235. popq %rcx;
  236. popq %rbx;
  237. popq %rbp;
  238. outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
  239. outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
  240. ret;
  241. ENDPROC(__twofish_enc_blk8)
  242. .align 8
  243. __twofish_dec_blk8:
  244. /* input:
  245. * %rdi: ctx, CTX
  246. * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
  247. * output:
  248. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
  249. */
  250. vmovdqu (w+4*4)(CTX), RK1;
  251. pushq %rbp;
  252. pushq %rbx;
  253. inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
  254. preload_rgi(RC1);
  255. rotate_1l(RA1);
  256. inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
  257. rotate_1l(RA2);
  258. decrypt_cycle(7);
  259. decrypt_cycle(6);
  260. decrypt_cycle(5);
  261. decrypt_cycle(4);
  262. decrypt_cycle(3);
  263. decrypt_cycle(2);
  264. decrypt_cycle(1);
  265. decrypt_cycle_last(0);
  266. vmovdqu (w)(CTX), RK1;
  267. popq %rbx;
  268. popq %rbp;
  269. outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
  270. outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
  271. ret;
  272. ENDPROC(__twofish_dec_blk8)
  273. ENTRY(twofish_ecb_enc_8way)
  274. /* input:
  275. * %rdi: ctx, CTX
  276. * %rsi: dst
  277. * %rdx: src
  278. */
  279. movq %rsi, %r11;
  280. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  281. call __twofish_enc_blk8;
  282. store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  283. ret;
  284. ENDPROC(twofish_ecb_enc_8way)
  285. ENTRY(twofish_ecb_dec_8way)
  286. /* input:
  287. * %rdi: ctx, CTX
  288. * %rsi: dst
  289. * %rdx: src
  290. */
  291. movq %rsi, %r11;
  292. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  293. call __twofish_dec_blk8;
  294. store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  295. ret;
  296. ENDPROC(twofish_ecb_dec_8way)
  297. ENTRY(twofish_cbc_dec_8way)
  298. /* input:
  299. * %rdi: ctx, CTX
  300. * %rsi: dst
  301. * %rdx: src
  302. */
  303. pushq %r12;
  304. movq %rsi, %r11;
  305. movq %rdx, %r12;
  306. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  307. call __twofish_dec_blk8;
  308. store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  309. popq %r12;
  310. ret;
  311. ENDPROC(twofish_cbc_dec_8way)
  312. ENTRY(twofish_ctr_8way)
  313. /* input:
  314. * %rdi: ctx, CTX
  315. * %rsi: dst
  316. * %rdx: src
  317. * %rcx: iv (little endian, 128bit)
  318. */
  319. pushq %r12;
  320. movq %rsi, %r11;
  321. movq %rdx, %r12;
  322. load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
  323. RD2, RX0, RX1, RY0);
  324. call __twofish_enc_blk8;
  325. store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  326. popq %r12;
  327. ret;
  328. ENDPROC(twofish_ctr_8way)