1#!/usr/bin/env perl
2#
3# ====================================================================
4# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5# project. Rights for redistribution and usage in source and binary
6# forms are granted according to the OpenSSL license.
7# ====================================================================
8#
9# Version 3.6.
10#
11# You might fail to appreciate this module performance from the first
12# try. If compared to "vanilla" linux-ia32-icc target, i.e. considered
13# to be *the* best Intel C compiler without -KPIC, performance appears
14# to be virtually identical... But try to re-configure with shared
15# library support... Aha! Intel compiler "suddenly" lags behind by 30%
16# [on P4, more on others]:-) And if compared to position-independent
17# code generated by GNU C, this code performs *more* than *twice* as
18# fast! Yes, all this buzz about PIC means that unlike other hand-
19# coded implementations, this one was explicitly designed to be safe
20# to use even in shared library context... This also means that this
21# code isn't necessarily absolutely fastest "ever," because in order
22# to achieve position independence an extra register has to be
23# off-loaded to stack, which affects the benchmark result.
24#
25# Special note about instruction choice. Do you recall RC4_INT code
26# performing poorly on P4? It might be the time to figure out why.
27# RC4_INT code implies effective address calculations in base+offset*4
28# form. Trouble is that it seems that offset scaling turned to be
29# critical path... At least eliminating scaling resulted in 2.8x RC4
30# performance improvement [as you might recall]. As AES code is hungry
31# for scaling too, I [try to] avoid the latter by favoring off-by-2
32# shifts and masking the result with 0xFF<<2 instead of "boring" 0xFF.
33#
34# As was shown by Dean Gaudet <dean@arctic.org>, the above note turned
35# void. Performance improvement with off-by-2 shifts was observed on
36# intermediate implementation, which was spilling yet another register
37# to stack... Final offset*4 code below runs just a tad faster on P4,
38# but exhibits up to 10% improvement on other cores.
39#
40# Second version is "monolithic" replacement for aes_core.c, which in
41# addition to AES_[de|en]crypt implements AES_set_[de|en]cryption_key.
42# This made it possible to implement little-endian variant of the
43# algorithm without modifying the base C code. Motivating factor for
44# the undertaken effort was that it appeared that in tight IA-32
45# register window little-endian flavor could achieve slightly higher
46# Instruction Level Parallelism, and it indeed resulted in up to 15%
47# better performance on most recent �-archs...
48#
49# Third version adds AES_cbc_encrypt implementation, which resulted in
50# up to 40% performance imrovement of CBC benchmark results. 40% was
51# observed on P4 core, where "overall" imrovement coefficient, i.e. if
52# compared to PIC generated by GCC and in CBC mode, was observed to be
53# as large as 4x:-) CBC performance is virtually identical to ECB now
54# and on some platforms even better, e.g. 17.6 "small" cycles/byte on
55# Opteron, because certain function prologues and epilogues are
56# effectively taken out of the loop...
57#
58# Version 3.2 implements compressed tables and prefetch of these tables
59# in CBC[!] mode. Former means that 3/4 of table references are now
60# misaligned, which unfortunately has negative impact on elder IA-32
61# implementations, Pentium suffered 30% penalty, PIII - 10%.
62#
63# Version 3.3 avoids L1 cache aliasing between stack frame and
64# S-boxes, and 3.4 - L1 cache aliasing even between key schedule. The
65# latter is achieved by copying the key schedule to controlled place in
66# stack. This unfortunately has rather strong impact on small block CBC
67# performance, ~2x deterioration on 16-byte block if compared to 3.3.
68#
69# Version 3.5 checks if there is L1 cache aliasing between user-supplied
70# key schedule and S-boxes and abstains from copying the former if
71# there is no. This allows end-user to consciously retain small block
72# performance by aligning key schedule in specific manner.
73#
74# Version 3.6 compresses Td4 to 256 bytes and prefetches it in ECB.
75#
76# Current ECB performance numbers for 128-bit key in CPU cycles per
77# processed byte [measure commonly used by AES benchmarkers] are:
78#
79#		small footprint		fully unrolled
80# P4		24			22
81# AMD K8	20			19
82# PIII		25			23
83# Pentium	81			78
84
85push(@INC,"perlasm","../../perlasm");
86require "x86asm.pl";
87
88&asm_init($ARGV[0],"aes-586.pl",$ARGV[$#ARGV] eq "386");
89
90$s0="eax";
91$s1="ebx";
92$s2="ecx";
93$s3="edx";
94$key="edi";
95$acc="esi";
96
97$compromise=0;		# $compromise=128 abstains from copying key
98			# schedule to stack when encrypting inputs
99			# shorter than 128 bytes at the cost of
100			# risksing aliasing with S-boxes. In return
101			# you get way better, up to +70%, small block
102			# performance.
103$small_footprint=1;	# $small_footprint=1 code is ~5% slower [on
104			# recent �-archs], but ~5 times smaller!
105			# I favor compact code to minimize cache
106			# contention and in hope to "collect" 5% back
107			# in real-life applications...
108$vertical_spin=0;	# shift "verticaly" defaults to 0, because of
109			# its proof-of-concept status...
110
111# Note that there is no decvert(), as well as last encryption round is
112# performed with "horizontal" shifts. This is because this "vertical"
113# implementation [one which groups shifts on a given $s[i] to form a
114# "column," unlike "horizontal" one, which groups shifts on different
115# $s[i] to form a "row"] is work in progress. It was observed to run
116# few percents faster on Intel cores, but not AMD. On AMD K8 core it's
117# whole 12% slower:-( So we face a trade-off... Shall it be resolved
118# some day? Till then the code is considered experimental and by
119# default remains dormant...
120
121sub encvert()
122{ my ($te,@s) = @_;
123  my $v0 = $acc, $v1 = $key;
124
125	&mov	($v0,$s[3]);				# copy s3
126	&mov	(&DWP(4,"esp"),$s[2]);			# save s2
127	&mov	($v1,$s[0]);				# copy s0
128	&mov	(&DWP(8,"esp"),$s[1]);			# save s1
129
130	&movz	($s[2],&HB($s[0]));
131	&and	($s[0],0xFF);
132	&mov	($s[0],&DWP(0,$te,$s[0],8));		# s0>>0
133	&shr	($v1,16);
134	&mov	($s[3],&DWP(3,$te,$s[2],8));		# s0>>8
135	&movz	($s[1],&HB($v1));
136	&and	($v1,0xFF);
137	&mov	($s[2],&DWP(2,$te,$v1,8));		# s0>>16
138	 &mov	($v1,$v0);
139	&mov	($s[1],&DWP(1,$te,$s[1],8));		# s0>>24
140
141	&and	($v0,0xFF);
142	&xor	($s[3],&DWP(0,$te,$v0,8));		# s3>>0
143	&movz	($v0,&HB($v1));
144	&shr	($v1,16);
145	&xor	($s[2],&DWP(3,$te,$v0,8));		# s3>>8
146	&movz	($v0,&HB($v1));
147	&and	($v1,0xFF);
148	&xor	($s[1],&DWP(2,$te,$v1,8));		# s3>>16
149	 &mov	($v1,&DWP(4,"esp"));			# restore s2
150	&xor	($s[0],&DWP(1,$te,$v0,8));		# s3>>24
151
152	&mov	($v0,$v1);
153	&and	($v1,0xFF);
154	&xor	($s[2],&DWP(0,$te,$v1,8));		# s2>>0
155	&movz	($v1,&HB($v0));
156	&shr	($v0,16);
157	&xor	($s[1],&DWP(3,$te,$v1,8));		# s2>>8
158	&movz	($v1,&HB($v0));
159	&and	($v0,0xFF);
160	&xor	($s[0],&DWP(2,$te,$v0,8));		# s2>>16
161	 &mov	($v0,&DWP(8,"esp"));			# restore s1
162	&xor	($s[3],&DWP(1,$te,$v1,8));		# s2>>24
163
164	&mov	($v1,$v0);
165	&and	($v0,0xFF);
166	&xor	($s[1],&DWP(0,$te,$v0,8));		# s1>>0
167	&movz	($v0,&HB($v1));
168	&shr	($v1,16);
169	&xor	($s[0],&DWP(3,$te,$v0,8));		# s1>>8
170	&movz	($v0,&HB($v1));
171	&and	($v1,0xFF);
172	&xor	($s[3],&DWP(2,$te,$v1,8));		# s1>>16
173	 &mov	($key,&DWP(12,"esp"));			# reincarnate v1 as key
174	&xor	($s[2],&DWP(1,$te,$v0,8));		# s1>>24
175}
176
177sub encstep()
178{ my ($i,$te,@s) = @_;
179  my $tmp = $key;
180  my $out = $i==3?$s[0]:$acc;
181
182	# lines marked with #%e?x[i] denote "reordered" instructions...
183	if ($i==3)  {	&mov	($key,&DWP(12,"esp"));		}##%edx
184	else        {	&mov	($out,$s[0]);
185			&and	($out,0xFF);			}
186	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
187	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
188			&mov	($out,&DWP(0,$te,$out,8));
189
190	if ($i==3)  {	$tmp=$s[1];				}##%eax
191			&movz	($tmp,&HB($s[1]));
192			&xor	($out,&DWP(3,$te,$tmp,8));
193
194	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],&DWP(4,"esp"));	}##%ebx
195	else        {	&mov	($tmp,$s[2]);
196			&shr	($tmp,16);			}
197	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
198			&and	($tmp,0xFF);
199			&xor	($out,&DWP(2,$te,$tmp,8));
200
201	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}##%ecx
202	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
203	else        {	&mov	($tmp,$s[3]);
204			&shr	($tmp,24)			}
205			&xor	($out,&DWP(1,$te,$tmp,8));
206	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
207	if ($i==3)  {	&mov	($s[3],$acc);			}
208			&comment();
209}
210
211sub enclast()
212{ my ($i,$te,@s)=@_;
213  my $tmp = $key;
214  my $out = $i==3?$s[0]:$acc;
215
216	if ($i==3)  {	&mov	($key,&DWP(12,"esp"));		}##%edx
217	else        {	&mov	($out,$s[0]);			}
218			&and	($out,0xFF);
219	if ($i==1)  {	&shr	($s[0],16);			}#%ebx[1]
220	if ($i==2)  {	&shr	($s[0],24);			}#%ecx[2]
221			&mov	($out,&DWP(2,$te,$out,8));
222			&and	($out,0x000000ff);
223
224	if ($i==3)  {	$tmp=$s[1];				}##%eax
225			&movz	($tmp,&HB($s[1]));
226			&mov	($tmp,&DWP(0,$te,$tmp,8));
227			&and	($tmp,0x0000ff00);
228			&xor	($out,$tmp);
229
230	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],&DWP(4,"esp"));	}##%ebx
231	else        {	mov	($tmp,$s[2]);
232			&shr	($tmp,16);			}
233	if ($i==2)  {	&and	($s[1],0xFF);			}#%edx[2]
234			&and	($tmp,0xFF);
235			&mov	($tmp,&DWP(0,$te,$tmp,8));
236			&and	($tmp,0x00ff0000);
237			&xor	($out,$tmp);
238
239	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}##%ecx
240	elsif($i==2){	&movz	($tmp,&HB($s[3]));		}#%ebx[2]
241	else        {	&mov	($tmp,$s[3]);
242			&shr	($tmp,24);			}
243			&mov	($tmp,&DWP(2,$te,$tmp,8));
244			&and	($tmp,0xff000000);
245			&xor	($out,$tmp);
246	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
247	if ($i==3)  {	&mov	($s[3],$acc);			}
248}
249
250sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
251
252&public_label("AES_Te");
253&function_begin_B("_x86_AES_encrypt");
254	if ($vertical_spin) {
255		# I need high parts of volatile registers to be accessible...
256		&exch	($s1="edi",$key="ebx");
257		&mov	($s2="esi",$acc="ecx");
258	}
259
260	# note that caller is expected to allocate stack frame for me!
261	&mov	(&DWP(12,"esp"),$key);		# save key
262
263	&xor	($s0,&DWP(0,$key));		# xor with key
264	&xor	($s1,&DWP(4,$key));
265	&xor	($s2,&DWP(8,$key));
266	&xor	($s3,&DWP(12,$key));
267
268	&mov	($acc,&DWP(240,$key));		# load key->rounds
269
270	if ($small_footprint) {
271	    &lea	($acc,&DWP(-2,$acc,$acc));
272	    &lea	($acc,&DWP(0,$key,$acc,8));
273	    &mov	(&DWP(16,"esp"),$acc);	# end of key schedule
274	    &align	(4);
275	    &set_label("loop");
276		if ($vertical_spin) {
277		    &encvert("ebp",$s0,$s1,$s2,$s3);
278		} else {
279		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
280		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
281		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
282		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
283		}
284		&add	($key,16);		# advance rd_key
285		&xor	($s0,&DWP(0,$key));
286		&xor	($s1,&DWP(4,$key));
287		&xor	($s2,&DWP(8,$key));
288		&xor	($s3,&DWP(12,$key));
289	    &cmp	($key,&DWP(16,"esp"));
290	    &mov	(&DWP(12,"esp"),$key);
291	    &jb		(&label("loop"));
292	}
293	else {
294	    &cmp	($acc,10);
295	    &jle	(&label("10rounds"));
296	    &cmp	($acc,12);
297	    &jle	(&label("12rounds"));
298
299	&set_label("14rounds");
300	    for ($i=1;$i<3;$i++) {
301		if ($vertical_spin) {
302		    &encvert("ebp",$s0,$s1,$s2,$s3);
303		} else {
304		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
305		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
306		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
307		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
308		}
309		&xor	($s0,&DWP(16*$i+0,$key));
310		&xor	($s1,&DWP(16*$i+4,$key));
311		&xor	($s2,&DWP(16*$i+8,$key));
312		&xor	($s3,&DWP(16*$i+12,$key));
313	    }
314	    &add	($key,32);
315	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
316	&set_label("12rounds");
317	    for ($i=1;$i<3;$i++) {
318		if ($vertical_spin) {
319		    &encvert("ebp",$s0,$s1,$s2,$s3);
320		} else {
321		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
322		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
323		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
324		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
325		}
326		&xor	($s0,&DWP(16*$i+0,$key));
327		&xor	($s1,&DWP(16*$i+4,$key));
328		&xor	($s2,&DWP(16*$i+8,$key));
329		&xor	($s3,&DWP(16*$i+12,$key));
330	    }
331	    &add	($key,32);
332	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
333	&set_label("10rounds");
334	    for ($i=1;$i<10;$i++) {
335		if ($vertical_spin) {
336		    &encvert("ebp",$s0,$s1,$s2,$s3);
337		} else {
338		    &encstep(0,"ebp",$s0,$s1,$s2,$s3);
339		    &encstep(1,"ebp",$s1,$s2,$s3,$s0);
340		    &encstep(2,"ebp",$s2,$s3,$s0,$s1);
341		    &encstep(3,"ebp",$s3,$s0,$s1,$s2);
342		}
343		&xor	($s0,&DWP(16*$i+0,$key));
344		&xor	($s1,&DWP(16*$i+4,$key));
345		&xor	($s2,&DWP(16*$i+8,$key));
346		&xor	($s3,&DWP(16*$i+12,$key));
347	    }
348	}
349
350	if ($vertical_spin) {
351	    # "reincarnate" some registers for "horizontal" spin...
352	    &mov	($s1="ebx",$key="edi");
353	    &mov	($s2="ecx",$acc="esi");
354	}
355	&enclast(0,"ebp",$s0,$s1,$s2,$s3);
356	&enclast(1,"ebp",$s1,$s2,$s3,$s0);
357	&enclast(2,"ebp",$s2,$s3,$s0,$s1);
358	&enclast(3,"ebp",$s3,$s0,$s1,$s2);
359
360	&add	($key,$small_footprint?16:160);
361	&xor	($s0,&DWP(0,$key));
362	&xor	($s1,&DWP(4,$key));
363	&xor	($s2,&DWP(8,$key));
364	&xor	($s3,&DWP(12,$key));
365
366	&ret	();
367
368&set_label("AES_Te",64);	# Yes! I keep it in the code segment!
369	&_data_word(0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6);
370	&_data_word(0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591);
371	&_data_word(0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56);
372	&_data_word(0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec);
373	&_data_word(0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa);
374	&_data_word(0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb);
375	&_data_word(0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45);
376	&_data_word(0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b);
377	&_data_word(0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c);
378	&_data_word(0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83);
379	&_data_word(0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9);
380	&_data_word(0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a);
381	&_data_word(0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d);
382	&_data_word(0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f);
383	&_data_word(0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df);
384	&_data_word(0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea);
385	&_data_word(0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34);
386	&_data_word(0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b);
387	&_data_word(0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d);
388	&_data_word(0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413);
389	&_data_word(0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1);
390	&_data_word(0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6);
391	&_data_word(0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972);
392	&_data_word(0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85);
393	&_data_word(0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed);
394	&_data_word(0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511);
395	&_data_word(0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe);
396	&_data_word(0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b);
397	&_data_word(0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05);
398	&_data_word(0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1);
399	&_data_word(0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142);
400	&_data_word(0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf);
401	&_data_word(0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3);
402	&_data_word(0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e);
403	&_data_word(0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a);
404	&_data_word(0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6);
405	&_data_word(0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3);
406	&_data_word(0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b);
407	&_data_word(0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428);
408	&_data_word(0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad);
409	&_data_word(0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14);
410	&_data_word(0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8);
411	&_data_word(0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4);
412	&_data_word(0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2);
413	&_data_word(0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda);
414	&_data_word(0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949);
415	&_data_word(0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf);
416	&_data_word(0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810);
417	&_data_word(0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c);
418	&_data_word(0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697);
419	&_data_word(0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e);
420	&_data_word(0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f);
421	&_data_word(0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc);
422	&_data_word(0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c);
423	&_data_word(0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969);
424	&_data_word(0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27);
425	&_data_word(0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122);
426	&_data_word(0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433);
427	&_data_word(0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9);
428	&_data_word(0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5);
429	&_data_word(0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a);
430	&_data_word(0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0);
431	&_data_word(0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e);
432	&_data_word(0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c);
433#rcon:
434	&data_word(0x00000001, 0x00000002, 0x00000004, 0x00000008);
435	&data_word(0x00000010, 0x00000020, 0x00000040, 0x00000080);
436	&data_word(0x0000001b, 0x00000036, 0, 0, 0, 0, 0, 0);
437&function_end_B("_x86_AES_encrypt");
438
439# void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
440&public_label("AES_Te");
441&function_begin("AES_encrypt");
442	&mov	($acc,&wparam(0));		# load inp
443	&mov	($key,&wparam(2));		# load key
444
445	&mov	($s0,"esp");
446	&sub	("esp",24);
447	&and	("esp",-64);
448	&add	("esp",4);
449	&mov	(&DWP(16,"esp"),$s0);
450
451	&call   (&label("pic_point"));          # make it PIC!
452	&set_label("pic_point");
453	&blindpop("ebp");
454	&lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
455
456	&mov	($s0,&DWP(0,$acc));		# load input data
457	&mov	($s1,&DWP(4,$acc));
458	&mov	($s2,&DWP(8,$acc));
459	&mov	($s3,&DWP(12,$acc));
460
461	&call	("_x86_AES_encrypt");
462
463	&mov	("esp",&DWP(16,"esp"));
464
465	&mov	($acc,&wparam(1));		# load out
466	&mov	(&DWP(0,$acc),$s0);		# write output data
467	&mov	(&DWP(4,$acc),$s1);
468	&mov	(&DWP(8,$acc),$s2);
469	&mov	(&DWP(12,$acc),$s3);
470&function_end("AES_encrypt");
471
472#------------------------------------------------------------------#
473
474sub decstep()
475{ my ($i,$td,@s) = @_;
476  my $tmp = $key;
477  my $out = $i==3?$s[0]:$acc;
478
479	# no instructions are reordered, as performance appears
480	# optimal... or rather that all attempts to reorder didn't
481	# result in better performance [which by the way is not a
482	# bit lower than ecryption].
483	if($i==3)   {	&mov	($key,&DWP(12,"esp"));		}
484	else        {	&mov	($out,$s[0]);			}
485			&and	($out,0xFF);
486			&mov	($out,&DWP(0,$td,$out,8));
487
488	if ($i==3)  {	$tmp=$s[1];				}
489			&movz	($tmp,&HB($s[1]));
490			&xor	($out,&DWP(3,$td,$tmp,8));
491
492	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
493	else        {	&mov	($tmp,$s[2]);			}
494			&shr	($tmp,16);
495			&and	($tmp,0xFF);
496			&xor	($out,&DWP(2,$td,$tmp,8));
497
498	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}
499	else        {	&mov	($tmp,$s[3]);			}
500			&shr	($tmp,24);
501			&xor	($out,&DWP(1,$td,$tmp,8));
502	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
503	if ($i==3)  {	&mov	($s[3],&DWP(4,"esp"));		}
504			&comment();
505}
506
507sub declast()
508{ my ($i,$td,@s)=@_;
509  my $tmp = $key;
510  my $out = $i==3?$s[0]:$acc;
511
512	if($i==3)   {	&mov	($key,&DWP(12,"esp"));		}
513	else        {	&mov	($out,$s[0]);			}
514			&and	($out,0xFF);
515			&movz	($out,&BP(2048,$td,$out,1));
516
517	if ($i==3)  {	$tmp=$s[1];				}
518			&movz	($tmp,&HB($s[1]));
519			&movz	($tmp,&BP(2048,$td,$tmp,1));
520			&shl	($tmp,8);
521			&xor	($out,$tmp);
522
523	if ($i==3)  {	$tmp=$s[2]; &mov ($s[1],$acc);		}
524	else        {	mov	($tmp,$s[2]);			}
525			&shr	($tmp,16);
526			&and	($tmp,0xFF);
527			&movz	($tmp,&BP(2048,$td,$tmp,1));
528			&shl	($tmp,16);
529			&xor	($out,$tmp);
530
531	if ($i==3)  {	$tmp=$s[3]; &mov ($s[2],&DWP(8,"esp"));	}
532	else        {	&mov	($tmp,$s[3]);			}
533			&shr	($tmp,24);
534			&movz	($tmp,&BP(2048,$td,$tmp,1));
535			&shl	($tmp,24);
536			&xor	($out,$tmp);
537	if ($i<2)   {	&mov	(&DWP(4+4*$i,"esp"),$out);	}
538	if ($i==3)  {	&mov	($s[3],&DWP(4,"esp"));		}
539}
540
541&public_label("AES_Td");
542&function_begin_B("_x86_AES_decrypt");
543	# note that caller is expected to allocate stack frame for me!
544	&mov	(&DWP(12,"esp"),$key);		# save key
545
546	&xor	($s0,&DWP(0,$key));		# xor with key
547	&xor	($s1,&DWP(4,$key));
548	&xor	($s2,&DWP(8,$key));
549	&xor	($s3,&DWP(12,$key));
550
551	&mov	($acc,&DWP(240,$key));		# load key->rounds
552
553	if ($small_footprint) {
554	    &lea	($acc,&DWP(-2,$acc,$acc));
555	    &lea	($acc,&DWP(0,$key,$acc,8));
556	    &mov	(&DWP(16,"esp"),$acc);	# end of key schedule
557	    &align	(4);
558	    &set_label("loop");
559		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
560		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
561		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
562		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
563		&add	($key,16);		# advance rd_key
564		&xor	($s0,&DWP(0,$key));
565		&xor	($s1,&DWP(4,$key));
566		&xor	($s2,&DWP(8,$key));
567		&xor	($s3,&DWP(12,$key));
568	    &cmp	($key,&DWP(16,"esp"));
569	    &mov	(&DWP(12,"esp"),$key);
570	    &jb		(&label("loop"));
571	}
572	else {
573	    &cmp	($acc,10);
574	    &jle	(&label("10rounds"));
575	    &cmp	($acc,12);
576	    &jle	(&label("12rounds"));
577
578	&set_label("14rounds");
579	    for ($i=1;$i<3;$i++) {
580		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
581		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
582		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
583		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
584		&xor	($s0,&DWP(16*$i+0,$key));
585		&xor	($s1,&DWP(16*$i+4,$key));
586		&xor	($s2,&DWP(16*$i+8,$key));
587		&xor	($s3,&DWP(16*$i+12,$key));
588	    }
589	    &add	($key,32);
590	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
591	&set_label("12rounds");
592	    for ($i=1;$i<3;$i++) {
593		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
594		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
595		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
596		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
597		&xor	($s0,&DWP(16*$i+0,$key));
598		&xor	($s1,&DWP(16*$i+4,$key));
599		&xor	($s2,&DWP(16*$i+8,$key));
600		&xor	($s3,&DWP(16*$i+12,$key));
601	    }
602	    &add	($key,32);
603	    &mov	(&DWP(12,"esp"),$key);	# advance rd_key
604	&set_label("10rounds");
605	    for ($i=1;$i<10;$i++) {
606		&decstep(0,"ebp",$s0,$s3,$s2,$s1);
607		&decstep(1,"ebp",$s1,$s0,$s3,$s2);
608		&decstep(2,"ebp",$s2,$s1,$s0,$s3);
609		&decstep(3,"ebp",$s3,$s2,$s1,$s0);
610		&xor	($s0,&DWP(16*$i+0,$key));
611		&xor	($s1,&DWP(16*$i+4,$key));
612		&xor	($s2,&DWP(16*$i+8,$key));
613		&xor	($s3,&DWP(16*$i+12,$key));
614	    }
615	}
616
617	&declast(0,"ebp",$s0,$s3,$s2,$s1);
618	&declast(1,"ebp",$s1,$s0,$s3,$s2);
619	&declast(2,"ebp",$s2,$s1,$s0,$s3);
620	&declast(3,"ebp",$s3,$s2,$s1,$s0);
621
622	&add	($key,$small_footprint?16:160);
623	&xor	($s0,&DWP(0,$key));
624	&xor	($s1,&DWP(4,$key));
625	&xor	($s2,&DWP(8,$key));
626	&xor	($s3,&DWP(12,$key));
627
628	&ret	();
629
630&set_label("AES_Td",64);	# Yes! I keep it in the code segment!
631	&_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
632	&_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
633	&_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
634	&_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
635	&_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
636	&_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
637	&_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
638	&_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
639	&_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
640	&_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
641	&_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
642	&_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
643	&_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
644	&_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
645	&_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
646	&_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
647	&_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
648	&_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
649	&_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
650	&_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
651	&_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
652	&_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
653	&_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
654	&_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
655	&_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
656	&_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
657	&_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
658	&_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
659	&_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
660	&_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
661	&_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
662	&_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
663	&_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
664	&_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
665	&_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
666	&_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
667	&_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
668	&_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
669	&_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
670	&_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
671	&_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
672	&_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
673	&_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
674	&_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
675	&_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
676	&_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
677	&_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
678	&_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
679	&_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
680	&_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
681	&_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
682	&_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
683	&_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
684	&_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
685	&_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
686	&_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
687	&_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
688	&_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
689	&_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
690	&_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
691	&_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
692	&_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
693	&_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
694	&_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
695#Td4:
696	&data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
697	&data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
698	&data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
699	&data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
700	&data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
701	&data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
702	&data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
703	&data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
704	&data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
705	&data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
706	&data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
707	&data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
708	&data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
709	&data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
710	&data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
711	&data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
712	&data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
713	&data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
714	&data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
715	&data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
716	&data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
717	&data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
718	&data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
719	&data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
720	&data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
721	&data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
722	&data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
723	&data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
724	&data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
725	&data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
726	&data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
727	&data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
728&function_end_B("_x86_AES_decrypt");
729
730# void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
731&public_label("AES_Td");
732&function_begin("AES_decrypt");
733	&mov	($acc,&wparam(0));		# load inp
734	&mov	($key,&wparam(2));		# load key
735
736	&mov	($s0,"esp");
737	&sub	("esp",24);
738	&and	("esp",-64);
739	&add	("esp",4);
740	&mov	(&DWP(16,"esp"),$s0);
741
742	&call   (&label("pic_point"));          # make it PIC!
743	&set_label("pic_point");
744	&blindpop("ebp");
745	&lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
746
747	# prefetch Td4
748	&lea	("ebp",&DWP(2048+128,"ebp"));
749	&mov	($s0,&DWP(0-128,"ebp"));
750	&mov	($s1,&DWP(32-128,"ebp"));
751	&mov	($s2,&DWP(64-128,"ebp"));
752	&mov	($s3,&DWP(96-128,"ebp"));
753	&mov	($s0,&DWP(128-128,"ebp"));
754	&mov	($s1,&DWP(160-128,"ebp"));
755	&mov	($s2,&DWP(192-128,"ebp"));
756	&mov	($s3,&DWP(224-128,"ebp"));
757	&lea	("ebp",&DWP(-2048-128,"ebp"));
758
759	&mov	($s0,&DWP(0,$acc));		# load input data
760	&mov	($s1,&DWP(4,$acc));
761	&mov	($s2,&DWP(8,$acc));
762	&mov	($s3,&DWP(12,$acc));
763
764	&call	("_x86_AES_decrypt");
765
766	&mov	("esp",&DWP(16,"esp"));
767
768	&mov	($acc,&wparam(1));		# load out
769	&mov	(&DWP(0,$acc),$s0);		# write output data
770	&mov	(&DWP(4,$acc),$s1);
771	&mov	(&DWP(8,$acc),$s2);
772	&mov	(&DWP(12,$acc),$s3);
773&function_end("AES_decrypt");
774
775# void AES_cbc_encrypt (const void char *inp, unsigned char *out,
776#			size_t length, const AES_KEY *key,
777#			unsigned char *ivp,const int enc);
778{
779# stack frame layout
780# -4(%esp)	0(%esp)		return address
781# 0(%esp)	4(%esp)		tmp1
782# 4(%esp)	8(%esp)		tmp2
783# 8(%esp)	12(%esp)	key
784# 12(%esp)	16(%esp)	end of key schedule
785my $_esp=&DWP(16,"esp");	#saved %esp
786my $_inp=&DWP(20,"esp");	#copy of wparam(0)
787my $_out=&DWP(24,"esp");	#copy of wparam(1)
788my $_len=&DWP(28,"esp");	#copy of wparam(2)
789my $_key=&DWP(32,"esp");	#copy of wparam(3)
790my $_ivp=&DWP(36,"esp");	#copy of wparam(4)
791my $_tmp=&DWP(40,"esp");	#volatile variable
792my $ivec=&DWP(44,"esp");	#ivec[16]
793my $aes_key=&DWP(60,"esp");	#copy of aes_key
794my $mark=&DWP(60+240,"esp");	#copy of aes_key->rounds
795
796&public_label("AES_Te");
797&public_label("AES_Td");
798&function_begin("AES_cbc_encrypt");
799	&mov	($s2 eq "ecx"? $s2 : "",&wparam(2));	# load len
800	&cmp	($s2,0);
801	&je	(&label("enc_out"));
802
803	&call   (&label("pic_point"));		# make it PIC!
804	&set_label("pic_point");
805	&blindpop("ebp");
806
807	&pushf	();
808	&cld	();
809
810	&cmp	(&wparam(5),0);
811	&je	(&label("DECRYPT"));
812
813	&lea    ("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
814
815	# allocate aligned stack frame...
816	&lea	($key,&DWP(-64-244,"esp"));
817	&and	($key,-64);
818
819	# ... and make sure it doesn't alias with AES_Te modulo 4096
820	&mov	($s0,"ebp");
821	&lea	($s1,&DWP(2048,"ebp"));
822	&mov	($s3,$key);
823	&and	($s0,0xfff);		# s = %ebp&0xfff
824	&and	($s1,0xfff);		# e = (%ebp+2048)&0xfff
825	&and	($s3,0xfff);		# p = %esp&0xfff
826
827	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
828	&jb	(&label("te_break_out"));
829	&sub	($s3,$s1);
830	&sub	($key,$s3);
831	&jmp	(&label("te_ok"));
832	&set_label("te_break_out");	# else %esp -= (p-s)&0xfff + framesz;
833	&sub	($s3,$s0);
834	&and	($s3,0xfff);
835	&add	($s3,64+256);
836	&sub	($key,$s3);
837	&align	(4);
838	&set_label("te_ok");
839
840	&mov	($s0,&wparam(0));	# load inp
841	&mov	($s1,&wparam(1));	# load out
842	&mov	($s3,&wparam(3));	# load key
843	&mov	($acc,&wparam(4));	# load ivp
844
845	&exch	("esp",$key);
846	&add	("esp",4);		# reserve for return address!
847	&mov	($_esp,$key);		# save %esp
848
849	&mov	($_inp,$s0);		# save copy of inp
850	&mov	($_out,$s1);		# save copy of out
851	&mov	($_len,$s2);		# save copy of len
852	&mov	($_key,$s3);		# save copy of key
853	&mov	($_ivp,$acc);		# save copy of ivp
854
855	&mov	($mark,0);		# copy of aes_key->rounds = 0;
856	if ($compromise) {
857		&cmp	($s2,$compromise);
858		&jb	(&label("skip_ecopy"));
859	}
860	# do we copy key schedule to stack?
861	&mov	($s1 eq "ebx" ? $s1 : "",$s3);
862	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
863	&sub	($s1,"ebp");
864	&mov	("esi",$s3);
865	&and	($s1,0xfff);
866	&lea	("edi",$aes_key);
867	&cmp	($s1,2048);
868	&jb	(&label("do_ecopy"));
869	&cmp	($s1,4096-244);
870	&jb	(&label("skip_ecopy"));
871	&align	(4);
872	&set_label("do_ecopy");
873		&mov	($_key,"edi");
874		&data_word(0xA5F3F689);	# rep movsd
875	&set_label("skip_ecopy");
876
877	&mov	($acc,$s0);
878	&mov	($key,16);
879	&align	(4);
880	&set_label("prefetch_te");
881		&mov	($s0,&DWP(0,"ebp"));
882		&mov	($s1,&DWP(32,"ebp"));
883		&mov	($s2,&DWP(64,"ebp"));
884		&mov	($s3,&DWP(96,"ebp"));
885		&lea	("ebp",&DWP(128,"ebp"));
886		&dec	($key);
887	&jnz	(&label("prefetch_te"));
888	&sub	("ebp",2048);
889
890	&mov	($s2,$_len);
891	&mov	($key,$_ivp);
892	&test	($s2,0xFFFFFFF0);
893	&jz	(&label("enc_tail"));		# short input...
894
895	&mov	($s0,&DWP(0,$key));		# load iv
896	&mov	($s1,&DWP(4,$key));
897
898	&align	(4);
899	&set_label("enc_loop");
900		&mov	($s2,&DWP(8,$key));
901		&mov	($s3,&DWP(12,$key));
902
903		&xor	($s0,&DWP(0,$acc));	# xor input data
904		&xor	($s1,&DWP(4,$acc));
905		&xor	($s2,&DWP(8,$acc));
906		&xor	($s3,&DWP(12,$acc));
907
908		&mov	($key,$_key);		# load key
909		&call	("_x86_AES_encrypt");
910
911		&mov	($acc,$_inp);		# load inp
912		&mov	($key,$_out);		# load out
913
914		&mov	(&DWP(0,$key),$s0);	# save output data
915		&mov	(&DWP(4,$key),$s1);
916		&mov	(&DWP(8,$key),$s2);
917		&mov	(&DWP(12,$key),$s3);
918
919		&mov	($s2,$_len);		# load len
920
921		&lea	($acc,&DWP(16,$acc));
922		&mov	($_inp,$acc);		# save inp
923
924		&lea	($s3,&DWP(16,$key));
925		&mov	($_out,$s3);		# save out
926
927		&sub	($s2,16);
928		&test	($s2,0xFFFFFFF0);
929		&mov	($_len,$s2);		# save len
930	&jnz	(&label("enc_loop"));
931	&test	($s2,15);
932	&jnz	(&label("enc_tail"));
933	&mov	($acc,$_ivp);		# load ivp
934	&mov	($s2,&DWP(8,$key));	# restore last dwords
935	&mov	($s3,&DWP(12,$key));
936	&mov	(&DWP(0,$acc),$s0);	# save ivec
937	&mov	(&DWP(4,$acc),$s1);
938	&mov	(&DWP(8,$acc),$s2);
939	&mov	(&DWP(12,$acc),$s3);
940
941	&cmp	($mark,0);		# was the key schedule copied?
942	&mov	("edi",$_key);
943	&je	(&label("skip_ezero"));
944	# zero copy of key schedule
945	&mov	("ecx",240/4);
946	&xor	("eax","eax");
947	&align	(4);
948	&data_word(0xABF3F689);	# rep stosd
949	&set_label("skip_ezero")
950	&mov	("esp",$_esp);
951	&popf	();
952    &set_label("enc_out");
953	&function_end_A();
954	&pushf	();			# kludge, never executed
955
956    &align	(4);
957    &set_label("enc_tail");
958	&mov	($s0,$key eq "edi" ? $key : "");
959	&mov	($key,$_out);			# load out
960	&push	($s0);				# push ivp
961	&mov	($s1,16);
962	&sub	($s1,$s2);
963	&cmp	($key,$acc);			# compare with inp
964	&je	(&label("enc_in_place"));
965	&align	(4);
966	&data_word(0xA4F3F689);	# rep movsb	# copy input
967	&jmp	(&label("enc_skip_in_place"));
968    &set_label("enc_in_place");
969	&lea	($key,&DWP(0,$key,$s2));
970    &set_label("enc_skip_in_place");
971	&mov	($s2,$s1);
972	&xor	($s0,$s0);
973	&align	(4);
974	&data_word(0xAAF3F689);	# rep stosb	# zero tail
975	&pop	($key);				# pop ivp
976
977	&mov	($acc,$_out);			# output as input
978	&mov	($s0,&DWP(0,$key));
979	&mov	($s1,&DWP(4,$key));
980	&mov	($_len,16);			# len=16
981	&jmp	(&label("enc_loop"));		# one more spin...
982
983#----------------------------- DECRYPT -----------------------------#
984&align	(4);
985&set_label("DECRYPT");
986	&lea    ("ebp",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
987
988	# allocate aligned stack frame...
989	&lea	($key,&DWP(-64-244,"esp"));
990	&and	($key,-64);
991
992	# ... and make sure it doesn't alias with AES_Td modulo 4096
993	&mov	($s0,"ebp");
994	&lea	($s1,&DWP(2048+256,"ebp"));
995	&mov	($s3,$key);
996	&and	($s0,0xfff);		# s = %ebp&0xfff
997	&and	($s1,0xfff);		# e = (%ebp+2048+256)&0xfff
998	&and	($s3,0xfff);		# p = %esp&0xfff
999
1000	&cmp	($s3,$s1);		# if (p>=e) %esp =- (p-e);
1001	&jb	(&label("td_break_out"));
1002	&sub	($s3,$s1);
1003	&sub	($key,$s3);
1004	&jmp	(&label("td_ok"));
1005	&set_label("td_break_out");	# else %esp -= (p-s)&0xfff + framesz;
1006	&sub	($s3,$s0);
1007	&and	($s3,0xfff);
1008	&add	($s3,64+256);
1009	&sub	($key,$s3);
1010	&align	(4);
1011	&set_label("td_ok");
1012
1013	&mov	($s0,&wparam(0));	# load inp
1014	&mov	($s1,&wparam(1));	# load out
1015	&mov	($s3,&wparam(3));	# load key
1016	&mov	($acc,&wparam(4));	# load ivp
1017
1018	&exch	("esp",$key);
1019	&add	("esp",4);		# reserve for return address!
1020	&mov	($_esp,$key);		# save %esp
1021
1022	&mov	($_inp,$s0);		# save copy of inp
1023	&mov	($_out,$s1);		# save copy of out
1024	&mov	($_len,$s2);		# save copy of len
1025	&mov	($_key,$s3);		# save copy of key
1026	&mov	($_ivp,$acc);		# save copy of ivp
1027
1028	&mov	($mark,0);		# copy of aes_key->rounds = 0;
1029	if ($compromise) {
1030		&cmp	($s2,$compromise);
1031		&jb	(&label("skip_dcopy"));
1032	}
1033	# do we copy key schedule to stack?
1034	&mov	($s1 eq "ebx" ? $s1 : "",$s3);
1035	&mov	($s2 eq "ecx" ? $s2 : "",244/4);
1036	&sub	($s1,"ebp");
1037	&mov	("esi",$s3);
1038	&and	($s1,0xfff);
1039	&lea	("edi",$aes_key);
1040	&cmp	($s1,2048+256);
1041	&jb	(&label("do_dcopy"));
1042	&cmp	($s1,4096-244);
1043	&jb	(&label("skip_dcopy"));
1044	&align	(4);
1045	&set_label("do_dcopy");
1046		&mov	($_key,"edi");
1047		&data_word(0xA5F3F689);	# rep movsd
1048	&set_label("skip_dcopy");
1049
1050	&mov	($acc,$s0);
1051	&mov	($key,18);
1052	&align	(4);
1053	&set_label("prefetch_td");
1054		&mov	($s0,&DWP(0,"ebp"));
1055		&mov	($s1,&DWP(32,"ebp"));
1056		&mov	($s2,&DWP(64,"ebp"));
1057		&mov	($s3,&DWP(96,"ebp"));
1058		&lea	("ebp",&DWP(128,"ebp"));
1059		&dec	($key);
1060	&jnz	(&label("prefetch_td"));
1061	&sub	("ebp",2048+256);
1062
1063	&cmp	($acc,$_out);
1064	&je	(&label("dec_in_place"));	# in-place processing...
1065
1066	&mov	($key,$_ivp);		# load ivp
1067	&mov	($_tmp,$key);
1068
1069	&align	(4);
1070	&set_label("dec_loop");
1071		&mov	($s0,&DWP(0,$acc));	# read input
1072		&mov	($s1,&DWP(4,$acc));
1073		&mov	($s2,&DWP(8,$acc));
1074		&mov	($s3,&DWP(12,$acc));
1075
1076		&mov	($key,$_key);		# load key
1077		&call	("_x86_AES_decrypt");
1078
1079		&mov	($key,$_tmp);		# load ivp
1080		&mov	($acc,$_len);		# load len
1081		&xor	($s0,&DWP(0,$key));	# xor iv
1082		&xor	($s1,&DWP(4,$key));
1083		&xor	($s2,&DWP(8,$key));
1084		&xor	($s3,&DWP(12,$key));
1085
1086		&sub	($acc,16);
1087		&jc	(&label("dec_partial"));
1088		&mov	($_len,$acc);		# save len
1089		&mov	($acc,$_inp);		# load inp
1090		&mov	($key,$_out);		# load out
1091
1092		&mov	(&DWP(0,$key),$s0);	# write output
1093		&mov	(&DWP(4,$key),$s1);
1094		&mov	(&DWP(8,$key),$s2);
1095		&mov	(&DWP(12,$key),$s3);
1096
1097		&mov	($_tmp,$acc);		# save ivp
1098		&lea	($acc,&DWP(16,$acc));
1099		&mov	($_inp,$acc);		# save inp
1100
1101		&lea	($key,&DWP(16,$key));
1102		&mov	($_out,$key);		# save out
1103
1104	&jnz	(&label("dec_loop"));
1105	&mov	($key,$_tmp);		# load temp ivp
1106    &set_label("dec_end");
1107	&mov	($acc,$_ivp);		# load user ivp
1108	&mov	($s0,&DWP(0,$key));	# load iv
1109	&mov	($s1,&DWP(4,$key));
1110	&mov	($s2,&DWP(8,$key));
1111	&mov	($s3,&DWP(12,$key));
1112	&mov	(&DWP(0,$acc),$s0);	# copy back to user
1113	&mov	(&DWP(4,$acc),$s1);
1114	&mov	(&DWP(8,$acc),$s2);
1115	&mov	(&DWP(12,$acc),$s3);
1116	&jmp	(&label("dec_out"));
1117
1118    &align	(4);
1119    &set_label("dec_partial");
1120	&lea	($key,$ivec);
1121	&mov	(&DWP(0,$key),$s0);	# dump output to stack
1122	&mov	(&DWP(4,$key),$s1);
1123	&mov	(&DWP(8,$key),$s2);
1124	&mov	(&DWP(12,$key),$s3);
1125	&lea	($s2 eq "ecx" ? $s2 : "",&DWP(16,$acc));
1126	&mov	($acc eq "esi" ? $acc : "",$key);
1127	&mov	($key eq "edi" ? $key : "",$_out);	# load out
1128	&data_word(0xA4F3F689);	# rep movsb		# copy output
1129	&mov	($key,$_inp);				# use inp as temp ivp
1130	&jmp	(&label("dec_end"));
1131
1132    &align	(4);
1133    &set_label("dec_in_place");
1134	&set_label("dec_in_place_loop");
1135		&lea	($key,$ivec);
1136		&mov	($s0,&DWP(0,$acc));	# read input
1137		&mov	($s1,&DWP(4,$acc));
1138		&mov	($s2,&DWP(8,$acc));
1139		&mov	($s3,&DWP(12,$acc));
1140
1141		&mov	(&DWP(0,$key),$s0);	# copy to temp
1142		&mov	(&DWP(4,$key),$s1);
1143		&mov	(&DWP(8,$key),$s2);
1144		&mov	(&DWP(12,$key),$s3);
1145
1146		&mov	($key,$_key);		# load key
1147		&call	("_x86_AES_decrypt");
1148
1149		&mov	($key,$_ivp);		# load ivp
1150		&mov	($acc,$_out);		# load out
1151		&xor	($s0,&DWP(0,$key));	# xor iv
1152		&xor	($s1,&DWP(4,$key));
1153		&xor	($s2,&DWP(8,$key));
1154		&xor	($s3,&DWP(12,$key));
1155
1156		&mov	(&DWP(0,$acc),$s0);	# write output
1157		&mov	(&DWP(4,$acc),$s1);
1158		&mov	(&DWP(8,$acc),$s2);
1159		&mov	(&DWP(12,$acc),$s3);
1160
1161		&lea	($acc,&DWP(16,$acc));
1162		&mov	($_out,$acc);		# save out
1163
1164		&lea	($acc,$ivec);
1165		&mov	($s0,&DWP(0,$acc));	# read temp
1166		&mov	($s1,&DWP(4,$acc));
1167		&mov	($s2,&DWP(8,$acc));
1168		&mov	($s3,&DWP(12,$acc));
1169
1170		&mov	(&DWP(0,$key),$s0);	# copy iv
1171		&mov	(&DWP(4,$key),$s1);
1172		&mov	(&DWP(8,$key),$s2);
1173		&mov	(&DWP(12,$key),$s3);
1174
1175		&mov	($acc,$_inp);		# load inp
1176
1177		&lea	($acc,&DWP(16,$acc));
1178		&mov	($_inp,$acc);		# save inp
1179
1180		&mov	($s2,$_len);		# load len
1181		&sub	($s2,16);
1182		&jc	(&label("dec_in_place_partial"));
1183		&mov	($_len,$s2);		# save len
1184	&jnz	(&label("dec_in_place_loop"));
1185	&jmp	(&label("dec_out"));
1186
1187    &align	(4);
1188    &set_label("dec_in_place_partial");
1189	# one can argue if this is actually required...
1190	&mov	($key eq "edi" ? $key : "",$_out);
1191	&lea	($acc eq "esi" ? $acc : "",$ivec);
1192	&lea	($key,&DWP(0,$key,$s2));
1193	&lea	($acc,&DWP(16,$acc,$s2));
1194	&neg	($s2 eq "ecx" ? $s2 : "");
1195	&data_word(0xA4F3F689);	# rep movsb	# restore tail
1196
1197    &align	(4);
1198    &set_label("dec_out");
1199    &cmp	($mark,0);		# was the key schedule copied?
1200    &mov	("edi",$_key);
1201    &je		(&label("skip_dzero"));
1202    # zero copy of key schedule
1203    &mov	("ecx",240/4);
1204    &xor	("eax","eax");
1205    &align	(4);
1206    &data_word(0xABF3F689);	# rep stosd
1207    &set_label("skip_dzero")
1208    &mov	("esp",$_esp);
1209    &popf	();
1210&function_end("AES_cbc_encrypt");
1211}
1212
1213#------------------------------------------------------------------#
1214
1215sub enckey()
1216{
1217	&movz	("esi",&LB("edx"));		# rk[i]>>0
1218	&mov	("ebx",&DWP(2,"ebp","esi",8));
1219	&movz	("esi",&HB("edx"));		# rk[i]>>8
1220	&and	("ebx",0xFF000000);
1221	&xor	("eax","ebx");
1222
1223	&mov	("ebx",&DWP(2,"ebp","esi",8));
1224	&shr	("edx",16);
1225	&and	("ebx",0x000000FF);
1226	&movz	("esi",&LB("edx"));		# rk[i]>>16
1227	&xor	("eax","ebx");
1228
1229	&mov	("ebx",&DWP(0,"ebp","esi",8));
1230	&movz	("esi",&HB("edx"));		# rk[i]>>24
1231	&and	("ebx",0x0000FF00);
1232	&xor	("eax","ebx");
1233
1234	&mov	("ebx",&DWP(0,"ebp","esi",8));
1235	&and	("ebx",0x00FF0000);
1236	&xor	("eax","ebx");
1237
1238	&xor	("eax",&DWP(2048,"ebp","ecx",4));	# rcon
1239}
1240
1241# int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
1242#                        AES_KEY *key)
1243&public_label("AES_Te");
1244&function_begin("AES_set_encrypt_key");
1245	&mov	("esi",&wparam(0));		# user supplied key
1246	&mov	("edi",&wparam(2));		# private key schedule
1247
1248	&test	("esi",-1);
1249	&jz	(&label("badpointer"));
1250	&test	("edi",-1);
1251	&jz	(&label("badpointer"));
1252
1253	&call	(&label("pic_point"));
1254	&set_label("pic_point");
1255	&blindpop("ebp");
1256	&lea	("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1257
1258	&mov	("ecx",&wparam(1));		# number of bits in key
1259	&cmp	("ecx",128);
1260	&je	(&label("10rounds"));
1261	&cmp	("ecx",192);
1262	&je	(&label("12rounds"));
1263	&cmp	("ecx",256);
1264	&je	(&label("14rounds"));
1265	&mov	("eax",-2);			# invalid number of bits
1266	&jmp	(&label("exit"));
1267
1268    &set_label("10rounds");
1269	&mov	("eax",&DWP(0,"esi"));		# copy first 4 dwords
1270	&mov	("ebx",&DWP(4,"esi"));
1271	&mov	("ecx",&DWP(8,"esi"));
1272	&mov	("edx",&DWP(12,"esi"));
1273	&mov	(&DWP(0,"edi"),"eax");
1274	&mov	(&DWP(4,"edi"),"ebx");
1275	&mov	(&DWP(8,"edi"),"ecx");
1276	&mov	(&DWP(12,"edi"),"edx");
1277
1278	&xor	("ecx","ecx");
1279	&jmp	(&label("10shortcut"));
1280
1281	&align	(4);
1282	&set_label("10loop");
1283		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1284		&mov	("edx",&DWP(12,"edi"));		# rk[3]
1285	&set_label("10shortcut");
1286		&enckey	();
1287
1288		&mov	(&DWP(16,"edi"),"eax");		# rk[4]
1289		&xor	("eax",&DWP(4,"edi"));
1290		&mov	(&DWP(20,"edi"),"eax");		# rk[5]
1291		&xor	("eax",&DWP(8,"edi"));
1292		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
1293		&xor	("eax",&DWP(12,"edi"));
1294		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
1295		&inc	("ecx");
1296		&add	("edi",16);
1297		&cmp	("ecx",10);
1298	&jl	(&label("10loop"));
1299
1300	&mov	(&DWP(80,"edi"),10);		# setup number of rounds
1301	&xor	("eax","eax");
1302	&jmp	(&label("exit"));
1303
1304    &set_label("12rounds");
1305	&mov	("eax",&DWP(0,"esi"));		# copy first 6 dwords
1306	&mov	("ebx",&DWP(4,"esi"));
1307	&mov	("ecx",&DWP(8,"esi"));
1308	&mov	("edx",&DWP(12,"esi"));
1309	&mov	(&DWP(0,"edi"),"eax");
1310	&mov	(&DWP(4,"edi"),"ebx");
1311	&mov	(&DWP(8,"edi"),"ecx");
1312	&mov	(&DWP(12,"edi"),"edx");
1313	&mov	("ecx",&DWP(16,"esi"));
1314	&mov	("edx",&DWP(20,"esi"));
1315	&mov	(&DWP(16,"edi"),"ecx");
1316	&mov	(&DWP(20,"edi"),"edx");
1317
1318	&xor	("ecx","ecx");
1319	&jmp	(&label("12shortcut"));
1320
1321	&align	(4);
1322	&set_label("12loop");
1323		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1324		&mov	("edx",&DWP(20,"edi"));		# rk[5]
1325	&set_label("12shortcut");
1326		&enckey	();
1327
1328		&mov	(&DWP(24,"edi"),"eax");		# rk[6]
1329		&xor	("eax",&DWP(4,"edi"));
1330		&mov	(&DWP(28,"edi"),"eax");		# rk[7]
1331		&xor	("eax",&DWP(8,"edi"));
1332		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
1333		&xor	("eax",&DWP(12,"edi"));
1334		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
1335
1336		&cmp	("ecx",7);
1337		&je	(&label("12break"));
1338		&inc	("ecx");
1339
1340		&xor	("eax",&DWP(16,"edi"));
1341		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
1342		&xor	("eax",&DWP(20,"edi"));
1343		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
1344
1345		&add	("edi",24);
1346	&jmp	(&label("12loop"));
1347
1348	&set_label("12break");
1349	&mov	(&DWP(72,"edi"),12);		# setup number of rounds
1350	&xor	("eax","eax");
1351	&jmp	(&label("exit"));
1352
1353    &set_label("14rounds");
1354	&mov	("eax",&DWP(0,"esi"));		# copy first 8 dwords
1355	&mov	("ebx",&DWP(4,"esi"));
1356	&mov	("ecx",&DWP(8,"esi"));
1357	&mov	("edx",&DWP(12,"esi"));
1358	&mov	(&DWP(0,"edi"),"eax");
1359	&mov	(&DWP(4,"edi"),"ebx");
1360	&mov	(&DWP(8,"edi"),"ecx");
1361	&mov	(&DWP(12,"edi"),"edx");
1362	&mov	("eax",&DWP(16,"esi"));
1363	&mov	("ebx",&DWP(20,"esi"));
1364	&mov	("ecx",&DWP(24,"esi"));
1365	&mov	("edx",&DWP(28,"esi"));
1366	&mov	(&DWP(16,"edi"),"eax");
1367	&mov	(&DWP(20,"edi"),"ebx");
1368	&mov	(&DWP(24,"edi"),"ecx");
1369	&mov	(&DWP(28,"edi"),"edx");
1370
1371	&xor	("ecx","ecx");
1372	&jmp	(&label("14shortcut"));
1373
1374	&align	(4);
1375	&set_label("14loop");
1376		&mov	("edx",&DWP(28,"edi"));		# rk[7]
1377	&set_label("14shortcut");
1378		&mov	("eax",&DWP(0,"edi"));		# rk[0]
1379
1380		&enckey	();
1381
1382		&mov	(&DWP(32,"edi"),"eax");		# rk[8]
1383		&xor	("eax",&DWP(4,"edi"));
1384		&mov	(&DWP(36,"edi"),"eax");		# rk[9]
1385		&xor	("eax",&DWP(8,"edi"));
1386		&mov	(&DWP(40,"edi"),"eax");		# rk[10]
1387		&xor	("eax",&DWP(12,"edi"));
1388		&mov	(&DWP(44,"edi"),"eax");		# rk[11]
1389
1390		&cmp	("ecx",6);
1391		&je	(&label("14break"));
1392		&inc	("ecx");
1393
1394		&mov	("edx","eax");
1395		&mov	("eax",&DWP(16,"edi"));		# rk[4]
1396		&movz	("esi",&LB("edx"));		# rk[11]>>0
1397		&mov	("ebx",&DWP(2,"ebp","esi",8));
1398		&movz	("esi",&HB("edx"));		# rk[11]>>8
1399		&and	("ebx",0x000000FF);
1400		&xor	("eax","ebx");
1401
1402		&mov	("ebx",&DWP(0,"ebp","esi",8));
1403		&shr	("edx",16);
1404		&and	("ebx",0x0000FF00);
1405		&movz	("esi",&LB("edx"));		# rk[11]>>16
1406		&xor	("eax","ebx");
1407
1408		&mov	("ebx",&DWP(0,"ebp","esi",8));
1409		&movz	("esi",&HB("edx"));		# rk[11]>>24
1410		&and	("ebx",0x00FF0000);
1411		&xor	("eax","ebx");
1412
1413		&mov	("ebx",&DWP(2,"ebp","esi",8));
1414		&and	("ebx",0xFF000000);
1415		&xor	("eax","ebx");
1416
1417		&mov	(&DWP(48,"edi"),"eax");		# rk[12]
1418		&xor	("eax",&DWP(20,"edi"));
1419		&mov	(&DWP(52,"edi"),"eax");		# rk[13]
1420		&xor	("eax",&DWP(24,"edi"));
1421		&mov	(&DWP(56,"edi"),"eax");		# rk[14]
1422		&xor	("eax",&DWP(28,"edi"));
1423		&mov	(&DWP(60,"edi"),"eax");		# rk[15]
1424
1425		&add	("edi",32);
1426	&jmp	(&label("14loop"));
1427
1428	&set_label("14break");
1429	&mov	(&DWP(48,"edi"),14);		# setup number of rounds
1430	&xor	("eax","eax");
1431	&jmp	(&label("exit"));
1432
1433    &set_label("badpointer");
1434	&mov	("eax",-1);
1435    &set_label("exit");
1436&function_end("AES_set_encrypt_key");
1437
1438sub deckey()
1439{ my ($i,$ptr,$te,$td) = @_;
1440
1441	&mov	("eax",&DWP($i,$ptr));
1442	&mov	("edx","eax");
1443	&movz	("ebx",&HB("eax"));
1444	&shr	("edx",16);
1445	&and	("eax",0xFF);
1446	&movz	("eax",&BP(2,$te,"eax",8));
1447	&movz	("ebx",&BP(2,$te,"ebx",8));
1448	&mov	("eax",&DWP(0,$td,"eax",8));
1449	&xor	("eax",&DWP(3,$td,"ebx",8));
1450	&movz	("ebx",&HB("edx"));
1451	&and	("edx",0xFF);
1452	&movz	("edx",&BP(2,$te,"edx",8));
1453	&movz	("ebx",&BP(2,$te,"ebx",8));
1454	&xor	("eax",&DWP(2,$td,"edx",8));
1455	&xor	("eax",&DWP(1,$td,"ebx",8));
1456	&mov	(&DWP($i,$ptr),"eax");
1457}
1458
1459# int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
1460#                        AES_KEY *key)
1461&public_label("AES_Td");
1462&public_label("AES_Te");
1463&function_begin_B("AES_set_decrypt_key");
1464	&mov	("eax",&wparam(0));
1465	&mov	("ecx",&wparam(1));
1466	&mov	("edx",&wparam(2));
1467	&sub	("esp",12);
1468	&mov	(&DWP(0,"esp"),"eax");
1469	&mov	(&DWP(4,"esp"),"ecx");
1470	&mov	(&DWP(8,"esp"),"edx");
1471	&call	("AES_set_encrypt_key");
1472	&add	("esp",12);
1473	&cmp	("eax",0);
1474	&je	(&label("proceed"));
1475	&ret	();
1476
1477    &set_label("proceed");
1478	&push	("ebp");
1479	&push	("ebx");
1480	&push	("esi");
1481	&push	("edi");
1482
1483	&mov	("esi",&wparam(2));
1484	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
1485	&lea	("ecx",&DWP(0,"","ecx",4));
1486	&lea	("edi",&DWP(0,"esi","ecx",4));	# pointer to last chunk
1487
1488	&align	(4);
1489	&set_label("invert");			# invert order of chunks
1490		&mov	("eax",&DWP(0,"esi"));
1491		&mov	("ebx",&DWP(4,"esi"));
1492		&mov	("ecx",&DWP(0,"edi"));
1493		&mov	("edx",&DWP(4,"edi"));
1494		&mov	(&DWP(0,"edi"),"eax");
1495		&mov	(&DWP(4,"edi"),"ebx");
1496		&mov	(&DWP(0,"esi"),"ecx");
1497		&mov	(&DWP(4,"esi"),"edx");
1498		&mov	("eax",&DWP(8,"esi"));
1499		&mov	("ebx",&DWP(12,"esi"));
1500		&mov	("ecx",&DWP(8,"edi"));
1501		&mov	("edx",&DWP(12,"edi"));
1502		&mov	(&DWP(8,"edi"),"eax");
1503		&mov	(&DWP(12,"edi"),"ebx");
1504		&mov	(&DWP(8,"esi"),"ecx");
1505		&mov	(&DWP(12,"esi"),"edx");
1506		&add	("esi",16);
1507		&sub	("edi",16);
1508		&cmp	("esi","edi");
1509	&jne	(&label("invert"));
1510
1511	&call	(&label("pic_point"));
1512	&set_label("pic_point");
1513	blindpop("ebp");
1514	&lea	("edi",&DWP(&label("AES_Td")."-".&label("pic_point"),"ebp"));
1515	&lea	("ebp",&DWP(&label("AES_Te")."-".&label("pic_point"),"ebp"));
1516
1517	&mov	("esi",&wparam(2));
1518	&mov	("ecx",&DWP(240,"esi"));	# pull number of rounds
1519	&dec	("ecx");
1520	&align	(4);
1521	&set_label("permute");			# permute the key schedule
1522		&add	("esi",16);
1523		&deckey	(0,"esi","ebp","edi");
1524		&deckey	(4,"esi","ebp","edi");
1525		&deckey	(8,"esi","ebp","edi");
1526		&deckey	(12,"esi","ebp","edi");
1527		&dec	("ecx");
1528	&jnz	(&label("permute"));
1529
1530	&xor	("eax","eax");			# return success
1531&function_end("AES_set_decrypt_key");
1532
1533&asm_finish();
1534