Add AES-NI GCM stitch.
[openssl.git] / crypto / modes / asm / aesni-gcm-x86_64.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9 #
10 #
11 # AES-NI-CTR+GHASH stitch.
12 #
13 # February 2013
14 #
15 # OpenSSL GCM implementation is organized in such way that its
16 # performance is rather close to the sum of its streamed components,
17 # in the context parallelized AES-NI CTR and modulo-scheduled
18 # PCLMULQDQ-enabled GHASH. Unfortunately, as no stitch implementation
19 # was observed to perform significantly better than the sum of the
20 # components on contemporary CPUs, the effort was deemed impossible to
21 # justify. This module is based on combination of Intel submissions,
22 # [1] and [2], with MOVBE twist suggested by Ilya Albrekht and Max
23 # Locktyukhin of Intel Corp. who verified that it reduces shuffles
24 # pressure with notable relative improvement on upcoming Haswell
25 # processor. [Exact performance numbers to be added at launch.]
26 #
27 # [1] http://rt.openssl.org/Ticket/Display.html?id=2900&user=guest&pass=guest
28 # [2] http://www.intel.com/content/dam/www/public/us/en/documents/software-support/enabling-high-performance-gcm.pdf
29
30 $flavour = shift;
31 $output  = shift;
32 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
33
34 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
35
36 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
37 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
38 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
39 die "can't locate x86_64-xlate.pl";
40
41 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
42                 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
43         $avx = ($1>=2.19) + ($1>=2.22);
44 }
45
46 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
47             `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
48         $avx = ($1>=2.09) + ($1>=2.10);
49 }
50
51 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
52             `ml64 2>&1` =~ /Version ([0-9]+)\./) {
53         $avx = ($1>=10) + ($1>=11);
54 }
55
56 open OUT,"| \"$^X\" $xlate $flavour $output";
57 *STDOUT=*OUT;
58
59 if ($avx>1) {{{
60
61 ($inp,$out,$len,$key,$ivp,$Xip)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
62
63 ($Ii,$T1,$T2,$Hkey,
64  $Z0,$Z1,$Z2,$Z3,$Xi) = map("%xmm$_",(0..8));
65
66 ($inout0,$inout1,$inout2,$inout3,$inout4,$inout5,$rndkey) = map("%xmm$_",(9..15));
67
68 ($counter,$rounds,$ret,$const,$in0,$end0)=("%ebx","%ebp","%r10","%r11","%r14","%r15");
69
70 $code=<<___;
71 .text
72
73 .type   _aesni_ctr32_ghash_6x,\@abi-omnipotent
74 .align  32
75 _aesni_ctr32_ghash_6x:
76         vmovdqu         0x20($const),$T2        # borrow $T2, .Lone_msb
77         sub             \$6,$len
78         vpxor           $Z0,$Z0,$Z0             # $Z0   = 0
79         vmovdqu         0x00-0x80($key),$rndkey
80         vpaddb          $T2,$T1,$inout1
81         vpaddb          $T2,$inout1,$inout2
82         vpaddb          $T2,$inout2,$inout3
83         vpaddb          $T2,$inout3,$inout4
84         vpaddb          $T2,$inout4,$inout5
85         vpxor           $rndkey,$T1,$inout0
86         vmovdqu         $Z0,16+8(%rsp)          # "$Z3" = 0
87         jmp             .Loop6x
88
89 .align  32
90 .Loop6x:
91         add             \$6<<24,$counter
92         jc              .Lhandle_ctr32          # discard $inout[1-5]?
93         vmovdqu         0x00-0x20($Xip),$Hkey   # $Hkey^1
94           vpaddb        $T2,$inout5,$T1         # next counter value
95           vpxor         $rndkey,$inout1,$inout1
96           vpxor         $rndkey,$inout2,$inout2
97
98 .Lresume_ctr32:
99         vmovdqu         $T1,($ivp)              # save next counter value
100         vpclmulqdq      \$0x10,$Hkey,$Z3,$Z1
101           vpxor         $rndkey,$inout3,$inout3
102           vmovups       0x10-0x80($key),$T2     # borrow $T2 for $rndkey
103         vpclmulqdq      \$0x01,$Hkey,$Z3,$Z2
104         xor             %r12,%r12
105         cmp             $in0,$end0
106
107           vaesenc       $T2,$inout0,$inout0
108         vmovdqu         0x30+8(%rsp),$Ii        # I[4]
109           vpxor         $rndkey,$inout4,$inout4
110         vpclmulqdq      \$0x00,$Hkey,$Z3,$T1
111           vaesenc       $T2,$inout1,$inout1
112           vpxor         $rndkey,$inout5,$inout5
113         setnc           %r12b
114         vpclmulqdq      \$0x11,$Hkey,$Z3,$Z3
115           vaesenc       $T2,$inout2,$inout2
116         vmovdqu         0x10-0x20($Xip),$Hkey   # $Hkey^2
117         neg             %r12
118           vaesenc       $T2,$inout3,$inout3
119          vpxor          $Z1,$Z2,$Z2
120         vpclmulqdq      \$0x00,$Hkey,$Ii,$Z1
121          vpxor          $Z0,$Xi,$Xi             # modulo-scheduled
122           vaesenc       $T2,$inout4,$inout4
123          vpxor          $Z1,$T1,$Z0
124         and             \$0x60,%r12
125           vmovups       0x20-0x80($key),$rndkey
126         vpclmulqdq      \$0x10,$Hkey,$Ii,$T1
127           vaesenc       $T2,$inout5,$inout5
128
129         vpclmulqdq      \$0x01,$Hkey,$Ii,$T2
130         lea             ($in0,%r12),$in0
131           vaesenc       $rndkey,$inout0,$inout0
132          vpxor          16+8(%rsp),$Xi,$Xi      # modulo-scheduled [vpxor $Z3,$Xi,$Xi]
133         vpclmulqdq      \$0x11,$Hkey,$Ii,$Hkey
134          vmovdqu        0x40+8(%rsp),$Ii        # I[3]
135           vaesenc       $rndkey,$inout1,$inout1
136         movbe           0x58($in0),%r13
137           vaesenc       $rndkey,$inout2,$inout2
138         movbe           0x50($in0),%r12
139           vaesenc       $rndkey,$inout3,$inout3
140         mov             %r13,0x20+8(%rsp)
141           vaesenc       $rndkey,$inout4,$inout4
142         mov             %r12,0x28+8(%rsp)
143         vmovdqu         0x30-0x20($Xip),$Z1     # borrow $Z1 for $Hkey^3
144           vaesenc       $rndkey,$inout5,$inout5
145
146           vmovups       0x30-0x80($key),$rndkey
147          vpxor          $T1,$Z2,$Z2
148         vpclmulqdq      \$0x00,$Z1,$Ii,$T1
149           vaesenc       $rndkey,$inout0,$inout0
150          vpxor          $T2,$Z2,$Z2
151         vpclmulqdq      \$0x10,$Z1,$Ii,$T2
152           vaesenc       $rndkey,$inout1,$inout1
153          vpxor          $Hkey,$Z3,$Z3
154         vpclmulqdq      \$0x01,$Z1,$Ii,$Hkey
155           vaesenc       $rndkey,$inout2,$inout2
156         vpclmulqdq      \$0x11,$Z1,$Ii,$Z1
157          vmovdqu        0x50+8(%rsp),$Ii        # I[2]
158           vaesenc       $rndkey,$inout3,$inout3
159           vaesenc       $rndkey,$inout4,$inout4
160          vpxor          $T1,$Z0,$Z0
161         vmovdqu         0x40-0x20($Xip),$T1     # borrow $T1 for $Hkey^4
162           vaesenc       $rndkey,$inout5,$inout5
163
164           vmovups       0x40-0x80($key),$rndkey
165          vpxor          $T2,$Z2,$Z2
166         vpclmulqdq      \$0x00,$T1,$Ii,$T2
167           vaesenc       $rndkey,$inout0,$inout0
168          vpxor          $Hkey,$Z2,$Z2
169         vpclmulqdq      \$0x10,$T1,$Ii,$Hkey
170           vaesenc       $rndkey,$inout1,$inout1
171         movbe           0x48($in0),%r13
172          vpxor          $Z1,$Z3,$Z3
173         vpclmulqdq      \$0x01,$T1,$Ii,$Z1
174           vaesenc       $rndkey,$inout2,$inout2
175         movbe           0x40($in0),%r12
176         vpclmulqdq      \$0x11,$T1,$Ii,$T1
177          vmovdqu        0x60+8(%rsp),$Ii        # I[1]
178           vaesenc       $rndkey,$inout3,$inout3
179         mov             %r13,0x30+8(%rsp)
180           vaesenc       $rndkey,$inout4,$inout4
181         mov             %r12,0x38+8(%rsp)
182          vpxor          $T2,$Z0,$Z0
183         vmovdqu         0x60-0x20($Xip),$T2     # borrow $T2 for $Hkey^5
184           vaesenc       $rndkey,$inout5,$inout5
185
186           vmovups       0x50-0x80($key),$rndkey
187          vpxor          $Hkey,$Z2,$Z2
188         vpclmulqdq      \$0x00,$T2,$Ii,$Hkey
189           vaesenc       $rndkey,$inout0,$inout0
190          vpxor          $Z1,$Z2,$Z2
191         vpclmulqdq      \$0x10,$T2,$Ii,$Z1
192           vaesenc       $rndkey,$inout1,$inout1
193         movbe           0x38($in0),%r13
194          vpxor          $T1,$Z3,$Z3
195         vpclmulqdq      \$0x01,$T2,$Ii,$T1
196          vpxor          0x70+8(%rsp),$Xi,$Xi    # accumulate I[0]
197           vaesenc       $rndkey,$inout2,$inout2
198         movbe           0x30($in0),%r12
199         vpclmulqdq      \$0x11,$T2,$Ii,$T2
200           vaesenc       $rndkey,$inout3,$inout3
201         mov             %r13,0x40+8(%rsp)
202           vaesenc       $rndkey,$inout4,$inout4
203         mov             %r12,0x48+8(%rsp)
204          vpxor          $Hkey,$Z0,$Z0
205          vmovdqu        0x70-0x20($Xip),$Hkey   # $Hkey^6
206           vaesenc       $rndkey,$inout5,$inout5
207
208           vmovups       0x60-0x80($key),$rndkey
209          vpxor          $Z1,$Z2,$Z2
210         vpclmulqdq      \$0x10,$Hkey,$Xi,$Z1
211           vaesenc       $rndkey,$inout0,$inout0
212          vpxor          $T1,$Z2,$Z2
213         vpclmulqdq      \$0x01,$Hkey,$Xi,$T1
214           vaesenc       $rndkey,$inout1,$inout1
215         movbe           0x28($in0),%r13
216          vpxor          $T2,$Z3,$Z3
217         vpclmulqdq      \$0x00,$Hkey,$Xi,$T2
218           vaesenc       $rndkey,$inout2,$inout2
219         movbe           0x20($in0),%r12
220         vpclmulqdq      \$0x11,$Hkey,$Xi,$Xi
221           vaesenc       $rndkey,$inout3,$inout3
222         mov             %r13,0x50+8(%rsp)
223           vaesenc       $rndkey,$inout4,$inout4
224         mov             %r12,0x58+8(%rsp)
225         vpxor           $Z1,$Z2,$Z2
226           vaesenc       $rndkey,$inout5,$inout5
227         vpxor           $T1,$Z2,$Z2
228
229           vmovups       0x70-0x80($key),$rndkey
230         vpslldq         \$8,$Z2,$Z1
231         vpxor           $T2,$Z0,$Z0
232         vmovdqu         0x10($const),$Hkey      # .Lpoly
233
234           vaesenc       $rndkey,$inout0,$inout0
235         vpxor           $Xi,$Z3,$Z3
236           vaesenc       $rndkey,$inout1,$inout1
237         vpxor           $Z1,$Z0,$Z0
238         movbe           0x18($in0),%r13
239           vaesenc       $rndkey,$inout2,$inout2
240         movbe           0x10($in0),%r12
241         vpalignr        \$8,$Z0,$Z0,$Ii         # 1st phase
242         vpclmulqdq      \$0x10,$Hkey,$Z0,$Z0
243         mov             %r13,0x60+8(%rsp)
244           vaesenc       $rndkey,$inout3,$inout3
245         mov             %r12,0x68+8(%rsp)
246           vaesenc       $rndkey,$inout4,$inout4
247           vmovups       0x80-0x80($key),$T1     # borrow $T1 for $rndkey
248           vaesenc       $rndkey,$inout5,$inout5
249
250           vaesenc       $T1,$inout0,$inout0
251           vmovups       0x90-0x80($key),$rndkey
252           vaesenc       $T1,$inout1,$inout1
253         vpsrldq         \$8,$Z2,$Z2
254           vaesenc       $T1,$inout2,$inout2
255         vpxor           $Z2,$Z3,$Z3
256           vaesenc       $T1,$inout3,$inout3
257         vpxor           $Ii,$Z0,$Z0
258         movbe           0x08($in0),%r13
259           vaesenc       $T1,$inout4,$inout4
260         movbe           0x00($in0),%r12
261           vaesenc       $T1,$inout5,$inout5
262           vmovups       0xa0-0x80($key),$T1
263           cmp           \$11,$rounds
264           jb            .Lenc_tail              # 128-bit key
265
266           vaesenc       $rndkey,$inout0,$inout0
267           vaesenc       $rndkey,$inout1,$inout1
268           vaesenc       $rndkey,$inout2,$inout2
269           vaesenc       $rndkey,$inout3,$inout3
270           vaesenc       $rndkey,$inout4,$inout4
271           vaesenc       $rndkey,$inout5,$inout5
272
273           vaesenc       $T1,$inout0,$inout0
274           vaesenc       $T1,$inout1,$inout1
275           vaesenc       $T1,$inout2,$inout2
276           vaesenc       $T1,$inout3,$inout3
277           vaesenc       $T1,$inout4,$inout4
278           vmovups       0xb0-0x80($key),$rndkey
279           vaesenc       $T1,$inout5,$inout5
280           vmovups       0xc0-0x80($key),$T1
281           je            .Lenc_tail              # 192-bit key
282
283           vaesenc       $rndkey,$inout0,$inout0
284           vaesenc       $rndkey,$inout1,$inout1
285           vaesenc       $rndkey,$inout2,$inout2
286           vaesenc       $rndkey,$inout3,$inout3
287           vaesenc       $rndkey,$inout4,$inout4
288           vaesenc       $rndkey,$inout5,$inout5
289
290           vaesenc       $T1,$inout0,$inout0
291           vaesenc       $T1,$inout1,$inout1
292           vaesenc       $T1,$inout2,$inout2
293           vaesenc       $T1,$inout3,$inout3
294           vaesenc       $T1,$inout4,$inout4
295           vmovups       0xd0-0x80($key),$rndkey
296           vaesenc       $T1,$inout5,$inout5
297           vmovups       0xe0-0x80($key),$T1
298           jmp           .Lenc_tail              # 256-bit key
299
300 .align  32
301 .Lhandle_ctr32:
302         vmovdqu         ($const),$Ii            # borrow $Ii for .Lbswap_mask
303           vpshufb       $Ii,$T1,$Z2             # byte-swap counter
304           vmovdqu       0x30($const),$Z1        # borrow $Z1, .Ltwo_lsb
305           vpaddd        0x40($const),$Z2,$inout1        # .Lone_lsb
306           vpaddd        $Z1,$Z2,$inout2
307         vmovdqu         0x00-0x20($Xip),$Hkey   # $Hkey^1
308           vpaddd        $Z1,$inout1,$inout3
309           vpshufb       $Ii,$inout1,$inout1
310           vpaddd        $Z1,$inout2,$inout4
311           vpshufb       $Ii,$inout2,$inout2
312           vpxor         $rndkey,$inout1,$inout1
313           vpaddd        $Z1,$inout3,$inout5
314           vpshufb       $Ii,$inout3,$inout3
315           vpxor         $rndkey,$inout2,$inout2
316           vpaddd        $Z1,$inout4,$T1         # byte-swapped next counter value
317           vpshufb       $Ii,$inout4,$inout4
318           vpshufb       $Ii,$inout5,$inout5
319           vpshufb       $Ii,$T1,$T1             # next counter value
320         jmp             .Lresume_ctr32
321
322 .align  32
323 .Lenc_tail:
324           vaesenc       $rndkey,$inout0,$inout0
325         vmovdqu         $Z3,16+8(%rsp)          # postpone vpxor $Z3,$Xi,$Xi
326         vpalignr        \$8,$Z0,$Z0,$Xi         # 2nd phase
327           vaesenc       $rndkey,$inout1,$inout1
328         vpclmulqdq      \$0x10,$Hkey,$Z0,$Z0
329           vpxor         0x00($inp),$T1,$T2
330           vaesenc       $rndkey,$inout2,$inout2
331           vpxor         0x10($inp),$T1,$Ii
332           vaesenc       $rndkey,$inout3,$inout3
333           vpxor         0x20($inp),$T1,$Z1
334           vaesenc       $rndkey,$inout4,$inout4
335           vpxor         0x30($inp),$T1,$Z2
336           vaesenc       $rndkey,$inout5,$inout5
337           vpxor         0x40($inp),$T1,$Z3
338           vpxor         0x50($inp),$T1,$Hkey
339           vmovdqu       ($ivp),$T1              # load next counter value
340
341           vaesenclast   $T2,$inout0,$inout0
342           vmovdqu       0x20($const),$T2        # borrow $T2, .Lone_msb
343           vaesenclast   $Ii,$inout1,$inout1
344          vpaddb         $T2,$T1,$Ii
345         mov             %r13,0x70+8(%rsp)
346         lea             0x60($inp),$inp
347           vaesenclast   $Z1,$inout2,$inout2
348          vpaddb         $T2,$Ii,$Z1
349         mov             %r12,0x78+8(%rsp)
350         lea             0x60($out),$out
351           vmovdqu       0x00-0x80($key),$rndkey
352           vaesenclast   $Z2,$inout3,$inout3
353          vpaddb         $T2,$Z1,$Z2
354           vaesenclast   $Z3, $inout4,$inout4
355          vpaddb         $T2,$Z2,$Z3
356           vaesenclast   $Hkey,$inout5,$inout5
357          vpaddb         $T2,$Z3,$Hkey
358
359         add             \$0x60,$ret
360         sub             \$0x6,$len
361         jc              .L6x_done
362
363           vmovups       $inout0,-0x60($out)     # save output
364          vpxor          $rndkey,$T1,$inout0
365           vmovups       $inout1,-0x50($out)
366          vmovdqa        $Ii,$inout1             # 0 latency
367           vmovups       $inout2,-0x40($out)
368          vmovdqa        $Z1,$inout2             # 0 latency
369           vmovups       $inout3,-0x30($out)
370          vmovdqa        $Z2,$inout3             # 0 latency
371           vmovups       $inout4,-0x20($out)
372          vmovdqa        $Z3,$inout4             # 0 latency
373           vmovups       $inout5,-0x10($out)
374          vmovdqa        $Hkey,$inout5           # 0 latency
375         vmovdqu         0x20+8(%rsp),$Z3        # I[5]
376         jmp             .Loop6x
377
378 .L6x_done:
379         vpxor           16+8(%rsp),$Xi,$Xi      # modulo-scheduled
380         vpxor           $Z0,$Xi,$Xi             # modulo-scheduled
381
382         ret
383 .size   _aesni_ctr32_ghash_6x,.-_aesni_ctr32_ghash_6x
384 ___
385 ######################################################################
386 #
387 # size_t aesni_gcm_[en|de]crypt(const void *inp, void *out, size_t len,
388 #               const AES_KEY *key, unsigned char iv[16],
389 #               struct { u128 Xi,H,Htbl[9]; } *Xip);
390 $code.=<<___;
391 .globl  aesni_gcm_decrypt
392 .type   aesni_gcm_decrypt,\@function,6
393 .align  32
394 aesni_gcm_decrypt:
395         xor     $ret,$ret
396         cmp     \$0x60,$len                     # minimal accepted length
397         jb      .Lgcm_dec_abort
398
399         lea     (%rsp),%rax                     # save stack pointer
400         push    %rbx
401         push    %rbp
402         push    %r12
403         push    %r13
404         push    %r14
405         push    %r15
406 ___
407 $code.=<<___ if ($win64);
408         lea     -0xa8(%rsp),%rsp
409         movaps  %xmm6,-0xd8(%rax)
410         movaps  %xmm7,-0xc8(%rax)
411         movaps  %xmm8,-0xb8(%rax)
412         movaps  %xmm9,-0xa8(%rax)
413         movaps  %xmm10,-0x98(%rax)
414         movaps  %xmm11,-0x88(%rax)
415         movaps  %xmm12,-0x78(%rax)
416         movaps  %xmm13,-0x68(%rax)
417         movaps  %xmm14,-0x58(%rax)
418         movaps  %xmm15,-0x48(%rax)
419 .Lgcm_dec_body:
420 ___
421 $code.=<<___;
422         vzeroupper
423
424         vmovdqu         ($ivp),$T1              # input counter value
425         sub             \$128,%rsp
426         mov             12($ivp),$counter
427         lea             .Lbswap_mask(%rip),$const
428         vmovdqu         ($Xip),$Xi              # load Xi
429         and             \$-64,%rsp              # ensure stack alignment
430         vmovdqu         ($const),$Ii            # borrow $Ii for .Lbswap_mask
431         lea             0x80($key),$key         # size optimization
432         lea             0x20+0x20($Xip),$Xip    # size optimization
433         mov             0xf0-0x80($key),$rounds
434         vpshufb         $Ii,$Xi,$Xi
435
436         vmovdqu         0x50($inp),$Z3          # I[5]
437         lea             ($inp),$in0
438         vmovdqu         0x40($inp),$Z0
439         lea             -0xc0($inp,$len),$end0
440         vmovdqu         0x30($inp),$Z1
441         shr             \$4,$len
442         xor             $ret,$ret
443         vmovdqu         0x20($inp),$Z2
444          vpshufb        $Ii,$Z3,$Z3             # passed to _aesni_ctr32_ghash_6x
445         vmovdqu         0x10($inp),$T2
446          vpshufb        $Ii,$Z0,$Z0
447         vmovdqu         ($inp),$Hkey
448          vpshufb        $Ii,$Z1,$Z1
449         vmovdqu         $Z0,0x30(%rsp)
450          vpshufb        $Ii,$Z2,$Z2
451         vmovdqu         $Z1,0x40(%rsp)
452          vpshufb        $Ii,$T2,$T2
453         vmovdqu         $Z2,0x50(%rsp)
454          vpshufb        $Ii,$Hkey,$Hkey
455         vmovdqu         $T2,0x60(%rsp)
456         vmovdqu         $Hkey,0x70(%rsp)
457
458         call            _aesni_ctr32_ghash_6x
459
460         vmovups         $inout0,-0x60($out)     # save output
461         vmovups         $inout1,-0x50($out)
462         vmovups         $inout2,-0x40($out)
463         vmovups         $inout3,-0x30($out)
464         vmovups         $inout4,-0x20($out)
465         vmovups         $inout5,-0x10($out)
466
467         vpshufb         ($const),$Xi,$Xi        # .Lbswap_mask
468         vmovdqu         $Xi,-0x40($Xip)         # output Xi
469
470         vzeroupper
471 ___
472 $code.=<<___ if ($win64);
473         movaps  -0xd8(%rax),%xmm6
474         movaps  -0xd8(%rax),%xmm7
475         movaps  -0xb8(%rax),%xmm8
476         movaps  -0xa8(%rax),%xmm9
477         movaps  -0x98(%rax),%xmm10
478         movaps  -0x88(%rax),%xmm11
479         movaps  -0x78(%rax),%xmm12
480         movaps  -0x68(%rax),%xmm13
481         movaps  -0x58(%rax),%xmm14
482         movaps  -0x48(%rax),%xmm15
483 ___
484 $code.=<<___;
485         mov     -48(%rax),%r15
486         mov     -40(%rax),%r14
487         mov     -32(%rax),%r13
488         mov     -24(%rax),%r12
489         mov     -16(%rax),%rbp
490         mov     -8(%rax),%rbx
491         lea     (%rax),%rsp             # restore %rsp
492 .Lgcm_dec_abort:
493         mov     $ret,%rax               # return value
494         ret
495 .size   aesni_gcm_decrypt,.-aesni_gcm_decrypt
496 ___
497
498 $code.=<<___;
499 .type   _aesni_ctr32_6x,\@abi-omnipotent
500 .align  32
501 _aesni_ctr32_6x:
502         vmovdqu         0x00-0x80($key),$Z0     # borrow $Z0 for $rndkey
503         vmovdqu         0x20($const),$T2        # borrow $T2, .Lone_msb
504         lea             -1($rounds),%r13
505         vmovups         0x10-0x80($key),$rndkey
506         lea             0x20-0x80($key),%r12
507         vpxor           $Z0,$T1,$inout0
508         add             \$6<<24,$counter
509         jc              .Lhandle_ctr32_2
510         vpaddb          $T2,$T1,$inout1
511         vpaddb          $T2,$inout1,$inout2
512         vpxor           $Z0,$inout1,$inout1
513         vpaddb          $T2,$inout2,$inout3
514         vpxor           $Z0,$inout2,$inout2
515         vpaddb          $T2,$inout3,$inout4
516         vpxor           $Z0,$inout3,$inout3
517         vpaddb          $T2,$inout4,$inout5
518         vpxor           $Z0,$inout4,$inout4
519         vpaddb          $T2,$inout5,$T1
520         vpxor           $Z0,$inout5,$inout5
521         jmp             .Loop_ctr32
522
523 .align  16
524 .Loop_ctr32:
525         vaesenc         $rndkey,$inout0,$inout0
526         vaesenc         $rndkey,$inout1,$inout1
527         vaesenc         $rndkey,$inout2,$inout2
528         vaesenc         $rndkey,$inout3,$inout3
529         vaesenc         $rndkey,$inout4,$inout4
530         vaesenc         $rndkey,$inout5,$inout5
531         vmovups         (%r12),$rndkey
532         lea             0x10(%r12),%r12
533         dec             %r13d
534         jnz             .Loop_ctr32
535
536         vmovdqu         (%r12),$Hkey            # last round key
537         vaesenc         $rndkey,$inout0,$inout0
538         vpxor           0x00($inp),$Hkey,$Z0
539         vaesenc         $rndkey,$inout1,$inout1
540         vpxor           0x10($inp),$Hkey,$Z1
541         vaesenc         $rndkey,$inout2,$inout2
542         vpxor           0x20($inp),$Hkey,$Z2
543         vaesenc         $rndkey,$inout3,$inout3
544         vpxor           0x30($inp),$Hkey,$Xi
545         vaesenc         $rndkey,$inout4,$inout4
546         vpxor           0x40($inp),$Hkey,$T2
547         vaesenc         $rndkey,$inout5,$inout5
548         vpxor           0x50($inp),$Hkey,$Hkey
549         lea             0x60($inp),$inp
550
551         vaesenclast     $Z0,$inout0,$inout0
552         vaesenclast     $Z1,$inout1,$inout1
553         vaesenclast     $Z2,$inout2,$inout2
554         vaesenclast     $Xi,$inout3,$inout3
555         vaesenclast     $T2,$inout4,$inout4
556         vaesenclast     $Hkey,$inout5,$inout5
557         vmovups         $inout0,0x00($out)
558         vmovups         $inout1,0x10($out)
559         vmovups         $inout2,0x20($out)
560         vmovups         $inout3,0x30($out)
561         vmovups         $inout4,0x40($out)
562         vmovups         $inout5,0x50($out)
563         lea             0x60($out),$out
564
565         ret
566 .align  32
567 .Lhandle_ctr32_2:
568         vpshufb         $Ii,$T1,$Z2             # byte-swap counter
569         vmovdqu         0x30($const),$Z1        # borrow $Z1, .Ltwo_lsb
570         vpaddd          0x40($const),$Z2,$inout1        # .Lone_lsb
571         vpaddd          $Z1,$Z2,$inout2
572         vpaddd          $Z1,$inout1,$inout3
573         vpshufb         $Ii,$inout1,$inout1
574         vpaddd          $Z1,$inout2,$inout4
575         vpshufb         $Ii,$inout2,$inout2
576         vpxor           $Z0,$inout1,$inout1
577         vpaddd          $Z1,$inout3,$inout5
578         vpshufb         $Ii,$inout3,$inout3
579         vpxor           $Z0,$inout2,$inout2
580         vpaddd          $Z1,$inout4,$T1         # byte-swapped next counter value
581         vpshufb         $Ii,$inout4,$inout4
582         vpxor           $Z0,$inout3,$inout3
583         vpshufb         $Ii,$inout5,$inout5
584         vpxor           $Z0,$inout4,$inout4
585         vpshufb         $Ii,$T1,$T1             # next counter value
586         vpxor           $Z0,$inout5,$inout5
587         jmp     .Loop_ctr32
588 .size   _aesni_ctr32_6x,.-_aesni_ctr32_6x
589
590 .globl  aesni_gcm_encrypt
591 .type   aesni_gcm_encrypt,\@function,6
592 .align  32
593 aesni_gcm_encrypt:
594         xor     $ret,$ret
595         cmp     \$0x60*3,$len                   # minimal accepted length
596         jb      .Lgcm_enc_abort
597
598         lea     (%rsp),%rax                     # save stack pointer
599         push    %rbx
600         push    %rbp
601         push    %r12
602         push    %r13
603         push    %r14
604         push    %r15
605 ___
606 $code.=<<___ if ($win64);
607         lea     -0xa8(%rsp),%rsp
608         movaps  %xmm6,-0xd8(%rax)
609         movaps  %xmm7,-0xc8(%rax)
610         movaps  %xmm8,-0xb8(%rax)
611         movaps  %xmm9,-0xa8(%rax)
612         movaps  %xmm10,-0x98(%rax)
613         movaps  %xmm11,-0x88(%rax)
614         movaps  %xmm12,-0x78(%rax)
615         movaps  %xmm13,-0x68(%rax)
616         movaps  %xmm14,-0x58(%rax)
617         movaps  %xmm15,-0x48(%rax)
618 .Lgcm_enc_body:
619 ___
620 $code.=<<___;
621         vzeroupper
622
623         vmovdqu         ($ivp),$T1              # input counter value
624         sub             \$128,%rsp
625         mov             12($ivp),$counter
626         lea             .Lbswap_mask(%rip),$const
627         lea             0x80($key),$key         # size optimization
628         vmovdqu         ($const),$Ii            # borrow $Ii for .Lbswap_mask
629         and             \$-64,%rsp              # ensure stack alignment
630         mov             0xf0-0x80($key),$rounds
631
632         lea             ($out),$in0
633         lea             -0xc0($out,$len),$end0
634         shr             \$4,$len
635
636         call            _aesni_ctr32_6x
637         vpshufb         $Ii,$inout0,$Xi         # save bswapped output on stack
638         vpshufb         $Ii,$inout1,$T2
639         vmovdqu         $Xi,0x70(%rsp)
640         vpshufb         $Ii,$inout2,$Z0
641         vmovdqu         $T2,0x60(%rsp)
642         vpshufb         $Ii,$inout3,$Z1
643         vmovdqu         $Z0,0x50(%rsp)
644         vpshufb         $Ii,$inout4,$Z2
645         vmovdqu         $Z1,0x40(%rsp)
646         vpshufb         $Ii,$inout5,$Z3         # passed to _aesni_ctr32_ghash_6x
647         vmovdqu         $Z2,0x30(%rsp)
648
649         call            _aesni_ctr32_6x
650
651         vmovdqu         ($Xip),$Xi              # load Xi
652         lea             0x20+0x20($Xip),$Xip    # size optimization
653         sub             \$12,$len
654         mov             \$0x60*2,$ret
655         vpshufb         $Ii,$Xi,$Xi
656
657         call            _aesni_ctr32_ghash_6x
658         vmovdqu         0x20(%rsp),$Z3          # I[5]
659          vmovdqu        ($const),$Ii            # borrow $Ii for .Lbswap_mask
660         vmovdqu         0x00-0x20($Xip),$Hkey   # $Hkey^1
661         vpunpckhqdq     $Z3,$Z3,$T1
662         vmovdqu         0x20-0x20($Xip),$rndkey # borrow $rndkey for $HK
663          vmovups        $inout0,-0x60($out)     # save output
664          vpshufb        $Ii,$inout0,$inout0     # but keep bswapped copy
665         vpxor           $Z3,$T1,$T1
666          vmovups        $inout1,-0x50($out)
667          vpshufb        $Ii,$inout1,$inout1
668          vmovups        $inout2,-0x40($out)
669          vpshufb        $Ii,$inout2,$inout2
670          vmovups        $inout3,-0x30($out)
671          vpshufb        $Ii,$inout3,$inout3
672          vmovups        $inout4,-0x20($out)
673          vpshufb        $Ii,$inout4,$inout4
674          vmovups        $inout5,-0x10($out)
675          vpshufb        $Ii,$inout5,$inout5
676          vmovdqu        $inout0,0x10(%rsp)      # free $inout0
677 ___
678 { my ($HK,$T3)=($rndkey,$inout0);
679
680 $code.=<<___;
681          vmovdqu        0x30(%rsp),$Z2          # I[4]
682          vmovdqu        0x10-0x20($Xip),$Ii     # borrow $Ii for $Hkey^2
683          vpunpckhqdq    $Z2,$Z2,$T2
684         vpclmulqdq      \$0x00,$Hkey,$Z3,$Z1
685          vpxor          $Z2,$T2,$T2
686         vpclmulqdq      \$0x11,$Hkey,$Z3,$Z3
687         vpclmulqdq      \$0x00,$HK,$T1,$T1
688
689          vmovdqu        0x40(%rsp),$T3          # I[3]
690         vpclmulqdq      \$0x00,$Ii,$Z2,$Z0
691          vmovdqu        0x30-0x20($Xip),$Hkey   # $Hkey^3
692         vpxor           $Z1,$Z0,$Z0
693          vpunpckhqdq    $T3,$T3,$Z1
694         vpclmulqdq      \$0x11,$Ii,$Z2,$Z2
695          vpxor          $T3,$Z1,$Z1
696         vpxor           $Z3,$Z2,$Z2
697         vpclmulqdq      \$0x10,$HK,$T2,$T2
698          vmovdqu        0x50-0x20($Xip),$HK
699         vpxor           $T1,$T2,$T2
700
701          vmovdqu        0x50(%rsp),$T1          # I[2]
702         vpclmulqdq      \$0x00,$Hkey,$T3,$Z3
703          vmovdqu        0x40-0x20($Xip),$Ii     # borrow $Ii for $Hkey^4
704         vpxor           $Z0,$Z3,$Z3
705          vpunpckhqdq    $T1,$T1,$Z0
706         vpclmulqdq      \$0x11,$Hkey,$T3,$T3
707          vpxor          $T1,$Z0,$Z0
708         vpxor           $Z2,$T3,$T3
709         vpclmulqdq      \$0x00,$HK,$Z1,$Z1
710         vpxor           $T2,$Z1,$Z1
711
712          vmovdqu        0x60(%rsp),$T2          # I[1]
713         vpclmulqdq      \$0x00,$Ii,$T1,$Z2
714          vmovdqu        0x60-0x20($Xip),$Hkey   # $Hkey^5
715         vpxor           $Z3,$Z2,$Z2
716          vpunpckhqdq    $T2,$T2,$Z3
717         vpclmulqdq      \$0x11,$Ii,$T1,$T1
718          vpxor          $T2,$Z3,$Z3
719         vpxor           $T3,$T1,$T1
720         vpclmulqdq      \$0x10,$HK,$Z0,$Z0
721          vmovdqu        0x80-0x20($Xip),$HK
722         vpxor           $Z1,$Z0,$Z0
723
724          vpxor          0x70(%rsp),$Xi,$Xi      # accumulate I[0]
725         vpclmulqdq      \$0x00,$Hkey,$T2,$Z1
726          vmovdqu        0x70-0x20($Xip),$Ii     # borrow $Ii for $Hkey^6
727          vpunpckhqdq    $Xi,$Xi,$T3
728         vpxor           $Z2,$Z1,$Z1
729         vpclmulqdq      \$0x11,$Hkey,$T2,$T2
730          vpxor          $Xi,$T3,$T3
731         vpxor           $T1,$T2,$T2
732         vpclmulqdq      \$0x00,$HK,$Z3,$Z3
733         vpxor           $Z0,$Z3,$Z0
734
735         vpclmulqdq      \$0x00,$Ii,$Xi,$Z2
736          vmovdqu        0x00-0x20($Xip),$Hkey   # $Hkey^1
737          vpunpckhqdq    $inout5,$inout5,$T1
738         vpclmulqdq      \$0x11,$Ii,$Xi,$Xi
739          vpxor          $inout5,$T1,$T1
740         vpxor           $Z1,$Z2,$Z1
741         vpclmulqdq      \$0x10,$HK,$T3,$T3
742          vmovdqu        0x20-0x20($Xip),$HK
743         vpxor           $T2,$Xi,$Z3
744         vpxor           $Z0,$T3,$Z2
745
746          vmovdqu        0x10-0x20($Xip),$Ii     # borrow $Ii for $Hkey^2
747           vpxor         $Z1,$Z3,$T3             # aggregated Karatsuba post-processing
748         vpclmulqdq      \$0x00,$Hkey,$inout5,$Z0
749           vpxor         $T3,$Z2,$Z2
750          vpunpckhqdq    $inout4,$inout4,$T2
751         vpclmulqdq      \$0x11,$Hkey,$inout5,$inout5
752          vpxor          $inout4,$T2,$T2
753           vpslldq       \$8,$Z2,$T3
754         vpclmulqdq      \$0x00,$HK,$T1,$T1
755           vpxor         $T3,$Z1,$Xi
756           vpsrldq       \$8,$Z2,$Z2
757           vpxor         $Z2,$Z3,$Z3
758
759         vpclmulqdq      \$0x00,$Ii,$inout4,$Z1
760          vmovdqu        0x30-0x20($Xip),$Hkey   # $Hkey^3
761         vpxor           $Z0,$Z1,$Z1
762          vpunpckhqdq    $inout3,$inout3,$T3
763         vpclmulqdq      \$0x11,$Ii,$inout4,$inout4
764          vpxor          $inout3,$T3,$T3
765         vpxor           $inout5,$inout4,$inout4
766           vpalignr      \$8,$Xi,$Xi,$inout5     # 1st phase
767         vpclmulqdq      \$0x10,$HK,$T2,$T2
768          vmovdqu        0x50-0x20($Xip),$HK
769         vpxor           $T1,$T2,$T2
770
771         vpclmulqdq      \$0x00,$Hkey,$inout3,$Z0
772          vmovdqu        0x40-0x20($Xip),$Ii     # borrow $Ii for $Hkey^4
773         vpxor           $Z1,$Z0,$Z0
774          vpunpckhqdq    $inout2,$inout2,$T1
775         vpclmulqdq      \$0x11,$Hkey,$inout3,$inout3
776          vpxor          $inout2,$T1,$T1
777         vpxor           $inout4,$inout3,$inout3
778           vxorps        0x10(%rsp),$Z3,$Z3      # accumulate $inout0
779         vpclmulqdq      \$0x00,$HK,$T3,$T3
780         vpxor           $T2,$T3,$T3
781
782           vpclmulqdq    \$0x10,0x10($const),$Xi,$Xi
783           vxorps        $inout5,$Xi,$Xi
784
785         vpclmulqdq      \$0x00,$Ii,$inout2,$Z1
786          vmovdqu        0x60-0x20($Xip),$Hkey   # $Hkey^5
787         vpxor           $Z0,$Z1,$Z1
788          vpunpckhqdq    $inout1,$inout1,$T2
789         vpclmulqdq      \$0x11,$Ii,$inout2,$inout2
790          vpxor          $inout1,$T2,$T2
791           vpalignr      \$8,$Xi,$Xi,$inout5     # 2nd phase
792         vpxor           $inout3,$inout2,$inout2
793         vpclmulqdq      \$0x10,$HK,$T1,$T1
794          vmovdqu        0x80-0x20($Xip),$HK
795         vpxor           $T3,$T1,$T1
796
797           vxorps        $Z3,$inout5,$inout5
798           vpclmulqdq    \$0x10,0x10($const),$Xi,$Xi
799           vxorps        $inout5,$Xi,$Xi
800
801         vpclmulqdq      \$0x00,$Hkey,$inout1,$Z0
802          vmovdqu        0x70-0x20($Xip),$Ii     # borrow $Ii for $Hkey^6
803         vpxor           $Z1,$Z0,$Z0
804          vpunpckhqdq    $Xi,$Xi,$T3
805         vpclmulqdq      \$0x11,$Hkey,$inout1,$inout1
806          vpxor          $Xi,$T3,$T3
807         vpxor           $inout2,$inout1,$inout1
808         vpclmulqdq      \$0x00,$HK,$T2,$T2
809         vpxor           $T1,$T2,$T2
810
811         vpclmulqdq      \$0x00,$Ii,$Xi,$Z1
812         vpclmulqdq      \$0x11,$Ii,$Xi,$Z3
813         vpxor           $Z0,$Z1,$Z1
814         vpclmulqdq      \$0x10,$HK,$T3,$Z2
815         vpxor           $inout1,$Z3,$Z3
816         vpxor           $T2,$Z2,$Z2
817
818         vpxor           $Z1,$Z3,$Z0             # aggregated Karatsuba post-processing
819         vpxor           $Z0,$Z2,$Z2
820         vpslldq         \$8,$Z2,$T1
821         vmovdqu         0x10($const),$Hkey      # .Lpoly
822         vpsrldq         \$8,$Z2,$Z2
823         vpxor           $T1,$Z1,$Xi
824         vpxor           $Z2,$Z3,$Z3
825
826         vpalignr        \$8,$Xi,$Xi,$T2         # 1st phase
827         vpclmulqdq      \$0x10,$Hkey,$Xi,$Xi
828         vpxor           $T2,$Xi,$Xi
829
830         vpalignr        \$8,$Xi,$Xi,$T2         # 2nd phase
831         vpclmulqdq      \$0x10,$Hkey,$Xi,$Xi
832         vpxor           $Z3,$T2,$T2
833         vpxor           $T2,$Xi,$Xi
834 ___
835 }
836 $code.=<<___;
837         vpshufb         ($const),$Xi,$Xi        # .Lbswap_mask
838         vmovdqu         $Xi,-0x40($Xip)         # output Xi
839
840         vzeroupper
841 ___
842 $code.=<<___ if ($win64);
843         movaps  -0xd8(%rax),%xmm6
844         movaps  -0xc8(%rax),%xmm7
845         movaps  -0xb8(%rax),%xmm8
846         movaps  -0xa8(%rax),%xmm9
847         movaps  -0x98(%rax),%xmm10
848         movaps  -0x88(%rax),%xmm11
849         movaps  -0x78(%rax),%xmm12
850         movaps  -0x68(%rax),%xmm13
851         movaps  -0x58(%rax),%xmm14
852         movaps  -0x48(%rax),%xmm15
853 ___
854 $code.=<<___;
855         mov     -48(%rax),%r15
856         mov     -40(%rax),%r14
857         mov     -32(%rax),%r13
858         mov     -24(%rax),%r12
859         mov     -16(%rax),%rbp
860         mov     -8(%rax),%rbx
861         lea     (%rax),%rsp             # restore %rsp
862 .Lgcm_enc_abort:
863         mov     $ret,%rax               # return value
864         ret
865 .size   aesni_gcm_encrypt,.-aesni_gcm_encrypt
866 ___
867
868 $code.=<<___;
869 .align  64
870 .Lbswap_mask:
871         .byte   15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
872 .Lpoly:
873         .byte   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
874 .Lone_msb:
875         .byte   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
876 .Ltwo_lsb:
877         .byte   2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
878 .Lone_lsb:
879         .byte   1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
880 .asciz  "AES-NI GCM module for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
881 .align  64
882 ___
883 if ($win64) {
884 $rec="%rcx";
885 $frame="%rdx";
886 $context="%r8";
887 $disp="%r9";
888
889 $code.=<<___
890 .extern __imp_RtlVirtualUnwind
891 .type   gcm_se_handler,\@abi-omnipotent
892 .align  16
893 gcm_se_handler:
894         push    %rsi
895         push    %rdi
896         push    %rbx
897         push    %rbp
898         push    %r12
899         push    %r13
900         push    %r14
901         push    %r15
902         pushfq
903         sub     \$64,%rsp
904
905         mov     120($context),%rax      # pull context->Rax
906         mov     248($context),%rbx      # pull context->Rip
907
908         mov     8($disp),%rsi           # disp->ImageBase
909         mov     56($disp),%r11          # disp->HandlerData
910
911         mov     0(%r11),%r10d           # HandlerData[0]
912         lea     (%rsi,%r10),%r10        # prologue label
913         cmp     %r10,%rbx               # context->Rip<prologue label
914         jb      .Lcommon_seh_tail
915
916         mov     152($context),%rax      # pull context->Rsp
917
918         mov     4(%r11),%r10d           # HandlerData[1]
919         lea     (%rsi,%r10),%r10        # epilogue label
920         cmp     %r10,%rbx               # context->Rip>=epilogue label
921         jae     .Lcommon_seh_tail
922
923         mov     120($context),%rax      # pull context->Rax
924
925         mov     -48(%rax),%r15
926         mov     -40(%rax),%r14
927         mov     -32(%rax),%r13
928         mov     -24(%rax),%r12
929         mov     -16(%rax),%rbp
930         mov     -8(%rax),%rbx
931         mov     %r15,240($context)
932         mov     %r14,232($context)
933         mov     %r13,224($context)
934         mov     %r12,216($context)
935         mov     %rbp,160($context)
936         mov     %rbx,144($context)
937
938         lea     -0xd8(%rax),%rsi        # %xmm save area
939         lea     512($context),%rdi      # & context.Xmm6
940         mov     \$20,%ecx               # 10*sizeof(%xmm0)/sizeof(%rax)
941         .long   0xa548f3fc              # cld; rep movsq
942
943 .Lcommon_seh_tail:
944         mov     8(%rax),%rdi
945         mov     16(%rax),%rsi
946         mov     %rax,152($context)      # restore context->Rsp
947         mov     %rsi,168($context)      # restore context->Rsi
948         mov     %rdi,176($context)      # restore context->Rdi
949
950         mov     40($disp),%rdi          # disp->ContextRecord
951         mov     $context,%rsi           # context
952         mov     \$154,%ecx              # sizeof(CONTEXT)
953         .long   0xa548f3fc              # cld; rep movsq
954
955         mov     $disp,%rsi
956         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
957         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
958         mov     0(%rsi),%r8             # arg3, disp->ControlPc
959         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
960         mov     40(%rsi),%r10           # disp->ContextRecord
961         lea     56(%rsi),%r11           # &disp->HandlerData
962         lea     24(%rsi),%r12           # &disp->EstablisherFrame
963         mov     %r10,32(%rsp)           # arg5
964         mov     %r11,40(%rsp)           # arg6
965         mov     %r12,48(%rsp)           # arg7
966         mov     %rcx,56(%rsp)           # arg8, (NULL)
967         call    *__imp_RtlVirtualUnwind(%rip)
968
969         mov     \$1,%eax                # ExceptionContinueSearch
970         add     \$64,%rsp
971         popfq
972         pop     %r15
973         pop     %r14
974         pop     %r13
975         pop     %r12
976         pop     %rbp
977         pop     %rbx
978         pop     %rdi
979         pop     %rsi
980         ret
981 .size   gcm_se_handler,.-gcm_se_handler
982
983 .section        .pdata
984 .align  4
985         .rva    .LSEH_begin_aesni_gcm_decrypt
986         .rva    .LSEH_end_aesni_gcm_decrypt
987         .rva    .LSEH_gcm_dec_info
988
989         .rva    .LSEH_begin_aesni_gcm_encrypt
990         .rva    .LSEH_end_aesni_gcm_encrypt
991         .rva    .LSEH_gcm_enc_info
992 .section        .xdata
993 .align  8
994 .LSEH_gcm_dec_info:
995         .byte   9,0,0,0
996         .rva    gcm_se_handler
997         .rva    .Lgcm_dec_body,.Lgcm_dec_abort
998 .LSEH_gcm_enc_info:
999         .byte   9,0,0,0
1000         .rva    gcm_se_handler
1001         .rva    .Lgcm_enc_body,.Lgcm_enc_abort
1002 ___
1003 }
1004 }}} else {{{
1005 $code=<<___;    # assembler is too old
1006 .text
1007
1008 .globl  aesni_gcm_encrypt
1009 .type   aesni_gcm_encrypt,\@abi-omnipotent
1010 aesni_gcm_encrypt:
1011         xor     %eax,%eax
1012         ret
1013 .size   aesni_gcm_encrypt,.-aesni_gcm_encrypt
1014
1015 .globl  aesni_gcm_decrypt
1016 .type   aesni_gcm_decrypt,\@abi-omnipotent
1017 aesni_gcm_decrypt:
1018         xor     %eax,%eax
1019         ret
1020 .size   aesni_gcm_decrypt,.-aesni_gcm_decrypt
1021 ___
1022 }}}
1023
1024 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1025
1026 print $code;
1027
1028 close STDOUT;