cast6-avx-x86_64-asm_64.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Cast6 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 "cast6-avx-x86_64-asm_64.S"
  28. .extern cast_s1
  29. .extern cast_s2
  30. .extern cast_s3
  31. .extern cast_s4
  32. /* structure of crypto context */
  33. #define km 0
  34. #define kr (12*4*4)
  35. /* s-boxes */
  36. #define s1 cast_s1
  37. #define s2 cast_s2
  38. #define s3 cast_s3
  39. #define s4 cast_s4
  40. /**********************************************************************
  41. 8-way AVX cast6
  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 RX %xmm8
  53. #define RKM %xmm9
  54. #define RKR %xmm10
  55. #define RKRF %xmm11
  56. #define RKRR %xmm12
  57. #define R32 %xmm13
  58. #define R1ST %xmm14
  59. #define RTMP %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 RFS1 %r8
  77. #define RFS1d %r8d
  78. #define RFS2 %r9
  79. #define RFS2d %r9d
  80. #define RFS3 %r10
  81. #define RFS3d %r10d
  82. #define lookup_32bit(src, dst, op1, op2, op3, interleave_op, il_reg) \
  83. movzbl src ## bh, RID1d; \
  84. movzbl src ## bl, RID2d; \
  85. shrq $16, src; \
  86. movl s1(, RID1, 4), dst ## d; \
  87. op1 s2(, RID2, 4), dst ## d; \
  88. movzbl src ## bh, RID1d; \
  89. movzbl src ## bl, RID2d; \
  90. interleave_op(il_reg); \
  91. op2 s3(, RID1, 4), dst ## d; \
  92. op3 s4(, RID2, 4), dst ## d;
  93. #define dummy(d) /* do nothing */
  94. #define shr_next(reg) \
  95. shrq $16, reg;
  96. #define F_head(a, x, gi1, gi2, op0) \
  97. op0 a, RKM, x; \
  98. vpslld RKRF, x, RTMP; \
  99. vpsrld RKRR, x, x; \
  100. vpor RTMP, x, x; \
  101. \
  102. vmovq x, gi1; \
  103. vpextrq $1, x, gi2;
  104. #define F_tail(a, x, gi1, gi2, op1, op2, op3) \
  105. lookup_32bit(##gi1, RFS1, op1, op2, op3, shr_next, ##gi1); \
  106. lookup_32bit(##gi2, RFS3, op1, op2, op3, shr_next, ##gi2); \
  107. \
  108. lookup_32bit(##gi1, RFS2, op1, op2, op3, dummy, none); \
  109. shlq $32, RFS2; \
  110. orq RFS1, RFS2; \
  111. lookup_32bit(##gi2, RFS1, op1, op2, op3, dummy, none); \
  112. shlq $32, RFS1; \
  113. orq RFS1, RFS3; \
  114. \
  115. vmovq RFS2, x; \
  116. vpinsrq $1, RFS3, x, x;
  117. #define F_2(a1, b1, a2, b2, op0, op1, op2, op3) \
  118. F_head(b1, RX, RGI1, RGI2, op0); \
  119. F_head(b2, RX, RGI3, RGI4, op0); \
  120. \
  121. F_tail(b1, RX, RGI1, RGI2, op1, op2, op3); \
  122. F_tail(b2, RTMP, RGI3, RGI4, op1, op2, op3); \
  123. \
  124. vpxor a1, RX, a1; \
  125. vpxor a2, RTMP, a2;
  126. #define F1_2(a1, b1, a2, b2) \
  127. F_2(a1, b1, a2, b2, vpaddd, xorl, subl, addl)
  128. #define F2_2(a1, b1, a2, b2) \
  129. F_2(a1, b1, a2, b2, vpxor, subl, addl, xorl)
  130. #define F3_2(a1, b1, a2, b2) \
  131. F_2(a1, b1, a2, b2, vpsubd, addl, xorl, subl)
  132. #define qop(in, out, f) \
  133. F ## f ## _2(out ## 1, in ## 1, out ## 2, in ## 2);
  134. #define get_round_keys(nn) \
  135. vbroadcastss (km+(4*(nn)))(CTX), RKM; \
  136. vpand R1ST, RKR, RKRF; \
  137. vpsubq RKRF, R32, RKRR; \
  138. vpsrldq $1, RKR, RKR;
  139. #define Q(n) \
  140. get_round_keys(4*n+0); \
  141. qop(RD, RC, 1); \
  142. \
  143. get_round_keys(4*n+1); \
  144. qop(RC, RB, 2); \
  145. \
  146. get_round_keys(4*n+2); \
  147. qop(RB, RA, 3); \
  148. \
  149. get_round_keys(4*n+3); \
  150. qop(RA, RD, 1);
  151. #define QBAR(n) \
  152. get_round_keys(4*n+3); \
  153. qop(RA, RD, 1); \
  154. \
  155. get_round_keys(4*n+2); \
  156. qop(RB, RA, 3); \
  157. \
  158. get_round_keys(4*n+1); \
  159. qop(RC, RB, 2); \
  160. \
  161. get_round_keys(4*n+0); \
  162. qop(RD, RC, 1);
  163. #define shuffle(mask) \
  164. vpshufb mask, RKR, RKR;
  165. #define preload_rkr(n, do_mask, mask) \
  166. vbroadcastss .L16_mask, RKR; \
  167. /* add 16-bit rotation to key rotations (mod 32) */ \
  168. vpxor (kr+n*16)(CTX), RKR, RKR; \
  169. do_mask(mask);
  170. #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  171. vpunpckldq x1, x0, t0; \
  172. vpunpckhdq x1, x0, t2; \
  173. vpunpckldq x3, x2, t1; \
  174. vpunpckhdq x3, x2, x3; \
  175. \
  176. vpunpcklqdq t1, t0, x0; \
  177. vpunpckhqdq t1, t0, x1; \
  178. vpunpcklqdq x3, t2, x2; \
  179. vpunpckhqdq x3, t2, x3;
  180. #define inpack_blocks(x0, x1, x2, x3, t0, t1, t2, rmask) \
  181. vpshufb rmask, x0, x0; \
  182. vpshufb rmask, x1, x1; \
  183. vpshufb rmask, x2, x2; \
  184. vpshufb rmask, x3, x3; \
  185. \
  186. transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
  187. #define outunpack_blocks(x0, x1, x2, x3, t0, t1, t2, rmask) \
  188. transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
  189. \
  190. vpshufb rmask, x0, x0; \
  191. vpshufb rmask, x1, x1; \
  192. vpshufb rmask, x2, x2; \
  193. vpshufb rmask, x3, x3;
  194. .data
  195. .align 16
  196. .Lxts_gf128mul_and_shl1_mask:
  197. .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
  198. .Lbswap_mask:
  199. .byte 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12
  200. .Lbswap128_mask:
  201. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  202. .Lrkr_enc_Q_Q_QBAR_QBAR:
  203. .byte 0, 1, 2, 3, 4, 5, 6, 7, 11, 10, 9, 8, 15, 14, 13, 12
  204. .Lrkr_enc_QBAR_QBAR_QBAR_QBAR:
  205. .byte 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12
  206. .Lrkr_dec_Q_Q_Q_Q:
  207. .byte 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3
  208. .Lrkr_dec_Q_Q_QBAR_QBAR:
  209. .byte 12, 13, 14, 15, 8, 9, 10, 11, 7, 6, 5, 4, 3, 2, 1, 0
  210. .Lrkr_dec_QBAR_QBAR_QBAR_QBAR:
  211. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  212. .L16_mask:
  213. .byte 16, 16, 16, 16
  214. .L32_mask:
  215. .byte 32, 0, 0, 0
  216. .Lfirst_mask:
  217. .byte 0x1f, 0, 0, 0
  218. .text
  219. .align 8
  220. __cast6_enc_blk8:
  221. /* input:
  222. * %rdi: ctx, CTX
  223. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
  224. * output:
  225. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: encrypted blocks
  226. */
  227. pushq %rbp;
  228. pushq %rbx;
  229. vmovdqa .Lbswap_mask, RKM;
  230. vmovd .Lfirst_mask, R1ST;
  231. vmovd .L32_mask, R32;
  232. inpack_blocks(RA1, RB1, RC1, RD1, RTMP, RX, RKRF, RKM);
  233. inpack_blocks(RA2, RB2, RC2, RD2, RTMP, RX, RKRF, RKM);
  234. preload_rkr(0, dummy, none);
  235. Q(0);
  236. Q(1);
  237. Q(2);
  238. Q(3);
  239. preload_rkr(1, shuffle, .Lrkr_enc_Q_Q_QBAR_QBAR);
  240. Q(4);
  241. Q(5);
  242. QBAR(6);
  243. QBAR(7);
  244. preload_rkr(2, shuffle, .Lrkr_enc_QBAR_QBAR_QBAR_QBAR);
  245. QBAR(8);
  246. QBAR(9);
  247. QBAR(10);
  248. QBAR(11);
  249. popq %rbx;
  250. popq %rbp;
  251. vmovdqa .Lbswap_mask, RKM;
  252. outunpack_blocks(RA1, RB1, RC1, RD1, RTMP, RX, RKRF, RKM);
  253. outunpack_blocks(RA2, RB2, RC2, RD2, RTMP, RX, RKRF, RKM);
  254. ret;
  255. ENDPROC(__cast6_enc_blk8)
  256. .align 8
  257. __cast6_dec_blk8:
  258. /* input:
  259. * %rdi: ctx, CTX
  260. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: encrypted blocks
  261. * output:
  262. * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
  263. */
  264. pushq %rbp;
  265. pushq %rbx;
  266. vmovdqa .Lbswap_mask, RKM;
  267. vmovd .Lfirst_mask, R1ST;
  268. vmovd .L32_mask, R32;
  269. inpack_blocks(RA1, RB1, RC1, RD1, RTMP, RX, RKRF, RKM);
  270. inpack_blocks(RA2, RB2, RC2, RD2, RTMP, RX, RKRF, RKM);
  271. preload_rkr(2, shuffle, .Lrkr_dec_Q_Q_Q_Q);
  272. Q(11);
  273. Q(10);
  274. Q(9);
  275. Q(8);
  276. preload_rkr(1, shuffle, .Lrkr_dec_Q_Q_QBAR_QBAR);
  277. Q(7);
  278. Q(6);
  279. QBAR(5);
  280. QBAR(4);
  281. preload_rkr(0, shuffle, .Lrkr_dec_QBAR_QBAR_QBAR_QBAR);
  282. QBAR(3);
  283. QBAR(2);
  284. QBAR(1);
  285. QBAR(0);
  286. popq %rbx;
  287. popq %rbp;
  288. vmovdqa .Lbswap_mask, RKM;
  289. outunpack_blocks(RA1, RB1, RC1, RD1, RTMP, RX, RKRF, RKM);
  290. outunpack_blocks(RA2, RB2, RC2, RD2, RTMP, RX, RKRF, RKM);
  291. ret;
  292. ENDPROC(__cast6_dec_blk8)
  293. ENTRY(cast6_ecb_enc_8way)
  294. /* input:
  295. * %rdi: ctx, CTX
  296. * %rsi: dst
  297. * %rdx: src
  298. */
  299. movq %rsi, %r11;
  300. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  301. call __cast6_enc_blk8;
  302. store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  303. ret;
  304. ENDPROC(cast6_ecb_enc_8way)
  305. ENTRY(cast6_ecb_dec_8way)
  306. /* input:
  307. * %rdi: ctx, CTX
  308. * %rsi: dst
  309. * %rdx: src
  310. */
  311. movq %rsi, %r11;
  312. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  313. call __cast6_dec_blk8;
  314. store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  315. ret;
  316. ENDPROC(cast6_ecb_dec_8way)
  317. ENTRY(cast6_cbc_dec_8way)
  318. /* input:
  319. * %rdi: ctx, CTX
  320. * %rsi: dst
  321. * %rdx: src
  322. */
  323. pushq %r12;
  324. movq %rsi, %r11;
  325. movq %rdx, %r12;
  326. load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  327. call __cast6_dec_blk8;
  328. store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  329. popq %r12;
  330. ret;
  331. ENDPROC(cast6_cbc_dec_8way)
  332. ENTRY(cast6_ctr_8way)
  333. /* input:
  334. * %rdi: ctx, CTX
  335. * %rsi: dst
  336. * %rdx: src
  337. * %rcx: iv (little endian, 128bit)
  338. */
  339. pushq %r12;
  340. movq %rsi, %r11;
  341. movq %rdx, %r12;
  342. load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
  343. RD2, RX, RKR, RKM);
  344. call __cast6_enc_blk8;
  345. store_ctr_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  346. popq %r12;
  347. ret;
  348. ENDPROC(cast6_ctr_8way)
  349. ENTRY(cast6_xts_enc_8way)
  350. /* input:
  351. * %rdi: ctx, CTX
  352. * %rsi: dst
  353. * %rdx: src
  354. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  355. */
  356. movq %rsi, %r11;
  357. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  358. load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
  359. RX, RKR, RKM, .Lxts_gf128mul_and_shl1_mask);
  360. call __cast6_enc_blk8;
  361. /* dst <= regs xor IVs(in dst) */
  362. store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  363. ret;
  364. ENDPROC(cast6_xts_enc_8way)
  365. ENTRY(cast6_xts_dec_8way)
  366. /* input:
  367. * %rdi: ctx, CTX
  368. * %rsi: dst
  369. * %rdx: src
  370. * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
  371. */
  372. movq %rsi, %r11;
  373. /* regs <= src, dst <= IVs, regs <= regs xor IVs */
  374. load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
  375. RX, RKR, RKM, .Lxts_gf128mul_and_shl1_mask);
  376. call __cast6_dec_blk8;
  377. /* dst <= regs xor IVs(in dst) */
  378. store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
  379. ret;
  380. ENDPROC(cast6_xts_dec_8way)