1160814Ssimon#!/usr/bin/env perl
2160814Ssimon#
3160814Ssimon# ====================================================================
4160814Ssimon# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5160814Ssimon# project. Rights for redistribution and usage in source and binary
6160814Ssimon# forms are granted according to the OpenSSL license.
7160814Ssimon# ====================================================================
8160814Ssimon#
9162911Ssimon# Version 3.6.
10160814Ssimon#
11160814Ssimon# You might fail to appreciate this module performance from the first
12160814Ssimon# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
13160814Ssimon# to be *the* best Intel C compiler without -KPIC, performance appears
14160814Ssimon# to be virtually identical... But try to re-configure with shared
15160814Ssimon# library support... Aha! Intel compiler "suddenly" lags behind by 30%
16160814Ssimon# [on P4, more on others]:-) And if compared to position-independent
17160814Ssimon# code generated by GNU C, this code performs *more* than *twice* as
18160814Ssimon# fast! Yes, all this buzz about PIC means that unlike other hand-
19160814Ssimon# coded implementations, this one was explicitly designed to be safe
20160814Ssimon# to use even in shared library context... This also means that this
21160814Ssimon# code isn't necessarily absolutely fastest "ever," because in order
22160814Ssimon# to achieve position independence an extra register has to be
23160814Ssimon# off-loaded to stack, which affects the benchmark result.
24160814Ssimon#
25160814Ssimon# Special note about instruction choice. Do you recall RC4_INT code
26160814Ssimon# performing poorly on P4? It might be the time to figure out why.
27160814Ssimon# RC4_INT code implies effective address calculations in base+offset*4
28160814Ssimon# form. Trouble is that it seems that offset scaling turned to be
29160814Ssimon# critical path... At least eliminating scaling resulted in 2.8x RC4
30160814Ssimon# performance improvement [as you might recall]. As AES code is hungry
31160814Ssimon# for scaling too, I [try to] avoid the latter by favoring off-by-2
32160814Ssimon# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
33160814Ssimon#
34160814Ssimon# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
35160814Ssimon# void. Performance improvement with off-by-2 shifts was observed on
36160814Ssimon# intermediate implementation, which was spilling yet another register
37160814Ssimon# to stack... Final offset*4 code below runs just a tad faster on P4,
38160814Ssimon# but exhibits up to 10% improvement on other cores.
39160814Ssimon#
40160814Ssimon# Second version is "monolithic" replacement for aes_core.c, which in
41160814Ssimon# addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
42160814Ssimon# This made it possible to implement little-endian variant of the
43160814Ssimon# algorithm without modifying the base C code. Motivating factor for
44160814Ssimon# the undertaken effort was that it appeared that in tight IA-32
45160814Ssimon# register window little-endian flavor could achieve slightly higher
46160814Ssimon# Instruction Level Parallelism, and it indeed resulted in up to 15%
47160814Ssimon# better performance on most recent �-archs...
48160814Ssimon#
49160814Ssimon# Third version adds AES_cbc_encrypt implementation, which resulted in
50160814Ssimon# up to 40% performance imrovement of CBC benchmark results. 40% was
51160814Ssimon# observed on P4 core, where "overall" imrovement coefficient, i.e. if
52160814Ssimon# compared to PIC generated by GCC and in CBC mode, was observed to be
53160814Ssimon# as large as 4x:-) CBC performance is virtually identical to ECB now
54160814Ssimon# and on some platforms even better, e.g. 17.6 "small" cycles/byte on
55160814Ssimon# Opteron, because certain function prologues and epilogues are
56160814Ssimon# effectively taken out of the loop...
57160814Ssimon#
58160814Ssimon# Version 3.2 implements compressed tables and prefetch of these tables
59160814Ssimon# in CBC[!] mode. Former means that 3/4 of table references are now
60160814Ssimon# misaligned, which unfortunately has negative impact on elder IA-32
61160814Ssimon# implementations, Pentium suffered 30% penalty, PIII - 10%.
62160814Ssimon#
63160814Ssimon# Version 3.3 avoids L1 cache aliasing between stack frame and
64160814Ssimon# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
65160814Ssimon# latter is achieved by copying the key schedule to controlled place in
66160814Ssimon# stack. This unfortunately has rather strong impact on small block CBC
67160814Ssimon# performance, ~2x deterioration on 16-byte block if compared to 3.3.
68160814Ssimon#
69162911Ssimon# Version 3.5 checks if there is L1 cache aliasing between user-supplied
70162911Ssimon# key schedule and S-boxes and abstains from copying the former if
71162911Ssimon# there is no. This allows end-user to consciously retain small block
72162911Ssimon# performance by aligning key schedule in specific manner.
73162911Ssimon#
74162911Ssimon# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
75162911Ssimon#
76160814Ssimon# Current ECB performance numbers for 128-bit key in CPU cycles per
77160814Ssimon# processed byte [measure commonly used by AES benchmarkers] are:
78160814Ssimon#
79160814Ssimon#		small footprint		fully unrolled
80160814Ssimon# P4		24			22
81160814Ssimon# AMD K8	20			19
82160814Ssimon# PIII		25			23
83160814Ssimon# Pentium	81			78
84160814Ssimon
85160814Ssimonpush(@INC,"perlasm","../../perlasm");
86160814Ssimonrequire "x86asm.pl";
87160814Ssimon
88160814Ssimon&asm_init($ARGV[0],"aes-586.pl",$ARGV[$#ARGV] eq "386");
89160814Ssimon
90160814Ssimon$s0="eax";
91160814Ssimon$s1="ebx";
92160814Ssimon$s2="ecx";
93160814Ssimon$s3="edx";
94160814Ssimon$key="edi";
95160814Ssimon$acc="esi";
96160814Ssimon
97160814Ssimon$compromise=0;		# $compromise=128 abstains from copying key
98160814Ssimon			# schedule to stack when encrypting inputs
99160814Ssimon			# shorter than 128 bytes at the cost of
100160814Ssimon			# risksing aliasing with S-boxes. In return
101160814Ssimon			# you get way better, up to +70%, small block
102160814Ssimon			# performance.
103160814Ssimon$small_footprint=1;	# $small_footprint=1 code is ~5% slower [on
104160814Ssimon			# recent �-archs], but ~5 times smaller!
105160814Ssimon			# I favor compact code to minimize cache
106160814Ssimon			# contention and in hope to "collect" 5% back
107160814Ssimon			# in real-life applications...
108160814Ssimon$vertical_spin=0;	# shift "verticaly" defaults to 0, because of
109160814Ssimon			# its proof-of-concept status...
110160814Ssimon
111160814Ssimon# Note that there is no decvert(), as well as last encryption round is
112160814Ssimon# performed with "horizontal" shifts. This is because this "vertical"
113160814Ssimon# implementation [one which groups shifts on a given $s[i] to form a
114160814Ssimon# "column," unlike "horizontal" one, which groups shifts on different
115160814Ssimon# $s[i] to form a "row"] is work in progress. It was observed to run
116160814Ssimon# few percents faster on Intel cores, but not AMD. On AMD K8 core it's
117160814Ssimon# whole 12% slower:-( So we face a trade-off... Shall it be resolved
118160814Ssimon# some day? Till then the code is considered experimental and by
119160814Ssimon# default remains dormant...
120160814Ssimon
121160814Ssimonsub encvert()
122160814Ssimon{ my ($te,@s) = @_;
123160814Ssimon  my $v0 = $acc, $v1 = $key;
124160814Ssimon
125160814Ssimon	&mov	($v0,$s[3]);				# copy s3
126160814Ssimon	&mov	(&DWP(4,"esp"),$s[2]);			# save s2
127160814Ssimon	&mov	($v1,$s[0]);				# copy s0
128160814Ssimon	&mov	(&DWP(8,"esp"),$s[1]);			# save s1
129160814Ssimon
130160814Ssimon	&movz	($s[2],&HB($s[0]));
131160814Ssimon	&and	($s[0],0xFF);
132160814Ssimon	&mov	($s[0],&DWP(0,$te,$s[0],8));		# s0>>0
133160814Ssimon	&shr	($v1,16);
134160814Ssimon	&mov	($s[3],&DWP(3,$te,$s[2],8));		# s0>>8
135160814Ssimon	&movz	($s[1],&HB($v1));
136160814Ssimon	&and	($v1,0xFF);
137160814Ssimon	&mov	($s[2],&DWP(2,$te,$v1,8));		# s0>>16
138160814Ssimon	 &mov	($v1,$v0);
139160814Ssimon	&mov	($s[1],&DWP(1,$te,$s[1],8));		# s0>>24
140160814Ssimon
141160814Ssimon	&and	($v0,0xFF);
142160814Ssimon	&xor	($s[3],&DWP(0,$te,$v0,8));		# s3>>0
143160814Ssimon	&movz	($v0,&HB($v1));
144160814Ssimon	&shr	($v1,16);
145160814Ssimon	&xor	($s[2],&DWP(3,$te,$v0,8));		# s3>>8
146160814Ssimon	&movz	($v0,&HB($v1));
147160814Ssimon	&and	($v1,0xFF);
148160814Ssimon	&xor	($s[1],&DWP(2,$te,$v1,8));		# s3>>16
149160814Ssimon	 &mov	($v1,&DWP(4,"esp"));			# restore s2
150160814Ssimon	&xor	($s[0],&DWP(1,$te,$v0,8));		# s3>>24
151160814Ssimon
152160814Ssimon	&mov	($v0,$v1);
153160814Ssimon	&and	($v1,0xFF);
154160814Ssimon	&xor	($s[2],&DWP(0,$te,$v1,8));		# s2>>0
155160814Ssimon	&movz	($v1,&HB($v0));
156160814Ssimon	&shr	($v0,16);
157160814Ssimon	&xor	($s[1],&DWP(3,$te,$v1,8));		# s2>>8
158160814Ssimon	&movz	($v1,&HB($v0));
159160814Ssimon	&and	($v0,0xFF);
160160814Ssimon	&xor	($s[0],&DWP(2,$te,$v0,8));		# s2>>16
161160814Ssimon	 &mov	($v0,&DWP(8,"esp"));			# restore s1
162160814Ssimon	&xor	($s[3],&DWP(1,$te,$v1,8));		# s2>>24
163160814Ssimon
164160814Ssimon	&mov	($v1,$v0);
165160814Ssimon	&and	($v0,0xFF);
166160814Ssimon	&xor	($s[1],&DWP(0,$te,$v0,8));		# s1>>0
167160814Ssimon	&movz	($v0,&HB($v1));
168160814Ssimon	&shr	($v1,16);
169160814Ssimon	&xor	($s[0],&DWP(3,$te,$v0,8));		# s1>>8
170160814Ssimon	&movz	($v0,&HB($v1));
171160814Ssimon	&and	($v1,0xFF);
172160814Ssimon	&xor	($s[3],&DWP(2,$te,$v1,8));		# s1>>16
173160814Ssimon	 &mov	($key,&DWP(12,"esp"));			# reincarnate v1 as key
174160814Ssimon	&xor	($s[2],&DWP(1,$te,$v0,8));		# s1>>24
175160814Ssimon}
176160814Ssimon
177160814Ssimonsub encstep()
178160814Ssimon{ my ($i,$te,@s) = @_;
179160814Ssimon  my $tmp = $key;
180160814Ssimon  my $out = $i==3?$s[0]:$acc;
181160814Ssimon
182160814Ssimon	# lines marked with #%e?x[i] denote "reordered" instructions...
183160814Ssimon	if ($i==3)  {	&mov	($key,&DWP(12,"esp"));		}##%edx
184160814Ssimon	else        {	&mov	($out,$s[0]);
185160814Ssimon			&and	($out,0xFF);			}
186160814Ssimon	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
187160814Ssimon	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
188160814Ssimon			&mov	($out,&DWP(0,$te,$out,8));
189160814Ssimon
190160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}##%eax
191160814Ssimon			&movz	($tmp,&HB($s[1]));
192160814Ssimon			&xor	($out,&DWP(3,$te,$tmp,8));
193160814Ssimon
194160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],&DWP(4,"esp"));	}##%ebx
195160814Ssimon	else        {	&mov	($tmp,$s[2]);
196160814Ssimon			&shr	($tmp,16);			}
197160814Ssimon	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
198160814Ssimon			&and	($tmp,0xFF);
199160814Ssimon			&xor	($out,&DWP(2,$te,$tmp,8));
200160814Ssimon
201160814Ssimon	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}##%ecx
202160814Ssimon	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
203160814Ssimon	else        {	&mov	($tmp,$s[3]);
204160814Ssimon			&shr	($tmp,24)			}
205160814Ssimon			&xor	($out,&DWP(1,$te,$tmp,8));
206160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
207160814Ssimon	if ($i==3)  {	&mov	($s[3],$acc);			}
208160814Ssimon			&comment();
209160814Ssimon}
210160814Ssimon
211160814Ssimonsub enclast()
212160814Ssimon{ my ($i,$te,@s)=@_;
213160814Ssimon  my $tmp = $key;
214160814Ssimon  my $out = $i==3?$s[0]:$acc;
215160814Ssimon
216160814Ssimon	if ($i==3)  {	&mov	($key,&DWP(12,"esp"));		}##%edx
217160814Ssimon	else        {	&mov	($out,$s[0]);			}
218160814Ssimon			&and	($out,0xFF);
219160814Ssimon	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
220160814Ssimon	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
221160814Ssimon			&mov	($out,&DWP(2,$te,$out,8));
222160814Ssimon			&and	($out,0x000000ff);
223160814Ssimon
224160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}##%eax
225160814Ssimon			&movz	($tmp,&HB($s[1]));
226160814Ssimon			&mov	($tmp,&DWP(0,$te,$tmp,8));
227160814Ssimon			&and	($tmp,0x0000ff00);
228160814Ssimon			&xor	($out,$tmp);
229160814Ssimon
230160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],&DWP(4,"esp"));	}##%ebx
231160814Ssimon	else        {	mov	($tmp,$s[2]);
232160814Ssimon			&shr	($tmp,16);			}
233160814Ssimon	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
234160814Ssimon			&and	($tmp,0xFF);
235160814Ssimon			&mov	($tmp,&DWP(0,$te,$tmp,8));
236160814Ssimon			&and	($tmp,0x00ff0000);
237160814Ssimon			&xor	($out,$tmp);
238160814Ssimon
239160814Ssimon	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}##%ecx
240160814Ssimon	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
241160814Ssimon	else        {	&mov	($tmp,$s[3]);
242160814Ssimon			&shr	($tmp,24);			}
243160814Ssimon			&mov	($tmp,&DWP(2,$te,$tmp,8));
244160814Ssimon			&and	($tmp,0xff000000);
245160814Ssimon			&xor	($out,$tmp);
246160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
247160814Ssimon	if ($i==3)  {	&mov	($s[3],$acc);			}
248160814Ssimon}
249160814Ssimon
250160814Ssimonsub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
251160814Ssimon
252160814Ssimon&public_label("AES_Te");
253160814Ssimon&function_begin_B("_x86_AES_encrypt");
254160814Ssimon	if ($vertical_spin) {
255160814Ssimon		# I need high parts of volatile registers to be accessible...
256160814Ssimon		&exch	($s1="edi",$key="ebx");
257160814Ssimon		&mov	($s2="esi",$acc="ecx");
258160814Ssimon	}
259160814Ssimon
260160814Ssimon	# note that caller is expected to allocate stack frame for me!
261160814Ssimon	&mov	(&DWP(12,"esp"),$key);		# save key
262160814Ssimon
263160814Ssimon	&xor	($s0,&DWP(0,$key));		# xor with key
264160814Ssimon	&xor	($s1,&DWP(4,$key));
265160814Ssimon	&xor	($s2,&DWP(8,$key));
266160814Ssimon	&xor	($s3,&DWP(12,$key));
267160814Ssimon
268160814Ssimon	&mov	($acc,&DWP(240,$key));		# load key->rounds
269160814Ssimon
270160814Ssimon	if ($small_footprint) {
271160814Ssimon	    &lea	($acc,&DWP(-2,$acc,$acc));
272160814Ssimon	    &lea	($acc,&DWP(0,$key,$acc,8));
273160814Ssimon	    &mov	(&DWP(16,"esp"),$acc);	# end of key schedule
274160814Ssimon	    &align	(4);
275160814Ssimon	    &set_label("loop");
276160814Ssimon		if ($vertical_spin) {
277160814Ssimon		    &encvert("ebp",$s0,$s1,$s2,$s3);
278160814Ssimon		} else {
279160814Ssimon		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
280160814Ssimon		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
281160814Ssimon		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
282160814Ssimon		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
283160814Ssimon		}
284160814Ssimon		&add	($key,16);		# advance rd_key
285160814Ssimon		&xor	($s0,&DWP(0,$key));
286160814Ssimon		&xor	($s1,&DWP(4,$key));
287160814Ssimon		&xor	($s2,&DWP(8,$key));
288160814Ssimon		&xor	($s3,&DWP(12,$key));
289160814Ssimon	    &cmp	($key,&DWP(16,"esp"));
290160814Ssimon	    &mov	(&DWP(12,"esp"),$key);
291160814Ssimon	    &jb		(&label("loop"));
292160814Ssimon	}
293160814Ssimon	else {
294160814Ssimon	    &cmp	($acc,10);
295160814Ssimon	    &jle	(&label("10rounds"));
296160814Ssimon	    &cmp	($acc,12);
297160814Ssimon	    &jle	(&label("12rounds"));
298160814Ssimon
299160814Ssimon	&set_label("14rounds");
300160814Ssimon	    for ($i=1;$i<3;$i++) {
301160814Ssimon		if ($vertical_spin) {
302160814Ssimon		    &encvert("ebp",$s0,$s1,$s2,$s3);
303160814Ssimon		} else {
304160814Ssimon		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
305160814Ssimon		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
306160814Ssimon		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
307160814Ssimon		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
308160814Ssimon		}
309160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
310160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
311160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
312160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
313160814Ssimon	    }
314160814Ssimon	    &add	($key,32);
315160814Ssimon	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
316160814Ssimon	&set_label("12rounds");
317160814Ssimon	    for ($i=1;$i<3;$i++) {
318160814Ssimon		if ($vertical_spin) {
319160814Ssimon		    &encvert("ebp",$s0,$s1,$s2,$s3);
320160814Ssimon		} else {
321160814Ssimon		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
322160814Ssimon		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
323160814Ssimon		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
324160814Ssimon		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
325160814Ssimon		}
326160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
327160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
328160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
329160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
330160814Ssimon	    }
331160814Ssimon	    &add	($key,32);
332160814Ssimon	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
333160814Ssimon	&set_label("10rounds");
334160814Ssimon	    for ($i=1;$i<10;$i++) {
335160814Ssimon		if ($vertical_spin) {
336160814Ssimon		    &encvert("ebp",$s0,$s1,$s2,$s3);
337160814Ssimon		} else {
338160814Ssimon		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
339160814Ssimon		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
340160814Ssimon		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
341160814Ssimon		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
342160814Ssimon		}
343160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
344160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
345160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
346160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
347160814Ssimon	    }
348160814Ssimon	}
349160814Ssimon
350160814Ssimon	if ($vertical_spin) {
351160814Ssimon	    # "reincarnate" some registers for "horizontal" spin...
352160814Ssimon	    &mov	($s1="ebx",$key="edi");
353160814Ssimon	    &mov	($s2="ecx",$acc="esi");
354160814Ssimon	}
355160814Ssimon	&enclast(0,"ebp",$s0,$s1,$s2,$s3);
356160814Ssimon	&enclast(1,"ebp",$s1,$s2,$s3,$s0);
357160814Ssimon	&enclast(2,"ebp",$s2,$s3,$s0,$s1);
358160814Ssimon	&enclast(3,"ebp",$s3,$s0,$s1,$s2);
359160814Ssimon
360160814Ssimon	&add	($key,$small_footprint?16:160);
361160814Ssimon	&xor	($s0,&DWP(0,$key));
362160814Ssimon	&xor	($s1,&DWP(4,$key));
363160814Ssimon	&xor	($s2,&DWP(8,$key));
364160814Ssimon	&xor	($s3,&DWP(12,$key));
365160814Ssimon
366160814Ssimon	&ret	();
367160814Ssimon
368160814Ssimon&set_label("AES_Te",64);	# Yes! I keep it in the code segment!
369160814Ssimon	&_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
370160814Ssimon	&_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
371160814Ssimon	&_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
372160814Ssimon	&_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
373160814Ssimon	&_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
374160814Ssimon	&_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
375160814Ssimon	&_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
376160814Ssimon	&_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
377160814Ssimon	&_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
378160814Ssimon	&_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
379160814Ssimon	&_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
380160814Ssimon	&_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
381160814Ssimon	&_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
382160814Ssimon	&_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
383160814Ssimon	&_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
384160814Ssimon	&_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
385160814Ssimon	&_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
386160814Ssimon	&_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
387160814Ssimon	&_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
388160814Ssimon	&_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
389160814Ssimon	&_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
390160814Ssimon	&_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
391160814Ssimon	&_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
392160814Ssimon	&_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
393160814Ssimon	&_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
394160814Ssimon	&_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
395160814Ssimon	&_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
396160814Ssimon	&_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
397160814Ssimon	&_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
398160814Ssimon	&_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
399160814Ssimon	&_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
400160814Ssimon	&_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
401160814Ssimon	&_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
402160814Ssimon	&_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
403160814Ssimon	&_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
404160814Ssimon	&_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
405160814Ssimon	&_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
406160814Ssimon	&_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
407160814Ssimon	&_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
408160814Ssimon	&_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
409160814Ssimon	&_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
410160814Ssimon	&_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
411160814Ssimon	&_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
412160814Ssimon	&_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
413160814Ssimon	&_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
414160814Ssimon	&_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
415160814Ssimon	&_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
416160814Ssimon	&_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
417160814Ssimon	&_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
418160814Ssimon	&_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
419160814Ssimon	&_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
420160814Ssimon	&_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
421160814Ssimon	&_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
422160814Ssimon	&_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
423160814Ssimon	&_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
424160814Ssimon	&_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
425160814Ssimon	&_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
426160814Ssimon	&_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
427160814Ssimon	&_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
428160814Ssimon	&_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
429160814Ssimon	&_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
430160814Ssimon	&_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
431160814Ssimon	&_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
432160814Ssimon	&_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
433160814Ssimon#rcon:
434160814Ssimon	&data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
435160814Ssimon	&data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
436160814Ssimon	&data_word(0x0000001b, 0x00000036, 0, 0, 0, 0, 0, 0);
437160814Ssimon&function_end_B("_x86_AES_encrypt");
438160814Ssimon
439160814Ssimon# void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
440160814Ssimon&public_label("AES_Te");
441160814Ssimon&function_begin("AES_encrypt");
442160814Ssimon	&mov	($acc,&wparam(0));		# load inp
443160814Ssimon	&mov	($key,&wparam(2));		# load key
444160814Ssimon
445160814Ssimon	&mov	($s0,"esp");
446160814Ssimon	&sub	("esp",24);
447160814Ssimon	&and	("esp",-64);
448160814Ssimon	&add	("esp",4);
449160814Ssimon	&mov	(&DWP(16,"esp"),$s0);
450160814Ssimon
451160814Ssimon	&call   (&label("pic_point"));          # make it PIC!
452160814Ssimon	&set_label("pic_point");
453160814Ssimon	&blindpop("ebp");
454160814Ssimon	&lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
455160814Ssimon
456160814Ssimon	&mov	($s0,&DWP(0,$acc));		# load input data
457160814Ssimon	&mov	($s1,&DWP(4,$acc));
458160814Ssimon	&mov	($s2,&DWP(8,$acc));
459160814Ssimon	&mov	($s3,&DWP(12,$acc));
460160814Ssimon
461160814Ssimon	&call	("_x86_AES_encrypt");
462160814Ssimon
463160814Ssimon	&mov	("esp",&DWP(16,"esp"));
464160814Ssimon
465160814Ssimon	&mov	($acc,&wparam(1));		# load out
466160814Ssimon	&mov	(&DWP(0,$acc),$s0);		# write output data
467160814Ssimon	&mov	(&DWP(4,$acc),$s1);
468160814Ssimon	&mov	(&DWP(8,$acc),$s2);
469160814Ssimon	&mov	(&DWP(12,$acc),$s3);
470160814Ssimon&function_end("AES_encrypt");
471160814Ssimon
472160814Ssimon#------------------------------------------------------------------#
473160814Ssimon
474160814Ssimonsub decstep()
475160814Ssimon{ my ($i,$td,@s) = @_;
476160814Ssimon  my $tmp = $key;
477160814Ssimon  my $out = $i==3?$s[0]:$acc;
478160814Ssimon
479160814Ssimon	# no instructions are reordered, as performance appears
480160814Ssimon	# optimal... or rather that all attempts to reorder didn't
481160814Ssimon	# result in better performance [which by the way is not a
482160814Ssimon	# bit lower than ecryption].
483160814Ssimon	if($i==3)   {	&mov	($key,&DWP(12,"esp"));		}
484160814Ssimon	else        {	&mov	($out,$s[0]);			}
485160814Ssimon			&and	($out,0xFF);
486160814Ssimon			&mov	($out,&DWP(0,$td,$out,8));
487160814Ssimon
488160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}
489160814Ssimon			&movz	($tmp,&HB($s[1]));
490160814Ssimon			&xor	($out,&DWP(3,$td,$tmp,8));
491160814Ssimon
492160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
493160814Ssimon	else        {	&mov	($tmp,$s[2]);			}
494160814Ssimon			&shr	($tmp,16);
495160814Ssimon			&and	($tmp,0xFF);
496160814Ssimon			&xor	($out,&DWP(2,$td,$tmp,8));
497160814Ssimon
498160814Ssimon	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}
499160814Ssimon	else        {	&mov	($tmp,$s[3]);			}
500160814Ssimon			&shr	($tmp,24);
501160814Ssimon			&xor	($out,&DWP(1,$td,$tmp,8));
502160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
503160814Ssimon	if ($i==3)  {	&mov	($s[3],&DWP(4,"esp"));		}
504160814Ssimon			&comment();
505160814Ssimon}
506160814Ssimon
507160814Ssimonsub declast()
508160814Ssimon{ my ($i,$td,@s)=@_;
509160814Ssimon  my $tmp = $key;
510160814Ssimon  my $out = $i==3?$s[0]:$acc;
511160814Ssimon
512160814Ssimon	if($i==3)   {	&mov	($key,&DWP(12,"esp"));		}
513160814Ssimon	else        {	&mov	($out,$s[0]);			}
514160814Ssimon			&and	($out,0xFF);
515194206Ssimon			&movz	($out,&BP(2048,$td,$out,1));
516160814Ssimon
517160814Ssimon	if ($i==3)  {	$tmp=$s[1];				}
518160814Ssimon			&movz	($tmp,&HB($s[1]));
519194206Ssimon			&movz	($tmp,&BP(2048,$td,$tmp,1));
520162911Ssimon			&shl	($tmp,8);
521160814Ssimon			&xor	($out,$tmp);
522160814Ssimon
523160814Ssimon	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
524160814Ssimon	else        {	mov	($tmp,$s[2]);			}
525160814Ssimon			&shr	($tmp,16);
526160814Ssimon			&and	($tmp,0xFF);
527194206Ssimon			&movz	($tmp,&BP(2048,$td,$tmp,1));
528162911Ssimon			&shl	($tmp,16);
529160814Ssimon			&xor	($out,$tmp);
530160814Ssimon
531160814Ssimon	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}
532160814Ssimon	else        {	&mov	($tmp,$s[3]);			}
533160814Ssimon			&shr	($tmp,24);
534194206Ssimon			&movz	($tmp,&BP(2048,$td,$tmp,1));
535162911Ssimon			&shl	($tmp,24);
536160814Ssimon			&xor	($out,$tmp);
537160814Ssimon	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
538160814Ssimon	if ($i==3)  {	&mov	($s[3],&DWP(4,"esp"));		}
539160814Ssimon}
540160814Ssimon
541160814Ssimon&public_label("AES_Td");
542160814Ssimon&function_begin_B("_x86_AES_decrypt");
543160814Ssimon	# note that caller is expected to allocate stack frame for me!
544160814Ssimon	&mov	(&DWP(12,"esp"),$key);		# save key
545160814Ssimon
546160814Ssimon	&xor	($s0,&DWP(0,$key));		# xor with key
547160814Ssimon	&xor	($s1,&DWP(4,$key));
548160814Ssimon	&xor	($s2,&DWP(8,$key));
549160814Ssimon	&xor	($s3,&DWP(12,$key));
550160814Ssimon
551160814Ssimon	&mov	($acc,&DWP(240,$key));		# load key->rounds
552160814Ssimon
553160814Ssimon	if ($small_footprint) {
554160814Ssimon	    &lea	($acc,&DWP(-2,$acc,$acc));
555160814Ssimon	    &lea	($acc,&DWP(0,$key,$acc,8));
556160814Ssimon	    &mov	(&DWP(16,"esp"),$acc);	# end of key schedule
557160814Ssimon	    &align	(4);
558160814Ssimon	    &set_label("loop");
559160814Ssimon		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
560160814Ssimon		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
561160814Ssimon		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
562160814Ssimon		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
563160814Ssimon		&add	($key,16);		# advance rd_key
564160814Ssimon		&xor	($s0,&DWP(0,$key));
565160814Ssimon		&xor	($s1,&DWP(4,$key));
566160814Ssimon		&xor	($s2,&DWP(8,$key));
567160814Ssimon		&xor	($s3,&DWP(12,$key));
568160814Ssimon	    &cmp	($key,&DWP(16,"esp"));
569160814Ssimon	    &mov	(&DWP(12,"esp"),$key);
570160814Ssimon	    &jb		(&label("loop"));
571160814Ssimon	}
572160814Ssimon	else {
573160814Ssimon	    &cmp	($acc,10);
574160814Ssimon	    &jle	(&label("10rounds"));
575160814Ssimon	    &cmp	($acc,12);
576160814Ssimon	    &jle	(&label("12rounds"));
577160814Ssimon
578160814Ssimon	&set_label("14rounds");
579160814Ssimon	    for ($i=1;$i<3;$i++) {
580160814Ssimon		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
581160814Ssimon		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
582160814Ssimon		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
583160814Ssimon		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
584160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
585160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
586160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
587160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
588160814Ssimon	    }
589160814Ssimon	    &add	($key,32);
590160814Ssimon	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
591160814Ssimon	&set_label("12rounds");
592160814Ssimon	    for ($i=1;$i<3;$i++) {
593160814Ssimon		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
594160814Ssimon		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
595160814Ssimon		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
596160814Ssimon		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
597160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
598160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
599160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
600160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
601160814Ssimon	    }
602160814Ssimon	    &add	($key,32);
603160814Ssimon	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
604160814Ssimon	&set_label("10rounds");
605160814Ssimon	    for ($i=1;$i<10;$i++) {
606160814Ssimon		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
607160814Ssimon		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
608160814Ssimon		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
609160814Ssimon		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
610160814Ssimon		&xor	($s0,&DWP(16*$i+0,$key));
611160814Ssimon		&xor	($s1,&DWP(16*$i+4,$key));
612160814Ssimon		&xor	($s2,&DWP(16*$i+8,$key));
613160814Ssimon		&xor	($s3,&DWP(16*$i+12,$key));
614160814Ssimon	    }
615160814Ssimon	}
616160814Ssimon
617160814Ssimon	&declast(0,"ebp",$s0,$s3,$s2,$s1);
618160814Ssimon	&declast(1,"ebp",$s1,$s0,$s3,$s2);
619160814Ssimon	&declast(2,"ebp",$s2,$s1,$s0,$s3);
620160814Ssimon	&declast(3,"ebp",$s3,$s2,$s1,$s0);
621160814Ssimon
622160814Ssimon	&add	($key,$small_footprint?16:160);
623160814Ssimon	&xor	($s0,&DWP(0,$key));
624160814Ssimon	&xor	($s1,&DWP(4,$key));
625160814Ssimon	&xor	($s2,&DWP(8,$key));
626160814Ssimon	&xor	($s3,&DWP(12,$key));
627160814Ssimon
628160814Ssimon	&ret	();
629160814Ssimon
630160814Ssimon&set_label("AES_Td",64);	# Yes! I keep it in the code segment!
631160814Ssimon	&_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
632160814Ssimon	&_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
633160814Ssimon	&_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
634160814Ssimon	&_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
635160814Ssimon	&_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
636160814Ssimon	&_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
637160814Ssimon	&_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
638160814Ssimon	&_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
639160814Ssimon	&_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
640160814Ssimon	&_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
641160814Ssimon	&_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
642160814Ssimon	&_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
643160814Ssimon	&_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
644160814Ssimon	&_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
645160814Ssimon	&_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
646160814Ssimon	&_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
647160814Ssimon	&_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
648160814Ssimon	&_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
649160814Ssimon	&_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
650160814Ssimon	&_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
651160814Ssimon	&_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
652160814Ssimon	&_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
653160814Ssimon	&_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
654160814Ssimon	&_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
655160814Ssimon	&_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
656160814Ssimon	&_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
657160814Ssimon	&_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
658160814Ssimon	&_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
659160814Ssimon	&_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
660160814Ssimon	&_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
661160814Ssimon	&_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
662160814Ssimon	&_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
663160814Ssimon	&_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
664160814Ssimon	&_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
665160814Ssimon	&_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
666160814Ssimon	&_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
667160814Ssimon	&_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
668160814Ssimon	&_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
669160814Ssimon	&_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
670160814Ssimon	&_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
671160814Ssimon	&_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
672160814Ssimon	&_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
673160814Ssimon	&_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
674160814Ssimon	&_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
675160814Ssimon	&_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
676160814Ssimon	&_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
677160814Ssimon	&_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
678160814Ssimon	&_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
679160814Ssimon	&_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
680160814Ssimon	&_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
681160814Ssimon	&_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
682160814Ssimon	&_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
683160814Ssimon	&_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
684160814Ssimon	&_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
685160814Ssimon	&_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
686160814Ssimon	&_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
687160814Ssimon	&_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
688160814Ssimon	&_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
689160814Ssimon	&_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
690160814Ssimon	&_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
691160814Ssimon	&_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
692160814Ssimon	&_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
693160814Ssimon	&_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
694160814Ssimon	&_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
695160814Ssimon#Td4:
696162911Ssimon	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
697162911Ssimon	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
698162911Ssimon	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
699162911Ssimon	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
700162911Ssimon	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
701162911Ssimon	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
702162911Ssimon	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
703162911Ssimon	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
704162911Ssimon	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
705162911Ssimon	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
706162911Ssimon	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
707162911Ssimon	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
708162911Ssimon	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
709162911Ssimon	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
710162911Ssimon	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
711162911Ssimon	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
712162911Ssimon	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
713162911Ssimon	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
714162911Ssimon	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
715162911Ssimon	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
716162911Ssimon	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
717162911Ssimon	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
718162911Ssimon	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
719162911Ssimon	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
720162911Ssimon	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
721162911Ssimon	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
722162911Ssimon	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
723162911Ssimon	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
724162911Ssimon	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
725162911Ssimon	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
726162911Ssimon	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
727162911Ssimon	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
728160814Ssimon&function_end_B("_x86_AES_decrypt");
729160814Ssimon
730160814Ssimon# void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
731160814Ssimon&public_label("AES_Td");
732160814Ssimon&function_begin("AES_decrypt");
733160814Ssimon	&mov	($acc,&wparam(0));		# load inp
734160814Ssimon	&mov	($key,&wparam(2));		# load key
735160814Ssimon
736160814Ssimon	&mov	($s0,"esp");
737160814Ssimon	&sub	("esp",24);
738160814Ssimon	&and	("esp",-64);
739160814Ssimon	&add	("esp",4);
740160814Ssimon	&mov	(&DWP(16,"esp"),$s0);
741160814Ssimon
742160814Ssimon	&call   (&label("pic_point"));          # make it PIC!
743160814Ssimon	&set_label("pic_point");
744160814Ssimon	&blindpop("ebp");
745160814Ssimon	&lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
746160814Ssimon
747162911Ssimon	# prefetch Td4
748162911Ssimon	&lea	("ebp",&DWP(2048+128,"ebp"));
749162911Ssimon	&mov	($s0,&DWP(0-128,"ebp"));
750162911Ssimon	&mov	($s1,&DWP(32-128,"ebp"));
751162911Ssimon	&mov	($s2,&DWP(64-128,"ebp"));
752162911Ssimon	&mov	($s3,&DWP(96-128,"ebp"));
753162911Ssimon	&mov	($s0,&DWP(128-128,"ebp"));
754162911Ssimon	&mov	($s1,&DWP(160-128,"ebp"));
755162911Ssimon	&mov	($s2,&DWP(192-128,"ebp"));
756162911Ssimon	&mov	($s3,&DWP(224-128,"ebp"));
757162911Ssimon	&lea	("ebp",&DWP(-2048-128,"ebp"));
758162911Ssimon
759160814Ssimon	&mov	($s0,&DWP(0,$acc));		# load input data
760160814Ssimon	&mov	($s1,&DWP(4,$acc));
761160814Ssimon	&mov	($s2,&DWP(8,$acc));
762160814Ssimon	&mov	($s3,&DWP(12,$acc));
763160814Ssimon
764160814Ssimon	&call	("_x86_AES_decrypt");
765160814Ssimon
766160814Ssimon	&mov	("esp",&DWP(16,"esp"));
767160814Ssimon
768160814Ssimon	&mov	($acc,&wparam(1));		# load out
769160814Ssimon	&mov	(&DWP(0,$acc),$s0);		# write output data
770160814Ssimon	&mov	(&DWP(4,$acc),$s1);
771160814Ssimon	&mov	(&DWP(8,$acc),$s2);
772160814Ssimon	&mov	(&DWP(12,$acc),$s3);
773160814Ssimon&function_end("AES_decrypt");
774160814Ssimon
775160814Ssimon# void AES_cbc_encrypt (const void char *inp, unsigned char *out,
776160814Ssimon#			size_t length, const AES_KEY *key,
777160814Ssimon#			unsigned char *ivp,const int enc);
778160814Ssimon{
779160814Ssimon# stack frame layout
780160814Ssimon# -4(%esp)	0(%esp)		return address
781160814Ssimon# 0(%esp)	4(%esp)		tmp1
782160814Ssimon# 4(%esp)	8(%esp)		tmp2
783160814Ssimon# 8(%esp)	12(%esp)	key
784160814Ssimon# 12(%esp)	16(%esp)	end of key schedule
785160814Ssimonmy $_esp=&DWP(16,"esp");	#saved %esp
786160814Ssimonmy $_inp=&DWP(20,"esp");	#copy of wparam(0)
787160814Ssimonmy $_out=&DWP(24,"esp");	#copy of wparam(1)
788160814Ssimonmy $_len=&DWP(28,"esp");	#copy of wparam(2)
789160814Ssimonmy $_key=&DWP(32,"esp");	#copy of wparam(3)
790160814Ssimonmy $_ivp=&DWP(36,"esp");	#copy of wparam(4)
791160814Ssimonmy $_tmp=&DWP(40,"esp");	#volatile variable
792160814Ssimonmy $ivec=&DWP(44,"esp");	#ivec[16]
793160814Ssimonmy $aes_key=&DWP(60,"esp");	#copy of aes_key
794162911Ssimonmy $mark=&DWP(60+240,"esp");	#copy of aes_key->rounds
795160814Ssimon
796160814Ssimon&public_label("AES_Te");
797160814Ssimon&public_label("AES_Td");
798160814Ssimon&function_begin("AES_cbc_encrypt");
799160814Ssimon	&mov	($s2 eq "ecx"? $s2 : "",&wparam(2));	# load len
800160814Ssimon	&cmp	($s2,0);
801160814Ssimon	&je	(&label("enc_out"));
802160814Ssimon
803160814Ssimon	&call   (&label("pic_point"));		# make it PIC!
804160814Ssimon	&set_label("pic_point");
805160814Ssimon	&blindpop("ebp");
806160814Ssimon
807160814Ssimon	&pushf	();
808160814Ssimon	&cld	();
809160814Ssimon
810160814Ssimon	&cmp	(&wparam(5),0);
811160814Ssimon	&je	(&label("DECRYPT"));
812160814Ssimon
813160814Ssimon	&lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
814160814Ssimon
815160814Ssimon	# allocate aligned stack frame...
816160814Ssimon	&lea	($key,&DWP(-64-244,"esp"));
817160814Ssimon	&and	($key,-64);
818160814Ssimon
819160814Ssimon	# ... and make sure it doesn't alias with AES_Te modulo 4096
820160814Ssimon	&mov	($s0,"ebp");
821160814Ssimon	&lea	($s1,&DWP(2048,"ebp"));
822160814Ssimon	&mov	($s3,$key);
823160814Ssimon	&and	($s0,0xfff);		# s = %ebp&0xfff
824160814Ssimon	&and	($s1,0xfff);		# e = (%ebp+2048)&0xfff
825160814Ssimon	&and	($s3,0xfff);		# p = %esp&0xfff
826160814Ssimon
827160814Ssimon	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
828160814Ssimon	&jb	(&label("te_break_out"));
829160814Ssimon	&sub	($s3,$s1);
830160814Ssimon	&sub	($key,$s3);
831160814Ssimon	&jmp	(&label("te_ok"));
832160814Ssimon	&set_label("te_break_out");	# else %esp -= (p-s)&0xfff + framesz;
833160814Ssimon	&sub	($s3,$s0);
834160814Ssimon	&and	($s3,0xfff);
835160814Ssimon	&add	($s3,64+256);
836160814Ssimon	&sub	($key,$s3);
837160814Ssimon	&align	(4);
838160814Ssimon	&set_label("te_ok");
839160814Ssimon
840160814Ssimon	&mov	($s0,&wparam(0));	# load inp
841160814Ssimon	&mov	($s1,&wparam(1));	# load out
842160814Ssimon	&mov	($s3,&wparam(3));	# load key
843160814Ssimon	&mov	($acc,&wparam(4));	# load ivp
844160814Ssimon
845160814Ssimon	&exch	("esp",$key);
846160814Ssimon	&add	("esp",4);		# reserve for return address!
847160814Ssimon	&mov	($_esp,$key);		# save %esp
848160814Ssimon
849160814Ssimon	&mov	($_inp,$s0);		# save copy of inp
850160814Ssimon	&mov	($_out,$s1);		# save copy of out
851160814Ssimon	&mov	($_len,$s2);		# save copy of len
852160814Ssimon	&mov	($_key,$s3);		# save copy of key
853160814Ssimon	&mov	($_ivp,$acc);		# save copy of ivp
854160814Ssimon
855162911Ssimon	&mov	($mark,0);		# copy of aes_key->rounds = 0;
856160814Ssimon	if ($compromise) {
857160814Ssimon		&cmp	($s2,$compromise);
858160814Ssimon		&jb	(&label("skip_ecopy"));
859160814Ssimon	}
860162911Ssimon	# do we copy key schedule to stack?
861162911Ssimon	&mov	($s1 eq "ebx" ? $s1 : "",$s3);
862162911Ssimon	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
863162911Ssimon	&sub	($s1,"ebp");
864160814Ssimon	&mov	("esi",$s3);
865162911Ssimon	&and	($s1,0xfff);
866160814Ssimon	&lea	("edi",$aes_key);
867162911Ssimon	&cmp	($s1,2048);
868162911Ssimon	&jb	(&label("do_ecopy"));
869162911Ssimon	&cmp	($s1,4096-244);
870162911Ssimon	&jb	(&label("skip_ecopy"));
871160814Ssimon	&align	(4);
872162911Ssimon	&set_label("do_ecopy");
873162911Ssimon		&mov	($_key,"edi");
874162911Ssimon		&data_word(0xA5F3F689);	# rep movsd
875162911Ssimon	&set_label("skip_ecopy");
876160814Ssimon
877160814Ssimon	&mov	($acc,$s0);
878160814Ssimon	&mov	($key,16);
879160814Ssimon	&align	(4);
880160814Ssimon	&set_label("prefetch_te");
881160814Ssimon		&mov	($s0,&DWP(0,"ebp"));
882160814Ssimon		&mov	($s1,&DWP(32,"ebp"));
883160814Ssimon		&mov	($s2,&DWP(64,"ebp"));
884160814Ssimon		&mov	($s3,&DWP(96,"ebp"));
885160814Ssimon		&lea	("ebp",&DWP(128,"ebp"));
886160814Ssimon		&dec	($key);
887160814Ssimon	&jnz	(&label("prefetch_te"));
888160814Ssimon	&sub	("ebp",2048);
889160814Ssimon
890160814Ssimon	&mov	($s2,$_len);
891160814Ssimon	&mov	($key,$_ivp);
892160814Ssimon	&test	($s2,0xFFFFFFF0);
893160814Ssimon	&jz	(&label("enc_tail"));		# short input...
894160814Ssimon
895160814Ssimon	&mov	($s0,&DWP(0,$key));		# load iv
896160814Ssimon	&mov	($s1,&DWP(4,$key));
897160814Ssimon
898160814Ssimon	&align	(4);
899160814Ssimon	&set_label("enc_loop");
900160814Ssimon		&mov	($s2,&DWP(8,$key));
901160814Ssimon		&mov	($s3,&DWP(12,$key));
902160814Ssimon
903160814Ssimon		&xor	($s0,&DWP(0,$acc));	# xor input data
904160814Ssimon		&xor	($s1,&DWP(4,$acc));
905160814Ssimon		&xor	($s2,&DWP(8,$acc));
906160814Ssimon		&xor	($s3,&DWP(12,$acc));
907160814Ssimon
908160814Ssimon		&mov	($key,$_key);		# load key
909160814Ssimon		&call	("_x86_AES_encrypt");
910160814Ssimon
911160814Ssimon		&mov	($acc,$_inp);		# load inp
912160814Ssimon		&mov	($key,$_out);		# load out
913160814Ssimon
914160814Ssimon		&mov	(&DWP(0,$key),$s0);	# save output data
915160814Ssimon		&mov	(&DWP(4,$key),$s1);
916160814Ssimon		&mov	(&DWP(8,$key),$s2);
917160814Ssimon		&mov	(&DWP(12,$key),$s3);
918160814Ssimon
919160814Ssimon		&mov	($s2,$_len);		# load len
920160814Ssimon
921160814Ssimon		&lea	($acc,&DWP(16,$acc));
922160814Ssimon		&mov	($_inp,$acc);		# save inp
923160814Ssimon
924160814Ssimon		&lea	($s3,&DWP(16,$key));
925160814Ssimon		&mov	($_out,$s3);		# save out
926160814Ssimon
927160814Ssimon		&sub	($s2,16);
928160814Ssimon		&test	($s2,0xFFFFFFF0);
929160814Ssimon		&mov	($_len,$s2);		# save len
930160814Ssimon	&jnz	(&label("enc_loop"));
931160814Ssimon	&test	($s2,15);
932160814Ssimon	&jnz	(&label("enc_tail"));
933160814Ssimon	&mov	($acc,$_ivp);		# load ivp
934160814Ssimon	&mov	($s2,&DWP(8,$key));	# restore last dwords
935160814Ssimon	&mov	($s3,&DWP(12,$key));
936160814Ssimon	&mov	(&DWP(0,$acc),$s0);	# save ivec
937160814Ssimon	&mov	(&DWP(4,$acc),$s1);
938160814Ssimon	&mov	(&DWP(8,$acc),$s2);
939160814Ssimon	&mov	(&DWP(12,$acc),$s3);
940160814Ssimon
941162911Ssimon	&cmp	($mark,0);		# was the key schedule copied?
942160814Ssimon	&mov	("edi",$_key);
943162911Ssimon	&je	(&label("skip_ezero"));
944160814Ssimon	# zero copy of key schedule
945160814Ssimon	&mov	("ecx",240/4);
946160814Ssimon	&xor	("eax","eax");
947160814Ssimon	&align	(4);
948162911Ssimon	&data_word(0xABF3F689);	# rep stosd
949162911Ssimon	&set_label("skip_ezero")
950194206Ssimon	&mov	("esp",$_esp);
951160814Ssimon	&popf	();
952160814Ssimon    &set_label("enc_out");
953160814Ssimon	&function_end_A();
954160814Ssimon	&pushf	();			# kludge, never executed
955160814Ssimon
956160814Ssimon    &align	(4);
957160814Ssimon    &set_label("enc_tail");
958194206Ssimon	&mov	($s0,$key eq "edi" ? $key : "");
959160814Ssimon	&mov	($key,$_out);			# load out
960194206Ssimon	&push	($s0);				# push ivp
961160814Ssimon	&mov	($s1,16);
962160814Ssimon	&sub	($s1,$s2);
963160814Ssimon	&cmp	($key,$acc);			# compare with inp
964160814Ssimon	&je	(&label("enc_in_place"));
965160814Ssimon	&align	(4);
966162911Ssimon	&data_word(0xA4F3F689);	# rep movsb	# copy input
967160814Ssimon	&jmp	(&label("enc_skip_in_place"));
968160814Ssimon    &set_label("enc_in_place");
969160814Ssimon	&lea	($key,&DWP(0,$key,$s2));
970160814Ssimon    &set_label("enc_skip_in_place");
971160814Ssimon	&mov	($s2,$s1);
972160814Ssimon	&xor	($s0,$s0);
973160814Ssimon	&align	(4);
974162911Ssimon	&data_word(0xAAF3F689);	# rep stosb	# zero tail
975160814Ssimon	&pop	($key);				# pop ivp
976160814Ssimon
977160814Ssimon	&mov	($acc,$_out);			# output as input
978160814Ssimon	&mov	($s0,&DWP(0,$key));
979160814Ssimon	&mov	($s1,&DWP(4,$key));
980160814Ssimon	&mov	($_len,16);			# len=16
981160814Ssimon	&jmp	(&label("enc_loop"));		# one more spin...
982160814Ssimon
983160814Ssimon#----------------------------- DECRYPT -----------------------------#
984160814Ssimon&align	(4);
985160814Ssimon&set_label("DECRYPT");
986160814Ssimon	&lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
987160814Ssimon
988160814Ssimon	# allocate aligned stack frame...
989160814Ssimon	&lea	($key,&DWP(-64-244,"esp"));
990160814Ssimon	&and	($key,-64);
991160814Ssimon
992160814Ssimon	# ... and make sure it doesn't alias with AES_Td modulo 4096
993160814Ssimon	&mov	($s0,"ebp");
994162911Ssimon	&lea	($s1,&DWP(2048+256,"ebp"));
995160814Ssimon	&mov	($s3,$key);
996160814Ssimon	&and	($s0,0xfff);		# s = %ebp&0xfff
997162911Ssimon	&and	($s1,0xfff);		# e = (%ebp+2048+256)&0xfff
998160814Ssimon	&and	($s3,0xfff);		# p = %esp&0xfff
999160814Ssimon
1000160814Ssimon	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
1001160814Ssimon	&jb	(&label("td_break_out"));
1002160814Ssimon	&sub	($s3,$s1);
1003160814Ssimon	&sub	($key,$s3);
1004160814Ssimon	&jmp	(&label("td_ok"));
1005160814Ssimon	&set_label("td_break_out");	# else %esp -= (p-s)&0xfff + framesz;
1006160814Ssimon	&sub	($s3,$s0);
1007160814Ssimon	&and	($s3,0xfff);
1008160814Ssimon	&add	($s3,64+256);
1009160814Ssimon	&sub	($key,$s3);
1010160814Ssimon	&align	(4);
1011160814Ssimon	&set_label("td_ok");
1012160814Ssimon
1013160814Ssimon	&mov	($s0,&wparam(0));	# load inp
1014160814Ssimon	&mov	($s1,&wparam(1));	# load out
1015160814Ssimon	&mov	($s3,&wparam(3));	# load key
1016160814Ssimon	&mov	($acc,&wparam(4));	# load ivp
1017160814Ssimon
1018160814Ssimon	&exch	("esp",$key);
1019160814Ssimon	&add	("esp",4);		# reserve for return address!
1020160814Ssimon	&mov	($_esp,$key);		# save %esp
1021160814Ssimon
1022160814Ssimon	&mov	($_inp,$s0);		# save copy of inp
1023160814Ssimon	&mov	($_out,$s1);		# save copy of out
1024160814Ssimon	&mov	($_len,$s2);		# save copy of len
1025160814Ssimon	&mov	($_key,$s3);		# save copy of key
1026160814Ssimon	&mov	($_ivp,$acc);		# save copy of ivp
1027160814Ssimon
1028162911Ssimon	&mov	($mark,0);		# copy of aes_key->rounds = 0;
1029160814Ssimon	if ($compromise) {
1030160814Ssimon		&cmp	($s2,$compromise);
1031160814Ssimon		&jb	(&label("skip_dcopy"));
1032160814Ssimon	}
1033162911Ssimon	# do we copy key schedule to stack?
1034162911Ssimon	&mov	($s1 eq "ebx" ? $s1 : "",$s3);
1035162911Ssimon	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
1036162911Ssimon	&sub	($s1,"ebp");
1037160814Ssimon	&mov	("esi",$s3);
1038162911Ssimon	&and	($s1,0xfff);
1039160814Ssimon	&lea	("edi",$aes_key);
1040162911Ssimon	&cmp	($s1,2048+256);
1041162911Ssimon	&jb	(&label("do_dcopy"));
1042162911Ssimon	&cmp	($s1,4096-244);
1043162911Ssimon	&jb	(&label("skip_dcopy"));
1044160814Ssimon	&align	(4);
1045162911Ssimon	&set_label("do_dcopy");
1046162911Ssimon		&mov	($_key,"edi");
1047162911Ssimon		&data_word(0xA5F3F689);	# rep movsd
1048162911Ssimon	&set_label("skip_dcopy");
1049160814Ssimon
1050160814Ssimon	&mov	($acc,$s0);
1051162911Ssimon	&mov	($key,18);
1052160814Ssimon	&align	(4);
1053160814Ssimon	&set_label("prefetch_td");
1054160814Ssimon		&mov	($s0,&DWP(0,"ebp"));
1055160814Ssimon		&mov	($s1,&DWP(32,"ebp"));
1056160814Ssimon		&mov	($s2,&DWP(64,"ebp"));
1057160814Ssimon		&mov	($s3,&DWP(96,"ebp"));
1058160814Ssimon		&lea	("ebp",&DWP(128,"ebp"));
1059160814Ssimon		&dec	($key);
1060160814Ssimon	&jnz	(&label("prefetch_td"));
1061162911Ssimon	&sub	("ebp",2048+256);
1062160814Ssimon
1063160814Ssimon	&cmp	($acc,$_out);
1064160814Ssimon	&je	(&label("dec_in_place"));	# in-place processing...
1065160814Ssimon
1066160814Ssimon	&mov	($key,$_ivp);		# load ivp
1067160814Ssimon	&mov	($_tmp,$key);
1068160814Ssimon
1069160814Ssimon	&align	(4);
1070160814Ssimon	&set_label("dec_loop");
1071160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read input
1072160814Ssimon		&mov	($s1,&DWP(4,$acc));
1073160814Ssimon		&mov	($s2,&DWP(8,$acc));
1074160814Ssimon		&mov	($s3,&DWP(12,$acc));
1075160814Ssimon
1076160814Ssimon		&mov	($key,$_key);		# load key
1077160814Ssimon		&call	("_x86_AES_decrypt");
1078160814Ssimon
1079160814Ssimon		&mov	($key,$_tmp);		# load ivp
1080160814Ssimon		&mov	($acc,$_len);		# load len
1081160814Ssimon		&xor	($s0,&DWP(0,$key));	# xor iv
1082160814Ssimon		&xor	($s1,&DWP(4,$key));
1083160814Ssimon		&xor	($s2,&DWP(8,$key));
1084160814Ssimon		&xor	($s3,&DWP(12,$key));
1085160814Ssimon
1086160814Ssimon		&sub	($acc,16);
1087160814Ssimon		&jc	(&label("dec_partial"));
1088160814Ssimon		&mov	($_len,$acc);		# save len
1089160814Ssimon		&mov	($acc,$_inp);		# load inp
1090160814Ssimon		&mov	($key,$_out);		# load out
1091160814Ssimon
1092160814Ssimon		&mov	(&DWP(0,$key),$s0);	# write output
1093160814Ssimon		&mov	(&DWP(4,$key),$s1);
1094160814Ssimon		&mov	(&DWP(8,$key),$s2);
1095160814Ssimon		&mov	(&DWP(12,$key),$s3);
1096160814Ssimon
1097160814Ssimon		&mov	($_tmp,$acc);		# save ivp
1098160814Ssimon		&lea	($acc,&DWP(16,$acc));
1099160814Ssimon		&mov	($_inp,$acc);		# save inp
1100160814Ssimon
1101160814Ssimon		&lea	($key,&DWP(16,$key));
1102160814Ssimon		&mov	($_out,$key);		# save out
1103160814Ssimon
1104160814Ssimon	&jnz	(&label("dec_loop"));
1105160814Ssimon	&mov	($key,$_tmp);		# load temp ivp
1106160814Ssimon    &set_label("dec_end");
1107160814Ssimon	&mov	($acc,$_ivp);		# load user ivp
1108160814Ssimon	&mov	($s0,&DWP(0,$key));	# load iv
1109160814Ssimon	&mov	($s1,&DWP(4,$key));
1110160814Ssimon	&mov	($s2,&DWP(8,$key));
1111160814Ssimon	&mov	($s3,&DWP(12,$key));
1112160814Ssimon	&mov	(&DWP(0,$acc),$s0);	# copy back to user
1113160814Ssimon	&mov	(&DWP(4,$acc),$s1);
1114160814Ssimon	&mov	(&DWP(8,$acc),$s2);
1115160814Ssimon	&mov	(&DWP(12,$acc),$s3);
1116160814Ssimon	&jmp	(&label("dec_out"));
1117160814Ssimon
1118160814Ssimon    &align	(4);
1119160814Ssimon    &set_label("dec_partial");
1120160814Ssimon	&lea	($key,$ivec);
1121160814Ssimon	&mov	(&DWP(0,$key),$s0);	# dump output to stack
1122160814Ssimon	&mov	(&DWP(4,$key),$s1);
1123160814Ssimon	&mov	(&DWP(8,$key),$s2);
1124160814Ssimon	&mov	(&DWP(12,$key),$s3);
1125160814Ssimon	&lea	($s2 eq "ecx" ? $s2 : "",&DWP(16,$acc));
1126160814Ssimon	&mov	($acc eq "esi" ? $acc : "",$key);
1127160814Ssimon	&mov	($key eq "edi" ? $key : "",$_out);	# load out
1128162911Ssimon	&data_word(0xA4F3F689);	# rep movsb		# copy output
1129160814Ssimon	&mov	($key,$_inp);				# use inp as temp ivp
1130160814Ssimon	&jmp	(&label("dec_end"));
1131160814Ssimon
1132160814Ssimon    &align	(4);
1133160814Ssimon    &set_label("dec_in_place");
1134160814Ssimon	&set_label("dec_in_place_loop");
1135160814Ssimon		&lea	($key,$ivec);
1136160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read input
1137160814Ssimon		&mov	($s1,&DWP(4,$acc));
1138160814Ssimon		&mov	($s2,&DWP(8,$acc));
1139160814Ssimon		&mov	($s3,&DWP(12,$acc));
1140160814Ssimon
1141160814Ssimon		&mov	(&DWP(0,$key),$s0);	# copy to temp
1142160814Ssimon		&mov	(&DWP(4,$key),$s1);
1143160814Ssimon		&mov	(&DWP(8,$key),$s2);
1144160814Ssimon		&mov	(&DWP(12,$key),$s3);
1145160814Ssimon
1146160814Ssimon		&mov	($key,$_key);		# load key
1147160814Ssimon		&call	("_x86_AES_decrypt");
1148160814Ssimon
1149160814Ssimon		&mov	($key,$_ivp);		# load ivp
1150160814Ssimon		&mov	($acc,$_out);		# load out
1151160814Ssimon		&xor	($s0,&DWP(0,$key));	# xor iv
1152160814Ssimon		&xor	($s1,&DWP(4,$key));
1153160814Ssimon		&xor	($s2,&DWP(8,$key));
1154160814Ssimon		&xor	($s3,&DWP(12,$key));
1155160814Ssimon
1156160814Ssimon		&mov	(&DWP(0,$acc),$s0);	# write output
1157160814Ssimon		&mov	(&DWP(4,$acc),$s1);
1158160814Ssimon		&mov	(&DWP(8,$acc),$s2);
1159160814Ssimon		&mov	(&DWP(12,$acc),$s3);
1160160814Ssimon
1161160814Ssimon		&lea	($acc,&DWP(16,$acc));
1162160814Ssimon		&mov	($_out,$acc);		# save out
1163160814Ssimon
1164160814Ssimon		&lea	($acc,$ivec);
1165160814Ssimon		&mov	($s0,&DWP(0,$acc));	# read temp
1166160814Ssimon		&mov	($s1,&DWP(4,$acc));
1167160814Ssimon		&mov	($s2,&DWP(8,$acc));
1168160814Ssimon		&mov	($s3,&DWP(12,$acc));
1169160814Ssimon
1170160814Ssimon		&mov	(&DWP(0,$key),$s0);	# copy iv
1171160814Ssimon		&mov	(&DWP(4,$key),$s1);
1172160814Ssimon		&mov	(&DWP(8,$key),$s2);
1173160814Ssimon		&mov	(&DWP(12,$key),$s3);
1174160814Ssimon
1175160814Ssimon		&mov	($acc,$_inp);		# load inp
1176160814Ssimon
1177160814Ssimon		&lea	($acc,&DWP(16,$acc));
1178160814Ssimon		&mov	($_inp,$acc);		# save inp
1179160814Ssimon
1180160814Ssimon		&mov	($s2,$_len);		# load len
1181160814Ssimon		&sub	($s2,16);
1182160814Ssimon		&jc	(&label("dec_in_place_partial"));
1183160814Ssimon		&mov	($_len,$s2);		# save len
1184160814Ssimon	&jnz	(&label("dec_in_place_loop"));
1185160814Ssimon	&jmp	(&label("dec_out"));
1186160814Ssimon
1187160814Ssimon    &align	(4);
1188160814Ssimon    &set_label("dec_in_place_partial");
1189160814Ssimon	# one can argue if this is actually required...
1190160814Ssimon	&mov	($key eq "edi" ? $key : "",$_out);
1191160814Ssimon	&lea	($acc eq "esi" ? $acc : "",$ivec);
1192160814Ssimon	&lea	($key,&DWP(0,$key,$s2));
1193160814Ssimon	&lea	($acc,&DWP(16,$acc,$s2));
1194160814Ssimon	&neg	($s2 eq "ecx" ? $s2 : "");
1195162911Ssimon	&data_word(0xA4F3F689);	# rep movsb	# restore tail
1196160814Ssimon
1197160814Ssimon    &align	(4);
1198160814Ssimon    &set_label("dec_out");
1199162911Ssimon    &cmp	($mark,0);		# was the key schedule copied?
1200160814Ssimon    &mov	("edi",$_key);
1201162911Ssimon    &je		(&label("skip_dzero"));
1202160814Ssimon    # zero copy of key schedule
1203160814Ssimon    &mov	("ecx",240/4);
1204160814Ssimon    &xor	("eax","eax");
1205160814Ssimon    &align	(4);
1206162911Ssimon    &data_word(0xABF3F689);	# rep stosd
1207162911Ssimon    &set_label("skip_dzero")
1208194206Ssimon    &mov	("esp",$_esp);
1209160814Ssimon    &popf	();
1210160814Ssimon&function_end("AES_cbc_encrypt");
1211160814Ssimon}
1212160814Ssimon
1213160814Ssimon#------------------------------------------------------------------#
1214160814Ssimon
1215160814Ssimonsub enckey()
1216160814Ssimon{
1217160814Ssimon	&movz	("esi",&LB("edx"));		# rk[i]>>0
1218160814Ssimon	&mov	("ebx",&DWP(2,"ebp","esi",8));
1219160814Ssimon	&movz	("esi",&HB("edx"));		# rk[i]>>8
1220160814Ssimon	&and	("ebx",0xFF000000);
1221160814Ssimon	&xor	("eax","ebx");
1222160814Ssimon
1223160814Ssimon	&mov	("ebx",&DWP(2,"ebp","esi",8));
1224160814Ssimon	&shr	("edx",16);
1225160814Ssimon	&and	("ebx",0x000000FF);
1226160814Ssimon	&movz	("esi",&LB("edx"));		# rk[i]>>16
1227160814Ssimon	&xor	("eax","ebx");
1228160814Ssimon
1229160814Ssimon	&mov	("ebx",&DWP(0,"ebp","esi",8));
1230160814Ssimon	&movz	("esi",&HB("edx"));		# rk[i]>>24
1231160814Ssimon	&and	("ebx",0x0000FF00);
1232160814Ssimon	&xor	("eax","ebx");
1233160814Ssimon
1234160814Ssimon	&mov	("ebx",&DWP(0,"ebp","esi",8));
1235160814Ssimon	&and	("ebx",0x00FF0000);
1236160814Ssimon	&xor	("eax","ebx");
1237160814Ssimon
1238160814Ssimon	&xor	("eax",&DWP(2048,"ebp","ecx",4));	# rcon
1239160814Ssimon}
1240160814Ssimon
1241160814Ssimon# int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
1242160814Ssimon#                        AES_KEY *key)
1243160814Ssimon&public_label("AES_Te");
1244160814Ssimon&function_begin("AES_set_encrypt_key");
1245160814Ssimon	&mov	("esi",&wparam(0));		# user supplied key
1246160814Ssimon	&mov	("edi",&wparam(2));		# private key schedule
1247160814Ssimon
1248160814Ssimon	&test	("esi",-1);
1249160814Ssimon	&jz	(&label("badpointer"));
1250160814Ssimon	&test	("edi",-1);
1251160814Ssimon	&jz	(&label("badpointer"));
1252160814Ssimon
1253160814Ssimon	&call	(&label("pic_point"));
1254160814Ssimon	&set_label("pic_point");
1255160814Ssimon	&blindpop("ebp");
1256160814Ssimon	&lea	("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1257160814Ssimon
1258160814Ssimon	&mov	("ecx",&wparam(1));		# number of bits in key
1259160814Ssimon	&cmp	("ecx",128);
1260160814Ssimon	&je	(&label("10rounds"));
1261160814Ssimon	&cmp	("ecx",192);
1262160814Ssimon	&je	(&label("12rounds"));
1263160814Ssimon	&cmp	("ecx",256);
1264160814Ssimon	&je	(&label("14rounds"));
1265160814Ssimon	&mov	("eax",-2);			# invalid number of bits
1266160814Ssimon	&jmp	(&label("exit"));
1267160814Ssimon
1268160814Ssimon    &set_label("10rounds");
1269160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 4 dwords
1270160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
1271160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
1272160814Ssimon	&mov	("edx",&DWP(12,"esi"));
1273160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
1274160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
1275160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
1276160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
1277160814Ssimon
1278160814Ssimon	&xor	("ecx","ecx");
1279160814Ssimon	&jmp	(&label("10shortcut"));
1280160814Ssimon
1281160814Ssimon	&align	(4);
1282160814Ssimon	&set_label("10loop");
1283160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1284160814Ssimon		&mov	("edx",&DWP(12,"edi"));		# rk[3]
1285160814Ssimon	&set_label("10shortcut");
1286160814Ssimon		&enckey	();
1287160814Ssimon
1288160814Ssimon		&mov	(&DWP(16,"edi"),"eax");		# rk[4]
1289160814Ssimon		&xor	("eax",&DWP(4,"edi"));
1290160814Ssimon		&mov	(&DWP(20,"edi"),"eax");		# rk[5]
1291160814Ssimon		&xor	("eax",&DWP(8,"edi"));
1292160814Ssimon		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
1293160814Ssimon		&xor	("eax",&DWP(12,"edi"));
1294160814Ssimon		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
1295160814Ssimon		&inc	("ecx");
1296160814Ssimon		&add	("edi",16);
1297160814Ssimon		&cmp	("ecx",10);
1298160814Ssimon	&jl	(&label("10loop"));
1299160814Ssimon
1300160814Ssimon	&mov	(&DWP(80,"edi"),10);		# setup number of rounds
1301160814Ssimon	&xor	("eax","eax");
1302160814Ssimon	&jmp	(&label("exit"));
1303160814Ssimon
1304160814Ssimon    &set_label("12rounds");
1305160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 6 dwords
1306160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
1307160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
1308160814Ssimon	&mov	("edx",&DWP(12,"esi"));
1309160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
1310160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
1311160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
1312160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
1313160814Ssimon	&mov	("ecx",&DWP(16,"esi"));
1314160814Ssimon	&mov	("edx",&DWP(20,"esi"));
1315160814Ssimon	&mov	(&DWP(16,"edi"),"ecx");
1316160814Ssimon	&mov	(&DWP(20,"edi"),"edx");
1317160814Ssimon
1318160814Ssimon	&xor	("ecx","ecx");
1319160814Ssimon	&jmp	(&label("12shortcut"));
1320160814Ssimon
1321160814Ssimon	&align	(4);
1322160814Ssimon	&set_label("12loop");
1323160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1324160814Ssimon		&mov	("edx",&DWP(20,"edi"));		# rk[5]
1325160814Ssimon	&set_label("12shortcut");
1326160814Ssimon		&enckey	();
1327160814Ssimon
1328160814Ssimon		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
1329160814Ssimon		&xor	("eax",&DWP(4,"edi"));
1330160814Ssimon		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
1331160814Ssimon		&xor	("eax",&DWP(8,"edi"));
1332160814Ssimon		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
1333160814Ssimon		&xor	("eax",&DWP(12,"edi"));
1334160814Ssimon		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
1335160814Ssimon
1336160814Ssimon		&cmp	("ecx",7);
1337160814Ssimon		&je	(&label("12break"));
1338160814Ssimon		&inc	("ecx");
1339160814Ssimon
1340160814Ssimon		&xor	("eax",&DWP(16,"edi"));
1341160814Ssimon		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
1342160814Ssimon		&xor	("eax",&DWP(20,"edi"));
1343160814Ssimon		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
1344160814Ssimon
1345160814Ssimon		&add	("edi",24);
1346160814Ssimon	&jmp	(&label("12loop"));
1347160814Ssimon
1348160814Ssimon	&set_label("12break");
1349160814Ssimon	&mov	(&DWP(72,"edi"),12);		# setup number of rounds
1350160814Ssimon	&xor	("eax","eax");
1351160814Ssimon	&jmp	(&label("exit"));
1352160814Ssimon
1353160814Ssimon    &set_label("14rounds");
1354160814Ssimon	&mov	("eax",&DWP(0,"esi"));		# copy first 8 dwords
1355160814Ssimon	&mov	("ebx",&DWP(4,"esi"));
1356160814Ssimon	&mov	("ecx",&DWP(8,"esi"));
1357160814Ssimon	&mov	("edx",&DWP(12,"esi"));
1358160814Ssimon	&mov	(&DWP(0,"edi"),"eax");
1359160814Ssimon	&mov	(&DWP(4,"edi"),"ebx");
1360160814Ssimon	&mov	(&DWP(8,"edi"),"ecx");
1361160814Ssimon	&mov	(&DWP(12,"edi"),"edx");
1362160814Ssimon	&mov	("eax",&DWP(16,"esi"));
1363160814Ssimon	&mov	("ebx",&DWP(20,"esi"));
1364160814Ssimon	&mov	("ecx",&DWP(24,"esi"));
1365160814Ssimon	&mov	("edx",&DWP(28,"esi"));
1366160814Ssimon	&mov	(&DWP(16,"edi"),"eax");
1367160814Ssimon	&mov	(&DWP(20,"edi"),"ebx");
1368160814Ssimon	&mov	(&DWP(24,"edi"),"ecx");
1369160814Ssimon	&mov	(&DWP(28,"edi"),"edx");
1370160814Ssimon
1371160814Ssimon	&xor	("ecx","ecx");
1372160814Ssimon	&jmp	(&label("14shortcut"));
1373160814Ssimon
1374160814Ssimon	&align	(4);
1375160814Ssimon	&set_label("14loop");
1376160814Ssimon		&mov	("edx",&DWP(28,"edi"));		# rk[7]
1377160814Ssimon	&set_label("14shortcut");
1378160814Ssimon		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1379160814Ssimon
1380160814Ssimon		&enckey	();
1381160814Ssimon
1382160814Ssimon		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
1383160814Ssimon		&xor	("eax",&DWP(4,"edi"));
1384160814Ssimon		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
1385160814Ssimon		&xor	("eax",&DWP(8,"edi"));
1386160814Ssimon		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
1387160814Ssimon		&xor	("eax",&DWP(12,"edi"));
1388160814Ssimon		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
1389160814Ssimon
1390160814Ssimon		&cmp	("ecx",6);
1391160814Ssimon		&je	(&label("14break"));
1392160814Ssimon		&inc	("ecx");
1393160814Ssimon
1394160814Ssimon		&mov	("edx","eax");
1395160814Ssimon		&mov	("eax",&DWP(16,"edi"));		# rk[4]
1396160814Ssimon		&movz	("esi",&LB("edx"));		# rk[11]>>0
1397160814Ssimon		&mov	("ebx",&DWP(2,"ebp","esi",8));
1398160814Ssimon		&movz	("esi",&HB("edx"));		# rk[11]>>8
1399160814Ssimon		&and	("ebx",0x000000FF);
1400160814Ssimon		&xor	("eax","ebx");
1401160814Ssimon
1402160814Ssimon		&mov	("ebx",&DWP(0,"ebp","esi",8));
1403160814Ssimon		&shr	("edx",16);
1404160814Ssimon		&and	("ebx",0x0000FF00);
1405160814Ssimon		&movz	("esi",&LB("edx"));		# rk[11]>>16
1406160814Ssimon		&xor	("eax","ebx");
1407160814Ssimon
1408160814Ssimon		&mov	("ebx",&DWP(0,"ebp","esi",8));
1409160814Ssimon		&movz	("esi",&HB("edx"));		# rk[11]>>24
1410160814Ssimon		&and	("ebx",0x00FF0000);
1411160814Ssimon		&xor	("eax","ebx");
1412160814Ssimon
1413160814Ssimon		&mov	("ebx",&DWP(2,"ebp","esi",8));
1414160814Ssimon		&and	("ebx",0xFF000000);
1415160814Ssimon		&xor	("eax","ebx");
1416160814Ssimon
1417160814Ssimon		&mov	(&DWP(48,"edi"),"eax");		# rk[12]
1418160814Ssimon		&xor	("eax",&DWP(20,"edi"));
1419160814Ssimon		&mov	(&DWP(52,"edi"),"eax");		# rk[13]
1420160814Ssimon		&xor	("eax",&DWP(24,"edi"));
1421160814Ssimon		&mov	(&DWP(56,"edi"),"eax");		# rk[14]
1422160814Ssimon		&xor	("eax",&DWP(28,"edi"));
1423160814Ssimon		&mov	(&DWP(60,"edi"),"eax");		# rk[15]
1424160814Ssimon
1425160814Ssimon		&add	("edi",32);
1426160814Ssimon	&jmp	(&label("14loop"));
1427160814Ssimon
1428160814Ssimon	&set_label("14break");
1429160814Ssimon	&mov	(&DWP(48,"edi"),14);		# setup number of rounds
1430160814Ssimon	&xor	("eax","eax");
1431160814Ssimon	&jmp	(&label("exit"));
1432160814Ssimon
1433160814Ssimon    &set_label("badpointer");
1434160814Ssimon	&mov	("eax",-1);
1435160814Ssimon    &set_label("exit");
1436160814Ssimon&function_end("AES_set_encrypt_key");
1437160814Ssimon
1438160814Ssimonsub deckey()
1439160814Ssimon{ my ($i,$ptr,$te,$td) = @_;
1440160814Ssimon
1441160814Ssimon	&mov	("eax",&DWP($i,$ptr));
1442160814Ssimon	&mov	("edx","eax");
1443160814Ssimon	&movz	("ebx",&HB("eax"));
1444160814Ssimon	&shr	("edx",16);
1445160814Ssimon	&and	("eax",0xFF);
1446160814Ssimon	&movz	("eax",&BP(2,$te,"eax",8));
1447160814Ssimon	&movz	("ebx",&BP(2,$te,"ebx",8));
1448160814Ssimon	&mov	("eax",&DWP(0,$td,"eax",8));
1449160814Ssimon	&xor	("eax",&DWP(3,$td,"ebx",8));
1450160814Ssimon	&movz	("ebx",&HB("edx"));
1451160814Ssimon	&and	("edx",0xFF);
1452160814Ssimon	&movz	("edx",&BP(2,$te,"edx",8));
1453160814Ssimon	&movz	("ebx",&BP(2,$te,"ebx",8));
1454160814Ssimon	&xor	("eax",&DWP(2,$td,"edx",8));
1455160814Ssimon	&xor	("eax",&DWP(1,$td,"ebx",8));
1456160814Ssimon	&mov	(&DWP($i,$ptr),"eax");
1457160814Ssimon}
1458160814Ssimon
1459160814Ssimon# int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
1460160814Ssimon#                        AES_KEY *key)
1461160814Ssimon&public_label("AES_Td");
1462160814Ssimon&public_label("AES_Te");
1463160814Ssimon&function_begin_B("AES_set_decrypt_key");
1464160814Ssimon	&mov	("eax",&wparam(0));
1465160814Ssimon	&mov	("ecx",&wparam(1));
1466160814Ssimon	&mov	("edx",&wparam(2));
1467160814Ssimon	&sub	("esp",12);
1468160814Ssimon	&mov	(&DWP(0,"esp"),"eax");
1469160814Ssimon	&mov	(&DWP(4,"esp"),"ecx");
1470160814Ssimon	&mov	(&DWP(8,"esp"),"edx");
1471160814Ssimon	&call	("AES_set_encrypt_key");
1472160814Ssimon	&add	("esp",12);
1473160814Ssimon	&cmp	("eax",0);
1474160814Ssimon	&je	(&label("proceed"));
1475160814Ssimon	&ret	();
1476160814Ssimon
1477160814Ssimon    &set_label("proceed");
1478160814Ssimon	&push	("ebp");
1479160814Ssimon	&push	("ebx");
1480160814Ssimon	&push	("esi");
1481160814Ssimon	&push	("edi");
1482160814Ssimon
1483160814Ssimon	&mov	("esi",&wparam(2));
1484160814Ssimon	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
1485160814Ssimon	&lea	("ecx",&DWP(0,"","ecx",4));
1486160814Ssimon	&lea	("edi",&DWP(0,"esi","ecx",4));	# pointer to last chunk
1487160814Ssimon
1488160814Ssimon	&align	(4);
1489160814Ssimon	&set_label("invert");			# invert order of chunks
1490160814Ssimon		&mov	("eax",&DWP(0,"esi"));
1491160814Ssimon		&mov	("ebx",&DWP(4,"esi"));
1492160814Ssimon		&mov	("ecx",&DWP(0,"edi"));
1493160814Ssimon		&mov	("edx",&DWP(4,"edi"));
1494160814Ssimon		&mov	(&DWP(0,"edi"),"eax");
1495160814Ssimon		&mov	(&DWP(4,"edi"),"ebx");
1496160814Ssimon		&mov	(&DWP(0,"esi"),"ecx");
1497160814Ssimon		&mov	(&DWP(4,"esi"),"edx");
1498160814Ssimon		&mov	("eax",&DWP(8,"esi"));
1499160814Ssimon		&mov	("ebx",&DWP(12,"esi"));
1500160814Ssimon		&mov	("ecx",&DWP(8,"edi"));
1501160814Ssimon		&mov	("edx",&DWP(12,"edi"));
1502160814Ssimon		&mov	(&DWP(8,"edi"),"eax");
1503160814Ssimon		&mov	(&DWP(12,"edi"),"ebx");
1504160814Ssimon		&mov	(&DWP(8,"esi"),"ecx");
1505160814Ssimon		&mov	(&DWP(12,"esi"),"edx");
1506160814Ssimon		&add	("esi",16);
1507160814Ssimon		&sub	("edi",16);
1508160814Ssimon		&cmp	("esi","edi");
1509160814Ssimon	&jne	(&label("invert"));
1510160814Ssimon
1511160814Ssimon	&call	(&label("pic_point"));
1512160814Ssimon	&set_label("pic_point");
1513160814Ssimon	blindpop("ebp");
1514160814Ssimon	&lea	("edi",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
1515160814Ssimon	&lea	("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1516160814Ssimon
1517160814Ssimon	&mov	("esi",&wparam(2));
1518160814Ssimon	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
1519160814Ssimon	&dec	("ecx");
1520160814Ssimon	&align	(4);
1521160814Ssimon	&set_label("permute");			# permute the key schedule
1522160814Ssimon		&add	("esi",16);
1523160814Ssimon		&deckey	(0,"esi","ebp","edi");
1524160814Ssimon		&deckey	(4,"esi","ebp","edi");
1525160814Ssimon		&deckey	(8,"esi","ebp","edi");
1526160814Ssimon		&deckey	(12,"esi","ebp","edi");
1527160814Ssimon		&dec	("ecx");
1528160814Ssimon	&jnz	(&label("permute"));
1529160814Ssimon
1530160814Ssimon	&xor	("eax","eax");			# return success
1531160814Ssimon&function_end("AES_set_decrypt_key");
1532160814Ssimon
1533160814Ssimon&asm_finish();
1534