blowfish-x86_64-asm_64.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 %rdi
  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 %rbp
  51. #define RT1 %rsi
  52. #define RT2 %r8
  53. #define RT3 %r9
  54. #define RT0d %ebp
  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, CTX
  104. * %rsi: dst
  105. * %rdx: src
  106. * %rcx: bool, if true: xor output
  107. */
  108. movq %rbp, %r11;
  109. movq %rsi, %r10;
  110. movq %rdx, RIO;
  111. read_block();
  112. round_enc(0);
  113. round_enc(2);
  114. round_enc(4);
  115. round_enc(6);
  116. round_enc(8);
  117. round_enc(10);
  118. round_enc(12);
  119. round_enc(14);
  120. add_roundkey_enc(16);
  121. movq %r11, %rbp;
  122. movq %r10, RIO;
  123. test %cl, %cl;
  124. jnz .L__enc_xor;
  125. write_block();
  126. ret;
  127. .L__enc_xor:
  128. xor_block();
  129. ret;
  130. ENDPROC(__blowfish_enc_blk)
  131. ENTRY(blowfish_dec_blk)
  132. /* input:
  133. * %rdi: ctx, CTX
  134. * %rsi: dst
  135. * %rdx: src
  136. */
  137. movq %rbp, %r11;
  138. movq %rsi, %r10;
  139. movq %rdx, RIO;
  140. read_block();
  141. round_dec(17);
  142. round_dec(15);
  143. round_dec(13);
  144. round_dec(11);
  145. round_dec(9);
  146. round_dec(7);
  147. round_dec(5);
  148. round_dec(3);
  149. add_roundkey_dec(1);
  150. movq %r10, RIO;
  151. write_block();
  152. movq %r11, %rbp;
  153. ret;
  154. ENDPROC(blowfish_dec_blk)
  155. /**********************************************************************
  156. 4-way blowfish, four blocks parallel
  157. **********************************************************************/
  158. /* F() for 4-way. Slower when used alone/1-way, but faster when used
  159. * parallel/4-way (tested on AMD Phenom II & Intel Xeon E7330).
  160. */
  161. #define F4(x) \
  162. movzbl x ## bh, RT1d; \
  163. movzbl x ## bl, RT3d; \
  164. rorq $16, x; \
  165. movzbl x ## bh, RT0d; \
  166. movzbl x ## bl, RT2d; \
  167. rorq $16, x; \
  168. movl s0(CTX,RT0,4), RT0d; \
  169. addl s1(CTX,RT2,4), RT0d; \
  170. xorl s2(CTX,RT1,4), RT0d; \
  171. addl s3(CTX,RT3,4), RT0d; \
  172. xorq RT0, x;
  173. #define add_preloaded_roundkey4() \
  174. xorq RKEY, RX0; \
  175. xorq RKEY, RX1; \
  176. xorq RKEY, RX2; \
  177. xorq RKEY, RX3;
  178. #define preload_roundkey_enc(n) \
  179. movq p+4*(n)(CTX), RKEY;
  180. #define add_roundkey_enc4(n) \
  181. add_preloaded_roundkey4(); \
  182. preload_roundkey_enc(n + 2);
  183. #define round_enc4(n) \
  184. add_roundkey_enc4(n); \
  185. \
  186. F4(RX0); \
  187. F4(RX1); \
  188. F4(RX2); \
  189. F4(RX3); \
  190. \
  191. F4(RX0); \
  192. F4(RX1); \
  193. F4(RX2); \
  194. F4(RX3);
  195. #define preload_roundkey_dec(n) \
  196. movq p+4*((n)-1)(CTX), RKEY; \
  197. rorq $32, RKEY;
  198. #define add_roundkey_dec4(n) \
  199. add_preloaded_roundkey4(); \
  200. preload_roundkey_dec(n - 2);
  201. #define round_dec4(n) \
  202. add_roundkey_dec4(n); \
  203. \
  204. F4(RX0); \
  205. F4(RX1); \
  206. F4(RX2); \
  207. F4(RX3); \
  208. \
  209. F4(RX0); \
  210. F4(RX1); \
  211. F4(RX2); \
  212. F4(RX3);
  213. #define read_block4() \
  214. movq (RIO), RX0; \
  215. rorq $32, RX0; \
  216. bswapq RX0; \
  217. \
  218. movq 8(RIO), RX1; \
  219. rorq $32, RX1; \
  220. bswapq RX1; \
  221. \
  222. movq 16(RIO), RX2; \
  223. rorq $32, RX2; \
  224. bswapq RX2; \
  225. \
  226. movq 24(RIO), RX3; \
  227. rorq $32, RX3; \
  228. bswapq RX3;
  229. #define write_block4() \
  230. bswapq RX0; \
  231. movq RX0, (RIO); \
  232. \
  233. bswapq RX1; \
  234. movq RX1, 8(RIO); \
  235. \
  236. bswapq RX2; \
  237. movq RX2, 16(RIO); \
  238. \
  239. bswapq RX3; \
  240. movq RX3, 24(RIO);
  241. #define xor_block4() \
  242. bswapq RX0; \
  243. xorq RX0, (RIO); \
  244. \
  245. bswapq RX1; \
  246. xorq RX1, 8(RIO); \
  247. \
  248. bswapq RX2; \
  249. xorq RX2, 16(RIO); \
  250. \
  251. bswapq RX3; \
  252. xorq RX3, 24(RIO);
  253. ENTRY(__blowfish_enc_blk_4way)
  254. /* input:
  255. * %rdi: ctx, CTX
  256. * %rsi: dst
  257. * %rdx: src
  258. * %rcx: bool, if true: xor output
  259. */
  260. pushq %rbp;
  261. pushq %rbx;
  262. pushq %rcx;
  263. preload_roundkey_enc(0);
  264. movq %rsi, %r11;
  265. movq %rdx, RIO;
  266. read_block4();
  267. round_enc4(0);
  268. round_enc4(2);
  269. round_enc4(4);
  270. round_enc4(6);
  271. round_enc4(8);
  272. round_enc4(10);
  273. round_enc4(12);
  274. round_enc4(14);
  275. add_preloaded_roundkey4();
  276. popq %rbp;
  277. movq %r11, RIO;
  278. test %bpl, %bpl;
  279. jnz .L__enc_xor4;
  280. write_block4();
  281. popq %rbx;
  282. popq %rbp;
  283. ret;
  284. .L__enc_xor4:
  285. xor_block4();
  286. popq %rbx;
  287. popq %rbp;
  288. ret;
  289. ENDPROC(__blowfish_enc_blk_4way)
  290. ENTRY(blowfish_dec_blk_4way)
  291. /* input:
  292. * %rdi: ctx, CTX
  293. * %rsi: dst
  294. * %rdx: src
  295. */
  296. pushq %rbp;
  297. pushq %rbx;
  298. preload_roundkey_dec(17);
  299. movq %rsi, %r11;
  300. movq %rdx, RIO;
  301. read_block4();
  302. round_dec4(17);
  303. round_dec4(15);
  304. round_dec4(13);
  305. round_dec4(11);
  306. round_dec4(9);
  307. round_dec4(7);
  308. round_dec4(5);
  309. round_dec4(3);
  310. add_preloaded_roundkey4();
  311. movq %r11, RIO;
  312. write_block4();
  313. popq %rbx;
  314. popq %rbp;
  315. ret;
  316. ENDPROC(blowfish_dec_blk_4way)