1#!/usr/bin/env perl
2#
3# ====================================================================
4# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5# project. The module is, however, dual licensed under OpenSSL and
6# CRYPTOGAMS licenses depending on where you obtain it. For further
7# details see http://www.openssl.org/~appro/cryptogams/.
8# ====================================================================
9#
10# Version 4.3.
11#
12# You might fail to appreciate this module performance from the first
13# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
14# to be *the* best Intel C compiler without -KPIC, performance appears
15# to be virtually identical... But try to re-configure with shared
16# library support... Aha! Intel compiler "suddenly" lags behind by 30%
17# [on P4, more on others]:-) And if compared to position-independent
18# code generated by GNU C, this code performs *more* than *twice* as
19# fast! Yes, all this buzz about PIC means that unlike other hand-
20# coded implementations, this one was explicitly designed to be safe
21# to use even in shared library context... This also means that this
22# code isn't necessarily absolutely fastest "ever," because in order
23# to achieve position independence an extra register has to be
24# off-loaded to stack, which affects the benchmark result.
25#
26# Special note about instruction choice. Do you recall RC4_INT code
27# performing poorly on P4? It might be the time to figure out why.
28# RC4_INT code implies effective address calculations in base+offset*4
29# form. Trouble is that it seems that offset scaling turned to be
30# critical path... At least eliminating scaling resulted in 2.8x RC4
31# performance improvement [as you might recall]. As AES code is hungry
32# for scaling too, I [try to] avoid the latter by favoring off-by-2
33# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
34#
35# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
36# void. Performance improvement with off-by-2 shifts was observed on
37# intermediate implementation, which was spilling yet another register
38# to stack... Final offset*4 code below runs just a tad faster on P4,
39# but exhibits up to 10% improvement on other cores.
40#
41# Second version is "monolithic" replacement for aes_core.c, which in
42# addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
43# This made it possible to implement little-endian variant of the
44# algorithm without modifying the base C code. Motivating factor for
45# the undertaken effort was that it appeared that in tight IA-32
46# register window little-endian flavor could achieve slightly higher
47# Instruction Level Parallelism, and it indeed resulted in up to 15%
48# better performance on most recent µ-archs...
49#
50# Third version adds AES_cbc_encrypt implementation, which resulted in
51# up to 40% performance imrovement of CBC benchmark results. 40% was
52# observed on P4 core, where "overall" imrovement coefficient, i.e. if
53# compared to PIC generated by GCC and in CBC mode, was observed to be
54# as large as 4x:-) CBC performance is virtually identical to ECB now
55# and on some platforms even better, e.g. 17.6 "small" cycles/byte on
56# Opteron, because certain function prologues and epilogues are
57# effectively taken out of the loop...
58#
59# Version 3.2 implements compressed tables and prefetch of these tables
60# in CBC[!] mode. Former means that 3/4 of table references are now
61# misaligned, which unfortunately has negative impact on elder IA-32
62# implementations, Pentium suffered 30% penalty, PIII - 10%.
63#
64# Version 3.3 avoids L1 cache aliasing between stack frame and
65# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
66# latter is achieved by copying the key schedule to controlled place in
67# stack. This unfortunately has rather strong impact on small block CBC
68# performance, ~2x deterioration on 16-byte block if compared to 3.3.
69#
70# Version 3.5 checks if there is L1 cache aliasing between user-supplied
71# key schedule and S-boxes and abstains from copying the former if
72# there is no. This allows end-user to consciously retain small block
73# performance by aligning key schedule in specific manner.
74#
75# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
76#
77# Current ECB performance numbers for 128-bit key in CPU cycles per
78# processed byte [measure commonly used by AES benchmarkers] are:
79#
80#		small footprint		fully unrolled
81# P4		24			22
82# AMD K8	20			19
83# PIII		25			23
84# Pentium	81			78
85#
86# Version 3.7 reimplements outer rounds as "compact." Meaning that
87# first and last rounds reference compact 256 bytes S-box. This means
88# that first round consumes a lot more CPU cycles and that encrypt
89# and decrypt performance becomes asymmetric. Encrypt performance
90# drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is
91# aggressively pre-fetched.
92#
93# Version 4.0 effectively rolls back to 3.6 and instead implements
94# additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact,
95# which use exclusively 256 byte S-box. These functions are to be
96# called in modes not concealing plain text, such as ECB, or when
97# we're asked to process smaller amount of data [or unconditionally
98# on hyper-threading CPU]. Currently it's called unconditionally from
99# AES_[en|de]crypt, which affects all modes, but CBC. CBC routine
100# still needs to be modified to switch between slower and faster
101# mode when appropriate... But in either case benchmark landscape
102# changes dramatically and below numbers are CPU cycles per processed
103# byte for 128-bit key.
104#
105#		ECB encrypt	ECB decrypt	CBC large chunk
106# P4		52[54]		83[95]		23
107# AMD K8	46[41]		66[70]		18
108# PIII		41[50]		60[77]		24
109# Core 2	31[36]		45[64]		18.5
110# Atom		76[100]		96[138]		60
111# Pentium	115		150		77
112#
113# Version 4.1 switches to compact S-box even in key schedule setup.
114#
115# Version 4.2 prefetches compact S-box in every SSE round or in other
116# words every cache-line is *guaranteed* to be accessed within ~50
117# cycles window. Why just SSE? Because it's needed on hyper-threading
118# CPU! Which is also why it's prefetched with 64 byte stride. Best
119# part is that it has no negative effect on performance:-)
120#
121# Version 4.3 implements switch between compact and non-compact block
122# functions in AES_cbc_encrypt depending on how much data was asked
123# to be processed in one stroke.
124#
125######################################################################
126# Timing attacks are classified in two classes: synchronous when
127# attacker consciously initiates cryptographic operation and collects
128# timing data of various character afterwards, and asynchronous when
129# malicious code is executed on same CPU simultaneously with AES,
130# instruments itself and performs statistical analysis of this data.
131#
132# As far as synchronous attacks go the root to the AES timing
133# vulnerability is twofold. Firstly, of 256 S-box elements at most 160
134# are referred to in single 128-bit block operation. Well, in C
135# implementation with 4 distinct tables it's actually as little as 40
136# references per 256 elements table, but anyway... Secondly, even
137# though S-box elements are clustered into smaller amount of cache-
138# lines, smaller than 160 and even 40, it turned out that for certain
139# plain-text pattern[s] or simply put chosen plain-text and given key
140# few cache-lines remain unaccessed during block operation. Now, if
141# attacker can figure out this access pattern, he can deduct the key
142# [or at least part of it]. The natural way to mitigate this kind of
143# attacks is to minimize the amount of cache-lines in S-box and/or
144# prefetch them to ensure that every one is accessed for more uniform
145# timing. But note that *if* plain-text was concealed in such way that
146# input to block function is distributed *uniformly*, then attack
147# wouldn't apply. Now note that some encryption modes, most notably
148# CBC, do mask the plain-text in this exact way [secure cipher output
149# is distributed uniformly]. Yes, one still might find input that
150# would reveal the information about given key, but if amount of
151# candidate inputs to be tried is larger than amount of possible key
152# combinations then attack becomes infeasible. This is why revised
153# AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk
154# of data is to be processed in one stroke. The current size limit of
155# 512 bytes is chosen to provide same [diminishigly low] probability
156# for cache-line to remain untouched in large chunk operation with
157# large S-box as for single block operation with compact S-box and
158# surely needs more careful consideration...
159#
160# As for asynchronous attacks. There are two flavours: attacker code
161# being interleaved with AES on hyper-threading CPU at *instruction*
162# level, and two processes time sharing single core. As for latter.
163# Two vectors. 1. Given that attacker process has higher priority,
164# yield execution to process performing AES just before timer fires
165# off the scheduler, immediately regain control of CPU and analyze the
166# cache state. For this attack to be efficient attacker would have to
167# effectively slow down the operation by several *orders* of magnitute,
168# by ratio of time slice to duration of handful of AES rounds, which
169# unlikely to remain unnoticed. Not to mention that this also means
170# that he would spend correspondigly more time to collect enough
171# statistical data to mount the attack. It's probably appropriate to
172# say that if adeversary reckons that this attack is beneficial and
173# risks to be noticed, you probably have larger problems having him
174# mere opportunity. In other words suggested code design expects you
175# to preclude/mitigate this attack by overall system security design.
176# 2. Attacker manages to make his code interrupt driven. In order for
177# this kind of attack to be feasible, interrupt rate has to be high
178# enough, again comparable to duration of handful of AES rounds. But
179# is there interrupt source of such rate? Hardly, not even 1Gbps NIC
180# generates interrupts at such raging rate...
181#
182# And now back to the former, hyper-threading CPU or more specifically
183# Intel P4. Recall that asynchronous attack implies that malicious
184# code instruments itself. And naturally instrumentation granularity
185# has be noticeably lower than duration of codepath accessing S-box.
186# Given that all cache-lines are accessed during that time that is.
187# Current implementation accesses *all* cache-lines within ~50 cycles
188# window, which is actually *less* than RDTSC latency on Intel P4!
189
190$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
191push(@INC,"${dir}","${dir}../../../perlasm");
192require "x86asm.pl";
193
194$output = pop;
195open OUT,">$output";
196*STDOUT=*OUT;
197
198&asm_init($ARGV[0],$x86only = $ARGV[$#ARGV] eq "386");
199&static_label("AES_Te");
200&static_label("AES_Td");
201
202$s0="eax";
203$s1="ebx";
204$s2="ecx";
205$s3="edx";
206$key="edi";
207$acc="esi";
208$tbl="ebp";
209
210# stack frame layout in _[x86|sse]_AES_* routines, frame is allocated
211# by caller
212$__ra=&DWP(0,"esp");	# return address
213$__s0=&DWP(4,"esp");	# s0 backing store
214$__s1=&DWP(8,"esp");	# s1 backing store
215$__s2=&DWP(12,"esp");	# s2 backing store
216$__s3=&DWP(16,"esp");	# s3 backing store
217$__key=&DWP(20,"esp");	# pointer to key schedule
218$__end=&DWP(24,"esp");	# pointer to end of key schedule
219$__tbl=&DWP(28,"esp");	# %ebp backing store
220
221# stack frame layout in AES_[en|crypt] routines, which differs from
222# above by 4 and overlaps by %ebp backing store
223$_tbl=&DWP(24,"esp");
224$_esp=&DWP(28,"esp");
225
226sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
227
228$speed_limit=512;	# chunks smaller than $speed_limit are
229			# processed with compact routine in CBC mode
230$small_footprint=1;	# $small_footprint=1 code is ~5% slower [on
231			# recent µ-archs], but ~5 times smaller!
232			# I favor compact code to minimize cache
233			# contention and in hope to "collect" 5% back
234			# in real-life applications...
235
236$vertical_spin=0;	# shift "verticaly" defaults to 0, because of
237			# its proof-of-concept status...
238# Note that there is no decvert(), as well as last encryption round is
239# performed with "horizontal" shifts. This is because this "vertical"
240# implementation [one which groups shifts on a given $s[i] to form a
241# "column," unlike "horizontal" one, which groups shifts on different
242# $s[i] to form a "row"] is work in progress. It was observed to run
243# few percents faster on Intel cores, but not AMD. On AMD K8 core it's
244# whole 12% slower:-( So we face a trade-off... Shall it be resolved
245# some day? Till then the code is considered experimental and by
246# default remains dormant...
247
248sub encvert()
249{ my ($te,@s) = @_;
250  my ($v0,$v1) = ($acc,$key);
251
252	&mov	($v0,$s[3]);				# copy s3
253	&mov	(&DWP(4,"esp"),$s[2]);			# save s2
254	&mov	($v1,$s[0]);				# copy s0
255	&mov	(&DWP(8,"esp"),$s[1]);			# save s1
256
257	&movz	($s[2],&HB($s[0]));
258	&and	($s[0],0xFF);
259	&mov	($s[0],&DWP(0,$te,$s[0],8));		# s0>>0
260	&shr	($v1,16);
261	&mov	($s[3],&DWP(3,$te,$s[2],8));		# s0>>8
262	&movz	($s[1],&HB($v1));
263	&and	($v1,0xFF);
264	&mov	($s[2],&DWP(2,$te,$v1,8));		# s0>>16
265	 &mov	($v1,$v0);
266	&mov	($s[1],&DWP(1,$te,$s[1],8));		# s0>>24
267
268	&and	($v0,0xFF);
269	&xor	($s[3],&DWP(0,$te,$v0,8));		# s3>>0
270	&movz	($v0,&HB($v1));
271	&shr	($v1,16);
272	&xor	($s[2],&DWP(3,$te,$v0,8));		# s3>>8
273	&movz	($v0,&HB($v1));
274	&and	($v1,0xFF);
275	&xor	($s[1],&DWP(2,$te,$v1,8));		# s3>>16
276	 &mov	($v1,&DWP(4,"esp"));			# restore s2
277	&xor	($s[0],&DWP(1,$te,$v0,8));		# s3>>24
278
279	&mov	($v0,$v1);
280	&and	($v1,0xFF);
281	&xor	($s[2],&DWP(0,$te,$v1,8));		# s2>>0
282	&movz	($v1,&HB($v0));
283	&shr	($v0,16);
284	&xor	($s[1],&DWP(3,$te,$v1,8));		# s2>>8
285	&movz	($v1,&HB($v0));
286	&and	($v0,0xFF);
287	&xor	($s[0],&DWP(2,$te,$v0,8));		# s2>>16
288	 &mov	($v0,&DWP(8,"esp"));			# restore s1
289	&xor	($s[3],&DWP(1,$te,$v1,8));		# s2>>24
290
291	&mov	($v1,$v0);
292	&and	($v0,0xFF);
293	&xor	($s[1],&DWP(0,$te,$v0,8));		# s1>>0
294	&movz	($v0,&HB($v1));
295	&shr	($v1,16);
296	&xor	($s[0],&DWP(3,$te,$v0,8));		# s1>>8
297	&movz	($v0,&HB($v1));
298	&and	($v1,0xFF);
299	&xor	($s[3],&DWP(2,$te,$v1,8));		# s1>>16
300	 &mov	($key,$__key);				# reincarnate v1 as key
301	&xor	($s[2],&DWP(1,$te,$v0,8));		# s1>>24
302}
303
304# Another experimental routine, which features "horizontal spin," but
305# eliminates one reference to stack. Strangely enough runs slower...
306sub enchoriz()
307{ my ($v0,$v1) = ($key,$acc);
308
309	&movz	($v0,&LB($s0));			#  3, 2, 1, 0*
310	&rotr	($s2,8);			#  8,11,10, 9
311	&mov	($v1,&DWP(0,$te,$v0,8));	#  0
312	&movz	($v0,&HB($s1));			#  7, 6, 5*, 4
313	&rotr	($s3,16);			# 13,12,15,14
314	&xor	($v1,&DWP(3,$te,$v0,8));	#  5
315	&movz	($v0,&HB($s2));			#  8,11,10*, 9
316	&rotr	($s0,16);			#  1, 0, 3, 2
317	&xor	($v1,&DWP(2,$te,$v0,8));	# 10
318	&movz	($v0,&HB($s3));			# 13,12,15*,14
319	&xor	($v1,&DWP(1,$te,$v0,8));	# 15, t[0] collected
320	&mov	($__s0,$v1);			# t[0] saved
321
322	&movz	($v0,&LB($s1));			#  7, 6, 5, 4*
323	&shr	($s1,16);			#  -, -, 7, 6
324	&mov	($v1,&DWP(0,$te,$v0,8));	#  4
325	&movz	($v0,&LB($s3));			# 13,12,15,14*
326	&xor	($v1,&DWP(2,$te,$v0,8));	# 14
327	&movz	($v0,&HB($s0));			#  1, 0, 3*, 2
328	&and	($s3,0xffff0000);		# 13,12, -, -
329	&xor	($v1,&DWP(1,$te,$v0,8));	#  3
330	&movz	($v0,&LB($s2));			#  8,11,10, 9*
331	&or	($s3,$s1);			# 13,12, 7, 6
332	&xor	($v1,&DWP(3,$te,$v0,8));	#  9, t[1] collected
333	&mov	($s1,$v1);			#  s[1]=t[1]
334
335	&movz	($v0,&LB($s0));			#  1, 0, 3, 2*
336	&shr	($s2,16);			#  -, -, 8,11
337	&mov	($v1,&DWP(2,$te,$v0,8));	#  2
338	&movz	($v0,&HB($s3));			# 13,12, 7*, 6
339	&xor	($v1,&DWP(1,$te,$v0,8));	#  7
340	&movz	($v0,&HB($s2));			#  -, -, 8*,11
341	&xor	($v1,&DWP(0,$te,$v0,8));	#  8
342	&mov	($v0,$s3);
343	&shr	($v0,24);			# 13
344	&xor	($v1,&DWP(3,$te,$v0,8));	# 13, t[2] collected
345
346	&movz	($v0,&LB($s2));			#  -, -, 8,11*
347	&shr	($s0,24);			#  1*
348	&mov	($s2,&DWP(1,$te,$v0,8));	# 11
349	&xor	($s2,&DWP(3,$te,$s0,8));	#  1
350	&mov	($s0,$__s0);			# s[0]=t[0]
351	&movz	($v0,&LB($s3));			# 13,12, 7, 6*
352	&shr	($s3,16);			#   ,  ,13,12
353	&xor	($s2,&DWP(2,$te,$v0,8));	#  6
354	&mov	($key,$__key);			# reincarnate v0 as key
355	&and	($s3,0xff);			#   ,  ,13,12*
356	&mov	($s3,&DWP(0,$te,$s3,8));	# 12
357	&xor	($s3,$s2);			# s[2]=t[3] collected
358	&mov	($s2,$v1);			# s[2]=t[2]
359}
360
361# More experimental code... SSE one... Even though this one eliminates
362# *all* references to stack, it's not faster...
363sub sse_encbody()
364{
365	&movz	($acc,&LB("eax"));		#  0
366	&mov	("ecx",&DWP(0,$tbl,$acc,8));	#  0
367	&pshufw	("mm2","mm0",0x0d);		#  7, 6, 3, 2
368	&movz	("edx",&HB("eax"));		#  1
369	&mov	("edx",&DWP(3,$tbl,"edx",8));	#  1
370	&shr	("eax",16);			#  5, 4
371
372	&movz	($acc,&LB("ebx"));		# 10
373	&xor	("ecx",&DWP(2,$tbl,$acc,8));	# 10
374	&pshufw	("mm6","mm4",0x08);		# 13,12, 9, 8
375	&movz	($acc,&HB("ebx"));		# 11
376	&xor	("edx",&DWP(1,$tbl,$acc,8));	# 11
377	&shr	("ebx",16);			# 15,14
378
379	&movz	($acc,&HB("eax"));		#  5
380	&xor	("ecx",&DWP(3,$tbl,$acc,8));	#  5
381	&movq	("mm3",QWP(16,$key));
382	&movz	($acc,&HB("ebx"));		# 15
383	&xor	("ecx",&DWP(1,$tbl,$acc,8));	# 15
384	&movd	("mm0","ecx");			# t[0] collected
385
386	&movz	($acc,&LB("eax"));		#  4
387	&mov	("ecx",&DWP(0,$tbl,$acc,8));	#  4
388	&movd	("eax","mm2");			#  7, 6, 3, 2
389	&movz	($acc,&LB("ebx"));		# 14
390	&xor	("ecx",&DWP(2,$tbl,$acc,8));	# 14
391	&movd	("ebx","mm6");			# 13,12, 9, 8
392
393	&movz	($acc,&HB("eax"));		#  3
394	&xor	("ecx",&DWP(1,$tbl,$acc,8));	#  3
395	&movz	($acc,&HB("ebx"));		#  9
396	&xor	("ecx",&DWP(3,$tbl,$acc,8));	#  9
397	&movd	("mm1","ecx");			# t[1] collected
398
399	&movz	($acc,&LB("eax"));		#  2
400	&mov	("ecx",&DWP(2,$tbl,$acc,8));	#  2
401	&shr	("eax",16);			#  7, 6
402	&punpckldq	("mm0","mm1");		# t[0,1] collected
403	&movz	($acc,&LB("ebx"));		#  8
404	&xor	("ecx",&DWP(0,$tbl,$acc,8));	#  8
405	&shr	("ebx",16);			# 13,12
406
407	&movz	($acc,&HB("eax"));		#  7
408	&xor	("ecx",&DWP(1,$tbl,$acc,8));	#  7
409	&pxor	("mm0","mm3");
410	&movz	("eax",&LB("eax"));		#  6
411	&xor	("edx",&DWP(2,$tbl,"eax",8));	#  6
412	&pshufw	("mm1","mm0",0x08);		#  5, 4, 1, 0
413	&movz	($acc,&HB("ebx"));		# 13
414	&xor	("ecx",&DWP(3,$tbl,$acc,8));	# 13
415	&xor	("ecx",&DWP(24,$key));		# t[2]
416	&movd	("mm4","ecx");			# t[2] collected
417	&movz	("ebx",&LB("ebx"));		# 12
418	&xor	("edx",&DWP(0,$tbl,"ebx",8));	# 12
419	&shr	("ecx",16);
420	&movd	("eax","mm1");			#  5, 4, 1, 0
421	&mov	("ebx",&DWP(28,$key));		# t[3]
422	&xor	("ebx","edx");
423	&movd	("mm5","ebx");			# t[3] collected
424	&and	("ebx",0xffff0000);
425	&or	("ebx","ecx");
426
427	&punpckldq	("mm4","mm5");		# t[2,3] collected
428}
429
430######################################################################
431# "Compact" block function
432######################################################################
433
434sub enccompact()
435{ my $Fn = \&mov;
436  while ($#_>5) { pop(@_); $Fn=sub{}; }
437  my ($i,$te,@s)=@_;
438  my $tmp = $key;
439  my $out = $i==3?$s[0]:$acc;
440
441	# $Fn is used in first compact round and its purpose is to
442	# void restoration of some values from stack, so that after
443	# 4xenccompact with extra argument $key value is left there...
444	if ($i==3)  {	&$Fn	($key,$__key);			}##%edx
445	else        {	&mov	($out,$s[0]);			}
446			&and	($out,0xFF);
447	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
448	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
449			&movz	($out,&BP(-128,$te,$out,1));
450
451	if ($i==3)  {	$tmp=$s[1];				}##%eax
452			&movz	($tmp,&HB($s[1]));
453			&movz	($tmp,&BP(-128,$te,$tmp,1));
454			&shl	($tmp,8);
455			&xor	($out,$tmp);
456
457	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
458	else        {	&mov	($tmp,$s[2]);
459			&shr	($tmp,16);			}
460	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
461			&and	($tmp,0xFF);
462			&movz	($tmp,&BP(-128,$te,$tmp,1));
463			&shl	($tmp,16);
464			&xor	($out,$tmp);
465
466	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
467	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
468	else        {	&mov	($tmp,$s[3]);
469			&shr	($tmp,24);			}
470			&movz	($tmp,&BP(-128,$te,$tmp,1));
471			&shl	($tmp,24);
472			&xor	($out,$tmp);
473	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
474	if ($i==3)  {	&mov	($s[3],$acc);			}
475	&comment();
476}
477
478sub enctransform()
479{ my @s = ($s0,$s1,$s2,$s3);
480  my $i = shift;
481  my $tmp = $tbl;
482  my $r2  = $key ;
483
484	&and	($tmp,$s[$i]);
485	&lea	($r2,&DWP(0,$s[$i],$s[$i]));
486	&mov	($acc,$tmp);
487	&shr	($tmp,7);
488	&and	($r2,0xfefefefe);
489	&sub	($acc,$tmp);
490	&mov	($tmp,$s[$i]);
491	&and	($acc,0x1b1b1b1b);
492	&rotr	($tmp,16);
493	&xor	($acc,$r2);	# r2
494	&mov	($r2,$s[$i]);
495
496	&xor	($s[$i],$acc);	# r0 ^ r2
497	&rotr	($r2,16+8);
498	&xor	($acc,$tmp);
499	&rotl	($s[$i],24);
500	&xor	($acc,$r2);
501	&mov	($tmp,0x80808080)	if ($i!=1);
502	&xor	($s[$i],$acc);	# ROTATE(r2^r0,24) ^ r2
503}
504
505&function_begin_B("_x86_AES_encrypt_compact");
506	# note that caller is expected to allocate stack frame for me!
507	&mov	($__key,$key);			# save key
508
509	&xor	($s0,&DWP(0,$key));		# xor with key
510	&xor	($s1,&DWP(4,$key));
511	&xor	($s2,&DWP(8,$key));
512	&xor	($s3,&DWP(12,$key));
513
514	&mov	($acc,&DWP(240,$key));		# load key->rounds
515	&lea	($acc,&DWP(-2,$acc,$acc));
516	&lea	($acc,&DWP(0,$key,$acc,8));
517	&mov	($__end,$acc);			# end of key schedule
518
519	# prefetch Te4
520	&mov	($key,&DWP(0-128,$tbl));
521	&mov	($acc,&DWP(32-128,$tbl));
522	&mov	($key,&DWP(64-128,$tbl));
523	&mov	($acc,&DWP(96-128,$tbl));
524	&mov	($key,&DWP(128-128,$tbl));
525	&mov	($acc,&DWP(160-128,$tbl));
526	&mov	($key,&DWP(192-128,$tbl));
527	&mov	($acc,&DWP(224-128,$tbl));
528
529	&set_label("loop",16);
530
531		&enccompact(0,$tbl,$s0,$s1,$s2,$s3,1);
532		&enccompact(1,$tbl,$s1,$s2,$s3,$s0,1);
533		&enccompact(2,$tbl,$s2,$s3,$s0,$s1,1);
534		&enccompact(3,$tbl,$s3,$s0,$s1,$s2,1);
535		&mov	($tbl,0x80808080);
536		&enctransform(2);
537		&enctransform(3);
538		&enctransform(0);
539		&enctransform(1);
540		&mov 	($key,$__key);
541		&mov	($tbl,$__tbl);
542		&add	($key,16);		# advance rd_key
543		&xor	($s0,&DWP(0,$key));
544		&xor	($s1,&DWP(4,$key));
545		&xor	($s2,&DWP(8,$key));
546		&xor	($s3,&DWP(12,$key));
547
548	&cmp	($key,$__end);
549	&mov	($__key,$key);
550	&jb	(&label("loop"));
551
552	&enccompact(0,$tbl,$s0,$s1,$s2,$s3);
553	&enccompact(1,$tbl,$s1,$s2,$s3,$s0);
554	&enccompact(2,$tbl,$s2,$s3,$s0,$s1);
555	&enccompact(3,$tbl,$s3,$s0,$s1,$s2);
556
557	&xor	($s0,&DWP(16,$key));
558	&xor	($s1,&DWP(20,$key));
559	&xor	($s2,&DWP(24,$key));
560	&xor	($s3,&DWP(28,$key));
561
562	&ret	();
563&function_end_B("_x86_AES_encrypt_compact");
564
565######################################################################
566# "Compact" SSE block function.
567######################################################################
568#
569# Performance is not actually extraordinary in comparison to pure
570# x86 code. In particular encrypt performance is virtually the same.
571# Decrypt performance on the other hand is 15-20% better on newer
572# µ-archs [but we're thankful for *any* improvement here], and ~50%
573# better on PIII:-) And additionally on the pros side this code
574# eliminates redundant references to stack and thus relieves/
575# minimizes the pressure on the memory bus.
576#
577# MMX register layout                           lsb
578# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
579# |          mm4          |          mm0          |
580# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
581# |     s3    |     s2    |     s1    |     s0    |
582# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
583# |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
584# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
585#
586# Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8.
587# In this terms encryption and decryption "compact" permutation
588# matrices can be depicted as following:
589#
590# encryption              lsb	# decryption              lsb
591# +----++----+----+----+----+	# +----++----+----+----+----+
592# | t0 || 15 | 10 |  5 |  0 |	# | t0 ||  7 | 10 | 13 |  0 |
593# +----++----+----+----+----+	# +----++----+----+----+----+
594# | t1 ||  3 | 14 |  9 |  4 |	# | t1 || 11 | 14 |  1 |  4 |
595# +----++----+----+----+----+	# +----++----+----+----+----+
596# | t2 ||  7 |  2 | 13 |  8 |	# | t2 || 15 |  2 |  5 |  8 |
597# +----++----+----+----+----+	# +----++----+----+----+----+
598# | t3 || 11 |  6 |  1 | 12 |	# | t3 ||  3 |  6 |  9 | 12 |
599# +----++----+----+----+----+	# +----++----+----+----+----+
600#
601######################################################################
602# Why not xmm registers? Short answer. It was actually tested and
603# was not any faster, but *contrary*, most notably on Intel CPUs.
604# Longer answer. Main advantage of using mm registers is that movd
605# latency is lower, especially on Intel P4. While arithmetic
606# instructions are twice as many, they can be scheduled every cycle
607# and not every second one when they are operating on xmm register,
608# so that "arithmetic throughput" remains virtually the same. And
609# finally the code can be executed even on elder SSE-only CPUs:-)
610
611sub sse_enccompact()
612{
613	&pshufw	("mm1","mm0",0x08);		#  5, 4, 1, 0
614	&pshufw	("mm5","mm4",0x0d);		# 15,14,11,10
615	&movd	("eax","mm1");			#  5, 4, 1, 0
616	&movd	("ebx","mm5");			# 15,14,11,10
617	&mov	($__key,$key);
618
619	&movz	($acc,&LB("eax"));		#  0
620	&movz	("edx",&HB("eax"));		#  1
621	&pshufw	("mm2","mm0",0x0d);		#  7, 6, 3, 2
622	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  0
623	&movz	($key,&LB("ebx"));		# 10
624	&movz	("edx",&BP(-128,$tbl,"edx",1));	#  1
625	&shr	("eax",16);			#  5, 4
626	&shl	("edx",8);			#  1
627
628	&movz	($acc,&BP(-128,$tbl,$key,1));	# 10
629	&movz	($key,&HB("ebx"));		# 11
630	&shl	($acc,16);			# 10
631	&pshufw	("mm6","mm4",0x08);		# 13,12, 9, 8
632	&or	("ecx",$acc);			# 10
633	&movz	($acc,&BP(-128,$tbl,$key,1));	# 11
634	&movz	($key,&HB("eax"));		#  5
635	&shl	($acc,24);			# 11
636	&shr	("ebx",16);			# 15,14
637	&or	("edx",$acc);			# 11
638
639	&movz	($acc,&BP(-128,$tbl,$key,1));	#  5
640	&movz	($key,&HB("ebx"));		# 15
641	&shl	($acc,8);			#  5
642	&or	("ecx",$acc);			#  5
643	&movz	($acc,&BP(-128,$tbl,$key,1));	# 15
644	&movz	($key,&LB("eax"));		#  4
645	&shl	($acc,24);			# 15
646	&or	("ecx",$acc);			# 15
647
648	&movz	($acc,&BP(-128,$tbl,$key,1));	#  4
649	&movz	($key,&LB("ebx"));		# 14
650	&movd	("eax","mm2");			#  7, 6, 3, 2
651	&movd	("mm0","ecx");			# t[0] collected
652	&movz	("ecx",&BP(-128,$tbl,$key,1));	# 14
653	&movz	($key,&HB("eax"));		#  3
654	&shl	("ecx",16);			# 14
655	&movd	("ebx","mm6");			# 13,12, 9, 8
656	&or	("ecx",$acc);			# 14
657
658	&movz	($acc,&BP(-128,$tbl,$key,1));	#  3
659	&movz	($key,&HB("ebx"));		#  9
660	&shl	($acc,24);			#  3
661	&or	("ecx",$acc);			#  3
662	&movz	($acc,&BP(-128,$tbl,$key,1));	#  9
663	&movz	($key,&LB("ebx"));		#  8
664	&shl	($acc,8);			#  9
665	&shr	("ebx",16);			# 13,12
666	&or	("ecx",$acc);			#  9
667
668	&movz	($acc,&BP(-128,$tbl,$key,1));	#  8
669	&movz	($key,&LB("eax"));		#  2
670	&shr	("eax",16);			#  7, 6
671	&movd	("mm1","ecx");			# t[1] collected
672	&movz	("ecx",&BP(-128,$tbl,$key,1));	#  2
673	&movz	($key,&HB("eax"));		#  7
674	&shl	("ecx",16);			#  2
675	&and	("eax",0xff);			#  6
676	&or	("ecx",$acc);			#  2
677
678	&punpckldq	("mm0","mm1");		# t[0,1] collected
679
680	&movz	($acc,&BP(-128,$tbl,$key,1));	#  7
681	&movz	($key,&HB("ebx"));		# 13
682	&shl	($acc,24);			#  7
683	&and	("ebx",0xff);			# 12
684	&movz	("eax",&BP(-128,$tbl,"eax",1));	#  6
685	&or	("ecx",$acc);			#  7
686	&shl	("eax",16);			#  6
687	&movz	($acc,&BP(-128,$tbl,$key,1));	# 13
688	&or	("edx","eax");			#  6
689	&shl	($acc,8);			# 13
690	&movz	("ebx",&BP(-128,$tbl,"ebx",1));	# 12
691	&or	("ecx",$acc);			# 13
692	&or	("edx","ebx");			# 12
693	&mov	($key,$__key);
694	&movd	("mm4","ecx");			# t[2] collected
695	&movd	("mm5","edx");			# t[3] collected
696
697	&punpckldq	("mm4","mm5");		# t[2,3] collected
698}
699
700					if (!$x86only) {
701&function_begin_B("_sse_AES_encrypt_compact");
702	&pxor	("mm0",&QWP(0,$key));	#  7, 6, 5, 4, 3, 2, 1, 0
703	&pxor	("mm4",&QWP(8,$key));	# 15,14,13,12,11,10, 9, 8
704
705	# note that caller is expected to allocate stack frame for me!
706	&mov	($acc,&DWP(240,$key));		# load key->rounds
707	&lea	($acc,&DWP(-2,$acc,$acc));
708	&lea	($acc,&DWP(0,$key,$acc,8));
709	&mov	($__end,$acc);			# end of key schedule
710
711	&mov	($s0,0x1b1b1b1b);		# magic constant
712	&mov	(&DWP(8,"esp"),$s0);
713	&mov	(&DWP(12,"esp"),$s0);
714
715	# prefetch Te4
716	&mov	($s0,&DWP(0-128,$tbl));
717	&mov	($s1,&DWP(32-128,$tbl));
718	&mov	($s2,&DWP(64-128,$tbl));
719	&mov	($s3,&DWP(96-128,$tbl));
720	&mov	($s0,&DWP(128-128,$tbl));
721	&mov	($s1,&DWP(160-128,$tbl));
722	&mov	($s2,&DWP(192-128,$tbl));
723	&mov	($s3,&DWP(224-128,$tbl));
724
725	&set_label("loop",16);
726		&sse_enccompact();
727		&add	($key,16);
728		&cmp	($key,$__end);
729		&ja	(&label("out"));
730
731		&movq	("mm2",&QWP(8,"esp"));
732		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
733		&movq	("mm1","mm0");		&movq	("mm5","mm4");	# r0
734		&pcmpgtb("mm3","mm0");		&pcmpgtb("mm7","mm4");
735		&pand	("mm3","mm2");		&pand	("mm7","mm2");
736		&pshufw	("mm2","mm0",0xb1);	&pshufw	("mm6","mm4",0xb1);# ROTATE(r0,16)
737		&paddb	("mm0","mm0");		&paddb	("mm4","mm4");
738		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# = r2
739		&pshufw	("mm3","mm2",0xb1);	&pshufw	("mm7","mm6",0xb1);# r0
740		&pxor	("mm1","mm0");		&pxor	("mm5","mm4");	# r0^r2
741		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= ROTATE(r0,16)
742
743		&movq	("mm2","mm3");		&movq	("mm6","mm7");
744		&pslld	("mm3",8);		&pslld	("mm7",8);
745		&psrld	("mm2",24);		&psrld	("mm6",24);
746		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= r0<<8
747		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= r0>>24
748
749		&movq	("mm3","mm1");		&movq	("mm7","mm5");
750		&movq	("mm2",&QWP(0,$key));	&movq	("mm6",&QWP(8,$key));
751		&psrld	("mm1",8);		&psrld	("mm5",8);
752		&mov	($s0,&DWP(0-128,$tbl));
753		&pslld	("mm3",24);		&pslld	("mm7",24);
754		&mov	($s1,&DWP(64-128,$tbl));
755		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= (r2^r0)<<8
756		&mov	($s2,&DWP(128-128,$tbl));
757		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= (r2^r0)>>24
758		&mov	($s3,&DWP(192-128,$tbl));
759
760		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");
761	&jmp	(&label("loop"));
762
763	&set_label("out",16);
764	&pxor	("mm0",&QWP(0,$key));
765	&pxor	("mm4",&QWP(8,$key));
766
767	&ret	();
768&function_end_B("_sse_AES_encrypt_compact");
769					}
770
771######################################################################
772# Vanilla block function.
773######################################################################
774
775sub encstep()
776{ my ($i,$te,@s) = @_;
777  my $tmp = $key;
778  my $out = $i==3?$s[0]:$acc;
779
780	# lines marked with #%e?x[i] denote "reordered" instructions...
781	if ($i==3)  {	&mov	($key,$__key);			}##%edx
782	else        {	&mov	($out,$s[0]);
783			&and	($out,0xFF);			}
784	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
785	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
786			&mov	($out,&DWP(0,$te,$out,8));
787
788	if ($i==3)  {	$tmp=$s[1];				}##%eax
789			&movz	($tmp,&HB($s[1]));
790			&xor	($out,&DWP(3,$te,$tmp,8));
791
792	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
793	else        {	&mov	($tmp,$s[2]);
794			&shr	($tmp,16);			}
795	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
796			&and	($tmp,0xFF);
797			&xor	($out,&DWP(2,$te,$tmp,8));
798
799	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
800	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
801	else        {	&mov	($tmp,$s[3]);
802			&shr	($tmp,24)			}
803			&xor	($out,&DWP(1,$te,$tmp,8));
804	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
805	if ($i==3)  {	&mov	($s[3],$acc);			}
806			&comment();
807}
808
809sub enclast()
810{ my ($i,$te,@s)=@_;
811  my $tmp = $key;
812  my $out = $i==3?$s[0]:$acc;
813
814	if ($i==3)  {	&mov	($key,$__key);			}##%edx
815	else        {	&mov	($out,$s[0]);			}
816			&and	($out,0xFF);
817	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
818	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
819			&mov	($out,&DWP(2,$te,$out,8));
820			&and	($out,0x000000ff);
821
822	if ($i==3)  {	$tmp=$s[1];				}##%eax
823			&movz	($tmp,&HB($s[1]));
824			&mov	($tmp,&DWP(0,$te,$tmp,8));
825			&and	($tmp,0x0000ff00);
826			&xor	($out,$tmp);
827
828	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
829	else        {	&mov	($tmp,$s[2]);
830			&shr	($tmp,16);			}
831	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
832			&and	($tmp,0xFF);
833			&mov	($tmp,&DWP(0,$te,$tmp,8));
834			&and	($tmp,0x00ff0000);
835			&xor	($out,$tmp);
836
837	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
838	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
839	else        {	&mov	($tmp,$s[3]);
840			&shr	($tmp,24);			}
841			&mov	($tmp,&DWP(2,$te,$tmp,8));
842			&and	($tmp,0xff000000);
843			&xor	($out,$tmp);
844	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
845	if ($i==3)  {	&mov	($s[3],$acc);			}
846}
847
848&function_begin_B("_x86_AES_encrypt");
849	if ($vertical_spin) {
850		# I need high parts of volatile registers to be accessible...
851		&exch	($s1="edi",$key="ebx");
852		&mov	($s2="esi",$acc="ecx");
853	}
854
855	# note that caller is expected to allocate stack frame for me!
856	&mov	($__key,$key);			# save key
857
858	&xor	($s0,&DWP(0,$key));		# xor with key
859	&xor	($s1,&DWP(4,$key));
860	&xor	($s2,&DWP(8,$key));
861	&xor	($s3,&DWP(12,$key));
862
863	&mov	($acc,&DWP(240,$key));		# load key->rounds
864
865	if ($small_footprint) {
866	    &lea	($acc,&DWP(-2,$acc,$acc));
867	    &lea	($acc,&DWP(0,$key,$acc,8));
868	    &mov	($__end,$acc);		# end of key schedule
869
870	    &set_label("loop",16);
871		if ($vertical_spin) {
872		    &encvert($tbl,$s0,$s1,$s2,$s3);
873		} else {
874		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
875		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
876		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
877		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
878		}
879		&add	($key,16);		# advance rd_key
880		&xor	($s0,&DWP(0,$key));
881		&xor	($s1,&DWP(4,$key));
882		&xor	($s2,&DWP(8,$key));
883		&xor	($s3,&DWP(12,$key));
884	    &cmp	($key,$__end);
885	    &mov	($__key,$key);
886	    &jb		(&label("loop"));
887	}
888	else {
889	    &cmp	($acc,10);
890	    &jle	(&label("10rounds"));
891	    &cmp	($acc,12);
892	    &jle	(&label("12rounds"));
893
894	&set_label("14rounds",4);
895	    for ($i=1;$i<3;$i++) {
896		if ($vertical_spin) {
897		    &encvert($tbl,$s0,$s1,$s2,$s3);
898		} else {
899		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
900		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
901		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
902		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
903		}
904		&xor	($s0,&DWP(16*$i+0,$key));
905		&xor	($s1,&DWP(16*$i+4,$key));
906		&xor	($s2,&DWP(16*$i+8,$key));
907		&xor	($s3,&DWP(16*$i+12,$key));
908	    }
909	    &add	($key,32);
910	    &mov	($__key,$key);		# advance rd_key
911	&set_label("12rounds",4);
912	    for ($i=1;$i<3;$i++) {
913		if ($vertical_spin) {
914		    &encvert($tbl,$s0,$s1,$s2,$s3);
915		} else {
916		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
917		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
918		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
919		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
920		}
921		&xor	($s0,&DWP(16*$i+0,$key));
922		&xor	($s1,&DWP(16*$i+4,$key));
923		&xor	($s2,&DWP(16*$i+8,$key));
924		&xor	($s3,&DWP(16*$i+12,$key));
925	    }
926	    &add	($key,32);
927	    &mov	($__key,$key);		# advance rd_key
928	&set_label("10rounds",4);
929	    for ($i=1;$i<10;$i++) {
930		if ($vertical_spin) {
931		    &encvert($tbl,$s0,$s1,$s2,$s3);
932		} else {
933		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
934		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
935		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
936		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
937		}
938		&xor	($s0,&DWP(16*$i+0,$key));
939		&xor	($s1,&DWP(16*$i+4,$key));
940		&xor	($s2,&DWP(16*$i+8,$key));
941		&xor	($s3,&DWP(16*$i+12,$key));
942	    }
943	}
944
945	if ($vertical_spin) {
946	    # "reincarnate" some registers for "horizontal" spin...
947	    &mov	($s1="ebx",$key="edi");
948	    &mov	($s2="ecx",$acc="esi");
949	}
950	&enclast(0,$tbl,$s0,$s1,$s2,$s3);
951	&enclast(1,$tbl,$s1,$s2,$s3,$s0);
952	&enclast(2,$tbl,$s2,$s3,$s0,$s1);
953	&enclast(3,$tbl,$s3,$s0,$s1,$s2);
954
955	&add	($key,$small_footprint?16:160);
956	&xor	($s0,&DWP(0,$key));
957	&xor	($s1,&DWP(4,$key));
958	&xor	($s2,&DWP(8,$key));
959	&xor	($s3,&DWP(12,$key));
960
961	&ret	();
962
963&set_label("AES_Te",64);	# Yes! I keep it in the code segment!
964	&_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
965	&_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
966	&_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
967	&_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
968	&_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
969	&_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
970	&_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
971	&_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
972	&_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
973	&_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
974	&_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
975	&_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
976	&_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
977	&_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
978	&_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
979	&_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
980	&_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
981	&_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
982	&_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
983	&_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
984	&_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
985	&_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
986	&_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
987	&_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
988	&_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
989	&_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
990	&_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
991	&_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
992	&_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
993	&_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
994	&_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
995	&_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
996	&_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
997	&_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
998	&_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
999	&_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
1000	&_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
1001	&_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
1002	&_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
1003	&_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
1004	&_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
1005	&_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
1006	&_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
1007	&_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
1008	&_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
1009	&_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
1010	&_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
1011	&_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
1012	&_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
1013	&_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
1014	&_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
1015	&_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
1016	&_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
1017	&_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
1018	&_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
1019	&_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
1020	&_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
1021	&_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
1022	&_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
1023	&_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
1024	&_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
1025	&_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
1026	&_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
1027	&_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
1028
1029#Te4	# four copies of Te4 to choose from to avoid L1 aliasing
1030	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1031	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1032	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1033	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1034	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1035	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1036	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1037	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1038	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1039	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1040	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1041	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1042	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1043	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1044	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1045	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1046	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1047	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1048	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1049	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1050	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1051	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1052	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1053	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1054	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1055	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1056	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1057	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1058	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1059	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1060	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1061	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1062
1063	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1064	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1065	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1066	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1067	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1068	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1069	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1070	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1071	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1072	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1073	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1074	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1075	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1076	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1077	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1078	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1079	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1080	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1081	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1082	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1083	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1084	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1085	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1086	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1087	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1088	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1089	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1090	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1091	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1092	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1093	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1094	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1095
1096	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1097	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1098	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1099	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1100	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1101	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1102	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1103	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1104	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1105	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1106	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1107	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1108	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1109	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1110	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1111	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1112	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1113	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1114	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1115	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1116	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1117	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1118	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1119	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1120	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1121	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1122	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1123	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1124	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1125	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1126	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1127	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1128
1129	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1130	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1131	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1132	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1133	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1134	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1135	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1136	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1137	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1138	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1139	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1140	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1141	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1142	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1143	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1144	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1145	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1146	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1147	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1148	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1149	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1150	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1151	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1152	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1153	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1154	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1155	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1156	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1157	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1158	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1159	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1160	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1161#rcon:
1162	&data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
1163	&data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
1164	&data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000);
1165	&data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000);
1166&function_end_B("_x86_AES_encrypt");
1167
1168# void asm_AES_encrypt (const void *inp,void *out,const AES_KEY *key);
1169&function_begin("asm_AES_encrypt");
1170	&mov	($acc,&wparam(0));		# load inp
1171	&mov	($key,&wparam(2));		# load key
1172
1173	&mov	($s0,"esp");
1174	&sub	("esp",36);
1175	&and	("esp",-64);			# align to cache-line
1176
1177	# place stack frame just "above" the key schedule
1178	&lea	($s1,&DWP(-64-63,$key));
1179	&sub	($s1,"esp");
1180	&neg	($s1);
1181	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
1182	&sub	("esp",$s1);
1183	&add	("esp",4);	# 4 is reserved for caller's return address
1184	&mov	($_esp,$s0);			# save stack pointer
1185
1186	&call   (&label("pic_point"));          # make it PIC!
1187	&set_label("pic_point");
1188	&blindpop($tbl);
1189	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only);
1190	&lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
1191
1192	# pick Te4 copy which can't "overlap" with stack frame or key schedule
1193	&lea	($s1,&DWP(768-4,"esp"));
1194	&sub	($s1,$tbl);
1195	&and	($s1,0x300);
1196	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
1197
1198					if (!$x86only) {
1199	&bt	(&DWP(0,$s0),25);	# check for SSE bit
1200	&jnc	(&label("x86"));
1201
1202	&movq	("mm0",&QWP(0,$acc));
1203	&movq	("mm4",&QWP(8,$acc));
1204	&call	("_sse_AES_encrypt_compact");
1205	&mov	("esp",$_esp);			# restore stack pointer
1206	&mov	($acc,&wparam(1));		# load out
1207	&movq	(&QWP(0,$acc),"mm0");		# write output data
1208	&movq	(&QWP(8,$acc),"mm4");
1209	&emms	();
1210	&function_end_A();
1211					}
1212	&set_label("x86",16);
1213	&mov	($_tbl,$tbl);
1214	&mov	($s0,&DWP(0,$acc));		# load input data
1215	&mov	($s1,&DWP(4,$acc));
1216	&mov	($s2,&DWP(8,$acc));
1217	&mov	($s3,&DWP(12,$acc));
1218	&call	("_x86_AES_encrypt_compact");
1219	&mov	("esp",$_esp);			# restore stack pointer
1220	&mov	($acc,&wparam(1));		# load out
1221	&mov	(&DWP(0,$acc),$s0);		# write output data
1222	&mov	(&DWP(4,$acc),$s1);
1223	&mov	(&DWP(8,$acc),$s2);
1224	&mov	(&DWP(12,$acc),$s3);
1225&function_end("asm_AES_encrypt");
1226
1227#--------------------------------------------------------------------#
1228
1229######################################################################
1230# "Compact" block function
1231######################################################################
1232
1233sub deccompact()
1234{ my $Fn = \&mov;
1235  while ($#_>5) { pop(@_); $Fn=sub{}; }
1236  my ($i,$td,@s)=@_;
1237  my $tmp = $key;
1238  my $out = $i==3?$s[0]:$acc;
1239
1240	# $Fn is used in first compact round and its purpose is to
1241	# void restoration of some values from stack, so that after
1242	# 4xdeccompact with extra argument $key, $s0 and $s1 values
1243	# are left there...
1244	if($i==3)   {	&$Fn	($key,$__key);			}
1245	else        {	&mov	($out,$s[0]);			}
1246			&and	($out,0xFF);
1247			&movz	($out,&BP(-128,$td,$out,1));
1248
1249	if ($i==3)  {	$tmp=$s[1];				}
1250			&movz	($tmp,&HB($s[1]));
1251			&movz	($tmp,&BP(-128,$td,$tmp,1));
1252			&shl	($tmp,8);
1253			&xor	($out,$tmp);
1254
1255	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1256	else        {	mov	($tmp,$s[2]);			}
1257			&shr	($tmp,16);
1258			&and	($tmp,0xFF);
1259			&movz	($tmp,&BP(-128,$td,$tmp,1));
1260			&shl	($tmp,16);
1261			&xor	($out,$tmp);
1262
1263	if ($i==3)  {	$tmp=$s[3]; &$Fn ($s[2],$__s1);		}
1264	else        {	&mov	($tmp,$s[3]);			}
1265			&shr	($tmp,24);
1266			&movz	($tmp,&BP(-128,$td,$tmp,1));
1267			&shl	($tmp,24);
1268			&xor	($out,$tmp);
1269	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1270	if ($i==3)  {	&$Fn	($s[3],$__s0);			}
1271}
1272
1273# must be called with 2,3,0,1 as argument sequence!!!
1274sub dectransform()
1275{ my @s = ($s0,$s1,$s2,$s3);
1276  my $i = shift;
1277  my $tmp = $key;
1278  my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1);
1279  my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1);
1280  my $tp8 = $tbl;
1281
1282	&mov	($tmp,0x80808080);
1283	&and	($tmp,$s[$i]);
1284	&mov	($acc,$tmp);
1285	&shr	($tmp,7);
1286	&lea	($tp2,&DWP(0,$s[$i],$s[$i]));
1287	&sub	($acc,$tmp);
1288	&and	($tp2,0xfefefefe);
1289	&and	($acc,0x1b1b1b1b);
1290	&xor	($tp2,$acc);
1291	&mov	($tmp,0x80808080);
1292
1293	&and	($tmp,$tp2);
1294	&mov	($acc,$tmp);
1295	&shr	($tmp,7);
1296	&lea	($tp4,&DWP(0,$tp2,$tp2));
1297	&sub	($acc,$tmp);
1298	&and	($tp4,0xfefefefe);
1299	&and	($acc,0x1b1b1b1b);
1300	 &xor	($tp2,$s[$i]);	# tp2^tp1
1301	&xor	($tp4,$acc);
1302	&mov	($tmp,0x80808080);
1303
1304	&and	($tmp,$tp4);
1305	&mov	($acc,$tmp);
1306	&shr	($tmp,7);
1307	&lea	($tp8,&DWP(0,$tp4,$tp4));
1308	&sub	($acc,$tmp);
1309	&and	($tp8,0xfefefefe);
1310	&and	($acc,0x1b1b1b1b);
1311	 &xor	($tp4,$s[$i]);	# tp4^tp1
1312	 &rotl	($s[$i],8);	# = ROTATE(tp1,8)
1313	&xor	($tp8,$acc);
1314
1315	&xor	($s[$i],$tp2);
1316	&xor	($tp2,$tp8);
1317	&xor	($s[$i],$tp4);
1318	&xor	($tp4,$tp8);
1319	&rotl	($tp2,24);
1320	&xor	($s[$i],$tp8);	# ^= tp8^(tp4^tp1)^(tp2^tp1)
1321	&rotl	($tp4,16);
1322	&xor	($s[$i],$tp2);	# ^= ROTATE(tp8^tp2^tp1,24)
1323	&rotl	($tp8,8);
1324	&xor	($s[$i],$tp4);	# ^= ROTATE(tp8^tp4^tp1,16)
1325	 &mov	($s[0],$__s0)			if($i==2); #prefetch $s0
1326	 &mov	($s[1],$__s1)			if($i==3); #prefetch $s1
1327	 &mov	($s[2],$__s2)			if($i==1);
1328	&xor	($s[$i],$tp8);	# ^= ROTATE(tp8,8)
1329
1330	&mov	($s[3],$__s3)			if($i==1);
1331	&mov	(&DWP(4+4*$i,"esp"),$s[$i])	if($i>=2);
1332}
1333
1334&function_begin_B("_x86_AES_decrypt_compact");
1335	# note that caller is expected to allocate stack frame for me!
1336	&mov	($__key,$key);			# save key
1337
1338	&xor	($s0,&DWP(0,$key));		# xor with key
1339	&xor	($s1,&DWP(4,$key));
1340	&xor	($s2,&DWP(8,$key));
1341	&xor	($s3,&DWP(12,$key));
1342
1343	&mov	($acc,&DWP(240,$key));		# load key->rounds
1344
1345	&lea	($acc,&DWP(-2,$acc,$acc));
1346	&lea	($acc,&DWP(0,$key,$acc,8));
1347	&mov	($__end,$acc);			# end of key schedule
1348
1349	# prefetch Td4
1350	&mov	($key,&DWP(0-128,$tbl));
1351	&mov	($acc,&DWP(32-128,$tbl));
1352	&mov	($key,&DWP(64-128,$tbl));
1353	&mov	($acc,&DWP(96-128,$tbl));
1354	&mov	($key,&DWP(128-128,$tbl));
1355	&mov	($acc,&DWP(160-128,$tbl));
1356	&mov	($key,&DWP(192-128,$tbl));
1357	&mov	($acc,&DWP(224-128,$tbl));
1358
1359	&set_label("loop",16);
1360
1361		&deccompact(0,$tbl,$s0,$s3,$s2,$s1,1);
1362		&deccompact(1,$tbl,$s1,$s0,$s3,$s2,1);
1363		&deccompact(2,$tbl,$s2,$s1,$s0,$s3,1);
1364		&deccompact(3,$tbl,$s3,$s2,$s1,$s0,1);
1365		&dectransform(2);
1366		&dectransform(3);
1367		&dectransform(0);
1368		&dectransform(1);
1369		&mov 	($key,$__key);
1370		&mov	($tbl,$__tbl);
1371		&add	($key,16);		# advance rd_key
1372		&xor	($s0,&DWP(0,$key));
1373		&xor	($s1,&DWP(4,$key));
1374		&xor	($s2,&DWP(8,$key));
1375		&xor	($s3,&DWP(12,$key));
1376
1377	&cmp	($key,$__end);
1378	&mov	($__key,$key);
1379	&jb	(&label("loop"));
1380
1381	&deccompact(0,$tbl,$s0,$s3,$s2,$s1);
1382	&deccompact(1,$tbl,$s1,$s0,$s3,$s2);
1383	&deccompact(2,$tbl,$s2,$s1,$s0,$s3);
1384	&deccompact(3,$tbl,$s3,$s2,$s1,$s0);
1385
1386	&xor	($s0,&DWP(16,$key));
1387	&xor	($s1,&DWP(20,$key));
1388	&xor	($s2,&DWP(24,$key));
1389	&xor	($s3,&DWP(28,$key));
1390
1391	&ret	();
1392&function_end_B("_x86_AES_decrypt_compact");
1393
1394######################################################################
1395# "Compact" SSE block function.
1396######################################################################
1397
1398sub sse_deccompact()
1399{
1400	&pshufw	("mm1","mm0",0x0c);		#  7, 6, 1, 0
1401	&pshufw	("mm5","mm4",0x09);		# 13,12,11,10
1402	&movd	("eax","mm1");			#  7, 6, 1, 0
1403	&movd	("ebx","mm5");			# 13,12,11,10
1404	&mov	($__key,$key);
1405
1406	&movz	($acc,&LB("eax"));		#  0
1407	&movz	("edx",&HB("eax"));		#  1
1408	&pshufw	("mm2","mm0",0x06);		#  3, 2, 5, 4
1409	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  0
1410	&movz	($key,&LB("ebx"));		# 10
1411	&movz	("edx",&BP(-128,$tbl,"edx",1));	#  1
1412	&shr	("eax",16);			#  7, 6
1413	&shl	("edx",8);			#  1
1414
1415	&movz	($acc,&BP(-128,$tbl,$key,1));	# 10
1416	&movz	($key,&HB("ebx"));		# 11
1417	&shl	($acc,16);			# 10
1418	&pshufw	("mm6","mm4",0x03);		# 9, 8,15,14
1419	&or	("ecx",$acc);			# 10
1420	&movz	($acc,&BP(-128,$tbl,$key,1));	# 11
1421	&movz	($key,&HB("eax"));		#  7
1422	&shl	($acc,24);			# 11
1423	&shr	("ebx",16);			# 13,12
1424	&or	("edx",$acc);			# 11
1425
1426	&movz	($acc,&BP(-128,$tbl,$key,1));	#  7
1427	&movz	($key,&HB("ebx"));		# 13
1428	&shl	($acc,24);			#  7
1429	&or	("ecx",$acc);			#  7
1430	&movz	($acc,&BP(-128,$tbl,$key,1));	# 13
1431	&movz	($key,&LB("eax"));		#  6
1432	&shl	($acc,8);			# 13
1433	&movd	("eax","mm2");			#  3, 2, 5, 4
1434	&or	("ecx",$acc);			# 13
1435
1436	&movz	($acc,&BP(-128,$tbl,$key,1));	#  6
1437	&movz	($key,&LB("ebx"));		# 12
1438	&shl	($acc,16);			#  6
1439	&movd	("ebx","mm6");			#  9, 8,15,14
1440	&movd	("mm0","ecx");			# t[0] collected
1441	&movz	("ecx",&BP(-128,$tbl,$key,1));	# 12
1442	&movz	($key,&LB("eax"));		#  4
1443	&or	("ecx",$acc);			# 12
1444
1445	&movz	($acc,&BP(-128,$tbl,$key,1));	#  4
1446	&movz	($key,&LB("ebx"));		# 14
1447	&or	("edx",$acc);			#  4
1448	&movz	($acc,&BP(-128,$tbl,$key,1));	# 14
1449	&movz	($key,&HB("eax"));		#  5
1450	&shl	($acc,16);			# 14
1451	&shr	("eax",16);			#  3, 2
1452	&or	("edx",$acc);			# 14
1453
1454	&movz	($acc,&BP(-128,$tbl,$key,1));	#  5
1455	&movz	($key,&HB("ebx"));		# 15
1456	&shr	("ebx",16);			#  9, 8
1457	&shl	($acc,8);			#  5
1458	&movd	("mm1","edx");			# t[1] collected
1459	&movz	("edx",&BP(-128,$tbl,$key,1));	# 15
1460	&movz	($key,&HB("ebx"));		#  9
1461	&shl	("edx",24);			# 15
1462	&and	("ebx",0xff);			#  8
1463	&or	("edx",$acc);			# 15
1464
1465	&punpckldq	("mm0","mm1");		# t[0,1] collected
1466
1467	&movz	($acc,&BP(-128,$tbl,$key,1));	#  9
1468	&movz	($key,&LB("eax"));		#  2
1469	&shl	($acc,8);			#  9
1470	&movz	("eax",&HB("eax"));		#  3
1471	&movz	("ebx",&BP(-128,$tbl,"ebx",1));	#  8
1472	&or	("ecx",$acc);			#  9
1473	&movz	($acc,&BP(-128,$tbl,$key,1));	#  2
1474	&or	("edx","ebx");			#  8
1475	&shl	($acc,16);			#  2
1476	&movz	("eax",&BP(-128,$tbl,"eax",1));	#  3
1477	&or	("edx",$acc);			#  2
1478	&shl	("eax",24);			#  3
1479	&or	("ecx","eax");			#  3
1480	&mov	($key,$__key);
1481	&movd	("mm4","edx");			# t[2] collected
1482	&movd	("mm5","ecx");			# t[3] collected
1483
1484	&punpckldq	("mm4","mm5");		# t[2,3] collected
1485}
1486
1487					if (!$x86only) {
1488&function_begin_B("_sse_AES_decrypt_compact");
1489	&pxor	("mm0",&QWP(0,$key));	#  7, 6, 5, 4, 3, 2, 1, 0
1490	&pxor	("mm4",&QWP(8,$key));	# 15,14,13,12,11,10, 9, 8
1491
1492	# note that caller is expected to allocate stack frame for me!
1493	&mov	($acc,&DWP(240,$key));		# load key->rounds
1494	&lea	($acc,&DWP(-2,$acc,$acc));
1495	&lea	($acc,&DWP(0,$key,$acc,8));
1496	&mov	($__end,$acc);			# end of key schedule
1497
1498	&mov	($s0,0x1b1b1b1b);		# magic constant
1499	&mov	(&DWP(8,"esp"),$s0);
1500	&mov	(&DWP(12,"esp"),$s0);
1501
1502	# prefetch Td4
1503	&mov	($s0,&DWP(0-128,$tbl));
1504	&mov	($s1,&DWP(32-128,$tbl));
1505	&mov	($s2,&DWP(64-128,$tbl));
1506	&mov	($s3,&DWP(96-128,$tbl));
1507	&mov	($s0,&DWP(128-128,$tbl));
1508	&mov	($s1,&DWP(160-128,$tbl));
1509	&mov	($s2,&DWP(192-128,$tbl));
1510	&mov	($s3,&DWP(224-128,$tbl));
1511
1512	&set_label("loop",16);
1513		&sse_deccompact();
1514		&add	($key,16);
1515		&cmp	($key,$__end);
1516		&ja	(&label("out"));
1517
1518		# ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N)
1519		&movq	("mm3","mm0");		&movq	("mm7","mm4");
1520		&movq	("mm2","mm0",1);	&movq	("mm6","mm4",1);
1521		&movq	("mm1","mm0");		&movq	("mm5","mm4");
1522		&pshufw	("mm0","mm0",0xb1);	&pshufw	("mm4","mm4",0xb1);# = ROTATE(tp0,16)
1523		&pslld	("mm2",8);		&pslld	("mm6",8);
1524		&psrld	("mm3",8);		&psrld	("mm7",8);
1525		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp0<<8
1526		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp0>>8
1527		&pslld	("mm2",16);		&pslld	("mm6",16);
1528		&psrld	("mm3",16);		&psrld	("mm7",16);
1529		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp0<<24
1530		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp0>>24
1531
1532		&movq	("mm3",&QWP(8,"esp"));
1533		&pxor	("mm2","mm2");		&pxor	("mm6","mm6");
1534		&pcmpgtb("mm2","mm1");		&pcmpgtb("mm6","mm5");
1535		&pand	("mm2","mm3");		&pand	("mm6","mm3");
1536		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1537		&pxor	("mm1","mm2");		&pxor	("mm5","mm6");	# tp2
1538		&movq	("mm3","mm1");		&movq	("mm7","mm5");
1539		&movq	("mm2","mm1");		&movq	("mm6","mm5");
1540		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp2
1541		&pslld	("mm3",24);		&pslld	("mm7",24);
1542		&psrld	("mm2",8);		&psrld	("mm6",8);
1543		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp2<<24
1544		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp2>>8
1545
1546		&movq	("mm2",&QWP(8,"esp"));
1547		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
1548		&pcmpgtb("mm3","mm1");		&pcmpgtb("mm7","mm5");
1549		&pand	("mm3","mm2");		&pand	("mm7","mm2");
1550		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1551		&pxor	("mm1","mm3");		&pxor	("mm5","mm7");	# tp4
1552		&pshufw	("mm3","mm1",0xb1);	&pshufw	("mm7","mm5",0xb1);
1553		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp4
1554		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= ROTATE(tp4,16)
1555
1556		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
1557		&pcmpgtb("mm3","mm1");		&pcmpgtb("mm7","mm5");
1558		&pand	("mm3","mm2");		&pand	("mm7","mm2");
1559		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1560		&pxor	("mm1","mm3");		&pxor	("mm5","mm7");	# tp8
1561		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8
1562		&movq	("mm3","mm1");		&movq	("mm7","mm5");
1563		&pshufw	("mm2","mm1",0xb1);	&pshufw	("mm6","mm5",0xb1);
1564		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= ROTATE(tp8,16)
1565		&pslld	("mm1",8);		&pslld	("mm5",8);
1566		&psrld	("mm3",8);		&psrld	("mm7",8);
1567		&movq	("mm2",&QWP(0,$key));	&movq	("mm6",&QWP(8,$key));
1568		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8<<8
1569		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp8>>8
1570		&mov	($s0,&DWP(0-128,$tbl));
1571		&pslld	("mm1",16);		&pslld	("mm5",16);
1572		&mov	($s1,&DWP(64-128,$tbl));
1573		&psrld	("mm3",16);		&psrld	("mm7",16);
1574		&mov	($s2,&DWP(128-128,$tbl));
1575		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8<<24
1576		&mov	($s3,&DWP(192-128,$tbl));
1577		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp8>>24
1578
1579		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");
1580	&jmp	(&label("loop"));
1581
1582	&set_label("out",16);
1583	&pxor	("mm0",&QWP(0,$key));
1584	&pxor	("mm4",&QWP(8,$key));
1585
1586	&ret	();
1587&function_end_B("_sse_AES_decrypt_compact");
1588					}
1589
1590######################################################################
1591# Vanilla block function.
1592######################################################################
1593
1594sub decstep()
1595{ my ($i,$td,@s) = @_;
1596  my $tmp = $key;
1597  my $out = $i==3?$s[0]:$acc;
1598
1599	# no instructions are reordered, as performance appears
1600	# optimal... or rather that all attempts to reorder didn't
1601	# result in better performance [which by the way is not a
1602	# bit lower than ecryption].
1603	if($i==3)   {	&mov	($key,$__key);			}
1604	else        {	&mov	($out,$s[0]);			}
1605			&and	($out,0xFF);
1606			&mov	($out,&DWP(0,$td,$out,8));
1607
1608	if ($i==3)  {	$tmp=$s[1];				}
1609			&movz	($tmp,&HB($s[1]));
1610			&xor	($out,&DWP(3,$td,$tmp,8));
1611
1612	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1613	else        {	&mov	($tmp,$s[2]);			}
1614			&shr	($tmp,16);
1615			&and	($tmp,0xFF);
1616			&xor	($out,&DWP(2,$td,$tmp,8));
1617
1618	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}
1619	else        {	&mov	($tmp,$s[3]);			}
1620			&shr	($tmp,24);
1621			&xor	($out,&DWP(1,$td,$tmp,8));
1622	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1623	if ($i==3)  {	&mov	($s[3],$__s0);			}
1624			&comment();
1625}
1626
1627sub declast()
1628{ my ($i,$td,@s)=@_;
1629  my $tmp = $key;
1630  my $out = $i==3?$s[0]:$acc;
1631
1632	if($i==0)   {	&lea	($td,&DWP(2048+128,$td));
1633			&mov	($tmp,&DWP(0-128,$td));
1634			&mov	($acc,&DWP(32-128,$td));
1635			&mov	($tmp,&DWP(64-128,$td));
1636			&mov	($acc,&DWP(96-128,$td));
1637			&mov	($tmp,&DWP(128-128,$td));
1638			&mov	($acc,&DWP(160-128,$td));
1639			&mov	($tmp,&DWP(192-128,$td));
1640			&mov	($acc,&DWP(224-128,$td));
1641			&lea	($td,&DWP(-128,$td));		}
1642	if($i==3)   {	&mov	($key,$__key);			}
1643	else        {	&mov	($out,$s[0]);			}
1644			&and	($out,0xFF);
1645			&movz	($out,&BP(0,$td,$out,1));
1646
1647	if ($i==3)  {	$tmp=$s[1];				}
1648			&movz	($tmp,&HB($s[1]));
1649			&movz	($tmp,&BP(0,$td,$tmp,1));
1650			&shl	($tmp,8);
1651			&xor	($out,$tmp);
1652
1653	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1654	else        {	mov	($tmp,$s[2]);			}
1655			&shr	($tmp,16);
1656			&and	($tmp,0xFF);
1657			&movz	($tmp,&BP(0,$td,$tmp,1));
1658			&shl	($tmp,16);
1659			&xor	($out,$tmp);
1660
1661	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}
1662	else        {	&mov	($tmp,$s[3]);			}
1663			&shr	($tmp,24);
1664			&movz	($tmp,&BP(0,$td,$tmp,1));
1665			&shl	($tmp,24);
1666			&xor	($out,$tmp);
1667	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1668	if ($i==3)  {	&mov	($s[3],$__s0);
1669			&lea	($td,&DWP(-2048,$td));		}
1670}
1671
1672&function_begin_B("_x86_AES_decrypt");
1673	# note that caller is expected to allocate stack frame for me!
1674	&mov	($__key,$key);			# save key
1675
1676	&xor	($s0,&DWP(0,$key));		# xor with key
1677	&xor	($s1,&DWP(4,$key));
1678	&xor	($s2,&DWP(8,$key));
1679	&xor	($s3,&DWP(12,$key));
1680
1681	&mov	($acc,&DWP(240,$key));		# load key->rounds
1682
1683	if ($small_footprint) {
1684	    &lea	($acc,&DWP(-2,$acc,$acc));
1685	    &lea	($acc,&DWP(0,$key,$acc,8));
1686	    &mov	($__end,$acc);		# end of key schedule
1687	    &set_label("loop",16);
1688		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1689		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1690		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1691		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1692		&add	($key,16);		# advance rd_key
1693		&xor	($s0,&DWP(0,$key));
1694		&xor	($s1,&DWP(4,$key));
1695		&xor	($s2,&DWP(8,$key));
1696		&xor	($s3,&DWP(12,$key));
1697	    &cmp	($key,$__end);
1698	    &mov	($__key,$key);
1699	    &jb		(&label("loop"));
1700	}
1701	else {
1702	    &cmp	($acc,10);
1703	    &jle	(&label("10rounds"));
1704	    &cmp	($acc,12);
1705	    &jle	(&label("12rounds"));
1706
1707	&set_label("14rounds",4);
1708	    for ($i=1;$i<3;$i++) {
1709		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1710		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1711		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1712		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1713		&xor	($s0,&DWP(16*$i+0,$key));
1714		&xor	($s1,&DWP(16*$i+4,$key));
1715		&xor	($s2,&DWP(16*$i+8,$key));
1716		&xor	($s3,&DWP(16*$i+12,$key));
1717	    }
1718	    &add	($key,32);
1719	    &mov	($__key,$key);		# advance rd_key
1720	&set_label("12rounds",4);
1721	    for ($i=1;$i<3;$i++) {
1722		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1723		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1724		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1725		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1726		&xor	($s0,&DWP(16*$i+0,$key));
1727		&xor	($s1,&DWP(16*$i+4,$key));
1728		&xor	($s2,&DWP(16*$i+8,$key));
1729		&xor	($s3,&DWP(16*$i+12,$key));
1730	    }
1731	    &add	($key,32);
1732	    &mov	($__key,$key);		# advance rd_key
1733	&set_label("10rounds",4);
1734	    for ($i=1;$i<10;$i++) {
1735		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1736		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1737		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1738		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1739		&xor	($s0,&DWP(16*$i+0,$key));
1740		&xor	($s1,&DWP(16*$i+4,$key));
1741		&xor	($s2,&DWP(16*$i+8,$key));
1742		&xor	($s3,&DWP(16*$i+12,$key));
1743	    }
1744	}
1745
1746	&declast(0,$tbl,$s0,$s3,$s2,$s1);
1747	&declast(1,$tbl,$s1,$s0,$s3,$s2);
1748	&declast(2,$tbl,$s2,$s1,$s0,$s3);
1749	&declast(3,$tbl,$s3,$s2,$s1,$s0);
1750
1751	&add	($key,$small_footprint?16:160);
1752	&xor	($s0,&DWP(0,$key));
1753	&xor	($s1,&DWP(4,$key));
1754	&xor	($s2,&DWP(8,$key));
1755	&xor	($s3,&DWP(12,$key));
1756
1757	&ret	();
1758
1759&set_label("AES_Td",64);	# Yes! I keep it in the code segment!
1760	&_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
1761	&_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
1762	&_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
1763	&_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
1764	&_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
1765	&_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
1766	&_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
1767	&_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
1768	&_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
1769	&_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
1770	&_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
1771	&_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
1772	&_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
1773	&_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
1774	&_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
1775	&_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
1776	&_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
1777	&_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
1778	&_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
1779	&_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
1780	&_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
1781	&_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
1782	&_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
1783	&_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
1784	&_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
1785	&_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
1786	&_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
1787	&_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
1788	&_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
1789	&_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
1790	&_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
1791	&_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
1792	&_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
1793	&_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
1794	&_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
1795	&_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
1796	&_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
1797	&_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
1798	&_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
1799	&_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
1800	&_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
1801	&_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
1802	&_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
1803	&_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
1804	&_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
1805	&_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
1806	&_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
1807	&_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
1808	&_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
1809	&_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
1810	&_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
1811	&_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
1812	&_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
1813	&_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
1814	&_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
1815	&_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
1816	&_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
1817	&_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
1818	&_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
1819	&_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
1820	&_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
1821	&_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
1822	&_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
1823	&_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
1824
1825#Td4:	# four copies of Td4 to choose from to avoid L1 aliasing
1826	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1827	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1828	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1829	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1830	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1831	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1832	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1833	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1834	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1835	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1836	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1837	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1838	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1839	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1840	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1841	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1842	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1843	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1844	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1845	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1846	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1847	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1848	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1849	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1850	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1851	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1852	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1853	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1854	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1855	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1856	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1857	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1858
1859	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1860	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1861	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1862	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1863	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1864	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1865	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1866	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1867	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1868	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1869	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1870	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1871	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1872	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1873	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1874	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1875	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1876	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1877	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1878	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1879	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1880	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1881	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1882	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1883	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1884	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1885	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1886	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1887	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1888	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1889	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1890	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1891
1892	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1893	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1894	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1895	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1896	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1897	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1898	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1899	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1900	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1901	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1902	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1903	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1904	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1905	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1906	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1907	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1908	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1909	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1910	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1911	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1912	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1913	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1914	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1915	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1916	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1917	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1918	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1919	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1920	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1921	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1922	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1923	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1924
1925	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1926	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1927	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1928	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1929	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1930	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1931	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1932	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1933	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1934	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1935	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1936	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1937	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1938	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1939	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1940	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1941	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1942	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1943	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1944	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1945	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1946	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1947	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1948	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1949	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1950	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1951	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1952	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1953	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1954	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1955	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1956	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1957&function_end_B("_x86_AES_decrypt");
1958
1959# void asm_AES_decrypt (const void *inp,void *out,const AES_KEY *key);
1960&function_begin("asm_AES_decrypt");
1961	&mov	($acc,&wparam(0));		# load inp
1962	&mov	($key,&wparam(2));		# load key
1963
1964	&mov	($s0,"esp");
1965	&sub	("esp",36);
1966	&and	("esp",-64);			# align to cache-line
1967
1968	# place stack frame just "above" the key schedule
1969	&lea	($s1,&DWP(-64-63,$key));
1970	&sub	($s1,"esp");
1971	&neg	($s1);
1972	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
1973	&sub	("esp",$s1);
1974	&add	("esp",4);	# 4 is reserved for caller's return address
1975	&mov	($_esp,$s0);	# save stack pointer
1976
1977	&call   (&label("pic_point"));          # make it PIC!
1978	&set_label("pic_point");
1979	&blindpop($tbl);
1980	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
1981	&lea    ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
1982
1983	# pick Td4 copy which can't "overlap" with stack frame or key schedule
1984	&lea	($s1,&DWP(768-4,"esp"));
1985	&sub	($s1,$tbl);
1986	&and	($s1,0x300);
1987	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
1988
1989					if (!$x86only) {
1990	&bt	(&DWP(0,$s0),25);	# check for SSE bit
1991	&jnc	(&label("x86"));
1992
1993	&movq	("mm0",&QWP(0,$acc));
1994	&movq	("mm4",&QWP(8,$acc));
1995	&call	("_sse_AES_decrypt_compact");
1996	&mov	("esp",$_esp);			# restore stack pointer
1997	&mov	($acc,&wparam(1));		# load out
1998	&movq	(&QWP(0,$acc),"mm0");		# write output data
1999	&movq	(&QWP(8,$acc),"mm4");
2000	&emms	();
2001	&function_end_A();
2002					}
2003	&set_label("x86",16);
2004	&mov	($_tbl,$tbl);
2005	&mov	($s0,&DWP(0,$acc));		# load input data
2006	&mov	($s1,&DWP(4,$acc));
2007	&mov	($s2,&DWP(8,$acc));
2008	&mov	($s3,&DWP(12,$acc));
2009	&call	("_x86_AES_decrypt_compact");
2010	&mov	("esp",$_esp);			# restore stack pointer
2011	&mov	($acc,&wparam(1));		# load out
2012	&mov	(&DWP(0,$acc),$s0);		# write output data
2013	&mov	(&DWP(4,$acc),$s1);
2014	&mov	(&DWP(8,$acc),$s2);
2015	&mov	(&DWP(12,$acc),$s3);
2016&function_end("asm_AES_decrypt");
2017
2018# void asm_AES_cbc_encrypt (const void char *inp, unsigned char *out,
2019#			    size_t length, const AES_KEY *key,
2020#			    unsigned char *ivp,const int enc);
2021{
2022# stack frame layout
2023#             -4(%esp)		# return address	 0(%esp)
2024#              0(%esp)		# s0 backing store	 4(%esp)
2025#              4(%esp)		# s1 backing store	 8(%esp)
2026#              8(%esp)		# s2 backing store	12(%esp)
2027#             12(%esp)		# s3 backing store	16(%esp)
2028#             16(%esp)		# key backup		20(%esp)
2029#             20(%esp)		# end of key schedule	24(%esp)
2030#             24(%esp)		# %ebp backup		28(%esp)
2031#             28(%esp)		# %esp backup
2032my $_inp=&DWP(32,"esp");	# copy of wparam(0)
2033my $_out=&DWP(36,"esp");	# copy of wparam(1)
2034my $_len=&DWP(40,"esp");	# copy of wparam(2)
2035my $_key=&DWP(44,"esp");	# copy of wparam(3)
2036my $_ivp=&DWP(48,"esp");	# copy of wparam(4)
2037my $_tmp=&DWP(52,"esp");	# volatile variable
2038#
2039my $ivec=&DWP(60,"esp");	# ivec[16]
2040my $aes_key=&DWP(76,"esp");	# copy of aes_key
2041my $mark=&DWP(76+240,"esp");	# copy of aes_key->rounds
2042
2043&function_begin("asm_AES_cbc_encrypt");
2044	&mov	($s2 eq "ecx"? $s2 : "",&wparam(2));	# load len
2045	&cmp	($s2,0);
2046	&je	(&label("drop_out"));
2047
2048	&call   (&label("pic_point"));		# make it PIC!
2049	&set_label("pic_point");
2050	&blindpop($tbl);
2051	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
2052
2053	&cmp	(&wparam(5),0);
2054	&lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
2055	&jne	(&label("picked_te"));
2056	&lea	($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl));
2057	&set_label("picked_te");
2058
2059	# one can argue if this is required
2060	&pushf	();
2061	&cld	();
2062
2063	&cmp	($s2,$speed_limit);
2064	&jb	(&label("slow_way"));
2065	&test	($s2,15);
2066	&jnz	(&label("slow_way"));
2067					if (!$x86only) {
2068	&bt	(&DWP(0,$s0),28);	# check for hyper-threading bit
2069	&jc	(&label("slow_way"));
2070					}
2071	# pre-allocate aligned stack frame...
2072	&lea	($acc,&DWP(-80-244,"esp"));
2073	&and	($acc,-64);
2074
2075	# ... and make sure it doesn't alias with $tbl modulo 4096
2076	&mov	($s0,$tbl);
2077	&lea	($s1,&DWP(2048+256,$tbl));
2078	&mov	($s3,$acc);
2079	&and	($s0,0xfff);		# s = %ebp&0xfff
2080	&and	($s1,0xfff);		# e = (%ebp+2048+256)&0xfff
2081	&and	($s3,0xfff);		# p = %esp&0xfff
2082
2083	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
2084	&jb	(&label("tbl_break_out"));
2085	&sub	($s3,$s1);
2086	&sub	($acc,$s3);
2087	&jmp	(&label("tbl_ok"));
2088	&set_label("tbl_break_out",4);	# else %esp -= (p-s)&0xfff + framesz;
2089	&sub	($s3,$s0);
2090	&and	($s3,0xfff);
2091	&add	($s3,384);
2092	&sub	($acc,$s3);
2093	&set_label("tbl_ok",4);
2094
2095	&lea	($s3,&wparam(0));	# obtain pointer to parameter block
2096	&exch	("esp",$acc);		# allocate stack frame
2097	&add	("esp",4);		# reserve for return address!
2098	&mov	($_tbl,$tbl);		# save %ebp
2099	&mov	($_esp,$acc);		# save %esp
2100
2101	&mov	($s0,&DWP(0,$s3));	# load inp
2102	&mov	($s1,&DWP(4,$s3));	# load out
2103	#&mov	($s2,&DWP(8,$s3));	# load len
2104	&mov	($key,&DWP(12,$s3));	# load key
2105	&mov	($acc,&DWP(16,$s3));	# load ivp
2106	&mov	($s3,&DWP(20,$s3));	# load enc flag
2107
2108	&mov	($_inp,$s0);		# save copy of inp
2109	&mov	($_out,$s1);		# save copy of out
2110	&mov	($_len,$s2);		# save copy of len
2111	&mov	($_key,$key);		# save copy of key
2112	&mov	($_ivp,$acc);		# save copy of ivp
2113
2114	&mov	($mark,0);		# copy of aes_key->rounds = 0;
2115	# do we copy key schedule to stack?
2116	&mov	($s1 eq "ebx" ? $s1 : "",$key);
2117	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
2118	&sub	($s1,$tbl);
2119	&mov	("esi",$key);
2120	&and	($s1,0xfff);
2121	&lea	("edi",$aes_key);
2122	&cmp	($s1,2048+256);
2123	&jb	(&label("do_copy"));
2124	&cmp	($s1,4096-244);
2125	&jb	(&label("skip_copy"));
2126	&set_label("do_copy",4);
2127		&mov	($_key,"edi");
2128		&data_word(0xA5F3F689);	# rep movsd
2129	&set_label("skip_copy");
2130
2131	&mov	($key,16);
2132	&set_label("prefetch_tbl",4);
2133		&mov	($s0,&DWP(0,$tbl));
2134		&mov	($s1,&DWP(32,$tbl));
2135		&mov	($s2,&DWP(64,$tbl));
2136		&mov	($acc,&DWP(96,$tbl));
2137		&lea	($tbl,&DWP(128,$tbl));
2138		&sub	($key,1);
2139	&jnz	(&label("prefetch_tbl"));
2140	&sub	($tbl,2048);
2141
2142	&mov	($acc,$_inp);
2143	&mov	($key,$_ivp);
2144
2145	&cmp	($s3,0);
2146	&je	(&label("fast_decrypt"));
2147
2148#----------------------------- ENCRYPT -----------------------------#
2149	&mov	($s0,&DWP(0,$key));		# load iv
2150	&mov	($s1,&DWP(4,$key));
2151
2152	&set_label("fast_enc_loop",16);
2153		&mov	($s2,&DWP(8,$key));
2154		&mov	($s3,&DWP(12,$key));
2155
2156		&xor	($s0,&DWP(0,$acc));	# xor input data
2157		&xor	($s1,&DWP(4,$acc));
2158		&xor	($s2,&DWP(8,$acc));
2159		&xor	($s3,&DWP(12,$acc));
2160
2161		&mov	($key,$_key);		# load key
2162		&call	("_x86_AES_encrypt");
2163
2164		&mov	($acc,$_inp);		# load inp
2165		&mov	($key,$_out);		# load out
2166
2167		&mov	(&DWP(0,$key),$s0);	# save output data
2168		&mov	(&DWP(4,$key),$s1);
2169		&mov	(&DWP(8,$key),$s2);
2170		&mov	(&DWP(12,$key),$s3);
2171
2172		&lea	($acc,&DWP(16,$acc));	# advance inp
2173		&mov	($s2,$_len);		# load len
2174		&mov	($_inp,$acc);		# save inp
2175		&lea	($s3,&DWP(16,$key));	# advance out
2176		&mov	($_out,$s3);		# save out
2177		&sub	($s2,16);		# decrease len
2178		&mov	($_len,$s2);		# save len
2179	&jnz	(&label("fast_enc_loop"));
2180	&mov	($acc,$_ivp);		# load ivp
2181	&mov	($s2,&DWP(8,$key));	# restore last 2 dwords
2182	&mov	($s3,&DWP(12,$key));
2183	&mov	(&DWP(0,$acc),$s0);	# save ivec
2184	&mov	(&DWP(4,$acc),$s1);
2185	&mov	(&DWP(8,$acc),$s2);
2186	&mov	(&DWP(12,$acc),$s3);
2187
2188	&cmp	($mark,0);		# was the key schedule copied?
2189	&mov	("edi",$_key);
2190	&je	(&label("skip_ezero"));
2191	# zero copy of key schedule
2192	&mov	("ecx",240/4);
2193	&xor	("eax","eax");
2194	&align	(4);
2195	&data_word(0xABF3F689);		# rep stosd
2196	&set_label("skip_ezero");
2197	&mov	("esp",$_esp);
2198	&popf	();
2199    &set_label("drop_out");
2200	&function_end_A();
2201	&pushf	();			# kludge, never executed
2202
2203#----------------------------- DECRYPT -----------------------------#
2204&set_label("fast_decrypt",16);
2205
2206	&cmp	($acc,$_out);
2207	&je	(&label("fast_dec_in_place"));	# in-place processing...
2208
2209	&mov	($_tmp,$key);
2210
2211	&align	(4);
2212	&set_label("fast_dec_loop",16);
2213		&mov	($s0,&DWP(0,$acc));	# read input
2214		&mov	($s1,&DWP(4,$acc));
2215		&mov	($s2,&DWP(8,$acc));
2216		&mov	($s3,&DWP(12,$acc));
2217
2218		&mov	($key,$_key);		# load key
2219		&call	("_x86_AES_decrypt");
2220
2221		&mov	($key,$_tmp);		# load ivp
2222		&mov	($acc,$_len);		# load len
2223		&xor	($s0,&DWP(0,$key));	# xor iv
2224		&xor	($s1,&DWP(4,$key));
2225		&xor	($s2,&DWP(8,$key));
2226		&xor	($s3,&DWP(12,$key));
2227
2228		&mov	($key,$_out);		# load out
2229		&mov	($acc,$_inp);		# load inp
2230
2231		&mov	(&DWP(0,$key),$s0);	# write output
2232		&mov	(&DWP(4,$key),$s1);
2233		&mov	(&DWP(8,$key),$s2);
2234		&mov	(&DWP(12,$key),$s3);
2235
2236		&mov	($s2,$_len);		# load len
2237		&mov	($_tmp,$acc);		# save ivp
2238		&lea	($acc,&DWP(16,$acc));	# advance inp
2239		&mov	($_inp,$acc);		# save inp
2240		&lea	($key,&DWP(16,$key));	# advance out
2241		&mov	($_out,$key);		# save out
2242		&sub	($s2,16);		# decrease len
2243		&mov	($_len,$s2);		# save len
2244	&jnz	(&label("fast_dec_loop"));
2245	&mov	($key,$_tmp);		# load temp ivp
2246	&mov	($acc,$_ivp);		# load user ivp
2247	&mov	($s0,&DWP(0,$key));	# load iv
2248	&mov	($s1,&DWP(4,$key));
2249	&mov	($s2,&DWP(8,$key));
2250	&mov	($s3,&DWP(12,$key));
2251	&mov	(&DWP(0,$acc),$s0);	# copy back to user
2252	&mov	(&DWP(4,$acc),$s1);
2253	&mov	(&DWP(8,$acc),$s2);
2254	&mov	(&DWP(12,$acc),$s3);
2255	&jmp	(&label("fast_dec_out"));
2256
2257    &set_label("fast_dec_in_place",16);
2258	&set_label("fast_dec_in_place_loop");
2259		&mov	($s0,&DWP(0,$acc));	# read input
2260		&mov	($s1,&DWP(4,$acc));
2261		&mov	($s2,&DWP(8,$acc));
2262		&mov	($s3,&DWP(12,$acc));
2263
2264		&lea	($key,$ivec);
2265		&mov	(&DWP(0,$key),$s0);	# copy to temp
2266		&mov	(&DWP(4,$key),$s1);
2267		&mov	(&DWP(8,$key),$s2);
2268		&mov	(&DWP(12,$key),$s3);
2269
2270		&mov	($key,$_key);		# load key
2271		&call	("_x86_AES_decrypt");
2272
2273		&mov	($key,$_ivp);		# load ivp
2274		&mov	($acc,$_out);		# load out
2275		&xor	($s0,&DWP(0,$key));	# xor iv
2276		&xor	($s1,&DWP(4,$key));
2277		&xor	($s2,&DWP(8,$key));
2278		&xor	($s3,&DWP(12,$key));
2279
2280		&mov	(&DWP(0,$acc),$s0);	# write output
2281		&mov	(&DWP(4,$acc),$s1);
2282		&mov	(&DWP(8,$acc),$s2);
2283		&mov	(&DWP(12,$acc),$s3);
2284
2285		&lea	($acc,&DWP(16,$acc));	# advance out
2286		&mov	($_out,$acc);		# save out
2287
2288		&lea	($acc,$ivec);
2289		&mov	($s0,&DWP(0,$acc));	# read temp
2290		&mov	($s1,&DWP(4,$acc));
2291		&mov	($s2,&DWP(8,$acc));
2292		&mov	($s3,&DWP(12,$acc));
2293
2294		&mov	(&DWP(0,$key),$s0);	# copy iv
2295		&mov	(&DWP(4,$key),$s1);
2296		&mov	(&DWP(8,$key),$s2);
2297		&mov	(&DWP(12,$key),$s3);
2298
2299		&mov	($acc,$_inp);		# load inp
2300		&mov	($s2,$_len);		# load len
2301		&lea	($acc,&DWP(16,$acc));	# advance inp
2302		&mov	($_inp,$acc);		# save inp
2303		&sub	($s2,16);		# decrease len
2304		&mov	($_len,$s2);		# save len
2305	&jnz	(&label("fast_dec_in_place_loop"));
2306
2307    &set_label("fast_dec_out",4);
2308	&cmp	($mark,0);		# was the key schedule copied?
2309	&mov	("edi",$_key);
2310	&je	(&label("skip_dzero"));
2311	# zero copy of key schedule
2312	&mov	("ecx",240/4);
2313	&xor	("eax","eax");
2314	&align	(4);
2315	&data_word(0xABF3F689);		# rep stosd
2316	&set_label("skip_dzero");
2317	&mov	("esp",$_esp);
2318	&popf	();
2319	&function_end_A();
2320	&pushf	();			# kludge, never executed
2321
2322#--------------------------- SLOW ROUTINE ---------------------------#
2323&set_label("slow_way",16);
2324
2325	&mov	($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap
2326	&mov	($key,&wparam(3));	# load key
2327
2328	# pre-allocate aligned stack frame...
2329	&lea	($acc,&DWP(-80,"esp"));
2330	&and	($acc,-64);
2331
2332	# ... and make sure it doesn't alias with $key modulo 1024
2333	&lea	($s1,&DWP(-80-63,$key));
2334	&sub	($s1,$acc);
2335	&neg	($s1);
2336	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
2337	&sub	($acc,$s1);
2338
2339	# pick S-box copy which can't overlap with stack frame or $key
2340	&lea	($s1,&DWP(768,$acc));
2341	&sub	($s1,$tbl);
2342	&and	($s1,0x300);
2343	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
2344
2345	&lea	($s3,&wparam(0));	# pointer to parameter block
2346
2347	&exch	("esp",$acc);
2348	&add	("esp",4);		# reserve for return address!
2349	&mov	($_tbl,$tbl);		# save %ebp
2350	&mov	($_esp,$acc);		# save %esp
2351	&mov	($_tmp,$s0);		# save OPENSSL_ia32cap
2352
2353	&mov	($s0,&DWP(0,$s3));	# load inp
2354	&mov	($s1,&DWP(4,$s3));	# load out
2355	#&mov	($s2,&DWP(8,$s3));	# load len
2356	#&mov	($key,&DWP(12,$s3));	# load key
2357	&mov	($acc,&DWP(16,$s3));	# load ivp
2358	&mov	($s3,&DWP(20,$s3));	# load enc flag
2359
2360	&mov	($_inp,$s0);		# save copy of inp
2361	&mov	($_out,$s1);		# save copy of out
2362	&mov	($_len,$s2);		# save copy of len
2363	&mov	($_key,$key);		# save copy of key
2364	&mov	($_ivp,$acc);		# save copy of ivp
2365
2366	&mov	($key,$acc);
2367	&mov	($acc,$s0);
2368
2369	&cmp	($s3,0);
2370	&je	(&label("slow_decrypt"));
2371
2372#--------------------------- SLOW ENCRYPT ---------------------------#
2373	&cmp	($s2,16);
2374	&mov	($s3,$s1);
2375	&jb	(&label("slow_enc_tail"));
2376
2377					if (!$x86only) {
2378	&bt	($_tmp,25);		# check for SSE bit
2379	&jnc	(&label("slow_enc_x86"));
2380
2381	&movq	("mm0",&QWP(0,$key));	# load iv
2382	&movq	("mm4",&QWP(8,$key));
2383
2384	&set_label("slow_enc_loop_sse",16);
2385		&pxor	("mm0",&QWP(0,$acc));	# xor input data
2386		&pxor	("mm4",&QWP(8,$acc));
2387
2388		&mov	($key,$_key);
2389		&call	("_sse_AES_encrypt_compact");
2390
2391		&mov	($acc,$_inp);		# load inp
2392		&mov	($key,$_out);		# load out
2393		&mov	($s2,$_len);		# load len
2394
2395		&movq	(&QWP(0,$key),"mm0");	# save output data
2396		&movq	(&QWP(8,$key),"mm4");
2397
2398		&lea	($acc,&DWP(16,$acc));	# advance inp
2399		&mov	($_inp,$acc);		# save inp
2400		&lea	($s3,&DWP(16,$key));	# advance out
2401		&mov	($_out,$s3);		# save out
2402		&sub	($s2,16);		# decrease len
2403		&cmp	($s2,16);
2404		&mov	($_len,$s2);		# save len
2405	&jae	(&label("slow_enc_loop_sse"));
2406	&test	($s2,15);
2407	&jnz	(&label("slow_enc_tail"));
2408	&mov	($acc,$_ivp);		# load ivp
2409	&movq	(&QWP(0,$acc),"mm0");	# save ivec
2410	&movq	(&QWP(8,$acc),"mm4");
2411	&emms	();
2412	&mov	("esp",$_esp);
2413	&popf	();
2414	&function_end_A();
2415	&pushf	();			# kludge, never executed
2416					}
2417    &set_label("slow_enc_x86",16);
2418	&mov	($s0,&DWP(0,$key));	# load iv
2419	&mov	($s1,&DWP(4,$key));
2420
2421	&set_label("slow_enc_loop_x86",4);
2422		&mov	($s2,&DWP(8,$key));
2423		&mov	($s3,&DWP(12,$key));
2424
2425		&xor	($s0,&DWP(0,$acc));	# xor input data
2426		&xor	($s1,&DWP(4,$acc));
2427		&xor	($s2,&DWP(8,$acc));
2428		&xor	($s3,&DWP(12,$acc));
2429
2430		&mov	($key,$_key);		# load key
2431		&call	("_x86_AES_encrypt_compact");
2432
2433		&mov	($acc,$_inp);		# load inp
2434		&mov	($key,$_out);		# load out
2435
2436		&mov	(&DWP(0,$key),$s0);	# save output data
2437		&mov	(&DWP(4,$key),$s1);
2438		&mov	(&DWP(8,$key),$s2);
2439		&mov	(&DWP(12,$key),$s3);
2440
2441		&mov	($s2,$_len);		# load len
2442		&lea	($acc,&DWP(16,$acc));	# advance inp
2443		&mov	($_inp,$acc);		# save inp
2444		&lea	($s3,&DWP(16,$key));	# advance out
2445		&mov	($_out,$s3);		# save out
2446		&sub	($s2,16);		# decrease len
2447		&cmp	($s2,16);
2448		&mov	($_len,$s2);		# save len
2449	&jae	(&label("slow_enc_loop_x86"));
2450	&test	($s2,15);
2451	&jnz	(&label("slow_enc_tail"));
2452	&mov	($acc,$_ivp);		# load ivp
2453	&mov	($s2,&DWP(8,$key));	# restore last dwords
2454	&mov	($s3,&DWP(12,$key));
2455	&mov	(&DWP(0,$acc),$s0);	# save ivec
2456	&mov	(&DWP(4,$acc),$s1);
2457	&mov	(&DWP(8,$acc),$s2);
2458	&mov	(&DWP(12,$acc),$s3);
2459
2460	&mov	("esp",$_esp);
2461	&popf	();
2462	&function_end_A();
2463	&pushf	();			# kludge, never executed
2464
2465    &set_label("slow_enc_tail",16);
2466	&emms	()	if (!$x86only);
2467	&mov	($key eq "edi"? $key:"",$s3);	# load out to edi
2468	&mov	($s1,16);
2469	&sub	($s1,$s2);
2470	&cmp	($key,$acc eq "esi"? $acc:"");	# compare with inp
2471	&je	(&label("enc_in_place"));
2472	&align	(4);
2473	&data_word(0xA4F3F689);	# rep movsb	# copy input
2474	&jmp	(&label("enc_skip_in_place"));
2475    &set_label("enc_in_place");
2476	&lea	($key,&DWP(0,$key,$s2));
2477    &set_label("enc_skip_in_place");
2478	&mov	($s2,$s1);
2479	&xor	($s0,$s0);
2480	&align	(4);
2481	&data_word(0xAAF3F689);	# rep stosb	# zero tail
2482
2483	&mov	($key,$_ivp);			# restore ivp
2484	&mov	($acc,$s3);			# output as input
2485	&mov	($s0,&DWP(0,$key));
2486	&mov	($s1,&DWP(4,$key));
2487	&mov	($_len,16);			# len=16
2488	&jmp	(&label("slow_enc_loop_x86"));	# one more spin...
2489
2490#--------------------------- SLOW DECRYPT ---------------------------#
2491&set_label("slow_decrypt",16);
2492					if (!$x86only) {
2493	&bt	($_tmp,25);		# check for SSE bit
2494	&jnc	(&label("slow_dec_loop_x86"));
2495
2496	&set_label("slow_dec_loop_sse",4);
2497		&movq	("mm0",&QWP(0,$acc));	# read input
2498		&movq	("mm4",&QWP(8,$acc));
2499
2500		&mov	($key,$_key);
2501		&call	("_sse_AES_decrypt_compact");
2502
2503		&mov	($acc,$_inp);		# load inp
2504		&lea	($s0,$ivec);
2505		&mov	($s1,$_out);		# load out
2506		&mov	($s2,$_len);		# load len
2507		&mov	($key,$_ivp);		# load ivp
2508
2509		&movq	("mm1",&QWP(0,$acc));	# re-read input
2510		&movq	("mm5",&QWP(8,$acc));
2511
2512		&pxor	("mm0",&QWP(0,$key));	# xor iv
2513		&pxor	("mm4",&QWP(8,$key));
2514
2515		&movq	(&QWP(0,$key),"mm1");	# copy input to iv
2516		&movq	(&QWP(8,$key),"mm5");
2517
2518		&sub	($s2,16);		# decrease len
2519		&jc	(&label("slow_dec_partial_sse"));
2520
2521		&movq	(&QWP(0,$s1),"mm0");	# write output
2522		&movq	(&QWP(8,$s1),"mm4");
2523
2524		&lea	($s1,&DWP(16,$s1));	# advance out
2525		&mov	($_out,$s1);		# save out
2526		&lea	($acc,&DWP(16,$acc));	# advance inp
2527		&mov	($_inp,$acc);		# save inp
2528		&mov	($_len,$s2);		# save len
2529	&jnz	(&label("slow_dec_loop_sse"));
2530	&emms	();
2531	&mov	("esp",$_esp);
2532	&popf	();
2533	&function_end_A();
2534	&pushf	();			# kludge, never executed
2535
2536    &set_label("slow_dec_partial_sse",16);
2537	&movq	(&QWP(0,$s0),"mm0");	# save output to temp
2538	&movq	(&QWP(8,$s0),"mm4");
2539	&emms	();
2540
2541	&add	($s2 eq "ecx" ? "ecx":"",16);
2542	&mov	("edi",$s1);		# out
2543	&mov	("esi",$s0);		# temp
2544	&align	(4);
2545	&data_word(0xA4F3F689);		# rep movsb # copy partial output
2546
2547	&mov	("esp",$_esp);
2548	&popf	();
2549	&function_end_A();
2550	&pushf	();			# kludge, never executed
2551					}
2552	&set_label("slow_dec_loop_x86",16);
2553		&mov	($s0,&DWP(0,$acc));	# read input
2554		&mov	($s1,&DWP(4,$acc));
2555		&mov	($s2,&DWP(8,$acc));
2556		&mov	($s3,&DWP(12,$acc));
2557
2558		&lea	($key,$ivec);
2559		&mov	(&DWP(0,$key),$s0);	# copy to temp
2560		&mov	(&DWP(4,$key),$s1);
2561		&mov	(&DWP(8,$key),$s2);
2562		&mov	(&DWP(12,$key),$s3);
2563
2564		&mov	($key,$_key);		# load key
2565		&call	("_x86_AES_decrypt_compact");
2566
2567		&mov	($key,$_ivp);		# load ivp
2568		&mov	($acc,$_len);		# load len
2569		&xor	($s0,&DWP(0,$key));	# xor iv
2570		&xor	($s1,&DWP(4,$key));
2571		&xor	($s2,&DWP(8,$key));
2572		&xor	($s3,&DWP(12,$key));
2573
2574		&sub	($acc,16);
2575		&jc	(&label("slow_dec_partial_x86"));
2576
2577		&mov	($_len,$acc);		# save len
2578		&mov	($acc,$_out);		# load out
2579
2580		&mov	(&DWP(0,$acc),$s0);	# write output
2581		&mov	(&DWP(4,$acc),$s1);
2582		&mov	(&DWP(8,$acc),$s2);
2583		&mov	(&DWP(12,$acc),$s3);
2584
2585		&lea	($acc,&DWP(16,$acc));	# advance out
2586		&mov	($_out,$acc);		# save out
2587
2588		&lea	($acc,$ivec);
2589		&mov	($s0,&DWP(0,$acc));	# read temp
2590		&mov	($s1,&DWP(4,$acc));
2591		&mov	($s2,&DWP(8,$acc));
2592		&mov	($s3,&DWP(12,$acc));
2593
2594		&mov	(&DWP(0,$key),$s0);	# copy it to iv
2595		&mov	(&DWP(4,$key),$s1);
2596		&mov	(&DWP(8,$key),$s2);
2597		&mov	(&DWP(12,$key),$s3);
2598
2599		&mov	($acc,$_inp);		# load inp
2600		&lea	($acc,&DWP(16,$acc));	# advance inp
2601		&mov	($_inp,$acc);		# save inp
2602	&jnz	(&label("slow_dec_loop_x86"));
2603	&mov	("esp",$_esp);
2604	&popf	();
2605	&function_end_A();
2606	&pushf	();			# kludge, never executed
2607
2608    &set_label("slow_dec_partial_x86",16);
2609	&lea	($acc,$ivec);
2610	&mov	(&DWP(0,$acc),$s0);	# save output to temp
2611	&mov	(&DWP(4,$acc),$s1);
2612	&mov	(&DWP(8,$acc),$s2);
2613	&mov	(&DWP(12,$acc),$s3);
2614
2615	&mov	($acc,$_inp);
2616	&mov	($s0,&DWP(0,$acc));	# re-read input
2617	&mov	($s1,&DWP(4,$acc));
2618	&mov	($s2,&DWP(8,$acc));
2619	&mov	($s3,&DWP(12,$acc));
2620
2621	&mov	(&DWP(0,$key),$s0);	# copy it to iv
2622	&mov	(&DWP(4,$key),$s1);
2623	&mov	(&DWP(8,$key),$s2);
2624	&mov	(&DWP(12,$key),$s3);
2625
2626	&mov	("ecx",$_len);
2627	&mov	("edi",$_out);
2628	&lea	("esi",$ivec);
2629	&align	(4);
2630	&data_word(0xA4F3F689);		# rep movsb # copy partial output
2631
2632	&mov	("esp",$_esp);
2633	&popf	();
2634&function_end("asm_AES_cbc_encrypt");
2635}
2636
2637#------------------------------------------------------------------#
2638
2639sub enckey()
2640{
2641	&movz	("esi",&LB("edx"));		# rk[i]>>0
2642	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2643	&movz	("esi",&HB("edx"));		# rk[i]>>8
2644	&shl	("ebx",24);
2645	&xor	("eax","ebx");
2646
2647	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2648	&shr	("edx",16);
2649	&movz	("esi",&LB("edx"));		# rk[i]>>16
2650	&xor	("eax","ebx");
2651
2652	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2653	&movz	("esi",&HB("edx"));		# rk[i]>>24
2654	&shl	("ebx",8);
2655	&xor	("eax","ebx");
2656
2657	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2658	&shl	("ebx",16);
2659	&xor	("eax","ebx");
2660
2661	&xor	("eax",&DWP(1024-128,$tbl,"ecx",4));	# rcon
2662}
2663
2664&function_begin("_x86_AES_set_encrypt_key");
2665	&mov	("esi",&wparam(1));		# user supplied key
2666	&mov	("edi",&wparam(3));		# private key schedule
2667
2668	&test	("esi",-1);
2669	&jz	(&label("badpointer"));
2670	&test	("edi",-1);
2671	&jz	(&label("badpointer"));
2672
2673	&call	(&label("pic_point"));
2674	&set_label("pic_point");
2675	&blindpop($tbl);
2676	&lea	($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
2677	&lea	($tbl,&DWP(2048+128,$tbl));
2678
2679	# prefetch Te4
2680	&mov	("eax",&DWP(0-128,$tbl));
2681	&mov	("ebx",&DWP(32-128,$tbl));
2682	&mov	("ecx",&DWP(64-128,$tbl));
2683	&mov	("edx",&DWP(96-128,$tbl));
2684	&mov	("eax",&DWP(128-128,$tbl));
2685	&mov	("ebx",&DWP(160-128,$tbl));
2686	&mov	("ecx",&DWP(192-128,$tbl));
2687	&mov	("edx",&DWP(224-128,$tbl));
2688
2689	&mov	("ecx",&wparam(2));		# number of bits in key
2690	&cmp	("ecx",128);
2691	&je	(&label("10rounds"));
2692	&cmp	("ecx",192);
2693	&je	(&label("12rounds"));
2694	&cmp	("ecx",256);
2695	&je	(&label("14rounds"));
2696	&mov	("eax",-2);			# invalid number of bits
2697	&jmp	(&label("exit"));
2698
2699    &set_label("10rounds");
2700	&mov	("eax",&DWP(0,"esi"));		# copy first 4 dwords
2701	&mov	("ebx",&DWP(4,"esi"));
2702	&mov	("ecx",&DWP(8,"esi"));
2703	&mov	("edx",&DWP(12,"esi"));
2704	&mov	(&DWP(0,"edi"),"eax");
2705	&mov	(&DWP(4,"edi"),"ebx");
2706	&mov	(&DWP(8,"edi"),"ecx");
2707	&mov	(&DWP(12,"edi"),"edx");
2708
2709	&xor	("ecx","ecx");
2710	&jmp	(&label("10shortcut"));
2711
2712	&align	(4);
2713	&set_label("10loop");
2714		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2715		&mov	("edx",&DWP(12,"edi"));		# rk[3]
2716	&set_label("10shortcut");
2717		&enckey	();
2718
2719		&mov	(&DWP(16,"edi"),"eax");		# rk[4]
2720		&xor	("eax",&DWP(4,"edi"));
2721		&mov	(&DWP(20,"edi"),"eax");		# rk[5]
2722		&xor	("eax",&DWP(8,"edi"));
2723		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
2724		&xor	("eax",&DWP(12,"edi"));
2725		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
2726		&inc	("ecx");
2727		&add	("edi",16);
2728		&cmp	("ecx",10);
2729	&jl	(&label("10loop"));
2730
2731	&mov	(&DWP(80,"edi"),10);		# setup number of rounds
2732	&xor	("eax","eax");
2733	&jmp	(&label("exit"));
2734
2735    &set_label("12rounds");
2736	&mov	("eax",&DWP(0,"esi"));		# copy first 6 dwords
2737	&mov	("ebx",&DWP(4,"esi"));
2738	&mov	("ecx",&DWP(8,"esi"));
2739	&mov	("edx",&DWP(12,"esi"));
2740	&mov	(&DWP(0,"edi"),"eax");
2741	&mov	(&DWP(4,"edi"),"ebx");
2742	&mov	(&DWP(8,"edi"),"ecx");
2743	&mov	(&DWP(12,"edi"),"edx");
2744	&mov	("ecx",&DWP(16,"esi"));
2745	&mov	("edx",&DWP(20,"esi"));
2746	&mov	(&DWP(16,"edi"),"ecx");
2747	&mov	(&DWP(20,"edi"),"edx");
2748
2749	&xor	("ecx","ecx");
2750	&jmp	(&label("12shortcut"));
2751
2752	&align	(4);
2753	&set_label("12loop");
2754		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2755		&mov	("edx",&DWP(20,"edi"));		# rk[5]
2756	&set_label("12shortcut");
2757		&enckey	();
2758
2759		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
2760		&xor	("eax",&DWP(4,"edi"));
2761		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
2762		&xor	("eax",&DWP(8,"edi"));
2763		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
2764		&xor	("eax",&DWP(12,"edi"));
2765		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
2766
2767		&cmp	("ecx",7);
2768		&je	(&label("12break"));
2769		&inc	("ecx");
2770
2771		&xor	("eax",&DWP(16,"edi"));
2772		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
2773		&xor	("eax",&DWP(20,"edi"));
2774		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
2775
2776		&add	("edi",24);
2777	&jmp	(&label("12loop"));
2778
2779	&set_label("12break");
2780	&mov	(&DWP(72,"edi"),12);		# setup number of rounds
2781	&xor	("eax","eax");
2782	&jmp	(&label("exit"));
2783
2784    &set_label("14rounds");
2785	&mov	("eax",&DWP(0,"esi"));		# copy first 8 dwords
2786	&mov	("ebx",&DWP(4,"esi"));
2787	&mov	("ecx",&DWP(8,"esi"));
2788	&mov	("edx",&DWP(12,"esi"));
2789	&mov	(&DWP(0,"edi"),"eax");
2790	&mov	(&DWP(4,"edi"),"ebx");
2791	&mov	(&DWP(8,"edi"),"ecx");
2792	&mov	(&DWP(12,"edi"),"edx");
2793	&mov	("eax",&DWP(16,"esi"));
2794	&mov	("ebx",&DWP(20,"esi"));
2795	&mov	("ecx",&DWP(24,"esi"));
2796	&mov	("edx",&DWP(28,"esi"));
2797	&mov	(&DWP(16,"edi"),"eax");
2798	&mov	(&DWP(20,"edi"),"ebx");
2799	&mov	(&DWP(24,"edi"),"ecx");
2800	&mov	(&DWP(28,"edi"),"edx");
2801
2802	&xor	("ecx","ecx");
2803	&jmp	(&label("14shortcut"));
2804
2805	&align	(4);
2806	&set_label("14loop");
2807		&mov	("edx",&DWP(28,"edi"));		# rk[7]
2808	&set_label("14shortcut");
2809		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2810
2811		&enckey	();
2812
2813		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
2814		&xor	("eax",&DWP(4,"edi"));
2815		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
2816		&xor	("eax",&DWP(8,"edi"));
2817		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
2818		&xor	("eax",&DWP(12,"edi"));
2819		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
2820
2821		&cmp	("ecx",6);
2822		&je	(&label("14break"));
2823		&inc	("ecx");
2824
2825		&mov	("edx","eax");
2826		&mov	("eax",&DWP(16,"edi"));		# rk[4]
2827		&movz	("esi",&LB("edx"));		# rk[11]>>0
2828		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2829		&movz	("esi",&HB("edx"));		# rk[11]>>8
2830		&xor	("eax","ebx");
2831
2832		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2833		&shr	("edx",16);
2834		&shl	("ebx",8);
2835		&movz	("esi",&LB("edx"));		# rk[11]>>16
2836		&xor	("eax","ebx");
2837
2838		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2839		&movz	("esi",&HB("edx"));		# rk[11]>>24
2840		&shl	("ebx",16);
2841		&xor	("eax","ebx");
2842
2843		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2844		&shl	("ebx",24);
2845		&xor	("eax","ebx");
2846
2847		&mov	(&DWP(48,"edi"),"eax");		# rk[12]
2848		&xor	("eax",&DWP(20,"edi"));
2849		&mov	(&DWP(52,"edi"),"eax");		# rk[13]
2850		&xor	("eax",&DWP(24,"edi"));
2851		&mov	(&DWP(56,"edi"),"eax");		# rk[14]
2852		&xor	("eax",&DWP(28,"edi"));
2853		&mov	(&DWP(60,"edi"),"eax");		# rk[15]
2854
2855		&add	("edi",32);
2856	&jmp	(&label("14loop"));
2857
2858	&set_label("14break");
2859	&mov	(&DWP(48,"edi"),14);		# setup number of rounds
2860	&xor	("eax","eax");
2861	&jmp	(&label("exit"));
2862
2863    &set_label("badpointer");
2864	&mov	("eax",-1);
2865    &set_label("exit");
2866&function_end("_x86_AES_set_encrypt_key");
2867
2868# int asm_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
2869#                             AES_KEY *key)
2870&function_begin_B("asm_AES_set_encrypt_key");
2871	&call	("_x86_AES_set_encrypt_key");
2872	&ret	();
2873&function_end_B("asm_AES_set_encrypt_key");
2874
2875sub deckey()
2876{ my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_;
2877  my $tmp = $tbl;
2878
2879	&mov	($tmp,0x80808080);
2880	&and	($tmp,$tp1);
2881	&lea	($tp2,&DWP(0,$tp1,$tp1));
2882	&mov	($acc,$tmp);
2883	&shr	($tmp,7);
2884	&sub	($acc,$tmp);
2885	&and	($tp2,0xfefefefe);
2886	&and	($acc,0x1b1b1b1b);
2887	&xor	($tp2,$acc);
2888	&mov	($tmp,0x80808080);
2889
2890	&and	($tmp,$tp2);
2891	&lea	($tp4,&DWP(0,$tp2,$tp2));
2892	&mov	($acc,$tmp);
2893	&shr	($tmp,7);
2894	&sub	($acc,$tmp);
2895	&and	($tp4,0xfefefefe);
2896	&and	($acc,0x1b1b1b1b);
2897	 &xor	($tp2,$tp1);	# tp2^tp1
2898	&xor	($tp4,$acc);
2899	&mov	($tmp,0x80808080);
2900
2901	&and	($tmp,$tp4);
2902	&lea	($tp8,&DWP(0,$tp4,$tp4));
2903	&mov	($acc,$tmp);
2904	&shr	($tmp,7);
2905	 &xor	($tp4,$tp1);	# tp4^tp1
2906	&sub	($acc,$tmp);
2907	&and	($tp8,0xfefefefe);
2908	&and	($acc,0x1b1b1b1b);
2909	 &rotl	($tp1,8);	# = ROTATE(tp1,8)
2910	&xor	($tp8,$acc);
2911
2912	&mov	($tmp,&DWP(4*($i+1),$key));	# modulo-scheduled load
2913
2914	&xor	($tp1,$tp2);
2915	&xor	($tp2,$tp8);
2916	&xor	($tp1,$tp4);
2917	&rotl	($tp2,24);
2918	&xor	($tp4,$tp8);
2919	&xor	($tp1,$tp8);	# ^= tp8^(tp4^tp1)^(tp2^tp1)
2920	&rotl	($tp4,16);
2921	&xor	($tp1,$tp2);	# ^= ROTATE(tp8^tp2^tp1,24)
2922	&rotl	($tp8,8);
2923	&xor	($tp1,$tp4);	# ^= ROTATE(tp8^tp4^tp1,16)
2924	&mov	($tp2,$tmp);
2925	&xor	($tp1,$tp8);	# ^= ROTATE(tp8,8)
2926
2927	&mov	(&DWP(4*$i,$key),$tp1);
2928}
2929
2930# int asm_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
2931#                             AES_KEY *key)
2932&function_begin_B("asm_AES_set_decrypt_key");
2933	&call	("_x86_AES_set_encrypt_key");
2934	&cmp	("eax",0);
2935	&je	(&label("proceed"));
2936	&ret	();
2937
2938    &set_label("proceed");
2939	&push	("ebp");
2940	&push	("ebx");
2941	&push	("esi");
2942	&push	("edi");
2943
2944	&mov	("esi",&wparam(2));
2945	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
2946	&lea	("ecx",&DWP(0,"","ecx",4));
2947	&lea	("edi",&DWP(0,"esi","ecx",4));	# pointer to last chunk
2948
2949	&set_label("invert",4);			# invert order of chunks
2950		&mov	("eax",&DWP(0,"esi"));
2951		&mov	("ebx",&DWP(4,"esi"));
2952		&mov	("ecx",&DWP(0,"edi"));
2953		&mov	("edx",&DWP(4,"edi"));
2954		&mov	(&DWP(0,"edi"),"eax");
2955		&mov	(&DWP(4,"edi"),"ebx");
2956		&mov	(&DWP(0,"esi"),"ecx");
2957		&mov	(&DWP(4,"esi"),"edx");
2958		&mov	("eax",&DWP(8,"esi"));
2959		&mov	("ebx",&DWP(12,"esi"));
2960		&mov	("ecx",&DWP(8,"edi"));
2961		&mov	("edx",&DWP(12,"edi"));
2962		&mov	(&DWP(8,"edi"),"eax");
2963		&mov	(&DWP(12,"edi"),"ebx");
2964		&mov	(&DWP(8,"esi"),"ecx");
2965		&mov	(&DWP(12,"esi"),"edx");
2966		&add	("esi",16);
2967		&sub	("edi",16);
2968		&cmp	("esi","edi");
2969	&jne	(&label("invert"));
2970
2971	&mov	($key,&wparam(2));
2972	&mov	($acc,&DWP(240,$key));		# pull number of rounds
2973	&lea	($acc,&DWP(-2,$acc,$acc));
2974	&lea	($acc,&DWP(0,$key,$acc,8));
2975	&mov	(&wparam(2),$acc);
2976
2977	&mov	($s0,&DWP(16,$key));		# modulo-scheduled load
2978	&set_label("permute",4);		# permute the key schedule
2979		&add	($key,16);
2980		&deckey	(0,$key,$s0,$s1,$s2,$s3);
2981		&deckey	(1,$key,$s1,$s2,$s3,$s0);
2982		&deckey	(2,$key,$s2,$s3,$s0,$s1);
2983		&deckey	(3,$key,$s3,$s0,$s1,$s2);
2984		&cmp	($key,&wparam(2));
2985	&jb	(&label("permute"));
2986
2987	&xor	("eax","eax");			# return success
2988&function_end("asm_AES_set_decrypt_key");
2989&asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>");
2990
2991&asm_finish();
2992
2993close STDOUT;
2994