blowfish-x86_64-asm_64.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Blowfish Cipher Algorithm (x86_64)
  3. *
  4. * Copyright (C) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. * USA
  20. *
  21. */
  22. #include <linux/linkage.h>
  23. .file "blowfish-x86_64-asm.S"
  24. .text
  25. /* structure of crypto context */
  26. #define p 0
  27. #define s0 ((16 + 2) * 4)
  28. #define s1 ((16 + 2 + (1 * 256)) * 4)
  29. #define s2 ((16 + 2 + (2 * 256)) * 4)
  30. #define s3 ((16 + 2 + (3 * 256)) * 4)
  31. /* register macros */
  32. #define CTX %r12
  33. #define RIO %rsi
  34. #define RX0 %rax
  35. #define RX1 %rbx
  36. #define RX2 %rcx
  37. #define RX3 %rdx
  38. #define RX0d %eax
  39. #define RX1d %ebx
  40. #define RX2d %ecx
  41. #define RX3d %edx
  42. #define RX0bl %al
  43. #define RX1bl %bl
  44. #define RX2bl %cl
  45. #define RX3bl %dl
  46. #define RX0bh %ah
  47. #define RX1bh %bh
  48. #define RX2bh %ch
  49. #define RX3bh %dh
  50. #define RT0 %rdi
  51. #define RT1 %rsi
  52. #define RT2 %r8
  53. #define RT3 %r9
  54. #define RT0d %edi
  55. #define RT1d %esi
  56. #define RT2d %r8d
  57. #define RT3d %r9d
  58. #define RKEY %r10
  59. /***********************************************************************
  60. * 1-way blowfish
  61. ***********************************************************************/
  62. #define F() \
  63. rorq $16, RX0; \
  64. movzbl RX0bh, RT0d; \
  65. movzbl RX0bl, RT1d; \
  66. rolq $16, RX0; \
  67. movl s0(CTX,RT0,4), RT0d; \
  68. addl s1(CTX,RT1,4), RT0d; \
  69. movzbl RX0bh, RT1d; \
  70. movzbl RX0bl, RT2d; \
  71. rolq $32, RX0; \
  72. xorl s2(CTX,RT1,4), RT0d; \
  73. addl s3(CTX,RT2,4), RT0d; \
  74. xorq RT0, RX0;
  75. #define add_roundkey_enc(n) \
  76. xorq p+4*(n)(CTX), RX0;
  77. #define round_enc(n) \
  78. add_roundkey_enc(n); \
  79. \
  80. F(); \
  81. F();
  82. #define add_roundkey_dec(n) \
  83. movq p+4*(n-1)(CTX), RT0; \
  84. rorq $32, RT0; \
  85. xorq RT0, RX0;
  86. #define round_dec(n) \
  87. add_roundkey_dec(n); \
  88. \
  89. F(); \
  90. F(); \
  91. #define read_block() \
  92. movq (RIO), RX0; \
  93. rorq $32, RX0; \
  94. bswapq RX0;
  95. #define write_block() \
  96. bswapq RX0; \
  97. movq RX0, (RIO);
  98. #define xor_block() \
  99. bswapq RX0; \
  100. xorq RX0, (RIO);
  101. ENTRY(__blowfish_enc_blk)
  102. /* input:
  103. * %rdi: ctx
  104. * %rsi: dst
  105. * %rdx: src
  106. * %rcx: bool, if true: xor output
  107. */
  108. movq %r12, %r11;
  109. movq %rdi, CTX;
  110. movq %rsi, %r10;
  111. movq %rdx, RIO;
  112. read_block();
  113. round_enc(0);
  114. round_enc(2);
  115. round_enc(4);
  116. round_enc(6);
  117. round_enc(8);
  118. round_enc(10);
  119. round_enc(12);
  120. round_enc(14);
  121. add_roundkey_enc(16);
  122. movq %r11, %r12;
  123. movq %r10, RIO;
  124. test %cl, %cl;
  125. jnz .L__enc_xor;
  126. write_block();
  127. ret;
  128. .L__enc_xor:
  129. xor_block();
  130. ret;
  131. ENDPROC(__blowfish_enc_blk)
  132. ENTRY(blowfish_dec_blk)
  133. /* input:
  134. * %rdi: ctx
  135. * %rsi: dst
  136. * %rdx: src
  137. */
  138. movq %r12, %r11;
  139. movq %rdi, CTX;
  140. movq %rsi, %r10;
  141. movq %rdx, RIO;
  142. read_block();
  143. round_dec(17);
  144. round_dec(15);
  145. round_dec(13);
  146. round_dec(11);
  147. round_dec(9);
  148. round_dec(7);
  149. round_dec(5);
  150. round_dec(3);
  151. add_roundkey_dec(1);
  152. movq %r10, RIO;
  153. write_block();
  154. movq %r11, %r12;
  155. ret;
  156. ENDPROC(blowfish_dec_blk)
  157. /**********************************************************************
  158. 4-way blowfish, four blocks parallel
  159. **********************************************************************/
  160. /* F() for 4-way. Slower when used alone/1-way, but faster when used
  161. * parallel/4-way (tested on AMD Phenom II & Intel Xeon E7330).
  162. */
  163. #define F4(x) \
  164. movzbl x ## bh, RT1d; \
  165. movzbl x ## bl, RT3d; \
  166. rorq $16, x; \
  167. movzbl x ## bh, RT0d; \
  168. movzbl x ## bl, RT2d; \
  169. rorq $16, x; \
  170. movl s0(CTX,RT0,4), RT0d; \
  171. addl s1(CTX,RT2,4), RT0d; \
  172. xorl s2(CTX,RT1,4), RT0d; \
  173. addl s3(CTX,RT3,4), RT0d; \
  174. xorq RT0, x;
  175. #define add_preloaded_roundkey4() \
  176. xorq RKEY, RX0; \
  177. xorq RKEY, RX1; \
  178. xorq RKEY, RX2; \
  179. xorq RKEY, RX3;
  180. #define preload_roundkey_enc(n) \
  181. movq p+4*(n)(CTX), RKEY;
  182. #define add_roundkey_enc4(n) \
  183. add_preloaded_roundkey4(); \
  184. preload_roundkey_enc(n + 2);
  185. #define round_enc4(n) \
  186. add_roundkey_enc4(n); \
  187. \
  188. F4(RX0); \
  189. F4(RX1); \
  190. F4(RX2); \
  191. F4(RX3); \
  192. \
  193. F4(RX0); \
  194. F4(RX1); \
  195. F4(RX2); \
  196. F4(RX3);
  197. #define preload_roundkey_dec(n) \
  198. movq p+4*((n)-1)(CTX), RKEY; \
  199. rorq $32, RKEY;
  200. #define add_roundkey_dec4(n) \
  201. add_preloaded_roundkey4(); \
  202. preload_roundkey_dec(n - 2);
  203. #define round_dec4(n) \
  204. add_roundkey_dec4(n); \
  205. \
  206. F4(RX0); \
  207. F4(RX1); \
  208. F4(RX2); \
  209. F4(RX3); \
  210. \
  211. F4(RX0); \
  212. F4(RX1); \
  213. F4(RX2); \
  214. F4(RX3);
  215. #define read_block4() \
  216. movq (RIO), RX0; \
  217. rorq $32, RX0; \
  218. bswapq RX0; \
  219. \
  220. movq 8(RIO), RX1; \
  221. rorq $32, RX1; \
  222. bswapq RX1; \
  223. \
  224. movq 16(RIO), RX2; \
  225. rorq $32, RX2; \
  226. bswapq RX2; \
  227. \
  228. movq 24(RIO), RX3; \
  229. rorq $32, RX3; \
  230. bswapq RX3;
  231. #define write_block4() \
  232. bswapq RX0; \
  233. movq RX0, (RIO); \
  234. \
  235. bswapq RX1; \
  236. movq RX1, 8(RIO); \
  237. \
  238. bswapq RX2; \
  239. movq RX2, 16(RIO); \
  240. \
  241. bswapq RX3; \
  242. movq RX3, 24(RIO);
  243. #define xor_block4() \
  244. bswapq RX0; \
  245. xorq RX0, (RIO); \
  246. \
  247. bswapq RX1; \
  248. xorq RX1, 8(RIO); \
  249. \
  250. bswapq RX2; \
  251. xorq RX2, 16(RIO); \
  252. \
  253. bswapq RX3; \
  254. xorq RX3, 24(RIO);
  255. ENTRY(__blowfish_enc_blk_4way)
  256. /* input:
  257. * %rdi: ctx
  258. * %rsi: dst
  259. * %rdx: src
  260. * %rcx: bool, if true: xor output
  261. */
  262. pushq %r12;
  263. pushq %rbx;
  264. pushq %rcx;
  265. movq %rdi, CTX
  266. movq %rsi, %r11;
  267. movq %rdx, RIO;
  268. preload_roundkey_enc(0);
  269. read_block4();
  270. round_enc4(0);
  271. round_enc4(2);
  272. round_enc4(4);
  273. round_enc4(6);
  274. round_enc4(8);
  275. round_enc4(10);
  276. round_enc4(12);
  277. round_enc4(14);
  278. add_preloaded_roundkey4();
  279. popq %r12;
  280. movq %r11, RIO;
  281. test %r12b, %r12b;
  282. jnz .L__enc_xor4;
  283. write_block4();
  284. popq %rbx;
  285. popq %r12;
  286. ret;
  287. .L__enc_xor4:
  288. xor_block4();
  289. popq %rbx;
  290. popq %r12;
  291. ret;
  292. ENDPROC(__blowfish_enc_blk_4way)
  293. ENTRY(blowfish_dec_blk_4way)
  294. /* input:
  295. * %rdi: ctx
  296. * %rsi: dst
  297. * %rdx: src
  298. */
  299. pushq %r12;
  300. pushq %rbx;
  301. movq %rdi, CTX;
  302. movq %rsi, %r11
  303. movq %rdx, RIO;
  304. preload_roundkey_dec(17);
  305. read_block4();
  306. round_dec4(17);
  307. round_dec4(15);
  308. round_dec4(13);
  309. round_dec4(11);
  310. round_dec4(9);
  311. round_dec4(7);
  312. round_dec4(5);
  313. round_dec4(3);
  314. add_preloaded_roundkey4();
  315. movq %r11, RIO;
  316. write_block4();
  317. popq %rbx;
  318. popq %r12;
  319. ret;
  320. ENDPROC(blowfish_dec_blk_4way)