1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 ; Redistribution and use in source and binary forms, with or without
5 ; modification, are permitted provided that the following conditions
7 ; * Redistributions of source code must retain the above copyright
8 ; notice, this list of conditions and the following disclaimer.
9 ; * Redistributions in binary form must reproduce the above copyright
10 ; notice, this list of conditions and the following disclaimer in
11 ; the documentation and/or other materials provided with the
13 ; * Neither the name of Intel Corporation nor the names of its
14 ; contributors may be used to endorse or promote products derived
15 ; from this software without specific prior written permission.
17 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 ; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ; XTS decrypt function with 256-bit AES
30 ; input keys are not aligned
31 ; keys are expanded in parallel with the tweak encryption
32 ; plaintext and ciphertext are not aligned
33 ; second key is stored in the stack as aligned to 16 Bytes
34 ; first key is required only once, no need for storage of this key
36 %include "reg_sizes.asm"
39 %define TW rsp ; store 8 tweak values
40 %define keys rsp + 16*8 ; store 15 expanded keys
42 %ifidn __OUTPUT_FORMAT__, win64
43 %define _xmm rsp + 16*23 ; store xmm6:xmm15
46 %ifidn __OUTPUT_FORMAT__, elf64
47 %define _gpr rsp + 16*23 ; store rbx
48 %define VARIABLE_OFFSET 16*8 + 16*15 + 8*1 ; VARIABLE_OFFSET has to be an odd multiple of 8
50 %define _gpr rsp + 16*33 ; store rdi, rsi, rbx
51 %define VARIABLE_OFFSET 16*8 + 16*15 + 16*10 + 8*3 ; VARIABLE_OFFSET has to be an odd multiple of 8
54 %define GHASH_POLY 0x87
56 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 ;void XTS_AES_256_dec_sse(
58 ; UINT8 *k2, // key used for tweaking, 16*2 bytes
59 ; UINT8 *k1, // key used for "ECB" encryption, 16*2 bytes
60 ; UINT8 *TW_initial, // initial tweak value, 16 bytes
61 ; UINT64 N, // sector size, in bytes
62 ; const UINT8 *ct, // ciphertext sector input data
63 ; UINT8 *pt); // plaintext sector output data
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 ; arguments for input parameters
67 %ifidn __OUTPUT_FORMAT__, elf64
72 %xdefine ptr_plaintext r8
73 %xdefine ptr_ciphertext r9
79 %xdefine ptr_plaintext r10; [rsp + VARIABLE_OFFSET + 8*5]
80 %xdefine ptr_ciphertext r11; [rsp + VARIABLE_OFFSET + 8*6]
83 ; arguments for temp parameters
84 %ifidn __OUTPUT_FORMAT__, elf64
86 %define target_ptr_val rsi
87 %define ghash_poly_8b r10
88 %define ghash_poly_8b_temp r11
91 %define target_ptr_val rdx
92 %define ghash_poly_8b rdi
93 %define ghash_poly_8b_temp rsi
96 %define twtempl rax ; global temp registers used for tweak computation
100 ; produce the key for the next round
101 ; raw_key is the output of aeskeygenassist instruction
102 ; round_key value before this key_expansion_128 macro is current round key
103 ; round_key value after this key_expansion_128 macro is next round key
104 ; 2 macros will be used for key generation in a flip-flopped fashion
105 %macro key_expansion_256_flip 3
106 %define %%xraw_key %1
108 %define %%xround_key %3
109 pshufd %%xraw_key, %%xraw_key, 11111111b
110 shufps %%xtmp, %%xround_key, 00010000b
111 pxor %%xround_key, %%xtmp
112 shufps %%xtmp, %%xround_key, 10001100b
113 pxor %%xround_key, %%xtmp
114 pxor %%xround_key, %%xraw_key
117 %macro key_expansion_256_flop 3
118 %define %%xraw_key %1
120 %define %%xround_key %3
121 pshufd %%xraw_key, %%xraw_key, 10101010b
122 shufps %%xtmp, %%xround_key, 00010000b
123 pxor %%xround_key, %%xtmp
124 shufps %%xtmp, %%xround_key, 10001100b
125 pxor %%xround_key, %%xtmp
126 pxor %%xround_key, %%xraw_key
129 ; macro to encrypt the tweak value in parallel with key generation of both keys
134 %define %%xstate_tweak %3
137 %define %%xraw_key %6
140 %define %%ptr_key2 %9
141 %define %%ptr_key1 %10
142 %define %%ptr_expanded_keys %11
145 movdqu %%xkey2, [%%ptr_key2]
146 pxor %%xstate_tweak, %%xkey2 ; ARK for tweak encryption
148 movdqu %%xkey1, [%%ptr_key1]
149 movdqa [%%ptr_expanded_keys+16*14], %%xkey1
151 movdqu %%xkey2_2, [%%ptr_key2 + 16*1]
152 aesenc %%xstate_tweak, %%xkey2_2 ; round 1 for tweak encryption
154 movdqu %%xkey1_2, [%%ptr_key1 + 16*1]
155 aesimc %%xtmp2, %%xkey1_2
156 movdqa [%%ptr_expanded_keys+16*13], %%xtmp2
161 aeskeygenassist %%xraw_key, %%xkey2_2, 0x1 ; Generating round key 2 for key2
162 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
163 aeskeygenassist %%xraw_key, %%xkey1_2, 0x1 ; Generating round key 2 for key1
164 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
165 aesenc %%xstate_tweak, %%xkey2 ; round 2 for tweak encryption
166 aesimc %%xtmp2, %%xkey1
167 movdqa [%%ptr_expanded_keys+16*12], %%xtmp2
169 aeskeygenassist %%xraw_key, %%xkey2, 0x1 ; Generating round key 3 for key2
170 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
171 aeskeygenassist %%xraw_key, %%xkey1, 0x1 ; Generating round key 3 for key1
172 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
173 aesenc %%xstate_tweak, %%xkey2_2 ; round 3 for tweak encryption
174 aesimc %%xtmp2, %%xkey1_2
175 movdqa [%%ptr_expanded_keys+16*11], %%xtmp2
179 aeskeygenassist %%xraw_key, %%xkey2_2, 0x2 ; Generating round key 4 for key2
180 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
181 aeskeygenassist %%xraw_key, %%xkey1_2, 0x2 ; Generating round key 4 for key1
182 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
183 aesenc %%xstate_tweak, %%xkey2 ; round 4 for tweak encryption
184 aesimc %%xtmp2, %%xkey1
185 movdqa [%%ptr_expanded_keys+16*10], %%xtmp2
187 aeskeygenassist %%xraw_key, %%xkey2, 0x2 ; Generating round key 5 for key2
188 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
189 aeskeygenassist %%xraw_key, %%xkey1, 0x2 ; Generating round key 5 for key1
190 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
191 aesenc %%xstate_tweak, %%xkey2_2 ; round 5 for tweak encryption
192 aesimc %%xtmp2, %%xkey1_2
193 movdqa [%%ptr_expanded_keys+16*9], %%xtmp2
197 aeskeygenassist %%xraw_key, %%xkey2_2, 0x4 ; Generating round key 6 for key2
198 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
199 aeskeygenassist %%xraw_key, %%xkey1_2, 0x4 ; Generating round key 6 for key1
200 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
201 aesenc %%xstate_tweak, %%xkey2 ; round 6 for tweak encryption
202 aesimc %%xtmp2, %%xkey1
203 movdqa [%%ptr_expanded_keys+16*8], %%xtmp2
205 aeskeygenassist %%xraw_key, %%xkey2, 0x4 ; Generating round key 7 for key2
206 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
207 aeskeygenassist %%xraw_key, %%xkey1, 0x4 ; Generating round key 7 for key1
208 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
209 aesenc %%xstate_tweak, %%xkey2_2 ; round 7 for tweak encryption
210 aesimc %%xtmp2, %%xkey1_2
211 movdqa [%%ptr_expanded_keys+16*7], %%xtmp2
214 aeskeygenassist %%xraw_key, %%xkey2_2, 0x8 ; Generating round key 8 for key2
215 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
216 aeskeygenassist %%xraw_key, %%xkey1_2, 0x8 ; Generating round key 8 for key1
217 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
218 aesenc %%xstate_tweak, %%xkey2 ; round 8 for tweak encryption
219 aesimc %%xtmp2, %%xkey1
220 movdqa [%%ptr_expanded_keys+16*6], %%xtmp2
222 aeskeygenassist %%xraw_key, %%xkey2, 0x8 ; Generating round key 9 for key2
223 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
224 aeskeygenassist %%xraw_key, %%xkey1, 0x8 ; Generating round key 9 for key1
225 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
226 aesenc %%xstate_tweak, %%xkey2_2 ; round 9 for tweak encryption
227 aesimc %%xtmp2, %%xkey1_2
228 movdqa [%%ptr_expanded_keys+16*5], %%xtmp2
231 aeskeygenassist %%xraw_key, %%xkey2_2, 0x10 ; Generating round key 10 for key2
232 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
233 aeskeygenassist %%xraw_key, %%xkey1_2, 0x10 ; Generating round key 10 for key1
234 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
235 aesenc %%xstate_tweak, %%xkey2 ; round 10 for tweak encryption
236 aesimc %%xtmp2, %%xkey1
237 movdqa [%%ptr_expanded_keys+16*4], %%xtmp2
239 aeskeygenassist %%xraw_key, %%xkey2, 0x10 ; Generating round key 11 for key2
240 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
241 aeskeygenassist %%xraw_key, %%xkey1, 0x10 ; Generating round key 11 for key1
242 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
243 aesenc %%xstate_tweak, %%xkey2_2 ; round 11 for tweak encryption
244 aesimc %%xtmp2, %%xkey1_2
245 movdqa [%%ptr_expanded_keys+16*3], %%xtmp2
248 aeskeygenassist %%xraw_key, %%xkey2_2, 0x20 ; Generating round key 12 for key2
249 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
250 aeskeygenassist %%xraw_key, %%xkey1_2, 0x20 ; Generating round key 12 for key1
251 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
252 aesenc %%xstate_tweak, %%xkey2 ; round 12 for tweak encryption
253 aesimc %%xtmp2, %%xkey1
254 movdqa [%%ptr_expanded_keys+16*2], %%xtmp2
256 aeskeygenassist %%xraw_key, %%xkey2, 0x20 ; Generating round key 13 for key2
257 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey2_2
258 aeskeygenassist %%xraw_key, %%xkey1, 0x20 ; Generating round key 13 for key1
259 key_expansion_256_flop %%xraw_key, %%xtmp, %%xkey1_2
260 aesenc %%xstate_tweak, %%xkey2_2 ; round 13 for tweak encryption
261 aesimc %%xtmp2, %%xkey1_2
262 movdqa [%%ptr_expanded_keys+16*1], %%xtmp2
265 aeskeygenassist %%xraw_key, %%xkey2_2, 0x40 ; Generating round key 14 for key2
266 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey2
267 aeskeygenassist %%xraw_key, %%xkey1_2, 0x40 ; Generating round key 14 for key1
268 key_expansion_256_flip %%xraw_key, %%xtmp, %%xkey1
269 aesenclast %%xstate_tweak, %%xkey2 ; round 14 for tweak encryption
270 movdqa [%%ptr_expanded_keys+16*0], %%xkey1
272 movdqa [TW], %%xstate_tweak ; Store the encrypted Tweak value
276 ; generate initial tweak values
277 ; load initial plaintext values
280 %define %%ST1 %1 ; state 1
281 %define %%ST2 %2 ; state 2
282 %define %%ST3 %3 ; state 3
283 %define %%ST4 %4 ; state 4
284 %define %%ST5 %5 ; state 5
285 %define %%ST6 %6 ; state 6
286 %define %%ST7 %7 ; state 7
287 %define %%ST8 %8 ; state 8
289 %define %%TW1 %9 ; tweak 1
290 %define %%TW2 %10 ; tweak 2
291 %define %%TW3 %11 ; tweak 3
292 %define %%TW4 %12 ; tweak 4
293 %define %%TW5 %13 ; tweak 5
294 %define %%TW6 %14 ; tweak 6
295 %define %%TW7 %15 ; tweak 7
297 %define %%num_initial_blocks %16
300 ; generate next Tweak values
301 movdqa %%TW1, [TW+16*0]
302 mov twtempl, [TW+8*0]
303 mov twtemph, [TW+8*1]
304 movdqu %%ST1, [ptr_plaintext+16*0]
305 %if (%%num_initial_blocks>=2)
306 xor ghash_poly_8b_temp, ghash_poly_8b_temp
309 cmovc ghash_poly_8b_temp, ghash_poly_8b
310 xor twtempl, ghash_poly_8b_temp
311 mov [TW+8*2], twtempl
312 mov [TW+8*3], twtemph;
313 movdqa %%TW2, [TW+16*1]
314 movdqu %%ST2, [ptr_plaintext+16*1]
316 %if (%%num_initial_blocks>=3)
317 xor ghash_poly_8b_temp, ghash_poly_8b_temp
320 cmovc ghash_poly_8b_temp, ghash_poly_8b
321 xor twtempl, ghash_poly_8b_temp
322 mov [TW+8*4], twtempl
323 mov [TW+8*5], twtemph;
324 movdqa %%TW3, [TW+16*2]
325 movdqu %%ST3, [ptr_plaintext+16*2]
327 %if (%%num_initial_blocks>=4)
328 xor ghash_poly_8b_temp, ghash_poly_8b_temp
331 cmovc ghash_poly_8b_temp, ghash_poly_8b
332 xor twtempl, ghash_poly_8b_temp
333 mov [TW+8*6], twtempl
334 mov [TW+8*7], twtemph;
335 movdqa %%TW4, [TW+16*3]
336 movdqu %%ST4, [ptr_plaintext+16*3]
338 %if (%%num_initial_blocks>=5)
339 xor ghash_poly_8b_temp, ghash_poly_8b_temp
342 cmovc ghash_poly_8b_temp, ghash_poly_8b
343 xor twtempl, ghash_poly_8b_temp
344 mov [TW+8*8], twtempl
345 mov [TW+8*9], twtemph;
346 movdqa %%TW5, [TW+16*4]
347 movdqu %%ST5, [ptr_plaintext+16*4]
349 %if (%%num_initial_blocks>=6)
350 xor ghash_poly_8b_temp, ghash_poly_8b_temp
353 cmovc ghash_poly_8b_temp, ghash_poly_8b
354 xor twtempl, ghash_poly_8b_temp
355 mov [TW+8*10], twtempl
356 mov [TW+8*11], twtemph;
357 movdqa %%TW6, [TW+16*5]
358 movdqu %%ST6, [ptr_plaintext+16*5]
360 %if (%%num_initial_blocks>=7)
361 xor ghash_poly_8b_temp, ghash_poly_8b_temp
364 cmovc ghash_poly_8b_temp, ghash_poly_8b
365 xor twtempl, ghash_poly_8b_temp
366 mov [TW+8*12], twtempl
367 mov [TW+8*13], twtemph;
368 movdqa %%TW7, [TW+16*6]
369 movdqu %%ST7, [ptr_plaintext+16*6]
377 ; encrypt initial blocks of AES
378 ; 1, 2, 3, 4, 5, 6 or 7 blocks are encrypted
379 ; next 8 Tweak values are generated
380 %macro encrypt_initial 18
381 %define %%ST1 %1 ; state 1
382 %define %%ST2 %2 ; state 2
383 %define %%ST3 %3 ; state 3
384 %define %%ST4 %4 ; state 4
385 %define %%ST5 %5 ; state 5
386 %define %%ST6 %6 ; state 6
387 %define %%ST7 %7 ; state 7
388 %define %%ST8 %8 ; state 8
390 %define %%TW1 %9 ; tweak 1
391 %define %%TW2 %10 ; tweak 2
392 %define %%TW3 %11 ; tweak 3
393 %define %%TW4 %12 ; tweak 4
394 %define %%TW5 %13 ; tweak 5
395 %define %%TW6 %14 ; tweak 6
396 %define %%TW7 %15 ; tweak 7
397 %define %%T0 %16 ; Temp register
398 %define %%num_blocks %17
399 ; %%num_blocks blocks encrypted
400 ; %%num_blocks can be 1, 2, 3, 4, 5, 6, 7
402 %define %%lt128 %18 ; less than 128 bytes
406 %if (%%num_blocks>=2)
409 %if (%%num_blocks>=3)
412 %if (%%num_blocks>=4)
415 %if (%%num_blocks>=5)
418 %if (%%num_blocks>=6)
421 %if (%%num_blocks>=7)
429 %if (%%num_blocks>=2)
432 %if (%%num_blocks>=3)
435 %if (%%num_blocks>=4)
438 %if (%%num_blocks>=5)
441 %if (%%num_blocks>=6)
444 %if (%%num_blocks>=7)
450 xor ghash_poly_8b_temp, ghash_poly_8b_temp
456 movdqa %%T0, [keys + 16*1]
458 %if (%%num_blocks>=2)
461 %if (%%num_blocks>=3)
464 %if (%%num_blocks>=4)
467 %if (%%num_blocks>=5)
470 %if (%%num_blocks>=6)
473 %if (%%num_blocks>=7)
477 cmovc ghash_poly_8b_temp, ghash_poly_8b
478 xor twtempl, ghash_poly_8b_temp
479 mov [TW + 8*0], twtempl ; next Tweak1 generated
480 mov [TW + 8*1], twtemph
481 xor ghash_poly_8b_temp, ghash_poly_8b_temp
485 movdqa %%T0, [keys + 16*2]
487 %if (%%num_blocks>=2)
490 %if (%%num_blocks>=3)
493 %if (%%num_blocks>=4)
496 %if (%%num_blocks>=5)
499 %if (%%num_blocks>=6)
502 %if (%%num_blocks>=7)
509 cmovc ghash_poly_8b_temp, ghash_poly_8b
510 xor twtempl, ghash_poly_8b_temp
511 mov [TW + 8*2], twtempl ; next Tweak2 generated
515 movdqa %%T0, [keys + 16*3]
517 %if (%%num_blocks>=2)
520 %if (%%num_blocks>=3)
523 %if (%%num_blocks>=4)
526 %if (%%num_blocks>=5)
529 %if (%%num_blocks>=6)
532 %if (%%num_blocks>=7)
536 mov [TW + 8*3], twtemph
537 xor ghash_poly_8b_temp, ghash_poly_8b_temp
540 cmovc ghash_poly_8b_temp, ghash_poly_8b
544 movdqa %%T0, [keys + 16*4]
546 %if (%%num_blocks>=2)
549 %if (%%num_blocks>=3)
552 %if (%%num_blocks>=4)
555 %if (%%num_blocks>=5)
558 %if (%%num_blocks>=6)
561 %if (%%num_blocks>=7)
566 xor twtempl, ghash_poly_8b_temp
567 mov [TW + 8*4], twtempl ; next Tweak3 generated
568 mov [TW + 8*5], twtemph
569 xor ghash_poly_8b_temp, ghash_poly_8b_temp
574 movdqa %%T0, [keys + 16*5]
576 %if (%%num_blocks>=2)
579 %if (%%num_blocks>=3)
582 %if (%%num_blocks>=4)
585 %if (%%num_blocks>=5)
588 %if (%%num_blocks>=6)
591 %if (%%num_blocks>=7)
597 cmovc ghash_poly_8b_temp, ghash_poly_8b
598 xor twtempl, ghash_poly_8b_temp
599 mov [TW + 8*6], twtempl ; next Tweak4 generated
600 mov [TW + 8*7], twtemph
604 movdqa %%T0, [keys + 16*6]
606 %if (%%num_blocks>=2)
609 %if (%%num_blocks>=3)
612 %if (%%num_blocks>=4)
615 %if (%%num_blocks>=5)
618 %if (%%num_blocks>=6)
621 %if (%%num_blocks>=7)
626 xor ghash_poly_8b_temp, ghash_poly_8b_temp
629 cmovc ghash_poly_8b_temp, ghash_poly_8b
630 xor twtempl, ghash_poly_8b_temp
631 mov [TW + 8*8], twtempl ; next Tweak5 generated
632 mov [TW + 8*9], twtemph
636 movdqa %%T0, [keys + 16*7]
638 %if (%%num_blocks>=2)
641 %if (%%num_blocks>=3)
644 %if (%%num_blocks>=4)
647 %if (%%num_blocks>=5)
650 %if (%%num_blocks>=6)
653 %if (%%num_blocks>=7)
658 xor ghash_poly_8b_temp, ghash_poly_8b_temp
661 cmovc ghash_poly_8b_temp, ghash_poly_8b
662 xor twtempl, ghash_poly_8b_temp
663 mov [TW + 8*10], twtempl ; next Tweak6 generated
664 mov [TW + 8*11], twtemph
667 movdqa %%T0, [keys + 16*8]
669 %if (%%num_blocks>=2)
672 %if (%%num_blocks>=3)
675 %if (%%num_blocks>=4)
678 %if (%%num_blocks>=5)
681 %if (%%num_blocks>=6)
684 %if (%%num_blocks>=7)
689 xor ghash_poly_8b_temp, ghash_poly_8b_temp
692 cmovc ghash_poly_8b_temp, ghash_poly_8b
693 xor twtempl, ghash_poly_8b_temp
694 mov [TW + 8*12], twtempl ; next Tweak7 generated
695 mov [TW + 8*13], twtemph
698 movdqa %%T0, [keys + 16*9]
700 %if (%%num_blocks>=2)
703 %if (%%num_blocks>=3)
706 %if (%%num_blocks>=4)
709 %if (%%num_blocks>=5)
712 %if (%%num_blocks>=6)
715 %if (%%num_blocks>=7)
720 xor ghash_poly_8b_temp, ghash_poly_8b_temp
723 cmovc ghash_poly_8b_temp, ghash_poly_8b
724 xor twtempl, ghash_poly_8b_temp
725 mov [TW + 8*14], twtempl ; next Tweak8 generated
726 mov [TW + 8*15], twtemph
729 movdqa %%T0, [keys + 16*10]
731 %if (%%num_blocks>=2)
734 %if (%%num_blocks>=3)
737 %if (%%num_blocks>=4)
740 %if (%%num_blocks>=5)
743 %if (%%num_blocks>=6)
746 %if (%%num_blocks>=7)
750 movdqa %%T0, [keys + 16*11]
752 %if (%%num_blocks>=2)
755 %if (%%num_blocks>=3)
758 %if (%%num_blocks>=4)
761 %if (%%num_blocks>=5)
764 %if (%%num_blocks>=6)
767 %if (%%num_blocks>=7)
772 movdqa %%T0, [keys + 16*12]
774 %if (%%num_blocks>=2)
777 %if (%%num_blocks>=3)
780 %if (%%num_blocks>=4)
783 %if (%%num_blocks>=5)
786 %if (%%num_blocks>=6)
789 %if (%%num_blocks>=7)
794 movdqa %%T0, [keys + 16*13]
796 %if (%%num_blocks>=2)
799 %if (%%num_blocks>=3)
802 %if (%%num_blocks>=4)
805 %if (%%num_blocks>=5)
808 %if (%%num_blocks>=6)
811 %if (%%num_blocks>=7)
816 movdqa %%T0, [keys + 16*14]
817 aesdeclast %%ST1, %%T0
818 %if (%%num_blocks>=2)
819 aesdeclast %%ST2, %%T0
821 %if (%%num_blocks>=3)
822 aesdeclast %%ST3, %%T0
824 %if (%%num_blocks>=4)
825 aesdeclast %%ST4, %%T0
827 %if (%%num_blocks>=5)
828 aesdeclast %%ST5, %%T0
830 %if (%%num_blocks>=6)
831 aesdeclast %%ST6, %%T0
833 %if (%%num_blocks>=7)
834 aesdeclast %%ST7, %%T0
839 %if (%%num_blocks>=2)
842 %if (%%num_blocks>=3)
845 %if (%%num_blocks>=4)
848 %if (%%num_blocks>=5)
851 %if (%%num_blocks>=6)
854 %if (%%num_blocks>=7)
860 ; load next Tweak values
861 movdqa %%TW1, [TW + 16*0]
862 movdqa %%TW2, [TW + 16*1]
863 movdqa %%TW3, [TW + 16*2]
864 movdqa %%TW4, [TW + 16*3]
865 movdqa %%TW5, [TW + 16*4]
866 movdqa %%TW6, [TW + 16*5]
867 movdqa %%TW7, [TW + 16*6]
874 ; Encrypt 8 blocks in parallel
875 ; generate next 8 tweak values
876 %macro encrypt_by_eight 18
877 %define %%ST1 %1 ; state 1
878 %define %%ST2 %2 ; state 2
879 %define %%ST3 %3 ; state 3
880 %define %%ST4 %4 ; state 4
881 %define %%ST5 %5 ; state 5
882 %define %%ST6 %6 ; state 6
883 %define %%ST7 %7 ; state 7
884 %define %%ST8 %8 ; state 8
885 %define %%TW1 %9 ; tweak 1
886 %define %%TW2 %10 ; tweak 2
887 %define %%TW3 %11 ; tweak 3
888 %define %%TW4 %12 ; tweak 4
889 %define %%TW5 %13 ; tweak 5
890 %define %%TW6 %14 ; tweak 6
891 %define %%TW7 %15 ; tweak 7
892 %define %%TW8 %16 ; tweak 8
893 %define %%T0 %17 ; Temp register
894 %define %%last_eight %18
917 %if (0 == %%last_eight)
918 xor ghash_poly_8b_temp, ghash_poly_8b_temp
921 cmovc ghash_poly_8b_temp, ghash_poly_8b
924 movdqa %%T0, [keys + 16*1]
933 %if (0 == %%last_eight)
934 xor twtempl, ghash_poly_8b_temp
935 mov [TW + 8*0], twtempl
936 mov [TW + 8*1], twtemph
937 xor ghash_poly_8b_temp, ghash_poly_8b_temp
940 movdqa %%T0, [keys + 16*2]
949 %if (0 == %%last_eight)
952 cmovc ghash_poly_8b_temp, ghash_poly_8b
953 xor twtempl, ghash_poly_8b_temp
957 movdqa %%T0, [keys + 16*3]
966 %if (0 == %%last_eight)
967 mov [TW + 8*2], twtempl
968 mov [TW + 8*3], twtemph
969 xor ghash_poly_8b_temp, ghash_poly_8b_temp
973 movdqa %%T0, [keys + 16*4]
982 %if (0 == %%last_eight)
984 cmovc ghash_poly_8b_temp, ghash_poly_8b
985 xor twtempl, ghash_poly_8b_temp
986 mov [TW + 8*4], twtempl
989 movdqa %%T0, [keys + 16*5]
998 %if (0 == %%last_eight)
999 mov [TW + 8*5], twtemph
1000 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1002 adc twtemph, twtemph
1005 movdqa %%T0, [keys + 16*6]
1014 %if (0 == %%last_eight)
1015 cmovc ghash_poly_8b_temp, ghash_poly_8b
1016 xor twtempl, ghash_poly_8b_temp
1017 mov [TW + 8*6], twtempl
1018 mov [TW + 8*7], twtemph
1021 movdqa %%T0, [keys + 16*7]
1030 %if (0 == %%last_eight)
1031 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1033 adc twtemph, twtemph
1034 cmovc ghash_poly_8b_temp, ghash_poly_8b
1037 movdqa %%T0, [keys + 16*8]
1046 %if (0 == %%last_eight)
1047 xor twtempl, ghash_poly_8b_temp
1048 mov [TW + 8*8], twtempl
1049 mov [TW + 8*9], twtemph
1050 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1053 movdqa %%T0, [keys + 16*9]
1062 %if (0 == %%last_eight)
1064 adc twtemph, twtemph
1065 cmovc ghash_poly_8b_temp, ghash_poly_8b
1066 xor twtempl, ghash_poly_8b_temp
1069 movdqa %%T0, [keys + 16*10]
1078 %if (0 == %%last_eight)
1079 mov [TW + 8*10], twtempl
1080 mov [TW + 8*11], twtemph
1081 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1085 movdqa %%T0, [keys + 16*11]
1094 %if (0 == %%last_eight)
1095 adc twtemph, twtemph
1096 cmovc ghash_poly_8b_temp, ghash_poly_8b
1097 xor twtempl, ghash_poly_8b_temp
1098 mov [TW + 8*12], twtempl
1101 movdqa %%T0, [keys + 16*12]
1110 %if (0 == %%last_eight)
1111 mov [TW + 8*13], twtemph
1112 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1114 adc twtemph, twtemph
1117 movdqa %%T0, [keys + 16*13]
1126 %if (0 == %%last_eight)
1127 cmovc ghash_poly_8b_temp, ghash_poly_8b
1128 xor twtempl, ghash_poly_8b_temp
1129 ; mov [TW + 8*14], twtempl
1130 ; mov [TW + 8*15], twtemph
1133 movdqa %%T0, [keys + 16*14]
1134 aesdeclast %%ST1, %%T0
1135 aesdeclast %%ST2, %%T0
1136 aesdeclast %%ST3, %%T0
1137 aesdeclast %%ST4, %%T0
1138 aesdeclast %%ST5, %%T0
1139 aesdeclast %%ST6, %%T0
1140 aesdeclast %%ST7, %%T0
1141 aesdeclast %%ST8, %%T0
1153 mov [TW + 8*14], twtempl
1154 mov [TW + 8*15], twtemph
1155 ; load next Tweak values
1156 movdqa %%TW1, [TW + 16*0]
1157 movdqa %%TW2, [TW + 16*1]
1158 movdqa %%TW3, [TW + 16*2]
1159 movdqa %%TW4, [TW + 16*3]
1160 movdqa %%TW5, [TW + 16*4]
1161 movdqa %%TW6, [TW + 16*5]
1162 movdqa %%TW7, [TW + 16*6]
1169 global XTS_AES_256_dec_sse:function
1170 XTS_AES_256_dec_sse:
1172 sub rsp, VARIABLE_OFFSET
1174 mov [_gpr + 8*0], rbx
1175 %ifidn __OUTPUT_FORMAT__, win64
1176 mov [_gpr + 8*1], rdi
1177 mov [_gpr + 8*2], rsi
1179 movdqa [_xmm + 16*0], xmm6
1180 movdqa [_xmm + 16*1], xmm7
1181 movdqa [_xmm + 16*2], xmm8
1182 movdqa [_xmm + 16*3], xmm9
1183 movdqa [_xmm + 16*4], xmm10
1184 movdqa [_xmm + 16*5], xmm11
1185 movdqa [_xmm + 16*6], xmm12
1186 movdqa [_xmm + 16*7], xmm13
1187 movdqa [_xmm + 16*8], xmm14
1188 movdqa [_xmm + 16*9], xmm15
1191 mov ghash_poly_8b, GHASH_POLY ; load 0x87 to ghash_poly_8b
1194 movdqu xmm1, [T_val] ; read initial Tweak value
1195 pxor xmm4, xmm4 ; for key expansion
1196 encrypt_T xmm0, xmm5, xmm1, xmm2, xmm6, xmm3, xmm4, xmm7, ptr_key2, ptr_key1, keys
1199 %ifidn __OUTPUT_FORMAT__, win64
1200 mov ptr_plaintext, [rsp + VARIABLE_OFFSET + 8*5] ; plaintext pointer
1201 mov ptr_ciphertext, [rsp + VARIABLE_OFFSET + 8*6] ; ciphertext pointer
1206 mov target_ptr_val, N_val
1207 and target_ptr_val, -16 ; target_ptr_val = target_ptr_val - (target_ptr_val mod 16)
1208 sub target_ptr_val, 128 ; adjust target_ptr_val because last 4 blocks will not be stitched with Tweak calculations
1209 jl _less_than_128_bytes
1211 add target_ptr_val, ptr_ciphertext
1216 jz _initial_num_blocks_is_0
1219 je _initial_num_blocks_is_4
1224 je _initial_num_blocks_is_6
1227 je _initial_num_blocks_is_5
1232 je _initial_num_blocks_is_3
1235 je _initial_num_blocks_is_2
1238 je _initial_num_blocks_is_1
1240 _initial_num_blocks_is_7:
1241 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 7
1242 add ptr_plaintext, 16*7
1243 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 0
1245 movdqu [ptr_ciphertext+16*0], xmm1
1246 movdqu [ptr_ciphertext+16*1], xmm2
1247 movdqu [ptr_ciphertext+16*2], xmm3
1248 movdqu [ptr_ciphertext+16*3], xmm4
1249 movdqu [ptr_ciphertext+16*4], xmm5
1250 movdqu [ptr_ciphertext+16*5], xmm6
1251 movdqu [ptr_ciphertext+16*6], xmm7
1252 add ptr_ciphertext, 16*7
1254 cmp ptr_ciphertext, target_ptr_val
1258 _initial_num_blocks_is_6:
1259 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 6
1260 add ptr_plaintext, 16*6
1261 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 0
1263 movdqu [ptr_ciphertext+16*0], xmm1
1264 movdqu [ptr_ciphertext+16*1], xmm2
1265 movdqu [ptr_ciphertext+16*2], xmm3
1266 movdqu [ptr_ciphertext+16*3], xmm4
1267 movdqu [ptr_ciphertext+16*4], xmm5
1268 movdqu [ptr_ciphertext+16*5], xmm6
1269 add ptr_ciphertext, 16*6
1271 cmp ptr_ciphertext, target_ptr_val
1275 _initial_num_blocks_is_5:
1276 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 5
1277 add ptr_plaintext, 16*5
1278 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 0
1280 movdqu [ptr_ciphertext+16*0], xmm1
1281 movdqu [ptr_ciphertext+16*1], xmm2
1282 movdqu [ptr_ciphertext+16*2], xmm3
1283 movdqu [ptr_ciphertext+16*3], xmm4
1284 movdqu [ptr_ciphertext+16*4], xmm5
1285 add ptr_ciphertext, 16*5
1287 cmp ptr_ciphertext, target_ptr_val
1291 _initial_num_blocks_is_4:
1292 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 4
1293 add ptr_plaintext, 16*4
1294 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 0
1296 movdqu [ptr_ciphertext+16*0], xmm1
1297 movdqu [ptr_ciphertext+16*1], xmm2
1298 movdqu [ptr_ciphertext+16*2], xmm3
1299 movdqu [ptr_ciphertext+16*3], xmm4
1300 add ptr_ciphertext, 16*4
1302 cmp ptr_ciphertext, target_ptr_val
1308 _initial_num_blocks_is_3:
1309 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 3
1310 add ptr_plaintext, 16*3
1311 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 0
1313 movdqu [ptr_ciphertext+16*0], xmm1
1314 movdqu [ptr_ciphertext+16*1], xmm2
1315 movdqu [ptr_ciphertext+16*2], xmm3
1316 add ptr_ciphertext, 16*3
1318 cmp ptr_ciphertext, target_ptr_val
1322 _initial_num_blocks_is_2:
1323 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 2
1324 add ptr_plaintext, 16*2
1325 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 0
1327 movdqu [ptr_ciphertext], xmm1
1328 movdqu [ptr_ciphertext+16], xmm2
1329 add ptr_ciphertext, 16*2
1331 cmp ptr_ciphertext, target_ptr_val
1336 _initial_num_blocks_is_1:
1337 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 1
1338 add ptr_plaintext, 16*1
1339 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 0
1341 movdqu [ptr_ciphertext], xmm1
1342 add ptr_ciphertext, 16
1344 cmp ptr_ciphertext, target_ptr_val
1349 _initial_num_blocks_is_0:
1350 mov twtempl, [TW+8*0]
1351 mov twtemph, [TW+8*1]
1352 movdqa xmm9, [TW+16*0]
1354 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1356 adc twtemph, twtemph
1357 cmovc ghash_poly_8b_temp, ghash_poly_8b
1358 xor twtempl, ghash_poly_8b_temp
1359 mov [TW+8*2], twtempl
1360 mov [TW+8*3], twtemph
1361 movdqa xmm10, [TW+16*1]
1363 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1365 adc twtemph, twtemph
1366 cmovc ghash_poly_8b_temp, ghash_poly_8b
1367 xor twtempl, ghash_poly_8b_temp
1368 mov [TW+8*4], twtempl
1369 mov [TW+8*5], twtemph
1370 movdqa xmm11, [TW+16*2]
1373 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1375 adc twtemph, twtemph
1376 cmovc ghash_poly_8b_temp, ghash_poly_8b
1377 xor twtempl, ghash_poly_8b_temp
1378 mov [TW+8*6], twtempl
1379 mov [TW+8*7], twtemph
1380 movdqa xmm12, [TW+16*3]
1383 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1385 adc twtemph, twtemph
1386 cmovc ghash_poly_8b_temp, ghash_poly_8b
1387 xor twtempl, ghash_poly_8b_temp
1388 mov [TW+8*8], twtempl
1389 mov [TW+8*9], twtemph
1390 movdqa xmm13, [TW+16*4]
1392 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1394 adc twtemph, twtemph
1395 cmovc ghash_poly_8b_temp, ghash_poly_8b
1396 xor twtempl, ghash_poly_8b_temp
1397 mov [TW+8*10], twtempl
1398 mov [TW+8*11], twtemph
1399 movdqa xmm14, [TW+16*5]
1401 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1403 adc twtemph, twtemph
1404 cmovc ghash_poly_8b_temp, ghash_poly_8b
1405 xor twtempl, ghash_poly_8b_temp
1406 mov [TW+8*12], twtempl
1407 mov [TW+8*13], twtemph
1408 movdqa xmm15, [TW+16*6]
1410 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1412 adc twtemph, twtemph
1413 cmovc ghash_poly_8b_temp, ghash_poly_8b
1414 xor twtempl, ghash_poly_8b_temp
1415 mov [TW+8*14], twtempl
1416 mov [TW+8*15], twtemph
1417 ;movdqa xmm16, [TW+16*7]
1419 cmp ptr_ciphertext, target_ptr_val
1423 movdqu xmm1, [ptr_plaintext+16*0]
1424 movdqu xmm2, [ptr_plaintext+16*1]
1425 movdqu xmm3, [ptr_plaintext+16*2]
1426 movdqu xmm4, [ptr_plaintext+16*3]
1427 movdqu xmm5, [ptr_plaintext+16*4]
1428 movdqu xmm6, [ptr_plaintext+16*5]
1429 movdqu xmm7, [ptr_plaintext+16*6]
1430 movdqu xmm8, [ptr_plaintext+16*7]
1432 add ptr_plaintext, 128
1434 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 0
1437 movdqu [ptr_ciphertext+16*0], xmm1
1438 movdqu [ptr_ciphertext+16*1], xmm2
1439 movdqu [ptr_ciphertext+16*2], xmm3
1440 movdqu [ptr_ciphertext+16*3], xmm4
1441 movdqu [ptr_ciphertext+16*4], xmm5
1442 movdqu [ptr_ciphertext+16*5], xmm6
1443 movdqu [ptr_ciphertext+16*6], xmm7
1444 movdqu [ptr_ciphertext+16*7], xmm8
1445 add ptr_ciphertext, 128
1447 cmp ptr_ciphertext, target_ptr_val
1452 and N_val, 15 ; N_val = N_val mod 16
1455 ; generate next Tweak value
1456 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1458 adc twtemph, twtemph
1459 cmovc ghash_poly_8b_temp, ghash_poly_8b
1460 xor twtempl, ghash_poly_8b_temp
1461 movdqa xmm1, [TW + 16*7]
1462 movdqa [TW + 16*0], xmm1 ; swap tweak values for cipher stealing for decrypt
1464 mov [TW + 16*7], twtempl
1465 mov [TW + 16*7+8], twtemph
1468 movdqu xmm1, [ptr_plaintext+16*0]
1469 movdqu xmm2, [ptr_plaintext+16*1]
1470 movdqu xmm3, [ptr_plaintext+16*2]
1471 movdqu xmm4, [ptr_plaintext+16*3]
1472 movdqu xmm5, [ptr_plaintext+16*4]
1473 movdqu xmm6, [ptr_plaintext+16*5]
1474 movdqu xmm7, [ptr_plaintext+16*6]
1475 movdqu xmm8, [ptr_plaintext+16*7]
1476 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 1
1479 movdqu [ptr_ciphertext+16*0], xmm1
1480 movdqu [ptr_ciphertext+16*1], xmm2
1481 movdqu [ptr_ciphertext+16*2], xmm3
1482 movdqu [ptr_ciphertext+16*3], xmm4
1483 movdqu [ptr_ciphertext+16*4], xmm5
1484 movdqu [ptr_ciphertext+16*5], xmm6
1485 movdqu [ptr_ciphertext+16*6], xmm7
1491 movdqu xmm1, [ptr_plaintext+16*0]
1492 movdqu xmm2, [ptr_plaintext+16*1]
1493 movdqu xmm3, [ptr_plaintext+16*2]
1494 movdqu xmm4, [ptr_plaintext+16*3]
1495 movdqu xmm5, [ptr_plaintext+16*4]
1496 movdqu xmm6, [ptr_plaintext+16*5]
1497 movdqu xmm7, [ptr_plaintext+16*6]
1498 movdqu xmm8, [ptr_plaintext+16*7]
1499 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 1
1502 movdqu [ptr_ciphertext+16*0], xmm1
1503 movdqu [ptr_ciphertext+16*1], xmm2
1504 movdqu [ptr_ciphertext+16*2], xmm3
1505 movdqu [ptr_ciphertext+16*3], xmm4
1506 movdqu [ptr_ciphertext+16*4], xmm5
1507 movdqu [ptr_ciphertext+16*5], xmm6
1508 movdqu [ptr_ciphertext+16*6], xmm7
1514 ; start cipher stealing
1519 ; shift xmm8 to the left by 16-N_val bytes
1520 lea twtempl, [pshufb_shf_table]
1521 movdqu xmm0, [twtempl+N_val]
1525 movdqu xmm3, [ptr_plaintext + 112 + N_val] ; state register is temporarily xmm3 to eliminate a move
1526 movdqu [ptr_ciphertext + 112 + N_val], xmm8
1528 ; shift xmm3 to the right by 16-N_val bytes
1529 lea twtempl, [pshufb_shf_table +16]
1531 movdqu xmm0, [twtempl]
1535 pblendvb xmm3, xmm2 ;xmm0 is implicit
1539 pxor xmm8, xmm3 ; state register is xmm8, instead of a move from xmm3 to xmm8, destination register of pxor instruction is swapped
1542 ;encrypt last block with cipher stealing
1543 pxor xmm8, [keys] ; ARK
1544 aesdec xmm8, [keys + 16*1] ; round 1
1545 aesdec xmm8, [keys + 16*2] ; round 2
1546 aesdec xmm8, [keys + 16*3] ; round 3
1547 aesdec xmm8, [keys + 16*4] ; round 4
1548 aesdec xmm8, [keys + 16*5] ; round 5
1549 aesdec xmm8, [keys + 16*6] ; round 6
1550 aesdec xmm8, [keys + 16*7] ; round 7
1551 aesdec xmm8, [keys + 16*8] ; round 8
1552 aesdec xmm8, [keys + 16*9] ; round 9
1553 aesdec xmm8, [keys + 16*10] ; round 9
1554 aesdec xmm8, [keys + 16*11] ; round 9
1555 aesdec xmm8, [keys + 16*12] ; round 9
1556 aesdec xmm8, [keys + 16*13] ; round 9
1557 aesdeclast xmm8, [keys + 16*14] ; round 10
1563 ; store last ciphertext value
1564 movdqu [ptr_ciphertext+16*7], xmm8
1568 mov rbx, [_gpr + 8*0]
1569 %ifidn __OUTPUT_FORMAT__, win64
1570 mov rdi, [_gpr + 8*1]
1571 mov rsi, [_gpr + 8*2]
1574 movdqa xmm6, [_xmm + 16*0]
1575 movdqa xmm7, [_xmm + 16*1]
1576 movdqa xmm8, [_xmm + 16*2]
1577 movdqa xmm9, [_xmm + 16*3]
1578 movdqa xmm10, [_xmm + 16*4]
1579 movdqa xmm11, [_xmm + 16*5]
1580 movdqa xmm12, [_xmm + 16*6]
1581 movdqa xmm13, [_xmm + 16*7]
1582 movdqa xmm14, [_xmm + 16*8]
1583 movdqa xmm15, [_xmm + 16*9]
1586 add rsp, VARIABLE_OFFSET
1594 _less_than_128_bytes:
1617 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 7
1619 sub ptr_plaintext, 16*1
1621 and N_val, 15 ; N_val = N_val mod 16
1625 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1627 adc twtemph, twtemph
1628 cmovc ghash_poly_8b_temp, ghash_poly_8b
1629 xor twtempl, ghash_poly_8b_temp
1630 mov [TW+8*2], twtempl
1631 mov [TW+8*3], twtemph
1633 movdqa [TW + 16*0] , xmm15
1634 movdqa xmm15, [TW+16*1]
1636 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 1
1638 movdqu [ptr_ciphertext+16*0], xmm1
1639 movdqu [ptr_ciphertext+16*1], xmm2
1640 movdqu [ptr_ciphertext+16*2], xmm3
1641 movdqu [ptr_ciphertext+16*3], xmm4
1642 movdqu [ptr_ciphertext+16*4], xmm5
1643 movdqu [ptr_ciphertext+16*5], xmm6
1645 sub ptr_ciphertext, 16*1
1650 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 1
1652 movdqu [ptr_ciphertext+16*0], xmm1
1653 movdqu [ptr_ciphertext+16*1], xmm2
1654 movdqu [ptr_ciphertext+16*2], xmm3
1655 movdqu [ptr_ciphertext+16*3], xmm4
1656 movdqu [ptr_ciphertext+16*4], xmm5
1657 movdqu [ptr_ciphertext+16*5], xmm6
1659 sub ptr_ciphertext, 16*1
1669 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 6
1671 sub ptr_plaintext, 16*2
1673 and N_val, 15 ; N_val = N_val mod 16
1677 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1679 adc twtemph, twtemph
1680 cmovc ghash_poly_8b_temp, ghash_poly_8b
1681 xor twtempl, ghash_poly_8b_temp
1682 mov [TW+8*2], twtempl
1683 mov [TW+8*3], twtemph
1685 movdqa [TW + 16*0] , xmm14
1686 movdqa xmm14, [TW+16*1]
1688 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 1
1690 movdqu [ptr_ciphertext+16*0], xmm1
1691 movdqu [ptr_ciphertext+16*1], xmm2
1692 movdqu [ptr_ciphertext+16*2], xmm3
1693 movdqu [ptr_ciphertext+16*3], xmm4
1694 movdqu [ptr_ciphertext+16*4], xmm5
1696 sub ptr_ciphertext, 16*2
1701 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 1
1703 movdqu [ptr_ciphertext+16*0], xmm1
1704 movdqu [ptr_ciphertext+16*1], xmm2
1705 movdqu [ptr_ciphertext+16*2], xmm3
1706 movdqu [ptr_ciphertext+16*3], xmm4
1707 movdqu [ptr_ciphertext+16*4], xmm5
1709 sub ptr_ciphertext, 16*2
1718 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 5
1720 sub ptr_plaintext, 16*3
1722 and N_val, 15 ; N_val = N_val mod 16
1726 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1728 adc twtemph, twtemph
1729 cmovc ghash_poly_8b_temp, ghash_poly_8b
1730 xor twtempl, ghash_poly_8b_temp
1731 mov [TW+8*2], twtempl
1732 mov [TW+8*3], twtemph
1734 movdqa [TW + 16*0] , xmm13
1735 movdqa xmm13, [TW+16*1]
1737 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 1
1739 movdqu [ptr_ciphertext+16*0], xmm1
1740 movdqu [ptr_ciphertext+16*1], xmm2
1741 movdqu [ptr_ciphertext+16*2], xmm3
1742 movdqu [ptr_ciphertext+16*3], xmm4
1744 sub ptr_ciphertext, 16*3
1749 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 1
1751 movdqu [ptr_ciphertext+16*0], xmm1
1752 movdqu [ptr_ciphertext+16*1], xmm2
1753 movdqu [ptr_ciphertext+16*2], xmm3
1754 movdqu [ptr_ciphertext+16*3], xmm4
1756 sub ptr_ciphertext, 16*3
1765 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 4
1767 sub ptr_plaintext, 16*4
1769 and N_val, 15 ; N_val = N_val mod 16
1773 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1775 adc twtemph, twtemph
1776 cmovc ghash_poly_8b_temp, ghash_poly_8b
1777 xor twtempl, ghash_poly_8b_temp
1778 mov [TW+8*2], twtempl
1779 mov [TW+8*3], twtemph
1781 movdqa [TW + 16*0] , xmm12
1782 movdqa xmm12, [TW+16*1]
1784 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 1
1786 movdqu [ptr_ciphertext+16*0], xmm1
1787 movdqu [ptr_ciphertext+16*1], xmm2
1788 movdqu [ptr_ciphertext+16*2], xmm3
1790 sub ptr_ciphertext, 16*4
1795 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 1
1797 movdqu [ptr_ciphertext+16*0], xmm1
1798 movdqu [ptr_ciphertext+16*1], xmm2
1799 movdqu [ptr_ciphertext+16*2], xmm3
1801 sub ptr_ciphertext, 16*4
1809 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 3
1811 sub ptr_plaintext, 16*5
1813 and N_val, 15 ; N_val = N_val mod 16
1817 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1819 adc twtemph, twtemph
1820 cmovc ghash_poly_8b_temp, ghash_poly_8b
1821 xor twtempl, ghash_poly_8b_temp
1822 mov [TW+8*2], twtempl
1823 mov [TW+8*3], twtemph
1825 movdqa [TW + 16*0] , xmm11
1826 movdqa xmm11, [TW+16*1]
1828 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 1
1830 movdqu [ptr_ciphertext+16*0], xmm1
1831 movdqu [ptr_ciphertext+16*1], xmm2
1833 sub ptr_ciphertext, 16*5
1838 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 1
1840 movdqu [ptr_ciphertext+16*0], xmm1
1841 movdqu [ptr_ciphertext+16*1], xmm2
1843 sub ptr_ciphertext, 16*5
1853 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 2
1855 sub ptr_plaintext, 16*6
1857 and N_val, 15 ; N_val = N_val mod 16
1861 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1863 adc twtemph, twtemph
1864 cmovc ghash_poly_8b_temp, ghash_poly_8b
1865 xor twtempl, ghash_poly_8b_temp
1866 mov [TW+8*2], twtempl
1867 mov [TW+8*3], twtemph
1869 movdqa [TW + 16*0] , xmm10
1870 movdqa xmm10, [TW+16*1]
1872 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 1
1874 movdqu [ptr_ciphertext], xmm1
1876 sub ptr_ciphertext, 16*6
1881 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 1
1883 movdqu [ptr_ciphertext], xmm1
1885 sub ptr_ciphertext, 16*6
1902 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 1
1904 sub ptr_plaintext, 16*7
1906 and N_val, 15 ; N_val = N_val mod 16
1910 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1912 adc twtemph, twtemph
1913 cmovc ghash_poly_8b_temp, ghash_poly_8b
1914 xor twtempl, ghash_poly_8b_temp
1915 mov [TW+8*2], twtempl
1916 mov [TW+8*3], twtemph
1918 movdqa [TW + 16*0] , xmm9
1919 movdqa xmm9, [TW+16*1]
1921 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 1
1924 sub ptr_ciphertext, 16*7
1929 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 1
1932 sub ptr_ciphertext, 16*7
1940 ; use these values for shift constants for the pshufb instruction
1941 ; different alignments result in values as shown:
1942 ; dq 0x8887868584838281, 0x008f8e8d8c8b8a89 ; shl 15 (16-1) / shr1
1943 ; dq 0x8988878685848382, 0x01008f8e8d8c8b8a ; shl 14 (16-3) / shr2
1944 ; dq 0x8a89888786858483, 0x0201008f8e8d8c8b ; shl 13 (16-4) / shr3
1945 ; dq 0x8b8a898887868584, 0x030201008f8e8d8c ; shl 12 (16-4) / shr4
1946 ; dq 0x8c8b8a8988878685, 0x04030201008f8e8d ; shl 11 (16-5) / shr5
1947 ; dq 0x8d8c8b8a89888786, 0x0504030201008f8e ; shl 10 (16-6) / shr6
1948 ; dq 0x8e8d8c8b8a898887, 0x060504030201008f ; shl 9 (16-7) / shr7
1949 ; dq 0x8f8e8d8c8b8a8988, 0x0706050403020100 ; shl 8 (16-8) / shr8
1950 ; dq 0x008f8e8d8c8b8a89, 0x0807060504030201 ; shl 7 (16-9) / shr9
1951 ; dq 0x01008f8e8d8c8b8a, 0x0908070605040302 ; shl 6 (16-10) / shr10
1952 ; dq 0x0201008f8e8d8c8b, 0x0a09080706050403 ; shl 5 (16-11) / shr11
1953 ; dq 0x030201008f8e8d8c, 0x0b0a090807060504 ; shl 4 (16-12) / shr12
1954 ; dq 0x04030201008f8e8d, 0x0c0b0a0908070605 ; shl 3 (16-13) / shr13
1955 ; dq 0x0504030201008f8e, 0x0d0c0b0a09080706 ; shl 2 (16-14) / shr14
1956 ; dq 0x060504030201008f, 0x0e0d0c0b0a090807 ; shl 1 (16-15) / shr15
1957 dq 0x8786858483828100, 0x8f8e8d8c8b8a8988
1958 dq 0x0706050403020100, 0x000e0d0c0b0a0908
1961 dq 0x8080808080808080, 0x8080808080808080