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 128-bit AES
30 ; expanded keys are not aligned
31 ; plaintext and ciphertext are not aligned
32 ; second key is stored in the stack as aligned to 16 Bytes
33 ; first key is required only once, no need for storage of this key
35 %include "reg_sizes.asm"
38 %define TW rsp ; store 8 tweak values
39 %define keys rsp + 16*8 ; store 11 expanded keys
41 %ifidn __OUTPUT_FORMAT__, win64
42 %define _xmm rsp + 16*19 ; store xmm6:xmm15
45 %ifidn __OUTPUT_FORMAT__, elf64
46 %define _gpr rsp + 16*19 ; store rbx
47 %define VARIABLE_OFFSET 16*8 + 16*11 + 8*1 ; VARIABLE_OFFSET has to be an odd multiple of 8
49 %define _gpr rsp + 16*29 ; store rdi, rsi, rbx
50 %define VARIABLE_OFFSET 16*8 + 16*11 + 16*10 + 8*3 ; VARIABLE_OFFSET has to be an odd multiple of 8
53 %define GHASH_POLY 0x87
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;void XTS_AES_128_dec_expanded_key_avx(
57 ; UINT8 *k2, // key used for tweaking, 16*11 bytes
58 ; UINT8 *k1, // key used for "ECB" encryption, 16*11 bytes
59 ; UINT8 *TW_initial, // initial tweak value, 16 bytes
60 ; UINT64 N, // sector size, in bytes
61 ; const UINT8 *ct, // ciphertext sector input data
62 ; UINT8 *pt); // plaintext sector output data
63 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 ; arguments for input parameters
66 %ifidn __OUTPUT_FORMAT__, elf64
71 %xdefine ptr_plaintext r8
72 %xdefine ptr_ciphertext r9
78 %xdefine ptr_plaintext r10; [rsp + VARIABLE_OFFSET + 8*5]
79 %xdefine ptr_ciphertext r11; [rsp + VARIABLE_OFFSET + 8*6]
82 ; arguments for temp parameters
83 %ifidn __OUTPUT_FORMAT__, elf64
85 %define target_ptr_val rsi
86 %define ghash_poly_8b r10
87 %define ghash_poly_8b_temp r11
90 %define target_ptr_val rdx
91 %define ghash_poly_8b rdi
92 %define ghash_poly_8b_temp rsi
95 %define twtempl rax ; global temp registers used for tweak computation
99 ; macro to encrypt the tweak value
103 %define %%xstate_tweak %2
105 %define %%xraw_key %4
107 %define %%ptr_key2 %6
108 %define %%ptr_key1 %7
109 %define %%ptr_expanded_keys %8
111 vmovdqu %%xkey2, [%%ptr_key2]
112 vpxor %%xstate_tweak, %%xkey2 ; ARK for tweak encryption
114 vmovdqu %%xkey1, [%%ptr_key1 + 16*10]
115 vmovdqa [%%ptr_expanded_keys+16*10], %%xkey1 ; store round keys in stack
117 vmovdqu %%xkey2, [%%ptr_key2 + 16*1]
118 vaesenc %%xstate_tweak, %%xkey2 ; round 1 for tweak encryption
120 vmovdqu %%xkey1, [%%ptr_key1 + 16*9]
121 vmovdqa [%%ptr_expanded_keys+16*9], %%xkey1 ; store round keys in stack
124 vmovdqu %%xkey2, [%%ptr_key2 + 16*2]
125 vaesenc %%xstate_tweak, %%xkey2 ; round 2 for tweak encryption
127 vmovdqu %%xkey1, [%%ptr_key1 + 16*8]
128 vmovdqa [%%ptr_expanded_keys+16*8], %%xkey1 ; store round keys in stack
130 vmovdqu %%xkey2, [%%ptr_key2 + 16*3]
131 vaesenc %%xstate_tweak, %%xkey2 ; round 3 for tweak encryption
133 vmovdqu %%xkey1, [%%ptr_key1 + 16*7]
134 vmovdqa [%%ptr_expanded_keys+16*7], %%xkey1 ; store round keys in stack
136 vmovdqu %%xkey2, [%%ptr_key2 + 16*4]
137 vaesenc %%xstate_tweak, %%xkey2 ; round 4 for tweak encryption
139 vmovdqu %%xkey1, [%%ptr_key1 + 16*6]
140 vmovdqa [%%ptr_expanded_keys+16*6], %%xkey1 ; store round keys in stack
142 vmovdqu %%xkey2, [%%ptr_key2 + 16*5]
143 vaesenc %%xstate_tweak, %%xkey2 ; round 5 for tweak encryption
145 vmovdqu %%xkey1, [%%ptr_key1 + 16*5]
146 vmovdqa [%%ptr_expanded_keys+16*5], %%xkey1 ; store round keys in stack
148 vmovdqu %%xkey2, [%%ptr_key2 + 16*6]
149 vaesenc %%xstate_tweak, %%xkey2 ; round 6 for tweak encryption
151 vmovdqu %%xkey1, [%%ptr_key1 + 16*4]
152 vmovdqa [%%ptr_expanded_keys+16*4], %%xkey1 ; store round keys in stack
154 vmovdqu %%xkey2, [%%ptr_key2 + 16*7]
155 vaesenc %%xstate_tweak, %%xkey2 ; round 7 for tweak encryption
157 vmovdqu %%xkey1, [%%ptr_key1 + 16*3]
158 vmovdqa [%%ptr_expanded_keys+16*3], %%xkey1 ; store round keys in stack
161 vmovdqu %%xkey2, [%%ptr_key2 + 16*8]
162 vaesenc %%xstate_tweak, %%xkey2 ; round 8 for tweak encryption
164 vmovdqu %%xkey1, [%%ptr_key1 + 16*2]
165 vmovdqa [%%ptr_expanded_keys+16*2], %%xkey1 ; store round keys in stack
168 vmovdqu %%xkey2, [%%ptr_key2 + 16*9]
169 vaesenc %%xstate_tweak, %%xkey2 ; round 9 for tweak encryption
171 vmovdqu %%xkey1, [%%ptr_key1 + 16*1]
172 vmovdqa [%%ptr_expanded_keys+16*1], %%xkey1 ; store round keys in stack
177 vmovdqu %%xkey2, [%%ptr_key2 + 16*10]
178 vaesenclast %%xstate_tweak, %%xkey2 ; round 10 for tweak encryption
180 vmovdqu %%xkey1, [%%ptr_key1 + 16*0]
181 vmovdqa [%%ptr_expanded_keys+16*0], %%xkey1 ; store round keys in stack
183 vmovdqa [TW], %%xstate_tweak ; Store the encrypted Tweak value
187 ; generate initial tweak values
188 ; load initial plaintext values
191 %define %%ST1 %1 ; state 1
192 %define %%ST2 %2 ; state 2
193 %define %%ST3 %3 ; state 3
194 %define %%ST4 %4 ; state 4
195 %define %%ST5 %5 ; state 5
196 %define %%ST6 %6 ; state 6
197 %define %%ST7 %7 ; state 7
198 %define %%ST8 %8 ; state 8
200 %define %%TW1 %9 ; tweak 1
201 %define %%TW2 %10 ; tweak 2
202 %define %%TW3 %11 ; tweak 3
203 %define %%TW4 %12 ; tweak 4
204 %define %%TW5 %13 ; tweak 5
205 %define %%TW6 %14 ; tweak 6
206 %define %%TW7 %15 ; tweak 7
208 %define %%num_initial_blocks %16
211 ; generate next Tweak values
212 vmovdqa %%TW1, [TW+16*0]
213 mov twtempl, [TW+8*0]
214 mov twtemph, [TW+8*1]
215 vmovdqu %%ST1, [ptr_plaintext+16*0]
216 %if (%%num_initial_blocks>=2)
217 xor ghash_poly_8b_temp, ghash_poly_8b_temp
220 cmovc ghash_poly_8b_temp, ghash_poly_8b
221 xor twtempl, ghash_poly_8b_temp
222 mov [TW+8*2], twtempl
223 mov [TW+8*3], twtemph;
224 vmovdqa %%TW2, [TW+16*1]
225 vmovdqu %%ST2, [ptr_plaintext+16*1]
227 %if (%%num_initial_blocks>=3)
228 xor ghash_poly_8b_temp, ghash_poly_8b_temp
231 cmovc ghash_poly_8b_temp, ghash_poly_8b
232 xor twtempl, ghash_poly_8b_temp
233 mov [TW+8*4], twtempl
234 mov [TW+8*5], twtemph;
235 vmovdqa %%TW3, [TW+16*2]
236 vmovdqu %%ST3, [ptr_plaintext+16*2]
238 %if (%%num_initial_blocks>=4)
239 xor ghash_poly_8b_temp, ghash_poly_8b_temp
242 cmovc ghash_poly_8b_temp, ghash_poly_8b
243 xor twtempl, ghash_poly_8b_temp
244 mov [TW+8*6], twtempl
245 mov [TW+8*7], twtemph;
246 vmovdqa %%TW4, [TW+16*3]
247 vmovdqu %%ST4, [ptr_plaintext+16*3]
249 %if (%%num_initial_blocks>=5)
250 xor ghash_poly_8b_temp, ghash_poly_8b_temp
253 cmovc ghash_poly_8b_temp, ghash_poly_8b
254 xor twtempl, ghash_poly_8b_temp
255 mov [TW+8*8], twtempl
256 mov [TW+8*9], twtemph;
257 vmovdqa %%TW5, [TW+16*4]
258 vmovdqu %%ST5, [ptr_plaintext+16*4]
260 %if (%%num_initial_blocks>=6)
261 xor ghash_poly_8b_temp, ghash_poly_8b_temp
264 cmovc ghash_poly_8b_temp, ghash_poly_8b
265 xor twtempl, ghash_poly_8b_temp
266 mov [TW+8*10], twtempl
267 mov [TW+8*11], twtemph;
268 vmovdqa %%TW6, [TW+16*5]
269 vmovdqu %%ST6, [ptr_plaintext+16*5]
271 %if (%%num_initial_blocks>=7)
272 xor ghash_poly_8b_temp, ghash_poly_8b_temp
275 cmovc ghash_poly_8b_temp, ghash_poly_8b
276 xor twtempl, ghash_poly_8b_temp
277 mov [TW+8*12], twtempl
278 mov [TW+8*13], twtemph;
279 vmovdqa %%TW7, [TW+16*6]
280 vmovdqu %%ST7, [ptr_plaintext+16*6]
288 ; encrypt initial blocks of AES
289 ; 1, 2, 3, 4, 5, 6 or 7 blocks are encrypted
290 ; next 8 Tweak values are generated
291 %macro encrypt_initial 18
292 %define %%ST1 %1 ; state 1
293 %define %%ST2 %2 ; state 2
294 %define %%ST3 %3 ; state 3
295 %define %%ST4 %4 ; state 4
296 %define %%ST5 %5 ; state 5
297 %define %%ST6 %6 ; state 6
298 %define %%ST7 %7 ; state 7
299 %define %%ST8 %8 ; state 8
301 %define %%TW1 %9 ; tweak 1
302 %define %%TW2 %10 ; tweak 2
303 %define %%TW3 %11 ; tweak 3
304 %define %%TW4 %12 ; tweak 4
305 %define %%TW5 %13 ; tweak 5
306 %define %%TW6 %14 ; tweak 6
307 %define %%TW7 %15 ; tweak 7
308 %define %%T0 %16 ; Temp register
309 %define %%num_blocks %17
310 ; %%num_blocks blocks encrypted
311 ; %%num_blocks can be 1, 2, 3, 4, 5, 6, 7
313 %define %%lt128 %18 ; less than 128 bytes
317 %if (%%num_blocks>=2)
320 %if (%%num_blocks>=3)
323 %if (%%num_blocks>=4)
326 %if (%%num_blocks>=5)
329 %if (%%num_blocks>=6)
332 %if (%%num_blocks>=7)
340 %if (%%num_blocks>=2)
343 %if (%%num_blocks>=3)
346 %if (%%num_blocks>=4)
349 %if (%%num_blocks>=5)
352 %if (%%num_blocks>=6)
355 %if (%%num_blocks>=7)
361 xor ghash_poly_8b_temp, ghash_poly_8b_temp
367 vmovdqa %%T0, [keys + 16*1]
369 %if (%%num_blocks>=2)
372 %if (%%num_blocks>=3)
375 %if (%%num_blocks>=4)
378 %if (%%num_blocks>=5)
381 %if (%%num_blocks>=6)
384 %if (%%num_blocks>=7)
388 cmovc ghash_poly_8b_temp, ghash_poly_8b
389 xor twtempl, ghash_poly_8b_temp
390 mov [TW + 8*0], twtempl ; next Tweak1 generated
391 mov [TW + 8*1], twtemph
392 xor ghash_poly_8b_temp, ghash_poly_8b_temp
396 vmovdqa %%T0, [keys + 16*2]
398 %if (%%num_blocks>=2)
401 %if (%%num_blocks>=3)
404 %if (%%num_blocks>=4)
407 %if (%%num_blocks>=5)
410 %if (%%num_blocks>=6)
413 %if (%%num_blocks>=7)
420 cmovc ghash_poly_8b_temp, ghash_poly_8b
421 xor twtempl, ghash_poly_8b_temp
422 mov [TW + 8*2], twtempl ; next Tweak2 generated
426 vmovdqa %%T0, [keys + 16*3]
428 %if (%%num_blocks>=2)
431 %if (%%num_blocks>=3)
434 %if (%%num_blocks>=4)
437 %if (%%num_blocks>=5)
440 %if (%%num_blocks>=6)
443 %if (%%num_blocks>=7)
447 mov [TW + 8*3], twtemph
448 xor ghash_poly_8b_temp, ghash_poly_8b_temp
451 cmovc ghash_poly_8b_temp, ghash_poly_8b
455 vmovdqa %%T0, [keys + 16*4]
457 %if (%%num_blocks>=2)
460 %if (%%num_blocks>=3)
463 %if (%%num_blocks>=4)
466 %if (%%num_blocks>=5)
469 %if (%%num_blocks>=6)
472 %if (%%num_blocks>=7)
477 xor twtempl, ghash_poly_8b_temp
478 mov [TW + 8*4], twtempl ; next Tweak3 generated
479 mov [TW + 8*5], twtemph
480 xor ghash_poly_8b_temp, ghash_poly_8b_temp
485 vmovdqa %%T0, [keys + 16*5]
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)
508 cmovc ghash_poly_8b_temp, ghash_poly_8b
509 xor twtempl, ghash_poly_8b_temp
510 mov [TW + 8*6], twtempl ; next Tweak4 generated
511 mov [TW + 8*7], twtemph
515 vmovdqa %%T0, [keys + 16*6]
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)
537 xor ghash_poly_8b_temp, ghash_poly_8b_temp
540 cmovc ghash_poly_8b_temp, ghash_poly_8b
541 xor twtempl, ghash_poly_8b_temp
542 mov [TW + 8*8], twtempl ; next Tweak5 generated
543 mov [TW + 8*9], twtemph
547 vmovdqa %%T0, [keys + 16*7]
549 %if (%%num_blocks>=2)
552 %if (%%num_blocks>=3)
555 %if (%%num_blocks>=4)
558 %if (%%num_blocks>=5)
561 %if (%%num_blocks>=6)
564 %if (%%num_blocks>=7)
569 xor ghash_poly_8b_temp, ghash_poly_8b_temp
572 cmovc ghash_poly_8b_temp, ghash_poly_8b
573 xor twtempl, ghash_poly_8b_temp
574 mov [TW + 8*10], twtempl ; next Tweak6 generated
575 mov [TW + 8*11], twtemph
578 vmovdqa %%T0, [keys + 16*8]
580 %if (%%num_blocks>=2)
583 %if (%%num_blocks>=3)
586 %if (%%num_blocks>=4)
589 %if (%%num_blocks>=5)
592 %if (%%num_blocks>=6)
595 %if (%%num_blocks>=7)
600 xor ghash_poly_8b_temp, ghash_poly_8b_temp
603 cmovc ghash_poly_8b_temp, ghash_poly_8b
604 xor twtempl, ghash_poly_8b_temp
605 mov [TW + 8*12], twtempl ; next Tweak7 generated
606 mov [TW + 8*13], twtemph
609 vmovdqa %%T0, [keys + 16*9]
611 %if (%%num_blocks>=2)
614 %if (%%num_blocks>=3)
617 %if (%%num_blocks>=4)
620 %if (%%num_blocks>=5)
623 %if (%%num_blocks>=6)
626 %if (%%num_blocks>=7)
631 xor ghash_poly_8b_temp, ghash_poly_8b_temp
634 cmovc ghash_poly_8b_temp, ghash_poly_8b
635 xor twtempl, ghash_poly_8b_temp
636 mov [TW + 8*14], twtempl ; next Tweak8 generated
637 mov [TW + 8*15], twtemph
642 vmovdqa %%T0, [keys + 16*10]
643 vaesdeclast %%ST1, %%T0
644 %if (%%num_blocks>=2)
645 vaesdeclast %%ST2, %%T0
647 %if (%%num_blocks>=3)
648 vaesdeclast %%ST3, %%T0
650 %if (%%num_blocks>=4)
651 vaesdeclast %%ST4, %%T0
653 %if (%%num_blocks>=5)
654 vaesdeclast %%ST5, %%T0
656 %if (%%num_blocks>=6)
657 vaesdeclast %%ST6, %%T0
659 %if (%%num_blocks>=7)
660 vaesdeclast %%ST7, %%T0
665 %if (%%num_blocks>=2)
668 %if (%%num_blocks>=3)
671 %if (%%num_blocks>=4)
674 %if (%%num_blocks>=5)
677 %if (%%num_blocks>=6)
680 %if (%%num_blocks>=7)
686 ; load next Tweak values
687 vmovdqa %%TW1, [TW + 16*0]
688 vmovdqa %%TW2, [TW + 16*1]
689 vmovdqa %%TW3, [TW + 16*2]
690 vmovdqa %%TW4, [TW + 16*3]
691 vmovdqa %%TW5, [TW + 16*4]
692 vmovdqa %%TW6, [TW + 16*5]
693 vmovdqa %%TW7, [TW + 16*6]
700 ; Encrypt 8 blocks in parallel
701 ; generate next 8 tweak values
702 %macro encrypt_by_eight 18
703 %define %%ST1 %1 ; state 1
704 %define %%ST2 %2 ; state 2
705 %define %%ST3 %3 ; state 3
706 %define %%ST4 %4 ; state 4
707 %define %%ST5 %5 ; state 5
708 %define %%ST6 %6 ; state 6
709 %define %%ST7 %7 ; state 7
710 %define %%ST8 %8 ; state 8
711 %define %%TW1 %9 ; tweak 1
712 %define %%TW2 %10 ; tweak 2
713 %define %%TW3 %11 ; tweak 3
714 %define %%TW4 %12 ; tweak 4
715 %define %%TW5 %13 ; tweak 5
716 %define %%TW6 %14 ; tweak 6
717 %define %%TW7 %15 ; tweak 7
718 %define %%TW8 %16 ; tweak 8
719 %define %%T0 %17 ; Temp register
720 %define %%last_eight %18
743 %if (0 == %%last_eight)
744 xor ghash_poly_8b_temp, ghash_poly_8b_temp
747 cmovc ghash_poly_8b_temp, ghash_poly_8b
750 vmovdqa %%T0, [keys + 16*1]
759 %if (0 == %%last_eight)
760 xor twtempl, ghash_poly_8b_temp
761 mov [TW + 8*0], twtempl
762 mov [TW + 8*1], twtemph
763 xor ghash_poly_8b_temp, ghash_poly_8b_temp
766 vmovdqa %%T0, [keys + 16*2]
775 %if (0 == %%last_eight)
778 cmovc ghash_poly_8b_temp, ghash_poly_8b
779 xor twtempl, ghash_poly_8b_temp
783 vmovdqa %%T0, [keys + 16*3]
792 %if (0 == %%last_eight)
793 mov [TW + 8*2], twtempl
794 mov [TW + 8*3], twtemph
795 xor ghash_poly_8b_temp, ghash_poly_8b_temp
799 vmovdqa %%T0, [keys + 16*4]
808 %if (0 == %%last_eight)
810 cmovc ghash_poly_8b_temp, ghash_poly_8b
811 xor twtempl, ghash_poly_8b_temp
812 mov [TW + 8*4], twtempl
815 vmovdqa %%T0, [keys + 16*5]
824 %if (0 == %%last_eight)
825 mov [TW + 8*5], twtemph
826 xor ghash_poly_8b_temp, ghash_poly_8b_temp
831 vmovdqa %%T0, [keys + 16*6]
840 %if (0 == %%last_eight)
841 cmovc ghash_poly_8b_temp, ghash_poly_8b
842 xor twtempl, ghash_poly_8b_temp
843 mov [TW + 8*6], twtempl
844 mov [TW + 8*7], twtemph
847 vmovdqa %%T0, [keys + 16*7]
856 %if (0 == %%last_eight)
857 xor ghash_poly_8b_temp, ghash_poly_8b_temp
860 cmovc ghash_poly_8b_temp, ghash_poly_8b
863 vmovdqa %%T0, [keys + 16*8]
872 %if (0 == %%last_eight)
873 xor twtempl, ghash_poly_8b_temp
874 mov [TW + 8*8], twtempl
875 mov [TW + 8*9], twtemph
876 xor ghash_poly_8b_temp, ghash_poly_8b_temp
879 vmovdqa %%T0, [keys + 16*9]
888 %if (0 == %%last_eight)
891 cmovc ghash_poly_8b_temp, ghash_poly_8b
892 xor twtempl, ghash_poly_8b_temp
895 %if (0 == %%last_eight)
896 mov [TW + 8*10], twtempl
897 mov [TW + 8*11], twtemph
898 xor ghash_poly_8b_temp, ghash_poly_8b_temp
902 %if (0 == %%last_eight)
904 cmovc ghash_poly_8b_temp, ghash_poly_8b
905 xor twtempl, ghash_poly_8b_temp
906 mov [TW + 8*12], twtempl
909 %if (0 == %%last_eight)
910 mov [TW + 8*13], twtemph
911 xor ghash_poly_8b_temp, ghash_poly_8b_temp
916 %if (0 == %%last_eight)
917 cmovc ghash_poly_8b_temp, ghash_poly_8b
918 xor twtempl, ghash_poly_8b_temp
919 ; mov [TW + 8*14], twtempl
920 ; mov [TW + 8*15], twtemph
923 vmovdqa %%T0, [keys + 16*10]
924 vaesdeclast %%ST1, %%T0
925 vaesdeclast %%ST2, %%T0
926 vaesdeclast %%ST3, %%T0
927 vaesdeclast %%ST4, %%T0
928 vaesdeclast %%ST5, %%T0
929 vaesdeclast %%ST6, %%T0
930 vaesdeclast %%ST7, %%T0
931 vaesdeclast %%ST8, %%T0
943 mov [TW + 8*14], twtempl
944 mov [TW + 8*15], twtemph
945 ; load next Tweak values
946 vmovdqa %%TW1, [TW + 16*0]
947 vmovdqa %%TW2, [TW + 16*1]
948 vmovdqa %%TW3, [TW + 16*2]
949 vmovdqa %%TW4, [TW + 16*3]
950 vmovdqa %%TW5, [TW + 16*4]
951 vmovdqa %%TW6, [TW + 16*5]
952 vmovdqa %%TW7, [TW + 16*6]
959 global XTS_AES_128_dec_expanded_key_avx:function
960 XTS_AES_128_dec_expanded_key_avx:
962 sub rsp, VARIABLE_OFFSET
964 mov [_gpr + 8*0], rbx
965 %ifidn __OUTPUT_FORMAT__, win64
966 mov [_gpr + 8*1], rdi
967 mov [_gpr + 8*2], rsi
969 vmovdqa [_xmm + 16*0], xmm6
970 vmovdqa [_xmm + 16*1], xmm7
971 vmovdqa [_xmm + 16*2], xmm8
972 vmovdqa [_xmm + 16*3], xmm9
973 vmovdqa [_xmm + 16*4], xmm10
974 vmovdqa [_xmm + 16*5], xmm11
975 vmovdqa [_xmm + 16*6], xmm12
976 vmovdqa [_xmm + 16*7], xmm13
977 vmovdqa [_xmm + 16*8], xmm14
978 vmovdqa [_xmm + 16*9], xmm15
981 mov ghash_poly_8b, GHASH_POLY ; load 0x87 to ghash_poly_8b
984 vmovdqu xmm1, [T_val] ; read initial Tweak value
985 vpxor xmm4, xmm4 ; for key expansion
986 encrypt_T xmm0, xmm1, xmm2, xmm3, xmm4, ptr_key2, ptr_key1, keys
989 %ifidn __OUTPUT_FORMAT__, win64
990 mov ptr_plaintext, [rsp + VARIABLE_OFFSET + 8*5] ; plaintext pointer
991 mov ptr_ciphertext, [rsp + VARIABLE_OFFSET + 8*6] ; ciphertext pointer
996 mov target_ptr_val, N_val
997 and target_ptr_val, -16 ; target_ptr_val = target_ptr_val - (target_ptr_val mod 16)
998 sub target_ptr_val, 128 ; adjust target_ptr_val because last 4 blocks will not be stitched with Tweak calculations
999 jl _less_than_128_bytes
1001 add target_ptr_val, ptr_ciphertext
1006 jz _initial_num_blocks_is_0
1009 je _initial_num_blocks_is_4
1014 je _initial_num_blocks_is_6
1017 je _initial_num_blocks_is_5
1022 je _initial_num_blocks_is_3
1025 je _initial_num_blocks_is_2
1028 je _initial_num_blocks_is_1
1030 _initial_num_blocks_is_7:
1031 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 7
1032 add ptr_plaintext, 16*7
1033 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 0
1035 vmovdqu [ptr_ciphertext+16*0], xmm1
1036 vmovdqu [ptr_ciphertext+16*1], xmm2
1037 vmovdqu [ptr_ciphertext+16*2], xmm3
1038 vmovdqu [ptr_ciphertext+16*3], xmm4
1039 vmovdqu [ptr_ciphertext+16*4], xmm5
1040 vmovdqu [ptr_ciphertext+16*5], xmm6
1041 vmovdqu [ptr_ciphertext+16*6], xmm7
1042 add ptr_ciphertext, 16*7
1044 cmp ptr_ciphertext, target_ptr_val
1048 _initial_num_blocks_is_6:
1049 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 6
1050 add ptr_plaintext, 16*6
1051 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 0
1053 vmovdqu [ptr_ciphertext+16*0], xmm1
1054 vmovdqu [ptr_ciphertext+16*1], xmm2
1055 vmovdqu [ptr_ciphertext+16*2], xmm3
1056 vmovdqu [ptr_ciphertext+16*3], xmm4
1057 vmovdqu [ptr_ciphertext+16*4], xmm5
1058 vmovdqu [ptr_ciphertext+16*5], xmm6
1059 add ptr_ciphertext, 16*6
1061 cmp ptr_ciphertext, target_ptr_val
1065 _initial_num_blocks_is_5:
1066 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 5
1067 add ptr_plaintext, 16*5
1068 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 0
1070 vmovdqu [ptr_ciphertext+16*0], xmm1
1071 vmovdqu [ptr_ciphertext+16*1], xmm2
1072 vmovdqu [ptr_ciphertext+16*2], xmm3
1073 vmovdqu [ptr_ciphertext+16*3], xmm4
1074 vmovdqu [ptr_ciphertext+16*4], xmm5
1075 add ptr_ciphertext, 16*5
1077 cmp ptr_ciphertext, target_ptr_val
1081 _initial_num_blocks_is_4:
1082 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 4
1083 add ptr_plaintext, 16*4
1084 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 0
1086 vmovdqu [ptr_ciphertext+16*0], xmm1
1087 vmovdqu [ptr_ciphertext+16*1], xmm2
1088 vmovdqu [ptr_ciphertext+16*2], xmm3
1089 vmovdqu [ptr_ciphertext+16*3], xmm4
1090 add ptr_ciphertext, 16*4
1092 cmp ptr_ciphertext, target_ptr_val
1098 _initial_num_blocks_is_3:
1099 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 3
1100 add ptr_plaintext, 16*3
1101 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 0
1103 vmovdqu [ptr_ciphertext+16*0], xmm1
1104 vmovdqu [ptr_ciphertext+16*1], xmm2
1105 vmovdqu [ptr_ciphertext+16*2], xmm3
1106 add ptr_ciphertext, 16*3
1108 cmp ptr_ciphertext, target_ptr_val
1112 _initial_num_blocks_is_2:
1113 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 2
1114 add ptr_plaintext, 16*2
1115 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 0
1117 vmovdqu [ptr_ciphertext], xmm1
1118 vmovdqu [ptr_ciphertext+16], xmm2
1119 add ptr_ciphertext, 16*2
1121 cmp ptr_ciphertext, target_ptr_val
1126 _initial_num_blocks_is_1:
1127 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 1
1128 add ptr_plaintext, 16*1
1129 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 0
1131 vmovdqu [ptr_ciphertext], xmm1
1132 add ptr_ciphertext, 16
1134 cmp ptr_ciphertext, target_ptr_val
1139 _initial_num_blocks_is_0:
1140 mov twtempl, [TW+8*0]
1141 mov twtemph, [TW+8*1]
1142 vmovdqa xmm9, [TW+16*0]
1144 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1146 adc twtemph, twtemph
1147 cmovc ghash_poly_8b_temp, ghash_poly_8b
1148 xor twtempl, ghash_poly_8b_temp
1149 mov [TW+8*2], twtempl
1150 mov [TW+8*3], twtemph
1151 vmovdqa xmm10, [TW+16*1]
1153 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1155 adc twtemph, twtemph
1156 cmovc ghash_poly_8b_temp, ghash_poly_8b
1157 xor twtempl, ghash_poly_8b_temp
1158 mov [TW+8*4], twtempl
1159 mov [TW+8*5], twtemph
1160 vmovdqa xmm11, [TW+16*2]
1163 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1165 adc twtemph, twtemph
1166 cmovc ghash_poly_8b_temp, ghash_poly_8b
1167 xor twtempl, ghash_poly_8b_temp
1168 mov [TW+8*6], twtempl
1169 mov [TW+8*7], twtemph
1170 vmovdqa xmm12, [TW+16*3]
1173 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1175 adc twtemph, twtemph
1176 cmovc ghash_poly_8b_temp, ghash_poly_8b
1177 xor twtempl, ghash_poly_8b_temp
1178 mov [TW+8*8], twtempl
1179 mov [TW+8*9], twtemph
1180 vmovdqa xmm13, [TW+16*4]
1182 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1184 adc twtemph, twtemph
1185 cmovc ghash_poly_8b_temp, ghash_poly_8b
1186 xor twtempl, ghash_poly_8b_temp
1187 mov [TW+8*10], twtempl
1188 mov [TW+8*11], twtemph
1189 vmovdqa xmm14, [TW+16*5]
1191 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1193 adc twtemph, twtemph
1194 cmovc ghash_poly_8b_temp, ghash_poly_8b
1195 xor twtempl, ghash_poly_8b_temp
1196 mov [TW+8*12], twtempl
1197 mov [TW+8*13], twtemph
1198 vmovdqa xmm15, [TW+16*6]
1200 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1202 adc twtemph, twtemph
1203 cmovc ghash_poly_8b_temp, ghash_poly_8b
1204 xor twtempl, ghash_poly_8b_temp
1205 mov [TW+8*14], twtempl
1206 mov [TW+8*15], twtemph
1207 ;vmovdqa xmm16, [TW+16*7]
1209 cmp ptr_ciphertext, target_ptr_val
1213 vmovdqu xmm1, [ptr_plaintext+16*0]
1214 vmovdqu xmm2, [ptr_plaintext+16*1]
1215 vmovdqu xmm3, [ptr_plaintext+16*2]
1216 vmovdqu xmm4, [ptr_plaintext+16*3]
1217 vmovdqu xmm5, [ptr_plaintext+16*4]
1218 vmovdqu xmm6, [ptr_plaintext+16*5]
1219 vmovdqu xmm7, [ptr_plaintext+16*6]
1220 vmovdqu xmm8, [ptr_plaintext+16*7]
1222 add ptr_plaintext, 128
1224 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 0
1227 vmovdqu [ptr_ciphertext+16*0], xmm1
1228 vmovdqu [ptr_ciphertext+16*1], xmm2
1229 vmovdqu [ptr_ciphertext+16*2], xmm3
1230 vmovdqu [ptr_ciphertext+16*3], xmm4
1231 vmovdqu [ptr_ciphertext+16*4], xmm5
1232 vmovdqu [ptr_ciphertext+16*5], xmm6
1233 vmovdqu [ptr_ciphertext+16*6], xmm7
1234 vmovdqu [ptr_ciphertext+16*7], xmm8
1235 add ptr_ciphertext, 128
1237 cmp ptr_ciphertext, target_ptr_val
1242 and N_val, 15 ; N_val = N_val mod 16
1245 ; generate next Tweak value
1246 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1248 adc twtemph, twtemph
1249 cmovc ghash_poly_8b_temp, ghash_poly_8b
1250 xor twtempl, ghash_poly_8b_temp
1251 vmovdqa xmm1, [TW + 16*7]
1252 vmovdqa [TW + 16*0], xmm1 ; swap tweak values for cipher stealing for decrypt
1254 mov [TW + 16*7], twtempl
1255 mov [TW + 16*7+8], twtemph
1258 vmovdqu xmm1, [ptr_plaintext+16*0]
1259 vmovdqu xmm2, [ptr_plaintext+16*1]
1260 vmovdqu xmm3, [ptr_plaintext+16*2]
1261 vmovdqu xmm4, [ptr_plaintext+16*3]
1262 vmovdqu xmm5, [ptr_plaintext+16*4]
1263 vmovdqu xmm6, [ptr_plaintext+16*5]
1264 vmovdqu xmm7, [ptr_plaintext+16*6]
1265 vmovdqu xmm8, [ptr_plaintext+16*7]
1266 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 1
1269 vmovdqu [ptr_ciphertext+16*0], xmm1
1270 vmovdqu [ptr_ciphertext+16*1], xmm2
1271 vmovdqu [ptr_ciphertext+16*2], xmm3
1272 vmovdqu [ptr_ciphertext+16*3], xmm4
1273 vmovdqu [ptr_ciphertext+16*4], xmm5
1274 vmovdqu [ptr_ciphertext+16*5], xmm6
1275 vmovdqu [ptr_ciphertext+16*6], xmm7
1281 vmovdqu xmm1, [ptr_plaintext+16*0]
1282 vmovdqu xmm2, [ptr_plaintext+16*1]
1283 vmovdqu xmm3, [ptr_plaintext+16*2]
1284 vmovdqu xmm4, [ptr_plaintext+16*3]
1285 vmovdqu xmm5, [ptr_plaintext+16*4]
1286 vmovdqu xmm6, [ptr_plaintext+16*5]
1287 vmovdqu xmm7, [ptr_plaintext+16*6]
1288 vmovdqu xmm8, [ptr_plaintext+16*7]
1289 encrypt_by_eight xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, [TW+16*7], xmm0, 1
1292 vmovdqu [ptr_ciphertext+16*0], xmm1
1293 vmovdqu [ptr_ciphertext+16*1], xmm2
1294 vmovdqu [ptr_ciphertext+16*2], xmm3
1295 vmovdqu [ptr_ciphertext+16*3], xmm4
1296 vmovdqu [ptr_ciphertext+16*4], xmm5
1297 vmovdqu [ptr_ciphertext+16*5], xmm6
1298 vmovdqu [ptr_ciphertext+16*6], xmm7
1304 ; start cipher stealing
1308 ; shift xmm8 to the left by 16-N_val bytes
1309 lea twtempl, [vpshufb_shf_table]
1310 vmovdqu xmm0, [twtempl+N_val]
1314 vmovdqu xmm3, [ptr_plaintext + 112 + N_val] ; state register is temporarily xmm3 to eliminate a move
1315 vmovdqu [ptr_ciphertext + 112 + N_val], xmm8
1317 ; shift xmm3 to the right by 16-N_val bytes
1318 lea twtempl, [vpshufb_shf_table +16]
1320 vmovdqu xmm0, [twtempl]
1324 vpblendvb xmm3, xmm3, xmm2, xmm0 ;xmm0 is implicit
1328 vpxor xmm8, xmm3 ; state register is xmm8, instead of a move from xmm3 to xmm8, destination register of vpxor instruction is swapped
1331 ;encrypt last block with cipher stealing
1332 vpxor xmm8, [keys] ; ARK
1333 vaesdec xmm8, [keys + 16*1] ; round 1
1334 vaesdec xmm8, [keys + 16*2] ; round 2
1335 vaesdec xmm8, [keys + 16*3] ; round 3
1336 vaesdec xmm8, [keys + 16*4] ; round 4
1337 vaesdec xmm8, [keys + 16*5] ; round 5
1338 vaesdec xmm8, [keys + 16*6] ; round 6
1339 vaesdec xmm8, [keys + 16*7] ; round 7
1340 vaesdec xmm8, [keys + 16*8] ; round 8
1341 vaesdec xmm8, [keys + 16*9] ; round 9
1342 vaesdeclast xmm8, [keys + 16*10] ; round 10
1348 ; store last ciphertext value
1349 vmovdqu [ptr_ciphertext+16*7], xmm8
1353 mov rbx, [_gpr + 8*0]
1354 %ifidn __OUTPUT_FORMAT__, win64
1355 mov rdi, [_gpr + 8*1]
1356 mov rsi, [_gpr + 8*2]
1359 vmovdqa xmm6, [_xmm + 16*0]
1360 vmovdqa xmm7, [_xmm + 16*1]
1361 vmovdqa xmm8, [_xmm + 16*2]
1362 vmovdqa xmm9, [_xmm + 16*3]
1363 vmovdqa xmm10, [_xmm + 16*4]
1364 vmovdqa xmm11, [_xmm + 16*5]
1365 vmovdqa xmm12, [_xmm + 16*6]
1366 vmovdqa xmm13, [_xmm + 16*7]
1367 vmovdqa xmm14, [_xmm + 16*8]
1368 vmovdqa xmm15, [_xmm + 16*9]
1371 add rsp, VARIABLE_OFFSET
1379 _less_than_128_bytes:
1402 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 7
1404 sub ptr_plaintext, 16*1
1406 and N_val, 15 ; N_val = N_val mod 16
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*2], twtempl
1416 mov [TW+8*3], twtemph
1418 vmovdqa [TW + 16*0] , xmm15
1419 vmovdqa xmm15, [TW+16*1]
1421 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 1
1423 vmovdqu [ptr_ciphertext+16*0], xmm1
1424 vmovdqu [ptr_ciphertext+16*1], xmm2
1425 vmovdqu [ptr_ciphertext+16*2], xmm3
1426 vmovdqu [ptr_ciphertext+16*3], xmm4
1427 vmovdqu [ptr_ciphertext+16*4], xmm5
1428 vmovdqu [ptr_ciphertext+16*5], xmm6
1430 sub ptr_ciphertext, 16*1
1435 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 7, 1
1437 vmovdqu [ptr_ciphertext+16*0], xmm1
1438 vmovdqu [ptr_ciphertext+16*1], xmm2
1439 vmovdqu [ptr_ciphertext+16*2], xmm3
1440 vmovdqu [ptr_ciphertext+16*3], xmm4
1441 vmovdqu [ptr_ciphertext+16*4], xmm5
1442 vmovdqu [ptr_ciphertext+16*5], xmm6
1444 sub ptr_ciphertext, 16*1
1454 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 6
1456 sub ptr_plaintext, 16*2
1458 and N_val, 15 ; N_val = N_val mod 16
1462 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1464 adc twtemph, twtemph
1465 cmovc ghash_poly_8b_temp, ghash_poly_8b
1466 xor twtempl, ghash_poly_8b_temp
1467 mov [TW+8*2], twtempl
1468 mov [TW+8*3], twtemph
1470 vmovdqa [TW + 16*0] , xmm14
1471 vmovdqa xmm14, [TW+16*1]
1473 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 1
1475 vmovdqu [ptr_ciphertext+16*0], xmm1
1476 vmovdqu [ptr_ciphertext+16*1], xmm2
1477 vmovdqu [ptr_ciphertext+16*2], xmm3
1478 vmovdqu [ptr_ciphertext+16*3], xmm4
1479 vmovdqu [ptr_ciphertext+16*4], xmm5
1481 sub ptr_ciphertext, 16*2
1486 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 6, 1
1488 vmovdqu [ptr_ciphertext+16*0], xmm1
1489 vmovdqu [ptr_ciphertext+16*1], xmm2
1490 vmovdqu [ptr_ciphertext+16*2], xmm3
1491 vmovdqu [ptr_ciphertext+16*3], xmm4
1492 vmovdqu [ptr_ciphertext+16*4], xmm5
1494 sub ptr_ciphertext, 16*2
1503 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 5
1505 sub ptr_plaintext, 16*3
1507 and N_val, 15 ; N_val = N_val mod 16
1511 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1513 adc twtemph, twtemph
1514 cmovc ghash_poly_8b_temp, ghash_poly_8b
1515 xor twtempl, ghash_poly_8b_temp
1516 mov [TW+8*2], twtempl
1517 mov [TW+8*3], twtemph
1519 vmovdqa [TW + 16*0] , xmm13
1520 vmovdqa xmm13, [TW+16*1]
1522 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 1
1524 vmovdqu [ptr_ciphertext+16*0], xmm1
1525 vmovdqu [ptr_ciphertext+16*1], xmm2
1526 vmovdqu [ptr_ciphertext+16*2], xmm3
1527 vmovdqu [ptr_ciphertext+16*3], xmm4
1529 sub ptr_ciphertext, 16*3
1534 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 5, 1
1536 vmovdqu [ptr_ciphertext+16*0], xmm1
1537 vmovdqu [ptr_ciphertext+16*1], xmm2
1538 vmovdqu [ptr_ciphertext+16*2], xmm3
1539 vmovdqu [ptr_ciphertext+16*3], xmm4
1541 sub ptr_ciphertext, 16*3
1550 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 4
1552 sub ptr_plaintext, 16*4
1554 and N_val, 15 ; N_val = N_val mod 16
1558 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1560 adc twtemph, twtemph
1561 cmovc ghash_poly_8b_temp, ghash_poly_8b
1562 xor twtempl, ghash_poly_8b_temp
1563 mov [TW+8*2], twtempl
1564 mov [TW+8*3], twtemph
1566 vmovdqa [TW + 16*0] , xmm12
1567 vmovdqa xmm12, [TW+16*1]
1569 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 1
1571 vmovdqu [ptr_ciphertext+16*0], xmm1
1572 vmovdqu [ptr_ciphertext+16*1], xmm2
1573 vmovdqu [ptr_ciphertext+16*2], xmm3
1575 sub ptr_ciphertext, 16*4
1580 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 4, 1
1582 vmovdqu [ptr_ciphertext+16*0], xmm1
1583 vmovdqu [ptr_ciphertext+16*1], xmm2
1584 vmovdqu [ptr_ciphertext+16*2], xmm3
1586 sub ptr_ciphertext, 16*4
1594 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 3
1596 sub ptr_plaintext, 16*5
1598 and N_val, 15 ; N_val = N_val mod 16
1602 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1604 adc twtemph, twtemph
1605 cmovc ghash_poly_8b_temp, ghash_poly_8b
1606 xor twtempl, ghash_poly_8b_temp
1607 mov [TW+8*2], twtempl
1608 mov [TW+8*3], twtemph
1610 vmovdqa [TW + 16*0] , xmm11
1611 vmovdqa xmm11, [TW+16*1]
1613 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 1
1615 vmovdqu [ptr_ciphertext+16*0], xmm1
1616 vmovdqu [ptr_ciphertext+16*1], xmm2
1618 sub ptr_ciphertext, 16*5
1623 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 3, 1
1625 vmovdqu [ptr_ciphertext+16*0], xmm1
1626 vmovdqu [ptr_ciphertext+16*1], xmm2
1628 sub ptr_ciphertext, 16*5
1638 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 2
1640 sub ptr_plaintext, 16*6
1642 and N_val, 15 ; N_val = N_val mod 16
1646 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1648 adc twtemph, twtemph
1649 cmovc ghash_poly_8b_temp, ghash_poly_8b
1650 xor twtempl, ghash_poly_8b_temp
1651 mov [TW+8*2], twtempl
1652 mov [TW+8*3], twtemph
1654 vmovdqa [TW + 16*0] , xmm10
1655 vmovdqa xmm10, [TW+16*1]
1657 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 1
1659 vmovdqu [ptr_ciphertext], xmm1
1661 sub ptr_ciphertext, 16*6
1666 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 2, 1
1668 vmovdqu [ptr_ciphertext], xmm1
1670 sub ptr_ciphertext, 16*6
1687 initialize xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 1
1689 sub ptr_plaintext, 16*7
1691 and N_val, 15 ; N_val = N_val mod 16
1695 xor ghash_poly_8b_temp, ghash_poly_8b_temp
1697 adc twtemph, twtemph
1698 cmovc ghash_poly_8b_temp, ghash_poly_8b
1699 xor twtempl, ghash_poly_8b_temp
1700 mov [TW+8*2], twtempl
1701 mov [TW+8*3], twtemph
1703 vmovdqa [TW + 16*0] , xmm9
1704 vmovdqa xmm9, [TW+16*1]
1706 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 1
1709 sub ptr_ciphertext, 16*7
1714 encrypt_initial xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, xmm0, 1, 1
1717 sub ptr_ciphertext, 16*7
1725 ; use these values for shift constants for the vpshufb instruction
1726 ; different alignments result in values as shown:
1727 ; dq 0x8887868584838281, 0x008f8e8d8c8b8a89 ; shl 15 (16-1) / shr1
1728 ; dq 0x8988878685848382, 0x01008f8e8d8c8b8a ; shl 14 (16-3) / shr2
1729 ; dq 0x8a89888786858483, 0x0201008f8e8d8c8b ; shl 13 (16-4) / shr3
1730 ; dq 0x8b8a898887868584, 0x030201008f8e8d8c ; shl 12 (16-4) / shr4
1731 ; dq 0x8c8b8a8988878685, 0x04030201008f8e8d ; shl 11 (16-5) / shr5
1732 ; dq 0x8d8c8b8a89888786, 0x0504030201008f8e ; shl 10 (16-6) / shr6
1733 ; dq 0x8e8d8c8b8a898887, 0x060504030201008f ; shl 9 (16-7) / shr7
1734 ; dq 0x8f8e8d8c8b8a8988, 0x0706050403020100 ; shl 8 (16-8) / shr8
1735 ; dq 0x008f8e8d8c8b8a89, 0x0807060504030201 ; shl 7 (16-9) / shr9
1736 ; dq 0x01008f8e8d8c8b8a, 0x0908070605040302 ; shl 6 (16-10) / shr10
1737 ; dq 0x0201008f8e8d8c8b, 0x0a09080706050403 ; shl 5 (16-11) / shr11
1738 ; dq 0x030201008f8e8d8c, 0x0b0a090807060504 ; shl 4 (16-12) / shr12
1739 ; dq 0x04030201008f8e8d, 0x0c0b0a0908070605 ; shl 3 (16-13) / shr13
1740 ; dq 0x0504030201008f8e, 0x0d0c0b0a09080706 ; shl 2 (16-14) / shr14
1741 ; dq 0x060504030201008f, 0x0e0d0c0b0a090807 ; shl 1 (16-15) / shr15
1742 dq 0x8786858483828100, 0x8f8e8d8c8b8a8988
1743 dq 0x0706050403020100, 0x000e0d0c0b0a0908
1746 dq 0x8080808080808080, 0x8080808080808080