1/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
2//
3// AES-XTS for modern x86_64 CPUs
4//
5// Copyright 2024 Google LLC
6//
7// Author: Eric Biggers <[email protected]>
8//
9//------------------------------------------------------------------------------
10//
11// This file is dual-licensed, meaning that you can use it under your choice of
12// either of the following two licenses:
13//
14// Licensed under the Apache License 2.0 (the "License").  You may obtain a copy
15// of the License at
16//
17//	http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software
20// distributed under the License is distributed on an "AS IS" BASIS,
21// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22// See the License for the specific language governing permissions and
23// limitations under the License.
24//
25// or
26//
27// Redistribution and use in source and binary forms, with or without
28// modification, are permitted provided that the following conditions are met:
29//
30// 1. Redistributions of source code must retain the above copyright notice,
31//    this list of conditions and the following disclaimer.
32//
33// 2. Redistributions in binary form must reproduce the above copyright
34//    notice, this list of conditions and the following disclaimer in the
35//    documentation and/or other materials provided with the distribution.
36//
37// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47// POSSIBILITY OF SUCH DAMAGE.
48
49/*
50 * This file implements AES-XTS for modern x86_64 CPUs.  To handle the
51 * complexities of coding for x86 SIMD, e.g. where every vector length needs
52 * different code, it uses a macro to generate several implementations that
53 * share similar source code but are targeted at different CPUs, listed below:
54 *
55 * AES-NI + AVX
56 *    - 128-bit vectors (1 AES block per vector)
57 *    - VEX-coded instructions
58 *    - xmm0-xmm15
59 *    - This is for older CPUs that lack VAES but do have AVX.
60 *
61 * VAES + VPCLMULQDQ + AVX2
62 *    - 256-bit vectors (2 AES blocks per vector)
63 *    - VEX-coded instructions
64 *    - ymm0-ymm15
65 *    - This is for CPUs that have VAES but lack AVX512 or AVX10,
66 *      e.g. Intel's Alder Lake and AMD's Zen 3.
67 *
68 * VAES + VPCLMULQDQ + AVX10/256 + BMI2
69 *    - 256-bit vectors (2 AES blocks per vector)
70 *    - EVEX-coded instructions
71 *    - ymm0-ymm31
72 *    - This is for CPUs that have AVX512 but where using zmm registers causes
73 *      downclocking, and for CPUs that have AVX10/256 but not AVX10/512.
74 *    - By "AVX10/256" we really mean (AVX512BW + AVX512VL) || AVX10/256.
75 *      To avoid confusion with 512-bit, we just write AVX10/256.
76 *
77 * VAES + VPCLMULQDQ + AVX10/512 + BMI2
78 *    - Same as the previous one, but upgrades to 512-bit vectors
79 *      (4 AES blocks per vector) in zmm0-zmm31.
80 *    - This is for CPUs that have good AVX512 or AVX10/512 support.
81 *
82 * This file doesn't have an implementation for AES-NI alone (without AVX), as
83 * the lack of VEX would make all the assembly code different.
84 *
85 * When we use VAES, we also use VPCLMULQDQ to parallelize the computation of
86 * the XTS tweaks.  This avoids a bottleneck.  Currently there don't seem to be
87 * any CPUs that support VAES but not VPCLMULQDQ.  If that changes, we might
88 * need to start also providing an implementation using VAES alone.
89 *
90 * The AES-XTS implementations in this file support everything required by the
91 * crypto API, including support for arbitrary input lengths and multi-part
92 * processing.  However, they are most heavily optimized for the common case of
93 * power-of-2 length inputs that are processed in a single part (disk sectors).
94 */
95
96#include <linux/linkage.h>
97#include <linux/cfi_types.h>
98
99.section .rodata
100.p2align 4
101.Lgf_poly:
102	// The low 64 bits of this value represent the polynomial x^7 + x^2 + x
103	// + 1.  It is the value that must be XOR'd into the low 64 bits of the
104	// tweak each time a 1 is carried out of the high 64 bits.
105	//
106	// The high 64 bits of this value is just the internal carry bit that
107	// exists when there's a carry out of the low 64 bits of the tweak.
108	.quad	0x87, 1
109
110	// This table contains constants for vpshufb and vpblendvb, used to
111	// handle variable byte shifts and blending during ciphertext stealing
112	// on CPUs that don't support AVX10-style masking.
113.Lcts_permute_table:
114	.byte	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
115	.byte	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
116	.byte	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
117	.byte	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
118	.byte	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
119	.byte	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
120.text
121
122.macro	_define_Vi	i
123.if VL == 16
124	.set	V\i,		%xmm\i
125.elseif VL == 32
126	.set	V\i,		%ymm\i
127.elseif VL == 64
128	.set	V\i,		%zmm\i
129.else
130	.error "Unsupported Vector Length (VL)"
131.endif
132.endm
133
134.macro _define_aliases
135	// Define register aliases V0-V15, or V0-V31 if all 32 SIMD registers
136	// are available, that map to the xmm, ymm, or zmm registers according
137	// to the selected Vector Length (VL).
138.irp i, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
139	_define_Vi	\i
140.endr
141.if USE_AVX10
142.irp i, 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
143	_define_Vi	\i
144.endr
145.endif
146
147	// Function parameters
148	.set	KEY,		%rdi	// Initially points to crypto_aes_ctx, then is
149					// advanced to point to 7th-from-last round key
150	.set	SRC,		%rsi	// Pointer to next source data
151	.set	DST,		%rdx	// Pointer to next destination data
152	.set	LEN,		%ecx	// Remaining length in bytes
153	.set	LEN8,		%cl
154	.set	LEN64,		%rcx
155	.set	TWEAK,		%r8	// Pointer to next tweak
156
157	// %rax holds the AES key length in bytes.
158	.set	KEYLEN,		%eax
159	.set	KEYLEN64,	%rax
160
161	// %r9-r11 are available as temporaries.
162
163	// V0-V3 hold the data blocks during the main loop, or temporary values
164	// otherwise.  V4-V5 hold temporary values.
165
166	// V6-V9 hold XTS tweaks.  Each 128-bit lane holds one tweak.
167	.set	TWEAK0_XMM,	%xmm6
168	.set	TWEAK0,		V6
169	.set	TWEAK1_XMM,	%xmm7
170	.set	TWEAK1,		V7
171	.set	TWEAK2,		V8
172	.set	TWEAK3,		V9
173
174	// V10-V13 are used for computing the next values of TWEAK[0-3].
175	.set	NEXT_TWEAK0,	V10
176	.set	NEXT_TWEAK1,	V11
177	.set	NEXT_TWEAK2,	V12
178	.set	NEXT_TWEAK3,	V13
179
180	// V14 holds the constant from .Lgf_poly, copied to all 128-bit lanes.
181	.set	GF_POLY_XMM,	%xmm14
182	.set	GF_POLY,	V14
183
184	// V15 holds the key for AES "round 0", copied to all 128-bit lanes.
185	.set	KEY0_XMM,	%xmm15
186	.set	KEY0,		V15
187
188	// If 32 SIMD registers are available, then V16-V29 hold the remaining
189	// AES round keys, copied to all 128-bit lanes.
190	//
191	// AES-128, AES-192, and AES-256 use different numbers of round keys.
192	// To allow handling all three variants efficiently, we align the round
193	// keys to the *end* of this register range.  I.e., AES-128 uses
194	// KEY5-KEY14, AES-192 uses KEY3-KEY14, and AES-256 uses KEY1-KEY14.
195	// (All also use KEY0 for the XOR-only "round" at the beginning.)
196.if USE_AVX10
197	.set	KEY1_XMM,	%xmm16
198	.set	KEY1,		V16
199	.set	KEY2_XMM,	%xmm17
200	.set	KEY2,		V17
201	.set	KEY3_XMM,	%xmm18
202	.set	KEY3,		V18
203	.set	KEY4_XMM,	%xmm19
204	.set	KEY4,		V19
205	.set	KEY5_XMM,	%xmm20
206	.set	KEY5,		V20
207	.set	KEY6_XMM,	%xmm21
208	.set	KEY6,		V21
209	.set	KEY7_XMM,	%xmm22
210	.set	KEY7,		V22
211	.set	KEY8_XMM,	%xmm23
212	.set	KEY8,		V23
213	.set	KEY9_XMM,	%xmm24
214	.set	KEY9,		V24
215	.set	KEY10_XMM,	%xmm25
216	.set	KEY10,		V25
217	.set	KEY11_XMM,	%xmm26
218	.set	KEY11,		V26
219	.set	KEY12_XMM,	%xmm27
220	.set	KEY12,		V27
221	.set	KEY13_XMM,	%xmm28
222	.set	KEY13,		V28
223	.set	KEY14_XMM,	%xmm29
224	.set	KEY14,		V29
225.endif
226	// V30-V31 are currently unused.
227.endm
228
229// Move a vector between memory and a register.
230// The register operand must be in the first 16 vector registers.
231.macro	_vmovdqu	src, dst
232.if VL < 64
233	vmovdqu		\src, \dst
234.else
235	vmovdqu8	\src, \dst
236.endif
237.endm
238
239// Broadcast a 128-bit value into a vector.
240.macro	_vbroadcast128	src, dst
241.if VL == 16 && !USE_AVX10
242	vmovdqu		\src, \dst
243.elseif VL == 32 && !USE_AVX10
244	vbroadcasti128	\src, \dst
245.else
246	vbroadcasti32x4	\src, \dst
247.endif
248.endm
249
250// XOR two vectors together.
251// Any register operands must be in the first 16 vector registers.
252.macro	_vpxor	src1, src2, dst
253.if VL < 64
254	vpxor		\src1, \src2, \dst
255.else
256	vpxord		\src1, \src2, \dst
257.endif
258.endm
259
260// XOR three vectors together.
261.macro	_xor3	src1, src2, src3_and_dst
262.if USE_AVX10
263	// vpternlogd with immediate 0x96 is a three-argument XOR.
264	vpternlogd	$0x96, \src1, \src2, \src3_and_dst
265.else
266	vpxor		\src1, \src3_and_dst, \src3_and_dst
267	vpxor		\src2, \src3_and_dst, \src3_and_dst
268.endif
269.endm
270
271// Given a 128-bit XTS tweak in the xmm register \src, compute the next tweak
272// (by multiplying by the polynomial 'x') and write it to \dst.
273.macro	_next_tweak	src, tmp, dst
274	vpshufd		$0x13, \src, \tmp
275	vpaddq		\src, \src, \dst
276	vpsrad		$31, \tmp, \tmp
277.if USE_AVX10
278	vpternlogd	$0x78, GF_POLY_XMM, \tmp, \dst
279.else
280	vpand		GF_POLY_XMM, \tmp, \tmp
281	vpxor		\tmp, \dst, \dst
282.endif
283.endm
284
285// Given the XTS tweak(s) in the vector \src, compute the next vector of
286// tweak(s) (by multiplying by the polynomial 'x^(VL/16)') and write it to \dst.
287//
288// If VL > 16, then there are multiple tweaks, and we use vpclmulqdq to compute
289// all tweaks in the vector in parallel.  If VL=16, we just do the regular
290// computation without vpclmulqdq, as it's the faster method for a single tweak.
291.macro	_next_tweakvec	src, tmp1, tmp2, dst
292.if VL == 16
293	_next_tweak	\src, \tmp1, \dst
294.else
295	vpsrlq		$64 - VL/16, \src, \tmp1
296	vpclmulqdq	$0x01, GF_POLY, \tmp1, \tmp2
297	vpslldq		$8, \tmp1, \tmp1
298	vpsllq		$VL/16, \src, \dst
299	_xor3		\tmp1, \tmp2, \dst
300.endif
301.endm
302
303// Given the first XTS tweak at (TWEAK), compute the first set of tweaks and
304// store them in the vector registers TWEAK0-TWEAK3.  Clobbers V0-V5.
305.macro	_compute_first_set_of_tweaks
306	vmovdqu		(TWEAK), TWEAK0_XMM
307	_vbroadcast128	.Lgf_poly(%rip), GF_POLY
308.if VL == 16
309	// With VL=16, multiplying by x serially is fastest.
310	_next_tweak	TWEAK0, %xmm0, TWEAK1
311	_next_tweak	TWEAK1, %xmm0, TWEAK2
312	_next_tweak	TWEAK2, %xmm0, TWEAK3
313.else
314.if VL == 32
315	// Compute the second block of TWEAK0.
316	_next_tweak	TWEAK0_XMM, %xmm0, %xmm1
317	vinserti128	$1, %xmm1, TWEAK0, TWEAK0
318.elseif VL == 64
319	// Compute the remaining blocks of TWEAK0.
320	_next_tweak	TWEAK0_XMM, %xmm0, %xmm1
321	_next_tweak	%xmm1, %xmm0, %xmm2
322	_next_tweak	%xmm2, %xmm0, %xmm3
323	vinserti32x4	$1, %xmm1, TWEAK0, TWEAK0
324	vinserti32x4	$2, %xmm2, TWEAK0, TWEAK0
325	vinserti32x4	$3, %xmm3, TWEAK0, TWEAK0
326.endif
327	// Compute TWEAK[1-3] from TWEAK0.
328	vpsrlq		$64 - 1*VL/16, TWEAK0, V0
329	vpsrlq		$64 - 2*VL/16, TWEAK0, V2
330	vpsrlq		$64 - 3*VL/16, TWEAK0, V4
331	vpclmulqdq	$0x01, GF_POLY, V0, V1
332	vpclmulqdq	$0x01, GF_POLY, V2, V3
333	vpclmulqdq	$0x01, GF_POLY, V4, V5
334	vpslldq		$8, V0, V0
335	vpslldq		$8, V2, V2
336	vpslldq		$8, V4, V4
337	vpsllq		$1*VL/16, TWEAK0, TWEAK1
338	vpsllq		$2*VL/16, TWEAK0, TWEAK2
339	vpsllq		$3*VL/16, TWEAK0, TWEAK3
340.if USE_AVX10
341	vpternlogd	$0x96, V0, V1, TWEAK1
342	vpternlogd	$0x96, V2, V3, TWEAK2
343	vpternlogd	$0x96, V4, V5, TWEAK3
344.else
345	vpxor		V0, TWEAK1, TWEAK1
346	vpxor		V2, TWEAK2, TWEAK2
347	vpxor		V4, TWEAK3, TWEAK3
348	vpxor		V1, TWEAK1, TWEAK1
349	vpxor		V3, TWEAK2, TWEAK2
350	vpxor		V5, TWEAK3, TWEAK3
351.endif
352.endif
353.endm
354
355// Do one step in computing the next set of tweaks using the method of just
356// multiplying by x repeatedly (the same method _next_tweak uses).
357.macro	_tweak_step_mulx	i
358.if \i == 0
359	.set PREV_TWEAK, TWEAK3
360	.set NEXT_TWEAK, NEXT_TWEAK0
361.elseif \i == 5
362	.set PREV_TWEAK, NEXT_TWEAK0
363	.set NEXT_TWEAK, NEXT_TWEAK1
364.elseif \i == 10
365	.set PREV_TWEAK, NEXT_TWEAK1
366	.set NEXT_TWEAK, NEXT_TWEAK2
367.elseif \i == 15
368	.set PREV_TWEAK, NEXT_TWEAK2
369	.set NEXT_TWEAK, NEXT_TWEAK3
370.endif
371.if \i >= 0 && \i < 20 && \i % 5 == 0
372	vpshufd		$0x13, PREV_TWEAK, V5
373.elseif \i >= 0 && \i < 20 && \i % 5 == 1
374	vpaddq		PREV_TWEAK, PREV_TWEAK, NEXT_TWEAK
375.elseif \i >= 0 && \i < 20 && \i % 5 == 2
376	vpsrad		$31, V5, V5
377.elseif \i >= 0 && \i < 20 && \i % 5 == 3
378	vpand		GF_POLY, V5, V5
379.elseif \i >= 0 && \i < 20 && \i % 5 == 4
380	vpxor		V5, NEXT_TWEAK, NEXT_TWEAK
381.elseif \i == 1000
382	vmovdqa		NEXT_TWEAK0, TWEAK0
383	vmovdqa		NEXT_TWEAK1, TWEAK1
384	vmovdqa		NEXT_TWEAK2, TWEAK2
385	vmovdqa		NEXT_TWEAK3, TWEAK3
386.endif
387.endm
388
389// Do one step in computing the next set of tweaks using the VPCLMULQDQ method
390// (the same method _next_tweakvec uses for VL > 16).  This means multiplying
391// each tweak by x^(4*VL/16) independently.
392//
393// Since 4*VL/16 is a multiple of 8 when VL > 16 (which it is here), the needed
394// shift amounts are byte-aligned, which allows the use of vpsrldq and vpslldq
395// to do 128-bit wide shifts.  The 128-bit left shift (vpslldq) saves
396// instructions directly.  The 128-bit right shift (vpsrldq) performs better
397// than a 64-bit right shift on Intel CPUs in the context where it is used here,
398// because it runs on a different execution port from the AES instructions.
399.macro	_tweak_step_pclmul	i
400.if \i == 0
401	vpsrldq		$(128 - 4*VL/16) / 8, TWEAK0, NEXT_TWEAK0
402.elseif \i == 2
403	vpsrldq		$(128 - 4*VL/16) / 8, TWEAK1, NEXT_TWEAK1
404.elseif \i == 4
405	vpsrldq		$(128 - 4*VL/16) / 8, TWEAK2, NEXT_TWEAK2
406.elseif \i == 6
407	vpsrldq		$(128 - 4*VL/16) / 8, TWEAK3, NEXT_TWEAK3
408.elseif \i == 8
409	vpclmulqdq	$0x00, GF_POLY, NEXT_TWEAK0, NEXT_TWEAK0
410.elseif \i == 10
411	vpclmulqdq	$0x00, GF_POLY, NEXT_TWEAK1, NEXT_TWEAK1
412.elseif \i == 12
413	vpclmulqdq	$0x00, GF_POLY, NEXT_TWEAK2, NEXT_TWEAK2
414.elseif \i == 14
415	vpclmulqdq	$0x00, GF_POLY, NEXT_TWEAK3, NEXT_TWEAK3
416.elseif \i == 1000
417	vpslldq		$(4*VL/16) / 8, TWEAK0, TWEAK0
418	vpslldq		$(4*VL/16) / 8, TWEAK1, TWEAK1
419	vpslldq		$(4*VL/16) / 8, TWEAK2, TWEAK2
420	vpslldq		$(4*VL/16) / 8, TWEAK3, TWEAK3
421	_vpxor		NEXT_TWEAK0, TWEAK0, TWEAK0
422	_vpxor		NEXT_TWEAK1, TWEAK1, TWEAK1
423	_vpxor		NEXT_TWEAK2, TWEAK2, TWEAK2
424	_vpxor		NEXT_TWEAK3, TWEAK3, TWEAK3
425.endif
426.endm
427
428// _tweak_step does one step of the computation of the next set of tweaks from
429// TWEAK[0-3].  To complete all steps, this is invoked with increasing values of
430// \i that include at least 0 through 19, then 1000 which signals the last step.
431//
432// This is used to interleave the computation of the next set of tweaks with the
433// AES en/decryptions, which increases performance in some cases.  Clobbers V5.
434.macro	_tweak_step	i
435.if VL == 16
436	_tweak_step_mulx	\i
437.else
438	_tweak_step_pclmul	\i
439.endif
440.endm
441
442.macro	_setup_round_keys	enc
443
444	// Select either the encryption round keys or the decryption round keys.
445.if \enc
446	.set	OFFS, 0
447.else
448	.set	OFFS, 240
449.endif
450
451	// Load the round key for "round 0".
452	_vbroadcast128	OFFS(KEY), KEY0
453
454	// Increment KEY to make it so that 7*16(KEY) is the last round key.
455	// For AES-128, increment by 3*16, resulting in the 10 round keys (not
456	// counting the zero-th round key which was just loaded into KEY0) being
457	// -2*16(KEY) through 7*16(KEY).  For AES-192, increment by 5*16 and use
458	// 12 round keys -4*16(KEY) through 7*16(KEY).  For AES-256, increment
459	// by 7*16 and use 14 round keys -6*16(KEY) through 7*16(KEY).
460	//
461	// This rebasing provides two benefits.  First, it makes the offset to
462	// any round key be in the range [-96, 112], fitting in a signed byte.
463	// This shortens VEX-encoded instructions that access the later round
464	// keys which otherwise would need 4-byte offsets.  Second, it makes it
465	// easy to do AES-128 and AES-192 by skipping irrelevant rounds at the
466	// beginning.  Skipping rounds at the end doesn't work as well because
467	// the last round needs different instructions.
468	//
469	// An alternative approach would be to roll up all the round loops.  We
470	// don't do that because (a) it isn't compatible with caching the round
471	// keys in registers which we do when possible (see below), (b) we
472	// interleave the AES rounds with the XTS tweak computation, and (c) it
473	// seems unwise to rely *too* heavily on the CPU's branch predictor.
474	lea		OFFS-16(KEY, KEYLEN64, 4), KEY
475
476	// If all 32 SIMD registers are available, cache all the round keys.
477.if USE_AVX10
478	cmp		$24, KEYLEN
479	jl		.Laes128\@
480	je		.Laes192\@
481	_vbroadcast128	-6*16(KEY), KEY1
482	_vbroadcast128	-5*16(KEY), KEY2
483.Laes192\@:
484	_vbroadcast128	-4*16(KEY), KEY3
485	_vbroadcast128	-3*16(KEY), KEY4
486.Laes128\@:
487	_vbroadcast128	-2*16(KEY), KEY5
488	_vbroadcast128	-1*16(KEY), KEY6
489	_vbroadcast128	0*16(KEY), KEY7
490	_vbroadcast128	1*16(KEY), KEY8
491	_vbroadcast128	2*16(KEY), KEY9
492	_vbroadcast128	3*16(KEY), KEY10
493	_vbroadcast128	4*16(KEY), KEY11
494	_vbroadcast128	5*16(KEY), KEY12
495	_vbroadcast128	6*16(KEY), KEY13
496	_vbroadcast128	7*16(KEY), KEY14
497.endif
498.endm
499
500// Do a single non-last round of AES encryption (if \enc==1) or decryption (if
501// \enc==0) on the block(s) in \data using the round key(s) in \key.  The
502// register length determines the number of AES blocks en/decrypted.
503.macro	_vaes	enc, key, data
504.if \enc
505	vaesenc		\key, \data, \data
506.else
507	vaesdec		\key, \data, \data
508.endif
509.endm
510
511// Same as _vaes, but does the last round.
512.macro	_vaeslast	enc, key, data
513.if \enc
514	vaesenclast	\key, \data, \data
515.else
516	vaesdeclast	\key, \data, \data
517.endif
518.endm
519
520// Do a single non-last round of AES en/decryption on the block(s) in \data,
521// using the same key for all block(s).  The round key is loaded from the
522// appropriate register or memory location for round \i.  May clobber \tmp.
523.macro _vaes_1x		enc, i, xmm_suffix, data, tmp
524.if USE_AVX10
525	_vaes		\enc, KEY\i\xmm_suffix, \data
526.else
527.ifnb \xmm_suffix
528	_vaes		\enc, (\i-7)*16(KEY), \data
529.else
530	_vbroadcast128	(\i-7)*16(KEY), \tmp
531	_vaes		\enc, \tmp, \data
532.endif
533.endif
534.endm
535
536// Do a single non-last round of AES en/decryption on the blocks in registers
537// V0-V3, using the same key for all blocks.  The round key is loaded from the
538// appropriate register or memory location for round \i.  In addition, does two
539// steps of the computation of the next set of tweaks.  May clobber V4 and V5.
540.macro	_vaes_4x	enc, i
541.if USE_AVX10
542	_tweak_step	(2*(\i-5))
543	_vaes		\enc, KEY\i, V0
544	_vaes		\enc, KEY\i, V1
545	_tweak_step	(2*(\i-5) + 1)
546	_vaes		\enc, KEY\i, V2
547	_vaes		\enc, KEY\i, V3
548.else
549	_vbroadcast128	(\i-7)*16(KEY), V4
550	_tweak_step	(2*(\i-5))
551	_vaes		\enc, V4, V0
552	_vaes		\enc, V4, V1
553	_tweak_step	(2*(\i-5) + 1)
554	_vaes		\enc, V4, V2
555	_vaes		\enc, V4, V3
556.endif
557.endm
558
559// Do tweaked AES en/decryption (i.e., XOR with \tweak, then AES en/decrypt,
560// then XOR with \tweak again) of the block(s) in \data.  To process a single
561// block, use xmm registers and set \xmm_suffix=_XMM.  To process a vector of
562// length VL, use V* registers and leave \xmm_suffix empty.  Clobbers \tmp.
563.macro	_aes_crypt	enc, xmm_suffix, tweak, data, tmp
564	_xor3		KEY0\xmm_suffix, \tweak, \data
565	cmp		$24, KEYLEN
566	jl		.Laes128\@
567	je		.Laes192\@
568	_vaes_1x	\enc, 1, \xmm_suffix, \data, tmp=\tmp
569	_vaes_1x	\enc, 2, \xmm_suffix, \data, tmp=\tmp
570.Laes192\@:
571	_vaes_1x	\enc, 3, \xmm_suffix, \data, tmp=\tmp
572	_vaes_1x	\enc, 4, \xmm_suffix, \data, tmp=\tmp
573.Laes128\@:
574.irp i, 5,6,7,8,9,10,11,12,13
575	_vaes_1x	\enc, \i, \xmm_suffix, \data, tmp=\tmp
576.endr
577.if USE_AVX10
578	vpxord		KEY14\xmm_suffix, \tweak, \tmp
579.else
580.ifnb \xmm_suffix
581	vpxor		7*16(KEY), \tweak, \tmp
582.else
583	_vbroadcast128	7*16(KEY), \tmp
584	vpxor		\tweak, \tmp, \tmp
585.endif
586.endif
587	_vaeslast	\enc, \tmp, \data
588.endm
589
590.macro	_aes_xts_crypt	enc
591	_define_aliases
592
593.if !\enc
594	// When decrypting a message whose length isn't a multiple of the AES
595	// block length, exclude the last full block from the main loop by
596	// subtracting 16 from LEN.  This is needed because ciphertext stealing
597	// decryption uses the last two tweaks in reverse order.  We'll handle
598	// the last full block and the partial block specially at the end.
599	lea		-16(LEN), %eax
600	test		$15, LEN8
601	cmovnz		%eax, LEN
602.endif
603
604	// Load the AES key length: 16 (AES-128), 24 (AES-192), or 32 (AES-256).
605	movl		480(KEY), KEYLEN
606
607	// Setup the pointer to the round keys and cache as many as possible.
608	_setup_round_keys	\enc
609
610	// Compute the first set of tweaks TWEAK[0-3].
611	_compute_first_set_of_tweaks
612
613	add		$-4*VL, LEN  // shorter than 'sub 4*VL' when VL=32
614	jl		.Lhandle_remainder\@
615
616.Lmain_loop\@:
617	// This is the main loop, en/decrypting 4*VL bytes per iteration.
618
619	// XOR each source block with its tweak and the zero-th round key.
620.if USE_AVX10
621	_vmovdqu	0*VL(SRC), V0
622	_vmovdqu	1*VL(SRC), V1
623	_vmovdqu	2*VL(SRC), V2
624	_vmovdqu	3*VL(SRC), V3
625	vpternlogd	$0x96, TWEAK0, KEY0, V0
626	vpternlogd	$0x96, TWEAK1, KEY0, V1
627	vpternlogd	$0x96, TWEAK2, KEY0, V2
628	vpternlogd	$0x96, TWEAK3, KEY0, V3
629.else
630	vpxor		0*VL(SRC), KEY0, V0
631	vpxor		1*VL(SRC), KEY0, V1
632	vpxor		2*VL(SRC), KEY0, V2
633	vpxor		3*VL(SRC), KEY0, V3
634	vpxor		TWEAK0, V0, V0
635	vpxor		TWEAK1, V1, V1
636	vpxor		TWEAK2, V2, V2
637	vpxor		TWEAK3, V3, V3
638.endif
639	cmp		$24, KEYLEN
640	jl		.Laes128\@
641	je		.Laes192\@
642	// Do all the AES rounds on the data blocks, interleaved with
643	// the computation of the next set of tweaks.
644	_vaes_4x	\enc, 1
645	_vaes_4x	\enc, 2
646.Laes192\@:
647	_vaes_4x	\enc, 3
648	_vaes_4x	\enc, 4
649.Laes128\@:
650.irp i, 5,6,7,8,9,10,11,12,13
651	_vaes_4x	\enc, \i
652.endr
653	// Do the last AES round, then XOR the results with the tweaks again.
654	// Reduce latency by doing the XOR before the vaesenclast, utilizing the
655	// property vaesenclast(key, a) ^ b == vaesenclast(key ^ b, a)
656	// (and likewise for vaesdeclast).
657.if USE_AVX10
658	_tweak_step	18
659	_tweak_step	19
660	vpxord		TWEAK0, KEY14, V4
661	vpxord		TWEAK1, KEY14, V5
662	_vaeslast	\enc, V4, V0
663	_vaeslast	\enc, V5, V1
664	vpxord		TWEAK2, KEY14, V4
665	vpxord		TWEAK3, KEY14, V5
666	_vaeslast	\enc, V4, V2
667	_vaeslast	\enc, V5, V3
668.else
669	_vbroadcast128	7*16(KEY), V4
670	_tweak_step	18 // uses V5
671	_tweak_step	19 // uses V5
672	vpxor		TWEAK0, V4, V5
673	_vaeslast	\enc, V5, V0
674	vpxor		TWEAK1, V4, V5
675	_vaeslast	\enc, V5, V1
676	vpxor		TWEAK2, V4, V5
677	vpxor		TWEAK3, V4, V4
678	_vaeslast	\enc, V5, V2
679	_vaeslast	\enc, V4, V3
680.endif
681
682	// Store the destination blocks.
683	_vmovdqu	V0, 0*VL(DST)
684	_vmovdqu	V1, 1*VL(DST)
685	_vmovdqu	V2, 2*VL(DST)
686	_vmovdqu	V3, 3*VL(DST)
687
688	// Finish computing the next set of tweaks.
689	_tweak_step	1000
690
691	sub		$-4*VL, SRC  // shorter than 'add 4*VL' when VL=32
692	sub		$-4*VL, DST
693	add		$-4*VL, LEN
694	jge		.Lmain_loop\@
695
696	// Check for the uncommon case where the data length isn't a multiple of
697	// 4*VL.  Handle it out-of-line in order to optimize for the common
698	// case.  In the common case, just fall through to the ret.
699	test		$4*VL-1, LEN8
700	jnz		.Lhandle_remainder\@
701.Ldone\@:
702	// Store the next tweak back to *TWEAK to support continuation calls.
703	vmovdqu		TWEAK0_XMM, (TWEAK)
704.if VL > 16
705	vzeroupper
706.endif
707	RET
708
709.Lhandle_remainder\@:
710
711	// En/decrypt any remaining full blocks, one vector at a time.
712.if VL > 16
713	add		$3*VL, LEN	// Undo extra sub of 4*VL, then sub VL.
714	jl		.Lvec_at_a_time_done\@
715.Lvec_at_a_time\@:
716	_vmovdqu	(SRC), V0
717	_aes_crypt	\enc, , TWEAK0, V0, tmp=V1
718	_vmovdqu	V0, (DST)
719	_next_tweakvec	TWEAK0, V0, V1, TWEAK0
720	add		$VL, SRC
721	add		$VL, DST
722	sub		$VL, LEN
723	jge		.Lvec_at_a_time\@
724.Lvec_at_a_time_done\@:
725	add		$VL-16, LEN	// Undo extra sub of VL, then sub 16.
726.else
727	add		$4*VL-16, LEN	// Undo extra sub of 4*VL, then sub 16.
728.endif
729
730	// En/decrypt any remaining full blocks, one at a time.
731	jl		.Lblock_at_a_time_done\@
732.Lblock_at_a_time\@:
733	vmovdqu		(SRC), %xmm0
734	_aes_crypt	\enc, _XMM, TWEAK0_XMM, %xmm0, tmp=%xmm1
735	vmovdqu		%xmm0, (DST)
736	_next_tweak	TWEAK0_XMM, %xmm0, TWEAK0_XMM
737	add		$16, SRC
738	add		$16, DST
739	sub		$16, LEN
740	jge		.Lblock_at_a_time\@
741.Lblock_at_a_time_done\@:
742	add		$16, LEN	// Undo the extra sub of 16.
743	// Now 0 <= LEN <= 15.  If LEN is zero, we're done.
744	jz		.Ldone\@
745
746	// Otherwise 1 <= LEN <= 15, but the real remaining length is 16 + LEN.
747	// Do ciphertext stealing to process the last 16 + LEN bytes.
748
749.if \enc
750	// If encrypting, the main loop already encrypted the last full block to
751	// create the CTS intermediate ciphertext.  Prepare for the rest of CTS
752	// by rewinding the pointers and loading the intermediate ciphertext.
753	sub		$16, SRC
754	sub		$16, DST
755	vmovdqu		(DST), %xmm0
756.else
757	// If decrypting, the main loop didn't decrypt the last full block
758	// because CTS decryption uses the last two tweaks in reverse order.
759	// Do it now by advancing the tweak and decrypting the last full block.
760	_next_tweak	TWEAK0_XMM, %xmm0, TWEAK1_XMM
761	vmovdqu		(SRC), %xmm0
762	_aes_crypt	\enc, _XMM, TWEAK1_XMM, %xmm0, tmp=%xmm1
763.endif
764
765.if USE_AVX10
766	// Create a mask that has the first LEN bits set.
767	mov		$-1, %r9d
768	bzhi		LEN, %r9d, %r9d
769	kmovd		%r9d, %k1
770
771	// Swap the first LEN bytes of the en/decryption of the last full block
772	// with the partial block.  Note that to support in-place en/decryption,
773	// the load from the src partial block must happen before the store to
774	// the dst partial block.
775	vmovdqa		%xmm0, %xmm1
776	vmovdqu8	16(SRC), %xmm0{%k1}
777	vmovdqu8	%xmm1, 16(DST){%k1}
778.else
779	lea		.Lcts_permute_table(%rip), %r9
780
781	// Load the src partial block, left-aligned.  Note that to support
782	// in-place en/decryption, this must happen before the store to the dst
783	// partial block.
784	vmovdqu		(SRC, LEN64, 1), %xmm1
785
786	// Shift the first LEN bytes of the en/decryption of the last full block
787	// to the end of a register, then store it to DST+LEN.  This stores the
788	// dst partial block.  It also writes to the second part of the dst last
789	// full block, but that part is overwritten later.
790	vpshufb		(%r9, LEN64, 1), %xmm0, %xmm2
791	vmovdqu		%xmm2, (DST, LEN64, 1)
792
793	// Make xmm3 contain [16-LEN,16-LEN+1,...,14,15,0x80,0x80,...].
794	sub		LEN64, %r9
795	vmovdqu		32(%r9), %xmm3
796
797	// Shift the src partial block to the beginning of its register.
798	vpshufb		%xmm3, %xmm1, %xmm1
799
800	// Do a blend to generate the src partial block followed by the second
801	// part of the en/decryption of the last full block.
802	vpblendvb	%xmm3, %xmm0, %xmm1, %xmm0
803.endif
804	// En/decrypt again and store the last full block.
805	_aes_crypt	\enc, _XMM, TWEAK0_XMM, %xmm0, tmp=%xmm1
806	vmovdqu		%xmm0, (DST)
807	jmp		.Ldone\@
808.endm
809
810// void aes_xts_encrypt_iv(const struct crypto_aes_ctx *tweak_key,
811//			   u8 iv[AES_BLOCK_SIZE]);
812//
813// Encrypt |iv| using the AES key |tweak_key| to get the first tweak.  Assumes
814// that the CPU supports AES-NI and AVX, but not necessarily VAES or AVX10.
815SYM_TYPED_FUNC_START(aes_xts_encrypt_iv)
816	.set	TWEAK_KEY,	%rdi
817	.set	IV,		%rsi
818	.set	KEYLEN,		%eax
819	.set	KEYLEN64,	%rax
820
821	vmovdqu		(IV), %xmm0
822	vpxor		(TWEAK_KEY), %xmm0, %xmm0
823	movl		480(TWEAK_KEY), KEYLEN
824	lea		-16(TWEAK_KEY, KEYLEN64, 4), TWEAK_KEY
825	cmp		$24, KEYLEN
826	jl		.Lencrypt_iv_aes128
827	je		.Lencrypt_iv_aes192
828	vaesenc		-6*16(TWEAK_KEY), %xmm0, %xmm0
829	vaesenc		-5*16(TWEAK_KEY), %xmm0, %xmm0
830.Lencrypt_iv_aes192:
831	vaesenc		-4*16(TWEAK_KEY), %xmm0, %xmm0
832	vaesenc		-3*16(TWEAK_KEY), %xmm0, %xmm0
833.Lencrypt_iv_aes128:
834.irp i, -2,-1,0,1,2,3,4,5,6
835	vaesenc		\i*16(TWEAK_KEY), %xmm0, %xmm0
836.endr
837	vaesenclast	7*16(TWEAK_KEY), %xmm0, %xmm0
838	vmovdqu		%xmm0, (IV)
839	RET
840SYM_FUNC_END(aes_xts_encrypt_iv)
841
842// Below are the actual AES-XTS encryption and decryption functions,
843// instantiated from the above macro.  They all have the following prototype:
844//
845// void (*xts_crypt_func)(const struct crypto_aes_ctx *key,
846//			  const u8 *src, u8 *dst, int len,
847//			  u8 tweak[AES_BLOCK_SIZE]);
848//
849// |key| is the data key.  |tweak| contains the next tweak; the encryption of
850// the original IV with the tweak key was already done.  This function supports
851// incremental computation, but |len| must always be >= 16 (AES_BLOCK_SIZE), and
852// |len| must be a multiple of 16 except on the last call.  If |len| is a
853// multiple of 16, then this function updates |tweak| to contain the next tweak.
854
855.set	VL, 16
856.set	USE_AVX10, 0
857SYM_TYPED_FUNC_START(aes_xts_encrypt_aesni_avx)
858	_aes_xts_crypt	1
859SYM_FUNC_END(aes_xts_encrypt_aesni_avx)
860SYM_TYPED_FUNC_START(aes_xts_decrypt_aesni_avx)
861	_aes_xts_crypt	0
862SYM_FUNC_END(aes_xts_decrypt_aesni_avx)
863
864#if defined(CONFIG_AS_VAES) && defined(CONFIG_AS_VPCLMULQDQ)
865.set	VL, 32
866.set	USE_AVX10, 0
867SYM_TYPED_FUNC_START(aes_xts_encrypt_vaes_avx2)
868	_aes_xts_crypt	1
869SYM_FUNC_END(aes_xts_encrypt_vaes_avx2)
870SYM_TYPED_FUNC_START(aes_xts_decrypt_vaes_avx2)
871	_aes_xts_crypt	0
872SYM_FUNC_END(aes_xts_decrypt_vaes_avx2)
873
874.set	VL, 32
875.set	USE_AVX10, 1
876SYM_TYPED_FUNC_START(aes_xts_encrypt_vaes_avx10_256)
877	_aes_xts_crypt	1
878SYM_FUNC_END(aes_xts_encrypt_vaes_avx10_256)
879SYM_TYPED_FUNC_START(aes_xts_decrypt_vaes_avx10_256)
880	_aes_xts_crypt	0
881SYM_FUNC_END(aes_xts_decrypt_vaes_avx10_256)
882
883.set	VL, 64
884.set	USE_AVX10, 1
885SYM_TYPED_FUNC_START(aes_xts_encrypt_vaes_avx10_512)
886	_aes_xts_crypt	1
887SYM_FUNC_END(aes_xts_encrypt_vaes_avx10_512)
888SYM_TYPED_FUNC_START(aes_xts_decrypt_vaes_avx10_512)
889	_aes_xts_crypt	0
890SYM_FUNC_END(aes_xts_decrypt_vaes_avx10_512)
891#endif /* CONFIG_AS_VAES && CONFIG_AS_VPCLMULQDQ */
892