]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/x86/crypto/aesni-intel_avx-x86_64.S
crypto: aesni - Merge GCM_ENC_DEC
[linux.git] / arch / x86 / crypto / aesni-intel_avx-x86_64.S
1 ########################################################################
2 # Copyright (c) 2013, Intel Corporation
3 #
4 # This software is available to you under a choice of one of two
5 # licenses.  You may choose to be licensed under the terms of the GNU
6 # General Public License (GPL) Version 2, available from the file
7 # COPYING in the main directory of this source tree, or the
8 # OpenIB.org BSD license below:
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are
12 # met:
13 #
14 # * Redistributions of source code must retain the above copyright
15 #   notice, this list of conditions and the following disclaimer.
16 #
17 # * Redistributions in binary form must reproduce the above copyright
18 #   notice, this list of conditions and the following disclaimer in the
19 #   documentation and/or other materials provided with the
20 #   distribution.
21 #
22 # * Neither the name of the Intel Corporation nor the names of its
23 #   contributors may be used to endorse or promote products derived from
24 #   this software without specific prior written permission.
25 #
26 #
27 # THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION ""AS IS"" AND ANY
28 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR
31 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES# LOSS OF USE, DATA, OR
34 # PROFITS# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ########################################################################
39 ##
40 ## Authors:
41 ##      Erdinc Ozturk <erdinc.ozturk@intel.com>
42 ##      Vinodh Gopal <vinodh.gopal@intel.com>
43 ##      James Guilford <james.guilford@intel.com>
44 ##      Tim Chen <tim.c.chen@linux.intel.com>
45 ##
46 ## References:
47 ##       This code was derived and highly optimized from the code described in paper:
48 ##               Vinodh Gopal et. al. Optimized Galois-Counter-Mode Implementation
49 ##                      on Intel Architecture Processors. August, 2010
50 ##       The details of the implementation is explained in:
51 ##               Erdinc Ozturk et. al. Enabling High-Performance Galois-Counter-Mode
52 ##                      on Intel Architecture Processors. October, 2012.
53 ##
54 ## Assumptions:
55 ##
56 ##
57 ##
58 ## iv:
59 ##       0                   1                   2                   3
60 ##       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
61 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 ##       |                             Salt  (From the SA)               |
63 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 ##       |                     Initialization Vector                     |
65 ##       |         (This is the sequence number from IPSec header)       |
66 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 ##       |                              0x1                              |
68 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 ##
70 ##
71 ##
72 ## AAD:
73 ##       AAD padded to 128 bits with 0
74 ##       for example, assume AAD is a u32 vector
75 ##
76 ##       if AAD is 8 bytes:
77 ##       AAD[3] = {A0, A1}#
78 ##       padded AAD in xmm register = {A1 A0 0 0}
79 ##
80 ##       0                   1                   2                   3
81 ##       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
82 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 ##       |                               SPI (A1)                        |
84 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 ##       |                     32-bit Sequence Number (A0)               |
86 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 ##       |                              0x0                              |
88 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 ##
90 ##                                       AAD Format with 32-bit Sequence Number
91 ##
92 ##       if AAD is 12 bytes:
93 ##       AAD[3] = {A0, A1, A2}#
94 ##       padded AAD in xmm register = {A2 A1 A0 0}
95 ##
96 ##       0                   1                   2                   3
97 ##       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
98 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 ##       |                               SPI (A2)                        |
100 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 ##       |                 64-bit Extended Sequence Number {A1,A0}       |
102 ##       |                                                               |
103 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 ##       |                              0x0                              |
105 ##       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ##
107 ##        AAD Format with 64-bit Extended Sequence Number
108 ##
109 ##
110 ## aadLen:
111 ##       from the definition of the spec, aadLen can only be 8 or 12 bytes.
112 ##       The code additionally supports aadLen of length 16 bytes.
113 ##
114 ## TLen:
115 ##       from the definition of the spec, TLen can only be 8, 12 or 16 bytes.
116 ##
117 ## poly = x^128 + x^127 + x^126 + x^121 + 1
118 ## throughout the code, one tab and two tab indentations are used. one tab is
119 ## for GHASH part, two tabs is for AES part.
120 ##
121
122 #include <linux/linkage.h>
123 #include <asm/inst.h>
124
125 # constants in mergeable sections, linker can reorder and merge
126 .section        .rodata.cst16.POLY, "aM", @progbits, 16
127 .align 16
128 POLY:            .octa     0xC2000000000000000000000000000001
129
130 .section        .rodata.cst16.POLY2, "aM", @progbits, 16
131 .align 16
132 POLY2:           .octa     0xC20000000000000000000001C2000000
133
134 .section        .rodata.cst16.TWOONE, "aM", @progbits, 16
135 .align 16
136 TWOONE:          .octa     0x00000001000000000000000000000001
137
138 .section        .rodata.cst16.SHUF_MASK, "aM", @progbits, 16
139 .align 16
140 SHUF_MASK:       .octa     0x000102030405060708090A0B0C0D0E0F
141
142 .section        .rodata.cst16.ONE, "aM", @progbits, 16
143 .align 16
144 ONE:             .octa     0x00000000000000000000000000000001
145
146 .section        .rodata.cst16.ONEf, "aM", @progbits, 16
147 .align 16
148 ONEf:            .octa     0x01000000000000000000000000000000
149
150 # order of these constants should not change.
151 # more specifically, ALL_F should follow SHIFT_MASK, and zero should follow ALL_F
152 .section        .rodata, "a", @progbits
153 .align 16
154 SHIFT_MASK:      .octa     0x0f0e0d0c0b0a09080706050403020100
155 ALL_F:           .octa     0xffffffffffffffffffffffffffffffff
156                  .octa     0x00000000000000000000000000000000
157
158 .section .rodata
159 .align 16
160 .type aad_shift_arr, @object
161 .size aad_shift_arr, 272
162 aad_shift_arr:
163         .octa     0xffffffffffffffffffffffffffffffff
164         .octa     0xffffffffffffffffffffffffffffff0C
165         .octa     0xffffffffffffffffffffffffffff0D0C
166         .octa     0xffffffffffffffffffffffffff0E0D0C
167         .octa     0xffffffffffffffffffffffff0F0E0D0C
168         .octa     0xffffffffffffffffffffff0C0B0A0908
169         .octa     0xffffffffffffffffffff0D0C0B0A0908
170         .octa     0xffffffffffffffffff0E0D0C0B0A0908
171         .octa     0xffffffffffffffff0F0E0D0C0B0A0908
172         .octa     0xffffffffffffff0C0B0A090807060504
173         .octa     0xffffffffffff0D0C0B0A090807060504
174         .octa     0xffffffffff0E0D0C0B0A090807060504
175         .octa     0xffffffff0F0E0D0C0B0A090807060504
176         .octa     0xffffff0C0B0A09080706050403020100
177         .octa     0xffff0D0C0B0A09080706050403020100
178         .octa     0xff0E0D0C0B0A09080706050403020100
179         .octa     0x0F0E0D0C0B0A09080706050403020100
180
181
182 .text
183
184
185 ##define the fields of the gcm aes context
186 #{
187 #        u8 expanded_keys[16*11] store expanded keys
188 #        u8 shifted_hkey_1[16]   store HashKey <<1 mod poly here
189 #        u8 shifted_hkey_2[16]   store HashKey^2 <<1 mod poly here
190 #        u8 shifted_hkey_3[16]   store HashKey^3 <<1 mod poly here
191 #        u8 shifted_hkey_4[16]   store HashKey^4 <<1 mod poly here
192 #        u8 shifted_hkey_5[16]   store HashKey^5 <<1 mod poly here
193 #        u8 shifted_hkey_6[16]   store HashKey^6 <<1 mod poly here
194 #        u8 shifted_hkey_7[16]   store HashKey^7 <<1 mod poly here
195 #        u8 shifted_hkey_8[16]   store HashKey^8 <<1 mod poly here
196 #        u8 shifted_hkey_1_k[16] store XOR HashKey <<1 mod poly here (for Karatsuba purposes)
197 #        u8 shifted_hkey_2_k[16] store XOR HashKey^2 <<1 mod poly here (for Karatsuba purposes)
198 #        u8 shifted_hkey_3_k[16] store XOR HashKey^3 <<1 mod poly here (for Karatsuba purposes)
199 #        u8 shifted_hkey_4_k[16] store XOR HashKey^4 <<1 mod poly here (for Karatsuba purposes)
200 #        u8 shifted_hkey_5_k[16] store XOR HashKey^5 <<1 mod poly here (for Karatsuba purposes)
201 #        u8 shifted_hkey_6_k[16] store XOR HashKey^6 <<1 mod poly here (for Karatsuba purposes)
202 #        u8 shifted_hkey_7_k[16] store XOR HashKey^7 <<1 mod poly here (for Karatsuba purposes)
203 #        u8 shifted_hkey_8_k[16] store XOR HashKey^8 <<1 mod poly here (for Karatsuba purposes)
204 #} gcm_ctx#
205
206 HashKey        = 16*11   # store HashKey <<1 mod poly here
207 HashKey_2      = 16*12   # store HashKey^2 <<1 mod poly here
208 HashKey_3      = 16*13   # store HashKey^3 <<1 mod poly here
209 HashKey_4      = 16*14   # store HashKey^4 <<1 mod poly here
210 HashKey_5      = 16*15   # store HashKey^5 <<1 mod poly here
211 HashKey_6      = 16*16   # store HashKey^6 <<1 mod poly here
212 HashKey_7      = 16*17   # store HashKey^7 <<1 mod poly here
213 HashKey_8      = 16*18   # store HashKey^8 <<1 mod poly here
214 HashKey_k      = 16*19   # store XOR of HashKey <<1 mod poly here (for Karatsuba purposes)
215 HashKey_2_k    = 16*20   # store XOR of HashKey^2 <<1 mod poly here (for Karatsuba purposes)
216 HashKey_3_k    = 16*21   # store XOR of HashKey^3 <<1 mod poly here (for Karatsuba purposes)
217 HashKey_4_k    = 16*22   # store XOR of HashKey^4 <<1 mod poly here (for Karatsuba purposes)
218 HashKey_5_k    = 16*23   # store XOR of HashKey^5 <<1 mod poly here (for Karatsuba purposes)
219 HashKey_6_k    = 16*24   # store XOR of HashKey^6 <<1 mod poly here (for Karatsuba purposes)
220 HashKey_7_k    = 16*25   # store XOR of HashKey^7 <<1 mod poly here (for Karatsuba purposes)
221 HashKey_8_k    = 16*26   # store XOR of HashKey^8 <<1 mod poly here (for Karatsuba purposes)
222
223 #define arg1 %rdi
224 #define arg2 %rsi
225 #define arg3 %rdx
226 #define arg4 %rcx
227 #define arg5 %r8
228 #define arg6 %r9
229 #define arg7 STACK_OFFSET+8*1(%r14)
230 #define arg8 STACK_OFFSET+8*2(%r14)
231 #define arg9 STACK_OFFSET+8*3(%r14)
232
233 i = 0
234 j = 0
235
236 out_order = 0
237 in_order = 1
238 DEC = 0
239 ENC = 1
240
241 .macro define_reg r n
242 reg_\r = %xmm\n
243 .endm
244
245 .macro setreg
246 .altmacro
247 define_reg i %i
248 define_reg j %j
249 .noaltmacro
250 .endm
251
252 # need to push 4 registers into stack to maintain
253 STACK_OFFSET = 8*4
254
255 TMP1 =   16*0    # Temporary storage for AAD
256 TMP2 =   16*1    # Temporary storage for AES State 2 (State 1 is stored in an XMM register)
257 TMP3 =   16*2    # Temporary storage for AES State 3
258 TMP4 =   16*3    # Temporary storage for AES State 4
259 TMP5 =   16*4    # Temporary storage for AES State 5
260 TMP6 =   16*5    # Temporary storage for AES State 6
261 TMP7 =   16*6    # Temporary storage for AES State 7
262 TMP8 =   16*7    # Temporary storage for AES State 8
263
264 VARIABLE_OFFSET = 16*8
265
266 ################################
267 # Utility Macros
268 ################################
269
270 # Encryption of a single block
271 .macro ENCRYPT_SINGLE_BLOCK XMM0
272                 vpxor    (arg1), \XMM0, \XMM0
273                 i = 1
274                 setreg
275 .rep 9
276                 vaesenc  16*i(arg1), \XMM0, \XMM0
277                 i = (i+1)
278                 setreg
279 .endr
280                 vaesenclast 16*10(arg1), \XMM0, \XMM0
281 .endm
282
283 # combined for GCM encrypt and decrypt functions
284 # clobbering all xmm registers
285 # clobbering r10, r11, r12, r13, r14, r15
286 .macro  GCM_ENC_DEC INITIAL_BLOCKS GHASH_8_ENCRYPT_8_PARALLEL GHASH_LAST_8 GHASH_MUL ENC_DEC
287
288         #the number of pushes must equal STACK_OFFSET
289         push    %r12
290         push    %r13
291         push    %r14
292         push    %r15
293
294         mov     %rsp, %r14
295
296
297
298
299         sub     $VARIABLE_OFFSET, %rsp
300         and     $~63, %rsp                  # align rsp to 64 bytes
301
302
303         vmovdqu  HashKey(arg1), %xmm13      # xmm13 = HashKey
304
305         mov     arg4, %r13                  # save the number of bytes of plaintext/ciphertext
306         and     $-16, %r13                  # r13 = r13 - (r13 mod 16)
307
308         mov     %r13, %r12
309         shr     $4, %r12
310         and     $7, %r12
311         jz      _initial_num_blocks_is_0\@
312
313         cmp     $7, %r12
314         je      _initial_num_blocks_is_7\@
315         cmp     $6, %r12
316         je      _initial_num_blocks_is_6\@
317         cmp     $5, %r12
318         je      _initial_num_blocks_is_5\@
319         cmp     $4, %r12
320         je      _initial_num_blocks_is_4\@
321         cmp     $3, %r12
322         je      _initial_num_blocks_is_3\@
323         cmp     $2, %r12
324         je      _initial_num_blocks_is_2\@
325
326         jmp     _initial_num_blocks_is_1\@
327
328 _initial_num_blocks_is_7\@:
329         \INITIAL_BLOCKS  7, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
330         sub     $16*7, %r13
331         jmp     _initial_blocks_encrypted\@
332
333 _initial_num_blocks_is_6\@:
334         \INITIAL_BLOCKS  6, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
335         sub     $16*6, %r13
336         jmp     _initial_blocks_encrypted\@
337
338 _initial_num_blocks_is_5\@:
339         \INITIAL_BLOCKS  5, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
340         sub     $16*5, %r13
341         jmp     _initial_blocks_encrypted\@
342
343 _initial_num_blocks_is_4\@:
344         \INITIAL_BLOCKS  4, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
345         sub     $16*4, %r13
346         jmp     _initial_blocks_encrypted\@
347
348 _initial_num_blocks_is_3\@:
349         \INITIAL_BLOCKS  3, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
350         sub     $16*3, %r13
351         jmp     _initial_blocks_encrypted\@
352
353 _initial_num_blocks_is_2\@:
354         \INITIAL_BLOCKS  2, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
355         sub     $16*2, %r13
356         jmp     _initial_blocks_encrypted\@
357
358 _initial_num_blocks_is_1\@:
359         \INITIAL_BLOCKS  1, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
360         sub     $16*1, %r13
361         jmp     _initial_blocks_encrypted\@
362
363 _initial_num_blocks_is_0\@:
364         \INITIAL_BLOCKS  0, %xmm12, %xmm13, %xmm14, %xmm15, %xmm11, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm10, %xmm0, \ENC_DEC
365
366
367 _initial_blocks_encrypted\@:
368         cmp     $0, %r13
369         je      _zero_cipher_left\@
370
371         sub     $128, %r13
372         je      _eight_cipher_left\@
373
374
375
376
377         vmovd   %xmm9, %r15d
378         and     $255, %r15d
379         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
380
381
382 _encrypt_by_8_new\@:
383         cmp     $(255-8), %r15d
384         jg      _encrypt_by_8\@
385
386
387
388         add     $8, %r15b
389         \GHASH_8_ENCRYPT_8_PARALLEL      %xmm0, %xmm10, %xmm11, %xmm12, %xmm13, %xmm14, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm15, out_order, \ENC_DEC
390         add     $128, %r11
391         sub     $128, %r13
392         jne     _encrypt_by_8_new\@
393
394         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
395         jmp     _eight_cipher_left\@
396
397 _encrypt_by_8\@:
398         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
399         add     $8, %r15b
400         \GHASH_8_ENCRYPT_8_PARALLEL      %xmm0, %xmm10, %xmm11, %xmm12, %xmm13, %xmm14, %xmm9, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, %xmm15, in_order, \ENC_DEC
401         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
402         add     $128, %r11
403         sub     $128, %r13
404         jne     _encrypt_by_8_new\@
405
406         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
407
408
409
410
411 _eight_cipher_left\@:
412         \GHASH_LAST_8    %xmm0, %xmm10, %xmm11, %xmm12, %xmm13, %xmm14, %xmm15, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8
413
414
415 _zero_cipher_left\@:
416         cmp     $16, arg4
417         jl      _only_less_than_16\@
418
419         mov     arg4, %r13
420         and     $15, %r13                            # r13 = (arg4 mod 16)
421
422         je      _multiple_of_16_bytes\@
423
424         # handle the last <16 Byte block seperately
425
426
427         vpaddd   ONE(%rip), %xmm9, %xmm9             # INCR CNT to get Yn
428         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
429         ENCRYPT_SINGLE_BLOCK    %xmm9                # E(K, Yn)
430
431         sub     $16, %r11
432         add     %r13, %r11
433         vmovdqu (arg3, %r11), %xmm1                  # receive the last <16 Byte block
434
435         lea     SHIFT_MASK+16(%rip), %r12
436         sub     %r13, %r12                           # adjust the shuffle mask pointer to be
437                                                      # able to shift 16-r13 bytes (r13 is the
438                                                      # number of bytes in plaintext mod 16)
439         vmovdqu (%r12), %xmm2                        # get the appropriate shuffle mask
440         vpshufb %xmm2, %xmm1, %xmm1                  # shift right 16-r13 bytes
441         jmp     _final_ghash_mul\@
442
443 _only_less_than_16\@:
444         # check for 0 length
445         mov     arg4, %r13
446         and     $15, %r13                            # r13 = (arg4 mod 16)
447
448         je      _multiple_of_16_bytes\@
449
450         # handle the last <16 Byte block separately
451
452
453         vpaddd  ONE(%rip), %xmm9, %xmm9              # INCR CNT to get Yn
454         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
455         ENCRYPT_SINGLE_BLOCK    %xmm9                # E(K, Yn)
456
457
458         lea     SHIFT_MASK+16(%rip), %r12
459         sub     %r13, %r12                           # adjust the shuffle mask pointer to be
460                                                      # able to shift 16-r13 bytes (r13 is the
461                                                      # number of bytes in plaintext mod 16)
462
463 _get_last_16_byte_loop\@:
464         movb    (arg3, %r11),  %al
465         movb    %al,  TMP1 (%rsp , %r11)
466         add     $1, %r11
467         cmp     %r13,  %r11
468         jne     _get_last_16_byte_loop\@
469
470         vmovdqu  TMP1(%rsp), %xmm1
471
472         sub     $16, %r11
473
474 _final_ghash_mul\@:
475         .if  \ENC_DEC ==  DEC
476         vmovdqa %xmm1, %xmm2
477         vpxor   %xmm1, %xmm9, %xmm9                  # Plaintext XOR E(K, Yn)
478         vmovdqu ALL_F-SHIFT_MASK(%r12), %xmm1        # get the appropriate mask to
479                                                      # mask out top 16-r13 bytes of xmm9
480         vpand   %xmm1, %xmm9, %xmm9                  # mask out top 16-r13 bytes of xmm9
481         vpand   %xmm1, %xmm2, %xmm2
482         vpshufb SHUF_MASK(%rip), %xmm2, %xmm2
483         vpxor   %xmm2, %xmm14, %xmm14
484         #GHASH computation for the last <16 Byte block
485         \GHASH_MUL       %xmm14, %xmm13, %xmm0, %xmm10, %xmm11, %xmm5, %xmm6
486         sub     %r13, %r11
487         add     $16, %r11
488         .else
489         vpxor   %xmm1, %xmm9, %xmm9                  # Plaintext XOR E(K, Yn)
490         vmovdqu ALL_F-SHIFT_MASK(%r12), %xmm1        # get the appropriate mask to
491                                                      # mask out top 16-r13 bytes of xmm9
492         vpand   %xmm1, %xmm9, %xmm9                  # mask out top 16-r13 bytes of xmm9
493         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9
494         vpxor   %xmm9, %xmm14, %xmm14
495         #GHASH computation for the last <16 Byte block
496         \GHASH_MUL       %xmm14, %xmm13, %xmm0, %xmm10, %xmm11, %xmm5, %xmm6
497         sub     %r13, %r11
498         add     $16, %r11
499         vpshufb SHUF_MASK(%rip), %xmm9, %xmm9        # shuffle xmm9 back to output as ciphertext
500         .endif
501
502
503         #############################
504         # output r13 Bytes
505         vmovq   %xmm9, %rax
506         cmp     $8, %r13
507         jle     _less_than_8_bytes_left\@
508
509         mov     %rax, (arg2 , %r11)
510         add     $8, %r11
511         vpsrldq $8, %xmm9, %xmm9
512         vmovq   %xmm9, %rax
513         sub     $8, %r13
514
515 _less_than_8_bytes_left\@:
516         movb    %al, (arg2 , %r11)
517         add     $1, %r11
518         shr     $8, %rax
519         sub     $1, %r13
520         jne     _less_than_8_bytes_left\@
521         #############################
522
523 _multiple_of_16_bytes\@:
524         mov     arg7, %r12                           # r12 = aadLen (number of bytes)
525         shl     $3, %r12                             # convert into number of bits
526         vmovd   %r12d, %xmm15                        # len(A) in xmm15
527
528         shl     $3, arg4                             # len(C) in bits  (*128)
529         vmovq   arg4, %xmm1
530         vpslldq $8, %xmm15, %xmm15                   # xmm15 = len(A)|| 0x0000000000000000
531         vpxor   %xmm1, %xmm15, %xmm15                # xmm15 = len(A)||len(C)
532
533         vpxor   %xmm15, %xmm14, %xmm14
534         \GHASH_MUL       %xmm14, %xmm13, %xmm0, %xmm10, %xmm11, %xmm5, %xmm6    # final GHASH computation
535         vpshufb SHUF_MASK(%rip), %xmm14, %xmm14      # perform a 16Byte swap
536
537         mov     arg5, %rax                           # rax = *Y0
538         vmovdqu (%rax), %xmm9                        # xmm9 = Y0
539
540         ENCRYPT_SINGLE_BLOCK    %xmm9                # E(K, Y0)
541
542         vpxor   %xmm14, %xmm9, %xmm9
543
544
545
546 _return_T\@:
547         mov     arg8, %r10              # r10 = authTag
548         mov     arg9, %r11              # r11 = auth_tag_len
549
550         cmp     $16, %r11
551         je      _T_16\@
552
553         cmp     $8, %r11
554         jl      _T_4\@
555
556 _T_8\@:
557         vmovq   %xmm9, %rax
558         mov     %rax, (%r10)
559         add     $8, %r10
560         sub     $8, %r11
561         vpsrldq $8, %xmm9, %xmm9
562         cmp     $0, %r11
563         je     _return_T_done\@
564 _T_4\@:
565         vmovd   %xmm9, %eax
566         mov     %eax, (%r10)
567         add     $4, %r10
568         sub     $4, %r11
569         vpsrldq     $4, %xmm9, %xmm9
570         cmp     $0, %r11
571         je     _return_T_done\@
572 _T_123\@:
573         vmovd     %xmm9, %eax
574         cmp     $2, %r11
575         jl     _T_1\@
576         mov     %ax, (%r10)
577         cmp     $2, %r11
578         je     _return_T_done\@
579         add     $2, %r10
580         sar     $16, %eax
581 _T_1\@:
582         mov     %al, (%r10)
583         jmp     _return_T_done\@
584
585 _T_16\@:
586         vmovdqu %xmm9, (%r10)
587
588 _return_T_done\@:
589         mov     %r14, %rsp
590
591         pop     %r15
592         pop     %r14
593         pop     %r13
594         pop     %r12
595 .endm
596
597 #ifdef CONFIG_AS_AVX
598 ###############################################################################
599 # GHASH_MUL MACRO to implement: Data*HashKey mod (128,127,126,121,0)
600 # Input: A and B (128-bits each, bit-reflected)
601 # Output: C = A*B*x mod poly, (i.e. >>1 )
602 # To compute GH = GH*HashKey mod poly, give HK = HashKey<<1 mod poly as input
603 # GH = GH * HK * x mod poly which is equivalent to GH*HashKey mod poly.
604 ###############################################################################
605 .macro  GHASH_MUL_AVX GH HK T1 T2 T3 T4 T5
606
607         vpshufd         $0b01001110, \GH, \T2
608         vpshufd         $0b01001110, \HK, \T3
609         vpxor           \GH     , \T2, \T2      # T2 = (a1+a0)
610         vpxor           \HK     , \T3, \T3      # T3 = (b1+b0)
611
612         vpclmulqdq      $0x11, \HK, \GH, \T1    # T1 = a1*b1
613         vpclmulqdq      $0x00, \HK, \GH, \GH    # GH = a0*b0
614         vpclmulqdq      $0x00, \T3, \T2, \T2    # T2 = (a1+a0)*(b1+b0)
615         vpxor           \GH, \T2,\T2
616         vpxor           \T1, \T2,\T2            # T2 = a0*b1+a1*b0
617
618         vpslldq         $8, \T2,\T3             # shift-L T3 2 DWs
619         vpsrldq         $8, \T2,\T2             # shift-R T2 2 DWs
620         vpxor           \T3, \GH, \GH
621         vpxor           \T2, \T1, \T1           # <T1:GH> = GH x HK
622
623         #first phase of the reduction
624         vpslld  $31, \GH, \T2                   # packed right shifting << 31
625         vpslld  $30, \GH, \T3                   # packed right shifting shift << 30
626         vpslld  $25, \GH, \T4                   # packed right shifting shift << 25
627
628         vpxor   \T3, \T2, \T2                   # xor the shifted versions
629         vpxor   \T4, \T2, \T2
630
631         vpsrldq $4, \T2, \T5                    # shift-R T5 1 DW
632
633         vpslldq $12, \T2, \T2                   # shift-L T2 3 DWs
634         vpxor   \T2, \GH, \GH                   # first phase of the reduction complete
635
636         #second phase of the reduction
637
638         vpsrld  $1,\GH, \T2                     # packed left shifting >> 1
639         vpsrld  $2,\GH, \T3                     # packed left shifting >> 2
640         vpsrld  $7,\GH, \T4                     # packed left shifting >> 7
641         vpxor   \T3, \T2, \T2                   # xor the shifted versions
642         vpxor   \T4, \T2, \T2
643
644         vpxor   \T5, \T2, \T2
645         vpxor   \T2, \GH, \GH
646         vpxor   \T1, \GH, \GH                   # the result is in GH
647
648
649 .endm
650
651 .macro PRECOMPUTE_AVX HK T1 T2 T3 T4 T5 T6
652
653         # Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
654         vmovdqa  \HK, \T5
655
656         vpshufd  $0b01001110, \T5, \T1
657         vpxor    \T5, \T1, \T1
658         vmovdqa  \T1, HashKey_k(arg1)
659
660         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^2<<1 mod poly
661         vmovdqa  \T5, HashKey_2(arg1)                    #  [HashKey_2] = HashKey^2<<1 mod poly
662         vpshufd  $0b01001110, \T5, \T1
663         vpxor    \T5, \T1, \T1
664         vmovdqa  \T1, HashKey_2_k(arg1)
665
666         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^3<<1 mod poly
667         vmovdqa  \T5, HashKey_3(arg1)
668         vpshufd  $0b01001110, \T5, \T1
669         vpxor    \T5, \T1, \T1
670         vmovdqa  \T1, HashKey_3_k(arg1)
671
672         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^4<<1 mod poly
673         vmovdqa  \T5, HashKey_4(arg1)
674         vpshufd  $0b01001110, \T5, \T1
675         vpxor    \T5, \T1, \T1
676         vmovdqa  \T1, HashKey_4_k(arg1)
677
678         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^5<<1 mod poly
679         vmovdqa  \T5, HashKey_5(arg1)
680         vpshufd  $0b01001110, \T5, \T1
681         vpxor    \T5, \T1, \T1
682         vmovdqa  \T1, HashKey_5_k(arg1)
683
684         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^6<<1 mod poly
685         vmovdqa  \T5, HashKey_6(arg1)
686         vpshufd  $0b01001110, \T5, \T1
687         vpxor    \T5, \T1, \T1
688         vmovdqa  \T1, HashKey_6_k(arg1)
689
690         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^7<<1 mod poly
691         vmovdqa  \T5, HashKey_7(arg1)
692         vpshufd  $0b01001110, \T5, \T1
693         vpxor    \T5, \T1, \T1
694         vmovdqa  \T1, HashKey_7_k(arg1)
695
696         GHASH_MUL_AVX \T5, \HK, \T1, \T3, \T4, \T6, \T2  #  T5 = HashKey^8<<1 mod poly
697         vmovdqa  \T5, HashKey_8(arg1)
698         vpshufd  $0b01001110, \T5, \T1
699         vpxor    \T5, \T1, \T1
700         vmovdqa  \T1, HashKey_8_k(arg1)
701
702 .endm
703
704 ## if a = number of total plaintext bytes
705 ## b = floor(a/16)
706 ## num_initial_blocks = b mod 4#
707 ## encrypt the initial num_initial_blocks blocks and apply ghash on the ciphertext
708 ## r10, r11, r12, rax are clobbered
709 ## arg1, arg2, arg3, r14 are used as a pointer only, not modified
710
711 .macro INITIAL_BLOCKS_AVX num_initial_blocks T1 T2 T3 T4 T5 CTR XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 T6 T_key ENC_DEC
712         i = (8-\num_initial_blocks)
713         j = 0
714         setreg
715
716         mov     arg6, %r10                      # r10 = AAD
717         mov     arg7, %r12                      # r12 = aadLen
718
719
720         mov     %r12, %r11
721
722         vpxor   reg_j, reg_j, reg_j
723         vpxor   reg_i, reg_i, reg_i
724         cmp     $16, %r11
725         jl      _get_AAD_rest8\@
726 _get_AAD_blocks\@:
727         vmovdqu (%r10), reg_i
728         vpshufb SHUF_MASK(%rip), reg_i, reg_i
729         vpxor   reg_i, reg_j, reg_j
730         GHASH_MUL_AVX       reg_j, \T2, \T1, \T3, \T4, \T5, \T6
731         add     $16, %r10
732         sub     $16, %r12
733         sub     $16, %r11
734         cmp     $16, %r11
735         jge     _get_AAD_blocks\@
736         vmovdqu reg_j, reg_i
737         cmp     $0, %r11
738         je      _get_AAD_done\@
739
740         vpxor   reg_i, reg_i, reg_i
741
742         /* read the last <16B of AAD. since we have at least 4B of
743         data right after the AAD (the ICV, and maybe some CT), we can
744         read 4B/8B blocks safely, and then get rid of the extra stuff */
745 _get_AAD_rest8\@:
746         cmp     $4, %r11
747         jle     _get_AAD_rest4\@
748         movq    (%r10), \T1
749         add     $8, %r10
750         sub     $8, %r11
751         vpslldq $8, \T1, \T1
752         vpsrldq $8, reg_i, reg_i
753         vpxor   \T1, reg_i, reg_i
754         jmp     _get_AAD_rest8\@
755 _get_AAD_rest4\@:
756         cmp     $0, %r11
757         jle      _get_AAD_rest0\@
758         mov     (%r10), %eax
759         movq    %rax, \T1
760         add     $4, %r10
761         sub     $4, %r11
762         vpslldq $12, \T1, \T1
763         vpsrldq $4, reg_i, reg_i
764         vpxor   \T1, reg_i, reg_i
765 _get_AAD_rest0\@:
766         /* finalize: shift out the extra bytes we read, and align
767         left. since pslldq can only shift by an immediate, we use
768         vpshufb and an array of shuffle masks */
769         movq    %r12, %r11
770         salq    $4, %r11
771         movdqu  aad_shift_arr(%r11), \T1
772         vpshufb \T1, reg_i, reg_i
773 _get_AAD_rest_final\@:
774         vpshufb SHUF_MASK(%rip), reg_i, reg_i
775         vpxor   reg_j, reg_i, reg_i
776         GHASH_MUL_AVX       reg_i, \T2, \T1, \T3, \T4, \T5, \T6
777
778 _get_AAD_done\@:
779         # initialize the data pointer offset as zero
780         xor     %r11d, %r11d
781
782         # start AES for num_initial_blocks blocks
783         mov     arg5, %rax                     # rax = *Y0
784         vmovdqu (%rax), \CTR                   # CTR = Y0
785         vpshufb SHUF_MASK(%rip), \CTR, \CTR
786
787
788         i = (9-\num_initial_blocks)
789         setreg
790 .rep \num_initial_blocks
791                 vpaddd  ONE(%rip), \CTR, \CTR           # INCR Y0
792                 vmovdqa \CTR, reg_i
793                 vpshufb SHUF_MASK(%rip), reg_i, reg_i   # perform a 16Byte swap
794         i = (i+1)
795         setreg
796 .endr
797
798         vmovdqa  (arg1), \T_key
799         i = (9-\num_initial_blocks)
800         setreg
801 .rep \num_initial_blocks
802                 vpxor   \T_key, reg_i, reg_i
803         i = (i+1)
804         setreg
805 .endr
806
807         j = 1
808         setreg
809 .rep 9
810         vmovdqa  16*j(arg1), \T_key
811         i = (9-\num_initial_blocks)
812         setreg
813 .rep \num_initial_blocks
814         vaesenc \T_key, reg_i, reg_i
815         i = (i+1)
816         setreg
817 .endr
818
819         j = (j+1)
820         setreg
821 .endr
822
823
824         vmovdqa  16*10(arg1), \T_key
825         i = (9-\num_initial_blocks)
826         setreg
827 .rep \num_initial_blocks
828         vaesenclast      \T_key, reg_i, reg_i
829         i = (i+1)
830         setreg
831 .endr
832
833         i = (9-\num_initial_blocks)
834         setreg
835 .rep \num_initial_blocks
836                 vmovdqu (arg3, %r11), \T1
837                 vpxor   \T1, reg_i, reg_i
838                 vmovdqu reg_i, (arg2 , %r11)           # write back ciphertext for num_initial_blocks blocks
839                 add     $16, %r11
840 .if  \ENC_DEC == DEC
841                 vmovdqa \T1, reg_i
842 .endif
843                 vpshufb SHUF_MASK(%rip), reg_i, reg_i  # prepare ciphertext for GHASH computations
844         i = (i+1)
845         setreg
846 .endr
847
848
849         i = (8-\num_initial_blocks)
850         j = (9-\num_initial_blocks)
851         setreg
852
853 .rep \num_initial_blocks
854         vpxor    reg_i, reg_j, reg_j
855         GHASH_MUL_AVX       reg_j, \T2, \T1, \T3, \T4, \T5, \T6 # apply GHASH on num_initial_blocks blocks
856         i = (i+1)
857         j = (j+1)
858         setreg
859 .endr
860         # XMM8 has the combined result here
861
862         vmovdqa  \XMM8, TMP1(%rsp)
863         vmovdqa  \XMM8, \T3
864
865         cmp     $128, %r13
866         jl      _initial_blocks_done\@                  # no need for precomputed constants
867
868 ###############################################################################
869 # Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
870                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
871                 vmovdqa  \CTR, \XMM1
872                 vpshufb  SHUF_MASK(%rip), \XMM1, \XMM1  # perform a 16Byte swap
873
874                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
875                 vmovdqa  \CTR, \XMM2
876                 vpshufb  SHUF_MASK(%rip), \XMM2, \XMM2  # perform a 16Byte swap
877
878                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
879                 vmovdqa  \CTR, \XMM3
880                 vpshufb  SHUF_MASK(%rip), \XMM3, \XMM3  # perform a 16Byte swap
881
882                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
883                 vmovdqa  \CTR, \XMM4
884                 vpshufb  SHUF_MASK(%rip), \XMM4, \XMM4  # perform a 16Byte swap
885
886                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
887                 vmovdqa  \CTR, \XMM5
888                 vpshufb  SHUF_MASK(%rip), \XMM5, \XMM5  # perform a 16Byte swap
889
890                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
891                 vmovdqa  \CTR, \XMM6
892                 vpshufb  SHUF_MASK(%rip), \XMM6, \XMM6  # perform a 16Byte swap
893
894                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
895                 vmovdqa  \CTR, \XMM7
896                 vpshufb  SHUF_MASK(%rip), \XMM7, \XMM7  # perform a 16Byte swap
897
898                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
899                 vmovdqa  \CTR, \XMM8
900                 vpshufb  SHUF_MASK(%rip), \XMM8, \XMM8  # perform a 16Byte swap
901
902                 vmovdqa  (arg1), \T_key
903                 vpxor    \T_key, \XMM1, \XMM1
904                 vpxor    \T_key, \XMM2, \XMM2
905                 vpxor    \T_key, \XMM3, \XMM3
906                 vpxor    \T_key, \XMM4, \XMM4
907                 vpxor    \T_key, \XMM5, \XMM5
908                 vpxor    \T_key, \XMM6, \XMM6
909                 vpxor    \T_key, \XMM7, \XMM7
910                 vpxor    \T_key, \XMM8, \XMM8
911
912                 i = 1
913                 setreg
914 .rep    9       # do 9 rounds
915                 vmovdqa  16*i(arg1), \T_key
916                 vaesenc  \T_key, \XMM1, \XMM1
917                 vaesenc  \T_key, \XMM2, \XMM2
918                 vaesenc  \T_key, \XMM3, \XMM3
919                 vaesenc  \T_key, \XMM4, \XMM4
920                 vaesenc  \T_key, \XMM5, \XMM5
921                 vaesenc  \T_key, \XMM6, \XMM6
922                 vaesenc  \T_key, \XMM7, \XMM7
923                 vaesenc  \T_key, \XMM8, \XMM8
924                 i = (i+1)
925                 setreg
926 .endr
927
928
929                 vmovdqa  16*i(arg1), \T_key
930                 vaesenclast  \T_key, \XMM1, \XMM1
931                 vaesenclast  \T_key, \XMM2, \XMM2
932                 vaesenclast  \T_key, \XMM3, \XMM3
933                 vaesenclast  \T_key, \XMM4, \XMM4
934                 vaesenclast  \T_key, \XMM5, \XMM5
935                 vaesenclast  \T_key, \XMM6, \XMM6
936                 vaesenclast  \T_key, \XMM7, \XMM7
937                 vaesenclast  \T_key, \XMM8, \XMM8
938
939                 vmovdqu  (arg3, %r11), \T1
940                 vpxor    \T1, \XMM1, \XMM1
941                 vmovdqu  \XMM1, (arg2 , %r11)
942                 .if   \ENC_DEC == DEC
943                 vmovdqa  \T1, \XMM1
944                 .endif
945
946                 vmovdqu  16*1(arg3, %r11), \T1
947                 vpxor    \T1, \XMM2, \XMM2
948                 vmovdqu  \XMM2, 16*1(arg2 , %r11)
949                 .if   \ENC_DEC == DEC
950                 vmovdqa  \T1, \XMM2
951                 .endif
952
953                 vmovdqu  16*2(arg3, %r11), \T1
954                 vpxor    \T1, \XMM3, \XMM3
955                 vmovdqu  \XMM3, 16*2(arg2 , %r11)
956                 .if   \ENC_DEC == DEC
957                 vmovdqa  \T1, \XMM3
958                 .endif
959
960                 vmovdqu  16*3(arg3, %r11), \T1
961                 vpxor    \T1, \XMM4, \XMM4
962                 vmovdqu  \XMM4, 16*3(arg2 , %r11)
963                 .if   \ENC_DEC == DEC
964                 vmovdqa  \T1, \XMM4
965                 .endif
966
967                 vmovdqu  16*4(arg3, %r11), \T1
968                 vpxor    \T1, \XMM5, \XMM5
969                 vmovdqu  \XMM5, 16*4(arg2 , %r11)
970                 .if   \ENC_DEC == DEC
971                 vmovdqa  \T1, \XMM5
972                 .endif
973
974                 vmovdqu  16*5(arg3, %r11), \T1
975                 vpxor    \T1, \XMM6, \XMM6
976                 vmovdqu  \XMM6, 16*5(arg2 , %r11)
977                 .if   \ENC_DEC == DEC
978                 vmovdqa  \T1, \XMM6
979                 .endif
980
981                 vmovdqu  16*6(arg3, %r11), \T1
982                 vpxor    \T1, \XMM7, \XMM7
983                 vmovdqu  \XMM7, 16*6(arg2 , %r11)
984                 .if   \ENC_DEC == DEC
985                 vmovdqa  \T1, \XMM7
986                 .endif
987
988                 vmovdqu  16*7(arg3, %r11), \T1
989                 vpxor    \T1, \XMM8, \XMM8
990                 vmovdqu  \XMM8, 16*7(arg2 , %r11)
991                 .if   \ENC_DEC == DEC
992                 vmovdqa  \T1, \XMM8
993                 .endif
994
995                 add     $128, %r11
996
997                 vpshufb  SHUF_MASK(%rip), \XMM1, \XMM1     # perform a 16Byte swap
998                 vpxor    TMP1(%rsp), \XMM1, \XMM1          # combine GHASHed value with the corresponding ciphertext
999                 vpshufb  SHUF_MASK(%rip), \XMM2, \XMM2     # perform a 16Byte swap
1000                 vpshufb  SHUF_MASK(%rip), \XMM3, \XMM3     # perform a 16Byte swap
1001                 vpshufb  SHUF_MASK(%rip), \XMM4, \XMM4     # perform a 16Byte swap
1002                 vpshufb  SHUF_MASK(%rip), \XMM5, \XMM5     # perform a 16Byte swap
1003                 vpshufb  SHUF_MASK(%rip), \XMM6, \XMM6     # perform a 16Byte swap
1004                 vpshufb  SHUF_MASK(%rip), \XMM7, \XMM7     # perform a 16Byte swap
1005                 vpshufb  SHUF_MASK(%rip), \XMM8, \XMM8     # perform a 16Byte swap
1006
1007 ###############################################################################
1008
1009 _initial_blocks_done\@:
1010
1011 .endm
1012
1013 # encrypt 8 blocks at a time
1014 # ghash the 8 previously encrypted ciphertext blocks
1015 # arg1, arg2, arg3 are used as pointers only, not modified
1016 # r11 is the data offset value
1017 .macro GHASH_8_ENCRYPT_8_PARALLEL_AVX T1 T2 T3 T4 T5 T6 CTR XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 T7 loop_idx ENC_DEC
1018
1019         vmovdqa \XMM1, \T2
1020         vmovdqa \XMM2, TMP2(%rsp)
1021         vmovdqa \XMM3, TMP3(%rsp)
1022         vmovdqa \XMM4, TMP4(%rsp)
1023         vmovdqa \XMM5, TMP5(%rsp)
1024         vmovdqa \XMM6, TMP6(%rsp)
1025         vmovdqa \XMM7, TMP7(%rsp)
1026         vmovdqa \XMM8, TMP8(%rsp)
1027
1028 .if \loop_idx == in_order
1029                 vpaddd  ONE(%rip), \CTR, \XMM1           # INCR CNT
1030                 vpaddd  ONE(%rip), \XMM1, \XMM2
1031                 vpaddd  ONE(%rip), \XMM2, \XMM3
1032                 vpaddd  ONE(%rip), \XMM3, \XMM4
1033                 vpaddd  ONE(%rip), \XMM4, \XMM5
1034                 vpaddd  ONE(%rip), \XMM5, \XMM6
1035                 vpaddd  ONE(%rip), \XMM6, \XMM7
1036                 vpaddd  ONE(%rip), \XMM7, \XMM8
1037                 vmovdqa \XMM8, \CTR
1038
1039                 vpshufb SHUF_MASK(%rip), \XMM1, \XMM1    # perform a 16Byte swap
1040                 vpshufb SHUF_MASK(%rip), \XMM2, \XMM2    # perform a 16Byte swap
1041                 vpshufb SHUF_MASK(%rip), \XMM3, \XMM3    # perform a 16Byte swap
1042                 vpshufb SHUF_MASK(%rip), \XMM4, \XMM4    # perform a 16Byte swap
1043                 vpshufb SHUF_MASK(%rip), \XMM5, \XMM5    # perform a 16Byte swap
1044                 vpshufb SHUF_MASK(%rip), \XMM6, \XMM6    # perform a 16Byte swap
1045                 vpshufb SHUF_MASK(%rip), \XMM7, \XMM7    # perform a 16Byte swap
1046                 vpshufb SHUF_MASK(%rip), \XMM8, \XMM8    # perform a 16Byte swap
1047 .else
1048                 vpaddd  ONEf(%rip), \CTR, \XMM1           # INCR CNT
1049                 vpaddd  ONEf(%rip), \XMM1, \XMM2
1050                 vpaddd  ONEf(%rip), \XMM2, \XMM3
1051                 vpaddd  ONEf(%rip), \XMM3, \XMM4
1052                 vpaddd  ONEf(%rip), \XMM4, \XMM5
1053                 vpaddd  ONEf(%rip), \XMM5, \XMM6
1054                 vpaddd  ONEf(%rip), \XMM6, \XMM7
1055                 vpaddd  ONEf(%rip), \XMM7, \XMM8
1056                 vmovdqa \XMM8, \CTR
1057 .endif
1058
1059
1060         #######################################################################
1061
1062                 vmovdqu (arg1), \T1
1063                 vpxor   \T1, \XMM1, \XMM1
1064                 vpxor   \T1, \XMM2, \XMM2
1065                 vpxor   \T1, \XMM3, \XMM3
1066                 vpxor   \T1, \XMM4, \XMM4
1067                 vpxor   \T1, \XMM5, \XMM5
1068                 vpxor   \T1, \XMM6, \XMM6
1069                 vpxor   \T1, \XMM7, \XMM7
1070                 vpxor   \T1, \XMM8, \XMM8
1071
1072         #######################################################################
1073
1074
1075
1076
1077
1078                 vmovdqu 16*1(arg1), \T1
1079                 vaesenc \T1, \XMM1, \XMM1
1080                 vaesenc \T1, \XMM2, \XMM2
1081                 vaesenc \T1, \XMM3, \XMM3
1082                 vaesenc \T1, \XMM4, \XMM4
1083                 vaesenc \T1, \XMM5, \XMM5
1084                 vaesenc \T1, \XMM6, \XMM6
1085                 vaesenc \T1, \XMM7, \XMM7
1086                 vaesenc \T1, \XMM8, \XMM8
1087
1088                 vmovdqu 16*2(arg1), \T1
1089                 vaesenc \T1, \XMM1, \XMM1
1090                 vaesenc \T1, \XMM2, \XMM2
1091                 vaesenc \T1, \XMM3, \XMM3
1092                 vaesenc \T1, \XMM4, \XMM4
1093                 vaesenc \T1, \XMM5, \XMM5
1094                 vaesenc \T1, \XMM6, \XMM6
1095                 vaesenc \T1, \XMM7, \XMM7
1096                 vaesenc \T1, \XMM8, \XMM8
1097
1098
1099         #######################################################################
1100
1101         vmovdqa         HashKey_8(arg1), \T5
1102         vpclmulqdq      $0x11, \T5, \T2, \T4             # T4 = a1*b1
1103         vpclmulqdq      $0x00, \T5, \T2, \T7             # T7 = a0*b0
1104
1105         vpshufd         $0b01001110, \T2, \T6
1106         vpxor           \T2, \T6, \T6
1107
1108         vmovdqa         HashKey_8_k(arg1), \T5
1109         vpclmulqdq      $0x00, \T5, \T6, \T6
1110
1111                 vmovdqu 16*3(arg1), \T1
1112                 vaesenc \T1, \XMM1, \XMM1
1113                 vaesenc \T1, \XMM2, \XMM2
1114                 vaesenc \T1, \XMM3, \XMM3
1115                 vaesenc \T1, \XMM4, \XMM4
1116                 vaesenc \T1, \XMM5, \XMM5
1117                 vaesenc \T1, \XMM6, \XMM6
1118                 vaesenc \T1, \XMM7, \XMM7
1119                 vaesenc \T1, \XMM8, \XMM8
1120
1121         vmovdqa         TMP2(%rsp), \T1
1122         vmovdqa         HashKey_7(arg1), \T5
1123         vpclmulqdq      $0x11, \T5, \T1, \T3
1124         vpxor           \T3, \T4, \T4
1125         vpclmulqdq      $0x00, \T5, \T1, \T3
1126         vpxor           \T3, \T7, \T7
1127
1128         vpshufd         $0b01001110, \T1, \T3
1129         vpxor           \T1, \T3, \T3
1130         vmovdqa         HashKey_7_k(arg1), \T5
1131         vpclmulqdq      $0x10, \T5, \T3, \T3
1132         vpxor           \T3, \T6, \T6
1133
1134                 vmovdqu 16*4(arg1), \T1
1135                 vaesenc \T1, \XMM1, \XMM1
1136                 vaesenc \T1, \XMM2, \XMM2
1137                 vaesenc \T1, \XMM3, \XMM3
1138                 vaesenc \T1, \XMM4, \XMM4
1139                 vaesenc \T1, \XMM5, \XMM5
1140                 vaesenc \T1, \XMM6, \XMM6
1141                 vaesenc \T1, \XMM7, \XMM7
1142                 vaesenc \T1, \XMM8, \XMM8
1143
1144         #######################################################################
1145
1146         vmovdqa         TMP3(%rsp), \T1
1147         vmovdqa         HashKey_6(arg1), \T5
1148         vpclmulqdq      $0x11, \T5, \T1, \T3
1149         vpxor           \T3, \T4, \T4
1150         vpclmulqdq      $0x00, \T5, \T1, \T3
1151         vpxor           \T3, \T7, \T7
1152
1153         vpshufd         $0b01001110, \T1, \T3
1154         vpxor           \T1, \T3, \T3
1155         vmovdqa         HashKey_6_k(arg1), \T5
1156         vpclmulqdq      $0x10, \T5, \T3, \T3
1157         vpxor           \T3, \T6, \T6
1158
1159                 vmovdqu 16*5(arg1), \T1
1160                 vaesenc \T1, \XMM1, \XMM1
1161                 vaesenc \T1, \XMM2, \XMM2
1162                 vaesenc \T1, \XMM3, \XMM3
1163                 vaesenc \T1, \XMM4, \XMM4
1164                 vaesenc \T1, \XMM5, \XMM5
1165                 vaesenc \T1, \XMM6, \XMM6
1166                 vaesenc \T1, \XMM7, \XMM7
1167                 vaesenc \T1, \XMM8, \XMM8
1168
1169         vmovdqa         TMP4(%rsp), \T1
1170         vmovdqa         HashKey_5(arg1), \T5
1171         vpclmulqdq      $0x11, \T5, \T1, \T3
1172         vpxor           \T3, \T4, \T4
1173         vpclmulqdq      $0x00, \T5, \T1, \T3
1174         vpxor           \T3, \T7, \T7
1175
1176         vpshufd         $0b01001110, \T1, \T3
1177         vpxor           \T1, \T3, \T3
1178         vmovdqa         HashKey_5_k(arg1), \T5
1179         vpclmulqdq      $0x10, \T5, \T3, \T3
1180         vpxor           \T3, \T6, \T6
1181
1182                 vmovdqu 16*6(arg1), \T1
1183                 vaesenc \T1, \XMM1, \XMM1
1184                 vaesenc \T1, \XMM2, \XMM2
1185                 vaesenc \T1, \XMM3, \XMM3
1186                 vaesenc \T1, \XMM4, \XMM4
1187                 vaesenc \T1, \XMM5, \XMM5
1188                 vaesenc \T1, \XMM6, \XMM6
1189                 vaesenc \T1, \XMM7, \XMM7
1190                 vaesenc \T1, \XMM8, \XMM8
1191
1192
1193         vmovdqa         TMP5(%rsp), \T1
1194         vmovdqa         HashKey_4(arg1), \T5
1195         vpclmulqdq      $0x11, \T5, \T1, \T3
1196         vpxor           \T3, \T4, \T4
1197         vpclmulqdq      $0x00, \T5, \T1, \T3
1198         vpxor           \T3, \T7, \T7
1199
1200         vpshufd         $0b01001110, \T1, \T3
1201         vpxor           \T1, \T3, \T3
1202         vmovdqa         HashKey_4_k(arg1), \T5
1203         vpclmulqdq      $0x10, \T5, \T3, \T3
1204         vpxor           \T3, \T6, \T6
1205
1206                 vmovdqu 16*7(arg1), \T1
1207                 vaesenc \T1, \XMM1, \XMM1
1208                 vaesenc \T1, \XMM2, \XMM2
1209                 vaesenc \T1, \XMM3, \XMM3
1210                 vaesenc \T1, \XMM4, \XMM4
1211                 vaesenc \T1, \XMM5, \XMM5
1212                 vaesenc \T1, \XMM6, \XMM6
1213                 vaesenc \T1, \XMM7, \XMM7
1214                 vaesenc \T1, \XMM8, \XMM8
1215
1216         vmovdqa         TMP6(%rsp), \T1
1217         vmovdqa         HashKey_3(arg1), \T5
1218         vpclmulqdq      $0x11, \T5, \T1, \T3
1219         vpxor           \T3, \T4, \T4
1220         vpclmulqdq      $0x00, \T5, \T1, \T3
1221         vpxor           \T3, \T7, \T7
1222
1223         vpshufd         $0b01001110, \T1, \T3
1224         vpxor           \T1, \T3, \T3
1225         vmovdqa         HashKey_3_k(arg1), \T5
1226         vpclmulqdq      $0x10, \T5, \T3, \T3
1227         vpxor           \T3, \T6, \T6
1228
1229
1230                 vmovdqu 16*8(arg1), \T1
1231                 vaesenc \T1, \XMM1, \XMM1
1232                 vaesenc \T1, \XMM2, \XMM2
1233                 vaesenc \T1, \XMM3, \XMM3
1234                 vaesenc \T1, \XMM4, \XMM4
1235                 vaesenc \T1, \XMM5, \XMM5
1236                 vaesenc \T1, \XMM6, \XMM6
1237                 vaesenc \T1, \XMM7, \XMM7
1238                 vaesenc \T1, \XMM8, \XMM8
1239
1240         vmovdqa         TMP7(%rsp), \T1
1241         vmovdqa         HashKey_2(arg1), \T5
1242         vpclmulqdq      $0x11, \T5, \T1, \T3
1243         vpxor           \T3, \T4, \T4
1244         vpclmulqdq      $0x00, \T5, \T1, \T3
1245         vpxor           \T3, \T7, \T7
1246
1247         vpshufd         $0b01001110, \T1, \T3
1248         vpxor           \T1, \T3, \T3
1249         vmovdqa         HashKey_2_k(arg1), \T5
1250         vpclmulqdq      $0x10, \T5, \T3, \T3
1251         vpxor           \T3, \T6, \T6
1252
1253         #######################################################################
1254
1255                 vmovdqu 16*9(arg1), \T5
1256                 vaesenc \T5, \XMM1, \XMM1
1257                 vaesenc \T5, \XMM2, \XMM2
1258                 vaesenc \T5, \XMM3, \XMM3
1259                 vaesenc \T5, \XMM4, \XMM4
1260                 vaesenc \T5, \XMM5, \XMM5
1261                 vaesenc \T5, \XMM6, \XMM6
1262                 vaesenc \T5, \XMM7, \XMM7
1263                 vaesenc \T5, \XMM8, \XMM8
1264
1265         vmovdqa         TMP8(%rsp), \T1
1266         vmovdqa         HashKey(arg1), \T5
1267         vpclmulqdq      $0x11, \T5, \T1, \T3
1268         vpxor           \T3, \T4, \T4
1269         vpclmulqdq      $0x00, \T5, \T1, \T3
1270         vpxor           \T3, \T7, \T7
1271
1272         vpshufd         $0b01001110, \T1, \T3
1273         vpxor           \T1, \T3, \T3
1274         vmovdqa         HashKey_k(arg1), \T5
1275         vpclmulqdq      $0x10, \T5, \T3, \T3
1276         vpxor           \T3, \T6, \T6
1277
1278         vpxor           \T4, \T6, \T6
1279         vpxor           \T7, \T6, \T6
1280
1281                 vmovdqu 16*10(arg1), \T5
1282
1283         i = 0
1284         j = 1
1285         setreg
1286 .rep 8
1287                 vpxor   16*i(arg3, %r11), \T5, \T2
1288                 .if \ENC_DEC == ENC
1289                 vaesenclast     \T2, reg_j, reg_j
1290                 .else
1291                 vaesenclast     \T2, reg_j, \T3
1292                 vmovdqu 16*i(arg3, %r11), reg_j
1293                 vmovdqu \T3, 16*i(arg2, %r11)
1294                 .endif
1295         i = (i+1)
1296         j = (j+1)
1297         setreg
1298 .endr
1299         #######################################################################
1300
1301
1302         vpslldq $8, \T6, \T3                            # shift-L T3 2 DWs
1303         vpsrldq $8, \T6, \T6                            # shift-R T2 2 DWs
1304         vpxor   \T3, \T7, \T7
1305         vpxor   \T4, \T6, \T6                           # accumulate the results in T6:T7
1306
1307
1308
1309         #######################################################################
1310         #first phase of the reduction
1311         #######################################################################
1312         vpslld  $31, \T7, \T2                           # packed right shifting << 31
1313         vpslld  $30, \T7, \T3                           # packed right shifting shift << 30
1314         vpslld  $25, \T7, \T4                           # packed right shifting shift << 25
1315
1316         vpxor   \T3, \T2, \T2                           # xor the shifted versions
1317         vpxor   \T4, \T2, \T2
1318
1319         vpsrldq $4, \T2, \T1                            # shift-R T1 1 DW
1320
1321         vpslldq $12, \T2, \T2                           # shift-L T2 3 DWs
1322         vpxor   \T2, \T7, \T7                           # first phase of the reduction complete
1323         #######################################################################
1324                 .if \ENC_DEC == ENC
1325                 vmovdqu  \XMM1, 16*0(arg2,%r11)         # Write to the Ciphertext buffer
1326                 vmovdqu  \XMM2, 16*1(arg2,%r11)         # Write to the Ciphertext buffer
1327                 vmovdqu  \XMM3, 16*2(arg2,%r11)         # Write to the Ciphertext buffer
1328                 vmovdqu  \XMM4, 16*3(arg2,%r11)         # Write to the Ciphertext buffer
1329                 vmovdqu  \XMM5, 16*4(arg2,%r11)         # Write to the Ciphertext buffer
1330                 vmovdqu  \XMM6, 16*5(arg2,%r11)         # Write to the Ciphertext buffer
1331                 vmovdqu  \XMM7, 16*6(arg2,%r11)         # Write to the Ciphertext buffer
1332                 vmovdqu  \XMM8, 16*7(arg2,%r11)         # Write to the Ciphertext buffer
1333                 .endif
1334
1335         #######################################################################
1336         #second phase of the reduction
1337         vpsrld  $1, \T7, \T2                            # packed left shifting >> 1
1338         vpsrld  $2, \T7, \T3                            # packed left shifting >> 2
1339         vpsrld  $7, \T7, \T4                            # packed left shifting >> 7
1340         vpxor   \T3, \T2, \T2                           # xor the shifted versions
1341         vpxor   \T4, \T2, \T2
1342
1343         vpxor   \T1, \T2, \T2
1344         vpxor   \T2, \T7, \T7
1345         vpxor   \T7, \T6, \T6                           # the result is in T6
1346         #######################################################################
1347
1348                 vpshufb SHUF_MASK(%rip), \XMM1, \XMM1   # perform a 16Byte swap
1349                 vpshufb SHUF_MASK(%rip), \XMM2, \XMM2   # perform a 16Byte swap
1350                 vpshufb SHUF_MASK(%rip), \XMM3, \XMM3   # perform a 16Byte swap
1351                 vpshufb SHUF_MASK(%rip), \XMM4, \XMM4   # perform a 16Byte swap
1352                 vpshufb SHUF_MASK(%rip), \XMM5, \XMM5   # perform a 16Byte swap
1353                 vpshufb SHUF_MASK(%rip), \XMM6, \XMM6   # perform a 16Byte swap
1354                 vpshufb SHUF_MASK(%rip), \XMM7, \XMM7   # perform a 16Byte swap
1355                 vpshufb SHUF_MASK(%rip), \XMM8, \XMM8   # perform a 16Byte swap
1356
1357
1358         vpxor   \T6, \XMM1, \XMM1
1359
1360
1361
1362 .endm
1363
1364
1365 # GHASH the last 4 ciphertext blocks.
1366 .macro  GHASH_LAST_8_AVX T1 T2 T3 T4 T5 T6 T7 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8
1367
1368         ## Karatsuba Method
1369
1370
1371         vpshufd         $0b01001110, \XMM1, \T2
1372         vpxor           \XMM1, \T2, \T2
1373         vmovdqa         HashKey_8(arg1), \T5
1374         vpclmulqdq      $0x11, \T5, \XMM1, \T6
1375         vpclmulqdq      $0x00, \T5, \XMM1, \T7
1376
1377         vmovdqa         HashKey_8_k(arg1), \T3
1378         vpclmulqdq      $0x00, \T3, \T2, \XMM1
1379
1380         ######################
1381
1382         vpshufd         $0b01001110, \XMM2, \T2
1383         vpxor           \XMM2, \T2, \T2
1384         vmovdqa         HashKey_7(arg1), \T5
1385         vpclmulqdq      $0x11, \T5, \XMM2, \T4
1386         vpxor           \T4, \T6, \T6
1387
1388         vpclmulqdq      $0x00, \T5, \XMM2, \T4
1389         vpxor           \T4, \T7, \T7
1390
1391         vmovdqa         HashKey_7_k(arg1), \T3
1392         vpclmulqdq      $0x00, \T3, \T2, \T2
1393         vpxor           \T2, \XMM1, \XMM1
1394
1395         ######################
1396
1397         vpshufd         $0b01001110, \XMM3, \T2
1398         vpxor           \XMM3, \T2, \T2
1399         vmovdqa         HashKey_6(arg1), \T5
1400         vpclmulqdq      $0x11, \T5, \XMM3, \T4
1401         vpxor           \T4, \T6, \T6
1402
1403         vpclmulqdq      $0x00, \T5, \XMM3, \T4
1404         vpxor           \T4, \T7, \T7
1405
1406         vmovdqa         HashKey_6_k(arg1), \T3
1407         vpclmulqdq      $0x00, \T3, \T2, \T2
1408         vpxor           \T2, \XMM1, \XMM1
1409
1410         ######################
1411
1412         vpshufd         $0b01001110, \XMM4, \T2
1413         vpxor           \XMM4, \T2, \T2
1414         vmovdqa         HashKey_5(arg1), \T5
1415         vpclmulqdq      $0x11, \T5, \XMM4, \T4
1416         vpxor           \T4, \T6, \T6
1417
1418         vpclmulqdq      $0x00, \T5, \XMM4, \T4
1419         vpxor           \T4, \T7, \T7
1420
1421         vmovdqa         HashKey_5_k(arg1), \T3
1422         vpclmulqdq      $0x00, \T3, \T2, \T2
1423         vpxor           \T2, \XMM1, \XMM1
1424
1425         ######################
1426
1427         vpshufd         $0b01001110, \XMM5, \T2
1428         vpxor           \XMM5, \T2, \T2
1429         vmovdqa         HashKey_4(arg1), \T5
1430         vpclmulqdq      $0x11, \T5, \XMM5, \T4
1431         vpxor           \T4, \T6, \T6
1432
1433         vpclmulqdq      $0x00, \T5, \XMM5, \T4
1434         vpxor           \T4, \T7, \T7
1435
1436         vmovdqa         HashKey_4_k(arg1), \T3
1437         vpclmulqdq      $0x00, \T3, \T2, \T2
1438         vpxor           \T2, \XMM1, \XMM1
1439
1440         ######################
1441
1442         vpshufd         $0b01001110, \XMM6, \T2
1443         vpxor           \XMM6, \T2, \T2
1444         vmovdqa         HashKey_3(arg1), \T5
1445         vpclmulqdq      $0x11, \T5, \XMM6, \T4
1446         vpxor           \T4, \T6, \T6
1447
1448         vpclmulqdq      $0x00, \T5, \XMM6, \T4
1449         vpxor           \T4, \T7, \T7
1450
1451         vmovdqa         HashKey_3_k(arg1), \T3
1452         vpclmulqdq      $0x00, \T3, \T2, \T2
1453         vpxor           \T2, \XMM1, \XMM1
1454
1455         ######################
1456
1457         vpshufd         $0b01001110, \XMM7, \T2
1458         vpxor           \XMM7, \T2, \T2
1459         vmovdqa         HashKey_2(arg1), \T5
1460         vpclmulqdq      $0x11, \T5, \XMM7, \T4
1461         vpxor           \T4, \T6, \T6
1462
1463         vpclmulqdq      $0x00, \T5, \XMM7, \T4
1464         vpxor           \T4, \T7, \T7
1465
1466         vmovdqa         HashKey_2_k(arg1), \T3
1467         vpclmulqdq      $0x00, \T3, \T2, \T2
1468         vpxor           \T2, \XMM1, \XMM1
1469
1470         ######################
1471
1472         vpshufd         $0b01001110, \XMM8, \T2
1473         vpxor           \XMM8, \T2, \T2
1474         vmovdqa         HashKey(arg1), \T5
1475         vpclmulqdq      $0x11, \T5, \XMM8, \T4
1476         vpxor           \T4, \T6, \T6
1477
1478         vpclmulqdq      $0x00, \T5, \XMM8, \T4
1479         vpxor           \T4, \T7, \T7
1480
1481         vmovdqa         HashKey_k(arg1), \T3
1482         vpclmulqdq      $0x00, \T3, \T2, \T2
1483
1484         vpxor           \T2, \XMM1, \XMM1
1485         vpxor           \T6, \XMM1, \XMM1
1486         vpxor           \T7, \XMM1, \T2
1487
1488
1489
1490
1491         vpslldq $8, \T2, \T4
1492         vpsrldq $8, \T2, \T2
1493
1494         vpxor   \T4, \T7, \T7
1495         vpxor   \T2, \T6, \T6   # <T6:T7> holds the result of
1496                                 # the accumulated carry-less multiplications
1497
1498         #######################################################################
1499         #first phase of the reduction
1500         vpslld  $31, \T7, \T2   # packed right shifting << 31
1501         vpslld  $30, \T7, \T3   # packed right shifting shift << 30
1502         vpslld  $25, \T7, \T4   # packed right shifting shift << 25
1503
1504         vpxor   \T3, \T2, \T2   # xor the shifted versions
1505         vpxor   \T4, \T2, \T2
1506
1507         vpsrldq $4, \T2, \T1    # shift-R T1 1 DW
1508
1509         vpslldq $12, \T2, \T2   # shift-L T2 3 DWs
1510         vpxor   \T2, \T7, \T7   # first phase of the reduction complete
1511         #######################################################################
1512
1513
1514         #second phase of the reduction
1515         vpsrld  $1, \T7, \T2    # packed left shifting >> 1
1516         vpsrld  $2, \T7, \T3    # packed left shifting >> 2
1517         vpsrld  $7, \T7, \T4    # packed left shifting >> 7
1518         vpxor   \T3, \T2, \T2   # xor the shifted versions
1519         vpxor   \T4, \T2, \T2
1520
1521         vpxor   \T1, \T2, \T2
1522         vpxor   \T2, \T7, \T7
1523         vpxor   \T7, \T6, \T6   # the result is in T6
1524
1525 .endm
1526
1527 #############################################################
1528 #void   aesni_gcm_precomp_avx_gen2
1529 #        (gcm_data     *my_ctx_data,
1530 #        u8     *hash_subkey)# /* H, the Hash sub key input. Data starts on a 16-byte boundary. */
1531 #############################################################
1532 ENTRY(aesni_gcm_precomp_avx_gen2)
1533         #the number of pushes must equal STACK_OFFSET
1534         push    %r12
1535         push    %r13
1536         push    %r14
1537         push    %r15
1538
1539         mov     %rsp, %r14
1540
1541
1542
1543         sub     $VARIABLE_OFFSET, %rsp
1544         and     $~63, %rsp                  # align rsp to 64 bytes
1545
1546         vmovdqu  (arg2), %xmm6              # xmm6 = HashKey
1547
1548         vpshufb  SHUF_MASK(%rip), %xmm6, %xmm6
1549         ###############  PRECOMPUTATION of HashKey<<1 mod poly from the HashKey
1550         vmovdqa  %xmm6, %xmm2
1551         vpsllq   $1, %xmm6, %xmm6
1552         vpsrlq   $63, %xmm2, %xmm2
1553         vmovdqa  %xmm2, %xmm1
1554         vpslldq  $8, %xmm2, %xmm2
1555         vpsrldq  $8, %xmm1, %xmm1
1556         vpor     %xmm2, %xmm6, %xmm6
1557         #reduction
1558         vpshufd  $0b00100100, %xmm1, %xmm2
1559         vpcmpeqd TWOONE(%rip), %xmm2, %xmm2
1560         vpand    POLY(%rip), %xmm2, %xmm2
1561         vpxor    %xmm2, %xmm6, %xmm6        # xmm6 holds the HashKey<<1 mod poly
1562         #######################################################################
1563         vmovdqa  %xmm6, HashKey(arg1)       # store HashKey<<1 mod poly
1564
1565
1566         PRECOMPUTE_AVX  %xmm6, %xmm0, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5
1567
1568         mov     %r14, %rsp
1569
1570         pop     %r15
1571         pop     %r14
1572         pop     %r13
1573         pop     %r12
1574         ret
1575 ENDPROC(aesni_gcm_precomp_avx_gen2)
1576
1577 ###############################################################################
1578 #void   aesni_gcm_enc_avx_gen2(
1579 #        gcm_data        *my_ctx_data,     /* aligned to 16 Bytes */
1580 #        u8      *out, /* Ciphertext output. Encrypt in-place is allowed.  */
1581 #        const   u8 *in, /* Plaintext input */
1582 #        u64     plaintext_len, /* Length of data in Bytes for encryption. */
1583 #        u8      *iv, /* Pre-counter block j0: 4 byte salt
1584 #                       (from Security Association) concatenated with 8 byte
1585 #                       Initialisation Vector (from IPSec ESP Payload)
1586 #                       concatenated with 0x00000001. 16-byte aligned pointer. */
1587 #        const   u8 *aad, /* Additional Authentication Data (AAD)*/
1588 #        u64     aad_len, /* Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 Bytes */
1589 #        u8      *auth_tag, /* Authenticated Tag output. */
1590 #        u64     auth_tag_len)# /* Authenticated Tag Length in bytes.
1591 #                               Valid values are 16 (most likely), 12 or 8. */
1592 ###############################################################################
1593 ENTRY(aesni_gcm_enc_avx_gen2)
1594         GCM_ENC_DEC INITIAL_BLOCKS_AVX GHASH_8_ENCRYPT_8_PARALLEL_AVX GHASH_LAST_8_AVX GHASH_MUL_AVX ENC
1595         ret
1596 ENDPROC(aesni_gcm_enc_avx_gen2)
1597
1598 ###############################################################################
1599 #void   aesni_gcm_dec_avx_gen2(
1600 #        gcm_data        *my_ctx_data,     /* aligned to 16 Bytes */
1601 #        u8      *out, /* Plaintext output. Decrypt in-place is allowed.  */
1602 #        const   u8 *in, /* Ciphertext input */
1603 #        u64     plaintext_len, /* Length of data in Bytes for encryption. */
1604 #        u8      *iv, /* Pre-counter block j0: 4 byte salt
1605 #                       (from Security Association) concatenated with 8 byte
1606 #                       Initialisation Vector (from IPSec ESP Payload)
1607 #                       concatenated with 0x00000001. 16-byte aligned pointer. */
1608 #        const   u8 *aad, /* Additional Authentication Data (AAD)*/
1609 #        u64     aad_len, /* Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 Bytes */
1610 #        u8      *auth_tag, /* Authenticated Tag output. */
1611 #        u64     auth_tag_len)# /* Authenticated Tag Length in bytes.
1612 #                               Valid values are 16 (most likely), 12 or 8. */
1613 ###############################################################################
1614 ENTRY(aesni_gcm_dec_avx_gen2)
1615         GCM_ENC_DEC INITIAL_BLOCKS_AVX GHASH_8_ENCRYPT_8_PARALLEL_AVX GHASH_LAST_8_AVX GHASH_MUL_AVX DEC
1616         ret
1617 ENDPROC(aesni_gcm_dec_avx_gen2)
1618 #endif /* CONFIG_AS_AVX */
1619
1620 #ifdef CONFIG_AS_AVX2
1621 ###############################################################################
1622 # GHASH_MUL MACRO to implement: Data*HashKey mod (128,127,126,121,0)
1623 # Input: A and B (128-bits each, bit-reflected)
1624 # Output: C = A*B*x mod poly, (i.e. >>1 )
1625 # To compute GH = GH*HashKey mod poly, give HK = HashKey<<1 mod poly as input
1626 # GH = GH * HK * x mod poly which is equivalent to GH*HashKey mod poly.
1627 ###############################################################################
1628 .macro  GHASH_MUL_AVX2 GH HK T1 T2 T3 T4 T5
1629
1630         vpclmulqdq      $0x11,\HK,\GH,\T1      # T1 = a1*b1
1631         vpclmulqdq      $0x00,\HK,\GH,\T2      # T2 = a0*b0
1632         vpclmulqdq      $0x01,\HK,\GH,\T3      # T3 = a1*b0
1633         vpclmulqdq      $0x10,\HK,\GH,\GH      # GH = a0*b1
1634         vpxor           \T3, \GH, \GH
1635
1636
1637         vpsrldq         $8 , \GH, \T3          # shift-R GH 2 DWs
1638         vpslldq         $8 , \GH, \GH          # shift-L GH 2 DWs
1639
1640         vpxor           \T3, \T1, \T1
1641         vpxor           \T2, \GH, \GH
1642
1643         #######################################################################
1644         #first phase of the reduction
1645         vmovdqa         POLY2(%rip), \T3
1646
1647         vpclmulqdq      $0x01, \GH, \T3, \T2
1648         vpslldq         $8, \T2, \T2           # shift-L T2 2 DWs
1649
1650         vpxor           \T2, \GH, \GH          # first phase of the reduction complete
1651         #######################################################################
1652         #second phase of the reduction
1653         vpclmulqdq      $0x00, \GH, \T3, \T2
1654         vpsrldq         $4, \T2, \T2           # shift-R T2 1 DW (Shift-R only 1-DW to obtain 2-DWs shift-R)
1655
1656         vpclmulqdq      $0x10, \GH, \T3, \GH
1657         vpslldq         $4, \GH, \GH           # shift-L GH 1 DW (Shift-L 1-DW to obtain result with no shifts)
1658
1659         vpxor           \T2, \GH, \GH          # second phase of the reduction complete
1660         #######################################################################
1661         vpxor           \T1, \GH, \GH          # the result is in GH
1662
1663
1664 .endm
1665
1666 .macro PRECOMPUTE_AVX2 HK T1 T2 T3 T4 T5 T6
1667
1668         # Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
1669         vmovdqa  \HK, \T5
1670         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^2<<1 mod poly
1671         vmovdqa  \T5, HashKey_2(arg1)                       #  [HashKey_2] = HashKey^2<<1 mod poly
1672
1673         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^3<<1 mod poly
1674         vmovdqa  \T5, HashKey_3(arg1)
1675
1676         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^4<<1 mod poly
1677         vmovdqa  \T5, HashKey_4(arg1)
1678
1679         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^5<<1 mod poly
1680         vmovdqa  \T5, HashKey_5(arg1)
1681
1682         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^6<<1 mod poly
1683         vmovdqa  \T5, HashKey_6(arg1)
1684
1685         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^7<<1 mod poly
1686         vmovdqa  \T5, HashKey_7(arg1)
1687
1688         GHASH_MUL_AVX2 \T5, \HK, \T1, \T3, \T4, \T6, \T2    #  T5 = HashKey^8<<1 mod poly
1689         vmovdqa  \T5, HashKey_8(arg1)
1690
1691 .endm
1692
1693
1694 ## if a = number of total plaintext bytes
1695 ## b = floor(a/16)
1696 ## num_initial_blocks = b mod 4#
1697 ## encrypt the initial num_initial_blocks blocks and apply ghash on the ciphertext
1698 ## r10, r11, r12, rax are clobbered
1699 ## arg1, arg2, arg3, r14 are used as a pointer only, not modified
1700
1701 .macro INITIAL_BLOCKS_AVX2 num_initial_blocks T1 T2 T3 T4 T5 CTR XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 T6 T_key ENC_DEC VER
1702         i = (8-\num_initial_blocks)
1703         j = 0
1704         setreg
1705
1706         mov     arg6, %r10                       # r10 = AAD
1707         mov     arg7, %r12                       # r12 = aadLen
1708
1709
1710         mov     %r12, %r11
1711
1712         vpxor   reg_j, reg_j, reg_j
1713         vpxor   reg_i, reg_i, reg_i
1714
1715         cmp     $16, %r11
1716         jl      _get_AAD_rest8\@
1717 _get_AAD_blocks\@:
1718         vmovdqu (%r10), reg_i
1719         vpshufb SHUF_MASK(%rip), reg_i, reg_i
1720         vpxor   reg_i, reg_j, reg_j
1721         GHASH_MUL_AVX2      reg_j, \T2, \T1, \T3, \T4, \T5, \T6
1722         add     $16, %r10
1723         sub     $16, %r12
1724         sub     $16, %r11
1725         cmp     $16, %r11
1726         jge     _get_AAD_blocks\@
1727         vmovdqu reg_j, reg_i
1728         cmp     $0, %r11
1729         je      _get_AAD_done\@
1730
1731         vpxor   reg_i, reg_i, reg_i
1732
1733         /* read the last <16B of AAD. since we have at least 4B of
1734         data right after the AAD (the ICV, and maybe some CT), we can
1735         read 4B/8B blocks safely, and then get rid of the extra stuff */
1736 _get_AAD_rest8\@:
1737         cmp     $4, %r11
1738         jle     _get_AAD_rest4\@
1739         movq    (%r10), \T1
1740         add     $8, %r10
1741         sub     $8, %r11
1742         vpslldq $8, \T1, \T1
1743         vpsrldq $8, reg_i, reg_i
1744         vpxor   \T1, reg_i, reg_i
1745         jmp     _get_AAD_rest8\@
1746 _get_AAD_rest4\@:
1747         cmp     $0, %r11
1748         jle     _get_AAD_rest0\@
1749         mov     (%r10), %eax
1750         movq    %rax, \T1
1751         add     $4, %r10
1752         sub     $4, %r11
1753         vpslldq $12, \T1, \T1
1754         vpsrldq $4, reg_i, reg_i
1755         vpxor   \T1, reg_i, reg_i
1756 _get_AAD_rest0\@:
1757         /* finalize: shift out the extra bytes we read, and align
1758         left. since pslldq can only shift by an immediate, we use
1759         vpshufb and an array of shuffle masks */
1760         movq    %r12, %r11
1761         salq    $4, %r11
1762         movdqu  aad_shift_arr(%r11), \T1
1763         vpshufb \T1, reg_i, reg_i
1764 _get_AAD_rest_final\@:
1765         vpshufb SHUF_MASK(%rip), reg_i, reg_i
1766         vpxor   reg_j, reg_i, reg_i
1767         GHASH_MUL_AVX2      reg_i, \T2, \T1, \T3, \T4, \T5, \T6
1768
1769 _get_AAD_done\@:
1770         # initialize the data pointer offset as zero
1771         xor     %r11d, %r11d
1772
1773         # start AES for num_initial_blocks blocks
1774         mov     arg5, %rax                     # rax = *Y0
1775         vmovdqu (%rax), \CTR                   # CTR = Y0
1776         vpshufb SHUF_MASK(%rip), \CTR, \CTR
1777
1778
1779         i = (9-\num_initial_blocks)
1780         setreg
1781 .rep \num_initial_blocks
1782                 vpaddd  ONE(%rip), \CTR, \CTR   # INCR Y0
1783                 vmovdqa \CTR, reg_i
1784                 vpshufb SHUF_MASK(%rip), reg_i, reg_i     # perform a 16Byte swap
1785         i = (i+1)
1786         setreg
1787 .endr
1788
1789         vmovdqa  (arg1), \T_key
1790         i = (9-\num_initial_blocks)
1791         setreg
1792 .rep \num_initial_blocks
1793                 vpxor   \T_key, reg_i, reg_i
1794         i = (i+1)
1795         setreg
1796 .endr
1797
1798         j = 1
1799         setreg
1800 .rep 9
1801         vmovdqa  16*j(arg1), \T_key
1802         i = (9-\num_initial_blocks)
1803         setreg
1804 .rep \num_initial_blocks
1805         vaesenc \T_key, reg_i, reg_i
1806         i = (i+1)
1807         setreg
1808 .endr
1809
1810         j = (j+1)
1811         setreg
1812 .endr
1813
1814
1815         vmovdqa  16*10(arg1), \T_key
1816         i = (9-\num_initial_blocks)
1817         setreg
1818 .rep \num_initial_blocks
1819         vaesenclast      \T_key, reg_i, reg_i
1820         i = (i+1)
1821         setreg
1822 .endr
1823
1824         i = (9-\num_initial_blocks)
1825         setreg
1826 .rep \num_initial_blocks
1827                 vmovdqu (arg3, %r11), \T1
1828                 vpxor   \T1, reg_i, reg_i
1829                 vmovdqu reg_i, (arg2 , %r11)           # write back ciphertext for
1830                                                        # num_initial_blocks blocks
1831                 add     $16, %r11
1832 .if  \ENC_DEC == DEC
1833                 vmovdqa \T1, reg_i
1834 .endif
1835                 vpshufb SHUF_MASK(%rip), reg_i, reg_i  # prepare ciphertext for GHASH computations
1836         i = (i+1)
1837         setreg
1838 .endr
1839
1840
1841         i = (8-\num_initial_blocks)
1842         j = (9-\num_initial_blocks)
1843         setreg
1844
1845 .rep \num_initial_blocks
1846         vpxor    reg_i, reg_j, reg_j
1847         GHASH_MUL_AVX2       reg_j, \T2, \T1, \T3, \T4, \T5, \T6  # apply GHASH on num_initial_blocks blocks
1848         i = (i+1)
1849         j = (j+1)
1850         setreg
1851 .endr
1852         # XMM8 has the combined result here
1853
1854         vmovdqa  \XMM8, TMP1(%rsp)
1855         vmovdqa  \XMM8, \T3
1856
1857         cmp     $128, %r13
1858         jl      _initial_blocks_done\@                  # no need for precomputed constants
1859
1860 ###############################################################################
1861 # Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
1862                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1863                 vmovdqa  \CTR, \XMM1
1864                 vpshufb  SHUF_MASK(%rip), \XMM1, \XMM1  # perform a 16Byte swap
1865
1866                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1867                 vmovdqa  \CTR, \XMM2
1868                 vpshufb  SHUF_MASK(%rip), \XMM2, \XMM2  # perform a 16Byte swap
1869
1870                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1871                 vmovdqa  \CTR, \XMM3
1872                 vpshufb  SHUF_MASK(%rip), \XMM3, \XMM3  # perform a 16Byte swap
1873
1874                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1875                 vmovdqa  \CTR, \XMM4
1876                 vpshufb  SHUF_MASK(%rip), \XMM4, \XMM4  # perform a 16Byte swap
1877
1878                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1879                 vmovdqa  \CTR, \XMM5
1880                 vpshufb  SHUF_MASK(%rip), \XMM5, \XMM5  # perform a 16Byte swap
1881
1882                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1883                 vmovdqa  \CTR, \XMM6
1884                 vpshufb  SHUF_MASK(%rip), \XMM6, \XMM6  # perform a 16Byte swap
1885
1886                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1887                 vmovdqa  \CTR, \XMM7
1888                 vpshufb  SHUF_MASK(%rip), \XMM7, \XMM7  # perform a 16Byte swap
1889
1890                 vpaddd   ONE(%rip), \CTR, \CTR          # INCR Y0
1891                 vmovdqa  \CTR, \XMM8
1892                 vpshufb  SHUF_MASK(%rip), \XMM8, \XMM8  # perform a 16Byte swap
1893
1894                 vmovdqa  (arg1), \T_key
1895                 vpxor    \T_key, \XMM1, \XMM1
1896                 vpxor    \T_key, \XMM2, \XMM2
1897                 vpxor    \T_key, \XMM3, \XMM3
1898                 vpxor    \T_key, \XMM4, \XMM4
1899                 vpxor    \T_key, \XMM5, \XMM5
1900                 vpxor    \T_key, \XMM6, \XMM6
1901                 vpxor    \T_key, \XMM7, \XMM7
1902                 vpxor    \T_key, \XMM8, \XMM8
1903
1904                 i = 1
1905                 setreg
1906 .rep    9       # do 9 rounds
1907                 vmovdqa  16*i(arg1), \T_key
1908                 vaesenc  \T_key, \XMM1, \XMM1
1909                 vaesenc  \T_key, \XMM2, \XMM2
1910                 vaesenc  \T_key, \XMM3, \XMM3
1911                 vaesenc  \T_key, \XMM4, \XMM4
1912                 vaesenc  \T_key, \XMM5, \XMM5
1913                 vaesenc  \T_key, \XMM6, \XMM6
1914                 vaesenc  \T_key, \XMM7, \XMM7
1915                 vaesenc  \T_key, \XMM8, \XMM8
1916                 i = (i+1)
1917                 setreg
1918 .endr
1919
1920
1921                 vmovdqa  16*i(arg1), \T_key
1922                 vaesenclast  \T_key, \XMM1, \XMM1
1923                 vaesenclast  \T_key, \XMM2, \XMM2
1924                 vaesenclast  \T_key, \XMM3, \XMM3
1925                 vaesenclast  \T_key, \XMM4, \XMM4
1926                 vaesenclast  \T_key, \XMM5, \XMM5
1927                 vaesenclast  \T_key, \XMM6, \XMM6
1928                 vaesenclast  \T_key, \XMM7, \XMM7
1929                 vaesenclast  \T_key, \XMM8, \XMM8
1930
1931                 vmovdqu  (arg3, %r11), \T1
1932                 vpxor    \T1, \XMM1, \XMM1
1933                 vmovdqu  \XMM1, (arg2 , %r11)
1934                 .if   \ENC_DEC == DEC
1935                 vmovdqa  \T1, \XMM1
1936                 .endif
1937
1938                 vmovdqu  16*1(arg3, %r11), \T1
1939                 vpxor    \T1, \XMM2, \XMM2
1940                 vmovdqu  \XMM2, 16*1(arg2 , %r11)
1941                 .if   \ENC_DEC == DEC
1942                 vmovdqa  \T1, \XMM2
1943                 .endif
1944
1945                 vmovdqu  16*2(arg3, %r11), \T1
1946                 vpxor    \T1, \XMM3, \XMM3
1947                 vmovdqu  \XMM3, 16*2(arg2 , %r11)
1948                 .if   \ENC_DEC == DEC
1949                 vmovdqa  \T1, \XMM3
1950                 .endif
1951
1952                 vmovdqu  16*3(arg3, %r11), \T1
1953                 vpxor    \T1, \XMM4, \XMM4
1954                 vmovdqu  \XMM4, 16*3(arg2 , %r11)
1955                 .if   \ENC_DEC == DEC
1956                 vmovdqa  \T1, \XMM4
1957                 .endif
1958
1959                 vmovdqu  16*4(arg3, %r11), \T1
1960                 vpxor    \T1, \XMM5, \XMM5
1961                 vmovdqu  \XMM5, 16*4(arg2 , %r11)
1962                 .if   \ENC_DEC == DEC
1963                 vmovdqa  \T1, \XMM5
1964                 .endif
1965
1966                 vmovdqu  16*5(arg3, %r11), \T1
1967                 vpxor    \T1, \XMM6, \XMM6
1968                 vmovdqu  \XMM6, 16*5(arg2 , %r11)
1969                 .if   \ENC_DEC == DEC
1970                 vmovdqa  \T1, \XMM6
1971                 .endif
1972
1973                 vmovdqu  16*6(arg3, %r11), \T1
1974                 vpxor    \T1, \XMM7, \XMM7
1975                 vmovdqu  \XMM7, 16*6(arg2 , %r11)
1976                 .if   \ENC_DEC == DEC
1977                 vmovdqa  \T1, \XMM7
1978                 .endif
1979
1980                 vmovdqu  16*7(arg3, %r11), \T1
1981                 vpxor    \T1, \XMM8, \XMM8
1982                 vmovdqu  \XMM8, 16*7(arg2 , %r11)
1983                 .if   \ENC_DEC == DEC
1984                 vmovdqa  \T1, \XMM8
1985                 .endif
1986
1987                 add     $128, %r11
1988
1989                 vpshufb  SHUF_MASK(%rip), \XMM1, \XMM1     # perform a 16Byte swap
1990                 vpxor    TMP1(%rsp), \XMM1, \XMM1          # combine GHASHed value with
1991                                                            # the corresponding ciphertext
1992                 vpshufb  SHUF_MASK(%rip), \XMM2, \XMM2     # perform a 16Byte swap
1993                 vpshufb  SHUF_MASK(%rip), \XMM3, \XMM3     # perform a 16Byte swap
1994                 vpshufb  SHUF_MASK(%rip), \XMM4, \XMM4     # perform a 16Byte swap
1995                 vpshufb  SHUF_MASK(%rip), \XMM5, \XMM5     # perform a 16Byte swap
1996                 vpshufb  SHUF_MASK(%rip), \XMM6, \XMM6     # perform a 16Byte swap
1997                 vpshufb  SHUF_MASK(%rip), \XMM7, \XMM7     # perform a 16Byte swap
1998                 vpshufb  SHUF_MASK(%rip), \XMM8, \XMM8     # perform a 16Byte swap
1999
2000 ###############################################################################
2001
2002 _initial_blocks_done\@:
2003
2004
2005 .endm
2006
2007
2008
2009 # encrypt 8 blocks at a time
2010 # ghash the 8 previously encrypted ciphertext blocks
2011 # arg1, arg2, arg3 are used as pointers only, not modified
2012 # r11 is the data offset value
2013 .macro GHASH_8_ENCRYPT_8_PARALLEL_AVX2 T1 T2 T3 T4 T5 T6 CTR XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 T7 loop_idx ENC_DEC
2014
2015         vmovdqa \XMM1, \T2
2016         vmovdqa \XMM2, TMP2(%rsp)
2017         vmovdqa \XMM3, TMP3(%rsp)
2018         vmovdqa \XMM4, TMP4(%rsp)
2019         vmovdqa \XMM5, TMP5(%rsp)
2020         vmovdqa \XMM6, TMP6(%rsp)
2021         vmovdqa \XMM7, TMP7(%rsp)
2022         vmovdqa \XMM8, TMP8(%rsp)
2023
2024 .if \loop_idx == in_order
2025                 vpaddd  ONE(%rip), \CTR, \XMM1            # INCR CNT
2026                 vpaddd  ONE(%rip), \XMM1, \XMM2
2027                 vpaddd  ONE(%rip), \XMM2, \XMM3
2028                 vpaddd  ONE(%rip), \XMM3, \XMM4
2029                 vpaddd  ONE(%rip), \XMM4, \XMM5
2030                 vpaddd  ONE(%rip), \XMM5, \XMM6
2031                 vpaddd  ONE(%rip), \XMM6, \XMM7
2032                 vpaddd  ONE(%rip), \XMM7, \XMM8
2033                 vmovdqa \XMM8, \CTR
2034
2035                 vpshufb SHUF_MASK(%rip), \XMM1, \XMM1     # perform a 16Byte swap
2036                 vpshufb SHUF_MASK(%rip), \XMM2, \XMM2     # perform a 16Byte swap
2037                 vpshufb SHUF_MASK(%rip), \XMM3, \XMM3     # perform a 16Byte swap
2038                 vpshufb SHUF_MASK(%rip), \XMM4, \XMM4     # perform a 16Byte swap
2039                 vpshufb SHUF_MASK(%rip), \XMM5, \XMM5     # perform a 16Byte swap
2040                 vpshufb SHUF_MASK(%rip), \XMM6, \XMM6     # perform a 16Byte swap
2041                 vpshufb SHUF_MASK(%rip), \XMM7, \XMM7     # perform a 16Byte swap
2042                 vpshufb SHUF_MASK(%rip), \XMM8, \XMM8     # perform a 16Byte swap
2043 .else
2044                 vpaddd  ONEf(%rip), \CTR, \XMM1            # INCR CNT
2045                 vpaddd  ONEf(%rip), \XMM1, \XMM2
2046                 vpaddd  ONEf(%rip), \XMM2, \XMM3
2047                 vpaddd  ONEf(%rip), \XMM3, \XMM4
2048                 vpaddd  ONEf(%rip), \XMM4, \XMM5
2049                 vpaddd  ONEf(%rip), \XMM5, \XMM6
2050                 vpaddd  ONEf(%rip), \XMM6, \XMM7
2051                 vpaddd  ONEf(%rip), \XMM7, \XMM8
2052                 vmovdqa \XMM8, \CTR
2053 .endif
2054
2055
2056         #######################################################################
2057
2058                 vmovdqu (arg1), \T1
2059                 vpxor   \T1, \XMM1, \XMM1
2060                 vpxor   \T1, \XMM2, \XMM2
2061                 vpxor   \T1, \XMM3, \XMM3
2062                 vpxor   \T1, \XMM4, \XMM4
2063                 vpxor   \T1, \XMM5, \XMM5
2064                 vpxor   \T1, \XMM6, \XMM6
2065                 vpxor   \T1, \XMM7, \XMM7
2066                 vpxor   \T1, \XMM8, \XMM8
2067
2068         #######################################################################
2069
2070
2071
2072
2073
2074                 vmovdqu 16*1(arg1), \T1
2075                 vaesenc \T1, \XMM1, \XMM1
2076                 vaesenc \T1, \XMM2, \XMM2
2077                 vaesenc \T1, \XMM3, \XMM3
2078                 vaesenc \T1, \XMM4, \XMM4
2079                 vaesenc \T1, \XMM5, \XMM5
2080                 vaesenc \T1, \XMM6, \XMM6
2081                 vaesenc \T1, \XMM7, \XMM7
2082                 vaesenc \T1, \XMM8, \XMM8
2083
2084                 vmovdqu 16*2(arg1), \T1
2085                 vaesenc \T1, \XMM1, \XMM1
2086                 vaesenc \T1, \XMM2, \XMM2
2087                 vaesenc \T1, \XMM3, \XMM3
2088                 vaesenc \T1, \XMM4, \XMM4
2089                 vaesenc \T1, \XMM5, \XMM5
2090                 vaesenc \T1, \XMM6, \XMM6
2091                 vaesenc \T1, \XMM7, \XMM7
2092                 vaesenc \T1, \XMM8, \XMM8
2093
2094
2095         #######################################################################
2096
2097         vmovdqa         HashKey_8(arg1), \T5
2098         vpclmulqdq      $0x11, \T5, \T2, \T4              # T4 = a1*b1
2099         vpclmulqdq      $0x00, \T5, \T2, \T7              # T7 = a0*b0
2100         vpclmulqdq      $0x01, \T5, \T2, \T6              # T6 = a1*b0
2101         vpclmulqdq      $0x10, \T5, \T2, \T5              # T5 = a0*b1
2102         vpxor           \T5, \T6, \T6
2103
2104                 vmovdqu 16*3(arg1), \T1
2105                 vaesenc \T1, \XMM1, \XMM1
2106                 vaesenc \T1, \XMM2, \XMM2
2107                 vaesenc \T1, \XMM3, \XMM3
2108                 vaesenc \T1, \XMM4, \XMM4
2109                 vaesenc \T1, \XMM5, \XMM5
2110                 vaesenc \T1, \XMM6, \XMM6
2111                 vaesenc \T1, \XMM7, \XMM7
2112                 vaesenc \T1, \XMM8, \XMM8
2113
2114         vmovdqa         TMP2(%rsp), \T1
2115         vmovdqa         HashKey_7(arg1), \T5
2116         vpclmulqdq      $0x11, \T5, \T1, \T3
2117         vpxor           \T3, \T4, \T4
2118
2119         vpclmulqdq      $0x00, \T5, \T1, \T3
2120         vpxor           \T3, \T7, \T7
2121
2122         vpclmulqdq      $0x01, \T5, \T1, \T3
2123         vpxor           \T3, \T6, \T6
2124
2125         vpclmulqdq      $0x10, \T5, \T1, \T3
2126         vpxor           \T3, \T6, \T6
2127
2128                 vmovdqu 16*4(arg1), \T1
2129                 vaesenc \T1, \XMM1, \XMM1
2130                 vaesenc \T1, \XMM2, \XMM2
2131                 vaesenc \T1, \XMM3, \XMM3
2132                 vaesenc \T1, \XMM4, \XMM4
2133                 vaesenc \T1, \XMM5, \XMM5
2134                 vaesenc \T1, \XMM6, \XMM6
2135                 vaesenc \T1, \XMM7, \XMM7
2136                 vaesenc \T1, \XMM8, \XMM8
2137
2138         #######################################################################
2139
2140         vmovdqa         TMP3(%rsp), \T1
2141         vmovdqa         HashKey_6(arg1), \T5
2142         vpclmulqdq      $0x11, \T5, \T1, \T3
2143         vpxor           \T3, \T4, \T4
2144
2145         vpclmulqdq      $0x00, \T5, \T1, \T3
2146         vpxor           \T3, \T7, \T7
2147
2148         vpclmulqdq      $0x01, \T5, \T1, \T3
2149         vpxor           \T3, \T6, \T6
2150
2151         vpclmulqdq      $0x10, \T5, \T1, \T3
2152         vpxor           \T3, \T6, \T6
2153
2154                 vmovdqu 16*5(arg1), \T1
2155                 vaesenc \T1, \XMM1, \XMM1
2156                 vaesenc \T1, \XMM2, \XMM2
2157                 vaesenc \T1, \XMM3, \XMM3
2158                 vaesenc \T1, \XMM4, \XMM4
2159                 vaesenc \T1, \XMM5, \XMM5
2160                 vaesenc \T1, \XMM6, \XMM6
2161                 vaesenc \T1, \XMM7, \XMM7
2162                 vaesenc \T1, \XMM8, \XMM8
2163
2164         vmovdqa         TMP4(%rsp), \T1
2165         vmovdqa         HashKey_5(arg1), \T5
2166         vpclmulqdq      $0x11, \T5, \T1, \T3
2167         vpxor           \T3, \T4, \T4
2168
2169         vpclmulqdq      $0x00, \T5, \T1, \T3
2170         vpxor           \T3, \T7, \T7
2171
2172         vpclmulqdq      $0x01, \T5, \T1, \T3
2173         vpxor           \T3, \T6, \T6
2174
2175         vpclmulqdq      $0x10, \T5, \T1, \T3
2176         vpxor           \T3, \T6, \T6
2177
2178                 vmovdqu 16*6(arg1), \T1
2179                 vaesenc \T1, \XMM1, \XMM1
2180                 vaesenc \T1, \XMM2, \XMM2
2181                 vaesenc \T1, \XMM3, \XMM3
2182                 vaesenc \T1, \XMM4, \XMM4
2183                 vaesenc \T1, \XMM5, \XMM5
2184                 vaesenc \T1, \XMM6, \XMM6
2185                 vaesenc \T1, \XMM7, \XMM7
2186                 vaesenc \T1, \XMM8, \XMM8
2187
2188
2189         vmovdqa         TMP5(%rsp), \T1
2190         vmovdqa         HashKey_4(arg1), \T5
2191         vpclmulqdq      $0x11, \T5, \T1, \T3
2192         vpxor           \T3, \T4, \T4
2193
2194         vpclmulqdq      $0x00, \T5, \T1, \T3
2195         vpxor           \T3, \T7, \T7
2196
2197         vpclmulqdq      $0x01, \T5, \T1, \T3
2198         vpxor           \T3, \T6, \T6
2199
2200         vpclmulqdq      $0x10, \T5, \T1, \T3
2201         vpxor           \T3, \T6, \T6
2202
2203                 vmovdqu 16*7(arg1), \T1
2204                 vaesenc \T1, \XMM1, \XMM1
2205                 vaesenc \T1, \XMM2, \XMM2
2206                 vaesenc \T1, \XMM3, \XMM3
2207                 vaesenc \T1, \XMM4, \XMM4
2208                 vaesenc \T1, \XMM5, \XMM5
2209                 vaesenc \T1, \XMM6, \XMM6
2210                 vaesenc \T1, \XMM7, \XMM7
2211                 vaesenc \T1, \XMM8, \XMM8
2212
2213         vmovdqa         TMP6(%rsp), \T1
2214         vmovdqa         HashKey_3(arg1), \T5
2215         vpclmulqdq      $0x11, \T5, \T1, \T3
2216         vpxor           \T3, \T4, \T4
2217
2218         vpclmulqdq      $0x00, \T5, \T1, \T3
2219         vpxor           \T3, \T7, \T7
2220
2221         vpclmulqdq      $0x01, \T5, \T1, \T3
2222         vpxor           \T3, \T6, \T6
2223
2224         vpclmulqdq      $0x10, \T5, \T1, \T3
2225         vpxor           \T3, \T6, \T6
2226
2227                 vmovdqu 16*8(arg1), \T1
2228                 vaesenc \T1, \XMM1, \XMM1
2229                 vaesenc \T1, \XMM2, \XMM2
2230                 vaesenc \T1, \XMM3, \XMM3
2231                 vaesenc \T1, \XMM4, \XMM4
2232                 vaesenc \T1, \XMM5, \XMM5
2233                 vaesenc \T1, \XMM6, \XMM6
2234                 vaesenc \T1, \XMM7, \XMM7
2235                 vaesenc \T1, \XMM8, \XMM8
2236
2237         vmovdqa         TMP7(%rsp), \T1
2238         vmovdqa         HashKey_2(arg1), \T5
2239         vpclmulqdq      $0x11, \T5, \T1, \T3
2240         vpxor           \T3, \T4, \T4
2241
2242         vpclmulqdq      $0x00, \T5, \T1, \T3
2243         vpxor           \T3, \T7, \T7
2244
2245         vpclmulqdq      $0x01, \T5, \T1, \T3
2246         vpxor           \T3, \T6, \T6
2247
2248         vpclmulqdq      $0x10, \T5, \T1, \T3
2249         vpxor           \T3, \T6, \T6
2250
2251
2252         #######################################################################
2253
2254                 vmovdqu 16*9(arg1), \T5
2255                 vaesenc \T5, \XMM1, \XMM1
2256                 vaesenc \T5, \XMM2, \XMM2
2257                 vaesenc \T5, \XMM3, \XMM3
2258                 vaesenc \T5, \XMM4, \XMM4
2259                 vaesenc \T5, \XMM5, \XMM5
2260                 vaesenc \T5, \XMM6, \XMM6
2261                 vaesenc \T5, \XMM7, \XMM7
2262                 vaesenc \T5, \XMM8, \XMM8
2263
2264         vmovdqa         TMP8(%rsp), \T1
2265         vmovdqa         HashKey(arg1), \T5
2266
2267         vpclmulqdq      $0x00, \T5, \T1, \T3
2268         vpxor           \T3, \T7, \T7
2269
2270         vpclmulqdq      $0x01, \T5, \T1, \T3
2271         vpxor           \T3, \T6, \T6
2272
2273         vpclmulqdq      $0x10, \T5, \T1, \T3
2274         vpxor           \T3, \T6, \T6
2275
2276         vpclmulqdq      $0x11, \T5, \T1, \T3
2277         vpxor           \T3, \T4, \T1
2278
2279
2280                 vmovdqu 16*10(arg1), \T5
2281
2282         i = 0
2283         j = 1
2284         setreg
2285 .rep 8
2286                 vpxor   16*i(arg3, %r11), \T5, \T2
2287                 .if \ENC_DEC == ENC
2288                 vaesenclast     \T2, reg_j, reg_j
2289                 .else
2290                 vaesenclast     \T2, reg_j, \T3
2291                 vmovdqu 16*i(arg3, %r11), reg_j
2292                 vmovdqu \T3, 16*i(arg2, %r11)
2293                 .endif
2294         i = (i+1)
2295         j = (j+1)
2296         setreg
2297 .endr
2298         #######################################################################
2299
2300
2301         vpslldq $8, \T6, \T3                            # shift-L T3 2 DWs
2302         vpsrldq $8, \T6, \T6                            # shift-R T2 2 DWs
2303         vpxor   \T3, \T7, \T7
2304         vpxor   \T6, \T1, \T1                           # accumulate the results in T1:T7
2305
2306
2307
2308         #######################################################################
2309         #first phase of the reduction
2310         vmovdqa         POLY2(%rip), \T3
2311
2312         vpclmulqdq      $0x01, \T7, \T3, \T2
2313         vpslldq         $8, \T2, \T2                    # shift-L xmm2 2 DWs
2314
2315         vpxor           \T2, \T7, \T7                   # first phase of the reduction complete
2316         #######################################################################
2317                 .if \ENC_DEC == ENC
2318                 vmovdqu  \XMM1, 16*0(arg2,%r11)         # Write to the Ciphertext buffer
2319                 vmovdqu  \XMM2, 16*1(arg2,%r11)         # Write to the Ciphertext buffer
2320                 vmovdqu  \XMM3, 16*2(arg2,%r11)         # Write to the Ciphertext buffer
2321                 vmovdqu  \XMM4, 16*3(arg2,%r11)         # Write to the Ciphertext buffer
2322                 vmovdqu  \XMM5, 16*4(arg2,%r11)         # Write to the Ciphertext buffer
2323                 vmovdqu  \XMM6, 16*5(arg2,%r11)         # Write to the Ciphertext buffer
2324                 vmovdqu  \XMM7, 16*6(arg2,%r11)         # Write to the Ciphertext buffer
2325                 vmovdqu  \XMM8, 16*7(arg2,%r11)         # Write to the Ciphertext buffer
2326                 .endif
2327
2328         #######################################################################
2329         #second phase of the reduction
2330         vpclmulqdq      $0x00, \T7, \T3, \T2
2331         vpsrldq         $4, \T2, \T2                    # shift-R xmm2 1 DW (Shift-R only 1-DW to obtain 2-DWs shift-R)
2332
2333         vpclmulqdq      $0x10, \T7, \T3, \T4
2334         vpslldq         $4, \T4, \T4                    # shift-L xmm0 1 DW (Shift-L 1-DW to obtain result with no shifts)
2335
2336         vpxor           \T2, \T4, \T4                   # second phase of the reduction complete
2337         #######################################################################
2338         vpxor           \T4, \T1, \T1                   # the result is in T1
2339
2340                 vpshufb SHUF_MASK(%rip), \XMM1, \XMM1   # perform a 16Byte swap
2341                 vpshufb SHUF_MASK(%rip), \XMM2, \XMM2   # perform a 16Byte swap
2342                 vpshufb SHUF_MASK(%rip), \XMM3, \XMM3   # perform a 16Byte swap
2343                 vpshufb SHUF_MASK(%rip), \XMM4, \XMM4   # perform a 16Byte swap
2344                 vpshufb SHUF_MASK(%rip), \XMM5, \XMM5   # perform a 16Byte swap
2345                 vpshufb SHUF_MASK(%rip), \XMM6, \XMM6   # perform a 16Byte swap
2346                 vpshufb SHUF_MASK(%rip), \XMM7, \XMM7   # perform a 16Byte swap
2347                 vpshufb SHUF_MASK(%rip), \XMM8, \XMM8   # perform a 16Byte swap
2348
2349
2350         vpxor   \T1, \XMM1, \XMM1
2351
2352
2353
2354 .endm
2355
2356
2357 # GHASH the last 4 ciphertext blocks.
2358 .macro  GHASH_LAST_8_AVX2 T1 T2 T3 T4 T5 T6 T7 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8
2359
2360         ## Karatsuba Method
2361
2362         vmovdqa         HashKey_8(arg1), \T5
2363
2364         vpshufd         $0b01001110, \XMM1, \T2
2365         vpshufd         $0b01001110, \T5, \T3
2366         vpxor           \XMM1, \T2, \T2
2367         vpxor           \T5, \T3, \T3
2368
2369         vpclmulqdq      $0x11, \T5, \XMM1, \T6
2370         vpclmulqdq      $0x00, \T5, \XMM1, \T7
2371
2372         vpclmulqdq      $0x00, \T3, \T2, \XMM1
2373
2374         ######################
2375
2376         vmovdqa         HashKey_7(arg1), \T5
2377         vpshufd         $0b01001110, \XMM2, \T2
2378         vpshufd         $0b01001110, \T5, \T3
2379         vpxor           \XMM2, \T2, \T2
2380         vpxor           \T5, \T3, \T3
2381
2382         vpclmulqdq      $0x11, \T5, \XMM2, \T4
2383         vpxor           \T4, \T6, \T6
2384
2385         vpclmulqdq      $0x00, \T5, \XMM2, \T4
2386         vpxor           \T4, \T7, \T7
2387
2388         vpclmulqdq      $0x00, \T3, \T2, \T2
2389
2390         vpxor           \T2, \XMM1, \XMM1
2391
2392         ######################
2393
2394         vmovdqa         HashKey_6(arg1), \T5
2395         vpshufd         $0b01001110, \XMM3, \T2
2396         vpshufd         $0b01001110, \T5, \T3
2397         vpxor           \XMM3, \T2, \T2
2398         vpxor           \T5, \T3, \T3
2399
2400         vpclmulqdq      $0x11, \T5, \XMM3, \T4
2401         vpxor           \T4, \T6, \T6
2402
2403         vpclmulqdq      $0x00, \T5, \XMM3, \T4
2404         vpxor           \T4, \T7, \T7
2405
2406         vpclmulqdq      $0x00, \T3, \T2, \T2
2407
2408         vpxor           \T2, \XMM1, \XMM1
2409
2410         ######################
2411
2412         vmovdqa         HashKey_5(arg1), \T5
2413         vpshufd         $0b01001110, \XMM4, \T2
2414         vpshufd         $0b01001110, \T5, \T3
2415         vpxor           \XMM4, \T2, \T2
2416         vpxor           \T5, \T3, \T3
2417
2418         vpclmulqdq      $0x11, \T5, \XMM4, \T4
2419         vpxor           \T4, \T6, \T6
2420
2421         vpclmulqdq      $0x00, \T5, \XMM4, \T4
2422         vpxor           \T4, \T7, \T7
2423
2424         vpclmulqdq      $0x00, \T3, \T2, \T2
2425
2426         vpxor           \T2, \XMM1, \XMM1
2427
2428         ######################
2429
2430         vmovdqa         HashKey_4(arg1), \T5
2431         vpshufd         $0b01001110, \XMM5, \T2
2432         vpshufd         $0b01001110, \T5, \T3
2433         vpxor           \XMM5, \T2, \T2
2434         vpxor           \T5, \T3, \T3
2435
2436         vpclmulqdq      $0x11, \T5, \XMM5, \T4
2437         vpxor           \T4, \T6, \T6
2438
2439         vpclmulqdq      $0x00, \T5, \XMM5, \T4
2440         vpxor           \T4, \T7, \T7
2441
2442         vpclmulqdq      $0x00, \T3, \T2, \T2
2443
2444         vpxor           \T2, \XMM1, \XMM1
2445
2446         ######################
2447
2448         vmovdqa         HashKey_3(arg1), \T5
2449         vpshufd         $0b01001110, \XMM6, \T2
2450         vpshufd         $0b01001110, \T5, \T3
2451         vpxor           \XMM6, \T2, \T2
2452         vpxor           \T5, \T3, \T3
2453
2454         vpclmulqdq      $0x11, \T5, \XMM6, \T4
2455         vpxor           \T4, \T6, \T6
2456
2457         vpclmulqdq      $0x00, \T5, \XMM6, \T4
2458         vpxor           \T4, \T7, \T7
2459
2460         vpclmulqdq      $0x00, \T3, \T2, \T2
2461
2462         vpxor           \T2, \XMM1, \XMM1
2463
2464         ######################
2465
2466         vmovdqa         HashKey_2(arg1), \T5
2467         vpshufd         $0b01001110, \XMM7, \T2
2468         vpshufd         $0b01001110, \T5, \T3
2469         vpxor           \XMM7, \T2, \T2
2470         vpxor           \T5, \T3, \T3
2471
2472         vpclmulqdq      $0x11, \T5, \XMM7, \T4
2473         vpxor           \T4, \T6, \T6
2474
2475         vpclmulqdq      $0x00, \T5, \XMM7, \T4
2476         vpxor           \T4, \T7, \T7
2477
2478         vpclmulqdq      $0x00, \T3, \T2, \T2
2479
2480         vpxor           \T2, \XMM1, \XMM1
2481
2482         ######################
2483
2484         vmovdqa         HashKey(arg1), \T5
2485         vpshufd         $0b01001110, \XMM8, \T2
2486         vpshufd         $0b01001110, \T5, \T3
2487         vpxor           \XMM8, \T2, \T2
2488         vpxor           \T5, \T3, \T3
2489
2490         vpclmulqdq      $0x11, \T5, \XMM8, \T4
2491         vpxor           \T4, \T6, \T6
2492
2493         vpclmulqdq      $0x00, \T5, \XMM8, \T4
2494         vpxor           \T4, \T7, \T7
2495
2496         vpclmulqdq      $0x00, \T3, \T2, \T2
2497
2498         vpxor           \T2, \XMM1, \XMM1
2499         vpxor           \T6, \XMM1, \XMM1
2500         vpxor           \T7, \XMM1, \T2
2501
2502
2503
2504
2505         vpslldq $8, \T2, \T4
2506         vpsrldq $8, \T2, \T2
2507
2508         vpxor   \T4, \T7, \T7
2509         vpxor   \T2, \T6, \T6                      # <T6:T7> holds the result of the
2510                                                    # accumulated carry-less multiplications
2511
2512         #######################################################################
2513         #first phase of the reduction
2514         vmovdqa         POLY2(%rip), \T3
2515
2516         vpclmulqdq      $0x01, \T7, \T3, \T2
2517         vpslldq         $8, \T2, \T2               # shift-L xmm2 2 DWs
2518
2519         vpxor           \T2, \T7, \T7              # first phase of the reduction complete
2520         #######################################################################
2521
2522
2523         #second phase of the reduction
2524         vpclmulqdq      $0x00, \T7, \T3, \T2
2525         vpsrldq         $4, \T2, \T2               # shift-R T2 1 DW (Shift-R only 1-DW to obtain 2-DWs shift-R)
2526
2527         vpclmulqdq      $0x10, \T7, \T3, \T4
2528         vpslldq         $4, \T4, \T4               # shift-L T4 1 DW (Shift-L 1-DW to obtain result with no shifts)
2529
2530         vpxor           \T2, \T4, \T4              # second phase of the reduction complete
2531         #######################################################################
2532         vpxor           \T4, \T6, \T6              # the result is in T6
2533 .endm
2534
2535
2536
2537 #############################################################
2538 #void   aesni_gcm_precomp_avx_gen4
2539 #        (gcm_data     *my_ctx_data,
2540 #        u8     *hash_subkey)# /* H, the Hash sub key input.
2541 #                               Data starts on a 16-byte boundary. */
2542 #############################################################
2543 ENTRY(aesni_gcm_precomp_avx_gen4)
2544         #the number of pushes must equal STACK_OFFSET
2545         push    %r12
2546         push    %r13
2547         push    %r14
2548         push    %r15
2549
2550         mov     %rsp, %r14
2551
2552
2553
2554         sub     $VARIABLE_OFFSET, %rsp
2555         and     $~63, %rsp                    # align rsp to 64 bytes
2556
2557         vmovdqu  (arg2), %xmm6                # xmm6 = HashKey
2558
2559         vpshufb  SHUF_MASK(%rip), %xmm6, %xmm6
2560         ###############  PRECOMPUTATION of HashKey<<1 mod poly from the HashKey
2561         vmovdqa  %xmm6, %xmm2
2562         vpsllq   $1, %xmm6, %xmm6
2563         vpsrlq   $63, %xmm2, %xmm2
2564         vmovdqa  %xmm2, %xmm1
2565         vpslldq  $8, %xmm2, %xmm2
2566         vpsrldq  $8, %xmm1, %xmm1
2567         vpor     %xmm2, %xmm6, %xmm6
2568         #reduction
2569         vpshufd  $0b00100100, %xmm1, %xmm2
2570         vpcmpeqd TWOONE(%rip), %xmm2, %xmm2
2571         vpand    POLY(%rip), %xmm2, %xmm2
2572         vpxor    %xmm2, %xmm6, %xmm6          # xmm6 holds the HashKey<<1 mod poly
2573         #######################################################################
2574         vmovdqa  %xmm6, HashKey(arg1)         # store HashKey<<1 mod poly
2575
2576
2577         PRECOMPUTE_AVX2  %xmm6, %xmm0, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5
2578
2579         mov     %r14, %rsp
2580
2581         pop     %r15
2582         pop     %r14
2583         pop     %r13
2584         pop     %r12
2585         ret
2586 ENDPROC(aesni_gcm_precomp_avx_gen4)
2587
2588
2589 ###############################################################################
2590 #void   aesni_gcm_enc_avx_gen4(
2591 #        gcm_data        *my_ctx_data,     /* aligned to 16 Bytes */
2592 #        u8      *out, /* Ciphertext output. Encrypt in-place is allowed.  */
2593 #        const   u8 *in, /* Plaintext input */
2594 #        u64     plaintext_len, /* Length of data in Bytes for encryption. */
2595 #        u8      *iv, /* Pre-counter block j0: 4 byte salt
2596 #                       (from Security Association) concatenated with 8 byte
2597 #                        Initialisation Vector (from IPSec ESP Payload)
2598 #                        concatenated with 0x00000001. 16-byte aligned pointer. */
2599 #        const   u8 *aad, /* Additional Authentication Data (AAD)*/
2600 #        u64     aad_len, /* Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 Bytes */
2601 #        u8      *auth_tag, /* Authenticated Tag output. */
2602 #        u64     auth_tag_len)# /* Authenticated Tag Length in bytes.
2603 #                               Valid values are 16 (most likely), 12 or 8. */
2604 ###############################################################################
2605 ENTRY(aesni_gcm_enc_avx_gen4)
2606         GCM_ENC_DEC INITIAL_BLOCKS_AVX2 GHASH_8_ENCRYPT_8_PARALLEL_AVX2 GHASH_LAST_8_AVX2 GHASH_MUL_AVX2 ENC
2607         ret
2608 ENDPROC(aesni_gcm_enc_avx_gen4)
2609
2610 ###############################################################################
2611 #void   aesni_gcm_dec_avx_gen4(
2612 #        gcm_data        *my_ctx_data,     /* aligned to 16 Bytes */
2613 #        u8      *out, /* Plaintext output. Decrypt in-place is allowed.  */
2614 #        const   u8 *in, /* Ciphertext input */
2615 #        u64     plaintext_len, /* Length of data in Bytes for encryption. */
2616 #        u8      *iv, /* Pre-counter block j0: 4 byte salt
2617 #                       (from Security Association) concatenated with 8 byte
2618 #                       Initialisation Vector (from IPSec ESP Payload)
2619 #                       concatenated with 0x00000001. 16-byte aligned pointer. */
2620 #        const   u8 *aad, /* Additional Authentication Data (AAD)*/
2621 #        u64     aad_len, /* Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 Bytes */
2622 #        u8      *auth_tag, /* Authenticated Tag output. */
2623 #        u64     auth_tag_len)# /* Authenticated Tag Length in bytes.
2624 #                               Valid values are 16 (most likely), 12 or 8. */
2625 ###############################################################################
2626 ENTRY(aesni_gcm_dec_avx_gen4)
2627         GCM_ENC_DEC INITIAL_BLOCKS_AVX2 GHASH_8_ENCRYPT_8_PARALLEL_AVX2 GHASH_LAST_8_AVX2 GHASH_MUL_AVX2 DEC
2628         ret
2629 ENDPROC(aesni_gcm_dec_avx_gen4)
2630
2631 #endif /* CONFIG_AS_AVX2 */