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