Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/crypto/aes-gcm-vaes-avx2.S
54067 views
1
/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
2
//
3
// AES-GCM implementation for x86_64 CPUs that support the following CPU
4
// features: VAES && VPCLMULQDQ && AVX2
5
//
6
// Copyright 2025 Google LLC
7
//
8
// Author: Eric Biggers <ebiggers@google.com>
9
//
10
//------------------------------------------------------------------------------
11
//
12
// This file is dual-licensed, meaning that you can use it under your choice of
13
// either of the following two licenses:
14
//
15
// Licensed under the Apache License 2.0 (the "License"). You may obtain a copy
16
// of the License at
17
//
18
// http://www.apache.org/licenses/LICENSE-2.0
19
//
20
// Unless required by applicable law or agreed to in writing, software
21
// distributed under the License is distributed on an "AS IS" BASIS,
22
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
// See the License for the specific language governing permissions and
24
// limitations under the License.
25
//
26
// or
27
//
28
// Redistribution and use in source and binary forms, with or without
29
// modification, are permitted provided that the following conditions are met:
30
//
31
// 1. Redistributions of source code must retain the above copyright notice,
32
// this list of conditions and the following disclaimer.
33
//
34
// 2. Redistributions in binary form must reproduce the above copyright
35
// notice, this list of conditions and the following disclaimer in the
36
// documentation and/or other materials provided with the distribution.
37
//
38
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
42
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48
// POSSIBILITY OF SUCH DAMAGE.
49
//
50
// -----------------------------------------------------------------------------
51
//
52
// This is similar to aes-gcm-vaes-avx512.S, but it uses AVX2 instead of AVX512.
53
// This means it can only use 16 vector registers instead of 32, the maximum
54
// vector length is 32 bytes, and some instructions such as vpternlogd and
55
// masked loads/stores are unavailable. However, it is able to run on CPUs that
56
// have VAES without AVX512, namely AMD Zen 3 (including "Milan" server CPUs),
57
// various Intel client CPUs such as Alder Lake, and Intel Sierra Forest.
58
//
59
// This implementation also uses Karatsuba multiplication instead of schoolbook
60
// multiplication for GHASH in its main loop. This does not help much on Intel,
61
// but it improves performance by ~5% on AMD Zen 3. Other factors weighing
62
// slightly in favor of Karatsuba multiplication in this implementation are the
63
// lower maximum vector length (which means there are fewer key powers, so we
64
// can cache the halves of each key power XOR'd together and still use less
65
// memory than the AVX512 implementation), and the unavailability of the
66
// vpternlogd instruction (which helped schoolbook a bit more than Karatsuba).
67
68
#include <linux/linkage.h>
69
70
.section .rodata
71
.p2align 4
72
73
// The below three 16-byte values must be in the order that they are, as
74
// they are really two 32-byte tables and a 16-byte value that overlap:
75
//
76
// - The first 32-byte table begins at .Lselect_high_bytes_table.
77
// For 0 <= len <= 16, the 16-byte value at
78
// '.Lselect_high_bytes_table + len' selects the high 'len' bytes of
79
// another 16-byte value when AND'ed with it.
80
//
81
// - The second 32-byte table begins at .Lrshift_and_bswap_table.
82
// For 0 <= len <= 16, the 16-byte value at
83
// '.Lrshift_and_bswap_table + len' is a vpshufb mask that does the
84
// following operation: right-shift by '16 - len' bytes (shifting in
85
// zeroes), then reflect all 16 bytes.
86
//
87
// - The 16-byte value at .Lbswap_mask is a vpshufb mask that reflects
88
// all 16 bytes.
89
.Lselect_high_bytes_table:
90
.octa 0
91
.Lrshift_and_bswap_table:
92
.octa 0xffffffffffffffffffffffffffffffff
93
.Lbswap_mask:
94
.octa 0x000102030405060708090a0b0c0d0e0f
95
96
// Sixteen 0x0f bytes. By XOR'ing an entry of .Lrshift_and_bswap_table
97
// with this, we get a mask that left-shifts by '16 - len' bytes.
98
.Lfifteens:
99
.octa 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f
100
101
// This is the GHASH reducing polynomial without its constant term, i.e.
102
// x^128 + x^7 + x^2 + x, represented using the backwards mapping
103
// between bits and polynomial coefficients.
104
//
105
// Alternatively, it can be interpreted as the naturally-ordered
106
// representation of the polynomial x^127 + x^126 + x^121 + 1, i.e. the
107
// "reversed" GHASH reducing polynomial without its x^128 term.
108
.Lgfpoly:
109
.octa 0xc2000000000000000000000000000001
110
111
// Same as above, but with the (1 << 64) bit set.
112
.Lgfpoly_and_internal_carrybit:
113
.octa 0xc2000000000000010000000000000001
114
115
// Values needed to prepare the initial vector of counter blocks.
116
.Lctr_pattern:
117
.octa 0
118
.octa 1
119
120
// The number of AES blocks per vector, as a 128-bit value.
121
.Linc_2blocks:
122
.octa 2
123
124
// Offsets in struct aes_gcm_key_vaes_avx2
125
#define OFFSETOF_AESKEYLEN 0
126
#define OFFSETOF_AESROUNDKEYS 16
127
#define OFFSETOF_H_POWERS 288
128
#define NUM_H_POWERS 8
129
#define OFFSETOFEND_H_POWERS (OFFSETOF_H_POWERS + (NUM_H_POWERS * 16))
130
#define OFFSETOF_H_POWERS_XORED OFFSETOFEND_H_POWERS
131
132
.text
133
134
// Do one step of GHASH-multiplying the 128-bit lanes of \a by the 128-bit lanes
135
// of \b and storing the reduced products in \dst. Uses schoolbook
136
// multiplication.
137
.macro _ghash_mul_step i, a, b, dst, gfpoly, t0, t1, t2
138
.if \i == 0
139
vpclmulqdq $0x00, \a, \b, \t0 // LO = a_L * b_L
140
vpclmulqdq $0x01, \a, \b, \t1 // MI_0 = a_L * b_H
141
.elseif \i == 1
142
vpclmulqdq $0x10, \a, \b, \t2 // MI_1 = a_H * b_L
143
.elseif \i == 2
144
vpxor \t2, \t1, \t1 // MI = MI_0 + MI_1
145
.elseif \i == 3
146
vpclmulqdq $0x01, \t0, \gfpoly, \t2 // LO_L*(x^63 + x^62 + x^57)
147
.elseif \i == 4
148
vpshufd $0x4e, \t0, \t0 // Swap halves of LO
149
.elseif \i == 5
150
vpxor \t0, \t1, \t1 // Fold LO into MI (part 1)
151
vpxor \t2, \t1, \t1 // Fold LO into MI (part 2)
152
.elseif \i == 6
153
vpclmulqdq $0x11, \a, \b, \dst // HI = a_H * b_H
154
.elseif \i == 7
155
vpclmulqdq $0x01, \t1, \gfpoly, \t0 // MI_L*(x^63 + x^62 + x^57)
156
.elseif \i == 8
157
vpshufd $0x4e, \t1, \t1 // Swap halves of MI
158
.elseif \i == 9
159
vpxor \t1, \dst, \dst // Fold MI into HI (part 1)
160
vpxor \t0, \dst, \dst // Fold MI into HI (part 2)
161
.endif
162
.endm
163
164
// GHASH-multiply the 128-bit lanes of \a by the 128-bit lanes of \b and store
165
// the reduced products in \dst. See _ghash_mul_step for full explanation.
166
.macro _ghash_mul a, b, dst, gfpoly, t0, t1, t2
167
.irp i, 0,1,2,3,4,5,6,7,8,9
168
_ghash_mul_step \i, \a, \b, \dst, \gfpoly, \t0, \t1, \t2
169
.endr
170
.endm
171
172
// GHASH-multiply the 128-bit lanes of \a by the 128-bit lanes of \b and add the
173
// *unreduced* products to \lo, \mi, and \hi.
174
.macro _ghash_mul_noreduce a, b, lo, mi, hi, t0
175
vpclmulqdq $0x00, \a, \b, \t0 // a_L * b_L
176
vpxor \t0, \lo, \lo
177
vpclmulqdq $0x01, \a, \b, \t0 // a_L * b_H
178
vpxor \t0, \mi, \mi
179
vpclmulqdq $0x10, \a, \b, \t0 // a_H * b_L
180
vpxor \t0, \mi, \mi
181
vpclmulqdq $0x11, \a, \b, \t0 // a_H * b_H
182
vpxor \t0, \hi, \hi
183
.endm
184
185
// Reduce the unreduced products from \lo, \mi, and \hi and store the 128-bit
186
// reduced products in \hi. See _ghash_mul_step for explanation of reduction.
187
.macro _ghash_reduce lo, mi, hi, gfpoly, t0
188
vpclmulqdq $0x01, \lo, \gfpoly, \t0
189
vpshufd $0x4e, \lo, \lo
190
vpxor \lo, \mi, \mi
191
vpxor \t0, \mi, \mi
192
vpclmulqdq $0x01, \mi, \gfpoly, \t0
193
vpshufd $0x4e, \mi, \mi
194
vpxor \mi, \hi, \hi
195
vpxor \t0, \hi, \hi
196
.endm
197
198
// This is a specialized version of _ghash_mul that computes \a * \a, i.e. it
199
// squares \a. It skips computing MI = (a_L * a_H) + (a_H * a_L) = 0.
200
.macro _ghash_square a, dst, gfpoly, t0, t1
201
vpclmulqdq $0x00, \a, \a, \t0 // LO = a_L * a_L
202
vpclmulqdq $0x11, \a, \a, \dst // HI = a_H * a_H
203
vpclmulqdq $0x01, \t0, \gfpoly, \t1 // LO_L*(x^63 + x^62 + x^57)
204
vpshufd $0x4e, \t0, \t0 // Swap halves of LO
205
vpxor \t0, \t1, \t1 // Fold LO into MI
206
vpclmulqdq $0x01, \t1, \gfpoly, \t0 // MI_L*(x^63 + x^62 + x^57)
207
vpshufd $0x4e, \t1, \t1 // Swap halves of MI
208
vpxor \t1, \dst, \dst // Fold MI into HI (part 1)
209
vpxor \t0, \dst, \dst // Fold MI into HI (part 2)
210
.endm
211
212
// void aes_gcm_precompute_vaes_avx2(struct aes_gcm_key_vaes_avx2 *key);
213
//
214
// Given the expanded AES key |key->base.aes_key|, derive the GHASH subkey and
215
// initialize |key->h_powers| and |key->h_powers_xored|.
216
//
217
// We use h_powers[0..7] to store H^8 through H^1, and h_powers_xored[0..7] to
218
// store the 64-bit halves of the key powers XOR'd together (for Karatsuba
219
// multiplication) in the order 8,6,7,5,4,2,3,1.
220
SYM_FUNC_START(aes_gcm_precompute_vaes_avx2)
221
222
// Function arguments
223
.set KEY, %rdi
224
225
// Additional local variables
226
.set POWERS_PTR, %rsi
227
.set RNDKEYLAST_PTR, %rdx
228
.set TMP0, %ymm0
229
.set TMP0_XMM, %xmm0
230
.set TMP1, %ymm1
231
.set TMP1_XMM, %xmm1
232
.set TMP2, %ymm2
233
.set TMP2_XMM, %xmm2
234
.set H_CUR, %ymm3
235
.set H_CUR_XMM, %xmm3
236
.set H_CUR2, %ymm4
237
.set H_INC, %ymm5
238
.set H_INC_XMM, %xmm5
239
.set GFPOLY, %ymm6
240
.set GFPOLY_XMM, %xmm6
241
242
// Encrypt an all-zeroes block to get the raw hash subkey.
243
movl OFFSETOF_AESKEYLEN(KEY), %eax
244
lea OFFSETOF_AESROUNDKEYS+6*16(KEY,%rax,4), RNDKEYLAST_PTR
245
vmovdqu OFFSETOF_AESROUNDKEYS(KEY), H_CUR_XMM
246
lea OFFSETOF_AESROUNDKEYS+16(KEY), %rax
247
1:
248
vaesenc (%rax), H_CUR_XMM, H_CUR_XMM
249
add $16, %rax
250
cmp %rax, RNDKEYLAST_PTR
251
jne 1b
252
vaesenclast (RNDKEYLAST_PTR), H_CUR_XMM, H_CUR_XMM
253
254
// Reflect the bytes of the raw hash subkey.
255
vpshufb .Lbswap_mask(%rip), H_CUR_XMM, H_CUR_XMM
256
257
// Finish preprocessing the byte-reflected hash subkey by multiplying it
258
// by x^-1 ("standard" interpretation of polynomial coefficients) or
259
// equivalently x^1 (natural interpretation). This gets the key into a
260
// format that avoids having to bit-reflect the data blocks later.
261
vpshufd $0xd3, H_CUR_XMM, TMP0_XMM
262
vpsrad $31, TMP0_XMM, TMP0_XMM
263
vpaddq H_CUR_XMM, H_CUR_XMM, H_CUR_XMM
264
vpand .Lgfpoly_and_internal_carrybit(%rip), TMP0_XMM, TMP0_XMM
265
vpxor TMP0_XMM, H_CUR_XMM, H_CUR_XMM
266
267
// Load the gfpoly constant.
268
vbroadcasti128 .Lgfpoly(%rip), GFPOLY
269
270
// Square H^1 to get H^2.
271
_ghash_square H_CUR_XMM, H_INC_XMM, GFPOLY_XMM, TMP0_XMM, TMP1_XMM
272
273
// Create H_CUR = [H^2, H^1] and H_INC = [H^2, H^2].
274
vinserti128 $1, H_CUR_XMM, H_INC, H_CUR
275
vinserti128 $1, H_INC_XMM, H_INC, H_INC
276
277
// Compute H_CUR2 = [H^4, H^3].
278
_ghash_mul H_INC, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1, TMP2
279
280
// Store [H^2, H^1] and [H^4, H^3].
281
vmovdqu H_CUR, OFFSETOF_H_POWERS+3*32(KEY)
282
vmovdqu H_CUR2, OFFSETOF_H_POWERS+2*32(KEY)
283
284
// For Karatsuba multiplication: compute and store the two 64-bit halves
285
// of each key power XOR'd together. Order is 4,2,3,1.
286
vpunpcklqdq H_CUR, H_CUR2, TMP0
287
vpunpckhqdq H_CUR, H_CUR2, TMP1
288
vpxor TMP1, TMP0, TMP0
289
vmovdqu TMP0, OFFSETOF_H_POWERS_XORED+32(KEY)
290
291
// Compute and store H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7].
292
_ghash_mul H_INC, H_CUR2, H_CUR, GFPOLY, TMP0, TMP1, TMP2
293
_ghash_mul H_INC, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1, TMP2
294
vmovdqu H_CUR, OFFSETOF_H_POWERS+1*32(KEY)
295
vmovdqu H_CUR2, OFFSETOF_H_POWERS+0*32(KEY)
296
297
// Again, compute and store the two 64-bit halves of each key power
298
// XOR'd together. Order is 8,6,7,5.
299
vpunpcklqdq H_CUR, H_CUR2, TMP0
300
vpunpckhqdq H_CUR, H_CUR2, TMP1
301
vpxor TMP1, TMP0, TMP0
302
vmovdqu TMP0, OFFSETOF_H_POWERS_XORED(KEY)
303
304
vzeroupper
305
RET
306
SYM_FUNC_END(aes_gcm_precompute_vaes_avx2)
307
308
// Do one step of the GHASH update of four vectors of data blocks.
309
// \i: the step to do, 0 through 9
310
// \ghashdata_ptr: pointer to the data blocks (ciphertext or AAD)
311
// KEY: pointer to struct aes_gcm_key_vaes_avx2
312
// BSWAP_MASK: mask for reflecting the bytes of blocks
313
// H_POW[2-1]_XORED: cached values from KEY->h_powers_xored
314
// TMP[0-2]: temporary registers. TMP[1-2] must be preserved across steps.
315
// LO, MI: working state for this macro that must be preserved across steps
316
// GHASH_ACC: the GHASH accumulator (input/output)
317
.macro _ghash_step_4x i, ghashdata_ptr
318
.set HI, GHASH_ACC # alias
319
.set HI_XMM, GHASH_ACC_XMM
320
.if \i == 0
321
// First vector
322
vmovdqu 0*32(\ghashdata_ptr), TMP1
323
vpshufb BSWAP_MASK, TMP1, TMP1
324
vmovdqu OFFSETOF_H_POWERS+0*32(KEY), TMP2
325
vpxor GHASH_ACC, TMP1, TMP1
326
vpclmulqdq $0x00, TMP2, TMP1, LO
327
vpclmulqdq $0x11, TMP2, TMP1, HI
328
vpunpckhqdq TMP1, TMP1, TMP0
329
vpxor TMP1, TMP0, TMP0
330
vpclmulqdq $0x00, H_POW2_XORED, TMP0, MI
331
.elseif \i == 1
332
.elseif \i == 2
333
// Second vector
334
vmovdqu 1*32(\ghashdata_ptr), TMP1
335
vpshufb BSWAP_MASK, TMP1, TMP1
336
vmovdqu OFFSETOF_H_POWERS+1*32(KEY), TMP2
337
vpclmulqdq $0x00, TMP2, TMP1, TMP0
338
vpxor TMP0, LO, LO
339
vpclmulqdq $0x11, TMP2, TMP1, TMP0
340
vpxor TMP0, HI, HI
341
vpunpckhqdq TMP1, TMP1, TMP0
342
vpxor TMP1, TMP0, TMP0
343
vpclmulqdq $0x10, H_POW2_XORED, TMP0, TMP0
344
vpxor TMP0, MI, MI
345
.elseif \i == 3
346
// Third vector
347
vmovdqu 2*32(\ghashdata_ptr), TMP1
348
vpshufb BSWAP_MASK, TMP1, TMP1
349
vmovdqu OFFSETOF_H_POWERS+2*32(KEY), TMP2
350
.elseif \i == 4
351
vpclmulqdq $0x00, TMP2, TMP1, TMP0
352
vpxor TMP0, LO, LO
353
vpclmulqdq $0x11, TMP2, TMP1, TMP0
354
vpxor TMP0, HI, HI
355
.elseif \i == 5
356
vpunpckhqdq TMP1, TMP1, TMP0
357
vpxor TMP1, TMP0, TMP0
358
vpclmulqdq $0x00, H_POW1_XORED, TMP0, TMP0
359
vpxor TMP0, MI, MI
360
361
// Fourth vector
362
vmovdqu 3*32(\ghashdata_ptr), TMP1
363
vpshufb BSWAP_MASK, TMP1, TMP1
364
.elseif \i == 6
365
vmovdqu OFFSETOF_H_POWERS+3*32(KEY), TMP2
366
vpclmulqdq $0x00, TMP2, TMP1, TMP0
367
vpxor TMP0, LO, LO
368
vpclmulqdq $0x11, TMP2, TMP1, TMP0
369
vpxor TMP0, HI, HI
370
vpunpckhqdq TMP1, TMP1, TMP0
371
vpxor TMP1, TMP0, TMP0
372
vpclmulqdq $0x10, H_POW1_XORED, TMP0, TMP0
373
vpxor TMP0, MI, MI
374
.elseif \i == 7
375
// Finalize 'mi' following Karatsuba multiplication.
376
vpxor LO, MI, MI
377
vpxor HI, MI, MI
378
379
// Fold lo into mi.
380
vbroadcasti128 .Lgfpoly(%rip), TMP2
381
vpclmulqdq $0x01, LO, TMP2, TMP0
382
vpshufd $0x4e, LO, LO
383
vpxor LO, MI, MI
384
vpxor TMP0, MI, MI
385
.elseif \i == 8
386
// Fold mi into hi.
387
vpclmulqdq $0x01, MI, TMP2, TMP0
388
vpshufd $0x4e, MI, MI
389
vpxor MI, HI, HI
390
vpxor TMP0, HI, HI
391
.elseif \i == 9
392
vextracti128 $1, HI, TMP0_XMM
393
vpxor TMP0_XMM, HI_XMM, GHASH_ACC_XMM
394
.endif
395
.endm
396
397
// Update GHASH with four vectors of data blocks. See _ghash_step_4x for full
398
// explanation.
399
.macro _ghash_4x ghashdata_ptr
400
.irp i, 0,1,2,3,4,5,6,7,8,9
401
_ghash_step_4x \i, \ghashdata_ptr
402
.endr
403
.endm
404
405
// Load 1 <= %ecx <= 16 bytes from the pointer \src into the xmm register \dst
406
// and zeroize any remaining bytes. Clobbers %rax, %rcx, and \tmp{64,32}.
407
.macro _load_partial_block src, dst, tmp64, tmp32
408
sub $8, %ecx // LEN - 8
409
jle .Lle8\@
410
411
// Load 9 <= LEN <= 16 bytes.
412
vmovq (\src), \dst // Load first 8 bytes
413
mov (\src, %rcx), %rax // Load last 8 bytes
414
neg %ecx
415
shl $3, %ecx
416
shr %cl, %rax // Discard overlapping bytes
417
vpinsrq $1, %rax, \dst, \dst
418
jmp .Ldone\@
419
420
.Lle8\@:
421
add $4, %ecx // LEN - 4
422
jl .Llt4\@
423
424
// Load 4 <= LEN <= 8 bytes.
425
mov (\src), %eax // Load first 4 bytes
426
mov (\src, %rcx), \tmp32 // Load last 4 bytes
427
jmp .Lcombine\@
428
429
.Llt4\@:
430
// Load 1 <= LEN <= 3 bytes.
431
add $2, %ecx // LEN - 2
432
movzbl (\src), %eax // Load first byte
433
jl .Lmovq\@
434
movzwl (\src, %rcx), \tmp32 // Load last 2 bytes
435
.Lcombine\@:
436
shl $3, %ecx
437
shl %cl, \tmp64
438
or \tmp64, %rax // Combine the two parts
439
.Lmovq\@:
440
vmovq %rax, \dst
441
.Ldone\@:
442
.endm
443
444
// Store 1 <= %ecx <= 16 bytes from the xmm register \src to the pointer \dst.
445
// Clobbers %rax, %rcx, and \tmp{64,32}.
446
.macro _store_partial_block src, dst, tmp64, tmp32
447
sub $8, %ecx // LEN - 8
448
jl .Llt8\@
449
450
// Store 8 <= LEN <= 16 bytes.
451
vpextrq $1, \src, %rax
452
mov %ecx, \tmp32
453
shl $3, %ecx
454
ror %cl, %rax
455
mov %rax, (\dst, \tmp64) // Store last LEN - 8 bytes
456
vmovq \src, (\dst) // Store first 8 bytes
457
jmp .Ldone\@
458
459
.Llt8\@:
460
add $4, %ecx // LEN - 4
461
jl .Llt4\@
462
463
// Store 4 <= LEN <= 7 bytes.
464
vpextrd $1, \src, %eax
465
mov %ecx, \tmp32
466
shl $3, %ecx
467
ror %cl, %eax
468
mov %eax, (\dst, \tmp64) // Store last LEN - 4 bytes
469
vmovd \src, (\dst) // Store first 4 bytes
470
jmp .Ldone\@
471
472
.Llt4\@:
473
// Store 1 <= LEN <= 3 bytes.
474
vpextrb $0, \src, 0(\dst)
475
cmp $-2, %ecx // LEN - 4 == -2, i.e. LEN == 2?
476
jl .Ldone\@
477
vpextrb $1, \src, 1(\dst)
478
je .Ldone\@
479
vpextrb $2, \src, 2(\dst)
480
.Ldone\@:
481
.endm
482
483
// void aes_gcm_aad_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
484
// u8 ghash_acc[16],
485
// const u8 *aad, int aadlen);
486
//
487
// This function processes the AAD (Additional Authenticated Data) in GCM.
488
// Using the key |key|, it updates the GHASH accumulator |ghash_acc| with the
489
// data given by |aad| and |aadlen|. On the first call, |ghash_acc| must be all
490
// zeroes. |aadlen| must be a multiple of 16, except on the last call where it
491
// can be any length. The caller must do any buffering needed to ensure this.
492
//
493
// This handles large amounts of AAD efficiently, while also keeping overhead
494
// low for small amounts which is the common case. TLS and IPsec use less than
495
// one block of AAD, but (uncommonly) other use cases may use much more.
496
SYM_FUNC_START(aes_gcm_aad_update_vaes_avx2)
497
498
// Function arguments
499
.set KEY, %rdi
500
.set GHASH_ACC_PTR, %rsi
501
.set AAD, %rdx
502
.set AADLEN, %ecx // Must be %ecx for _load_partial_block
503
.set AADLEN64, %rcx // Zero-extend AADLEN before using!
504
505
// Additional local variables.
506
// %rax and %r8 are used as temporary registers.
507
.set TMP0, %ymm0
508
.set TMP0_XMM, %xmm0
509
.set TMP1, %ymm1
510
.set TMP1_XMM, %xmm1
511
.set TMP2, %ymm2
512
.set TMP2_XMM, %xmm2
513
.set LO, %ymm3
514
.set LO_XMM, %xmm3
515
.set MI, %ymm4
516
.set MI_XMM, %xmm4
517
.set GHASH_ACC, %ymm5
518
.set GHASH_ACC_XMM, %xmm5
519
.set BSWAP_MASK, %ymm6
520
.set BSWAP_MASK_XMM, %xmm6
521
.set GFPOLY, %ymm7
522
.set GFPOLY_XMM, %xmm7
523
.set H_POW2_XORED, %ymm8
524
.set H_POW1_XORED, %ymm9
525
526
// Load the bswap_mask and gfpoly constants. Since AADLEN is usually
527
// small, usually only 128-bit vectors will be used. So as an
528
// optimization, don't broadcast these constants to both 128-bit lanes
529
// quite yet.
530
vmovdqu .Lbswap_mask(%rip), BSWAP_MASK_XMM
531
vmovdqu .Lgfpoly(%rip), GFPOLY_XMM
532
533
// Load the GHASH accumulator.
534
vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
535
536
// Check for the common case of AADLEN <= 16, as well as AADLEN == 0.
537
test AADLEN, AADLEN
538
jz .Laad_done
539
cmp $16, AADLEN
540
jle .Laad_lastblock
541
542
// AADLEN > 16, so we'll operate on full vectors. Broadcast bswap_mask
543
// and gfpoly to both 128-bit lanes.
544
vinserti128 $1, BSWAP_MASK_XMM, BSWAP_MASK, BSWAP_MASK
545
vinserti128 $1, GFPOLY_XMM, GFPOLY, GFPOLY
546
547
// If AADLEN >= 128, update GHASH with 128 bytes of AAD at a time.
548
add $-128, AADLEN // 128 is 4 bytes, -128 is 1 byte
549
jl .Laad_loop_4x_done
550
vmovdqu OFFSETOF_H_POWERS_XORED(KEY), H_POW2_XORED
551
vmovdqu OFFSETOF_H_POWERS_XORED+32(KEY), H_POW1_XORED
552
.Laad_loop_4x:
553
_ghash_4x AAD
554
sub $-128, AAD
555
add $-128, AADLEN
556
jge .Laad_loop_4x
557
.Laad_loop_4x_done:
558
559
// If AADLEN >= 32, update GHASH with 32 bytes of AAD at a time.
560
add $96, AADLEN
561
jl .Laad_loop_1x_done
562
.Laad_loop_1x:
563
vmovdqu (AAD), TMP0
564
vpshufb BSWAP_MASK, TMP0, TMP0
565
vpxor TMP0, GHASH_ACC, GHASH_ACC
566
vmovdqu OFFSETOFEND_H_POWERS-32(KEY), TMP0
567
_ghash_mul TMP0, GHASH_ACC, GHASH_ACC, GFPOLY, TMP1, TMP2, LO
568
vextracti128 $1, GHASH_ACC, TMP0_XMM
569
vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
570
add $32, AAD
571
sub $32, AADLEN
572
jge .Laad_loop_1x
573
.Laad_loop_1x_done:
574
add $32, AADLEN
575
// Now 0 <= AADLEN < 32.
576
577
jz .Laad_done
578
cmp $16, AADLEN
579
jle .Laad_lastblock
580
581
// Update GHASH with the remaining 17 <= AADLEN <= 31 bytes of AAD.
582
mov AADLEN, AADLEN // Zero-extend AADLEN to AADLEN64.
583
vmovdqu (AAD), TMP0_XMM
584
vmovdqu -16(AAD, AADLEN64), TMP1_XMM
585
vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
586
vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
587
lea .Lrshift_and_bswap_table(%rip), %rax
588
vpshufb -16(%rax, AADLEN64), TMP1_XMM, TMP1_XMM
589
vinserti128 $1, TMP1_XMM, GHASH_ACC, GHASH_ACC
590
vmovdqu OFFSETOFEND_H_POWERS-32(KEY), TMP0
591
_ghash_mul TMP0, GHASH_ACC, GHASH_ACC, GFPOLY, TMP1, TMP2, LO
592
vextracti128 $1, GHASH_ACC, TMP0_XMM
593
vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
594
jmp .Laad_done
595
596
.Laad_lastblock:
597
// Update GHASH with the remaining 1 <= AADLEN <= 16 bytes of AAD.
598
_load_partial_block AAD, TMP0_XMM, %r8, %r8d
599
vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
600
vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
601
vmovdqu OFFSETOFEND_H_POWERS-16(KEY), TMP0_XMM
602
_ghash_mul TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM, GFPOLY_XMM, \
603
TMP1_XMM, TMP2_XMM, LO_XMM
604
605
.Laad_done:
606
// Store the updated GHASH accumulator back to memory.
607
vmovdqu GHASH_ACC_XMM, (GHASH_ACC_PTR)
608
609
vzeroupper
610
RET
611
SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
612
613
// Do one non-last round of AES encryption on the blocks in the given AESDATA
614
// vectors using the round key that has been broadcast to all 128-bit lanes of
615
// \round_key.
616
.macro _vaesenc round_key, vecs:vararg
617
.irp i, \vecs
618
vaesenc \round_key, AESDATA\i, AESDATA\i
619
.endr
620
.endm
621
622
// Generate counter blocks in the given AESDATA vectors, then do the zero-th AES
623
// round on them. Clobbers TMP0.
624
.macro _ctr_begin vecs:vararg
625
vbroadcasti128 .Linc_2blocks(%rip), TMP0
626
.irp i, \vecs
627
vpshufb BSWAP_MASK, LE_CTR, AESDATA\i
628
vpaddd TMP0, LE_CTR, LE_CTR
629
.endr
630
.irp i, \vecs
631
vpxor RNDKEY0, AESDATA\i, AESDATA\i
632
.endr
633
.endm
634
635
// Generate and encrypt counter blocks in the given AESDATA vectors, excluding
636
// the last AES round. Clobbers %rax and TMP0.
637
.macro _aesenc_loop vecs:vararg
638
_ctr_begin \vecs
639
lea OFFSETOF_AESROUNDKEYS+16(KEY), %rax
640
.Laesenc_loop\@:
641
vbroadcasti128 (%rax), TMP0
642
_vaesenc TMP0, \vecs
643
add $16, %rax
644
cmp %rax, RNDKEYLAST_PTR
645
jne .Laesenc_loop\@
646
.endm
647
648
// Finalize the keystream blocks in the given AESDATA vectors by doing the last
649
// AES round, then XOR those keystream blocks with the corresponding data.
650
// Reduce latency by doing the XOR before the vaesenclast, utilizing the
651
// property vaesenclast(key, a) ^ b == vaesenclast(key ^ b, a). Clobbers TMP0.
652
.macro _aesenclast_and_xor vecs:vararg
653
.irp i, \vecs
654
vpxor \i*32(SRC), RNDKEYLAST, TMP0
655
vaesenclast TMP0, AESDATA\i, AESDATA\i
656
.endr
657
.irp i, \vecs
658
vmovdqu AESDATA\i, \i*32(DST)
659
.endr
660
.endm
661
662
// void aes_gcm_{enc,dec}_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
663
// const u32 le_ctr[4], u8 ghash_acc[16],
664
// const u8 *src, u8 *dst, int datalen);
665
//
666
// This macro generates a GCM encryption or decryption update function with the
667
// above prototype (with \enc selecting which one). The function computes the
668
// next portion of the CTR keystream, XOR's it with |datalen| bytes from |src|,
669
// and writes the resulting encrypted or decrypted data to |dst|. It also
670
// updates the GHASH accumulator |ghash_acc| using the next |datalen| ciphertext
671
// bytes.
672
//
673
// |datalen| must be a multiple of 16, except on the last call where it can be
674
// any length. The caller must do any buffering needed to ensure this. Both
675
// in-place and out-of-place en/decryption are supported.
676
//
677
// |le_ctr| must give the current counter in little-endian format. This
678
// function loads the counter from |le_ctr| and increments the loaded counter as
679
// needed, but it does *not* store the updated counter back to |le_ctr|. The
680
// caller must update |le_ctr| if any more data segments follow. Internally,
681
// only the low 32-bit word of the counter is incremented, following the GCM
682
// standard.
683
.macro _aes_gcm_update enc
684
685
// Function arguments
686
.set KEY, %rdi
687
.set LE_CTR_PTR, %rsi
688
.set LE_CTR_PTR32, %esi
689
.set GHASH_ACC_PTR, %rdx
690
.set SRC, %rcx // Assumed to be %rcx.
691
// See .Ltail_xor_and_ghash_1to16bytes
692
.set DST, %r8
693
.set DATALEN, %r9d
694
.set DATALEN64, %r9 // Zero-extend DATALEN before using!
695
696
// Additional local variables
697
698
// %rax is used as a temporary register. LE_CTR_PTR is also available
699
// as a temporary register after the counter is loaded.
700
701
// AES key length in bytes
702
.set AESKEYLEN, %r10d
703
.set AESKEYLEN64, %r10
704
705
// Pointer to the last AES round key for the chosen AES variant
706
.set RNDKEYLAST_PTR, %r11
707
708
// BSWAP_MASK is the shuffle mask for byte-reflecting 128-bit values
709
// using vpshufb, copied to all 128-bit lanes.
710
.set BSWAP_MASK, %ymm0
711
.set BSWAP_MASK_XMM, %xmm0
712
713
// GHASH_ACC is the accumulator variable for GHASH. When fully reduced,
714
// only the lowest 128-bit lane can be nonzero. When not fully reduced,
715
// more than one lane may be used, and they need to be XOR'd together.
716
.set GHASH_ACC, %ymm1
717
.set GHASH_ACC_XMM, %xmm1
718
719
// TMP[0-2] are temporary registers.
720
.set TMP0, %ymm2
721
.set TMP0_XMM, %xmm2
722
.set TMP1, %ymm3
723
.set TMP1_XMM, %xmm3
724
.set TMP2, %ymm4
725
.set TMP2_XMM, %xmm4
726
727
// LO and MI are used to accumulate unreduced GHASH products.
728
.set LO, %ymm5
729
.set LO_XMM, %xmm5
730
.set MI, %ymm6
731
.set MI_XMM, %xmm6
732
733
// H_POW[2-1]_XORED contain cached values from KEY->h_powers_xored. The
734
// descending numbering reflects the order of the key powers.
735
.set H_POW2_XORED, %ymm7
736
.set H_POW2_XORED_XMM, %xmm7
737
.set H_POW1_XORED, %ymm8
738
739
// RNDKEY0 caches the zero-th round key, and RNDKEYLAST the last one.
740
.set RNDKEY0, %ymm9
741
.set RNDKEYLAST, %ymm10
742
743
// LE_CTR contains the next set of little-endian counter blocks.
744
.set LE_CTR, %ymm11
745
746
// AESDATA[0-3] hold the counter blocks that are being encrypted by AES.
747
.set AESDATA0, %ymm12
748
.set AESDATA0_XMM, %xmm12
749
.set AESDATA1, %ymm13
750
.set AESDATA1_XMM, %xmm13
751
.set AESDATA2, %ymm14
752
.set AESDATA3, %ymm15
753
754
.if \enc
755
.set GHASHDATA_PTR, DST
756
.else
757
.set GHASHDATA_PTR, SRC
758
.endif
759
760
vbroadcasti128 .Lbswap_mask(%rip), BSWAP_MASK
761
762
// Load the GHASH accumulator and the starting counter.
763
vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
764
vbroadcasti128 (LE_CTR_PTR), LE_CTR
765
766
// Load the AES key length in bytes.
767
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
768
769
// Make RNDKEYLAST_PTR point to the last AES round key. This is the
770
// round key with index 10, 12, or 14 for AES-128, AES-192, or AES-256
771
// respectively. Then load the zero-th and last round keys.
772
lea OFFSETOF_AESROUNDKEYS+6*16(KEY,AESKEYLEN64,4), RNDKEYLAST_PTR
773
vbroadcasti128 OFFSETOF_AESROUNDKEYS(KEY), RNDKEY0
774
vbroadcasti128 (RNDKEYLAST_PTR), RNDKEYLAST
775
776
// Finish initializing LE_CTR by adding 1 to the second block.
777
vpaddd .Lctr_pattern(%rip), LE_CTR, LE_CTR
778
779
// If there are at least 128 bytes of data, then continue into the loop
780
// that processes 128 bytes of data at a time. Otherwise skip it.
781
add $-128, DATALEN // 128 is 4 bytes, -128 is 1 byte
782
jl .Lcrypt_loop_4x_done\@
783
784
vmovdqu OFFSETOF_H_POWERS_XORED(KEY), H_POW2_XORED
785
vmovdqu OFFSETOF_H_POWERS_XORED+32(KEY), H_POW1_XORED
786
787
// Main loop: en/decrypt and hash 4 vectors (128 bytes) at a time.
788
789
.if \enc
790
// Encrypt the first 4 vectors of plaintext blocks.
791
_aesenc_loop 0,1,2,3
792
_aesenclast_and_xor 0,1,2,3
793
sub $-128, SRC // 128 is 4 bytes, -128 is 1 byte
794
add $-128, DATALEN
795
jl .Lghash_last_ciphertext_4x\@
796
.endif
797
798
.align 16
799
.Lcrypt_loop_4x\@:
800
801
// Start the AES encryption of the counter blocks.
802
_ctr_begin 0,1,2,3
803
cmp $24, AESKEYLEN
804
jl 128f // AES-128?
805
je 192f // AES-192?
806
// AES-256
807
vbroadcasti128 -13*16(RNDKEYLAST_PTR), TMP0
808
_vaesenc TMP0, 0,1,2,3
809
vbroadcasti128 -12*16(RNDKEYLAST_PTR), TMP0
810
_vaesenc TMP0, 0,1,2,3
811
192:
812
vbroadcasti128 -11*16(RNDKEYLAST_PTR), TMP0
813
_vaesenc TMP0, 0,1,2,3
814
vbroadcasti128 -10*16(RNDKEYLAST_PTR), TMP0
815
_vaesenc TMP0, 0,1,2,3
816
128:
817
818
// Finish the AES encryption of the counter blocks in AESDATA[0-3],
819
// interleaved with the GHASH update of the ciphertext blocks.
820
.irp i, 9,8,7,6,5,4,3,2,1
821
_ghash_step_4x (9 - \i), GHASHDATA_PTR
822
vbroadcasti128 -\i*16(RNDKEYLAST_PTR), TMP0
823
_vaesenc TMP0, 0,1,2,3
824
.endr
825
_ghash_step_4x 9, GHASHDATA_PTR
826
.if \enc
827
sub $-128, DST // 128 is 4 bytes, -128 is 1 byte
828
.endif
829
_aesenclast_and_xor 0,1,2,3
830
sub $-128, SRC
831
.if !\enc
832
sub $-128, DST
833
.endif
834
add $-128, DATALEN
835
jge .Lcrypt_loop_4x\@
836
837
.if \enc
838
.Lghash_last_ciphertext_4x\@:
839
// Update GHASH with the last set of ciphertext blocks.
840
_ghash_4x DST
841
sub $-128, DST
842
.endif
843
844
.Lcrypt_loop_4x_done\@:
845
846
// Undo the extra subtraction by 128 and check whether data remains.
847
sub $-128, DATALEN // 128 is 4 bytes, -128 is 1 byte
848
jz .Ldone\@
849
850
// The data length isn't a multiple of 128 bytes. Process the remaining
851
// data of length 1 <= DATALEN < 128.
852
//
853
// Since there are enough key powers available for all remaining data,
854
// there is no need to do a GHASH reduction after each iteration.
855
// Instead, multiply each remaining block by its own key power, and only
856
// do a GHASH reduction at the very end.
857
858
// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N
859
// is the number of blocks that remain.
860
.set POWERS_PTR, LE_CTR_PTR // LE_CTR_PTR is free to be reused.
861
.set POWERS_PTR32, LE_CTR_PTR32
862
mov DATALEN, %eax
863
neg %rax
864
and $~15, %rax // -round_up(DATALEN, 16)
865
lea OFFSETOFEND_H_POWERS(KEY,%rax), POWERS_PTR
866
867
// Start collecting the unreduced GHASH intermediate value LO, MI, HI.
868
.set HI, H_POW2_XORED // H_POW2_XORED is free to be reused.
869
.set HI_XMM, H_POW2_XORED_XMM
870
vpxor LO_XMM, LO_XMM, LO_XMM
871
vpxor MI_XMM, MI_XMM, MI_XMM
872
vpxor HI_XMM, HI_XMM, HI_XMM
873
874
// 1 <= DATALEN < 128. Generate 2 or 4 more vectors of keystream blocks
875
// excluding the last AES round, depending on the remaining DATALEN.
876
cmp $64, DATALEN
877
jg .Ltail_gen_4_keystream_vecs\@
878
_aesenc_loop 0,1
879
cmp $32, DATALEN
880
jge .Ltail_xor_and_ghash_full_vec_loop\@
881
jmp .Ltail_xor_and_ghash_partial_vec\@
882
.Ltail_gen_4_keystream_vecs\@:
883
_aesenc_loop 0,1,2,3
884
885
// XOR the remaining data and accumulate the unreduced GHASH products
886
// for DATALEN >= 32, starting with one full 32-byte vector at a time.
887
.Ltail_xor_and_ghash_full_vec_loop\@:
888
.if \enc
889
_aesenclast_and_xor 0
890
vpshufb BSWAP_MASK, AESDATA0, AESDATA0
891
.else
892
vmovdqu (SRC), TMP1
893
vpxor TMP1, RNDKEYLAST, TMP0
894
vaesenclast TMP0, AESDATA0, AESDATA0
895
vmovdqu AESDATA0, (DST)
896
vpshufb BSWAP_MASK, TMP1, AESDATA0
897
.endif
898
// The ciphertext blocks (i.e. GHASH input data) are now in AESDATA0.
899
vpxor GHASH_ACC, AESDATA0, AESDATA0
900
vmovdqu (POWERS_PTR), TMP2
901
_ghash_mul_noreduce TMP2, AESDATA0, LO, MI, HI, TMP0
902
vmovdqa AESDATA1, AESDATA0
903
vmovdqa AESDATA2, AESDATA1
904
vmovdqa AESDATA3, AESDATA2
905
vpxor GHASH_ACC_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
906
add $32, SRC
907
add $32, DST
908
add $32, POWERS_PTR
909
sub $32, DATALEN
910
cmp $32, DATALEN
911
jge .Ltail_xor_and_ghash_full_vec_loop\@
912
test DATALEN, DATALEN
913
jz .Ltail_ghash_reduce\@
914
915
.Ltail_xor_and_ghash_partial_vec\@:
916
// XOR the remaining data and accumulate the unreduced GHASH products,
917
// for 1 <= DATALEN < 32.
918
vaesenclast RNDKEYLAST, AESDATA0, AESDATA0
919
cmp $16, DATALEN
920
jle .Ltail_xor_and_ghash_1to16bytes\@
921
922
// Handle 17 <= DATALEN < 32.
923
924
// Load a vpshufb mask that will right-shift by '32 - DATALEN' bytes
925
// (shifting in zeroes), then reflect all 16 bytes.
926
lea .Lrshift_and_bswap_table(%rip), %rax
927
vmovdqu -16(%rax, DATALEN64), TMP2_XMM
928
929
// Move the second keystream block to its own register and left-align it
930
vextracti128 $1, AESDATA0, AESDATA1_XMM
931
vpxor .Lfifteens(%rip), TMP2_XMM, TMP0_XMM
932
vpshufb TMP0_XMM, AESDATA1_XMM, AESDATA1_XMM
933
934
// Using overlapping loads and stores, XOR the source data with the
935
// keystream and write the destination data. Then prepare the GHASH
936
// input data: the full ciphertext block and the zero-padded partial
937
// ciphertext block, both byte-reflected, in AESDATA0.
938
.if \enc
939
vpxor -16(SRC, DATALEN64), AESDATA1_XMM, AESDATA1_XMM
940
vpxor (SRC), AESDATA0_XMM, AESDATA0_XMM
941
vmovdqu AESDATA1_XMM, -16(DST, DATALEN64)
942
vmovdqu AESDATA0_XMM, (DST)
943
vpshufb TMP2_XMM, AESDATA1_XMM, AESDATA1_XMM
944
vpshufb BSWAP_MASK_XMM, AESDATA0_XMM, AESDATA0_XMM
945
.else
946
vmovdqu -16(SRC, DATALEN64), TMP1_XMM
947
vmovdqu (SRC), TMP0_XMM
948
vpxor TMP1_XMM, AESDATA1_XMM, AESDATA1_XMM
949
vpxor TMP0_XMM, AESDATA0_XMM, AESDATA0_XMM
950
vmovdqu AESDATA1_XMM, -16(DST, DATALEN64)
951
vmovdqu AESDATA0_XMM, (DST)
952
vpshufb TMP2_XMM, TMP1_XMM, AESDATA1_XMM
953
vpshufb BSWAP_MASK_XMM, TMP0_XMM, AESDATA0_XMM
954
.endif
955
vpxor GHASH_ACC_XMM, AESDATA0_XMM, AESDATA0_XMM
956
vinserti128 $1, AESDATA1_XMM, AESDATA0, AESDATA0
957
vmovdqu (POWERS_PTR), TMP2
958
jmp .Ltail_ghash_last_vec\@
959
960
.Ltail_xor_and_ghash_1to16bytes\@:
961
// Handle 1 <= DATALEN <= 16. Carefully load and store the
962
// possibly-partial block, which we mustn't access out of bounds.
963
vmovdqu (POWERS_PTR), TMP2_XMM
964
mov SRC, KEY // Free up %rcx, assuming SRC == %rcx
965
mov DATALEN, %ecx
966
_load_partial_block KEY, TMP0_XMM, POWERS_PTR, POWERS_PTR32
967
vpxor TMP0_XMM, AESDATA0_XMM, AESDATA0_XMM
968
mov DATALEN, %ecx
969
_store_partial_block AESDATA0_XMM, DST, POWERS_PTR, POWERS_PTR32
970
.if \enc
971
lea .Lselect_high_bytes_table(%rip), %rax
972
vpshufb BSWAP_MASK_XMM, AESDATA0_XMM, AESDATA0_XMM
973
vpand (%rax, DATALEN64), AESDATA0_XMM, AESDATA0_XMM
974
.else
975
vpshufb BSWAP_MASK_XMM, TMP0_XMM, AESDATA0_XMM
976
.endif
977
vpxor GHASH_ACC_XMM, AESDATA0_XMM, AESDATA0_XMM
978
979
.Ltail_ghash_last_vec\@:
980
// Accumulate the unreduced GHASH products for the last 1-2 blocks. The
981
// GHASH input data is in AESDATA0. If only one block remains, then the
982
// second block in AESDATA0 is zero and does not affect the result.
983
_ghash_mul_noreduce TMP2, AESDATA0, LO, MI, HI, TMP0
984
985
.Ltail_ghash_reduce\@:
986
// Finally, do the GHASH reduction.
987
vbroadcasti128 .Lgfpoly(%rip), TMP0
988
_ghash_reduce LO, MI, HI, TMP0, TMP1
989
vextracti128 $1, HI, GHASH_ACC_XMM
990
vpxor HI_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
991
992
.Ldone\@:
993
// Store the updated GHASH accumulator back to memory.
994
vmovdqu GHASH_ACC_XMM, (GHASH_ACC_PTR)
995
996
vzeroupper
997
RET
998
.endm
999
1000
// void aes_gcm_enc_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
1001
// const u32 le_ctr[4], u8 ghash_acc[16],
1002
// u64 total_aadlen, u64 total_datalen);
1003
// bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
1004
// const u32 le_ctr[4], const u8 ghash_acc[16],
1005
// u64 total_aadlen, u64 total_datalen,
1006
// const u8 tag[16], int taglen);
1007
//
1008
// This macro generates one of the above two functions (with \enc selecting
1009
// which one). Both functions finish computing the GCM authentication tag by
1010
// updating GHASH with the lengths block and encrypting the GHASH accumulator.
1011
// |total_aadlen| and |total_datalen| must be the total length of the additional
1012
// authenticated data and the en/decrypted data in bytes, respectively.
1013
//
1014
// The encryption function then stores the full-length (16-byte) computed
1015
// authentication tag to |ghash_acc|. The decryption function instead loads the
1016
// expected authentication tag (the one that was transmitted) from the 16-byte
1017
// buffer |tag|, compares the first 4 <= |taglen| <= 16 bytes of it to the
1018
// computed tag in constant time, and returns true if and only if they match.
1019
.macro _aes_gcm_final enc
1020
1021
// Function arguments
1022
.set KEY, %rdi
1023
.set LE_CTR_PTR, %rsi
1024
.set GHASH_ACC_PTR, %rdx
1025
.set TOTAL_AADLEN, %rcx
1026
.set TOTAL_DATALEN, %r8
1027
.set TAG, %r9
1028
.set TAGLEN, %r10d // Originally at 8(%rsp)
1029
.set TAGLEN64, %r10
1030
1031
// Additional local variables.
1032
// %rax and %xmm0-%xmm3 are used as temporary registers.
1033
.set AESKEYLEN, %r11d
1034
.set AESKEYLEN64, %r11
1035
.set GFPOLY, %xmm4
1036
.set BSWAP_MASK, %xmm5
1037
.set LE_CTR, %xmm6
1038
.set GHASH_ACC, %xmm7
1039
.set H_POW1, %xmm8
1040
1041
// Load some constants.
1042
vmovdqa .Lgfpoly(%rip), GFPOLY
1043
vmovdqa .Lbswap_mask(%rip), BSWAP_MASK
1044
1045
// Load the AES key length in bytes.
1046
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
1047
1048
// Set up a counter block with 1 in the low 32-bit word. This is the
1049
// counter that produces the ciphertext needed to encrypt the auth tag.
1050
// GFPOLY has 1 in the low word, so grab the 1 from there using a blend.
1051
vpblendd $0xe, (LE_CTR_PTR), GFPOLY, LE_CTR
1052
1053
// Build the lengths block and XOR it with the GHASH accumulator.
1054
// Although the lengths block is defined as the AAD length followed by
1055
// the en/decrypted data length, both in big-endian byte order, a byte
1056
// reflection of the full block is needed because of the way we compute
1057
// GHASH (see _ghash_mul_step). By using little-endian values in the
1058
// opposite order, we avoid having to reflect any bytes here.
1059
vmovq TOTAL_DATALEN, %xmm0
1060
vpinsrq $1, TOTAL_AADLEN, %xmm0, %xmm0
1061
vpsllq $3, %xmm0, %xmm0 // Bytes to bits
1062
vpxor (GHASH_ACC_PTR), %xmm0, GHASH_ACC
1063
1064
// Load the first hash key power (H^1), which is stored last.
1065
vmovdqu OFFSETOFEND_H_POWERS-16(KEY), H_POW1
1066
1067
// Load TAGLEN if decrypting.
1068
.if !\enc
1069
movl 8(%rsp), TAGLEN
1070
.endif
1071
1072
// Make %rax point to the last AES round key for the chosen AES variant.
1073
lea OFFSETOF_AESROUNDKEYS+6*16(KEY,AESKEYLEN64,4), %rax
1074
1075
// Start the AES encryption of the counter block by swapping the counter
1076
// block to big-endian and XOR-ing it with the zero-th AES round key.
1077
vpshufb BSWAP_MASK, LE_CTR, %xmm0
1078
vpxor OFFSETOF_AESROUNDKEYS(KEY), %xmm0, %xmm0
1079
1080
// Complete the AES encryption and multiply GHASH_ACC by H^1.
1081
// Interleave the AES and GHASH instructions to improve performance.
1082
cmp $24, AESKEYLEN
1083
jl 128f // AES-128?
1084
je 192f // AES-192?
1085
// AES-256
1086
vaesenc -13*16(%rax), %xmm0, %xmm0
1087
vaesenc -12*16(%rax), %xmm0, %xmm0
1088
192:
1089
vaesenc -11*16(%rax), %xmm0, %xmm0
1090
vaesenc -10*16(%rax), %xmm0, %xmm0
1091
128:
1092
.irp i, 0,1,2,3,4,5,6,7,8
1093
_ghash_mul_step \i, H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \
1094
%xmm1, %xmm2, %xmm3
1095
vaesenc (\i-9)*16(%rax), %xmm0, %xmm0
1096
.endr
1097
_ghash_mul_step 9, H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \
1098
%xmm1, %xmm2, %xmm3
1099
1100
// Undo the byte reflection of the GHASH accumulator.
1101
vpshufb BSWAP_MASK, GHASH_ACC, GHASH_ACC
1102
1103
// Do the last AES round and XOR the resulting keystream block with the
1104
// GHASH accumulator to produce the full computed authentication tag.
1105
//
1106
// Reduce latency by taking advantage of the property vaesenclast(key,
1107
// a) ^ b == vaesenclast(key ^ b, a). I.e., XOR GHASH_ACC into the last
1108
// round key, instead of XOR'ing the final AES output with GHASH_ACC.
1109
//
1110
// enc_final then returns the computed auth tag, while dec_final
1111
// compares it with the transmitted one and returns a bool. To compare
1112
// the tags, dec_final XORs them together and uses vptest to check
1113
// whether the result is all-zeroes. This should be constant-time.
1114
// dec_final applies the vaesenclast optimization to this additional
1115
// value XOR'd too.
1116
.if \enc
1117
vpxor (%rax), GHASH_ACC, %xmm1
1118
vaesenclast %xmm1, %xmm0, GHASH_ACC
1119
vmovdqu GHASH_ACC, (GHASH_ACC_PTR)
1120
.else
1121
vpxor (TAG), GHASH_ACC, GHASH_ACC
1122
vpxor (%rax), GHASH_ACC, GHASH_ACC
1123
vaesenclast GHASH_ACC, %xmm0, %xmm0
1124
lea .Lselect_high_bytes_table(%rip), %rax
1125
vmovdqu (%rax, TAGLEN64), %xmm1
1126
vpshufb BSWAP_MASK, %xmm1, %xmm1 // select low bytes, not high
1127
xor %eax, %eax
1128
vptest %xmm1, %xmm0
1129
sete %al
1130
.endif
1131
// No need for vzeroupper here, since only used xmm registers were used.
1132
RET
1133
.endm
1134
1135
SYM_FUNC_START(aes_gcm_enc_update_vaes_avx2)
1136
_aes_gcm_update 1
1137
SYM_FUNC_END(aes_gcm_enc_update_vaes_avx2)
1138
SYM_FUNC_START(aes_gcm_dec_update_vaes_avx2)
1139
_aes_gcm_update 0
1140
SYM_FUNC_END(aes_gcm_dec_update_vaes_avx2)
1141
1142
SYM_FUNC_START(aes_gcm_enc_final_vaes_avx2)
1143
_aes_gcm_final 1
1144
SYM_FUNC_END(aes_gcm_enc_final_vaes_avx2)
1145
SYM_FUNC_START(aes_gcm_dec_final_vaes_avx2)
1146
_aes_gcm_final 0
1147
SYM_FUNC_END(aes_gcm_dec_final_vaes_avx2)
1148
1149