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