1/*
2 * Scalar AES core transform
3 *
4 * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
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 version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/linkage.h>
12#include <asm/assembler.h>
13
14	.text
15
16	rk		.req	x0
17	out		.req	x1
18	in		.req	x2
19	rounds		.req	x3
20	tt		.req	x4
21	lt		.req	x2
22
23	.macro		__pair, enc, reg0, reg1, in0, in1e, in1d, shift
24	ubfx		\reg0, \in0, #\shift, #8
25	.if		\enc
26	ubfx		\reg1, \in1e, #\shift, #8
27	.else
28	ubfx		\reg1, \in1d, #\shift, #8
29	.endif
30	ldr		\reg0, [tt, \reg0, uxtw #2]
31	ldr		\reg1, [tt, \reg1, uxtw #2]
32	.endm
33
34	.macro		__hround, out0, out1, in0, in1, in2, in3, t0, t1, enc
35	ldp		\out0, \out1, [rk], #8
36
37	__pair		\enc, w13, w14, \in0, \in1, \in3, 0
38	__pair		\enc, w15, w16, \in1, \in2, \in0, 8
39	__pair		\enc, w17, w18, \in2, \in3, \in1, 16
40	__pair		\enc, \t0, \t1, \in3, \in0, \in2, 24
41
42	eor		\out0, \out0, w13
43	eor		\out1, \out1, w14
44	eor		\out0, \out0, w15, ror #24
45	eor		\out1, \out1, w16, ror #24
46	eor		\out0, \out0, w17, ror #16
47	eor		\out1, \out1, w18, ror #16
48	eor		\out0, \out0, \t0, ror #8
49	eor		\out1, \out1, \t1, ror #8
50	.endm
51
52	.macro		fround, out0, out1, out2, out3, in0, in1, in2, in3
53	__hround	\out0, \out1, \in0, \in1, \in2, \in3, \out2, \out3, 1
54	__hround	\out2, \out3, \in2, \in3, \in0, \in1, \in1, \in2, 1
55	.endm
56
57	.macro		iround, out0, out1, out2, out3, in0, in1, in2, in3
58	__hround	\out0, \out1, \in0, \in3, \in2, \in1, \out2, \out3, 0
59	__hround	\out2, \out3, \in2, \in1, \in0, \in3, \in1, \in0, 0
60	.endm
61
62	.macro		do_crypt, round, ttab, ltab
63	ldp		w5, w6, [in]
64	ldp		w7, w8, [in, #8]
65	ldp		w9, w10, [rk], #16
66	ldp		w11, w12, [rk, #-8]
67
68CPU_BE(	rev		w5, w5		)
69CPU_BE(	rev		w6, w6		)
70CPU_BE(	rev		w7, w7		)
71CPU_BE(	rev		w8, w8		)
72
73	eor		w5, w5, w9
74	eor		w6, w6, w10
75	eor		w7, w7, w11
76	eor		w8, w8, w12
77
78	adr_l		tt, \ttab
79	adr_l		lt, \ltab
80
81	tbnz		rounds, #1, 1f
82
830:	\round		w9, w10, w11, w12, w5, w6, w7, w8
84	\round		w5, w6, w7, w8, w9, w10, w11, w12
85
861:	subs		rounds, rounds, #4
87	\round		w9, w10, w11, w12, w5, w6, w7, w8
88	csel		tt, tt, lt, hi
89	\round		w5, w6, w7, w8, w9, w10, w11, w12
90	b.hi		0b
91
92CPU_BE(	rev		w5, w5		)
93CPU_BE(	rev		w6, w6		)
94CPU_BE(	rev		w7, w7		)
95CPU_BE(	rev		w8, w8		)
96
97	stp		w5, w6, [out]
98	stp		w7, w8, [out, #8]
99	ret
100	.endm
101
102	.align		5
103ENTRY(__aes_arm64_encrypt)
104	do_crypt	fround, crypto_ft_tab, crypto_fl_tab
105ENDPROC(__aes_arm64_encrypt)
106
107	.align		5
108ENTRY(__aes_arm64_decrypt)
109	do_crypt	iround, crypto_it_tab, crypto_il_tab
110ENDPROC(__aes_arm64_decrypt)
111