camellia-x86_64-asm_64.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Camellia Cipher Algorithm (x86_64)
  3. *
  4. * Copyright (C) 2012 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 "camellia-x86_64-asm_64.S"
  24. .text
  25. .extern camellia_sp10011110;
  26. .extern camellia_sp22000222;
  27. .extern camellia_sp03303033;
  28. .extern camellia_sp00444404;
  29. .extern camellia_sp02220222;
  30. .extern camellia_sp30333033;
  31. .extern camellia_sp44044404;
  32. .extern camellia_sp11101110;
  33. #define sp10011110 camellia_sp10011110
  34. #define sp22000222 camellia_sp22000222
  35. #define sp03303033 camellia_sp03303033
  36. #define sp00444404 camellia_sp00444404
  37. #define sp02220222 camellia_sp02220222
  38. #define sp30333033 camellia_sp30333033
  39. #define sp44044404 camellia_sp44044404
  40. #define sp11101110 camellia_sp11101110
  41. #define CAMELLIA_TABLE_BYTE_LEN 272
  42. /* struct camellia_ctx: */
  43. #define key_table 0
  44. #define key_length CAMELLIA_TABLE_BYTE_LEN
  45. /* register macros */
  46. #define CTX %rdi
  47. #define RIO %rsi
  48. #define RIOd %esi
  49. #define RAB0 %rax
  50. #define RCD0 %rcx
  51. #define RAB1 %rbx
  52. #define RCD1 %rdx
  53. #define RAB0d %eax
  54. #define RCD0d %ecx
  55. #define RAB1d %ebx
  56. #define RCD1d %edx
  57. #define RAB0bl %al
  58. #define RCD0bl %cl
  59. #define RAB1bl %bl
  60. #define RCD1bl %dl
  61. #define RAB0bh %ah
  62. #define RCD0bh %ch
  63. #define RAB1bh %bh
  64. #define RCD1bh %dh
  65. #define RT0 %rsi
  66. #define RT1 %r12
  67. #define RT2 %r8
  68. #define RT0d %esi
  69. #define RT1d %r12d
  70. #define RT2d %r8d
  71. #define RT2bl %r8b
  72. #define RXOR %r9
  73. #define RR12 %r10
  74. #define RDST %r11
  75. #define RXORd %r9d
  76. #define RXORbl %r9b
  77. #define xor2ror16(T0, T1, tmp1, tmp2, ab, dst) \
  78. movzbl ab ## bl, tmp2 ## d; \
  79. movzbl ab ## bh, tmp1 ## d; \
  80. rorq $16, ab; \
  81. xorq T0(, tmp2, 8), dst; \
  82. xorq T1(, tmp1, 8), dst;
  83. /**********************************************************************
  84. 1-way camellia
  85. **********************************************************************/
  86. #define roundsm(ab, subkey, cd) \
  87. movq (key_table + ((subkey) * 2) * 4)(CTX), RT2; \
  88. \
  89. xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 0, cd ## 0); \
  90. xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 0, RT2); \
  91. xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 0, cd ## 0); \
  92. xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 0, RT2); \
  93. \
  94. xorq RT2, cd ## 0;
  95. #define fls(l, r, kl, kr) \
  96. movl (key_table + ((kl) * 2) * 4)(CTX), RT0d; \
  97. andl l ## 0d, RT0d; \
  98. roll $1, RT0d; \
  99. shlq $32, RT0; \
  100. xorq RT0, l ## 0; \
  101. movq (key_table + ((kr) * 2) * 4)(CTX), RT1; \
  102. orq r ## 0, RT1; \
  103. shrq $32, RT1; \
  104. xorq RT1, r ## 0; \
  105. \
  106. movq (key_table + ((kl) * 2) * 4)(CTX), RT2; \
  107. orq l ## 0, RT2; \
  108. shrq $32, RT2; \
  109. xorq RT2, l ## 0; \
  110. movl (key_table + ((kr) * 2) * 4)(CTX), RT0d; \
  111. andl r ## 0d, RT0d; \
  112. roll $1, RT0d; \
  113. shlq $32, RT0; \
  114. xorq RT0, r ## 0;
  115. #define enc_rounds(i) \
  116. roundsm(RAB, i + 2, RCD); \
  117. roundsm(RCD, i + 3, RAB); \
  118. roundsm(RAB, i + 4, RCD); \
  119. roundsm(RCD, i + 5, RAB); \
  120. roundsm(RAB, i + 6, RCD); \
  121. roundsm(RCD, i + 7, RAB);
  122. #define enc_fls(i) \
  123. fls(RAB, RCD, i + 0, i + 1);
  124. #define enc_inpack() \
  125. movq (RIO), RAB0; \
  126. bswapq RAB0; \
  127. rolq $32, RAB0; \
  128. movq 4*2(RIO), RCD0; \
  129. bswapq RCD0; \
  130. rorq $32, RCD0; \
  131. xorq key_table(CTX), RAB0;
  132. #define enc_outunpack(op, max) \
  133. xorq key_table(CTX, max, 8), RCD0; \
  134. rorq $32, RCD0; \
  135. bswapq RCD0; \
  136. op ## q RCD0, (RIO); \
  137. rolq $32, RAB0; \
  138. bswapq RAB0; \
  139. op ## q RAB0, 4*2(RIO);
  140. #define dec_rounds(i) \
  141. roundsm(RAB, i + 7, RCD); \
  142. roundsm(RCD, i + 6, RAB); \
  143. roundsm(RAB, i + 5, RCD); \
  144. roundsm(RCD, i + 4, RAB); \
  145. roundsm(RAB, i + 3, RCD); \
  146. roundsm(RCD, i + 2, RAB);
  147. #define dec_fls(i) \
  148. fls(RAB, RCD, i + 1, i + 0);
  149. #define dec_inpack(max) \
  150. movq (RIO), RAB0; \
  151. bswapq RAB0; \
  152. rolq $32, RAB0; \
  153. movq 4*2(RIO), RCD0; \
  154. bswapq RCD0; \
  155. rorq $32, RCD0; \
  156. xorq key_table(CTX, max, 8), RAB0;
  157. #define dec_outunpack() \
  158. xorq key_table(CTX), RCD0; \
  159. rorq $32, RCD0; \
  160. bswapq RCD0; \
  161. movq RCD0, (RIO); \
  162. rolq $32, RAB0; \
  163. bswapq RAB0; \
  164. movq RAB0, 4*2(RIO);
  165. ENTRY(__camellia_enc_blk)
  166. /* input:
  167. * %rdi: ctx, CTX
  168. * %rsi: dst
  169. * %rdx: src
  170. * %rcx: bool xor
  171. */
  172. movq %r12, RR12;
  173. movq %rcx, RXOR;
  174. movq %rsi, RDST;
  175. movq %rdx, RIO;
  176. enc_inpack();
  177. enc_rounds(0);
  178. enc_fls(8);
  179. enc_rounds(8);
  180. enc_fls(16);
  181. enc_rounds(16);
  182. movl $24, RT1d; /* max */
  183. cmpb $16, key_length(CTX);
  184. je .L__enc_done;
  185. enc_fls(24);
  186. enc_rounds(24);
  187. movl $32, RT1d; /* max */
  188. .L__enc_done:
  189. testb RXORbl, RXORbl;
  190. movq RDST, RIO;
  191. jnz .L__enc_xor;
  192. enc_outunpack(mov, RT1);
  193. movq RR12, %r12;
  194. ret;
  195. .L__enc_xor:
  196. enc_outunpack(xor, RT1);
  197. movq RR12, %r12;
  198. ret;
  199. ENDPROC(__camellia_enc_blk)
  200. ENTRY(camellia_dec_blk)
  201. /* input:
  202. * %rdi: ctx, CTX
  203. * %rsi: dst
  204. * %rdx: src
  205. */
  206. cmpl $16, key_length(CTX);
  207. movl $32, RT2d;
  208. movl $24, RXORd;
  209. cmovel RXORd, RT2d; /* max */
  210. movq %r12, RR12;
  211. movq %rsi, RDST;
  212. movq %rdx, RIO;
  213. dec_inpack(RT2);
  214. cmpb $24, RT2bl;
  215. je .L__dec_rounds16;
  216. dec_rounds(24);
  217. dec_fls(24);
  218. .L__dec_rounds16:
  219. dec_rounds(16);
  220. dec_fls(16);
  221. dec_rounds(8);
  222. dec_fls(8);
  223. dec_rounds(0);
  224. movq RDST, RIO;
  225. dec_outunpack();
  226. movq RR12, %r12;
  227. ret;
  228. ENDPROC(camellia_dec_blk)
  229. /**********************************************************************
  230. 2-way camellia
  231. **********************************************************************/
  232. #define roundsm2(ab, subkey, cd) \
  233. movq (key_table + ((subkey) * 2) * 4)(CTX), RT2; \
  234. xorq RT2, cd ## 1; \
  235. \
  236. xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 0, cd ## 0); \
  237. xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 0, RT2); \
  238. xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 0, cd ## 0); \
  239. xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 0, RT2); \
  240. \
  241. xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 1, cd ## 1); \
  242. xorq RT2, cd ## 0; \
  243. xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 1, cd ## 1); \
  244. xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 1, cd ## 1); \
  245. xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 1, cd ## 1);
  246. #define fls2(l, r, kl, kr) \
  247. movl (key_table + ((kl) * 2) * 4)(CTX), RT0d; \
  248. andl l ## 0d, RT0d; \
  249. roll $1, RT0d; \
  250. shlq $32, RT0; \
  251. xorq RT0, l ## 0; \
  252. movq (key_table + ((kr) * 2) * 4)(CTX), RT1; \
  253. orq r ## 0, RT1; \
  254. shrq $32, RT1; \
  255. xorq RT1, r ## 0; \
  256. \
  257. movl (key_table + ((kl) * 2) * 4)(CTX), RT2d; \
  258. andl l ## 1d, RT2d; \
  259. roll $1, RT2d; \
  260. shlq $32, RT2; \
  261. xorq RT2, l ## 1; \
  262. movq (key_table + ((kr) * 2) * 4)(CTX), RT0; \
  263. orq r ## 1, RT0; \
  264. shrq $32, RT0; \
  265. xorq RT0, r ## 1; \
  266. \
  267. movq (key_table + ((kl) * 2) * 4)(CTX), RT1; \
  268. orq l ## 0, RT1; \
  269. shrq $32, RT1; \
  270. xorq RT1, l ## 0; \
  271. movl (key_table + ((kr) * 2) * 4)(CTX), RT2d; \
  272. andl r ## 0d, RT2d; \
  273. roll $1, RT2d; \
  274. shlq $32, RT2; \
  275. xorq RT2, r ## 0; \
  276. \
  277. movq (key_table + ((kl) * 2) * 4)(CTX), RT0; \
  278. orq l ## 1, RT0; \
  279. shrq $32, RT0; \
  280. xorq RT0, l ## 1; \
  281. movl (key_table + ((kr) * 2) * 4)(CTX), RT1d; \
  282. andl r ## 1d, RT1d; \
  283. roll $1, RT1d; \
  284. shlq $32, RT1; \
  285. xorq RT1, r ## 1;
  286. #define enc_rounds2(i) \
  287. roundsm2(RAB, i + 2, RCD); \
  288. roundsm2(RCD, i + 3, RAB); \
  289. roundsm2(RAB, i + 4, RCD); \
  290. roundsm2(RCD, i + 5, RAB); \
  291. roundsm2(RAB, i + 6, RCD); \
  292. roundsm2(RCD, i + 7, RAB);
  293. #define enc_fls2(i) \
  294. fls2(RAB, RCD, i + 0, i + 1);
  295. #define enc_inpack2() \
  296. movq (RIO), RAB0; \
  297. bswapq RAB0; \
  298. rorq $32, RAB0; \
  299. movq 4*2(RIO), RCD0; \
  300. bswapq RCD0; \
  301. rolq $32, RCD0; \
  302. xorq key_table(CTX), RAB0; \
  303. \
  304. movq 8*2(RIO), RAB1; \
  305. bswapq RAB1; \
  306. rorq $32, RAB1; \
  307. movq 12*2(RIO), RCD1; \
  308. bswapq RCD1; \
  309. rolq $32, RCD1; \
  310. xorq key_table(CTX), RAB1;
  311. #define enc_outunpack2(op, max) \
  312. xorq key_table(CTX, max, 8), RCD0; \
  313. rolq $32, RCD0; \
  314. bswapq RCD0; \
  315. op ## q RCD0, (RIO); \
  316. rorq $32, RAB0; \
  317. bswapq RAB0; \
  318. op ## q RAB0, 4*2(RIO); \
  319. \
  320. xorq key_table(CTX, max, 8), RCD1; \
  321. rolq $32, RCD1; \
  322. bswapq RCD1; \
  323. op ## q RCD1, 8*2(RIO); \
  324. rorq $32, RAB1; \
  325. bswapq RAB1; \
  326. op ## q RAB1, 12*2(RIO);
  327. #define dec_rounds2(i) \
  328. roundsm2(RAB, i + 7, RCD); \
  329. roundsm2(RCD, i + 6, RAB); \
  330. roundsm2(RAB, i + 5, RCD); \
  331. roundsm2(RCD, i + 4, RAB); \
  332. roundsm2(RAB, i + 3, RCD); \
  333. roundsm2(RCD, i + 2, RAB);
  334. #define dec_fls2(i) \
  335. fls2(RAB, RCD, i + 1, i + 0);
  336. #define dec_inpack2(max) \
  337. movq (RIO), RAB0; \
  338. bswapq RAB0; \
  339. rorq $32, RAB0; \
  340. movq 4*2(RIO), RCD0; \
  341. bswapq RCD0; \
  342. rolq $32, RCD0; \
  343. xorq key_table(CTX, max, 8), RAB0; \
  344. \
  345. movq 8*2(RIO), RAB1; \
  346. bswapq RAB1; \
  347. rorq $32, RAB1; \
  348. movq 12*2(RIO), RCD1; \
  349. bswapq RCD1; \
  350. rolq $32, RCD1; \
  351. xorq key_table(CTX, max, 8), RAB1;
  352. #define dec_outunpack2() \
  353. xorq key_table(CTX), RCD0; \
  354. rolq $32, RCD0; \
  355. bswapq RCD0; \
  356. movq RCD0, (RIO); \
  357. rorq $32, RAB0; \
  358. bswapq RAB0; \
  359. movq RAB0, 4*2(RIO); \
  360. \
  361. xorq key_table(CTX), RCD1; \
  362. rolq $32, RCD1; \
  363. bswapq RCD1; \
  364. movq RCD1, 8*2(RIO); \
  365. rorq $32, RAB1; \
  366. bswapq RAB1; \
  367. movq RAB1, 12*2(RIO);
  368. ENTRY(__camellia_enc_blk_2way)
  369. /* input:
  370. * %rdi: ctx, CTX
  371. * %rsi: dst
  372. * %rdx: src
  373. * %rcx: bool xor
  374. */
  375. pushq %rbx;
  376. movq %r12, RR12;
  377. movq %rcx, RXOR;
  378. movq %rsi, RDST;
  379. movq %rdx, RIO;
  380. enc_inpack2();
  381. enc_rounds2(0);
  382. enc_fls2(8);
  383. enc_rounds2(8);
  384. enc_fls2(16);
  385. enc_rounds2(16);
  386. movl $24, RT2d; /* max */
  387. cmpb $16, key_length(CTX);
  388. je .L__enc2_done;
  389. enc_fls2(24);
  390. enc_rounds2(24);
  391. movl $32, RT2d; /* max */
  392. .L__enc2_done:
  393. test RXORbl, RXORbl;
  394. movq RDST, RIO;
  395. jnz .L__enc2_xor;
  396. enc_outunpack2(mov, RT2);
  397. movq RR12, %r12;
  398. popq %rbx;
  399. ret;
  400. .L__enc2_xor:
  401. enc_outunpack2(xor, RT2);
  402. movq RR12, %r12;
  403. popq %rbx;
  404. ret;
  405. ENDPROC(__camellia_enc_blk_2way)
  406. ENTRY(camellia_dec_blk_2way)
  407. /* input:
  408. * %rdi: ctx, CTX
  409. * %rsi: dst
  410. * %rdx: src
  411. */
  412. cmpl $16, key_length(CTX);
  413. movl $32, RT2d;
  414. movl $24, RXORd;
  415. cmovel RXORd, RT2d; /* max */
  416. movq %rbx, RXOR;
  417. movq %r12, RR12;
  418. movq %rsi, RDST;
  419. movq %rdx, RIO;
  420. dec_inpack2(RT2);
  421. cmpb $24, RT2bl;
  422. je .L__dec2_rounds16;
  423. dec_rounds2(24);
  424. dec_fls2(24);
  425. .L__dec2_rounds16:
  426. dec_rounds2(16);
  427. dec_fls2(16);
  428. dec_rounds2(8);
  429. dec_fls2(8);
  430. dec_rounds2(0);
  431. movq RDST, RIO;
  432. dec_outunpack2();
  433. movq RR12, %r12;
  434. movq RXOR, %rbx;
  435. ret;
  436. ENDPROC(camellia_dec_blk_2way)