1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Camellia Cipher Algorithm (x86_64)
5 * Copyright (C) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
8 #include <linux/linkage.h>
10 .file "camellia-x86_64-asm_64.S"
13 .extern camellia_sp10011110;
14 .extern camellia_sp22000222;
15 .extern camellia_sp03303033;
16 .extern camellia_sp00444404;
17 .extern camellia_sp02220222;
18 .extern camellia_sp30333033;
19 .extern camellia_sp44044404;
20 .extern camellia_sp11101110;
22 #define sp10011110 camellia_sp10011110
23 #define sp22000222 camellia_sp22000222
24 #define sp03303033 camellia_sp03303033
25 #define sp00444404 camellia_sp00444404
26 #define sp02220222 camellia_sp02220222
27 #define sp30333033 camellia_sp30333033
28 #define sp44044404 camellia_sp44044404
29 #define sp11101110 camellia_sp11101110
31 #define CAMELLIA_TABLE_BYTE_LEN 272
33 /* struct camellia_ctx: */
35 #define key_length CAMELLIA_TABLE_BYTE_LEN
79 #define xor2ror16(T0, T1, tmp1, tmp2, ab, dst) \
80 movzbl ab ## bl, tmp2 ## d; \
81 movzbl ab ## bh, tmp1 ## d; \
83 xorq T0(, tmp2, 8), dst; \
84 xorq T1(, tmp1, 8), dst;
86 /**********************************************************************
88 **********************************************************************/
89 #define roundsm(ab, subkey, cd) \
90 movq (key_table + ((subkey) * 2) * 4)(CTX), RT2; \
92 xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 0, cd ## 0); \
93 xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 0, RT2); \
94 xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 0, cd ## 0); \
95 xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 0, RT2); \
99 #define fls(l, r, kl, kr) \
100 movl (key_table + ((kl) * 2) * 4)(CTX), RT0d; \
101 andl l ## 0d, RT0d; \
105 movq (key_table + ((kr) * 2) * 4)(CTX), RT1; \
110 movq (key_table + ((kl) * 2) * 4)(CTX), RT2; \
114 movl (key_table + ((kr) * 2) * 4)(CTX), RT0d; \
115 andl r ## 0d, RT0d; \
120 #define enc_rounds(i) \
121 roundsm(RAB, i + 2, RCD); \
122 roundsm(RCD, i + 3, RAB); \
123 roundsm(RAB, i + 4, RCD); \
124 roundsm(RCD, i + 5, RAB); \
125 roundsm(RAB, i + 6, RCD); \
126 roundsm(RCD, i + 7, RAB);
129 fls(RAB, RCD, i + 0, i + 1);
131 #define enc_inpack() \
135 movq 4*2(RIO), RCD0; \
138 xorq key_table(CTX), RAB0;
140 #define enc_outunpack(op, max) \
141 xorq key_table(CTX, max, 8), RCD0; \
144 op ## q RCD0, (RIO); \
147 op ## q RAB0, 4*2(RIO);
149 #define dec_rounds(i) \
150 roundsm(RAB, i + 7, RCD); \
151 roundsm(RCD, i + 6, RAB); \
152 roundsm(RAB, i + 5, RCD); \
153 roundsm(RCD, i + 4, RAB); \
154 roundsm(RAB, i + 3, RCD); \
155 roundsm(RCD, i + 2, RAB);
158 fls(RAB, RCD, i + 1, i + 0);
160 #define dec_inpack(max) \
164 movq 4*2(RIO), RCD0; \
167 xorq key_table(CTX, max, 8), RAB0;
169 #define dec_outunpack() \
170 xorq key_table(CTX), RCD0; \
178 SYM_FUNC_START(__camellia_enc_blk)
198 movl $24, RT1d; /* max */
200 cmpb $16, key_length(CTX);
205 movl $32, RT1d; /* max */
208 testb RXORbl, RXORbl;
213 enc_outunpack(mov, RT1);
219 enc_outunpack(xor, RT1);
223 SYM_FUNC_END(__camellia_enc_blk)
225 SYM_FUNC_START(camellia_dec_blk)
231 cmpl $16, key_length(CTX);
234 cmovel RXORd, RT2d; /* max */
261 SYM_FUNC_END(camellia_dec_blk)
263 /**********************************************************************
265 **********************************************************************/
266 #define roundsm2(ab, subkey, cd) \
267 movq (key_table + ((subkey) * 2) * 4)(CTX), RT2; \
270 xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 0, cd ## 0); \
271 xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 0, RT2); \
272 xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 0, cd ## 0); \
273 xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 0, RT2); \
275 xor2ror16(sp00444404, sp03303033, RT0, RT1, ab ## 1, cd ## 1); \
277 xor2ror16(sp22000222, sp10011110, RT0, RT1, ab ## 1, cd ## 1); \
278 xor2ror16(sp11101110, sp44044404, RT0, RT1, ab ## 1, cd ## 1); \
279 xor2ror16(sp30333033, sp02220222, RT0, RT1, ab ## 1, cd ## 1);
281 #define fls2(l, r, kl, kr) \
282 movl (key_table + ((kl) * 2) * 4)(CTX), RT0d; \
283 andl l ## 0d, RT0d; \
287 movq (key_table + ((kr) * 2) * 4)(CTX), RT1; \
292 movl (key_table + ((kl) * 2) * 4)(CTX), RT2d; \
293 andl l ## 1d, RT2d; \
297 movq (key_table + ((kr) * 2) * 4)(CTX), RT0; \
302 movq (key_table + ((kl) * 2) * 4)(CTX), RT1; \
306 movl (key_table + ((kr) * 2) * 4)(CTX), RT2d; \
307 andl r ## 0d, RT2d; \
312 movq (key_table + ((kl) * 2) * 4)(CTX), RT0; \
316 movl (key_table + ((kr) * 2) * 4)(CTX), RT1d; \
317 andl r ## 1d, RT1d; \
322 #define enc_rounds2(i) \
323 roundsm2(RAB, i + 2, RCD); \
324 roundsm2(RCD, i + 3, RAB); \
325 roundsm2(RAB, i + 4, RCD); \
326 roundsm2(RCD, i + 5, RAB); \
327 roundsm2(RAB, i + 6, RCD); \
328 roundsm2(RCD, i + 7, RAB);
330 #define enc_fls2(i) \
331 fls2(RAB, RCD, i + 0, i + 1);
333 #define enc_inpack2() \
337 movq 4*2(RIO), RCD0; \
340 xorq key_table(CTX), RAB0; \
342 movq 8*2(RIO), RAB1; \
345 movq 12*2(RIO), RCD1; \
348 xorq key_table(CTX), RAB1;
350 #define enc_outunpack2(op, max) \
351 xorq key_table(CTX, max, 8), RCD0; \
354 op ## q RCD0, (RIO); \
357 op ## q RAB0, 4*2(RIO); \
359 xorq key_table(CTX, max, 8), RCD1; \
362 op ## q RCD1, 8*2(RIO); \
365 op ## q RAB1, 12*2(RIO);
367 #define dec_rounds2(i) \
368 roundsm2(RAB, i + 7, RCD); \
369 roundsm2(RCD, i + 6, RAB); \
370 roundsm2(RAB, i + 5, RCD); \
371 roundsm2(RCD, i + 4, RAB); \
372 roundsm2(RAB, i + 3, RCD); \
373 roundsm2(RCD, i + 2, RAB);
375 #define dec_fls2(i) \
376 fls2(RAB, RCD, i + 1, i + 0);
378 #define dec_inpack2(max) \
382 movq 4*2(RIO), RCD0; \
385 xorq key_table(CTX, max, 8), RAB0; \
387 movq 8*2(RIO), RAB1; \
390 movq 12*2(RIO), RCD1; \
393 xorq key_table(CTX, max, 8), RAB1;
395 #define dec_outunpack2() \
396 xorq key_table(CTX), RCD0; \
402 movq RAB0, 4*2(RIO); \
404 xorq key_table(CTX), RCD1; \
407 movq RCD1, 8*2(RIO); \
410 movq RAB1, 12*2(RIO);
412 SYM_FUNC_START(__camellia_enc_blk_2way)
433 movl $24, RT2d; /* max */
435 cmpb $16, key_length(CTX);
440 movl $32, RT2d; /* max */
447 enc_outunpack2(mov, RT2);
454 enc_outunpack2(xor, RT2);
459 SYM_FUNC_END(__camellia_enc_blk_2way)
461 SYM_FUNC_START(camellia_dec_blk_2way)
467 cmpl $16, key_length(CTX);
470 cmovel RXORd, RT2d; /* max */
480 je .L__dec2_rounds16;
499 SYM_FUNC_END(camellia_dec_blk_2way)