Switch to compact S-box when generating AES key schedule.
[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.1.
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            56[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 # Version 4.1 switches to compact S-box even in key schedule setup.
111
112 push(@INC,"perlasm","../../perlasm");
113 require "x86asm.pl";
114
115 &asm_init($ARGV[0],"aes-586.pl",$ARGV[$#ARGV] eq "386");
116
117 $s0="eax";
118 $s1="ebx";
119 $s2="ecx";
120 $s3="edx";
121 $key="edi";
122 $acc="esi";
123 $tbl="ebp";
124
125 sub _data_word() { my $i; while(defined($i=shift)) { &data_word($i,$i); } }
126
127 $compromise=0;          # $compromise=128 abstains from copying key
128                         # schedule to stack when encrypting inputs
129                         # shorter than 128 bytes at the cost of
130                         # risksing aliasing with S-boxes. In return
131                         # you get way better, up to +70%, small block
132                         # performance.
133 $small_footprint=1;     # $small_footprint=1 code is ~5% slower [on
134                         # recent µ-archs], but ~5 times smaller!
135                         # I favor compact code to minimize cache
136                         # contention and in hope to "collect" 5% back
137                         # in real-life applications...
138
139 $vertical_spin=0;       # shift "verticaly" defaults to 0, because of
140                         # its proof-of-concept status...
141 # Note that there is no decvert(), as well as last encryption round is
142 # performed with "horizontal" shifts. This is because this "vertical"
143 # implementation [one which groups shifts on a given $s[i] to form a
144 # "column," unlike "horizontal" one, which groups shifts on different
145 # $s[i] to form a "row"] is work in progress. It was observed to run
146 # few percents faster on Intel cores, but not AMD. On AMD K8 core it's
147 # whole 12% slower:-( So we face a trade-off... Shall it be resolved
148 # some day? Till then the code is considered experimental and by
149 # default remains dormant...
150
151 sub encvert()
152 { my ($te,@s) = @_;
153   my $v0 = $acc, $v1 = $key;
154
155         &mov    ($v0,$s[3]);                            # copy s3
156         &mov    (&DWP(4,"esp"),$s[2]);                  # save s2
157         &mov    ($v1,$s[0]);                            # copy s0
158         &mov    (&DWP(8,"esp"),$s[1]);                  # save s1
159
160         &movz   ($s[2],&HB($s[0]));
161         &and    ($s[0],0xFF);
162         &mov    ($s[0],&DWP(0,$te,$s[0],8));            # s0>>0
163         &shr    ($v1,16);
164         &mov    ($s[3],&DWP(3,$te,$s[2],8));            # s0>>8
165         &movz   ($s[1],&HB($v1));
166         &and    ($v1,0xFF);
167         &mov    ($s[2],&DWP(2,$te,$v1,8));              # s0>>16
168          &mov   ($v1,$v0);
169         &mov    ($s[1],&DWP(1,$te,$s[1],8));            # s0>>24
170
171         &and    ($v0,0xFF);
172         &xor    ($s[3],&DWP(0,$te,$v0,8));              # s3>>0
173         &movz   ($v0,&HB($v1));
174         &shr    ($v1,16);
175         &xor    ($s[2],&DWP(3,$te,$v0,8));              # s3>>8
176         &movz   ($v0,&HB($v1));
177         &and    ($v1,0xFF);
178         &xor    ($s[1],&DWP(2,$te,$v1,8));              # s3>>16
179          &mov   ($v1,&DWP(4,"esp"));                    # restore s2
180         &xor    ($s[0],&DWP(1,$te,$v0,8));              # s3>>24
181
182         &mov    ($v0,$v1);
183         &and    ($v1,0xFF);
184         &xor    ($s[2],&DWP(0,$te,$v1,8));              # s2>>0
185         &movz   ($v1,&HB($v0));
186         &shr    ($v0,16);
187         &xor    ($s[1],&DWP(3,$te,$v1,8));              # s2>>8
188         &movz   ($v1,&HB($v0));
189         &and    ($v0,0xFF);
190         &xor    ($s[0],&DWP(2,$te,$v0,8));              # s2>>16
191          &mov   ($v0,&DWP(8,"esp"));                    # restore s1
192         &xor    ($s[3],&DWP(1,$te,$v1,8));              # s2>>24
193
194         &mov    ($v1,$v0);
195         &and    ($v0,0xFF);
196         &xor    ($s[1],&DWP(0,$te,$v0,8));              # s1>>0
197         &movz   ($v0,&HB($v1));
198         &shr    ($v1,16);
199         &xor    ($s[0],&DWP(3,$te,$v0,8));              # s1>>8
200         &movz   ($v0,&HB($v1));
201         &and    ($v1,0xFF);
202         &xor    ($s[3],&DWP(2,$te,$v1,8));              # s1>>16
203          &mov   ($key,&DWP(20,"esp"));                  # reincarnate v1 as key
204         &xor    ($s[2],&DWP(1,$te,$v0,8));              # s1>>24
205 }
206
207 # Another experimental routine, which features "horizontal spin," but
208 # eliminates one reference to stack. Strangely enough runs slower...
209 sub enchoriz()
210 { my $v0 = $key, $v1 = $acc;
211
212         &movz   ($v0,&LB($s0));                 #  3, 2, 1, 0*
213         &rotr   ($s2,8);                        #  8,11,10, 9
214         &mov    ($v1,&DWP(0,$te,$v0,8));        #  0
215         &movz   ($v0,&HB($s1));                 #  7, 6, 5*, 4
216         &rotr   ($s3,16);                       # 13,12,15,14
217         &xor    ($v1,&DWP(3,$te,$v0,8));        #  5
218         &movz   ($v0,&HB($s2));                 #  8,11,10*, 9
219         &rotr   ($s0,16);                       #  1, 0, 3, 2
220         &xor    ($v1,&DWP(2,$te,$v0,8));        # 10
221         &movz   ($v0,&HB($s3));                 # 13,12,15*,14
222         &xor    ($v1,&DWP(1,$te,$v0,8));        # 15, t[0] collected
223         &mov    (&DWP(4,"esp"),$v1);            # t[0] saved
224
225         &movz   ($v0,&LB($s1));                 #  7, 6, 5, 4*
226         &shr    ($s1,16);                       #  -, -, 7, 6
227         &mov    ($v1,&DWP(0,$te,$v0,8));        #  4
228         &movz   ($v0,&LB($s3));                 # 13,12,15,14*
229         &xor    ($v1,&DWP(2,$te,$v0,8));        # 14
230         &movz   ($v0,&HB($s0));                 #  1, 0, 3*, 2
231         &and    ($s3,0xffff0000);               # 13,12, -, -
232         &xor    ($v1,&DWP(1,$te,$v0,8));        #  3
233         &movz   ($v0,&LB($s2));                 #  8,11,10, 9*
234         &or     ($s3,$s1);                      # 13,12, 7, 6
235         &xor    ($v1,&DWP(3,$te,$v0,8));        #  9, t[1] collected
236         &mov    ($s1,$v1);                      #  s[1]=t[1]
237
238         &movz   ($v0,&LB($s0));                 #  1, 0, 3, 2*
239         &shr    ($s2,16);                       #  -, -, 8,11
240         &mov    ($v1,&DWP(2,$te,$v0,8));        #  2
241         &movz   ($v0,&HB($s3));                 # 13,12, 7*, 6
242         &xor    ($v1,&DWP(1,$te,$v0,8));        #  7
243         &movz   ($v0,&HB($s2));                 #  -, -, 8*,11
244         &xor    ($v1,&DWP(0,$te,$v0,8));        #  8
245         &mov    ($v0,$s3);
246         &shr    ($v0,24);                       # 13
247         &xor    ($v1,&DWP(3,$te,$v0,8));        # 13, t[2] collected
248
249         &movz   ($v0,&LB($s2));                 #  -, -, 8,11*
250         &shr    ($s0,24);                       #  1*
251         &mov    ($s2,&DWP(1,$te,$v0,8));        # 11
252         &xor    ($s2,&DWP(3,$te,$s0,8));        #  1
253         &mov    ($s0,&DWP(4,"esp"));            # s[0]=t[0]
254         &movz   ($v0,&LB($s3));                 # 13,12, 7, 6*
255         &shr    ($s3,16);                       #   ,  ,13,12
256         &xor    ($s2,&DWP(2,$te,$v0,8));        #  6
257         &mov    ($key,&DWP(20,"esp"));          # reincarnate v0 as key
258         &and    ($s3,0xff);                     #   ,  ,13,12*
259         &mov    ($s3,&DWP(0,$te,$s3,8));        # 12
260         &xor    ($s3,$s2);                      # s[2]=t[3] collected
261         &mov    ($s2,$v1);                      # s[2]=t[2]
262 }
263
264 # More experimental code... MMX one... Even though this one eliminates
265 # *all* references to stack, it's not faster...
266 sub mmx_encbody()
267 {
268         &movz   ($acc,&LB("eax"));              #  0
269         &mov    ("ecx",&DWP(0,$tbl,$acc,8));    #  0
270         &pshufw ("mm2","mm0",0x0d);             #  7, 6, 3, 2
271         &movz   ("edx",&HB("eax"));             #  1
272         &mov    ("edx",&DWP(3,$tbl,"edx",8));   #  1
273         &shr    ("eax",16);                     #  5, 4
274
275         &movz   ($acc,&LB("ebx"));              # 10
276         &xor    ("ecx",&DWP(2,$tbl,$acc,8));    # 10
277         &pshufw ("mm6","mm4",0x08);             # 13,12, 9, 8
278         &movz   ($acc,&HB("ebx"));              # 11
279         &xor    ("edx",&DWP(1,$tbl,$acc,8));    # 11
280         &shr    ("ebx",16);                     # 15,14
281
282         &movz   ($acc,&HB("eax"));              #  5
283         &xor    ("ecx",&DWP(3,$tbl,$acc,8));    #  5
284         &movq   ("mm3",QWP(16,$key));
285         &movz   ($acc,&HB("ebx"));              # 15
286         &xor    ("ecx",&DWP(1,$tbl,$acc,8));    # 15
287         &movd   ("mm0","ecx");                  # t[0] collected
288
289         &movz   ($acc,&LB("eax"));              #  4
290         &mov    ("ecx",&DWP(0,$tbl,$acc,8));    #  4
291         &movd   ("eax","mm2");                  #  7, 6, 3, 2
292         &movz   ($acc,&LB("ebx"));              # 14
293         &xor    ("ecx",&DWP(2,$tbl,$acc,8));    # 14
294         &movd   ("ebx","mm6");                  # 13,12, 9, 8
295
296         &movz   ($acc,&HB("eax"));              #  3
297         &xor    ("ecx",&DWP(1,$tbl,$acc,8));    #  3
298         &movz   ($acc,&HB("ebx"));              #  9
299         &xor    ("ecx",&DWP(3,$tbl,$acc,8));    #  9
300         &movd   ("mm1","ecx");                  # t[1] collected
301
302         &movz   ($acc,&LB("eax"));              #  2
303         &mov    ("ecx",&DWP(2,$tbl,$acc,8));    #  2
304         &shr    ("eax",16);                     #  7, 6
305         &punpckldq      ("mm0","mm1");          # t[0,1] collected
306         &movz   ($acc,&LB("ebx"));              #  8
307         &xor    ("ecx",&DWP(0,$tbl,$acc,8));    #  8
308         &shr    ("ebx",16);                     # 13,12
309
310         &movz   ($acc,&HB("eax"));              #  7
311         &xor    ("ecx",&DWP(1,$tbl,$acc,8));    #  7
312         &pxor   ("mm0","mm3");
313         &movz   ("eax",&LB("eax"));             #  6
314         &xor    ("edx",&DWP(2,$tbl,"eax",8));   #  6
315         &pshufw ("mm1","mm0",0x08);             #  5, 4, 1, 0
316         &movz   ($acc,&HB("ebx"));              # 13
317         &xor    ("ecx",&DWP(3,$tbl,$acc,8));    # 13
318         &xor    ("ecx",&DWP(24,$key));          # t[2]
319         &movd   ("mm4","ecx");                  # t[2] collected
320         &movz   ("ebx",&LB("ebx"));             # 12
321         &xor    ("edx",&DWP(0,$tbl,"ebx",8));   # 12
322         &shr    ("ecx",16);
323         &movd   ("eax","mm1");                  #  5, 4, 1, 0
324         &mov    ("ebx",&DWP(28,$key));          # t[3]
325         &xor    ("ebx","edx");
326         &movd   ("mm5","ebx");                  # t[3] collected
327         &and    ("ebx",0xffff0000);
328         &or     ("ebx","ecx");
329
330         &punpckldq      ("mm4","mm5");          # t[2,3] collected
331 }
332
333 ######################################################################
334 # "Compact" block function
335 ######################################################################
336
337 sub enccompact()
338 { my $Fn = mov;
339   while ($#_>5) { pop(@_); $Fn=sub{}; }
340   my ($i,$te,@s)=@_;
341   my $tmp = $key;
342   my $out = $i==3?$s[0]:$acc;
343
344         # $Fn is used in first compact round and its purpose is to
345         # void restoration of some values from stack, so that after
346         # 4xenccompact with extra argument $key value is left there...
347         if ($i==3)  {   &$Fn    ($key,&DWP(20,"esp"));          }##%edx
348         else        {   &mov    ($out,$s[0]);                   }
349                         &and    ($out,0xFF);
350         if ($i==1)  {   &shr    ($s[0],16);                     }#%ebx[1]
351         if ($i==2)  {   &shr    ($s[0],24);                     }#%ecx[2]
352                         &movz   ($out,&BP(-128,$te,$out,1));
353
354         if ($i==3)  {   $tmp=$s[1];                             }##%eax
355                         &movz   ($tmp,&HB($s[1]));
356                         &movz   ($tmp,&BP(-128,$te,$tmp,1));
357                         &shl    ($tmp,8);
358                         &xor    ($out,$tmp);
359
360         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],&DWP(4,"esp")); }##%ebx
361         else        {   &mov    ($tmp,$s[2]);
362                         &shr    ($tmp,16);                      }
363         if ($i==2)  {   &and    ($s[1],0xFF);                   }#%edx[2]
364                         &and    ($tmp,0xFF);
365                         &movz   ($tmp,&BP(-128,$te,$tmp,1));
366                         &shl    ($tmp,16);
367                         &xor    ($out,$tmp);
368
369         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }##%ecx
370         elsif($i==2){   &movz   ($tmp,&HB($s[3]));              }#%ebx[2]
371         else        {   &mov    ($tmp,$s[3]);
372                         &shr    ($tmp,24);                      }
373                         &movz   ($tmp,&BP(-128,$te,$tmp,1));
374                         &shl    ($tmp,24);
375                         &xor    ($out,$tmp);
376         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
377         if ($i==3)  {   &mov    ($s[3],$acc);                   }
378         &comment();
379 }
380
381 sub enctransform()
382 { my @s = ($s0,$s1,$s2,$s3);
383   my $i = shift;
384   my $tmp = $tbl;
385   my $r2  = $key ;
386
387         &mov    ($acc,$s[$i]);
388         &and    ($acc,0x80808080);
389         &mov    ($tmp,$acc);
390         &mov    ($r2,$s[$i]);
391         &shr    ($tmp,7);
392         &and    ($r2,0x7f7f7f7f);
393         &sub    ($acc,$tmp);
394         &lea    ($r2,&DWP(0,$r2,$r2));
395         &and    ($acc,0x1b1b1b1b);
396         &mov    ($tmp,$s[$i]);
397         &xor    ($acc,$r2);     # r2
398
399         &xor    ($s[$i],$acc);  # r0 ^ r2
400         &rotl   ($s[$i],24);
401         &xor    ($s[$i],$acc)   # ROTATE(r2^r0,24) ^ r2
402         &rotr   ($tmp,16);
403         &xor    ($s[$i],$tmp);
404         &rotr   ($tmp,8);
405         &xor    ($s[$i],$tmp);
406 }
407
408 &public_label("AES_Te");
409 &function_begin_B("_x86_AES_encrypt_compact");
410         # note that caller is expected to allocate stack frame for me!
411         &mov    (&DWP(20,"esp"),$key);          # save key
412
413         &xor    ($s0,&DWP(0,$key));             # xor with key
414         &xor    ($s1,&DWP(4,$key));
415         &xor    ($s2,&DWP(8,$key));
416         &xor    ($s3,&DWP(12,$key));
417
418         &mov    ($acc,&DWP(240,$key));          # load key->rounds
419         &lea    ($acc,&DWP(-2,$acc,$acc));
420         &lea    ($acc,&DWP(0,$key,$acc,8));
421         &mov    (&DWP(24,"esp"),$acc);          # end of key schedule
422
423         # prefetch Te4
424         &mov    ($key,&DWP(0-128,$tbl));
425         &mov    ($acc,&DWP(32-128,$tbl));
426         &mov    ($key,&DWP(64-128,$tbl));
427         &mov    ($acc,&DWP(96-128,$tbl));
428         &mov    ($key,&DWP(128-128,$tbl));
429         &mov    ($acc,&DWP(160-128,$tbl));
430         &mov    ($key,&DWP(192-128,$tbl));
431         &mov    ($acc,&DWP(224-128,$tbl));
432
433         &set_label("loop",16);
434
435                 &enccompact(0,$tbl,$s0,$s1,$s2,$s3,1);
436                 &enccompact(1,$tbl,$s1,$s2,$s3,$s0,1);
437                 &enccompact(2,$tbl,$s2,$s3,$s0,$s1,1);
438                 &enccompact(3,$tbl,$s3,$s0,$s1,$s2,1);
439                 &enctransform(2);
440                 &enctransform(3);
441                 &enctransform(0);
442                 &enctransform(1);
443                 &mov    ($key,&DWP(20,"esp"));
444                 &mov    ($tbl,&DWP(28,"esp"));
445                 &add    ($key,16);              # advance rd_key
446                 &xor    ($s0,&DWP(0,$key));
447                 &xor    ($s1,&DWP(4,$key));
448                 &xor    ($s2,&DWP(8,$key));
449                 &xor    ($s3,&DWP(12,$key));
450
451         &cmp    ($key,&DWP(24,"esp"));
452         &mov    (&DWP(20,"esp"),$key);
453         &jb     (&label("loop"));
454
455         &enccompact(0,$tbl,$s0,$s1,$s2,$s3);
456         &enccompact(1,$tbl,$s1,$s2,$s3,$s0);
457         &enccompact(2,$tbl,$s2,$s3,$s0,$s1);
458         &enccompact(3,$tbl,$s3,$s0,$s1,$s2);
459
460         &xor    ($s0,&DWP(16,$key));
461         &xor    ($s1,&DWP(20,$key));
462         &xor    ($s2,&DWP(24,$key));
463         &xor    ($s3,&DWP(28,$key));
464
465         &ret    ();
466 &function_end_B("_x86_AES_encrypt_compact");
467
468 ######################################################################
469 # "Compact" MMX block function.
470 ######################################################################
471 #
472 # Performance is not actually extraordinary in comparison to pure
473 # x86 code. In particular encrypt performance is virtually the same.
474 # Decrypt performance on the other hand is 15-20% better on newer
475 # µ-archs [but we're thankful for *any* improvement here], and ~50%
476 # better on PIII:-) And additionally on the pros side this code
477 # eliminates redundant references to stack and thus relieves/
478 # minimizes the pressure on the memory bus.
479 #
480 # MMX register layout                           lsb
481 # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
482 # |          mm4          |          mm0          |
483 # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
484 # |     s3    |     s2    |     s1    |     s0    |    
485 # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
486 # |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
487 # +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
488 #
489 # Indexes translate as s[N/4]>>(8*(N%4)), e.g. 5 means s1>>8.
490 # In this terms encryption and decryption "compact" permutation
491 # matrices can be depicted as following:
492 #
493 # encryption              lsb   # decryption              lsb
494 # +----++----+----+----+----+   # +----++----+----+----+----+
495 # | t0 || 15 | 10 |  5 |  0 |   # | t0 ||  7 | 10 | 13 |  0 |
496 # +----++----+----+----+----+   # +----++----+----+----+----+
497 # | t1 ||  3 | 14 |  9 |  4 |   # | t1 || 11 | 14 |  1 |  4 |
498 # +----++----+----+----+----+   # +----++----+----+----+----+
499 # | t2 ||  7 |  2 | 13 |  8 |   # | t2 || 15 |  2 |  5 |  8 |
500 # +----++----+----+----+----+   # +----++----+----+----+----+
501 # | t3 || 11 |  6 |  1 | 12 |   # | t3 ||  3 |  6 |  9 | 12 |
502 # +----++----+----+----+----+   # +----++----+----+----+----+
503 #
504 ######################################################################
505 # Why not xmm registers? Short answer. It was actually tested and
506 # was not any faster, but *contrary*, most notably on Intel CPUs.
507 # Longer answer. Main advantage of using mm registers is that movd
508 # latency is lower, especially on Intel P4. While arithmetic
509 # instructions are twice as many, they can be scheduled every cycle
510 # and not every second one when they are operating on xmm register,
511 # so that "arithmetic throughput" remains virtually the same. And
512 # finally the code can be executed even on elder MMX-only CPUs:-)
513
514 sub mmx_enccompact()
515 {
516         &pshufw ("mm1","mm0",0x08);             #  5, 4, 1, 0
517         &pshufw ("mm5","mm4",0x0d);             # 15,14,11,10
518         &movd   ("eax","mm1");                  #  5, 4, 1, 0
519         &movd   ("ebx","mm5");                  # 15,14,11,10
520
521         &movz   ($acc,&LB("eax"));              #  0
522         &movz   ("ecx",&BP(-128,$tbl,$acc,1));  #  0
523         &pshufw ("mm2","mm0",0x0d);             #  7, 6, 3, 2
524         &movz   ("edx",&HB("eax"));             #  1
525         &movz   ("edx",&BP(-128,$tbl,"edx",1)); #  1
526         &shl    ("edx",8);                      #  1
527         &shr    ("eax",16);                     #  5, 4
528
529         &movz   ($acc,&LB("ebx"));              # 10
530         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 10
531         &shl    ($acc,16);                      # 10
532         &or     ("ecx",$acc);                   # 10
533         &pshufw ("mm6","mm4",0x08);             # 13,12, 9, 8
534         &movz   ($acc,&HB("ebx"));              # 11
535         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 11
536         &shl    ($acc,24);                      # 11
537         &or     ("edx",$acc);                   # 11
538         &shr    ("ebx",16);                     # 15,14
539
540         &movz   ($acc,&HB("eax"));              #  5
541         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  5
542         &shl    ($acc,8);                       #  5
543         &or     ("ecx",$acc);                   #  5
544         &movz   ($acc,&HB("ebx"));              # 15
545         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 15
546         &shl    ($acc,24);                      # 15
547         &or     ("ecx",$acc);                   # 15
548         &movd   ("mm0","ecx");                  # t[0] collected
549
550         &movz   ($acc,&LB("eax"));              #  4
551         &movz   ("ecx",&BP(-128,$tbl,$acc,1));  #  4
552         &movd   ("eax","mm2");                  #  7, 6, 3, 2
553         &movz   ($acc,&LB("ebx"));              # 14
554         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 14
555         &shl    ($acc,16);                      # 14
556         &or     ("ecx",$acc);                   # 14
557
558         &movd   ("ebx","mm6");                  # 13,12, 9, 8
559         &movz   ($acc,&HB("eax"));              #  3
560         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  3
561         &shl    ($acc,24);                      #  3
562         &or     ("ecx",$acc);                   #  3
563         &movz   ($acc,&HB("ebx"));              #  9
564         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  9
565         &shl    ($acc,8);                       #  9
566         &or     ("ecx",$acc);                   #  9
567         &movd   ("mm1","ecx");                  # t[1] collected
568
569         &movz   ($acc,&LB("ebx"));              #  8
570         &movz   ("ecx",&BP(-128,$tbl,$acc,1));  #  8
571         &shr    ("ebx",16);                     # 13,12
572         &movz   ($acc,&LB("eax"));              #  2
573         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  2
574         &shl    ($acc,16);                      #  2
575         &or     ("ecx",$acc);                   #  2
576         &shr    ("eax",16);                     #  7, 6
577
578         &punpckldq      ("mm0","mm1");          # t[0,1] collected
579
580         &movz   ($acc,&HB("eax"));              #  7
581         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  7
582         &shl    ($acc,24);                      #  7
583         &or     ("ecx",$acc);                   #  7
584         &and    ("eax",0xff);                   #  6
585         &movz   ("eax",&BP(-128,$tbl,"eax",1)); #  6
586         &shl    ("eax",16);                     #  6
587         &or     ("edx","eax");                  #  6
588         &movz   ($acc,&HB("ebx"));              # 13
589         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 13
590         &shl    ($acc,8);                       # 13
591         &or     ("ecx",$acc);                   # 13
592         &movd   ("mm4","ecx");                  # t[2] collected
593         &and    ("ebx",0xff);                   # 12
594         &movz   ("ebx",&BP(-128,$tbl,"ebx",1)); # 12
595         &or     ("edx","ebx");                  # 12
596         &movd   ("mm5","edx");                  # t[3] collected
597
598         &punpckldq      ("mm4","mm5");          # t[2,3] collected
599 }
600
601 &public_label("AES_Te");
602 &function_begin_B("_mmx_AES_encrypt_compact");
603         &pxor   ("mm0",&QWP(0,$key));   #  7, 6, 5, 4, 3, 2, 1, 0
604         &pxor   ("mm4",&QWP(8,$key));   # 15,14,13,12,11,10, 9, 8
605
606         # note that caller is expected to allocate stack frame for me!
607         &mov    ($acc,&DWP(240,$key));          # load key->rounds
608         &lea    ($acc,&DWP(-2,$acc,$acc));
609         &lea    ($acc,&DWP(0,$key,$acc,8));
610         &mov    (&DWP(24,"esp"),$acc);          # end of key schedule
611
612         &mov    ($s0,0x1b1b1b1b);               # magic constant
613         &mov    (&DWP(8,"esp"),$s0);
614         &mov    (&DWP(12,"esp"),$s0);
615
616         # prefetch Te4
617         &mov    ($s0,&DWP(0-128,$tbl));
618         &mov    ($s1,&DWP(32-128,$tbl));
619         &mov    ($s2,&DWP(64-128,$tbl));
620         &mov    ($s3,&DWP(96-128,$tbl));
621         &mov    ($s0,&DWP(128-128,$tbl));
622         &mov    ($s1,&DWP(160-128,$tbl));
623         &mov    ($s2,&DWP(192-128,$tbl));
624         &mov    ($s3,&DWP(224-128,$tbl));
625
626         &set_label("loop",16);
627                 &mmx_enccompact();
628                 &add    ($key,16);
629                 &cmp    ($key,&DWP(24,"esp"));
630                 &ja     (&label("out"));
631
632                 &movq   ("mm2",&QWP(8,"esp"));
633                 &pxor   ("mm3","mm3");          &pxor   ("mm7","mm7");
634                 &movq   ("mm1","mm0");          &movq   ("mm5","mm4");  # r0
635                 &pcmpgtb("mm3","mm0");          &pcmpgtb("mm7","mm4");
636                 &pand   ("mm3","mm2");          &pand   ("mm7","mm2");
637                 &pshufw ("mm2","mm0",0xb1);     &pshufw ("mm6","mm4",0xb1);# ROTATE(r0,16)
638                 &paddb  ("mm0","mm0");          &paddb  ("mm4","mm4");
639                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # = r2
640                 &pshufw ("mm3","mm2",0xb1);     &pshufw ("mm7","mm6",0xb1);# r0
641                 &pxor   ("mm1","mm0");          &pxor   ("mm5","mm4");  # r0^r2
642                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= ROTATE(r0,16)
643
644                 &movq   ("mm2","mm3");          &movq   ("mm6","mm7");
645                 &pslld  ("mm3",8);              &pslld  ("mm7",8);
646                 &psrld  ("mm2",24);             &psrld  ("mm6",24);
647                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= r0<<8
648                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= r0>>24
649
650                 &movq   ("mm3","mm1");          &movq   ("mm7","mm5");
651                 &movq   ("mm2",&QWP(0,$key));   &movq   ("mm6",&QWP(8,$key));
652                 &psrld  ("mm1",8);              &psrld  ("mm5",8);
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         &function_end_A();
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 &function_end("AES_encrypt");
1120
1121 #--------------------------------------------------------------------#
1122
1123 ######################################################################
1124 # "Compact" block function
1125 ######################################################################
1126
1127 sub deccompact()
1128 { my $Fn = mov;
1129   while ($#_>5) { pop(@_); $Fn=sub{}; }
1130   my ($i,$td,@s)=@_;
1131   my $tmp = $key;
1132   my $out = $i==3?$s[0]:$acc;
1133
1134         # $Fn is used in first compact round and its purpose is to
1135         # void restoration of some values from stack, so that after
1136         # 4xdeccompact with extra argument $key, $s0 and $s1 values
1137         # are left there...
1138         if($i==3)   {   &$Fn    ($key,&DWP(20,"esp"));          }
1139         else        {   &mov    ($out,$s[0]);                   }
1140                         &and    ($out,0xFF);
1141                         &movz   ($out,&BP(-128,$td,$out,1));
1142
1143         if ($i==3)  {   $tmp=$s[1];                             }
1144                         &movz   ($tmp,&HB($s[1]));
1145                         &movz   ($tmp,&BP(-128,$td,$tmp,1));
1146                         &shl    ($tmp,8);
1147                         &xor    ($out,$tmp);
1148
1149         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],$acc);          }
1150         else        {   mov     ($tmp,$s[2]);                   }
1151                         &shr    ($tmp,16);
1152                         &and    ($tmp,0xFF);
1153                         &movz   ($tmp,&BP(-128,$td,$tmp,1));
1154                         &shl    ($tmp,16);
1155                         &xor    ($out,$tmp);
1156
1157         if ($i==3)  {   $tmp=$s[3]; &$Fn ($s[2],&DWP(8,"esp")); }
1158         else        {   &mov    ($tmp,$s[3]);                   }
1159                         &shr    ($tmp,24);
1160                         &movz   ($tmp,&BP(-128,$td,$tmp,1));
1161                         &shl    ($tmp,24);
1162                         &xor    ($out,$tmp);
1163         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
1164         if ($i==3)  {   &$Fn    ($s[3],&DWP(4,"esp"));          }
1165 }
1166
1167 # must be called with 2,3,0,1 as argument sequence!!!
1168 sub dectransform()
1169 { my @s = ($s0,$s1,$s2,$s3);
1170   my $i = shift;
1171   my $tmp = $key;
1172   my $tp2 = @s[($i+2)%4]; $tp2 = @s[2] if ($i==1);
1173   my $tp4 = @s[($i+3)%4]; $tp4 = @s[3] if ($i==1);
1174   my $tp8 = $tbl;
1175
1176         &mov    ($acc,$s[$i]);
1177         &and    ($acc,0x80808080);
1178         &mov    ($tmp,$acc);
1179         &mov    ($tp2,$s[$i]);
1180         &shr    ($tmp,7);
1181         &and    ($tp2,0x7f7f7f7f);
1182         &sub    ($acc,$tmp);
1183         &add    ($tp2,$tp2);
1184         &and    ($acc,0x1b1b1b1b);
1185         &xor    ($acc,$tp2);
1186         &mov    ($tp2,$acc);
1187
1188         &and    ($acc,0x80808080);
1189         &mov    ($tmp,$acc);
1190         &mov    ($tp4,$tp2);
1191          &xor   ($tp2,$s[$i]);  # tp2^tp1
1192         &shr    ($tmp,7);
1193         &and    ($tp4,0x7f7f7f7f);
1194         &sub    ($acc,$tmp);
1195         &add    ($tp4,$tp4);
1196         &and    ($acc,0x1b1b1b1b);
1197         &xor    ($acc,$tp4);
1198         &mov    ($tp4,$acc);
1199
1200         &and    ($acc,0x80808080);
1201         &mov    ($tmp,$acc);
1202         &mov    ($tp8,$tp4);
1203          &xor   ($tp4,$s[$i]);  # tp4^tp1
1204         &shr    ($tmp,7);
1205         &and    ($tp8,0x7f7f7f7f);
1206         &sub    ($acc,$tmp);
1207         &add    ($tp8,$tp8);
1208         &and    ($acc,0x1b1b1b1b);
1209          &rotl  ($s[$i],8);     # = ROTATE(tp1,8)
1210         &xor    ($tp8,$acc);
1211
1212         &xor    ($s[$i],$tp2);
1213         &xor    ($tp2,$tp8);
1214         &xor    ($s[$i],$tp4);
1215         &rotl   ($tp2,24);
1216         &xor    ($tp4,$tp8);
1217         &xor    ($s[$i],$tp8);  # ^= tp8^(tp4^tp1)^(tp2^tp1)
1218         &rotl   ($tp4,16);
1219         &xor    ($s[$i],$tp2);  # ^= ROTATE(tp8^tp2^tp1,24)
1220         &rotl   ($tp8,8);
1221         &xor    ($s[$i],$tp4);  # ^= ROTATE(tp8^tp4^tp1,16)
1222         &xor    ($s[$i],$tp8);  # ^= ROTATE(tp8,8)
1223
1224         &mov    ($s[0],&DWP(4,"esp"))           if($i==2); #prefetch $s0
1225         &mov    ($s[1],&DWP(8,"esp"))           if($i==3); #prefetch $s1
1226         &mov    ($s[2],&DWP(12,"esp"))          if($i==1);
1227         &mov    ($s[3],&DWP(16,"esp"))          if($i==1);
1228         &mov    (&DWP(4+4*$i,"esp"),$s[$i])     if($i>=2);
1229 }
1230
1231 &public_label("AES_Td");
1232 &function_begin_B("_x86_AES_decrypt_compact");
1233         # note that caller is expected to allocate stack frame for me!
1234         &mov    (&DWP(20,"esp"),$key);          # save key
1235
1236         &xor    ($s0,&DWP(0,$key));             # xor with key
1237         &xor    ($s1,&DWP(4,$key));
1238         &xor    ($s2,&DWP(8,$key));
1239         &xor    ($s3,&DWP(12,$key));
1240
1241         &mov    ($acc,&DWP(240,$key));          # load key->rounds
1242
1243         &lea    ($acc,&DWP(-2,$acc,$acc));
1244         &lea    ($acc,&DWP(0,$key,$acc,8));
1245         &mov    (&DWP(24,"esp"),$acc);          # end of key schedule
1246
1247         # prefetch Td4
1248         &mov    ($key,&DWP(0-128,$tbl));
1249         &mov    ($acc,&DWP(32-128,$tbl));
1250         &mov    ($key,&DWP(64-128,$tbl));
1251         &mov    ($acc,&DWP(96-128,$tbl));
1252         &mov    ($key,&DWP(128-128,$tbl));
1253         &mov    ($acc,&DWP(160-128,$tbl));
1254         &mov    ($key,&DWP(192-128,$tbl));
1255         &mov    ($acc,&DWP(224-128,$tbl));
1256
1257         &set_label("loop",16);
1258
1259                 &deccompact(0,$tbl,$s0,$s3,$s2,$s1,1);
1260                 &deccompact(1,$tbl,$s1,$s0,$s3,$s2,1);
1261                 &deccompact(2,$tbl,$s2,$s1,$s0,$s3,1);
1262                 &deccompact(3,$tbl,$s3,$s2,$s1,$s0,1);
1263                 &dectransform(2);
1264                 &dectransform(3);
1265                 &dectransform(0);
1266                 &dectransform(1);
1267                 &mov    ($key,&DWP(20,"esp"));
1268                 &mov    ($tbl,&DWP(28,"esp"));
1269                 &add    ($key,16);              # advance rd_key
1270                 &xor    ($s0,&DWP(0,$key));
1271                 &xor    ($s1,&DWP(4,$key));
1272                 &xor    ($s2,&DWP(8,$key));
1273                 &xor    ($s3,&DWP(12,$key));
1274
1275         &cmp    ($key,&DWP(24,"esp"));
1276         &mov    (&DWP(20,"esp"),$key);
1277         &jb     (&label("loop"));
1278
1279         &deccompact(0,$tbl,$s0,$s3,$s2,$s1);
1280         &deccompact(1,$tbl,$s1,$s0,$s3,$s2);
1281         &deccompact(2,$tbl,$s2,$s1,$s0,$s3);
1282         &deccompact(3,$tbl,$s3,$s2,$s1,$s0);
1283
1284         &xor    ($s0,&DWP(16,$key));
1285         &xor    ($s1,&DWP(20,$key));
1286         &xor    ($s2,&DWP(24,$key));
1287         &xor    ($s3,&DWP(28,$key));
1288
1289         &ret    ();
1290 &function_end_B("_x86_AES_decrypt_compact");
1291
1292 ######################################################################
1293 # "Compact" MMX block function.
1294 ######################################################################
1295
1296 sub mmx_deccompact()
1297 {
1298         &pshufw ("mm1","mm0",0x0c);             #  7, 6, 1, 0
1299         &movd   ("eax","mm1");                  #  7, 6, 1, 0
1300
1301         &pshufw ("mm5","mm4",0x09);             # 13,12,11,10
1302         &movz   ($acc,&LB("eax"));              #  0
1303         &movz   ("ecx",&BP(-128,$tbl,$acc,1));  #  0
1304         &movd   ("ebx","mm5");                  # 13,12,11,10
1305         &movz   ("edx",&HB("eax"));             #  1
1306         &movz   ("edx",&BP(-128,$tbl,"edx",1)); #  1
1307         &shl    ("edx",8);                      #  1
1308
1309         &pshufw ("mm2","mm0",0x06);             #  3, 2, 5, 4
1310         &movz   ($acc,&LB("ebx"));              # 10
1311         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 10
1312         &shl    ($acc,16);                      # 10
1313         &or     ("ecx",$acc);                   # 10
1314         &shr    ("eax",16);                     #  7, 6
1315         &movz   ($acc,&HB("ebx"));              # 11
1316         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 11
1317         &shl    ($acc,24);                      # 11
1318         &or     ("edx",$acc);                   # 11
1319         &shr    ("ebx",16);                     # 13,12
1320
1321         &pshufw ("mm6","mm4",0x03);             # 9, 8,15,14
1322         &movz   ($acc,&HB("eax"));              #  7
1323         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  7
1324         &shl    ($acc,24);                      #  7
1325         &or     ("ecx",$acc);                   #  7
1326         &movz   ($acc,&HB("ebx"));              # 13
1327         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 13
1328         &shl    ($acc,8);                       # 13
1329         &or     ("ecx",$acc);                   # 13
1330         &movd   ("mm0","ecx");                  # t[0] collected
1331
1332         &movz   ($acc,&LB("eax"));              #  6
1333         &movd   ("eax","mm2");                  #  3, 2, 5, 4
1334         &movz   ("ecx",&BP(-128,$tbl,$acc,1));  #  6
1335         &shl    ("ecx",16);                     #  6
1336         &movz   ($acc,&LB("ebx"));              # 12
1337         &movd   ("ebx","mm6");                  #  9, 8,15,14
1338         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 12
1339         &or     ("ecx",$acc);                   # 12
1340
1341         &movz   ($acc,&LB("eax"));              #  4
1342         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  4
1343         &or     ("edx",$acc);                   #  4
1344         &movz   ($acc,&LB("ebx"));              # 14
1345         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 14
1346         &shl    ($acc,16);                      # 14
1347         &or     ("edx",$acc);                   # 14
1348         &movd   ("mm1","edx");                  # t[1] collected
1349
1350         &movz   ($acc,&HB("eax"));              #  5
1351         &movz   ("edx",&BP(-128,$tbl,$acc,1));  #  5
1352         &shl    ("edx",8);                      #  5
1353         &movz   ($acc,&HB("ebx"));              # 15
1354         &shr    ("eax",16);                     #  3, 2
1355         &movz   ($acc,&BP(-128,$tbl,$acc,1));   # 15
1356         &shl    ($acc,24);                      # 15
1357         &or     ("edx",$acc);                   # 15
1358         &shr    ("ebx",16);                     #  9, 8
1359
1360         &punpckldq      ("mm0","mm1");          # t[0,1] collected
1361
1362         &movz   ($acc,&HB("ebx"));              #  9
1363         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  9
1364         &shl    ($acc,8);                       #  9
1365         &or     ("ecx",$acc);                   #  9
1366         &and    ("ebx",0xff);                   #  8
1367         &movz   ("ebx",&BP(-128,$tbl,"ebx",1)); #  8
1368         &or     ("edx","ebx");                  #  8
1369         &movz   ($acc,&LB("eax"));              #  2
1370         &movz   ($acc,&BP(-128,$tbl,$acc,1));   #  2
1371         &shl    ($acc,16);                      #  2
1372         &or     ("edx",$acc);                   #  2
1373         &movd   ("mm4","edx");                  # t[2] collected
1374         &movz   ("eax",&HB("eax"));             #  3
1375         &movz   ("eax",&BP(-128,$tbl,"eax",1)); #  3
1376         &shl    ("eax",24);                     #  3
1377         &or     ("ecx","eax");                  #  3
1378         &movd   ("mm5","ecx");                  # t[3] collected
1379
1380         &punpckldq      ("mm4","mm5");          # t[2,3] collected
1381 }
1382
1383 &public_label("AES_Td");
1384 &function_begin_B("_mmx_AES_decrypt_compact");
1385         &pxor   ("mm0",&QWP(0,$key));   #  7, 6, 5, 4, 3, 2, 1, 0
1386         &pxor   ("mm4",&QWP(8,$key));   # 15,14,13,12,11,10, 9, 8
1387
1388         # note that caller is expected to allocate stack frame for me!
1389         &mov    ($acc,&DWP(240,$key));          # load key->rounds
1390         &lea    ($acc,&DWP(-2,$acc,$acc));
1391         &lea    ($acc,&DWP(0,$key,$acc,8));
1392         &mov    (&DWP(24,"esp"),$acc);          # end of key schedule
1393
1394         &mov    ($s0,0x1b1b1b1b);               # magic constant
1395         &mov    (&DWP(8,"esp"),$s0);
1396         &mov    (&DWP(12,"esp"),$s0);
1397
1398         # prefetch Td4
1399         &mov    ($s0,&DWP(0-128,$tbl));
1400         &mov    ($s1,&DWP(32-128,$tbl));
1401         &mov    ($s2,&DWP(64-128,$tbl));
1402         &mov    ($s3,&DWP(96-128,$tbl));
1403         &mov    ($s0,&DWP(128-128,$tbl));
1404         &mov    ($s1,&DWP(160-128,$tbl));
1405         &mov    ($s2,&DWP(192-128,$tbl));
1406         &mov    ($s3,&DWP(224-128,$tbl));
1407
1408         &set_label("loop",16);
1409                 &mmx_deccompact();
1410                 &add    ($key,16);
1411                 &cmp    ($key,&DWP(24,"esp"));
1412                 &ja     (&label("out"));
1413
1414                 # ROTATE(x^y,N) == ROTATE(x,N)^ROTATE(y,N)
1415                 &movq   ("mm3","mm0");          &movq   ("mm7","mm4");
1416                 &movq   ("mm2","mm0",1);        &movq   ("mm6","mm4",1);
1417                 &movq   ("mm1","mm0");          &movq   ("mm5","mm4");
1418                 &pshufw ("mm0","mm0",0xb1);     &pshufw ("mm4","mm4",0xb1);# = ROTATE(tp0,16)
1419                 &pslld  ("mm2",8);              &pslld  ("mm6",8);
1420                 &psrld  ("mm3",8);              &psrld  ("mm7",8);
1421                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= tp0<<8
1422                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= tp0>>8
1423                 &pslld  ("mm2",16);             &pslld  ("mm6",16);
1424                 &psrld  ("mm3",16);             &psrld  ("mm7",16);
1425                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= tp0<<24
1426                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= tp0>>24
1427
1428                 &movq   ("mm3",&QWP(8,"esp"));
1429                 &pxor   ("mm2","mm2");          &pxor   ("mm6","mm6");
1430                 &pcmpgtb("mm2","mm1");          &pcmpgtb("mm6","mm5");
1431                 &pand   ("mm2","mm3");          &pand   ("mm6","mm3");
1432                 &paddb  ("mm1","mm1");          &paddb  ("mm5","mm5");
1433                 &pxor   ("mm1","mm2");          &pxor   ("mm5","mm6");  # tp2
1434                 &movq   ("mm3","mm1");          &movq   ("mm7","mm5");
1435                 &movq   ("mm2","mm1");          &movq   ("mm6","mm5");
1436                 &pxor   ("mm0","mm1");          &pxor   ("mm4","mm5");  # ^= tp2
1437                 &pslld  ("mm3",24);             &pslld  ("mm7",24);
1438                 &psrld  ("mm2",8);              &psrld  ("mm6",8);
1439                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= tp2<<24
1440                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= tp2>>8
1441
1442                 &movq   ("mm2",&QWP(8,"esp"));
1443                 &pxor   ("mm3","mm3");          &pxor   ("mm7","mm7");
1444                 &pcmpgtb("mm3","mm1");          &pcmpgtb("mm7","mm5");
1445                 &pand   ("mm3","mm2");          &pand   ("mm7","mm2");
1446                 &paddb  ("mm1","mm1");          &paddb  ("mm5","mm5");
1447                 &pxor   ("mm1","mm3");          &pxor   ("mm5","mm7");  # tp4
1448                 &pshufw ("mm3","mm1",0xb1);     &pshufw ("mm7","mm5",0xb1);
1449                 &pxor   ("mm0","mm1");          &pxor   ("mm4","mm5");  # ^= tp4
1450                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= ROTATE(tp4,16)     
1451
1452                 &pxor   ("mm3","mm3");          &pxor   ("mm7","mm7");
1453                 &pcmpgtb("mm3","mm1");          &pcmpgtb("mm7","mm5");
1454                 &pand   ("mm3","mm2");          &pand   ("mm7","mm2");
1455                 &paddb  ("mm1","mm1");          &paddb  ("mm5","mm5");
1456                 &pxor   ("mm1","mm3");          &pxor   ("mm5","mm7");  # tp8
1457                 &pxor   ("mm0","mm1");          &pxor   ("mm4","mm5");  # ^= tp8
1458                 &movq   ("mm3","mm1");          &movq   ("mm7","mm5");
1459                 &pshufw ("mm2","mm1",0xb1);     &pshufw ("mm6","mm5",0xb1);
1460                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");  # ^= ROTATE(tp8,16)
1461                 &pslld  ("mm1",8);              &pslld  ("mm5",8);
1462                 &psrld  ("mm3",8);              &psrld  ("mm7",8);
1463                 &movq   ("mm2",&QWP(0,$key));   &movq   ("mm6",&DWP(8,$key));
1464                 &pxor   ("mm0","mm1");          &pxor   ("mm4","mm5");  # ^= tp8<<8
1465                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= tp8>>8
1466                 &pslld  ("mm1",16);             &pslld  ("mm5",16);
1467                 &psrld  ("mm3",16);             &psrld  ("mm7",16);
1468                 &pxor   ("mm0","mm1");          &pxor   ("mm4","mm5");  # ^= tp8<<24
1469                 &pxor   ("mm0","mm3");          &pxor   ("mm4","mm7");  # ^= tp8>>24
1470
1471                 &pxor   ("mm0","mm2");          &pxor   ("mm4","mm6");
1472         &jmp    (&label("loop"));
1473
1474         &set_label("out",16);
1475         &pxor   ("mm0",&QWP(0,$key));
1476         &pxor   ("mm4",&QWP(8,$key));
1477
1478         &ret    ();
1479 &function_end_B("_mmx_AES_decrypt_compact");
1480
1481 ######################################################################
1482 # Vanilla block function.
1483 ######################################################################
1484
1485 sub decstep()
1486 { my ($i,$td,@s) = @_;
1487   my $tmp = $key;
1488   my $out = $i==3?$s[0]:$acc;
1489
1490         # no instructions are reordered, as performance appears
1491         # optimal... or rather that all attempts to reorder didn't
1492         # result in better performance [which by the way is not a
1493         # bit lower than ecryption].
1494         if($i==3)   {   &mov    ($key,&DWP(20,"esp"));          }
1495         else        {   &mov    ($out,$s[0]);                   }
1496                         &and    ($out,0xFF);
1497                         &mov    ($out,&DWP(0,$td,$out,8));
1498
1499         if ($i==3)  {   $tmp=$s[1];                             }
1500                         &movz   ($tmp,&HB($s[1]));
1501                         &xor    ($out,&DWP(3,$td,$tmp,8));
1502
1503         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],$acc);          }
1504         else        {   &mov    ($tmp,$s[2]);                   }
1505                         &shr    ($tmp,16);
1506                         &and    ($tmp,0xFF);
1507                         &xor    ($out,&DWP(2,$td,$tmp,8));
1508
1509         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }
1510         else        {   &mov    ($tmp,$s[3]);                   }
1511                         &shr    ($tmp,24);
1512                         &xor    ($out,&DWP(1,$td,$tmp,8));
1513         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
1514         if ($i==3)  {   &mov    ($s[3],&DWP(4,"esp"));          }
1515                         &comment();
1516 }
1517
1518 sub declast()
1519 { my ($i,$td,@s)=@_;
1520   my $tmp = $key;
1521   my $out = $i==3?$s[0]:$acc;
1522
1523         if($i==0)   {   &lea    ($td,&DWP(2048+128,$td));
1524                         &mov    ($tmp,&DWP(0-128,$td));
1525                         &mov    ($acc,&DWP(32-128,$td));
1526                         &mov    ($tmp,&DWP(64-128,$td));
1527                         &mov    ($acc,&DWP(96-128,$td));
1528                         &mov    ($tmp,&DWP(128-128,$td));
1529                         &mov    ($acc,&DWP(160-128,$td));
1530                         &mov    ($tmp,&DWP(192-128,$td));
1531                         &mov    ($acc,&DWP(224-128,$td));
1532                         &lea    ($td,&DWP(-128,$td));           }
1533         if($i==3)   {   &mov    ($key,&DWP(20,"esp"));          }
1534         else        {   &mov    ($out,$s[0]);                   }
1535                         &and    ($out,0xFF);
1536                         &movz   ($out,&BP(0,$td,$out,1));
1537
1538         if ($i==3)  {   $tmp=$s[1];                             }
1539                         &movz   ($tmp,&HB($s[1]));
1540                         &movz   ($tmp,&BP(0,$td,$tmp,1));
1541                         &shl    ($tmp,8);
1542                         &xor    ($out,$tmp);
1543
1544         if ($i==3)  {   $tmp=$s[2]; &mov ($s[1],$acc);          }
1545         else        {   mov     ($tmp,$s[2]);                   }
1546                         &shr    ($tmp,16);
1547                         &and    ($tmp,0xFF);
1548                         &movz   ($tmp,&BP(0,$td,$tmp,1));
1549                         &shl    ($tmp,16);
1550                         &xor    ($out,$tmp);
1551
1552         if ($i==3)  {   $tmp=$s[3]; &mov ($s[2],&DWP(8,"esp")); }
1553         else        {   &mov    ($tmp,$s[3]);                   }
1554                         &shr    ($tmp,24);
1555                         &movz   ($tmp,&BP(0,$td,$tmp,1));
1556                         &shl    ($tmp,24);
1557                         &xor    ($out,$tmp);
1558         if ($i<2)   {   &mov    (&DWP(4+4*$i,"esp"),$out);      }
1559         if ($i==3)  {   &mov    ($s[3],&DWP(4,"esp"));
1560                         &lea    ($td,&DWP(-2048,$td));          }
1561 }
1562
1563 &public_label("AES_Td");
1564 &function_begin_B("_x86_AES_decrypt");
1565         # note that caller is expected to allocate stack frame for me!
1566         &mov    (&DWP(20,"esp"),$key);          # save key
1567
1568         &xor    ($s0,&DWP(0,$key));             # xor with key
1569         &xor    ($s1,&DWP(4,$key));
1570         &xor    ($s2,&DWP(8,$key));
1571         &xor    ($s3,&DWP(12,$key));
1572
1573         &mov    ($acc,&DWP(240,$key));          # load key->rounds
1574
1575         if ($small_footprint) {
1576             &lea        ($acc,&DWP(-2,$acc,$acc));
1577             &lea        ($acc,&DWP(0,$key,$acc,8));
1578             &mov        (&DWP(24,"esp"),$acc);  # end of key schedule
1579             &set_label("loop",16);
1580                 &decstep(0,$tbl,$s0,$s3,$s2,$s1);
1581                 &decstep(1,$tbl,$s1,$s0,$s3,$s2);
1582                 &decstep(2,$tbl,$s2,$s1,$s0,$s3);
1583                 &decstep(3,$tbl,$s3,$s2,$s1,$s0);
1584                 &add    ($key,16);              # advance rd_key
1585                 &xor    ($s0,&DWP(0,$key));
1586                 &xor    ($s1,&DWP(4,$key));
1587                 &xor    ($s2,&DWP(8,$key));
1588                 &xor    ($s3,&DWP(12,$key));
1589             &cmp        ($key,&DWP(24,"esp"));
1590             &mov        (&DWP(20,"esp"),$key);
1591             &jb         (&label("loop"));
1592         }
1593         else {
1594             &cmp        ($acc,10);
1595             &jle        (&label("10rounds"));
1596             &cmp        ($acc,12);
1597             &jle        (&label("12rounds"));
1598
1599         &set_label("14rounds",4);
1600             for ($i=1;$i<3;$i++) {
1601                 &decstep(0,$tbl,$s0,$s3,$s2,$s1);
1602                 &decstep(1,$tbl,$s1,$s0,$s3,$s2);
1603                 &decstep(2,$tbl,$s2,$s1,$s0,$s3);
1604                 &decstep(3,$tbl,$s3,$s2,$s1,$s0);
1605                 &xor    ($s0,&DWP(16*$i+0,$key));
1606                 &xor    ($s1,&DWP(16*$i+4,$key));
1607                 &xor    ($s2,&DWP(16*$i+8,$key));
1608                 &xor    ($s3,&DWP(16*$i+12,$key));
1609             }
1610             &add        ($key,32);
1611             &mov        (&DWP(20,"esp"),$key);  # advance rd_key
1612         &set_label("12rounds",4);
1613             for ($i=1;$i<3;$i++) {
1614                 &decstep(0,$tbl,$s0,$s3,$s2,$s1);
1615                 &decstep(1,$tbl,$s1,$s0,$s3,$s2);
1616                 &decstep(2,$tbl,$s2,$s1,$s0,$s3);
1617                 &decstep(3,$tbl,$s3,$s2,$s1,$s0);
1618                 &xor    ($s0,&DWP(16*$i+0,$key));
1619                 &xor    ($s1,&DWP(16*$i+4,$key));
1620                 &xor    ($s2,&DWP(16*$i+8,$key));
1621                 &xor    ($s3,&DWP(16*$i+12,$key));
1622             }
1623             &add        ($key,32);
1624             &mov        (&DWP(20,"esp"),$key);  # advance rd_key
1625         &set_label("10rounds",4);
1626             for ($i=1;$i<10;$i++) {
1627                 &decstep(0,$tbl,$s0,$s3,$s2,$s1);
1628                 &decstep(1,$tbl,$s1,$s0,$s3,$s2);
1629                 &decstep(2,$tbl,$s2,$s1,$s0,$s3);
1630                 &decstep(3,$tbl,$s3,$s2,$s1,$s0);
1631                 &xor    ($s0,&DWP(16*$i+0,$key));
1632                 &xor    ($s1,&DWP(16*$i+4,$key));
1633                 &xor    ($s2,&DWP(16*$i+8,$key));
1634                 &xor    ($s3,&DWP(16*$i+12,$key));
1635             }
1636         }
1637
1638         &declast(0,$tbl,$s0,$s3,$s2,$s1);
1639         &declast(1,$tbl,$s1,$s0,$s3,$s2);
1640         &declast(2,$tbl,$s2,$s1,$s0,$s3);
1641         &declast(3,$tbl,$s3,$s2,$s1,$s0);
1642
1643         &add    ($key,$small_footprint?16:160);
1644         &xor    ($s0,&DWP(0,$key));
1645         &xor    ($s1,&DWP(4,$key));
1646         &xor    ($s2,&DWP(8,$key));
1647         &xor    ($s3,&DWP(12,$key));
1648
1649         &ret    ();
1650
1651 &set_label("AES_Td",1024);      # Yes! I keep it in the code segment!
1652         &_data_word(0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a);
1653         &_data_word(0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b);
1654         &_data_word(0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5);
1655         &_data_word(0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5);
1656         &_data_word(0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d);
1657         &_data_word(0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b);
1658         &_data_word(0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295);
1659         &_data_word(0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e);
1660         &_data_word(0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927);
1661         &_data_word(0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d);
1662         &_data_word(0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362);
1663         &_data_word(0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9);
1664         &_data_word(0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52);
1665         &_data_word(0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566);
1666         &_data_word(0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3);
1667         &_data_word(0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed);
1668         &_data_word(0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e);
1669         &_data_word(0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4);
1670         &_data_word(0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4);
1671         &_data_word(0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd);
1672         &_data_word(0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d);
1673         &_data_word(0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060);
1674         &_data_word(0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967);
1675         &_data_word(0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879);
1676         &_data_word(0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000);
1677         &_data_word(0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c);
1678         &_data_word(0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36);
1679         &_data_word(0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624);
1680         &_data_word(0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b);
1681         &_data_word(0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c);
1682         &_data_word(0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12);
1683         &_data_word(0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14);
1684         &_data_word(0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3);
1685         &_data_word(0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b);
1686         &_data_word(0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8);
1687         &_data_word(0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684);
1688         &_data_word(0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7);
1689         &_data_word(0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177);
1690         &_data_word(0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947);
1691         &_data_word(0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322);
1692         &_data_word(0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498);
1693         &_data_word(0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f);
1694         &_data_word(0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54);
1695         &_data_word(0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382);
1696         &_data_word(0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf);
1697         &_data_word(0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb);
1698         &_data_word(0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83);
1699         &_data_word(0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef);
1700         &_data_word(0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029);
1701         &_data_word(0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235);
1702         &_data_word(0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733);
1703         &_data_word(0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117);
1704         &_data_word(0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4);
1705         &_data_word(0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546);
1706         &_data_word(0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb);
1707         &_data_word(0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d);
1708         &_data_word(0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb);
1709         &_data_word(0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a);
1710         &_data_word(0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773);
1711         &_data_word(0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478);
1712         &_data_word(0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2);
1713         &_data_word(0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff);
1714         &_data_word(0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664);
1715         &_data_word(0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0);
1716
1717 #Td4:   # four copies of Td4 to choose from to avoid L1 aliasing
1718         &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1719         &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1720         &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1721         &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1722         &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1723         &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1724         &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1725         &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1726         &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1727         &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1728         &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1729         &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1730         &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1731         &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1732         &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1733         &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1734         &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1735         &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1736         &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1737         &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1738         &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1739         &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1740         &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1741         &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1742         &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1743         &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1744         &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1745         &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1746         &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1747         &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1748         &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1749         &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1750
1751         &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1752         &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1753         &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1754         &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1755         &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1756         &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1757         &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1758         &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1759         &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1760         &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1761         &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1762         &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1763         &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1764         &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1765         &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1766         &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1767         &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1768         &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1769         &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1770         &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1771         &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1772         &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1773         &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1774         &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1775         &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1776         &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1777         &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1778         &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1779         &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1780         &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1781         &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1782         &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1783
1784         &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1785         &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1786         &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1787         &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1788         &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1789         &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1790         &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1791         &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1792         &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1793         &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1794         &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1795         &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1796         &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1797         &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1798         &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1799         &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1800         &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1801         &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1802         &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1803         &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1804         &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1805         &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1806         &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1807         &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1808         &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1809         &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1810         &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1811         &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1812         &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1813         &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1814         &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1815         &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1816
1817         &data_byte(0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38);
1818         &data_byte(0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb);
1819         &data_byte(0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87);
1820         &data_byte(0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb);
1821         &data_byte(0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d);
1822         &data_byte(0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e);
1823         &data_byte(0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2);
1824         &data_byte(0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25);
1825         &data_byte(0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16);
1826         &data_byte(0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92);
1827         &data_byte(0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda);
1828         &data_byte(0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84);
1829         &data_byte(0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a);
1830         &data_byte(0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06);
1831         &data_byte(0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02);
1832         &data_byte(0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b);
1833         &data_byte(0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea);
1834         &data_byte(0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73);
1835         &data_byte(0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85);
1836         &data_byte(0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e);
1837         &data_byte(0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89);
1838         &data_byte(0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b);
1839         &data_byte(0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20);
1840         &data_byte(0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4);
1841         &data_byte(0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31);
1842         &data_byte(0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f);
1843         &data_byte(0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d);
1844         &data_byte(0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef);
1845         &data_byte(0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0);
1846         &data_byte(0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61);
1847         &data_byte(0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26);
1848         &data_byte(0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d);
1849 &function_end_B("_x86_AES_decrypt");
1850
1851 # void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
1852 &public_label("AES_Td");
1853 &function_begin("AES_decrypt");
1854         &mov    ($acc,&wparam(0));              # load inp
1855         &mov    ($key,&wparam(2));              # load key
1856
1857         &mov    ($s0,"esp");
1858         &sub    ("esp",36);
1859         &and    ("esp",-64);                    # align to cache-line
1860
1861         # place stack frame just "above" the key schedule
1862         &lea    ($s1,&DWP(-64-63,$key));
1863         &sub    ($s1,"esp");
1864         &neg    ($s1);
1865         &and    ($s1,0x3C0);    # modulo 1024, but aligned to cache-line
1866         &sub    ("esp",$s1);
1867         &add    ("esp",4);      # 4 is reserved for caller's return address
1868         &mov    (&DWP(28,"esp"),$s0);           # save stack pointer
1869
1870         &call   (&label("pic_point"));          # make it PIC!
1871         &set_label("pic_point");
1872         &blindpop($tbl);
1873         &picmeup($s0,"OPENSSL_ia32cap_P",$tbl,&label("pic_point"));
1874         &lea    ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
1875         # pick Td4 copy which can't "overlap" with stack frame or key schedule
1876         &lea    ($s1,&DWP(768,"esp"));
1877         &and    ($s1,0x300);
1878         &lea    ($tbl,&DWP(2048+128,$tbl,$s1));
1879
1880         &bt     (&DWP(0,$s0),23);               # check for MMX bit
1881         &jc     (&label("mmx"));
1882
1883         &mov    (&DWP(24,"esp"),$tbl);
1884         &mov    ($s0,&DWP(0,$acc));             # load input data
1885         &mov    ($s1,&DWP(4,$acc));
1886         &mov    ($s2,&DWP(8,$acc));
1887         &mov    ($s3,&DWP(12,$acc));
1888         &call   ("_x86_AES_decrypt_compact");
1889         &mov    ("esp",&DWP(28,"esp"));         # restore stack pointer
1890         &mov    ($acc,&wparam(1));              # load out
1891         &mov    (&DWP(0,$acc),$s0);             # write output data
1892         &mov    (&DWP(4,$acc),$s1);
1893         &mov    (&DWP(8,$acc),$s2);
1894         &mov    (&DWP(12,$acc),$s3);
1895         &function_end_A();
1896
1897         &set_label("mmx",16);
1898         &movq   ("mm0",&QWP(0,$acc));
1899         &movq   ("mm4",&QWP(8,$acc));
1900         &call   ("_mmx_AES_decrypt_compact");
1901         &mov    ("esp",&DWP(28,"esp"));         # restore stack pointer
1902         &mov    ($acc,&wparam(1));              # load out
1903         &movq   (&QWP(0,$acc),"mm0");           # write output data
1904         &movq   (&QWP(8,$acc),"mm4");
1905         &emms   ();
1906 &function_end("AES_decrypt");
1907
1908 # void AES_cbc_encrypt (const void char *inp, unsigned char *out,
1909 #                       size_t length, const AES_KEY *key,
1910 #                       unsigned char *ivp,const int enc);
1911 {
1912 # stack frame layout
1913 # -4(%esp)      0(%esp)         return address
1914 # 0(%esp)       4(%esp)         s0 backup
1915 # 4(%esp)       8(%esp)         s1 backup
1916 # 8(%esp)       12(%esp)        s2 backup
1917 # 12(%esp)      16(%esp)        s3 backup
1918 # 16(%esp)      20(%esp)        key backup      
1919 # 20(%esp)      24(%esp)        end of key schedule
1920 # 24(%esp)      28(%esp)        ebp backup
1921 my $_esp=&DWP(28,"esp");        #saved %esp
1922 my $_inp=&DWP(32,"esp");        #copy of wparam(0)
1923 my $_out=&DWP(36,"esp");        #copy of wparam(1)
1924 my $_len=&DWP(40,"esp");        #copy of wparam(2)
1925 my $_key=&DWP(44,"esp");        #copy of wparam(3)
1926 my $_ivp=&DWP(48,"esp");        #copy of wparam(4)
1927 my $_tmp=&DWP(52,"esp");        #volatile variable
1928 my $ivec=&DWP(56,"esp");        #ivec[16]
1929 my $aes_key=&DWP(72,"esp");     #copy of aes_key
1930 my $mark=&DWP(72+240,"esp");    #copy of aes_key->rounds
1931
1932 &public_label("AES_Te");
1933 &public_label("AES_Td");
1934 &function_begin("AES_cbc_encrypt");
1935         &mov    ($s2 eq "ecx"? $s2 : "",&wparam(2));    # load len
1936         &cmp    ($s2,0);
1937         &je     (&label("enc_out"));
1938
1939         &call   (&label("pic_point"));          # make it PIC!
1940         &set_label("pic_point");
1941         &blindpop($tbl);
1942
1943         &pushf  ();
1944         &cld    ();
1945
1946         &cmp    (&wparam(5),0);
1947         &je     (&label("DECRYPT"));
1948
1949         &lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
1950
1951         # allocate aligned stack frame...
1952         &lea    ($key,&DWP(-76-244,"esp"));
1953         &and    ($key,-64);
1954
1955         # ... and make sure it doesn't alias with AES_Te modulo 4096
1956         &mov    ($s0,$tbl);
1957         &lea    ($s1,&DWP(2048,$tbl));
1958         &mov    ($s3,$key);
1959         &and    ($s0,0xfff);            # s = %ebp&0xfff
1960         &and    ($s1,0xfff);            # e = (%ebp+2048)&0xfff
1961         &and    ($s3,0xfff);            # p = %esp&0xfff
1962
1963         &cmp    ($s3,$s1);              # if (p>=e) %esp =- (p-e);
1964         &jb     (&label("te_break_out"));
1965         &sub    ($s3,$s1);
1966         &sub    ($key,$s3);
1967         &jmp    (&label("te_ok"));
1968         &set_label("te_break_out");     # else %esp -= (p-s)&0xfff + framesz;
1969         &sub    ($s3,$s0);
1970         &and    ($s3,0xfff);
1971         &add    ($s3,72+256);
1972         &sub    ($key,$s3);
1973         &align  (4);
1974         &set_label("te_ok");
1975
1976         &mov    ($s0,&wparam(0));       # load inp
1977         &mov    ($s1,&wparam(1));       # load out
1978         &mov    ($s3,&wparam(3));       # load key
1979         &mov    ($acc,&wparam(4));      # load ivp
1980
1981         &exch   ("esp",$key);
1982         &add    ("esp",4);              # reserve for return address!
1983         &mov    ($_esp,$key);           # save %esp
1984
1985         &mov    ($_inp,$s0);            # save copy of inp
1986         &mov    ($_out,$s1);            # save copy of out
1987         &mov    ($_len,$s2);            # save copy of len
1988         &mov    ($_key,$s3);            # save copy of key
1989         &mov    ($_ivp,$acc);           # save copy of ivp
1990
1991         &mov    ($mark,0);              # copy of aes_key->rounds = 0;
1992         if ($compromise) {
1993                 &cmp    ($s2,$compromise);
1994                 &jb     (&label("skip_ecopy"));
1995         }
1996         # do we copy key schedule to stack?
1997         &mov    ($s1 eq "ebx" ? $s1 : "",$s3);
1998         &mov    ($s2 eq "ecx" ? $s2 : "",244/4);
1999         &sub    ($s1,$tbl);
2000         &mov    ("esi",$s3);
2001         &and    ($s1,0xfff);
2002         &lea    ("edi",$aes_key);
2003         &cmp    ($s1,2048);
2004         &jb     (&label("do_ecopy"));
2005         &cmp    ($s1,4096-244);
2006         &jb     (&label("skip_ecopy"));
2007         &align  (4);
2008         &set_label("do_ecopy");
2009                 &mov    ($_key,"edi");
2010                 &data_word(0xA5F3F689); # rep movsd
2011         &set_label("skip_ecopy");
2012
2013         &mov    ($acc,$s0);
2014         &mov    ($key,16);
2015         &align  (4);
2016         &set_label("prefetch_te");
2017                 &mov    ($s0,&DWP(0,$tbl));
2018                 &mov    ($s1,&DWP(32,$tbl));
2019                 &mov    ($s2,&DWP(64,$tbl));
2020                 &mov    ($s3,&DWP(96,$tbl));
2021                 &lea    ($tbl,&DWP(128,$tbl));
2022                 &dec    ($key);
2023         &jnz    (&label("prefetch_te"));
2024         &sub    ($tbl,2048);
2025         &mov    (&DWP(24,"esp"),$tbl);
2026
2027         &mov    ($s2,$_len);
2028         &mov    ($key,$_ivp);
2029         &test   ($s2,0xFFFFFFF0);
2030         &jz     (&label("enc_tail"));           # short input...
2031
2032         &mov    ($s0,&DWP(0,$key));             # load iv
2033         &mov    ($s1,&DWP(4,$key));
2034
2035         &align  (4);
2036         &set_label("enc_loop");
2037                 &mov    ($s2,&DWP(8,$key));
2038                 &mov    ($s3,&DWP(12,$key));
2039
2040                 &xor    ($s0,&DWP(0,$acc));     # xor input data
2041                 &xor    ($s1,&DWP(4,$acc));
2042                 &xor    ($s2,&DWP(8,$acc));
2043                 &xor    ($s3,&DWP(12,$acc));
2044
2045                 &mov    ($key,$_key);           # load key
2046                 &call   ("_x86_AES_encrypt");
2047
2048                 &mov    ($acc,$_inp);           # load inp
2049                 &mov    ($key,$_out);           # load out
2050
2051                 &mov    (&DWP(0,$key),$s0);     # save output data
2052                 &mov    (&DWP(4,$key),$s1);
2053                 &mov    (&DWP(8,$key),$s2);
2054                 &mov    (&DWP(12,$key),$s3);
2055
2056                 &mov    ($s2,$_len);            # load len
2057
2058                 &lea    ($acc,&DWP(16,$acc));
2059                 &mov    ($_inp,$acc);           # save inp
2060
2061                 &lea    ($s3,&DWP(16,$key));
2062                 &mov    ($_out,$s3);            # save out
2063
2064                 &sub    ($s2,16);
2065                 &test   ($s2,0xFFFFFFF0);
2066                 &mov    ($_len,$s2);            # save len
2067         &jnz    (&label("enc_loop"));
2068         &test   ($s2,15);
2069         &jnz    (&label("enc_tail"));
2070         &mov    ($acc,$_ivp);           # load ivp
2071         &mov    ($s2,&DWP(8,$key));     # restore last dwords
2072         &mov    ($s3,&DWP(12,$key));
2073         &mov    (&DWP(0,$acc),$s0);     # save ivec
2074         &mov    (&DWP(4,$acc),$s1);
2075         &mov    (&DWP(8,$acc),$s2);
2076         &mov    (&DWP(12,$acc),$s3);
2077
2078         &cmp    ($mark,0);              # was the key schedule copied?
2079         &mov    ("edi",$_key);
2080         &mov    ("esp",$_esp);
2081         &je     (&label("skip_ezero"));
2082         # zero copy of key schedule
2083         &mov    ("ecx",240/4);
2084         &xor    ("eax","eax");
2085         &align  (4);
2086         &data_word(0xABF3F689); # rep stosd
2087         &set_label("skip_ezero")
2088         &popf   ();
2089     &set_label("enc_out");
2090         &function_end_A();
2091         &pushf  ();                     # kludge, never executed
2092
2093     &align      (4);
2094     &set_label("enc_tail");
2095         &push   ($key eq "edi" ? $key : "");    # push ivp
2096         &mov    ($key,$_out);                   # load out
2097         &mov    ($s1,16);
2098         &sub    ($s1,$s2);
2099         &cmp    ($key,$acc);                    # compare with inp
2100         &je     (&label("enc_in_place"));
2101         &align  (4);
2102         &data_word(0xA4F3F689); # rep movsb     # copy input
2103         &jmp    (&label("enc_skip_in_place"));
2104     &set_label("enc_in_place");
2105         &lea    ($key,&DWP(0,$key,$s2));
2106     &set_label("enc_skip_in_place");
2107         &mov    ($s2,$s1);
2108         &xor    ($s0,$s0);
2109         &align  (4);
2110         &data_word(0xAAF3F689); # rep stosb     # zero tail
2111         &pop    ($key);                         # pop ivp
2112
2113         &mov    ($acc,$_out);                   # output as input
2114         &mov    ($s0,&DWP(0,$key));
2115         &mov    ($s1,&DWP(4,$key));
2116         &mov    ($_len,16);                     # len=16
2117         &jmp    (&label("enc_loop"));           # one more spin...
2118
2119 #----------------------------- DECRYPT -----------------------------#
2120 &align  (4);
2121 &set_label("DECRYPT");
2122         &lea    ($tbl,&DWP(&label("AES_Td")."-".&label("pic_point"),$tbl));
2123
2124         # allocate aligned stack frame...
2125         &lea    ($key,&DWP(-64-244,"esp"));
2126         &and    ($key,-64);
2127
2128         # ... and make sure it doesn't alias with AES_Td modulo 4096
2129         &mov    ($s0,$tbl);
2130         &lea    ($s1,&DWP(2048+256,$tbl));
2131         &mov    ($s3,$key);
2132         &and    ($s0,0xfff);            # s = %ebp&0xfff
2133         &and    ($s1,0xfff);            # e = (%ebp+2048+256)&0xfff
2134         &and    ($s3,0xfff);            # p = %esp&0xfff
2135
2136         &cmp    ($s3,$s1);              # if (p>=e) %esp =- (p-e);
2137         &jb     (&label("td_break_out"));
2138         &sub    ($s3,$s1);
2139         &sub    ($key,$s3);
2140         &jmp    (&label("td_ok"));
2141         &set_label("td_break_out");     # else %esp -= (p-s)&0xfff + framesz;
2142         &sub    ($s3,$s0);
2143         &and    ($s3,0xfff);
2144         &add    ($s3,72+256);
2145         &sub    ($key,$s3);
2146         &align  (4);
2147         &set_label("td_ok");
2148
2149         &mov    ($s0,&wparam(0));       # load inp
2150         &mov    ($s1,&wparam(1));       # load out
2151         &mov    ($s3,&wparam(3));       # load key
2152         &mov    ($acc,&wparam(4));      # load ivp
2153
2154         &exch   ("esp",$key);
2155         &add    ("esp",4);              # reserve for return address!
2156         &mov    ($_esp,$key);           # save %esp
2157
2158         &mov    ($_inp,$s0);            # save copy of inp
2159         &mov    ($_out,$s1);            # save copy of out
2160         &mov    ($_len,$s2);            # save copy of len
2161         &mov    ($_key,$s3);            # save copy of key
2162         &mov    ($_ivp,$acc);           # save copy of ivp
2163
2164         &mov    ($mark,0);              # copy of aes_key->rounds = 0;
2165         if ($compromise) {
2166                 &cmp    ($s2,$compromise);
2167                 &jb     (&label("skip_dcopy"));
2168         }
2169         # do we copy key schedule to stack?
2170         &mov    ($s1 eq "ebx" ? $s1 : "",$s3);
2171         &mov    ($s2 eq "ecx" ? $s2 : "",244/4);
2172         &sub    ($s1,$tbl);
2173         &mov    ("esi",$s3);
2174         &and    ($s1,0xfff);
2175         &lea    ("edi",$aes_key);
2176         &cmp    ($s1,2048+256);
2177         &jb     (&label("do_dcopy"));
2178         &cmp    ($s1,4096-244);
2179         &jb     (&label("skip_dcopy"));
2180         &align  (4);
2181         &set_label("do_dcopy");
2182                 &mov    ($_key,"edi");
2183                 &data_word(0xA5F3F689); # rep movsd
2184         &set_label("skip_dcopy");
2185
2186         &mov    ($acc,$s0);
2187         &mov    ($key,18);
2188         &align  (4);
2189         &set_label("prefetch_td");
2190                 &mov    ($s0,&DWP(0,$tbl));
2191                 &mov    ($s1,&DWP(32,$tbl));
2192                 &mov    ($s2,&DWP(64,$tbl));
2193                 &mov    ($s3,&DWP(96,$tbl));
2194                 &lea    ($tbl,&DWP(128,$tbl));
2195                 &dec    ($key);
2196         &jnz    (&label("prefetch_td"));
2197         &sub    ($tbl,2048+256);
2198         &mov    (&DWP(24,"esp"),$tbl);
2199
2200         &cmp    ($acc,$_out);
2201         &je     (&label("dec_in_place"));       # in-place processing...
2202
2203         &mov    ($key,$_ivp);           # load ivp
2204         &mov    ($_tmp,$key);
2205
2206         &align  (4);
2207         &set_label("dec_loop");
2208                 &mov    ($s0,&DWP(0,$acc));     # read input
2209                 &mov    ($s1,&DWP(4,$acc));
2210                 &mov    ($s2,&DWP(8,$acc));
2211                 &mov    ($s3,&DWP(12,$acc));
2212
2213                 &mov    ($key,$_key);           # load key
2214                 &call   ("_x86_AES_decrypt");
2215
2216                 &mov    ($key,$_tmp);           # load ivp
2217                 &mov    ($acc,$_len);           # load len
2218                 &xor    ($s0,&DWP(0,$key));     # xor iv
2219                 &xor    ($s1,&DWP(4,$key));
2220                 &xor    ($s2,&DWP(8,$key));
2221                 &xor    ($s3,&DWP(12,$key));
2222
2223                 &sub    ($acc,16);
2224                 &jc     (&label("dec_partial"));
2225                 &mov    ($_len,$acc);           # save len
2226                 &mov    ($acc,$_inp);           # load inp
2227                 &mov    ($key,$_out);           # load out
2228
2229                 &mov    (&DWP(0,$key),$s0);     # write output
2230                 &mov    (&DWP(4,$key),$s1);
2231                 &mov    (&DWP(8,$key),$s2);
2232                 &mov    (&DWP(12,$key),$s3);
2233
2234                 &mov    ($_tmp,$acc);           # save ivp
2235                 &lea    ($acc,&DWP(16,$acc));
2236                 &mov    ($_inp,$acc);           # save inp
2237
2238                 &lea    ($key,&DWP(16,$key));
2239                 &mov    ($_out,$key);           # save out
2240
2241         &jnz    (&label("dec_loop"));
2242         &mov    ($key,$_tmp);           # load temp ivp
2243     &set_label("dec_end");
2244         &mov    ($acc,$_ivp);           # load user ivp
2245         &mov    ($s0,&DWP(0,$key));     # load iv
2246         &mov    ($s1,&DWP(4,$key));
2247         &mov    ($s2,&DWP(8,$key));
2248         &mov    ($s3,&DWP(12,$key));
2249         &mov    (&DWP(0,$acc),$s0);     # copy back to user
2250         &mov    (&DWP(4,$acc),$s1);
2251         &mov    (&DWP(8,$acc),$s2);
2252         &mov    (&DWP(12,$acc),$s3);
2253         &jmp    (&label("dec_out"));
2254
2255     &align      (4);
2256     &set_label("dec_partial");
2257         &lea    ($key,$ivec);
2258         &mov    (&DWP(0,$key),$s0);     # dump output to stack
2259         &mov    (&DWP(4,$key),$s1);
2260         &mov    (&DWP(8,$key),$s2);
2261         &mov    (&DWP(12,$key),$s3);
2262         &lea    ($s2 eq "ecx" ? $s2 : "",&DWP(16,$acc));
2263         &mov    ($acc eq "esi" ? $acc : "",$key);
2264         &mov    ($key eq "edi" ? $key : "",$_out);      # load out
2265         &data_word(0xA4F3F689); # rep movsb             # copy output
2266         &mov    ($key,$_inp);                           # use inp as temp ivp
2267         &jmp    (&label("dec_end"));
2268
2269     &align      (4);
2270     &set_label("dec_in_place");
2271         &set_label("dec_in_place_loop");
2272                 &lea    ($key,$ivec);
2273                 &mov    ($s0,&DWP(0,$acc));     # read input
2274                 &mov    ($s1,&DWP(4,$acc));
2275                 &mov    ($s2,&DWP(8,$acc));
2276                 &mov    ($s3,&DWP(12,$acc));
2277
2278                 &mov    (&DWP(0,$key),$s0);     # copy to temp
2279                 &mov    (&DWP(4,$key),$s1);
2280                 &mov    (&DWP(8,$key),$s2);
2281                 &mov    (&DWP(12,$key),$s3);
2282
2283                 &mov    ($key,$_key);           # load key
2284                 &call   ("_x86_AES_decrypt");
2285
2286                 &mov    ($key,$_ivp);           # load ivp
2287                 &mov    ($acc,$_out);           # load out
2288                 &xor    ($s0,&DWP(0,$key));     # xor iv
2289                 &xor    ($s1,&DWP(4,$key));
2290                 &xor    ($s2,&DWP(8,$key));
2291                 &xor    ($s3,&DWP(12,$key));
2292
2293                 &mov    (&DWP(0,$acc),$s0);     # write output
2294                 &mov    (&DWP(4,$acc),$s1);
2295                 &mov    (&DWP(8,$acc),$s2);
2296                 &mov    (&DWP(12,$acc),$s3);
2297
2298                 &lea    ($acc,&DWP(16,$acc));
2299                 &mov    ($_out,$acc);           # save out
2300
2301                 &lea    ($acc,$ivec);
2302                 &mov    ($s0,&DWP(0,$acc));     # read temp
2303                 &mov    ($s1,&DWP(4,$acc));
2304                 &mov    ($s2,&DWP(8,$acc));
2305                 &mov    ($s3,&DWP(12,$acc));
2306
2307                 &mov    (&DWP(0,$key),$s0);     # copy iv
2308                 &mov    (&DWP(4,$key),$s1);
2309                 &mov    (&DWP(8,$key),$s2);
2310                 &mov    (&DWP(12,$key),$s3);
2311
2312                 &mov    ($acc,$_inp);           # load inp
2313
2314                 &lea    ($acc,&DWP(16,$acc));
2315                 &mov    ($_inp,$acc);           # save inp
2316
2317                 &mov    ($s2,$_len);            # load len
2318                 &sub    ($s2,16);
2319                 &jc     (&label("dec_in_place_partial"));
2320                 &mov    ($_len,$s2);            # save len
2321         &jnz    (&label("dec_in_place_loop"));
2322         &jmp    (&label("dec_out"));
2323
2324     &align      (4);
2325     &set_label("dec_in_place_partial");
2326         # one can argue if this is actually required...
2327         &mov    ($key eq "edi" ? $key : "",$_out);
2328         &lea    ($acc eq "esi" ? $acc : "",$ivec);
2329         &lea    ($key,&DWP(0,$key,$s2));
2330         &lea    ($acc,&DWP(16,$acc,$s2));
2331         &neg    ($s2 eq "ecx" ? $s2 : "");
2332         &data_word(0xA4F3F689); # rep movsb     # restore tail
2333
2334     &align      (4);
2335     &set_label("dec_out");
2336     &cmp        ($mark,0);              # was the key schedule copied?
2337     &mov        ("edi",$_key);
2338     &mov        ("esp",$_esp);
2339     &je         (&label("skip_dzero"));
2340     # zero copy of key schedule
2341     &mov        ("ecx",240/4);
2342     &xor        ("eax","eax");
2343     &align      (4);
2344     &data_word(0xABF3F689);     # rep stosd
2345     &set_label("skip_dzero")
2346     &popf       ();
2347 &function_end("AES_cbc_encrypt");
2348 }
2349
2350 #------------------------------------------------------------------#
2351
2352 sub enckey()
2353 {
2354         &movz   ("esi",&LB("edx"));             # rk[i]>>0
2355         &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2356         &movz   ("esi",&HB("edx"));             # rk[i]>>8
2357         &shl    ("ebx",24);
2358         &xor    ("eax","ebx");
2359
2360         &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2361         &shr    ("edx",16);
2362         &movz   ("esi",&LB("edx"));             # rk[i]>>16
2363         &xor    ("eax","ebx");
2364
2365         &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2366         &movz   ("esi",&HB("edx"));             # rk[i]>>24
2367         &shl    ("ebx",8);
2368         &xor    ("eax","ebx");
2369
2370         &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2371         &shl    ("ebx",16);
2372         &xor    ("eax","ebx");
2373
2374         &xor    ("eax",&BP(1024-128,$tbl,"ecx",4));     # rcon
2375 }
2376
2377 # int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
2378 #                        AES_KEY *key)
2379 &public_label("AES_Te");
2380 &function_begin("AES_set_encrypt_key");
2381         &mov    ("esi",&wparam(0));             # user supplied key
2382         &mov    ("edi",&wparam(2));             # private key schedule
2383
2384         &test   ("esi",-1);
2385         &jz     (&label("badpointer"));
2386         &test   ("edi",-1);
2387         &jz     (&label("badpointer"));
2388
2389         &call   (&label("pic_point"));
2390         &set_label("pic_point");
2391         &blindpop($tbl);
2392         &lea    ($tbl,&DWP(&label("AES_Te")."-".&label("pic_point"),$tbl));
2393         &lea    ($tbl,&DWP(2048+128,$tbl));
2394
2395         # prefetch Te4
2396         &mov    ("eax",&DWP(0-128,$tbl));
2397         &mov    ("ebx",&DWP(32-128,$tbl));
2398         &mov    ("ecx",&DWP(64-128,$tbl));
2399         &mov    ("edx",&DWP(96-128,$tbl));
2400         &mov    ("eax",&DWP(128-128,$tbl));
2401         &mov    ("ebx",&DWP(160-128,$tbl));
2402         &mov    ("ecx",&DWP(192-128,$tbl));
2403         &mov    ("edx",&DWP(224-128,$tbl));
2404
2405         &mov    ("ecx",&wparam(1));             # number of bits in key
2406         &cmp    ("ecx",128);
2407         &je     (&label("10rounds"));
2408         &cmp    ("ecx",192);
2409         &je     (&label("12rounds"));
2410         &cmp    ("ecx",256);
2411         &je     (&label("14rounds"));
2412         &mov    ("eax",-2);                     # invalid number of bits
2413         &jmp    (&label("exit"));
2414
2415     &set_label("10rounds");
2416         &mov    ("eax",&DWP(0,"esi"));          # copy first 4 dwords
2417         &mov    ("ebx",&DWP(4,"esi"));
2418         &mov    ("ecx",&DWP(8,"esi"));
2419         &mov    ("edx",&DWP(12,"esi"));
2420         &mov    (&DWP(0,"edi"),"eax");
2421         &mov    (&DWP(4,"edi"),"ebx");
2422         &mov    (&DWP(8,"edi"),"ecx");
2423         &mov    (&DWP(12,"edi"),"edx");
2424
2425         &xor    ("ecx","ecx");
2426         &jmp    (&label("10shortcut"));
2427
2428         &align  (4);
2429         &set_label("10loop");
2430                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
2431                 &mov    ("edx",&DWP(12,"edi"));         # rk[3]
2432         &set_label("10shortcut");
2433                 &enckey ();
2434
2435                 &mov    (&DWP(16,"edi"),"eax");         # rk[4]
2436                 &xor    ("eax",&DWP(4,"edi"));
2437                 &mov    (&DWP(20,"edi"),"eax");         # rk[5]
2438                 &xor    ("eax",&DWP(8,"edi"));
2439                 &mov    (&DWP(24,"edi"),"eax");         # rk[6]
2440                 &xor    ("eax",&DWP(12,"edi"));
2441                 &mov    (&DWP(28,"edi"),"eax");         # rk[7]
2442                 &inc    ("ecx");
2443                 &add    ("edi",16);
2444                 &cmp    ("ecx",10);
2445         &jl     (&label("10loop"));
2446
2447         &mov    (&DWP(80,"edi"),10);            # setup number of rounds
2448         &xor    ("eax","eax");
2449         &jmp    (&label("exit"));
2450                 
2451     &set_label("12rounds");
2452         &mov    ("eax",&DWP(0,"esi"));          # copy first 6 dwords
2453         &mov    ("ebx",&DWP(4,"esi"));
2454         &mov    ("ecx",&DWP(8,"esi"));
2455         &mov    ("edx",&DWP(12,"esi"));
2456         &mov    (&DWP(0,"edi"),"eax");
2457         &mov    (&DWP(4,"edi"),"ebx");
2458         &mov    (&DWP(8,"edi"),"ecx");
2459         &mov    (&DWP(12,"edi"),"edx");
2460         &mov    ("ecx",&DWP(16,"esi"));
2461         &mov    ("edx",&DWP(20,"esi"));
2462         &mov    (&DWP(16,"edi"),"ecx");
2463         &mov    (&DWP(20,"edi"),"edx");
2464
2465         &xor    ("ecx","ecx");
2466         &jmp    (&label("12shortcut"));
2467
2468         &align  (4);
2469         &set_label("12loop");
2470                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
2471                 &mov    ("edx",&DWP(20,"edi"));         # rk[5]
2472         &set_label("12shortcut");
2473                 &enckey ();
2474
2475                 &mov    (&DWP(24,"edi"),"eax");         # rk[6]
2476                 &xor    ("eax",&DWP(4,"edi"));
2477                 &mov    (&DWP(28,"edi"),"eax");         # rk[7]
2478                 &xor    ("eax",&DWP(8,"edi"));
2479                 &mov    (&DWP(32,"edi"),"eax");         # rk[8]
2480                 &xor    ("eax",&DWP(12,"edi"));
2481                 &mov    (&DWP(36,"edi"),"eax");         # rk[9]
2482
2483                 &cmp    ("ecx",7);
2484                 &je     (&label("12break"));
2485                 &inc    ("ecx");
2486
2487                 &xor    ("eax",&DWP(16,"edi"));
2488                 &mov    (&DWP(40,"edi"),"eax");         # rk[10]
2489                 &xor    ("eax",&DWP(20,"edi"));
2490                 &mov    (&DWP(44,"edi"),"eax");         # rk[11]
2491
2492                 &add    ("edi",24);
2493         &jmp    (&label("12loop"));
2494
2495         &set_label("12break");
2496         &mov    (&DWP(72,"edi"),12);            # setup number of rounds
2497         &xor    ("eax","eax");
2498         &jmp    (&label("exit"));
2499
2500     &set_label("14rounds");
2501         &mov    ("eax",&DWP(0,"esi"));          # copy first 8 dwords
2502         &mov    ("ebx",&DWP(4,"esi"));
2503         &mov    ("ecx",&DWP(8,"esi"));
2504         &mov    ("edx",&DWP(12,"esi"));
2505         &mov    (&DWP(0,"edi"),"eax");
2506         &mov    (&DWP(4,"edi"),"ebx");
2507         &mov    (&DWP(8,"edi"),"ecx");
2508         &mov    (&DWP(12,"edi"),"edx");
2509         &mov    ("eax",&DWP(16,"esi"));
2510         &mov    ("ebx",&DWP(20,"esi"));
2511         &mov    ("ecx",&DWP(24,"esi"));
2512         &mov    ("edx",&DWP(28,"esi"));
2513         &mov    (&DWP(16,"edi"),"eax");
2514         &mov    (&DWP(20,"edi"),"ebx");
2515         &mov    (&DWP(24,"edi"),"ecx");
2516         &mov    (&DWP(28,"edi"),"edx");
2517
2518         &xor    ("ecx","ecx");
2519         &jmp    (&label("14shortcut"));
2520
2521         &align  (4);
2522         &set_label("14loop");
2523                 &mov    ("edx",&DWP(28,"edi"));         # rk[7]
2524         &set_label("14shortcut");
2525                 &mov    ("eax",&DWP(0,"edi"));          # rk[0]
2526
2527                 &enckey ();
2528
2529                 &mov    (&DWP(32,"edi"),"eax");         # rk[8]
2530                 &xor    ("eax",&DWP(4,"edi"));
2531                 &mov    (&DWP(36,"edi"),"eax");         # rk[9]
2532                 &xor    ("eax",&DWP(8,"edi"));
2533                 &mov    (&DWP(40,"edi"),"eax");         # rk[10]
2534                 &xor    ("eax",&DWP(12,"edi"));
2535                 &mov    (&DWP(44,"edi"),"eax");         # rk[11]
2536
2537                 &cmp    ("ecx",6);
2538                 &je     (&label("14break"));
2539                 &inc    ("ecx");
2540
2541                 &mov    ("edx","eax");
2542                 &mov    ("eax",&DWP(16,"edi"));         # rk[4]
2543                 &movz   ("esi",&LB("edx"));             # rk[11]>>0
2544                 &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2545                 &movz   ("esi",&HB("edx"));             # rk[11]>>8
2546                 &xor    ("eax","ebx");
2547
2548                 &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2549                 &shr    ("edx",16);
2550                 &shl    ("ebx",8);
2551                 &movz   ("esi",&LB("edx"));             # rk[11]>>16
2552                 &xor    ("eax","ebx");
2553
2554                 &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2555                 &movz   ("esi",&HB("edx"));             # rk[11]>>24
2556                 &shl    ("ebx",16);
2557                 &xor    ("eax","ebx");
2558
2559                 &movz   ("ebx",&BP(-128,$tbl,"esi",1));
2560                 &shl    ("ebx",24);
2561                 &xor    ("eax","ebx");
2562
2563                 &mov    (&DWP(48,"edi"),"eax");         # rk[12]
2564                 &xor    ("eax",&DWP(20,"edi"));
2565                 &mov    (&DWP(52,"edi"),"eax");         # rk[13]
2566                 &xor    ("eax",&DWP(24,"edi"));
2567                 &mov    (&DWP(56,"edi"),"eax");         # rk[14]
2568                 &xor    ("eax",&DWP(28,"edi"));
2569                 &mov    (&DWP(60,"edi"),"eax");         # rk[15]
2570
2571                 &add    ("edi",32);
2572         &jmp    (&label("14loop"));
2573
2574         &set_label("14break");
2575         &mov    (&DWP(48,"edi"),14);            # setup number of rounds
2576         &xor    ("eax","eax");
2577         &jmp    (&label("exit"));
2578
2579     &set_label("badpointer");
2580         &mov    ("eax",-1);
2581     &set_label("exit");
2582 &function_end("AES_set_encrypt_key");
2583
2584 sub deckey()
2585 { my ($i,$key,$tp1,$tp2,$tp4,$tp8) = @_;
2586   my $tmp = $tbl;
2587
2588         &mov    ($acc,$tp1);
2589         &and    ($acc,0x80808080);
2590         &mov    ($tmp,$acc);
2591         &mov    ($tp2,$tp1);
2592         &shr    ($tmp,7);
2593         &and    ($tp2,0x7f7f7f7f);
2594         &sub    ($acc,$tmp);
2595         &add    ($tp2,$tp2);
2596         &and    ($acc,0x1b1b1b1b);
2597         &xor    ($acc,$tp2);
2598         &mov    ($tp2,$acc);
2599
2600         &and    ($acc,0x80808080);
2601         &mov    ($tmp,$acc);
2602         &mov    ($tp4,$tp2);
2603          &xor   ($tp2,$tp1);    # tp2^tp1
2604         &shr    ($tmp,7);
2605         &and    ($tp4,0x7f7f7f7f);
2606         &sub    ($acc,$tmp);
2607         &add    ($tp4,$tp4);
2608         &and    ($acc,0x1b1b1b1b);
2609         &xor    ($acc,$tp4);
2610         &mov    ($tp4,$acc);
2611
2612         &and    ($acc,0x80808080);
2613         &mov    ($tmp,$acc);
2614         &mov    ($tp8,$tp4);
2615          &xor   ($tp4,$tp1);    # tp4^tp1
2616         &shr    ($tmp,7);
2617         &and    ($tp8,0x7f7f7f7f);
2618         &sub    ($acc,$tmp);
2619         &add    ($tp8,$tp8);
2620         &and    ($acc,0x1b1b1b1b);
2621          &rotl  ($tp1,8);       # = ROTATE(tp1,8)
2622         &xor    ($tp8,$acc);
2623
2624         &mov    ($tmp,&DWP(4*($i+1),$key));     # modulo-scheduled load
2625
2626         &xor    ($tp1,$tp2);
2627         &xor    ($tp2,$tp8);
2628         &xor    ($tp1,$tp4);
2629         &rotl   ($tp2,24);
2630         &xor    ($tp4,$tp8);
2631         &xor    ($tp1,$tp8);    # ^= tp8^(tp4^tp1)^(tp2^tp1)
2632         &rotl   ($tp4,16);
2633         &xor    ($tp1,$tp2);    # ^= ROTATE(tp8^tp2^tp1,24)
2634         &rotl   ($tp8,8);
2635         &xor    ($tp1,$tp4);    # ^= ROTATE(tp8^tp4^tp1,16)
2636         &mov    ($tp2,$tmp);
2637         &xor    ($tp1,$tp8);    # ^= ROTATE(tp8,8)
2638
2639         &mov    (&DWP(4*$i,$key),$tp1);
2640 }
2641
2642 # int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
2643 #                        AES_KEY *key)
2644 &public_label("AES_Td");
2645 &public_label("AES_Te");
2646 &function_begin_B("AES_set_decrypt_key");
2647         &mov    ("eax",&wparam(0));
2648         &mov    ("ecx",&wparam(1));
2649         &mov    ("edx",&wparam(2));
2650         &sub    ("esp",12);
2651         &mov    (&DWP(0,"esp"),"eax");
2652         &mov    (&DWP(4,"esp"),"ecx");
2653         &mov    (&DWP(8,"esp"),"edx");
2654         &call   ("AES_set_encrypt_key");
2655         &add    ("esp",12);
2656         &cmp    ("eax",0);
2657         &je     (&label("proceed"));
2658         &ret    ();
2659
2660     &set_label("proceed");
2661         &push   ("ebp");
2662         &push   ("ebx");
2663         &push   ("esi");
2664         &push   ("edi");
2665
2666         &mov    ("esi",&wparam(2));
2667         &mov    ("ecx",&DWP(240,"esi"));        # pull number of rounds
2668         &lea    ("ecx",&DWP(0,"","ecx",4));
2669         &lea    ("edi",&DWP(0,"esi","ecx",4));  # pointer to last chunk
2670
2671         &set_label("invert",4);                 # invert order of chunks
2672                 &mov    ("eax",&DWP(0,"esi"));
2673                 &mov    ("ebx",&DWP(4,"esi"));
2674                 &mov    ("ecx",&DWP(0,"edi"));
2675                 &mov    ("edx",&DWP(4,"edi"));
2676                 &mov    (&DWP(0,"edi"),"eax");
2677                 &mov    (&DWP(4,"edi"),"ebx");
2678                 &mov    (&DWP(0,"esi"),"ecx");
2679                 &mov    (&DWP(4,"esi"),"edx");
2680                 &mov    ("eax",&DWP(8,"esi"));
2681                 &mov    ("ebx",&DWP(12,"esi"));
2682                 &mov    ("ecx",&DWP(8,"edi"));
2683                 &mov    ("edx",&DWP(12,"edi"));
2684                 &mov    (&DWP(8,"edi"),"eax");
2685                 &mov    (&DWP(12,"edi"),"ebx");
2686                 &mov    (&DWP(8,"esi"),"ecx");
2687                 &mov    (&DWP(12,"esi"),"edx");
2688                 &add    ("esi",16);
2689                 &sub    ("edi",16);
2690                 &cmp    ("esi","edi");
2691         &jne    (&label("invert"));
2692
2693         &mov    ($key,&wparam(2));
2694         &mov    ($acc,&DWP(240,$key));          # pull number of rounds
2695         &lea    ($acc,&DWP(-2,$acc,$acc));
2696         &lea    ($acc,&DWP(0,$key,$acc,8));
2697         &mov    (&wparam(2),$acc);
2698
2699         &mov    ($s0,&DWP(16,$key));            # modulo-scheduled load
2700         &set_label("permute",4);                # permute the key schedule
2701                 &add    ($key,16);
2702                 &deckey (0,$key,$s0,$s1,$s2,$s3);
2703                 &deckey (1,$key,$s1,$s2,$s3,$s0);
2704                 &deckey (2,$key,$s2,$s3,$s0,$s1);
2705                 &deckey (3,$key,$s3,$s0,$s1,$s2);
2706                 &cmp    ($key,&wparam(2));
2707         &jb     (&label("permute"));
2708
2709         &xor    ("eax","eax");                  # return success
2710 &function_end("AES_set_decrypt_key");
2711
2712 &asm_finish();