1#! /usr/bin/env perl
2# Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9#
10# ====================================================================
11# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12# project. The module is, however, dual licensed under OpenSSL and
13# CRYPTOGAMS licenses depending on where you obtain it. For further
14# details see http://www.openssl.org/~appro/cryptogams/.
15# ====================================================================
16#
17# Version 2.1.
18#
19# aes-*-cbc benchmarks are improved by >70% [compared to gcc 3.3.2 on
20# Opteron 240 CPU] plus all the bells-n-whistles from 32-bit version
21# [you'll notice a lot of resemblance], such as compressed S-boxes
22# in little-endian byte order, prefetch of these tables in CBC mode,
23# as well as avoiding L1 cache aliasing between stack frame and key
24# schedule and already mentioned tables, compressed Td4...
25#
26# Performance in number of cycles per processed byte for 128-bit key:
27#
28#		ECB encrypt	ECB decrypt	CBC large chunk
29# AMD64		33		43		13.0
30# EM64T		38		56		18.6(*)
31# Core 2	30		42		14.5(*)
32# Atom		65		86		32.1(*)
33#
34# (*) with hyper-threading off
35
36# $output is the last argument if it looks like a file (it has an extension)
37# $flavour is the first argument if it doesn't look like a file
38$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
39$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
40
41$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
42
43$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
44( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
45( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
46die "can't locate x86_64-xlate.pl";
47
48open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
49    or die "can't call $xlate: $!";
50*STDOUT=*OUT;
51
52$verticalspin=1;	# unlike 32-bit version $verticalspin performs
53			# ~15% better on both AMD and Intel cores
54$speed_limit=512;	# see aes-586.pl for details
55
56$code=".text\n";
57
58$s0="%eax";
59$s1="%ebx";
60$s2="%ecx";
61$s3="%edx";
62$acc0="%esi";	$mask80="%rsi";
63$acc1="%edi";	$maskfe="%rdi";
64$acc2="%ebp";	$mask1b="%rbp";
65$inp="%r8";
66$out="%r9";
67$t0="%r10d";
68$t1="%r11d";
69$t2="%r12d";
70$rnds="%r13d";
71$sbox="%r14";
72$key="%r15";
73
74sub hi() { my $r=shift;	$r =~ s/%[er]([a-d])x/%\1h/;	$r; }
75sub lo() { my $r=shift;	$r =~ s/%[er]([a-d])x/%\1l/;
76			$r =~ s/%[er]([sd]i)/%\1l/;
77			$r =~ s/%(r[0-9]+)[d]?/%\1b/;	$r; }
78sub LO() { my $r=shift; $r =~ s/%r([a-z]+)/%e\1/;
79			$r =~ s/%r([0-9]+)/%r\1d/;	$r; }
80sub _data_word()
81{ my $i;
82    while(defined($i=shift)) { $code.=sprintf".long\t0x%08x,0x%08x\n",$i,$i; }
83}
84sub data_word()
85{ my $i;
86  my $last=pop(@_);
87    $code.=".long\t";
88    while(defined($i=shift)) { $code.=sprintf"0x%08x,",$i; }
89    $code.=sprintf"0x%08x\n",$last;
90}
91
92sub data_byte()
93{ my $i;
94  my $last=pop(@_);
95    $code.=".byte\t";
96    while(defined($i=shift)) { $code.=sprintf"0x%02x,",$i&0xff; }
97    $code.=sprintf"0x%02x\n",$last&0xff;
98}
99
100sub encvert()
101{ my $t3="%r8d";	# zaps $inp!
102
103$code.=<<___;
104	# favor 3-way issue Opteron pipeline...
105	movzb	`&lo("$s0")`,$acc0
106	movzb	`&lo("$s1")`,$acc1
107	movzb	`&lo("$s2")`,$acc2
108	mov	0($sbox,$acc0,8),$t0
109	mov	0($sbox,$acc1,8),$t1
110	mov	0($sbox,$acc2,8),$t2
111
112	movzb	`&hi("$s1")`,$acc0
113	movzb	`&hi("$s2")`,$acc1
114	movzb	`&lo("$s3")`,$acc2
115	xor	3($sbox,$acc0,8),$t0
116	xor	3($sbox,$acc1,8),$t1
117	mov	0($sbox,$acc2,8),$t3
118
119	movzb	`&hi("$s3")`,$acc0
120	shr	\$16,$s2
121	movzb	`&hi("$s0")`,$acc2
122	xor	3($sbox,$acc0,8),$t2
123	shr	\$16,$s3
124	xor	3($sbox,$acc2,8),$t3
125
126	shr	\$16,$s1
127	lea	16($key),$key
128	shr	\$16,$s0
129
130	movzb	`&lo("$s2")`,$acc0
131	movzb	`&lo("$s3")`,$acc1
132	movzb	`&lo("$s0")`,$acc2
133	xor	2($sbox,$acc0,8),$t0
134	xor	2($sbox,$acc1,8),$t1
135	xor	2($sbox,$acc2,8),$t2
136
137	movzb	`&hi("$s3")`,$acc0
138	movzb	`&hi("$s0")`,$acc1
139	movzb	`&lo("$s1")`,$acc2
140	xor	1($sbox,$acc0,8),$t0
141	xor	1($sbox,$acc1,8),$t1
142	xor	2($sbox,$acc2,8),$t3
143
144	mov	12($key),$s3
145	movzb	`&hi("$s1")`,$acc1
146	movzb	`&hi("$s2")`,$acc2
147	mov	0($key),$s0
148	xor	1($sbox,$acc1,8),$t2
149	xor	1($sbox,$acc2,8),$t3
150
151	mov	4($key),$s1
152	mov	8($key),$s2
153	xor	$t0,$s0
154	xor	$t1,$s1
155	xor	$t2,$s2
156	xor	$t3,$s3
157___
158}
159
160sub enclastvert()
161{ my $t3="%r8d";	# zaps $inp!
162
163$code.=<<___;
164	movzb	`&lo("$s0")`,$acc0
165	movzb	`&lo("$s1")`,$acc1
166	movzb	`&lo("$s2")`,$acc2
167	movzb	2($sbox,$acc0,8),$t0
168	movzb	2($sbox,$acc1,8),$t1
169	movzb	2($sbox,$acc2,8),$t2
170
171	movzb	`&lo("$s3")`,$acc0
172	movzb	`&hi("$s1")`,$acc1
173	movzb	`&hi("$s2")`,$acc2
174	movzb	2($sbox,$acc0,8),$t3
175	mov	0($sbox,$acc1,8),$acc1	#$t0
176	mov	0($sbox,$acc2,8),$acc2	#$t1
177
178	and	\$0x0000ff00,$acc1
179	and	\$0x0000ff00,$acc2
180
181	xor	$acc1,$t0
182	xor	$acc2,$t1
183	shr	\$16,$s2
184
185	movzb	`&hi("$s3")`,$acc0
186	movzb	`&hi("$s0")`,$acc1
187	shr	\$16,$s3
188	mov	0($sbox,$acc0,8),$acc0	#$t2
189	mov	0($sbox,$acc1,8),$acc1	#$t3
190
191	and	\$0x0000ff00,$acc0
192	and	\$0x0000ff00,$acc1
193	shr	\$16,$s1
194	xor	$acc0,$t2
195	xor	$acc1,$t3
196	shr	\$16,$s0
197
198	movzb	`&lo("$s2")`,$acc0
199	movzb	`&lo("$s3")`,$acc1
200	movzb	`&lo("$s0")`,$acc2
201	mov	0($sbox,$acc0,8),$acc0	#$t0
202	mov	0($sbox,$acc1,8),$acc1	#$t1
203	mov	0($sbox,$acc2,8),$acc2	#$t2
204
205	and	\$0x00ff0000,$acc0
206	and	\$0x00ff0000,$acc1
207	and	\$0x00ff0000,$acc2
208
209	xor	$acc0,$t0
210	xor	$acc1,$t1
211	xor	$acc2,$t2
212
213	movzb	`&lo("$s1")`,$acc0
214	movzb	`&hi("$s3")`,$acc1
215	movzb	`&hi("$s0")`,$acc2
216	mov	0($sbox,$acc0,8),$acc0	#$t3
217	mov	2($sbox,$acc1,8),$acc1	#$t0
218	mov	2($sbox,$acc2,8),$acc2	#$t1
219
220	and	\$0x00ff0000,$acc0
221	and	\$0xff000000,$acc1
222	and	\$0xff000000,$acc2
223
224	xor	$acc0,$t3
225	xor	$acc1,$t0
226	xor	$acc2,$t1
227
228	movzb	`&hi("$s1")`,$acc0
229	movzb	`&hi("$s2")`,$acc1
230	mov	16+12($key),$s3
231	mov	2($sbox,$acc0,8),$acc0	#$t2
232	mov	2($sbox,$acc1,8),$acc1	#$t3
233	mov	16+0($key),$s0
234
235	and	\$0xff000000,$acc0
236	and	\$0xff000000,$acc1
237
238	xor	$acc0,$t2
239	xor	$acc1,$t3
240
241	mov	16+4($key),$s1
242	mov	16+8($key),$s2
243	xor	$t0,$s0
244	xor	$t1,$s1
245	xor	$t2,$s2
246	xor	$t3,$s3
247___
248}
249
250sub encstep()
251{ my ($i,@s) = @_;
252  my $tmp0=$acc0;
253  my $tmp1=$acc1;
254  my $tmp2=$acc2;
255  my $out=($t0,$t1,$t2,$s[0])[$i];
256
257	if ($i==3) {
258		$tmp0=$s[1];
259		$tmp1=$s[2];
260		$tmp2=$s[3];
261	}
262	$code.="	movzb	".&lo($s[0]).",$out\n";
263	$code.="	mov	$s[2],$tmp1\n"		if ($i!=3);
264	$code.="	lea	16($key),$key\n"	if ($i==0);
265
266	$code.="	movzb	".&hi($s[1]).",$tmp0\n";
267	$code.="	mov	0($sbox,$out,8),$out\n";
268
269	$code.="	shr	\$16,$tmp1\n";
270	$code.="	mov	$s[3],$tmp2\n"		if ($i!=3);
271	$code.="	xor	3($sbox,$tmp0,8),$out\n";
272
273	$code.="	movzb	".&lo($tmp1).",$tmp1\n";
274	$code.="	shr	\$24,$tmp2\n";
275	$code.="	xor	4*$i($key),$out\n";
276
277	$code.="	xor	2($sbox,$tmp1,8),$out\n";
278	$code.="	xor	1($sbox,$tmp2,8),$out\n";
279
280	$code.="	mov	$t0,$s[1]\n"		if ($i==3);
281	$code.="	mov	$t1,$s[2]\n"		if ($i==3);
282	$code.="	mov	$t2,$s[3]\n"		if ($i==3);
283	$code.="\n";
284}
285
286sub enclast()
287{ my ($i,@s)=@_;
288  my $tmp0=$acc0;
289  my $tmp1=$acc1;
290  my $tmp2=$acc2;
291  my $out=($t0,$t1,$t2,$s[0])[$i];
292
293	if ($i==3) {
294		$tmp0=$s[1];
295		$tmp1=$s[2];
296		$tmp2=$s[3];
297	}
298	$code.="	movzb	".&lo($s[0]).",$out\n";
299	$code.="	mov	$s[2],$tmp1\n"		if ($i!=3);
300
301	$code.="	mov	2($sbox,$out,8),$out\n";
302	$code.="	shr	\$16,$tmp1\n";
303	$code.="	mov	$s[3],$tmp2\n"		if ($i!=3);
304
305	$code.="	and	\$0x000000ff,$out\n";
306	$code.="	movzb	".&hi($s[1]).",$tmp0\n";
307	$code.="	movzb	".&lo($tmp1).",$tmp1\n";
308	$code.="	shr	\$24,$tmp2\n";
309
310	$code.="	mov	0($sbox,$tmp0,8),$tmp0\n";
311	$code.="	mov	0($sbox,$tmp1,8),$tmp1\n";
312	$code.="	mov	2($sbox,$tmp2,8),$tmp2\n";
313
314	$code.="	and	\$0x0000ff00,$tmp0\n";
315	$code.="	and	\$0x00ff0000,$tmp1\n";
316	$code.="	and	\$0xff000000,$tmp2\n";
317
318	$code.="	xor	$tmp0,$out\n";
319	$code.="	mov	$t0,$s[1]\n"		if ($i==3);
320	$code.="	xor	$tmp1,$out\n";
321	$code.="	mov	$t1,$s[2]\n"		if ($i==3);
322	$code.="	xor	$tmp2,$out\n";
323	$code.="	mov	$t2,$s[3]\n"		if ($i==3);
324	$code.="\n";
325}
326
327$code.=<<___;
328.type	_x86_64_AES_encrypt,\@abi-omnipotent
329.align	16
330_x86_64_AES_encrypt:
331.cfi_startproc
332	xor	0($key),$s0			# xor with key
333	xor	4($key),$s1
334	xor	8($key),$s2
335	xor	12($key),$s3
336
337	mov	240($key),$rnds			# load key->rounds
338	sub	\$1,$rnds
339	jmp	.Lenc_loop
340.align	16
341.Lenc_loop:
342___
343	if ($verticalspin) { &encvert(); }
344	else {	&encstep(0,$s0,$s1,$s2,$s3);
345		&encstep(1,$s1,$s2,$s3,$s0);
346		&encstep(2,$s2,$s3,$s0,$s1);
347		&encstep(3,$s3,$s0,$s1,$s2);
348	}
349$code.=<<___;
350	sub	\$1,$rnds
351	jnz	.Lenc_loop
352___
353	if ($verticalspin) { &enclastvert(); }
354	else {	&enclast(0,$s0,$s1,$s2,$s3);
355		&enclast(1,$s1,$s2,$s3,$s0);
356		&enclast(2,$s2,$s3,$s0,$s1);
357		&enclast(3,$s3,$s0,$s1,$s2);
358		$code.=<<___;
359		xor	16+0($key),$s0		# xor with key
360		xor	16+4($key),$s1
361		xor	16+8($key),$s2
362		xor	16+12($key),$s3
363___
364	}
365$code.=<<___;
366	.byte	0xf3,0xc3			# rep ret
367.cfi_endproc
368.size	_x86_64_AES_encrypt,.-_x86_64_AES_encrypt
369___
370
371# it's possible to implement this by shifting tN by 8, filling least
372# significant byte with byte load and finally bswap-ing at the end,
373# but such partial register load kills Core 2...
374sub enccompactvert()
375{ my ($t3,$t4,$t5)=("%r8d","%r9d","%r13d");
376
377$code.=<<___;
378	movzb	`&lo("$s0")`,$t0
379	movzb	`&lo("$s1")`,$t1
380	movzb	`&lo("$s2")`,$t2
381	movzb	`&lo("$s3")`,$t3
382	movzb	`&hi("$s1")`,$acc0
383	movzb	`&hi("$s2")`,$acc1
384	shr	\$16,$s2
385	movzb	`&hi("$s3")`,$acc2
386	movzb	($sbox,$t0,1),$t0
387	movzb	($sbox,$t1,1),$t1
388	movzb	($sbox,$t2,1),$t2
389	movzb	($sbox,$t3,1),$t3
390
391	movzb	($sbox,$acc0,1),$t4	#$t0
392	movzb	`&hi("$s0")`,$acc0
393	movzb	($sbox,$acc1,1),$t5	#$t1
394	movzb	`&lo("$s2")`,$acc1
395	movzb	($sbox,$acc2,1),$acc2	#$t2
396	movzb	($sbox,$acc0,1),$acc0	#$t3
397
398	shl	\$8,$t4
399	shr	\$16,$s3
400	shl	\$8,$t5
401	xor	$t4,$t0
402	shr	\$16,$s0
403	movzb	`&lo("$s3")`,$t4
404	shr	\$16,$s1
405	xor	$t5,$t1
406	shl	\$8,$acc2
407	movzb	`&lo("$s0")`,$t5
408	movzb	($sbox,$acc1,1),$acc1	#$t0
409	xor	$acc2,$t2
410
411	shl	\$8,$acc0
412	movzb	`&lo("$s1")`,$acc2
413	shl	\$16,$acc1
414	xor	$acc0,$t3
415	movzb	($sbox,$t4,1),$t4	#$t1
416	movzb	`&hi("$s3")`,$acc0
417	movzb	($sbox,$t5,1),$t5	#$t2
418	xor	$acc1,$t0
419
420	shr	\$8,$s2
421	movzb	`&hi("$s0")`,$acc1
422	shl	\$16,$t4
423	shr	\$8,$s1
424	shl	\$16,$t5
425	xor	$t4,$t1
426	movzb	($sbox,$acc2,1),$acc2	#$t3
427	movzb	($sbox,$acc0,1),$acc0	#$t0
428	movzb	($sbox,$acc1,1),$acc1	#$t1
429	movzb	($sbox,$s2,1),$s3	#$t3
430	movzb	($sbox,$s1,1),$s2	#$t2
431
432	shl	\$16,$acc2
433	xor	$t5,$t2
434	shl	\$24,$acc0
435	xor	$acc2,$t3
436	shl	\$24,$acc1
437	xor	$acc0,$t0
438	shl	\$24,$s3
439	xor	$acc1,$t1
440	shl	\$24,$s2
441	mov	$t0,$s0
442	mov	$t1,$s1
443	xor	$t2,$s2
444	xor	$t3,$s3
445___
446}
447
448sub enctransform_ref()
449{ my $sn = shift;
450  my ($acc,$r2,$tmp)=("%r8d","%r9d","%r13d");
451
452$code.=<<___;
453	mov	$sn,$acc
454	and	\$0x80808080,$acc
455	mov	$acc,$tmp
456	shr	\$7,$tmp
457	lea	($sn,$sn),$r2
458	sub	$tmp,$acc
459	and	\$0xfefefefe,$r2
460	and	\$0x1b1b1b1b,$acc
461	mov	$sn,$tmp
462	xor	$acc,$r2
463
464	xor	$r2,$sn
465	rol	\$24,$sn
466	xor	$r2,$sn
467	ror	\$16,$tmp
468	xor	$tmp,$sn
469	ror	\$8,$tmp
470	xor	$tmp,$sn
471___
472}
473
474# unlike decrypt case it does not pay off to parallelize enctransform
475sub enctransform()
476{ my ($t3,$r20,$r21)=($acc2,"%r8d","%r9d");
477
478$code.=<<___;
479	mov	\$0x80808080,$t0
480	mov	\$0x80808080,$t1
481	and	$s0,$t0
482	and	$s1,$t1
483	mov	$t0,$acc0
484	mov	$t1,$acc1
485	shr	\$7,$t0
486	lea	($s0,$s0),$r20
487	shr	\$7,$t1
488	lea	($s1,$s1),$r21
489	sub	$t0,$acc0
490	sub	$t1,$acc1
491	and	\$0xfefefefe,$r20
492	and	\$0xfefefefe,$r21
493	and	\$0x1b1b1b1b,$acc0
494	and	\$0x1b1b1b1b,$acc1
495	mov	$s0,$t0
496	mov	$s1,$t1
497	xor	$acc0,$r20
498	xor	$acc1,$r21
499
500	xor	$r20,$s0
501	xor	$r21,$s1
502	 mov	\$0x80808080,$t2
503	rol	\$24,$s0
504	 mov	\$0x80808080,$t3
505	rol	\$24,$s1
506	 and	$s2,$t2
507	 and	$s3,$t3
508	xor	$r20,$s0
509	xor	$r21,$s1
510	 mov	$t2,$acc0
511	ror	\$16,$t0
512	 mov	$t3,$acc1
513	ror	\$16,$t1
514	 lea	($s2,$s2),$r20
515	 shr	\$7,$t2
516	xor	$t0,$s0
517	 shr	\$7,$t3
518	xor	$t1,$s1
519	ror	\$8,$t0
520	 lea	($s3,$s3),$r21
521	ror	\$8,$t1
522	 sub	$t2,$acc0
523	 sub	$t3,$acc1
524	xor	$t0,$s0
525	xor	$t1,$s1
526
527	and	\$0xfefefefe,$r20
528	and	\$0xfefefefe,$r21
529	and	\$0x1b1b1b1b,$acc0
530	and	\$0x1b1b1b1b,$acc1
531	mov	$s2,$t2
532	mov	$s3,$t3
533	xor	$acc0,$r20
534	xor	$acc1,$r21
535
536	ror	\$16,$t2
537	xor	$r20,$s2
538	ror	\$16,$t3
539	xor	$r21,$s3
540	rol	\$24,$s2
541	mov	0($sbox),$acc0			# prefetch Te4
542	rol	\$24,$s3
543	xor	$r20,$s2
544	mov	64($sbox),$acc1
545	xor	$r21,$s3
546	mov	128($sbox),$r20
547	xor	$t2,$s2
548	ror	\$8,$t2
549	xor	$t3,$s3
550	ror	\$8,$t3
551	xor	$t2,$s2
552	mov	192($sbox),$r21
553	xor	$t3,$s3
554___
555}
556
557$code.=<<___;
558.type	_x86_64_AES_encrypt_compact,\@abi-omnipotent
559.align	16
560_x86_64_AES_encrypt_compact:
561.cfi_startproc
562	lea	128($sbox),$inp			# size optimization
563	mov	0-128($inp),$acc1		# prefetch Te4
564	mov	32-128($inp),$acc2
565	mov	64-128($inp),$t0
566	mov	96-128($inp),$t1
567	mov	128-128($inp),$acc1
568	mov	160-128($inp),$acc2
569	mov	192-128($inp),$t0
570	mov	224-128($inp),$t1
571	jmp	.Lenc_loop_compact
572.align	16
573.Lenc_loop_compact:
574		xor	0($key),$s0		# xor with key
575		xor	4($key),$s1
576		xor	8($key),$s2
577		xor	12($key),$s3
578		lea	16($key),$key
579___
580		&enccompactvert();
581$code.=<<___;
582		cmp	16(%rsp),$key
583		je	.Lenc_compact_done
584___
585		&enctransform();
586$code.=<<___;
587	jmp	.Lenc_loop_compact
588.align	16
589.Lenc_compact_done:
590	xor	0($key),$s0
591	xor	4($key),$s1
592	xor	8($key),$s2
593	xor	12($key),$s3
594	.byte	0xf3,0xc3			# rep ret
595.cfi_endproc
596.size	_x86_64_AES_encrypt_compact,.-_x86_64_AES_encrypt_compact
597___
598
599# void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
600$code.=<<___;
601.globl	AES_encrypt
602.type	AES_encrypt,\@function,3
603.align	16
604.globl	asm_AES_encrypt
605.hidden	asm_AES_encrypt
606asm_AES_encrypt:
607AES_encrypt:
608.cfi_startproc
609	endbranch
610	mov	%rsp,%rax
611.cfi_def_cfa_register	%rax
612	push	%rbx
613.cfi_push	%rbx
614	push	%rbp
615.cfi_push	%rbp
616	push	%r12
617.cfi_push	%r12
618	push	%r13
619.cfi_push	%r13
620	push	%r14
621.cfi_push	%r14
622	push	%r15
623.cfi_push	%r15
624
625	# allocate frame "above" key schedule
626	lea	-63(%rdx),%rcx	# %rdx is key argument
627	and	\$-64,%rsp
628	sub	%rsp,%rcx
629	neg	%rcx
630	and	\$0x3c0,%rcx
631	sub	%rcx,%rsp
632	sub	\$32,%rsp
633
634	mov	%rsi,16(%rsp)	# save out
635	mov	%rax,24(%rsp)	# save original stack pointer
636.cfi_cfa_expression	%rsp+24,deref,+8
637.Lenc_prologue:
638
639	mov	%rdx,$key
640	mov	240($key),$rnds	# load rounds
641
642	mov	0(%rdi),$s0	# load input vector
643	mov	4(%rdi),$s1
644	mov	8(%rdi),$s2
645	mov	12(%rdi),$s3
646
647	shl	\$4,$rnds
648	lea	($key,$rnds),%rbp
649	mov	$key,(%rsp)	# key schedule
650	mov	%rbp,8(%rsp)	# end of key schedule
651
652	# pick Te4 copy which can't "overlap" with stack frame or key schedule
653	lea	.LAES_Te+2048(%rip),$sbox
654	lea	768(%rsp),%rbp
655	sub	$sbox,%rbp
656	and	\$0x300,%rbp
657	lea	($sbox,%rbp),$sbox
658
659	call	_x86_64_AES_encrypt_compact
660
661	mov	16(%rsp),$out	# restore out
662	mov	24(%rsp),%rsi	# restore saved stack pointer
663.cfi_def_cfa	%rsi,8
664	mov	$s0,0($out)	# write output vector
665	mov	$s1,4($out)
666	mov	$s2,8($out)
667	mov	$s3,12($out)
668
669	mov	-48(%rsi),%r15
670.cfi_restore	%r15
671	mov	-40(%rsi),%r14
672.cfi_restore	%r14
673	mov	-32(%rsi),%r13
674.cfi_restore	%r13
675	mov	-24(%rsi),%r12
676.cfi_restore	%r12
677	mov	-16(%rsi),%rbp
678.cfi_restore	%rbp
679	mov	-8(%rsi),%rbx
680.cfi_restore	%rbx
681	lea	(%rsi),%rsp
682.cfi_def_cfa_register	%rsp
683.Lenc_epilogue:
684	ret
685.cfi_endproc
686.size	AES_encrypt,.-AES_encrypt
687___
688
689#------------------------------------------------------------------#
690
691sub decvert()
692{ my $t3="%r8d";	# zaps $inp!
693
694$code.=<<___;
695	# favor 3-way issue Opteron pipeline...
696	movzb	`&lo("$s0")`,$acc0
697	movzb	`&lo("$s1")`,$acc1
698	movzb	`&lo("$s2")`,$acc2
699	mov	0($sbox,$acc0,8),$t0
700	mov	0($sbox,$acc1,8),$t1
701	mov	0($sbox,$acc2,8),$t2
702
703	movzb	`&hi("$s3")`,$acc0
704	movzb	`&hi("$s0")`,$acc1
705	movzb	`&lo("$s3")`,$acc2
706	xor	3($sbox,$acc0,8),$t0
707	xor	3($sbox,$acc1,8),$t1
708	mov	0($sbox,$acc2,8),$t3
709
710	movzb	`&hi("$s1")`,$acc0
711	shr	\$16,$s0
712	movzb	`&hi("$s2")`,$acc2
713	xor	3($sbox,$acc0,8),$t2
714	shr	\$16,$s3
715	xor	3($sbox,$acc2,8),$t3
716
717	shr	\$16,$s1
718	lea	16($key),$key
719	shr	\$16,$s2
720
721	movzb	`&lo("$s2")`,$acc0
722	movzb	`&lo("$s3")`,$acc1
723	movzb	`&lo("$s0")`,$acc2
724	xor	2($sbox,$acc0,8),$t0
725	xor	2($sbox,$acc1,8),$t1
726	xor	2($sbox,$acc2,8),$t2
727
728	movzb	`&hi("$s1")`,$acc0
729	movzb	`&hi("$s2")`,$acc1
730	movzb	`&lo("$s1")`,$acc2
731	xor	1($sbox,$acc0,8),$t0
732	xor	1($sbox,$acc1,8),$t1
733	xor	2($sbox,$acc2,8),$t3
734
735	movzb	`&hi("$s3")`,$acc0
736	mov	12($key),$s3
737	movzb	`&hi("$s0")`,$acc2
738	xor	1($sbox,$acc0,8),$t2
739	mov	0($key),$s0
740	xor	1($sbox,$acc2,8),$t3
741
742	xor	$t0,$s0
743	mov	4($key),$s1
744	mov	8($key),$s2
745	xor	$t2,$s2
746	xor	$t1,$s1
747	xor	$t3,$s3
748___
749}
750
751sub declastvert()
752{ my $t3="%r8d";	# zaps $inp!
753
754$code.=<<___;
755	lea	2048($sbox),$sbox	# size optimization
756	movzb	`&lo("$s0")`,$acc0
757	movzb	`&lo("$s1")`,$acc1
758	movzb	`&lo("$s2")`,$acc2
759	movzb	($sbox,$acc0,1),$t0
760	movzb	($sbox,$acc1,1),$t1
761	movzb	($sbox,$acc2,1),$t2
762
763	movzb	`&lo("$s3")`,$acc0
764	movzb	`&hi("$s3")`,$acc1
765	movzb	`&hi("$s0")`,$acc2
766	movzb	($sbox,$acc0,1),$t3
767	movzb	($sbox,$acc1,1),$acc1	#$t0
768	movzb	($sbox,$acc2,1),$acc2	#$t1
769
770	shl	\$8,$acc1
771	shl	\$8,$acc2
772
773	xor	$acc1,$t0
774	xor	$acc2,$t1
775	shr	\$16,$s3
776
777	movzb	`&hi("$s1")`,$acc0
778	movzb	`&hi("$s2")`,$acc1
779	shr	\$16,$s0
780	movzb	($sbox,$acc0,1),$acc0	#$t2
781	movzb	($sbox,$acc1,1),$acc1	#$t3
782
783	shl	\$8,$acc0
784	shl	\$8,$acc1
785	shr	\$16,$s1
786	xor	$acc0,$t2
787	xor	$acc1,$t3
788	shr	\$16,$s2
789
790	movzb	`&lo("$s2")`,$acc0
791	movzb	`&lo("$s3")`,$acc1
792	movzb	`&lo("$s0")`,$acc2
793	movzb	($sbox,$acc0,1),$acc0	#$t0
794	movzb	($sbox,$acc1,1),$acc1	#$t1
795	movzb	($sbox,$acc2,1),$acc2	#$t2
796
797	shl	\$16,$acc0
798	shl	\$16,$acc1
799	shl	\$16,$acc2
800
801	xor	$acc0,$t0
802	xor	$acc1,$t1
803	xor	$acc2,$t2
804
805	movzb	`&lo("$s1")`,$acc0
806	movzb	`&hi("$s1")`,$acc1
807	movzb	`&hi("$s2")`,$acc2
808	movzb	($sbox,$acc0,1),$acc0	#$t3
809	movzb	($sbox,$acc1,1),$acc1	#$t0
810	movzb	($sbox,$acc2,1),$acc2	#$t1
811
812	shl	\$16,$acc0
813	shl	\$24,$acc1
814	shl	\$24,$acc2
815
816	xor	$acc0,$t3
817	xor	$acc1,$t0
818	xor	$acc2,$t1
819
820	movzb	`&hi("$s3")`,$acc0
821	movzb	`&hi("$s0")`,$acc1
822	mov	16+12($key),$s3
823	movzb	($sbox,$acc0,1),$acc0	#$t2
824	movzb	($sbox,$acc1,1),$acc1	#$t3
825	mov	16+0($key),$s0
826
827	shl	\$24,$acc0
828	shl	\$24,$acc1
829
830	xor	$acc0,$t2
831	xor	$acc1,$t3
832
833	mov	16+4($key),$s1
834	mov	16+8($key),$s2
835	lea	-2048($sbox),$sbox
836	xor	$t0,$s0
837	xor	$t1,$s1
838	xor	$t2,$s2
839	xor	$t3,$s3
840___
841}
842
843sub decstep()
844{ my ($i,@s) = @_;
845  my $tmp0=$acc0;
846  my $tmp1=$acc1;
847  my $tmp2=$acc2;
848  my $out=($t0,$t1,$t2,$s[0])[$i];
849
850	$code.="	mov	$s[0],$out\n"		if ($i!=3);
851			$tmp1=$s[2]			if ($i==3);
852	$code.="	mov	$s[2],$tmp1\n"		if ($i!=3);
853	$code.="	and	\$0xFF,$out\n";
854
855	$code.="	mov	0($sbox,$out,8),$out\n";
856	$code.="	shr	\$16,$tmp1\n";
857			$tmp2=$s[3]			if ($i==3);
858	$code.="	mov	$s[3],$tmp2\n"		if ($i!=3);
859
860			$tmp0=$s[1]			if ($i==3);
861	$code.="	movzb	".&hi($s[1]).",$tmp0\n";
862	$code.="	and	\$0xFF,$tmp1\n";
863	$code.="	shr	\$24,$tmp2\n";
864
865	$code.="	xor	3($sbox,$tmp0,8),$out\n";
866	$code.="	xor	2($sbox,$tmp1,8),$out\n";
867	$code.="	xor	1($sbox,$tmp2,8),$out\n";
868
869	$code.="	mov	$t2,$s[1]\n"		if ($i==3);
870	$code.="	mov	$t1,$s[2]\n"		if ($i==3);
871	$code.="	mov	$t0,$s[3]\n"		if ($i==3);
872	$code.="\n";
873}
874
875sub declast()
876{ my ($i,@s)=@_;
877  my $tmp0=$acc0;
878  my $tmp1=$acc1;
879  my $tmp2=$acc2;
880  my $out=($t0,$t1,$t2,$s[0])[$i];
881
882	$code.="	mov	$s[0],$out\n"		if ($i!=3);
883			$tmp1=$s[2]			if ($i==3);
884	$code.="	mov	$s[2],$tmp1\n"		if ($i!=3);
885	$code.="	and	\$0xFF,$out\n";
886
887	$code.="	movzb	2048($sbox,$out,1),$out\n";
888	$code.="	shr	\$16,$tmp1\n";
889			$tmp2=$s[3]			if ($i==3);
890	$code.="	mov	$s[3],$tmp2\n"		if ($i!=3);
891
892			$tmp0=$s[1]			if ($i==3);
893	$code.="	movzb	".&hi($s[1]).",$tmp0\n";
894	$code.="	and	\$0xFF,$tmp1\n";
895	$code.="	shr	\$24,$tmp2\n";
896
897	$code.="	movzb	2048($sbox,$tmp0,1),$tmp0\n";
898	$code.="	movzb	2048($sbox,$tmp1,1),$tmp1\n";
899	$code.="	movzb	2048($sbox,$tmp2,1),$tmp2\n";
900
901	$code.="	shl	\$8,$tmp0\n";
902	$code.="	shl	\$16,$tmp1\n";
903	$code.="	shl	\$24,$tmp2\n";
904
905	$code.="	xor	$tmp0,$out\n";
906	$code.="	mov	$t2,$s[1]\n"		if ($i==3);
907	$code.="	xor	$tmp1,$out\n";
908	$code.="	mov	$t1,$s[2]\n"		if ($i==3);
909	$code.="	xor	$tmp2,$out\n";
910	$code.="	mov	$t0,$s[3]\n"		if ($i==3);
911	$code.="\n";
912}
913
914$code.=<<___;
915.type	_x86_64_AES_decrypt,\@abi-omnipotent
916.align	16
917_x86_64_AES_decrypt:
918.cfi_startproc
919	xor	0($key),$s0			# xor with key
920	xor	4($key),$s1
921	xor	8($key),$s2
922	xor	12($key),$s3
923
924	mov	240($key),$rnds			# load key->rounds
925	sub	\$1,$rnds
926	jmp	.Ldec_loop
927.align	16
928.Ldec_loop:
929___
930	if ($verticalspin) { &decvert(); }
931	else {	&decstep(0,$s0,$s3,$s2,$s1);
932		&decstep(1,$s1,$s0,$s3,$s2);
933		&decstep(2,$s2,$s1,$s0,$s3);
934		&decstep(3,$s3,$s2,$s1,$s0);
935		$code.=<<___;
936		lea	16($key),$key
937		xor	0($key),$s0			# xor with key
938		xor	4($key),$s1
939		xor	8($key),$s2
940		xor	12($key),$s3
941___
942	}
943$code.=<<___;
944	sub	\$1,$rnds
945	jnz	.Ldec_loop
946___
947	if ($verticalspin) { &declastvert(); }
948	else {	&declast(0,$s0,$s3,$s2,$s1);
949		&declast(1,$s1,$s0,$s3,$s2);
950		&declast(2,$s2,$s1,$s0,$s3);
951		&declast(3,$s3,$s2,$s1,$s0);
952		$code.=<<___;
953		xor	16+0($key),$s0			# xor with key
954		xor	16+4($key),$s1
955		xor	16+8($key),$s2
956		xor	16+12($key),$s3
957___
958	}
959$code.=<<___;
960	.byte	0xf3,0xc3			# rep ret
961.cfi_endproc
962.size	_x86_64_AES_decrypt,.-_x86_64_AES_decrypt
963___
964
965sub deccompactvert()
966{ my ($t3,$t4,$t5)=("%r8d","%r9d","%r13d");
967
968$code.=<<___;
969	movzb	`&lo("$s0")`,$t0
970	movzb	`&lo("$s1")`,$t1
971	movzb	`&lo("$s2")`,$t2
972	movzb	`&lo("$s3")`,$t3
973	movzb	`&hi("$s3")`,$acc0
974	movzb	`&hi("$s0")`,$acc1
975	shr	\$16,$s3
976	movzb	`&hi("$s1")`,$acc2
977	movzb	($sbox,$t0,1),$t0
978	movzb	($sbox,$t1,1),$t1
979	movzb	($sbox,$t2,1),$t2
980	movzb	($sbox,$t3,1),$t3
981
982	movzb	($sbox,$acc0,1),$t4	#$t0
983	movzb	`&hi("$s2")`,$acc0
984	movzb	($sbox,$acc1,1),$t5	#$t1
985	movzb	($sbox,$acc2,1),$acc2	#$t2
986	movzb	($sbox,$acc0,1),$acc0	#$t3
987
988	shr	\$16,$s2
989	shl	\$8,$t5
990	shl	\$8,$t4
991	movzb	`&lo("$s2")`,$acc1
992	shr	\$16,$s0
993	xor	$t4,$t0
994	shr	\$16,$s1
995	movzb	`&lo("$s3")`,$t4
996
997	shl	\$8,$acc2
998	xor	$t5,$t1
999	shl	\$8,$acc0
1000	movzb	`&lo("$s0")`,$t5
1001	movzb	($sbox,$acc1,1),$acc1	#$t0
1002	xor	$acc2,$t2
1003	movzb	`&lo("$s1")`,$acc2
1004
1005	shl	\$16,$acc1
1006	xor	$acc0,$t3
1007	movzb	($sbox,$t4,1),$t4	#$t1
1008	movzb	`&hi("$s1")`,$acc0
1009	movzb	($sbox,$acc2,1),$acc2	#$t3
1010	xor	$acc1,$t0
1011	movzb	($sbox,$t5,1),$t5	#$t2
1012	movzb	`&hi("$s2")`,$acc1
1013
1014	shl	\$16,$acc2
1015	shl	\$16,$t4
1016	shl	\$16,$t5
1017	xor	$acc2,$t3
1018	movzb	`&hi("$s3")`,$acc2
1019	xor	$t4,$t1
1020	shr	\$8,$s0
1021	xor	$t5,$t2
1022
1023	movzb	($sbox,$acc0,1),$acc0	#$t0
1024	movzb	($sbox,$acc1,1),$s1	#$t1
1025	movzb	($sbox,$acc2,1),$s2	#$t2
1026	movzb	($sbox,$s0,1),$s3	#$t3
1027
1028	mov	$t0,$s0
1029	shl	\$24,$acc0
1030	shl	\$24,$s1
1031	shl	\$24,$s2
1032	xor	$acc0,$s0
1033	shl	\$24,$s3
1034	xor	$t1,$s1
1035	xor	$t2,$s2
1036	xor	$t3,$s3
1037___
1038}
1039
1040# parallelized version! input is pair of 64-bit values: %rax=s1.s0
1041# and %rcx=s3.s2, output is four 32-bit values in %eax=s0, %ebx=s1,
1042# %ecx=s2 and %edx=s3.
1043sub dectransform()
1044{ my ($tp10,$tp20,$tp40,$tp80,$acc0)=("%rax","%r8", "%r9", "%r10","%rbx");
1045  my ($tp18,$tp28,$tp48,$tp88,$acc8)=("%rcx","%r11","%r12","%r13","%rdx");
1046  my $prefetch = shift;
1047
1048$code.=<<___;
1049	mov	$mask80,$tp40
1050	mov	$mask80,$tp48
1051	and	$tp10,$tp40
1052	and	$tp18,$tp48
1053	mov	$tp40,$acc0
1054	mov	$tp48,$acc8
1055	shr	\$7,$tp40
1056	lea	($tp10,$tp10),$tp20
1057	shr	\$7,$tp48
1058	lea	($tp18,$tp18),$tp28
1059	sub	$tp40,$acc0
1060	sub	$tp48,$acc8
1061	and	$maskfe,$tp20
1062	and	$maskfe,$tp28
1063	and	$mask1b,$acc0
1064	and	$mask1b,$acc8
1065	xor	$acc0,$tp20
1066	xor	$acc8,$tp28
1067	mov	$mask80,$tp80
1068	mov	$mask80,$tp88
1069
1070	and	$tp20,$tp80
1071	and	$tp28,$tp88
1072	mov	$tp80,$acc0
1073	mov	$tp88,$acc8
1074	shr	\$7,$tp80
1075	lea	($tp20,$tp20),$tp40
1076	shr	\$7,$tp88
1077	lea	($tp28,$tp28),$tp48
1078	sub	$tp80,$acc0
1079	sub	$tp88,$acc8
1080	and	$maskfe,$tp40
1081	and	$maskfe,$tp48
1082	and	$mask1b,$acc0
1083	and	$mask1b,$acc8
1084	xor	$acc0,$tp40
1085	xor	$acc8,$tp48
1086	mov	$mask80,$tp80
1087	mov	$mask80,$tp88
1088
1089	and	$tp40,$tp80
1090	and	$tp48,$tp88
1091	mov	$tp80,$acc0
1092	mov	$tp88,$acc8
1093	shr	\$7,$tp80
1094	 xor	$tp10,$tp20		# tp2^=tp1
1095	shr	\$7,$tp88
1096	 xor	$tp18,$tp28		# tp2^=tp1
1097	sub	$tp80,$acc0
1098	sub	$tp88,$acc8
1099	lea	($tp40,$tp40),$tp80
1100	lea	($tp48,$tp48),$tp88
1101	 xor	$tp10,$tp40		# tp4^=tp1
1102	 xor	$tp18,$tp48		# tp4^=tp1
1103	and	$maskfe,$tp80
1104	and	$maskfe,$tp88
1105	and	$mask1b,$acc0
1106	and	$mask1b,$acc8
1107	xor	$acc0,$tp80
1108	xor	$acc8,$tp88
1109
1110	xor	$tp80,$tp10		# tp1^=tp8
1111	xor	$tp88,$tp18		# tp1^=tp8
1112	xor	$tp80,$tp20		# tp2^tp1^=tp8
1113	xor	$tp88,$tp28		# tp2^tp1^=tp8
1114	mov	$tp10,$acc0
1115	mov	$tp18,$acc8
1116	xor	$tp80,$tp40		# tp4^tp1^=tp8
1117	shr	\$32,$acc0
1118	xor	$tp88,$tp48		# tp4^tp1^=tp8
1119	shr	\$32,$acc8
1120	xor	$tp20,$tp80		# tp8^=tp8^tp2^tp1=tp2^tp1
1121	rol	\$8,`&LO("$tp10")`	# ROTATE(tp1^tp8,8)
1122	xor	$tp28,$tp88		# tp8^=tp8^tp2^tp1=tp2^tp1
1123	rol	\$8,`&LO("$tp18")`	# ROTATE(tp1^tp8,8)
1124	xor	$tp40,$tp80		# tp2^tp1^=tp8^tp4^tp1=tp8^tp4^tp2
1125	rol	\$8,`&LO("$acc0")`	# ROTATE(tp1^tp8,8)
1126	xor	$tp48,$tp88		# tp2^tp1^=tp8^tp4^tp1=tp8^tp4^tp2
1127
1128	rol	\$8,`&LO("$acc8")`	# ROTATE(tp1^tp8,8)
1129	xor	`&LO("$tp80")`,`&LO("$tp10")`
1130	shr	\$32,$tp80
1131	xor	`&LO("$tp88")`,`&LO("$tp18")`
1132	shr	\$32,$tp88
1133	xor	`&LO("$tp80")`,`&LO("$acc0")`
1134	xor	`&LO("$tp88")`,`&LO("$acc8")`
1135
1136	mov	$tp20,$tp80
1137	rol	\$24,`&LO("$tp20")`	# ROTATE(tp2^tp1^tp8,24)
1138	mov	$tp28,$tp88
1139	rol	\$24,`&LO("$tp28")`	# ROTATE(tp2^tp1^tp8,24)
1140	shr	\$32,$tp80
1141	xor	`&LO("$tp20")`,`&LO("$tp10")`
1142	shr	\$32,$tp88
1143	xor	`&LO("$tp28")`,`&LO("$tp18")`
1144	rol	\$24,`&LO("$tp80")`	# ROTATE(tp2^tp1^tp8,24)
1145	mov	$tp40,$tp20
1146	rol	\$24,`&LO("$tp88")`	# ROTATE(tp2^tp1^tp8,24)
1147	mov	$tp48,$tp28
1148	shr	\$32,$tp20
1149	xor	`&LO("$tp80")`,`&LO("$acc0")`
1150	shr	\$32,$tp28
1151	xor	`&LO("$tp88")`,`&LO("$acc8")`
1152
1153	`"mov	0($sbox),$mask80"	if ($prefetch)`
1154	rol	\$16,`&LO("$tp40")`	# ROTATE(tp4^tp1^tp8,16)
1155	`"mov	64($sbox),$maskfe"	if ($prefetch)`
1156	rol	\$16,`&LO("$tp48")`	# ROTATE(tp4^tp1^tp8,16)
1157	`"mov	128($sbox),$mask1b"	if ($prefetch)`
1158	rol	\$16,`&LO("$tp20")`	# ROTATE(tp4^tp1^tp8,16)
1159	`"mov	192($sbox),$tp80"	if ($prefetch)`
1160	xor	`&LO("$tp40")`,`&LO("$tp10")`
1161	rol	\$16,`&LO("$tp28")`	# ROTATE(tp4^tp1^tp8,16)
1162	xor	`&LO("$tp48")`,`&LO("$tp18")`
1163	`"mov	256($sbox),$tp88"	if ($prefetch)`
1164	xor	`&LO("$tp20")`,`&LO("$acc0")`
1165	xor	`&LO("$tp28")`,`&LO("$acc8")`
1166___
1167}
1168
1169$code.=<<___;
1170.type	_x86_64_AES_decrypt_compact,\@abi-omnipotent
1171.align	16
1172_x86_64_AES_decrypt_compact:
1173.cfi_startproc
1174	lea	128($sbox),$inp			# size optimization
1175	mov	0-128($inp),$acc1		# prefetch Td4
1176	mov	32-128($inp),$acc2
1177	mov	64-128($inp),$t0
1178	mov	96-128($inp),$t1
1179	mov	128-128($inp),$acc1
1180	mov	160-128($inp),$acc2
1181	mov	192-128($inp),$t0
1182	mov	224-128($inp),$t1
1183	jmp	.Ldec_loop_compact
1184
1185.align	16
1186.Ldec_loop_compact:
1187		xor	0($key),$s0		# xor with key
1188		xor	4($key),$s1
1189		xor	8($key),$s2
1190		xor	12($key),$s3
1191		lea	16($key),$key
1192___
1193		&deccompactvert();
1194$code.=<<___;
1195		cmp	16(%rsp),$key
1196		je	.Ldec_compact_done
1197
1198		mov	256+0($sbox),$mask80
1199		shl	\$32,%rbx
1200		shl	\$32,%rdx
1201		mov	256+8($sbox),$maskfe
1202		or	%rbx,%rax
1203		or	%rdx,%rcx
1204		mov	256+16($sbox),$mask1b
1205___
1206		&dectransform(1);
1207$code.=<<___;
1208	jmp	.Ldec_loop_compact
1209.align	16
1210.Ldec_compact_done:
1211	xor	0($key),$s0
1212	xor	4($key),$s1
1213	xor	8($key),$s2
1214	xor	12($key),$s3
1215	.byte	0xf3,0xc3			# rep ret
1216.cfi_endproc
1217.size	_x86_64_AES_decrypt_compact,.-_x86_64_AES_decrypt_compact
1218___
1219
1220# void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
1221$code.=<<___;
1222.globl	AES_decrypt
1223.type	AES_decrypt,\@function,3
1224.align	16
1225.globl	asm_AES_decrypt
1226.hidden	asm_AES_decrypt
1227asm_AES_decrypt:
1228AES_decrypt:
1229.cfi_startproc
1230	endbranch
1231	mov	%rsp,%rax
1232.cfi_def_cfa_register	%rax
1233	push	%rbx
1234.cfi_push	%rbx
1235	push	%rbp
1236.cfi_push	%rbp
1237	push	%r12
1238.cfi_push	%r12
1239	push	%r13
1240.cfi_push	%r13
1241	push	%r14
1242.cfi_push	%r14
1243	push	%r15
1244.cfi_push	%r15
1245
1246	# allocate frame "above" key schedule
1247	lea	-63(%rdx),%rcx	# %rdx is key argument
1248	and	\$-64,%rsp
1249	sub	%rsp,%rcx
1250	neg	%rcx
1251	and	\$0x3c0,%rcx
1252	sub	%rcx,%rsp
1253	sub	\$32,%rsp
1254
1255	mov	%rsi,16(%rsp)	# save out
1256	mov	%rax,24(%rsp)	# save original stack pointer
1257.cfi_cfa_expression	%rsp+24,deref,+8
1258.Ldec_prologue:
1259
1260	mov	%rdx,$key
1261	mov	240($key),$rnds	# load rounds
1262
1263	mov	0(%rdi),$s0	# load input vector
1264	mov	4(%rdi),$s1
1265	mov	8(%rdi),$s2
1266	mov	12(%rdi),$s3
1267
1268	shl	\$4,$rnds
1269	lea	($key,$rnds),%rbp
1270	mov	$key,(%rsp)	# key schedule
1271	mov	%rbp,8(%rsp)	# end of key schedule
1272
1273	# pick Td4 copy which can't "overlap" with stack frame or key schedule
1274	lea	.LAES_Td+2048(%rip),$sbox
1275	lea	768(%rsp),%rbp
1276	sub	$sbox,%rbp
1277	and	\$0x300,%rbp
1278	lea	($sbox,%rbp),$sbox
1279	shr	\$3,%rbp	# recall "magic" constants!
1280	add	%rbp,$sbox
1281
1282	call	_x86_64_AES_decrypt_compact
1283
1284	mov	16(%rsp),$out	# restore out
1285	mov	24(%rsp),%rsi	# restore saved stack pointer
1286.cfi_def_cfa	%rsi,8
1287	mov	$s0,0($out)	# write output vector
1288	mov	$s1,4($out)
1289	mov	$s2,8($out)
1290	mov	$s3,12($out)
1291
1292	mov	-48(%rsi),%r15
1293.cfi_restore	%r15
1294	mov	-40(%rsi),%r14
1295.cfi_restore	%r14
1296	mov	-32(%rsi),%r13
1297.cfi_restore	%r13
1298	mov	-24(%rsi),%r12
1299.cfi_restore	%r12
1300	mov	-16(%rsi),%rbp
1301.cfi_restore	%rbp
1302	mov	-8(%rsi),%rbx
1303.cfi_restore	%rbx
1304	lea	(%rsi),%rsp
1305.cfi_def_cfa_register	%rsp
1306.Ldec_epilogue:
1307	ret
1308.cfi_endproc
1309.size	AES_decrypt,.-AES_decrypt
1310___
1311#------------------------------------------------------------------#
1312
1313sub enckey()
1314{
1315$code.=<<___;
1316	movz	%dl,%esi		# rk[i]>>0
1317	movzb	-128(%rbp,%rsi),%ebx
1318	movz	%dh,%esi		# rk[i]>>8
1319	shl	\$24,%ebx
1320	xor	%ebx,%eax
1321
1322	movzb	-128(%rbp,%rsi),%ebx
1323	shr	\$16,%edx
1324	movz	%dl,%esi		# rk[i]>>16
1325	xor	%ebx,%eax
1326
1327	movzb	-128(%rbp,%rsi),%ebx
1328	movz	%dh,%esi		# rk[i]>>24
1329	shl	\$8,%ebx
1330	xor	%ebx,%eax
1331
1332	movzb	-128(%rbp,%rsi),%ebx
1333	shl	\$16,%ebx
1334	xor	%ebx,%eax
1335
1336	xor	1024-128(%rbp,%rcx,4),%eax		# rcon
1337___
1338}
1339
1340# int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
1341#                        AES_KEY *key)
1342$code.=<<___;
1343.globl	AES_set_encrypt_key
1344.type	AES_set_encrypt_key,\@function,3
1345.align	16
1346AES_set_encrypt_key:
1347.cfi_startproc
1348	endbranch
1349	push	%rbx
1350.cfi_push	%rbx
1351	push	%rbp
1352.cfi_push	%rbp
1353	push	%r12			# redundant, but allows to share
1354.cfi_push	%r12
1355	push	%r13			# exception handler...
1356.cfi_push	%r13
1357	push	%r14
1358.cfi_push	%r14
1359	push	%r15
1360.cfi_push	%r15
1361	sub	\$8,%rsp
1362.cfi_adjust_cfa_offset	8
1363.Lenc_key_prologue:
1364
1365	call	_x86_64_AES_set_encrypt_key
1366
1367	mov	40(%rsp),%rbp
1368.cfi_restore	%rbp
1369	mov	48(%rsp),%rbx
1370.cfi_restore	%rbx
1371	add	\$56,%rsp
1372.cfi_adjust_cfa_offset	-56
1373.Lenc_key_epilogue:
1374	ret
1375.cfi_endproc
1376.size	AES_set_encrypt_key,.-AES_set_encrypt_key
1377
1378.type	_x86_64_AES_set_encrypt_key,\@abi-omnipotent
1379.align	16
1380_x86_64_AES_set_encrypt_key:
1381.cfi_startproc
1382	mov	%esi,%ecx			# %ecx=bits
1383	mov	%rdi,%rsi			# %rsi=userKey
1384	mov	%rdx,%rdi			# %rdi=key
1385
1386	test	\$-1,%rsi
1387	jz	.Lbadpointer
1388	test	\$-1,%rdi
1389	jz	.Lbadpointer
1390
1391	lea	.LAES_Te(%rip),%rbp
1392	lea	2048+128(%rbp),%rbp
1393
1394	# prefetch Te4
1395	mov	0-128(%rbp),%eax
1396	mov	32-128(%rbp),%ebx
1397	mov	64-128(%rbp),%r8d
1398	mov	96-128(%rbp),%edx
1399	mov	128-128(%rbp),%eax
1400	mov	160-128(%rbp),%ebx
1401	mov	192-128(%rbp),%r8d
1402	mov	224-128(%rbp),%edx
1403
1404	cmp	\$128,%ecx
1405	je	.L10rounds
1406	cmp	\$192,%ecx
1407	je	.L12rounds
1408	cmp	\$256,%ecx
1409	je	.L14rounds
1410	mov	\$-2,%rax			# invalid number of bits
1411	jmp	.Lexit
1412
1413.L10rounds:
1414	mov	0(%rsi),%rax			# copy first 4 dwords
1415	mov	8(%rsi),%rdx
1416	mov	%rax,0(%rdi)
1417	mov	%rdx,8(%rdi)
1418
1419	shr	\$32,%rdx
1420	xor	%ecx,%ecx
1421	jmp	.L10shortcut
1422.align	4
1423.L10loop:
1424		mov	0(%rdi),%eax			# rk[0]
1425		mov	12(%rdi),%edx			# rk[3]
1426.L10shortcut:
1427___
1428		&enckey	();
1429$code.=<<___;
1430		mov	%eax,16(%rdi)			# rk[4]
1431		xor	4(%rdi),%eax
1432		mov	%eax,20(%rdi)			# rk[5]
1433		xor	8(%rdi),%eax
1434		mov	%eax,24(%rdi)			# rk[6]
1435		xor	12(%rdi),%eax
1436		mov	%eax,28(%rdi)			# rk[7]
1437		add	\$1,%ecx
1438		lea	16(%rdi),%rdi
1439		cmp	\$10,%ecx
1440	jl	.L10loop
1441
1442	movl	\$10,80(%rdi)			# setup number of rounds
1443	xor	%rax,%rax
1444	jmp	.Lexit
1445
1446.L12rounds:
1447	mov	0(%rsi),%rax			# copy first 6 dwords
1448	mov	8(%rsi),%rbx
1449	mov	16(%rsi),%rdx
1450	mov	%rax,0(%rdi)
1451	mov	%rbx,8(%rdi)
1452	mov	%rdx,16(%rdi)
1453
1454	shr	\$32,%rdx
1455	xor	%ecx,%ecx
1456	jmp	.L12shortcut
1457.align	4
1458.L12loop:
1459		mov	0(%rdi),%eax			# rk[0]
1460		mov	20(%rdi),%edx			# rk[5]
1461.L12shortcut:
1462___
1463		&enckey	();
1464$code.=<<___;
1465		mov	%eax,24(%rdi)			# rk[6]
1466		xor	4(%rdi),%eax
1467		mov	%eax,28(%rdi)			# rk[7]
1468		xor	8(%rdi),%eax
1469		mov	%eax,32(%rdi)			# rk[8]
1470		xor	12(%rdi),%eax
1471		mov	%eax,36(%rdi)			# rk[9]
1472
1473		cmp	\$7,%ecx
1474		je	.L12break
1475		add	\$1,%ecx
1476
1477		xor	16(%rdi),%eax
1478		mov	%eax,40(%rdi)			# rk[10]
1479		xor	20(%rdi),%eax
1480		mov	%eax,44(%rdi)			# rk[11]
1481
1482		lea	24(%rdi),%rdi
1483	jmp	.L12loop
1484.L12break:
1485	movl	\$12,72(%rdi)		# setup number of rounds
1486	xor	%rax,%rax
1487	jmp	.Lexit
1488
1489.L14rounds:
1490	mov	0(%rsi),%rax			# copy first 8 dwords
1491	mov	8(%rsi),%rbx
1492	mov	16(%rsi),%rcx
1493	mov	24(%rsi),%rdx
1494	mov	%rax,0(%rdi)
1495	mov	%rbx,8(%rdi)
1496	mov	%rcx,16(%rdi)
1497	mov	%rdx,24(%rdi)
1498
1499	shr	\$32,%rdx
1500	xor	%ecx,%ecx
1501	jmp	.L14shortcut
1502.align	4
1503.L14loop:
1504		mov	0(%rdi),%eax			# rk[0]
1505		mov	28(%rdi),%edx			# rk[4]
1506.L14shortcut:
1507___
1508		&enckey	();
1509$code.=<<___;
1510		mov	%eax,32(%rdi)			# rk[8]
1511		xor	4(%rdi),%eax
1512		mov	%eax,36(%rdi)			# rk[9]
1513		xor	8(%rdi),%eax
1514		mov	%eax,40(%rdi)			# rk[10]
1515		xor	12(%rdi),%eax
1516		mov	%eax,44(%rdi)			# rk[11]
1517
1518		cmp	\$6,%ecx
1519		je	.L14break
1520		add	\$1,%ecx
1521
1522		mov	%eax,%edx
1523		mov	16(%rdi),%eax			# rk[4]
1524		movz	%dl,%esi			# rk[11]>>0
1525		movzb	-128(%rbp,%rsi),%ebx
1526		movz	%dh,%esi			# rk[11]>>8
1527		xor	%ebx,%eax
1528
1529		movzb	-128(%rbp,%rsi),%ebx
1530		shr	\$16,%edx
1531		shl	\$8,%ebx
1532		movz	%dl,%esi			# rk[11]>>16
1533		xor	%ebx,%eax
1534
1535		movzb	-128(%rbp,%rsi),%ebx
1536		movz	%dh,%esi			# rk[11]>>24
1537		shl	\$16,%ebx
1538		xor	%ebx,%eax
1539
1540		movzb	-128(%rbp,%rsi),%ebx
1541		shl	\$24,%ebx
1542		xor	%ebx,%eax
1543
1544		mov	%eax,48(%rdi)			# rk[12]
1545		xor	20(%rdi),%eax
1546		mov	%eax,52(%rdi)			# rk[13]
1547		xor	24(%rdi),%eax
1548		mov	%eax,56(%rdi)			# rk[14]
1549		xor	28(%rdi),%eax
1550		mov	%eax,60(%rdi)			# rk[15]
1551
1552		lea	32(%rdi),%rdi
1553	jmp	.L14loop
1554.L14break:
1555	movl	\$14,48(%rdi)		# setup number of rounds
1556	xor	%rax,%rax
1557	jmp	.Lexit
1558
1559.Lbadpointer:
1560	mov	\$-1,%rax
1561.Lexit:
1562	.byte	0xf3,0xc3			# rep ret
1563.cfi_endproc
1564.size	_x86_64_AES_set_encrypt_key,.-_x86_64_AES_set_encrypt_key
1565___
1566
1567sub deckey_ref()
1568{ my ($i,$ptr,$te,$td) = @_;
1569  my ($tp1,$tp2,$tp4,$tp8,$acc)=("%eax","%ebx","%edi","%edx","%r8d");
1570$code.=<<___;
1571	mov	$i($ptr),$tp1
1572	mov	$tp1,$acc
1573	and	\$0x80808080,$acc
1574	mov	$acc,$tp4
1575	shr	\$7,$tp4
1576	lea	0($tp1,$tp1),$tp2
1577	sub	$tp4,$acc
1578	and	\$0xfefefefe,$tp2
1579	and	\$0x1b1b1b1b,$acc
1580	xor	$tp2,$acc
1581	mov	$acc,$tp2
1582
1583	and	\$0x80808080,$acc
1584	mov	$acc,$tp8
1585	shr	\$7,$tp8
1586	lea	0($tp2,$tp2),$tp4
1587	sub	$tp8,$acc
1588	and	\$0xfefefefe,$tp4
1589	and	\$0x1b1b1b1b,$acc
1590	 xor	$tp1,$tp2		# tp2^tp1
1591	xor	$tp4,$acc
1592	mov	$acc,$tp4
1593
1594	and	\$0x80808080,$acc
1595	mov	$acc,$tp8
1596	shr	\$7,$tp8
1597	sub	$tp8,$acc
1598	lea	0($tp4,$tp4),$tp8
1599	 xor	$tp1,$tp4		# tp4^tp1
1600	and	\$0xfefefefe,$tp8
1601	and	\$0x1b1b1b1b,$acc
1602	xor	$acc,$tp8
1603
1604	xor	$tp8,$tp1		# tp1^tp8
1605	rol	\$8,$tp1		# ROTATE(tp1^tp8,8)
1606	xor	$tp8,$tp2		# tp2^tp1^tp8
1607	xor	$tp8,$tp4		# tp4^tp1^tp8
1608	xor	$tp2,$tp8
1609	xor	$tp4,$tp8		# tp8^(tp8^tp4^tp1)^(tp8^tp2^tp1)=tp8^tp4^tp2
1610
1611	xor	$tp8,$tp1
1612	rol	\$24,$tp2		# ROTATE(tp2^tp1^tp8,24)
1613	xor	$tp2,$tp1
1614	rol	\$16,$tp4		# ROTATE(tp4^tp1^tp8,16)
1615	xor	$tp4,$tp1
1616
1617	mov	$tp1,$i($ptr)
1618___
1619}
1620
1621# int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
1622#                        AES_KEY *key)
1623$code.=<<___;
1624.globl	AES_set_decrypt_key
1625.type	AES_set_decrypt_key,\@function,3
1626.align	16
1627AES_set_decrypt_key:
1628.cfi_startproc
1629	endbranch
1630	push	%rbx
1631.cfi_push	%rbx
1632	push	%rbp
1633.cfi_push	%rbp
1634	push	%r12
1635.cfi_push	%r12
1636	push	%r13
1637.cfi_push	%r13
1638	push	%r14
1639.cfi_push	%r14
1640	push	%r15
1641.cfi_push	%r15
1642	push	%rdx			# save key schedule
1643.cfi_adjust_cfa_offset	8
1644.Ldec_key_prologue:
1645
1646	call	_x86_64_AES_set_encrypt_key
1647	mov	(%rsp),%r8		# restore key schedule
1648	cmp	\$0,%eax
1649	jne	.Labort
1650
1651	mov	240(%r8),%r14d		# pull number of rounds
1652	xor	%rdi,%rdi
1653	lea	(%rdi,%r14d,4),%rcx
1654	mov	%r8,%rsi
1655	lea	(%r8,%rcx,4),%rdi	# pointer to last chunk
1656.align	4
1657.Linvert:
1658		mov	0(%rsi),%rax
1659		mov	8(%rsi),%rbx
1660		mov	0(%rdi),%rcx
1661		mov	8(%rdi),%rdx
1662		mov	%rax,0(%rdi)
1663		mov	%rbx,8(%rdi)
1664		mov	%rcx,0(%rsi)
1665		mov	%rdx,8(%rsi)
1666		lea	16(%rsi),%rsi
1667		lea	-16(%rdi),%rdi
1668		cmp	%rsi,%rdi
1669	jne	.Linvert
1670
1671	lea	.LAES_Te+2048+1024(%rip),%rax	# rcon
1672
1673	mov	40(%rax),$mask80
1674	mov	48(%rax),$maskfe
1675	mov	56(%rax),$mask1b
1676
1677	mov	%r8,$key
1678	sub	\$1,%r14d
1679.align	4
1680.Lpermute:
1681		lea	16($key),$key
1682		mov	0($key),%rax
1683		mov	8($key),%rcx
1684___
1685		&dectransform ();
1686$code.=<<___;
1687		mov	%eax,0($key)
1688		mov	%ebx,4($key)
1689		mov	%ecx,8($key)
1690		mov	%edx,12($key)
1691		sub	\$1,%r14d
1692	jnz	.Lpermute
1693
1694	xor	%rax,%rax
1695.Labort:
1696	mov	8(%rsp),%r15
1697.cfi_restore	%r15
1698	mov	16(%rsp),%r14
1699.cfi_restore	%r14
1700	mov	24(%rsp),%r13
1701.cfi_restore	%r13
1702	mov	32(%rsp),%r12
1703.cfi_restore	%r12
1704	mov	40(%rsp),%rbp
1705.cfi_restore	%rbp
1706	mov	48(%rsp),%rbx
1707.cfi_restore	%rbx
1708	add	\$56,%rsp
1709.cfi_adjust_cfa_offset	-56
1710.Ldec_key_epilogue:
1711	ret
1712.cfi_endproc
1713.size	AES_set_decrypt_key,.-AES_set_decrypt_key
1714___
1715
1716# void AES_cbc_encrypt (const void char *inp, unsigned char *out,
1717#			size_t length, const AES_KEY *key,
1718#			unsigned char *ivp,const int enc);
1719{
1720# stack frame layout
1721# -8(%rsp)		return address
1722my $keyp="0(%rsp)";		# one to pass as $key
1723my $keyend="8(%rsp)";		# &(keyp->rd_key[4*keyp->rounds])
1724my $_rsp="16(%rsp)";		# saved %rsp
1725my $_inp="24(%rsp)";		# copy of 1st parameter, inp
1726my $_out="32(%rsp)";		# copy of 2nd parameter, out
1727my $_len="40(%rsp)";		# copy of 3rd parameter, length
1728my $_key="48(%rsp)";		# copy of 4th parameter, key
1729my $_ivp="56(%rsp)";		# copy of 5th parameter, ivp
1730my $ivec="64(%rsp)";		# ivec[16]
1731my $aes_key="80(%rsp)";		# copy of aes_key
1732my $mark="80+240(%rsp)";	# copy of aes_key->rounds
1733
1734$code.=<<___;
1735.globl	AES_cbc_encrypt
1736.type	AES_cbc_encrypt,\@function,6
1737.align	16
1738.extern	OPENSSL_ia32cap_P
1739.globl	asm_AES_cbc_encrypt
1740.hidden	asm_AES_cbc_encrypt
1741asm_AES_cbc_encrypt:
1742AES_cbc_encrypt:
1743.cfi_startproc
1744	endbranch
1745	cmp	\$0,%rdx	# check length
1746	je	.Lcbc_epilogue
1747	pushfq
1748# This could be .cfi_push 49, but libunwind fails on registers it does not
1749# recognize. See https://bugzilla.redhat.com/show_bug.cgi?id=217087.
1750.cfi_adjust_cfa_offset	8
1751	push	%rbx
1752.cfi_push	%rbx
1753	push	%rbp
1754.cfi_push	%rbp
1755	push	%r12
1756.cfi_push	%r12
1757	push	%r13
1758.cfi_push	%r13
1759	push	%r14
1760.cfi_push	%r14
1761	push	%r15
1762.cfi_push	%r15
1763.Lcbc_prologue:
1764
1765	cld
1766	mov	%r9d,%r9d	# clear upper half of enc
1767
1768	lea	.LAES_Te(%rip),$sbox
1769	lea	.LAES_Td(%rip),%r10
1770	cmp	\$0,%r9
1771	cmoveq	%r10,$sbox
1772
1773.cfi_remember_state
1774	mov	OPENSSL_ia32cap_P(%rip),%r10d
1775	cmp	\$$speed_limit,%rdx
1776	jb	.Lcbc_slow_prologue
1777	test	\$15,%rdx
1778	jnz	.Lcbc_slow_prologue
1779	bt	\$28,%r10d
1780	jc	.Lcbc_slow_prologue
1781
1782	# allocate aligned stack frame...
1783	lea	-88-248(%rsp),$key
1784	and	\$-64,$key
1785
1786	# ... and make sure it doesn't alias with AES_T[ed] modulo 4096
1787	mov	$sbox,%r10
1788	lea	2304($sbox),%r11
1789	mov	$key,%r12
1790	and	\$0xFFF,%r10	# s = $sbox&0xfff
1791	and	\$0xFFF,%r11	# e = ($sbox+2048)&0xfff
1792	and	\$0xFFF,%r12	# p = %rsp&0xfff
1793
1794	cmp	%r11,%r12	# if (p=>e) %rsp =- (p-e);
1795	jb	.Lcbc_te_break_out
1796	sub	%r11,%r12
1797	sub	%r12,$key
1798	jmp	.Lcbc_te_ok
1799.Lcbc_te_break_out:		# else %rsp -= (p-s)&0xfff + framesz
1800	sub	%r10,%r12
1801	and	\$0xFFF,%r12
1802	add	\$320,%r12
1803	sub	%r12,$key
1804.align	4
1805.Lcbc_te_ok:
1806
1807	xchg	%rsp,$key
1808.cfi_def_cfa_register	$key
1809	#add	\$8,%rsp	# reserve for return address!
1810	mov	$key,$_rsp	# save %rsp
1811.cfi_cfa_expression	$_rsp,deref,+64
1812.Lcbc_fast_body:
1813	mov	%rdi,$_inp	# save copy of inp
1814	mov	%rsi,$_out	# save copy of out
1815	mov	%rdx,$_len	# save copy of len
1816	mov	%rcx,$_key	# save copy of key
1817	mov	%r8,$_ivp	# save copy of ivp
1818	movl	\$0,$mark	# copy of aes_key->rounds = 0;
1819	mov	%r8,%rbp	# rearrange input arguments
1820	mov	%r9,%rbx
1821	mov	%rsi,$out
1822	mov	%rdi,$inp
1823	mov	%rcx,$key
1824
1825	mov	240($key),%eax		# key->rounds
1826	# do we copy key schedule to stack?
1827	mov	$key,%r10
1828	sub	$sbox,%r10
1829	and	\$0xfff,%r10
1830	cmp	\$2304,%r10
1831	jb	.Lcbc_do_ecopy
1832	cmp	\$4096-248,%r10
1833	jb	.Lcbc_skip_ecopy
1834.align	4
1835.Lcbc_do_ecopy:
1836		mov	$key,%rsi
1837		lea	$aes_key,%rdi
1838		lea	$aes_key,$key
1839		mov	\$240/8,%ecx
1840		.long	0x90A548F3	# rep movsq
1841		mov	%eax,(%rdi)	# copy aes_key->rounds
1842.Lcbc_skip_ecopy:
1843	mov	$key,$keyp	# save key pointer
1844
1845	mov	\$18,%ecx
1846.align	4
1847.Lcbc_prefetch_te:
1848		mov	0($sbox),%r10
1849		mov	32($sbox),%r11
1850		mov	64($sbox),%r12
1851		mov	96($sbox),%r13
1852		lea	128($sbox),$sbox
1853		sub	\$1,%ecx
1854	jnz	.Lcbc_prefetch_te
1855	lea	-2304($sbox),$sbox
1856
1857	cmp	\$0,%rbx
1858	je	.LFAST_DECRYPT
1859
1860#----------------------------- ENCRYPT -----------------------------#
1861	mov	0(%rbp),$s0		# load iv
1862	mov	4(%rbp),$s1
1863	mov	8(%rbp),$s2
1864	mov	12(%rbp),$s3
1865
1866.align	4
1867.Lcbc_fast_enc_loop:
1868		xor	0($inp),$s0
1869		xor	4($inp),$s1
1870		xor	8($inp),$s2
1871		xor	12($inp),$s3
1872		mov	$keyp,$key	# restore key
1873		mov	$inp,$_inp	# if ($verticalspin) save inp
1874
1875		call	_x86_64_AES_encrypt
1876
1877		mov	$_inp,$inp	# if ($verticalspin) restore inp
1878		mov	$_len,%r10
1879		mov	$s0,0($out)
1880		mov	$s1,4($out)
1881		mov	$s2,8($out)
1882		mov	$s3,12($out)
1883
1884		lea	16($inp),$inp
1885		lea	16($out),$out
1886		sub	\$16,%r10
1887		test	\$-16,%r10
1888		mov	%r10,$_len
1889	jnz	.Lcbc_fast_enc_loop
1890	mov	$_ivp,%rbp	# restore ivp
1891	mov	$s0,0(%rbp)	# save ivec
1892	mov	$s1,4(%rbp)
1893	mov	$s2,8(%rbp)
1894	mov	$s3,12(%rbp)
1895
1896	jmp	.Lcbc_fast_cleanup
1897
1898#----------------------------- DECRYPT -----------------------------#
1899.align	16
1900.LFAST_DECRYPT:
1901	cmp	$inp,$out
1902	je	.Lcbc_fast_dec_in_place
1903
1904	mov	%rbp,$ivec
1905.align	4
1906.Lcbc_fast_dec_loop:
1907		mov	0($inp),$s0	# read input
1908		mov	4($inp),$s1
1909		mov	8($inp),$s2
1910		mov	12($inp),$s3
1911		mov	$keyp,$key	# restore key
1912		mov	$inp,$_inp	# if ($verticalspin) save inp
1913
1914		call	_x86_64_AES_decrypt
1915
1916		mov	$ivec,%rbp	# load ivp
1917		mov	$_inp,$inp	# if ($verticalspin) restore inp
1918		mov	$_len,%r10	# load len
1919		xor	0(%rbp),$s0	# xor iv
1920		xor	4(%rbp),$s1
1921		xor	8(%rbp),$s2
1922		xor	12(%rbp),$s3
1923		mov	$inp,%rbp	# current input, next iv
1924
1925		sub	\$16,%r10
1926		mov	%r10,$_len	# update len
1927		mov	%rbp,$ivec	# update ivp
1928
1929		mov	$s0,0($out)	# write output
1930		mov	$s1,4($out)
1931		mov	$s2,8($out)
1932		mov	$s3,12($out)
1933
1934		lea	16($inp),$inp
1935		lea	16($out),$out
1936	jnz	.Lcbc_fast_dec_loop
1937	mov	$_ivp,%r12		# load user ivp
1938	mov	0(%rbp),%r10		# load iv
1939	mov	8(%rbp),%r11
1940	mov	%r10,0(%r12)		# copy back to user
1941	mov	%r11,8(%r12)
1942	jmp	.Lcbc_fast_cleanup
1943
1944.align	16
1945.Lcbc_fast_dec_in_place:
1946	mov	0(%rbp),%r10		# copy iv to stack
1947	mov	8(%rbp),%r11
1948	mov	%r10,0+$ivec
1949	mov	%r11,8+$ivec
1950.align	4
1951.Lcbc_fast_dec_in_place_loop:
1952		mov	0($inp),$s0	# load input
1953		mov	4($inp),$s1
1954		mov	8($inp),$s2
1955		mov	12($inp),$s3
1956		mov	$keyp,$key	# restore key
1957		mov	$inp,$_inp	# if ($verticalspin) save inp
1958
1959		call	_x86_64_AES_decrypt
1960
1961		mov	$_inp,$inp	# if ($verticalspin) restore inp
1962		mov	$_len,%r10
1963		xor	0+$ivec,$s0
1964		xor	4+$ivec,$s1
1965		xor	8+$ivec,$s2
1966		xor	12+$ivec,$s3
1967
1968		mov	0($inp),%r11	# load input
1969		mov	8($inp),%r12
1970		sub	\$16,%r10
1971		jz	.Lcbc_fast_dec_in_place_done
1972
1973		mov	%r11,0+$ivec	# copy input to iv
1974		mov	%r12,8+$ivec
1975
1976		mov	$s0,0($out)	# save output [zaps input]
1977		mov	$s1,4($out)
1978		mov	$s2,8($out)
1979		mov	$s3,12($out)
1980
1981		lea	16($inp),$inp
1982		lea	16($out),$out
1983		mov	%r10,$_len
1984	jmp	.Lcbc_fast_dec_in_place_loop
1985.Lcbc_fast_dec_in_place_done:
1986	mov	$_ivp,%rdi
1987	mov	%r11,0(%rdi)	# copy iv back to user
1988	mov	%r12,8(%rdi)
1989
1990	mov	$s0,0($out)	# save output [zaps input]
1991	mov	$s1,4($out)
1992	mov	$s2,8($out)
1993	mov	$s3,12($out)
1994
1995.align	4
1996.Lcbc_fast_cleanup:
1997	cmpl	\$0,$mark	# was the key schedule copied?
1998	lea	$aes_key,%rdi
1999	je	.Lcbc_exit
2000		mov	\$240/8,%ecx
2001		xor	%rax,%rax
2002		.long	0x90AB48F3	# rep stosq
2003
2004	jmp	.Lcbc_exit
2005
2006#--------------------------- SLOW ROUTINE ---------------------------#
2007.align	16
2008.Lcbc_slow_prologue:
2009.cfi_restore_state
2010	# allocate aligned stack frame...
2011	lea	-88(%rsp),%rbp
2012	and	\$-64,%rbp
2013	# ... just "above" key schedule
2014	lea	-88-63(%rcx),%r10
2015	sub	%rbp,%r10
2016	neg	%r10
2017	and	\$0x3c0,%r10
2018	sub	%r10,%rbp
2019
2020	xchg	%rsp,%rbp
2021.cfi_def_cfa_register	%rbp
2022	#add	\$8,%rsp	# reserve for return address!
2023	mov	%rbp,$_rsp	# save %rsp
2024.cfi_cfa_expression	$_rsp,deref,+64
2025.Lcbc_slow_body:
2026	#mov	%rdi,$_inp	# save copy of inp
2027	#mov	%rsi,$_out	# save copy of out
2028	#mov	%rdx,$_len	# save copy of len
2029	#mov	%rcx,$_key	# save copy of key
2030	mov	%r8,$_ivp	# save copy of ivp
2031	mov	%r8,%rbp	# rearrange input arguments
2032	mov	%r9,%rbx
2033	mov	%rsi,$out
2034	mov	%rdi,$inp
2035	mov	%rcx,$key
2036	mov	%rdx,%r10
2037
2038	mov	240($key),%eax
2039	mov	$key,$keyp	# save key pointer
2040	shl	\$4,%eax
2041	lea	($key,%rax),%rax
2042	mov	%rax,$keyend
2043
2044	# pick Te4 copy which can't "overlap" with stack frame or key schedule
2045	lea	2048($sbox),$sbox
2046	lea	768-8(%rsp),%rax
2047	sub	$sbox,%rax
2048	and	\$0x300,%rax
2049	lea	($sbox,%rax),$sbox
2050
2051	cmp	\$0,%rbx
2052	je	.LSLOW_DECRYPT
2053
2054#--------------------------- SLOW ENCRYPT ---------------------------#
2055	test	\$-16,%r10		# check upon length
2056	mov	0(%rbp),$s0		# load iv
2057	mov	4(%rbp),$s1
2058	mov	8(%rbp),$s2
2059	mov	12(%rbp),$s3
2060	jz	.Lcbc_slow_enc_tail	# short input...
2061
2062.align	4
2063.Lcbc_slow_enc_loop:
2064		xor	0($inp),$s0
2065		xor	4($inp),$s1
2066		xor	8($inp),$s2
2067		xor	12($inp),$s3
2068		mov	$keyp,$key	# restore key
2069		mov	$inp,$_inp	# save inp
2070		mov	$out,$_out	# save out
2071		mov	%r10,$_len	# save len
2072
2073		call	_x86_64_AES_encrypt_compact
2074
2075		mov	$_inp,$inp	# restore inp
2076		mov	$_out,$out	# restore out
2077		mov	$_len,%r10	# restore len
2078		mov	$s0,0($out)
2079		mov	$s1,4($out)
2080		mov	$s2,8($out)
2081		mov	$s3,12($out)
2082
2083		lea	16($inp),$inp
2084		lea	16($out),$out
2085		sub	\$16,%r10
2086		test	\$-16,%r10
2087	jnz	.Lcbc_slow_enc_loop
2088	test	\$15,%r10
2089	jnz	.Lcbc_slow_enc_tail
2090	mov	$_ivp,%rbp	# restore ivp
2091	mov	$s0,0(%rbp)	# save ivec
2092	mov	$s1,4(%rbp)
2093	mov	$s2,8(%rbp)
2094	mov	$s3,12(%rbp)
2095
2096	jmp	.Lcbc_exit
2097
2098.align	4
2099.Lcbc_slow_enc_tail:
2100	mov	%rax,%r11
2101	mov	%rcx,%r12
2102	mov	%r10,%rcx
2103	mov	$inp,%rsi
2104	mov	$out,%rdi
2105	.long	0x9066A4F3		# rep movsb
2106	mov	\$16,%rcx		# zero tail
2107	sub	%r10,%rcx
2108	xor	%rax,%rax
2109	.long	0x9066AAF3		# rep stosb
2110	mov	$out,$inp		# this is not a mistake!
2111	mov	\$16,%r10		# len=16
2112	mov	%r11,%rax
2113	mov	%r12,%rcx
2114	jmp	.Lcbc_slow_enc_loop	# one more spin...
2115#--------------------------- SLOW DECRYPT ---------------------------#
2116.align	16
2117.LSLOW_DECRYPT:
2118	shr	\$3,%rax
2119	add	%rax,$sbox		# recall "magic" constants!
2120
2121	mov	0(%rbp),%r11		# copy iv to stack
2122	mov	8(%rbp),%r12
2123	mov	%r11,0+$ivec
2124	mov	%r12,8+$ivec
2125
2126.align	4
2127.Lcbc_slow_dec_loop:
2128		mov	0($inp),$s0	# load input
2129		mov	4($inp),$s1
2130		mov	8($inp),$s2
2131		mov	12($inp),$s3
2132		mov	$keyp,$key	# restore key
2133		mov	$inp,$_inp	# save inp
2134		mov	$out,$_out	# save out
2135		mov	%r10,$_len	# save len
2136
2137		call	_x86_64_AES_decrypt_compact
2138
2139		mov	$_inp,$inp	# restore inp
2140		mov	$_out,$out	# restore out
2141		mov	$_len,%r10
2142		xor	0+$ivec,$s0
2143		xor	4+$ivec,$s1
2144		xor	8+$ivec,$s2
2145		xor	12+$ivec,$s3
2146
2147		mov	0($inp),%r11	# load input
2148		mov	8($inp),%r12
2149		sub	\$16,%r10
2150		jc	.Lcbc_slow_dec_partial
2151		jz	.Lcbc_slow_dec_done
2152
2153		mov	%r11,0+$ivec	# copy input to iv
2154		mov	%r12,8+$ivec
2155
2156		mov	$s0,0($out)	# save output [can zap input]
2157		mov	$s1,4($out)
2158		mov	$s2,8($out)
2159		mov	$s3,12($out)
2160
2161		lea	16($inp),$inp
2162		lea	16($out),$out
2163	jmp	.Lcbc_slow_dec_loop
2164.Lcbc_slow_dec_done:
2165	mov	$_ivp,%rdi
2166	mov	%r11,0(%rdi)		# copy iv back to user
2167	mov	%r12,8(%rdi)
2168
2169	mov	$s0,0($out)		# save output [can zap input]
2170	mov	$s1,4($out)
2171	mov	$s2,8($out)
2172	mov	$s3,12($out)
2173
2174	jmp	.Lcbc_exit
2175
2176.align	4
2177.Lcbc_slow_dec_partial:
2178	mov	$_ivp,%rdi
2179	mov	%r11,0(%rdi)		# copy iv back to user
2180	mov	%r12,8(%rdi)
2181
2182	mov	$s0,0+$ivec		# save output to stack
2183	mov	$s1,4+$ivec
2184	mov	$s2,8+$ivec
2185	mov	$s3,12+$ivec
2186
2187	mov	$out,%rdi
2188	lea	$ivec,%rsi
2189	lea	16(%r10),%rcx
2190	.long	0x9066A4F3	# rep movsb
2191	jmp	.Lcbc_exit
2192
2193.align	16
2194.Lcbc_exit:
2195	mov	$_rsp,%rsi
2196.cfi_def_cfa	%rsi,64
2197	mov	(%rsi),%r15
2198.cfi_restore	%r15
2199	mov	8(%rsi),%r14
2200.cfi_restore	%r14
2201	mov	16(%rsi),%r13
2202.cfi_restore	%r13
2203	mov	24(%rsi),%r12
2204.cfi_restore	%r12
2205	mov	32(%rsi),%rbp
2206.cfi_restore	%rbp
2207	mov	40(%rsi),%rbx
2208.cfi_restore	%rbx
2209	lea	48(%rsi),%rsp
2210.cfi_def_cfa	%rsp,16
2211.Lcbc_popfq:
2212	popfq
2213# This could be .cfi_pop 49, but libunwind fails on registers it does not
2214# recognize. See https://bugzilla.redhat.com/show_bug.cgi?id=217087.
2215.cfi_adjust_cfa_offset	-8
2216.Lcbc_epilogue:
2217	ret
2218.cfi_endproc
2219.size	AES_cbc_encrypt,.-AES_cbc_encrypt
2220___
2221}
2222
2223$code.=<<___;
2224.align	64
2225.LAES_Te:
2226___
2227	&_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
2228	&_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
2229	&_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
2230	&_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
2231	&_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
2232	&_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
2233	&_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
2234	&_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
2235	&_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
2236	&_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
2237	&_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
2238	&_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
2239	&_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
2240	&_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
2241	&_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
2242	&_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
2243	&_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
2244	&_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
2245	&_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
2246	&_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
2247	&_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
2248	&_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
2249	&_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
2250	&_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
2251	&_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
2252	&_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
2253	&_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
2254	&_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
2255	&_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
2256	&_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
2257	&_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
2258	&_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
2259	&_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
2260	&_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
2261	&_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
2262	&_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
2263	&_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
2264	&_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
2265	&_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
2266	&_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
2267	&_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
2268	&_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
2269	&_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
2270	&_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
2271	&_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
2272	&_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
2273	&_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
2274	&_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
2275	&_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
2276	&_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
2277	&_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
2278	&_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
2279	&_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
2280	&_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
2281	&_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
2282	&_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
2283	&_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
2284	&_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
2285	&_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
2286	&_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
2287	&_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
2288	&_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
2289	&_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
2290	&_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
2291
2292#Te4	# four copies of Te4 to choose from to avoid L1 aliasing
2293	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
2294	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
2295	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
2296	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
2297	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
2298	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
2299	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
2300	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
2301	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
2302	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
2303	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
2304	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
2305	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
2306	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
2307	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
2308	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
2309	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
2310	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
2311	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
2312	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
2313	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
2314	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
2315	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
2316	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
2317	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
2318	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
2319	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
2320	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
2321	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
2322	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
2323	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
2324	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
2325
2326	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
2327	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
2328	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
2329	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
2330	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
2331	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
2332	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
2333	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
2334	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
2335	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
2336	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
2337	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
2338	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
2339	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
2340	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
2341	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
2342	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
2343	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
2344	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
2345	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
2346	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
2347	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
2348	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
2349	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
2350	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
2351	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
2352	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
2353	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
2354	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
2355	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
2356	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
2357	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
2358
2359	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
2360	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
2361	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
2362	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
2363	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
2364	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
2365	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
2366	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
2367	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
2368	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
2369	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
2370	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
2371	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
2372	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
2373	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
2374	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
2375	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
2376	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
2377	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
2378	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
2379	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
2380	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
2381	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
2382	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
2383	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
2384	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
2385	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
2386	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
2387	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
2388	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
2389	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
2390	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
2391
2392	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
2393	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
2394	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
2395	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
2396	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
2397	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
2398	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
2399	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
2400	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
2401	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
2402	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
2403	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
2404	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
2405	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
2406	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
2407	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
2408	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
2409	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
2410	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
2411	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
2412	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
2413	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
2414	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
2415	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
2416	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
2417	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
2418	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
2419	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
2420	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
2421	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
2422	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
2423	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
2424#rcon:
2425$code.=<<___;
2426	.long	0x00000001, 0x00000002, 0x00000004, 0x00000008
2427	.long	0x00000010, 0x00000020, 0x00000040, 0x00000080
2428	.long	0x0000001b, 0x00000036, 0x80808080, 0x80808080
2429	.long	0xfefefefe, 0xfefefefe, 0x1b1b1b1b, 0x1b1b1b1b
2430___
2431$code.=<<___;
2432.align	64
2433.LAES_Td:
2434___
2435	&_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
2436	&_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
2437	&_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
2438	&_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
2439	&_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
2440	&_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
2441	&_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
2442	&_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
2443	&_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
2444	&_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
2445	&_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
2446	&_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
2447	&_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
2448	&_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
2449	&_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
2450	&_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
2451	&_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
2452	&_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
2453	&_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
2454	&_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
2455	&_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
2456	&_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
2457	&_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
2458	&_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
2459	&_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
2460	&_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
2461	&_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
2462	&_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
2463	&_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
2464	&_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
2465	&_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
2466	&_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
2467	&_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
2468	&_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
2469	&_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
2470	&_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
2471	&_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
2472	&_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
2473	&_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
2474	&_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
2475	&_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
2476	&_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
2477	&_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
2478	&_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
2479	&_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
2480	&_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
2481	&_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
2482	&_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
2483	&_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
2484	&_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
2485	&_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
2486	&_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
2487	&_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
2488	&_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
2489	&_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
2490	&_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
2491	&_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
2492	&_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
2493	&_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
2494	&_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
2495	&_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
2496	&_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
2497	&_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
2498	&_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
2499
2500#Td4:	# four copies of Td4 to choose from to avoid L1 aliasing
2501	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
2502	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
2503	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
2504	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
2505	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
2506	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
2507	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
2508	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
2509	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
2510	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
2511	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
2512	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
2513	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
2514	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
2515	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
2516	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
2517	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
2518	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
2519	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
2520	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
2521	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
2522	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
2523	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
2524	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
2525	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
2526	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
2527	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
2528	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
2529	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
2530	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
2531	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
2532	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
2533$code.=<<___;
2534	.long	0x80808080, 0x80808080, 0xfefefefe, 0xfefefefe
2535	.long	0x1b1b1b1b, 0x1b1b1b1b, 0, 0
2536___
2537	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
2538	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
2539	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
2540	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
2541	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
2542	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
2543	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
2544	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
2545	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
2546	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
2547	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
2548	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
2549	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
2550	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
2551	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
2552	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
2553	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
2554	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
2555	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
2556	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
2557	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
2558	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
2559	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
2560	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
2561	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
2562	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
2563	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
2564	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
2565	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
2566	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
2567	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
2568	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
2569$code.=<<___;
2570	.long	0x80808080, 0x80808080, 0xfefefefe, 0xfefefefe
2571	.long	0x1b1b1b1b, 0x1b1b1b1b, 0, 0
2572___
2573	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
2574	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
2575	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
2576	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
2577	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
2578	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
2579	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
2580	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
2581	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
2582	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
2583	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
2584	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
2585	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
2586	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
2587	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
2588	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
2589	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
2590	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
2591	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
2592	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
2593	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
2594	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
2595	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
2596	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
2597	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
2598	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
2599	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
2600	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
2601	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
2602	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
2603	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
2604	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
2605$code.=<<___;
2606	.long	0x80808080, 0x80808080, 0xfefefefe, 0xfefefefe
2607	.long	0x1b1b1b1b, 0x1b1b1b1b, 0, 0
2608___
2609	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
2610	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
2611	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
2612	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
2613	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
2614	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
2615	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
2616	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
2617	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
2618	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
2619	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
2620	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
2621	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
2622	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
2623	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
2624	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
2625	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
2626	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
2627	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
2628	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
2629	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
2630	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
2631	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
2632	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
2633	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
2634	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
2635	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
2636	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
2637	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
2638	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
2639	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
2640	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
2641$code.=<<___;
2642	.long	0x80808080, 0x80808080, 0xfefefefe, 0xfefefefe
2643	.long	0x1b1b1b1b, 0x1b1b1b1b, 0, 0
2644.asciz  "AES for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
2645.align	64
2646___
2647
2648# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2649#		CONTEXT *context,DISPATCHER_CONTEXT *disp)
2650if ($win64) {
2651$rec="%rcx";
2652$frame="%rdx";
2653$context="%r8";
2654$disp="%r9";
2655
2656$code.=<<___;
2657.extern	__imp_RtlVirtualUnwind
2658.type	block_se_handler,\@abi-omnipotent
2659.align	16
2660block_se_handler:
2661	push	%rsi
2662	push	%rdi
2663	push	%rbx
2664	push	%rbp
2665	push	%r12
2666	push	%r13
2667	push	%r14
2668	push	%r15
2669	pushfq
2670	sub	\$64,%rsp
2671
2672	mov	120($context),%rax	# pull context->Rax
2673	mov	248($context),%rbx	# pull context->Rip
2674
2675	mov	8($disp),%rsi		# disp->ImageBase
2676	mov	56($disp),%r11		# disp->HandlerData
2677
2678	mov	0(%r11),%r10d		# HandlerData[0]
2679	lea	(%rsi,%r10),%r10	# prologue label
2680	cmp	%r10,%rbx		# context->Rip<prologue label
2681	jb	.Lin_block_prologue
2682
2683	mov	152($context),%rax	# pull context->Rsp
2684
2685	mov	4(%r11),%r10d		# HandlerData[1]
2686	lea	(%rsi,%r10),%r10	# epilogue label
2687	cmp	%r10,%rbx		# context->Rip>=epilogue label
2688	jae	.Lin_block_prologue
2689
2690	mov	24(%rax),%rax		# pull saved real stack pointer
2691
2692	mov	-8(%rax),%rbx
2693	mov	-16(%rax),%rbp
2694	mov	-24(%rax),%r12
2695	mov	-32(%rax),%r13
2696	mov	-40(%rax),%r14
2697	mov	-48(%rax),%r15
2698	mov	%rbx,144($context)	# restore context->Rbx
2699	mov	%rbp,160($context)	# restore context->Rbp
2700	mov	%r12,216($context)	# restore context->R12
2701	mov	%r13,224($context)	# restore context->R13
2702	mov	%r14,232($context)	# restore context->R14
2703	mov	%r15,240($context)	# restore context->R15
2704
2705.Lin_block_prologue:
2706	mov	8(%rax),%rdi
2707	mov	16(%rax),%rsi
2708	mov	%rax,152($context)	# restore context->Rsp
2709	mov	%rsi,168($context)	# restore context->Rsi
2710	mov	%rdi,176($context)	# restore context->Rdi
2711
2712	jmp	.Lcommon_seh_exit
2713.size	block_se_handler,.-block_se_handler
2714
2715.type	key_se_handler,\@abi-omnipotent
2716.align	16
2717key_se_handler:
2718	push	%rsi
2719	push	%rdi
2720	push	%rbx
2721	push	%rbp
2722	push	%r12
2723	push	%r13
2724	push	%r14
2725	push	%r15
2726	pushfq
2727	sub	\$64,%rsp
2728
2729	mov	120($context),%rax	# pull context->Rax
2730	mov	248($context),%rbx	# pull context->Rip
2731
2732	mov	8($disp),%rsi		# disp->ImageBase
2733	mov	56($disp),%r11		# disp->HandlerData
2734
2735	mov	0(%r11),%r10d		# HandlerData[0]
2736	lea	(%rsi,%r10),%r10	# prologue label
2737	cmp	%r10,%rbx		# context->Rip<prologue label
2738	jb	.Lin_key_prologue
2739
2740	mov	152($context),%rax	# pull context->Rsp
2741
2742	mov	4(%r11),%r10d		# HandlerData[1]
2743	lea	(%rsi,%r10),%r10	# epilogue label
2744	cmp	%r10,%rbx		# context->Rip>=epilogue label
2745	jae	.Lin_key_prologue
2746
2747	lea	56(%rax),%rax
2748
2749	mov	-8(%rax),%rbx
2750	mov	-16(%rax),%rbp
2751	mov	-24(%rax),%r12
2752	mov	-32(%rax),%r13
2753	mov	-40(%rax),%r14
2754	mov	-48(%rax),%r15
2755	mov	%rbx,144($context)	# restore context->Rbx
2756	mov	%rbp,160($context)	# restore context->Rbp
2757	mov	%r12,216($context)	# restore context->R12
2758	mov	%r13,224($context)	# restore context->R13
2759	mov	%r14,232($context)	# restore context->R14
2760	mov	%r15,240($context)	# restore context->R15
2761
2762.Lin_key_prologue:
2763	mov	8(%rax),%rdi
2764	mov	16(%rax),%rsi
2765	mov	%rax,152($context)	# restore context->Rsp
2766	mov	%rsi,168($context)	# restore context->Rsi
2767	mov	%rdi,176($context)	# restore context->Rdi
2768
2769	jmp	.Lcommon_seh_exit
2770.size	key_se_handler,.-key_se_handler
2771
2772.type	cbc_se_handler,\@abi-omnipotent
2773.align	16
2774cbc_se_handler:
2775	push	%rsi
2776	push	%rdi
2777	push	%rbx
2778	push	%rbp
2779	push	%r12
2780	push	%r13
2781	push	%r14
2782	push	%r15
2783	pushfq
2784	sub	\$64,%rsp
2785
2786	mov	120($context),%rax	# pull context->Rax
2787	mov	248($context),%rbx	# pull context->Rip
2788
2789	lea	.Lcbc_prologue(%rip),%r10
2790	cmp	%r10,%rbx		# context->Rip<.Lcbc_prologue
2791	jb	.Lin_cbc_prologue
2792
2793	lea	.Lcbc_fast_body(%rip),%r10
2794	cmp	%r10,%rbx		# context->Rip<.Lcbc_fast_body
2795	jb	.Lin_cbc_frame_setup
2796
2797	lea	.Lcbc_slow_prologue(%rip),%r10
2798	cmp	%r10,%rbx		# context->Rip<.Lcbc_slow_prologue
2799	jb	.Lin_cbc_body
2800
2801	lea	.Lcbc_slow_body(%rip),%r10
2802	cmp	%r10,%rbx		# context->Rip<.Lcbc_slow_body
2803	jb	.Lin_cbc_frame_setup
2804
2805.Lin_cbc_body:
2806	mov	152($context),%rax	# pull context->Rsp
2807
2808	lea	.Lcbc_epilogue(%rip),%r10
2809	cmp	%r10,%rbx		# context->Rip>=.Lcbc_epilogue
2810	jae	.Lin_cbc_prologue
2811
2812	lea	8(%rax),%rax
2813
2814	lea	.Lcbc_popfq(%rip),%r10
2815	cmp	%r10,%rbx		# context->Rip>=.Lcbc_popfq
2816	jae	.Lin_cbc_prologue
2817
2818	mov	`16-8`(%rax),%rax	# biased $_rsp
2819	lea	56(%rax),%rax
2820
2821.Lin_cbc_frame_setup:
2822	mov	-16(%rax),%rbx
2823	mov	-24(%rax),%rbp
2824	mov	-32(%rax),%r12
2825	mov	-40(%rax),%r13
2826	mov	-48(%rax),%r14
2827	mov	-56(%rax),%r15
2828	mov	%rbx,144($context)	# restore context->Rbx
2829	mov	%rbp,160($context)	# restore context->Rbp
2830	mov	%r12,216($context)	# restore context->R12
2831	mov	%r13,224($context)	# restore context->R13
2832	mov	%r14,232($context)	# restore context->R14
2833	mov	%r15,240($context)	# restore context->R15
2834
2835.Lin_cbc_prologue:
2836	mov	8(%rax),%rdi
2837	mov	16(%rax),%rsi
2838	mov	%rax,152($context)	# restore context->Rsp
2839	mov	%rsi,168($context)	# restore context->Rsi
2840	mov	%rdi,176($context)	# restore context->Rdi
2841
2842.Lcommon_seh_exit:
2843
2844	mov	40($disp),%rdi		# disp->ContextRecord
2845	mov	$context,%rsi		# context
2846	mov	\$`1232/8`,%ecx		# sizeof(CONTEXT)
2847	.long	0xa548f3fc		# cld; rep movsq
2848
2849	mov	$disp,%rsi
2850	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
2851	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
2852	mov	0(%rsi),%r8		# arg3, disp->ControlPc
2853	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
2854	mov	40(%rsi),%r10		# disp->ContextRecord
2855	lea	56(%rsi),%r11		# &disp->HandlerData
2856	lea	24(%rsi),%r12		# &disp->EstablisherFrame
2857	mov	%r10,32(%rsp)		# arg5
2858	mov	%r11,40(%rsp)		# arg6
2859	mov	%r12,48(%rsp)		# arg7
2860	mov	%rcx,56(%rsp)		# arg8, (NULL)
2861	call	*__imp_RtlVirtualUnwind(%rip)
2862
2863	mov	\$1,%eax		# ExceptionContinueSearch
2864	add	\$64,%rsp
2865	popfq
2866	pop	%r15
2867	pop	%r14
2868	pop	%r13
2869	pop	%r12
2870	pop	%rbp
2871	pop	%rbx
2872	pop	%rdi
2873	pop	%rsi
2874	ret
2875.size	cbc_se_handler,.-cbc_se_handler
2876
2877.section	.pdata
2878.align	4
2879	.rva	.LSEH_begin_AES_encrypt
2880	.rva	.LSEH_end_AES_encrypt
2881	.rva	.LSEH_info_AES_encrypt
2882
2883	.rva	.LSEH_begin_AES_decrypt
2884	.rva	.LSEH_end_AES_decrypt
2885	.rva	.LSEH_info_AES_decrypt
2886
2887	.rva	.LSEH_begin_AES_set_encrypt_key
2888	.rva	.LSEH_end_AES_set_encrypt_key
2889	.rva	.LSEH_info_AES_set_encrypt_key
2890
2891	.rva	.LSEH_begin_AES_set_decrypt_key
2892	.rva	.LSEH_end_AES_set_decrypt_key
2893	.rva	.LSEH_info_AES_set_decrypt_key
2894
2895	.rva	.LSEH_begin_AES_cbc_encrypt
2896	.rva	.LSEH_end_AES_cbc_encrypt
2897	.rva	.LSEH_info_AES_cbc_encrypt
2898
2899.section	.xdata
2900.align	8
2901.LSEH_info_AES_encrypt:
2902	.byte	9,0,0,0
2903	.rva	block_se_handler
2904	.rva	.Lenc_prologue,.Lenc_epilogue	# HandlerData[]
2905.LSEH_info_AES_decrypt:
2906	.byte	9,0,0,0
2907	.rva	block_se_handler
2908	.rva	.Ldec_prologue,.Ldec_epilogue	# HandlerData[]
2909.LSEH_info_AES_set_encrypt_key:
2910	.byte	9,0,0,0
2911	.rva	key_se_handler
2912	.rva	.Lenc_key_prologue,.Lenc_key_epilogue	# HandlerData[]
2913.LSEH_info_AES_set_decrypt_key:
2914	.byte	9,0,0,0
2915	.rva	key_se_handler
2916	.rva	.Ldec_key_prologue,.Ldec_key_epilogue	# HandlerData[]
2917.LSEH_info_AES_cbc_encrypt:
2918	.byte	9,0,0,0
2919	.rva	cbc_se_handler
2920___
2921}
2922
2923$code =~ s/\`([^\`]*)\`/eval($1)/gem;
2924
2925print $code;
2926
2927close STDOUT or die "error closing STDOUT: $!";
2928