1160814Ssimon#!/usr/bin/env perl
2160814Ssimon#
3160814Ssimon# ====================================================================
4160814Ssimon# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5238405Sjkim# project. The module is, however, dual licensed under OpenSSL and
6238405Sjkim# CRYPTOGAMS licenses depending on where you obtain it. For further
7238405Sjkim# details see http://www.openssl.org/~appro/cryptogams/.
8160814Ssimon# ====================================================================
9160814Ssimon#
10238405Sjkim# Version 4.3.
11160814Ssimon#
12160814Ssimon# You might fail to appreciate this module performance from the first
13160814Ssimon# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
14160814Ssimon# to be *the* best Intel C compiler without -KPIC, performance appears
15160814Ssimon# to be virtually identical... But try to re-configure with shared
16160814Ssimon# library support... Aha! Intel compiler "suddenly" lags behind by 30%
17160814Ssimon# [on P4, more on others]:-) And if compared to position-independent
18160814Ssimon# code generated by GNU C, this code performs *more* than *twice* as
19160814Ssimon# fast! Yes, all this buzz about PIC means that unlike other hand-
20160814Ssimon# coded implementations, this one was explicitly designed to be safe
21160814Ssimon# to use even in shared library context... This also means that this
22160814Ssimon# code isn't necessarily absolutely fastest "ever," because in order
23160814Ssimon# to achieve position independence an extra register has to be
24160814Ssimon# off-loaded to stack, which affects the benchmark result.
25160814Ssimon#
26160814Ssimon# Special note about instruction choice. Do you recall RC4_INT code
27160814Ssimon# performing poorly on P4? It might be the time to figure out why.
28160814Ssimon# RC4_INT code implies effective address calculations in base+offset*4
29160814Ssimon# form. Trouble is that it seems that offset scaling turned to be
30160814Ssimon# critical path... At least eliminating scaling resulted in 2.8x RC4
31160814Ssimon# performance improvement [as you might recall]. As AES code is hungry
32160814Ssimon# for scaling too, I [try to] avoid the latter by favoring off-by-2
33160814Ssimon# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
34160814Ssimon#
35160814Ssimon# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
36160814Ssimon# void. Performance improvement with off-by-2 shifts was observed on
37160814Ssimon# intermediate implementation, which was spilling yet another register
38160814Ssimon# to stack... Final offset*4 code below runs just a tad faster on P4,
39160814Ssimon# but exhibits up to 10% improvement on other cores.
40160814Ssimon#
41160814Ssimon# Second version is "monolithic" replacement for aes_core.c, which in
42238405Sjkim# addition to AES_[de|en]crypt implements private_AES_set_[de|en]cryption_key.
43160814Ssimon# This made it possible to implement little-endian variant of the
44160814Ssimon# algorithm without modifying the base C code. Motivating factor for
45160814Ssimon# the undertaken effort was that it appeared that in tight IA-32
46160814Ssimon# register window little-endian flavor could achieve slightly higher
47160814Ssimon# Instruction Level Parallelism, and it indeed resulted in up to 15%
48291721Sjkim# better performance on most recent ��-archs...
49160814Ssimon#
50160814Ssimon# Third version adds AES_cbc_encrypt implementation, which resulted in
51160814Ssimon# up to 40% performance imrovement of CBC benchmark results. 40% was
52160814Ssimon# observed on P4 core, where "overall" imrovement coefficient, i.e. if
53160814Ssimon# compared to PIC generated by GCC and in CBC mode, was observed to be
54160814Ssimon# as large as 4x:-) CBC performance is virtually identical to ECB now
55160814Ssimon# and on some platforms even better, e.g. 17.6 "small" cycles/byte on
56160814Ssimon# Opteron, because certain function prologues and epilogues are
57160814Ssimon# effectively taken out of the loop...
58160814Ssimon#
59160814Ssimon# Version 3.2 implements compressed tables and prefetch of these tables
60160814Ssimon# in CBC[!] mode. Former means that 3/4 of table references are now
61160814Ssimon# misaligned, which unfortunately has negative impact on elder IA-32
62160814Ssimon# implementations, Pentium suffered 30% penalty, PIII - 10%.
63160814Ssimon#
64160814Ssimon# Version 3.3 avoids L1 cache aliasing between stack frame and
65160814Ssimon# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
66160814Ssimon# latter is achieved by copying the key schedule to controlled place in
67160814Ssimon# stack. This unfortunately has rather strong impact on small block CBC
68160814Ssimon# performance, ~2x deterioration on 16-byte block if compared to 3.3.
69160814Ssimon#
70162911Ssimon# Version 3.5 checks if there is L1 cache aliasing between user-supplied
71162911Ssimon# key schedule and S-boxes and abstains from copying the former if
72162911Ssimon# there is no. This allows end-user to consciously retain small block
73162911Ssimon# performance by aligning key schedule in specific manner.
74162911Ssimon#
75162911Ssimon# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
76162911Ssimon#
77160814Ssimon# Current ECB performance numbers for 128-bit key in CPU cycles per
78160814Ssimon# processed byte [measure commonly used by AES benchmarkers] are:
79160814Ssimon#
80160814Ssimon#		small footprint		fully unrolled
81160814Ssimon# P4		24			22
82160814Ssimon# AMD K8	20			19
83160814Ssimon# PIII		25			23
84160814Ssimon# Pentium	81			78
85238405Sjkim#
86238405Sjkim# Version 3.7 reimplements outer rounds as "compact." Meaning that
87238405Sjkim# first and last rounds reference compact 256 bytes S-box. This means
88238405Sjkim# that first round consumes a lot more CPU cycles and that encrypt
89238405Sjkim# and decrypt performance becomes asymmetric. Encrypt performance
90238405Sjkim# drops by 10-12%, while decrypt - by 20-25%:-( 256 bytes S-box is
91238405Sjkim# aggressively pre-fetched.
92238405Sjkim#
93238405Sjkim# Version 4.0 effectively rolls back to 3.6 and instead implements
94238405Sjkim# additional set of functions, _[x86|sse]_AES_[en|de]crypt_compact,
95238405Sjkim# which use exclusively 256 byte S-box. These functions are to be
96238405Sjkim# called in modes not concealing plain text, such as ECB, or when
97238405Sjkim# we're asked to process smaller amount of data [or unconditionally
98238405Sjkim# on hyper-threading CPU]. Currently it's called unconditionally from
99238405Sjkim# AES_[en|de]crypt, which affects all modes, but CBC. CBC routine
100238405Sjkim# still needs to be modified to switch between slower and faster
101238405Sjkim# mode when appropriate... But in either case benchmark landscape
102238405Sjkim# changes dramatically and below numbers are CPU cycles per processed
103238405Sjkim# byte for 128-bit key.
104238405Sjkim#
105238405Sjkim#		ECB encrypt	ECB decrypt	CBC large chunk
106238405Sjkim# P4		56[60]		84[100]		23
107238405Sjkim# AMD K8	48[44]		70[79]		18
108238405Sjkim# PIII		41[50]		61[91]		24
109238405Sjkim# Core 2	32[38]		45[70]		18.5
110238405Sjkim# Pentium	120		160		77
111238405Sjkim#
112238405Sjkim# Version 4.1 switches to compact S-box even in key schedule setup.
113238405Sjkim#
114238405Sjkim# Version 4.2 prefetches compact S-box in every SSE round or in other
115238405Sjkim# words every cache-line is *guaranteed* to be accessed within ~50
116238405Sjkim# cycles window. Why just SSE? Because it's needed on hyper-threading
117238405Sjkim# CPU! Which is also why it's prefetched with 64 byte stride. Best
118238405Sjkim# part is that it has no negative effect on performance:-)
119238405Sjkim#
120238405Sjkim# Version 4.3 implements switch between compact and non-compact block
121238405Sjkim# functions in AES_cbc_encrypt depending on how much data was asked
122238405Sjkim# to be processed in one stroke.
123238405Sjkim#
124238405Sjkim######################################################################
125238405Sjkim# Timing attacks are classified in two classes: synchronous when
126238405Sjkim# attacker consciously initiates cryptographic operation and collects
127238405Sjkim# timing data of various character afterwards, and asynchronous when
128238405Sjkim# malicious code is executed on same CPU simultaneously with AES,
129238405Sjkim# instruments itself and performs statistical analysis of this data.
130238405Sjkim#
131238405Sjkim# As far as synchronous attacks go the root to the AES timing
132238405Sjkim# vulnerability is twofold. Firstly, of 256 S-box elements at most 160
133238405Sjkim# are referred to in single 128-bit block operation. Well, in C
134238405Sjkim# implementation with 4 distinct tables it's actually as little as 40
135238405Sjkim# references per 256 elements table, but anyway... Secondly, even
136238405Sjkim# though S-box elements are clustered into smaller amount of cache-
137238405Sjkim# lines, smaller than 160 and even 40, it turned out that for certain
138238405Sjkim# plain-text pattern[s] or simply put chosen plain-text and given key
139238405Sjkim# few cache-lines remain unaccessed during block operation. Now, if
140238405Sjkim# attacker can figure out this access pattern, he can deduct the key
141238405Sjkim# [or at least part of it]. The natural way to mitigate this kind of
142238405Sjkim# attacks is to minimize the amount of cache-lines in S-box and/or
143238405Sjkim# prefetch them to ensure that every one is accessed for more uniform
144238405Sjkim# timing. But note that *if* plain-text was concealed in such way that
145238405Sjkim# input to block function is distributed *uniformly*, then attack
146238405Sjkim# wouldn't apply. Now note that some encryption modes, most notably
147238405Sjkim# CBC, do mask the plain-text in this exact way [secure cipher output
148238405Sjkim# is distributed uniformly]. Yes, one still might find input that
149238405Sjkim# would reveal the information about given key, but if amount of
150238405Sjkim# candidate inputs to be tried is larger than amount of possible key
151238405Sjkim# combinations then attack becomes infeasible. This is why revised
152238405Sjkim# AES_cbc_encrypt "dares" to switch to larger S-box when larger chunk
153238405Sjkim# of data is to be processed in one stroke. The current size limit of
154238405Sjkim# 512 bytes is chosen to provide same [diminishigly low] probability
155238405Sjkim# for cache-line to remain untouched in large chunk operation with
156238405Sjkim# large S-box as for single block operation with compact S-box and
157238405Sjkim# surely needs more careful consideration...
158238405Sjkim#
159238405Sjkim# As for asynchronous attacks. There are two flavours: attacker code
160238405Sjkim# being interleaved with AES on hyper-threading CPU at *instruction*
161238405Sjkim# level, and two processes time sharing single core. As for latter.
162238405Sjkim# Two vectors. 1. Given that attacker process has higher priority,
163238405Sjkim# yield execution to process performing AES just before timer fires
164238405Sjkim# off the scheduler, immediately regain control of CPU and analyze the
165238405Sjkim# cache state. For this attack to be efficient attacker would have to
166238405Sjkim# effectively slow down the operation by several *orders* of magnitute,
167238405Sjkim# by ratio of time slice to duration of handful of AES rounds, which
168238405Sjkim# unlikely to remain unnoticed. Not to mention that this also means
169238405Sjkim# that he would spend correspondigly more time to collect enough
170238405Sjkim# statistical data to mount the attack. It's probably appropriate to
171238405Sjkim# say that if adeversary reckons that this attack is beneficial and
172238405Sjkim# risks to be noticed, you probably have larger problems having him
173238405Sjkim# mere opportunity. In other words suggested code design expects you
174238405Sjkim# to preclude/mitigate this attack by overall system security design.
175238405Sjkim# 2. Attacker manages to make his code interrupt driven. In order for
176238405Sjkim# this kind of attack to be feasible, interrupt rate has to be high
177238405Sjkim# enough, again comparable to duration of handful of AES rounds. But
178238405Sjkim# is there interrupt source of such rate? Hardly, not even 1Gbps NIC
179238405Sjkim# generates interrupts at such raging rate...
180238405Sjkim#
181238405Sjkim# And now back to the former, hyper-threading CPU or more specifically
182238405Sjkim# Intel P4. Recall that asynchronous attack implies that malicious
183238405Sjkim# code instruments itself. And naturally instrumentation granularity
184238405Sjkim# has be noticeably lower than duration of codepath accessing S-box.
185238405Sjkim# Given that all cache-lines are accessed during that time that is.
186238405Sjkim# Current implementation accesses *all* cache-lines within ~50 cycles
187238405Sjkim# window, which is actually *less* than RDTSC latency on Intel P4!
188160814Ssimon
189238405Sjkim$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
190238405Sjkimpush(@INC,"${dir}","${dir}../../perlasm");
191160814Ssimonrequire "x86asm.pl";
192160814Ssimon
193238405Sjkim&asm_init($ARGV[0],"aes-586.pl",$x86only = $ARGV[$#ARGV] eq "386");
194238405Sjkim&static_label("AES_Te");
195238405Sjkim&static_label("AES_Td");
196160814Ssimon
197160814Ssimon$s0="eax";
198160814Ssimon$s1="ebx";
199160814Ssimon$s2="ecx";
200160814Ssimon$s3="edx";
201160814Ssimon$key="edi";
202160814Ssimon$acc="esi";
203238405Sjkim$tbl="ebp";
204160814Ssimon
205238405Sjkim# stack frame layout in _[x86|sse]_AES_* routines, frame is allocated
206238405Sjkim# by caller
207238405Sjkim$__ra=&DWP(0,"esp");	# return address
208238405Sjkim$__s0=&DWP(4,"esp");	# s0 backing store
209238405Sjkim$__s1=&DWP(8,"esp");	# s1 backing store
210238405Sjkim$__s2=&DWP(12,"esp");	# s2 backing store
211238405Sjkim$__s3=&DWP(16,"esp");	# s3 backing store
212238405Sjkim$__key=&DWP(20,"esp");	# pointer to key schedule
213238405Sjkim$__end=&DWP(24,"esp");	# pointer to end of key schedule
214238405Sjkim$__tbl=&DWP(28,"esp");	# %ebp backing store
215238405Sjkim
216238405Sjkim# stack frame layout in AES_[en|crypt] routines, which differs from
217238405Sjkim# above by 4 and overlaps by %ebp backing store
218238405Sjkim$_tbl=&DWP(24,"esp");
219238405Sjkim$_esp=&DWP(28,"esp");
220238405Sjkim
221238405Sjkimsub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
222238405Sjkim
223238405Sjkim$speed_limit=512;	# chunks smaller than $speed_limit are
224238405Sjkim			# processed with compact routine in CBC mode
225160814Ssimon$small_footprint=1;	# $small_footprint=1 code is ~5% slower [on
226291721Sjkim			# recent ��-archs], but ~5 times smaller!
227160814Ssimon			# I favor compact code to minimize cache
228160814Ssimon			# contention and in hope to "collect" 5% back
229160814Ssimon			# in real-life applications...
230238405Sjkim
231160814Ssimon$vertical_spin=0;	# shift "verticaly" defaults to 0, because of
232160814Ssimon			# its proof-of-concept status...
233160814Ssimon# Note that there is no decvert(), as well as last encryption round is
234160814Ssimon# performed with "horizontal" shifts. This is because this "vertical"
235160814Ssimon# implementation [one which groups shifts on a given $s[i] to form a
236160814Ssimon# "column," unlike "horizontal" one, which groups shifts on different
237160814Ssimon# $s[i] to form a "row"] is work in progress. It was observed to run
238160814Ssimon# few percents faster on Intel cores, but not AMD. On AMD K8 core it's
239160814Ssimon# whole 12% slower:-( So we face a trade-off... Shall it be resolved
240160814Ssimon# some day? Till then the code is considered experimental and by
241160814Ssimon# default remains dormant...
242160814Ssimon
243160814Ssimonsub encvert()
244160814Ssimon{ my ($te,@s) = @_;
245160814Ssimon  my $v0 = $acc, $v1 = $key;
246160814Ssimon
247160814Ssimon	&mov	($v0,$s[3]);				# copy s3
248160814Ssimon	&mov	(&DWP(4,"esp"),$s[2]);			# save s2
249160814Ssimon	&mov	($v1,$s[0]);				# copy s0
250160814Ssimon	&mov	(&DWP(8,"esp"),$s[1]);			# save s1
251160814Ssimon
252160814Ssimon	&movz	($s[2],&HB($s[0]));
253160814Ssimon	&and	($s[0],0xFF);
254160814Ssimon	&mov	($s[0],&DWP(0,$te,$s[0],8));		# s0>>0
255160814Ssimon	&shr	($v1,16);
256160814Ssimon	&mov	($s[3],&DWP(3,$te,$s[2],8));		# s0>>8
257160814Ssimon	&movz	($s[1],&HB($v1));
258160814Ssimon	&and	($v1,0xFF);
259160814Ssimon	&mov	($s[2],&DWP(2,$te,$v1,8));		# s0>>16
260160814Ssimon	 &mov	($v1,$v0);
261160814Ssimon	&mov	($s[1],&DWP(1,$te,$s[1],8));		# s0>>24
262160814Ssimon
263160814Ssimon	&and	($v0,0xFF);
264160814Ssimon	&xor	($s[3],&DWP(0,$te,$v0,8));		# s3>>0
265160814Ssimon	&movz	($v0,&HB($v1));
266160814Ssimon	&shr	($v1,16);
267160814Ssimon	&xor	($s[2],&DWP(3,$te,$v0,8));		# s3>>8
268160814Ssimon	&movz	($v0,&HB($v1));
269160814Ssimon	&and	($v1,0xFF);
270160814Ssimon	&xor	($s[1],&DWP(2,$te,$v1,8));		# s3>>16
271160814Ssimon	 &mov	($v1,&DWP(4,"esp"));			# restore s2
272160814Ssimon	&xor	($s[0],&DWP(1,$te,$v0,8));		# s3>>24
273160814Ssimon
274160814Ssimon	&mov	($v0,$v1);
275160814Ssimon	&and	($v1,0xFF);
276160814Ssimon	&xor	($s[2],&DWP(0,$te,$v1,8));		# s2>>0
277160814Ssimon	&movz	($v1,&HB($v0));
278160814Ssimon	&shr	($v0,16);
279160814Ssimon	&xor	($s[1],&DWP(3,$te,$v1,8));		# s2>>8
280160814Ssimon	&movz	($v1,&HB($v0));
281160814Ssimon	&and	($v0,0xFF);
282160814Ssimon	&xor	($s[0],&DWP(2,$te,$v0,8));		# s2>>16
283160814Ssimon	 &mov	($v0,&DWP(8,"esp"));			# restore s1
284160814Ssimon	&xor	($s[3],&DWP(1,$te,$v1,8));		# s2>>24
285160814Ssimon
286160814Ssimon	&mov	($v1,$v0);
287160814Ssimon	&and	($v0,0xFF);
288160814Ssimon	&xor	($s[1],&DWP(0,$te,$v0,8));		# s1>>0
289160814Ssimon	&movz	($v0,&HB($v1));
290160814Ssimon	&shr	($v1,16);
291160814Ssimon	&xor	($s[0],&DWP(3,$te,$v0,8));		# s1>>8
292160814Ssimon	&movz	($v0,&HB($v1));
293160814Ssimon	&and	($v1,0xFF);
294160814Ssimon	&xor	($s[3],&DWP(2,$te,$v1,8));		# s1>>16
295238405Sjkim	 &mov	($key,$__key);				# reincarnate v1 as key
296160814Ssimon	&xor	($s[2],&DWP(1,$te,$v0,8));		# s1>>24
297160814Ssimon}
298160814Ssimon
299238405Sjkim# Another experimental routine, which features "horizontal spin," but
300238405Sjkim# eliminates one reference to stack. Strangely enough runs slower...
301238405Sjkimsub enchoriz()
302238405Sjkim{ my $v0 = $key, $v1 = $acc;
303238405Sjkim
304238405Sjkim	&movz	($v0,&LB($s0));			#  3, 2, 1, 0*
305238405Sjkim	&rotr	($s2,8);			#  8,11,10, 9
306238405Sjkim	&mov	($v1,&DWP(0,$te,$v0,8));	#  0
307238405Sjkim	&movz	($v0,&HB($s1));			#  7, 6, 5*, 4
308238405Sjkim	&rotr	($s3,16);			# 13,12,15,14
309238405Sjkim	&xor	($v1,&DWP(3,$te,$v0,8));	#  5
310238405Sjkim	&movz	($v0,&HB($s2));			#  8,11,10*, 9
311238405Sjkim	&rotr	($s0,16);			#  1, 0, 3, 2
312238405Sjkim	&xor	($v1,&DWP(2,$te,$v0,8));	# 10
313238405Sjkim	&movz	($v0,&HB($s3));			# 13,12,15*,14
314238405Sjkim	&xor	($v1,&DWP(1,$te,$v0,8));	# 15, t[0] collected
315238405Sjkim	&mov	($__s0,$v1);			# t[0] saved
316238405Sjkim
317238405Sjkim	&movz	($v0,&LB($s1));			#  7, 6, 5, 4*
318238405Sjkim	&shr	($s1,16);			#  -, -, 7, 6
319238405Sjkim	&mov	($v1,&DWP(0,$te,$v0,8));	#  4
320238405Sjkim	&movz	($v0,&LB($s3));			# 13,12,15,14*
321238405Sjkim	&xor	($v1,&DWP(2,$te,$v0,8));	# 14
322238405Sjkim	&movz	($v0,&HB($s0));			#  1, 0, 3*, 2
323238405Sjkim	&and	($s3,0xffff0000);		# 13,12, -, -
324238405Sjkim	&xor	($v1,&DWP(1,$te,$v0,8));	#  3
325238405Sjkim	&movz	($v0,&LB($s2));			#  8,11,10, 9*
326238405Sjkim	&or	($s3,$s1);			# 13,12, 7, 6
327238405Sjkim	&xor	($v1,&DWP(3,$te,$v0,8));	#  9, t[1] collected
328238405Sjkim	&mov	($s1,$v1);			#  s[1]=t[1]
329238405Sjkim
330238405Sjkim	&movz	($v0,&LB($s0));			#  1, 0, 3, 2*
331238405Sjkim	&shr	($s2,16);			#  -, -, 8,11
332238405Sjkim	&mov	($v1,&DWP(2,$te,$v0,8));	#  2
333238405Sjkim	&movz	($v0,&HB($s3));			# 13,12, 7*, 6
334238405Sjkim	&xor	($v1,&DWP(1,$te,$v0,8));	#  7
335238405Sjkim	&movz	($v0,&HB($s2));			#  -, -, 8*,11
336238405Sjkim	&xor	($v1,&DWP(0,$te,$v0,8));	#  8
337238405Sjkim	&mov	($v0,$s3);
338238405Sjkim	&shr	($v0,24);			# 13
339238405Sjkim	&xor	($v1,&DWP(3,$te,$v0,8));	# 13, t[2] collected
340238405Sjkim
341238405Sjkim	&movz	($v0,&LB($s2));			#  -, -, 8,11*
342238405Sjkim	&shr	($s0,24);			#  1*
343238405Sjkim	&mov	($s2,&DWP(1,$te,$v0,8));	# 11
344238405Sjkim	&xor	($s2,&DWP(3,$te,$s0,8));	#  1
345238405Sjkim	&mov	($s0,$__s0);			# s[0]=t[0]
346238405Sjkim	&movz	($v0,&LB($s3));			# 13,12, 7, 6*
347238405Sjkim	&shr	($s3,16);			#   ,  ,13,12
348238405Sjkim	&xor	($s2,&DWP(2,$te,$v0,8));	#  6
349238405Sjkim	&mov	($key,$__key);			# reincarnate v0 as key
350238405Sjkim	&and	($s3,0xff);			#   ,  ,13,12*
351238405Sjkim	&mov	($s3,&DWP(0,$te,$s3,8));	# 12
352238405Sjkim	&xor	($s3,$s2);			# s[2]=t[3] collected
353238405Sjkim	&mov	($s2,$v1);			# s[2]=t[2]
354238405Sjkim}
355238405Sjkim
356238405Sjkim# More experimental code... SSE one... Even though this one eliminates
357238405Sjkim# *all* references to stack, it's not faster...
358238405Sjkimsub sse_encbody()
359238405Sjkim{
360238405Sjkim	&movz	($acc,&LB("eax"));		#  0
361238405Sjkim	&mov	("ecx",&DWP(0,$tbl,$acc,8));	#  0
362238405Sjkim	&pshufw	("mm2","mm0",0x0d);		#  7, 6, 3, 2
363238405Sjkim	&movz	("edx",&HB("eax"));		#  1
364238405Sjkim	&mov	("edx",&DWP(3,$tbl,"edx",8));	#  1
365238405Sjkim	&shr	("eax",16);			#  5, 4
366238405Sjkim
367238405Sjkim	&movz	($acc,&LB("ebx"));		# 10
368238405Sjkim	&xor	("ecx",&DWP(2,$tbl,$acc,8));	# 10
369238405Sjkim	&pshufw	("mm6","mm4",0x08);		# 13,12, 9, 8
370238405Sjkim	&movz	($acc,&HB("ebx"));		# 11
371238405Sjkim	&xor	("edx",&DWP(1,$tbl,$acc,8));	# 11
372238405Sjkim	&shr	("ebx",16);			# 15,14
373238405Sjkim
374238405Sjkim	&movz	($acc,&HB("eax"));		#  5
375238405Sjkim	&xor	("ecx",&DWP(3,$tbl,$acc,8));	#  5
376238405Sjkim	&movq	("mm3",QWP(16,$key));
377238405Sjkim	&movz	($acc,&HB("ebx"));		# 15
378238405Sjkim	&xor	("ecx",&DWP(1,$tbl,$acc,8));	# 15
379238405Sjkim	&movd	("mm0","ecx");			# t[0] collected
380238405Sjkim
381238405Sjkim	&movz	($acc,&LB("eax"));		#  4
382238405Sjkim	&mov	("ecx",&DWP(0,$tbl,$acc,8));	#  4
383238405Sjkim	&movd	("eax","mm2");			#  7, 6, 3, 2
384238405Sjkim	&movz	($acc,&LB("ebx"));		# 14
385238405Sjkim	&xor	("ecx",&DWP(2,$tbl,$acc,8));	# 14
386238405Sjkim	&movd	("ebx","mm6");			# 13,12, 9, 8
387238405Sjkim
388238405Sjkim	&movz	($acc,&HB("eax"));		#  3
389238405Sjkim	&xor	("ecx",&DWP(1,$tbl,$acc,8));	#  3
390238405Sjkim	&movz	($acc,&HB("ebx"));		#  9
391238405Sjkim	&xor	("ecx",&DWP(3,$tbl,$acc,8));	#  9
392238405Sjkim	&movd	("mm1","ecx");			# t[1] collected
393238405Sjkim
394238405Sjkim	&movz	($acc,&LB("eax"));		#  2
395238405Sjkim	&mov	("ecx",&DWP(2,$tbl,$acc,8));	#  2
396238405Sjkim	&shr	("eax",16);			#  7, 6
397238405Sjkim	&punpckldq	("mm0","mm1");		# t[0,1] collected
398238405Sjkim	&movz	($acc,&LB("ebx"));		#  8
399238405Sjkim	&xor	("ecx",&DWP(0,$tbl,$acc,8));	#  8
400238405Sjkim	&shr	("ebx",16);			# 13,12
401238405Sjkim
402238405Sjkim	&movz	($acc,&HB("eax"));		#  7
403238405Sjkim	&xor	("ecx",&DWP(1,$tbl,$acc,8));	#  7
404238405Sjkim	&pxor	("mm0","mm3");
405238405Sjkim	&movz	("eax",&LB("eax"));		#  6
406238405Sjkim	&xor	("edx",&DWP(2,$tbl,"eax",8));	#  6
407238405Sjkim	&pshufw	("mm1","mm0",0x08);		#  5, 4, 1, 0
408238405Sjkim	&movz	($acc,&HB("ebx"));		# 13
409238405Sjkim	&xor	("ecx",&DWP(3,$tbl,$acc,8));	# 13
410238405Sjkim	&xor	("ecx",&DWP(24,$key));		# t[2]
411238405Sjkim	&movd	("mm4","ecx");			# t[2] collected
412238405Sjkim	&movz	("ebx",&LB("ebx"));		# 12
413238405Sjkim	&xor	("edx",&DWP(0,$tbl,"ebx",8));	# 12
414238405Sjkim	&shr	("ecx",16);
415238405Sjkim	&movd	("eax","mm1");			#  5, 4, 1, 0
416238405Sjkim	&mov	("ebx",&DWP(28,$key));		# t[3]
417238405Sjkim	&xor	("ebx","edx");
418238405Sjkim	&movd	("mm5","ebx");			# t[3] collected
419238405Sjkim	&and	("ebx",0xffff0000);
420238405Sjkim	&or	("ebx","ecx");
421238405Sjkim
422238405Sjkim	&punpckldq	("mm4","mm5");		# t[2,3] collected
423238405Sjkim}
424238405Sjkim
425238405Sjkim######################################################################
426238405Sjkim# "Compact" block function
427238405Sjkim######################################################################
428238405Sjkim
429238405Sjkimsub enccompact()
430238405Sjkim{ my $Fn = mov;
431238405Sjkim  while ($#_>5) { pop(@_); $Fn=sub{}; }
432238405Sjkim  my ($i,$te,@s)=@_;
433238405Sjkim  my $tmp = $key;
434238405Sjkim  my $out = $i==3?$s[0]:$acc;
435238405Sjkim
436238405Sjkim	# $Fn is used in first compact round and its purpose is to
437238405Sjkim	# void restoration of some values from stack, so that after
438238405Sjkim	# 4xenccompact with extra argument $key value is left there...
439238405Sjkim	if ($i==3)  {	&$Fn	($key,$__key);			}##%edx
440238405Sjkim	else        {	&mov	($out,$s[0]);			}
441238405Sjkim			&and	($out,0xFF);
442238405Sjkim	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
443238405Sjkim	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
444238405Sjkim			&movz	($out,&BP(-128,$te,$out,1));
445238405Sjkim
446238405Sjkim	if ($i==3)  {	$tmp=$s[1];				}##%eax
447238405Sjkim			&movz	($tmp,&HB($s[1]));
448238405Sjkim			&movz	($tmp,&BP(-128,$te,$tmp,1));
449238405Sjkim			&shl	($tmp,8);
450238405Sjkim			&xor	($out,$tmp);
451238405Sjkim
452238405Sjkim	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
453238405Sjkim	else        {	&mov	($tmp,$s[2]);
454238405Sjkim			&shr	($tmp,16);			}
455238405Sjkim	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
456238405Sjkim			&and	($tmp,0xFF);
457238405Sjkim			&movz	($tmp,&BP(-128,$te,$tmp,1));
458238405Sjkim			&shl	($tmp,16);
459238405Sjkim			&xor	($out,$tmp);
460238405Sjkim
461238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
462238405Sjkim	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
463238405Sjkim	else        {	&mov	($tmp,$s[3]);
464238405Sjkim			&shr	($tmp,24);			}
465238405Sjkim			&movz	($tmp,&BP(-128,$te,$tmp,1));
466238405Sjkim			&shl	($tmp,24);
467238405Sjkim			&xor	($out,$tmp);
468238405Sjkim	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
469238405Sjkim	if ($i==3)  {	&mov	($s[3],$acc);			}
470238405Sjkim	&comment();
471238405Sjkim}
472238405Sjkim
473238405Sjkimsub enctransform()
474238405Sjkim{ my @s = ($s0,$s1,$s2,$s3);
475238405Sjkim  my $i = shift;
476238405Sjkim  my $tmp = $tbl;
477238405Sjkim  my $r2  = $key ;
478238405Sjkim
479238405Sjkim	&mov	($acc,$s[$i]);
480238405Sjkim	&and	($acc,0x80808080);
481238405Sjkim	&mov	($tmp,$acc);
482238405Sjkim	&shr	($tmp,7);
483238405Sjkim	&lea	($r2,&DWP(0,$s[$i],$s[$i]));
484238405Sjkim	&sub	($acc,$tmp);
485238405Sjkim	&and	($r2,0xfefefefe);
486238405Sjkim	&and	($acc,0x1b1b1b1b);
487238405Sjkim	&mov	($tmp,$s[$i]);
488238405Sjkim	&xor	($acc,$r2);	# r2
489238405Sjkim
490238405Sjkim	&xor	($s[$i],$acc);	# r0 ^ r2
491238405Sjkim	&rotl	($s[$i],24);
492238405Sjkim	&xor	($s[$i],$acc)	# ROTATE(r2^r0,24) ^ r2
493238405Sjkim	&rotr	($tmp,16);
494238405Sjkim	&xor	($s[$i],$tmp);
495238405Sjkim	&rotr	($tmp,8);
496238405Sjkim	&xor	($s[$i],$tmp);
497238405Sjkim}
498238405Sjkim
499238405Sjkim&function_begin_B("_x86_AES_encrypt_compact");
500238405Sjkim	# note that caller is expected to allocate stack frame for me!
501238405Sjkim	&mov	($__key,$key);			# save key
502238405Sjkim
503238405Sjkim	&xor	($s0,&DWP(0,$key));		# xor with key
504238405Sjkim	&xor	($s1,&DWP(4,$key));
505238405Sjkim	&xor	($s2,&DWP(8,$key));
506238405Sjkim	&xor	($s3,&DWP(12,$key));
507238405Sjkim
508238405Sjkim	&mov	($acc,&DWP(240,$key));		# load key->rounds
509238405Sjkim	&lea	($acc,&DWP(-2,$acc,$acc));
510238405Sjkim	&lea	($acc,&DWP(0,$key,$acc,8));
511238405Sjkim	&mov	($__end,$acc);			# end of key schedule
512238405Sjkim
513238405Sjkim	# prefetch Te4
514238405Sjkim	&mov	($key,&DWP(0-128,$tbl));
515238405Sjkim	&mov	($acc,&DWP(32-128,$tbl));
516238405Sjkim	&mov	($key,&DWP(64-128,$tbl));
517238405Sjkim	&mov	($acc,&DWP(96-128,$tbl));
518238405Sjkim	&mov	($key,&DWP(128-128,$tbl));
519238405Sjkim	&mov	($acc,&DWP(160-128,$tbl));
520238405Sjkim	&mov	($key,&DWP(192-128,$tbl));
521238405Sjkim	&mov	($acc,&DWP(224-128,$tbl));
522238405Sjkim
523238405Sjkim	&set_label("loop",16);
524238405Sjkim
525238405Sjkim		&enccompact(0,$tbl,$s0,$s1,$s2,$s3,1);
526238405Sjkim		&enccompact(1,$tbl,$s1,$s2,$s3,$s0,1);
527238405Sjkim		&enccompact(2,$tbl,$s2,$s3,$s0,$s1,1);
528238405Sjkim		&enccompact(3,$tbl,$s3,$s0,$s1,$s2,1);
529238405Sjkim		&enctransform(2);
530238405Sjkim		&enctransform(3);
531238405Sjkim		&enctransform(0);
532238405Sjkim		&enctransform(1);
533238405Sjkim		&mov 	($key,$__key);
534238405Sjkim		&mov	($tbl,$__tbl);
535238405Sjkim		&add	($key,16);		# advance rd_key
536238405Sjkim		&xor	($s0,&DWP(0,$key));
537238405Sjkim		&xor	($s1,&DWP(4,$key));
538238405Sjkim		&xor	($s2,&DWP(8,$key));
539238405Sjkim		&xor	($s3,&DWP(12,$key));
540238405Sjkim
541238405Sjkim	&cmp	($key,$__end);
542238405Sjkim	&mov	($__key,$key);
543238405Sjkim	&jb	(&label("loop"));
544238405Sjkim
545238405Sjkim	&enccompact(0,$tbl,$s0,$s1,$s2,$s3);
546238405Sjkim	&enccompact(1,$tbl,$s1,$s2,$s3,$s0);
547238405Sjkim	&enccompact(2,$tbl,$s2,$s3,$s0,$s1);
548238405Sjkim	&enccompact(3,$tbl,$s3,$s0,$s1,$s2);
549238405Sjkim
550238405Sjkim	&xor	($s0,&DWP(16,$key));
551238405Sjkim	&xor	($s1,&DWP(20,$key));
552238405Sjkim	&xor	($s2,&DWP(24,$key));
553238405Sjkim	&xor	($s3,&DWP(28,$key));
554238405Sjkim
555238405Sjkim	&ret	();
556238405Sjkim&function_end_B("_x86_AES_encrypt_compact");
557238405Sjkim
558238405Sjkim######################################################################
559238405Sjkim# "Compact" SSE block function.
560238405Sjkim######################################################################
561238405Sjkim#
562238405Sjkim# Performance is not actually extraordinary in comparison to pure
563238405Sjkim# x86 code. In particular encrypt performance is virtually the same.
564238405Sjkim# Decrypt performance on the other hand is 15-20% better on newer
565291721Sjkim# ��-archs [but we're thankful for *any* improvement here], and ~50%
566238405Sjkim# better on PIII:-) And additionally on the pros side this code
567238405Sjkim# eliminates redundant references to stack and thus relieves/
568238405Sjkim# minimizes the pressure on the memory bus.
569238405Sjkim#
570238405Sjkim# MMX register layout                           lsb
571238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
572238405Sjkim# |          mm4          |          mm0          |
573238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
574238405Sjkim# |     s3    |     s2    |     s1    |     s0    |
575238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
576238405Sjkim# |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
577238405Sjkim# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
578238405Sjkim#
579238405Sjkim# Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8.
580238405Sjkim# In this terms encryption and decryption "compact" permutation
581238405Sjkim# matrices can be depicted as following:
582238405Sjkim#
583238405Sjkim# encryption              lsb	# decryption              lsb
584238405Sjkim# +----++----+----+----+----+	# +----++----+----+----+----+
585238405Sjkim# | t0 || 15 | 10 |  5 |  0 |	# | t0 ||  7 | 10 | 13 |  0 |
586238405Sjkim# +----++----+----+----+----+	# +----++----+----+----+----+
587238405Sjkim# | t1 ||  3 | 14 |  9 |  4 |	# | t1 || 11 | 14 |  1 |  4 |
588238405Sjkim# +----++----+----+----+----+	# +----++----+----+----+----+
589238405Sjkim# | t2 ||  7 |  2 | 13 |  8 |	# | t2 || 15 |  2 |  5 |  8 |
590238405Sjkim# +----++----+----+----+----+	# +----++----+----+----+----+
591238405Sjkim# | t3 || 11 |  6 |  1 | 12 |	# | t3 ||  3 |  6 |  9 | 12 |
592238405Sjkim# +----++----+----+----+----+	# +----++----+----+----+----+
593238405Sjkim#
594238405Sjkim######################################################################
595238405Sjkim# Why not xmm registers? Short answer. It was actually tested and
596238405Sjkim# was not any faster, but *contrary*, most notably on Intel CPUs.
597238405Sjkim# Longer answer. Main advantage of using mm registers is that movd
598238405Sjkim# latency is lower, especially on Intel P4. While arithmetic
599238405Sjkim# instructions are twice as many, they can be scheduled every cycle
600238405Sjkim# and not every second one when they are operating on xmm register,
601238405Sjkim# so that "arithmetic throughput" remains virtually the same. And
602238405Sjkim# finally the code can be executed even on elder SSE-only CPUs:-)
603238405Sjkim
604238405Sjkimsub sse_enccompact()
605238405Sjkim{
606238405Sjkim	&pshufw	("mm1","mm0",0x08);		#  5, 4, 1, 0
607238405Sjkim	&pshufw	("mm5","mm4",0x0d);		# 15,14,11,10
608238405Sjkim	&movd	("eax","mm1");			#  5, 4, 1, 0
609238405Sjkim	&movd	("ebx","mm5");			# 15,14,11,10
610238405Sjkim
611238405Sjkim	&movz	($acc,&LB("eax"));		#  0
612238405Sjkim	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  0
613238405Sjkim	&pshufw	("mm2","mm0",0x0d);		#  7, 6, 3, 2
614238405Sjkim	&movz	("edx",&HB("eax"));		#  1
615238405Sjkim	&movz	("edx",&BP(-128,$tbl,"edx",1));	#  1
616238405Sjkim	&shl	("edx",8);			#  1
617238405Sjkim	&shr	("eax",16);			#  5, 4
618238405Sjkim
619238405Sjkim	&movz	($acc,&LB("ebx"));		# 10
620238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 10
621238405Sjkim	&shl	($acc,16);			# 10
622238405Sjkim	&or	("ecx",$acc);			# 10
623238405Sjkim	&pshufw	("mm6","mm4",0x08);		# 13,12, 9, 8
624238405Sjkim	&movz	($acc,&HB("ebx"));		# 11
625238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 11
626238405Sjkim	&shl	($acc,24);			# 11
627238405Sjkim	&or	("edx",$acc);			# 11
628238405Sjkim	&shr	("ebx",16);			# 15,14
629238405Sjkim
630238405Sjkim	&movz	($acc,&HB("eax"));		#  5
631238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  5
632238405Sjkim	&shl	($acc,8);			#  5
633238405Sjkim	&or	("ecx",$acc);			#  5
634238405Sjkim	&movz	($acc,&HB("ebx"));		# 15
635238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 15
636238405Sjkim	&shl	($acc,24);			# 15
637238405Sjkim	&or	("ecx",$acc);			# 15
638238405Sjkim	&movd	("mm0","ecx");			# t[0] collected
639238405Sjkim
640238405Sjkim	&movz	($acc,&LB("eax"));		#  4
641238405Sjkim	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  4
642238405Sjkim	&movd	("eax","mm2");			#  7, 6, 3, 2
643238405Sjkim	&movz	($acc,&LB("ebx"));		# 14
644238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 14
645238405Sjkim	&shl	($acc,16);			# 14
646238405Sjkim	&or	("ecx",$acc);			# 14
647238405Sjkim
648238405Sjkim	&movd	("ebx","mm6");			# 13,12, 9, 8
649238405Sjkim	&movz	($acc,&HB("eax"));		#  3
650238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  3
651238405Sjkim	&shl	($acc,24);			#  3
652238405Sjkim	&or	("ecx",$acc);			#  3
653238405Sjkim	&movz	($acc,&HB("ebx"));		#  9
654238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  9
655238405Sjkim	&shl	($acc,8);			#  9
656238405Sjkim	&or	("ecx",$acc);			#  9
657238405Sjkim	&movd	("mm1","ecx");			# t[1] collected
658238405Sjkim
659238405Sjkim	&movz	($acc,&LB("ebx"));		#  8
660238405Sjkim	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  8
661238405Sjkim	&shr	("ebx",16);			# 13,12
662238405Sjkim	&movz	($acc,&LB("eax"));		#  2
663238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  2
664238405Sjkim	&shl	($acc,16);			#  2
665238405Sjkim	&or	("ecx",$acc);			#  2
666238405Sjkim	&shr	("eax",16);			#  7, 6
667238405Sjkim
668238405Sjkim	&punpckldq	("mm0","mm1");		# t[0,1] collected
669238405Sjkim
670238405Sjkim	&movz	($acc,&HB("eax"));		#  7
671238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  7
672238405Sjkim	&shl	($acc,24);			#  7
673238405Sjkim	&or	("ecx",$acc);			#  7
674238405Sjkim	&and	("eax",0xff);			#  6
675238405Sjkim	&movz	("eax",&BP(-128,$tbl,"eax",1));	#  6
676238405Sjkim	&shl	("eax",16);			#  6
677238405Sjkim	&or	("edx","eax");			#  6
678238405Sjkim	&movz	($acc,&HB("ebx"));		# 13
679238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 13
680238405Sjkim	&shl	($acc,8);			# 13
681238405Sjkim	&or	("ecx",$acc);			# 13
682238405Sjkim	&movd	("mm4","ecx");			# t[2] collected
683238405Sjkim	&and	("ebx",0xff);			# 12
684238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"ebx",1));	# 12
685238405Sjkim	&or	("edx","ebx");			# 12
686238405Sjkim	&movd	("mm5","edx");			# t[3] collected
687238405Sjkim
688238405Sjkim	&punpckldq	("mm4","mm5");		# t[2,3] collected
689238405Sjkim}
690238405Sjkim
691238405Sjkim					if (!$x86only) {
692238405Sjkim&function_begin_B("_sse_AES_encrypt_compact");
693238405Sjkim	&pxor	("mm0",&QWP(0,$key));	#  7, 6, 5, 4, 3, 2, 1, 0
694238405Sjkim	&pxor	("mm4",&QWP(8,$key));	# 15,14,13,12,11,10, 9, 8
695238405Sjkim
696238405Sjkim	# note that caller is expected to allocate stack frame for me!
697238405Sjkim	&mov	($acc,&DWP(240,$key));		# load key->rounds
698238405Sjkim	&lea	($acc,&DWP(-2,$acc,$acc));
699238405Sjkim	&lea	($acc,&DWP(0,$key,$acc,8));
700238405Sjkim	&mov	($__end,$acc);			# end of key schedule
701238405Sjkim
702238405Sjkim	&mov	($s0,0x1b1b1b1b);		# magic constant
703238405Sjkim	&mov	(&DWP(8,"esp"),$s0);
704238405Sjkim	&mov	(&DWP(12,"esp"),$s0);
705238405Sjkim
706238405Sjkim	# prefetch Te4
707238405Sjkim	&mov	($s0,&DWP(0-128,$tbl));
708238405Sjkim	&mov	($s1,&DWP(32-128,$tbl));
709238405Sjkim	&mov	($s2,&DWP(64-128,$tbl));
710238405Sjkim	&mov	($s3,&DWP(96-128,$tbl));
711238405Sjkim	&mov	($s0,&DWP(128-128,$tbl));
712238405Sjkim	&mov	($s1,&DWP(160-128,$tbl));
713238405Sjkim	&mov	($s2,&DWP(192-128,$tbl));
714238405Sjkim	&mov	($s3,&DWP(224-128,$tbl));
715238405Sjkim
716238405Sjkim	&set_label("loop",16);
717238405Sjkim		&sse_enccompact();
718238405Sjkim		&add	($key,16);
719238405Sjkim		&cmp	($key,$__end);
720238405Sjkim		&ja	(&label("out"));
721238405Sjkim
722238405Sjkim		&movq	("mm2",&QWP(8,"esp"));
723238405Sjkim		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
724238405Sjkim		&movq	("mm1","mm0");		&movq	("mm5","mm4");	# r0
725238405Sjkim		&pcmpgtb("mm3","mm0");		&pcmpgtb("mm7","mm4");
726238405Sjkim		&pand	("mm3","mm2");		&pand	("mm7","mm2");
727238405Sjkim		&pshufw	("mm2","mm0",0xb1);	&pshufw	("mm6","mm4",0xb1);# ROTATE(r0,16)
728238405Sjkim		&paddb	("mm0","mm0");		&paddb	("mm4","mm4");
729238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# = r2
730238405Sjkim		&pshufw	("mm3","mm2",0xb1);	&pshufw	("mm7","mm6",0xb1);# r0
731238405Sjkim		&pxor	("mm1","mm0");		&pxor	("mm5","mm4");	# r0^r2
732238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= ROTATE(r0,16)
733238405Sjkim
734238405Sjkim		&movq	("mm2","mm3");		&movq	("mm6","mm7");
735238405Sjkim		&pslld	("mm3",8);		&pslld	("mm7",8);
736238405Sjkim		&psrld	("mm2",24);		&psrld	("mm6",24);
737238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= r0<<8
738238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= r0>>24
739238405Sjkim
740238405Sjkim		&movq	("mm3","mm1");		&movq	("mm7","mm5");
741238405Sjkim		&movq	("mm2",&QWP(0,$key));	&movq	("mm6",&QWP(8,$key));
742238405Sjkim		&psrld	("mm1",8);		&psrld	("mm5",8);
743238405Sjkim		&mov	($s0,&DWP(0-128,$tbl));
744238405Sjkim		&pslld	("mm3",24);		&pslld	("mm7",24);
745238405Sjkim		&mov	($s1,&DWP(64-128,$tbl));
746238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= (r2^r0)<<8
747238405Sjkim		&mov	($s2,&DWP(128-128,$tbl));
748238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= (r2^r0)>>24
749238405Sjkim		&mov	($s3,&DWP(192-128,$tbl));
750238405Sjkim
751238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");
752238405Sjkim	&jmp	(&label("loop"));
753238405Sjkim
754238405Sjkim	&set_label("out",16);
755238405Sjkim	&pxor	("mm0",&QWP(0,$key));
756238405Sjkim	&pxor	("mm4",&QWP(8,$key));
757238405Sjkim
758238405Sjkim	&ret	();
759238405Sjkim&function_end_B("_sse_AES_encrypt_compact");
760238405Sjkim					}
761238405Sjkim
762238405Sjkim######################################################################
763238405Sjkim# Vanilla block function.
764238405Sjkim######################################################################
765238405Sjkim
766160814Ssimonsub encstep()
767160814Ssimon{ my ($i,$te,@s) = @_;
768160814Ssimon  my $tmp = $key;
769160814Ssimon  my $out = $i==3?$s[0]:$acc;
770160814Ssimon
771160814Ssimon	# lines marked with #%e?x[i] denote "reordered" instructions...
772238405Sjkim	if ($i==3)  {	&mov	($key,$__key);			}##%edx
773160814Ssimon	else        {	&mov	($out,$s[0]);
774160814Ssimon			&and	($out,0xFF);			}
775160814Ssimon	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
776160814Ssimon	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
777160814Ssimon			&mov	($out,&DWP(0,$te,$out,8));
778160814Ssimon
779160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}##%eax
780160814Ssimon			&movz	($tmp,&HB($s[1]));
781160814Ssimon			&xor	($out,&DWP(3,$te,$tmp,8));
782160814Ssimon
783238405Sjkim	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
784160814Ssimon	else        {	&mov	($tmp,$s[2]);
785160814Ssimon			&shr	($tmp,16);			}
786160814Ssimon	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
787160814Ssimon			&and	($tmp,0xFF);
788160814Ssimon			&xor	($out,&DWP(2,$te,$tmp,8));
789160814Ssimon
790238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
791160814Ssimon	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
792160814Ssimon	else        {	&mov	($tmp,$s[3]);
793160814Ssimon			&shr	($tmp,24)			}
794160814Ssimon			&xor	($out,&DWP(1,$te,$tmp,8));
795160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
796160814Ssimon	if ($i==3)  {	&mov	($s[3],$acc);			}
797160814Ssimon			&comment();
798160814Ssimon}
799160814Ssimon
800160814Ssimonsub enclast()
801160814Ssimon{ my ($i,$te,@s)=@_;
802160814Ssimon  my $tmp = $key;
803160814Ssimon  my $out = $i==3?$s[0]:$acc;
804160814Ssimon
805238405Sjkim	if ($i==3)  {	&mov	($key,$__key);			}##%edx
806160814Ssimon	else        {	&mov	($out,$s[0]);			}
807160814Ssimon			&and	($out,0xFF);
808160814Ssimon	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
809160814Ssimon	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
810160814Ssimon			&mov	($out,&DWP(2,$te,$out,8));
811160814Ssimon			&and	($out,0x000000ff);
812160814Ssimon
813160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}##%eax
814160814Ssimon			&movz	($tmp,&HB($s[1]));
815160814Ssimon			&mov	($tmp,&DWP(0,$te,$tmp,8));
816160814Ssimon			&and	($tmp,0x0000ff00);
817160814Ssimon			&xor	($out,$tmp);
818160814Ssimon
819238405Sjkim	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$__s0);		}##%ebx
820238405Sjkim	else        {	&mov	($tmp,$s[2]);
821160814Ssimon			&shr	($tmp,16);			}
822160814Ssimon	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
823160814Ssimon			&and	($tmp,0xFF);
824160814Ssimon			&mov	($tmp,&DWP(0,$te,$tmp,8));
825160814Ssimon			&and	($tmp,0x00ff0000);
826160814Ssimon			&xor	($out,$tmp);
827160814Ssimon
828238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}##%ecx
829160814Ssimon	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
830160814Ssimon	else        {	&mov	($tmp,$s[3]);
831160814Ssimon			&shr	($tmp,24);			}
832160814Ssimon			&mov	($tmp,&DWP(2,$te,$tmp,8));
833160814Ssimon			&and	($tmp,0xff000000);
834160814Ssimon			&xor	($out,$tmp);
835160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
836160814Ssimon	if ($i==3)  {	&mov	($s[3],$acc);			}
837160814Ssimon}
838160814Ssimon
839160814Ssimon&function_begin_B("_x86_AES_encrypt");
840160814Ssimon	if ($vertical_spin) {
841160814Ssimon		# I need high parts of volatile registers to be accessible...
842160814Ssimon		&exch	($s1="edi",$key="ebx");
843160814Ssimon		&mov	($s2="esi",$acc="ecx");
844160814Ssimon	}
845160814Ssimon
846160814Ssimon	# note that caller is expected to allocate stack frame for me!
847238405Sjkim	&mov	($__key,$key);			# save key
848160814Ssimon
849160814Ssimon	&xor	($s0,&DWP(0,$key));		# xor with key
850160814Ssimon	&xor	($s1,&DWP(4,$key));
851160814Ssimon	&xor	($s2,&DWP(8,$key));
852160814Ssimon	&xor	($s3,&DWP(12,$key));
853160814Ssimon
854160814Ssimon	&mov	($acc,&DWP(240,$key));		# load key->rounds
855160814Ssimon
856160814Ssimon	if ($small_footprint) {
857160814Ssimon	    &lea	($acc,&DWP(-2,$acc,$acc));
858160814Ssimon	    &lea	($acc,&DWP(0,$key,$acc,8));
859238405Sjkim	    &mov	($__end,$acc);		# end of key schedule
860238405Sjkim
861238405Sjkim	    &set_label("loop",16);
862160814Ssimon		if ($vertical_spin) {
863238405Sjkim		    &encvert($tbl,$s0,$s1,$s2,$s3);
864160814Ssimon		} else {
865238405Sjkim		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
866238405Sjkim		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
867238405Sjkim		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
868238405Sjkim		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
869160814Ssimon		}
870160814Ssimon		&add	($key,16);		# advance rd_key
871160814Ssimon		&xor	($s0,&DWP(0,$key));
872160814Ssimon		&xor	($s1,&DWP(4,$key));
873160814Ssimon		&xor	($s2,&DWP(8,$key));
874160814Ssimon		&xor	($s3,&DWP(12,$key));
875238405Sjkim	    &cmp	($key,$__end);
876238405Sjkim	    &mov	($__key,$key);
877160814Ssimon	    &jb		(&label("loop"));
878160814Ssimon	}
879160814Ssimon	else {
880160814Ssimon	    &cmp	($acc,10);
881160814Ssimon	    &jle	(&label("10rounds"));
882160814Ssimon	    &cmp	($acc,12);
883160814Ssimon	    &jle	(&label("12rounds"));
884160814Ssimon
885238405Sjkim	&set_label("14rounds",4);
886160814Ssimon	    for ($i=1;$i<3;$i++) {
887160814Ssimon		if ($vertical_spin) {
888238405Sjkim		    &encvert($tbl,$s0,$s1,$s2,$s3);
889160814Ssimon		} else {
890238405Sjkim		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
891238405Sjkim		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
892238405Sjkim		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
893238405Sjkim		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
894160814Ssimon		}
895160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
896160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
897160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
898160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
899160814Ssimon	    }
900160814Ssimon	    &add	($key,32);
901238405Sjkim	    &mov	($__key,$key);		# advance rd_key
902238405Sjkim	&set_label("12rounds",4);
903160814Ssimon	    for ($i=1;$i<3;$i++) {
904160814Ssimon		if ($vertical_spin) {
905238405Sjkim		    &encvert($tbl,$s0,$s1,$s2,$s3);
906160814Ssimon		} else {
907238405Sjkim		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
908238405Sjkim		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
909238405Sjkim		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
910238405Sjkim		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
911160814Ssimon		}
912160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
913160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
914160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
915160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
916160814Ssimon	    }
917160814Ssimon	    &add	($key,32);
918238405Sjkim	    &mov	($__key,$key);		# advance rd_key
919238405Sjkim	&set_label("10rounds",4);
920160814Ssimon	    for ($i=1;$i<10;$i++) {
921160814Ssimon		if ($vertical_spin) {
922238405Sjkim		    &encvert($tbl,$s0,$s1,$s2,$s3);
923160814Ssimon		} else {
924238405Sjkim		    &encstep(0,$tbl,$s0,$s1,$s2,$s3);
925238405Sjkim		    &encstep(1,$tbl,$s1,$s2,$s3,$s0);
926238405Sjkim		    &encstep(2,$tbl,$s2,$s3,$s0,$s1);
927238405Sjkim		    &encstep(3,$tbl,$s3,$s0,$s1,$s2);
928160814Ssimon		}
929160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
930160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
931160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
932160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
933160814Ssimon	    }
934160814Ssimon	}
935160814Ssimon
936160814Ssimon	if ($vertical_spin) {
937160814Ssimon	    # "reincarnate" some registers for "horizontal" spin...
938160814Ssimon	    &mov	($s1="ebx",$key="edi");
939160814Ssimon	    &mov	($s2="ecx",$acc="esi");
940160814Ssimon	}
941238405Sjkim	&enclast(0,$tbl,$s0,$s1,$s2,$s3);
942238405Sjkim	&enclast(1,$tbl,$s1,$s2,$s3,$s0);
943238405Sjkim	&enclast(2,$tbl,$s2,$s3,$s0,$s1);
944238405Sjkim	&enclast(3,$tbl,$s3,$s0,$s1,$s2);
945160814Ssimon
946160814Ssimon	&add	($key,$small_footprint?16:160);
947160814Ssimon	&xor	($s0,&DWP(0,$key));
948160814Ssimon	&xor	($s1,&DWP(4,$key));
949160814Ssimon	&xor	($s2,&DWP(8,$key));
950160814Ssimon	&xor	($s3,&DWP(12,$key));
951160814Ssimon
952160814Ssimon	&ret	();
953160814Ssimon
954160814Ssimon&set_label("AES_Te",64);	# Yes! I keep it in the code segment!
955160814Ssimon	&_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
956160814Ssimon	&_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
957160814Ssimon	&_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
958160814Ssimon	&_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
959160814Ssimon	&_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
960160814Ssimon	&_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
961160814Ssimon	&_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
962160814Ssimon	&_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
963160814Ssimon	&_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
964160814Ssimon	&_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
965160814Ssimon	&_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
966160814Ssimon	&_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
967160814Ssimon	&_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
968160814Ssimon	&_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
969160814Ssimon	&_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
970160814Ssimon	&_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
971160814Ssimon	&_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
972160814Ssimon	&_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
973160814Ssimon	&_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
974160814Ssimon	&_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
975160814Ssimon	&_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
976160814Ssimon	&_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
977160814Ssimon	&_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
978160814Ssimon	&_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
979160814Ssimon	&_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
980160814Ssimon	&_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
981160814Ssimon	&_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
982160814Ssimon	&_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
983160814Ssimon	&_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
984160814Ssimon	&_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
985160814Ssimon	&_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
986160814Ssimon	&_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
987160814Ssimon	&_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
988160814Ssimon	&_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
989160814Ssimon	&_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
990160814Ssimon	&_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
991160814Ssimon	&_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
992160814Ssimon	&_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
993160814Ssimon	&_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
994160814Ssimon	&_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
995160814Ssimon	&_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
996160814Ssimon	&_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
997160814Ssimon	&_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
998160814Ssimon	&_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
999160814Ssimon	&_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
1000160814Ssimon	&_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
1001160814Ssimon	&_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
1002160814Ssimon	&_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
1003160814Ssimon	&_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
1004160814Ssimon	&_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
1005160814Ssimon	&_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
1006160814Ssimon	&_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
1007160814Ssimon	&_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
1008160814Ssimon	&_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
1009160814Ssimon	&_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
1010160814Ssimon	&_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
1011160814Ssimon	&_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
1012160814Ssimon	&_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
1013160814Ssimon	&_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
1014160814Ssimon	&_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
1015160814Ssimon	&_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
1016160814Ssimon	&_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
1017160814Ssimon	&_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
1018160814Ssimon	&_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
1019238405Sjkim
1020238405Sjkim#Te4	# four copies of Te4 to choose from to avoid L1 aliasing
1021238405Sjkim	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1022238405Sjkim	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1023238405Sjkim	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1024238405Sjkim	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1025238405Sjkim	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1026238405Sjkim	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1027238405Sjkim	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1028238405Sjkim	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1029238405Sjkim	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1030238405Sjkim	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1031238405Sjkim	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1032238405Sjkim	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1033238405Sjkim	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1034238405Sjkim	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1035238405Sjkim	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1036238405Sjkim	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1037238405Sjkim	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1038238405Sjkim	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1039238405Sjkim	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1040238405Sjkim	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1041238405Sjkim	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1042238405Sjkim	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1043238405Sjkim	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1044238405Sjkim	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1045238405Sjkim	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1046238405Sjkim	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1047238405Sjkim	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1048238405Sjkim	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1049238405Sjkim	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1050238405Sjkim	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1051238405Sjkim	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1052238405Sjkim	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1053238405Sjkim
1054238405Sjkim	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1055238405Sjkim	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1056238405Sjkim	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1057238405Sjkim	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1058238405Sjkim	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1059238405Sjkim	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1060238405Sjkim	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1061238405Sjkim	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1062238405Sjkim	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1063238405Sjkim	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1064238405Sjkim	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1065238405Sjkim	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1066238405Sjkim	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1067238405Sjkim	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1068238405Sjkim	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1069238405Sjkim	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1070238405Sjkim	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1071238405Sjkim	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1072238405Sjkim	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1073238405Sjkim	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1074238405Sjkim	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1075238405Sjkim	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1076238405Sjkim	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1077238405Sjkim	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1078238405Sjkim	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1079238405Sjkim	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1080238405Sjkim	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1081238405Sjkim	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1082238405Sjkim	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1083238405Sjkim	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1084238405Sjkim	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1085238405Sjkim	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1086238405Sjkim
1087238405Sjkim	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1088238405Sjkim	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1089238405Sjkim	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1090238405Sjkim	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1091238405Sjkim	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1092238405Sjkim	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1093238405Sjkim	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1094238405Sjkim	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1095238405Sjkim	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1096238405Sjkim	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1097238405Sjkim	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1098238405Sjkim	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1099238405Sjkim	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1100238405Sjkim	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1101238405Sjkim	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1102238405Sjkim	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1103238405Sjkim	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1104238405Sjkim	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1105238405Sjkim	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1106238405Sjkim	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1107238405Sjkim	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1108238405Sjkim	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1109238405Sjkim	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1110238405Sjkim	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1111238405Sjkim	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1112238405Sjkim	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1113238405Sjkim	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1114238405Sjkim	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1115238405Sjkim	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1116238405Sjkim	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1117238405Sjkim	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1118238405Sjkim	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1119238405Sjkim
1120238405Sjkim	&data_byte(0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5);
1121238405Sjkim	&data_byte(0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76);
1122238405Sjkim	&data_byte(0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0);
1123238405Sjkim	&data_byte(0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0);
1124238405Sjkim	&data_byte(0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc);
1125238405Sjkim	&data_byte(0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15);
1126238405Sjkim	&data_byte(0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a);
1127238405Sjkim	&data_byte(0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75);
1128238405Sjkim	&data_byte(0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0);
1129238405Sjkim	&data_byte(0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84);
1130238405Sjkim	&data_byte(0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b);
1131238405Sjkim	&data_byte(0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf);
1132238405Sjkim	&data_byte(0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85);
1133238405Sjkim	&data_byte(0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8);
1134238405Sjkim	&data_byte(0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5);
1135238405Sjkim	&data_byte(0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2);
1136238405Sjkim	&data_byte(0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17);
1137238405Sjkim	&data_byte(0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73);
1138238405Sjkim	&data_byte(0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88);
1139238405Sjkim	&data_byte(0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb);
1140238405Sjkim	&data_byte(0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c);
1141238405Sjkim	&data_byte(0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79);
1142238405Sjkim	&data_byte(0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9);
1143238405Sjkim	&data_byte(0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08);
1144238405Sjkim	&data_byte(0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6);
1145238405Sjkim	&data_byte(0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a);
1146238405Sjkim	&data_byte(0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e);
1147238405Sjkim	&data_byte(0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e);
1148238405Sjkim	&data_byte(0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94);
1149238405Sjkim	&data_byte(0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf);
1150238405Sjkim	&data_byte(0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68);
1151238405Sjkim	&data_byte(0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16);
1152160814Ssimon#rcon:
1153160814Ssimon	&data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
1154160814Ssimon	&data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
1155238405Sjkim	&data_word(0x0000001b, 0x00000036, 0x00000000, 0x00000000);
1156238405Sjkim	&data_word(0x00000000, 0x00000000, 0x00000000, 0x00000000);
1157160814Ssimon&function_end_B("_x86_AES_encrypt");
1158160814Ssimon
1159160814Ssimon# void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
1160160814Ssimon&function_begin("AES_encrypt");
1161160814Ssimon	&mov	($acc,&wparam(0));		# load inp
1162160814Ssimon	&mov	($key,&wparam(2));		# load key
1163160814Ssimon
1164160814Ssimon	&mov	($s0,"esp");
1165238405Sjkim	&sub	("esp",36);
1166238405Sjkim	&and	("esp",-64);			# align to cache-line
1167160814Ssimon
1168238405Sjkim	# place stack frame just "above" the key schedule
1169238405Sjkim	&lea	($s1,&DWP(-64-63,$key));
1170238405Sjkim	&sub	($s1,"esp");
1171238405Sjkim	&neg	($s1);
1172238405Sjkim	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
1173238405Sjkim	&sub	("esp",$s1);
1174238405Sjkim	&add	("esp",4);	# 4 is reserved for caller's return address
1175238405Sjkim	&mov	($_esp,$s0);			# save stack pointer
1176238405Sjkim
1177160814Ssimon	&call   (&label("pic_point"));          # make it PIC!
1178160814Ssimon	&set_label("pic_point");
1179238405Sjkim	&blindpop($tbl);
1180238405Sjkim	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if (!$x86only);
1181238405Sjkim	&lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
1182160814Ssimon
1183238405Sjkim	# pick Te4 copy which can't "overlap" with stack frame or key schedule
1184238405Sjkim	&lea	($s1,&DWP(768-4,"esp"));
1185238405Sjkim	&sub	($s1,$tbl);
1186238405Sjkim	&and	($s1,0x300);
1187238405Sjkim	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
1188238405Sjkim
1189238405Sjkim					if (!$x86only) {
1190238405Sjkim	&bt	(&DWP(0,$s0),25);	# check for SSE bit
1191238405Sjkim	&jnc	(&label("x86"));
1192238405Sjkim
1193238405Sjkim	&movq	("mm0",&QWP(0,$acc));
1194238405Sjkim	&movq	("mm4",&QWP(8,$acc));
1195238405Sjkim	&call	("_sse_AES_encrypt_compact");
1196238405Sjkim	&mov	("esp",$_esp);			# restore stack pointer
1197238405Sjkim	&mov	($acc,&wparam(1));		# load out
1198238405Sjkim	&movq	(&QWP(0,$acc),"mm0");		# write output data
1199238405Sjkim	&movq	(&QWP(8,$acc),"mm4");
1200238405Sjkim	&emms	();
1201238405Sjkim	&function_end_A();
1202238405Sjkim					}
1203238405Sjkim	&set_label("x86",16);
1204238405Sjkim	&mov	($_tbl,$tbl);
1205160814Ssimon	&mov	($s0,&DWP(0,$acc));		# load input data
1206160814Ssimon	&mov	($s1,&DWP(4,$acc));
1207160814Ssimon	&mov	($s2,&DWP(8,$acc));
1208160814Ssimon	&mov	($s3,&DWP(12,$acc));
1209238405Sjkim	&call	("_x86_AES_encrypt_compact");
1210238405Sjkim	&mov	("esp",$_esp);			# restore stack pointer
1211160814Ssimon	&mov	($acc,&wparam(1));		# load out
1212160814Ssimon	&mov	(&DWP(0,$acc),$s0);		# write output data
1213160814Ssimon	&mov	(&DWP(4,$acc),$s1);
1214160814Ssimon	&mov	(&DWP(8,$acc),$s2);
1215160814Ssimon	&mov	(&DWP(12,$acc),$s3);
1216160814Ssimon&function_end("AES_encrypt");
1217160814Ssimon
1218238405Sjkim#--------------------------------------------------------------------#
1219160814Ssimon
1220238405Sjkim######################################################################
1221238405Sjkim# "Compact" block function
1222238405Sjkim######################################################################
1223238405Sjkim
1224238405Sjkimsub deccompact()
1225238405Sjkim{ my $Fn = mov;
1226238405Sjkim  while ($#_>5) { pop(@_); $Fn=sub{}; }
1227238405Sjkim  my ($i,$td,@s)=@_;
1228238405Sjkim  my $tmp = $key;
1229238405Sjkim  my $out = $i==3?$s[0]:$acc;
1230238405Sjkim
1231238405Sjkim	# $Fn is used in first compact round and its purpose is to
1232238405Sjkim	# void restoration of some values from stack, so that after
1233238405Sjkim	# 4xdeccompact with extra argument $key, $s0 and $s1 values
1234238405Sjkim	# are left there...
1235238405Sjkim	if($i==3)   {	&$Fn	($key,$__key);			}
1236238405Sjkim	else        {	&mov	($out,$s[0]);			}
1237238405Sjkim			&and	($out,0xFF);
1238238405Sjkim			&movz	($out,&BP(-128,$td,$out,1));
1239238405Sjkim
1240238405Sjkim	if ($i==3)  {	$tmp=$s[1];				}
1241238405Sjkim			&movz	($tmp,&HB($s[1]));
1242238405Sjkim			&movz	($tmp,&BP(-128,$td,$tmp,1));
1243238405Sjkim			&shl	($tmp,8);
1244238405Sjkim			&xor	($out,$tmp);
1245238405Sjkim
1246238405Sjkim	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1247238405Sjkim	else        {	mov	($tmp,$s[2]);			}
1248238405Sjkim			&shr	($tmp,16);
1249238405Sjkim			&and	($tmp,0xFF);
1250238405Sjkim			&movz	($tmp,&BP(-128,$td,$tmp,1));
1251238405Sjkim			&shl	($tmp,16);
1252238405Sjkim			&xor	($out,$tmp);
1253238405Sjkim
1254238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &$Fn ($s[2],$__s1);		}
1255238405Sjkim	else        {	&mov	($tmp,$s[3]);			}
1256238405Sjkim			&shr	($tmp,24);
1257238405Sjkim			&movz	($tmp,&BP(-128,$td,$tmp,1));
1258238405Sjkim			&shl	($tmp,24);
1259238405Sjkim			&xor	($out,$tmp);
1260238405Sjkim	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1261238405Sjkim	if ($i==3)  {	&$Fn	($s[3],$__s0);			}
1262238405Sjkim}
1263238405Sjkim
1264238405Sjkim# must be called with 2,3,0,1 as argument sequence!!!
1265238405Sjkimsub dectransform()
1266238405Sjkim{ my @s = ($s0,$s1,$s2,$s3);
1267238405Sjkim  my $i = shift;
1268238405Sjkim  my $tmp = $key;
1269238405Sjkim  my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1);
1270238405Sjkim  my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1);
1271238405Sjkim  my $tp8 = $tbl;
1272238405Sjkim
1273238405Sjkim	&mov	($acc,$s[$i]);
1274238405Sjkim	&and	($acc,0x80808080);
1275238405Sjkim	&mov	($tmp,$acc);
1276238405Sjkim	&shr	($tmp,7);
1277238405Sjkim	&lea	($tp2,&DWP(0,$s[$i],$s[$i]));
1278238405Sjkim	&sub	($acc,$tmp);
1279238405Sjkim	&and	($tp2,0xfefefefe);
1280238405Sjkim	&and	($acc,0x1b1b1b1b);
1281238405Sjkim	&xor	($acc,$tp2);
1282238405Sjkim	&mov	($tp2,$acc);
1283238405Sjkim
1284238405Sjkim	&and	($acc,0x80808080);
1285238405Sjkim	&mov	($tmp,$acc);
1286238405Sjkim	&shr	($tmp,7);
1287238405Sjkim	&lea	($tp4,&DWP(0,$tp2,$tp2));
1288238405Sjkim	&sub	($acc,$tmp);
1289238405Sjkim	&and	($tp4,0xfefefefe);
1290238405Sjkim	&and	($acc,0x1b1b1b1b);
1291238405Sjkim	 &xor	($tp2,$s[$i]);	# tp2^tp1
1292238405Sjkim	&xor	($acc,$tp4);
1293238405Sjkim	&mov	($tp4,$acc);
1294238405Sjkim
1295238405Sjkim	&and	($acc,0x80808080);
1296238405Sjkim	&mov	($tmp,$acc);
1297238405Sjkim	&shr	($tmp,7);
1298238405Sjkim	&lea	($tp8,&DWP(0,$tp4,$tp4));
1299238405Sjkim	&sub	($acc,$tmp);
1300238405Sjkim	&and	($tp8,0xfefefefe);
1301238405Sjkim	&and	($acc,0x1b1b1b1b);
1302238405Sjkim	 &xor	($tp4,$s[$i]);	# tp4^tp1
1303238405Sjkim	 &rotl	($s[$i],8);	# = ROTATE(tp1,8)
1304238405Sjkim	&xor	($tp8,$acc);
1305238405Sjkim
1306238405Sjkim	&xor	($s[$i],$tp2);
1307238405Sjkim	&xor	($tp2,$tp8);
1308238405Sjkim	&rotl	($tp2,24);
1309238405Sjkim	&xor	($s[$i],$tp4);
1310238405Sjkim	&xor	($tp4,$tp8);
1311238405Sjkim	&rotl	($tp4,16);
1312238405Sjkim	&xor	($s[$i],$tp8);	# ^= tp8^(tp4^tp1)^(tp2^tp1)
1313238405Sjkim	&rotl	($tp8,8);
1314238405Sjkim	&xor	($s[$i],$tp2);	# ^= ROTATE(tp8^tp2^tp1,24)
1315238405Sjkim	&xor	($s[$i],$tp4);	# ^= ROTATE(tp8^tp4^tp1,16)
1316238405Sjkim	 &mov	($s[0],$__s0)			if($i==2); #prefetch $s0
1317238405Sjkim	 &mov	($s[1],$__s1)			if($i==3); #prefetch $s1
1318238405Sjkim	 &mov	($s[2],$__s2)			if($i==1);
1319238405Sjkim	&xor	($s[$i],$tp8);	# ^= ROTATE(tp8,8)
1320238405Sjkim
1321238405Sjkim	&mov	($s[3],$__s3)			if($i==1);
1322238405Sjkim	&mov	(&DWP(4+4*$i,"esp"),$s[$i])	if($i>=2);
1323238405Sjkim}
1324238405Sjkim
1325238405Sjkim&function_begin_B("_x86_AES_decrypt_compact");
1326238405Sjkim	# note that caller is expected to allocate stack frame for me!
1327238405Sjkim	&mov	($__key,$key);			# save key
1328238405Sjkim
1329238405Sjkim	&xor	($s0,&DWP(0,$key));		# xor with key
1330238405Sjkim	&xor	($s1,&DWP(4,$key));
1331238405Sjkim	&xor	($s2,&DWP(8,$key));
1332238405Sjkim	&xor	($s3,&DWP(12,$key));
1333238405Sjkim
1334238405Sjkim	&mov	($acc,&DWP(240,$key));		# load key->rounds
1335238405Sjkim
1336238405Sjkim	&lea	($acc,&DWP(-2,$acc,$acc));
1337238405Sjkim	&lea	($acc,&DWP(0,$key,$acc,8));
1338238405Sjkim	&mov	($__end,$acc);			# end of key schedule
1339238405Sjkim
1340238405Sjkim	# prefetch Td4
1341238405Sjkim	&mov	($key,&DWP(0-128,$tbl));
1342238405Sjkim	&mov	($acc,&DWP(32-128,$tbl));
1343238405Sjkim	&mov	($key,&DWP(64-128,$tbl));
1344238405Sjkim	&mov	($acc,&DWP(96-128,$tbl));
1345238405Sjkim	&mov	($key,&DWP(128-128,$tbl));
1346238405Sjkim	&mov	($acc,&DWP(160-128,$tbl));
1347238405Sjkim	&mov	($key,&DWP(192-128,$tbl));
1348238405Sjkim	&mov	($acc,&DWP(224-128,$tbl));
1349238405Sjkim
1350238405Sjkim	&set_label("loop",16);
1351238405Sjkim
1352238405Sjkim		&deccompact(0,$tbl,$s0,$s3,$s2,$s1,1);
1353238405Sjkim		&deccompact(1,$tbl,$s1,$s0,$s3,$s2,1);
1354238405Sjkim		&deccompact(2,$tbl,$s2,$s1,$s0,$s3,1);
1355238405Sjkim		&deccompact(3,$tbl,$s3,$s2,$s1,$s0,1);
1356238405Sjkim		&dectransform(2);
1357238405Sjkim		&dectransform(3);
1358238405Sjkim		&dectransform(0);
1359238405Sjkim		&dectransform(1);
1360238405Sjkim		&mov 	($key,$__key);
1361238405Sjkim		&mov	($tbl,$__tbl);
1362238405Sjkim		&add	($key,16);		# advance rd_key
1363238405Sjkim		&xor	($s0,&DWP(0,$key));
1364238405Sjkim		&xor	($s1,&DWP(4,$key));
1365238405Sjkim		&xor	($s2,&DWP(8,$key));
1366238405Sjkim		&xor	($s3,&DWP(12,$key));
1367238405Sjkim
1368238405Sjkim	&cmp	($key,$__end);
1369238405Sjkim	&mov	($__key,$key);
1370238405Sjkim	&jb	(&label("loop"));
1371238405Sjkim
1372238405Sjkim	&deccompact(0,$tbl,$s0,$s3,$s2,$s1);
1373238405Sjkim	&deccompact(1,$tbl,$s1,$s0,$s3,$s2);
1374238405Sjkim	&deccompact(2,$tbl,$s2,$s1,$s0,$s3);
1375238405Sjkim	&deccompact(3,$tbl,$s3,$s2,$s1,$s0);
1376238405Sjkim
1377238405Sjkim	&xor	($s0,&DWP(16,$key));
1378238405Sjkim	&xor	($s1,&DWP(20,$key));
1379238405Sjkim	&xor	($s2,&DWP(24,$key));
1380238405Sjkim	&xor	($s3,&DWP(28,$key));
1381238405Sjkim
1382238405Sjkim	&ret	();
1383238405Sjkim&function_end_B("_x86_AES_decrypt_compact");
1384238405Sjkim
1385238405Sjkim######################################################################
1386238405Sjkim# "Compact" SSE block function.
1387238405Sjkim######################################################################
1388238405Sjkim
1389238405Sjkimsub sse_deccompact()
1390238405Sjkim{
1391238405Sjkim	&pshufw	("mm1","mm0",0x0c);		#  7, 6, 1, 0
1392238405Sjkim	&movd	("eax","mm1");			#  7, 6, 1, 0
1393238405Sjkim
1394238405Sjkim	&pshufw	("mm5","mm4",0x09);		# 13,12,11,10
1395238405Sjkim	&movz	($acc,&LB("eax"));		#  0
1396238405Sjkim	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  0
1397238405Sjkim	&movd	("ebx","mm5");			# 13,12,11,10
1398238405Sjkim	&movz	("edx",&HB("eax"));		#  1
1399238405Sjkim	&movz	("edx",&BP(-128,$tbl,"edx",1));	#  1
1400238405Sjkim	&shl	("edx",8);			#  1
1401238405Sjkim
1402238405Sjkim	&pshufw	("mm2","mm0",0x06);		#  3, 2, 5, 4
1403238405Sjkim	&movz	($acc,&LB("ebx"));		# 10
1404238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 10
1405238405Sjkim	&shl	($acc,16);			# 10
1406238405Sjkim	&or	("ecx",$acc);			# 10
1407238405Sjkim	&shr	("eax",16);			#  7, 6
1408238405Sjkim	&movz	($acc,&HB("ebx"));		# 11
1409238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 11
1410238405Sjkim	&shl	($acc,24);			# 11
1411238405Sjkim	&or	("edx",$acc);			# 11
1412238405Sjkim	&shr	("ebx",16);			# 13,12
1413238405Sjkim
1414238405Sjkim	&pshufw	("mm6","mm4",0x03);		# 9, 8,15,14
1415238405Sjkim	&movz	($acc,&HB("eax"));		#  7
1416238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  7
1417238405Sjkim	&shl	($acc,24);			#  7
1418238405Sjkim	&or	("ecx",$acc);			#  7
1419238405Sjkim	&movz	($acc,&HB("ebx"));		# 13
1420238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 13
1421238405Sjkim	&shl	($acc,8);			# 13
1422238405Sjkim	&or	("ecx",$acc);			# 13
1423238405Sjkim	&movd	("mm0","ecx");			# t[0] collected
1424238405Sjkim
1425238405Sjkim	&movz	($acc,&LB("eax"));		#  6
1426238405Sjkim	&movd	("eax","mm2");			#  3, 2, 5, 4
1427238405Sjkim	&movz	("ecx",&BP(-128,$tbl,$acc,1));	#  6
1428238405Sjkim	&shl	("ecx",16);			#  6
1429238405Sjkim	&movz	($acc,&LB("ebx"));		# 12
1430238405Sjkim	&movd	("ebx","mm6");			#  9, 8,15,14
1431238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 12
1432238405Sjkim	&or	("ecx",$acc);			# 12
1433238405Sjkim
1434238405Sjkim	&movz	($acc,&LB("eax"));		#  4
1435238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  4
1436238405Sjkim	&or	("edx",$acc);			#  4
1437238405Sjkim	&movz	($acc,&LB("ebx"));		# 14
1438238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 14
1439238405Sjkim	&shl	($acc,16);			# 14
1440238405Sjkim	&or	("edx",$acc);			# 14
1441238405Sjkim	&movd	("mm1","edx");			# t[1] collected
1442238405Sjkim
1443238405Sjkim	&movz	($acc,&HB("eax"));		#  5
1444238405Sjkim	&movz	("edx",&BP(-128,$tbl,$acc,1));	#  5
1445238405Sjkim	&shl	("edx",8);			#  5
1446238405Sjkim	&movz	($acc,&HB("ebx"));		# 15
1447238405Sjkim	&shr	("eax",16);			#  3, 2
1448238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	# 15
1449238405Sjkim	&shl	($acc,24);			# 15
1450238405Sjkim	&or	("edx",$acc);			# 15
1451238405Sjkim	&shr	("ebx",16);			#  9, 8
1452238405Sjkim
1453238405Sjkim	&punpckldq	("mm0","mm1");		# t[0,1] collected
1454238405Sjkim
1455238405Sjkim	&movz	($acc,&HB("ebx"));		#  9
1456238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  9
1457238405Sjkim	&shl	($acc,8);			#  9
1458238405Sjkim	&or	("ecx",$acc);			#  9
1459238405Sjkim	&and	("ebx",0xff);			#  8
1460238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"ebx",1));	#  8
1461238405Sjkim	&or	("edx","ebx");			#  8
1462238405Sjkim	&movz	($acc,&LB("eax"));		#  2
1463238405Sjkim	&movz	($acc,&BP(-128,$tbl,$acc,1));	#  2
1464238405Sjkim	&shl	($acc,16);			#  2
1465238405Sjkim	&or	("edx",$acc);			#  2
1466238405Sjkim	&movd	("mm4","edx");			# t[2] collected
1467238405Sjkim	&movz	("eax",&HB("eax"));		#  3
1468238405Sjkim	&movz	("eax",&BP(-128,$tbl,"eax",1));	#  3
1469238405Sjkim	&shl	("eax",24);			#  3
1470238405Sjkim	&or	("ecx","eax");			#  3
1471238405Sjkim	&movd	("mm5","ecx");			# t[3] collected
1472238405Sjkim
1473238405Sjkim	&punpckldq	("mm4","mm5");		# t[2,3] collected
1474238405Sjkim}
1475238405Sjkim
1476238405Sjkim					if (!$x86only) {
1477238405Sjkim&function_begin_B("_sse_AES_decrypt_compact");
1478238405Sjkim	&pxor	("mm0",&QWP(0,$key));	#  7, 6, 5, 4, 3, 2, 1, 0
1479238405Sjkim	&pxor	("mm4",&QWP(8,$key));	# 15,14,13,12,11,10, 9, 8
1480238405Sjkim
1481238405Sjkim	# note that caller is expected to allocate stack frame for me!
1482238405Sjkim	&mov	($acc,&DWP(240,$key));		# load key->rounds
1483238405Sjkim	&lea	($acc,&DWP(-2,$acc,$acc));
1484238405Sjkim	&lea	($acc,&DWP(0,$key,$acc,8));
1485238405Sjkim	&mov	($__end,$acc);			# end of key schedule
1486238405Sjkim
1487238405Sjkim	&mov	($s0,0x1b1b1b1b);		# magic constant
1488238405Sjkim	&mov	(&DWP(8,"esp"),$s0);
1489238405Sjkim	&mov	(&DWP(12,"esp"),$s0);
1490238405Sjkim
1491238405Sjkim	# prefetch Td4
1492238405Sjkim	&mov	($s0,&DWP(0-128,$tbl));
1493238405Sjkim	&mov	($s1,&DWP(32-128,$tbl));
1494238405Sjkim	&mov	($s2,&DWP(64-128,$tbl));
1495238405Sjkim	&mov	($s3,&DWP(96-128,$tbl));
1496238405Sjkim	&mov	($s0,&DWP(128-128,$tbl));
1497238405Sjkim	&mov	($s1,&DWP(160-128,$tbl));
1498238405Sjkim	&mov	($s2,&DWP(192-128,$tbl));
1499238405Sjkim	&mov	($s3,&DWP(224-128,$tbl));
1500238405Sjkim
1501238405Sjkim	&set_label("loop",16);
1502238405Sjkim		&sse_deccompact();
1503238405Sjkim		&add	($key,16);
1504238405Sjkim		&cmp	($key,$__end);
1505238405Sjkim		&ja	(&label("out"));
1506238405Sjkim
1507238405Sjkim		# ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N)
1508238405Sjkim		&movq	("mm3","mm0");		&movq	("mm7","mm4");
1509238405Sjkim		&movq	("mm2","mm0",1);	&movq	("mm6","mm4",1);
1510238405Sjkim		&movq	("mm1","mm0");		&movq	("mm5","mm4");
1511238405Sjkim		&pshufw	("mm0","mm0",0xb1);	&pshufw	("mm4","mm4",0xb1);# = ROTATE(tp0,16)
1512238405Sjkim		&pslld	("mm2",8);		&pslld	("mm6",8);
1513238405Sjkim		&psrld	("mm3",8);		&psrld	("mm7",8);
1514238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp0<<8
1515238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp0>>8
1516238405Sjkim		&pslld	("mm2",16);		&pslld	("mm6",16);
1517238405Sjkim		&psrld	("mm3",16);		&psrld	("mm7",16);
1518238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp0<<24
1519238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp0>>24
1520238405Sjkim
1521238405Sjkim		&movq	("mm3",&QWP(8,"esp"));
1522238405Sjkim		&pxor	("mm2","mm2");		&pxor	("mm6","mm6");
1523238405Sjkim		&pcmpgtb("mm2","mm1");		&pcmpgtb("mm6","mm5");
1524238405Sjkim		&pand	("mm2","mm3");		&pand	("mm6","mm3");
1525238405Sjkim		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1526238405Sjkim		&pxor	("mm1","mm2");		&pxor	("mm5","mm6");	# tp2
1527238405Sjkim		&movq	("mm3","mm1");		&movq	("mm7","mm5");
1528238405Sjkim		&movq	("mm2","mm1");		&movq	("mm6","mm5");
1529238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp2
1530238405Sjkim		&pslld	("mm3",24);		&pslld	("mm7",24);
1531238405Sjkim		&psrld	("mm2",8);		&psrld	("mm6",8);
1532238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp2<<24
1533238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= tp2>>8
1534238405Sjkim
1535238405Sjkim		&movq	("mm2",&QWP(8,"esp"));
1536238405Sjkim		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
1537238405Sjkim		&pcmpgtb("mm3","mm1");		&pcmpgtb("mm7","mm5");
1538238405Sjkim		&pand	("mm3","mm2");		&pand	("mm7","mm2");
1539238405Sjkim		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1540238405Sjkim		&pxor	("mm1","mm3");		&pxor	("mm5","mm7");	# tp4
1541238405Sjkim		&pshufw	("mm3","mm1",0xb1);	&pshufw	("mm7","mm5",0xb1);
1542238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp4
1543238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= ROTATE(tp4,16)
1544238405Sjkim
1545238405Sjkim		&pxor	("mm3","mm3");		&pxor	("mm7","mm7");
1546238405Sjkim		&pcmpgtb("mm3","mm1");		&pcmpgtb("mm7","mm5");
1547238405Sjkim		&pand	("mm3","mm2");		&pand	("mm7","mm2");
1548238405Sjkim		&paddb	("mm1","mm1");		&paddb	("mm5","mm5");
1549238405Sjkim		&pxor	("mm1","mm3");		&pxor	("mm5","mm7");	# tp8
1550238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8
1551238405Sjkim		&movq	("mm3","mm1");		&movq	("mm7","mm5");
1552238405Sjkim		&pshufw	("mm2","mm1",0xb1);	&pshufw	("mm6","mm5",0xb1);
1553238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");	# ^= ROTATE(tp8,16)
1554238405Sjkim		&pslld	("mm1",8);		&pslld	("mm5",8);
1555238405Sjkim		&psrld	("mm3",8);		&psrld	("mm7",8);
1556238405Sjkim		&movq	("mm2",&QWP(0,$key));	&movq	("mm6",&QWP(8,$key));
1557238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8<<8
1558238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp8>>8
1559238405Sjkim		&mov	($s0,&DWP(0-128,$tbl));
1560238405Sjkim		&pslld	("mm1",16);		&pslld	("mm5",16);
1561238405Sjkim		&mov	($s1,&DWP(64-128,$tbl));
1562238405Sjkim		&psrld	("mm3",16);		&psrld	("mm7",16);
1563238405Sjkim		&mov	($s2,&DWP(128-128,$tbl));
1564238405Sjkim		&pxor	("mm0","mm1");		&pxor	("mm4","mm5");	# ^= tp8<<24
1565238405Sjkim		&mov	($s3,&DWP(192-128,$tbl));
1566238405Sjkim		&pxor	("mm0","mm3");		&pxor	("mm4","mm7");	# ^= tp8>>24
1567238405Sjkim
1568238405Sjkim		&pxor	("mm0","mm2");		&pxor	("mm4","mm6");
1569238405Sjkim	&jmp	(&label("loop"));
1570238405Sjkim
1571238405Sjkim	&set_label("out",16);
1572238405Sjkim	&pxor	("mm0",&QWP(0,$key));
1573238405Sjkim	&pxor	("mm4",&QWP(8,$key));
1574238405Sjkim
1575238405Sjkim	&ret	();
1576238405Sjkim&function_end_B("_sse_AES_decrypt_compact");
1577238405Sjkim					}
1578238405Sjkim
1579238405Sjkim######################################################################
1580238405Sjkim# Vanilla block function.
1581238405Sjkim######################################################################
1582238405Sjkim
1583160814Ssimonsub decstep()
1584160814Ssimon{ my ($i,$td,@s) = @_;
1585160814Ssimon  my $tmp = $key;
1586160814Ssimon  my $out = $i==3?$s[0]:$acc;
1587160814Ssimon
1588160814Ssimon	# no instructions are reordered, as performance appears
1589160814Ssimon	# optimal... or rather that all attempts to reorder didn't
1590160814Ssimon	# result in better performance [which by the way is not a
1591160814Ssimon	# bit lower than ecryption].
1592238405Sjkim	if($i==3)   {	&mov	($key,$__key);			}
1593160814Ssimon	else        {	&mov	($out,$s[0]);			}
1594160814Ssimon			&and	($out,0xFF);
1595160814Ssimon			&mov	($out,&DWP(0,$td,$out,8));
1596160814Ssimon
1597160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}
1598160814Ssimon			&movz	($tmp,&HB($s[1]));
1599160814Ssimon			&xor	($out,&DWP(3,$td,$tmp,8));
1600160814Ssimon
1601160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1602160814Ssimon	else        {	&mov	($tmp,$s[2]);			}
1603160814Ssimon			&shr	($tmp,16);
1604160814Ssimon			&and	($tmp,0xFF);
1605160814Ssimon			&xor	($out,&DWP(2,$td,$tmp,8));
1606160814Ssimon
1607238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}
1608160814Ssimon	else        {	&mov	($tmp,$s[3]);			}
1609160814Ssimon			&shr	($tmp,24);
1610160814Ssimon			&xor	($out,&DWP(1,$td,$tmp,8));
1611160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1612238405Sjkim	if ($i==3)  {	&mov	($s[3],$__s0);			}
1613160814Ssimon			&comment();
1614160814Ssimon}
1615160814Ssimon
1616160814Ssimonsub declast()
1617160814Ssimon{ my ($i,$td,@s)=@_;
1618160814Ssimon  my $tmp = $key;
1619160814Ssimon  my $out = $i==3?$s[0]:$acc;
1620160814Ssimon
1621238405Sjkim	if($i==0)   {	&lea	($td,&DWP(2048+128,$td));
1622238405Sjkim			&mov	($tmp,&DWP(0-128,$td));
1623238405Sjkim			&mov	($acc,&DWP(32-128,$td));
1624238405Sjkim			&mov	($tmp,&DWP(64-128,$td));
1625238405Sjkim			&mov	($acc,&DWP(96-128,$td));
1626238405Sjkim			&mov	($tmp,&DWP(128-128,$td));
1627238405Sjkim			&mov	($acc,&DWP(160-128,$td));
1628238405Sjkim			&mov	($tmp,&DWP(192-128,$td));
1629238405Sjkim			&mov	($acc,&DWP(224-128,$td));
1630238405Sjkim			&lea	($td,&DWP(-128,$td));		}
1631238405Sjkim	if($i==3)   {	&mov	($key,$__key);			}
1632160814Ssimon	else        {	&mov	($out,$s[0]);			}
1633160814Ssimon			&and	($out,0xFF);
1634238405Sjkim			&movz	($out,&BP(0,$td,$out,1));
1635160814Ssimon
1636160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}
1637160814Ssimon			&movz	($tmp,&HB($s[1]));
1638238405Sjkim			&movz	($tmp,&BP(0,$td,$tmp,1));
1639162911Ssimon			&shl	($tmp,8);
1640160814Ssimon			&xor	($out,$tmp);
1641160814Ssimon
1642160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
1643160814Ssimon	else        {	mov	($tmp,$s[2]);			}
1644160814Ssimon			&shr	($tmp,16);
1645160814Ssimon			&and	($tmp,0xFF);
1646238405Sjkim			&movz	($tmp,&BP(0,$td,$tmp,1));
1647162911Ssimon			&shl	($tmp,16);
1648160814Ssimon			&xor	($out,$tmp);
1649160814Ssimon
1650238405Sjkim	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],$__s1);		}
1651160814Ssimon	else        {	&mov	($tmp,$s[3]);			}
1652160814Ssimon			&shr	($tmp,24);
1653238405Sjkim			&movz	($tmp,&BP(0,$td,$tmp,1));
1654162911Ssimon			&shl	($tmp,24);
1655160814Ssimon			&xor	($out,$tmp);
1656160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
1657238405Sjkim	if ($i==3)  {	&mov	($s[3],$__s0);
1658238405Sjkim			&lea	($td,&DWP(-2048,$td));		}
1659160814Ssimon}
1660160814Ssimon
1661160814Ssimon&function_begin_B("_x86_AES_decrypt");
1662160814Ssimon	# note that caller is expected to allocate stack frame for me!
1663238405Sjkim	&mov	($__key,$key);			# save key
1664160814Ssimon
1665160814Ssimon	&xor	($s0,&DWP(0,$key));		# xor with key
1666160814Ssimon	&xor	($s1,&DWP(4,$key));
1667160814Ssimon	&xor	($s2,&DWP(8,$key));
1668160814Ssimon	&xor	($s3,&DWP(12,$key));
1669160814Ssimon
1670160814Ssimon	&mov	($acc,&DWP(240,$key));		# load key->rounds
1671160814Ssimon
1672160814Ssimon	if ($small_footprint) {
1673160814Ssimon	    &lea	($acc,&DWP(-2,$acc,$acc));
1674160814Ssimon	    &lea	($acc,&DWP(0,$key,$acc,8));
1675238405Sjkim	    &mov	($__end,$acc);		# end of key schedule
1676238405Sjkim	    &set_label("loop",16);
1677238405Sjkim		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1678238405Sjkim		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1679238405Sjkim		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1680238405Sjkim		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1681160814Ssimon		&add	($key,16);		# advance rd_key
1682160814Ssimon		&xor	($s0,&DWP(0,$key));
1683160814Ssimon		&xor	($s1,&DWP(4,$key));
1684160814Ssimon		&xor	($s2,&DWP(8,$key));
1685160814Ssimon		&xor	($s3,&DWP(12,$key));
1686238405Sjkim	    &cmp	($key,$__end);
1687238405Sjkim	    &mov	($__key,$key);
1688160814Ssimon	    &jb		(&label("loop"));
1689160814Ssimon	}
1690160814Ssimon	else {
1691160814Ssimon	    &cmp	($acc,10);
1692160814Ssimon	    &jle	(&label("10rounds"));
1693160814Ssimon	    &cmp	($acc,12);
1694160814Ssimon	    &jle	(&label("12rounds"));
1695160814Ssimon
1696238405Sjkim	&set_label("14rounds",4);
1697160814Ssimon	    for ($i=1;$i<3;$i++) {
1698238405Sjkim		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1699238405Sjkim		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1700238405Sjkim		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1701238405Sjkim		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1702160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
1703160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
1704160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
1705160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
1706160814Ssimon	    }
1707160814Ssimon	    &add	($key,32);
1708238405Sjkim	    &mov	($__key,$key);		# advance rd_key
1709238405Sjkim	&set_label("12rounds",4);
1710160814Ssimon	    for ($i=1;$i<3;$i++) {
1711238405Sjkim		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1712238405Sjkim		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1713238405Sjkim		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1714238405Sjkim		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1715160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
1716160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
1717160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
1718160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
1719160814Ssimon	    }
1720160814Ssimon	    &add	($key,32);
1721238405Sjkim	    &mov	($__key,$key);		# advance rd_key
1722238405Sjkim	&set_label("10rounds",4);
1723160814Ssimon	    for ($i=1;$i<10;$i++) {
1724238405Sjkim		&decstep(0,$tbl,$s0,$s3,$s2,$s1);
1725238405Sjkim		&decstep(1,$tbl,$s1,$s0,$s3,$s2);
1726238405Sjkim		&decstep(2,$tbl,$s2,$s1,$s0,$s3);
1727238405Sjkim		&decstep(3,$tbl,$s3,$s2,$s1,$s0);
1728160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
1729160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
1730160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
1731160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
1732160814Ssimon	    }
1733160814Ssimon	}
1734160814Ssimon
1735238405Sjkim	&declast(0,$tbl,$s0,$s3,$s2,$s1);
1736238405Sjkim	&declast(1,$tbl,$s1,$s0,$s3,$s2);
1737238405Sjkim	&declast(2,$tbl,$s2,$s1,$s0,$s3);
1738238405Sjkim	&declast(3,$tbl,$s3,$s2,$s1,$s0);
1739160814Ssimon
1740160814Ssimon	&add	($key,$small_footprint?16:160);
1741160814Ssimon	&xor	($s0,&DWP(0,$key));
1742160814Ssimon	&xor	($s1,&DWP(4,$key));
1743160814Ssimon	&xor	($s2,&DWP(8,$key));
1744160814Ssimon	&xor	($s3,&DWP(12,$key));
1745160814Ssimon
1746160814Ssimon	&ret	();
1747160814Ssimon
1748160814Ssimon&set_label("AES_Td",64);	# Yes! I keep it in the code segment!
1749160814Ssimon	&_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
1750160814Ssimon	&_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
1751160814Ssimon	&_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
1752160814Ssimon	&_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
1753160814Ssimon	&_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
1754160814Ssimon	&_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
1755160814Ssimon	&_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
1756160814Ssimon	&_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
1757160814Ssimon	&_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
1758160814Ssimon	&_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
1759160814Ssimon	&_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
1760160814Ssimon	&_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
1761160814Ssimon	&_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
1762160814Ssimon	&_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
1763160814Ssimon	&_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
1764160814Ssimon	&_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
1765160814Ssimon	&_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
1766160814Ssimon	&_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
1767160814Ssimon	&_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
1768160814Ssimon	&_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
1769160814Ssimon	&_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
1770160814Ssimon	&_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
1771160814Ssimon	&_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
1772160814Ssimon	&_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
1773160814Ssimon	&_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
1774160814Ssimon	&_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
1775160814Ssimon	&_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
1776160814Ssimon	&_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
1777160814Ssimon	&_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
1778160814Ssimon	&_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
1779160814Ssimon	&_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
1780160814Ssimon	&_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
1781160814Ssimon	&_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
1782160814Ssimon	&_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
1783160814Ssimon	&_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
1784160814Ssimon	&_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
1785160814Ssimon	&_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
1786160814Ssimon	&_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
1787160814Ssimon	&_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
1788160814Ssimon	&_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
1789160814Ssimon	&_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
1790160814Ssimon	&_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
1791160814Ssimon	&_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
1792160814Ssimon	&_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
1793160814Ssimon	&_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
1794160814Ssimon	&_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
1795160814Ssimon	&_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
1796160814Ssimon	&_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
1797160814Ssimon	&_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
1798160814Ssimon	&_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
1799160814Ssimon	&_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
1800160814Ssimon	&_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
1801160814Ssimon	&_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
1802160814Ssimon	&_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
1803160814Ssimon	&_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
1804160814Ssimon	&_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
1805160814Ssimon	&_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
1806160814Ssimon	&_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
1807160814Ssimon	&_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
1808160814Ssimon	&_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
1809160814Ssimon	&_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
1810160814Ssimon	&_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
1811160814Ssimon	&_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
1812160814Ssimon	&_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
1813238405Sjkim
1814238405Sjkim#Td4:	# four copies of Td4 to choose from to avoid L1 aliasing
1815162911Ssimon	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1816162911Ssimon	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1817162911Ssimon	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1818162911Ssimon	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1819162911Ssimon	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1820162911Ssimon	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1821162911Ssimon	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1822162911Ssimon	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1823162911Ssimon	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1824162911Ssimon	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1825162911Ssimon	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1826162911Ssimon	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1827162911Ssimon	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1828162911Ssimon	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1829162911Ssimon	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1830162911Ssimon	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1831162911Ssimon	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1832162911Ssimon	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1833162911Ssimon	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1834162911Ssimon	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1835162911Ssimon	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1836162911Ssimon	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1837162911Ssimon	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1838162911Ssimon	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1839162911Ssimon	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1840162911Ssimon	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1841162911Ssimon	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1842162911Ssimon	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1843162911Ssimon	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1844162911Ssimon	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1845162911Ssimon	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1846162911Ssimon	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1847238405Sjkim
1848238405Sjkim	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1849238405Sjkim	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1850238405Sjkim	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1851238405Sjkim	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1852238405Sjkim	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1853238405Sjkim	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1854238405Sjkim	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1855238405Sjkim	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1856238405Sjkim	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1857238405Sjkim	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1858238405Sjkim	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1859238405Sjkim	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1860238405Sjkim	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1861238405Sjkim	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1862238405Sjkim	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1863238405Sjkim	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1864238405Sjkim	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1865238405Sjkim	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1866238405Sjkim	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1867238405Sjkim	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1868238405Sjkim	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1869238405Sjkim	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1870238405Sjkim	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1871238405Sjkim	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1872238405Sjkim	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1873238405Sjkim	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1874238405Sjkim	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1875238405Sjkim	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1876238405Sjkim	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1877238405Sjkim	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1878238405Sjkim	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1879238405Sjkim	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1880238405Sjkim
1881238405Sjkim	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1882238405Sjkim	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1883238405Sjkim	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1884238405Sjkim	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1885238405Sjkim	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1886238405Sjkim	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1887238405Sjkim	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1888238405Sjkim	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1889238405Sjkim	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1890238405Sjkim	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1891238405Sjkim	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1892238405Sjkim	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1893238405Sjkim	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1894238405Sjkim	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1895238405Sjkim	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1896238405Sjkim	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1897238405Sjkim	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1898238405Sjkim	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1899238405Sjkim	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1900238405Sjkim	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1901238405Sjkim	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1902238405Sjkim	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1903238405Sjkim	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1904238405Sjkim	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1905238405Sjkim	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1906238405Sjkim	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1907238405Sjkim	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1908238405Sjkim	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1909238405Sjkim	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1910238405Sjkim	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1911238405Sjkim	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1912238405Sjkim	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1913238405Sjkim
1914238405Sjkim	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1915238405Sjkim	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1916238405Sjkim	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1917238405Sjkim	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1918238405Sjkim	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1919238405Sjkim	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1920238405Sjkim	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1921238405Sjkim	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1922238405Sjkim	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1923238405Sjkim	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1924238405Sjkim	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1925238405Sjkim	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1926238405Sjkim	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1927238405Sjkim	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1928238405Sjkim	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1929238405Sjkim	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1930238405Sjkim	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1931238405Sjkim	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1932238405Sjkim	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1933238405Sjkim	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1934238405Sjkim	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1935238405Sjkim	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1936238405Sjkim	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1937238405Sjkim	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1938238405Sjkim	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1939238405Sjkim	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1940238405Sjkim	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1941238405Sjkim	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1942238405Sjkim	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1943238405Sjkim	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1944238405Sjkim	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1945238405Sjkim	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1946160814Ssimon&function_end_B("_x86_AES_decrypt");
1947160814Ssimon
1948160814Ssimon# void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
1949160814Ssimon&function_begin("AES_decrypt");
1950160814Ssimon	&mov	($acc,&wparam(0));		# load inp
1951160814Ssimon	&mov	($key,&wparam(2));		# load key
1952160814Ssimon
1953160814Ssimon	&mov	($s0,"esp");
1954238405Sjkim	&sub	("esp",36);
1955238405Sjkim	&and	("esp",-64);			# align to cache-line
1956160814Ssimon
1957238405Sjkim	# place stack frame just "above" the key schedule
1958238405Sjkim	&lea	($s1,&DWP(-64-63,$key));
1959238405Sjkim	&sub	($s1,"esp");
1960238405Sjkim	&neg	($s1);
1961238405Sjkim	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
1962238405Sjkim	&sub	("esp",$s1);
1963238405Sjkim	&add	("esp",4);	# 4 is reserved for caller's return address
1964238405Sjkim	&mov	($_esp,$s0);	# save stack pointer
1965238405Sjkim
1966160814Ssimon	&call   (&label("pic_point"));          # make it PIC!
1967160814Ssimon	&set_label("pic_point");
1968238405Sjkim	&blindpop($tbl);
1969238405Sjkim	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
1970238405Sjkim	&lea    ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
1971160814Ssimon
1972238405Sjkim	# pick Td4 copy which can't "overlap" with stack frame or key schedule
1973238405Sjkim	&lea	($s1,&DWP(768-4,"esp"));
1974238405Sjkim	&sub	($s1,$tbl);
1975238405Sjkim	&and	($s1,0x300);
1976238405Sjkim	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
1977162911Ssimon
1978238405Sjkim					if (!$x86only) {
1979238405Sjkim	&bt	(&DWP(0,$s0),25);	# check for SSE bit
1980238405Sjkim	&jnc	(&label("x86"));
1981238405Sjkim
1982238405Sjkim	&movq	("mm0",&QWP(0,$acc));
1983238405Sjkim	&movq	("mm4",&QWP(8,$acc));
1984238405Sjkim	&call	("_sse_AES_decrypt_compact");
1985238405Sjkim	&mov	("esp",$_esp);			# restore stack pointer
1986238405Sjkim	&mov	($acc,&wparam(1));		# load out
1987238405Sjkim	&movq	(&QWP(0,$acc),"mm0");		# write output data
1988238405Sjkim	&movq	(&QWP(8,$acc),"mm4");
1989238405Sjkim	&emms	();
1990238405Sjkim	&function_end_A();
1991238405Sjkim					}
1992238405Sjkim	&set_label("x86",16);
1993238405Sjkim	&mov	($_tbl,$tbl);
1994160814Ssimon	&mov	($s0,&DWP(0,$acc));		# load input data
1995160814Ssimon	&mov	($s1,&DWP(4,$acc));
1996160814Ssimon	&mov	($s2,&DWP(8,$acc));
1997160814Ssimon	&mov	($s3,&DWP(12,$acc));
1998238405Sjkim	&call	("_x86_AES_decrypt_compact");
1999238405Sjkim	&mov	("esp",$_esp);			# restore stack pointer
2000160814Ssimon	&mov	($acc,&wparam(1));		# load out
2001160814Ssimon	&mov	(&DWP(0,$acc),$s0);		# write output data
2002160814Ssimon	&mov	(&DWP(4,$acc),$s1);
2003160814Ssimon	&mov	(&DWP(8,$acc),$s2);
2004160814Ssimon	&mov	(&DWP(12,$acc),$s3);
2005160814Ssimon&function_end("AES_decrypt");
2006160814Ssimon
2007160814Ssimon# void AES_cbc_encrypt (const void char *inp, unsigned char *out,
2008160814Ssimon#			size_t length, const AES_KEY *key,
2009160814Ssimon#			unsigned char *ivp,const int enc);
2010160814Ssimon{
2011160814Ssimon# stack frame layout
2012238405Sjkim#             -4(%esp)		# return address	 0(%esp)
2013238405Sjkim#              0(%esp)		# s0 backing store	 4(%esp)
2014238405Sjkim#              4(%esp)		# s1 backing store	 8(%esp)
2015238405Sjkim#              8(%esp)		# s2 backing store	12(%esp)
2016238405Sjkim#             12(%esp)		# s3 backing store	16(%esp)
2017238405Sjkim#             16(%esp)		# key backup		20(%esp)
2018238405Sjkim#             20(%esp)		# end of key schedule	24(%esp)
2019238405Sjkim#             24(%esp)		# %ebp backup		28(%esp)
2020238405Sjkim#             28(%esp)		# %esp backup
2021238405Sjkimmy $_inp=&DWP(32,"esp");	# copy of wparam(0)
2022238405Sjkimmy $_out=&DWP(36,"esp");	# copy of wparam(1)
2023238405Sjkimmy $_len=&DWP(40,"esp");	# copy of wparam(2)
2024238405Sjkimmy $_key=&DWP(44,"esp");	# copy of wparam(3)
2025238405Sjkimmy $_ivp=&DWP(48,"esp");	# copy of wparam(4)
2026238405Sjkimmy $_tmp=&DWP(52,"esp");	# volatile variable
2027238405Sjkim#
2028238405Sjkimmy $ivec=&DWP(60,"esp");	# ivec[16]
2029238405Sjkimmy $aes_key=&DWP(76,"esp");	# copy of aes_key
2030238405Sjkimmy $mark=&DWP(76+240,"esp");	# copy of aes_key->rounds
2031160814Ssimon
2032160814Ssimon&function_begin("AES_cbc_encrypt");
2033160814Ssimon	&mov	($s2 eq "ecx"? $s2 : "",&wparam(2));	# load len
2034160814Ssimon	&cmp	($s2,0);
2035238405Sjkim	&je	(&label("drop_out"));
2036160814Ssimon
2037160814Ssimon	&call   (&label("pic_point"));		# make it PIC!
2038160814Ssimon	&set_label("pic_point");
2039238405Sjkim	&blindpop($tbl);
2040238405Sjkim	&picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point")) if(!$x86only);
2041160814Ssimon
2042238405Sjkim	&cmp	(&wparam(5),0);
2043238405Sjkim	&lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
2044238405Sjkim	&jne	(&label("picked_te"));
2045238405Sjkim	&lea	($tbl,&DWP(&label("AES_Td")."-".&label("AES_Te"),$tbl));
2046238405Sjkim	&set_label("picked_te");
2047238405Sjkim
2048238405Sjkim	# one can argue if this is required
2049160814Ssimon	&pushf	();
2050160814Ssimon	&cld	();
2051160814Ssimon
2052238405Sjkim	&cmp	($s2,$speed_limit);
2053238405Sjkim	&jb	(&label("slow_way"));
2054238405Sjkim	&test	($s2,15);
2055238405Sjkim	&jnz	(&label("slow_way"));
2056238405Sjkim					if (!$x86only) {
2057238405Sjkim	&bt	(&DWP(0,$s0),28);	# check for hyper-threading bit
2058238405Sjkim	&jc	(&label("slow_way"));
2059238405Sjkim					}
2060238405Sjkim	# pre-allocate aligned stack frame...
2061238405Sjkim	&lea	($acc,&DWP(-80-244,"esp"));
2062238405Sjkim	&and	($acc,-64);
2063160814Ssimon
2064238405Sjkim	# ... and make sure it doesn't alias with $tbl modulo 4096
2065238405Sjkim	&mov	($s0,$tbl);
2066238405Sjkim	&lea	($s1,&DWP(2048+256,$tbl));
2067238405Sjkim	&mov	($s3,$acc);
2068160814Ssimon	&and	($s0,0xfff);		# s = %ebp&0xfff
2069238405Sjkim	&and	($s1,0xfff);		# e = (%ebp+2048+256)&0xfff
2070160814Ssimon	&and	($s3,0xfff);		# p = %esp&0xfff
2071160814Ssimon
2072160814Ssimon	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
2073238405Sjkim	&jb	(&label("tbl_break_out"));
2074160814Ssimon	&sub	($s3,$s1);
2075238405Sjkim	&sub	($acc,$s3);
2076238405Sjkim	&jmp	(&label("tbl_ok"));
2077238405Sjkim	&set_label("tbl_break_out",4);	# else %esp -= (p-s)&0xfff + framesz;
2078160814Ssimon	&sub	($s3,$s0);
2079160814Ssimon	&and	($s3,0xfff);
2080238405Sjkim	&add	($s3,384);
2081238405Sjkim	&sub	($acc,$s3);
2082238405Sjkim	&set_label("tbl_ok",4);
2083160814Ssimon
2084238405Sjkim	&lea	($s3,&wparam(0));	# obtain pointer to parameter block
2085238405Sjkim	&exch	("esp",$acc);		# allocate stack frame
2086160814Ssimon	&add	("esp",4);		# reserve for return address!
2087238405Sjkim	&mov	($_tbl,$tbl);		# save %ebp
2088238405Sjkim	&mov	($_esp,$acc);		# save %esp
2089160814Ssimon
2090238405Sjkim	&mov	($s0,&DWP(0,$s3));	# load inp
2091238405Sjkim	&mov	($s1,&DWP(4,$s3));	# load out
2092238405Sjkim	#&mov	($s2,&DWP(8,$s3));	# load len
2093238405Sjkim	&mov	($key,&DWP(12,$s3));	# load key
2094238405Sjkim	&mov	($acc,&DWP(16,$s3));	# load ivp
2095238405Sjkim	&mov	($s3,&DWP(20,$s3));	# load enc flag
2096238405Sjkim
2097160814Ssimon	&mov	($_inp,$s0);		# save copy of inp
2098160814Ssimon	&mov	($_out,$s1);		# save copy of out
2099160814Ssimon	&mov	($_len,$s2);		# save copy of len
2100238405Sjkim	&mov	($_key,$key);		# save copy of key
2101160814Ssimon	&mov	($_ivp,$acc);		# save copy of ivp
2102160814Ssimon
2103162911Ssimon	&mov	($mark,0);		# copy of aes_key->rounds = 0;
2104162911Ssimon	# do we copy key schedule to stack?
2105238405Sjkim	&mov	($s1 eq "ebx" ? $s1 : "",$key);
2106162911Ssimon	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
2107238405Sjkim	&sub	($s1,$tbl);
2108238405Sjkim	&mov	("esi",$key);
2109162911Ssimon	&and	($s1,0xfff);
2110160814Ssimon	&lea	("edi",$aes_key);
2111238405Sjkim	&cmp	($s1,2048+256);
2112238405Sjkim	&jb	(&label("do_copy"));
2113162911Ssimon	&cmp	($s1,4096-244);
2114238405Sjkim	&jb	(&label("skip_copy"));
2115238405Sjkim	&set_label("do_copy",4);
2116162911Ssimon		&mov	($_key,"edi");
2117162911Ssimon		&data_word(0xA5F3F689);	# rep movsd
2118238405Sjkim	&set_label("skip_copy");
2119160814Ssimon
2120160814Ssimon	&mov	($key,16);
2121238405Sjkim	&set_label("prefetch_tbl",4);
2122238405Sjkim		&mov	($s0,&DWP(0,$tbl));
2123238405Sjkim		&mov	($s1,&DWP(32,$tbl));
2124238405Sjkim		&mov	($s2,&DWP(64,$tbl));
2125238405Sjkim		&mov	($acc,&DWP(96,$tbl));
2126238405Sjkim		&lea	($tbl,&DWP(128,$tbl));
2127238405Sjkim		&sub	($key,1);
2128238405Sjkim	&jnz	(&label("prefetch_tbl"));
2129238405Sjkim	&sub	($tbl,2048);
2130160814Ssimon
2131238405Sjkim	&mov	($acc,$_inp);
2132160814Ssimon	&mov	($key,$_ivp);
2133160814Ssimon
2134238405Sjkim	&cmp	($s3,0);
2135238405Sjkim	&je	(&label("fast_decrypt"));
2136238405Sjkim
2137238405Sjkim#----------------------------- ENCRYPT -----------------------------#
2138160814Ssimon	&mov	($s0,&DWP(0,$key));		# load iv
2139160814Ssimon	&mov	($s1,&DWP(4,$key));
2140160814Ssimon
2141238405Sjkim	&set_label("fast_enc_loop",16);
2142160814Ssimon		&mov	($s2,&DWP(8,$key));
2143160814Ssimon		&mov	($s3,&DWP(12,$key));
2144160814Ssimon
2145160814Ssimon		&xor	($s0,&DWP(0,$acc));	# xor input data
2146160814Ssimon		&xor	($s1,&DWP(4,$acc));
2147160814Ssimon		&xor	($s2,&DWP(8,$acc));
2148160814Ssimon		&xor	($s3,&DWP(12,$acc));
2149160814Ssimon
2150160814Ssimon		&mov	($key,$_key);		# load key
2151160814Ssimon		&call	("_x86_AES_encrypt");
2152160814Ssimon
2153160814Ssimon		&mov	($acc,$_inp);		# load inp
2154160814Ssimon		&mov	($key,$_out);		# load out
2155160814Ssimon
2156160814Ssimon		&mov	(&DWP(0,$key),$s0);	# save output data
2157160814Ssimon		&mov	(&DWP(4,$key),$s1);
2158160814Ssimon		&mov	(&DWP(8,$key),$s2);
2159160814Ssimon		&mov	(&DWP(12,$key),$s3);
2160160814Ssimon
2161238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2162160814Ssimon		&mov	($s2,$_len);		# load len
2163160814Ssimon		&mov	($_inp,$acc);		# save inp
2164238405Sjkim		&lea	($s3,&DWP(16,$key));	# advance out
2165160814Ssimon		&mov	($_out,$s3);		# save out
2166238405Sjkim		&sub	($s2,16);		# decrease len
2167160814Ssimon		&mov	($_len,$s2);		# save len
2168238405Sjkim	&jnz	(&label("fast_enc_loop"));
2169160814Ssimon	&mov	($acc,$_ivp);		# load ivp
2170238405Sjkim	&mov	($s2,&DWP(8,$key));	# restore last 2 dwords
2171160814Ssimon	&mov	($s3,&DWP(12,$key));
2172160814Ssimon	&mov	(&DWP(0,$acc),$s0);	# save ivec
2173160814Ssimon	&mov	(&DWP(4,$acc),$s1);
2174160814Ssimon	&mov	(&DWP(8,$acc),$s2);
2175160814Ssimon	&mov	(&DWP(12,$acc),$s3);
2176160814Ssimon
2177162911Ssimon	&cmp	($mark,0);		# was the key schedule copied?
2178160814Ssimon	&mov	("edi",$_key);
2179162911Ssimon	&je	(&label("skip_ezero"));
2180160814Ssimon	# zero copy of key schedule
2181160814Ssimon	&mov	("ecx",240/4);
2182160814Ssimon	&xor	("eax","eax");
2183160814Ssimon	&align	(4);
2184162911Ssimon	&data_word(0xABF3F689);	# rep stosd
2185162911Ssimon	&set_label("skip_ezero")
2186194206Ssimon	&mov	("esp",$_esp);
2187160814Ssimon	&popf	();
2188238405Sjkim    &set_label("drop_out");
2189160814Ssimon	&function_end_A();
2190160814Ssimon	&pushf	();			# kludge, never executed
2191160814Ssimon
2192160814Ssimon#----------------------------- DECRYPT -----------------------------#
2193238405Sjkim&set_label("fast_decrypt",16);
2194160814Ssimon
2195160814Ssimon	&cmp	($acc,$_out);
2196238405Sjkim	&je	(&label("fast_dec_in_place"));	# in-place processing...
2197160814Ssimon
2198160814Ssimon	&mov	($_tmp,$key);
2199160814Ssimon
2200160814Ssimon	&align	(4);
2201238405Sjkim	&set_label("fast_dec_loop",16);
2202160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read input
2203160814Ssimon		&mov	($s1,&DWP(4,$acc));
2204160814Ssimon		&mov	($s2,&DWP(8,$acc));
2205160814Ssimon		&mov	($s3,&DWP(12,$acc));
2206160814Ssimon
2207160814Ssimon		&mov	($key,$_key);		# load key
2208160814Ssimon		&call	("_x86_AES_decrypt");
2209160814Ssimon
2210160814Ssimon		&mov	($key,$_tmp);		# load ivp
2211160814Ssimon		&mov	($acc,$_len);		# load len
2212160814Ssimon		&xor	($s0,&DWP(0,$key));	# xor iv
2213160814Ssimon		&xor	($s1,&DWP(4,$key));
2214160814Ssimon		&xor	($s2,&DWP(8,$key));
2215160814Ssimon		&xor	($s3,&DWP(12,$key));
2216160814Ssimon
2217238405Sjkim		&mov	($key,$_out);		# load out
2218160814Ssimon		&mov	($acc,$_inp);		# load inp
2219160814Ssimon
2220160814Ssimon		&mov	(&DWP(0,$key),$s0);	# write output
2221160814Ssimon		&mov	(&DWP(4,$key),$s1);
2222160814Ssimon		&mov	(&DWP(8,$key),$s2);
2223160814Ssimon		&mov	(&DWP(12,$key),$s3);
2224160814Ssimon
2225238405Sjkim		&mov	($s2,$_len);		# load len
2226160814Ssimon		&mov	($_tmp,$acc);		# save ivp
2227238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2228160814Ssimon		&mov	($_inp,$acc);		# save inp
2229238405Sjkim		&lea	($key,&DWP(16,$key));	# advance out
2230160814Ssimon		&mov	($_out,$key);		# save out
2231238405Sjkim		&sub	($s2,16);		# decrease len
2232238405Sjkim		&mov	($_len,$s2);		# save len
2233238405Sjkim	&jnz	(&label("fast_dec_loop"));
2234160814Ssimon	&mov	($key,$_tmp);		# load temp ivp
2235160814Ssimon	&mov	($acc,$_ivp);		# load user ivp
2236160814Ssimon	&mov	($s0,&DWP(0,$key));	# load iv
2237160814Ssimon	&mov	($s1,&DWP(4,$key));
2238160814Ssimon	&mov	($s2,&DWP(8,$key));
2239160814Ssimon	&mov	($s3,&DWP(12,$key));
2240160814Ssimon	&mov	(&DWP(0,$acc),$s0);	# copy back to user
2241160814Ssimon	&mov	(&DWP(4,$acc),$s1);
2242160814Ssimon	&mov	(&DWP(8,$acc),$s2);
2243160814Ssimon	&mov	(&DWP(12,$acc),$s3);
2244238405Sjkim	&jmp	(&label("fast_dec_out"));
2245160814Ssimon
2246238405Sjkim    &set_label("fast_dec_in_place",16);
2247238405Sjkim	&set_label("fast_dec_in_place_loop");
2248160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read input
2249160814Ssimon		&mov	($s1,&DWP(4,$acc));
2250160814Ssimon		&mov	($s2,&DWP(8,$acc));
2251160814Ssimon		&mov	($s3,&DWP(12,$acc));
2252160814Ssimon
2253238405Sjkim		&lea	($key,$ivec);
2254160814Ssimon		&mov	(&DWP(0,$key),$s0);	# copy to temp
2255160814Ssimon		&mov	(&DWP(4,$key),$s1);
2256160814Ssimon		&mov	(&DWP(8,$key),$s2);
2257160814Ssimon		&mov	(&DWP(12,$key),$s3);
2258160814Ssimon
2259160814Ssimon		&mov	($key,$_key);		# load key
2260160814Ssimon		&call	("_x86_AES_decrypt");
2261160814Ssimon
2262160814Ssimon		&mov	($key,$_ivp);		# load ivp
2263160814Ssimon		&mov	($acc,$_out);		# load out
2264160814Ssimon		&xor	($s0,&DWP(0,$key));	# xor iv
2265160814Ssimon		&xor	($s1,&DWP(4,$key));
2266160814Ssimon		&xor	($s2,&DWP(8,$key));
2267160814Ssimon		&xor	($s3,&DWP(12,$key));
2268160814Ssimon
2269160814Ssimon		&mov	(&DWP(0,$acc),$s0);	# write output
2270160814Ssimon		&mov	(&DWP(4,$acc),$s1);
2271160814Ssimon		&mov	(&DWP(8,$acc),$s2);
2272160814Ssimon		&mov	(&DWP(12,$acc),$s3);
2273160814Ssimon
2274238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance out
2275160814Ssimon		&mov	($_out,$acc);		# save out
2276160814Ssimon
2277160814Ssimon		&lea	($acc,$ivec);
2278160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read temp
2279160814Ssimon		&mov	($s1,&DWP(4,$acc));
2280160814Ssimon		&mov	($s2,&DWP(8,$acc));
2281160814Ssimon		&mov	($s3,&DWP(12,$acc));
2282160814Ssimon
2283160814Ssimon		&mov	(&DWP(0,$key),$s0);	# copy iv
2284160814Ssimon		&mov	(&DWP(4,$key),$s1);
2285160814Ssimon		&mov	(&DWP(8,$key),$s2);
2286160814Ssimon		&mov	(&DWP(12,$key),$s3);
2287160814Ssimon
2288160814Ssimon		&mov	($acc,$_inp);		# load inp
2289238405Sjkim		&mov	($s2,$_len);		# load len
2290238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2291238405Sjkim		&mov	($_inp,$acc);		# save inp
2292238405Sjkim		&sub	($s2,16);		# decrease len
2293238405Sjkim		&mov	($_len,$s2);		# save len
2294238405Sjkim	&jnz	(&label("fast_dec_in_place_loop"));
2295160814Ssimon
2296238405Sjkim    &set_label("fast_dec_out",4);
2297238405Sjkim	&cmp	($mark,0);		# was the key schedule copied?
2298238405Sjkim	&mov	("edi",$_key);
2299238405Sjkim	&je	(&label("skip_dzero"));
2300238405Sjkim	# zero copy of key schedule
2301238405Sjkim	&mov	("ecx",240/4);
2302238405Sjkim	&xor	("eax","eax");
2303238405Sjkim	&align	(4);
2304238405Sjkim	&data_word(0xABF3F689);	# rep stosd
2305238405Sjkim	&set_label("skip_dzero")
2306238405Sjkim	&mov	("esp",$_esp);
2307238405Sjkim	&popf	();
2308238405Sjkim	&function_end_A();
2309238405Sjkim	&pushf	();			# kludge, never executed
2310238405Sjkim
2311238405Sjkim#--------------------------- SLOW ROUTINE ---------------------------#
2312238405Sjkim&set_label("slow_way",16);
2313238405Sjkim
2314238405Sjkim	&mov	($s0,&DWP(0,$s0)) if (!$x86only);# load OPENSSL_ia32cap
2315238405Sjkim	&mov	($key,&wparam(3));	# load key
2316238405Sjkim
2317238405Sjkim	# pre-allocate aligned stack frame...
2318238405Sjkim	&lea	($acc,&DWP(-80,"esp"));
2319238405Sjkim	&and	($acc,-64);
2320238405Sjkim
2321238405Sjkim	# ... and make sure it doesn't alias with $key modulo 1024
2322238405Sjkim	&lea	($s1,&DWP(-80-63,$key));
2323238405Sjkim	&sub	($s1,$acc);
2324238405Sjkim	&neg	($s1);
2325238405Sjkim	&and	($s1,0x3C0);	# modulo 1024, but aligned to cache-line
2326238405Sjkim	&sub	($acc,$s1);
2327238405Sjkim
2328238405Sjkim	# pick S-box copy which can't overlap with stack frame or $key
2329238405Sjkim	&lea	($s1,&DWP(768,$acc));
2330238405Sjkim	&sub	($s1,$tbl);
2331238405Sjkim	&and	($s1,0x300);
2332238405Sjkim	&lea	($tbl,&DWP(2048+128,$tbl,$s1));
2333238405Sjkim
2334238405Sjkim	&lea	($s3,&wparam(0));	# pointer to parameter block
2335238405Sjkim
2336238405Sjkim	&exch	("esp",$acc);
2337238405Sjkim	&add	("esp",4);		# reserve for return address!
2338238405Sjkim	&mov	($_tbl,$tbl);		# save %ebp
2339238405Sjkim	&mov	($_esp,$acc);		# save %esp
2340238405Sjkim	&mov	($_tmp,$s0);		# save OPENSSL_ia32cap
2341238405Sjkim
2342238405Sjkim	&mov	($s0,&DWP(0,$s3));	# load inp
2343238405Sjkim	&mov	($s1,&DWP(4,$s3));	# load out
2344238405Sjkim	#&mov	($s2,&DWP(8,$s3));	# load len
2345238405Sjkim	#&mov	($key,&DWP(12,$s3));	# load key
2346238405Sjkim	&mov	($acc,&DWP(16,$s3));	# load ivp
2347238405Sjkim	&mov	($s3,&DWP(20,$s3));	# load enc flag
2348238405Sjkim
2349238405Sjkim	&mov	($_inp,$s0);		# save copy of inp
2350238405Sjkim	&mov	($_out,$s1);		# save copy of out
2351238405Sjkim	&mov	($_len,$s2);		# save copy of len
2352238405Sjkim	&mov	($_key,$key);		# save copy of key
2353238405Sjkim	&mov	($_ivp,$acc);		# save copy of ivp
2354238405Sjkim
2355238405Sjkim	&mov	($key,$acc);
2356238405Sjkim	&mov	($acc,$s0);
2357238405Sjkim
2358238405Sjkim	&cmp	($s3,0);
2359238405Sjkim	&je	(&label("slow_decrypt"));
2360238405Sjkim
2361238405Sjkim#--------------------------- SLOW ENCRYPT ---------------------------#
2362238405Sjkim	&cmp	($s2,16);
2363238405Sjkim	&mov	($s3,$s1);
2364238405Sjkim	&jb	(&label("slow_enc_tail"));
2365238405Sjkim
2366238405Sjkim					if (!$x86only) {
2367238405Sjkim	&bt	($_tmp,25);		# check for SSE bit
2368238405Sjkim	&jnc	(&label("slow_enc_x86"));
2369238405Sjkim
2370238405Sjkim	&movq	("mm0",&QWP(0,$key));	# load iv
2371238405Sjkim	&movq	("mm4",&QWP(8,$key));
2372238405Sjkim
2373238405Sjkim	&set_label("slow_enc_loop_sse",16);
2374238405Sjkim		&pxor	("mm0",&QWP(0,$acc));	# xor input data
2375238405Sjkim		&pxor	("mm4",&QWP(8,$acc));
2376238405Sjkim
2377238405Sjkim		&mov	($key,$_key);
2378238405Sjkim		&call	("_sse_AES_encrypt_compact");
2379238405Sjkim
2380238405Sjkim		&mov	($acc,$_inp);		# load inp
2381238405Sjkim		&mov	($key,$_out);		# load out
2382238405Sjkim		&mov	($s2,$_len);		# load len
2383238405Sjkim
2384238405Sjkim		&movq	(&QWP(0,$key),"mm0");	# save output data
2385238405Sjkim		&movq	(&QWP(8,$key),"mm4");
2386238405Sjkim
2387238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2388160814Ssimon		&mov	($_inp,$acc);		# save inp
2389238405Sjkim		&lea	($s3,&DWP(16,$key));	# advance out
2390238405Sjkim		&mov	($_out,$s3);		# save out
2391238405Sjkim		&sub	($s2,16);		# decrease len
2392238405Sjkim		&cmp	($s2,16);
2393238405Sjkim		&mov	($_len,$s2);		# save len
2394238405Sjkim	&jae	(&label("slow_enc_loop_sse"));
2395238405Sjkim	&test	($s2,15);
2396238405Sjkim	&jnz	(&label("slow_enc_tail"));
2397238405Sjkim	&mov	($acc,$_ivp);		# load ivp
2398238405Sjkim	&movq	(&QWP(0,$acc),"mm0");	# save ivec
2399238405Sjkim	&movq	(&QWP(8,$acc),"mm4");
2400238405Sjkim	&emms	();
2401238405Sjkim	&mov	("esp",$_esp);
2402238405Sjkim	&popf	();
2403238405Sjkim	&function_end_A();
2404238405Sjkim	&pushf	();			# kludge, never executed
2405238405Sjkim					}
2406238405Sjkim    &set_label("slow_enc_x86",16);
2407238405Sjkim	&mov	($s0,&DWP(0,$key));	# load iv
2408238405Sjkim	&mov	($s1,&DWP(4,$key));
2409160814Ssimon
2410238405Sjkim	&set_label("slow_enc_loop_x86",4);
2411238405Sjkim		&mov	($s2,&DWP(8,$key));
2412238405Sjkim		&mov	($s3,&DWP(12,$key));
2413238405Sjkim
2414238405Sjkim		&xor	($s0,&DWP(0,$acc));	# xor input data
2415238405Sjkim		&xor	($s1,&DWP(4,$acc));
2416238405Sjkim		&xor	($s2,&DWP(8,$acc));
2417238405Sjkim		&xor	($s3,&DWP(12,$acc));
2418238405Sjkim
2419238405Sjkim		&mov	($key,$_key);		# load key
2420238405Sjkim		&call	("_x86_AES_encrypt_compact");
2421238405Sjkim
2422238405Sjkim		&mov	($acc,$_inp);		# load inp
2423238405Sjkim		&mov	($key,$_out);		# load out
2424238405Sjkim
2425238405Sjkim		&mov	(&DWP(0,$key),$s0);	# save output data
2426238405Sjkim		&mov	(&DWP(4,$key),$s1);
2427238405Sjkim		&mov	(&DWP(8,$key),$s2);
2428238405Sjkim		&mov	(&DWP(12,$key),$s3);
2429238405Sjkim
2430160814Ssimon		&mov	($s2,$_len);		# load len
2431238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2432238405Sjkim		&mov	($_inp,$acc);		# save inp
2433238405Sjkim		&lea	($s3,&DWP(16,$key));	# advance out
2434238405Sjkim		&mov	($_out,$s3);		# save out
2435238405Sjkim		&sub	($s2,16);		# decrease len
2436238405Sjkim		&cmp	($s2,16);
2437160814Ssimon		&mov	($_len,$s2);		# save len
2438238405Sjkim	&jae	(&label("slow_enc_loop_x86"));
2439238405Sjkim	&test	($s2,15);
2440238405Sjkim	&jnz	(&label("slow_enc_tail"));
2441238405Sjkim	&mov	($acc,$_ivp);		# load ivp
2442238405Sjkim	&mov	($s2,&DWP(8,$key));	# restore last dwords
2443238405Sjkim	&mov	($s3,&DWP(12,$key));
2444238405Sjkim	&mov	(&DWP(0,$acc),$s0);	# save ivec
2445238405Sjkim	&mov	(&DWP(4,$acc),$s1);
2446238405Sjkim	&mov	(&DWP(8,$acc),$s2);
2447238405Sjkim	&mov	(&DWP(12,$acc),$s3);
2448160814Ssimon
2449238405Sjkim	&mov	("esp",$_esp);
2450238405Sjkim	&popf	();
2451238405Sjkim	&function_end_A();
2452238405Sjkim	&pushf	();			# kludge, never executed
2453238405Sjkim
2454238405Sjkim    &set_label("slow_enc_tail",16);
2455238405Sjkim	&emms	()	if (!$x86only);
2456238405Sjkim	&mov	($key eq "edi"? $key:"",$s3);	# load out to edi
2457238405Sjkim	&mov	($s1,16);
2458238405Sjkim	&sub	($s1,$s2);
2459238405Sjkim	&cmp	($key,$acc eq "esi"? $acc:"");	# compare with inp
2460238405Sjkim	&je	(&label("enc_in_place"));
2461238405Sjkim	&align	(4);
2462238405Sjkim	&data_word(0xA4F3F689);	# rep movsb	# copy input
2463238405Sjkim	&jmp	(&label("enc_skip_in_place"));
2464238405Sjkim    &set_label("enc_in_place");
2465160814Ssimon	&lea	($key,&DWP(0,$key,$s2));
2466238405Sjkim    &set_label("enc_skip_in_place");
2467238405Sjkim	&mov	($s2,$s1);
2468238405Sjkim	&xor	($s0,$s0);
2469238405Sjkim	&align	(4);
2470238405Sjkim	&data_word(0xAAF3F689);	# rep stosb	# zero tail
2471160814Ssimon
2472238405Sjkim	&mov	($key,$_ivp);			# restore ivp
2473238405Sjkim	&mov	($acc,$s3);			# output as input
2474238405Sjkim	&mov	($s0,&DWP(0,$key));
2475238405Sjkim	&mov	($s1,&DWP(4,$key));
2476238405Sjkim	&mov	($_len,16);			# len=16
2477238405Sjkim	&jmp	(&label("slow_enc_loop_x86"));	# one more spin...
2478238405Sjkim
2479238405Sjkim#--------------------------- SLOW DECRYPT ---------------------------#
2480238405Sjkim&set_label("slow_decrypt",16);
2481238405Sjkim					if (!$x86only) {
2482238405Sjkim	&bt	($_tmp,25);		# check for SSE bit
2483238405Sjkim	&jnc	(&label("slow_dec_loop_x86"));
2484238405Sjkim
2485238405Sjkim	&set_label("slow_dec_loop_sse",4);
2486238405Sjkim		&movq	("mm0",&QWP(0,$acc));	# read input
2487238405Sjkim		&movq	("mm4",&QWP(8,$acc));
2488238405Sjkim
2489238405Sjkim		&mov	($key,$_key);
2490238405Sjkim		&call	("_sse_AES_decrypt_compact");
2491238405Sjkim
2492238405Sjkim		&mov	($acc,$_inp);		# load inp
2493238405Sjkim		&lea	($s0,$ivec);
2494238405Sjkim		&mov	($s1,$_out);		# load out
2495238405Sjkim		&mov	($s2,$_len);		# load len
2496238405Sjkim		&mov	($key,$_ivp);		# load ivp
2497238405Sjkim
2498238405Sjkim		&movq	("mm1",&QWP(0,$acc));	# re-read input
2499238405Sjkim		&movq	("mm5",&QWP(8,$acc));
2500238405Sjkim
2501238405Sjkim		&pxor	("mm0",&QWP(0,$key));	# xor iv
2502238405Sjkim		&pxor	("mm4",&QWP(8,$key));
2503238405Sjkim
2504238405Sjkim		&movq	(&QWP(0,$key),"mm1");	# copy input to iv
2505238405Sjkim		&movq	(&QWP(8,$key),"mm5");
2506238405Sjkim
2507238405Sjkim		&sub	($s2,16);		# decrease len
2508238405Sjkim		&jc	(&label("slow_dec_partial_sse"));
2509238405Sjkim
2510238405Sjkim		&movq	(&QWP(0,$s1),"mm0");	# write output
2511238405Sjkim		&movq	(&QWP(8,$s1),"mm4");
2512238405Sjkim
2513238405Sjkim		&lea	($s1,&DWP(16,$s1));	# advance out
2514238405Sjkim		&mov	($_out,$s1);		# save out
2515238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2516238405Sjkim		&mov	($_inp,$acc);		# save inp
2517238405Sjkim		&mov	($_len,$s2);		# save len
2518238405Sjkim	&jnz	(&label("slow_dec_loop_sse"));
2519238405Sjkim	&emms	();
2520238405Sjkim	&mov	("esp",$_esp);
2521238405Sjkim	&popf	();
2522238405Sjkim	&function_end_A();
2523238405Sjkim	&pushf	();			# kludge, never executed
2524238405Sjkim
2525238405Sjkim    &set_label("slow_dec_partial_sse",16);
2526238405Sjkim	&movq	(&QWP(0,$s0),"mm0");	# save output to temp
2527238405Sjkim	&movq	(&QWP(8,$s0),"mm4");
2528238405Sjkim	&emms	();
2529238405Sjkim
2530238405Sjkim	&add	($s2 eq "ecx" ? "ecx":"",16);
2531238405Sjkim	&mov	("edi",$s1);		# out
2532238405Sjkim	&mov	("esi",$s0);		# temp
2533238405Sjkim	&align	(4);
2534238405Sjkim	&data_word(0xA4F3F689);		# rep movsb # copy partial output
2535238405Sjkim
2536238405Sjkim	&mov	("esp",$_esp);
2537238405Sjkim	&popf	();
2538238405Sjkim	&function_end_A();
2539238405Sjkim	&pushf	();			# kludge, never executed
2540238405Sjkim					}
2541238405Sjkim	&set_label("slow_dec_loop_x86",16);
2542238405Sjkim		&mov	($s0,&DWP(0,$acc));	# read input
2543238405Sjkim		&mov	($s1,&DWP(4,$acc));
2544238405Sjkim		&mov	($s2,&DWP(8,$acc));
2545238405Sjkim		&mov	($s3,&DWP(12,$acc));
2546238405Sjkim
2547238405Sjkim		&lea	($key,$ivec);
2548238405Sjkim		&mov	(&DWP(0,$key),$s0);	# copy to temp
2549238405Sjkim		&mov	(&DWP(4,$key),$s1);
2550238405Sjkim		&mov	(&DWP(8,$key),$s2);
2551238405Sjkim		&mov	(&DWP(12,$key),$s3);
2552238405Sjkim
2553238405Sjkim		&mov	($key,$_key);		# load key
2554238405Sjkim		&call	("_x86_AES_decrypt_compact");
2555238405Sjkim
2556238405Sjkim		&mov	($key,$_ivp);		# load ivp
2557238405Sjkim		&mov	($acc,$_len);		# load len
2558238405Sjkim		&xor	($s0,&DWP(0,$key));	# xor iv
2559238405Sjkim		&xor	($s1,&DWP(4,$key));
2560238405Sjkim		&xor	($s2,&DWP(8,$key));
2561238405Sjkim		&xor	($s3,&DWP(12,$key));
2562238405Sjkim
2563238405Sjkim		&sub	($acc,16);
2564238405Sjkim		&jc	(&label("slow_dec_partial_x86"));
2565238405Sjkim
2566238405Sjkim		&mov	($_len,$acc);		# save len
2567238405Sjkim		&mov	($acc,$_out);		# load out
2568238405Sjkim
2569238405Sjkim		&mov	(&DWP(0,$acc),$s0);	# write output
2570238405Sjkim		&mov	(&DWP(4,$acc),$s1);
2571238405Sjkim		&mov	(&DWP(8,$acc),$s2);
2572238405Sjkim		&mov	(&DWP(12,$acc),$s3);
2573238405Sjkim
2574238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance out
2575238405Sjkim		&mov	($_out,$acc);		# save out
2576238405Sjkim
2577238405Sjkim		&lea	($acc,$ivec);
2578238405Sjkim		&mov	($s0,&DWP(0,$acc));	# read temp
2579238405Sjkim		&mov	($s1,&DWP(4,$acc));
2580238405Sjkim		&mov	($s2,&DWP(8,$acc));
2581238405Sjkim		&mov	($s3,&DWP(12,$acc));
2582238405Sjkim
2583238405Sjkim		&mov	(&DWP(0,$key),$s0);	# copy it to iv
2584238405Sjkim		&mov	(&DWP(4,$key),$s1);
2585238405Sjkim		&mov	(&DWP(8,$key),$s2);
2586238405Sjkim		&mov	(&DWP(12,$key),$s3);
2587238405Sjkim
2588238405Sjkim		&mov	($acc,$_inp);		# load inp
2589238405Sjkim		&lea	($acc,&DWP(16,$acc));	# advance inp
2590238405Sjkim		&mov	($_inp,$acc);		# save inp
2591238405Sjkim	&jnz	(&label("slow_dec_loop_x86"));
2592238405Sjkim	&mov	("esp",$_esp);
2593238405Sjkim	&popf	();
2594238405Sjkim	&function_end_A();
2595238405Sjkim	&pushf	();			# kludge, never executed
2596238405Sjkim
2597238405Sjkim    &set_label("slow_dec_partial_x86",16);
2598238405Sjkim	&lea	($acc,$ivec);
2599238405Sjkim	&mov	(&DWP(0,$acc),$s0);	# save output to temp
2600238405Sjkim	&mov	(&DWP(4,$acc),$s1);
2601238405Sjkim	&mov	(&DWP(8,$acc),$s2);
2602238405Sjkim	&mov	(&DWP(12,$acc),$s3);
2603238405Sjkim
2604238405Sjkim	&mov	($acc,$_inp);
2605238405Sjkim	&mov	($s0,&DWP(0,$acc));	# re-read input
2606238405Sjkim	&mov	($s1,&DWP(4,$acc));
2607238405Sjkim	&mov	($s2,&DWP(8,$acc));
2608238405Sjkim	&mov	($s3,&DWP(12,$acc));
2609238405Sjkim
2610238405Sjkim	&mov	(&DWP(0,$key),$s0);	# copy it to iv
2611238405Sjkim	&mov	(&DWP(4,$key),$s1);
2612238405Sjkim	&mov	(&DWP(8,$key),$s2);
2613238405Sjkim	&mov	(&DWP(12,$key),$s3);
2614238405Sjkim
2615238405Sjkim	&mov	("ecx",$_len);
2616238405Sjkim	&mov	("edi",$_out);
2617238405Sjkim	&lea	("esi",$ivec);
2618238405Sjkim	&align	(4);
2619238405Sjkim	&data_word(0xA4F3F689);		# rep movsb # copy partial output
2620238405Sjkim
2621238405Sjkim	&mov	("esp",$_esp);
2622238405Sjkim	&popf	();
2623160814Ssimon&function_end("AES_cbc_encrypt");
2624160814Ssimon}
2625160814Ssimon
2626160814Ssimon#------------------------------------------------------------------#
2627160814Ssimon
2628160814Ssimonsub enckey()
2629160814Ssimon{
2630160814Ssimon	&movz	("esi",&LB("edx"));		# rk[i]>>0
2631238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2632160814Ssimon	&movz	("esi",&HB("edx"));		# rk[i]>>8
2633238405Sjkim	&shl	("ebx",24);
2634160814Ssimon	&xor	("eax","ebx");
2635160814Ssimon
2636238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2637160814Ssimon	&shr	("edx",16);
2638160814Ssimon	&movz	("esi",&LB("edx"));		# rk[i]>>16
2639160814Ssimon	&xor	("eax","ebx");
2640160814Ssimon
2641238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2642160814Ssimon	&movz	("esi",&HB("edx"));		# rk[i]>>24
2643238405Sjkim	&shl	("ebx",8);
2644160814Ssimon	&xor	("eax","ebx");
2645160814Ssimon
2646238405Sjkim	&movz	("ebx",&BP(-128,$tbl,"esi",1));
2647238405Sjkim	&shl	("ebx",16);
2648160814Ssimon	&xor	("eax","ebx");
2649160814Ssimon
2650238405Sjkim	&xor	("eax",&DWP(1024-128,$tbl,"ecx",4));	# rcon
2651160814Ssimon}
2652160814Ssimon
2653238405Sjkim&function_begin("_x86_AES_set_encrypt_key");
2654238405Sjkim	&mov	("esi",&wparam(1));		# user supplied key
2655238405Sjkim	&mov	("edi",&wparam(3));		# private key schedule
2656160814Ssimon
2657160814Ssimon	&test	("esi",-1);
2658160814Ssimon	&jz	(&label("badpointer"));
2659160814Ssimon	&test	("edi",-1);
2660160814Ssimon	&jz	(&label("badpointer"));
2661160814Ssimon
2662160814Ssimon	&call	(&label("pic_point"));
2663160814Ssimon	&set_label("pic_point");
2664238405Sjkim	&blindpop($tbl);
2665238405Sjkim	&lea	($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
2666238405Sjkim	&lea	($tbl,&DWP(2048+128,$tbl));
2667160814Ssimon
2668238405Sjkim	# prefetch Te4
2669238405Sjkim	&mov	("eax",&DWP(0-128,$tbl));
2670238405Sjkim	&mov	("ebx",&DWP(32-128,$tbl));
2671238405Sjkim	&mov	("ecx",&DWP(64-128,$tbl));
2672238405Sjkim	&mov	("edx",&DWP(96-128,$tbl));
2673238405Sjkim	&mov	("eax",&DWP(128-128,$tbl));
2674238405Sjkim	&mov	("ebx",&DWP(160-128,$tbl));
2675238405Sjkim	&mov	("ecx",&DWP(192-128,$tbl));
2676238405Sjkim	&mov	("edx",&DWP(224-128,$tbl));
2677238405Sjkim
2678238405Sjkim	&mov	("ecx",&wparam(2));		# number of bits in key
2679160814Ssimon	&cmp	("ecx",128);
2680160814Ssimon	&je	(&label("10rounds"));
2681160814Ssimon	&cmp	("ecx",192);
2682160814Ssimon	&je	(&label("12rounds"));
2683160814Ssimon	&cmp	("ecx",256);
2684160814Ssimon	&je	(&label("14rounds"));
2685160814Ssimon	&mov	("eax",-2);			# invalid number of bits
2686160814Ssimon	&jmp	(&label("exit"));
2687160814Ssimon
2688160814Ssimon    &set_label("10rounds");
2689160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 4 dwords
2690160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
2691160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
2692160814Ssimon	&mov	("edx",&DWP(12,"esi"));
2693160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
2694160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
2695160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
2696160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
2697160814Ssimon
2698160814Ssimon	&xor	("ecx","ecx");
2699160814Ssimon	&jmp	(&label("10shortcut"));
2700160814Ssimon
2701160814Ssimon	&align	(4);
2702160814Ssimon	&set_label("10loop");
2703160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2704160814Ssimon		&mov	("edx",&DWP(12,"edi"));		# rk[3]
2705160814Ssimon	&set_label("10shortcut");
2706160814Ssimon		&enckey	();
2707160814Ssimon
2708160814Ssimon		&mov	(&DWP(16,"edi"),"eax");		# rk[4]
2709160814Ssimon		&xor	("eax",&DWP(4,"edi"));
2710160814Ssimon		&mov	(&DWP(20,"edi"),"eax");		# rk[5]
2711160814Ssimon		&xor	("eax",&DWP(8,"edi"));
2712160814Ssimon		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
2713160814Ssimon		&xor	("eax",&DWP(12,"edi"));
2714160814Ssimon		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
2715160814Ssimon		&inc	("ecx");
2716160814Ssimon		&add	("edi",16);
2717160814Ssimon		&cmp	("ecx",10);
2718160814Ssimon	&jl	(&label("10loop"));
2719160814Ssimon
2720160814Ssimon	&mov	(&DWP(80,"edi"),10);		# setup number of rounds
2721160814Ssimon	&xor	("eax","eax");
2722160814Ssimon	&jmp	(&label("exit"));
2723160814Ssimon
2724160814Ssimon    &set_label("12rounds");
2725160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 6 dwords
2726160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
2727160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
2728160814Ssimon	&mov	("edx",&DWP(12,"esi"));
2729160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
2730160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
2731160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
2732160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
2733160814Ssimon	&mov	("ecx",&DWP(16,"esi"));
2734160814Ssimon	&mov	("edx",&DWP(20,"esi"));
2735160814Ssimon	&mov	(&DWP(16,"edi"),"ecx");
2736160814Ssimon	&mov	(&DWP(20,"edi"),"edx");
2737160814Ssimon
2738160814Ssimon	&xor	("ecx","ecx");
2739160814Ssimon	&jmp	(&label("12shortcut"));
2740160814Ssimon
2741160814Ssimon	&align	(4);
2742160814Ssimon	&set_label("12loop");
2743160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2744160814Ssimon		&mov	("edx",&DWP(20,"edi"));		# rk[5]
2745160814Ssimon	&set_label("12shortcut");
2746160814Ssimon		&enckey	();
2747160814Ssimon
2748160814Ssimon		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
2749160814Ssimon		&xor	("eax",&DWP(4,"edi"));
2750160814Ssimon		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
2751160814Ssimon		&xor	("eax",&DWP(8,"edi"));
2752160814Ssimon		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
2753160814Ssimon		&xor	("eax",&DWP(12,"edi"));
2754160814Ssimon		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
2755160814Ssimon
2756160814Ssimon		&cmp	("ecx",7);
2757160814Ssimon		&je	(&label("12break"));
2758160814Ssimon		&inc	("ecx");
2759160814Ssimon
2760160814Ssimon		&xor	("eax",&DWP(16,"edi"));
2761160814Ssimon		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
2762160814Ssimon		&xor	("eax",&DWP(20,"edi"));
2763160814Ssimon		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
2764160814Ssimon
2765160814Ssimon		&add	("edi",24);
2766160814Ssimon	&jmp	(&label("12loop"));
2767160814Ssimon
2768160814Ssimon	&set_label("12break");
2769160814Ssimon	&mov	(&DWP(72,"edi"),12);		# setup number of rounds
2770160814Ssimon	&xor	("eax","eax");
2771160814Ssimon	&jmp	(&label("exit"));
2772160814Ssimon
2773160814Ssimon    &set_label("14rounds");
2774160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 8 dwords
2775160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
2776160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
2777160814Ssimon	&mov	("edx",&DWP(12,"esi"));
2778160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
2779160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
2780160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
2781160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
2782160814Ssimon	&mov	("eax",&DWP(16,"esi"));
2783160814Ssimon	&mov	("ebx",&DWP(20,"esi"));
2784160814Ssimon	&mov	("ecx",&DWP(24,"esi"));
2785160814Ssimon	&mov	("edx",&DWP(28,"esi"));
2786160814Ssimon	&mov	(&DWP(16,"edi"),"eax");
2787160814Ssimon	&mov	(&DWP(20,"edi"),"ebx");
2788160814Ssimon	&mov	(&DWP(24,"edi"),"ecx");
2789160814Ssimon	&mov	(&DWP(28,"edi"),"edx");
2790160814Ssimon
2791160814Ssimon	&xor	("ecx","ecx");
2792160814Ssimon	&jmp	(&label("14shortcut"));
2793160814Ssimon
2794160814Ssimon	&align	(4);
2795160814Ssimon	&set_label("14loop");
2796160814Ssimon		&mov	("edx",&DWP(28,"edi"));		# rk[7]
2797160814Ssimon	&set_label("14shortcut");
2798160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
2799160814Ssimon
2800160814Ssimon		&enckey	();
2801160814Ssimon
2802160814Ssimon		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
2803160814Ssimon		&xor	("eax",&DWP(4,"edi"));
2804160814Ssimon		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
2805160814Ssimon		&xor	("eax",&DWP(8,"edi"));
2806160814Ssimon		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
2807160814Ssimon		&xor	("eax",&DWP(12,"edi"));
2808160814Ssimon		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
2809160814Ssimon
2810160814Ssimon		&cmp	("ecx",6);
2811160814Ssimon		&je	(&label("14break"));
2812160814Ssimon		&inc	("ecx");
2813160814Ssimon
2814160814Ssimon		&mov	("edx","eax");
2815160814Ssimon		&mov	("eax",&DWP(16,"edi"));		# rk[4]
2816160814Ssimon		&movz	("esi",&LB("edx"));		# rk[11]>>0
2817238405Sjkim		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2818160814Ssimon		&movz	("esi",&HB("edx"));		# rk[11]>>8
2819160814Ssimon		&xor	("eax","ebx");
2820160814Ssimon
2821238405Sjkim		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2822160814Ssimon		&shr	("edx",16);
2823238405Sjkim		&shl	("ebx",8);
2824160814Ssimon		&movz	("esi",&LB("edx"));		# rk[11]>>16
2825160814Ssimon		&xor	("eax","ebx");
2826160814Ssimon
2827238405Sjkim		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2828160814Ssimon		&movz	("esi",&HB("edx"));		# rk[11]>>24
2829238405Sjkim		&shl	("ebx",16);
2830160814Ssimon		&xor	("eax","ebx");
2831160814Ssimon
2832238405Sjkim		&movz	("ebx",&BP(-128,$tbl,"esi",1));
2833238405Sjkim		&shl	("ebx",24);
2834160814Ssimon		&xor	("eax","ebx");
2835160814Ssimon
2836160814Ssimon		&mov	(&DWP(48,"edi"),"eax");		# rk[12]
2837160814Ssimon		&xor	("eax",&DWP(20,"edi"));
2838160814Ssimon		&mov	(&DWP(52,"edi"),"eax");		# rk[13]
2839160814Ssimon		&xor	("eax",&DWP(24,"edi"));
2840160814Ssimon		&mov	(&DWP(56,"edi"),"eax");		# rk[14]
2841160814Ssimon		&xor	("eax",&DWP(28,"edi"));
2842160814Ssimon		&mov	(&DWP(60,"edi"),"eax");		# rk[15]
2843160814Ssimon
2844160814Ssimon		&add	("edi",32);
2845160814Ssimon	&jmp	(&label("14loop"));
2846160814Ssimon
2847160814Ssimon	&set_label("14break");
2848160814Ssimon	&mov	(&DWP(48,"edi"),14);		# setup number of rounds
2849160814Ssimon	&xor	("eax","eax");
2850160814Ssimon	&jmp	(&label("exit"));
2851160814Ssimon
2852160814Ssimon    &set_label("badpointer");
2853160814Ssimon	&mov	("eax",-1);
2854160814Ssimon    &set_label("exit");
2855238405Sjkim&function_end("_x86_AES_set_encrypt_key");
2856160814Ssimon
2857238405Sjkim# int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
2858238405Sjkim#                        AES_KEY *key)
2859238405Sjkim&function_begin_B("private_AES_set_encrypt_key");
2860238405Sjkim	&call	("_x86_AES_set_encrypt_key");
2861238405Sjkim	&ret	();
2862238405Sjkim&function_end_B("private_AES_set_encrypt_key");
2863238405Sjkim
2864160814Ssimonsub deckey()
2865238405Sjkim{ my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_;
2866238405Sjkim  my $tmp = $tbl;
2867160814Ssimon
2868238405Sjkim	&mov	($acc,$tp1);
2869238405Sjkim	&and	($acc,0x80808080);
2870238405Sjkim	&mov	($tmp,$acc);
2871238405Sjkim	&shr	($tmp,7);
2872238405Sjkim	&lea	($tp2,&DWP(0,$tp1,$tp1));
2873238405Sjkim	&sub	($acc,$tmp);
2874238405Sjkim	&and	($tp2,0xfefefefe);
2875238405Sjkim	&and	($acc,0x1b1b1b1b);
2876238405Sjkim	&xor	($acc,$tp2);
2877238405Sjkim	&mov	($tp2,$acc);
2878238405Sjkim
2879238405Sjkim	&and	($acc,0x80808080);
2880238405Sjkim	&mov	($tmp,$acc);
2881238405Sjkim	&shr	($tmp,7);
2882238405Sjkim	&lea	($tp4,&DWP(0,$tp2,$tp2));
2883238405Sjkim	&sub	($acc,$tmp);
2884238405Sjkim	&and	($tp4,0xfefefefe);
2885238405Sjkim	&and	($acc,0x1b1b1b1b);
2886238405Sjkim	 &xor	($tp2,$tp1);	# tp2^tp1
2887238405Sjkim	&xor	($acc,$tp4);
2888238405Sjkim	&mov	($tp4,$acc);
2889238405Sjkim
2890238405Sjkim	&and	($acc,0x80808080);
2891238405Sjkim	&mov	($tmp,$acc);
2892238405Sjkim	&shr	($tmp,7);
2893238405Sjkim	&lea	($tp8,&DWP(0,$tp4,$tp4));
2894238405Sjkim	 &xor	($tp4,$tp1);	# tp4^tp1
2895238405Sjkim	&sub	($acc,$tmp);
2896238405Sjkim	&and	($tp8,0xfefefefe);
2897238405Sjkim	&and	($acc,0x1b1b1b1b);
2898238405Sjkim	 &rotl	($tp1,8);	# = ROTATE(tp1,8)
2899238405Sjkim	&xor	($tp8,$acc);
2900238405Sjkim
2901238405Sjkim	&mov	($tmp,&DWP(4*($i+1),$key));	# modulo-scheduled load
2902238405Sjkim
2903238405Sjkim	&xor	($tp1,$tp2);
2904238405Sjkim	&xor	($tp2,$tp8);
2905238405Sjkim	&xor	($tp1,$tp4);
2906238405Sjkim	&rotl	($tp2,24);
2907238405Sjkim	&xor	($tp4,$tp8);
2908238405Sjkim	&xor	($tp1,$tp8);	# ^= tp8^(tp4^tp1)^(tp2^tp1)
2909238405Sjkim	&rotl	($tp4,16);
2910238405Sjkim	&xor	($tp1,$tp2);	# ^= ROTATE(tp8^tp2^tp1,24)
2911238405Sjkim	&rotl	($tp8,8);
2912238405Sjkim	&xor	($tp1,$tp4);	# ^= ROTATE(tp8^tp4^tp1,16)
2913238405Sjkim	&mov	($tp2,$tmp);
2914238405Sjkim	&xor	($tp1,$tp8);	# ^= ROTATE(tp8,8)
2915238405Sjkim
2916238405Sjkim	&mov	(&DWP(4*$i,$key),$tp1);
2917160814Ssimon}
2918160814Ssimon
2919238405Sjkim# int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
2920160814Ssimon#                        AES_KEY *key)
2921238405Sjkim&function_begin_B("private_AES_set_decrypt_key");
2922238405Sjkim	&call	("_x86_AES_set_encrypt_key");
2923160814Ssimon	&cmp	("eax",0);
2924160814Ssimon	&je	(&label("proceed"));
2925160814Ssimon	&ret	();
2926160814Ssimon
2927160814Ssimon    &set_label("proceed");
2928160814Ssimon	&push	("ebp");
2929160814Ssimon	&push	("ebx");
2930160814Ssimon	&push	("esi");
2931160814Ssimon	&push	("edi");
2932160814Ssimon
2933160814Ssimon	&mov	("esi",&wparam(2));
2934160814Ssimon	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
2935160814Ssimon	&lea	("ecx",&DWP(0,"","ecx",4));
2936160814Ssimon	&lea	("edi",&DWP(0,"esi","ecx",4));	# pointer to last chunk
2937160814Ssimon
2938238405Sjkim	&set_label("invert",4);			# invert order of chunks
2939160814Ssimon		&mov	("eax",&DWP(0,"esi"));
2940160814Ssimon		&mov	("ebx",&DWP(4,"esi"));
2941160814Ssimon		&mov	("ecx",&DWP(0,"edi"));
2942160814Ssimon		&mov	("edx",&DWP(4,"edi"));
2943160814Ssimon		&mov	(&DWP(0,"edi"),"eax");
2944160814Ssimon		&mov	(&DWP(4,"edi"),"ebx");
2945160814Ssimon		&mov	(&DWP(0,"esi"),"ecx");
2946160814Ssimon		&mov	(&DWP(4,"esi"),"edx");
2947160814Ssimon		&mov	("eax",&DWP(8,"esi"));
2948160814Ssimon		&mov	("ebx",&DWP(12,"esi"));
2949160814Ssimon		&mov	("ecx",&DWP(8,"edi"));
2950160814Ssimon		&mov	("edx",&DWP(12,"edi"));
2951160814Ssimon		&mov	(&DWP(8,"edi"),"eax");
2952160814Ssimon		&mov	(&DWP(12,"edi"),"ebx");
2953160814Ssimon		&mov	(&DWP(8,"esi"),"ecx");
2954160814Ssimon		&mov	(&DWP(12,"esi"),"edx");
2955160814Ssimon		&add	("esi",16);
2956160814Ssimon		&sub	("edi",16);
2957160814Ssimon		&cmp	("esi","edi");
2958160814Ssimon	&jne	(&label("invert"));
2959160814Ssimon
2960238405Sjkim	&mov	($key,&wparam(2));
2961238405Sjkim	&mov	($acc,&DWP(240,$key));		# pull number of rounds
2962238405Sjkim	&lea	($acc,&DWP(-2,$acc,$acc));
2963238405Sjkim	&lea	($acc,&DWP(0,$key,$acc,8));
2964238405Sjkim	&mov	(&wparam(2),$acc);
2965160814Ssimon
2966238405Sjkim	&mov	($s0,&DWP(16,$key));		# modulo-scheduled load
2967238405Sjkim	&set_label("permute",4);		# permute the key schedule
2968238405Sjkim		&add	($key,16);
2969238405Sjkim		&deckey	(0,$key,$s0,$s1,$s2,$s3);
2970238405Sjkim		&deckey	(1,$key,$s1,$s2,$s3,$s0);
2971238405Sjkim		&deckey	(2,$key,$s2,$s3,$s0,$s1);
2972238405Sjkim		&deckey	(3,$key,$s3,$s0,$s1,$s2);
2973238405Sjkim		&cmp	($key,&wparam(2));
2974238405Sjkim	&jb	(&label("permute"));
2975160814Ssimon
2976160814Ssimon	&xor	("eax","eax");			# return success
2977238405Sjkim&function_end("private_AES_set_decrypt_key");
2978238405Sjkim&asciz("AES for x86, CRYPTOGAMS by <appro\@openssl.org>");
2979160814Ssimon
2980160814Ssimon&asm_finish();
2981