cast5-avx-x86_64-asm_64.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Cast5 Cipher 16-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 <asm/frame.h>
  27. .file "cast5-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 (16*4)
  35. #define rr ((16*4)+16)
  36. /* s-boxes */
  37. #define s1 cast_s1
  38. #define s2 cast_s2
  39. #define s3 cast_s3
  40. #define s4 cast_s4
  41. /**********************************************************************
  42. 16-way AVX cast5
  43. **********************************************************************/
  44. #define CTX %rdi
  45. #define RL1 %xmm0
  46. #define RR1 %xmm1
  47. #define RL2 %xmm2
  48. #define RR2 %xmm3
  49. #define RL3 %xmm4
  50. #define RR3 %xmm5
  51. #define RL4 %xmm6
  52. #define RR4 %xmm7
  53. #define RX %xmm8
  54. #define RKM %xmm9
  55. #define RKR %xmm10
  56. #define RKRF %xmm11
  57. #define RKRR %xmm12
  58. #define R32 %xmm13
  59. #define R1ST %xmm14
  60. #define RTMP %xmm15
  61. #define RID1 %rbp
  62. #define RID1d %ebp
  63. #define RID2 %rsi
  64. #define RID2d %esi
  65. #define RGI1 %rdx
  66. #define RGI1bl %dl
  67. #define RGI1bh %dh
  68. #define RGI2 %rcx
  69. #define RGI2bl %cl
  70. #define RGI2bh %ch
  71. #define RGI3 %rax
  72. #define RGI3bl %al
  73. #define RGI3bh %ah
  74. #define RGI4 %rbx
  75. #define RGI4bl %bl
  76. #define RGI4bh %bh
  77. #define RFS1 %r8
  78. #define RFS1d %r8d
  79. #define RFS2 %r9
  80. #define RFS2d %r9d
  81. #define RFS3 %r10
  82. #define RFS3d %r10d
  83. #define lookup_32bit(src, dst, op1, op2, op3, interleave_op, il_reg) \
  84. movzbl src ## bh, RID1d; \
  85. movzbl src ## bl, RID2d; \
  86. shrq $16, src; \
  87. movl s1(, RID1, 4), dst ## d; \
  88. op1 s2(, RID2, 4), dst ## d; \
  89. movzbl src ## bh, RID1d; \
  90. movzbl src ## bl, RID2d; \
  91. interleave_op(il_reg); \
  92. op2 s3(, RID1, 4), dst ## d; \
  93. op3 s4(, RID2, 4), dst ## d;
  94. #define dummy(d) /* do nothing */
  95. #define shr_next(reg) \
  96. shrq $16, reg;
  97. #define F_head(a, x, gi1, gi2, op0) \
  98. op0 a, RKM, x; \
  99. vpslld RKRF, x, RTMP; \
  100. vpsrld RKRR, x, x; \
  101. vpor RTMP, x, x; \
  102. \
  103. vmovq x, gi1; \
  104. vpextrq $1, x, gi2;
  105. #define F_tail(a, x, gi1, gi2, op1, op2, op3) \
  106. lookup_32bit(##gi1, RFS1, op1, op2, op3, shr_next, ##gi1); \
  107. lookup_32bit(##gi2, RFS3, op1, op2, op3, shr_next, ##gi2); \
  108. \
  109. lookup_32bit(##gi1, RFS2, op1, op2, op3, dummy, none); \
  110. shlq $32, RFS2; \
  111. orq RFS1, RFS2; \
  112. lookup_32bit(##gi2, RFS1, op1, op2, op3, dummy, none); \
  113. shlq $32, RFS1; \
  114. orq RFS1, RFS3; \
  115. \
  116. vmovq RFS2, x; \
  117. vpinsrq $1, RFS3, x, x;
  118. #define F_2(a1, b1, a2, b2, op0, op1, op2, op3) \
  119. F_head(b1, RX, RGI1, RGI2, op0); \
  120. F_head(b2, RX, RGI3, RGI4, op0); \
  121. \
  122. F_tail(b1, RX, RGI1, RGI2, op1, op2, op3); \
  123. F_tail(b2, RTMP, RGI3, RGI4, op1, op2, op3); \
  124. \
  125. vpxor a1, RX, a1; \
  126. vpxor a2, RTMP, a2;
  127. #define F1_2(a1, b1, a2, b2) \
  128. F_2(a1, b1, a2, b2, vpaddd, xorl, subl, addl)
  129. #define F2_2(a1, b1, a2, b2) \
  130. F_2(a1, b1, a2, b2, vpxor, subl, addl, xorl)
  131. #define F3_2(a1, b1, a2, b2) \
  132. F_2(a1, b1, a2, b2, vpsubd, addl, xorl, subl)
  133. #define subround(a1, b1, a2, b2, f) \
  134. F ## f ## _2(a1, b1, a2, b2);
  135. #define round(l, r, n, f) \
  136. vbroadcastss (km+(4*n))(CTX), RKM; \
  137. vpand R1ST, RKR, RKRF; \
  138. vpsubq RKRF, R32, RKRR; \
  139. vpsrldq $1, RKR, RKR; \
  140. subround(l ## 1, r ## 1, l ## 2, r ## 2, f); \
  141. subround(l ## 3, r ## 3, l ## 4, r ## 4, f);
  142. #define enc_preload_rkr() \
  143. vbroadcastss .L16_mask, RKR; \
  144. /* add 16-bit rotation to key rotations (mod 32) */ \
  145. vpxor kr(CTX), RKR, RKR;
  146. #define dec_preload_rkr() \
  147. vbroadcastss .L16_mask, RKR; \
  148. /* add 16-bit rotation to key rotations (mod 32) */ \
  149. vpxor kr(CTX), RKR, RKR; \
  150. vpshufb .Lbswap128_mask, RKR, RKR;
  151. #define transpose_2x4(x0, x1, t0, t1) \
  152. vpunpckldq x1, x0, t0; \
  153. vpunpckhdq x1, x0, t1; \
  154. \
  155. vpunpcklqdq t1, t0, x0; \
  156. vpunpckhqdq t1, t0, x1;
  157. #define inpack_blocks(x0, x1, t0, t1, rmask) \
  158. vpshufb rmask, x0, x0; \
  159. vpshufb rmask, x1, x1; \
  160. \
  161. transpose_2x4(x0, x1, t0, t1)
  162. #define outunpack_blocks(x0, x1, t0, t1, rmask) \
  163. transpose_2x4(x0, x1, t0, t1) \
  164. \
  165. vpshufb rmask, x0, x0; \
  166. vpshufb rmask, x1, x1;
  167. .data
  168. .align 16
  169. .Lbswap_mask:
  170. .byte 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12
  171. .Lbswap128_mask:
  172. .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  173. .Lbswap_iv_mask:
  174. .byte 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0
  175. .L16_mask:
  176. .byte 16, 16, 16, 16
  177. .L32_mask:
  178. .byte 32, 0, 0, 0
  179. .Lfirst_mask:
  180. .byte 0x1f, 0, 0, 0
  181. .text
  182. .align 16
  183. __cast5_enc_blk16:
  184. /* input:
  185. * %rdi: ctx, CTX
  186. * RL1: blocks 1 and 2
  187. * RR1: blocks 3 and 4
  188. * RL2: blocks 5 and 6
  189. * RR2: blocks 7 and 8
  190. * RL3: blocks 9 and 10
  191. * RR3: blocks 11 and 12
  192. * RL4: blocks 13 and 14
  193. * RR4: blocks 15 and 16
  194. * output:
  195. * RL1: encrypted blocks 1 and 2
  196. * RR1: encrypted blocks 3 and 4
  197. * RL2: encrypted blocks 5 and 6
  198. * RR2: encrypted blocks 7 and 8
  199. * RL3: encrypted blocks 9 and 10
  200. * RR3: encrypted blocks 11 and 12
  201. * RL4: encrypted blocks 13 and 14
  202. * RR4: encrypted blocks 15 and 16
  203. */
  204. pushq %rbp;
  205. pushq %rbx;
  206. vmovdqa .Lbswap_mask, RKM;
  207. vmovd .Lfirst_mask, R1ST;
  208. vmovd .L32_mask, R32;
  209. enc_preload_rkr();
  210. inpack_blocks(RL1, RR1, RTMP, RX, RKM);
  211. inpack_blocks(RL2, RR2, RTMP, RX, RKM);
  212. inpack_blocks(RL3, RR3, RTMP, RX, RKM);
  213. inpack_blocks(RL4, RR4, RTMP, RX, RKM);
  214. round(RL, RR, 0, 1);
  215. round(RR, RL, 1, 2);
  216. round(RL, RR, 2, 3);
  217. round(RR, RL, 3, 1);
  218. round(RL, RR, 4, 2);
  219. round(RR, RL, 5, 3);
  220. round(RL, RR, 6, 1);
  221. round(RR, RL, 7, 2);
  222. round(RL, RR, 8, 3);
  223. round(RR, RL, 9, 1);
  224. round(RL, RR, 10, 2);
  225. round(RR, RL, 11, 3);
  226. movzbl rr(CTX), %eax;
  227. testl %eax, %eax;
  228. jnz .L__skip_enc;
  229. round(RL, RR, 12, 1);
  230. round(RR, RL, 13, 2);
  231. round(RL, RR, 14, 3);
  232. round(RR, RL, 15, 1);
  233. .L__skip_enc:
  234. popq %rbx;
  235. popq %rbp;
  236. vmovdqa .Lbswap_mask, RKM;
  237. outunpack_blocks(RR1, RL1, RTMP, RX, RKM);
  238. outunpack_blocks(RR2, RL2, RTMP, RX, RKM);
  239. outunpack_blocks(RR3, RL3, RTMP, RX, RKM);
  240. outunpack_blocks(RR4, RL4, RTMP, RX, RKM);
  241. ret;
  242. ENDPROC(__cast5_enc_blk16)
  243. .align 16
  244. __cast5_dec_blk16:
  245. /* input:
  246. * %rdi: ctx, CTX
  247. * RL1: encrypted blocks 1 and 2
  248. * RR1: encrypted blocks 3 and 4
  249. * RL2: encrypted blocks 5 and 6
  250. * RR2: encrypted blocks 7 and 8
  251. * RL3: encrypted blocks 9 and 10
  252. * RR3: encrypted blocks 11 and 12
  253. * RL4: encrypted blocks 13 and 14
  254. * RR4: encrypted blocks 15 and 16
  255. * output:
  256. * RL1: decrypted blocks 1 and 2
  257. * RR1: decrypted blocks 3 and 4
  258. * RL2: decrypted blocks 5 and 6
  259. * RR2: decrypted blocks 7 and 8
  260. * RL3: decrypted blocks 9 and 10
  261. * RR3: decrypted blocks 11 and 12
  262. * RL4: decrypted blocks 13 and 14
  263. * RR4: decrypted blocks 15 and 16
  264. */
  265. pushq %rbp;
  266. pushq %rbx;
  267. vmovdqa .Lbswap_mask, RKM;
  268. vmovd .Lfirst_mask, R1ST;
  269. vmovd .L32_mask, R32;
  270. dec_preload_rkr();
  271. inpack_blocks(RL1, RR1, RTMP, RX, RKM);
  272. inpack_blocks(RL2, RR2, RTMP, RX, RKM);
  273. inpack_blocks(RL3, RR3, RTMP, RX, RKM);
  274. inpack_blocks(RL4, RR4, RTMP, RX, RKM);
  275. movzbl rr(CTX), %eax;
  276. testl %eax, %eax;
  277. jnz .L__skip_dec;
  278. round(RL, RR, 15, 1);
  279. round(RR, RL, 14, 3);
  280. round(RL, RR, 13, 2);
  281. round(RR, RL, 12, 1);
  282. .L__dec_tail:
  283. round(RL, RR, 11, 3);
  284. round(RR, RL, 10, 2);
  285. round(RL, RR, 9, 1);
  286. round(RR, RL, 8, 3);
  287. round(RL, RR, 7, 2);
  288. round(RR, RL, 6, 1);
  289. round(RL, RR, 5, 3);
  290. round(RR, RL, 4, 2);
  291. round(RL, RR, 3, 1);
  292. round(RR, RL, 2, 3);
  293. round(RL, RR, 1, 2);
  294. round(RR, RL, 0, 1);
  295. vmovdqa .Lbswap_mask, RKM;
  296. popq %rbx;
  297. popq %rbp;
  298. outunpack_blocks(RR1, RL1, RTMP, RX, RKM);
  299. outunpack_blocks(RR2, RL2, RTMP, RX, RKM);
  300. outunpack_blocks(RR3, RL3, RTMP, RX, RKM);
  301. outunpack_blocks(RR4, RL4, RTMP, RX, RKM);
  302. ret;
  303. .L__skip_dec:
  304. vpsrldq $4, RKR, RKR;
  305. jmp .L__dec_tail;
  306. ENDPROC(__cast5_dec_blk16)
  307. ENTRY(cast5_ecb_enc_16way)
  308. /* input:
  309. * %rdi: ctx, CTX
  310. * %rsi: dst
  311. * %rdx: src
  312. */
  313. FRAME_BEGIN
  314. movq %rsi, %r11;
  315. vmovdqu (0*4*4)(%rdx), RL1;
  316. vmovdqu (1*4*4)(%rdx), RR1;
  317. vmovdqu (2*4*4)(%rdx), RL2;
  318. vmovdqu (3*4*4)(%rdx), RR2;
  319. vmovdqu (4*4*4)(%rdx), RL3;
  320. vmovdqu (5*4*4)(%rdx), RR3;
  321. vmovdqu (6*4*4)(%rdx), RL4;
  322. vmovdqu (7*4*4)(%rdx), RR4;
  323. call __cast5_enc_blk16;
  324. vmovdqu RR1, (0*4*4)(%r11);
  325. vmovdqu RL1, (1*4*4)(%r11);
  326. vmovdqu RR2, (2*4*4)(%r11);
  327. vmovdqu RL2, (3*4*4)(%r11);
  328. vmovdqu RR3, (4*4*4)(%r11);
  329. vmovdqu RL3, (5*4*4)(%r11);
  330. vmovdqu RR4, (6*4*4)(%r11);
  331. vmovdqu RL4, (7*4*4)(%r11);
  332. FRAME_END
  333. ret;
  334. ENDPROC(cast5_ecb_enc_16way)
  335. ENTRY(cast5_ecb_dec_16way)
  336. /* input:
  337. * %rdi: ctx, CTX
  338. * %rsi: dst
  339. * %rdx: src
  340. */
  341. FRAME_BEGIN
  342. movq %rsi, %r11;
  343. vmovdqu (0*4*4)(%rdx), RL1;
  344. vmovdqu (1*4*4)(%rdx), RR1;
  345. vmovdqu (2*4*4)(%rdx), RL2;
  346. vmovdqu (3*4*4)(%rdx), RR2;
  347. vmovdqu (4*4*4)(%rdx), RL3;
  348. vmovdqu (5*4*4)(%rdx), RR3;
  349. vmovdqu (6*4*4)(%rdx), RL4;
  350. vmovdqu (7*4*4)(%rdx), RR4;
  351. call __cast5_dec_blk16;
  352. vmovdqu RR1, (0*4*4)(%r11);
  353. vmovdqu RL1, (1*4*4)(%r11);
  354. vmovdqu RR2, (2*4*4)(%r11);
  355. vmovdqu RL2, (3*4*4)(%r11);
  356. vmovdqu RR3, (4*4*4)(%r11);
  357. vmovdqu RL3, (5*4*4)(%r11);
  358. vmovdqu RR4, (6*4*4)(%r11);
  359. vmovdqu RL4, (7*4*4)(%r11);
  360. FRAME_END
  361. ret;
  362. ENDPROC(cast5_ecb_dec_16way)
  363. ENTRY(cast5_cbc_dec_16way)
  364. /* input:
  365. * %rdi: ctx, CTX
  366. * %rsi: dst
  367. * %rdx: src
  368. */
  369. FRAME_BEGIN
  370. pushq %r12;
  371. movq %rsi, %r11;
  372. movq %rdx, %r12;
  373. vmovdqu (0*16)(%rdx), RL1;
  374. vmovdqu (1*16)(%rdx), RR1;
  375. vmovdqu (2*16)(%rdx), RL2;
  376. vmovdqu (3*16)(%rdx), RR2;
  377. vmovdqu (4*16)(%rdx), RL3;
  378. vmovdqu (5*16)(%rdx), RR3;
  379. vmovdqu (6*16)(%rdx), RL4;
  380. vmovdqu (7*16)(%rdx), RR4;
  381. call __cast5_dec_blk16;
  382. /* xor with src */
  383. vmovq (%r12), RX;
  384. vpshufd $0x4f, RX, RX;
  385. vpxor RX, RR1, RR1;
  386. vpxor 0*16+8(%r12), RL1, RL1;
  387. vpxor 1*16+8(%r12), RR2, RR2;
  388. vpxor 2*16+8(%r12), RL2, RL2;
  389. vpxor 3*16+8(%r12), RR3, RR3;
  390. vpxor 4*16+8(%r12), RL3, RL3;
  391. vpxor 5*16+8(%r12), RR4, RR4;
  392. vpxor 6*16+8(%r12), RL4, RL4;
  393. vmovdqu RR1, (0*16)(%r11);
  394. vmovdqu RL1, (1*16)(%r11);
  395. vmovdqu RR2, (2*16)(%r11);
  396. vmovdqu RL2, (3*16)(%r11);
  397. vmovdqu RR3, (4*16)(%r11);
  398. vmovdqu RL3, (5*16)(%r11);
  399. vmovdqu RR4, (6*16)(%r11);
  400. vmovdqu RL4, (7*16)(%r11);
  401. popq %r12;
  402. FRAME_END
  403. ret;
  404. ENDPROC(cast5_cbc_dec_16way)
  405. ENTRY(cast5_ctr_16way)
  406. /* input:
  407. * %rdi: ctx, CTX
  408. * %rsi: dst
  409. * %rdx: src
  410. * %rcx: iv (big endian, 64bit)
  411. */
  412. FRAME_BEGIN
  413. pushq %r12;
  414. movq %rsi, %r11;
  415. movq %rdx, %r12;
  416. vpcmpeqd RTMP, RTMP, RTMP;
  417. vpsrldq $8, RTMP, RTMP; /* low: -1, high: 0 */
  418. vpcmpeqd RKR, RKR, RKR;
  419. vpaddq RKR, RKR, RKR; /* low: -2, high: -2 */
  420. vmovdqa .Lbswap_iv_mask, R1ST;
  421. vmovdqa .Lbswap128_mask, RKM;
  422. /* load IV and byteswap */
  423. vmovq (%rcx), RX;
  424. vpshufb R1ST, RX, RX;
  425. /* construct IVs */
  426. vpsubq RTMP, RX, RX; /* le: IV1, IV0 */
  427. vpshufb RKM, RX, RL1; /* be: IV0, IV1 */
  428. vpsubq RKR, RX, RX;
  429. vpshufb RKM, RX, RR1; /* be: IV2, IV3 */
  430. vpsubq RKR, RX, RX;
  431. vpshufb RKM, RX, RL2; /* be: IV4, IV5 */
  432. vpsubq RKR, RX, RX;
  433. vpshufb RKM, RX, RR2; /* be: IV6, IV7 */
  434. vpsubq RKR, RX, RX;
  435. vpshufb RKM, RX, RL3; /* be: IV8, IV9 */
  436. vpsubq RKR, RX, RX;
  437. vpshufb RKM, RX, RR3; /* be: IV10, IV11 */
  438. vpsubq RKR, RX, RX;
  439. vpshufb RKM, RX, RL4; /* be: IV12, IV13 */
  440. vpsubq RKR, RX, RX;
  441. vpshufb RKM, RX, RR4; /* be: IV14, IV15 */
  442. /* store last IV */
  443. vpsubq RTMP, RX, RX; /* le: IV16, IV14 */
  444. vpshufb R1ST, RX, RX; /* be: IV16, IV16 */
  445. vmovq RX, (%rcx);
  446. call __cast5_enc_blk16;
  447. /* dst = src ^ iv */
  448. vpxor (0*16)(%r12), RR1, RR1;
  449. vpxor (1*16)(%r12), RL1, RL1;
  450. vpxor (2*16)(%r12), RR2, RR2;
  451. vpxor (3*16)(%r12), RL2, RL2;
  452. vpxor (4*16)(%r12), RR3, RR3;
  453. vpxor (5*16)(%r12), RL3, RL3;
  454. vpxor (6*16)(%r12), RR4, RR4;
  455. vpxor (7*16)(%r12), RL4, RL4;
  456. vmovdqu RR1, (0*16)(%r11);
  457. vmovdqu RL1, (1*16)(%r11);
  458. vmovdqu RR2, (2*16)(%r11);
  459. vmovdqu RL2, (3*16)(%r11);
  460. vmovdqu RR3, (4*16)(%r11);
  461. vmovdqu RL3, (5*16)(%r11);
  462. vmovdqu RR4, (6*16)(%r11);
  463. vmovdqu RL4, (7*16)(%r11);
  464. popq %r12;
  465. FRAME_END
  466. ret;
  467. ENDPROC(cast5_ctr_16way)