blob: 383a6f84a060f103c7c43923ad84029e0ce2a9f1 [file] [log] [blame]
Huang Ying54b6a1b2009-01-18 16:28:34 +11001/*
2 * Implement AES algorithm in Intel AES-NI instructions.
3 *
4 * The white paper of AES-NI instructions can be downloaded from:
5 * http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
6 *
7 * Copyright (C) 2008, Intel Corp.
8 * Author: Huang Ying <ying.huang@intel.com>
9 * Vinodh Gopal <vinodh.gopal@intel.com>
10 * Kahraman Akdemir
11 *
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040012 * Added RFC4106 AES-GCM support for 128-bit keys under the AEAD
13 * interface for 64-bit kernels.
14 * Authors: Erdinc Ozturk (erdinc.ozturk@intel.com)
15 * Aidan O'Mahony (aidan.o.mahony@intel.com)
16 * Adrian Hoban <adrian.hoban@intel.com>
17 * James Guilford (james.guilford@intel.com)
18 * Gabriele Paoloni <gabriele.paoloni@intel.com>
19 * Tadeusz Struk (tadeusz.struk@intel.com)
20 * Wajdi Feghali (wajdi.k.feghali@intel.com)
21 * Copyright (c) 2010, Intel Corporation.
22 *
Mathias Krause0d258ef2010-11-27 16:34:46 +080023 * Ported x86_64 version to x86:
24 * Author: Mathias Krause <minipli@googlemail.com>
25 *
Huang Ying54b6a1b2009-01-18 16:28:34 +110026 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 */
31
32#include <linux/linkage.h>
Huang Yingb369e522009-11-23 19:54:06 +080033#include <asm/inst.h>
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -060034#include <asm/frame.h>
Huang Ying54b6a1b2009-01-18 16:28:34 +110035
Timothy McCaffreye31ac322015-01-13 13:16:43 -050036/*
37 * The following macros are used to move an (un)aligned 16 byte value to/from
38 * an XMM register. This can done for either FP or integer values, for FP use
39 * movaps (move aligned packed single) or integer use movdqa (move double quad
40 * aligned). It doesn't make a performance difference which instruction is used
41 * since Nehalem (original Core i7) was released. However, the movaps is a byte
42 * shorter, so that is the one we'll use for now. (same for unaligned).
43 */
44#define MOVADQ movaps
45#define MOVUDQ movups
46
Mathias Krause559ad0f2010-11-29 08:35:39 +080047#ifdef __x86_64__
Timothy McCaffreye31ac322015-01-13 13:16:43 -050048
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040049.data
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +030050.align 16
51.Lgf128mul_x_ble_mask:
52 .octa 0x00000000000000010000000000000087
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040053POLY: .octa 0xC2000000000000000000000000000001
54TWOONE: .octa 0x00000001000000000000000000000001
55
56# order of these constants should not change.
57# more specifically, ALL_F should follow SHIFT_MASK,
58# and ZERO should follow ALL_F
59
60SHUF_MASK: .octa 0x000102030405060708090A0B0C0D0E0F
61MASK1: .octa 0x0000000000000000ffffffffffffffff
62MASK2: .octa 0xffffffffffffffff0000000000000000
63SHIFT_MASK: .octa 0x0f0e0d0c0b0a09080706050403020100
64ALL_F: .octa 0xffffffffffffffffffffffffffffffff
65ZERO: .octa 0x00000000000000000000000000000000
66ONE: .octa 0x00000000000000000000000000000001
67F_MIN_MASK: .octa 0xf1f2f3f4f5f6f7f8f9fafbfcfdfeff0
68dec: .octa 0x1
69enc: .octa 0x2
70
71
Huang Ying54b6a1b2009-01-18 16:28:34 +110072.text
73
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040074
75#define STACK_OFFSET 8*3
76#define HashKey 16*0 // store HashKey <<1 mod poly here
77#define HashKey_2 16*1 // store HashKey^2 <<1 mod poly here
78#define HashKey_3 16*2 // store HashKey^3 <<1 mod poly here
79#define HashKey_4 16*3 // store HashKey^4 <<1 mod poly here
80#define HashKey_k 16*4 // store XOR of High 64 bits and Low 64
81 // bits of HashKey <<1 mod poly here
82 //(for Karatsuba purposes)
83#define HashKey_2_k 16*5 // store XOR of High 64 bits and Low 64
84 // bits of HashKey^2 <<1 mod poly here
85 // (for Karatsuba purposes)
86#define HashKey_3_k 16*6 // store XOR of High 64 bits and Low 64
87 // bits of HashKey^3 <<1 mod poly here
88 // (for Karatsuba purposes)
89#define HashKey_4_k 16*7 // store XOR of High 64 bits and Low 64
90 // bits of HashKey^4 <<1 mod poly here
91 // (for Karatsuba purposes)
92#define VARIABLE_OFFSET 16*8
93
94#define arg1 rdi
95#define arg2 rsi
96#define arg3 rdx
97#define arg4 rcx
98#define arg5 r8
99#define arg6 r9
100#define arg7 STACK_OFFSET+8(%r14)
101#define arg8 STACK_OFFSET+16(%r14)
102#define arg9 STACK_OFFSET+24(%r14)
103#define arg10 STACK_OFFSET+32(%r14)
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500104#define keysize 2*15*16(%arg1)
Mathias Krause559ad0f2010-11-29 08:35:39 +0800105#endif
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400106
107
Huang Ying54b6a1b2009-01-18 16:28:34 +1100108#define STATE1 %xmm0
109#define STATE2 %xmm4
110#define STATE3 %xmm5
111#define STATE4 %xmm6
112#define STATE STATE1
113#define IN1 %xmm1
114#define IN2 %xmm7
115#define IN3 %xmm8
116#define IN4 %xmm9
117#define IN IN1
118#define KEY %xmm2
119#define IV %xmm3
Mathias Krause0d258ef2010-11-27 16:34:46 +0800120
Huang Ying12387a42010-03-10 18:28:55 +0800121#define BSWAP_MASK %xmm10
122#define CTR %xmm11
123#define INC %xmm12
Huang Ying54b6a1b2009-01-18 16:28:34 +1100124
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300125#define GF128MUL_MASK %xmm10
126
Mathias Krause0d258ef2010-11-27 16:34:46 +0800127#ifdef __x86_64__
128#define AREG %rax
Huang Ying54b6a1b2009-01-18 16:28:34 +1100129#define KEYP %rdi
130#define OUTP %rsi
Mathias Krause0d258ef2010-11-27 16:34:46 +0800131#define UKEYP OUTP
Huang Ying54b6a1b2009-01-18 16:28:34 +1100132#define INP %rdx
133#define LEN %rcx
134#define IVP %r8
135#define KLEN %r9d
136#define T1 %r10
137#define TKEYP T1
138#define T2 %r11
Huang Ying12387a42010-03-10 18:28:55 +0800139#define TCTR_LOW T2
Mathias Krause0d258ef2010-11-27 16:34:46 +0800140#else
141#define AREG %eax
142#define KEYP %edi
143#define OUTP AREG
144#define UKEYP OUTP
145#define INP %edx
146#define LEN %esi
147#define IVP %ebp
148#define KLEN %ebx
149#define T1 %ecx
150#define TKEYP T1
151#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +1100152
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400153
Mathias Krause559ad0f2010-11-29 08:35:39 +0800154#ifdef __x86_64__
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400155/* GHASH_MUL MACRO to implement: Data*HashKey mod (128,127,126,121,0)
156*
157*
158* Input: A and B (128-bits each, bit-reflected)
159* Output: C = A*B*x mod poly, (i.e. >>1 )
160* To compute GH = GH*HashKey mod poly, give HK = HashKey<<1 mod poly as input
161* GH = GH * HK * x mod poly which is equivalent to GH*HashKey mod poly.
162*
163*/
164.macro GHASH_MUL GH HK TMP1 TMP2 TMP3 TMP4 TMP5
165 movdqa \GH, \TMP1
166 pshufd $78, \GH, \TMP2
167 pshufd $78, \HK, \TMP3
168 pxor \GH, \TMP2 # TMP2 = a1+a0
169 pxor \HK, \TMP3 # TMP3 = b1+b0
170 PCLMULQDQ 0x11, \HK, \TMP1 # TMP1 = a1*b1
171 PCLMULQDQ 0x00, \HK, \GH # GH = a0*b0
172 PCLMULQDQ 0x00, \TMP3, \TMP2 # TMP2 = (a0+a1)*(b1+b0)
173 pxor \GH, \TMP2
174 pxor \TMP1, \TMP2 # TMP2 = (a0*b0)+(a1*b0)
175 movdqa \TMP2, \TMP3
176 pslldq $8, \TMP3 # left shift TMP3 2 DWs
177 psrldq $8, \TMP2 # right shift TMP2 2 DWs
178 pxor \TMP3, \GH
179 pxor \TMP2, \TMP1 # TMP2:GH holds the result of GH*HK
180
181 # first phase of the reduction
182
183 movdqa \GH, \TMP2
184 movdqa \GH, \TMP3
185 movdqa \GH, \TMP4 # copy GH into TMP2,TMP3 and TMP4
186 # in in order to perform
187 # independent shifts
188 pslld $31, \TMP2 # packed right shift <<31
189 pslld $30, \TMP3 # packed right shift <<30
190 pslld $25, \TMP4 # packed right shift <<25
191 pxor \TMP3, \TMP2 # xor the shifted versions
192 pxor \TMP4, \TMP2
193 movdqa \TMP2, \TMP5
194 psrldq $4, \TMP5 # right shift TMP5 1 DW
195 pslldq $12, \TMP2 # left shift TMP2 3 DWs
196 pxor \TMP2, \GH
197
198 # second phase of the reduction
199
200 movdqa \GH,\TMP2 # copy GH into TMP2,TMP3 and TMP4
201 # in in order to perform
202 # independent shifts
203 movdqa \GH,\TMP3
204 movdqa \GH,\TMP4
205 psrld $1,\TMP2 # packed left shift >>1
206 psrld $2,\TMP3 # packed left shift >>2
207 psrld $7,\TMP4 # packed left shift >>7
208 pxor \TMP3,\TMP2 # xor the shifted versions
209 pxor \TMP4,\TMP2
210 pxor \TMP5, \TMP2
211 pxor \TMP2, \GH
212 pxor \TMP1, \GH # result is in TMP1
213.endm
214
215/*
216* if a = number of total plaintext bytes
217* b = floor(a/16)
218* num_initial_blocks = b mod 4
219* encrypt the initial num_initial_blocks blocks and apply ghash on
220* the ciphertext
221* %r10, %r11, %r12, %rax, %xmm5, %xmm6, %xmm7, %xmm8, %xmm9 registers
222* are clobbered
223* arg1, %arg2, %arg3, %r14 are used as a pointer only, not modified
224*/
225
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400226
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800227.macro INITIAL_BLOCKS_DEC num_initial_blocks TMP1 TMP2 TMP3 TMP4 TMP5 XMM0 XMM1 \
228XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500229 MOVADQ SHUF_MASK(%rip), %xmm14
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400230 mov arg7, %r10 # %r10 = AAD
231 mov arg8, %r12 # %r12 = aadLen
232 mov %r12, %r11
233 pxor %xmm\i, %xmm\i
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500234
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400235_get_AAD_loop\num_initial_blocks\operation:
236 movd (%r10), \TMP1
237 pslldq $12, \TMP1
238 psrldq $4, %xmm\i
239 pxor \TMP1, %xmm\i
240 add $4, %r10
241 sub $4, %r12
242 jne _get_AAD_loop\num_initial_blocks\operation
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500243
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400244 cmp $16, %r11
245 je _get_AAD_loop2_done\num_initial_blocks\operation
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500246
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400247 mov $16, %r12
248_get_AAD_loop2\num_initial_blocks\operation:
249 psrldq $4, %xmm\i
250 sub $4, %r12
251 cmp %r11, %r12
252 jne _get_AAD_loop2\num_initial_blocks\operation
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500253
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400254_get_AAD_loop2_done\num_initial_blocks\operation:
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800255 PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
256
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400257 xor %r11, %r11 # initialise the data pointer offset as zero
258
259 # start AES for num_initial_blocks blocks
260
261 mov %arg5, %rax # %rax = *Y0
262 movdqu (%rax), \XMM0 # XMM0 = Y0
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800263 PSHUFB_XMM %xmm14, \XMM0
264
265.if (\i == 5) || (\i == 6) || (\i == 7)
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500266 MOVADQ ONE(%RIP),\TMP1
267 MOVADQ (%arg1),\TMP2
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400268.irpc index, \i_seq
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500269 paddd \TMP1, \XMM0 # INCR Y0
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400270 movdqa \XMM0, %xmm\index
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800271 PSHUFB_XMM %xmm14, %xmm\index # perform a 16 byte swap
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500272 pxor \TMP2, %xmm\index
273.endr
274 lea 0x10(%arg1),%r10
275 mov keysize,%eax
276 shr $2,%eax # 128->4, 192->6, 256->8
277 add $5,%eax # 128->9, 192->11, 256->13
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800278
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500279aes_loop_initial_dec\num_initial_blocks:
280 MOVADQ (%r10),\TMP1
281.irpc index, \i_seq
282 AESENC \TMP1, %xmm\index
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400283.endr
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500284 add $16,%r10
285 sub $1,%eax
286 jnz aes_loop_initial_dec\num_initial_blocks
287
288 MOVADQ (%r10), \TMP1
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400289.irpc index, \i_seq
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500290 AESENCLAST \TMP1, %xmm\index # Last Round
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400291.endr
292.irpc index, \i_seq
293 movdqu (%arg3 , %r11, 1), \TMP1
294 pxor \TMP1, %xmm\index
295 movdqu %xmm\index, (%arg2 , %r11, 1)
296 # write back plaintext/ciphertext for num_initial_blocks
297 add $16, %r11
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800298
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400299 movdqa \TMP1, %xmm\index
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800300 PSHUFB_XMM %xmm14, %xmm\index
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500301 # prepare plaintext/ciphertext for GHASH computation
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400302.endr
303.endif
304 GHASH_MUL %xmm\i, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
305 # apply GHASH on num_initial_blocks blocks
306
307.if \i == 5
308 pxor %xmm5, %xmm6
309 GHASH_MUL %xmm6, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
310 pxor %xmm6, %xmm7
311 GHASH_MUL %xmm7, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
312 pxor %xmm7, %xmm8
313 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
314.elseif \i == 6
315 pxor %xmm6, %xmm7
316 GHASH_MUL %xmm7, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
317 pxor %xmm7, %xmm8
318 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
319.elseif \i == 7
320 pxor %xmm7, %xmm8
321 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
322.endif
323 cmp $64, %r13
324 jl _initial_blocks_done\num_initial_blocks\operation
325 # no need for precomputed values
326/*
327*
328* Precomputations for HashKey parallel with encryption of first 4 blocks.
329* Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
330*/
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500331 MOVADQ ONE(%rip), \TMP1
332 paddd \TMP1, \XMM0 # INCR Y0
333 MOVADQ \XMM0, \XMM1
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800334 PSHUFB_XMM %xmm14, \XMM1 # perform a 16 byte swap
335
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500336 paddd \TMP1, \XMM0 # INCR Y0
337 MOVADQ \XMM0, \XMM2
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800338 PSHUFB_XMM %xmm14, \XMM2 # perform a 16 byte swap
339
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500340 paddd \TMP1, \XMM0 # INCR Y0
341 MOVADQ \XMM0, \XMM3
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800342 PSHUFB_XMM %xmm14, \XMM3 # perform a 16 byte swap
343
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500344 paddd \TMP1, \XMM0 # INCR Y0
345 MOVADQ \XMM0, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800346 PSHUFB_XMM %xmm14, \XMM4 # perform a 16 byte swap
347
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500348 MOVADQ 0(%arg1),\TMP1
349 pxor \TMP1, \XMM1
350 pxor \TMP1, \XMM2
351 pxor \TMP1, \XMM3
352 pxor \TMP1, \XMM4
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400353 movdqa \TMP3, \TMP5
354 pshufd $78, \TMP3, \TMP1
355 pxor \TMP3, \TMP1
356 movdqa \TMP1, HashKey_k(%rsp)
357 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
358# TMP5 = HashKey^2<<1 (mod poly)
359 movdqa \TMP5, HashKey_2(%rsp)
360# HashKey_2 = HashKey^2<<1 (mod poly)
361 pshufd $78, \TMP5, \TMP1
362 pxor \TMP5, \TMP1
363 movdqa \TMP1, HashKey_2_k(%rsp)
364.irpc index, 1234 # do 4 rounds
365 movaps 0x10*\index(%arg1), \TMP1
366 AESENC \TMP1, \XMM1
367 AESENC \TMP1, \XMM2
368 AESENC \TMP1, \XMM3
369 AESENC \TMP1, \XMM4
370.endr
371 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
372# TMP5 = HashKey^3<<1 (mod poly)
373 movdqa \TMP5, HashKey_3(%rsp)
374 pshufd $78, \TMP5, \TMP1
375 pxor \TMP5, \TMP1
376 movdqa \TMP1, HashKey_3_k(%rsp)
377.irpc index, 56789 # do next 5 rounds
378 movaps 0x10*\index(%arg1), \TMP1
379 AESENC \TMP1, \XMM1
380 AESENC \TMP1, \XMM2
381 AESENC \TMP1, \XMM3
382 AESENC \TMP1, \XMM4
383.endr
384 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
385# TMP5 = HashKey^3<<1 (mod poly)
386 movdqa \TMP5, HashKey_4(%rsp)
387 pshufd $78, \TMP5, \TMP1
388 pxor \TMP5, \TMP1
389 movdqa \TMP1, HashKey_4_k(%rsp)
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500390 lea 0xa0(%arg1),%r10
391 mov keysize,%eax
392 shr $2,%eax # 128->4, 192->6, 256->8
393 sub $4,%eax # 128->0, 192->2, 256->4
394 jz aes_loop_pre_dec_done\num_initial_blocks
395
396aes_loop_pre_dec\num_initial_blocks:
397 MOVADQ (%r10),\TMP2
398.irpc index, 1234
399 AESENC \TMP2, %xmm\index
400.endr
401 add $16,%r10
402 sub $1,%eax
403 jnz aes_loop_pre_dec\num_initial_blocks
404
405aes_loop_pre_dec_done\num_initial_blocks:
406 MOVADQ (%r10), \TMP2
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400407 AESENCLAST \TMP2, \XMM1
408 AESENCLAST \TMP2, \XMM2
409 AESENCLAST \TMP2, \XMM3
410 AESENCLAST \TMP2, \XMM4
411 movdqu 16*0(%arg3 , %r11 , 1), \TMP1
412 pxor \TMP1, \XMM1
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400413 movdqu \XMM1, 16*0(%arg2 , %r11 , 1)
414 movdqa \TMP1, \XMM1
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400415 movdqu 16*1(%arg3 , %r11 , 1), \TMP1
416 pxor \TMP1, \XMM2
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400417 movdqu \XMM2, 16*1(%arg2 , %r11 , 1)
418 movdqa \TMP1, \XMM2
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400419 movdqu 16*2(%arg3 , %r11 , 1), \TMP1
420 pxor \TMP1, \XMM3
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400421 movdqu \XMM3, 16*2(%arg2 , %r11 , 1)
422 movdqa \TMP1, \XMM3
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400423 movdqu 16*3(%arg3 , %r11 , 1), \TMP1
424 pxor \TMP1, \XMM4
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400425 movdqu \XMM4, 16*3(%arg2 , %r11 , 1)
426 movdqa \TMP1, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800427 add $64, %r11
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800428 PSHUFB_XMM %xmm14, \XMM1 # perform a 16 byte swap
429 pxor \XMMDst, \XMM1
430# combine GHASHed value with the corresponding ciphertext
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800431 PSHUFB_XMM %xmm14, \XMM2 # perform a 16 byte swap
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800432 PSHUFB_XMM %xmm14, \XMM3 # perform a 16 byte swap
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800433 PSHUFB_XMM %xmm14, \XMM4 # perform a 16 byte swap
434
435_initial_blocks_done\num_initial_blocks\operation:
436
437.endm
438
439
440/*
441* if a = number of total plaintext bytes
442* b = floor(a/16)
443* num_initial_blocks = b mod 4
444* encrypt the initial num_initial_blocks blocks and apply ghash on
445* the ciphertext
446* %r10, %r11, %r12, %rax, %xmm5, %xmm6, %xmm7, %xmm8, %xmm9 registers
447* are clobbered
448* arg1, %arg2, %arg3, %r14 are used as a pointer only, not modified
449*/
450
451
452.macro INITIAL_BLOCKS_ENC num_initial_blocks TMP1 TMP2 TMP3 TMP4 TMP5 XMM0 XMM1 \
453XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500454 MOVADQ SHUF_MASK(%rip), %xmm14
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800455 mov arg7, %r10 # %r10 = AAD
456 mov arg8, %r12 # %r12 = aadLen
457 mov %r12, %r11
458 pxor %xmm\i, %xmm\i
459_get_AAD_loop\num_initial_blocks\operation:
460 movd (%r10), \TMP1
461 pslldq $12, \TMP1
462 psrldq $4, %xmm\i
463 pxor \TMP1, %xmm\i
464 add $4, %r10
465 sub $4, %r12
466 jne _get_AAD_loop\num_initial_blocks\operation
467 cmp $16, %r11
468 je _get_AAD_loop2_done\num_initial_blocks\operation
469 mov $16, %r12
470_get_AAD_loop2\num_initial_blocks\operation:
471 psrldq $4, %xmm\i
472 sub $4, %r12
473 cmp %r11, %r12
474 jne _get_AAD_loop2\num_initial_blocks\operation
475_get_AAD_loop2_done\num_initial_blocks\operation:
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800476 PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
477
478 xor %r11, %r11 # initialise the data pointer offset as zero
479
480 # start AES for num_initial_blocks blocks
481
482 mov %arg5, %rax # %rax = *Y0
483 movdqu (%rax), \XMM0 # XMM0 = Y0
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800484 PSHUFB_XMM %xmm14, \XMM0
485
486.if (\i == 5) || (\i == 6) || (\i == 7)
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800487
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500488 MOVADQ ONE(%RIP),\TMP1
489 MOVADQ 0(%arg1),\TMP2
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800490.irpc index, \i_seq
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500491 paddd \TMP1, \XMM0 # INCR Y0
492 MOVADQ \XMM0, %xmm\index
493 PSHUFB_XMM %xmm14, %xmm\index # perform a 16 byte swap
494 pxor \TMP2, %xmm\index
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800495.endr
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500496 lea 0x10(%arg1),%r10
497 mov keysize,%eax
498 shr $2,%eax # 128->4, 192->6, 256->8
499 add $5,%eax # 128->9, 192->11, 256->13
500
501aes_loop_initial_enc\num_initial_blocks:
502 MOVADQ (%r10),\TMP1
503.irpc index, \i_seq
504 AESENC \TMP1, %xmm\index
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800505.endr
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500506 add $16,%r10
507 sub $1,%eax
508 jnz aes_loop_initial_enc\num_initial_blocks
509
510 MOVADQ (%r10), \TMP1
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800511.irpc index, \i_seq
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500512 AESENCLAST \TMP1, %xmm\index # Last Round
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800513.endr
514.irpc index, \i_seq
515 movdqu (%arg3 , %r11, 1), \TMP1
516 pxor \TMP1, %xmm\index
517 movdqu %xmm\index, (%arg2 , %r11, 1)
518 # write back plaintext/ciphertext for num_initial_blocks
519 add $16, %r11
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800520 PSHUFB_XMM %xmm14, %xmm\index
521
522 # prepare plaintext/ciphertext for GHASH computation
523.endr
524.endif
525 GHASH_MUL %xmm\i, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
526 # apply GHASH on num_initial_blocks blocks
527
528.if \i == 5
529 pxor %xmm5, %xmm6
530 GHASH_MUL %xmm6, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
531 pxor %xmm6, %xmm7
532 GHASH_MUL %xmm7, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
533 pxor %xmm7, %xmm8
534 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
535.elseif \i == 6
536 pxor %xmm6, %xmm7
537 GHASH_MUL %xmm7, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
538 pxor %xmm7, %xmm8
539 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
540.elseif \i == 7
541 pxor %xmm7, %xmm8
542 GHASH_MUL %xmm8, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
543.endif
544 cmp $64, %r13
545 jl _initial_blocks_done\num_initial_blocks\operation
546 # no need for precomputed values
547/*
548*
549* Precomputations for HashKey parallel with encryption of first 4 blocks.
550* Haskey_i_k holds XORed values of the low and high parts of the Haskey_i
551*/
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500552 MOVADQ ONE(%RIP),\TMP1
553 paddd \TMP1, \XMM0 # INCR Y0
554 MOVADQ \XMM0, \XMM1
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800555 PSHUFB_XMM %xmm14, \XMM1 # perform a 16 byte swap
556
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500557 paddd \TMP1, \XMM0 # INCR Y0
558 MOVADQ \XMM0, \XMM2
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800559 PSHUFB_XMM %xmm14, \XMM2 # perform a 16 byte swap
560
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500561 paddd \TMP1, \XMM0 # INCR Y0
562 MOVADQ \XMM0, \XMM3
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800563 PSHUFB_XMM %xmm14, \XMM3 # perform a 16 byte swap
564
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500565 paddd \TMP1, \XMM0 # INCR Y0
566 MOVADQ \XMM0, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800567 PSHUFB_XMM %xmm14, \XMM4 # perform a 16 byte swap
568
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500569 MOVADQ 0(%arg1),\TMP1
570 pxor \TMP1, \XMM1
571 pxor \TMP1, \XMM2
572 pxor \TMP1, \XMM3
573 pxor \TMP1, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800574 movdqa \TMP3, \TMP5
575 pshufd $78, \TMP3, \TMP1
576 pxor \TMP3, \TMP1
577 movdqa \TMP1, HashKey_k(%rsp)
578 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
579# TMP5 = HashKey^2<<1 (mod poly)
580 movdqa \TMP5, HashKey_2(%rsp)
581# HashKey_2 = HashKey^2<<1 (mod poly)
582 pshufd $78, \TMP5, \TMP1
583 pxor \TMP5, \TMP1
584 movdqa \TMP1, HashKey_2_k(%rsp)
585.irpc index, 1234 # do 4 rounds
586 movaps 0x10*\index(%arg1), \TMP1
587 AESENC \TMP1, \XMM1
588 AESENC \TMP1, \XMM2
589 AESENC \TMP1, \XMM3
590 AESENC \TMP1, \XMM4
591.endr
592 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
593# TMP5 = HashKey^3<<1 (mod poly)
594 movdqa \TMP5, HashKey_3(%rsp)
595 pshufd $78, \TMP5, \TMP1
596 pxor \TMP5, \TMP1
597 movdqa \TMP1, HashKey_3_k(%rsp)
598.irpc index, 56789 # do next 5 rounds
599 movaps 0x10*\index(%arg1), \TMP1
600 AESENC \TMP1, \XMM1
601 AESENC \TMP1, \XMM2
602 AESENC \TMP1, \XMM3
603 AESENC \TMP1, \XMM4
604.endr
605 GHASH_MUL \TMP5, \TMP3, \TMP1, \TMP2, \TMP4, \TMP6, \TMP7
606# TMP5 = HashKey^3<<1 (mod poly)
607 movdqa \TMP5, HashKey_4(%rsp)
608 pshufd $78, \TMP5, \TMP1
609 pxor \TMP5, \TMP1
610 movdqa \TMP1, HashKey_4_k(%rsp)
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500611 lea 0xa0(%arg1),%r10
612 mov keysize,%eax
613 shr $2,%eax # 128->4, 192->6, 256->8
614 sub $4,%eax # 128->0, 192->2, 256->4
615 jz aes_loop_pre_enc_done\num_initial_blocks
616
617aes_loop_pre_enc\num_initial_blocks:
618 MOVADQ (%r10),\TMP2
619.irpc index, 1234
620 AESENC \TMP2, %xmm\index
621.endr
622 add $16,%r10
623 sub $1,%eax
624 jnz aes_loop_pre_enc\num_initial_blocks
625
626aes_loop_pre_enc_done\num_initial_blocks:
627 MOVADQ (%r10), \TMP2
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800628 AESENCLAST \TMP2, \XMM1
629 AESENCLAST \TMP2, \XMM2
630 AESENCLAST \TMP2, \XMM3
631 AESENCLAST \TMP2, \XMM4
632 movdqu 16*0(%arg3 , %r11 , 1), \TMP1
633 pxor \TMP1, \XMM1
634 movdqu 16*1(%arg3 , %r11 , 1), \TMP1
635 pxor \TMP1, \XMM2
636 movdqu 16*2(%arg3 , %r11 , 1), \TMP1
637 pxor \TMP1, \XMM3
638 movdqu 16*3(%arg3 , %r11 , 1), \TMP1
639 pxor \TMP1, \XMM4
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400640 movdqu \XMM1, 16*0(%arg2 , %r11 , 1)
641 movdqu \XMM2, 16*1(%arg2 , %r11 , 1)
642 movdqu \XMM3, 16*2(%arg2 , %r11 , 1)
643 movdqu \XMM4, 16*3(%arg2 , %r11 , 1)
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800644
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400645 add $64, %r11
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800646 PSHUFB_XMM %xmm14, \XMM1 # perform a 16 byte swap
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400647 pxor \XMMDst, \XMM1
648# combine GHASHed value with the corresponding ciphertext
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800649 PSHUFB_XMM %xmm14, \XMM2 # perform a 16 byte swap
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800650 PSHUFB_XMM %xmm14, \XMM3 # perform a 16 byte swap
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800651 PSHUFB_XMM %xmm14, \XMM4 # perform a 16 byte swap
652
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400653_initial_blocks_done\num_initial_blocks\operation:
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800654
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400655.endm
656
657/*
658* encrypt 4 blocks at a time
659* ghash the 4 previously encrypted ciphertext blocks
660* arg1, %arg2, %arg3 are used as pointers only, not modified
661* %r11 is the data offset value
662*/
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800663.macro GHASH_4_ENCRYPT_4_PARALLEL_ENC TMP1 TMP2 TMP3 TMP4 TMP5 \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400664TMP6 XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 operation
665
666 movdqa \XMM1, \XMM5
667 movdqa \XMM2, \XMM6
668 movdqa \XMM3, \XMM7
669 movdqa \XMM4, \XMM8
670
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800671 movdqa SHUF_MASK(%rip), %xmm15
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400672 # multiply TMP5 * HashKey using karatsuba
673
674 movdqa \XMM5, \TMP4
675 pshufd $78, \XMM5, \TMP6
676 pxor \XMM5, \TMP6
677 paddd ONE(%rip), \XMM0 # INCR CNT
678 movdqa HashKey_4(%rsp), \TMP5
679 PCLMULQDQ 0x11, \TMP5, \TMP4 # TMP4 = a1*b1
680 movdqa \XMM0, \XMM1
681 paddd ONE(%rip), \XMM0 # INCR CNT
682 movdqa \XMM0, \XMM2
683 paddd ONE(%rip), \XMM0 # INCR CNT
684 movdqa \XMM0, \XMM3
685 paddd ONE(%rip), \XMM0 # INCR CNT
686 movdqa \XMM0, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800687 PSHUFB_XMM %xmm15, \XMM1 # perform a 16 byte swap
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400688 PCLMULQDQ 0x00, \TMP5, \XMM5 # XMM5 = a0*b0
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800689 PSHUFB_XMM %xmm15, \XMM2 # perform a 16 byte swap
690 PSHUFB_XMM %xmm15, \XMM3 # perform a 16 byte swap
691 PSHUFB_XMM %xmm15, \XMM4 # perform a 16 byte swap
692
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400693 pxor (%arg1), \XMM1
694 pxor (%arg1), \XMM2
695 pxor (%arg1), \XMM3
696 pxor (%arg1), \XMM4
697 movdqa HashKey_4_k(%rsp), \TMP5
698 PCLMULQDQ 0x00, \TMP5, \TMP6 # TMP6 = (a1+a0)*(b1+b0)
699 movaps 0x10(%arg1), \TMP1
700 AESENC \TMP1, \XMM1 # Round 1
701 AESENC \TMP1, \XMM2
702 AESENC \TMP1, \XMM3
703 AESENC \TMP1, \XMM4
704 movaps 0x20(%arg1), \TMP1
705 AESENC \TMP1, \XMM1 # Round 2
706 AESENC \TMP1, \XMM2
707 AESENC \TMP1, \XMM3
708 AESENC \TMP1, \XMM4
709 movdqa \XMM6, \TMP1
710 pshufd $78, \XMM6, \TMP2
711 pxor \XMM6, \TMP2
712 movdqa HashKey_3(%rsp), \TMP5
713 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1 * b1
714 movaps 0x30(%arg1), \TMP3
715 AESENC \TMP3, \XMM1 # Round 3
716 AESENC \TMP3, \XMM2
717 AESENC \TMP3, \XMM3
718 AESENC \TMP3, \XMM4
719 PCLMULQDQ 0x00, \TMP5, \XMM6 # XMM6 = a0*b0
720 movaps 0x40(%arg1), \TMP3
721 AESENC \TMP3, \XMM1 # Round 4
722 AESENC \TMP3, \XMM2
723 AESENC \TMP3, \XMM3
724 AESENC \TMP3, \XMM4
725 movdqa HashKey_3_k(%rsp), \TMP5
726 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
727 movaps 0x50(%arg1), \TMP3
728 AESENC \TMP3, \XMM1 # Round 5
729 AESENC \TMP3, \XMM2
730 AESENC \TMP3, \XMM3
731 AESENC \TMP3, \XMM4
732 pxor \TMP1, \TMP4
733# accumulate the results in TMP4:XMM5, TMP6 holds the middle part
734 pxor \XMM6, \XMM5
735 pxor \TMP2, \TMP6
736 movdqa \XMM7, \TMP1
737 pshufd $78, \XMM7, \TMP2
738 pxor \XMM7, \TMP2
739 movdqa HashKey_2(%rsp ), \TMP5
740
741 # Multiply TMP5 * HashKey using karatsuba
742
743 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
744 movaps 0x60(%arg1), \TMP3
745 AESENC \TMP3, \XMM1 # Round 6
746 AESENC \TMP3, \XMM2
747 AESENC \TMP3, \XMM3
748 AESENC \TMP3, \XMM4
749 PCLMULQDQ 0x00, \TMP5, \XMM7 # XMM7 = a0*b0
750 movaps 0x70(%arg1), \TMP3
751 AESENC \TMP3, \XMM1 # Round 7
752 AESENC \TMP3, \XMM2
753 AESENC \TMP3, \XMM3
754 AESENC \TMP3, \XMM4
755 movdqa HashKey_2_k(%rsp), \TMP5
756 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
757 movaps 0x80(%arg1), \TMP3
758 AESENC \TMP3, \XMM1 # Round 8
759 AESENC \TMP3, \XMM2
760 AESENC \TMP3, \XMM3
761 AESENC \TMP3, \XMM4
762 pxor \TMP1, \TMP4
763# accumulate the results in TMP4:XMM5, TMP6 holds the middle part
764 pxor \XMM7, \XMM5
765 pxor \TMP2, \TMP6
766
767 # Multiply XMM8 * HashKey
768 # XMM8 and TMP5 hold the values for the two operands
769
770 movdqa \XMM8, \TMP1
771 pshufd $78, \XMM8, \TMP2
772 pxor \XMM8, \TMP2
773 movdqa HashKey(%rsp), \TMP5
774 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
775 movaps 0x90(%arg1), \TMP3
776 AESENC \TMP3, \XMM1 # Round 9
777 AESENC \TMP3, \XMM2
778 AESENC \TMP3, \XMM3
779 AESENC \TMP3, \XMM4
780 PCLMULQDQ 0x00, \TMP5, \XMM8 # XMM8 = a0*b0
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500781 lea 0xa0(%arg1),%r10
782 mov keysize,%eax
783 shr $2,%eax # 128->4, 192->6, 256->8
784 sub $4,%eax # 128->0, 192->2, 256->4
785 jz aes_loop_par_enc_done
786
787aes_loop_par_enc:
788 MOVADQ (%r10),\TMP3
789.irpc index, 1234
790 AESENC \TMP3, %xmm\index
791.endr
792 add $16,%r10
793 sub $1,%eax
794 jnz aes_loop_par_enc
795
796aes_loop_par_enc_done:
797 MOVADQ (%r10), \TMP3
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400798 AESENCLAST \TMP3, \XMM1 # Round 10
799 AESENCLAST \TMP3, \XMM2
800 AESENCLAST \TMP3, \XMM3
801 AESENCLAST \TMP3, \XMM4
802 movdqa HashKey_k(%rsp), \TMP5
803 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
804 movdqu (%arg3,%r11,1), \TMP3
805 pxor \TMP3, \XMM1 # Ciphertext/Plaintext XOR EK
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400806 movdqu 16(%arg3,%r11,1), \TMP3
807 pxor \TMP3, \XMM2 # Ciphertext/Plaintext XOR EK
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400808 movdqu 32(%arg3,%r11,1), \TMP3
809 pxor \TMP3, \XMM3 # Ciphertext/Plaintext XOR EK
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400810 movdqu 48(%arg3,%r11,1), \TMP3
811 pxor \TMP3, \XMM4 # Ciphertext/Plaintext XOR EK
Tadeusz Struk3c097b82010-12-13 19:51:15 +0800812 movdqu \XMM1, (%arg2,%r11,1) # Write to the ciphertext buffer
813 movdqu \XMM2, 16(%arg2,%r11,1) # Write to the ciphertext buffer
814 movdqu \XMM3, 32(%arg2,%r11,1) # Write to the ciphertext buffer
815 movdqu \XMM4, 48(%arg2,%r11,1) # Write to the ciphertext buffer
816 PSHUFB_XMM %xmm15, \XMM1 # perform a 16 byte swap
817 PSHUFB_XMM %xmm15, \XMM2 # perform a 16 byte swap
818 PSHUFB_XMM %xmm15, \XMM3 # perform a 16 byte swap
819 PSHUFB_XMM %xmm15, \XMM4 # perform a 16 byte swap
820
821 pxor \TMP4, \TMP1
822 pxor \XMM8, \XMM5
823 pxor \TMP6, \TMP2
824 pxor \TMP1, \TMP2
825 pxor \XMM5, \TMP2
826 movdqa \TMP2, \TMP3
827 pslldq $8, \TMP3 # left shift TMP3 2 DWs
828 psrldq $8, \TMP2 # right shift TMP2 2 DWs
829 pxor \TMP3, \XMM5
830 pxor \TMP2, \TMP1 # accumulate the results in TMP1:XMM5
831
832 # first phase of reduction
833
834 movdqa \XMM5, \TMP2
835 movdqa \XMM5, \TMP3
836 movdqa \XMM5, \TMP4
837# move XMM5 into TMP2, TMP3, TMP4 in order to perform shifts independently
838 pslld $31, \TMP2 # packed right shift << 31
839 pslld $30, \TMP3 # packed right shift << 30
840 pslld $25, \TMP4 # packed right shift << 25
841 pxor \TMP3, \TMP2 # xor the shifted versions
842 pxor \TMP4, \TMP2
843 movdqa \TMP2, \TMP5
844 psrldq $4, \TMP5 # right shift T5 1 DW
845 pslldq $12, \TMP2 # left shift T2 3 DWs
846 pxor \TMP2, \XMM5
847
848 # second phase of reduction
849
850 movdqa \XMM5,\TMP2 # make 3 copies of XMM5 into TMP2, TMP3, TMP4
851 movdqa \XMM5,\TMP3
852 movdqa \XMM5,\TMP4
853 psrld $1, \TMP2 # packed left shift >>1
854 psrld $2, \TMP3 # packed left shift >>2
855 psrld $7, \TMP4 # packed left shift >>7
856 pxor \TMP3,\TMP2 # xor the shifted versions
857 pxor \TMP4,\TMP2
858 pxor \TMP5, \TMP2
859 pxor \TMP2, \XMM5
860 pxor \TMP1, \XMM5 # result is in TMP1
861
862 pxor \XMM5, \XMM1
863.endm
864
865/*
866* decrypt 4 blocks at a time
867* ghash the 4 previously decrypted ciphertext blocks
868* arg1, %arg2, %arg3 are used as pointers only, not modified
869* %r11 is the data offset value
870*/
871.macro GHASH_4_ENCRYPT_4_PARALLEL_DEC TMP1 TMP2 TMP3 TMP4 TMP5 \
872TMP6 XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 XMM8 operation
873
874 movdqa \XMM1, \XMM5
875 movdqa \XMM2, \XMM6
876 movdqa \XMM3, \XMM7
877 movdqa \XMM4, \XMM8
878
879 movdqa SHUF_MASK(%rip), %xmm15
880 # multiply TMP5 * HashKey using karatsuba
881
882 movdqa \XMM5, \TMP4
883 pshufd $78, \XMM5, \TMP6
884 pxor \XMM5, \TMP6
885 paddd ONE(%rip), \XMM0 # INCR CNT
886 movdqa HashKey_4(%rsp), \TMP5
887 PCLMULQDQ 0x11, \TMP5, \TMP4 # TMP4 = a1*b1
888 movdqa \XMM0, \XMM1
889 paddd ONE(%rip), \XMM0 # INCR CNT
890 movdqa \XMM0, \XMM2
891 paddd ONE(%rip), \XMM0 # INCR CNT
892 movdqa \XMM0, \XMM3
893 paddd ONE(%rip), \XMM0 # INCR CNT
894 movdqa \XMM0, \XMM4
895 PSHUFB_XMM %xmm15, \XMM1 # perform a 16 byte swap
896 PCLMULQDQ 0x00, \TMP5, \XMM5 # XMM5 = a0*b0
897 PSHUFB_XMM %xmm15, \XMM2 # perform a 16 byte swap
898 PSHUFB_XMM %xmm15, \XMM3 # perform a 16 byte swap
899 PSHUFB_XMM %xmm15, \XMM4 # perform a 16 byte swap
900
901 pxor (%arg1), \XMM1
902 pxor (%arg1), \XMM2
903 pxor (%arg1), \XMM3
904 pxor (%arg1), \XMM4
905 movdqa HashKey_4_k(%rsp), \TMP5
906 PCLMULQDQ 0x00, \TMP5, \TMP6 # TMP6 = (a1+a0)*(b1+b0)
907 movaps 0x10(%arg1), \TMP1
908 AESENC \TMP1, \XMM1 # Round 1
909 AESENC \TMP1, \XMM2
910 AESENC \TMP1, \XMM3
911 AESENC \TMP1, \XMM4
912 movaps 0x20(%arg1), \TMP1
913 AESENC \TMP1, \XMM1 # Round 2
914 AESENC \TMP1, \XMM2
915 AESENC \TMP1, \XMM3
916 AESENC \TMP1, \XMM4
917 movdqa \XMM6, \TMP1
918 pshufd $78, \XMM6, \TMP2
919 pxor \XMM6, \TMP2
920 movdqa HashKey_3(%rsp), \TMP5
921 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1 * b1
922 movaps 0x30(%arg1), \TMP3
923 AESENC \TMP3, \XMM1 # Round 3
924 AESENC \TMP3, \XMM2
925 AESENC \TMP3, \XMM3
926 AESENC \TMP3, \XMM4
927 PCLMULQDQ 0x00, \TMP5, \XMM6 # XMM6 = a0*b0
928 movaps 0x40(%arg1), \TMP3
929 AESENC \TMP3, \XMM1 # Round 4
930 AESENC \TMP3, \XMM2
931 AESENC \TMP3, \XMM3
932 AESENC \TMP3, \XMM4
933 movdqa HashKey_3_k(%rsp), \TMP5
934 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
935 movaps 0x50(%arg1), \TMP3
936 AESENC \TMP3, \XMM1 # Round 5
937 AESENC \TMP3, \XMM2
938 AESENC \TMP3, \XMM3
939 AESENC \TMP3, \XMM4
940 pxor \TMP1, \TMP4
941# accumulate the results in TMP4:XMM5, TMP6 holds the middle part
942 pxor \XMM6, \XMM5
943 pxor \TMP2, \TMP6
944 movdqa \XMM7, \TMP1
945 pshufd $78, \XMM7, \TMP2
946 pxor \XMM7, \TMP2
947 movdqa HashKey_2(%rsp ), \TMP5
948
949 # Multiply TMP5 * HashKey using karatsuba
950
951 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
952 movaps 0x60(%arg1), \TMP3
953 AESENC \TMP3, \XMM1 # Round 6
954 AESENC \TMP3, \XMM2
955 AESENC \TMP3, \XMM3
956 AESENC \TMP3, \XMM4
957 PCLMULQDQ 0x00, \TMP5, \XMM7 # XMM7 = a0*b0
958 movaps 0x70(%arg1), \TMP3
959 AESENC \TMP3, \XMM1 # Round 7
960 AESENC \TMP3, \XMM2
961 AESENC \TMP3, \XMM3
962 AESENC \TMP3, \XMM4
963 movdqa HashKey_2_k(%rsp), \TMP5
964 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
965 movaps 0x80(%arg1), \TMP3
966 AESENC \TMP3, \XMM1 # Round 8
967 AESENC \TMP3, \XMM2
968 AESENC \TMP3, \XMM3
969 AESENC \TMP3, \XMM4
970 pxor \TMP1, \TMP4
971# accumulate the results in TMP4:XMM5, TMP6 holds the middle part
972 pxor \XMM7, \XMM5
973 pxor \TMP2, \TMP6
974
975 # Multiply XMM8 * HashKey
976 # XMM8 and TMP5 hold the values for the two operands
977
978 movdqa \XMM8, \TMP1
979 pshufd $78, \XMM8, \TMP2
980 pxor \XMM8, \TMP2
981 movdqa HashKey(%rsp), \TMP5
982 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
983 movaps 0x90(%arg1), \TMP3
984 AESENC \TMP3, \XMM1 # Round 9
985 AESENC \TMP3, \XMM2
986 AESENC \TMP3, \XMM3
987 AESENC \TMP3, \XMM4
988 PCLMULQDQ 0x00, \TMP5, \XMM8 # XMM8 = a0*b0
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500989 lea 0xa0(%arg1),%r10
990 mov keysize,%eax
991 shr $2,%eax # 128->4, 192->6, 256->8
992 sub $4,%eax # 128->0, 192->2, 256->4
993 jz aes_loop_par_dec_done
994
995aes_loop_par_dec:
996 MOVADQ (%r10),\TMP3
997.irpc index, 1234
998 AESENC \TMP3, %xmm\index
999.endr
1000 add $16,%r10
1001 sub $1,%eax
1002 jnz aes_loop_par_dec
1003
1004aes_loop_par_dec_done:
1005 MOVADQ (%r10), \TMP3
1006 AESENCLAST \TMP3, \XMM1 # last round
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001007 AESENCLAST \TMP3, \XMM2
1008 AESENCLAST \TMP3, \XMM3
1009 AESENCLAST \TMP3, \XMM4
1010 movdqa HashKey_k(%rsp), \TMP5
1011 PCLMULQDQ 0x00, \TMP5, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
1012 movdqu (%arg3,%r11,1), \TMP3
1013 pxor \TMP3, \XMM1 # Ciphertext/Plaintext XOR EK
1014 movdqu \XMM1, (%arg2,%r11,1) # Write to plaintext buffer
1015 movdqa \TMP3, \XMM1
1016 movdqu 16(%arg3,%r11,1), \TMP3
1017 pxor \TMP3, \XMM2 # Ciphertext/Plaintext XOR EK
1018 movdqu \XMM2, 16(%arg2,%r11,1) # Write to plaintext buffer
1019 movdqa \TMP3, \XMM2
1020 movdqu 32(%arg3,%r11,1), \TMP3
1021 pxor \TMP3, \XMM3 # Ciphertext/Plaintext XOR EK
1022 movdqu \XMM3, 32(%arg2,%r11,1) # Write to plaintext buffer
1023 movdqa \TMP3, \XMM3
1024 movdqu 48(%arg3,%r11,1), \TMP3
1025 pxor \TMP3, \XMM4 # Ciphertext/Plaintext XOR EK
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001026 movdqu \XMM4, 48(%arg2,%r11,1) # Write to plaintext buffer
1027 movdqa \TMP3, \XMM4
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001028 PSHUFB_XMM %xmm15, \XMM1 # perform a 16 byte swap
1029 PSHUFB_XMM %xmm15, \XMM2 # perform a 16 byte swap
1030 PSHUFB_XMM %xmm15, \XMM3 # perform a 16 byte swap
1031 PSHUFB_XMM %xmm15, \XMM4 # perform a 16 byte swap
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001032
1033 pxor \TMP4, \TMP1
1034 pxor \XMM8, \XMM5
1035 pxor \TMP6, \TMP2
1036 pxor \TMP1, \TMP2
1037 pxor \XMM5, \TMP2
1038 movdqa \TMP2, \TMP3
1039 pslldq $8, \TMP3 # left shift TMP3 2 DWs
1040 psrldq $8, \TMP2 # right shift TMP2 2 DWs
1041 pxor \TMP3, \XMM5
1042 pxor \TMP2, \TMP1 # accumulate the results in TMP1:XMM5
1043
1044 # first phase of reduction
1045
1046 movdqa \XMM5, \TMP2
1047 movdqa \XMM5, \TMP3
1048 movdqa \XMM5, \TMP4
1049# move XMM5 into TMP2, TMP3, TMP4 in order to perform shifts independently
1050 pslld $31, \TMP2 # packed right shift << 31
1051 pslld $30, \TMP3 # packed right shift << 30
1052 pslld $25, \TMP4 # packed right shift << 25
1053 pxor \TMP3, \TMP2 # xor the shifted versions
1054 pxor \TMP4, \TMP2
1055 movdqa \TMP2, \TMP5
1056 psrldq $4, \TMP5 # right shift T5 1 DW
1057 pslldq $12, \TMP2 # left shift T2 3 DWs
1058 pxor \TMP2, \XMM5
1059
1060 # second phase of reduction
1061
1062 movdqa \XMM5,\TMP2 # make 3 copies of XMM5 into TMP2, TMP3, TMP4
1063 movdqa \XMM5,\TMP3
1064 movdqa \XMM5,\TMP4
1065 psrld $1, \TMP2 # packed left shift >>1
1066 psrld $2, \TMP3 # packed left shift >>2
1067 psrld $7, \TMP4 # packed left shift >>7
1068 pxor \TMP3,\TMP2 # xor the shifted versions
1069 pxor \TMP4,\TMP2
1070 pxor \TMP5, \TMP2
1071 pxor \TMP2, \XMM5
1072 pxor \TMP1, \XMM5 # result is in TMP1
1073
1074 pxor \XMM5, \XMM1
1075.endm
1076
1077/* GHASH the last 4 ciphertext blocks. */
1078.macro GHASH_LAST_4 TMP1 TMP2 TMP3 TMP4 TMP5 TMP6 \
1079TMP7 XMM1 XMM2 XMM3 XMM4 XMMDst
1080
1081 # Multiply TMP6 * HashKey (using Karatsuba)
1082
1083 movdqa \XMM1, \TMP6
1084 pshufd $78, \XMM1, \TMP2
1085 pxor \XMM1, \TMP2
1086 movdqa HashKey_4(%rsp), \TMP5
1087 PCLMULQDQ 0x11, \TMP5, \TMP6 # TMP6 = a1*b1
1088 PCLMULQDQ 0x00, \TMP5, \XMM1 # XMM1 = a0*b0
1089 movdqa HashKey_4_k(%rsp), \TMP4
1090 PCLMULQDQ 0x00, \TMP4, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
1091 movdqa \XMM1, \XMMDst
1092 movdqa \TMP2, \XMM1 # result in TMP6, XMMDst, XMM1
1093
1094 # Multiply TMP1 * HashKey (using Karatsuba)
1095
1096 movdqa \XMM2, \TMP1
1097 pshufd $78, \XMM2, \TMP2
1098 pxor \XMM2, \TMP2
1099 movdqa HashKey_3(%rsp), \TMP5
1100 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
1101 PCLMULQDQ 0x00, \TMP5, \XMM2 # XMM2 = a0*b0
1102 movdqa HashKey_3_k(%rsp), \TMP4
1103 PCLMULQDQ 0x00, \TMP4, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
1104 pxor \TMP1, \TMP6
1105 pxor \XMM2, \XMMDst
1106 pxor \TMP2, \XMM1
1107# results accumulated in TMP6, XMMDst, XMM1
1108
1109 # Multiply TMP1 * HashKey (using Karatsuba)
1110
1111 movdqa \XMM3, \TMP1
1112 pshufd $78, \XMM3, \TMP2
1113 pxor \XMM3, \TMP2
1114 movdqa HashKey_2(%rsp), \TMP5
1115 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
1116 PCLMULQDQ 0x00, \TMP5, \XMM3 # XMM3 = a0*b0
1117 movdqa HashKey_2_k(%rsp), \TMP4
1118 PCLMULQDQ 0x00, \TMP4, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
1119 pxor \TMP1, \TMP6
1120 pxor \XMM3, \XMMDst
1121 pxor \TMP2, \XMM1 # results accumulated in TMP6, XMMDst, XMM1
1122
1123 # Multiply TMP1 * HashKey (using Karatsuba)
1124 movdqa \XMM4, \TMP1
1125 pshufd $78, \XMM4, \TMP2
1126 pxor \XMM4, \TMP2
1127 movdqa HashKey(%rsp), \TMP5
1128 PCLMULQDQ 0x11, \TMP5, \TMP1 # TMP1 = a1*b1
1129 PCLMULQDQ 0x00, \TMP5, \XMM4 # XMM4 = a0*b0
1130 movdqa HashKey_k(%rsp), \TMP4
1131 PCLMULQDQ 0x00, \TMP4, \TMP2 # TMP2 = (a1+a0)*(b1+b0)
1132 pxor \TMP1, \TMP6
1133 pxor \XMM4, \XMMDst
1134 pxor \XMM1, \TMP2
1135 pxor \TMP6, \TMP2
1136 pxor \XMMDst, \TMP2
1137 # middle section of the temp results combined as in karatsuba algorithm
1138 movdqa \TMP2, \TMP4
1139 pslldq $8, \TMP4 # left shift TMP4 2 DWs
1140 psrldq $8, \TMP2 # right shift TMP2 2 DWs
1141 pxor \TMP4, \XMMDst
1142 pxor \TMP2, \TMP6
1143# TMP6:XMMDst holds the result of the accumulated carry-less multiplications
1144 # first phase of the reduction
1145 movdqa \XMMDst, \TMP2
1146 movdqa \XMMDst, \TMP3
1147 movdqa \XMMDst, \TMP4
1148# move XMMDst into TMP2, TMP3, TMP4 in order to perform 3 shifts independently
1149 pslld $31, \TMP2 # packed right shifting << 31
1150 pslld $30, \TMP3 # packed right shifting << 30
1151 pslld $25, \TMP4 # packed right shifting << 25
1152 pxor \TMP3, \TMP2 # xor the shifted versions
1153 pxor \TMP4, \TMP2
1154 movdqa \TMP2, \TMP7
1155 psrldq $4, \TMP7 # right shift TMP7 1 DW
1156 pslldq $12, \TMP2 # left shift TMP2 3 DWs
1157 pxor \TMP2, \XMMDst
1158
1159 # second phase of the reduction
1160 movdqa \XMMDst, \TMP2
1161 # make 3 copies of XMMDst for doing 3 shift operations
1162 movdqa \XMMDst, \TMP3
1163 movdqa \XMMDst, \TMP4
1164 psrld $1, \TMP2 # packed left shift >> 1
1165 psrld $2, \TMP3 # packed left shift >> 2
1166 psrld $7, \TMP4 # packed left shift >> 7
1167 pxor \TMP3, \TMP2 # xor the shifted versions
1168 pxor \TMP4, \TMP2
1169 pxor \TMP7, \TMP2
1170 pxor \TMP2, \XMMDst
1171 pxor \TMP6, \XMMDst # reduced result is in XMMDst
1172.endm
1173
Timothy McCaffreye31ac322015-01-13 13:16:43 -05001174
1175/* Encryption of a single block
1176* uses eax & r10
1177*/
1178
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001179.macro ENCRYPT_SINGLE_BLOCK XMM0 TMP1
1180
Timothy McCaffreye31ac322015-01-13 13:16:43 -05001181 pxor (%arg1), \XMM0
1182 mov keysize,%eax
1183 shr $2,%eax # 128->4, 192->6, 256->8
1184 add $5,%eax # 128->9, 192->11, 256->13
1185 lea 16(%arg1), %r10 # get first expanded key address
1186
1187_esb_loop_\@:
1188 MOVADQ (%r10),\TMP1
1189 AESENC \TMP1,\XMM0
1190 add $16,%r10
1191 sub $1,%eax
1192 jnz _esb_loop_\@
1193
1194 MOVADQ (%r10),\TMP1
1195 AESENCLAST \TMP1,\XMM0
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001196.endm
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001197/*****************************************************************************
1198* void aesni_gcm_dec(void *aes_ctx, // AES Key schedule. Starts on a 16 byte boundary.
1199* u8 *out, // Plaintext output. Encrypt in-place is allowed.
1200* const u8 *in, // Ciphertext input
1201* u64 plaintext_len, // Length of data in bytes for decryption.
1202* u8 *iv, // Pre-counter block j0: 4 byte salt (from Security Association)
1203* // concatenated with 8 byte Initialisation Vector (from IPSec ESP Payload)
1204* // concatenated with 0x00000001. 16-byte aligned pointer.
1205* u8 *hash_subkey, // H, the Hash sub key input. Data starts on a 16-byte boundary.
1206* const u8 *aad, // Additional Authentication Data (AAD)
1207* u64 aad_len, // Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 bytes
1208* u8 *auth_tag, // Authenticated Tag output. The driver will compare this to the
1209* // given authentication tag and only return the plaintext if they match.
1210* u64 auth_tag_len); // Authenticated Tag Length in bytes. Valid values are 16
1211* // (most likely), 12 or 8.
1212*
1213* Assumptions:
1214*
1215* keys:
1216* keys are pre-expanded and aligned to 16 bytes. we are using the first
1217* set of 11 keys in the data structure void *aes_ctx
1218*
1219* iv:
1220* 0 1 2 3
1221* 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
1222* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1223* | Salt (From the SA) |
1224* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1225* | Initialization Vector |
1226* | (This is the sequence number from IPSec header) |
1227* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1228* | 0x1 |
1229* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1230*
1231*
1232*
1233* AAD:
1234* AAD padded to 128 bits with 0
1235* for example, assume AAD is a u32 vector
1236*
1237* if AAD is 8 bytes:
1238* AAD[3] = {A0, A1};
1239* padded AAD in xmm register = {A1 A0 0 0}
1240*
1241* 0 1 2 3
1242* 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
1243* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1244* | SPI (A1) |
1245* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1246* | 32-bit Sequence Number (A0) |
1247* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1248* | 0x0 |
1249* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1250*
1251* AAD Format with 32-bit Sequence Number
1252*
1253* if AAD is 12 bytes:
1254* AAD[3] = {A0, A1, A2};
1255* padded AAD in xmm register = {A2 A1 A0 0}
1256*
1257* 0 1 2 3
1258* 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
1259* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1260* 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
1261* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1262* | SPI (A2) |
1263* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1264* | 64-bit Extended Sequence Number {A1,A0} |
1265* | |
1266* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1267* | 0x0 |
1268* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1269*
1270* AAD Format with 64-bit Extended Sequence Number
1271*
1272* aadLen:
1273* from the definition of the spec, aadLen can only be 8 or 12 bytes.
1274* The code supports 16 too but for other sizes, the code will fail.
1275*
1276* TLen:
1277* from the definition of the spec, TLen can only be 8, 12 or 16 bytes.
1278* For other sizes, the code will fail.
1279*
1280* poly = x^128 + x^127 + x^126 + x^121 + 1
1281*
1282*****************************************************************************/
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001283ENTRY(aesni_gcm_dec)
1284 push %r12
1285 push %r13
1286 push %r14
1287 mov %rsp, %r14
1288/*
1289* states of %xmm registers %xmm6:%xmm15 not saved
1290* all %xmm registers are clobbered
1291*/
1292 sub $VARIABLE_OFFSET, %rsp
1293 and $~63, %rsp # align rsp to 64 bytes
1294 mov %arg6, %r12
1295 movdqu (%r12), %xmm13 # %xmm13 = HashKey
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001296 movdqa SHUF_MASK(%rip), %xmm2
1297 PSHUFB_XMM %xmm2, %xmm13
1298
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001299
1300# Precompute HashKey<<1 (mod poly) from the hash key (required for GHASH)
1301
1302 movdqa %xmm13, %xmm2
1303 psllq $1, %xmm13
1304 psrlq $63, %xmm2
1305 movdqa %xmm2, %xmm1
1306 pslldq $8, %xmm2
1307 psrldq $8, %xmm1
1308 por %xmm2, %xmm13
1309
1310 # Reduction
1311
1312 pshufd $0x24, %xmm1, %xmm2
1313 pcmpeqd TWOONE(%rip), %xmm2
1314 pand POLY(%rip), %xmm2
1315 pxor %xmm2, %xmm13 # %xmm13 holds the HashKey<<1 (mod poly)
1316
1317
1318 # Decrypt first few blocks
1319
1320 movdqa %xmm13, HashKey(%rsp) # store HashKey<<1 (mod poly)
1321 mov %arg4, %r13 # save the number of bytes of plaintext/ciphertext
1322 and $-16, %r13 # %r13 = %r13 - (%r13 mod 16)
1323 mov %r13, %r12
1324 and $(3<<4), %r12
1325 jz _initial_num_blocks_is_0_decrypt
1326 cmp $(2<<4), %r12
1327 jb _initial_num_blocks_is_1_decrypt
1328 je _initial_num_blocks_is_2_decrypt
1329_initial_num_blocks_is_3_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001330 INITIAL_BLOCKS_DEC 3, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001331%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 5, 678, dec
1332 sub $48, %r13
1333 jmp _initial_blocks_decrypted
1334_initial_num_blocks_is_2_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001335 INITIAL_BLOCKS_DEC 2, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001336%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 6, 78, dec
1337 sub $32, %r13
1338 jmp _initial_blocks_decrypted
1339_initial_num_blocks_is_1_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001340 INITIAL_BLOCKS_DEC 1, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001341%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 7, 8, dec
1342 sub $16, %r13
1343 jmp _initial_blocks_decrypted
1344_initial_num_blocks_is_0_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001345 INITIAL_BLOCKS_DEC 0, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001346%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 8, 0, dec
1347_initial_blocks_decrypted:
1348 cmp $0, %r13
1349 je _zero_cipher_left_decrypt
1350 sub $64, %r13
1351 je _four_cipher_left_decrypt
1352_decrypt_by_4:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001353 GHASH_4_ENCRYPT_4_PARALLEL_DEC %xmm9, %xmm10, %xmm11, %xmm12, %xmm13, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001354%xmm14, %xmm0, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, dec
1355 add $64, %r11
1356 sub $64, %r13
1357 jne _decrypt_by_4
1358_four_cipher_left_decrypt:
1359 GHASH_LAST_4 %xmm9, %xmm10, %xmm11, %xmm12, %xmm13, %xmm14, \
1360%xmm15, %xmm1, %xmm2, %xmm3, %xmm4, %xmm8
1361_zero_cipher_left_decrypt:
1362 mov %arg4, %r13
1363 and $15, %r13 # %r13 = arg4 (mod 16)
1364 je _multiple_of_16_bytes_decrypt
1365
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001366 # Handle the last <16 byte block separately
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001367
1368 paddd ONE(%rip), %xmm0 # increment CNT to get Yn
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001369 movdqa SHUF_MASK(%rip), %xmm10
1370 PSHUFB_XMM %xmm10, %xmm0
1371
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001372 ENCRYPT_SINGLE_BLOCK %xmm0, %xmm1 # E(K, Yn)
1373 sub $16, %r11
1374 add %r13, %r11
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001375 movdqu (%arg3,%r11,1), %xmm1 # receive the last <16 byte block
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001376 lea SHIFT_MASK+16(%rip), %r12
1377 sub %r13, %r12
1378# adjust the shuffle mask pointer to be able to shift 16-%r13 bytes
1379# (%r13 is the number of bytes in plaintext mod 16)
1380 movdqu (%r12), %xmm2 # get the appropriate shuffle mask
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001381 PSHUFB_XMM %xmm2, %xmm1 # right shift 16-%r13 butes
1382
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001383 movdqa %xmm1, %xmm2
1384 pxor %xmm1, %xmm0 # Ciphertext XOR E(K, Yn)
1385 movdqu ALL_F-SHIFT_MASK(%r12), %xmm1
1386 # get the appropriate mask to mask out top 16-%r13 bytes of %xmm0
1387 pand %xmm1, %xmm0 # mask out top 16-%r13 bytes of %xmm0
1388 pand %xmm1, %xmm2
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001389 movdqa SHUF_MASK(%rip), %xmm10
1390 PSHUFB_XMM %xmm10 ,%xmm2
1391
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001392 pxor %xmm2, %xmm8
1393 GHASH_MUL %xmm8, %xmm13, %xmm9, %xmm10, %xmm11, %xmm5, %xmm6
1394 # GHASH computation for the last <16 byte block
1395 sub %r13, %r11
1396 add $16, %r11
1397
1398 # output %r13 bytes
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001399 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001400 cmp $8, %r13
1401 jle _less_than_8_bytes_left_decrypt
1402 mov %rax, (%arg2 , %r11, 1)
1403 add $8, %r11
1404 psrldq $8, %xmm0
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001405 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001406 sub $8, %r13
1407_less_than_8_bytes_left_decrypt:
1408 mov %al, (%arg2, %r11, 1)
1409 add $1, %r11
1410 shr $8, %rax
1411 sub $1, %r13
1412 jne _less_than_8_bytes_left_decrypt
1413_multiple_of_16_bytes_decrypt:
1414 mov arg8, %r12 # %r13 = aadLen (number of bytes)
1415 shl $3, %r12 # convert into number of bits
1416 movd %r12d, %xmm15 # len(A) in %xmm15
1417 shl $3, %arg4 # len(C) in bits (*128)
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001418 MOVQ_R64_XMM %arg4, %xmm1
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001419 pslldq $8, %xmm15 # %xmm15 = len(A)||0x0000000000000000
1420 pxor %xmm1, %xmm15 # %xmm15 = len(A)||len(C)
1421 pxor %xmm15, %xmm8
1422 GHASH_MUL %xmm8, %xmm13, %xmm9, %xmm10, %xmm11, %xmm5, %xmm6
1423 # final GHASH computation
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001424 movdqa SHUF_MASK(%rip), %xmm10
1425 PSHUFB_XMM %xmm10, %xmm8
1426
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001427 mov %arg5, %rax # %rax = *Y0
1428 movdqu (%rax), %xmm0 # %xmm0 = Y0
1429 ENCRYPT_SINGLE_BLOCK %xmm0, %xmm1 # E(K, Y0)
1430 pxor %xmm8, %xmm0
1431_return_T_decrypt:
1432 mov arg9, %r10 # %r10 = authTag
1433 mov arg10, %r11 # %r11 = auth_tag_len
1434 cmp $16, %r11
1435 je _T_16_decrypt
1436 cmp $12, %r11
1437 je _T_12_decrypt
1438_T_8_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001439 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001440 mov %rax, (%r10)
1441 jmp _return_T_done_decrypt
1442_T_12_decrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001443 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001444 mov %rax, (%r10)
1445 psrldq $8, %xmm0
1446 movd %xmm0, %eax
1447 mov %eax, 8(%r10)
1448 jmp _return_T_done_decrypt
1449_T_16_decrypt:
1450 movdqu %xmm0, (%r10)
1451_return_T_done_decrypt:
1452 mov %r14, %rsp
1453 pop %r14
1454 pop %r13
1455 pop %r12
1456 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001457ENDPROC(aesni_gcm_dec)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001458
1459
1460/*****************************************************************************
1461* void aesni_gcm_enc(void *aes_ctx, // AES Key schedule. Starts on a 16 byte boundary.
1462* u8 *out, // Ciphertext output. Encrypt in-place is allowed.
1463* const u8 *in, // Plaintext input
1464* u64 plaintext_len, // Length of data in bytes for encryption.
1465* u8 *iv, // Pre-counter block j0: 4 byte salt (from Security Association)
1466* // concatenated with 8 byte Initialisation Vector (from IPSec ESP Payload)
1467* // concatenated with 0x00000001. 16-byte aligned pointer.
1468* u8 *hash_subkey, // H, the Hash sub key input. Data starts on a 16-byte boundary.
1469* const u8 *aad, // Additional Authentication Data (AAD)
1470* u64 aad_len, // Length of AAD in bytes. With RFC4106 this is going to be 8 or 12 bytes
1471* u8 *auth_tag, // Authenticated Tag output.
1472* u64 auth_tag_len); // Authenticated Tag Length in bytes. Valid values are 16 (most likely),
1473* // 12 or 8.
1474*
1475* Assumptions:
1476*
1477* keys:
1478* keys are pre-expanded and aligned to 16 bytes. we are using the
1479* first set of 11 keys in the data structure void *aes_ctx
1480*
1481*
1482* iv:
1483* 0 1 2 3
1484* 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
1485* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1486* | Salt (From the SA) |
1487* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1488* | Initialization Vector |
1489* | (This is the sequence number from IPSec header) |
1490* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1491* | 0x1 |
1492* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1493*
1494*
1495*
1496* AAD:
1497* AAD padded to 128 bits with 0
1498* for example, assume AAD is a u32 vector
1499*
1500* if AAD is 8 bytes:
1501* AAD[3] = {A0, A1};
1502* padded AAD in xmm register = {A1 A0 0 0}
1503*
1504* 0 1 2 3
1505* 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
1506* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1507* | SPI (A1) |
1508* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1509* | 32-bit Sequence Number (A0) |
1510* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1511* | 0x0 |
1512* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1513*
1514* AAD Format with 32-bit Sequence Number
1515*
1516* if AAD is 12 bytes:
1517* AAD[3] = {A0, A1, A2};
1518* padded AAD in xmm register = {A2 A1 A0 0}
1519*
1520* 0 1 2 3
1521* 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
1522* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1523* | SPI (A2) |
1524* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1525* | 64-bit Extended Sequence Number {A1,A0} |
1526* | |
1527* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1528* | 0x0 |
1529* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1530*
1531* AAD Format with 64-bit Extended Sequence Number
1532*
1533* aadLen:
1534* from the definition of the spec, aadLen can only be 8 or 12 bytes.
1535* The code supports 16 too but for other sizes, the code will fail.
1536*
1537* TLen:
1538* from the definition of the spec, TLen can only be 8, 12 or 16 bytes.
1539* For other sizes, the code will fail.
1540*
1541* poly = x^128 + x^127 + x^126 + x^121 + 1
1542***************************************************************************/
1543ENTRY(aesni_gcm_enc)
1544 push %r12
1545 push %r13
1546 push %r14
1547 mov %rsp, %r14
1548#
1549# states of %xmm registers %xmm6:%xmm15 not saved
1550# all %xmm registers are clobbered
1551#
1552 sub $VARIABLE_OFFSET, %rsp
1553 and $~63, %rsp
1554 mov %arg6, %r12
1555 movdqu (%r12), %xmm13
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001556 movdqa SHUF_MASK(%rip), %xmm2
1557 PSHUFB_XMM %xmm2, %xmm13
1558
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001559
1560# precompute HashKey<<1 mod poly from the HashKey (required for GHASH)
1561
1562 movdqa %xmm13, %xmm2
1563 psllq $1, %xmm13
1564 psrlq $63, %xmm2
1565 movdqa %xmm2, %xmm1
1566 pslldq $8, %xmm2
1567 psrldq $8, %xmm1
1568 por %xmm2, %xmm13
1569
1570 # reduce HashKey<<1
1571
1572 pshufd $0x24, %xmm1, %xmm2
1573 pcmpeqd TWOONE(%rip), %xmm2
1574 pand POLY(%rip), %xmm2
1575 pxor %xmm2, %xmm13
1576 movdqa %xmm13, HashKey(%rsp)
1577 mov %arg4, %r13 # %xmm13 holds HashKey<<1 (mod poly)
1578 and $-16, %r13
1579 mov %r13, %r12
1580
1581 # Encrypt first few blocks
1582
1583 and $(3<<4), %r12
1584 jz _initial_num_blocks_is_0_encrypt
1585 cmp $(2<<4), %r12
1586 jb _initial_num_blocks_is_1_encrypt
1587 je _initial_num_blocks_is_2_encrypt
1588_initial_num_blocks_is_3_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001589 INITIAL_BLOCKS_ENC 3, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001590%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 5, 678, enc
1591 sub $48, %r13
1592 jmp _initial_blocks_encrypted
1593_initial_num_blocks_is_2_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001594 INITIAL_BLOCKS_ENC 2, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001595%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 6, 78, enc
1596 sub $32, %r13
1597 jmp _initial_blocks_encrypted
1598_initial_num_blocks_is_1_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001599 INITIAL_BLOCKS_ENC 1, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001600%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 7, 8, enc
1601 sub $16, %r13
1602 jmp _initial_blocks_encrypted
1603_initial_num_blocks_is_0_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001604 INITIAL_BLOCKS_ENC 0, %xmm9, %xmm10, %xmm13, %xmm11, %xmm12, %xmm0, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001605%xmm1, %xmm2, %xmm3, %xmm4, %xmm8, %xmm5, %xmm6, 8, 0, enc
1606_initial_blocks_encrypted:
1607
1608 # Main loop - Encrypt remaining blocks
1609
1610 cmp $0, %r13
1611 je _zero_cipher_left_encrypt
1612 sub $64, %r13
1613 je _four_cipher_left_encrypt
1614_encrypt_by_4_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001615 GHASH_4_ENCRYPT_4_PARALLEL_ENC %xmm9, %xmm10, %xmm11, %xmm12, %xmm13, \
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001616%xmm14, %xmm0, %xmm1, %xmm2, %xmm3, %xmm4, %xmm5, %xmm6, %xmm7, %xmm8, enc
1617 add $64, %r11
1618 sub $64, %r13
1619 jne _encrypt_by_4_encrypt
1620_four_cipher_left_encrypt:
1621 GHASH_LAST_4 %xmm9, %xmm10, %xmm11, %xmm12, %xmm13, %xmm14, \
1622%xmm15, %xmm1, %xmm2, %xmm3, %xmm4, %xmm8
1623_zero_cipher_left_encrypt:
1624 mov %arg4, %r13
1625 and $15, %r13 # %r13 = arg4 (mod 16)
1626 je _multiple_of_16_bytes_encrypt
1627
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001628 # Handle the last <16 Byte block separately
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001629 paddd ONE(%rip), %xmm0 # INCR CNT to get Yn
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001630 movdqa SHUF_MASK(%rip), %xmm10
1631 PSHUFB_XMM %xmm10, %xmm0
1632
Tadeusz Struk60af5202011-03-13 16:56:17 +08001633
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001634 ENCRYPT_SINGLE_BLOCK %xmm0, %xmm1 # Encrypt(K, Yn)
1635 sub $16, %r11
1636 add %r13, %r11
1637 movdqu (%arg3,%r11,1), %xmm1 # receive the last <16 byte blocks
1638 lea SHIFT_MASK+16(%rip), %r12
1639 sub %r13, %r12
1640 # adjust the shuffle mask pointer to be able to shift 16-r13 bytes
1641 # (%r13 is the number of bytes in plaintext mod 16)
1642 movdqu (%r12), %xmm2 # get the appropriate shuffle mask
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001643 PSHUFB_XMM %xmm2, %xmm1 # shift right 16-r13 byte
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001644 pxor %xmm1, %xmm0 # Plaintext XOR Encrypt(K, Yn)
1645 movdqu ALL_F-SHIFT_MASK(%r12), %xmm1
1646 # get the appropriate mask to mask out top 16-r13 bytes of xmm0
1647 pand %xmm1, %xmm0 # mask out top 16-r13 bytes of xmm0
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001648 movdqa SHUF_MASK(%rip), %xmm10
1649 PSHUFB_XMM %xmm10,%xmm0
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001650
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001651 pxor %xmm0, %xmm8
1652 GHASH_MUL %xmm8, %xmm13, %xmm9, %xmm10, %xmm11, %xmm5, %xmm6
1653 # GHASH computation for the last <16 byte block
1654 sub %r13, %r11
1655 add $16, %r11
Tadeusz Struk60af5202011-03-13 16:56:17 +08001656
1657 movdqa SHUF_MASK(%rip), %xmm10
1658 PSHUFB_XMM %xmm10, %xmm0
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001659
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001660 # shuffle xmm0 back to output as ciphertext
1661
1662 # Output %r13 bytes
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001663 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001664 cmp $8, %r13
1665 jle _less_than_8_bytes_left_encrypt
1666 mov %rax, (%arg2 , %r11, 1)
1667 add $8, %r11
1668 psrldq $8, %xmm0
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001669 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001670 sub $8, %r13
1671_less_than_8_bytes_left_encrypt:
1672 mov %al, (%arg2, %r11, 1)
1673 add $1, %r11
1674 shr $8, %rax
1675 sub $1, %r13
1676 jne _less_than_8_bytes_left_encrypt
1677_multiple_of_16_bytes_encrypt:
1678 mov arg8, %r12 # %r12 = addLen (number of bytes)
1679 shl $3, %r12
1680 movd %r12d, %xmm15 # len(A) in %xmm15
1681 shl $3, %arg4 # len(C) in bits (*128)
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001682 MOVQ_R64_XMM %arg4, %xmm1
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001683 pslldq $8, %xmm15 # %xmm15 = len(A)||0x0000000000000000
1684 pxor %xmm1, %xmm15 # %xmm15 = len(A)||len(C)
1685 pxor %xmm15, %xmm8
1686 GHASH_MUL %xmm8, %xmm13, %xmm9, %xmm10, %xmm11, %xmm5, %xmm6
1687 # final GHASH computation
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001688 movdqa SHUF_MASK(%rip), %xmm10
1689 PSHUFB_XMM %xmm10, %xmm8 # perform a 16 byte swap
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001690
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001691 mov %arg5, %rax # %rax = *Y0
1692 movdqu (%rax), %xmm0 # %xmm0 = Y0
1693 ENCRYPT_SINGLE_BLOCK %xmm0, %xmm15 # Encrypt(K, Y0)
1694 pxor %xmm8, %xmm0
1695_return_T_encrypt:
1696 mov arg9, %r10 # %r10 = authTag
1697 mov arg10, %r11 # %r11 = auth_tag_len
1698 cmp $16, %r11
1699 je _T_16_encrypt
1700 cmp $12, %r11
1701 je _T_12_encrypt
1702_T_8_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001703 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001704 mov %rax, (%r10)
1705 jmp _return_T_done_encrypt
1706_T_12_encrypt:
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001707 MOVQ_R64_XMM %xmm0, %rax
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001708 mov %rax, (%r10)
1709 psrldq $8, %xmm0
1710 movd %xmm0, %eax
1711 mov %eax, 8(%r10)
1712 jmp _return_T_done_encrypt
1713_T_16_encrypt:
1714 movdqu %xmm0, (%r10)
1715_return_T_done_encrypt:
1716 mov %r14, %rsp
1717 pop %r14
1718 pop %r13
1719 pop %r12
1720 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001721ENDPROC(aesni_gcm_enc)
Tadeusz Struk3c097b82010-12-13 19:51:15 +08001722
Mathias Krause559ad0f2010-11-29 08:35:39 +08001723#endif
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001724
1725
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001726.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001727_key_expansion_128:
1728_key_expansion_256a:
1729 pshufd $0b11111111, %xmm1, %xmm1
1730 shufps $0b00010000, %xmm0, %xmm4
1731 pxor %xmm4, %xmm0
1732 shufps $0b10001100, %xmm0, %xmm4
1733 pxor %xmm4, %xmm0
1734 pxor %xmm1, %xmm0
Mathias Krause0d258ef2010-11-27 16:34:46 +08001735 movaps %xmm0, (TKEYP)
1736 add $0x10, TKEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001737 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001738ENDPROC(_key_expansion_128)
1739ENDPROC(_key_expansion_256a)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001740
Mathias Krause0d258ef2010-11-27 16:34:46 +08001741.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001742_key_expansion_192a:
1743 pshufd $0b01010101, %xmm1, %xmm1
1744 shufps $0b00010000, %xmm0, %xmm4
1745 pxor %xmm4, %xmm0
1746 shufps $0b10001100, %xmm0, %xmm4
1747 pxor %xmm4, %xmm0
1748 pxor %xmm1, %xmm0
1749
1750 movaps %xmm2, %xmm5
1751 movaps %xmm2, %xmm6
1752 pslldq $4, %xmm5
1753 pshufd $0b11111111, %xmm0, %xmm3
1754 pxor %xmm3, %xmm2
1755 pxor %xmm5, %xmm2
1756
1757 movaps %xmm0, %xmm1
1758 shufps $0b01000100, %xmm0, %xmm6
Mathias Krause0d258ef2010-11-27 16:34:46 +08001759 movaps %xmm6, (TKEYP)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001760 shufps $0b01001110, %xmm2, %xmm1
Mathias Krause0d258ef2010-11-27 16:34:46 +08001761 movaps %xmm1, 0x10(TKEYP)
1762 add $0x20, TKEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001763 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001764ENDPROC(_key_expansion_192a)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001765
Mathias Krause0d258ef2010-11-27 16:34:46 +08001766.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001767_key_expansion_192b:
1768 pshufd $0b01010101, %xmm1, %xmm1
1769 shufps $0b00010000, %xmm0, %xmm4
1770 pxor %xmm4, %xmm0
1771 shufps $0b10001100, %xmm0, %xmm4
1772 pxor %xmm4, %xmm0
1773 pxor %xmm1, %xmm0
1774
1775 movaps %xmm2, %xmm5
1776 pslldq $4, %xmm5
1777 pshufd $0b11111111, %xmm0, %xmm3
1778 pxor %xmm3, %xmm2
1779 pxor %xmm5, %xmm2
1780
Mathias Krause0d258ef2010-11-27 16:34:46 +08001781 movaps %xmm0, (TKEYP)
1782 add $0x10, TKEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001783 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001784ENDPROC(_key_expansion_192b)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001785
Mathias Krause0d258ef2010-11-27 16:34:46 +08001786.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001787_key_expansion_256b:
1788 pshufd $0b10101010, %xmm1, %xmm1
1789 shufps $0b00010000, %xmm2, %xmm4
1790 pxor %xmm4, %xmm2
1791 shufps $0b10001100, %xmm2, %xmm4
1792 pxor %xmm4, %xmm2
1793 pxor %xmm1, %xmm2
Mathias Krause0d258ef2010-11-27 16:34:46 +08001794 movaps %xmm2, (TKEYP)
1795 add $0x10, TKEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001796 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001797ENDPROC(_key_expansion_256b)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001798
1799/*
1800 * int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
1801 * unsigned int key_len)
1802 */
1803ENTRY(aesni_set_key)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001804 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08001805#ifndef __x86_64__
1806 pushl KEYP
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001807 movl (FRAME_OFFSET+8)(%esp), KEYP # ctx
1808 movl (FRAME_OFFSET+12)(%esp), UKEYP # in_key
1809 movl (FRAME_OFFSET+16)(%esp), %edx # key_len
Mathias Krause0d258ef2010-11-27 16:34:46 +08001810#endif
1811 movups (UKEYP), %xmm0 # user key (first 16 bytes)
1812 movaps %xmm0, (KEYP)
1813 lea 0x10(KEYP), TKEYP # key addr
1814 movl %edx, 480(KEYP)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001815 pxor %xmm4, %xmm4 # xmm4 is assumed 0 in _key_expansion_x
1816 cmp $24, %dl
1817 jb .Lenc_key128
1818 je .Lenc_key192
Mathias Krause0d258ef2010-11-27 16:34:46 +08001819 movups 0x10(UKEYP), %xmm2 # other user key
1820 movaps %xmm2, (TKEYP)
1821 add $0x10, TKEYP
Huang Yingb369e522009-11-23 19:54:06 +08001822 AESKEYGENASSIST 0x1 %xmm2 %xmm1 # round 1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001823 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001824 AESKEYGENASSIST 0x1 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001825 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001826 AESKEYGENASSIST 0x2 %xmm2 %xmm1 # round 2
Huang Ying54b6a1b2009-01-18 16:28:34 +11001827 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001828 AESKEYGENASSIST 0x2 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001829 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001830 AESKEYGENASSIST 0x4 %xmm2 %xmm1 # round 3
Huang Ying54b6a1b2009-01-18 16:28:34 +11001831 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001832 AESKEYGENASSIST 0x4 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001833 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001834 AESKEYGENASSIST 0x8 %xmm2 %xmm1 # round 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001835 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001836 AESKEYGENASSIST 0x8 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001837 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001838 AESKEYGENASSIST 0x10 %xmm2 %xmm1 # round 5
Huang Ying54b6a1b2009-01-18 16:28:34 +11001839 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001840 AESKEYGENASSIST 0x10 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001841 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001842 AESKEYGENASSIST 0x20 %xmm2 %xmm1 # round 6
Huang Ying54b6a1b2009-01-18 16:28:34 +11001843 call _key_expansion_256a
Huang Yingb369e522009-11-23 19:54:06 +08001844 AESKEYGENASSIST 0x20 %xmm0 %xmm1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001845 call _key_expansion_256b
Huang Yingb369e522009-11-23 19:54:06 +08001846 AESKEYGENASSIST 0x40 %xmm2 %xmm1 # round 7
Huang Ying54b6a1b2009-01-18 16:28:34 +11001847 call _key_expansion_256a
1848 jmp .Ldec_key
1849.Lenc_key192:
Mathias Krause0d258ef2010-11-27 16:34:46 +08001850 movq 0x10(UKEYP), %xmm2 # other user key
Huang Yingb369e522009-11-23 19:54:06 +08001851 AESKEYGENASSIST 0x1 %xmm2 %xmm1 # round 1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001852 call _key_expansion_192a
Huang Yingb369e522009-11-23 19:54:06 +08001853 AESKEYGENASSIST 0x2 %xmm2 %xmm1 # round 2
Huang Ying54b6a1b2009-01-18 16:28:34 +11001854 call _key_expansion_192b
Huang Yingb369e522009-11-23 19:54:06 +08001855 AESKEYGENASSIST 0x4 %xmm2 %xmm1 # round 3
Huang Ying54b6a1b2009-01-18 16:28:34 +11001856 call _key_expansion_192a
Huang Yingb369e522009-11-23 19:54:06 +08001857 AESKEYGENASSIST 0x8 %xmm2 %xmm1 # round 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001858 call _key_expansion_192b
Huang Yingb369e522009-11-23 19:54:06 +08001859 AESKEYGENASSIST 0x10 %xmm2 %xmm1 # round 5
Huang Ying54b6a1b2009-01-18 16:28:34 +11001860 call _key_expansion_192a
Huang Yingb369e522009-11-23 19:54:06 +08001861 AESKEYGENASSIST 0x20 %xmm2 %xmm1 # round 6
Huang Ying54b6a1b2009-01-18 16:28:34 +11001862 call _key_expansion_192b
Huang Yingb369e522009-11-23 19:54:06 +08001863 AESKEYGENASSIST 0x40 %xmm2 %xmm1 # round 7
Huang Ying54b6a1b2009-01-18 16:28:34 +11001864 call _key_expansion_192a
Huang Yingb369e522009-11-23 19:54:06 +08001865 AESKEYGENASSIST 0x80 %xmm2 %xmm1 # round 8
Huang Ying54b6a1b2009-01-18 16:28:34 +11001866 call _key_expansion_192b
1867 jmp .Ldec_key
1868.Lenc_key128:
Huang Yingb369e522009-11-23 19:54:06 +08001869 AESKEYGENASSIST 0x1 %xmm0 %xmm1 # round 1
Huang Ying54b6a1b2009-01-18 16:28:34 +11001870 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001871 AESKEYGENASSIST 0x2 %xmm0 %xmm1 # round 2
Huang Ying54b6a1b2009-01-18 16:28:34 +11001872 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001873 AESKEYGENASSIST 0x4 %xmm0 %xmm1 # round 3
Huang Ying54b6a1b2009-01-18 16:28:34 +11001874 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001875 AESKEYGENASSIST 0x8 %xmm0 %xmm1 # round 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001876 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001877 AESKEYGENASSIST 0x10 %xmm0 %xmm1 # round 5
Huang Ying54b6a1b2009-01-18 16:28:34 +11001878 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001879 AESKEYGENASSIST 0x20 %xmm0 %xmm1 # round 6
Huang Ying54b6a1b2009-01-18 16:28:34 +11001880 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001881 AESKEYGENASSIST 0x40 %xmm0 %xmm1 # round 7
Huang Ying54b6a1b2009-01-18 16:28:34 +11001882 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001883 AESKEYGENASSIST 0x80 %xmm0 %xmm1 # round 8
Huang Ying54b6a1b2009-01-18 16:28:34 +11001884 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001885 AESKEYGENASSIST 0x1b %xmm0 %xmm1 # round 9
Huang Ying54b6a1b2009-01-18 16:28:34 +11001886 call _key_expansion_128
Huang Yingb369e522009-11-23 19:54:06 +08001887 AESKEYGENASSIST 0x36 %xmm0 %xmm1 # round 10
Huang Ying54b6a1b2009-01-18 16:28:34 +11001888 call _key_expansion_128
1889.Ldec_key:
Mathias Krause0d258ef2010-11-27 16:34:46 +08001890 sub $0x10, TKEYP
1891 movaps (KEYP), %xmm0
1892 movaps (TKEYP), %xmm1
1893 movaps %xmm0, 240(TKEYP)
1894 movaps %xmm1, 240(KEYP)
1895 add $0x10, KEYP
1896 lea 240-16(TKEYP), UKEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001897.align 4
1898.Ldec_key_loop:
Mathias Krause0d258ef2010-11-27 16:34:46 +08001899 movaps (KEYP), %xmm0
Huang Yingb369e522009-11-23 19:54:06 +08001900 AESIMC %xmm0 %xmm1
Mathias Krause0d258ef2010-11-27 16:34:46 +08001901 movaps %xmm1, (UKEYP)
1902 add $0x10, KEYP
1903 sub $0x10, UKEYP
1904 cmp TKEYP, KEYP
Huang Ying54b6a1b2009-01-18 16:28:34 +11001905 jb .Ldec_key_loop
Mathias Krause0d258ef2010-11-27 16:34:46 +08001906 xor AREG, AREG
1907#ifndef __x86_64__
1908 popl KEYP
1909#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001910 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11001911 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001912ENDPROC(aesni_set_key)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001913
1914/*
1915 * void aesni_enc(struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src)
1916 */
1917ENTRY(aesni_enc)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001918 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08001919#ifndef __x86_64__
1920 pushl KEYP
1921 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001922 movl (FRAME_OFFSET+12)(%esp), KEYP # ctx
1923 movl (FRAME_OFFSET+16)(%esp), OUTP # dst
1924 movl (FRAME_OFFSET+20)(%esp), INP # src
Mathias Krause0d258ef2010-11-27 16:34:46 +08001925#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11001926 movl 480(KEYP), KLEN # key length
1927 movups (INP), STATE # input
1928 call _aesni_enc1
1929 movups STATE, (OUTP) # output
Mathias Krause0d258ef2010-11-27 16:34:46 +08001930#ifndef __x86_64__
1931 popl KLEN
1932 popl KEYP
1933#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06001934 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11001935 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001936ENDPROC(aesni_enc)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001937
1938/*
1939 * _aesni_enc1: internal ABI
1940 * input:
1941 * KEYP: key struct pointer
1942 * KLEN: round count
1943 * STATE: initial state (input)
1944 * output:
1945 * STATE: finial state (output)
1946 * changed:
1947 * KEY
1948 * TKEYP (T1)
1949 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08001950.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11001951_aesni_enc1:
1952 movaps (KEYP), KEY # key
1953 mov KEYP, TKEYP
1954 pxor KEY, STATE # round 0
1955 add $0x30, TKEYP
1956 cmp $24, KLEN
1957 jb .Lenc128
1958 lea 0x20(TKEYP), TKEYP
1959 je .Lenc192
1960 add $0x20, TKEYP
1961 movaps -0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001962 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001963 movaps -0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001964 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001965.align 4
1966.Lenc192:
1967 movaps -0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001968 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001969 movaps -0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001970 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001971.align 4
1972.Lenc128:
1973 movaps -0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001974 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001975 movaps -0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001976 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001977 movaps (TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001978 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001979 movaps 0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001980 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001981 movaps 0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001982 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001983 movaps 0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001984 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001985 movaps 0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001986 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001987 movaps 0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001988 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001989 movaps 0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001990 AESENC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001991 movaps 0x70(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08001992 AESENCLAST KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11001993 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02001994ENDPROC(_aesni_enc1)
Huang Ying54b6a1b2009-01-18 16:28:34 +11001995
1996/*
1997 * _aesni_enc4: internal ABI
1998 * input:
1999 * KEYP: key struct pointer
2000 * KLEN: round count
2001 * STATE1: initial state (input)
2002 * STATE2
2003 * STATE3
2004 * STATE4
2005 * output:
2006 * STATE1: finial state (output)
2007 * STATE2
2008 * STATE3
2009 * STATE4
2010 * changed:
2011 * KEY
2012 * TKEYP (T1)
2013 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08002014.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002015_aesni_enc4:
2016 movaps (KEYP), KEY # key
2017 mov KEYP, TKEYP
2018 pxor KEY, STATE1 # round 0
2019 pxor KEY, STATE2
2020 pxor KEY, STATE3
2021 pxor KEY, STATE4
2022 add $0x30, TKEYP
2023 cmp $24, KLEN
2024 jb .L4enc128
2025 lea 0x20(TKEYP), TKEYP
2026 je .L4enc192
2027 add $0x20, TKEYP
2028 movaps -0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002029 AESENC KEY STATE1
2030 AESENC KEY STATE2
2031 AESENC KEY STATE3
2032 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002033 movaps -0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002034 AESENC KEY STATE1
2035 AESENC KEY STATE2
2036 AESENC KEY STATE3
2037 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002038#.align 4
2039.L4enc192:
2040 movaps -0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002041 AESENC KEY STATE1
2042 AESENC KEY STATE2
2043 AESENC KEY STATE3
2044 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002045 movaps -0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002046 AESENC KEY STATE1
2047 AESENC KEY STATE2
2048 AESENC KEY STATE3
2049 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002050#.align 4
2051.L4enc128:
2052 movaps -0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002053 AESENC KEY STATE1
2054 AESENC KEY STATE2
2055 AESENC KEY STATE3
2056 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002057 movaps -0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002058 AESENC KEY STATE1
2059 AESENC KEY STATE2
2060 AESENC KEY STATE3
2061 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002062 movaps (TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002063 AESENC KEY STATE1
2064 AESENC KEY STATE2
2065 AESENC KEY STATE3
2066 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002067 movaps 0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002068 AESENC KEY STATE1
2069 AESENC KEY STATE2
2070 AESENC KEY STATE3
2071 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002072 movaps 0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002073 AESENC KEY STATE1
2074 AESENC KEY STATE2
2075 AESENC KEY STATE3
2076 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002077 movaps 0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002078 AESENC KEY STATE1
2079 AESENC KEY STATE2
2080 AESENC KEY STATE3
2081 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002082 movaps 0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002083 AESENC KEY STATE1
2084 AESENC KEY STATE2
2085 AESENC KEY STATE3
2086 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002087 movaps 0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002088 AESENC KEY STATE1
2089 AESENC KEY STATE2
2090 AESENC KEY STATE3
2091 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002092 movaps 0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002093 AESENC KEY STATE1
2094 AESENC KEY STATE2
2095 AESENC KEY STATE3
2096 AESENC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002097 movaps 0x70(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002098 AESENCLAST KEY STATE1 # last round
2099 AESENCLAST KEY STATE2
2100 AESENCLAST KEY STATE3
2101 AESENCLAST KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002102 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002103ENDPROC(_aesni_enc4)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002104
2105/*
2106 * void aesni_dec (struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src)
2107 */
2108ENTRY(aesni_dec)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002109 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08002110#ifndef __x86_64__
2111 pushl KEYP
2112 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002113 movl (FRAME_OFFSET+12)(%esp), KEYP # ctx
2114 movl (FRAME_OFFSET+16)(%esp), OUTP # dst
2115 movl (FRAME_OFFSET+20)(%esp), INP # src
Mathias Krause0d258ef2010-11-27 16:34:46 +08002116#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002117 mov 480(KEYP), KLEN # key length
2118 add $240, KEYP
2119 movups (INP), STATE # input
2120 call _aesni_dec1
2121 movups STATE, (OUTP) #output
Mathias Krause0d258ef2010-11-27 16:34:46 +08002122#ifndef __x86_64__
2123 popl KLEN
2124 popl KEYP
2125#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002126 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11002127 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002128ENDPROC(aesni_dec)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002129
2130/*
2131 * _aesni_dec1: internal ABI
2132 * input:
2133 * KEYP: key struct pointer
2134 * KLEN: key length
2135 * STATE: initial state (input)
2136 * output:
2137 * STATE: finial state (output)
2138 * changed:
2139 * KEY
2140 * TKEYP (T1)
2141 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08002142.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002143_aesni_dec1:
2144 movaps (KEYP), KEY # key
2145 mov KEYP, TKEYP
2146 pxor KEY, STATE # round 0
2147 add $0x30, TKEYP
2148 cmp $24, KLEN
2149 jb .Ldec128
2150 lea 0x20(TKEYP), TKEYP
2151 je .Ldec192
2152 add $0x20, TKEYP
2153 movaps -0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002154 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002155 movaps -0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002156 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002157.align 4
2158.Ldec192:
2159 movaps -0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002160 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002161 movaps -0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002162 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002163.align 4
2164.Ldec128:
2165 movaps -0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002166 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002167 movaps -0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002168 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002169 movaps (TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002170 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002171 movaps 0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002172 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002173 movaps 0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002174 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002175 movaps 0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002176 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002177 movaps 0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002178 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002179 movaps 0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002180 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002181 movaps 0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002182 AESDEC KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002183 movaps 0x70(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002184 AESDECLAST KEY STATE
Huang Ying54b6a1b2009-01-18 16:28:34 +11002185 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002186ENDPROC(_aesni_dec1)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002187
2188/*
2189 * _aesni_dec4: internal ABI
2190 * input:
2191 * KEYP: key struct pointer
2192 * KLEN: key length
2193 * STATE1: initial state (input)
2194 * STATE2
2195 * STATE3
2196 * STATE4
2197 * output:
2198 * STATE1: finial state (output)
2199 * STATE2
2200 * STATE3
2201 * STATE4
2202 * changed:
2203 * KEY
2204 * TKEYP (T1)
2205 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08002206.align 4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002207_aesni_dec4:
2208 movaps (KEYP), KEY # key
2209 mov KEYP, TKEYP
2210 pxor KEY, STATE1 # round 0
2211 pxor KEY, STATE2
2212 pxor KEY, STATE3
2213 pxor KEY, STATE4
2214 add $0x30, TKEYP
2215 cmp $24, KLEN
2216 jb .L4dec128
2217 lea 0x20(TKEYP), TKEYP
2218 je .L4dec192
2219 add $0x20, TKEYP
2220 movaps -0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002221 AESDEC KEY STATE1
2222 AESDEC KEY STATE2
2223 AESDEC KEY STATE3
2224 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002225 movaps -0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002226 AESDEC KEY STATE1
2227 AESDEC KEY STATE2
2228 AESDEC KEY STATE3
2229 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002230.align 4
2231.L4dec192:
2232 movaps -0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002233 AESDEC KEY STATE1
2234 AESDEC KEY STATE2
2235 AESDEC KEY STATE3
2236 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002237 movaps -0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002238 AESDEC KEY STATE1
2239 AESDEC KEY STATE2
2240 AESDEC KEY STATE3
2241 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002242.align 4
2243.L4dec128:
2244 movaps -0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002245 AESDEC KEY STATE1
2246 AESDEC KEY STATE2
2247 AESDEC KEY STATE3
2248 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002249 movaps -0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002250 AESDEC KEY STATE1
2251 AESDEC KEY STATE2
2252 AESDEC KEY STATE3
2253 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002254 movaps (TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002255 AESDEC KEY STATE1
2256 AESDEC KEY STATE2
2257 AESDEC KEY STATE3
2258 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002259 movaps 0x10(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002260 AESDEC KEY STATE1
2261 AESDEC KEY STATE2
2262 AESDEC KEY STATE3
2263 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002264 movaps 0x20(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002265 AESDEC KEY STATE1
2266 AESDEC KEY STATE2
2267 AESDEC KEY STATE3
2268 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002269 movaps 0x30(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002270 AESDEC KEY STATE1
2271 AESDEC KEY STATE2
2272 AESDEC KEY STATE3
2273 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002274 movaps 0x40(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002275 AESDEC KEY STATE1
2276 AESDEC KEY STATE2
2277 AESDEC KEY STATE3
2278 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002279 movaps 0x50(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002280 AESDEC KEY STATE1
2281 AESDEC KEY STATE2
2282 AESDEC KEY STATE3
2283 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002284 movaps 0x60(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002285 AESDEC KEY STATE1
2286 AESDEC KEY STATE2
2287 AESDEC KEY STATE3
2288 AESDEC KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002289 movaps 0x70(TKEYP), KEY
Huang Yingb369e522009-11-23 19:54:06 +08002290 AESDECLAST KEY STATE1 # last round
2291 AESDECLAST KEY STATE2
2292 AESDECLAST KEY STATE3
2293 AESDECLAST KEY STATE4
Huang Ying54b6a1b2009-01-18 16:28:34 +11002294 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002295ENDPROC(_aesni_dec4)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002296
2297/*
2298 * void aesni_ecb_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2299 * size_t len)
2300 */
2301ENTRY(aesni_ecb_enc)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002302 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08002303#ifndef __x86_64__
2304 pushl LEN
2305 pushl KEYP
2306 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002307 movl (FRAME_OFFSET+16)(%esp), KEYP # ctx
2308 movl (FRAME_OFFSET+20)(%esp), OUTP # dst
2309 movl (FRAME_OFFSET+24)(%esp), INP # src
2310 movl (FRAME_OFFSET+28)(%esp), LEN # len
Mathias Krause0d258ef2010-11-27 16:34:46 +08002311#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002312 test LEN, LEN # check length
2313 jz .Lecb_enc_ret
2314 mov 480(KEYP), KLEN
2315 cmp $16, LEN
2316 jb .Lecb_enc_ret
2317 cmp $64, LEN
2318 jb .Lecb_enc_loop1
2319.align 4
2320.Lecb_enc_loop4:
2321 movups (INP), STATE1
2322 movups 0x10(INP), STATE2
2323 movups 0x20(INP), STATE3
2324 movups 0x30(INP), STATE4
2325 call _aesni_enc4
2326 movups STATE1, (OUTP)
2327 movups STATE2, 0x10(OUTP)
2328 movups STATE3, 0x20(OUTP)
2329 movups STATE4, 0x30(OUTP)
2330 sub $64, LEN
2331 add $64, INP
2332 add $64, OUTP
2333 cmp $64, LEN
2334 jge .Lecb_enc_loop4
2335 cmp $16, LEN
2336 jb .Lecb_enc_ret
2337.align 4
2338.Lecb_enc_loop1:
2339 movups (INP), STATE1
2340 call _aesni_enc1
2341 movups STATE1, (OUTP)
2342 sub $16, LEN
2343 add $16, INP
2344 add $16, OUTP
2345 cmp $16, LEN
2346 jge .Lecb_enc_loop1
2347.Lecb_enc_ret:
Mathias Krause0d258ef2010-11-27 16:34:46 +08002348#ifndef __x86_64__
2349 popl KLEN
2350 popl KEYP
2351 popl LEN
2352#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002353 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11002354 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002355ENDPROC(aesni_ecb_enc)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002356
2357/*
2358 * void aesni_ecb_dec(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2359 * size_t len);
2360 */
2361ENTRY(aesni_ecb_dec)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002362 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08002363#ifndef __x86_64__
2364 pushl LEN
2365 pushl KEYP
2366 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002367 movl (FRAME_OFFSET+16)(%esp), KEYP # ctx
2368 movl (FRAME_OFFSET+20)(%esp), OUTP # dst
2369 movl (FRAME_OFFSET+24)(%esp), INP # src
2370 movl (FRAME_OFFSET+28)(%esp), LEN # len
Mathias Krause0d258ef2010-11-27 16:34:46 +08002371#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002372 test LEN, LEN
2373 jz .Lecb_dec_ret
2374 mov 480(KEYP), KLEN
2375 add $240, KEYP
2376 cmp $16, LEN
2377 jb .Lecb_dec_ret
2378 cmp $64, LEN
2379 jb .Lecb_dec_loop1
2380.align 4
2381.Lecb_dec_loop4:
2382 movups (INP), STATE1
2383 movups 0x10(INP), STATE2
2384 movups 0x20(INP), STATE3
2385 movups 0x30(INP), STATE4
2386 call _aesni_dec4
2387 movups STATE1, (OUTP)
2388 movups STATE2, 0x10(OUTP)
2389 movups STATE3, 0x20(OUTP)
2390 movups STATE4, 0x30(OUTP)
2391 sub $64, LEN
2392 add $64, INP
2393 add $64, OUTP
2394 cmp $64, LEN
2395 jge .Lecb_dec_loop4
2396 cmp $16, LEN
2397 jb .Lecb_dec_ret
2398.align 4
2399.Lecb_dec_loop1:
2400 movups (INP), STATE1
2401 call _aesni_dec1
2402 movups STATE1, (OUTP)
2403 sub $16, LEN
2404 add $16, INP
2405 add $16, OUTP
2406 cmp $16, LEN
2407 jge .Lecb_dec_loop1
2408.Lecb_dec_ret:
Mathias Krause0d258ef2010-11-27 16:34:46 +08002409#ifndef __x86_64__
2410 popl KLEN
2411 popl KEYP
2412 popl LEN
2413#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002414 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11002415 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002416ENDPROC(aesni_ecb_dec)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002417
2418/*
2419 * void aesni_cbc_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2420 * size_t len, u8 *iv)
2421 */
2422ENTRY(aesni_cbc_enc)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002423 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08002424#ifndef __x86_64__
2425 pushl IVP
2426 pushl LEN
2427 pushl KEYP
2428 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002429 movl (FRAME_OFFSET+20)(%esp), KEYP # ctx
2430 movl (FRAME_OFFSET+24)(%esp), OUTP # dst
2431 movl (FRAME_OFFSET+28)(%esp), INP # src
2432 movl (FRAME_OFFSET+32)(%esp), LEN # len
2433 movl (FRAME_OFFSET+36)(%esp), IVP # iv
Mathias Krause0d258ef2010-11-27 16:34:46 +08002434#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002435 cmp $16, LEN
2436 jb .Lcbc_enc_ret
2437 mov 480(KEYP), KLEN
2438 movups (IVP), STATE # load iv as initial state
2439.align 4
2440.Lcbc_enc_loop:
2441 movups (INP), IN # load input
2442 pxor IN, STATE
2443 call _aesni_enc1
2444 movups STATE, (OUTP) # store output
2445 sub $16, LEN
2446 add $16, INP
2447 add $16, OUTP
2448 cmp $16, LEN
2449 jge .Lcbc_enc_loop
2450 movups STATE, (IVP)
2451.Lcbc_enc_ret:
Mathias Krause0d258ef2010-11-27 16:34:46 +08002452#ifndef __x86_64__
2453 popl KLEN
2454 popl KEYP
2455 popl LEN
2456 popl IVP
2457#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002458 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11002459 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002460ENDPROC(aesni_cbc_enc)
Huang Ying54b6a1b2009-01-18 16:28:34 +11002461
2462/*
2463 * void aesni_cbc_dec(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2464 * size_t len, u8 *iv)
2465 */
2466ENTRY(aesni_cbc_dec)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002467 FRAME_BEGIN
Mathias Krause0d258ef2010-11-27 16:34:46 +08002468#ifndef __x86_64__
2469 pushl IVP
2470 pushl LEN
2471 pushl KEYP
2472 pushl KLEN
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002473 movl (FRAME_OFFSET+20)(%esp), KEYP # ctx
2474 movl (FRAME_OFFSET+24)(%esp), OUTP # dst
2475 movl (FRAME_OFFSET+28)(%esp), INP # src
2476 movl (FRAME_OFFSET+32)(%esp), LEN # len
2477 movl (FRAME_OFFSET+36)(%esp), IVP # iv
Mathias Krause0d258ef2010-11-27 16:34:46 +08002478#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002479 cmp $16, LEN
Huang Yinge6efaa02009-06-18 19:33:57 +08002480 jb .Lcbc_dec_just_ret
Huang Ying54b6a1b2009-01-18 16:28:34 +11002481 mov 480(KEYP), KLEN
2482 add $240, KEYP
2483 movups (IVP), IV
2484 cmp $64, LEN
2485 jb .Lcbc_dec_loop1
2486.align 4
2487.Lcbc_dec_loop4:
2488 movups (INP), IN1
2489 movaps IN1, STATE1
2490 movups 0x10(INP), IN2
2491 movaps IN2, STATE2
Mathias Krause0d258ef2010-11-27 16:34:46 +08002492#ifdef __x86_64__
Huang Ying54b6a1b2009-01-18 16:28:34 +11002493 movups 0x20(INP), IN3
2494 movaps IN3, STATE3
2495 movups 0x30(INP), IN4
2496 movaps IN4, STATE4
Mathias Krause0d258ef2010-11-27 16:34:46 +08002497#else
2498 movups 0x20(INP), IN1
2499 movaps IN1, STATE3
2500 movups 0x30(INP), IN2
2501 movaps IN2, STATE4
2502#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002503 call _aesni_dec4
2504 pxor IV, STATE1
Mathias Krause0d258ef2010-11-27 16:34:46 +08002505#ifdef __x86_64__
Huang Ying54b6a1b2009-01-18 16:28:34 +11002506 pxor IN1, STATE2
2507 pxor IN2, STATE3
2508 pxor IN3, STATE4
2509 movaps IN4, IV
Mathias Krause0d258ef2010-11-27 16:34:46 +08002510#else
Mathias Krause0d258ef2010-11-27 16:34:46 +08002511 pxor IN1, STATE4
2512 movaps IN2, IV
Mathias Krause7c8d5182012-05-30 01:43:08 +02002513 movups (INP), IN1
2514 pxor IN1, STATE2
2515 movups 0x10(INP), IN2
2516 pxor IN2, STATE3
Mathias Krause0d258ef2010-11-27 16:34:46 +08002517#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +11002518 movups STATE1, (OUTP)
2519 movups STATE2, 0x10(OUTP)
2520 movups STATE3, 0x20(OUTP)
2521 movups STATE4, 0x30(OUTP)
2522 sub $64, LEN
2523 add $64, INP
2524 add $64, OUTP
2525 cmp $64, LEN
2526 jge .Lcbc_dec_loop4
2527 cmp $16, LEN
2528 jb .Lcbc_dec_ret
2529.align 4
2530.Lcbc_dec_loop1:
2531 movups (INP), IN
2532 movaps IN, STATE
2533 call _aesni_dec1
2534 pxor IV, STATE
2535 movups STATE, (OUTP)
2536 movaps IN, IV
2537 sub $16, LEN
2538 add $16, INP
2539 add $16, OUTP
2540 cmp $16, LEN
2541 jge .Lcbc_dec_loop1
Huang Ying54b6a1b2009-01-18 16:28:34 +11002542.Lcbc_dec_ret:
Huang Yinge6efaa02009-06-18 19:33:57 +08002543 movups IV, (IVP)
2544.Lcbc_dec_just_ret:
Mathias Krause0d258ef2010-11-27 16:34:46 +08002545#ifndef __x86_64__
2546 popl KLEN
2547 popl KEYP
2548 popl LEN
2549 popl IVP
2550#endif
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002551 FRAME_END
Huang Ying54b6a1b2009-01-18 16:28:34 +11002552 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002553ENDPROC(aesni_cbc_dec)
Huang Ying12387a42010-03-10 18:28:55 +08002554
Mathias Krause0d258ef2010-11-27 16:34:46 +08002555#ifdef __x86_64__
Josh Poimboeuf1253cab2016-01-21 16:49:15 -06002556.pushsection .rodata
Huang Ying12387a42010-03-10 18:28:55 +08002557.align 16
2558.Lbswap_mask:
2559 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
Josh Poimboeuf1253cab2016-01-21 16:49:15 -06002560.popsection
Huang Ying12387a42010-03-10 18:28:55 +08002561
2562/*
2563 * _aesni_inc_init: internal ABI
2564 * setup registers used by _aesni_inc
2565 * input:
2566 * IV
2567 * output:
2568 * CTR: == IV, in little endian
2569 * TCTR_LOW: == lower qword of CTR
2570 * INC: == 1, in little endian
2571 * BSWAP_MASK == endian swapping mask
2572 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08002573.align 4
Huang Ying12387a42010-03-10 18:28:55 +08002574_aesni_inc_init:
2575 movaps .Lbswap_mask, BSWAP_MASK
2576 movaps IV, CTR
2577 PSHUFB_XMM BSWAP_MASK CTR
2578 mov $1, TCTR_LOW
Huang Ying32cbd7d2010-03-13 16:28:42 +08002579 MOVQ_R64_XMM TCTR_LOW INC
2580 MOVQ_R64_XMM CTR TCTR_LOW
Huang Ying12387a42010-03-10 18:28:55 +08002581 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002582ENDPROC(_aesni_inc_init)
Huang Ying12387a42010-03-10 18:28:55 +08002583
2584/*
2585 * _aesni_inc: internal ABI
2586 * Increase IV by 1, IV is in big endian
2587 * input:
2588 * IV
2589 * CTR: == IV, in little endian
2590 * TCTR_LOW: == lower qword of CTR
2591 * INC: == 1, in little endian
2592 * BSWAP_MASK == endian swapping mask
2593 * output:
2594 * IV: Increase by 1
2595 * changed:
2596 * CTR: == output IV, in little endian
2597 * TCTR_LOW: == lower qword of CTR
2598 */
Mathias Krause0d258ef2010-11-27 16:34:46 +08002599.align 4
Huang Ying12387a42010-03-10 18:28:55 +08002600_aesni_inc:
2601 paddq INC, CTR
2602 add $1, TCTR_LOW
2603 jnc .Linc_low
2604 pslldq $8, INC
2605 paddq INC, CTR
2606 psrldq $8, INC
2607.Linc_low:
2608 movaps CTR, IV
2609 PSHUFB_XMM BSWAP_MASK IV
2610 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002611ENDPROC(_aesni_inc)
Huang Ying12387a42010-03-10 18:28:55 +08002612
2613/*
2614 * void aesni_ctr_enc(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2615 * size_t len, u8 *iv)
2616 */
2617ENTRY(aesni_ctr_enc)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002618 FRAME_BEGIN
Huang Ying12387a42010-03-10 18:28:55 +08002619 cmp $16, LEN
2620 jb .Lctr_enc_just_ret
2621 mov 480(KEYP), KLEN
2622 movups (IVP), IV
2623 call _aesni_inc_init
2624 cmp $64, LEN
2625 jb .Lctr_enc_loop1
2626.align 4
2627.Lctr_enc_loop4:
2628 movaps IV, STATE1
2629 call _aesni_inc
2630 movups (INP), IN1
2631 movaps IV, STATE2
2632 call _aesni_inc
2633 movups 0x10(INP), IN2
2634 movaps IV, STATE3
2635 call _aesni_inc
2636 movups 0x20(INP), IN3
2637 movaps IV, STATE4
2638 call _aesni_inc
2639 movups 0x30(INP), IN4
2640 call _aesni_enc4
2641 pxor IN1, STATE1
2642 movups STATE1, (OUTP)
2643 pxor IN2, STATE2
2644 movups STATE2, 0x10(OUTP)
2645 pxor IN3, STATE3
2646 movups STATE3, 0x20(OUTP)
2647 pxor IN4, STATE4
2648 movups STATE4, 0x30(OUTP)
2649 sub $64, LEN
2650 add $64, INP
2651 add $64, OUTP
2652 cmp $64, LEN
2653 jge .Lctr_enc_loop4
2654 cmp $16, LEN
2655 jb .Lctr_enc_ret
2656.align 4
2657.Lctr_enc_loop1:
2658 movaps IV, STATE
2659 call _aesni_inc
2660 movups (INP), IN
2661 call _aesni_enc1
2662 pxor IN, STATE
2663 movups STATE, (OUTP)
2664 sub $16, LEN
2665 add $16, INP
2666 add $16, OUTP
2667 cmp $16, LEN
2668 jge .Lctr_enc_loop1
2669.Lctr_enc_ret:
2670 movups IV, (IVP)
2671.Lctr_enc_just_ret:
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002672 FRAME_END
Huang Ying12387a42010-03-10 18:28:55 +08002673 ret
Jussi Kivilinna8309b742013-01-19 13:38:55 +02002674ENDPROC(aesni_ctr_enc)
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002675
2676/*
2677 * _aesni_gf128mul_x_ble: internal ABI
2678 * Multiply in GF(2^128) for XTS IVs
2679 * input:
2680 * IV: current IV
2681 * GF128MUL_MASK == mask with 0x87 and 0x01
2682 * output:
2683 * IV: next IV
2684 * changed:
2685 * CTR: == temporary value
2686 */
2687#define _aesni_gf128mul_x_ble() \
2688 pshufd $0x13, IV, CTR; \
2689 paddq IV, IV; \
2690 psrad $31, CTR; \
2691 pand GF128MUL_MASK, CTR; \
2692 pxor CTR, IV;
2693
2694/*
2695 * void aesni_xts_crypt8(struct crypto_aes_ctx *ctx, const u8 *dst, u8 *src,
2696 * bool enc, u8 *iv)
2697 */
2698ENTRY(aesni_xts_crypt8)
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002699 FRAME_BEGIN
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002700 cmpb $0, %cl
2701 movl $0, %ecx
2702 movl $240, %r10d
2703 leaq _aesni_enc4, %r11
2704 leaq _aesni_dec4, %rax
2705 cmovel %r10d, %ecx
2706 cmoveq %rax, %r11
2707
2708 movdqa .Lgf128mul_x_ble_mask, GF128MUL_MASK
2709 movups (IVP), IV
2710
2711 mov 480(KEYP), KLEN
2712 addq %rcx, KEYP
2713
2714 movdqa IV, STATE1
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002715 movdqu 0x00(INP), INC
2716 pxor INC, STATE1
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002717 movdqu IV, 0x00(OUTP)
2718
2719 _aesni_gf128mul_x_ble()
2720 movdqa IV, STATE2
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002721 movdqu 0x10(INP), INC
2722 pxor INC, STATE2
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002723 movdqu IV, 0x10(OUTP)
2724
2725 _aesni_gf128mul_x_ble()
2726 movdqa IV, STATE3
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002727 movdqu 0x20(INP), INC
2728 pxor INC, STATE3
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002729 movdqu IV, 0x20(OUTP)
2730
2731 _aesni_gf128mul_x_ble()
2732 movdqa IV, STATE4
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002733 movdqu 0x30(INP), INC
2734 pxor INC, STATE4
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002735 movdqu IV, 0x30(OUTP)
2736
2737 call *%r11
2738
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002739 movdqu 0x00(OUTP), INC
2740 pxor INC, STATE1
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002741 movdqu STATE1, 0x00(OUTP)
2742
2743 _aesni_gf128mul_x_ble()
2744 movdqa IV, STATE1
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002745 movdqu 0x40(INP), INC
2746 pxor INC, STATE1
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002747 movdqu IV, 0x40(OUTP)
2748
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002749 movdqu 0x10(OUTP), INC
2750 pxor INC, STATE2
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002751 movdqu STATE2, 0x10(OUTP)
2752
2753 _aesni_gf128mul_x_ble()
2754 movdqa IV, STATE2
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002755 movdqu 0x50(INP), INC
2756 pxor INC, STATE2
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002757 movdqu IV, 0x50(OUTP)
2758
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002759 movdqu 0x20(OUTP), INC
2760 pxor INC, STATE3
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002761 movdqu STATE3, 0x20(OUTP)
2762
2763 _aesni_gf128mul_x_ble()
2764 movdqa IV, STATE3
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002765 movdqu 0x60(INP), INC
2766 pxor INC, STATE3
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002767 movdqu IV, 0x60(OUTP)
2768
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002769 movdqu 0x30(OUTP), INC
2770 pxor INC, STATE4
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002771 movdqu STATE4, 0x30(OUTP)
2772
2773 _aesni_gf128mul_x_ble()
2774 movdqa IV, STATE4
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002775 movdqu 0x70(INP), INC
2776 pxor INC, STATE4
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002777 movdqu IV, 0x70(OUTP)
2778
2779 _aesni_gf128mul_x_ble()
2780 movups IV, (IVP)
2781
2782 call *%r11
2783
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002784 movdqu 0x40(OUTP), INC
2785 pxor INC, STATE1
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002786 movdqu STATE1, 0x40(OUTP)
2787
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002788 movdqu 0x50(OUTP), INC
2789 pxor INC, STATE2
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002790 movdqu STATE2, 0x50(OUTP)
2791
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002792 movdqu 0x60(OUTP), INC
2793 pxor INC, STATE3
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002794 movdqu STATE3, 0x60(OUTP)
2795
Jussi Kivilinnafe6510b2013-06-11 22:25:22 +03002796 movdqu 0x70(OUTP), INC
2797 pxor INC, STATE4
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002798 movdqu STATE4, 0x70(OUTP)
2799
Josh Poimboeuf8691ccd2016-01-21 16:49:19 -06002800 FRAME_END
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +03002801 ret
2802ENDPROC(aesni_xts_crypt8)
2803
Mathias Krause0d258ef2010-11-27 16:34:46 +08002804#endif