twofish-avx-x86_64-asm_64.S 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "glue_helper-asm-avx.S"
  26. .file "twofish-avx-x86_64-asm_64.S"
  27. .data
  28. .align 16
  29. .Lbswap128_mask:
  30. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  31. .text
  32. /* structure of crypto context */
  33. #define s0 0
  34. #define s1 1024
  35. #define s2 2048
  36. #define s3 3072
  37. #define w 4096
  38. #define k 4128
  39. /**********************************************************************
  40. 8-way AVX twofish
  41. **********************************************************************/
  42. #define CTX %rdi
  43. #define RA1 %xmm0
  44. #define RB1 %xmm1
  45. #define RC1 %xmm2
  46. #define RD1 %xmm3
  47. #define RA2 %xmm4
  48. #define RB2 %xmm5
  49. #define RC2 %xmm6
  50. #define RD2 %xmm7
  51. #define RX0 %xmm8
  52. #define RY0 %xmm9
  53. #define RX1 %xmm10
  54. #define RY1 %xmm11
  55. #define RK1 %xmm12
  56. #define RK2 %xmm13
  57. #define RT %xmm14
  58. #define RR %xmm15
  59. #define RID1 %rbp
  60. #define RID1d %ebp
  61. #define RID2 %rsi
  62. #define RID2d %esi
  63. #define RGI1 %rdx
  64. #define RGI1bl %dl
  65. #define RGI1bh %dh
  66. #define RGI2 %rcx
  67. #define RGI2bl %cl
  68. #define RGI2bh %ch
  69. #define RGI3 %rax
  70. #define RGI3bl %al
  71. #define RGI3bh %ah
  72. #define RGI4 %rbx
  73. #define RGI4bl %bl
  74. #define RGI4bh %bh
  75. #define RGS1 %r8
  76. #define RGS1d %r8d
  77. #define RGS2 %r9
  78. #define RGS2d %r9d
  79. #define RGS3 %r10
  80. #define RGS3d %r10d
  81. #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
  82. movzbl src ## bl, RID1d; \
  83. movzbl src ## bh, RID2d; \
  84. shrq $16, src; \
  85. movl t0(CTX, RID1, 4), dst ## d; \
  86. movl t1(CTX, RID2, 4), RID2d; \
  87. movzbl src ## bl, RID1d; \
  88. xorl RID2d, dst ## d; \
  89. movzbl src ## bh, RID2d; \
  90. interleave_op(il_reg); \
  91. xorl t2(CTX, RID1, 4), dst ## d; \
  92. xorl t3(CTX, RID2, 4), dst ## d;
  93. #define dummy(d) /* do nothing */
  94. #define shr_next(reg) \
  95. shrq $16, reg;
  96. #define G(gi1, gi2, x, t0, t1, t2, t3) \
  97. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
  98. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
  99. \
  100. lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
  101. shlq $32, RGS2; \
  102. orq RGS1, RGS2; \
  103. lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
  104. shlq $32, RGS1; \
  105. orq RGS1, RGS3;
  106. #define round_head_2(a, b, x1, y1, x2, y2) \
  107. vmovq b ## 1, RGI3; \
  108. vpextrq $1, b ## 1, RGI4; \
  109. \
  110. G(RGI1, RGI2, x1, s0, s1, s2, s3); \
  111. vmovq a ## 2, RGI1; \
  112. vpextrq $1, a ## 2, RGI2; \
  113. vmovq RGS2, x1; \
  114. vpinsrq $1, RGS3, x1, x1; \
  115. \
  116. G(RGI3, RGI4, y1, s1, s2, s3, s0); \
  117. vmovq b ## 2, RGI3; \
  118. vpextrq $1, b ## 2, RGI4; \
  119. vmovq RGS2, y1; \
  120. vpinsrq $1, RGS3, y1, y1; \
  121. \
  122. G(RGI1, RGI2, x2, s0, s1, s2, s3); \
  123. vmovq RGS2, x2; \
  124. vpinsrq $1, RGS3, x2, x2; \
  125. \
  126. G(RGI3, RGI4, y2, s1, s2, s3, s0); \
  127. vmovq RGS2, y2; \
  128. vpinsrq $1, RGS3, y2, y2;
  129. #define encround_tail(a, b, c, d, x, y, prerotate) \
  130. vpaddd x, y, x; \
  131. vpaddd x, RK1, RT;\
  132. prerotate(b); \
  133. vpxor RT, c, c; \
  134. vpaddd y, x, y; \
  135. vpaddd y, RK2, y; \
  136. vpsrld $1, c, RT; \
  137. vpslld $(32 - 1), c, c; \
  138. vpor c, RT, c; \
  139. vpxor d, y, d; \
  140. #define decround_tail(a, b, c, d, x, y, prerotate) \
  141. vpaddd x, y, x; \
  142. vpaddd x, RK1, RT;\
  143. prerotate(a); \
  144. vpxor RT, c, c; \
  145. vpaddd y, x, y; \
  146. vpaddd y, RK2, y; \
  147. vpxor d, y, d; \
  148. vpsrld $1, d, y; \
  149. vpslld $(32 - 1), d, d; \
  150. vpor d, y, d; \
  151. #define rotate_1l(x) \
  152. vpslld $1, x, RR; \
  153. vpsrld $(32 - 1), x, x; \
  154. vpor x, RR, x;
  155. #define preload_rgi(c) \
  156. vmovq c, RGI1; \
  157. vpextrq $1, c, RGI2;
  158. #define encrypt_round(n, a, b, c, d, preload, prerotate) \
  159. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  160. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  161. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  162. encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  163. preload(c ## 1); \
  164. encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  165. #define decrypt_round(n, a, b, c, d, preload, prerotate) \
  166. vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
  167. vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
  168. round_head_2(a, b, RX0, RY0, RX1, RY1); \
  169. decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
  170. preload(c ## 1); \
  171. decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
  172. #define encrypt_cycle(n) \
  173. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  174. encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
  175. #define encrypt_cycle_last(n) \
  176. encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
  177. encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
  178. #define decrypt_cycle(n) \
  179. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  180. decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
  181. #define decrypt_cycle_last(n) \
  182. decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
  183. decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
  184. #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  185. vpunpckldq x1, x0, t0; \
  186. vpunpckhdq x1, x0, t2; \
  187. vpunpckldq x3, x2, t1; \
  188. vpunpckhdq x3, x2, x3; \
  189. \
  190. vpunpcklqdq t1, t0, x0; \
  191. vpunpckhqdq t1, t0, x1; \
  192. vpunpcklqdq x3, t2, x2; \
  193. vpunpckhqdq x3, t2, x3;
  194. #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  195. vpxor x0, wkey, x0; \
  196. vpxor x1, wkey, x1; \
  197. vpxor x2, wkey, x2; \
  198. vpxor x3, wkey, x3; \
  199. \
  200. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  201. #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
  202. transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  203. \
  204. vpxor x0, wkey, x0; \
  205. vpxor x1, wkey, x1; \
  206. vpxor x2, wkey, x2; \
  207. vpxor x3, wkey, x3;
  208. .align 8
  209. .type __twofish_enc_blk8,@function;
  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. .align 8
  242. .type __twofish_dec_blk8,@function;
  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. .align 8
  273. .global twofish_ecb_enc_8way
  274. .type twofish_ecb_enc_8way,@function;
  275. 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. .align 8
  287. .global twofish_ecb_dec_8way
  288. .type twofish_ecb_dec_8way,@function;
  289. twofish_ecb_dec_8way:
  290. /* input:
  291. * %rdi: ctx, CTX
  292. * %rsi: dst
  293. * %rdx: src
  294. */
  295. movq %rsi, %r11;
  296. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  297. call __twofish_dec_blk8;
  298. store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  299. ret;
  300. .align 8
  301. .global twofish_cbc_dec_8way
  302. .type twofish_cbc_dec_8way,@function;
  303. twofish_cbc_dec_8way:
  304. /* input:
  305. * %rdi: ctx, CTX
  306. * %rsi: dst
  307. * %rdx: src
  308. */
  309. pushq %r12;
  310. movq %rsi, %r11;
  311. movq %rdx, %r12;
  312. load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  313. call __twofish_dec_blk8;
  314. store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  315. popq %r12;
  316. ret;
  317. .align 8
  318. .global twofish_ctr_8way
  319. .type twofish_ctr_8way,@function;
  320. twofish_ctr_8way:
  321. /* input:
  322. * %rdi: ctx, CTX
  323. * %rsi: dst
  324. * %rdx: src
  325. * %rcx: iv (little endian, 128bit)
  326. */
  327. pushq %r12;
  328. movq %rsi, %r11;
  329. movq %rdx, %r12;
  330. load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
  331. RD2, RX0, RX1, RY0);
  332. call __twofish_enc_blk8;
  333. store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
  334. popq %r12;
  335. ret;