162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Scalar AES core transform
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/linkage.h>
962306a36Sopenharmony_ci#include <asm/assembler.h>
1062306a36Sopenharmony_ci#include <asm/cache.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci	.text
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci	rk		.req	x0
1562306a36Sopenharmony_ci	out		.req	x1
1662306a36Sopenharmony_ci	in		.req	x2
1762306a36Sopenharmony_ci	rounds		.req	x3
1862306a36Sopenharmony_ci	tt		.req	x2
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci	.macro		__pair1, sz, op, reg0, reg1, in0, in1e, in1d, shift
2162306a36Sopenharmony_ci	.ifc		\op\shift, b0
2262306a36Sopenharmony_ci	ubfiz		\reg0, \in0, #2, #8
2362306a36Sopenharmony_ci	ubfiz		\reg1, \in1e, #2, #8
2462306a36Sopenharmony_ci	.else
2562306a36Sopenharmony_ci	ubfx		\reg0, \in0, #\shift, #8
2662306a36Sopenharmony_ci	ubfx		\reg1, \in1e, #\shift, #8
2762306a36Sopenharmony_ci	.endif
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	/*
3062306a36Sopenharmony_ci	 * AArch64 cannot do byte size indexed loads from a table containing
3162306a36Sopenharmony_ci	 * 32-bit quantities, i.e., 'ldrb w12, [tt, w12, uxtw #2]' is not a
3262306a36Sopenharmony_ci	 * valid instruction. So perform the shift explicitly first for the
3362306a36Sopenharmony_ci	 * high bytes (the low byte is shifted implicitly by using ubfiz rather
3462306a36Sopenharmony_ci	 * than ubfx above)
3562306a36Sopenharmony_ci	 */
3662306a36Sopenharmony_ci	.ifnc		\op, b
3762306a36Sopenharmony_ci	ldr		\reg0, [tt, \reg0, uxtw #2]
3862306a36Sopenharmony_ci	ldr		\reg1, [tt, \reg1, uxtw #2]
3962306a36Sopenharmony_ci	.else
4062306a36Sopenharmony_ci	.if		\shift > 0
4162306a36Sopenharmony_ci	lsl		\reg0, \reg0, #2
4262306a36Sopenharmony_ci	lsl		\reg1, \reg1, #2
4362306a36Sopenharmony_ci	.endif
4462306a36Sopenharmony_ci	ldrb		\reg0, [tt, \reg0, uxtw]
4562306a36Sopenharmony_ci	ldrb		\reg1, [tt, \reg1, uxtw]
4662306a36Sopenharmony_ci	.endif
4762306a36Sopenharmony_ci	.endm
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	.macro		__pair0, sz, op, reg0, reg1, in0, in1e, in1d, shift
5062306a36Sopenharmony_ci	ubfx		\reg0, \in0, #\shift, #8
5162306a36Sopenharmony_ci	ubfx		\reg1, \in1d, #\shift, #8
5262306a36Sopenharmony_ci	ldr\op		\reg0, [tt, \reg0, uxtw #\sz]
5362306a36Sopenharmony_ci	ldr\op		\reg1, [tt, \reg1, uxtw #\sz]
5462306a36Sopenharmony_ci	.endm
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	.macro		__hround, out0, out1, in0, in1, in2, in3, t0, t1, enc, sz, op
5762306a36Sopenharmony_ci	ldp		\out0, \out1, [rk], #8
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	__pair\enc	\sz, \op, w12, w13, \in0, \in1, \in3, 0
6062306a36Sopenharmony_ci	__pair\enc	\sz, \op, w14, w15, \in1, \in2, \in0, 8
6162306a36Sopenharmony_ci	__pair\enc	\sz, \op, w16, w17, \in2, \in3, \in1, 16
6262306a36Sopenharmony_ci	__pair\enc	\sz, \op, \t0, \t1, \in3, \in0, \in2, 24
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	eor		\out0, \out0, w12
6562306a36Sopenharmony_ci	eor		\out1, \out1, w13
6662306a36Sopenharmony_ci	eor		\out0, \out0, w14, ror #24
6762306a36Sopenharmony_ci	eor		\out1, \out1, w15, ror #24
6862306a36Sopenharmony_ci	eor		\out0, \out0, w16, ror #16
6962306a36Sopenharmony_ci	eor		\out1, \out1, w17, ror #16
7062306a36Sopenharmony_ci	eor		\out0, \out0, \t0, ror #8
7162306a36Sopenharmony_ci	eor		\out1, \out1, \t1, ror #8
7262306a36Sopenharmony_ci	.endm
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	.macro		fround, out0, out1, out2, out3, in0, in1, in2, in3, sz=2, op
7562306a36Sopenharmony_ci	__hround	\out0, \out1, \in0, \in1, \in2, \in3, \out2, \out3, 1, \sz, \op
7662306a36Sopenharmony_ci	__hround	\out2, \out3, \in2, \in3, \in0, \in1, \in1, \in2, 1, \sz, \op
7762306a36Sopenharmony_ci	.endm
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	.macro		iround, out0, out1, out2, out3, in0, in1, in2, in3, sz=2, op
8062306a36Sopenharmony_ci	__hround	\out0, \out1, \in0, \in3, \in2, \in1, \out2, \out3, 0, \sz, \op
8162306a36Sopenharmony_ci	__hround	\out2, \out3, \in2, \in1, \in0, \in3, \in1, \in0, 0, \sz, \op
8262306a36Sopenharmony_ci	.endm
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	.macro		do_crypt, round, ttab, ltab, bsz
8562306a36Sopenharmony_ci	ldp		w4, w5, [in]
8662306a36Sopenharmony_ci	ldp		w6, w7, [in, #8]
8762306a36Sopenharmony_ci	ldp		w8, w9, [rk], #16
8862306a36Sopenharmony_ci	ldp		w10, w11, [rk, #-8]
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciCPU_BE(	rev		w4, w4		)
9162306a36Sopenharmony_ciCPU_BE(	rev		w5, w5		)
9262306a36Sopenharmony_ciCPU_BE(	rev		w6, w6		)
9362306a36Sopenharmony_ciCPU_BE(	rev		w7, w7		)
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	eor		w4, w4, w8
9662306a36Sopenharmony_ci	eor		w5, w5, w9
9762306a36Sopenharmony_ci	eor		w6, w6, w10
9862306a36Sopenharmony_ci	eor		w7, w7, w11
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	adr_l		tt, \ttab
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	tbnz		rounds, #1, 1f
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci0:	\round		w8, w9, w10, w11, w4, w5, w6, w7
10562306a36Sopenharmony_ci	\round		w4, w5, w6, w7, w8, w9, w10, w11
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci1:	subs		rounds, rounds, #4
10862306a36Sopenharmony_ci	\round		w8, w9, w10, w11, w4, w5, w6, w7
10962306a36Sopenharmony_ci	b.ls		3f
11062306a36Sopenharmony_ci2:	\round		w4, w5, w6, w7, w8, w9, w10, w11
11162306a36Sopenharmony_ci	b		0b
11262306a36Sopenharmony_ci3:	adr_l		tt, \ltab
11362306a36Sopenharmony_ci	\round		w4, w5, w6, w7, w8, w9, w10, w11, \bsz, b
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciCPU_BE(	rev		w4, w4		)
11662306a36Sopenharmony_ciCPU_BE(	rev		w5, w5		)
11762306a36Sopenharmony_ciCPU_BE(	rev		w6, w6		)
11862306a36Sopenharmony_ciCPU_BE(	rev		w7, w7		)
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	stp		w4, w5, [out]
12162306a36Sopenharmony_ci	stp		w6, w7, [out, #8]
12262306a36Sopenharmony_ci	ret
12362306a36Sopenharmony_ci	.endm
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciSYM_FUNC_START(__aes_arm64_encrypt)
12662306a36Sopenharmony_ci	do_crypt	fround, crypto_ft_tab, crypto_ft_tab + 1, 2
12762306a36Sopenharmony_ciSYM_FUNC_END(__aes_arm64_encrypt)
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	.align		5
13062306a36Sopenharmony_ciSYM_FUNC_START(__aes_arm64_decrypt)
13162306a36Sopenharmony_ci	do_crypt	iround, crypto_it_tab, crypto_aes_inv_sbox, 0
13262306a36Sopenharmony_ciSYM_FUNC_END(__aes_arm64_decrypt)
133