18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Scalar AES core transform
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/linkage.h>
98c2ecf20Sopenharmony_ci#include <asm/assembler.h>
108c2ecf20Sopenharmony_ci#include <asm/cache.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci	.text
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci	rk		.req	x0
158c2ecf20Sopenharmony_ci	out		.req	x1
168c2ecf20Sopenharmony_ci	in		.req	x2
178c2ecf20Sopenharmony_ci	rounds		.req	x3
188c2ecf20Sopenharmony_ci	tt		.req	x2
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	.macro		__pair1, sz, op, reg0, reg1, in0, in1e, in1d, shift
218c2ecf20Sopenharmony_ci	.ifc		\op\shift, b0
228c2ecf20Sopenharmony_ci	ubfiz		\reg0, \in0, #2, #8
238c2ecf20Sopenharmony_ci	ubfiz		\reg1, \in1e, #2, #8
248c2ecf20Sopenharmony_ci	.else
258c2ecf20Sopenharmony_ci	ubfx		\reg0, \in0, #\shift, #8
268c2ecf20Sopenharmony_ci	ubfx		\reg1, \in1e, #\shift, #8
278c2ecf20Sopenharmony_ci	.endif
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * AArch64 cannot do byte size indexed loads from a table containing
318c2ecf20Sopenharmony_ci	 * 32-bit quantities, i.e., 'ldrb w12, [tt, w12, uxtw #2]' is not a
328c2ecf20Sopenharmony_ci	 * valid instruction. So perform the shift explicitly first for the
338c2ecf20Sopenharmony_ci	 * high bytes (the low byte is shifted implicitly by using ubfiz rather
348c2ecf20Sopenharmony_ci	 * than ubfx above)
358c2ecf20Sopenharmony_ci	 */
368c2ecf20Sopenharmony_ci	.ifnc		\op, b
378c2ecf20Sopenharmony_ci	ldr		\reg0, [tt, \reg0, uxtw #2]
388c2ecf20Sopenharmony_ci	ldr		\reg1, [tt, \reg1, uxtw #2]
398c2ecf20Sopenharmony_ci	.else
408c2ecf20Sopenharmony_ci	.if		\shift > 0
418c2ecf20Sopenharmony_ci	lsl		\reg0, \reg0, #2
428c2ecf20Sopenharmony_ci	lsl		\reg1, \reg1, #2
438c2ecf20Sopenharmony_ci	.endif
448c2ecf20Sopenharmony_ci	ldrb		\reg0, [tt, \reg0, uxtw]
458c2ecf20Sopenharmony_ci	ldrb		\reg1, [tt, \reg1, uxtw]
468c2ecf20Sopenharmony_ci	.endif
478c2ecf20Sopenharmony_ci	.endm
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	.macro		__pair0, sz, op, reg0, reg1, in0, in1e, in1d, shift
508c2ecf20Sopenharmony_ci	ubfx		\reg0, \in0, #\shift, #8
518c2ecf20Sopenharmony_ci	ubfx		\reg1, \in1d, #\shift, #8
528c2ecf20Sopenharmony_ci	ldr\op		\reg0, [tt, \reg0, uxtw #\sz]
538c2ecf20Sopenharmony_ci	ldr\op		\reg1, [tt, \reg1, uxtw #\sz]
548c2ecf20Sopenharmony_ci	.endm
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	.macro		__hround, out0, out1, in0, in1, in2, in3, t0, t1, enc, sz, op
578c2ecf20Sopenharmony_ci	ldp		\out0, \out1, [rk], #8
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	__pair\enc	\sz, \op, w12, w13, \in0, \in1, \in3, 0
608c2ecf20Sopenharmony_ci	__pair\enc	\sz, \op, w14, w15, \in1, \in2, \in0, 8
618c2ecf20Sopenharmony_ci	__pair\enc	\sz, \op, w16, w17, \in2, \in3, \in1, 16
628c2ecf20Sopenharmony_ci	__pair\enc	\sz, \op, \t0, \t1, \in3, \in0, \in2, 24
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	eor		\out0, \out0, w12
658c2ecf20Sopenharmony_ci	eor		\out1, \out1, w13
668c2ecf20Sopenharmony_ci	eor		\out0, \out0, w14, ror #24
678c2ecf20Sopenharmony_ci	eor		\out1, \out1, w15, ror #24
688c2ecf20Sopenharmony_ci	eor		\out0, \out0, w16, ror #16
698c2ecf20Sopenharmony_ci	eor		\out1, \out1, w17, ror #16
708c2ecf20Sopenharmony_ci	eor		\out0, \out0, \t0, ror #8
718c2ecf20Sopenharmony_ci	eor		\out1, \out1, \t1, ror #8
728c2ecf20Sopenharmony_ci	.endm
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	.macro		fround, out0, out1, out2, out3, in0, in1, in2, in3, sz=2, op
758c2ecf20Sopenharmony_ci	__hround	\out0, \out1, \in0, \in1, \in2, \in3, \out2, \out3, 1, \sz, \op
768c2ecf20Sopenharmony_ci	__hround	\out2, \out3, \in2, \in3, \in0, \in1, \in1, \in2, 1, \sz, \op
778c2ecf20Sopenharmony_ci	.endm
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	.macro		iround, out0, out1, out2, out3, in0, in1, in2, in3, sz=2, op
808c2ecf20Sopenharmony_ci	__hround	\out0, \out1, \in0, \in3, \in2, \in1, \out2, \out3, 0, \sz, \op
818c2ecf20Sopenharmony_ci	__hround	\out2, \out3, \in2, \in1, \in0, \in3, \in1, \in0, 0, \sz, \op
828c2ecf20Sopenharmony_ci	.endm
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	.macro		do_crypt, round, ttab, ltab, bsz
858c2ecf20Sopenharmony_ci	ldp		w4, w5, [in]
868c2ecf20Sopenharmony_ci	ldp		w6, w7, [in, #8]
878c2ecf20Sopenharmony_ci	ldp		w8, w9, [rk], #16
888c2ecf20Sopenharmony_ci	ldp		w10, w11, [rk, #-8]
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciCPU_BE(	rev		w4, w4		)
918c2ecf20Sopenharmony_ciCPU_BE(	rev		w5, w5		)
928c2ecf20Sopenharmony_ciCPU_BE(	rev		w6, w6		)
938c2ecf20Sopenharmony_ciCPU_BE(	rev		w7, w7		)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	eor		w4, w4, w8
968c2ecf20Sopenharmony_ci	eor		w5, w5, w9
978c2ecf20Sopenharmony_ci	eor		w6, w6, w10
988c2ecf20Sopenharmony_ci	eor		w7, w7, w11
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	adr_l		tt, \ttab
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	tbnz		rounds, #1, 1f
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci0:	\round		w8, w9, w10, w11, w4, w5, w6, w7
1058c2ecf20Sopenharmony_ci	\round		w4, w5, w6, w7, w8, w9, w10, w11
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci1:	subs		rounds, rounds, #4
1088c2ecf20Sopenharmony_ci	\round		w8, w9, w10, w11, w4, w5, w6, w7
1098c2ecf20Sopenharmony_ci	b.ls		3f
1108c2ecf20Sopenharmony_ci2:	\round		w4, w5, w6, w7, w8, w9, w10, w11
1118c2ecf20Sopenharmony_ci	b		0b
1128c2ecf20Sopenharmony_ci3:	adr_l		tt, \ltab
1138c2ecf20Sopenharmony_ci	\round		w4, w5, w6, w7, w8, w9, w10, w11, \bsz, b
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciCPU_BE(	rev		w4, w4		)
1168c2ecf20Sopenharmony_ciCPU_BE(	rev		w5, w5		)
1178c2ecf20Sopenharmony_ciCPU_BE(	rev		w6, w6		)
1188c2ecf20Sopenharmony_ciCPU_BE(	rev		w7, w7		)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	stp		w4, w5, [out]
1218c2ecf20Sopenharmony_ci	stp		w6, w7, [out, #8]
1228c2ecf20Sopenharmony_ci	ret
1238c2ecf20Sopenharmony_ci	.endm
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ciSYM_FUNC_START(__aes_arm64_encrypt)
1268c2ecf20Sopenharmony_ci	do_crypt	fround, crypto_ft_tab, crypto_ft_tab + 1, 2
1278c2ecf20Sopenharmony_ciSYM_FUNC_END(__aes_arm64_encrypt)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	.align		5
1308c2ecf20Sopenharmony_ciSYM_FUNC_START(__aes_arm64_decrypt)
1318c2ecf20Sopenharmony_ci	do_crypt	iround, crypto_it_tab, crypto_aes_inv_sbox, 0
1328c2ecf20Sopenharmony_ciSYM_FUNC_END(__aes_arm64_decrypt)
133